1 /* $NetBSD: main.c,v 1.25 1999/12/13 15:22:55 itojun 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.25 1999/12/13 15:22:55 itojun 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 <err.h> 59 #include <errno.h> 60 #include <kvm.h> 61 #include <limits.h> 62 #include <netdb.h> 63 #include <nlist.h> 64 #include <paths.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 #include "netstat.h" 70 71 struct nlist nl[] = { 72 #define N_MBSTAT 0 73 { "_mbstat" }, 74 #define N_IPSTAT 1 75 { "_ipstat" }, 76 #define N_TCBTABLE 2 77 { "_tcbtable" }, 78 #define N_TCPSTAT 3 79 { "_tcpstat" }, 80 #define N_UDBTABLE 4 81 { "_udbtable" }, 82 #define N_UDPSTAT 5 83 { "_udpstat" }, 84 #define N_IFNET 6 85 { "_ifnet" }, 86 #define N_IMP 7 87 { "_imp_softc" }, 88 #define N_ICMPSTAT 8 89 { "_icmpstat" }, 90 #define N_RTSTAT 9 91 { "_rtstat" }, 92 #define N_UNIXSW 10 93 { "_unixsw" }, 94 #define N_IDP 11 95 { "_nspcb"}, 96 #define N_IDPSTAT 12 97 { "_idpstat"}, 98 #define N_SPPSTAT 13 99 { "_spp_istat"}, 100 #define N_NSERR 14 101 { "_ns_errstat"}, 102 #define N_CLNPSTAT 15 103 { "_clnp_stat"}, 104 #define IN_NOTUSED 16 105 { "_tp_inpcb" }, 106 #define ISO_TP 17 107 { "_tp_refinfo" }, 108 #define N_TPSTAT 18 109 { "_tp_stat" }, 110 #define N_ESISSTAT 19 111 { "_esis_stat"}, 112 #define N_NIMP 20 113 { "_nimp"}, 114 #define N_RTREE 21 115 { "_rt_tables"}, 116 #define N_CLTP 22 117 { "_cltb"}, 118 #define N_CLTPSTAT 23 119 { "_cltpstat"}, 120 #define N_NFILE 24 121 { "_nfile" }, 122 #define N_FILE 25 123 { "_file" }, 124 #define N_IGMPSTAT 26 125 { "_igmpstat" }, 126 #define N_MRTPROTO 27 127 { "_ip_mrtproto" }, 128 #define N_MRTSTAT 28 129 { "_mrtstat" }, 130 #define N_MFCHASHTBL 29 131 { "_mfchashtbl" }, 132 #define N_MFCHASH 30 133 { "_mfchash" }, 134 #define N_VIFTABLE 31 135 { "_viftable" }, 136 #define N_MSIZE 32 137 { "_msize" }, 138 #define N_MCLBYTES 33 139 { "_mclbytes" }, 140 #define N_DDPSTAT 34 141 { "_ddpstat"}, 142 #define N_DDPCB 35 143 { "_ddpcb"}, 144 #define N_MBPOOL 36 145 { "_mbpool" }, 146 #define N_MCLPOOL 37 147 { "_mclpool" }, 148 #define N_DIVPCB 38 149 { "_divcb"}, 150 #define N_DIVSTAT 39 151 { "_divstat"}, 152 #define N_IP6STAT 40 153 { "_ip6stat" }, 154 #define N_TCB6 41 155 { "_tcb6" }, 156 #define N_TCP6STAT 42 157 { "_tcp6stat" }, 158 #define N_UDB6 43 159 { "_udb6" }, 160 #define N_UDP6STAT 44 161 { "_udp6stat" }, 162 #define N_ICMP6STAT 45 163 { "_icmp6stat" }, 164 #define N_IPSECSTAT 46 165 { "_ipsecstat" }, 166 #define N_IPSEC6STAT 47 167 { "_ipsec6stat" }, 168 #define N_PIM6STAT 48 169 { "_pim6stat" }, 170 #define N_MRT6PROTO 49 171 { "_ip6_mrtproto" }, 172 #define N_MRT6STAT 50 173 { "_mrt6stat" }, 174 #define N_MF6CTABLE 51 175 { "_mf6ctable" }, 176 #define N_MIF6TABLE 52 177 { "_mif6table" }, 178 { "" }, 179 }; 180 181 struct protox { 182 u_char pr_index; /* index into nlist of cb head */ 183 u_char pr_sindex; /* index into nlist of stat block */ 184 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 185 void (*pr_cblocks) /* control blocks printing routine */ 186 __P((u_long, char *)); 187 void (*pr_stats) /* statistics printing routine */ 188 __P((u_long, char *)); 189 void (*pr_istats) 190 __P((char *)); /* per/if statistics printing routine */ 191 void (*pr_dump) /* PCB state dump routine */ 192 __P((u_long)); 193 char *pr_name; /* well-known name */ 194 } protox[] = { 195 { N_TCBTABLE, N_TCPSTAT, 1, protopr, 196 tcp_stats, NULL, tcp_dump, "tcp" }, 197 { N_UDBTABLE, N_UDPSTAT, 1, protopr, 198 udp_stats, NULL, 0, "udp" }, 199 { -1, N_IPSTAT, 1, 0, 200 ip_stats, NULL, 0, "ip" }, 201 { -1, N_ICMPSTAT, 1, 0, 202 icmp_stats, NULL, 0, "icmp" }, 203 { -1, N_IGMPSTAT, 1, 0, 204 igmp_stats, NULL, 0, "igmp" }, 205 #ifdef IPSEC 206 { -1, N_IPSECSTAT, 1, 0, 207 ipsec_stats, NULL, 0, "ipsec" }, 208 #endif 209 { -1, -1, 0, 0, 210 0, NULL, 0, 0 } 211 }; 212 213 #ifdef INET6 214 struct protox ip6protox[] = { 215 { -1, N_IP6STAT, 1, 0, 216 ip6_stats, ip6_ifstats, 0, "ip6" }, 217 { -1, N_ICMP6STAT, 1, 0, 218 icmp6_stats, icmp6_ifstats, 0, "icmp6" }, 219 #ifdef TCP6 220 { N_TCB6, N_TCP6STAT, 1, ip6protopr, 221 tcp6_stats, NULL, tcp6_dump, "tcp6" }, 222 #else 223 { N_TCB6, N_TCP6STAT, 1, ip6protopr, 224 tcp_stats, NULL, tcp_dump, "tcp6" }, 225 #endif 226 { N_UDB6, N_UDP6STAT, 1, ip6protopr, 227 udp6_stats, NULL, 0, "udp6" }, 228 #ifdef IPSEC 229 { -1, N_IPSEC6STAT, 1, 0, 230 ipsec_stats, NULL, 0, "ipsec6" }, 231 #endif 232 { -1, N_PIM6STAT, 1, 0, 233 pim6_stats, NULL, 0, "pim6" }, 234 { -1, -1, 0, 0, 235 0, NULL, 0, 0 } 236 }; 237 #endif 238 239 #ifndef SMALL 240 struct protox atalkprotox[] = { 241 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 242 ddp_stats, NULL, 0, "ddp" }, 243 { -1, -1, 0, 0, 244 0, NULL, 0 } 245 }; 246 247 struct protox nsprotox[] = { 248 { N_IDP, N_IDPSTAT, 1, nsprotopr, 249 idp_stats, NULL, 0, "idp" }, 250 { N_IDP, N_SPPSTAT, 1, nsprotopr, 251 spp_stats, NULL, 0, "spp" }, 252 { -1, N_NSERR, 1, 0, 253 nserr_stats, NULL, 0, "ns_err" }, 254 { -1, -1, 0, 0, 255 0, NULL, 0 } 256 }; 257 258 struct protox isoprotox[] = { 259 { ISO_TP, N_TPSTAT, 1, iso_protopr, 260 tp_stats, NULL, 0, "tp" }, 261 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 262 cltp_stats, NULL, 0, "cltp" }, 263 { -1, N_CLNPSTAT, 1, 0, 264 clnp_stats, NULL, 0, "clnp"}, 265 { -1, N_ESISSTAT, 1, 0, 266 esis_stats, NULL, 0, "esis"}, 267 { -1, -1, 0, 0, 268 0, NULL, 0, 0 } 269 }; 270 #endif 271 272 struct protox *protoprotox[] = { protox, 273 #ifdef INET6 274 ip6protox, 275 #endif 276 #ifndef SMALL 277 atalkprotox, nsprotox, isoprotox, 278 #endif 279 NULL }; 280 281 int main __P((int, char *[])); 282 static void printproto __P((struct protox *, char *)); 283 static void usage __P((void)); 284 static struct protox *name2protox __P((char *)); 285 static struct protox *knownname __P((char *)); 286 287 kvm_t *kvmd; 288 289 int 290 main(argc, argv) 291 int argc; 292 char *argv[]; 293 { 294 extern char *optarg; 295 extern int optind; 296 struct protoent *p; 297 struct protox *tp; /* for printing cblocks & stats */ 298 int ch; 299 char *nlistf = NULL, *memf = NULL; 300 char buf[_POSIX2_LINE_MAX], *cp; 301 u_long pcbaddr; 302 gid_t egid = getegid(); 303 304 (void)setegid(getgid()); 305 tp = NULL; 306 af = AF_UNSPEC; 307 pcbaddr = 0; 308 309 while ((ch = getopt(argc, argv, "Aabdf:ghI:LliM:mN:nP:p:rstuvw:")) != -1) 310 switch(ch) { 311 case 'A': 312 Aflag = 1; 313 break; 314 case 'a': 315 aflag = 1; 316 break; 317 case 'b': 318 bflag = 1; 319 break; 320 case 'd': 321 dflag = 1; 322 break; 323 case 'f': 324 if (strcmp(optarg, "ns") == 0) 325 af = AF_NS; 326 else if (strcmp(optarg, "inet") == 0) 327 af = AF_INET; 328 else if (strcmp(optarg, "inet6") == 0) 329 af = AF_INET6; 330 else if (strcmp(optarg, "unix") == 0 331 || strcmp(optarg, "local") == 0) 332 af = AF_LOCAL; 333 else if (strcmp(optarg, "iso") == 0) 334 af = AF_ISO; 335 else if (strcmp(optarg, "atalk") == 0) 336 af = AF_APPLETALK; 337 else 338 errx(1, "%s: unknown address family", 339 optarg); 340 break; 341 #ifndef SMALL 342 case 'g': 343 gflag = 1; 344 break; 345 #endif 346 case 'I': 347 iflag = 1; 348 interface = optarg; 349 break; 350 case 'i': 351 iflag = 1; 352 break; 353 case 'L': 354 Lflag = 1; 355 break; 356 case 'l': 357 lflag = 1; 358 break; 359 case 'M': 360 memf = optarg; 361 break; 362 case 'm': 363 mflag = 1; 364 break; 365 case 'N': 366 nlistf = optarg; 367 break; 368 case 'n': 369 nflag = 1; 370 break; 371 case 'P': 372 pcbaddr = strtoul(optarg, &cp, 16); 373 if (*cp != '\0' || errno == ERANGE) 374 errx(1, "invalid PCB address %s", 375 optarg); 376 Pflag = 1; 377 break; 378 case 'p': 379 if ((tp = name2protox(optarg)) == NULL) 380 errx(1, "%s: unknown or uninstrumented protocol", 381 optarg); 382 pflag = 1; 383 break; 384 case 'r': 385 rflag = 1; 386 break; 387 case 's': 388 ++sflag; 389 break; 390 case 't': 391 tflag = 1; 392 break; 393 case 'u': 394 af = AF_LOCAL; 395 break; 396 case 'v': 397 vflag = 1; 398 break; 399 case 'w': 400 interval = atoi(optarg); 401 iflag = 1; 402 break; 403 case '?': 404 default: 405 usage(); 406 } 407 argv += optind; 408 argc -= optind; 409 410 #define BACKWARD_COMPATIBILITY 411 #ifdef BACKWARD_COMPATIBILITY 412 if (*argv) { 413 if (isdigit(**argv)) { 414 interval = atoi(*argv); 415 if (interval <= 0) 416 usage(); 417 ++argv; 418 iflag = 1; 419 } 420 if (*argv) { 421 nlistf = *argv; 422 if (*++argv) 423 memf = *argv; 424 } 425 } 426 #endif 427 428 /* 429 * Discard setgid privileges. If not the running kernel, we toss 430 * them away totally so that bad guys can't print interesting stuff 431 * from kernel memory, otherwise switch back to kmem for the 432 * duration of the kvm_openfiles() call. 433 */ 434 if (nlistf != NULL || memf != NULL || Pflag) 435 (void)setgid(getgid()); 436 else 437 (void)setegid(egid); 438 439 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, 440 buf)) == NULL) 441 errx(1, "%s", buf); 442 443 /* do this now anyway */ 444 if (nlistf == NULL && memf == NULL) 445 (void)setgid(getgid()); 446 447 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) { 448 if (nlistf) 449 errx(1, "%s: no namelist", nlistf); 450 else 451 errx(1, "no namelist"); 452 } 453 if (mflag) { 454 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value, 455 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value, 456 nl[N_MCLPOOL].n_value); 457 exit(0); 458 } 459 if (Pflag) { 460 if (tp == NULL) { 461 /* Default to TCP. */ 462 tp = name2protox("tcp"); 463 } 464 if (tp->pr_dump) 465 (*tp->pr_dump)(pcbaddr); 466 else 467 printf("%s: no PCB dump routine\n", tp->pr_name); 468 exit(0); 469 } 470 if (pflag) { 471 if (iflag && tp->pr_istats) 472 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats); 473 else if (tp->pr_stats) 474 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 475 tp->pr_name); 476 else 477 printf("%s: no stats routine\n", tp->pr_name); 478 exit(0); 479 } 480 /* 481 * Keep file descriptors open to avoid overhead 482 * of open/close on each call to get* routines. 483 */ 484 sethostent(1); 485 setnetent(1); 486 if (iflag) { 487 if (af != AF_UNSPEC) 488 goto protostat; 489 490 intpr(interval, nl[N_IFNET].n_value, NULL); 491 exit(0); 492 } 493 if (rflag) { 494 if (sflag) 495 rt_stats(nl[N_RTSTAT].n_value); 496 else 497 routepr(nl[N_RTREE].n_value); 498 exit(0); 499 } 500 #ifndef SMALL 501 if (gflag) { 502 if (sflag) { 503 if (af == AF_INET || af == AF_UNSPEC) 504 mrt_stats(nl[N_MRTPROTO].n_value, 505 nl[N_MRTSTAT].n_value); 506 #ifdef INET6 507 if (af == AF_INET6 || af == AF_UNSPEC) 508 mrt6_stats(nl[N_MRT6PROTO].n_value, 509 nl[N_MRT6STAT].n_value); 510 #endif 511 } 512 else { 513 if (af == AF_INET || af == AF_UNSPEC) 514 mroutepr(nl[N_MRTPROTO].n_value, 515 nl[N_MFCHASHTBL].n_value, 516 nl[N_MFCHASH].n_value, 517 nl[N_VIFTABLE].n_value); 518 #ifdef INET6 519 if (af == AF_INET6 || af == AF_UNSPEC) 520 mroute6pr(nl[N_MRT6PROTO].n_value, 521 nl[N_MF6CTABLE].n_value, 522 nl[N_MIF6TABLE].n_value); 523 #endif 524 } 525 exit(0); 526 } 527 #endif 528 protostat: 529 if (af == AF_INET || af == AF_UNSPEC) { 530 setprotoent(1); 531 setservent(1); 532 /* ugh, this is O(MN) ... why do we do this? */ 533 while ((p = getprotoent()) != NULL) { 534 for (tp = protox; tp->pr_name; tp++) 535 if (strcmp(tp->pr_name, p->p_name) == 0) 536 break; 537 if (tp->pr_name == 0 || tp->pr_wanted == 0) 538 continue; 539 printproto(tp, p->p_name); 540 tp->pr_wanted = 0; 541 } 542 endprotoent(); 543 for (tp = protox; tp->pr_name; tp++) 544 if (tp->pr_wanted) 545 printproto(tp, tp->pr_name); 546 } 547 #ifdef INET6 548 if (af == AF_INET6 || af == AF_UNSPEC) 549 for (tp = ip6protox; tp->pr_name; tp++) 550 printproto(tp, tp->pr_name); 551 #endif 552 #ifndef SMALL 553 if (af == AF_APPLETALK || af == AF_UNSPEC) 554 for (tp = atalkprotox; tp->pr_name; tp++) 555 printproto(tp, tp->pr_name); 556 if (af == AF_NS || af == AF_UNSPEC) 557 for (tp = nsprotox; tp->pr_name; tp++) 558 printproto(tp, tp->pr_name); 559 if (af == AF_ISO || af == AF_UNSPEC) 560 for (tp = isoprotox; tp->pr_name; tp++) 561 printproto(tp, tp->pr_name); 562 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag) 563 unixpr(nl[N_UNIXSW].n_value); 564 #endif 565 exit(0); 566 } 567 568 /* 569 * Print out protocol statistics or control blocks (per sflag). 570 * If the interface was not specifically requested, and the symbol 571 * is not in the namelist, ignore this one. 572 */ 573 static void 574 printproto(tp, name) 575 struct protox *tp; 576 char *name; 577 { 578 void (*pr) __P((u_long, char *)); 579 u_long off; 580 581 if (sflag) { 582 if (iflag) { 583 if (tp->pr_istats) 584 intpr(interval, nl[N_IFNET].n_value, 585 tp->pr_istats); 586 return; 587 } 588 else { 589 pr = tp->pr_stats; 590 off = nl[tp->pr_sindex].n_value; 591 } 592 } else { 593 pr = tp->pr_cblocks; 594 off = nl[tp->pr_index].n_value; 595 } 596 if (pr != NULL && (off || af != AF_UNSPEC)) 597 (*pr)(off, name); 598 } 599 600 /* 601 * Read kernel memory, return 0 on success. 602 */ 603 int 604 kread(addr, buf, size) 605 u_long addr; 606 char *buf; 607 int size; 608 { 609 610 if (kvm_read(kvmd, addr, buf, size) != size) { 611 warnx("%s\n", kvm_geterr(kvmd)); 612 return (-1); 613 } 614 return (0); 615 } 616 617 char * 618 plural(n) 619 int n; 620 { 621 622 return (n != 1 ? "s" : ""); 623 } 624 625 char * 626 plurales(n) 627 int n; 628 { 629 630 return (n != 1 ? "es" : ""); 631 } 632 633 /* 634 * Find the protox for the given "well-known" name. 635 */ 636 static struct protox * 637 knownname(name) 638 char *name; 639 { 640 struct protox **tpp, *tp; 641 642 for (tpp = protoprotox; *tpp; tpp++) 643 for (tp = *tpp; tp->pr_name; tp++) 644 if (strcmp(tp->pr_name, name) == 0) 645 return (tp); 646 return (NULL); 647 } 648 649 /* 650 * Find the protox corresponding to name. 651 */ 652 static struct protox * 653 name2protox(name) 654 char *name; 655 { 656 struct protox *tp; 657 char **alias; /* alias from p->aliases */ 658 struct protoent *p; 659 660 /* 661 * Try to find the name in the list of "well-known" names. If that 662 * fails, check if name is an alias for an Internet protocol. 663 */ 664 if ((tp = knownname(name)) != NULL) 665 return (tp); 666 667 setprotoent(1); /* make protocol lookup cheaper */ 668 while ((p = getprotoent()) != NULL) { 669 /* assert: name not same as p->name */ 670 for (alias = p->p_aliases; *alias; alias++) 671 if (strcmp(name, *alias) == 0) { 672 endprotoent(); 673 return (knownname(p->p_name)); 674 } 675 } 676 endprotoent(); 677 return (NULL); 678 } 679 680 static void 681 usage() 682 { 683 (void)fprintf(stderr, 684 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname); 685 (void)fprintf(stderr, 686 " %s [-gimnrsv] [-f address_family] [-M core] [-N system]\n", 687 __progname); 688 (void)fprintf(stderr, 689 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname); 690 (void)fprintf(stderr, 691 " %s [-M core] [-N system] [-p protocol]\n", __progname); 692 (void)fprintf(stderr, 693 " %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname); 694 exit(1); 695 } 696