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