main.c revision 262743
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 31char 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 38static 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: stable/10/usr.bin/netstat/main.c 262743 2014-03-04 15:14:47Z glebius $"); 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 72static 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 170struct 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 { -1, N_PFSYNCSTAT, 1, NULL, 218 pfsync_stats, NULL, "pfsync", 1, 0 }, 219 { -1, N_ARPSTAT, 1, NULL, 220 arp_stats, NULL, "arp", 1, 0 }, 221 { -1, -1, 0, NULL, 222 NULL, NULL, NULL, 0, 0 } 223}; 224 225#ifdef INET6 226struct protox ip6protox[] = { 227 { N_TCBINFO, N_TCPSTAT, 1, protopr, 228 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 229 { N_UDBINFO, N_UDPSTAT, 1, protopr, 230 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 231 { N_RIPCBINFO, N_IP6STAT, 1, protopr, 232 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 233 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, 234 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 235#ifdef SDP 236 { -1, -1, 1, protopr, 237 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 238#endif 239#ifdef IPSEC 240 { -1, N_IPSEC6STAT, 1, NULL, 241 ipsec_stats, NULL, "ipsec6", 0, 0 }, 242#endif 243#ifdef notyet 244 { -1, N_PIM6STAT, 1, NULL, 245 pim6_stats, NULL, "pim6", 1, 0 }, 246#endif 247 { -1, N_RIP6STAT, 1, NULL, 248 rip6_stats, NULL, "rip6", 1, 0 }, 249 { -1, -1, 0, NULL, 250 NULL, NULL, NULL, 0, 0 } 251}; 252#endif /*INET6*/ 253 254#ifdef IPSEC 255struct protox pfkeyprotox[] = { 256 { -1, N_PFKEYSTAT, 1, NULL, 257 pfkey_stats, NULL, "pfkey", 0, 0 }, 258 { -1, -1, 0, NULL, 259 NULL, NULL, NULL, 0, 0 } 260}; 261#endif 262 263struct protox atalkprotox[] = { 264 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 265 ddp_stats, NULL, "ddp", 0, 0 }, 266 { -1, -1, 0, NULL, 267 NULL, NULL, NULL, 0, 0 } 268}; 269#ifdef NETGRAPH 270struct protox netgraphprotox[] = { 271 { N_NGSOCKS, -1, 1, netgraphprotopr, 272 NULL, NULL, "ctrl", 0, 0 }, 273 { N_NGSOCKS, -1, 1, netgraphprotopr, 274 NULL, NULL, "data", 0, 0 }, 275 { -1, -1, 0, NULL, 276 NULL, NULL, NULL, 0, 0 } 277}; 278#endif 279#ifdef IPX 280struct protox ipxprotox[] = { 281 { N_IPX, N_IPXSTAT, 1, ipxprotopr, 282 ipx_stats, NULL, "ipx", 0, 0 }, 283 { N_IPX, N_SPXSTAT, 1, ipxprotopr, 284 spx_stats, NULL, "spx", 0, 0 }, 285 { -1, -1, 0, NULL, 286 NULL, NULL, 0, 0, 0 } 287}; 288#endif 289 290struct protox *protoprotox[] = { 291 protox, 292#ifdef INET6 293 ip6protox, 294#endif 295#ifdef IPSEC 296 pfkeyprotox, 297#endif 298#ifdef IPX 299 ipxprotox, 300#endif 301 atalkprotox, NULL }; 302 303static void printproto(struct protox *, const char *); 304static void usage(void); 305static struct protox *name2protox(const char *); 306static struct protox *knownname(const char *); 307 308static kvm_t *kvmd; 309static char *nlistf = NULL, *memf = NULL; 310 311int Aflag; /* show addresses of protocol control block */ 312int aflag; /* show all sockets (including servers) */ 313int Bflag; /* show information about bpf consumers */ 314int bflag; /* show i/f total bytes in/out */ 315int dflag; /* show i/f dropped packets */ 316int gflag; /* show group (multicast) routing or stats */ 317int hflag; /* show counters in human readable format */ 318int iflag; /* show interfaces */ 319int Lflag; /* show size of listen queues */ 320int mflag; /* show memory stats */ 321int noutputs = 0; /* how much outputs before we exit */ 322int numeric_addr; /* show addresses numerically */ 323int numeric_port; /* show ports numerically */ 324static int pflag; /* show given protocol */ 325int Qflag; /* show netisr information */ 326int rflag; /* show routing tables (or routing stats) */ 327int sflag; /* show protocol statistics */ 328int Wflag; /* wide display */ 329int Tflag; /* TCP Information */ 330int xflag; /* extra information, includes all socket buffer info */ 331int zflag; /* zero stats */ 332 333int interval; /* repeat interval for i/f stats */ 334 335char *interface; /* desired i/f for stats, or NULL for all i/fs */ 336int unit; /* unit number for above */ 337 338int af; /* address family */ 339int live; /* true if we are examining a live system */ 340 341int 342main(int argc, char *argv[]) 343{ 344 struct protox *tp = NULL; /* for printing cblocks & stats */ 345 int ch; 346 int fib = -1; 347 char *endptr; 348 349 af = AF_UNSPEC; 350 351 while ((ch = getopt(argc, argv, "AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz")) 352 != -1) 353 switch(ch) { 354 case 'A': 355 Aflag = 1; 356 break; 357 case 'a': 358 aflag = 1; 359 break; 360 case 'B': 361 Bflag = 1; 362 break; 363 case 'b': 364 bflag = 1; 365 break; 366 case 'd': 367 dflag = 1; 368 break; 369 case 'F': 370 fib = strtol(optarg, &endptr, 0); 371 if (*endptr != '\0' || 372 (fib == 0 && (errno == EINVAL || errno == ERANGE))) 373 errx(1, "%s: invalid fib", optarg); 374 break; 375 case 'f': 376 if (strcmp(optarg, "ipx") == 0) 377 af = AF_IPX; 378 else if (strcmp(optarg, "inet") == 0) 379 af = AF_INET; 380#ifdef INET6 381 else if (strcmp(optarg, "inet6") == 0) 382 af = AF_INET6; 383#endif 384#ifdef IPSEC 385 else if (strcmp(optarg, "pfkey") == 0) 386 af = PF_KEY; 387#endif 388 else if (strcmp(optarg, "unix") == 0) 389 af = AF_UNIX; 390 else if (strcmp(optarg, "atalk") == 0) 391 af = AF_APPLETALK; 392#ifdef NETGRAPH 393 else if (strcmp(optarg, "ng") == 0 394 || strcmp(optarg, "netgraph") == 0) 395 af = AF_NETGRAPH; 396#endif 397 else if (strcmp(optarg, "link") == 0) 398 af = AF_LINK; 399 else { 400 errx(1, "%s: unknown address family", optarg); 401 } 402 break; 403 case 'g': 404 gflag = 1; 405 break; 406 case 'h': 407 hflag = 1; 408 break; 409 case 'I': { 410 char *cp; 411 412 iflag = 1; 413 for (cp = interface = optarg; isalpha(*cp); cp++) 414 continue; 415 unit = atoi(cp); 416 break; 417 } 418 case 'i': 419 iflag = 1; 420 break; 421 case 'L': 422 Lflag = 1; 423 break; 424 case 'M': 425 memf = optarg; 426 break; 427 case 'm': 428 mflag = 1; 429 break; 430 case 'N': 431 nlistf = optarg; 432 break; 433 case 'n': 434 numeric_addr = numeric_port = 1; 435 break; 436 case 'p': 437 if ((tp = name2protox(optarg)) == NULL) { 438 errx(1, 439 "%s: unknown or uninstrumented protocol", 440 optarg); 441 } 442 pflag = 1; 443 break; 444 case 'Q': 445 Qflag = 1; 446 break; 447 case 'q': 448 noutputs = atoi(optarg); 449 if (noutputs != 0) 450 noutputs++; 451 break; 452 case 'r': 453 rflag = 1; 454 break; 455 case 's': 456 ++sflag; 457 break; 458 case 'S': 459 numeric_addr = 1; 460 break; 461 case 'u': 462 af = AF_UNIX; 463 break; 464 case 'W': 465 case 'l': 466 Wflag = 1; 467 break; 468 case 'w': 469 interval = atoi(optarg); 470 iflag = 1; 471 break; 472 case 'T': 473 Tflag = 1; 474 break; 475 case 'x': 476 xflag = 1; 477 break; 478 case 'z': 479 zflag = 1; 480 break; 481 case '?': 482 default: 483 usage(); 484 } 485 argv += optind; 486 argc -= optind; 487 488#define BACKWARD_COMPATIBILITY 489#ifdef BACKWARD_COMPATIBILITY 490 if (*argv) { 491 if (isdigit(**argv)) { 492 interval = atoi(*argv); 493 if (interval <= 0) 494 usage(); 495 ++argv; 496 iflag = 1; 497 } 498 if (*argv) { 499 nlistf = *argv; 500 if (*++argv) 501 memf = *argv; 502 } 503 } 504#endif 505 506 /* 507 * Discard setgid privileges if not the running kernel so that bad 508 * guys can't print interesting stuff from kernel memory. 509 */ 510 live = (nlistf == NULL && memf == NULL); 511 if (!live) 512 setgid(getgid()); 513 514 if (xflag && Tflag) 515 errx(1, "-x and -T are incompatible, pick one."); 516 517 if (Bflag) { 518 if (!live) 519 usage(); 520 bpf_stats(interface); 521 exit(0); 522 } 523 if (mflag) { 524 if (!live) { 525 if (kread(0, NULL, 0) == 0) 526 mbpr(kvmd, nl[N_SFSTAT].n_value); 527 } else 528 mbpr(NULL, 0); 529 exit(0); 530 } 531 if (Qflag) { 532 if (!live) { 533 if (kread(0, NULL, 0) == 0) 534 netisr_stats(kvmd); 535 } else 536 netisr_stats(NULL); 537 exit(0); 538 } 539#if 0 540 /* 541 * Keep file descriptors open to avoid overhead 542 * of open/close on each call to get* routines. 543 */ 544 sethostent(1); 545 setnetent(1); 546#else 547 /* 548 * This does not make sense any more with DNS being default over 549 * the files. Doing a setXXXXent(1) causes a tcp connection to be 550 * used for the queries, which is slower. 551 */ 552#endif 553 kread(0, NULL, 0); 554 if (iflag && !sflag) { 555 intpr(interval, NULL); 556 exit(0); 557 } 558 if (rflag) { 559 if (sflag) { 560 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value); 561 flowtable_stats(); 562 } else 563 routepr(nl[N_RTREE].n_value, fib); 564 exit(0); 565 } 566 if (gflag) { 567 if (sflag) { 568 if (af == AF_INET || af == AF_UNSPEC) 569 mrt_stats(nl[N_MRTSTAT].n_value); 570#ifdef INET6 571 if (af == AF_INET6 || af == AF_UNSPEC) 572 mrt6_stats(nl[N_MRT6STAT].n_value); 573#endif 574 } else { 575 if (af == AF_INET || af == AF_UNSPEC) 576 mroutepr(nl[N_MFCHASHTBL].n_value, 577 nl[N_MFCTABLESIZE].n_value, 578 nl[N_VIFTABLE].n_value); 579#ifdef INET6 580 if (af == AF_INET6 || af == AF_UNSPEC) 581 mroute6pr(nl[N_MF6CTABLE].n_value, 582 nl[N_MIF6TABLE].n_value); 583#endif 584 } 585 exit(0); 586 } 587 588 if (tp) { 589 printproto(tp, tp->pr_name); 590 exit(0); 591 } 592 if (af == AF_INET || af == AF_UNSPEC) 593 for (tp = protox; tp->pr_name; tp++) 594 printproto(tp, tp->pr_name); 595#ifdef INET6 596 if (af == AF_INET6 || af == AF_UNSPEC) 597 for (tp = ip6protox; tp->pr_name; tp++) 598 printproto(tp, tp->pr_name); 599#endif /*INET6*/ 600#ifdef IPSEC 601 if (af == PF_KEY || af == AF_UNSPEC) 602 for (tp = pfkeyprotox; tp->pr_name; tp++) 603 printproto(tp, tp->pr_name); 604#endif /*IPSEC*/ 605#ifdef IPX 606 if (af == AF_IPX || af == AF_UNSPEC) { 607 for (tp = ipxprotox; tp->pr_name; tp++) 608 printproto(tp, tp->pr_name); 609 } 610#endif /* IPX */ 611 if (af == AF_APPLETALK || af == AF_UNSPEC) 612 for (tp = atalkprotox; tp->pr_name; tp++) 613 printproto(tp, tp->pr_name); 614#ifdef NETGRAPH 615 if (af == AF_NETGRAPH || af == AF_UNSPEC) 616 for (tp = netgraphprotox; tp->pr_name; tp++) 617 printproto(tp, tp->pr_name); 618#endif /* NETGRAPH */ 619 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 620 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 621 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 622 nl[N_UNP_SPHEAD].n_value); 623 exit(0); 624} 625 626/* 627 * Print out protocol statistics or control blocks (per sflag). 628 * If the interface was not specifically requested, and the symbol 629 * is not in the namelist, ignore this one. 630 */ 631static void 632printproto(struct protox *tp, const char *name) 633{ 634 void (*pr)(u_long, const char *, int, int); 635 u_long off; 636 637 if (sflag) { 638 if (iflag) { 639 if (tp->pr_istats) 640 intpr(interval, tp->pr_istats); 641 else if (pflag) 642 printf("%s: no per-interface stats routine\n", 643 tp->pr_name); 644 return; 645 } else { 646 pr = tp->pr_stats; 647 if (!pr) { 648 if (pflag) 649 printf("%s: no stats routine\n", 650 tp->pr_name); 651 return; 652 } 653 if (tp->pr_usesysctl && live) 654 off = 0; 655 else if (tp->pr_sindex < 0) { 656 if (pflag) 657 printf( 658 "%s: stats routine doesn't work on cores\n", 659 tp->pr_name); 660 return; 661 } else 662 off = nl[tp->pr_sindex].n_value; 663 } 664 } else { 665 pr = tp->pr_cblocks; 666 if (!pr) { 667 if (pflag) 668 printf("%s: no PCB routine\n", tp->pr_name); 669 return; 670 } 671 if (tp->pr_usesysctl && live) 672 off = 0; 673 else if (tp->pr_index < 0) { 674 if (pflag) 675 printf( 676 "%s: PCB routine doesn't work on cores\n", 677 tp->pr_name); 678 return; 679 } else 680 off = nl[tp->pr_index].n_value; 681 } 682 if (pr != NULL && (off || (live && tp->pr_usesysctl) || 683 af != AF_UNSPEC)) 684 (*pr)(off, name, af, tp->pr_protocol); 685} 686 687static int 688kvmd_init(void) 689{ 690 char errbuf[_POSIX2_LINE_MAX]; 691 692 if (kvmd != NULL) 693 return (0); 694 695 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 696 setgid(getgid()); 697 698 if (kvmd == NULL) { 699 warnx("kvm not available: %s", errbuf); 700 return (-1); 701 } 702 703 if (kvm_nlist(kvmd, nl) < 0) { 704 if (nlistf) 705 errx(1, "%s: kvm_nlist: %s", nlistf, 706 kvm_geterr(kvmd)); 707 else 708 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 709 } 710 711 if (nl[0].n_type == 0) { 712 if (nlistf) 713 errx(1, "%s: no namelist", nlistf); 714 else 715 errx(1, "no namelist"); 716 } 717 718 return (0); 719} 720 721/* 722 * Read kernel memory, return 0 on success. 723 */ 724int 725kread(u_long addr, void *buf, size_t size) 726{ 727 728 if (kvmd_init() < 0) 729 return (-1); 730 731 if (!buf) 732 return (0); 733 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 734 warnx("%s", kvm_geterr(kvmd)); 735 return (-1); 736 } 737 return (0); 738} 739 740/* 741 * Read an array of N counters in kernel memory into array of N uint64_t's. 742 */ 743int 744kread_counters(u_long addr, void *buf, size_t size) 745{ 746 uint64_t *c = buf; 747 748 if (kvmd_init() < 0) 749 return (-1); 750 751 if (kread(addr, buf, size) < 0) 752 return (-1); 753 754 while (size != 0) { 755 *c = kvm_counter_u64_fetch(kvmd, *c); 756 size -= sizeof(*c); 757 c++; 758 } 759 return (0); 760} 761 762const char * 763plural(uintmax_t n) 764{ 765 return (n != 1 ? "s" : ""); 766} 767 768const char * 769plurales(uintmax_t n) 770{ 771 return (n != 1 ? "es" : ""); 772} 773 774const char * 775pluralies(uintmax_t n) 776{ 777 return (n != 1 ? "ies" : "y"); 778} 779 780/* 781 * Find the protox for the given "well-known" name. 782 */ 783static struct protox * 784knownname(const char *name) 785{ 786 struct protox **tpp, *tp; 787 788 for (tpp = protoprotox; *tpp; tpp++) 789 for (tp = *tpp; tp->pr_name; tp++) 790 if (strcmp(tp->pr_name, name) == 0) 791 return (tp); 792 return (NULL); 793} 794 795/* 796 * Find the protox corresponding to name. 797 */ 798static struct protox * 799name2protox(const char *name) 800{ 801 struct protox *tp; 802 char **alias; /* alias from p->aliases */ 803 struct protoent *p; 804 805 /* 806 * Try to find the name in the list of "well-known" names. If that 807 * fails, check if name is an alias for an Internet protocol. 808 */ 809 if ((tp = knownname(name)) != NULL) 810 return (tp); 811 812 setprotoent(1); /* make protocol lookup cheaper */ 813 while ((p = getprotoent()) != NULL) { 814 /* assert: name not same as p->name */ 815 for (alias = p->p_aliases; *alias; alias++) 816 if (strcmp(name, *alias) == 0) { 817 endprotoent(); 818 return (knownname(p->p_name)); 819 } 820 } 821 endprotoent(); 822 return (NULL); 823} 824 825static void 826usage(void) 827{ 828 (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", 829"usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n" 830" [-M core] [-N system]", 831" netstat -i | -I interface [-abdhnW] [-f address_family]\n" 832" [-M core] [-N system]", 833" netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]", 834" netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n" 835" [-M core] [-N system]", 836" netstat -i | -I interface -s [-f protocol_family | -p protocol]\n" 837" [-M core] [-N system]", 838" netstat -m [-M core] [-N system]", 839" netstat -B [-I interface]", 840" netstat -r [-AanW] [-f address_family] [-M core] [-N system]", 841" netstat -rs [-s] [-M core] [-N system]", 842" netstat -g [-W] [-f address_family] [-M core] [-N system]", 843" netstat -gs [-s] [-f address_family] [-M core] [-N system]", 844" netstat -Q"); 845 exit(1); 846} 847