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