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