1 /* $NetBSD: main.c,v 1.27 2000/04/14 06:11:09 simonb 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.27 2000/04/14 06:11:09 simonb 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 #define N_PFKEYSTAT 53 179 { "_pfkeystat" }, 180 { "" }, 181 }; 182 183 struct protox { 184 u_char pr_index; /* index into nlist of cb head */ 185 u_char pr_sindex; /* index into nlist of stat block */ 186 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 187 void (*pr_cblocks) /* control blocks printing routine */ 188 __P((u_long, char *)); 189 void (*pr_stats) /* statistics printing routine */ 190 __P((u_long, char *)); 191 void (*pr_istats) 192 __P((char *)); /* per/if statistics printing routine */ 193 void (*pr_dump) /* PCB state dump routine */ 194 __P((u_long)); 195 char *pr_name; /* well-known name */ 196 } protox[] = { 197 { N_TCBTABLE, N_TCPSTAT, 1, protopr, 198 tcp_stats, NULL, tcp_dump, "tcp" }, 199 { N_UDBTABLE, N_UDPSTAT, 1, protopr, 200 udp_stats, NULL, 0, "udp" }, 201 { -1, N_IPSTAT, 1, 0, 202 ip_stats, NULL, 0, "ip" }, 203 { -1, N_ICMPSTAT, 1, 0, 204 icmp_stats, NULL, 0, "icmp" }, 205 { -1, N_IGMPSTAT, 1, 0, 206 igmp_stats, NULL, 0, "igmp" }, 207 #ifdef IPSEC 208 { -1, N_IPSECSTAT, 1, 0, 209 ipsec_stats, NULL, 0, "ipsec" }, 210 #endif 211 { -1, -1, 0, 0, 212 0, NULL, 0, 0 } 213 }; 214 215 #ifdef INET6 216 struct protox ip6protox[] = { 217 { -1, N_IP6STAT, 1, 0, 218 ip6_stats, ip6_ifstats, 0, "ip6" }, 219 { -1, N_ICMP6STAT, 1, 0, 220 icmp6_stats, icmp6_ifstats, 0, "icmp6" }, 221 #ifdef TCP6 222 { N_TCB6, N_TCP6STAT, 1, ip6protopr, 223 tcp6_stats, NULL, tcp6_dump, "tcp6" }, 224 #else 225 { N_TCB6, N_TCP6STAT, 1, ip6protopr, 226 tcp_stats, NULL, tcp_dump, "tcp6" }, 227 #endif 228 { N_UDB6, N_UDP6STAT, 1, ip6protopr, 229 udp6_stats, NULL, 0, "udp6" }, 230 #ifdef IPSEC 231 { -1, N_IPSEC6STAT, 1, 0, 232 ipsec_stats, NULL, 0, "ipsec6" }, 233 #endif 234 { -1, N_PIM6STAT, 1, 0, 235 pim6_stats, NULL, 0, "pim6" }, 236 { -1, -1, 0, 0, 237 0, NULL, 0, 0 } 238 }; 239 #endif 240 241 #ifdef IPSEC 242 struct protox pfkeyprotox[] = { 243 { -1, N_PFKEYSTAT, 1, 0, 244 pfkey_stats, NULL, 0, "pfkey" }, 245 { -1, -1, 0, 0, 246 0, NULL, 0, 0 } 247 }; 248 #endif 249 250 #ifndef SMALL 251 struct protox atalkprotox[] = { 252 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 253 ddp_stats, NULL, 0, "ddp" }, 254 { -1, -1, 0, 0, 255 0, NULL, 0 } 256 }; 257 258 struct protox nsprotox[] = { 259 { N_IDP, N_IDPSTAT, 1, nsprotopr, 260 idp_stats, NULL, 0, "idp" }, 261 { N_IDP, N_SPPSTAT, 1, nsprotopr, 262 spp_stats, NULL, 0, "spp" }, 263 { -1, N_NSERR, 1, 0, 264 nserr_stats, NULL, 0, "ns_err" }, 265 { -1, -1, 0, 0, 266 0, NULL, 0 } 267 }; 268 269 struct protox isoprotox[] = { 270 { ISO_TP, N_TPSTAT, 1, iso_protopr, 271 tp_stats, NULL, 0, "tp" }, 272 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 273 cltp_stats, NULL, 0, "cltp" }, 274 { -1, N_CLNPSTAT, 1, 0, 275 clnp_stats, NULL, 0, "clnp"}, 276 { -1, N_ESISSTAT, 1, 0, 277 esis_stats, NULL, 0, "esis"}, 278 { -1, -1, 0, 0, 279 0, NULL, 0, 0 } 280 }; 281 #endif 282 283 struct protox *protoprotox[] = { protox, 284 #ifdef INET6 285 ip6protox, 286 #endif 287 #ifdef IPSEC 288 pfkeyprotox, 289 #endif 290 #ifndef SMALL 291 atalkprotox, nsprotox, isoprotox, 292 #endif 293 NULL }; 294 295 int main __P((int, char *[])); 296 static void printproto __P((struct protox *, char *)); 297 static void usage __P((void)); 298 static struct protox *name2protox __P((char *)); 299 static struct protox *knownname __P((char *)); 300 301 kvm_t *kvmd; 302 303 int 304 main(argc, argv) 305 int argc; 306 char *argv[]; 307 { 308 struct protoent *p; 309 struct protox *tp; /* for printing cblocks & stats */ 310 int ch; 311 char *nlistf = NULL, *memf = NULL; 312 char buf[_POSIX2_LINE_MAX], *cp; 313 u_long pcbaddr; 314 gid_t egid = getegid(); 315 316 (void)setegid(getgid()); 317 tp = NULL; 318 af = AF_UNSPEC; 319 pcbaddr = 0; 320 321 while ((ch = getopt(argc, argv, "Aabdf:ghI:LliM:mN:nP:p:rstuvw:")) != -1) 322 switch(ch) { 323 case 'A': 324 Aflag = 1; 325 break; 326 case 'a': 327 aflag = 1; 328 break; 329 case 'b': 330 bflag = 1; 331 break; 332 case 'd': 333 dflag = 1; 334 break; 335 case 'f': 336 if (strcmp(optarg, "ns") == 0) 337 af = AF_NS; 338 else if (strcmp(optarg, "inet") == 0) 339 af = AF_INET; 340 else if (strcmp(optarg, "inet6") == 0) 341 af = AF_INET6; 342 else if (strcmp(optarg, "unix") == 0 343 || strcmp(optarg, "local") == 0) 344 af = AF_LOCAL; 345 else if (strcmp(optarg, "iso") == 0) 346 af = AF_ISO; 347 else if (strcmp(optarg, "atalk") == 0) 348 af = AF_APPLETALK; 349 else 350 errx(1, "%s: unknown address family", 351 optarg); 352 break; 353 #ifndef SMALL 354 case 'g': 355 gflag = 1; 356 break; 357 #endif 358 case 'I': 359 iflag = 1; 360 interface = optarg; 361 break; 362 case 'i': 363 iflag = 1; 364 break; 365 case 'L': 366 Lflag = 1; 367 break; 368 case 'l': 369 lflag = 1; 370 break; 371 case 'M': 372 memf = optarg; 373 break; 374 case 'm': 375 mflag = 1; 376 break; 377 case 'N': 378 nlistf = optarg; 379 break; 380 case 'n': 381 nflag = 1; 382 break; 383 case 'P': 384 pcbaddr = strtoul(optarg, &cp, 16); 385 if (*cp != '\0' || errno == ERANGE) 386 errx(1, "invalid PCB address %s", 387 optarg); 388 Pflag = 1; 389 break; 390 case 'p': 391 if ((tp = name2protox(optarg)) == NULL) 392 errx(1, "%s: unknown or uninstrumented protocol", 393 optarg); 394 pflag = 1; 395 break; 396 case 'r': 397 rflag = 1; 398 break; 399 case 's': 400 ++sflag; 401 break; 402 case 't': 403 tflag = 1; 404 break; 405 case 'u': 406 af = AF_LOCAL; 407 break; 408 case 'v': 409 vflag = 1; 410 break; 411 case 'w': 412 interval = atoi(optarg); 413 iflag = 1; 414 break; 415 case '?': 416 default: 417 usage(); 418 } 419 argv += optind; 420 argc -= optind; 421 422 #define BACKWARD_COMPATIBILITY 423 #ifdef BACKWARD_COMPATIBILITY 424 if (*argv) { 425 if (isdigit(**argv)) { 426 interval = atoi(*argv); 427 if (interval <= 0) 428 usage(); 429 ++argv; 430 iflag = 1; 431 } 432 if (*argv) { 433 nlistf = *argv; 434 if (*++argv) 435 memf = *argv; 436 } 437 } 438 #endif 439 440 /* 441 * Discard setgid privileges. If not the running kernel, we toss 442 * them away totally so that bad guys can't print interesting stuff 443 * from kernel memory, otherwise switch back to kmem for the 444 * duration of the kvm_openfiles() call. 445 */ 446 if (nlistf != NULL || memf != NULL || Pflag) 447 (void)setgid(getgid()); 448 else 449 (void)setegid(egid); 450 451 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, 452 buf)) == NULL) 453 errx(1, "%s", buf); 454 455 /* do this now anyway */ 456 if (nlistf == NULL && memf == NULL) 457 (void)setgid(getgid()); 458 459 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) { 460 if (nlistf) 461 errx(1, "%s: no namelist", nlistf); 462 else 463 errx(1, "no namelist"); 464 } 465 if (mflag) { 466 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value, 467 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value, 468 nl[N_MCLPOOL].n_value); 469 exit(0); 470 } 471 if (Pflag) { 472 if (tp == NULL) { 473 /* Default to TCP. */ 474 tp = name2protox("tcp"); 475 } 476 if (tp->pr_dump) 477 (*tp->pr_dump)(pcbaddr); 478 else 479 printf("%s: no PCB dump routine\n", tp->pr_name); 480 exit(0); 481 } 482 if (pflag) { 483 if (iflag && tp->pr_istats) 484 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats); 485 else if (tp->pr_stats) 486 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 487 tp->pr_name); 488 else 489 printf("%s: no stats routine\n", tp->pr_name); 490 exit(0); 491 } 492 /* 493 * Keep file descriptors open to avoid overhead 494 * of open/close on each call to get* routines. 495 */ 496 sethostent(1); 497 setnetent(1); 498 if (iflag) { 499 if (af != AF_UNSPEC) 500 goto protostat; 501 502 intpr(interval, nl[N_IFNET].n_value, NULL); 503 exit(0); 504 } 505 if (rflag) { 506 if (sflag) 507 rt_stats(nl[N_RTSTAT].n_value); 508 else 509 routepr(nl[N_RTREE].n_value); 510 exit(0); 511 } 512 #ifndef SMALL 513 if (gflag) { 514 if (sflag) { 515 if (af == AF_INET || af == AF_UNSPEC) 516 mrt_stats(nl[N_MRTPROTO].n_value, 517 nl[N_MRTSTAT].n_value); 518 #ifdef INET6 519 if (af == AF_INET6 || af == AF_UNSPEC) 520 mrt6_stats(nl[N_MRT6PROTO].n_value, 521 nl[N_MRT6STAT].n_value); 522 #endif 523 } 524 else { 525 if (af == AF_INET || af == AF_UNSPEC) 526 mroutepr(nl[N_MRTPROTO].n_value, 527 nl[N_MFCHASHTBL].n_value, 528 nl[N_MFCHASH].n_value, 529 nl[N_VIFTABLE].n_value); 530 #ifdef INET6 531 if (af == AF_INET6 || af == AF_UNSPEC) 532 mroute6pr(nl[N_MRT6PROTO].n_value, 533 nl[N_MF6CTABLE].n_value, 534 nl[N_MIF6TABLE].n_value); 535 #endif 536 } 537 exit(0); 538 } 539 #endif 540 protostat: 541 if (af == AF_INET || af == AF_UNSPEC) { 542 setprotoent(1); 543 setservent(1); 544 /* ugh, this is O(MN) ... why do we do this? */ 545 while ((p = getprotoent()) != NULL) { 546 for (tp = protox; tp->pr_name; tp++) 547 if (strcmp(tp->pr_name, p->p_name) == 0) 548 break; 549 if (tp->pr_name == 0 || tp->pr_wanted == 0) 550 continue; 551 printproto(tp, p->p_name); 552 tp->pr_wanted = 0; 553 } 554 endprotoent(); 555 for (tp = protox; tp->pr_name; tp++) 556 if (tp->pr_wanted) 557 printproto(tp, tp->pr_name); 558 } 559 #ifdef INET6 560 if (af == AF_INET6 || af == AF_UNSPEC) 561 for (tp = ip6protox; tp->pr_name; tp++) 562 printproto(tp, tp->pr_name); 563 #endif 564 #ifdef IPSEC 565 if (af == PF_KEY || af == AF_UNSPEC) 566 for (tp = pfkeyprotox; tp->pr_name; tp++) 567 printproto(tp, tp->pr_name); 568 #endif 569 #ifndef SMALL 570 if (af == AF_APPLETALK || af == AF_UNSPEC) 571 for (tp = atalkprotox; tp->pr_name; tp++) 572 printproto(tp, tp->pr_name); 573 if (af == AF_NS || af == AF_UNSPEC) 574 for (tp = nsprotox; tp->pr_name; tp++) 575 printproto(tp, tp->pr_name); 576 if (af == AF_ISO || af == AF_UNSPEC) 577 for (tp = isoprotox; tp->pr_name; tp++) 578 printproto(tp, tp->pr_name); 579 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag) 580 unixpr(nl[N_UNIXSW].n_value); 581 #endif 582 exit(0); 583 } 584 585 /* 586 * Print out protocol statistics or control blocks (per sflag). 587 * If the interface was not specifically requested, and the symbol 588 * is not in the namelist, ignore this one. 589 */ 590 static void 591 printproto(tp, name) 592 struct protox *tp; 593 char *name; 594 { 595 void (*pr) __P((u_long, char *)); 596 u_long off; 597 598 if (sflag) { 599 if (iflag) { 600 if (tp->pr_istats) 601 intpr(interval, nl[N_IFNET].n_value, 602 tp->pr_istats); 603 return; 604 } 605 else { 606 pr = tp->pr_stats; 607 off = nl[tp->pr_sindex].n_value; 608 } 609 } else { 610 pr = tp->pr_cblocks; 611 off = nl[tp->pr_index].n_value; 612 } 613 if (pr != NULL && (off || af != AF_UNSPEC)) 614 (*pr)(off, name); 615 } 616 617 /* 618 * Read kernel memory, return 0 on success. 619 */ 620 int 621 kread(addr, buf, size) 622 u_long addr; 623 char *buf; 624 int size; 625 { 626 627 if (kvm_read(kvmd, addr, buf, size) != size) { 628 warnx("%s\n", kvm_geterr(kvmd)); 629 return (-1); 630 } 631 return (0); 632 } 633 634 char * 635 plural(n) 636 int n; 637 { 638 639 return (n != 1 ? "s" : ""); 640 } 641 642 char * 643 plurales(n) 644 int n; 645 { 646 647 return (n != 1 ? "es" : ""); 648 } 649 650 /* 651 * Find the protox for the given "well-known" name. 652 */ 653 static struct protox * 654 knownname(name) 655 char *name; 656 { 657 struct protox **tpp, *tp; 658 659 for (tpp = protoprotox; *tpp; tpp++) 660 for (tp = *tpp; tp->pr_name; tp++) 661 if (strcmp(tp->pr_name, name) == 0) 662 return (tp); 663 return (NULL); 664 } 665 666 /* 667 * Find the protox corresponding to name. 668 */ 669 static struct protox * 670 name2protox(name) 671 char *name; 672 { 673 struct protox *tp; 674 char **alias; /* alias from p->aliases */ 675 struct protoent *p; 676 677 /* 678 * Try to find the name in the list of "well-known" names. If that 679 * fails, check if name is an alias for an Internet protocol. 680 */ 681 if ((tp = knownname(name)) != NULL) 682 return (tp); 683 684 setprotoent(1); /* make protocol lookup cheaper */ 685 while ((p = getprotoent()) != NULL) { 686 /* assert: name not same as p->name */ 687 for (alias = p->p_aliases; *alias; alias++) 688 if (strcmp(name, *alias) == 0) { 689 endprotoent(); 690 return (knownname(p->p_name)); 691 } 692 } 693 endprotoent(); 694 return (NULL); 695 } 696 697 static void 698 usage() 699 { 700 (void)fprintf(stderr, 701 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname); 702 (void)fprintf(stderr, 703 " %s [-gimnrsv] [-f address_family] [-M core] [-N system]\n", 704 __progname); 705 (void)fprintf(stderr, 706 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname); 707 (void)fprintf(stderr, 708 " %s [-M core] [-N system] [-p protocol]\n", __progname); 709 (void)fprintf(stderr, 710 " %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname); 711 exit(1); 712 } 713