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> 49821df508SXin LI #include <errno.h> 50182e8ae2SDoug Rabson #ifdef JAIL 51182e8ae2SDoug Rabson #include <jail.h> 52182e8ae2SDoug Rabson #endif 539b50d902SRodney W. Grimes #include <kvm.h> 549b50d902SRodney W. Grimes #include <limits.h> 559b50d902SRodney W. Grimes #include <netdb.h> 569b50d902SRodney W. Grimes #include <nlist.h> 57821df508SXin LI #include <paths.h> 587b95a1ebSYaroslav Tykhiy #include <stdint.h> 599b50d902SRodney W. Grimes #include <stdio.h> 609b50d902SRodney W. Grimes #include <stdlib.h> 61ade9ccfeSMarcel Moolenaar #include <stdbool.h> 629b50d902SRodney W. Grimes #include <string.h> 6395968ea7SYan-Hao Wang #include <sysexits.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 }, 1195dea523bSKristof Provost { -1, N_PFLOWSTATS, 1, NULL, 1205dea523bSKristof 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 26195968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "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 26895968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "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))) 29695968ea7SYan-Hao Wang xo_errx(EX_DATAERR, "%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 { 32095968ea7SYan-Hao Wang xo_errx(EX_DATAERR, "%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 34895968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "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) { 37795968ea7SYan-Hao Wang xo_errx(EX_DATAERR, "%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) 45195968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "Jail not found"); 452182e8ae2SDoug Rabson if (jail_attach(jid) != 0) 45395968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "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) 46495968ea7SYan-Hao Wang xo_err(EX_OSERR, "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) 47095968ea7SYan-Hao Wang xo_errx(EX_USAGE, "-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); 47695968ea7SYan-Hao Wang if (xo_finish() < 0) 47795968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 47895968ea7SYan-Hao Wang exit(EX_OK); 4796b463eedSChristian S.J. Peron } 4809b50d902SRodney W. Grimes if (mflag) { 48183708764SRuslan Ermilov if (!live) { 482feda1a43SJohn Baldwin if (kread(0, NULL, 0) == 0) 48305d1f5bcSAndrey V. Elsukov mbpr(kvmd, nl[N_SFSTAT].n_value); 484d15c5f56SRuslan Ermilov } else 485d4426f28SRobert Watson mbpr(NULL, 0); 48695968ea7SYan-Hao Wang if (xo_finish() < 0) 48795968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 48895968ea7SYan-Hao Wang exit(EX_OK); 4899b50d902SRodney W. Grimes } 4900153eb66SRobert Watson if (Qflag) { 49188737be2SRobert Watson if (!live) { 49288737be2SRobert Watson if (kread(0, NULL, 0) == 0) 49381dacd8bSHiroki Sato netisr_stats(); 49488737be2SRobert Watson } else 49581dacd8bSHiroki Sato netisr_stats(); 49695968ea7SYan-Hao Wang if (xo_finish() < 0) 49795968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 49895968ea7SYan-Hao Wang exit(EX_OK); 4990153eb66SRobert Watson } 500cc63cd56SPeter Wemm #if 0 5019b50d902SRodney W. Grimes /* 5029b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 5039b50d902SRodney W. Grimes * of open/close on each call to get* routines. 5049b50d902SRodney W. Grimes */ 5059b50d902SRodney W. Grimes sethostent(1); 5069b50d902SRodney W. Grimes setnetent(1); 507cc63cd56SPeter Wemm #else 508cc63cd56SPeter Wemm /* 509cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 510cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 511cc63cd56SPeter Wemm * used for the queries, which is slower. 512cc63cd56SPeter Wemm */ 513cc63cd56SPeter Wemm #endif 514cf5e44f8SRuslan Ermilov if (iflag && !sflag) { 515ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5165c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 51710d5269fSHiroki Sato intpr(NULL, af); 518ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 51995968ea7SYan-Hao Wang if (xo_finish() < 0) 52095968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 52195968ea7SYan-Hao Wang exit(EX_OK); 5229b50d902SRodney W. Grimes } 5239b50d902SRodney W. Grimes if (rflag) { 524ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5255c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 526*f3a097d0SKyle Evans if (sflag) 527fc47e028SAlexander V. Chernikov rt_stats(); 528*f3a097d0SKyle Evans else 529fc47e028SAlexander V. Chernikov routepr(fib, af); 530ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 53195968ea7SYan-Hao Wang if (xo_finish() < 0) 53295968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 53395968ea7SYan-Hao Wang exit(EX_OK); 5349b50d902SRodney W. Grimes } 535a6663252SAlexander V. Chernikov if (oflag) { 536a6663252SAlexander V. Chernikov xo_open_container("statistics"); 5375c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 538a6663252SAlexander V. Chernikov nhops_print(fib, af); 539a6663252SAlexander V. Chernikov xo_close_container("statistics"); 54095968ea7SYan-Hao Wang if (xo_finish() < 0) 54195968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 54295968ea7SYan-Hao Wang exit(EX_OK); 543a6663252SAlexander V. Chernikov } 544fedeb08bSAlexander V. Chernikov if (Oflag) { 545fedeb08bSAlexander V. Chernikov xo_open_container("statistics"); 5465c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 547fedeb08bSAlexander V. Chernikov nhgrp_print(fib, af); 548fedeb08bSAlexander V. Chernikov xo_close_container("statistics"); 54995968ea7SYan-Hao Wang if (xo_finish() < 0) 55095968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 55195968ea7SYan-Hao Wang exit(EX_OK); 552fedeb08bSAlexander V. Chernikov } 553fedeb08bSAlexander V. Chernikov 554a6663252SAlexander V. Chernikov 555fc47e028SAlexander V. Chernikov 5569b50d902SRodney W. Grimes if (gflag) { 557ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5585c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 559cfa1ca9dSYoshinobu Inoue if (sflag) { 560cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 561fc47e028SAlexander V. Chernikov mrt_stats(); 562cfa1ca9dSYoshinobu Inoue #ifdef INET6 563cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 564fc47e028SAlexander V. Chernikov mrt6_stats(); 565cfa1ca9dSYoshinobu Inoue #endif 566cfa1ca9dSYoshinobu Inoue } else { 567cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 568fc47e028SAlexander V. Chernikov mroutepr(); 569cfa1ca9dSYoshinobu Inoue #ifdef INET6 570cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 571fc47e028SAlexander V. Chernikov mroute6pr(); 572cfa1ca9dSYoshinobu Inoue #endif 573cfa1ca9dSYoshinobu Inoue } 574ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 57595968ea7SYan-Hao Wang if (xo_finish() < 0) 57695968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 57795968ea7SYan-Hao Wang exit(EX_OK); 5789b50d902SRodney W. Grimes } 579cfa1ca9dSYoshinobu Inoue 580cf5e44f8SRuslan Ermilov if (tp) { 581ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5825c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 583ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 584ade9ccfeSMarcel Moolenaar if (!first) 585ade9ccfeSMarcel Moolenaar xo_close_list("socket"); 586ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 58795968ea7SYan-Hao Wang if (xo_finish() < 0) 58895968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 58995968ea7SYan-Hao Wang exit(EX_OK); 590cf5e44f8SRuslan Ermilov } 591ade9ccfeSMarcel Moolenaar 592ade9ccfeSMarcel Moolenaar xo_open_container("statistics"); 5935c4f64bdSBram xo_set_version(NETSTAT_XO_VERSION); 594cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 5959b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 596ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 597cfa1ca9dSYoshinobu Inoue #ifdef INET6 598cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 599cfa1ca9dSYoshinobu Inoue for (tp = ip6protox; tp->pr_name; tp++) 600ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 601cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 6023b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 6033b8a8567SJun-ichiro itojun Hagino if (af == PF_KEY || af == AF_UNSPEC) 6043b8a8567SJun-ichiro itojun Hagino for (tp = pfkeyprotox; tp->pr_name; tp++) 605ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 6063b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/ 607690f477dSSam Leffler #ifdef NETGRAPH 6084cf49a43SJulian Elischer if (af == AF_NETGRAPH || af == AF_UNSPEC) 6094cf49a43SJulian Elischer for (tp = netgraphprotox; tp->pr_name; tp++) 610ade9ccfeSMarcel Moolenaar printproto(tp, tp->pr_name, &first); 611690f477dSSam Leffler #endif /* NETGRAPH */ 612f1c0a78dSMaxim Konovalov if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 613feda1a43SJohn Baldwin unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 614963b7ccdSRobert Watson nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 615ade9ccfeSMarcel Moolenaar nl[N_UNP_SPHEAD].n_value, &first); 616ade9ccfeSMarcel Moolenaar 617ade9ccfeSMarcel Moolenaar if (!first) 618ade9ccfeSMarcel Moolenaar xo_close_list("socket"); 619ade9ccfeSMarcel Moolenaar xo_close_container("statistics"); 62095968ea7SYan-Hao Wang if (xo_finish() < 0) 62195968ea7SYan-Hao Wang xo_err(EX_IOERR, "stdout"); 62295968ea7SYan-Hao Wang exit(EX_OK); 6239b50d902SRodney W. Grimes } 6249b50d902SRodney W. Grimes 625dbfd8708SGleb Smirnoff static int 626dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats, 627dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn, int zero) 6289eddb899SMark Johnston { 6299eddb899SMark Johnston int error; 6309eddb899SMark Johnston 6319eddb899SMark Johnston if (live) { 6329eddb899SMark Johnston memset(stats, 0, len); 633dbfd8708SGleb Smirnoff if (zero) 6349eddb899SMark Johnston error = sysctlbyname(sysctlname, NULL, NULL, stats, 6359eddb899SMark Johnston len); 6369eddb899SMark Johnston else 6379eddb899SMark Johnston error = sysctlbyname(sysctlname, stats, &len, NULL, 0); 6389eddb899SMark Johnston if (error == -1 && errno != ENOENT) 6399eddb899SMark Johnston xo_warn("sysctl %s", sysctlname); 6409eddb899SMark Johnston } else { 6419eddb899SMark Johnston if (off == 0) 6429eddb899SMark Johnston return (1); 6439eddb899SMark Johnston error = kreadfn(off, stats, len); 6449eddb899SMark Johnston } 6459eddb899SMark Johnston return (error); 6469eddb899SMark Johnston } 6479eddb899SMark Johnston 648dbfd8708SGleb Smirnoff int 649dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats, 650dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn) 651dbfd8708SGleb Smirnoff { 652dbfd8708SGleb Smirnoff 653dbfd8708SGleb Smirnoff return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 654dbfd8708SGleb Smirnoff zflag)); 655dbfd8708SGleb Smirnoff } 656dbfd8708SGleb Smirnoff 657dbfd8708SGleb Smirnoff int 658dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats, 659dbfd8708SGleb Smirnoff size_t len, kreadfn_t kreadfn) 660dbfd8708SGleb Smirnoff { 661dbfd8708SGleb Smirnoff 662dbfd8708SGleb Smirnoff return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); 663dbfd8708SGleb Smirnoff } 664dbfd8708SGleb Smirnoff 6659b50d902SRodney W. Grimes /* 6669b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 6679b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 6689b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 6699b50d902SRodney W. Grimes */ 6709b50d902SRodney W. Grimes static void 671ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first) 6729b50d902SRodney W. Grimes { 673feda1a43SJohn Baldwin void (*pr)(u_long, const char *, int, int); 6749b50d902SRodney W. Grimes u_long off; 675ade9ccfeSMarcel Moolenaar bool doingdblocks = false; 6769b50d902SRodney W. Grimes 6779b50d902SRodney W. Grimes if (sflag) { 678cfa1ca9dSYoshinobu Inoue if (iflag) { 679cfa1ca9dSYoshinobu Inoue if (tp->pr_istats) 68010d5269fSHiroki Sato intpr(tp->pr_istats, af); 681cf5e44f8SRuslan Ermilov else if (pflag) 682ade9ccfeSMarcel Moolenaar xo_message("%s: no per-interface stats routine", 683cf5e44f8SRuslan Ermilov tp->pr_name); 684cfa1ca9dSYoshinobu Inoue return; 685feda1a43SJohn Baldwin } else { 6869b50d902SRodney W. Grimes pr = tp->pr_stats; 687cf5e44f8SRuslan Ermilov if (!pr) { 688cf5e44f8SRuslan Ermilov if (pflag) 689ade9ccfeSMarcel Moolenaar xo_message("%s: no stats routine", 690cf5e44f8SRuslan Ermilov tp->pr_name); 691cf5e44f8SRuslan Ermilov return; 692cf5e44f8SRuslan Ermilov } 693feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 694feda1a43SJohn Baldwin off = 0; 695feda1a43SJohn Baldwin else if (tp->pr_sindex < 0) { 696feda1a43SJohn Baldwin if (pflag) 697ade9ccfeSMarcel Moolenaar xo_message("%s: stats routine doesn't " 698ade9ccfeSMarcel Moolenaar "work on cores", tp->pr_name); 699feda1a43SJohn Baldwin return; 700feda1a43SJohn Baldwin } else 701feda1a43SJohn Baldwin off = nl[tp->pr_sindex].n_value; 702cfa1ca9dSYoshinobu Inoue } 7039b50d902SRodney W. Grimes } else { 704ade9ccfeSMarcel Moolenaar doingdblocks = true; 7059b50d902SRodney W. Grimes pr = tp->pr_cblocks; 706cf5e44f8SRuslan Ermilov if (!pr) { 707cf5e44f8SRuslan Ermilov if (pflag) 708ade9ccfeSMarcel Moolenaar xo_message("%s: no PCB routine", tp->pr_name); 709cf5e44f8SRuslan Ermilov return; 710cf5e44f8SRuslan Ermilov } 711feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 712feda1a43SJohn Baldwin off = 0; 713feda1a43SJohn Baldwin else if (tp->pr_index < 0) { 714feda1a43SJohn Baldwin if (pflag) 715ade9ccfeSMarcel Moolenaar xo_message("%s: PCB routine doesn't work on " 716ade9ccfeSMarcel Moolenaar "cores", tp->pr_name); 717feda1a43SJohn Baldwin return; 718feda1a43SJohn Baldwin } else 719feda1a43SJohn Baldwin off = nl[tp->pr_index].n_value; 7209b50d902SRodney W. Grimes } 721feda1a43SJohn Baldwin if (pr != NULL && (off || (live && tp->pr_usesysctl) || 722ade9ccfeSMarcel Moolenaar af != AF_UNSPEC)) { 723ade9ccfeSMarcel Moolenaar if (doingdblocks && *first) { 724ade9ccfeSMarcel Moolenaar xo_open_list("socket"); 725ade9ccfeSMarcel Moolenaar *first = false; 726ade9ccfeSMarcel Moolenaar } 727ade9ccfeSMarcel Moolenaar 728feda1a43SJohn Baldwin (*pr)(off, name, af, tp->pr_protocol); 7299b50d902SRodney W. Grimes } 730ade9ccfeSMarcel Moolenaar } 7319b50d902SRodney W. Grimes 73229dde48dSGleb Smirnoff static int 73329dde48dSGleb Smirnoff kvmd_init(void) 7349b50d902SRodney W. Grimes { 735feda1a43SJohn Baldwin char errbuf[_POSIX2_LINE_MAX]; 736feda1a43SJohn Baldwin 73729dde48dSGleb Smirnoff if (kvmd != NULL) 73829dde48dSGleb Smirnoff return (0); 73929dde48dSGleb Smirnoff 740feda1a43SJohn Baldwin kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 741a4a889a0SXin LI if (setgid(getgid()) != 0) 74295968ea7SYan-Hao Wang xo_err(EX_OSERR, "setgid"); 74329dde48dSGleb Smirnoff 74429dde48dSGleb Smirnoff if (kvmd == NULL) { 745ade9ccfeSMarcel Moolenaar xo_warnx("kvm not available: %s", errbuf); 74629dde48dSGleb Smirnoff return (-1); 74729dde48dSGleb Smirnoff } 74829dde48dSGleb Smirnoff 749fc47e028SAlexander V. Chernikov return (0); 750fc47e028SAlexander V. Chernikov } 751fc47e028SAlexander V. Chernikov 752fc47e028SAlexander V. Chernikov /* 753fc47e028SAlexander V. Chernikov * Resolve symbol list, return 0 on success. 754fc47e028SAlexander V. Chernikov */ 75581dacd8bSHiroki Sato static int 756fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl) 757fc47e028SAlexander V. Chernikov { 758fc47e028SAlexander V. Chernikov 759fc47e028SAlexander V. Chernikov if ((kvmd == NULL) && (kvmd_init() != 0)) 760fc47e028SAlexander V. Chernikov return (-1); 761fc47e028SAlexander V. Chernikov 762fc47e028SAlexander V. Chernikov if (_nl[0].n_type != 0) 763fc47e028SAlexander V. Chernikov return (0); 764fc47e028SAlexander V. Chernikov 765fc47e028SAlexander V. Chernikov if (kvm_nlist(kvmd, _nl) < 0) { 76699453c6aSPoul-Henning Kamp if (nlistf) 76795968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "%s: kvm_nlist: %s", nlistf, 76899453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 76999453c6aSPoul-Henning Kamp else 77095968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "kvm_nlist: %s", kvm_geterr(kvmd)); 77199453c6aSPoul-Henning Kamp } 77299453c6aSPoul-Henning Kamp 77329dde48dSGleb Smirnoff return (0); 77429dde48dSGleb Smirnoff } 77529dde48dSGleb Smirnoff 77629dde48dSGleb Smirnoff /* 77781dacd8bSHiroki Sato * Wrapper of kvm_dpcpu_setcpu(). 77881dacd8bSHiroki Sato */ 77981dacd8bSHiroki Sato void 78081dacd8bSHiroki Sato kset_dpcpu(u_int cpuid) 78181dacd8bSHiroki Sato { 78281dacd8bSHiroki Sato 78381dacd8bSHiroki Sato if ((kvmd == NULL) && (kvmd_init() != 0)) 78495968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "%s: kvm is not available", __func__); 78581dacd8bSHiroki Sato 78681dacd8bSHiroki Sato if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0) 78795968ea7SYan-Hao Wang xo_errx(EX_UNAVAILABLE, "%s: kvm_dpcpu_setcpu(%u): %s", __func__, 78881dacd8bSHiroki Sato cpuid, kvm_geterr(kvmd)); 78981dacd8bSHiroki Sato return; 79081dacd8bSHiroki Sato } 79181dacd8bSHiroki Sato 79281dacd8bSHiroki Sato /* 79329dde48dSGleb Smirnoff * Read kernel memory, return 0 on success. 79429dde48dSGleb Smirnoff */ 79529dde48dSGleb Smirnoff int 79629dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size) 79729dde48dSGleb Smirnoff { 79829dde48dSGleb Smirnoff 79929dde48dSGleb Smirnoff if (kvmd_init() < 0) 80099453c6aSPoul-Henning Kamp return (-1); 80129dde48dSGleb Smirnoff 802c6620a13SPoul-Henning Kamp if (!buf) 803c6620a13SPoul-Henning Kamp return (0); 804feda1a43SJohn Baldwin if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 805ade9ccfeSMarcel Moolenaar xo_warnx("%s", kvm_geterr(kvmd)); 8069b50d902SRodney W. Grimes return (-1); 8079b50d902SRodney W. Grimes } 8089b50d902SRodney W. Grimes return (0); 8099b50d902SRodney W. Grimes } 8109b50d902SRodney W. Grimes 81129dde48dSGleb Smirnoff /* 812e3a7aa6fSGleb Smirnoff * Read single counter(9). 813e3a7aa6fSGleb Smirnoff */ 814e3a7aa6fSGleb Smirnoff uint64_t 815e3a7aa6fSGleb Smirnoff kread_counter(u_long addr) 816e3a7aa6fSGleb Smirnoff { 817e3a7aa6fSGleb Smirnoff 818e3a7aa6fSGleb Smirnoff if (kvmd_init() < 0) 819e3a7aa6fSGleb Smirnoff return (-1); 820e3a7aa6fSGleb Smirnoff 821e3a7aa6fSGleb Smirnoff return (kvm_counter_u64_fetch(kvmd, addr)); 822e3a7aa6fSGleb Smirnoff } 823e3a7aa6fSGleb Smirnoff 824e3a7aa6fSGleb Smirnoff /* 82529dde48dSGleb Smirnoff * Read an array of N counters in kernel memory into array of N uint64_t's. 82629dde48dSGleb Smirnoff */ 82729dde48dSGleb Smirnoff int 8285da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size) 82929dde48dSGleb Smirnoff { 830794c57d3SMark Johnston uint64_t *c; 831794c57d3SMark Johnston u_long *counters; 832794c57d3SMark Johnston size_t i, n; 83329dde48dSGleb Smirnoff 83429dde48dSGleb Smirnoff if (kvmd_init() < 0) 83529dde48dSGleb Smirnoff return (-1); 83629dde48dSGleb Smirnoff 837794c57d3SMark Johnston if (size % sizeof(uint64_t) != 0) { 838794c57d3SMark Johnston xo_warnx("kread_counters: invalid counter set size"); 8395da0521fSAndrey V. Elsukov return (-1); 8405da0521fSAndrey V. Elsukov } 841794c57d3SMark Johnston 842794c57d3SMark Johnston n = size / sizeof(uint64_t); 843794c57d3SMark Johnston if ((counters = malloc(n * sizeof(u_long))) == NULL) 84495968ea7SYan-Hao Wang xo_err(EX_OSERR, "malloc"); 845794c57d3SMark Johnston if (kread(addr, counters, n * sizeof(u_long)) < 0) { 846794c57d3SMark Johnston free(counters); 847794c57d3SMark Johnston return (-1); 848794c57d3SMark Johnston } 849794c57d3SMark Johnston 850794c57d3SMark Johnston c = buf; 851794c57d3SMark Johnston for (i = 0; i < n; i++) 852794c57d3SMark Johnston c[i] = kvm_counter_u64_fetch(kvmd, counters[i]); 853794c57d3SMark Johnston 854794c57d3SMark Johnston free(counters); 85529dde48dSGleb Smirnoff return (0); 85629dde48dSGleb Smirnoff } 85729dde48dSGleb Smirnoff 858a01e3379SDavid Malone const char * 8597b95a1ebSYaroslav Tykhiy plural(uintmax_t n) 8609b50d902SRodney W. Grimes { 8619b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 8629b50d902SRodney W. Grimes } 8639b50d902SRodney W. Grimes 864a01e3379SDavid Malone const char * 8657b95a1ebSYaroslav Tykhiy plurales(uintmax_t n) 8669b50d902SRodney W. Grimes { 8679b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 8689b50d902SRodney W. Grimes } 8699b50d902SRodney W. Grimes 870f99a4046SMike Makonnen const char * 8717b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n) 872f99a4046SMike Makonnen { 873f99a4046SMike Makonnen return (n != 1 ? "ies" : "y"); 874f99a4046SMike Makonnen } 875f99a4046SMike Makonnen 8769b50d902SRodney W. Grimes /* 8779b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 8789b50d902SRodney W. Grimes */ 8799b50d902SRodney W. Grimes static struct protox * 880096146f8SYaroslav Tykhiy knownname(const char *name) 8819b50d902SRodney W. Grimes { 8829b50d902SRodney W. Grimes struct protox **tpp, *tp; 8839b50d902SRodney W. Grimes 8849b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 8859b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 8869b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 8879b50d902SRodney W. Grimes return (tp); 8889b50d902SRodney W. Grimes return (NULL); 8899b50d902SRodney W. Grimes } 8909b50d902SRodney W. Grimes 8919b50d902SRodney W. Grimes /* 8929b50d902SRodney W. Grimes * Find the protox corresponding to name. 8939b50d902SRodney W. Grimes */ 8949b50d902SRodney W. Grimes static struct protox * 895096146f8SYaroslav Tykhiy name2protox(const char *name) 8969b50d902SRodney W. Grimes { 8979b50d902SRodney W. Grimes struct protox *tp; 8989b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 8999b50d902SRodney W. Grimes struct protoent *p; 9009b50d902SRodney W. Grimes 9019b50d902SRodney W. Grimes /* 9029b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 9039b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 9049b50d902SRodney W. Grimes */ 905cfa1ca9dSYoshinobu Inoue if ((tp = knownname(name)) != NULL) 9069b50d902SRodney W. Grimes return (tp); 9079b50d902SRodney W. Grimes 9089b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 909cfa1ca9dSYoshinobu Inoue while ((p = getprotoent()) != NULL) { 9109b50d902SRodney W. Grimes /* assert: name not same as p->name */ 9119b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 9129b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 9139b50d902SRodney W. Grimes endprotoent(); 9149b50d902SRodney W. Grimes return (knownname(p->p_name)); 9159b50d902SRodney W. Grimes } 9169b50d902SRodney W. Grimes } 9179b50d902SRodney W. Grimes endprotoent(); 9189b50d902SRodney W. Grimes return (NULL); 9199b50d902SRodney W. Grimes } 9209b50d902SRodney W. Grimes 9219b50d902SRodney W. Grimes static void 9225e051718SAssar Westerlund usage(void) 9239b50d902SRodney W. Grimes { 92495968ea7SYan-Hao Wang 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", 925182e8ae2SDoug Rabson "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n" 9261cb467b1SRuslan Ermilov " [-M core] [-N system]", 927182e8ae2SDoug Rabson " netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n" 928d44ddba9SRuslan Ermilov " [-M core] [-N system]", 929182e8ae2SDoug Rabson " netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n" 930fd2c6bc9SAllan Jude " [-q howmany]", 931182e8ae2SDoug Rabson " netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n" 93282d383bcSRuslan Ermilov " [-M core] [-N system]", 933182e8ae2SDoug Rabson " netstat [-j jail] -i | -I interface -s [-46s]\n" 934fd2c6bc9SAllan Jude " [-f protocol_family | -p protocol] [-M core] [-N system]", 935182e8ae2SDoug Rabson " netstat [-j jail] -m [-M core] [-N system]", 936182e8ae2SDoug Rabson " netstat [-j jail] -B [-z] [-I interface]", 937182e8ae2SDoug Rabson " netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n" 938fd2c6bc9SAllan Jude " [-M core] [-N system]", 939182e8ae2SDoug Rabson " netstat [-j jail] -rs [-s] [-M core] [-N system]", 940182e8ae2SDoug Rabson " netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]", 941182e8ae2SDoug Rabson " netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]", 942182e8ae2SDoug Rabson " netstat [-j jail] -Q"); 94395968ea7SYan-Hao Wang exit(EX_USAGE); 9449b50d902SRodney W. Grimes } 945