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 #ifdef SCTP 87 { -1, N_SCTPSTAT, 1, sctp_protopr, 88 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 89 #endif 90 #ifdef SDP 91 { -1, -1, 1, protopr, 92 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 93 #endif 94 { -1 , -1, 1, protopr, 95 divert_stats, NULL, "divert", 1, 0 }, 96 { -1 , N_IPSTAT, 1, protopr, 97 ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 98 { -1 , N_ICMPSTAT, 1, protopr, 99 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 100 { -1 , N_IGMPSTAT, 1, protopr, 101 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 102 #ifdef IPSEC 103 { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */ 104 ipsec_stats, NULL, "ipsec", 1, 0}, 105 { -1, N_AHSTAT, 1, NULL, 106 ah_stats, NULL, "ah", 1, 0}, 107 { -1, N_ESPSTAT, 1, NULL, 108 esp_stats, NULL, "esp", 1, 0}, 109 { -1, N_IPCOMPSTAT, 1, NULL, 110 ipcomp_stats, NULL, "ipcomp", 1, 0}, 111 #endif 112 { -1 , N_PIMSTAT, 1, protopr, 113 pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 114 { -1, N_CARPSTATS, 1, NULL, 115 carp_stats, NULL, "carp", 1, 0 }, 116 #ifdef PF 117 { -1, N_PFSYNCSTATS, 1, NULL, 118 pfsync_stats, NULL, "pfsync", 1, 0 }, 119 { -1, N_PFLOWSTATS, 1, NULL, 120 pflow_stats, NULL, "pflow", 1, 0 }, 121 #endif 122 { -1, N_ARPSTAT, 1, NULL, 123 arp_stats, NULL, "arp", 1, 0 }, 124 { -1, -1, 0, NULL, 125 NULL, NULL, NULL, 0, 0 } 126 }; 127 128 #ifdef INET6 129 static struct protox ip6protox[] = { 130 { -1 , N_TCPSTAT, 1, protopr, 131 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 132 { -1 , N_UDPSTAT, 1, protopr, 133 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 134 { -1 , N_IP6STAT, 1, protopr, 135 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 136 { -1 , N_ICMP6STAT, 1, protopr, 137 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 138 #ifdef SDP 139 { -1, -1, 1, protopr, 140 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 141 #endif 142 #ifdef IPSEC 143 { -1, N_IPSEC6STAT, 1, NULL, 144 ipsec_stats, NULL, "ipsec6", 1, 0 }, 145 #endif 146 #ifdef notyet 147 { -1, N_PIM6STAT, 1, NULL, 148 pim6_stats, NULL, "pim6", 1, 0 }, 149 #endif 150 { -1, N_RIP6STAT, 1, NULL, 151 rip6_stats, NULL, "rip6", 1, 0 }, 152 { -1, -1, 0, NULL, 153 NULL, NULL, NULL, 0, 0 } 154 }; 155 #endif /*INET6*/ 156 157 #ifdef IPSEC 158 static struct protox pfkeyprotox[] = { 159 { -1, N_PFKEYSTAT, 1, NULL, 160 pfkey_stats, NULL, "pfkey", 0, 0 }, 161 { -1, -1, 0, NULL, 162 NULL, NULL, NULL, 0, 0 } 163 }; 164 #endif 165 166 #ifdef NETGRAPH 167 static struct protox netgraphprotox[] = { 168 { N_NGSOCKLIST, -1, 1, netgraphprotopr, 169 NULL, NULL, "ctrl", 0, 0 }, 170 { N_NGSOCKLIST, -1, 1, netgraphprotopr, 171 NULL, NULL, "data", 0, 0 }, 172 { -1, -1, 0, NULL, 173 NULL, NULL, NULL, 0, 0 } 174 }; 175 #endif 176 177 static struct protox *protoprotox[] = { 178 protox, 179 #ifdef INET6 180 ip6protox, 181 #endif 182 #ifdef IPSEC 183 pfkeyprotox, 184 #endif 185 NULL }; 186 187 static void printproto(struct protox *, const char *, bool *); 188 static void usage(void) __dead2; 189 static struct protox *name2protox(const char *); 190 static struct protox *knownname(const char *); 191 192 static int kresolve_list(struct nlist *_nl); 193 194 static kvm_t *kvmd; 195 static char *nlistf = NULL, *memf = NULL; 196 197 bool Aflag; /* show addresses of protocol control block */ 198 bool aflag; /* show all sockets (including servers) */ 199 static bool Bflag; /* show information about bpf consumers */ 200 bool bflag; /* show i/f total bytes in/out */ 201 bool cflag; /* show TCP congestion control stack */ 202 bool Cflag; /* show congestion control algo and vars */ 203 bool dflag; /* show i/f dropped packets */ 204 bool gflag; /* show group (multicast) routing or stats */ 205 bool hflag; /* show counters in human readable format */ 206 bool iflag; /* show interfaces */ 207 bool Lflag; /* show size of listen queues */ 208 bool mflag; /* show memory stats */ 209 int noutputs = 0; /* how much outputs before we exit */ 210 u_int numeric_addr = 0; /* show addresses numerically */ 211 bool numeric_port; /* show ports numerically */ 212 bool Oflag; /* show nhgrp objects*/ 213 bool oflag; /* show nexthop objects*/ 214 bool Pflag; /* show TCP log ID */ 215 static bool pflag; /* show given protocol */ 216 static bool Qflag; /* show netisr information */ 217 bool rflag; /* show routing tables (or routing stats) */ 218 bool Rflag; /* show flow / RSS statistics */ 219 int sflag; /* show protocol statistics */ 220 bool Wflag; /* wide display */ 221 bool Tflag; /* TCP Information */ 222 bool xflag; /* extra information, includes all socket buffer info */ 223 bool 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 #ifdef JAIL 230 char *jail_name; /* desired jail to operate in */ 231 #endif 232 233 static int af; /* address family */ 234 int live; /* true if we are examining a live system */ 235 236 int 237 main(int argc, char *argv[]) 238 { 239 struct protox *tp = NULL; /* for printing cblocks & stats */ 240 int ch; 241 int fib = -1; 242 char *endptr; 243 bool first = true; 244 #ifdef JAIL 245 int jid; 246 #endif 247 248 af = AF_UNSPEC; 249 250 argc = xo_parse_args(argc, argv); 251 if (argc < 0) 252 exit(EXIT_FAILURE); 253 254 while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:ij:LlM:mN:nOoPp:Qq:RrSTsuWw:xz")) 255 != -1) 256 switch(ch) { 257 case '4': 258 #ifdef INET 259 af = AF_INET; 260 #else 261 xo_errx(EX_UNAVAILABLE, "IPv4 support is not compiled in"); 262 #endif 263 break; 264 case '6': 265 #ifdef INET6 266 af = AF_INET6; 267 #else 268 xo_errx(EX_UNAVAILABLE, "IPv6 support is not compiled in"); 269 #endif 270 break; 271 case 'A': 272 Aflag = true; 273 break; 274 case 'a': 275 aflag = true; 276 break; 277 case 'B': 278 Bflag = true; 279 break; 280 case 'b': 281 bflag = true; 282 break; 283 case 'c': 284 cflag = true; 285 break; 286 case 'C': 287 Cflag = true; 288 break; 289 case 'd': 290 dflag = true; 291 break; 292 case 'F': 293 fib = strtol(optarg, &endptr, 0); 294 if (*endptr != '\0' || 295 (fib == 0 && (errno == EINVAL || errno == ERANGE))) 296 xo_errx(EX_DATAERR, "%s: invalid fib", optarg); 297 break; 298 case 'f': 299 if (strcmp(optarg, "inet") == 0) 300 af = AF_INET; 301 #ifdef INET6 302 else if (strcmp(optarg, "inet6") == 0) 303 af = AF_INET6; 304 #endif 305 #ifdef IPSEC 306 else if (strcmp(optarg, "pfkey") == 0) 307 af = PF_KEY; 308 #endif 309 else if (strcmp(optarg, "unix") == 0 || 310 strcmp(optarg, "local") == 0) 311 af = AF_UNIX; 312 #ifdef NETGRAPH 313 else if (strcmp(optarg, "ng") == 0 314 || strcmp(optarg, "netgraph") == 0) 315 af = AF_NETGRAPH; 316 #endif 317 else if (strcmp(optarg, "link") == 0) 318 af = AF_LINK; 319 else { 320 xo_errx(EX_DATAERR, "%s: unknown address family", 321 optarg); 322 } 323 break; 324 case 'g': 325 gflag = true; 326 break; 327 case 'h': 328 hflag = true; 329 break; 330 case 'I': { 331 char *cp; 332 333 iflag = true; 334 for (cp = interface = optarg; isalpha(*cp); cp++) 335 continue; 336 unit = atoi(cp); 337 break; 338 } 339 case 'i': 340 iflag = true; 341 break; 342 case 'j': 343 #ifdef JAIL 344 if (optarg == NULL) 345 usage(); 346 jail_name = optarg; 347 #else 348 xo_errx(EX_UNAVAILABLE, "Jail support is not compiled in"); 349 #endif 350 break; 351 case 'L': 352 Lflag = true; 353 break; 354 case 'M': 355 memf = optarg; 356 break; 357 case 'm': 358 mflag = true; 359 break; 360 case 'N': 361 nlistf = optarg; 362 break; 363 case 'n': 364 numeric_addr++; 365 numeric_port = true; 366 break; 367 case 'o': 368 oflag = true; 369 break; 370 case 'O': 371 Oflag = true; 372 break; 373 case 'P': 374 Pflag = true; 375 break; 376 case 'p': 377 if ((tp = name2protox(optarg)) == NULL) { 378 xo_errx(EX_DATAERR, "%s: unknown or uninstrumented " 379 "protocol", optarg); 380 } 381 pflag = true; 382 break; 383 case 'Q': 384 Qflag = true; 385 break; 386 case 'q': 387 noutputs = atoi(optarg); 388 if (noutputs != 0) 389 noutputs++; 390 break; 391 case 'r': 392 rflag = true; 393 break; 394 case 'R': 395 Rflag = true; 396 break; 397 case 's': 398 ++sflag; 399 break; 400 case 'S': 401 numeric_addr = 1; 402 break; 403 case 'u': 404 af = AF_UNIX; 405 break; 406 case 'W': 407 case 'l': 408 Wflag = true; 409 break; 410 case 'w': 411 interval = atoi(optarg); 412 iflag = true; 413 break; 414 case 'T': 415 Tflag = true; 416 break; 417 case 'x': 418 xflag = true; 419 break; 420 case 'z': 421 zflag = true; 422 break; 423 case '?': 424 default: 425 usage(); 426 } 427 argv += optind; 428 argc -= optind; 429 430 #define BACKWARD_COMPATIBILITY 431 #ifdef BACKWARD_COMPATIBILITY 432 if (*argv) { 433 if (isdigit(**argv)) { 434 interval = atoi(*argv); 435 if (interval <= 0) 436 usage(); 437 ++argv; 438 iflag = true; 439 } 440 if (*argv) { 441 nlistf = *argv; 442 if (*++argv) 443 memf = *argv; 444 } 445 } 446 #endif 447 448 #ifdef JAIL 449 if (jail_name != NULL) { 450 jid = jail_getid(jail_name); 451 if (jid == -1) 452 xo_errx(EX_UNAVAILABLE, "Jail not found"); 453 if (jail_attach(jid) != 0) 454 xo_errx(EX_UNAVAILABLE, "Cannot attach to jail"); 455 } 456 #endif 457 458 /* 459 * Discard setgid privileges if not the running kernel so that bad 460 * guys can't print interesting stuff from kernel memory. 461 */ 462 live = (nlistf == NULL && memf == NULL); 463 if (!live) { 464 if (setgid(getgid()) != 0) 465 xo_err(EX_OSERR, "setgid"); 466 /* Load all necessary kvm symbols */ 467 kresolve_list(nl); 468 } 469 470 if (xflag && Tflag) 471 xo_errx(EX_USAGE, "-x and -T are incompatible, pick one."); 472 473 if (Bflag) { 474 if (!live) 475 usage(); 476 bpf_stats(interface); 477 if (xo_finish() < 0) 478 xo_err(EX_IOERR, "stdout"); 479 exit(EX_OK); 480 } 481 if (mflag) { 482 if (!live) { 483 if (kread(0, NULL, 0) == 0) 484 mbpr(kvmd, nl[N_SFSTAT].n_value); 485 } else 486 mbpr(NULL, 0); 487 if (xo_finish() < 0) 488 xo_err(EX_IOERR, "stdout"); 489 exit(EX_OK); 490 } 491 if (Qflag) { 492 if (!live) { 493 if (kread(0, NULL, 0) == 0) 494 netisr_stats(); 495 } else 496 netisr_stats(); 497 if (xo_finish() < 0) 498 xo_err(EX_IOERR, "stdout"); 499 exit(EX_OK); 500 } 501 #if 0 502 /* 503 * Keep file descriptors open to avoid overhead 504 * of open/close on each call to get* routines. 505 */ 506 sethostent(1); 507 setnetent(1); 508 #else 509 /* 510 * This does not make sense any more with DNS being default over 511 * the files. Doing a setXXXXent(1) causes a tcp connection to be 512 * used for the queries, which is slower. 513 */ 514 #endif 515 if (iflag && !sflag) { 516 xo_open_container("statistics"); 517 xo_set_version(NETSTAT_XO_VERSION); 518 intpr(NULL, af); 519 xo_close_container("statistics"); 520 if (xo_finish() < 0) 521 xo_err(EX_IOERR, "stdout"); 522 exit(EX_OK); 523 } 524 if (rflag) { 525 xo_open_container("statistics"); 526 xo_set_version(NETSTAT_XO_VERSION); 527 if (sflag) 528 rt_stats(); 529 else 530 routepr(fib, af); 531 xo_close_container("statistics"); 532 if (xo_finish() < 0) 533 xo_err(EX_IOERR, "stdout"); 534 exit(EX_OK); 535 } 536 if (oflag) { 537 xo_open_container("statistics"); 538 xo_set_version(NETSTAT_XO_VERSION); 539 nhops_print(fib, af); 540 xo_close_container("statistics"); 541 if (xo_finish() < 0) 542 xo_err(EX_IOERR, "stdout"); 543 exit(EX_OK); 544 } 545 if (Oflag) { 546 xo_open_container("statistics"); 547 xo_set_version(NETSTAT_XO_VERSION); 548 nhgrp_print(fib, af); 549 xo_close_container("statistics"); 550 if (xo_finish() < 0) 551 xo_err(EX_IOERR, "stdout"); 552 exit(EX_OK); 553 } 554 555 556 557 if (gflag) { 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 (setgid(getgid()) != 0) 743 xo_err(EX_OSERR, "setgid"); 744 745 if (kvmd == NULL) { 746 xo_warnx("kvm not available: %s", errbuf); 747 return (-1); 748 } 749 750 return (0); 751 } 752 753 /* 754 * Resolve symbol list, return 0 on success. 755 */ 756 static int 757 kresolve_list(struct nlist *_nl) 758 { 759 760 if ((kvmd == NULL) && (kvmd_init() != 0)) 761 return (-1); 762 763 if (_nl[0].n_type != 0) 764 return (0); 765 766 if (kvm_nlist(kvmd, _nl) < 0) { 767 if (nlistf) 768 xo_errx(EX_UNAVAILABLE, "%s: kvm_nlist: %s", nlistf, 769 kvm_geterr(kvmd)); 770 else 771 xo_errx(EX_UNAVAILABLE, "kvm_nlist: %s", kvm_geterr(kvmd)); 772 } 773 774 return (0); 775 } 776 777 /* 778 * Wrapper of kvm_dpcpu_setcpu(). 779 */ 780 void 781 kset_dpcpu(u_int cpuid) 782 { 783 784 if ((kvmd == NULL) && (kvmd_init() != 0)) 785 xo_errx(EX_UNAVAILABLE, "%s: kvm is not available", __func__); 786 787 if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0) 788 xo_errx(EX_UNAVAILABLE, "%s: kvm_dpcpu_setcpu(%u): %s", __func__, 789 cpuid, kvm_geterr(kvmd)); 790 return; 791 } 792 793 /* 794 * Read kernel memory, return 0 on success. 795 */ 796 int 797 kread(u_long addr, void *buf, size_t size) 798 { 799 800 if (kvmd_init() < 0) 801 return (-1); 802 803 if (!buf) 804 return (0); 805 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 806 xo_warnx("%s", kvm_geterr(kvmd)); 807 return (-1); 808 } 809 return (0); 810 } 811 812 /* 813 * Read single counter(9). 814 */ 815 uint64_t 816 kread_counter(u_long addr) 817 { 818 819 if (kvmd_init() < 0) 820 return (-1); 821 822 return (kvm_counter_u64_fetch(kvmd, addr)); 823 } 824 825 /* 826 * Read an array of N counters in kernel memory into array of N uint64_t's. 827 */ 828 int 829 kread_counters(u_long addr, void *buf, size_t size) 830 { 831 uint64_t *c; 832 u_long *counters; 833 size_t i, n; 834 835 if (kvmd_init() < 0) 836 return (-1); 837 838 if (size % sizeof(uint64_t) != 0) { 839 xo_warnx("kread_counters: invalid counter set size"); 840 return (-1); 841 } 842 843 n = size / sizeof(uint64_t); 844 if ((counters = malloc(n * sizeof(u_long))) == NULL) 845 xo_err(EX_OSERR, "malloc"); 846 if (kread(addr, counters, n * sizeof(u_long)) < 0) { 847 free(counters); 848 return (-1); 849 } 850 851 c = buf; 852 for (i = 0; i < n; i++) 853 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); 854 855 free(counters); 856 return (0); 857 } 858 859 const char * 860 plural(uintmax_t n) 861 { 862 return (n != 1 ? "s" : ""); 863 } 864 865 const char * 866 plurales(uintmax_t n) 867 { 868 return (n != 1 ? "es" : ""); 869 } 870 871 const char * 872 pluralies(uintmax_t n) 873 { 874 return (n != 1 ? "ies" : "y"); 875 } 876 877 /* 878 * Find the protox for the given "well-known" name. 879 */ 880 static struct protox * 881 knownname(const char *name) 882 { 883 struct protox **tpp, *tp; 884 885 for (tpp = protoprotox; *tpp; tpp++) 886 for (tp = *tpp; tp->pr_name; tp++) 887 if (strcmp(tp->pr_name, name) == 0) 888 return (tp); 889 return (NULL); 890 } 891 892 /* 893 * Find the protox corresponding to name. 894 */ 895 static struct protox * 896 name2protox(const char *name) 897 { 898 struct protox *tp; 899 char **alias; /* alias from p->aliases */ 900 struct protoent *p; 901 902 /* 903 * Try to find the name in the list of "well-known" names. If that 904 * fails, check if name is an alias for an Internet protocol. 905 */ 906 if ((tp = knownname(name)) != NULL) 907 return (tp); 908 909 setprotoent(1); /* make protocol lookup cheaper */ 910 while ((p = getprotoent()) != NULL) { 911 /* assert: name not same as p->name */ 912 for (alias = p->p_aliases; *alias; alias++) 913 if (strcmp(name, *alias) == 0) { 914 endprotoent(); 915 return (knownname(p->p_name)); 916 } 917 } 918 endprotoent(); 919 return (NULL); 920 } 921 922 static void 923 usage(void) 924 { 925 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", 926 "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n" 927 " [-M core] [-N system]", 928 " netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n" 929 " [-M core] [-N system]", 930 " netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n" 931 " [-q howmany]", 932 " netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n" 933 " [-M core] [-N system]", 934 " netstat [-j jail] -i | -I interface -s [-46s]\n" 935 " [-f protocol_family | -p protocol] [-M core] [-N system]", 936 " netstat [-j jail] -m [-M core] [-N system]", 937 " netstat [-j jail] -B [-z] [-I interface]", 938 " netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n" 939 " [-M core] [-N system]", 940 " netstat [-j jail] -rs [-s] [-M core] [-N system]", 941 " netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]", 942 " netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]", 943 " netstat [-j jail] -Q"); 944 exit(EX_USAGE); 945 } 946