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