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