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