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 #if 0 41 #ifndef lint 42 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 43 #endif /* not lint */ 44 #endif 45 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <sys/param.h> 50 #include <sys/file.h> 51 #include <sys/protosw.h> 52 #include <sys/socket.h> 53 54 #include <netinet/in.h> 55 56 #include <netgraph/ng_socket.h> 57 58 #include <ctype.h> 59 #include <err.h> 60 #include <errno.h> 61 #include <kvm.h> 62 #include <limits.h> 63 #include <netdb.h> 64 #include <nlist.h> 65 #include <paths.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 #include "netstat.h" 71 72 static struct nlist nl[] = { 73 #define N_IFNET 0 74 { "_ifnet" }, 75 #define N_RTSTAT 1 76 { "_rtstat" }, 77 #define N_RTREE 2 78 { "_rt_tables"}, 79 #define N_MRTSTAT 3 80 { "_mrtstat" }, 81 #define N_MFCTABLE 4 82 { "_mfctable" }, 83 #define N_VIFTABLE 5 84 { "_viftable" }, 85 #define N_IPX 6 86 { "_ipxpcb"}, 87 #define N_IPXSTAT 7 88 { "_ipxstat"}, 89 #define N_SPXSTAT 8 90 { "_spx_istat"}, 91 #define N_DDPSTAT 9 92 { "_ddpstat"}, 93 #define N_DDPCB 10 94 { "_ddpcb"}, 95 #define N_NGSOCKS 11 96 { "_ngsocklist"}, 97 #define N_IP6STAT 12 98 { "_ip6stat" }, 99 #define N_ICMP6STAT 13 100 { "_icmp6stat" }, 101 #define N_IPSECSTAT 14 102 { "_ipsecstat" }, 103 #define N_IPSEC6STAT 15 104 { "_ipsec6stat" }, 105 #define N_PIM6STAT 16 106 { "_pim6stat" }, 107 #define N_MRT6STAT 17 108 { "_mrt6stat" }, 109 #define N_MF6CTABLE 18 110 { "_mf6ctable" }, 111 #define N_MIF6TABLE 19 112 { "_mif6table" }, 113 #define N_PFKEYSTAT 20 114 { "_pfkeystat" }, 115 #define N_MBSTAT 21 116 { "_mbstat" }, 117 #define N_MBTYPES 22 118 { "_mbtypes" }, 119 #define N_NMBCLUSTERS 23 120 { "_nmbclusters" }, 121 #define N_NMBUFS 24 122 { "_nmbufs" }, 123 #define N_MBHI 25 124 { "_mbuf_hiwm" }, 125 #define N_CLHI 26 126 { "_clust_hiwm" }, 127 #define N_NCPUS 27 128 { "_smp_cpus" }, 129 #define N_PAGESZ 28 130 { "_pagesize" }, 131 #define N_MBPSTAT 29 132 { "_mb_statpcpu" }, 133 #define N_RTTRASH 30 134 { "_rttrash" }, 135 #define N_MBLO 31 136 { "_mbuf_lowm" }, 137 #define N_CLLO 32 138 { "_clust_lowm" }, 139 { "" }, 140 }; 141 142 struct protox { 143 u_char pr_index; /* index into nlist of cb head */ 144 u_char pr_sindex; /* index into nlist of stat block */ 145 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 146 void (*pr_cblocks)(u_long, const char *, int); 147 /* control blocks printing routine */ 148 void (*pr_stats)(u_long, const char *, int); 149 /* statistics printing routine */ 150 void (*pr_istats)(char *); /* per/if statistics printing routine */ 151 const char *pr_name; /* well-known name */ 152 u_long pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 153 } protox[] = { 154 { -1, -1, 1, protopr, 155 tcp_stats, NULL, "tcp", IPPROTO_TCP }, 156 { -1, -1, 1, protopr, 157 udp_stats, NULL, "udp", IPPROTO_UDP }, 158 { -1, -1, 1, protopr, 159 NULL, NULL, "divert",IPPROTO_DIVERT }, 160 { -1, -1, 1, protopr, 161 ip_stats, NULL, "ip", IPPROTO_RAW }, 162 { -1, -1, 1, protopr, 163 icmp_stats, NULL, "icmp", IPPROTO_ICMP }, 164 { -1, -1, 1, protopr, 165 igmp_stats, NULL, "igmp", IPPROTO_IGMP }, 166 #ifdef IPSEC 167 { -1, N_IPSECSTAT, 1, NULL, 168 ipsec_stats, NULL, "ipsec", 0}, 169 #endif 170 { -1, -1, 1, NULL, 171 bdg_stats, NULL, "bdg", 1 /* bridging... */ }, 172 { -1, -1, 1, protopr, 173 pim_stats, NULL, "pim", IPPROTO_PIM }, 174 { -1, -1, 0, NULL, 175 NULL, NULL, NULL, 0 } 176 }; 177 178 #ifdef INET6 179 struct protox ip6protox[] = { 180 { -1, -1, 1, protopr, 181 tcp_stats, NULL, "tcp", IPPROTO_TCP }, 182 { -1, -1, 1, protopr, 183 udp_stats, NULL, "udp", IPPROTO_UDP }, 184 { -1, N_IP6STAT, 1, protopr, 185 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW }, 186 { -1, N_ICMP6STAT, 1, protopr, 187 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 }, 188 #ifdef IPSEC 189 { -1, N_IPSEC6STAT, 1, NULL, 190 ipsec_stats, NULL, "ipsec6",0 }, 191 #endif 192 #ifdef notyet 193 { -1, N_PIM6STAT, 1, NULL, 194 pim6_stats, NULL, "pim6", 0 }, 195 #endif 196 { -1, -1, 1, NULL, 197 rip6_stats, NULL, "rip6", 0 }, 198 { -1, -1, 1, NULL, 199 bdg_stats, NULL, "bdg", 1 /* bridging... */ }, 200 { -1, -1, 0, NULL, 201 NULL, NULL, NULL, 0 } 202 }; 203 #endif /*INET6*/ 204 205 #ifdef IPSEC 206 struct protox pfkeyprotox[] = { 207 { -1, N_PFKEYSTAT, 1, NULL, 208 pfkey_stats, NULL, "pfkey", 0 }, 209 { -1, -1, 0, NULL, 210 NULL, NULL, NULL, 0 } 211 }; 212 #endif 213 214 struct protox atalkprotox[] = { 215 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 216 ddp_stats, NULL, "ddp", 0 }, 217 { -1, -1, 0, NULL, 218 NULL, NULL, NULL, 0 } 219 }; 220 221 struct protox netgraphprotox[] = { 222 { N_NGSOCKS, -1, 1, netgraphprotopr, 223 NULL, NULL, "ctrl", 0 }, 224 { N_NGSOCKS, -1, 1, netgraphprotopr, 225 NULL, NULL, "data", 0 }, 226 { -1, -1, 0, NULL, 227 NULL, NULL, NULL, 0 } 228 }; 229 230 struct protox ipxprotox[] = { 231 { N_IPX, N_IPXSTAT, 1, ipxprotopr, 232 ipx_stats, NULL, "ipx", 0 }, 233 { N_IPX, N_SPXSTAT, 1, ipxprotopr, 234 spx_stats, NULL, "spx", 0 }, 235 { -1, -1, 0, NULL, 236 NULL, NULL, 0, 0 } 237 }; 238 239 struct protox *protoprotox[] = { 240 protox, 241 #ifdef INET6 242 ip6protox, 243 #endif 244 #ifdef IPSEC 245 pfkeyprotox, 246 #endif 247 ipxprotox, atalkprotox, NULL }; 248 249 const char *pluralies(int); 250 static void printproto(struct protox *, const char *); 251 static void usage(void); 252 static struct protox *name2protox(char *); 253 static struct protox *knownname(char *); 254 255 static kvm_t *kvmd; 256 static char *nlistf = NULL, *memf = NULL; 257 258 int Aflag; /* show addresses of protocol control block */ 259 int aflag; /* show all sockets (including servers) */ 260 int bflag; /* show i/f total bytes in/out */ 261 int dflag; /* show i/f dropped packets */ 262 int gflag; /* show group (multicast) routing or stats */ 263 int iflag; /* show interfaces */ 264 int Lflag; /* show size of listen queues */ 265 int mflag; /* show memory stats */ 266 int numeric_addr; /* show addresses numerically */ 267 int numeric_port; /* show ports numerically */ 268 static int pflag; /* show given protocol */ 269 int rflag; /* show routing tables (or routing stats) */ 270 int sflag; /* show protocol statistics */ 271 int tflag; /* show i/f watchdog timers */ 272 int Wflag; /* wide display */ 273 int zflag; /* zero stats */ 274 275 int interval; /* repeat interval for i/f stats */ 276 277 char *interface; /* desired i/f for stats, or NULL for all i/fs */ 278 int unit; /* unit number for above */ 279 280 int af; /* address family */ 281 282 int 283 main(int argc, char *argv[]) 284 { 285 struct protox *tp = NULL; /* for printing cblocks & stats */ 286 int ch; 287 288 af = AF_UNSPEC; 289 290 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1) 291 switch(ch) { 292 case 'A': 293 Aflag = 1; 294 break; 295 case 'a': 296 aflag = 1; 297 break; 298 case 'b': 299 bflag = 1; 300 break; 301 case 'd': 302 dflag = 1; 303 break; 304 case 'f': 305 if (strcmp(optarg, "ipx") == 0) 306 af = AF_IPX; 307 else if (strcmp(optarg, "inet") == 0) 308 af = AF_INET; 309 #ifdef INET6 310 else if (strcmp(optarg, "inet6") == 0) 311 af = AF_INET6; 312 #endif /*INET6*/ 313 #ifdef INET6 314 else if (strcmp(optarg, "pfkey") == 0) 315 af = PF_KEY; 316 #endif /*INET6*/ 317 else if (strcmp(optarg, "unix") == 0) 318 af = AF_UNIX; 319 else if (strcmp(optarg, "atalk") == 0) 320 af = AF_APPLETALK; 321 else if (strcmp(optarg, "ng") == 0 322 || strcmp(optarg, "netgraph") == 0) 323 af = AF_NETGRAPH; 324 else if (strcmp(optarg, "link") == 0) 325 af = AF_LINK; 326 else { 327 errx(1, "%s: unknown address family", optarg); 328 } 329 break; 330 case 'g': 331 gflag = 1; 332 break; 333 case 'I': { 334 char *cp; 335 336 iflag = 1; 337 for (cp = interface = optarg; isalpha(*cp); cp++) 338 continue; 339 unit = atoi(cp); 340 break; 341 } 342 case 'i': 343 iflag = 1; 344 break; 345 case 'L': 346 Lflag = 1; 347 break; 348 case 'M': 349 memf = optarg; 350 break; 351 case 'm': 352 mflag = 1; 353 break; 354 case 'N': 355 nlistf = optarg; 356 break; 357 case 'n': 358 numeric_addr = numeric_port = 1; 359 break; 360 case 'p': 361 if ((tp = name2protox(optarg)) == NULL) { 362 errx(1, 363 "%s: unknown or uninstrumented protocol", 364 optarg); 365 } 366 pflag = 1; 367 break; 368 case 'r': 369 rflag = 1; 370 break; 371 case 's': 372 ++sflag; 373 break; 374 case 'S': 375 numeric_addr = 1; 376 break; 377 case 't': 378 tflag = 1; 379 break; 380 case 'u': 381 af = AF_UNIX; 382 break; 383 case 'W': 384 case 'l': 385 Wflag = 1; 386 break; 387 case 'w': 388 interval = atoi(optarg); 389 iflag = 1; 390 break; 391 case 'z': 392 zflag = 1; 393 break; 394 case '?': 395 default: 396 usage(); 397 } 398 argv += optind; 399 argc -= optind; 400 401 #define BACKWARD_COMPATIBILITY 402 #ifdef BACKWARD_COMPATIBILITY 403 if (*argv) { 404 if (isdigit(**argv)) { 405 interval = atoi(*argv); 406 if (interval <= 0) 407 usage(); 408 ++argv; 409 iflag = 1; 410 } 411 if (*argv) { 412 nlistf = *argv; 413 if (*++argv) 414 memf = *argv; 415 } 416 } 417 #endif 418 419 /* 420 * Discard setgid privileges if not the running kernel so that bad 421 * guys can't print interesting stuff from kernel memory. 422 */ 423 if (nlistf != NULL || memf != NULL) 424 setgid(getgid()); 425 426 if (mflag) { 427 if (memf != NULL) { 428 if (kread(0, 0, 0) == 0) 429 mbpr(nl[N_MBSTAT].n_value, 430 nl[N_MBTYPES].n_value, 431 nl[N_NMBCLUSTERS].n_value, 432 nl[N_NMBUFS].n_value, 433 nl[N_MBHI].n_value, 434 nl[N_CLHI].n_value, 435 nl[N_MBLO].n_value, 436 nl[N_CLLO].n_value, 437 nl[N_NCPUS].n_value, 438 nl[N_PAGESZ].n_value, 439 nl[N_MBPSTAT].n_value); 440 } else 441 mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 442 exit(0); 443 } 444 #if 0 445 /* 446 * Keep file descriptors open to avoid overhead 447 * of open/close on each call to get* routines. 448 */ 449 sethostent(1); 450 setnetent(1); 451 #else 452 /* 453 * This does not make sense any more with DNS being default over 454 * the files. Doing a setXXXXent(1) causes a tcp connection to be 455 * used for the queries, which is slower. 456 */ 457 #endif 458 if (iflag && !sflag) { 459 kread(0, 0, 0); 460 intpr(interval, nl[N_IFNET].n_value, NULL); 461 exit(0); 462 } 463 if (rflag) { 464 kread(0, 0, 0); 465 if (sflag) 466 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value); 467 else 468 routepr(nl[N_RTREE].n_value); 469 exit(0); 470 } 471 if (gflag) { 472 kread(0, 0, 0); 473 if (sflag) { 474 if (af == AF_INET || af == AF_UNSPEC) 475 mrt_stats(nl[N_MRTSTAT].n_value); 476 #ifdef INET6 477 if (af == AF_INET6 || af == AF_UNSPEC) 478 mrt6_stats(nl[N_MRT6STAT].n_value); 479 #endif 480 } else { 481 if (af == AF_INET || af == AF_UNSPEC) 482 mroutepr(nl[N_MFCTABLE].n_value, 483 nl[N_VIFTABLE].n_value); 484 #ifdef INET6 485 if (af == AF_INET6 || af == AF_UNSPEC) 486 mroute6pr(nl[N_MF6CTABLE].n_value, 487 nl[N_MIF6TABLE].n_value); 488 #endif 489 } 490 ifmalist_dump(); 491 exit(0); 492 } 493 494 kread(0, 0, 0); 495 if (tp) { 496 printproto(tp, tp->pr_name); 497 exit(0); 498 } 499 if (af == AF_INET || af == AF_UNSPEC) 500 for (tp = protox; tp->pr_name; tp++) 501 printproto(tp, tp->pr_name); 502 #ifdef INET6 503 if (af == AF_INET6 || af == AF_UNSPEC) 504 for (tp = ip6protox; tp->pr_name; tp++) 505 printproto(tp, tp->pr_name); 506 #endif /*INET6*/ 507 #ifdef IPSEC 508 if (af == PF_KEY || af == AF_UNSPEC) 509 for (tp = pfkeyprotox; tp->pr_name; tp++) 510 printproto(tp, tp->pr_name); 511 #endif /*IPSEC*/ 512 if (af == AF_IPX || af == AF_UNSPEC) { 513 kread(0, 0, 0); 514 for (tp = ipxprotox; tp->pr_name; tp++) 515 printproto(tp, tp->pr_name); 516 } 517 if (af == AF_APPLETALK || af == AF_UNSPEC) 518 for (tp = atalkprotox; tp->pr_name; tp++) 519 printproto(tp, tp->pr_name); 520 if (af == AF_NETGRAPH || af == AF_UNSPEC) 521 for (tp = netgraphprotox; tp->pr_name; tp++) 522 printproto(tp, tp->pr_name); 523 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag) 524 unixpr(); 525 exit(0); 526 } 527 528 /* 529 * Print out protocol statistics or control blocks (per sflag). 530 * If the interface was not specifically requested, and the symbol 531 * is not in the namelist, ignore this one. 532 */ 533 static void 534 printproto(tp, name) 535 struct protox *tp; 536 const char *name; 537 { 538 void (*pr)(u_long, const char *, int); 539 u_long off; 540 541 if (sflag) { 542 if (iflag) { 543 if (tp->pr_istats) 544 intpr(interval, nl[N_IFNET].n_value, 545 tp->pr_istats); 546 else if (pflag) 547 printf("%s: no per-interface stats routine\n", 548 tp->pr_name); 549 return; 550 } 551 else { 552 pr = tp->pr_stats; 553 if (!pr) { 554 if (pflag) 555 printf("%s: no stats routine\n", 556 tp->pr_name); 557 return; 558 } 559 off = tp->pr_usesysctl ? tp->pr_usesysctl 560 : nl[tp->pr_sindex].n_value; 561 } 562 } else { 563 pr = tp->pr_cblocks; 564 if (!pr) { 565 if (pflag) 566 printf("%s: no PCB routine\n", tp->pr_name); 567 return; 568 } 569 off = tp->pr_usesysctl ? tp->pr_usesysctl 570 : nl[tp->pr_index].n_value; 571 } 572 if (pr != NULL && (off || af != AF_UNSPEC)) 573 (*pr)(off, name, af); 574 } 575 576 /* 577 * Read kernel memory, return 0 on success. 578 */ 579 int 580 kread(u_long addr, char *buf, int size) 581 { 582 if (kvmd == 0) { 583 /* 584 * XXX. 585 */ 586 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 587 setgid(getgid()); 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 const char * 618 plural(int n) 619 { 620 return (n != 1 ? "s" : ""); 621 } 622 623 const char * 624 plurales(int n) 625 { 626 return (n != 1 ? "es" : ""); 627 } 628 629 const char * 630 pluralies(int n) 631 { 632 return (n != 1 ? "ies" : "y"); 633 } 634 635 /* 636 * Find the protox for the given "well-known" name. 637 */ 638 static struct protox * 639 knownname(char *name) 640 { 641 struct protox **tpp, *tp; 642 643 for (tpp = protoprotox; *tpp; tpp++) 644 for (tp = *tpp; tp->pr_name; tp++) 645 if (strcmp(tp->pr_name, name) == 0) 646 return (tp); 647 return (NULL); 648 } 649 650 /* 651 * Find the protox corresponding to name. 652 */ 653 static struct protox * 654 name2protox(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(void) 682 { 683 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 684 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n" 685 " [-M core] [-N system]", 686 " netstat -i | -I interface [-abdnt] [-f address_family]\n" 687 " [-M core] [-N system]", 688 " netstat -w wait [-I interface] [-d] [-M core] [-N system]", 689 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]", 690 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n" 691 " [-M core] [-N system]", 692 " netstat -m [-M core] [-N system]", 693 " netstat -r [-AenW] [-f address_family] [-M core] [-N system]", 694 " netstat -rs [-s] [-M core] [-N system]", 695 " netstat -g [-W] [-f address_family] [-M core] [-N system]", 696 " netstat -gs [-s] [-f address_family] [-M core] [-N system]"); 697 exit(1); 698 } 699