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 ff_ipc_init(); 251 252 af = AF_UNSPEC; 253 254 argc = xo_parse_args(argc, argv); 255 if (argc < 0) 256 exit(EXIT_FAILURE); 257 258 #ifndef FSTACK 259 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz")) 260 #else 261 while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlnp:Qq:RrSTsuWw:xzP:")) 262 #endif 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 'd': 292 dflag = 1; 293 break; 294 case 'F': 295 fib = strtol(optarg, &endptr, 0); 296 if (*endptr != '\0' || 297 (fib == 0 && (errno == EINVAL || errno == ERANGE))) 298 xo_errx(1, "%s: invalid fib", optarg); 299 break; 300 case 'f': 301 if (strcmp(optarg, "inet") == 0) 302 af = AF_INET; 303 #ifdef INET6 304 else if (strcmp(optarg, "inet6") == 0) 305 af = AF_INET6; 306 #endif 307 #ifdef IPSEC 308 else if (strcmp(optarg, "pfkey") == 0) 309 af = PF_KEY; 310 #endif 311 else if (strcmp(optarg, "unix") == 0 || 312 strcmp(optarg, "local") == 0) 313 af = AF_UNIX; 314 #ifdef NETGRAPH 315 else if (strcmp(optarg, "ng") == 0 316 || strcmp(optarg, "netgraph") == 0) 317 af = AF_NETGRAPH; 318 #endif 319 else if (strcmp(optarg, "link") == 0) 320 af = AF_LINK; 321 else { 322 xo_errx(1, "%s: unknown address family", 323 optarg); 324 } 325 break; 326 case 'g': 327 gflag = 1; 328 break; 329 case 'h': 330 hflag = 1; 331 break; 332 case 'I': { 333 char *cp; 334 335 iflag = 1; 336 for (cp = interface = optarg; isalpha(*cp); cp++) 337 continue; 338 unit = atoi(cp); 339 break; 340 } 341 case 'i': 342 iflag = 1; 343 break; 344 case 'L': 345 Lflag = 1; 346 break; 347 #ifndef FSTACK 348 case 'M': 349 memf = optarg; 350 break; 351 case 'm': 352 mflag = 1; 353 break; 354 case 'N': 355 nlistf = optarg; 356 break; 357 #endif 358 case 'n': 359 numeric_addr = numeric_port = 1; 360 break; 361 case 'p': 362 if ((tp = name2protox(optarg)) == NULL) { 363 xo_errx(1, "%s: unknown or uninstrumented " 364 "protocol", optarg); 365 } 366 pflag = 1; 367 break; 368 case 'Q': 369 Qflag = 1; 370 break; 371 case 'q': 372 noutputs = atoi(optarg); 373 if (noutputs != 0) 374 noutputs++; 375 break; 376 case 'r': 377 rflag = 1; 378 break; 379 case 'R': 380 Rflag = 1; 381 break; 382 case 's': 383 ++sflag; 384 break; 385 case 'S': 386 numeric_addr = 1; 387 break; 388 case 'u': 389 af = AF_UNIX; 390 break; 391 case 'W': 392 case 'l': 393 Wflag = 1; 394 break; 395 case 'w': 396 interval = atoi(optarg); 397 iflag = 1; 398 break; 399 case 'T': 400 Tflag = 1; 401 break; 402 case 'x': 403 xflag = 1; 404 break; 405 case 'z': 406 zflag = 1; 407 break; 408 #ifdef FSTACK 409 case 'P': 410 ff_set_proc_id(atoi(optarg)); 411 break; 412 #endif 413 case '?': 414 default: 415 usage(); 416 } 417 argv += optind; 418 argc -= optind; 419 420 #define BACKWARD_COMPATIBILITY 421 #ifdef BACKWARD_COMPATIBILITY 422 if (*argv) { 423 if (isdigit(**argv)) { 424 interval = atoi(*argv); 425 if (interval <= 0) 426 usage(); 427 ++argv; 428 iflag = 1; 429 } 430 if (*argv) { 431 nlistf = *argv; 432 if (*++argv) 433 memf = *argv; 434 } 435 } 436 #endif 437 438 /* 439 * Discard setgid privileges if not the running kernel so that bad 440 * guys can't print interesting stuff from kernel memory. 441 */ 442 #ifndef FSTACK 443 live = (nlistf == NULL && memf == NULL); 444 #else 445 live = 1; 446 #endif 447 if (!live) { 448 if (setgid(getgid()) != 0) 449 xo_err(-1, "setgid"); 450 } 451 452 if (xflag && Tflag) 453 xo_errx(1, "-x and -T are incompatible, pick one."); 454 455 if (Bflag) { 456 if (!live) 457 usage(); 458 bpf_stats(interface); 459 xo_finish(); 460 exit(0); 461 } 462 if (mflag) { 463 if (!live) { 464 #ifndef FSTACK 465 if (kread(0, NULL, 0) == 0) 466 mbpr(kvmd, nl[N_SFSTAT].n_value); 467 #endif 468 } else 469 mbpr(NULL, 0); 470 xo_finish(); 471 exit(0); 472 } 473 if (Qflag) { 474 if (!live) { 475 #ifndef FSTACK 476 if (kread(0, NULL, 0) == 0) 477 netisr_stats(); 478 #endif 479 } else 480 netisr_stats(); 481 xo_finish(); 482 exit(0); 483 } 484 485 #if 0 486 /* 487 * Keep file descriptors open to avoid overhead 488 * of open/close on each call to get* routines. 489 */ 490 sethostent(1); 491 setnetent(1); 492 #else 493 /* 494 * This does not make sense any more with DNS being default over 495 * the files. Doing a setXXXXent(1) causes a tcp connection to be 496 * used for the queries, which is slower. 497 */ 498 #endif 499 if (iflag && !sflag) { 500 xo_open_container("statistics"); 501 intpr(NULL, af); 502 xo_close_container("statistics"); 503 xo_finish(); 504 exit(0); 505 } 506 if (rflag) { 507 xo_open_container("statistics"); 508 if (sflag) { 509 rt_stats(); 510 flowtable_stats(); 511 } else 512 routepr(fib, af); 513 xo_close_container("statistics"); 514 xo_finish(); 515 exit(0); 516 } 517 518 if (gflag) { 519 xo_open_container("statistics"); 520 if (sflag) { 521 if (af == AF_INET || af == AF_UNSPEC) 522 mrt_stats(); 523 #ifdef INET6 524 if (af == AF_INET6 || af == AF_UNSPEC) 525 mrt6_stats(); 526 #endif 527 } else { 528 if (af == AF_INET || af == AF_UNSPEC) 529 mroutepr(); 530 #ifdef INET6 531 if (af == AF_INET6 || af == AF_UNSPEC) 532 mroute6pr(); 533 #endif 534 } 535 xo_close_container("statistics"); 536 xo_finish(); 537 exit(0); 538 } 539 540 #ifndef FSTACK 541 /* Load all necessary kvm symbols */ 542 kresolve_list(nl); 543 #endif 544 545 if (tp) { 546 xo_open_container("statistics"); 547 printproto(tp, tp->pr_name, &first); 548 if (!first) 549 xo_close_list("socket"); 550 xo_close_container("statistics"); 551 xo_finish(); 552 exit(0); 553 } 554 555 xo_open_container("statistics"); 556 if (af == AF_INET || af == AF_UNSPEC) 557 for (tp = protox; tp->pr_name; tp++) 558 printproto(tp, tp->pr_name, &first); 559 #ifdef INET6 560 if (af == AF_INET6 || af == AF_UNSPEC) 561 for (tp = ip6protox; tp->pr_name; tp++) 562 printproto(tp, tp->pr_name, &first); 563 #endif /*INET6*/ 564 #ifdef IPSEC 565 if (af == PF_KEY || af == AF_UNSPEC) 566 for (tp = pfkeyprotox; tp->pr_name; tp++) 567 printproto(tp, tp->pr_name, &first); 568 #endif /*IPSEC*/ 569 #ifdef NETGRAPH 570 if (af == AF_NETGRAPH || af == AF_UNSPEC) 571 for (tp = netgraphprotox; tp->pr_name; tp++) 572 printproto(tp, tp->pr_name, &first); 573 #endif /* NETGRAPH */ 574 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 575 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 576 nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 577 nl[N_UNP_SPHEAD].n_value, &first); 578 579 if (!first) 580 xo_close_list("socket"); 581 xo_close_container("statistics"); 582 xo_finish(); 583 exit(0); 584 } 585 586 static int 587 fetch_stats_internal(const char *sysctlname, u_long off, void *stats, 588 size_t len, kreadfn_t kreadfn, int zero) 589 { 590 int error; 591 592 if (live) { 593 memset(stats, 0, len); 594 if (zero) 595 error = sysctlbyname(sysctlname, NULL, NULL, stats, 596 len); 597 else 598 error = sysctlbyname(sysctlname, stats, &len, NULL, 0); 599 if (error == -1 && errno != ENOENT) 600 xo_warn("sysctl %s", sysctlname); 601 } else { 602 if (off == 0) 603 return (1); 604 error = kreadfn(off, stats, len); 605 } 606 return (error); 607 } 608 609 int 610 fetch_stats(const char *sysctlname, u_long off, void *stats, 611 size_t len, kreadfn_t kreadfn) 612 { 613 614 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 615 zflag)); 616 } 617 618 int 619 fetch_stats_ro(const char *sysctlname, u_long off, void *stats, 620 size_t len, kreadfn_t kreadfn) 621 { 622 623 return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); 624 } 625 626 /* 627 * Print out protocol statistics or control blocks (per sflag). 628 * If the interface was not specifically requested, and the symbol 629 * is not in the namelist, ignore this one. 630 */ 631 static void 632 printproto(struct protox *tp, const char *name, bool *first) 633 { 634 void (*pr)(u_long, const char *, int, int); 635 u_long off; 636 bool doingdblocks = false; 637 638 if (sflag) { 639 if (iflag) { 640 if (tp->pr_istats) 641 intpr(tp->pr_istats, af); 642 else if (pflag) 643 xo_message("%s: no per-interface stats routine", 644 tp->pr_name); 645 return; 646 } else { 647 pr = tp->pr_stats; 648 if (!pr) { 649 if (pflag) 650 xo_message("%s: no stats routine", 651 tp->pr_name); 652 return; 653 } 654 if (tp->pr_usesysctl && live) 655 off = 0; 656 else if (tp->pr_sindex < 0) { 657 if (pflag) 658 xo_message("%s: stats routine doesn't " 659 "work on cores", tp->pr_name); 660 return; 661 } else 662 off = nl[tp->pr_sindex].n_value; 663 } 664 } else { 665 doingdblocks = true; 666 pr = tp->pr_cblocks; 667 if (!pr) { 668 if (pflag) 669 xo_message("%s: no PCB routine", tp->pr_name); 670 return; 671 } 672 if (tp->pr_usesysctl && live) 673 off = 0; 674 else if (tp->pr_index < 0) { 675 if (pflag) 676 xo_message("%s: PCB routine doesn't work on " 677 "cores", tp->pr_name); 678 return; 679 } else 680 off = nl[tp->pr_index].n_value; 681 } 682 if (pr != NULL && (off || (live && tp->pr_usesysctl) || 683 af != AF_UNSPEC)) { 684 if (doingdblocks && *first) { 685 xo_open_list("socket"); 686 *first = false; 687 } 688 689 (*pr)(off, name, af, tp->pr_protocol); 690 } 691 } 692 693 #ifndef FSTACK 694 static int 695 kvmd_init(void) 696 { 697 char errbuf[_POSIX2_LINE_MAX]; 698 699 if (kvmd != NULL) 700 return (0); 701 702 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 703 if (setgid(getgid()) != 0) 704 xo_err(-1, "setgid"); 705 706 if (kvmd == NULL) { 707 xo_warnx("kvm not available: %s", errbuf); 708 return (-1); 709 } 710 711 return (0); 712 } 713 714 /* 715 * Resolve symbol list, return 0 on success. 716 */ 717 static int 718 kresolve_list(struct nlist *_nl) 719 { 720 721 if ((kvmd == NULL) && (kvmd_init() != 0)) 722 return (-1); 723 724 if (_nl[0].n_type != 0) 725 return (0); 726 727 if (kvm_nlist(kvmd, _nl) < 0) { 728 if (nlistf) 729 xo_errx(1, "%s: kvm_nlist: %s", nlistf, 730 kvm_geterr(kvmd)); 731 else 732 xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 733 } 734 735 return (0); 736 } 737 738 /* 739 * Wrapper of kvm_dpcpu_setcpu(). 740 */ 741 void 742 kset_dpcpu(u_int cpuid) 743 { 744 745 if ((kvmd == NULL) && (kvmd_init() != 0)) 746 xo_errx(-1, "%s: kvm is not available", __func__); 747 748 if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0) 749 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__, 750 cpuid, kvm_geterr(kvmd)); 751 return; 752 } 753 754 /* 755 * Read kernel memory, return 0 on success. 756 */ 757 int 758 kread(u_long addr, void *buf, size_t size) 759 { 760 761 if (kvmd_init() < 0) 762 return (-1); 763 764 if (!buf) 765 return (0); 766 if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 767 xo_warnx("%s", kvm_geterr(kvmd)); 768 return (-1); 769 } 770 return (0); 771 } 772 773 /* 774 * Read single counter(9). 775 */ 776 uint64_t 777 kread_counter(u_long addr) 778 { 779 780 if (kvmd_init() < 0) 781 return (-1); 782 783 return (kvm_counter_u64_fetch(kvmd, addr)); 784 } 785 786 /* 787 * Read an array of N counters in kernel memory into array of N uint64_t's. 788 */ 789 int 790 kread_counters(u_long addr, void *buf, size_t size) 791 { 792 uint64_t *c; 793 u_long *counters; 794 size_t i, n; 795 796 if (kvmd_init() < 0) 797 return (-1); 798 799 if (size % sizeof(uint64_t) != 0) { 800 xo_warnx("kread_counters: invalid counter set size"); 801 return (-1); 802 } 803 804 n = size / sizeof(uint64_t); 805 if ((counters = malloc(n * sizeof(u_long))) == NULL) 806 xo_err(-1, "malloc"); 807 if (kread(addr, counters, n * sizeof(u_long)) < 0) { 808 free(counters); 809 return (-1); 810 } 811 812 c = buf; 813 for (i = 0; i < n; i++) 814 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); 815 816 free(counters); 817 return (0); 818 } 819 820 #else 821 int 822 kread(u_long addr, void *buf, size_t size) 823 { 824 return 0; 825 } 826 827 uint64_t 828 kread_counter(u_long addr) 829 { 830 return 0; 831 } 832 833 int 834 kread_counters(u_long addr, void *buf, size_t size) 835 { 836 return 0; 837 } 838 void 839 kset_dpcpu(u_int cpuid) 840 { 841 842 } 843 844 #endif 845 846 const char * 847 plural(uintmax_t n) 848 { 849 return (n != 1 ? "s" : ""); 850 } 851 852 const char * 853 plurales(uintmax_t n) 854 { 855 return (n != 1 ? "es" : ""); 856 } 857 858 const char * 859 pluralies(uintmax_t n) 860 { 861 return (n != 1 ? "ies" : "y"); 862 } 863 864 /* 865 * Find the protox for the given "well-known" name. 866 */ 867 static struct protox * 868 knownname(const char *name) 869 { 870 struct protox **tpp, *tp; 871 872 for (tpp = protoprotox; *tpp; tpp++) 873 for (tp = *tpp; tp->pr_name; tp++) 874 if (strcmp(tp->pr_name, name) == 0) 875 return (tp); 876 return (NULL); 877 } 878 879 /* 880 * Find the protox corresponding to name. 881 */ 882 static struct protox * 883 name2protox(const char *name) 884 { 885 struct protox *tp; 886 char **alias; /* alias from p->aliases */ 887 struct protoent *p; 888 889 /* 890 * Try to find the name in the list of "well-known" names. If that 891 * fails, check if name is an alias for an Internet protocol. 892 */ 893 if ((tp = knownname(name)) != NULL) 894 return (tp); 895 896 setprotoent(1); /* make protocol lookup cheaper */ 897 while ((p = getprotoent()) != NULL) { 898 /* assert: name not same as p->name */ 899 for (alias = p->p_aliases; *alias; alias++) 900 if (strcmp(name, *alias) == 0) { 901 endprotoent(); 902 return (knownname(p->p_name)); 903 } 904 } 905 endprotoent(); 906 return (NULL); 907 } 908 909 static void 910 usage(void) 911 { 912 (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", 913 #ifndef FSTACK 914 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n" 915 " [-M core] [-N system]", 916 " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" 917 " [-M core] [-N system]", 918 " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n" 919 " [-q howmany]", 920 " netstat -s [-46sz] [-f protocol_family | -p protocol]\n" 921 " [-M core] [-N system]", 922 " netstat -i | -I interface -s [-46s]\n" 923 " [-f protocol_family | -p protocol] [-M core] [-N system]", 924 " netstat -m [-M core] [-N system]", 925 " netstat -B [-z] [-I interface]", 926 " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n" 927 " [-M core] [-N system]", 928 " netstat -rs [-s] [-M core] [-N system]", 929 " netstat -g [-46W] [-f address_family] [-M core] [-N system]", 930 " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", 931 " netstat -Q"); 932 #else 933 "usage: netstat -P <f-stack proc_id> [-46AaLnRSTWx] [-f protocol_family | -p protocol]", 934 " netstat -P <f-stack proc_id> -i | -I interface [-46abdhnW] [-f address_family]", 935 " netstat -P <f-stack proc_id> -w wait [-I interface] [-46d] [-q howmany]", 936 " netstat -P <f-stack proc_id> -s [-46sz] [-f protocol_family | -p protocol]", 937 " netstat -P <f-stack proc_id> -i | -I interface -s [-46s]\n" 938 " [-f protocol_family | -p protocol]", 939 " netstat -P <f-stack proc_id> -B [-z] [-I interface]", 940 " netstat -P <f-stack proc_id> -r [-46AnW] [-F fibnum] [-f address_family]", 941 " netstat -P <f-stack proc_id> -rs [-s]", 942 " netstat -P <f-stack proc_id> -g [-46W] [-f address_family]", 943 " netstat -P <f-stack proc_id> -gs [-46s] [-f address_family]", 944 " netstat -P <f-stack proc_id> -Q"); 945 946 #endif 947 xo_finish(); 948 exit(1); 949 } 950