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