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