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