1 /* 2 * Copyright (c) 1983, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)main.c 5.31 (Berkeley) 07/12/92"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/file.h> 20 #include <sys/protosw.h> 21 #include <sys/socket.h> 22 23 #include <netinet/in.h> 24 25 #include <ctype.h> 26 #include <errno.h> 27 #include <kvm.h> 28 #include <limits.h> 29 #include <netdb.h> 30 #include <nlist.h> 31 #include <paths.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.h> 36 #include "netstat.h" 37 38 struct nlist nl[] = { 39 #define N_MBSTAT 0 40 { "_mbstat" }, 41 #define N_IPSTAT 1 42 { "_ipstat" }, 43 #define N_TCB 2 44 { "_tcb" }, 45 #define N_TCPSTAT 3 46 { "_tcpstat" }, 47 #define N_UDB 4 48 { "_udb" }, 49 #define N_UDPSTAT 5 50 { "_udpstat" }, 51 #define N_IFNET 6 52 { "_ifnet" }, 53 #define N_IMP 7 54 { "_imp_softc" }, 55 #define N_ICMPSTAT 8 56 { "_icmpstat" }, 57 #define N_RTSTAT 9 58 { "_rtstat" }, 59 #define N_UNIXSW 10 60 { "_unixsw" }, 61 #define N_IDP 11 62 { "_nspcb"}, 63 #define N_IDPSTAT 12 64 { "_idpstat"}, 65 #define N_SPPSTAT 13 66 { "_spp_istat"}, 67 #define N_NSERR 14 68 { "_ns_errstat"}, 69 #define N_CLNPSTAT 15 70 { "_clnp_stat"}, 71 #define IN_NOTUSED 16 72 { "_tp_inpcb" }, 73 #define ISO_TP 17 74 { "_tp_refinfo" }, 75 #define N_TPSTAT 18 76 { "_tp_stat" }, 77 #define N_ESISSTAT 19 78 { "_esis_stat"}, 79 #define N_NIMP 20 80 { "_nimp"}, 81 #define N_RTREE 21 82 { "_rt_tables"}, 83 #define N_CLTP 22 84 { "_cltb"}, 85 #define N_CLTPSTAT 23 86 { "_cltpstat"}, 87 #define N_NFILE 24 88 { "_nfile" }, 89 #define N_FILE 25 90 { "_file" }, 91 #define N_IGMPSTAT 26 92 { "_igmpstat" }, 93 #define N_MRTPROTO 27 94 { "_ip_mrtproto" }, 95 #define N_MRTSTAT 28 96 { "_mrtstat" }, 97 #define N_MRTTABLE 29 98 { "_mrttable" }, 99 #define N_VIFTABLE 30 100 { "_viftable" }, 101 "", 102 }; 103 104 struct protox { 105 u_char pr_index; /* index into nlist of cb head */ 106 u_char pr_sindex; /* index into nlist of stat block */ 107 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 108 void (*pr_cblocks)(); /* control blocks printing routine */ 109 void (*pr_stats)(); /* statistics printing routine */ 110 char *pr_name; /* well-known name */ 111 } protox[] = { 112 { N_TCB, N_TCPSTAT, 1, protopr, 113 tcp_stats, "tcp" }, 114 { N_UDB, N_UDPSTAT, 1, protopr, 115 udp_stats, "udp" }, 116 { -1, N_IPSTAT, 1, 0, 117 ip_stats, "ip" }, 118 { -1, N_ICMPSTAT, 1, 0, 119 icmp_stats, "icmp" }, 120 { -1, -1, 0, 0, 121 0, 0 } 122 }; 123 124 struct protox nsprotox[] = { 125 { N_IDP, N_IDPSTAT, 1, nsprotopr, 126 idp_stats, "idp" }, 127 { N_IDP, N_SPPSTAT, 1, nsprotopr, 128 spp_stats, "spp" }, 129 { -1, N_NSERR, 1, 0, 130 nserr_stats, "ns_err" }, 131 { -1, -1, 0, 0, 132 0, 0 } 133 }; 134 135 struct protox isoprotox[] = { 136 { ISO_TP, N_TPSTAT, 1, iso_protopr, 137 tp_stats, "tp" }, 138 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 139 cltp_stats, "cltp" }, 140 { -1, N_CLNPSTAT, 1, 0, 141 clnp_stats, "clnp"}, 142 { -1, N_ESISSTAT, 1, 0, 143 esis_stats, "esis"}, 144 { -1, -1, 0, 0, 145 0, 0 } 146 }; 147 148 struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL }; 149 150 static void printproto __P((struct protox *, char *)); 151 static void usage __P(()); 152 static struct protox *name2protox __P((char *)); 153 static struct protox *knownname __P((char *)); 154 155 kvm_t *kvmd; 156 157 int 158 main(argc, argv) 159 int argc; 160 char **argv; 161 { 162 extern char *optarg; 163 extern int optind; 164 register struct protoent *p; 165 register struct protox *tp; /* for printing cblocks & stats */ 166 register char *cp; 167 int ch; 168 char *nlistf = NULL, *memf = NULL; 169 char buf[_POSIX2_LINE_MAX]; 170 171 if (cp = rindex(argv[0], '/')) 172 prog = cp + 1; 173 else 174 prog = argv[0]; 175 af = AF_UNSPEC; 176 177 while ((ch = getopt(argc, argv, "AaBdf:hI:iM:mN:np:rstuw:")) != EOF) 178 switch(ch) { 179 case 'A': 180 Aflag = 1; 181 break; 182 case 'a': 183 aflag = 1; 184 break; 185 case 'B': 186 Bflag = 1; 187 break; 188 case 'd': 189 dflag = 1; 190 break; 191 case 'f': 192 if (strcmp(optarg, "ns") == 0) 193 af = AF_NS; 194 else if (strcmp(optarg, "inet") == 0) 195 af = AF_INET; 196 else if (strcmp(optarg, "unix") == 0) 197 af = AF_UNIX; 198 else if (strcmp(optarg, "iso") == 0) 199 af = AF_ISO; 200 else { 201 (void)fprintf(stderr, 202 "%s: %s: unknown address family\n", 203 prog, optarg); 204 exit(1); 205 } 206 break; 207 case 'h': 208 hflag = 1; 209 break; 210 case 'I': { 211 char *cp; 212 213 iflag = 1; 214 for (cp = interface = optarg; isalpha(*cp); cp++) 215 continue; 216 unit = atoi(cp); 217 *cp = '\0'; 218 break; 219 } 220 case 'i': 221 iflag = 1; 222 break; 223 case 'M': 224 memf = optarg; 225 break; 226 case 'm': 227 mflag = 1; 228 break; 229 case 'N': 230 nlistf = optarg; 231 break; 232 case 'n': 233 nflag = 1; 234 break; 235 case 'p': 236 if ((tp = name2protox(optarg)) == NULL) { 237 (void)fprintf(stderr, 238 "%s: %s: unknown or uninstrumented protocol\n", 239 prog, optarg); 240 exit(1); 241 } 242 pflag = 1; 243 break; 244 case 'r': 245 rflag = 1; 246 break; 247 case 's': 248 ++sflag; 249 break; 250 case 't': 251 tflag = 1; 252 break; 253 case 'u': 254 af = AF_UNIX; 255 break; 256 case 'w': 257 interval = atoi(optarg); 258 break; 259 case '?': 260 default: 261 usage(); 262 } 263 argv += optind; 264 argc -= optind; 265 266 #define BACKWARD_COMPATIBILITY 267 #ifdef BACKWARD_COMPATIBILITY 268 if (*argv) { 269 if (isdigit(**argv)) { 270 interval = atoi(*argv); 271 if (interval <= 0) 272 usage(); 273 ++argv; 274 iflag = 1; 275 } 276 if (*argv) { 277 nlistf = *argv; 278 if (*++argv) 279 memf = *argv; 280 } 281 } 282 #endif 283 284 /* 285 * Discard setgid privileges if not the running kernel so that bad 286 * guys can't print interesting stuff from kernel memory. 287 */ 288 if (nlistf != NULL || memf != NULL) 289 setgid(getgid()); 290 291 if ((kvmd = kvm_open(nlistf, memf, NULL, O_RDONLY, prog)) == NULL) { 292 fprintf(stderr, "%s: kvm_open: %s\n", prog, buf); 293 exit(1); 294 } 295 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) { 296 if (nlistf) 297 fprintf(stderr, "%s: %s: no namelist\n", prog, nlistf); 298 else 299 fprintf(stderr, "%s: no namelist\n", prog); 300 exit(1); 301 } 302 if (mflag) { 303 mbpr(nl[N_MBSTAT].n_value); 304 exit(0); 305 } 306 if (pflag) { 307 if (tp->pr_stats) 308 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 309 tp->pr_name); 310 else 311 printf("%s: no stats routine\n", tp->pr_name); 312 exit(0); 313 } 314 if (hflag) { 315 hostpr(nl[N_IMP].n_value, nl[N_NIMP].n_value); 316 exit(0); 317 } 318 /* 319 * Keep file descriptors open to avoid overhead 320 * of open/close on each call to get* routines. 321 */ 322 sethostent(1); 323 setnetent(1); 324 if (iflag) { 325 intpr(interval, nl[N_IFNET].n_value); 326 exit(0); 327 } 328 if (rflag) { 329 if (sflag) 330 rt_stats(nl[N_RTSTAT].n_value); 331 else 332 routepr(nl[N_RTREE].n_value); 333 exit(0); 334 } 335 if (Bflag) { 336 if (sflag) 337 mrt_stats(nl[N_MRTPROTO].n_value, 338 nl[N_MRTSTAT].n_value); 339 else 340 mroutepr(nl[N_MRTPROTO].n_value, 341 nl[N_MRTTABLE].n_value, 342 nl[N_VIFTABLE].n_value); 343 exit(0); 344 } 345 if (af == AF_INET || af == AF_UNSPEC) { 346 setprotoent(1); 347 setservent(1); 348 /* ugh, this is O(MN) ... why do we do this? */ 349 while (p = getprotoent()) { 350 for (tp = protox; tp->pr_name; tp++) 351 if (strcmp(tp->pr_name, p->p_name) == 0) 352 break; 353 if (tp->pr_name == 0 || tp->pr_wanted == 0) 354 continue; 355 printproto(tp, p->p_name); 356 } 357 endprotoent(); 358 } 359 if (af == AF_NS || af == AF_UNSPEC) 360 for (tp = nsprotox; tp->pr_name; tp++) 361 printproto(tp, tp->pr_name); 362 if (af == AF_ISO || af == AF_UNSPEC) 363 for (tp = isoprotox; tp->pr_name; tp++) 364 printproto(tp, tp->pr_name); 365 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 366 unixpr(nl[N_UNIXSW].n_value); 367 if (af == AF_UNSPEC && sflag) 368 impstats(nl[N_IMP].n_value, nl[N_NIMP].n_value); 369 exit(0); 370 } 371 372 /* 373 * Print out protocol statistics or control blocks (per sflag). 374 * If the interface was not specifically requested, and the symbol 375 * is not in the namelist, ignore this one. 376 */ 377 static void 378 printproto(tp, name) 379 register struct protox *tp; 380 char *name; 381 { 382 void (*pr)(); 383 u_long off; 384 385 if (sflag) { 386 pr = tp->pr_stats; 387 off = nl[tp->pr_sindex].n_value; 388 } else { 389 pr = tp->pr_cblocks; 390 off = nl[tp->pr_index].n_value; 391 } 392 if (pr != NULL && (off || af != AF_UNSPEC)) 393 (*pr)(off, name); 394 } 395 396 /* 397 * Read kernel memory, return 0 on success. 398 */ 399 int 400 kread(addr, buf, size) 401 u_long addr; 402 char *buf; 403 int size; 404 { 405 406 if (kvm_read(kvmd, addr, buf, size) != size) { 407 /* XXX this duplicates kvm_read's error printout */ 408 (void)fprintf(stderr, "%s: kvm_read %s\n", prog, 409 kvm_geterr(kvmd)); 410 return (-1); 411 } 412 return (0); 413 } 414 415 char * 416 plural(n) 417 int n; 418 { 419 return (n != 1 ? "s" : ""); 420 } 421 422 char * 423 plurales(n) 424 int n; 425 { 426 return (n != 1 ? "es" : ""); 427 } 428 429 /* 430 * Find the protox for the given "well-known" name. 431 */ 432 static struct protox * 433 knownname(name) 434 char *name; 435 { 436 struct protox **tpp, *tp; 437 438 for (tpp = protoprotox; *tpp; tpp++) 439 for (tp = *tpp; tp->pr_name; tp++) 440 if (strcmp(tp->pr_name, name) == 0) 441 return (tp); 442 return (NULL); 443 } 444 445 /* 446 * Find the protox corresponding to name. 447 */ 448 static struct protox * 449 name2protox(name) 450 char *name; 451 { 452 struct protox *tp; 453 char **alias; /* alias from p->aliases */ 454 struct protoent *p; 455 456 /* 457 * Try to find the name in the list of "well-known" names. If that 458 * fails, check if name is an alias for an Internet protocol. 459 */ 460 if (tp = knownname(name)) 461 return (tp); 462 463 setprotoent(1); /* make protocol lookup cheaper */ 464 while (p = getprotoent()) { 465 /* assert: name not same as p->name */ 466 for (alias = p->p_aliases; *alias; alias++) 467 if (strcmp(name, *alias) == 0) { 468 endprotoent(); 469 return (knownname(p->p_name)); 470 } 471 } 472 endprotoent(); 473 return (NULL); 474 } 475 476 static void 477 usage() 478 { 479 (void)fprintf(stderr, 480 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog); 481 (void)fprintf(stderr, 482 " [-himnrs] [-f address_family] [-M core] [-N system]\n"); 483 (void)fprintf(stderr, 484 " [-n] [-I interface] [-M core] [-N system] [-w wait]\n"); 485 (void)fprintf(stderr, 486 " [-M core] [-N system] [-p protocol]\n"); 487 exit(1); 488 } 489