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