1 /* $NetBSD: main.c,v 1.13 1998/03/19 02:42:57 kml Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1988, 1993 5 * Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\ 39 Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94"; 45 #else 46 __RCSID("$NetBSD: main.c,v 1.13 1998/03/19 02:42:57 kml Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/file.h> 52 #include <sys/protosw.h> 53 #include <sys/socket.h> 54 55 #include <netinet/in.h> 56 57 #include <ctype.h> 58 #include <errno.h> 59 #include <kvm.h> 60 #include <limits.h> 61 #include <netdb.h> 62 #include <nlist.h> 63 #include <paths.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 #include "netstat.h" 69 70 struct nlist nl[] = { 71 #define N_MBSTAT 0 72 { "_mbstat" }, 73 #define N_IPSTAT 1 74 { "_ipstat" }, 75 #define N_TCBTABLE 2 76 { "_tcbtable" }, 77 #define N_TCPSTAT 3 78 { "_tcpstat" }, 79 #define N_UDBTABLE 4 80 { "_udbtable" }, 81 #define N_UDPSTAT 5 82 { "_udpstat" }, 83 #define N_IFNET 6 84 { "_ifnet" }, 85 #define N_IMP 7 86 { "_imp_softc" }, 87 #define N_ICMPSTAT 8 88 { "_icmpstat" }, 89 #define N_RTSTAT 9 90 { "_rtstat" }, 91 #define N_UNIXSW 10 92 { "_unixsw" }, 93 #define N_IDP 11 94 { "_nspcb"}, 95 #define N_IDPSTAT 12 96 { "_idpstat"}, 97 #define N_SPPSTAT 13 98 { "_spp_istat"}, 99 #define N_NSERR 14 100 { "_ns_errstat"}, 101 #define N_CLNPSTAT 15 102 { "_clnp_stat"}, 103 #define IN_NOTUSED 16 104 { "_tp_inpcb" }, 105 #define ISO_TP 17 106 { "_tp_refinfo" }, 107 #define N_TPSTAT 18 108 { "_tp_stat" }, 109 #define N_ESISSTAT 19 110 { "_esis_stat"}, 111 #define N_NIMP 20 112 { "_nimp"}, 113 #define N_RTREE 21 114 { "_rt_tables"}, 115 #define N_CLTP 22 116 { "_cltb"}, 117 #define N_CLTPSTAT 23 118 { "_cltpstat"}, 119 #define N_NFILE 24 120 { "_nfile" }, 121 #define N_FILE 25 122 { "_file" }, 123 #define N_IGMPSTAT 26 124 { "_igmpstat" }, 125 #define N_MRTPROTO 27 126 { "_ip_mrtproto" }, 127 #define N_MRTSTAT 28 128 { "_mrtstat" }, 129 #define N_MFCHASHTBL 29 130 { "_mfchashtbl" }, 131 #define N_MFCHASH 30 132 { "_mfchash" }, 133 #define N_VIFTABLE 31 134 { "_viftable" }, 135 #define N_MSIZE 32 136 { "_msize" }, 137 #define N_MCLBYTES 33 138 { "_mclbytes" }, 139 #define N_DDPSTAT 34 140 { "_ddpstat"}, 141 #define N_DDPCB 35 142 { "_ddpcb"}, 143 { "" }, 144 }; 145 146 struct protox { 147 u_char pr_index; /* index into nlist of cb head */ 148 u_char pr_sindex; /* index into nlist of stat block */ 149 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 150 void (*pr_cblocks) /* control blocks printing routine */ 151 __P((u_long, char *)); 152 void (*pr_stats) /* statistics printing routine */ 153 __P((u_long, char *)); 154 char *pr_name; /* well-known name */ 155 } protox[] = { 156 { N_TCBTABLE, N_TCPSTAT, 1, protopr, 157 tcp_stats, "tcp" }, 158 { N_UDBTABLE, N_UDPSTAT, 1, protopr, 159 udp_stats, "udp" }, 160 { -1, N_IPSTAT, 1, 0, 161 ip_stats, "ip" }, 162 { -1, N_ICMPSTAT, 1, 0, 163 icmp_stats, "icmp" }, 164 { -1, N_IGMPSTAT, 1, 0, 165 igmp_stats, "igmp" }, 166 { -1, -1, 0, 0, 167 0, 0 } 168 }; 169 170 struct protox atalkprotox[] = { 171 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 172 ddp_stats, "ddp" }, 173 { -1, -1, 0, 0, 174 0, 0 } 175 }; 176 177 struct protox nsprotox[] = { 178 { N_IDP, N_IDPSTAT, 1, nsprotopr, 179 idp_stats, "idp" }, 180 { N_IDP, N_SPPSTAT, 1, nsprotopr, 181 spp_stats, "spp" }, 182 { -1, N_NSERR, 1, 0, 183 nserr_stats, "ns_err" }, 184 { -1, -1, 0, 0, 185 0, 0 } 186 }; 187 188 struct protox isoprotox[] = { 189 { ISO_TP, N_TPSTAT, 1, iso_protopr, 190 tp_stats, "tp" }, 191 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 192 cltp_stats, "cltp" }, 193 { -1, N_CLNPSTAT, 1, 0, 194 clnp_stats, "clnp"}, 195 { -1, N_ESISSTAT, 1, 0, 196 esis_stats, "esis"}, 197 { -1, -1, 0, 0, 198 0, 0 } 199 }; 200 201 struct protox *protoprotox[] = { protox, atalkprotox, 202 nsprotox, isoprotox, NULL }; 203 204 int main __P((int, char *[])); 205 static void printproto __P((struct protox *, char *)); 206 static void usage __P((void)); 207 static struct protox *name2protox __P((char *)); 208 static struct protox *knownname __P((char *)); 209 210 211 kvm_t *kvmd; 212 213 int 214 main(argc, argv) 215 int argc; 216 char *argv[]; 217 { 218 extern char *optarg; 219 extern int optind; 220 struct protoent *p; 221 struct protox *tp; /* for printing cblocks & stats */ 222 int ch; 223 char *nlistf = NULL, *memf = NULL; 224 char buf[_POSIX2_LINE_MAX]; 225 226 tp = NULL; 227 af = AF_UNSPEC; 228 229 while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != -1) 230 switch(ch) { 231 case 'A': 232 Aflag = 1; 233 break; 234 case 'a': 235 aflag = 1; 236 break; 237 case 'b': 238 bflag = 1; 239 break; 240 case 'd': 241 dflag = 1; 242 break; 243 case 'f': 244 if (strcmp(optarg, "ns") == 0) 245 af = AF_NS; 246 else if (strcmp(optarg, "inet") == 0) 247 af = AF_INET; 248 else if (strcmp(optarg, "unix") == 0) 249 af = AF_UNIX; 250 else if (strcmp(optarg, "iso") == 0) 251 af = AF_ISO; 252 else if (strcmp(optarg, "atalk") == 0) 253 af = AF_APPLETALK; 254 else { 255 (void)fprintf(stderr, 256 "%s: %s: unknown address family\n", 257 __progname, optarg); 258 exit(1); 259 } 260 break; 261 case 'g': 262 gflag = 1; 263 break; 264 case 'I': 265 iflag = 1; 266 interface = optarg; 267 break; 268 case 'i': 269 iflag = 1; 270 break; 271 case 'M': 272 memf = optarg; 273 break; 274 case 'm': 275 mflag = 1; 276 break; 277 case 'N': 278 nlistf = optarg; 279 break; 280 case 'n': 281 nflag = 1; 282 break; 283 case 'p': 284 if ((tp = name2protox(optarg)) == NULL) { 285 (void)fprintf(stderr, 286 "%s: %s: unknown or uninstrumented protocol\n", 287 __progname, optarg); 288 exit(1); 289 } 290 pflag = 1; 291 break; 292 case 'r': 293 rflag = 1; 294 break; 295 case 's': 296 ++sflag; 297 break; 298 case 't': 299 tflag = 1; 300 break; 301 case 'u': 302 af = AF_UNIX; 303 break; 304 case 'w': 305 interval = atoi(optarg); 306 iflag = 1; 307 break; 308 case '?': 309 default: 310 usage(); 311 } 312 argv += optind; 313 argc -= optind; 314 315 #define BACKWARD_COMPATIBILITY 316 #ifdef BACKWARD_COMPATIBILITY 317 if (*argv) { 318 if (isdigit(**argv)) { 319 interval = atoi(*argv); 320 if (interval <= 0) 321 usage(); 322 ++argv; 323 iflag = 1; 324 } 325 if (*argv) { 326 nlistf = *argv; 327 if (*++argv) 328 memf = *argv; 329 } 330 } 331 #endif 332 333 /* 334 * Discard setgid privileges if not the running kernel so that bad 335 * guys can't print interesting stuff from kernel memory. 336 */ 337 if (nlistf != NULL || memf != NULL) 338 setgid(getgid()); 339 340 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, 341 buf)) == NULL) { 342 fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf); 343 exit(1); 344 } 345 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) { 346 if (nlistf) 347 fprintf(stderr, "%s: %s: no namelist\n", __progname, 348 nlistf); 349 else 350 fprintf(stderr, "%s: no namelist\n", __progname); 351 exit(1); 352 } 353 if (mflag) { 354 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value, 355 nl[N_MCLBYTES].n_value); 356 exit(0); 357 } 358 if (pflag) { 359 if (tp->pr_stats) 360 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 361 tp->pr_name); 362 else 363 printf("%s: no stats routine\n", tp->pr_name); 364 exit(0); 365 } 366 /* 367 * Keep file descriptors open to avoid overhead 368 * of open/close on each call to get* routines. 369 */ 370 sethostent(1); 371 setnetent(1); 372 if (iflag) { 373 intpr(interval, nl[N_IFNET].n_value); 374 exit(0); 375 } 376 if (rflag) { 377 if (sflag) 378 rt_stats(nl[N_RTSTAT].n_value); 379 else 380 routepr(nl[N_RTREE].n_value); 381 exit(0); 382 } 383 if (gflag) { 384 if (sflag) 385 mrt_stats(nl[N_MRTPROTO].n_value, 386 nl[N_MRTSTAT].n_value); 387 else 388 mroutepr(nl[N_MRTPROTO].n_value, 389 nl[N_MFCHASHTBL].n_value, 390 nl[N_MFCHASH].n_value, 391 nl[N_VIFTABLE].n_value); 392 exit(0); 393 } 394 if (af == AF_INET || af == AF_UNSPEC) { 395 setprotoent(1); 396 setservent(1); 397 /* ugh, this is O(MN) ... why do we do this? */ 398 while ((p = getprotoent()) != NULL) { 399 for (tp = protox; tp->pr_name; tp++) 400 if (strcmp(tp->pr_name, p->p_name) == 0) 401 break; 402 if (tp->pr_name == 0 || tp->pr_wanted == 0) 403 continue; 404 printproto(tp, p->p_name); 405 } 406 endprotoent(); 407 } 408 if (af == AF_APPLETALK || af == AF_UNSPEC) 409 for (tp = atalkprotox; tp->pr_name; tp++) 410 printproto(tp, tp->pr_name); 411 if (af == AF_NS || af == AF_UNSPEC) 412 for (tp = nsprotox; tp->pr_name; tp++) 413 printproto(tp, tp->pr_name); 414 if (af == AF_ISO || af == AF_UNSPEC) 415 for (tp = isoprotox; tp->pr_name; tp++) 416 printproto(tp, tp->pr_name); 417 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 418 unixpr(nl[N_UNIXSW].n_value); 419 exit(0); 420 } 421 422 /* 423 * Print out protocol statistics or control blocks (per sflag). 424 * If the interface was not specifically requested, and the symbol 425 * is not in the namelist, ignore this one. 426 */ 427 static void 428 printproto(tp, name) 429 struct protox *tp; 430 char *name; 431 { 432 void (*pr) __P((u_long, char *)); 433 u_long off; 434 435 if (sflag) { 436 pr = tp->pr_stats; 437 off = nl[tp->pr_sindex].n_value; 438 } else { 439 pr = tp->pr_cblocks; 440 off = nl[tp->pr_index].n_value; 441 } 442 if (pr != NULL && (off || af != AF_UNSPEC)) 443 (*pr)(off, name); 444 } 445 446 /* 447 * Read kernel memory, return 0 on success. 448 */ 449 int 450 kread(addr, buf, size) 451 u_long addr; 452 char *buf; 453 int size; 454 { 455 456 if (kvm_read(kvmd, addr, buf, size) != size) { 457 (void)fprintf(stderr, "%s: %s\n", __progname, 458 kvm_geterr(kvmd)); 459 return (-1); 460 } 461 return (0); 462 } 463 464 char * 465 plural(n) 466 int n; 467 { 468 return (n != 1 ? "s" : ""); 469 } 470 471 char * 472 plurales(n) 473 int n; 474 { 475 return (n != 1 ? "es" : ""); 476 } 477 478 /* 479 * Find the protox for the given "well-known" name. 480 */ 481 static struct protox * 482 knownname(name) 483 char *name; 484 { 485 struct protox **tpp, *tp; 486 487 for (tpp = protoprotox; *tpp; tpp++) 488 for (tp = *tpp; tp->pr_name; tp++) 489 if (strcmp(tp->pr_name, name) == 0) 490 return (tp); 491 return (NULL); 492 } 493 494 /* 495 * Find the protox corresponding to name. 496 */ 497 static struct protox * 498 name2protox(name) 499 char *name; 500 { 501 struct protox *tp; 502 char **alias; /* alias from p->aliases */ 503 struct protoent *p; 504 505 /* 506 * Try to find the name in the list of "well-known" names. If that 507 * fails, check if name is an alias for an Internet protocol. 508 */ 509 if ((tp = knownname(name)) != NULL) 510 return (tp); 511 512 setprotoent(1); /* make protocol lookup cheaper */ 513 while ((p = getprotoent()) != NULL) { 514 /* assert: name not same as p->name */ 515 for (alias = p->p_aliases; *alias; alias++) 516 if (strcmp(name, *alias) == 0) { 517 endprotoent(); 518 return (knownname(p->p_name)); 519 } 520 } 521 endprotoent(); 522 return (NULL); 523 } 524 525 static void 526 usage() 527 { 528 (void)fprintf(stderr, 529 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname); 530 (void)fprintf(stderr, 531 " %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname); 532 (void)fprintf(stderr, 533 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname); 534 (void)fprintf(stderr, 535 " %s [-M core] [-N system] [-p protocol]\n", __progname); 536 exit(1); 537 } 538