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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 char const copyright[] = 32 "@(#) Copyright (c) 1983, 1988, 1993\n\ 33 Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #if 0 37 #ifndef lint 38 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 39 #endif /* not lint */ 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/file.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 51 #include <netinet/in.h> 52 53 #ifdef NETGRAPH 54 #include <netgraph/ng_socket.h> 55 #endif 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 <stdint.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 { .n_name = "_ifnet" }, /* XXXGL: can be deleted */ 75 #define N_RTSTAT 1 76 { .n_name = "_rtstat" }, 77 #define N_RTREE 2 78 { .n_name = "_rt_tables"}, 79 #define N_MRTSTAT 3 80 { .n_name = "_mrtstat" }, 81 #define N_MFCHASHTBL 4 82 { .n_name = "_mfchashtbl" }, 83 #define N_VIFTABLE 5 84 { .n_name = "_viftable" }, 85 #define N_IPX 6 86 { .n_name = "_ipxpcb_list"}, 87 #define N_IPXSTAT 7 88 { .n_name = "_ipxstat"}, 89 #define N_SPXSTAT 8 90 { .n_name = "_spx_istat"}, 91 #define N_DDPSTAT 9 92 { .n_name = "_ddpstat"}, 93 #define N_DDPCB 10 94 { .n_name = "_ddpcb"}, 95 #define N_NGSOCKS 11 96 { .n_name = "_ngsocklist"}, 97 #define N_IP6STAT 12 98 { .n_name = "_ip6stat" }, 99 #define N_ICMP6STAT 13 100 { .n_name = "_icmp6stat" }, 101 #define N_IPSECSTAT 14 102 { .n_name = "_ipsec4stat" }, 103 #define N_IPSEC6STAT 15 104 { .n_name = "_ipsec6stat" }, 105 #define N_PIM6STAT 16 106 { .n_name = "_pim6stat" }, 107 #define N_MRT6STAT 17 108 { .n_name = "_mrt6stat" }, 109 #define N_MF6CTABLE 18 110 { .n_name = "_mf6ctable" }, 111 #define N_MIF6TABLE 19 112 { .n_name = "_mif6table" }, 113 #define N_PFKEYSTAT 20 114 { .n_name = "_pfkeystat" }, 115 #define N_RTTRASH 21 116 { .n_name = "_rttrash" }, 117 #define N_CARPSTAT 22 118 { .n_name = "_carpstats" }, 119 #define N_PFSYNCSTAT 23 120 { .n_name = "_pfsyncstats" }, 121 #define N_AHSTAT 24 122 { .n_name = "_ahstat" }, 123 #define N_ESPSTAT 25 124 { .n_name = "_espstat" }, 125 #define N_IPCOMPSTAT 26 126 { .n_name = "_ipcompstat" }, 127 #define N_TCPSTAT 27 128 { .n_name = "_tcpstat" }, 129 #define N_UDPSTAT 28 130 { .n_name = "_udpstat" }, 131 #define N_IPSTAT 29 132 { .n_name = "_ipstat" }, 133 #define N_ICMPSTAT 30 134 { .n_name = "_icmpstat" }, 135 #define N_IGMPSTAT 31 136 { .n_name = "_igmpstat" }, 137 #define N_PIMSTAT 32 138 { .n_name = "_pimstat" }, 139 #define N_TCBINFO 33 140 { .n_name = "_tcbinfo" }, 141 #define N_UDBINFO 34 142 { .n_name = "_udbinfo" }, 143 #define N_DIVCBINFO 35 144 { .n_name = "_divcbinfo" }, 145 #define N_RIPCBINFO 36 146 { .n_name = "_ripcbinfo" }, 147 #define N_UNP_COUNT 37 148 { .n_name = "_unp_count" }, 149 #define N_UNP_GENCNT 38 150 { .n_name = "_unp_gencnt" }, 151 #define N_UNP_DHEAD 39 152 { .n_name = "_unp_dhead" }, 153 #define N_UNP_SHEAD 40 154 { .n_name = "_unp_shead" }, 155 #define N_RIP6STAT 41 156 { .n_name = "_rip6stat" }, 157 #define N_SCTPSTAT 42 158 { .n_name = "_sctpstat" }, 159 #define N_MFCTABLESIZE 43 160 { .n_name = "_mfctablesize" }, 161 #define N_ARPSTAT 44 162 { .n_name = "_arpstat" }, 163 #define N_UNP_SPHEAD 45 164 { .n_name = "unp_sphead" }, 165 #define N_SFSTAT 46 166 { .n_name = "_sfstat"}, 167 { .n_name = NULL }, 168 }; 169 170 struct protox { 171 int pr_index; /* index into nlist of cb head */ 172 int pr_sindex; /* index into nlist of stat block */ 173 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 174 void (*pr_cblocks)(u_long, const char *, int, int); 175 /* control blocks printing routine */ 176 void (*pr_stats)(u_long, const char *, int, int); 177 /* statistics printing routine */ 178 void (*pr_istats)(char *); /* per/if statistics printing routine */ 179 const char *pr_name; /* well-known name */ 180 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 181 int pr_protocol; 182 } protox[] = { 183 { N_TCBINFO, N_TCPSTAT, 1, protopr, 184 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 185 { N_UDBINFO, N_UDPSTAT, 1, protopr, 186 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 187 #ifdef SCTP 188 { -1, N_SCTPSTAT, 1, sctp_protopr, 189 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 190 #endif 191 #ifdef SDP 192 { -1, -1, 1, protopr, 193 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 194 #endif 195 { N_DIVCBINFO, -1, 1, protopr, 196 NULL, NULL, "divert", 1, IPPROTO_DIVERT }, 197 { N_RIPCBINFO, N_IPSTAT, 1, protopr, 198 ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 199 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr, 200 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 201 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr, 202 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 203 #ifdef IPSEC 204 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */ 205 ipsec_stats, NULL, "ipsec", 0, 0}, 206 { -1, N_AHSTAT, 1, NULL, 207 ah_stats, NULL, "ah", 0, 0}, 208 { -1, N_ESPSTAT, 1, NULL, 209 esp_stats, NULL, "esp", 0, 0}, 210 { -1, N_IPCOMPSTAT, 1, NULL, 211 ipcomp_stats, NULL, "ipcomp", 0, 0}, 212 #endif 213 { N_RIPCBINFO, N_PIMSTAT, 1, protopr, 214 pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 215 { -1, N_CARPSTAT, 1, NULL, 216 carp_stats, NULL, "carp", 1, 0 }, 217 #ifdef PF 218 { -1, N_PFSYNCSTAT, 1, NULL, 219 pfsync_stats, NULL, "pfsync", 1, 0 }, 220 #endif 221 { -1, N_ARPSTAT, 1, NULL, 222 arp_stats, NULL, "arp", 1, 0 }, 223 { -1, -1, 0, NULL, 224 NULL, NULL, NULL, 0, 0 } 225 }; 226 227 #ifdef INET6 228 struct protox ip6protox[] = { 229 { N_TCBINFO, N_TCPSTAT, 1, protopr, 230 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 231 { N_UDBINFO, N_UDPSTAT, 1, protopr, 232 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 233 { N_RIPCBINFO, N_IP6STAT, 1, protopr, 234 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 235 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, 236 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 237 #ifdef SDP 238 { -1, -1, 1, protopr, 239 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 240 #endif 241 #ifdef IPSEC 242 { -1, N_IPSEC6STAT, 1, NULL, 243 ipsec_stats, NULL, "ipsec6", 0, 0 }, 244 #endif 245 #ifdef notyet 246 { -1, N_PIM6STAT, 1, NULL, 247 pim6_stats, NULL, "pim6", 1, 0 }, 248 #endif 249 { -1, N_RIP6STAT, 1, NULL, 250 rip6_stats, NULL, "rip6", 1, 0 }, 251 { -1, -1, 0, NULL, 252 NULL, NULL, NULL, 0, 0 } 253 }; 254 #endif /*INET6*/ 255 256 #ifdef IPSEC 257 struct protox pfkeyprotox[] = { 258 { -1, N_PFKEYSTAT, 1, NULL, 259 pfkey_stats, NULL, "pfkey", 0, 0 }, 260 { -1, -1, 0, NULL, 261 NULL, NULL, NULL, 0, 0 } 262 }; 263 #endif 264 265 struct protox atalkprotox[] = { 266 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 267 ddp_stats, NULL, "ddp", 0, 0 }, 268 { -1, -1, 0, NULL, 269 NULL, NULL, NULL, 0, 0 } 270 }; 271 #ifdef NETGRAPH 272 struct protox netgraphprotox[] = { 273 { N_NGSOCKS, -1, 1, netgraphprotopr, 274 NULL, NULL, "ctrl", 0, 0 }, 275 { N_NGSOCKS, -1, 1, netgraphprotopr, 276 NULL, NULL, "data", 0, 0 }, 277 { -1, -1, 0, NULL, 278 NULL, NULL, NULL, 0, 0 } 279 }; 280 #endif 281 #ifdef IPX 282 struct protox ipxprotox[] = { 283 { N_IPX, N_IPXSTAT, 1, ipxprotopr, 284 ipx_stats, NULL, "ipx", 0, 0 }, 285 { N_IPX, N_SPXSTAT, 1, ipxprotopr, 286 spx_stats, NULL, "spx", 0, 0 }, 287 { -1, -1, 0, NULL, 288 NULL, NULL, 0, 0, 0 } 289 }; 290 #endif 291 292 struct protox *protoprotox[] = { 293 protox, 294 #ifdef INET6 295 ip6protox, 296 #endif 297 #ifdef IPSEC 298 pfkeyprotox, 299 #endif 300 #ifdef IPX 301 ipxprotox, 302 #endif 303 atalkprotox, NULL }; 304 305 static void printproto(struct protox *, const char *); 306 static void usage(void); 307 static struct protox *name2protox(const char *); 308 static struct protox *knownname(const char *); 309 310 static kvm_t *kvmd; 311 static char *nlistf = NULL, *memf = NULL; 312 313 int Aflag; /* show addresses of protocol control block */ 314 int aflag; /* show all sockets (including servers) */ 315 int Bflag; /* show information about bpf consumers */ 316 int bflag; /* show i/f total bytes in/out */ 317 int dflag; /* show i/f dropped packets */ 318 int gflag; /* show group (multicast) routing or stats */ 319 int hflag; /* show counters in human readable format */ 320 int iflag; /* show interfaces */ 321 int Lflag; /* show size of listen queues */ 322 int mflag; /* show memory stats */ 323 int noutputs = 0; /* how much outputs before we exit */ 324 int numeric_addr; /* show addresses numerically */ 325 int numeric_port; /* show ports numerically */ 326 static int pflag; /* show given protocol */ 327 int Qflag; /* show netisr information */ 328 int rflag; /* show routing tables (or routing stats) */ 329 int sflag; /* show protocol statistics */ 330 int Wflag; /* wide display */ 331 int Tflag; /* TCP Information */ 332 int xflag; /* extra information, includes all socket buffer info */ 333 int zflag; /* zero stats */ 334 335 int interval; /* repeat interval for i/f stats */ 336 337 char *interface; /* desired i/f for stats, or NULL for all i/fs */ 338 int unit; /* unit number for above */ 339 340 int af; /* address family */ 341 int live; /* true if we are examining a live system */ 342 343 int 344 main(int argc, char *argv[]) 345 { 346 struct protox *tp = NULL; /* for printing cblocks & stats */ 347 int ch; 348 int fib = -1; 349 char *endptr; 350 351 af = AF_UNSPEC; 352 353 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz")) 354 != -1) 355 switch(ch) { 356 case '4': 357 #ifdef INET 358 af = AF_INET; 359 #else 360 errx(1, "IPv4 support is not compiled in"); 361 #endif 362 break; 363 case '6': 364 #ifdef INET6 365 af = AF_INET6; 366 #else 367 errx(1, "IPv6 support is not compiled in"); 368 #endif 369 break; 370 case 'A': 371 Aflag = 1; 372 break; 373 case 'a': 374 aflag = 1; 375 break; 376 case 'B': 377 Bflag = 1; 378 break; 379 case 'b': 380 bflag = 1; 381 break; 382 case 'd': 383 dflag = 1; 384 break; 385 case 'F': 386 fib = strtol(optarg, &endptr, 0); 387 if (*endptr != '\0' || 388 (fib == 0 && (errno == EINVAL || errno == ERANGE))) 389 errx(1, "%s: invalid fib", optarg); 390 break; 391 case 'f': 392 if (strcmp(optarg, "ipx") == 0) 393 af = AF_IPX; 394 else if (strcmp(optarg, "inet") == 0) 395 af = AF_INET; 396 #ifdef INET6 397 else if (strcmp(optarg, "inet6") == 0) 398 af = AF_INET6; 399 #endif 400 #ifdef IPSEC 401 else if (strcmp(optarg, "pfkey") == 0) 402 af = PF_KEY; 403 #endif 404 else if (strcmp(optarg, "unix") == 0) 405 af = AF_UNIX; 406 else if (strcmp(optarg, "atalk") == 0) 407 af = AF_APPLETALK; 408 #ifdef NETGRAPH 409 else if (strcmp(optarg, "ng") == 0 410 || strcmp(optarg, "netgraph") == 0) 411 af = AF_NETGRAPH; 412 #endif 413 else if (strcmp(optarg, "link") == 0) 414 af = AF_LINK; 415 else { 416 errx(1, "%s: unknown address family", optarg); 417 } 418 break; 419 case 'g': 420 gflag = 1; 421 break; 422 case 'h': 423 hflag = 1; 424 break; 425 case 'I': { 426 char *cp; 427 428 iflag = 1; 429 for (cp = interface = optarg; isalpha(*cp); cp++) 430 continue; 431 unit = atoi(cp); 432 break; 433 } 434 case 'i': 435 iflag = 1; 436 break; 437 case 'L': 438 Lflag = 1; 439 break; 440 case 'M': 441 memf = optarg; 442 break; 443 case 'm': 444 mflag = 1; 445 break; 446 case 'N': 447 nlistf = optarg; 448 break; 449 case 'n': 450 numeric_addr = numeric_port = 1; 451 break; 452 case 'p': 453 if ((tp = name2protox(optarg)) == NULL) { 454 errx(1, 455 "%s: unknown or uninstrumented protocol", 456 optarg); 457 } 458 pflag = 1; 459 break; 460 case 'Q': 461 Qflag = 1; 462 break; 463 case 'q': 464 noutputs = atoi(optarg); 465 if (noutputs != 0) 466 noutputs++; 467 break; 468 case 'r': 469 rflag = 1; 470 break; 471 case 's': 472 ++sflag; 473 break; 474 case 'S': 475 numeric_addr = 1; 476 break; 477 case 'u': 478 af = AF_UNIX; 479 break; 480 case 'W': 481 case 'l': 482 Wflag = 1; 483 break; 484 case 'w': 485 interval = atoi(optarg); 486 iflag = 1; 487 break; 488 case 'T': 489 Tflag = 1; 490 break; 491 case 'x': 492 xflag = 1; 493 break; 494 case 'z': 495 zflag = 1; 496 break; 497 case '?': 498 default: 499 usage(); 500 } 501 argv += optind; 502 argc -= optind; 503 504 #define BACKWARD_COMPATIBILITY 505 #ifdef BACKWARD_COMPATIBILITY 506 if (*argv) { 507 if (isdigit(**argv)) { 508 interval = atoi(*argv); 509 if (interval <= 0) 510 usage(); 511 ++argv; 512 iflag = 1; 513 } 514 if (*argv) { 515 nlistf = *argv; 516 if (*++argv) 517 memf = *argv; 518 } 519 } 520 #endif 521 522 /* 523 * Discard setgid privileges if not the running kernel so that bad 524 * guys can't print interesting stuff from kernel memory. 525 */ 526 live = (nlistf == NULL && memf == NULL); 527 if (!live) 528 setgid(getgid()); 529 530 if (xflag && Tflag) 531 errx(1, "-x and -T are incompatible, pick one."); 532 533 if (Bflag) { 534 if (!live) 535 usage(); 536 bpf_stats(interface); 537 exit(0); 538 } 539 if (mflag) { 540 if (!live) { 541 if (kread(0, NULL, 0) == 0) 542 mbpr(kvmd, nl[N_SFSTAT].n_value); 543 } else 544 mbpr(NULL, 0); 545 exit(0); 546 } 547 if (Qflag) { 548 if (!live) { 549 if (kread(0, NULL, 0) == 0) 550 netisr_stats(kvmd); 551 } else 552 netisr_stats(NULL); 553 exit(0); 554 } 555 #if 0 556 /* 557 * Keep file descriptors open to avoid overhead 558 * of open/close on each call to get* routines. 559 */ 560 sethostent(1); 561 setnetent(1); 562 #else 563 /* 564 * This does not make sense any more with DNS being default over 565 * the files. Doing a setXXXXent(1) causes a tcp connection to be 566 * used for the queries, which is slower. 567 */ 568 #endif 569 if (iflag && !sflag) { 570 intpr(interval, NULL, af); 571 exit(0); 572 } 573 if (rflag) { 574 if (sflag) { 575 rt_stats(); 576 flowtable_stats(); 577 } else 578 routepr(fib, af); 579 exit(0); 580 } 581 582 if (gflag) { 583 if (sflag) { 584 if (af == AF_INET || af == AF_UNSPEC) 585 mrt_stats(); 586 #ifdef INET6 587 if (af == AF_INET6 || af == AF_UNSPEC) 588 mrt6_stats(); 589 #endif 590 } else { 591 if (af == AF_INET || af == AF_UNSPEC) 592 mroutepr(); 593 #ifdef INET6 594 if (af == AF_INET6 || af == AF_UNSPEC) 595 mroute6pr(); 596 #endif 597 } 598 exit(0); 599 } 600 601 /* Load all necessary kvm symbols */ 602 kresolve_list(nl); 603 604 if (tp) { 605 printproto(tp, tp->pr_name); 606 exit(0); 607 } 608 if (af == AF_INET || af == AF_UNSPEC) 609 for (tp = protox; tp->pr_name; tp++) 610 printproto(tp, tp->pr_name); 611 #ifdef INET6 612 if (af == AF_INET6 || af == AF_UNSPEC) 613 for (tp = ip6protox; tp->pr_name; tp++) 614 printproto(tp, tp->pr_name); 615 #endif /*INET6*/ 616 #ifdef IPSEC 617 if (af == PF_KEY || af == AF_UNSPEC) 618 for (tp = pfkeyprotox; tp->pr_name; tp++) 619 printproto(tp, tp->pr_name); 620 #endif /*IPSEC*/ 621 #ifdef IPX 622 if (af == AF_IPX || af == AF_UNSPEC) { 623 for (tp = ipxprotox; tp->pr_name; tp++) 624 printproto(tp, tp->pr_name); 625 } 626 #endif /* IPX */ 627 if (af == AF_APPLETALK || af == AF_UNSPEC) 628 for (tp = atalkprotox; tp->pr_name; tp++) 629 printproto(tp, tp->pr_name); 630 #ifdef NETGRAPH 631 if (af == AF_NETGRAPH || af == AF_UNSPEC) 632 for (tp = netgraphprotox; tp->pr_name; tp++) 633 printproto(tp, tp->pr_name); 634 #endif /* NETGRAPH */ 635 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 636 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 637 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 638 nl[N_UNP_SPHEAD].n_value); 639 exit(0); 640 } 641 642 /* 643 * Print out protocol statistics or control blocks (per sflag). 644 * If the interface was not specifically requested, and the symbol 645 * is not in the namelist, ignore this one. 646 */ 647 static void 648 printproto(struct protox *tp, const char *name) 649 { 650 void (*pr)(u_long, const char *, int, int); 651 u_long off; 652 653 if (sflag) { 654 if (iflag) { 655 if (tp->pr_istats) 656 intpr(interval, tp->pr_istats, af); 657 else if (pflag) 658 printf("%s: no per-interface stats routine\n", 659 tp->pr_name); 660 return; 661 } else { 662 pr = tp->pr_stats; 663 if (!pr) { 664 if (pflag) 665 printf("%s: no stats routine\n", 666 tp->pr_name); 667 return; 668 } 669 if (tp->pr_usesysctl && live) 670 off = 0; 671 else if (tp->pr_sindex < 0) { 672 if (pflag) 673 printf( 674 "%s: stats routine doesn't work on cores\n", 675 tp->pr_name); 676 return; 677 } else 678 off = nl[tp->pr_sindex].n_value; 679 } 680 } else { 681 pr = tp->pr_cblocks; 682 if (!pr) { 683 if (pflag) 684 printf("%s: no PCB routine\n", tp->pr_name); 685 return; 686 } 687 if (tp->pr_usesysctl && live) 688 off = 0; 689 else if (tp->pr_index < 0) { 690 if (pflag) 691 printf( 692 "%s: PCB routine doesn't work on cores\n", 693 tp->pr_name); 694 return; 695 } else 696 off = nl[tp->pr_index].n_value; 697 } 698 if (pr != NULL && (off || (live && tp->pr_usesysctl) || 699 af != AF_UNSPEC)) 700 (*pr)(off, name, af, tp->pr_protocol); 701 } 702 703 static int 704 kvmd_init(void) 705 { 706 char errbuf[_POSIX2_LINE_MAX]; 707 708 if (kvmd != NULL) 709 return (0); 710 711 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 712 setgid(getgid()); 713 714 if (kvmd == NULL) { 715 warnx("kvm not available: %s", errbuf); 716 return (-1); 717 } 718 719 return (0); 720 } 721 722 /* 723 * Resolve symbol list, return 0 on success. 724 */ 725 int 726 kresolve_list(struct nlist *_nl) 727 { 728 729 if ((kvmd == NULL) && (kvmd_init() != 0)) 730 return (-1); 731 732 if (_nl[0].n_type != 0) 733 return (0); 734 735 if (kvm_nlist(kvmd, _nl) < 0) { 736 if (nlistf) 737 errx(1, "%s: kvm_nlist: %s", nlistf, 738 kvm_geterr(kvmd)); 739 else 740 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 741 } 742 743 return (0); 744 } 745 746 /* 747 * Read kernel memory, return 0 on success. 748 */ 749 int 750 kread(u_long addr, void *buf, size_t size) 751 { 752 753 if (kvmd_init() < 0) 754 return (-1); 755 756 if (!buf) 757 return (0); 758 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 759 warnx("%s", kvm_geterr(kvmd)); 760 return (-1); 761 } 762 return (0); 763 } 764 765 /* 766 * Read an array of N counters in kernel memory into array of N uint64_t's. 767 */ 768 int 769 kread_counters(u_long addr, void *buf, size_t size) 770 { 771 uint64_t *c = buf; 772 773 if (kvmd_init() < 0) 774 return (-1); 775 776 if (kread(addr, buf, size) < 0) 777 return (-1); 778 779 while (size != 0) { 780 *c = kvm_counter_u64_fetch(kvmd, *c); 781 size -= sizeof(*c); 782 c++; 783 } 784 return (0); 785 } 786 787 const char * 788 plural(uintmax_t n) 789 { 790 return (n != 1 ? "s" : ""); 791 } 792 793 const char * 794 plurales(uintmax_t n) 795 { 796 return (n != 1 ? "es" : ""); 797 } 798 799 const char * 800 pluralies(uintmax_t n) 801 { 802 return (n != 1 ? "ies" : "y"); 803 } 804 805 /* 806 * Find the protox for the given "well-known" name. 807 */ 808 static struct protox * 809 knownname(const char *name) 810 { 811 struct protox **tpp, *tp; 812 813 for (tpp = protoprotox; *tpp; tpp++) 814 for (tp = *tpp; tp->pr_name; tp++) 815 if (strcmp(tp->pr_name, name) == 0) 816 return (tp); 817 return (NULL); 818 } 819 820 /* 821 * Find the protox corresponding to name. 822 */ 823 static struct protox * 824 name2protox(const char *name) 825 { 826 struct protox *tp; 827 char **alias; /* alias from p->aliases */ 828 struct protoent *p; 829 830 /* 831 * Try to find the name in the list of "well-known" names. If that 832 * fails, check if name is an alias for an Internet protocol. 833 */ 834 if ((tp = knownname(name)) != NULL) 835 return (tp); 836 837 setprotoent(1); /* make protocol lookup cheaper */ 838 while ((p = getprotoent()) != NULL) { 839 /* assert: name not same as p->name */ 840 for (alias = p->p_aliases; *alias; alias++) 841 if (strcmp(name, *alias) == 0) { 842 endprotoent(); 843 return (knownname(p->p_name)); 844 } 845 } 846 endprotoent(); 847 return (NULL); 848 } 849 850 static void 851 usage(void) 852 { 853 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 854 "usage: netstat [-46AaLnSTWx] [-f protocol_family | -p protocol]\n" 855 " [-M core] [-N system]", 856 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" 857 " [-M core] [-N system]", 858 " netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]", 859 " netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n" 860 " [-M core] [-N system]", 861 " netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n" 862 " [-M core] [-N system]", 863 " netstat -m [-M core] [-N system]", 864 " netstat -B [-I interface]", 865 " netstat -r [-46AanW] [-f address_family] [-M core] [-N system]", 866 " netstat -rs [-s] [-M core] [-N system]", 867 " netstat -g [-46W] [-f address_family] [-M core] [-N system]", 868 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", 869 " netstat -Q"); 870 exit(1); 871 } 872