165475bc8SDavid E. O'Brien /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 59b50d902SRodney W. Grimes * Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 329b50d902SRodney W. Grimes #ifndef lint 3381dacd8bSHiroki Sato static char const copyright[] = 349b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\ 359b50d902SRodney W. Grimes Regents of the University of California. All rights reserved.\n"; 369b50d902SRodney W. Grimes #endif /* not lint */ 379b50d902SRodney W. Grimes 385d422d6aSPhilippe Charnier #if 0 396cc6f122SPhilippe Charnier #ifndef lint 409b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 419b50d902SRodney W. Grimes #endif /* not lint */ 426cc6f122SPhilippe Charnier #endif 436cc6f122SPhilippe Charnier 446cc6f122SPhilippe Charnier #include <sys/cdefs.h> 456cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <sys/param.h> 489b50d902SRodney W. Grimes #include <sys/file.h> 499b50d902SRodney W. Grimes #include <sys/protosw.h> 509b50d902SRodney W. Grimes #include <sys/socket.h> 51feda1a43SJohn Baldwin #include <sys/socketvar.h> 529eddb899SMark Johnston #include <sys/sysctl.h> 539b50d902SRodney W. Grimes 549b50d902SRodney W. Grimes #include <netinet/in.h> 559b50d902SRodney W. Grimes 56690f477dSSam Leffler #ifdef NETGRAPH 574cf49a43SJulian Elischer #include <netgraph/ng_socket.h> 58690f477dSSam Leffler #endif 594cf49a43SJulian Elischer 609b50d902SRodney W. Grimes #include <ctype.h> 615d422d6aSPhilippe Charnier #include <err.h> 62821df508SXin LI #include <errno.h> 639b50d902SRodney W. Grimes #include <kvm.h> 649b50d902SRodney W. Grimes #include <limits.h> 659b50d902SRodney W. Grimes #include <netdb.h> 669b50d902SRodney W. Grimes #include <nlist.h> 67821df508SXin LI #include <paths.h> 687b95a1ebSYaroslav Tykhiy #include <stdint.h> 699b50d902SRodney W. Grimes #include <stdio.h> 709b50d902SRodney W. Grimes #include <stdlib.h> 71ade9ccfeSMarcel Moolenaar #include <stdbool.h> 729b50d902SRodney W. Grimes #include <string.h> 739b50d902SRodney W. Grimes #include <unistd.h> 749b50d902SRodney W. Grimes #include "netstat.h" 7581dacd8bSHiroki Sato #include "nl_defs.h" 76ade9ccfeSMarcel Moolenaar #include <libxo/xo.h> 779b50d902SRodney W. Grimes 7881dacd8bSHiroki Sato static struct protox { 79feda1a43SJohn Baldwin int pr_index; /* index into nlist of cb head */ 80feda1a43SJohn Baldwin int pr_sindex; /* index into nlist of stat block */ 819b50d902SRodney W. Grimes u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 82feda1a43SJohn Baldwin void (*pr_cblocks)(u_long, const char *, int, int); 835e051718SAssar Westerlund /* control blocks printing routine */ 84feda1a43SJohn Baldwin void (*pr_stats)(u_long, const char *, int, int); 855e051718SAssar Westerlund /* statistics printing routine */ 865e051718SAssar Westerlund void (*pr_istats)(char *); /* per/if statistics printing routine */ 87fa6d48c0SMark Murray const char *pr_name; /* well-known name */ 8855fd53e2SJohn Baldwin int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 89feda1a43SJohn Baldwin int pr_protocol; 909b50d902SRodney W. Grimes } protox[] = { 91*ddf24a50SMichael Tuexen { -1 , N_TCPSTAT, 1, protopr, 92feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 93*ddf24a50SMichael Tuexen { -1 , N_UDPSTAT, 1, protopr, 94feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 9574fd40c9SRandall Stewart #ifdef SCTP 96feda1a43SJohn Baldwin { -1, N_SCTPSTAT, 1, sctp_protopr, 97feda1a43SJohn Baldwin sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 9874fd40c9SRandall Stewart #endif 99aa0a1e58SJeff Roberson #ifdef SDP 100aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 101aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 102aa0a1e58SJeff Roberson #endif 103*ddf24a50SMichael Tuexen { -1 , -1, 1, protopr, 1042b1c7217SGleb Smirnoff divert_stats, NULL, "divert", 1, 0 }, 105*ddf24a50SMichael Tuexen { -1 , N_IPSTAT, 1, protopr, 106feda1a43SJohn Baldwin ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 107*ddf24a50SMichael Tuexen { -1 , N_ICMPSTAT, 1, protopr, 108feda1a43SJohn Baldwin icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 109*ddf24a50SMichael Tuexen { -1 , N_IGMPSTAT, 1, protopr, 110feda1a43SJohn Baldwin igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 111cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 11281dacd8bSHiroki Sato { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */ 1139d2d8e7bSGeorge V. Neville-Neil ipsec_stats, NULL, "ipsec", 1, 0}, 1148409aedfSGeorge V. Neville-Neil { -1, N_AHSTAT, 1, NULL, 1159d2d8e7bSGeorge V. Neville-Neil ah_stats, NULL, "ah", 1, 0}, 1168409aedfSGeorge V. Neville-Neil { -1, N_ESPSTAT, 1, NULL, 1179d2d8e7bSGeorge V. Neville-Neil esp_stats, NULL, "esp", 1, 0}, 1188409aedfSGeorge V. Neville-Neil { -1, N_IPCOMPSTAT, 1, NULL, 1199d2d8e7bSGeorge V. Neville-Neil ipcomp_stats, NULL, "ipcomp", 1, 0}, 120100b98dbSKelly Yancey #endif 121*ddf24a50SMichael Tuexen { -1 , N_PIMSTAT, 1, protopr, 122feda1a43SJohn Baldwin pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 12381dacd8bSHiroki Sato { -1, N_CARPSTATS, 1, NULL, 124feda1a43SJohn Baldwin carp_stats, NULL, "carp", 1, 0 }, 1253e4d5cd3SGleb Smirnoff #ifdef PF 12681dacd8bSHiroki Sato { -1, N_PFSYNCSTATS, 1, NULL, 127feda1a43SJohn Baldwin pfsync_stats, NULL, "pfsync", 1, 0 }, 1283e4d5cd3SGleb Smirnoff #endif 12954fc657dSGeorge V. Neville-Neil { -1, N_ARPSTAT, 1, NULL, 13054fc657dSGeorge V. Neville-Neil arp_stats, NULL, "arp", 1, 0 }, 1316bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 132feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 1339b50d902SRodney W. Grimes }; 1349b50d902SRodney W. Grimes 135cfa1ca9dSYoshinobu Inoue #ifdef INET6 13681dacd8bSHiroki Sato static struct protox ip6protox[] = { 137*ddf24a50SMichael Tuexen { -1 , N_TCPSTAT, 1, protopr, 138feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 139*ddf24a50SMichael Tuexen { -1 , N_UDPSTAT, 1, protopr, 140feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 141*ddf24a50SMichael Tuexen { -1 , N_IP6STAT, 1, protopr, 142feda1a43SJohn Baldwin ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 143*ddf24a50SMichael Tuexen { -1 , N_ICMP6STAT, 1, protopr, 144feda1a43SJohn Baldwin icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 145aa0a1e58SJeff Roberson #ifdef SDP 146aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 147aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 148aa0a1e58SJeff Roberson #endif 149cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 1506bb3f207SRuslan Ermilov { -1, N_IPSEC6STAT, 1, NULL, 1519d2d8e7bSGeorge V. Neville-Neil ipsec_stats, NULL, "ipsec6", 1, 0 }, 152cfa1ca9dSYoshinobu Inoue #endif 153cfa1ca9dSYoshinobu Inoue #ifdef notyet 1546bb3f207SRuslan Ermilov { -1, N_PIM6STAT, 1, NULL, 155feda1a43SJohn Baldwin pim6_stats, NULL, "pim6", 1, 0 }, 156cfa1ca9dSYoshinobu Inoue #endif 157feda1a43SJohn Baldwin { -1, N_RIP6STAT, 1, NULL, 158feda1a43SJohn Baldwin rip6_stats, NULL, "rip6", 1, 0 }, 1596bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 160feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 161cfa1ca9dSYoshinobu Inoue }; 162cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 163cfa1ca9dSYoshinobu Inoue 1643b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 16581dacd8bSHiroki Sato static struct protox pfkeyprotox[] = { 1666bb3f207SRuslan Ermilov { -1, N_PFKEYSTAT, 1, NULL, 167feda1a43SJohn Baldwin pfkey_stats, NULL, "pfkey", 0, 0 }, 1686bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 169feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 1703b8a8567SJun-ichiro itojun Hagino }; 1713b8a8567SJun-ichiro itojun Hagino #endif 1723b8a8567SJun-ichiro itojun Hagino 173690f477dSSam Leffler #ifdef NETGRAPH 17481dacd8bSHiroki Sato static struct protox netgraphprotox[] = { 17581dacd8bSHiroki Sato { N_NGSOCKLIST, -1, 1, netgraphprotopr, 176feda1a43SJohn Baldwin NULL, NULL, "ctrl", 0, 0 }, 17781dacd8bSHiroki Sato { N_NGSOCKLIST, -1, 1, netgraphprotopr, 178feda1a43SJohn Baldwin NULL, NULL, "data", 0, 0 }, 1796bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 180feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 1814cf49a43SJulian Elischer }; 182690f477dSSam Leffler #endif 183cc6a66f2SJulian Elischer 18481dacd8bSHiroki Sato static struct protox *protoprotox[] = { 185cfa1ca9dSYoshinobu Inoue protox, 186cfa1ca9dSYoshinobu Inoue #ifdef INET6 187cfa1ca9dSYoshinobu Inoue ip6protox, 188cfa1ca9dSYoshinobu Inoue #endif 1893b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 1903b8a8567SJun-ichiro itojun Hagino pfkeyprotox, 1913b8a8567SJun-ichiro itojun Hagino #endif 19245c203fcSGleb Smirnoff NULL }; 1939b50d902SRodney W. Grimes 194ade9ccfeSMarcel Moolenaar static void printproto(struct protox *, const char *, bool *); 1955e051718SAssar Westerlund static void usage(void); 196096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *); 197096146f8SYaroslav Tykhiy static struct protox *knownname(const char *); 1989b50d902SRodney W. Grimes 19981dacd8bSHiroki Sato static int kresolve_list(struct nlist *_nl); 20081dacd8bSHiroki Sato 201c6620a13SPoul-Henning Kamp static kvm_t *kvmd; 202080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL; 203080b7f49SDag-Erling Smørgrav 204080b7f49SDag-Erling Smørgrav int Aflag; /* show addresses of protocol control block */ 205080b7f49SDag-Erling Smørgrav int aflag; /* show all sockets (including servers) */ 20681dacd8bSHiroki Sato static int Bflag; /* show information about bpf consumers */ 207080b7f49SDag-Erling Smørgrav int bflag; /* show i/f total bytes in/out */ 2080e5e35e3SRichard Scheffenegger int cflag; /* show TCP congestion control stack */ 2090e5e35e3SRichard Scheffenegger int Cflag; /* show congestion control algo and vars */ 210080b7f49SDag-Erling Smørgrav int dflag; /* show i/f dropped packets */ 211080b7f49SDag-Erling Smørgrav int gflag; /* show group (multicast) routing or stats */ 212c2dfd19fSGleb Smirnoff int hflag; /* show counters in human readable format */ 213080b7f49SDag-Erling Smørgrav int iflag; /* show interfaces */ 214080b7f49SDag-Erling Smørgrav int Lflag; /* show size of listen queues */ 215080b7f49SDag-Erling Smørgrav int mflag; /* show memory stats */ 216bf10ffe1SXin LI int noutputs = 0; /* how much outputs before we exit */ 21765ea0024SAssar Westerlund int numeric_addr; /* show addresses numerically */ 21865ea0024SAssar Westerlund int numeric_port; /* show ports numerically */ 219fedeb08bSAlexander V. Chernikov int Oflag; /* show nhgrp objects*/ 220a6663252SAlexander V. Chernikov int oflag; /* show nexthop objects*/ 2212529f56eSJonathan T. Looney int Pflag; /* show TCP log ID */ 222cf5e44f8SRuslan Ermilov static int pflag; /* show given protocol */ 22381dacd8bSHiroki Sato static int Qflag; /* show netisr information */ 224080b7f49SDag-Erling Smørgrav int rflag; /* show routing tables (or routing stats) */ 22585b0f0f3SAdrian Chadd int Rflag; /* show flow / RSS statistics */ 226080b7f49SDag-Erling Smørgrav int sflag; /* show protocol statistics */ 227080b7f49SDag-Erling Smørgrav int Wflag; /* wide display */ 228f5d34df5SGeorge V. Neville-Neil int Tflag; /* TCP Information */ 22949f287f8SGeorge V. Neville-Neil int xflag; /* extra information, includes all socket buffer info */ 230c73d99b5SRuslan Ermilov int zflag; /* zero stats */ 231080b7f49SDag-Erling Smørgrav 232080b7f49SDag-Erling Smørgrav int interval; /* repeat interval for i/f stats */ 233080b7f49SDag-Erling Smørgrav 234080b7f49SDag-Erling Smørgrav char *interface; /* desired i/f for stats, or NULL for all i/fs */ 235080b7f49SDag-Erling Smørgrav int unit; /* unit number for above */ 236080b7f49SDag-Erling Smørgrav 23781dacd8bSHiroki Sato static int af; /* address family */ 238feda1a43SJohn Baldwin int live; /* true if we are examining a live system */ 2399b50d902SRodney W. Grimes 2409b50d902SRodney W. Grimes int 241a01e3379SDavid Malone main(int argc, char *argv[]) 2429b50d902SRodney W. Grimes { 243a01e3379SDavid Malone struct protox *tp = NULL; /* for printing cblocks & stats */ 2449b50d902SRodney W. Grimes int ch; 2453fddef95SHiroki Sato int fib = -1; 2463fddef95SHiroki Sato char *endptr; 247ade9ccfeSMarcel Moolenaar bool first = true; 2489b50d902SRodney W. Grimes 2499b50d902SRodney W. Grimes af = AF_UNSPEC; 2509b50d902SRodney W. Grimes 251ade9ccfeSMarcel Moolenaar argc = xo_parse_args(argc, argv); 25206ff7ccbSHajimu UMEMOTO if (argc < 0) 25306ff7ccbSHajimu UMEMOTO exit(EXIT_FAILURE); 254ade9ccfeSMarcel Moolenaar 2550e5e35e3SRichard Scheffenegger while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:iLlM:mN:nOoPp:Qq:RrSTsuWw:xz")) 2560153eb66SRobert Watson != -1) 2579b50d902SRodney W. Grimes switch(ch) { 25817ed2e8eSAlexander V. Chernikov case '4': 25917ed2e8eSAlexander V. Chernikov #ifdef INET 26017ed2e8eSAlexander V. Chernikov af = AF_INET; 26117ed2e8eSAlexander V. Chernikov #else 26217ed2e8eSAlexander V. Chernikov errx(1, "IPv4 support is not compiled in"); 26317ed2e8eSAlexander V. Chernikov #endif 26417ed2e8eSAlexander V. Chernikov break; 26517ed2e8eSAlexander V. Chernikov case '6': 26617ed2e8eSAlexander V. Chernikov #ifdef INET6 26717ed2e8eSAlexander V. Chernikov af = AF_INET6; 26817ed2e8eSAlexander V. Chernikov #else 26917ed2e8eSAlexander V. Chernikov errx(1, "IPv6 support is not compiled in"); 27017ed2e8eSAlexander V. Chernikov #endif 27117ed2e8eSAlexander V. Chernikov break; 2729b50d902SRodney W. Grimes case 'A': 2739b50d902SRodney W. Grimes Aflag = 1; 2749b50d902SRodney W. Grimes break; 2759b50d902SRodney W. Grimes case 'a': 2769b50d902SRodney W. Grimes aflag = 1; 2779b50d902SRodney W. Grimes break; 2786b463eedSChristian S.J. Peron case 'B': 2796b463eedSChristian S.J. Peron Bflag = 1; 2806b463eedSChristian S.J. Peron break; 281e1e293a5SDavid Greenman case 'b': 282e1e293a5SDavid Greenman bflag = 1; 283e1e293a5SDavid Greenman break; 2840e5e35e3SRichard Scheffenegger case 'c': 2850e5e35e3SRichard Scheffenegger cflag = 1; 2860e5e35e3SRichard Scheffenegger break; 287b98a21f6SMichael Tuexen case 'C': 288b98a21f6SMichael Tuexen Cflag = 1; 289b98a21f6SMichael Tuexen break; 2909b50d902SRodney W. Grimes case 'd': 2919b50d902SRodney W. Grimes dflag = 1; 2929b50d902SRodney W. Grimes break; 2933fddef95SHiroki Sato case 'F': 2943fddef95SHiroki Sato fib = strtol(optarg, &endptr, 0); 2953fddef95SHiroki Sato if (*endptr != '\0' || 2963fddef95SHiroki Sato (fib == 0 && (errno == EINVAL || errno == ERANGE))) 297ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: invalid fib", optarg); 2983fddef95SHiroki Sato break; 2999b50d902SRodney W. Grimes case 'f': 3002c284d93SGleb Smirnoff if (strcmp(optarg, "inet") == 0) 3019b50d902SRodney W. Grimes af = AF_INET; 302cfa1ca9dSYoshinobu Inoue #ifdef INET6 303cfa1ca9dSYoshinobu Inoue else if (strcmp(optarg, "inet6") == 0) 304cfa1ca9dSYoshinobu Inoue af = AF_INET6; 305e5134d2eSMax Laier #endif 306e5134d2eSMax Laier #ifdef IPSEC 3073b8a8567SJun-ichiro itojun Hagino else if (strcmp(optarg, "pfkey") == 0) 3083b8a8567SJun-ichiro itojun Hagino af = PF_KEY; 309e5134d2eSMax Laier #endif 31010d5269fSHiroki Sato else if (strcmp(optarg, "unix") == 0 || 31110d5269fSHiroki Sato strcmp(optarg, "local") == 0) 3129b50d902SRodney W. Grimes af = AF_UNIX; 313690f477dSSam Leffler #ifdef NETGRAPH 3144cf49a43SJulian Elischer else if (strcmp(optarg, "ng") == 0 3154cf49a43SJulian Elischer || strcmp(optarg, "netgraph") == 0) 3164cf49a43SJulian Elischer af = AF_NETGRAPH; 317690f477dSSam Leffler #endif 318d44ddba9SRuslan Ermilov else if (strcmp(optarg, "link") == 0) 319d44ddba9SRuslan Ermilov af = AF_LINK; 3209b50d902SRodney W. Grimes else { 321ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: unknown address family", 322ade9ccfeSMarcel Moolenaar optarg); 3239b50d902SRodney W. Grimes } 3249b50d902SRodney W. Grimes break; 3259b50d902SRodney W. Grimes case 'g': 3269b50d902SRodney W. Grimes gflag = 1; 3279b50d902SRodney W. Grimes break; 328c2dfd19fSGleb Smirnoff case 'h': 329c2dfd19fSGleb Smirnoff hflag = 1; 330c2dfd19fSGleb Smirnoff break; 3319b50d902SRodney W. Grimes case 'I': { 3329b50d902SRodney W. Grimes char *cp; 3339b50d902SRodney W. Grimes 3349b50d902SRodney W. Grimes iflag = 1; 3359b50d902SRodney W. Grimes for (cp = interface = optarg; isalpha(*cp); cp++) 3369b50d902SRodney W. Grimes continue; 3379b50d902SRodney W. Grimes unit = atoi(cp); 3389b50d902SRodney W. Grimes break; 3399b50d902SRodney W. Grimes } 3409b50d902SRodney W. Grimes case 'i': 3419b50d902SRodney W. Grimes iflag = 1; 3429b50d902SRodney W. Grimes break; 343ac55add0SGuido van Rooij case 'L': 344ac55add0SGuido van Rooij Lflag = 1; 345ac55add0SGuido van Rooij break; 3469b50d902SRodney W. Grimes case 'M': 3479b50d902SRodney W. Grimes memf = optarg; 3489b50d902SRodney W. Grimes break; 3499b50d902SRodney W. Grimes case 'm': 3509b50d902SRodney W. Grimes mflag = 1; 3519b50d902SRodney W. Grimes break; 3529b50d902SRodney W. Grimes case 'N': 3539b50d902SRodney W. Grimes nlistf = optarg; 3549b50d902SRodney W. Grimes break; 3559b50d902SRodney W. Grimes case 'n': 35665ea0024SAssar Westerlund numeric_addr = numeric_port = 1; 3579b50d902SRodney W. Grimes break; 358a6663252SAlexander V. Chernikov case 'o': 359a6663252SAlexander V. Chernikov oflag = 1; 360a6663252SAlexander V. Chernikov break; 361fedeb08bSAlexander V. Chernikov case 'O': 362fedeb08bSAlexander V. Chernikov Oflag = 1; 363fedeb08bSAlexander V. Chernikov break; 3642529f56eSJonathan T. Looney case 'P': 3652529f56eSJonathan T. Looney Pflag = 1; 3662529f56eSJonathan T. Looney break; 3679b50d902SRodney W. Grimes case 'p': 3689b50d902SRodney W. Grimes if ((tp = name2protox(optarg)) == NULL) { 369ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: unknown or uninstrumented " 370ade9ccfeSMarcel Moolenaar "protocol", optarg); 3719b50d902SRodney W. Grimes } 3729b50d902SRodney W. Grimes pflag = 1; 3739b50d902SRodney W. Grimes break; 3740153eb66SRobert Watson case 'Q': 3750153eb66SRobert Watson Qflag = 1; 3760153eb66SRobert Watson break; 377bf10ffe1SXin LI case 'q': 378bf10ffe1SXin LI noutputs = atoi(optarg); 379bf10ffe1SXin LI if (noutputs != 0) 380bf10ffe1SXin LI noutputs++; 381bf10ffe1SXin LI break; 3829b50d902SRodney W. Grimes case 'r': 3839b50d902SRodney W. Grimes rflag = 1; 3849b50d902SRodney W. Grimes break; 38585b0f0f3SAdrian Chadd case 'R': 38685b0f0f3SAdrian Chadd Rflag = 1; 38785b0f0f3SAdrian Chadd break; 3889b50d902SRodney W. Grimes case 's': 3899b50d902SRodney W. Grimes ++sflag; 3909b50d902SRodney W. Grimes break; 39165ea0024SAssar Westerlund case 'S': 39265ea0024SAssar Westerlund numeric_addr = 1; 39365ea0024SAssar Westerlund break; 3949b50d902SRodney W. Grimes case 'u': 3959b50d902SRodney W. Grimes af = AF_UNIX; 3969b50d902SRodney W. Grimes break; 397080b7f49SDag-Erling Smørgrav case 'W': 39870057abfSRuslan Ermilov case 'l': 399080b7f49SDag-Erling Smørgrav Wflag = 1; 400080b7f49SDag-Erling Smørgrav break; 4019b50d902SRodney W. Grimes case 'w': 4029b50d902SRodney W. Grimes interval = atoi(optarg); 4039b50d902SRodney W. Grimes iflag = 1; 4049b50d902SRodney W. Grimes break; 405f5d34df5SGeorge V. Neville-Neil case 'T': 406f5d34df5SGeorge V. Neville-Neil Tflag = 1; 407f5d34df5SGeorge V. Neville-Neil break; 40849f287f8SGeorge V. Neville-Neil case 'x': 40949f287f8SGeorge V. Neville-Neil xflag = 1; 41049f287f8SGeorge V. Neville-Neil break; 411c73d99b5SRuslan Ermilov case 'z': 412c73d99b5SRuslan Ermilov zflag = 1; 413c73d99b5SRuslan Ermilov break; 4149b50d902SRodney W. Grimes case '?': 4159b50d902SRodney W. Grimes default: 4169b50d902SRodney W. Grimes usage(); 4179b50d902SRodney W. Grimes } 4189b50d902SRodney W. Grimes argv += optind; 4199b50d902SRodney W. Grimes argc -= optind; 4209b50d902SRodney W. Grimes 4219b50d902SRodney W. Grimes #define BACKWARD_COMPATIBILITY 4229b50d902SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 4239b50d902SRodney W. Grimes if (*argv) { 4249b50d902SRodney W. Grimes if (isdigit(**argv)) { 4259b50d902SRodney W. Grimes interval = atoi(*argv); 4269b50d902SRodney W. Grimes if (interval <= 0) 4279b50d902SRodney W. Grimes usage(); 4289b50d902SRodney W. Grimes ++argv; 4299b50d902SRodney W. Grimes iflag = 1; 4309b50d902SRodney W. Grimes } 4319b50d902SRodney W. Grimes if (*argv) { 4329b50d902SRodney W. Grimes nlistf = *argv; 4339b50d902SRodney W. Grimes if (*++argv) 4349b50d902SRodney W. Grimes memf = *argv; 4359b50d902SRodney W. Grimes } 4369b50d902SRodney W. Grimes } 4379b50d902SRodney W. Grimes #endif 4389b50d902SRodney W. Grimes 4399b50d902SRodney W. Grimes /* 4409b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 4419b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 4429b50d902SRodney W. Grimes */ 443feda1a43SJohn Baldwin live = (nlistf == NULL && memf == NULL); 444a4a889a0SXin LI if (!live) { 445a4a889a0SXin LI if (setgid(getgid()) != 0) 446a4a889a0SXin LI xo_err(-1, "setgid"); 4471caaf3eaSBaptiste Daroussin /* Load all necessary kvm symbols */ 4481caaf3eaSBaptiste Daroussin kresolve_list(nl); 449a4a889a0SXin LI } 4509b50d902SRodney W. Grimes 451f5d34df5SGeorge V. Neville-Neil if (xflag && Tflag) 452ade9ccfeSMarcel Moolenaar xo_errx(1, "-x and -T are incompatible, pick one."); 453f5d34df5SGeorge V. Neville-Neil 4546b463eedSChristian S.J. Peron if (Bflag) { 455feda1a43SJohn Baldwin if (!live) 456feda1a43SJohn Baldwin usage(); 4576b463eedSChristian S.J. Peron bpf_stats(interface); 458ade9ccfeSMarcel Moolenaar xo_finish(); 4596b463eedSChristian S.J. Peron exit(0); 4606b463eedSChristian S.J. Peron } 4619b50d902SRodney W. Grimes if (mflag) { 46283708764SRuslan Ermilov if (!live) { 463feda1a43SJohn Baldwin if (kread(0, NULL, 0) == 0) 46405d1f5bcSAndrey V. Elsukov mbpr(kvmd, nl[N_SFSTAT].n_value); 465d15c5f56SRuslan Ermilov } else 466d4426f28SRobert Watson mbpr(NULL, 0); 467ade9ccfeSMarcel Moolenaar xo_finish(); 4689b50d902SRodney W. Grimes exit(0); 4699b50d902SRodney W. Grimes } 4700153eb66SRobert Watson if (Qflag) { 47188737be2SRobert Watson if (!live) { 47288737be2SRobert Watson if (kread(0, NULL, 0) == 0) 47381dacd8bSHiroki Sato netisr_stats(); 47488737be2SRobert Watson } else 47581dacd8bSHiroki Sato netisr_stats(); 476ade9ccfeSMarcel Moolenaar xo_finish(); 4770153eb66SRobert Watson exit(0); 4780153eb66SRobert Watson } 479cc63cd56SPeter Wemm #if 0 4809b50d902SRodney W. Grimes /* 4819b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 4829b50d902SRodney W. Grimes * of open/close on each call to get* routines. 4839b50d902SRodney W. Grimes */ 4849b50d902SRodney W. Grimes sethostent(1); 4859b50d902SRodney W. Grimes setnetent(1); 486cc63cd56SPeter Wemm #else 487cc63cd56SPeter Wemm /* 488cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 489cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 490cc63cd56SPeter Wemm * used for the queries, which is slower. 491cc63cd56SPeter Wemm */ 492cc63cd56SPeter Wemm #endif 493cf5e44f8SRuslan Ermilov if (iflag && !sflag) { 494ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 49510d5269fSHiroki Sato intpr(NULL, af); 496ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 497ade9ccfeSMarcel Moolenaar xo_finish(); 4989b50d902SRodney W. Grimes exit(0); 4999b50d902SRodney W. Grimes } 5009b50d902SRodney W. Grimes if (rflag) { 501ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5025d6d7e75SGleb Smirnoff if (sflag) { 5032ccdd4b3SBaptiste Daroussin if (live) { 5042ccdd4b3SBaptiste Daroussin kresolve_list(nl); 5052ccdd4b3SBaptiste Daroussin } 506fc47e028SAlexander V. Chernikov rt_stats(); 5075d6d7e75SGleb Smirnoff } else 508fc47e028SAlexander V. Chernikov routepr(fib, af); 509ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 510ade9ccfeSMarcel Moolenaar xo_finish(); 5119b50d902SRodney W. Grimes exit(0); 5129b50d902SRodney W. Grimes } 513a6663252SAlexander V. Chernikov if (oflag) { 514a6663252SAlexander V. Chernikov xo_open_container("statistics"); 515a6663252SAlexander V. Chernikov nhops_print(fib, af); 516a6663252SAlexander V. Chernikov xo_close_container("statistics"); 517a6663252SAlexander V. Chernikov xo_finish(); 518a6663252SAlexander V. Chernikov exit(0); 519a6663252SAlexander V. Chernikov } 520fedeb08bSAlexander V. Chernikov if (Oflag) { 521fedeb08bSAlexander V. Chernikov xo_open_container("statistics"); 522fedeb08bSAlexander V. Chernikov nhgrp_print(fib, af); 523fedeb08bSAlexander V. Chernikov xo_close_container("statistics"); 524fedeb08bSAlexander V. Chernikov xo_finish(); 525fedeb08bSAlexander V. Chernikov exit(0); 526fedeb08bSAlexander V. Chernikov } 527fedeb08bSAlexander V. Chernikov 528a6663252SAlexander V. Chernikov 529fc47e028SAlexander V. Chernikov 5309b50d902SRodney W. Grimes if (gflag) { 531ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 532cfa1ca9dSYoshinobu Inoue if (sflag) { 533cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 534fc47e028SAlexander V. Chernikov mrt_stats(); 535cfa1ca9dSYoshinobu Inoue #ifdef INET6 536cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 537fc47e028SAlexander V. Chernikov mrt6_stats(); 538cfa1ca9dSYoshinobu Inoue #endif 539cfa1ca9dSYoshinobu Inoue } else { 540cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 541fc47e028SAlexander V. Chernikov mroutepr(); 542cfa1ca9dSYoshinobu Inoue #ifdef INET6 543cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 544fc47e028SAlexander V. Chernikov mroute6pr(); 545cfa1ca9dSYoshinobu Inoue #endif 546cfa1ca9dSYoshinobu Inoue } 547ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 548ade9ccfeSMarcel Moolenaar xo_finish(); 5499b50d902SRodney W. Grimes exit(0); 5509b50d902SRodney W. Grimes } 551cfa1ca9dSYoshinobu Inoue 552cf5e44f8SRuslan Ermilov if (tp) { 553ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 554ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 555ade9ccfeSMarcel Moolenaar if (!first) 556ade9ccfeSMarcel Moolenaar xo_close_list("socket"); 557ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 558ade9ccfeSMarcel Moolenaar xo_finish(); 559cf5e44f8SRuslan Ermilov exit(0); 560cf5e44f8SRuslan Ermilov } 561ade9ccfeSMarcel Moolenaar 562ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 563cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 5649b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 565ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 566cfa1ca9dSYoshinobu Inoue #ifdef INET6 567cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 568cfa1ca9dSYoshinobu Inoue for (tp = ip6protox; tp->pr_name; tp++) 569ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 570cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 5713b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 5723b8a8567SJun-ichiro itojun Hagino if (af == PF_KEY || af == AF_UNSPEC) 5733b8a8567SJun-ichiro itojun Hagino for (tp = pfkeyprotox; tp->pr_name; tp++) 574ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 5753b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/ 576690f477dSSam Leffler #ifdef NETGRAPH 5774cf49a43SJulian Elischer if (af == AF_NETGRAPH || af == AF_UNSPEC) 5784cf49a43SJulian Elischer for (tp = netgraphprotox; tp->pr_name; tp++) 579ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 580690f477dSSam Leffler #endif /* NETGRAPH */ 581f1c0a78dSMaxim Konovalov if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 582feda1a43SJohn Baldwin unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 583963b7ccdSRobert Watson nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 584ade9ccfeSMarcel Moolenaar nl[N_UNP_SPHEAD].n_value, &first); 585ade9ccfeSMarcel Moolenaar 586ade9ccfeSMarcel Moolenaar if (!first) 587ade9ccfeSMarcel Moolenaar xo_close_list("socket"); 588ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 589ade9ccfeSMarcel Moolenaar xo_finish(); 5909b50d902SRodney W. Grimes exit(0); 5919b50d902SRodney W. Grimes } 5929b50d902SRodney W. Grimes 593dbfd8708SGleb Smirnoff static int 594dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats, 595dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn, int zero) 5969eddb899SMark Johnston { 5979eddb899SMark Johnston int error; 5989eddb899SMark Johnston 5999eddb899SMark Johnston if (live) { 6009eddb899SMark Johnston memset(stats, 0, len); 601dbfd8708SGleb Smirnoff if (zero) 6029eddb899SMark Johnston error = sysctlbyname(sysctlname, NULL, NULL, stats, 6039eddb899SMark Johnston len); 6049eddb899SMark Johnston else 6059eddb899SMark Johnston error = sysctlbyname(sysctlname, stats, &len, NULL, 0); 6069eddb899SMark Johnston if (error == -1 && errno != ENOENT) 6079eddb899SMark Johnston xo_warn("sysctl %s", sysctlname); 6089eddb899SMark Johnston } else { 6099eddb899SMark Johnston if (off == 0) 6109eddb899SMark Johnston return (1); 6119eddb899SMark Johnston error = kreadfn(off, stats, len); 6129eddb899SMark Johnston } 6139eddb899SMark Johnston return (error); 6149eddb899SMark Johnston } 6159eddb899SMark Johnston 616dbfd8708SGleb Smirnoff int 617dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats, 618dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn) 619dbfd8708SGleb Smirnoff { 620dbfd8708SGleb Smirnoff 621dbfd8708SGleb Smirnoff return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 622dbfd8708SGleb Smirnoff zflag)); 623dbfd8708SGleb Smirnoff } 624dbfd8708SGleb Smirnoff 625dbfd8708SGleb Smirnoff int 626dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats, 627dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn) 628dbfd8708SGleb Smirnoff { 629dbfd8708SGleb Smirnoff 630dbfd8708SGleb Smirnoff return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); 631dbfd8708SGleb Smirnoff } 632dbfd8708SGleb Smirnoff 6339b50d902SRodney W. Grimes /* 6349b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 6359b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 6369b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 6379b50d902SRodney W. Grimes */ 6389b50d902SRodney W. Grimes static void 639ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first) 6409b50d902SRodney W. Grimes { 641feda1a43SJohn Baldwin void (*pr)(u_long, const char *, int, int); 6429b50d902SRodney W. Grimes u_long off; 643ade9ccfeSMarcel Moolenaar bool doingdblocks = false; 6449b50d902SRodney W. Grimes 6459b50d902SRodney W. Grimes if (sflag) { 646cfa1ca9dSYoshinobu Inoue if (iflag) { 647cfa1ca9dSYoshinobu Inoue if (tp->pr_istats) 64810d5269fSHiroki Sato intpr(tp->pr_istats, af); 649cf5e44f8SRuslan Ermilov else if (pflag) 650ade9ccfeSMarcel Moolenaar xo_message("%s: no per-interface stats routine", 651cf5e44f8SRuslan Ermilov tp->pr_name); 652cfa1ca9dSYoshinobu Inoue return; 653feda1a43SJohn Baldwin } else { 6549b50d902SRodney W. Grimes pr = tp->pr_stats; 655cf5e44f8SRuslan Ermilov if (!pr) { 656cf5e44f8SRuslan Ermilov if (pflag) 657ade9ccfeSMarcel Moolenaar xo_message("%s: no stats routine", 658cf5e44f8SRuslan Ermilov tp->pr_name); 659cf5e44f8SRuslan Ermilov return; 660cf5e44f8SRuslan Ermilov } 661feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 662feda1a43SJohn Baldwin off = 0; 663feda1a43SJohn Baldwin else if (tp->pr_sindex < 0) { 664feda1a43SJohn Baldwin if (pflag) 665ade9ccfeSMarcel Moolenaar xo_message("%s: stats routine doesn't " 666ade9ccfeSMarcel Moolenaar "work on cores", tp->pr_name); 667feda1a43SJohn Baldwin return; 668feda1a43SJohn Baldwin } else 669feda1a43SJohn Baldwin off = nl[tp->pr_sindex].n_value; 670cfa1ca9dSYoshinobu Inoue } 6719b50d902SRodney W. Grimes } else { 672ade9ccfeSMarcel Moolenaar doingdblocks = true; 6739b50d902SRodney W. Grimes pr = tp->pr_cblocks; 674cf5e44f8SRuslan Ermilov if (!pr) { 675cf5e44f8SRuslan Ermilov if (pflag) 676ade9ccfeSMarcel Moolenaar xo_message("%s: no PCB routine", tp->pr_name); 677cf5e44f8SRuslan Ermilov return; 678cf5e44f8SRuslan Ermilov } 679feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 680feda1a43SJohn Baldwin off = 0; 681feda1a43SJohn Baldwin else if (tp->pr_index < 0) { 682feda1a43SJohn Baldwin if (pflag) 683ade9ccfeSMarcel Moolenaar xo_message("%s: PCB routine doesn't work on " 684ade9ccfeSMarcel Moolenaar "cores", tp->pr_name); 685feda1a43SJohn Baldwin return; 686feda1a43SJohn Baldwin } else 687feda1a43SJohn Baldwin off = nl[tp->pr_index].n_value; 6889b50d902SRodney W. Grimes } 689feda1a43SJohn Baldwin if (pr != NULL && (off || (live && tp->pr_usesysctl) || 690ade9ccfeSMarcel Moolenaar af != AF_UNSPEC)) { 691ade9ccfeSMarcel Moolenaar if (doingdblocks && *first) { 692ade9ccfeSMarcel Moolenaar xo_open_list("socket"); 693ade9ccfeSMarcel Moolenaar *first = false; 694ade9ccfeSMarcel Moolenaar } 695ade9ccfeSMarcel Moolenaar 696feda1a43SJohn Baldwin (*pr)(off, name, af, tp->pr_protocol); 6979b50d902SRodney W. Grimes } 698ade9ccfeSMarcel Moolenaar } 6999b50d902SRodney W. Grimes 70029dde48dSGleb Smirnoff static int 70129dde48dSGleb Smirnoff kvmd_init(void) 7029b50d902SRodney W. Grimes { 703feda1a43SJohn Baldwin char errbuf[_POSIX2_LINE_MAX]; 704feda1a43SJohn Baldwin 70529dde48dSGleb Smirnoff if (kvmd != NULL) 70629dde48dSGleb Smirnoff return (0); 70729dde48dSGleb Smirnoff 708feda1a43SJohn Baldwin kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 709a4a889a0SXin LI if (setgid(getgid()) != 0) 710a4a889a0SXin LI xo_err(-1, "setgid"); 71129dde48dSGleb Smirnoff 71229dde48dSGleb Smirnoff if (kvmd == NULL) { 713ade9ccfeSMarcel Moolenaar xo_warnx("kvm not available: %s", errbuf); 71429dde48dSGleb Smirnoff return (-1); 71529dde48dSGleb Smirnoff } 71629dde48dSGleb Smirnoff 717fc47e028SAlexander V. Chernikov return (0); 718fc47e028SAlexander V. Chernikov } 719fc47e028SAlexander V. Chernikov 720fc47e028SAlexander V. Chernikov /* 721fc47e028SAlexander V. Chernikov * Resolve symbol list, return 0 on success. 722fc47e028SAlexander V. Chernikov */ 72381dacd8bSHiroki Sato static int 724fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl) 725fc47e028SAlexander V. Chernikov { 726fc47e028SAlexander V. Chernikov 727fc47e028SAlexander V. Chernikov if ((kvmd == NULL) && (kvmd_init() != 0)) 728fc47e028SAlexander V. Chernikov return (-1); 729fc47e028SAlexander V. Chernikov 730fc47e028SAlexander V. Chernikov if (_nl[0].n_type != 0) 731fc47e028SAlexander V. Chernikov return (0); 732fc47e028SAlexander V. Chernikov 733fc47e028SAlexander V. Chernikov if (kvm_nlist(kvmd, _nl) < 0) { 73499453c6aSPoul-Henning Kamp if (nlistf) 735ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: kvm_nlist: %s", nlistf, 73699453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 73799453c6aSPoul-Henning Kamp else 738ade9ccfeSMarcel Moolenaar xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 73999453c6aSPoul-Henning Kamp } 74099453c6aSPoul-Henning Kamp 74129dde48dSGleb Smirnoff return (0); 74229dde48dSGleb Smirnoff } 74329dde48dSGleb Smirnoff 74429dde48dSGleb Smirnoff /* 74581dacd8bSHiroki Sato * Wrapper of kvm_dpcpu_setcpu(). 74681dacd8bSHiroki Sato */ 74781dacd8bSHiroki Sato void 74881dacd8bSHiroki Sato kset_dpcpu(u_int cpuid) 74981dacd8bSHiroki Sato { 75081dacd8bSHiroki Sato 75181dacd8bSHiroki Sato if ((kvmd == NULL) && (kvmd_init() != 0)) 75281dacd8bSHiroki Sato xo_errx(-1, "%s: kvm is not available", __func__); 75381dacd8bSHiroki Sato 75481dacd8bSHiroki Sato if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0) 75581dacd8bSHiroki Sato xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__, 75681dacd8bSHiroki Sato cpuid, kvm_geterr(kvmd)); 75781dacd8bSHiroki Sato return; 75881dacd8bSHiroki Sato } 75981dacd8bSHiroki Sato 76081dacd8bSHiroki Sato /* 76129dde48dSGleb Smirnoff * Read kernel memory, return 0 on success. 76229dde48dSGleb Smirnoff */ 76329dde48dSGleb Smirnoff int 76429dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size) 76529dde48dSGleb Smirnoff { 76629dde48dSGleb Smirnoff 76729dde48dSGleb Smirnoff if (kvmd_init() < 0) 76899453c6aSPoul-Henning Kamp return (-1); 76929dde48dSGleb Smirnoff 770c6620a13SPoul-Henning Kamp if (!buf) 771c6620a13SPoul-Henning Kamp return (0); 772feda1a43SJohn Baldwin if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 773ade9ccfeSMarcel Moolenaar xo_warnx("%s", kvm_geterr(kvmd)); 7749b50d902SRodney W. Grimes return (-1); 7759b50d902SRodney W. Grimes } 7769b50d902SRodney W. Grimes return (0); 7779b50d902SRodney W. Grimes } 7789b50d902SRodney W. Grimes 77929dde48dSGleb Smirnoff /* 780e3a7aa6fSGleb Smirnoff * Read single counter(9). 781e3a7aa6fSGleb Smirnoff */ 782e3a7aa6fSGleb Smirnoff uint64_t 783e3a7aa6fSGleb Smirnoff kread_counter(u_long addr) 784e3a7aa6fSGleb Smirnoff { 785e3a7aa6fSGleb Smirnoff 786e3a7aa6fSGleb Smirnoff if (kvmd_init() < 0) 787e3a7aa6fSGleb Smirnoff return (-1); 788e3a7aa6fSGleb Smirnoff 789e3a7aa6fSGleb Smirnoff return (kvm_counter_u64_fetch(kvmd, addr)); 790e3a7aa6fSGleb Smirnoff } 791e3a7aa6fSGleb Smirnoff 792e3a7aa6fSGleb Smirnoff /* 79329dde48dSGleb Smirnoff * Read an array of N counters in kernel memory into array of N uint64_t's. 79429dde48dSGleb Smirnoff */ 79529dde48dSGleb Smirnoff int 7965da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size) 79729dde48dSGleb Smirnoff { 798794c57d3SMark Johnston uint64_t *c; 799794c57d3SMark Johnston u_long *counters; 800794c57d3SMark Johnston size_t i, n; 80129dde48dSGleb Smirnoff 80229dde48dSGleb Smirnoff if (kvmd_init() < 0) 80329dde48dSGleb Smirnoff return (-1); 80429dde48dSGleb Smirnoff 805794c57d3SMark Johnston if (size % sizeof(uint64_t) != 0) { 806794c57d3SMark Johnston xo_warnx("kread_counters: invalid counter set size"); 8075da0521fSAndrey V. Elsukov return (-1); 8085da0521fSAndrey V. Elsukov } 809794c57d3SMark Johnston 810794c57d3SMark Johnston n = size / sizeof(uint64_t); 811794c57d3SMark Johnston if ((counters = malloc(n * sizeof(u_long))) == NULL) 812794c57d3SMark Johnston xo_err(-1, "malloc"); 813794c57d3SMark Johnston if (kread(addr, counters, n * sizeof(u_long)) < 0) { 814794c57d3SMark Johnston free(counters); 815794c57d3SMark Johnston return (-1); 816794c57d3SMark Johnston } 817794c57d3SMark Johnston 818794c57d3SMark Johnston c = buf; 819794c57d3SMark Johnston for (i = 0; i < n; i++) 820794c57d3SMark Johnston c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); 821794c57d3SMark Johnston 822794c57d3SMark Johnston free(counters); 82329dde48dSGleb Smirnoff return (0); 82429dde48dSGleb Smirnoff } 82529dde48dSGleb Smirnoff 826a01e3379SDavid Malone const char * 8277b95a1ebSYaroslav Tykhiy plural(uintmax_t n) 8289b50d902SRodney W. Grimes { 8299b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 8309b50d902SRodney W. Grimes } 8319b50d902SRodney W. Grimes 832a01e3379SDavid Malone const char * 8337b95a1ebSYaroslav Tykhiy plurales(uintmax_t n) 8349b50d902SRodney W. Grimes { 8359b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 8369b50d902SRodney W. Grimes } 8379b50d902SRodney W. Grimes 838f99a4046SMike Makonnen const char * 8397b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n) 840f99a4046SMike Makonnen { 841f99a4046SMike Makonnen return (n != 1 ? "ies" : "y"); 842f99a4046SMike Makonnen } 843f99a4046SMike Makonnen 8449b50d902SRodney W. Grimes /* 8459b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 8469b50d902SRodney W. Grimes */ 8479b50d902SRodney W. Grimes static struct protox * 848096146f8SYaroslav Tykhiy knownname(const char *name) 8499b50d902SRodney W. Grimes { 8509b50d902SRodney W. Grimes struct protox **tpp, *tp; 8519b50d902SRodney W. Grimes 8529b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 8539b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 8549b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 8559b50d902SRodney W. Grimes return (tp); 8569b50d902SRodney W. Grimes return (NULL); 8579b50d902SRodney W. Grimes } 8589b50d902SRodney W. Grimes 8599b50d902SRodney W. Grimes /* 8609b50d902SRodney W. Grimes * Find the protox corresponding to name. 8619b50d902SRodney W. Grimes */ 8629b50d902SRodney W. Grimes static struct protox * 863096146f8SYaroslav Tykhiy name2protox(const char *name) 8649b50d902SRodney W. Grimes { 8659b50d902SRodney W. Grimes struct protox *tp; 8669b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 8679b50d902SRodney W. Grimes struct protoent *p; 8689b50d902SRodney W. Grimes 8699b50d902SRodney W. Grimes /* 8709b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 8719b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 8729b50d902SRodney W. Grimes */ 873cfa1ca9dSYoshinobu Inoue if ((tp = knownname(name)) != NULL) 8749b50d902SRodney W. Grimes return (tp); 8759b50d902SRodney W. Grimes 8769b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 877cfa1ca9dSYoshinobu Inoue while ((p = getprotoent()) != NULL) { 8789b50d902SRodney W. Grimes /* assert: name not same as p->name */ 8799b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 8809b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 8819b50d902SRodney W. Grimes endprotoent(); 8829b50d902SRodney W. Grimes return (knownname(p->p_name)); 8839b50d902SRodney W. Grimes } 8849b50d902SRodney W. Grimes } 8859b50d902SRodney W. Grimes endprotoent(); 8869b50d902SRodney W. Grimes return (NULL); 8879b50d902SRodney W. Grimes } 8889b50d902SRodney W. Grimes 8899b50d902SRodney W. Grimes static void 8905e051718SAssar Westerlund usage(void) 8919b50d902SRodney W. Grimes { 892ade9ccfeSMarcel Moolenaar (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", 8930e5e35e3SRichard Scheffenegger "usage: netstat [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n" 8941cb467b1SRuslan Ermilov " [-M core] [-N system]", 89517ed2e8eSAlexander V. Chernikov " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" 896d44ddba9SRuslan Ermilov " [-M core] [-N system]", 897fd2c6bc9SAllan Jude " netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n" 898fd2c6bc9SAllan Jude " [-q howmany]", 899fd2c6bc9SAllan Jude " netstat -s [-46sz] [-f protocol_family | -p protocol]\n" 90082d383bcSRuslan Ermilov " [-M core] [-N system]", 901fd2c6bc9SAllan Jude " netstat -i | -I interface -s [-46s]\n" 902fd2c6bc9SAllan Jude " [-f protocol_family | -p protocol] [-M core] [-N system]", 90349c2dc64SMaxim Konovalov " netstat -m [-M core] [-N system]", 904fd2c6bc9SAllan Jude " netstat -B [-z] [-I interface]", 905fd2c6bc9SAllan Jude " netstat -r [-46AnW] [-F fibnum] [-f address_family]\n" 906fd2c6bc9SAllan Jude " [-M core] [-N system]", 9071cb467b1SRuslan Ermilov " netstat -rs [-s] [-M core] [-N system]", 90817ed2e8eSAlexander V. Chernikov " netstat -g [-46W] [-f address_family] [-M core] [-N system]", 90917ed2e8eSAlexander V. Chernikov " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", 9100153eb66SRobert Watson " netstat -Q"); 911ade9ccfeSMarcel Moolenaar xo_finish(); 9129b50d902SRodney W. Grimes exit(1); 9139b50d902SRodney W. Grimes } 914