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 #include <sys/param.h> 339b50d902SRodney W. Grimes #include <sys/file.h> 34182e8ae2SDoug Rabson #ifdef JAIL 35182e8ae2SDoug Rabson #include <sys/jail.h> 36182e8ae2SDoug Rabson #endif 379b50d902SRodney W. Grimes #include <sys/protosw.h> 389b50d902SRodney W. Grimes #include <sys/socket.h> 39feda1a43SJohn Baldwin #include <sys/socketvar.h> 409eddb899SMark Johnston #include <sys/sysctl.h> 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include <netinet/in.h> 439b50d902SRodney W. Grimes 44690f477dSSam Leffler #ifdef NETGRAPH 454cf49a43SJulian Elischer #include <netgraph/ng_socket.h> 46690f477dSSam Leffler #endif 474cf49a43SJulian Elischer 489b50d902SRodney W. Grimes #include <ctype.h> 495d422d6aSPhilippe Charnier #include <err.h> 50821df508SXin LI #include <errno.h> 51182e8ae2SDoug Rabson #ifdef JAIL 52182e8ae2SDoug Rabson #include <jail.h> 53182e8ae2SDoug Rabson #endif 549b50d902SRodney W. Grimes #include <kvm.h> 559b50d902SRodney W. Grimes #include <limits.h> 569b50d902SRodney W. Grimes #include <netdb.h> 579b50d902SRodney W. Grimes #include <nlist.h> 58821df508SXin LI #include <paths.h> 597b95a1ebSYaroslav Tykhiy #include <stdint.h> 609b50d902SRodney W. Grimes #include <stdio.h> 619b50d902SRodney W. Grimes #include <stdlib.h> 62ade9ccfeSMarcel Moolenaar #include <stdbool.h> 639b50d902SRodney W. Grimes #include <string.h> 649b50d902SRodney W. Grimes #include <unistd.h> 659b50d902SRodney W. Grimes #include "netstat.h" 6681dacd8bSHiroki Sato #include "nl_defs.h" 67ade9ccfeSMarcel Moolenaar #include <libxo/xo.h> 689b50d902SRodney W. Grimes 6981dacd8bSHiroki Sato static struct protox { 70feda1a43SJohn Baldwin int pr_index; /* index into nlist of cb head */ 71feda1a43SJohn Baldwin int pr_sindex; /* index into nlist of stat block */ 729b50d902SRodney W. Grimes u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 73feda1a43SJohn Baldwin void (*pr_cblocks)(u_long, const char *, int, int); 745e051718SAssar Westerlund /* control blocks printing routine */ 75feda1a43SJohn Baldwin void (*pr_stats)(u_long, const char *, int, int); 765e051718SAssar Westerlund /* statistics printing routine */ 775e051718SAssar Westerlund void (*pr_istats)(char *); /* per/if statistics printing routine */ 78fa6d48c0SMark Murray const char *pr_name; /* well-known name */ 7955fd53e2SJohn Baldwin int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 80feda1a43SJohn Baldwin int pr_protocol; 819b50d902SRodney W. Grimes } protox[] = { 82ddf24a50SMichael Tuexen { -1 , N_TCPSTAT, 1, protopr, 83feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 84ddf24a50SMichael Tuexen { -1 , N_UDPSTAT, 1, protopr, 85feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 8674fd40c9SRandall Stewart #ifdef SCTP 87feda1a43SJohn Baldwin { -1, N_SCTPSTAT, 1, sctp_protopr, 88feda1a43SJohn Baldwin sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 8974fd40c9SRandall Stewart #endif 90aa0a1e58SJeff Roberson #ifdef SDP 91aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 92aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 93aa0a1e58SJeff Roberson #endif 94ddf24a50SMichael Tuexen { -1 , -1, 1, protopr, 952b1c7217SGleb Smirnoff divert_stats, NULL, "divert", 1, 0 }, 96ddf24a50SMichael Tuexen { -1 , N_IPSTAT, 1, protopr, 97feda1a43SJohn Baldwin ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 98ddf24a50SMichael Tuexen { -1 , N_ICMPSTAT, 1, protopr, 99feda1a43SJohn Baldwin icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 100ddf24a50SMichael Tuexen { -1 , N_IGMPSTAT, 1, protopr, 101feda1a43SJohn Baldwin igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 102cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 10381dacd8bSHiroki Sato { -1, N_IPSEC4STAT, 1, NULL, /* keep as compat */ 1049d2d8e7bSGeorge V. Neville-Neil ipsec_stats, NULL, "ipsec", 1, 0}, 1058409aedfSGeorge V. Neville-Neil { -1, N_AHSTAT, 1, NULL, 1069d2d8e7bSGeorge V. Neville-Neil ah_stats, NULL, "ah", 1, 0}, 1078409aedfSGeorge V. Neville-Neil { -1, N_ESPSTAT, 1, NULL, 1089d2d8e7bSGeorge V. Neville-Neil esp_stats, NULL, "esp", 1, 0}, 1098409aedfSGeorge V. Neville-Neil { -1, N_IPCOMPSTAT, 1, NULL, 1109d2d8e7bSGeorge V. Neville-Neil ipcomp_stats, NULL, "ipcomp", 1, 0}, 111100b98dbSKelly Yancey #endif 112ddf24a50SMichael Tuexen { -1 , N_PIMSTAT, 1, protopr, 113feda1a43SJohn Baldwin pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 11481dacd8bSHiroki Sato { -1, N_CARPSTATS, 1, NULL, 115feda1a43SJohn Baldwin carp_stats, NULL, "carp", 1, 0 }, 1163e4d5cd3SGleb Smirnoff #ifdef PF 11781dacd8bSHiroki Sato { -1, N_PFSYNCSTATS, 1, NULL, 118feda1a43SJohn Baldwin pfsync_stats, NULL, "pfsync", 1, 0 }, 119*5dea523bSKristof Provost { -1, N_PFLOWSTATS, 1, NULL, 120*5dea523bSKristof Provost pflow_stats, NULL, "pflow", 1, 0 }, 1213e4d5cd3SGleb Smirnoff #endif 12254fc657dSGeorge V. Neville-Neil { -1, N_ARPSTAT, 1, NULL, 12354fc657dSGeorge V. Neville-Neil arp_stats, NULL, "arp", 1, 0 }, 1246bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 125feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 1269b50d902SRodney W. Grimes }; 1279b50d902SRodney W. Grimes 128cfa1ca9dSYoshinobu Inoue #ifdef INET6 12981dacd8bSHiroki Sato static struct protox ip6protox[] = { 130ddf24a50SMichael Tuexen { -1 , N_TCPSTAT, 1, protopr, 131feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 132ddf24a50SMichael Tuexen { -1 , N_UDPSTAT, 1, protopr, 133feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 134ddf24a50SMichael Tuexen { -1 , N_IP6STAT, 1, protopr, 135feda1a43SJohn Baldwin ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 136ddf24a50SMichael Tuexen { -1 , N_ICMP6STAT, 1, protopr, 137feda1a43SJohn Baldwin icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 138aa0a1e58SJeff Roberson #ifdef SDP 139aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 140aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 141aa0a1e58SJeff Roberson #endif 142cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 1436bb3f207SRuslan Ermilov { -1, N_IPSEC6STAT, 1, NULL, 1449d2d8e7bSGeorge V. Neville-Neil ipsec_stats, NULL, "ipsec6", 1, 0 }, 145cfa1ca9dSYoshinobu Inoue #endif 146cfa1ca9dSYoshinobu Inoue #ifdef notyet 1476bb3f207SRuslan Ermilov { -1, N_PIM6STAT, 1, NULL, 148feda1a43SJohn Baldwin pim6_stats, NULL, "pim6", 1, 0 }, 149cfa1ca9dSYoshinobu Inoue #endif 150feda1a43SJohn Baldwin { -1, N_RIP6STAT, 1, NULL, 151feda1a43SJohn Baldwin rip6_stats, NULL, "rip6", 1, 0 }, 1526bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 153feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 154cfa1ca9dSYoshinobu Inoue }; 155cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 156cfa1ca9dSYoshinobu Inoue 1573b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 15881dacd8bSHiroki Sato static struct protox pfkeyprotox[] = { 1596bb3f207SRuslan Ermilov { -1, N_PFKEYSTAT, 1, NULL, 160feda1a43SJohn Baldwin pfkey_stats, NULL, "pfkey", 0, 0 }, 1616bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 162feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 1633b8a8567SJun-ichiro itojun Hagino }; 1643b8a8567SJun-ichiro itojun Hagino #endif 1653b8a8567SJun-ichiro itojun Hagino 166690f477dSSam Leffler #ifdef NETGRAPH 16781dacd8bSHiroki Sato static struct protox netgraphprotox[] = { 16881dacd8bSHiroki Sato { N_NGSOCKLIST, -1, 1, netgraphprotopr, 169feda1a43SJohn Baldwin NULL, NULL, "ctrl", 0, 0 }, 17081dacd8bSHiroki Sato { N_NGSOCKLIST, -1, 1, netgraphprotopr, 171feda1a43SJohn Baldwin NULL, NULL, "data", 0, 0 }, 1726bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 173feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 1744cf49a43SJulian Elischer }; 175690f477dSSam Leffler #endif 176cc6a66f2SJulian Elischer 17781dacd8bSHiroki Sato static struct protox *protoprotox[] = { 178cfa1ca9dSYoshinobu Inoue protox, 179cfa1ca9dSYoshinobu Inoue #ifdef INET6 180cfa1ca9dSYoshinobu Inoue ip6protox, 181cfa1ca9dSYoshinobu Inoue #endif 1823b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 1833b8a8567SJun-ichiro itojun Hagino pfkeyprotox, 1843b8a8567SJun-ichiro itojun Hagino #endif 18545c203fcSGleb Smirnoff NULL }; 1869b50d902SRodney W. Grimes 187ade9ccfeSMarcel Moolenaar static void printproto(struct protox *, const char *, bool *); 1881a7ac2bdSAlfonso Gregory static void usage(void) __dead2; 189096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *); 190096146f8SYaroslav Tykhiy static struct protox *knownname(const char *); 1919b50d902SRodney W. Grimes 19281dacd8bSHiroki Sato static int kresolve_list(struct nlist *_nl); 19381dacd8bSHiroki Sato 194c6620a13SPoul-Henning Kamp static kvm_t *kvmd; 195080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL; 196080b7f49SDag-Erling Smørgrav 197080b7f49SDag-Erling Smørgrav int Aflag; /* show addresses of protocol control block */ 198080b7f49SDag-Erling Smørgrav int aflag; /* show all sockets (including servers) */ 19981dacd8bSHiroki Sato static int Bflag; /* show information about bpf consumers */ 200080b7f49SDag-Erling Smørgrav int bflag; /* show i/f total bytes in/out */ 2010e5e35e3SRichard Scheffenegger int cflag; /* show TCP congestion control stack */ 2020e5e35e3SRichard Scheffenegger int Cflag; /* show congestion control algo and vars */ 203080b7f49SDag-Erling Smørgrav int dflag; /* show i/f dropped packets */ 204080b7f49SDag-Erling Smørgrav int gflag; /* show group (multicast) routing or stats */ 205c2dfd19fSGleb Smirnoff int hflag; /* show counters in human readable format */ 206080b7f49SDag-Erling Smørgrav int iflag; /* show interfaces */ 207080b7f49SDag-Erling Smørgrav int Lflag; /* show size of listen queues */ 208080b7f49SDag-Erling Smørgrav int mflag; /* show memory stats */ 209bf10ffe1SXin LI int noutputs = 0; /* how much outputs before we exit */ 21065ea0024SAssar Westerlund int numeric_addr; /* show addresses numerically */ 21165ea0024SAssar Westerlund int numeric_port; /* show ports numerically */ 212fedeb08bSAlexander V. Chernikov int Oflag; /* show nhgrp objects*/ 213a6663252SAlexander V. Chernikov int oflag; /* show nexthop objects*/ 2142529f56eSJonathan T. Looney int Pflag; /* show TCP log ID */ 215cf5e44f8SRuslan Ermilov static int pflag; /* show given protocol */ 21681dacd8bSHiroki Sato static int Qflag; /* show netisr information */ 217080b7f49SDag-Erling Smørgrav int rflag; /* show routing tables (or routing stats) */ 21885b0f0f3SAdrian Chadd int Rflag; /* show flow / RSS statistics */ 219080b7f49SDag-Erling Smørgrav int sflag; /* show protocol statistics */ 220080b7f49SDag-Erling Smørgrav int Wflag; /* wide display */ 221f5d34df5SGeorge V. Neville-Neil int Tflag; /* TCP Information */ 22249f287f8SGeorge V. Neville-Neil int xflag; /* extra information, includes all socket buffer info */ 223c73d99b5SRuslan Ermilov int zflag; /* zero stats */ 224080b7f49SDag-Erling Smørgrav 225080b7f49SDag-Erling Smørgrav int interval; /* repeat interval for i/f stats */ 226080b7f49SDag-Erling Smørgrav 227080b7f49SDag-Erling Smørgrav char *interface; /* desired i/f for stats, or NULL for all i/fs */ 228080b7f49SDag-Erling Smørgrav int unit; /* unit number for above */ 229182e8ae2SDoug Rabson #ifdef JAIL 230182e8ae2SDoug Rabson char *jail_name; /* desired jail to operate in */ 231182e8ae2SDoug Rabson #endif 232080b7f49SDag-Erling Smørgrav 23381dacd8bSHiroki Sato static int af; /* address family */ 234feda1a43SJohn Baldwin int live; /* true if we are examining a live system */ 2359b50d902SRodney W. Grimes 2369b50d902SRodney W. Grimes int 237a01e3379SDavid Malone main(int argc, char *argv[]) 2389b50d902SRodney W. Grimes { 239a01e3379SDavid Malone struct protox *tp = NULL; /* for printing cblocks & stats */ 2409b50d902SRodney W. Grimes int ch; 2413fddef95SHiroki Sato int fib = -1; 2423fddef95SHiroki Sato char *endptr; 243ade9ccfeSMarcel Moolenaar bool first = true; 244182e8ae2SDoug Rabson #ifdef JAIL 245182e8ae2SDoug Rabson int jid; 246182e8ae2SDoug Rabson #endif 2479b50d902SRodney W. Grimes 2489b50d902SRodney W. Grimes af = AF_UNSPEC; 2499b50d902SRodney W. Grimes 250ade9ccfeSMarcel Moolenaar argc = xo_parse_args(argc, argv); 25106ff7ccbSHajimu UMEMOTO if (argc < 0) 25206ff7ccbSHajimu UMEMOTO exit(EXIT_FAILURE); 253ade9ccfeSMarcel Moolenaar 254182e8ae2SDoug Rabson while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:ij:LlM:mN:nOoPp:Qq:RrSTsuWw:xz")) 2550153eb66SRobert Watson != -1) 2569b50d902SRodney W. Grimes switch(ch) { 25717ed2e8eSAlexander V. Chernikov case '4': 25817ed2e8eSAlexander V. Chernikov #ifdef INET 25917ed2e8eSAlexander V. Chernikov af = AF_INET; 26017ed2e8eSAlexander V. Chernikov #else 26117ed2e8eSAlexander V. Chernikov errx(1, "IPv4 support is not compiled in"); 26217ed2e8eSAlexander V. Chernikov #endif 26317ed2e8eSAlexander V. Chernikov break; 26417ed2e8eSAlexander V. Chernikov case '6': 26517ed2e8eSAlexander V. Chernikov #ifdef INET6 26617ed2e8eSAlexander V. Chernikov af = AF_INET6; 26717ed2e8eSAlexander V. Chernikov #else 26817ed2e8eSAlexander V. Chernikov errx(1, "IPv6 support is not compiled in"); 26917ed2e8eSAlexander V. Chernikov #endif 27017ed2e8eSAlexander V. Chernikov break; 2719b50d902SRodney W. Grimes case 'A': 2729b50d902SRodney W. Grimes Aflag = 1; 2739b50d902SRodney W. Grimes break; 2749b50d902SRodney W. Grimes case 'a': 2759b50d902SRodney W. Grimes aflag = 1; 2769b50d902SRodney W. Grimes break; 2776b463eedSChristian S.J. Peron case 'B': 2786b463eedSChristian S.J. Peron Bflag = 1; 2796b463eedSChristian S.J. Peron break; 280e1e293a5SDavid Greenman case 'b': 281e1e293a5SDavid Greenman bflag = 1; 282e1e293a5SDavid Greenman break; 2830e5e35e3SRichard Scheffenegger case 'c': 2840e5e35e3SRichard Scheffenegger cflag = 1; 2850e5e35e3SRichard Scheffenegger break; 286b98a21f6SMichael Tuexen case 'C': 287b98a21f6SMichael Tuexen Cflag = 1; 288b98a21f6SMichael Tuexen break; 2899b50d902SRodney W. Grimes case 'd': 2909b50d902SRodney W. Grimes dflag = 1; 2919b50d902SRodney W. Grimes break; 2923fddef95SHiroki Sato case 'F': 2933fddef95SHiroki Sato fib = strtol(optarg, &endptr, 0); 2943fddef95SHiroki Sato if (*endptr != '\0' || 2953fddef95SHiroki Sato (fib == 0 && (errno == EINVAL || errno == ERANGE))) 296ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: invalid fib", optarg); 2973fddef95SHiroki Sato break; 2989b50d902SRodney W. Grimes case 'f': 2992c284d93SGleb Smirnoff if (strcmp(optarg, "inet") == 0) 3009b50d902SRodney W. Grimes af = AF_INET; 301cfa1ca9dSYoshinobu Inoue #ifdef INET6 302cfa1ca9dSYoshinobu Inoue else if (strcmp(optarg, "inet6") == 0) 303cfa1ca9dSYoshinobu Inoue af = AF_INET6; 304e5134d2eSMax Laier #endif 305e5134d2eSMax Laier #ifdef IPSEC 3063b8a8567SJun-ichiro itojun Hagino else if (strcmp(optarg, "pfkey") == 0) 3073b8a8567SJun-ichiro itojun Hagino af = PF_KEY; 308e5134d2eSMax Laier #endif 30910d5269fSHiroki Sato else if (strcmp(optarg, "unix") == 0 || 31010d5269fSHiroki Sato strcmp(optarg, "local") == 0) 3119b50d902SRodney W. Grimes af = AF_UNIX; 312690f477dSSam Leffler #ifdef NETGRAPH 3134cf49a43SJulian Elischer else if (strcmp(optarg, "ng") == 0 3144cf49a43SJulian Elischer || strcmp(optarg, "netgraph") == 0) 3154cf49a43SJulian Elischer af = AF_NETGRAPH; 316690f477dSSam Leffler #endif 317d44ddba9SRuslan Ermilov else if (strcmp(optarg, "link") == 0) 318d44ddba9SRuslan Ermilov af = AF_LINK; 3199b50d902SRodney W. Grimes else { 320ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: unknown address family", 321ade9ccfeSMarcel Moolenaar optarg); 3229b50d902SRodney W. Grimes } 3239b50d902SRodney W. Grimes break; 3249b50d902SRodney W. Grimes case 'g': 3259b50d902SRodney W. Grimes gflag = 1; 3269b50d902SRodney W. Grimes break; 327c2dfd19fSGleb Smirnoff case 'h': 328c2dfd19fSGleb Smirnoff hflag = 1; 329c2dfd19fSGleb Smirnoff break; 3309b50d902SRodney W. Grimes case 'I': { 3319b50d902SRodney W. Grimes char *cp; 3329b50d902SRodney W. Grimes 3339b50d902SRodney W. Grimes iflag = 1; 3349b50d902SRodney W. Grimes for (cp = interface = optarg; isalpha(*cp); cp++) 3359b50d902SRodney W. Grimes continue; 3369b50d902SRodney W. Grimes unit = atoi(cp); 3379b50d902SRodney W. Grimes break; 3389b50d902SRodney W. Grimes } 3399b50d902SRodney W. Grimes case 'i': 3409b50d902SRodney W. Grimes iflag = 1; 3419b50d902SRodney W. Grimes break; 342182e8ae2SDoug Rabson case 'j': 343182e8ae2SDoug Rabson #ifdef JAIL 344182e8ae2SDoug Rabson if (optarg == NULL) 345182e8ae2SDoug Rabson usage(); 346182e8ae2SDoug Rabson jail_name = optarg; 347182e8ae2SDoug Rabson #else 348182e8ae2SDoug Rabson errx(1, "Jail support is not compiled in"); 349182e8ae2SDoug Rabson #endif 350182e8ae2SDoug Rabson break; 351ac55add0SGuido van Rooij case 'L': 352ac55add0SGuido van Rooij Lflag = 1; 353ac55add0SGuido van Rooij break; 3549b50d902SRodney W. Grimes case 'M': 3559b50d902SRodney W. Grimes memf = optarg; 3569b50d902SRodney W. Grimes break; 3579b50d902SRodney W. Grimes case 'm': 3589b50d902SRodney W. Grimes mflag = 1; 3599b50d902SRodney W. Grimes break; 3609b50d902SRodney W. Grimes case 'N': 3619b50d902SRodney W. Grimes nlistf = optarg; 3629b50d902SRodney W. Grimes break; 3639b50d902SRodney W. Grimes case 'n': 36465ea0024SAssar Westerlund numeric_addr = numeric_port = 1; 3659b50d902SRodney W. Grimes break; 366a6663252SAlexander V. Chernikov case 'o': 367a6663252SAlexander V. Chernikov oflag = 1; 368a6663252SAlexander V. Chernikov break; 369fedeb08bSAlexander V. Chernikov case 'O': 370fedeb08bSAlexander V. Chernikov Oflag = 1; 371fedeb08bSAlexander V. Chernikov break; 3722529f56eSJonathan T. Looney case 'P': 3732529f56eSJonathan T. Looney Pflag = 1; 3742529f56eSJonathan T. Looney break; 3759b50d902SRodney W. Grimes case 'p': 3769b50d902SRodney W. Grimes if ((tp = name2protox(optarg)) == NULL) { 377ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: unknown or uninstrumented " 378ade9ccfeSMarcel Moolenaar "protocol", optarg); 3799b50d902SRodney W. Grimes } 3809b50d902SRodney W. Grimes pflag = 1; 3819b50d902SRodney W. Grimes break; 3820153eb66SRobert Watson case 'Q': 3830153eb66SRobert Watson Qflag = 1; 3840153eb66SRobert Watson break; 385bf10ffe1SXin LI case 'q': 386bf10ffe1SXin LI noutputs = atoi(optarg); 387bf10ffe1SXin LI if (noutputs != 0) 388bf10ffe1SXin LI noutputs++; 389bf10ffe1SXin LI break; 3909b50d902SRodney W. Grimes case 'r': 3919b50d902SRodney W. Grimes rflag = 1; 3929b50d902SRodney W. Grimes break; 39385b0f0f3SAdrian Chadd case 'R': 39485b0f0f3SAdrian Chadd Rflag = 1; 39585b0f0f3SAdrian Chadd break; 3969b50d902SRodney W. Grimes case 's': 3979b50d902SRodney W. Grimes ++sflag; 3989b50d902SRodney W. Grimes break; 39965ea0024SAssar Westerlund case 'S': 40065ea0024SAssar Westerlund numeric_addr = 1; 40165ea0024SAssar Westerlund break; 4029b50d902SRodney W. Grimes case 'u': 4039b50d902SRodney W. Grimes af = AF_UNIX; 4049b50d902SRodney W. Grimes break; 405080b7f49SDag-Erling Smørgrav case 'W': 40670057abfSRuslan Ermilov case 'l': 407080b7f49SDag-Erling Smørgrav Wflag = 1; 408080b7f49SDag-Erling Smørgrav break; 4099b50d902SRodney W. Grimes case 'w': 4109b50d902SRodney W. Grimes interval = atoi(optarg); 4119b50d902SRodney W. Grimes iflag = 1; 4129b50d902SRodney W. Grimes break; 413f5d34df5SGeorge V. Neville-Neil case 'T': 414f5d34df5SGeorge V. Neville-Neil Tflag = 1; 415f5d34df5SGeorge V. Neville-Neil break; 41649f287f8SGeorge V. Neville-Neil case 'x': 41749f287f8SGeorge V. Neville-Neil xflag = 1; 41849f287f8SGeorge V. Neville-Neil break; 419c73d99b5SRuslan Ermilov case 'z': 420c73d99b5SRuslan Ermilov zflag = 1; 421c73d99b5SRuslan Ermilov break; 4229b50d902SRodney W. Grimes case '?': 4239b50d902SRodney W. Grimes default: 4249b50d902SRodney W. Grimes usage(); 4259b50d902SRodney W. Grimes } 4269b50d902SRodney W. Grimes argv += optind; 4279b50d902SRodney W. Grimes argc -= optind; 4289b50d902SRodney W. Grimes 4299b50d902SRodney W. Grimes #define BACKWARD_COMPATIBILITY 4309b50d902SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 4319b50d902SRodney W. Grimes if (*argv) { 4329b50d902SRodney W. Grimes if (isdigit(**argv)) { 4339b50d902SRodney W. Grimes interval = atoi(*argv); 4349b50d902SRodney W. Grimes if (interval <= 0) 4359b50d902SRodney W. Grimes usage(); 4369b50d902SRodney W. Grimes ++argv; 4379b50d902SRodney W. Grimes iflag = 1; 4389b50d902SRodney W. Grimes } 4399b50d902SRodney W. Grimes if (*argv) { 4409b50d902SRodney W. Grimes nlistf = *argv; 4419b50d902SRodney W. Grimes if (*++argv) 4429b50d902SRodney W. Grimes memf = *argv; 4439b50d902SRodney W. Grimes } 4449b50d902SRodney W. Grimes } 4459b50d902SRodney W. Grimes #endif 4469b50d902SRodney W. Grimes 447182e8ae2SDoug Rabson #ifdef JAIL 448182e8ae2SDoug Rabson if (jail_name != NULL) { 449182e8ae2SDoug Rabson jid = jail_getid(jail_name); 450182e8ae2SDoug Rabson if (jid == -1) 451182e8ae2SDoug Rabson errx(1, "Jail not found"); 452182e8ae2SDoug Rabson if (jail_attach(jid) != 0) 453182e8ae2SDoug Rabson errx(1, "Cannot attach to jail"); 454182e8ae2SDoug Rabson } 455182e8ae2SDoug Rabson #endif 456182e8ae2SDoug Rabson 4579b50d902SRodney W. Grimes /* 4589b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 4599b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 4609b50d902SRodney W. Grimes */ 461feda1a43SJohn Baldwin live = (nlistf == NULL && memf == NULL); 462a4a889a0SXin LI if (!live) { 463a4a889a0SXin LI if (setgid(getgid()) != 0) 464a4a889a0SXin LI xo_err(-1, "setgid"); 4651caaf3eaSBaptiste Daroussin /* Load all necessary kvm symbols */ 4661caaf3eaSBaptiste Daroussin kresolve_list(nl); 467a4a889a0SXin LI } 4689b50d902SRodney W. Grimes 469f5d34df5SGeorge V. Neville-Neil if (xflag && Tflag) 470ade9ccfeSMarcel Moolenaar xo_errx(1, "-x and -T are incompatible, pick one."); 471f5d34df5SGeorge V. Neville-Neil 4726b463eedSChristian S.J. Peron if (Bflag) { 473feda1a43SJohn Baldwin if (!live) 474feda1a43SJohn Baldwin usage(); 4756b463eedSChristian S.J. Peron bpf_stats(interface); 476ade9ccfeSMarcel Moolenaar xo_finish(); 4776b463eedSChristian S.J. Peron exit(0); 4786b463eedSChristian S.J. Peron } 4799b50d902SRodney W. Grimes if (mflag) { 48083708764SRuslan Ermilov if (!live) { 481feda1a43SJohn Baldwin if (kread(0, NULL, 0) == 0) 48205d1f5bcSAndrey V. Elsukov mbpr(kvmd, nl[N_SFSTAT].n_value); 483d15c5f56SRuslan Ermilov } else 484d4426f28SRobert Watson mbpr(NULL, 0); 485ade9ccfeSMarcel Moolenaar xo_finish(); 4869b50d902SRodney W. Grimes exit(0); 4879b50d902SRodney W. Grimes } 4880153eb66SRobert Watson if (Qflag) { 48988737be2SRobert Watson if (!live) { 49088737be2SRobert Watson if (kread(0, NULL, 0) == 0) 49181dacd8bSHiroki Sato netisr_stats(); 49288737be2SRobert Watson } else 49381dacd8bSHiroki Sato netisr_stats(); 494ade9ccfeSMarcel Moolenaar xo_finish(); 4950153eb66SRobert Watson exit(0); 4960153eb66SRobert Watson } 497cc63cd56SPeter Wemm #if 0 4989b50d902SRodney W. Grimes /* 4999b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 5009b50d902SRodney W. Grimes * of open/close on each call to get* routines. 5019b50d902SRodney W. Grimes */ 5029b50d902SRodney W. Grimes sethostent(1); 5039b50d902SRodney W. Grimes setnetent(1); 504cc63cd56SPeter Wemm #else 505cc63cd56SPeter Wemm /* 506cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 507cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 508cc63cd56SPeter Wemm * used for the queries, which is slower. 509cc63cd56SPeter Wemm */ 510cc63cd56SPeter Wemm #endif 511cf5e44f8SRuslan Ermilov if (iflag && !sflag) { 512ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 51310d5269fSHiroki Sato intpr(NULL, af); 514ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 515ade9ccfeSMarcel Moolenaar xo_finish(); 5169b50d902SRodney W. Grimes exit(0); 5179b50d902SRodney W. Grimes } 5189b50d902SRodney W. Grimes if (rflag) { 519ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5205d6d7e75SGleb Smirnoff if (sflag) { 5212ccdd4b3SBaptiste Daroussin if (live) { 5222ccdd4b3SBaptiste Daroussin kresolve_list(nl); 5232ccdd4b3SBaptiste Daroussin } 524fc47e028SAlexander V. Chernikov rt_stats(); 5255d6d7e75SGleb Smirnoff } else 526fc47e028SAlexander V. Chernikov routepr(fib, af); 527ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 528ade9ccfeSMarcel Moolenaar xo_finish(); 5299b50d902SRodney W. Grimes exit(0); 5309b50d902SRodney W. Grimes } 531a6663252SAlexander V. Chernikov if (oflag) { 532a6663252SAlexander V. Chernikov xo_open_container("statistics"); 533a6663252SAlexander V. Chernikov nhops_print(fib, af); 534a6663252SAlexander V. Chernikov xo_close_container("statistics"); 535a6663252SAlexander V. Chernikov xo_finish(); 536a6663252SAlexander V. Chernikov exit(0); 537a6663252SAlexander V. Chernikov } 538fedeb08bSAlexander V. Chernikov if (Oflag) { 539fedeb08bSAlexander V. Chernikov xo_open_container("statistics"); 540fedeb08bSAlexander V. Chernikov nhgrp_print(fib, af); 541fedeb08bSAlexander V. Chernikov xo_close_container("statistics"); 542fedeb08bSAlexander V. Chernikov xo_finish(); 543fedeb08bSAlexander V. Chernikov exit(0); 544fedeb08bSAlexander V. Chernikov } 545fedeb08bSAlexander V. Chernikov 546a6663252SAlexander V. Chernikov 547fc47e028SAlexander V. Chernikov 5489b50d902SRodney W. Grimes if (gflag) { 549ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 550cfa1ca9dSYoshinobu Inoue if (sflag) { 551cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 552fc47e028SAlexander V. Chernikov mrt_stats(); 553cfa1ca9dSYoshinobu Inoue #ifdef INET6 554cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 555fc47e028SAlexander V. Chernikov mrt6_stats(); 556cfa1ca9dSYoshinobu Inoue #endif 557cfa1ca9dSYoshinobu Inoue } else { 558cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 559fc47e028SAlexander V. Chernikov mroutepr(); 560cfa1ca9dSYoshinobu Inoue #ifdef INET6 561cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 562fc47e028SAlexander V. Chernikov mroute6pr(); 563cfa1ca9dSYoshinobu Inoue #endif 564cfa1ca9dSYoshinobu Inoue } 565ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 566ade9ccfeSMarcel Moolenaar xo_finish(); 5679b50d902SRodney W. Grimes exit(0); 5689b50d902SRodney W. Grimes } 569cfa1ca9dSYoshinobu Inoue 570cf5e44f8SRuslan Ermilov if (tp) { 571ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 572ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 573ade9ccfeSMarcel Moolenaar if (!first) 574ade9ccfeSMarcel Moolenaar xo_close_list("socket"); 575ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 576ade9ccfeSMarcel Moolenaar xo_finish(); 577cf5e44f8SRuslan Ermilov exit(0); 578cf5e44f8SRuslan Ermilov } 579ade9ccfeSMarcel Moolenaar 580ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 581cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 5829b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 583ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 584cfa1ca9dSYoshinobu Inoue #ifdef INET6 585cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 586cfa1ca9dSYoshinobu Inoue for (tp = ip6protox; tp->pr_name; tp++) 587ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 588cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 5893b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 5903b8a8567SJun-ichiro itojun Hagino if (af == PF_KEY || af == AF_UNSPEC) 5913b8a8567SJun-ichiro itojun Hagino for (tp = pfkeyprotox; tp->pr_name; tp++) 592ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 5933b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/ 594690f477dSSam Leffler #ifdef NETGRAPH 5954cf49a43SJulian Elischer if (af == AF_NETGRAPH || af == AF_UNSPEC) 5964cf49a43SJulian Elischer for (tp = netgraphprotox; tp->pr_name; tp++) 597ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 598690f477dSSam Leffler #endif /* NETGRAPH */ 599f1c0a78dSMaxim Konovalov if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 600feda1a43SJohn Baldwin unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 601963b7ccdSRobert Watson nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 602ade9ccfeSMarcel Moolenaar nl[N_UNP_SPHEAD].n_value, &first); 603ade9ccfeSMarcel Moolenaar 604ade9ccfeSMarcel Moolenaar if (!first) 605ade9ccfeSMarcel Moolenaar xo_close_list("socket"); 606ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 607ade9ccfeSMarcel Moolenaar xo_finish(); 6089b50d902SRodney W. Grimes exit(0); 6099b50d902SRodney W. Grimes } 6109b50d902SRodney W. Grimes 611dbfd8708SGleb Smirnoff static int 612dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats, 613dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn, int zero) 6149eddb899SMark Johnston { 6159eddb899SMark Johnston int error; 6169eddb899SMark Johnston 6179eddb899SMark Johnston if (live) { 6189eddb899SMark Johnston memset(stats, 0, len); 619dbfd8708SGleb Smirnoff if (zero) 6209eddb899SMark Johnston error = sysctlbyname(sysctlname, NULL, NULL, stats, 6219eddb899SMark Johnston len); 6229eddb899SMark Johnston else 6239eddb899SMark Johnston error = sysctlbyname(sysctlname, stats, &len, NULL, 0); 6249eddb899SMark Johnston if (error == -1 && errno != ENOENT) 6259eddb899SMark Johnston xo_warn("sysctl %s", sysctlname); 6269eddb899SMark Johnston } else { 6279eddb899SMark Johnston if (off == 0) 6289eddb899SMark Johnston return (1); 6299eddb899SMark Johnston error = kreadfn(off, stats, len); 6309eddb899SMark Johnston } 6319eddb899SMark Johnston return (error); 6329eddb899SMark Johnston } 6339eddb899SMark Johnston 634dbfd8708SGleb Smirnoff int 635dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats, 636dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn) 637dbfd8708SGleb Smirnoff { 638dbfd8708SGleb Smirnoff 639dbfd8708SGleb Smirnoff return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 640dbfd8708SGleb Smirnoff zflag)); 641dbfd8708SGleb Smirnoff } 642dbfd8708SGleb Smirnoff 643dbfd8708SGleb Smirnoff int 644dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats, 645dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn) 646dbfd8708SGleb Smirnoff { 647dbfd8708SGleb Smirnoff 648dbfd8708SGleb Smirnoff return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); 649dbfd8708SGleb Smirnoff } 650dbfd8708SGleb Smirnoff 6519b50d902SRodney W. Grimes /* 6529b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 6539b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 6549b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 6559b50d902SRodney W. Grimes */ 6569b50d902SRodney W. Grimes static void 657ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first) 6589b50d902SRodney W. Grimes { 659feda1a43SJohn Baldwin void (*pr)(u_long, const char *, int, int); 6609b50d902SRodney W. Grimes u_long off; 661ade9ccfeSMarcel Moolenaar bool doingdblocks = false; 6629b50d902SRodney W. Grimes 6639b50d902SRodney W. Grimes if (sflag) { 664cfa1ca9dSYoshinobu Inoue if (iflag) { 665cfa1ca9dSYoshinobu Inoue if (tp->pr_istats) 66610d5269fSHiroki Sato intpr(tp->pr_istats, af); 667cf5e44f8SRuslan Ermilov else if (pflag) 668ade9ccfeSMarcel Moolenaar xo_message("%s: no per-interface stats routine", 669cf5e44f8SRuslan Ermilov tp->pr_name); 670cfa1ca9dSYoshinobu Inoue return; 671feda1a43SJohn Baldwin } else { 6729b50d902SRodney W. Grimes pr = tp->pr_stats; 673cf5e44f8SRuslan Ermilov if (!pr) { 674cf5e44f8SRuslan Ermilov if (pflag) 675ade9ccfeSMarcel Moolenaar xo_message("%s: no stats routine", 676cf5e44f8SRuslan Ermilov 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_sindex < 0) { 682feda1a43SJohn Baldwin if (pflag) 683ade9ccfeSMarcel Moolenaar xo_message("%s: stats routine doesn't " 684ade9ccfeSMarcel Moolenaar "work on cores", tp->pr_name); 685feda1a43SJohn Baldwin return; 686feda1a43SJohn Baldwin } else 687feda1a43SJohn Baldwin off = nl[tp->pr_sindex].n_value; 688cfa1ca9dSYoshinobu Inoue } 6899b50d902SRodney W. Grimes } else { 690ade9ccfeSMarcel Moolenaar doingdblocks = true; 6919b50d902SRodney W. Grimes pr = tp->pr_cblocks; 692cf5e44f8SRuslan Ermilov if (!pr) { 693cf5e44f8SRuslan Ermilov if (pflag) 694ade9ccfeSMarcel Moolenaar xo_message("%s: no PCB routine", tp->pr_name); 695cf5e44f8SRuslan Ermilov return; 696cf5e44f8SRuslan Ermilov } 697feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 698feda1a43SJohn Baldwin off = 0; 699feda1a43SJohn Baldwin else if (tp->pr_index < 0) { 700feda1a43SJohn Baldwin if (pflag) 701ade9ccfeSMarcel Moolenaar xo_message("%s: PCB routine doesn't work on " 702ade9ccfeSMarcel Moolenaar "cores", tp->pr_name); 703feda1a43SJohn Baldwin return; 704feda1a43SJohn Baldwin } else 705feda1a43SJohn Baldwin off = nl[tp->pr_index].n_value; 7069b50d902SRodney W. Grimes } 707feda1a43SJohn Baldwin if (pr != NULL && (off || (live && tp->pr_usesysctl) || 708ade9ccfeSMarcel Moolenaar af != AF_UNSPEC)) { 709ade9ccfeSMarcel Moolenaar if (doingdblocks && *first) { 710ade9ccfeSMarcel Moolenaar xo_open_list("socket"); 711ade9ccfeSMarcel Moolenaar *first = false; 712ade9ccfeSMarcel Moolenaar } 713ade9ccfeSMarcel Moolenaar 714feda1a43SJohn Baldwin (*pr)(off, name, af, tp->pr_protocol); 7159b50d902SRodney W. Grimes } 716ade9ccfeSMarcel Moolenaar } 7179b50d902SRodney W. Grimes 71829dde48dSGleb Smirnoff static int 71929dde48dSGleb Smirnoff kvmd_init(void) 7209b50d902SRodney W. Grimes { 721feda1a43SJohn Baldwin char errbuf[_POSIX2_LINE_MAX]; 722feda1a43SJohn Baldwin 72329dde48dSGleb Smirnoff if (kvmd != NULL) 72429dde48dSGleb Smirnoff return (0); 72529dde48dSGleb Smirnoff 726feda1a43SJohn Baldwin kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 727a4a889a0SXin LI if (setgid(getgid()) != 0) 728a4a889a0SXin LI xo_err(-1, "setgid"); 72929dde48dSGleb Smirnoff 73029dde48dSGleb Smirnoff if (kvmd == NULL) { 731ade9ccfeSMarcel Moolenaar xo_warnx("kvm not available: %s", errbuf); 73229dde48dSGleb Smirnoff return (-1); 73329dde48dSGleb Smirnoff } 73429dde48dSGleb Smirnoff 735fc47e028SAlexander V. Chernikov return (0); 736fc47e028SAlexander V. Chernikov } 737fc47e028SAlexander V. Chernikov 738fc47e028SAlexander V. Chernikov /* 739fc47e028SAlexander V. Chernikov * Resolve symbol list, return 0 on success. 740fc47e028SAlexander V. Chernikov */ 74181dacd8bSHiroki Sato static int 742fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl) 743fc47e028SAlexander V. Chernikov { 744fc47e028SAlexander V. Chernikov 745fc47e028SAlexander V. Chernikov if ((kvmd == NULL) && (kvmd_init() != 0)) 746fc47e028SAlexander V. Chernikov return (-1); 747fc47e028SAlexander V. Chernikov 748fc47e028SAlexander V. Chernikov if (_nl[0].n_type != 0) 749fc47e028SAlexander V. Chernikov return (0); 750fc47e028SAlexander V. Chernikov 751fc47e028SAlexander V. Chernikov if (kvm_nlist(kvmd, _nl) < 0) { 75299453c6aSPoul-Henning Kamp if (nlistf) 753ade9ccfeSMarcel Moolenaar xo_errx(1, "%s: kvm_nlist: %s", nlistf, 75499453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 75599453c6aSPoul-Henning Kamp else 756ade9ccfeSMarcel Moolenaar xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 75799453c6aSPoul-Henning Kamp } 75899453c6aSPoul-Henning Kamp 75929dde48dSGleb Smirnoff return (0); 76029dde48dSGleb Smirnoff } 76129dde48dSGleb Smirnoff 76229dde48dSGleb Smirnoff /* 76381dacd8bSHiroki Sato * Wrapper of kvm_dpcpu_setcpu(). 76481dacd8bSHiroki Sato */ 76581dacd8bSHiroki Sato void 76681dacd8bSHiroki Sato kset_dpcpu(u_int cpuid) 76781dacd8bSHiroki Sato { 76881dacd8bSHiroki Sato 76981dacd8bSHiroki Sato if ((kvmd == NULL) && (kvmd_init() != 0)) 77081dacd8bSHiroki Sato xo_errx(-1, "%s: kvm is not available", __func__); 77181dacd8bSHiroki Sato 77281dacd8bSHiroki Sato if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0) 77381dacd8bSHiroki Sato xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__, 77481dacd8bSHiroki Sato cpuid, kvm_geterr(kvmd)); 77581dacd8bSHiroki Sato return; 77681dacd8bSHiroki Sato } 77781dacd8bSHiroki Sato 77881dacd8bSHiroki Sato /* 77929dde48dSGleb Smirnoff * Read kernel memory, return 0 on success. 78029dde48dSGleb Smirnoff */ 78129dde48dSGleb Smirnoff int 78229dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size) 78329dde48dSGleb Smirnoff { 78429dde48dSGleb Smirnoff 78529dde48dSGleb Smirnoff if (kvmd_init() < 0) 78699453c6aSPoul-Henning Kamp return (-1); 78729dde48dSGleb Smirnoff 788c6620a13SPoul-Henning Kamp if (!buf) 789c6620a13SPoul-Henning Kamp return (0); 790feda1a43SJohn Baldwin if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 791ade9ccfeSMarcel Moolenaar xo_warnx("%s", kvm_geterr(kvmd)); 7929b50d902SRodney W. Grimes return (-1); 7939b50d902SRodney W. Grimes } 7949b50d902SRodney W. Grimes return (0); 7959b50d902SRodney W. Grimes } 7969b50d902SRodney W. Grimes 79729dde48dSGleb Smirnoff /* 798e3a7aa6fSGleb Smirnoff * Read single counter(9). 799e3a7aa6fSGleb Smirnoff */ 800e3a7aa6fSGleb Smirnoff uint64_t 801e3a7aa6fSGleb Smirnoff kread_counter(u_long addr) 802e3a7aa6fSGleb Smirnoff { 803e3a7aa6fSGleb Smirnoff 804e3a7aa6fSGleb Smirnoff if (kvmd_init() < 0) 805e3a7aa6fSGleb Smirnoff return (-1); 806e3a7aa6fSGleb Smirnoff 807e3a7aa6fSGleb Smirnoff return (kvm_counter_u64_fetch(kvmd, addr)); 808e3a7aa6fSGleb Smirnoff } 809e3a7aa6fSGleb Smirnoff 810e3a7aa6fSGleb Smirnoff /* 81129dde48dSGleb Smirnoff * Read an array of N counters in kernel memory into array of N uint64_t's. 81229dde48dSGleb Smirnoff */ 81329dde48dSGleb Smirnoff int 8145da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size) 81529dde48dSGleb Smirnoff { 816794c57d3SMark Johnston uint64_t *c; 817794c57d3SMark Johnston u_long *counters; 818794c57d3SMark Johnston size_t i, n; 81929dde48dSGleb Smirnoff 82029dde48dSGleb Smirnoff if (kvmd_init() < 0) 82129dde48dSGleb Smirnoff return (-1); 82229dde48dSGleb Smirnoff 823794c57d3SMark Johnston if (size % sizeof(uint64_t) != 0) { 824794c57d3SMark Johnston xo_warnx("kread_counters: invalid counter set size"); 8255da0521fSAndrey V. Elsukov return (-1); 8265da0521fSAndrey V. Elsukov } 827794c57d3SMark Johnston 828794c57d3SMark Johnston n = size / sizeof(uint64_t); 829794c57d3SMark Johnston if ((counters = malloc(n * sizeof(u_long))) == NULL) 830794c57d3SMark Johnston xo_err(-1, "malloc"); 831794c57d3SMark Johnston if (kread(addr, counters, n * sizeof(u_long)) < 0) { 832794c57d3SMark Johnston free(counters); 833794c57d3SMark Johnston return (-1); 834794c57d3SMark Johnston } 835794c57d3SMark Johnston 836794c57d3SMark Johnston c = buf; 837794c57d3SMark Johnston for (i = 0; i < n; i++) 838794c57d3SMark Johnston c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); 839794c57d3SMark Johnston 840794c57d3SMark Johnston free(counters); 84129dde48dSGleb Smirnoff return (0); 84229dde48dSGleb Smirnoff } 84329dde48dSGleb Smirnoff 844a01e3379SDavid Malone const char * 8457b95a1ebSYaroslav Tykhiy plural(uintmax_t n) 8469b50d902SRodney W. Grimes { 8479b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 8489b50d902SRodney W. Grimes } 8499b50d902SRodney W. Grimes 850a01e3379SDavid Malone const char * 8517b95a1ebSYaroslav Tykhiy plurales(uintmax_t n) 8529b50d902SRodney W. Grimes { 8539b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 8549b50d902SRodney W. Grimes } 8559b50d902SRodney W. Grimes 856f99a4046SMike Makonnen const char * 8577b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n) 858f99a4046SMike Makonnen { 859f99a4046SMike Makonnen return (n != 1 ? "ies" : "y"); 860f99a4046SMike Makonnen } 861f99a4046SMike Makonnen 8629b50d902SRodney W. Grimes /* 8639b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 8649b50d902SRodney W. Grimes */ 8659b50d902SRodney W. Grimes static struct protox * 866096146f8SYaroslav Tykhiy knownname(const char *name) 8679b50d902SRodney W. Grimes { 8689b50d902SRodney W. Grimes struct protox **tpp, *tp; 8699b50d902SRodney W. Grimes 8709b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 8719b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 8729b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 8739b50d902SRodney W. Grimes return (tp); 8749b50d902SRodney W. Grimes return (NULL); 8759b50d902SRodney W. Grimes } 8769b50d902SRodney W. Grimes 8779b50d902SRodney W. Grimes /* 8789b50d902SRodney W. Grimes * Find the protox corresponding to name. 8799b50d902SRodney W. Grimes */ 8809b50d902SRodney W. Grimes static struct protox * 881096146f8SYaroslav Tykhiy name2protox(const char *name) 8829b50d902SRodney W. Grimes { 8839b50d902SRodney W. Grimes struct protox *tp; 8849b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 8859b50d902SRodney W. Grimes struct protoent *p; 8869b50d902SRodney W. Grimes 8879b50d902SRodney W. Grimes /* 8889b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 8899b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 8909b50d902SRodney W. Grimes */ 891cfa1ca9dSYoshinobu Inoue if ((tp = knownname(name)) != NULL) 8929b50d902SRodney W. Grimes return (tp); 8939b50d902SRodney W. Grimes 8949b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 895cfa1ca9dSYoshinobu Inoue while ((p = getprotoent()) != NULL) { 8969b50d902SRodney W. Grimes /* assert: name not same as p->name */ 8979b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 8989b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 8999b50d902SRodney W. Grimes endprotoent(); 9009b50d902SRodney W. Grimes return (knownname(p->p_name)); 9019b50d902SRodney W. Grimes } 9029b50d902SRodney W. Grimes } 9039b50d902SRodney W. Grimes endprotoent(); 9049b50d902SRodney W. Grimes return (NULL); 9059b50d902SRodney W. Grimes } 9069b50d902SRodney W. Grimes 9079b50d902SRodney W. Grimes static void 9085e051718SAssar Westerlund usage(void) 9099b50d902SRodney W. Grimes { 910ade9ccfeSMarcel 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", 911182e8ae2SDoug Rabson "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n" 9121cb467b1SRuslan Ermilov " [-M core] [-N system]", 913182e8ae2SDoug Rabson " netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n" 914d44ddba9SRuslan Ermilov " [-M core] [-N system]", 915182e8ae2SDoug Rabson " netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n" 916fd2c6bc9SAllan Jude " [-q howmany]", 917182e8ae2SDoug Rabson " netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n" 91882d383bcSRuslan Ermilov " [-M core] [-N system]", 919182e8ae2SDoug Rabson " netstat [-j jail] -i | -I interface -s [-46s]\n" 920fd2c6bc9SAllan Jude " [-f protocol_family | -p protocol] [-M core] [-N system]", 921182e8ae2SDoug Rabson " netstat [-j jail] -m [-M core] [-N system]", 922182e8ae2SDoug Rabson " netstat [-j jail] -B [-z] [-I interface]", 923182e8ae2SDoug Rabson " netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n" 924fd2c6bc9SAllan Jude " [-M core] [-N system]", 925182e8ae2SDoug Rabson " netstat [-j jail] -rs [-s] [-M core] [-N system]", 926182e8ae2SDoug Rabson " netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]", 927182e8ae2SDoug Rabson " netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]", 928182e8ae2SDoug Rabson " netstat [-j jail] -Q"); 929ade9ccfeSMarcel Moolenaar xo_finish(); 9309b50d902SRodney W. Grimes exit(1); 9319b50d902SRodney W. Grimes } 932