1 /*- 2 * Copyright (c) 1983, 1988, 1993 3 * Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static char const copyright[] = 32 "@(#) Copyright (c) 1983, 1988, 1993\n\ 33 Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #if 0 37 #ifndef lint 38 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 39 #endif /* not lint */ 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/file.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sysctl.h> 51 52 #include <netinet/in.h> 53 54 #ifdef NETGRAPH 55 #include <netgraph/ng_socket.h> 56 #endif 57 58 #include <ctype.h> 59 #include <err.h> 60 #include <errno.h> 61 #ifndef FSTACK 62 #include <kvm.h> 63 #endif 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 #ifdef FSTACK 79 #include "ff_ipc.h" 80 #endif 81 82 static struct protox { 83 int pr_index; /* index into nlist of cb head */ 84 int pr_sindex; /* index into nlist of stat block */ 85 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 86 void (*pr_cblocks)(u_long, const char *, int, int); 87 /* control blocks printing routine */ 88 void (*pr_stats)(u_long, const char *, int, int); 89 /* statistics printing routine */ 90 void (*pr_istats)(char *); /* per/if statistics printing routine */ 91 const char *pr_name; /* well-known name */ 92 int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 93 int pr_protocol; 94 } protox[] = { 95 { N_TCBINFO, N_TCPSTAT, 1, protopr, 96 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 97 { N_UDBINFO, N_UDPSTAT, 1, protopr, 98 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 99 #ifdef SCTP 100 { -1, N_SCTPSTAT, 1, sctp_protopr, 101 sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 102 #endif 103 #ifdef SDP 104 { -1, -1, 1, protopr, 105 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 106 #endif 107 { N_DIVCBINFO, -1, 1, protopr, 108 NULL, NULL, "divert", 1, IPPROTO_DIVERT }, 109 { N_RIPCBINFO, N_IPSTAT, 1, protopr, 110 ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 111 { N_RIPCBINFO, N_ICMPSTAT, 1, protopr, 112 icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 113 { N_RIPCBINFO, N_IGMPSTAT, 1, protopr, 114 igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 115 #ifdef IPSEC 116 { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */ 117 ipsec_stats, NULL, "ipsec", 1, 0}, 118 { -1, N_AHSTAT, 1, NULL, 119 ah_stats, NULL, "ah", 1, 0}, 120 { -1, N_ESPSTAT, 1, NULL, 121 esp_stats, NULL, "esp", 1, 0}, 122 { -1, N_IPCOMPSTAT, 1, NULL, 123 ipcomp_stats, NULL, "ipcomp", 1, 0}, 124 #endif 125 { N_RIPCBINFO, N_PIMSTAT, 1, protopr, 126 pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 127 { -1, N_CARPSTATS, 1, NULL, 128 carp_stats, NULL, "carp", 1, 0 }, 129 #ifdef PF 130 { -1, N_PFSYNCSTATS, 1, NULL, 131 pfsync_stats, NULL, "pfsync", 1, 0 }, 132 #endif 133 { -1, N_ARPSTAT, 1, NULL, 134 arp_stats, NULL, "arp", 1, 0 }, 135 { -1, -1, 0, NULL, 136 NULL, NULL, NULL, 0, 0 } 137 }; 138 139 #ifdef INET6 140 static struct protox ip6protox[] = { 141 { N_TCBINFO, N_TCPSTAT, 1, protopr, 142 tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 143 { N_UDBINFO, N_UDPSTAT, 1, protopr, 144 udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 145 { N_RIPCBINFO, N_IP6STAT, 1, protopr, 146 ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 147 { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, 148 icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 149 #ifdef SDP 150 { -1, -1, 1, protopr, 151 NULL, NULL, "sdp", 1, IPPROTO_TCP }, 152 #endif 153 #ifdef IPSEC 154 { -1, N_IPSEC6STAT, 1, NULL, 155 ipsec_stats, NULL, "ipsec6", 1, 0 }, 156 #endif 157 #ifdef notyet 158 { -1, N_PIM6STAT, 1, NULL, 159 pim6_stats, NULL, "pim6", 1, 0 }, 160 #endif 161 { -1, N_RIP6STAT, 1, NULL, 162 rip6_stats, NULL, "rip6", 1, 0 }, 163 { -1, -1, 0, NULL, 164 NULL, NULL, NULL, 0, 0 } 165 }; 166 #endif /*INET6*/ 167 168 #ifdef IPSEC 169 static struct protox pfkeyprotox[] = { 170 { -1, N_PFKEYSTAT, 1, NULL, 171 pfkey_stats, NULL, "pfkey", 0, 0 }, 172 { -1, -1, 0, NULL, 173 NULL, NULL, NULL, 0, 0 } 174 }; 175 #endif 176 177 #ifdef NETGRAPH 178 static struct protox netgraphprotox[] = { 179 { N_NGSOCKLIST, -1, 1, netgraphprotopr, 180 NULL, NULL, "ctrl", 0, 0 }, 181 { N_NGSOCKLIST, -1, 1, netgraphprotopr, 182 NULL, NULL, "data", 0, 0 }, 183 { -1, -1, 0, NULL, 184 NULL, NULL, NULL, 0, 0 } 185 }; 186 #endif 187 188 static struct protox *protoprotox[] = { 189 protox, 190 #ifdef INET6 191 ip6protox, 192 #endif 193 #ifdef IPSEC 194 pfkeyprotox, 195 #endif 196 NULL }; 197 198 static void printproto(struct protox *, const char *, bool *); 199 static void usage(void); 200 static struct protox *name2protox(const char *); 201 static struct protox *knownname(const char *); 202 203 #ifndef FSTACK 204 static int kresolve_list(struct nlist *_nl); 205 206 static kvm_t *kvmd; 207 #endif 208 static char *nlistf = NULL, *memf = NULL; 209 210 int Aflag; /* show addresses of protocol control block */ 211 int aflag; /* show all sockets (including servers) */ 212 static int Bflag; /* show information about bpf consumers */ 213 int bflag; /* show i/f total bytes in/out */ 214 int dflag; /* show i/f dropped packets */ 215 int gflag; /* show group (multicast) routing or stats */ 216 int hflag; /* show counters in human readable format */ 217 int iflag; /* show interfaces */ 218 int Lflag; /* show size of listen queues */ 219 int mflag; /* show memory stats */ 220 int noutputs = 0; /* how much outputs before we exit */ 221 int numeric_addr; /* show addresses numerically */ 222 int numeric_port; /* show ports numerically */ 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 238 static int af; /* address family */ 239 int live; /* true if we are examining a live system */ 240 241 int 242 main(int argc, char *argv[]) 243 { 244 struct protox *tp = NULL; /* for printing cblocks & stats */ 245 int ch; 246 int fib = -1; 247 char *endptr; 248 bool first = true; 249 250 #ifdef FSTACK 251 ff_ipc_init(); 252 #endif 253 254 af = AF_UNSPEC; 255 256 argc = xo_parse_args(argc, argv); 257 if (argc < 0) { 258 #ifdef FSTACK 259 ff_ipc_exit(); 260 #endif 261 exit(EXIT_FAILURE); 262 } 263 264 #ifndef FSTACK 265 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz")) 266 #else 267 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlnp:Qq:RrSTsuWw:xzP:")) 268 #endif 269 != -1) 270 switch(ch) { 271 case '4': 272 #ifdef INET 273 af = AF_INET; 274 #else 275 errx(1, "IPv4 support is not compiled in"); 276 #endif 277 break; 278 case '6': 279 #ifdef INET6 280 af = AF_INET6; 281 #else 282 errx(1, "IPv6 support is not compiled in"); 283 #endif 284 break; 285 case 'A': 286 Aflag = 1; 287 break; 288 case 'a': 289 aflag = 1; 290 break; 291 case 'B': 292 Bflag = 1; 293 break; 294 case 'b': 295 bflag = 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 'L': 351 Lflag = 1; 352 break; 353 #ifndef FSTACK 354 case 'M': 355 memf = optarg; 356 break; 357 case 'm': 358 mflag = 1; 359 break; 360 case 'N': 361 nlistf = optarg; 362 break; 363 #endif 364 case 'n': 365 numeric_addr = numeric_port = 1; 366 break; 367 case 'p': 368 if ((tp = name2protox(optarg)) == NULL) { 369 xo_errx(1, "%s: unknown or uninstrumented " 370 "protocol", optarg); 371 } 372 pflag = 1; 373 break; 374 case 'Q': 375 Qflag = 1; 376 break; 377 case 'q': 378 noutputs = atoi(optarg); 379 if (noutputs != 0) 380 noutputs++; 381 break; 382 case 'r': 383 rflag = 1; 384 break; 385 case 'R': 386 Rflag = 1; 387 break; 388 case 's': 389 ++sflag; 390 break; 391 case 'S': 392 numeric_addr = 1; 393 break; 394 case 'u': 395 af = AF_UNIX; 396 break; 397 case 'W': 398 case 'l': 399 Wflag = 1; 400 break; 401 case 'w': 402 interval = atoi(optarg); 403 iflag = 1; 404 break; 405 case 'T': 406 Tflag = 1; 407 break; 408 case 'x': 409 xflag = 1; 410 break; 411 case 'z': 412 zflag = 1; 413 break; 414 #ifdef FSTACK 415 case 'P': 416 ff_set_proc_id(atoi(optarg)); 417 break; 418 #endif 419 case '?': 420 default: 421 usage(); 422 } 423 argv += optind; 424 argc -= optind; 425 426 #define BACKWARD_COMPATIBILITY 427 #ifdef BACKWARD_COMPATIBILITY 428 if (*argv) { 429 if (isdigit(**argv)) { 430 interval = atoi(*argv); 431 if (interval <= 0) 432 usage(); 433 ++argv; 434 iflag = 1; 435 } 436 if (*argv) { 437 nlistf = *argv; 438 if (*++argv) 439 memf = *argv; 440 } 441 } 442 #endif 443 444 /* 445 * Discard setgid privileges if not the running kernel so that bad 446 * guys can't print interesting stuff from kernel memory. 447 */ 448 #ifndef FSTACK 449 live = (nlistf == NULL && memf == NULL); 450 #else 451 live = 1; 452 #endif 453 if (!live) { 454 if (setgid(getgid()) != 0) 455 xo_err(-1, "setgid"); 456 } 457 458 if (xflag && Tflag) 459 xo_errx(1, "-x and -T are incompatible, pick one."); 460 461 if (Bflag) { 462 if (!live) 463 usage(); 464 bpf_stats(interface); 465 xo_finish(); 466 #ifdef FSTACK 467 ff_ipc_exit(); 468 #endif 469 exit(0); 470 } 471 if (mflag) { 472 if (!live) { 473 #ifndef FSTACK 474 if (kread(0, NULL, 0) == 0) 475 mbpr(kvmd, nl[N_SFSTAT].n_value); 476 #endif 477 } else 478 mbpr(NULL, 0); 479 xo_finish(); 480 #ifdef FSTACK 481 ff_ipc_exit(); 482 #endif 483 exit(0); 484 } 485 if (Qflag) { 486 if (!live) { 487 #ifndef FSTACK 488 if (kread(0, NULL, 0) == 0) 489 netisr_stats(); 490 #endif 491 } else 492 netisr_stats(); 493 xo_finish(); 494 #ifdef FSTACK 495 ff_ipc_exit(); 496 #endif 497 exit(0); 498 } 499 500 #if 0 501 /* 502 * Keep file descriptors open to avoid overhead 503 * of open/close on each call to get* routines. 504 */ 505 sethostent(1); 506 setnetent(1); 507 #else 508 /* 509 * This does not make sense any more with DNS being default over 510 * the files. Doing a setXXXXent(1) causes a tcp connection to be 511 * used for the queries, which is slower. 512 */ 513 #endif 514 if (iflag && !sflag) { 515 xo_open_container("statistics"); 516 intpr(NULL, af); 517 xo_close_container("statistics"); 518 xo_finish(); 519 #ifdef FSTACK 520 ff_ipc_exit(); 521 #endif 522 exit(0); 523 } 524 if (rflag) { 525 xo_open_container("statistics"); 526 if (sflag) { 527 rt_stats(); 528 flowtable_stats(); 529 } else 530 routepr(fib, af); 531 xo_close_container("statistics"); 532 xo_finish(); 533 #ifdef FSTACK 534 ff_ipc_exit(); 535 #endif 536 exit(0); 537 } 538 539 if (gflag) { 540 xo_open_container("statistics"); 541 if (sflag) { 542 if (af == AF_INET || af == AF_UNSPEC) 543 mrt_stats(); 544 #ifdef INET6 545 if (af == AF_INET6 || af == AF_UNSPEC) 546 mrt6_stats(); 547 #endif 548 } else { 549 if (af == AF_INET || af == AF_UNSPEC) 550 mroutepr(); 551 #ifdef INET6 552 if (af == AF_INET6 || af == AF_UNSPEC) 553 mroute6pr(); 554 #endif 555 } 556 xo_close_container("statistics"); 557 xo_finish(); 558 #ifdef FSTACK 559 ff_ipc_exit(); 560 #endif 561 exit(0); 562 } 563 564 #ifndef FSTACK 565 /* Load all necessary kvm symbols */ 566 kresolve_list(nl); 567 #endif 568 569 if (tp) { 570 xo_open_container("statistics"); 571 printproto(tp, tp->pr_name, &first); 572 if (!first) 573 xo_close_list("socket"); 574 xo_close_container("statistics"); 575 xo_finish(); 576 #ifdef FSTACK 577 ff_ipc_exit(); 578 #endif 579 exit(0); 580 } 581 582 xo_open_container("statistics"); 583 if (af == AF_INET || af == AF_UNSPEC) 584 for (tp = protox; tp->pr_name; tp++) 585 printproto(tp, tp->pr_name, &first); 586 #ifdef INET6 587 if (af == AF_INET6 || af == AF_UNSPEC) 588 for (tp = ip6protox; tp->pr_name; tp++) 589 printproto(tp, tp->pr_name, &first); 590 #endif /*INET6*/ 591 #ifdef IPSEC 592 if (af == PF_KEY || af == AF_UNSPEC) 593 for (tp = pfkeyprotox; tp->pr_name; tp++) 594 printproto(tp, tp->pr_name, &first); 595 #endif /*IPSEC*/ 596 #ifdef NETGRAPH 597 if (af == AF_NETGRAPH || af == AF_UNSPEC) 598 for (tp = netgraphprotox; tp->pr_name; tp++) 599 printproto(tp, tp->pr_name, &first); 600 #endif /* NETGRAPH */ 601 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 602 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 603 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 604 nl[N_UNP_SPHEAD].n_value, &first); 605 606 if (!first) 607 xo_close_list("socket"); 608 xo_close_container("statistics"); 609 xo_finish(); 610 611 #ifdef FSTACK 612 ff_ipc_exit(); 613 #endif 614 615 exit(0); 616 } 617 618 static int 619 fetch_stats_internal(const char *sysctlname, u_long off, void *stats, 620 size_t len, kreadfn_t kreadfn, int zero) 621 { 622 int error; 623 624 if (live) { 625 memset(stats, 0, len); 626 if (zero) 627 error = sysctlbyname(sysctlname, NULL, NULL, stats, 628 len); 629 else 630 error = sysctlbyname(sysctlname, stats, &len, NULL, 0); 631 if (error == -1 && errno != ENOENT) 632 xo_warn("sysctl %s", sysctlname); 633 } else { 634 if (off == 0) 635 return (1); 636 error = kreadfn(off, stats, len); 637 } 638 return (error); 639 } 640 641 int 642 fetch_stats(const char *sysctlname, u_long off, void *stats, 643 size_t len, kreadfn_t kreadfn) 644 { 645 646 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 647 zflag)); 648 } 649 650 int 651 fetch_stats_ro(const char *sysctlname, u_long off, void *stats, 652 size_t len, kreadfn_t kreadfn) 653 { 654 655 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); 656 } 657 658 /* 659 * Print out protocol statistics or control blocks (per sflag). 660 * If the interface was not specifically requested, and the symbol 661 * is not in the namelist, ignore this one. 662 */ 663 static void 664 printproto(struct protox *tp, const char *name, bool *first) 665 { 666 void (*pr)(u_long, const char *, int, int); 667 u_long off; 668 bool doingdblocks = false; 669 670 if (sflag) { 671 if (iflag) { 672 if (tp->pr_istats) 673 intpr(tp->pr_istats, af); 674 else if (pflag) 675 xo_message("%s: no per-interface stats routine", 676 tp->pr_name); 677 return; 678 } else { 679 pr = tp->pr_stats; 680 if (!pr) { 681 if (pflag) 682 xo_message("%s: no stats routine", 683 tp->pr_name); 684 return; 685 } 686 if (tp->pr_usesysctl && live) 687 off = 0; 688 else if (tp->pr_sindex < 0) { 689 if (pflag) 690 xo_message("%s: stats routine doesn't " 691 "work on cores", tp->pr_name); 692 return; 693 } else 694 off = nl[tp->pr_sindex].n_value; 695 } 696 } else { 697 doingdblocks = true; 698 pr = tp->pr_cblocks; 699 if (!pr) { 700 if (pflag) 701 xo_message("%s: no PCB routine", tp->pr_name); 702 return; 703 } 704 if (tp->pr_usesysctl && live) 705 off = 0; 706 else if (tp->pr_index < 0) { 707 if (pflag) 708 xo_message("%s: PCB routine doesn't work on " 709 "cores", tp->pr_name); 710 return; 711 } else 712 off = nl[tp->pr_index].n_value; 713 } 714 if (pr != NULL && (off || (live && tp->pr_usesysctl) || 715 af != AF_UNSPEC)) { 716 if (doingdblocks && *first) { 717 xo_open_list("socket"); 718 *first = false; 719 } 720 721 (*pr)(off, name, af, tp->pr_protocol); 722 } 723 } 724 725 #ifndef FSTACK 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 #else 853 int 854 kread(u_long addr, void *buf, size_t size) 855 { 856 return 0; 857 } 858 859 uint64_t 860 kread_counter(u_long addr) 861 { 862 return 0; 863 } 864 865 int 866 kread_counters(u_long addr, void *buf, size_t size) 867 { 868 return 0; 869 } 870 void 871 kset_dpcpu(u_int cpuid) 872 { 873 874 } 875 876 #endif 877 878 const char * 879 plural(uintmax_t n) 880 { 881 return (n != 1 ? "s" : ""); 882 } 883 884 const char * 885 plurales(uintmax_t n) 886 { 887 return (n != 1 ? "es" : ""); 888 } 889 890 const char * 891 pluralies(uintmax_t n) 892 { 893 return (n != 1 ? "ies" : "y"); 894 } 895 896 /* 897 * Find the protox for the given "well-known" name. 898 */ 899 static struct protox * 900 knownname(const char *name) 901 { 902 struct protox **tpp, *tp; 903 904 for (tpp = protoprotox; *tpp; tpp++) 905 for (tp = *tpp; tp->pr_name; tp++) 906 if (strcmp(tp->pr_name, name) == 0) 907 return (tp); 908 return (NULL); 909 } 910 911 /* 912 * Find the protox corresponding to name. 913 */ 914 static struct protox * 915 name2protox(const char *name) 916 { 917 struct protox *tp; 918 char **alias; /* alias from p->aliases */ 919 struct protoent *p; 920 921 /* 922 * Try to find the name in the list of "well-known" names. If that 923 * fails, check if name is an alias for an Internet protocol. 924 */ 925 if ((tp = knownname(name)) != NULL) 926 return (tp); 927 928 setprotoent(1); /* make protocol lookup cheaper */ 929 while ((p = getprotoent()) != NULL) { 930 /* assert: name not same as p->name */ 931 for (alias = p->p_aliases; *alias; alias++) 932 if (strcmp(name, *alias) == 0) { 933 endprotoent(); 934 return (knownname(p->p_name)); 935 } 936 } 937 endprotoent(); 938 return (NULL); 939 } 940 941 static void 942 usage(void) 943 { 944 (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", 945 #ifndef FSTACK 946 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n" 947 " [-M core] [-N system]", 948 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" 949 " [-M core] [-N system]", 950 " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n" 951 " [-q howmany]", 952 " netstat -s [-46sz] [-f protocol_family | -p protocol]\n" 953 " [-M core] [-N system]", 954 " netstat -i | -I interface -s [-46s]\n" 955 " [-f protocol_family | -p protocol] [-M core] [-N system]", 956 " netstat -m [-M core] [-N system]", 957 " netstat -B [-z] [-I interface]", 958 " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n" 959 " [-M core] [-N system]", 960 " netstat -rs [-s] [-M core] [-N system]", 961 " netstat -g [-46W] [-f address_family] [-M core] [-N system]", 962 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", 963 " netstat -Q"); 964 #else 965 "usage: netstat -P <f-stack proc_id> [-46AaLnRSTWx] [-f protocol_family | -p protocol]", 966 " netstat -P <f-stack proc_id> -i | -I interface [-46abdhnW] [-f address_family]", 967 " netstat -P <f-stack proc_id> -w wait [-I interface] [-46d] [-q howmany]", 968 " netstat -P <f-stack proc_id> -s [-46sz] [-f protocol_family | -p protocol]", 969 " netstat -P <f-stack proc_id> -i | -I interface -s [-46s]\n" 970 " [-f protocol_family | -p protocol]", 971 " netstat -P <f-stack proc_id> -B [-z] [-I interface]", 972 " netstat -P <f-stack proc_id> -r [-46AnW] [-F fibnum] [-f address_family]", 973 " netstat -P <f-stack proc_id> -rs [-s]", 974 " netstat -P <f-stack proc_id> -g [-46W] [-f address_family]", 975 " netstat -P <f-stack proc_id> -gs [-46s] [-f address_family]", 976 " netstat -P <f-stack proc_id> -Q"); 977 978 #endif 979 xo_finish(); 980 #ifdef FSTACK 981 ff_ipc_exit(); 982 #endif 983 exit(1); 984 } 985