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 #include <sys/socketvar.h> 54 55 #include <netinet/in.h> 56 57 #ifdef NETGRAPH 58 #include <netgraph/ng_socket.h> 59 #endif 60 61 #include <ctype.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <kvm.h> 65 #include <limits.h> 66 #include <netdb.h> 67 #include <nlist.h> 68 #include <paths.h> 69 #include <stdint.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <unistd.h> 74 #include "netstat.h" 75 76 static struct nlist nl[] = { 77 #define N_IFNET 0 78 { .n_name = "_ifnet" }, 79 #define N_RTSTAT 1 80 { .n_name = "_rtstat" }, 81 #define N_RTREE 2 82 { .n_name = "_rt_tables"}, 83 #define N_MRTSTAT 3 84 { .n_name = "_mrtstat" }, 85 #define N_MFCHASHTBL 4 86 { .n_name = "_mfchashtbl" }, 87 #define N_VIFTABLE 5 88 { .n_name = "_viftable" }, 89 #define N_IPX 6 90 { .n_name = "_ipxpcb_list"}, 91 #define N_IPXSTAT 7 92 { .n_name = "_ipxstat"}, 93 #define N_SPXSTAT 8 94 { .n_name = "_spx_istat"}, 95 #define N_DDPSTAT 9 96 { .n_name = "_ddpstat"}, 97 #define N_DDPCB 10 98 { .n_name = "_ddpcb"}, 99 #define N_NGSOCKS 11 100 { .n_name = "_ngsocklist"}, 101 #define N_IP6STAT 12 102 { .n_name = "_ip6stat" }, 103 #define N_ICMP6STAT 13 104 { .n_name = "_icmp6stat" }, 105 #define N_IPSECSTAT 14 106 { .n_name = "_ipsec4stat" }, 107 #define N_IPSEC6STAT 15 108 { .n_name = "_ipsec6stat" }, 109 #define N_PIM6STAT 16 110 { .n_name = "_pim6stat" }, 111 #define N_MRT6STAT 17 112 { .n_name = "_mrt6stat" }, 113 #define N_MF6CTABLE 18 114 { .n_name = "_mf6ctable" }, 115 #define N_MIF6TABLE 19 116 { .n_name = "_mif6table" }, 117 #define N_PFKEYSTAT 20 118 { .n_name = "_pfkeystat" }, 119 #define N_MBSTAT 21 120 { .n_name = "_mbstat" }, 121 #define N_MBTYPES 22 122 { .n_name = "_mbtypes" }, 123 #define N_NMBCLUSTERS 23 124 { .n_name = "_nmbclusters" }, 125 #define N_NMBUFS 24 126 { .n_name = "_nmbufs" }, 127 #define N_MBHI 25 128 { .n_name = "_mbuf_hiwm" }, 129 #define N_CLHI 26 130 { .n_name = "_clust_hiwm" }, 131 #define N_NCPUS 27 132 { .n_name = "_smp_cpus" }, 133 #define N_PAGESZ 28 134 { .n_name = "_pagesize" }, 135 #define N_MBPSTAT 29 136 { .n_name = "_mb_statpcpu" }, 137 #define N_RTTRASH 30 138 { .n_name = "_rttrash" }, 139 #define N_MBLO 31 140 { .n_name = "_mbuf_lowm" }, 141 #define N_CLLO 32 142 { .n_name = "_clust_lowm" }, 143 #define N_CARPSTAT 33 144 { .n_name = "_carpstats" }, 145 #define N_PFSYNCSTAT 34 146 { .n_name = "_pfsyncstats" }, 147 #define N_AHSTAT 35 148 { .n_name = "_ahstat" }, 149 #define N_ESPSTAT 36 150 { .n_name = "_espstat" }, 151 #define N_IPCOMPSTAT 37 152 { .n_name = "_ipcompstat" }, 153 #define N_TCPSTAT 38 154 { .n_name = "_tcpstat" }, 155 #define N_UDPSTAT 39 156 { .n_name = "_udpstat" }, 157 #define N_IPSTAT 40 158 { .n_name = "_ipstat" }, 159 #define N_ICMPSTAT 41 160 { .n_name = "_icmpstat" }, 161 #define N_IGMPSTAT 42 162 { .n_name = "_igmpstat" }, 163 #define N_PIMSTAT 43 164 { .n_name = "_pimstat" }, 165 #define N_TCBINFO 44 166 { .n_name = "_tcbinfo" }, 167 #define N_UDBINFO 45 168 { .n_name = "_udbinfo" }, 169 #define N_DIVCBINFO 46 170 { .n_name = "_divcbinfo" }, 171 #define N_RIPCBINFO 47 172 { .n_name = "_ripcbinfo" }, 173 #define N_UNP_COUNT 48 174 { .n_name = "_unp_count" }, 175 #define N_UNP_GENCNT 49 176 { .n_name = "_unp_gencnt" }, 177 #define N_UNP_DHEAD 50 178 { .n_name = "_unp_dhead" }, 179 #define N_UNP_SHEAD 51 180 { .n_name = "_unp_shead" }, 181 #define N_RIP6STAT 52 182 { .n_name = "_rip6stat" }, 183 #define N_SCTPSTAT 53 184 { .n_name = "_sctpstat" }, 185 #define N_MFCTABLESIZE 54 186 { .n_name = "_mfctablesize" }, 187 #define N_ARPSTAT 55 188 { .n_name = "_arpstat" }, 189 #define N_UNP_SPHEAD 56 190 { .n_name = "unp_sphead" }, 191 { .n_name = NULL }, 192 }; 193 194 struct protox { 195 int pr_index; /* index into nlist of cb head */ 196 int pr_sindex; /* index into nlist of stat block */ 197 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 198 void (*pr_cblocks)(u_long, const char *, int, int); 199 /* control blocks printing routine */ 200 void (*pr_stats)(u_long, const char *, int, int); 201 /* statistics printing routine */ 202 void (*pr_istats)(char *); /* per/if statistics printing routine */ 203 const char *pr_name; /* well-known name */ 204 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 205 int pr_protocol; 206 } protox[] = { 207 { N_TCBINFO, N_TCPSTAT, 1, protopr, 208 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 209 { N_UDBINFO, N_UDPSTAT, 1, protopr, 210 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 211 #ifdef SCTP 212 { -1, N_SCTPSTAT, 1, sctp_protopr, 213 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 214 #endif 215 { N_DIVCBINFO, -1, 1, protopr, 216 NULL, NULL, "divert", 1, IPPROTO_DIVERT }, 217 { N_RIPCBINFO, N_IPSTAT, 1, protopr, 218 ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 219 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr, 220 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 221 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr, 222 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 223 #ifdef IPSEC 224 { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */ 225 ipsec_stats, NULL, "ipsec", 0, 0}, 226 { -1, N_AHSTAT, 1, NULL, 227 ah_stats, NULL, "ah", 0, 0}, 228 { -1, N_ESPSTAT, 1, NULL, 229 esp_stats, NULL, "esp", 0, 0}, 230 { -1, N_IPCOMPSTAT, 1, NULL, 231 ipcomp_stats, NULL, "ipcomp", 0, 0}, 232 #endif 233 { N_RIPCBINFO, N_PIMSTAT, 1, protopr, 234 pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 235 { -1, N_CARPSTAT, 1, NULL, 236 carp_stats, NULL, "carp", 1, 0 }, 237 { -1, N_PFSYNCSTAT, 1, NULL, 238 pfsync_stats, NULL, "pfsync", 1, 0 }, 239 { -1, N_ARPSTAT, 1, NULL, 240 arp_stats, NULL, "arp", 1, 0 }, 241 { -1, -1, 0, NULL, 242 NULL, NULL, NULL, 0, 0 } 243 }; 244 245 #ifdef INET6 246 struct protox ip6protox[] = { 247 { N_TCBINFO, N_TCPSTAT, 1, protopr, 248 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 249 { N_UDBINFO, N_UDPSTAT, 1, protopr, 250 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 251 { N_RIPCBINFO, N_IP6STAT, 1, protopr, 252 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 253 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, 254 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 255 #ifdef IPSEC 256 { -1, N_IPSEC6STAT, 1, NULL, 257 ipsec_stats, NULL, "ipsec6", 0, 0 }, 258 #endif 259 #ifdef notyet 260 { -1, N_PIM6STAT, 1, NULL, 261 pim6_stats, NULL, "pim6", 1, 0 }, 262 #endif 263 { -1, N_RIP6STAT, 1, NULL, 264 rip6_stats, NULL, "rip6", 1, 0 }, 265 { -1, -1, 0, NULL, 266 NULL, NULL, NULL, 0, 0 } 267 }; 268 #endif /*INET6*/ 269 270 #ifdef IPSEC 271 struct protox pfkeyprotox[] = { 272 { -1, N_PFKEYSTAT, 1, NULL, 273 pfkey_stats, NULL, "pfkey", 0, 0 }, 274 { -1, -1, 0, NULL, 275 NULL, NULL, NULL, 0, 0 } 276 }; 277 #endif 278 279 struct protox atalkprotox[] = { 280 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 281 ddp_stats, NULL, "ddp", 0, 0 }, 282 { -1, -1, 0, NULL, 283 NULL, NULL, NULL, 0, 0 } 284 }; 285 #ifdef NETGRAPH 286 struct protox netgraphprotox[] = { 287 { N_NGSOCKS, -1, 1, netgraphprotopr, 288 NULL, NULL, "ctrl", 0, 0 }, 289 { N_NGSOCKS, -1, 1, netgraphprotopr, 290 NULL, NULL, "data", 0, 0 }, 291 { -1, -1, 0, NULL, 292 NULL, NULL, NULL, 0, 0 } 293 }; 294 #endif 295 #ifdef IPX 296 struct protox ipxprotox[] = { 297 { N_IPX, N_IPXSTAT, 1, ipxprotopr, 298 ipx_stats, NULL, "ipx", 0, 0 }, 299 { N_IPX, N_SPXSTAT, 1, ipxprotopr, 300 spx_stats, NULL, "spx", 0, 0 }, 301 { -1, -1, 0, NULL, 302 NULL, NULL, 0, 0, 0 } 303 }; 304 #endif 305 306 struct protox *protoprotox[] = { 307 protox, 308 #ifdef INET6 309 ip6protox, 310 #endif 311 #ifdef IPSEC 312 pfkeyprotox, 313 #endif 314 #ifdef IPX 315 ipxprotox, 316 #endif 317 atalkprotox, NULL }; 318 319 static void printproto(struct protox *, const char *); 320 static void usage(void); 321 static struct protox *name2protox(const char *); 322 static struct protox *knownname(const char *); 323 324 static kvm_t *kvmd; 325 static char *nlistf = NULL, *memf = NULL; 326 327 int Aflag; /* show addresses of protocol control block */ 328 int aflag; /* show all sockets (including servers) */ 329 int Bflag; /* show information about bpf consumers */ 330 int bflag; /* show i/f total bytes in/out */ 331 int dflag; /* show i/f dropped packets */ 332 int gflag; /* show group (multicast) routing or stats */ 333 int hflag; /* show counters in human readable format */ 334 int iflag; /* show interfaces */ 335 int Lflag; /* show size of listen queues */ 336 int mflag; /* show memory stats */ 337 int noutputs = 0; /* how much outputs before we exit */ 338 int numeric_addr; /* show addresses numerically */ 339 int numeric_port; /* show ports numerically */ 340 static int pflag; /* show given protocol */ 341 int Qflag; /* show netisr information */ 342 int rflag; /* show routing tables (or routing stats) */ 343 int sflag; /* show protocol statistics */ 344 int Wflag; /* wide display */ 345 int xflag; /* extra information, includes all socket buffer info */ 346 int zflag; /* zero stats */ 347 348 int interval; /* repeat interval for i/f stats */ 349 350 char *interface; /* desired i/f for stats, or NULL for all i/fs */ 351 int unit; /* unit number for above */ 352 353 int af; /* address family */ 354 int live; /* true if we are examining a live system */ 355 356 int 357 main(int argc, char *argv[]) 358 { 359 struct protox *tp = NULL; /* for printing cblocks & stats */ 360 int ch; 361 362 af = AF_UNSPEC; 363 364 while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:Qq:rSsuWw:xz")) 365 != -1) 366 switch(ch) { 367 case 'A': 368 Aflag = 1; 369 break; 370 case 'a': 371 aflag = 1; 372 break; 373 case 'B': 374 Bflag = 1; 375 break; 376 case 'b': 377 bflag = 1; 378 break; 379 case 'd': 380 dflag = 1; 381 break; 382 case 'f': 383 if (strcmp(optarg, "ipx") == 0) 384 af = AF_IPX; 385 else if (strcmp(optarg, "inet") == 0) 386 af = AF_INET; 387 #ifdef INET6 388 else if (strcmp(optarg, "inet6") == 0) 389 af = AF_INET6; 390 #endif 391 #ifdef IPSEC 392 else if (strcmp(optarg, "pfkey") == 0) 393 af = PF_KEY; 394 #endif 395 else if (strcmp(optarg, "unix") == 0) 396 af = AF_UNIX; 397 else if (strcmp(optarg, "atalk") == 0) 398 af = AF_APPLETALK; 399 #ifdef NETGRAPH 400 else if (strcmp(optarg, "ng") == 0 401 || strcmp(optarg, "netgraph") == 0) 402 af = AF_NETGRAPH; 403 #endif 404 else if (strcmp(optarg, "link") == 0) 405 af = AF_LINK; 406 else { 407 errx(1, "%s: unknown address family", optarg); 408 } 409 break; 410 case 'g': 411 gflag = 1; 412 break; 413 case 'h': 414 hflag = 1; 415 break; 416 case 'I': { 417 char *cp; 418 419 iflag = 1; 420 for (cp = interface = optarg; isalpha(*cp); cp++) 421 continue; 422 unit = atoi(cp); 423 break; 424 } 425 case 'i': 426 iflag = 1; 427 break; 428 case 'L': 429 Lflag = 1; 430 break; 431 case 'M': 432 memf = optarg; 433 break; 434 case 'm': 435 mflag = 1; 436 break; 437 case 'N': 438 nlistf = optarg; 439 break; 440 case 'n': 441 numeric_addr = numeric_port = 1; 442 break; 443 case 'p': 444 if ((tp = name2protox(optarg)) == NULL) { 445 errx(1, 446 "%s: unknown or uninstrumented protocol", 447 optarg); 448 } 449 pflag = 1; 450 break; 451 case 'Q': 452 Qflag = 1; 453 break; 454 case 'q': 455 noutputs = atoi(optarg); 456 if (noutputs != 0) 457 noutputs++; 458 break; 459 case 'r': 460 rflag = 1; 461 break; 462 case 's': 463 ++sflag; 464 break; 465 case 'S': 466 numeric_addr = 1; 467 break; 468 case 'u': 469 af = AF_UNIX; 470 break; 471 case 'W': 472 case 'l': 473 Wflag = 1; 474 break; 475 case 'w': 476 interval = atoi(optarg); 477 iflag = 1; 478 break; 479 case 'x': 480 xflag = 1; 481 break; 482 case 'z': 483 zflag = 1; 484 break; 485 case '?': 486 default: 487 usage(); 488 } 489 argv += optind; 490 argc -= optind; 491 492 #define BACKWARD_COMPATIBILITY 493 #ifdef BACKWARD_COMPATIBILITY 494 if (*argv) { 495 if (isdigit(**argv)) { 496 interval = atoi(*argv); 497 if (interval <= 0) 498 usage(); 499 ++argv; 500 iflag = 1; 501 } 502 if (*argv) { 503 nlistf = *argv; 504 if (*++argv) 505 memf = *argv; 506 } 507 } 508 #endif 509 510 /* 511 * Discard setgid privileges if not the running kernel so that bad 512 * guys can't print interesting stuff from kernel memory. 513 */ 514 live = (nlistf == NULL && memf == NULL); 515 if (!live) 516 setgid(getgid()); 517 518 if (Bflag) { 519 if (!live) 520 usage(); 521 bpf_stats(interface); 522 exit(0); 523 } 524 if (mflag) { 525 if (!live) { 526 if (kread(0, NULL, 0) == 0) 527 mbpr(kvmd, nl[N_MBSTAT].n_value); 528 } else 529 mbpr(NULL, 0); 530 exit(0); 531 } 532 if (Qflag) { 533 if (!live) { 534 if (kread(0, NULL, 0) == 0) 535 netisr_stats(kvmd); 536 } else 537 netisr_stats(NULL); 538 exit(0); 539 } 540 #if 0 541 /* 542 * Keep file descriptors open to avoid overhead 543 * of open/close on each call to get* routines. 544 */ 545 sethostent(1); 546 setnetent(1); 547 #else 548 /* 549 * This does not make sense any more with DNS being default over 550 * the files. Doing a setXXXXent(1) causes a tcp connection to be 551 * used for the queries, which is slower. 552 */ 553 #endif 554 kread(0, NULL, 0); 555 if (iflag && !sflag) { 556 intpr(interval, nl[N_IFNET].n_value, NULL); 557 exit(0); 558 } 559 if (rflag) { 560 if (sflag) 561 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value); 562 else 563 routepr(nl[N_RTREE].n_value); 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 */ 631 static void 632 printproto(tp, name) 633 struct protox *tp; 634 const char *name; 635 { 636 void (*pr)(u_long, const char *, int, int); 637 u_long off; 638 639 if (sflag) { 640 if (iflag) { 641 if (tp->pr_istats) 642 intpr(interval, nl[N_IFNET].n_value, 643 tp->pr_istats); 644 else if (pflag) 645 printf("%s: no per-interface stats routine\n", 646 tp->pr_name); 647 return; 648 } else { 649 pr = tp->pr_stats; 650 if (!pr) { 651 if (pflag) 652 printf("%s: no stats routine\n", 653 tp->pr_name); 654 return; 655 } 656 if (tp->pr_usesysctl && live) 657 off = 0; 658 else if (tp->pr_sindex < 0) { 659 if (pflag) 660 printf( 661 "%s: stats routine doesn't work on cores\n", 662 tp->pr_name); 663 return; 664 } else 665 off = nl[tp->pr_sindex].n_value; 666 } 667 } else { 668 pr = tp->pr_cblocks; 669 if (!pr) { 670 if (pflag) 671 printf("%s: no PCB routine\n", tp->pr_name); 672 return; 673 } 674 if (tp->pr_usesysctl && live) 675 off = 0; 676 else if (tp->pr_index < 0) { 677 if (pflag) 678 printf( 679 "%s: PCB routine doesn't work on cores\n", 680 tp->pr_name); 681 return; 682 } else 683 off = nl[tp->pr_index].n_value; 684 } 685 if (pr != NULL && (off || (live && tp->pr_usesysctl) || 686 af != AF_UNSPEC)) 687 (*pr)(off, name, af, tp->pr_protocol); 688 } 689 690 /* 691 * Read kernel memory, return 0 on success. 692 */ 693 int 694 kread(u_long addr, void *buf, size_t size) 695 { 696 char errbuf[_POSIX2_LINE_MAX]; 697 698 if (kvmd == NULL) { 699 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 700 setgid(getgid()); 701 if (kvmd != NULL) { 702 if (kvm_nlist(kvmd, nl) < 0) { 703 if (nlistf) 704 errx(1, "%s: kvm_nlist: %s", nlistf, 705 kvm_geterr(kvmd)); 706 else 707 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 708 } 709 710 if (nl[0].n_type == 0) { 711 if (nlistf) 712 errx(1, "%s: no namelist", nlistf); 713 else 714 errx(1, "no namelist"); 715 } 716 } else { 717 warnx("kvm not available: %s", errbuf); 718 return(-1); 719 } 720 } 721 if (!buf) 722 return (0); 723 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 724 warnx("%s", kvm_geterr(kvmd)); 725 return (-1); 726 } 727 return (0); 728 } 729 730 const char * 731 plural(uintmax_t n) 732 { 733 return (n != 1 ? "s" : ""); 734 } 735 736 const char * 737 plurales(uintmax_t n) 738 { 739 return (n != 1 ? "es" : ""); 740 } 741 742 const char * 743 pluralies(uintmax_t n) 744 { 745 return (n != 1 ? "ies" : "y"); 746 } 747 748 /* 749 * Find the protox for the given "well-known" name. 750 */ 751 static struct protox * 752 knownname(const char *name) 753 { 754 struct protox **tpp, *tp; 755 756 for (tpp = protoprotox; *tpp; tpp++) 757 for (tp = *tpp; tp->pr_name; tp++) 758 if (strcmp(tp->pr_name, name) == 0) 759 return (tp); 760 return (NULL); 761 } 762 763 /* 764 * Find the protox corresponding to name. 765 */ 766 static struct protox * 767 name2protox(const char *name) 768 { 769 struct protox *tp; 770 char **alias; /* alias from p->aliases */ 771 struct protoent *p; 772 773 /* 774 * Try to find the name in the list of "well-known" names. If that 775 * fails, check if name is an alias for an Internet protocol. 776 */ 777 if ((tp = knownname(name)) != NULL) 778 return (tp); 779 780 setprotoent(1); /* make protocol lookup cheaper */ 781 while ((p = getprotoent()) != NULL) { 782 /* assert: name not same as p->name */ 783 for (alias = p->p_aliases; *alias; alias++) 784 if (strcmp(name, *alias) == 0) { 785 endprotoent(); 786 return (knownname(p->p_name)); 787 } 788 } 789 endprotoent(); 790 return (NULL); 791 } 792 793 static void 794 usage(void) 795 { 796 (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", 797 "usage: netstat [-AaLnSWx] [-f protocol_family | -p protocol]\n" 798 " [-M core] [-N system]", 799 " netstat -i | -I interface [-abdhnW] [-f address_family]\n" 800 " [-M core] [-N system]", 801 " netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]", 802 " netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n" 803 " [-M core] [-N system]", 804 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n" 805 " [-M core] [-N system]", 806 " netstat -m [-M core] [-N system]", 807 " netstat -B [-I interface]", 808 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]", 809 " netstat -rs [-s] [-M core] [-N system]", 810 " netstat -g [-W] [-f address_family] [-M core] [-N system]", 811 " netstat -gs [-s] [-f address_family] [-M core] [-N system]", 812 " netstat -Q"); 813 exit(1); 814 } 815