165475bc8SDavid E. O'Brien /*- 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 39b50d902SRodney W. Grimes * Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 299b50d902SRodney W. Grimes 309b50d902SRodney W. Grimes #ifndef lint 315d422d6aSPhilippe Charnier char const copyright[] = 329b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\ 339b50d902SRodney W. Grimes Regents of the University of California. All rights reserved.\n"; 349b50d902SRodney W. Grimes #endif /* not lint */ 359b50d902SRodney W. Grimes 365d422d6aSPhilippe Charnier #if 0 376cc6f122SPhilippe Charnier #ifndef lint 389b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 399b50d902SRodney W. Grimes #endif /* not lint */ 406cc6f122SPhilippe Charnier #endif 416cc6f122SPhilippe Charnier 426cc6f122SPhilippe Charnier #include <sys/cdefs.h> 436cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <sys/param.h> 469b50d902SRodney W. Grimes #include <sys/file.h> 479b50d902SRodney W. Grimes #include <sys/protosw.h> 489b50d902SRodney W. Grimes #include <sys/socket.h> 49feda1a43SJohn Baldwin #include <sys/socketvar.h> 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #include <netinet/in.h> 529b50d902SRodney W. Grimes 53690f477dSSam Leffler #ifdef NETGRAPH 544cf49a43SJulian Elischer #include <netgraph/ng_socket.h> 55690f477dSSam Leffler #endif 564cf49a43SJulian Elischer 579b50d902SRodney W. Grimes #include <ctype.h> 585d422d6aSPhilippe Charnier #include <err.h> 59821df508SXin LI #include <errno.h> 609b50d902SRodney W. Grimes #include <kvm.h> 619b50d902SRodney W. Grimes #include <limits.h> 629b50d902SRodney W. Grimes #include <netdb.h> 639b50d902SRodney W. Grimes #include <nlist.h> 64821df508SXin LI #include <paths.h> 657b95a1ebSYaroslav Tykhiy #include <stdint.h> 669b50d902SRodney W. Grimes #include <stdio.h> 679b50d902SRodney W. Grimes #include <stdlib.h> 689b50d902SRodney W. Grimes #include <string.h> 699b50d902SRodney W. Grimes #include <unistd.h> 709b50d902SRodney W. Grimes #include "netstat.h" 719b50d902SRodney W. Grimes 72c6620a13SPoul-Henning Kamp static struct nlist nl[] = { 734f81ef50SGarrett Wollman #define N_IFNET 0 74096146f8SYaroslav Tykhiy { .n_name = "_ifnet" }, 75ab54ea99SPeter Wemm #define N_RTSTAT 1 76096146f8SYaroslav Tykhiy { .n_name = "_rtstat" }, 77ab54ea99SPeter Wemm #define N_RTREE 2 78096146f8SYaroslav Tykhiy { .n_name = "_rt_tables"}, 79ab54ea99SPeter Wemm #define N_MRTSTAT 3 80096146f8SYaroslav Tykhiy { .n_name = "_mrtstat" }, 81443fc317SBruce M Simpson #define N_MFCHASHTBL 4 82443fc317SBruce M Simpson { .n_name = "_mfchashtbl" }, 83ab54ea99SPeter Wemm #define N_VIFTABLE 5 84096146f8SYaroslav Tykhiy { .n_name = "_viftable" }, 85ab54ea99SPeter Wemm #define N_IPX 6 86096146f8SYaroslav Tykhiy { .n_name = "_ipxpcb_list"}, 87ab54ea99SPeter Wemm #define N_IPXSTAT 7 88096146f8SYaroslav Tykhiy { .n_name = "_ipxstat"}, 89ab54ea99SPeter Wemm #define N_SPXSTAT 8 90096146f8SYaroslav Tykhiy { .n_name = "_spx_istat"}, 91ab54ea99SPeter Wemm #define N_DDPSTAT 9 92096146f8SYaroslav Tykhiy { .n_name = "_ddpstat"}, 93ab54ea99SPeter Wemm #define N_DDPCB 10 94096146f8SYaroslav Tykhiy { .n_name = "_ddpcb"}, 95ab54ea99SPeter Wemm #define N_NGSOCKS 11 96096146f8SYaroslav Tykhiy { .n_name = "_ngsocklist"}, 97ab54ea99SPeter Wemm #define N_IP6STAT 12 98096146f8SYaroslav Tykhiy { .n_name = "_ip6stat" }, 99ab54ea99SPeter Wemm #define N_ICMP6STAT 13 100096146f8SYaroslav Tykhiy { .n_name = "_icmp6stat" }, 101ab54ea99SPeter Wemm #define N_IPSECSTAT 14 1028409aedfSGeorge V. Neville-Neil { .n_name = "_ipsec4stat" }, 103ab54ea99SPeter Wemm #define N_IPSEC6STAT 15 104096146f8SYaroslav Tykhiy { .n_name = "_ipsec6stat" }, 105ab54ea99SPeter Wemm #define N_PIM6STAT 16 106096146f8SYaroslav Tykhiy { .n_name = "_pim6stat" }, 107ab54ea99SPeter Wemm #define N_MRT6STAT 17 108096146f8SYaroslav Tykhiy { .n_name = "_mrt6stat" }, 109ab54ea99SPeter Wemm #define N_MF6CTABLE 18 110096146f8SYaroslav Tykhiy { .n_name = "_mf6ctable" }, 111ab54ea99SPeter Wemm #define N_MIF6TABLE 19 112096146f8SYaroslav Tykhiy { .n_name = "_mif6table" }, 113ab54ea99SPeter Wemm #define N_PFKEYSTAT 20 114096146f8SYaroslav Tykhiy { .n_name = "_pfkeystat" }, 115ab54ea99SPeter Wemm #define N_MBSTAT 21 116096146f8SYaroslav Tykhiy { .n_name = "_mbstat" }, 117ab54ea99SPeter Wemm #define N_MBTYPES 22 118096146f8SYaroslav Tykhiy { .n_name = "_mbtypes" }, 119ab54ea99SPeter Wemm #define N_NMBCLUSTERS 23 120096146f8SYaroslav Tykhiy { .n_name = "_nmbclusters" }, 121ab54ea99SPeter Wemm #define N_NMBUFS 24 122096146f8SYaroslav Tykhiy { .n_name = "_nmbufs" }, 123ab54ea99SPeter Wemm #define N_MBHI 25 124096146f8SYaroslav Tykhiy { .n_name = "_mbuf_hiwm" }, 125ab54ea99SPeter Wemm #define N_CLHI 26 126096146f8SYaroslav Tykhiy { .n_name = "_clust_hiwm" }, 127ab54ea99SPeter Wemm #define N_NCPUS 27 128096146f8SYaroslav Tykhiy { .n_name = "_smp_cpus" }, 129ab54ea99SPeter Wemm #define N_PAGESZ 28 130096146f8SYaroslav Tykhiy { .n_name = "_pagesize" }, 131ab54ea99SPeter Wemm #define N_MBPSTAT 29 132096146f8SYaroslav Tykhiy { .n_name = "_mb_statpcpu" }, 133ab54ea99SPeter Wemm #define N_RTTRASH 30 134096146f8SYaroslav Tykhiy { .n_name = "_rttrash" }, 135ab54ea99SPeter Wemm #define N_MBLO 31 136096146f8SYaroslav Tykhiy { .n_name = "_mbuf_lowm" }, 137ab54ea99SPeter Wemm #define N_CLLO 32 138096146f8SYaroslav Tykhiy { .n_name = "_clust_lowm" }, 139a9771948SGleb Smirnoff #define N_CARPSTAT 33 140096146f8SYaroslav Tykhiy { .n_name = "_carpstats" }, 1412e37c5a3SMax Laier #define N_PFSYNCSTAT 34 142096146f8SYaroslav Tykhiy { .n_name = "_pfsyncstats" }, 143feda1a43SJohn Baldwin #define N_AHSTAT 35 144096146f8SYaroslav Tykhiy { .n_name = "_ahstat" }, 145feda1a43SJohn Baldwin #define N_ESPSTAT 36 146096146f8SYaroslav Tykhiy { .n_name = "_espstat" }, 147feda1a43SJohn Baldwin #define N_IPCOMPSTAT 37 148096146f8SYaroslav Tykhiy { .n_name = "_ipcompstat" }, 149feda1a43SJohn Baldwin #define N_TCPSTAT 38 1505da0521fSAndrey V. Elsukov { .n_name = "_tcpstat" }, 151feda1a43SJohn Baldwin #define N_UDPSTAT 39 152feda1a43SJohn Baldwin { .n_name = "_udpstat" }, 153feda1a43SJohn Baldwin #define N_IPSTAT 40 1545da0521fSAndrey V. Elsukov { .n_name = "_ipstat" }, 155feda1a43SJohn Baldwin #define N_ICMPSTAT 41 156feda1a43SJohn Baldwin { .n_name = "_icmpstat" }, 157feda1a43SJohn Baldwin #define N_IGMPSTAT 42 158feda1a43SJohn Baldwin { .n_name = "_igmpstat" }, 159feda1a43SJohn Baldwin #define N_PIMSTAT 43 160feda1a43SJohn Baldwin { .n_name = "_pimstat" }, 161feda1a43SJohn Baldwin #define N_TCBINFO 44 162feda1a43SJohn Baldwin { .n_name = "_tcbinfo" }, 163feda1a43SJohn Baldwin #define N_UDBINFO 45 164feda1a43SJohn Baldwin { .n_name = "_udbinfo" }, 165feda1a43SJohn Baldwin #define N_DIVCBINFO 46 166feda1a43SJohn Baldwin { .n_name = "_divcbinfo" }, 167feda1a43SJohn Baldwin #define N_RIPCBINFO 47 168feda1a43SJohn Baldwin { .n_name = "_ripcbinfo" }, 169feda1a43SJohn Baldwin #define N_UNP_COUNT 48 170feda1a43SJohn Baldwin { .n_name = "_unp_count" }, 171feda1a43SJohn Baldwin #define N_UNP_GENCNT 49 172feda1a43SJohn Baldwin { .n_name = "_unp_gencnt" }, 173feda1a43SJohn Baldwin #define N_UNP_DHEAD 50 174feda1a43SJohn Baldwin { .n_name = "_unp_dhead" }, 175feda1a43SJohn Baldwin #define N_UNP_SHEAD 51 176feda1a43SJohn Baldwin { .n_name = "_unp_shead" }, 177feda1a43SJohn Baldwin #define N_RIP6STAT 52 178feda1a43SJohn Baldwin { .n_name = "_rip6stat" }, 179feda1a43SJohn Baldwin #define N_SCTPSTAT 53 180feda1a43SJohn Baldwin { .n_name = "_sctpstat" }, 181443fc317SBruce M Simpson #define N_MFCTABLESIZE 54 182443fc317SBruce M Simpson { .n_name = "_mfctablesize" }, 18354fc657dSGeorge V. Neville-Neil #define N_ARPSTAT 55 18454fc657dSGeorge V. Neville-Neil { .n_name = "_arpstat" }, 185963b7ccdSRobert Watson #define N_UNP_SPHEAD 56 186963b7ccdSRobert Watson { .n_name = "unp_sphead" }, 187096146f8SYaroslav Tykhiy { .n_name = NULL }, 1889b50d902SRodney W. Grimes }; 1899b50d902SRodney W. Grimes 1909b50d902SRodney W. Grimes struct protox { 191feda1a43SJohn Baldwin int pr_index; /* index into nlist of cb head */ 192feda1a43SJohn Baldwin int pr_sindex; /* index into nlist of stat block */ 1939b50d902SRodney W. Grimes u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 194feda1a43SJohn Baldwin void (*pr_cblocks)(u_long, const char *, int, int); 1955e051718SAssar Westerlund /* control blocks printing routine */ 196feda1a43SJohn Baldwin void (*pr_stats)(u_long, const char *, int, int); 1975e051718SAssar Westerlund /* statistics printing routine */ 1985e051718SAssar Westerlund void (*pr_istats)(char *); /* per/if statistics printing routine */ 199fa6d48c0SMark Murray const char *pr_name; /* well-known name */ 20055fd53e2SJohn Baldwin int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 201feda1a43SJohn Baldwin int pr_protocol; 2029b50d902SRodney W. Grimes } protox[] = { 203feda1a43SJohn Baldwin { N_TCBINFO, N_TCPSTAT, 1, protopr, 204feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 205feda1a43SJohn Baldwin { N_UDBINFO, N_UDPSTAT, 1, protopr, 206feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 20774fd40c9SRandall Stewart #ifdef SCTP 208feda1a43SJohn Baldwin { -1, N_SCTPSTAT, 1, sctp_protopr, 209feda1a43SJohn Baldwin sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 21074fd40c9SRandall Stewart #endif 211aa0a1e58SJeff Roberson #ifdef SDP 212aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 213aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 214aa0a1e58SJeff Roberson #endif 215feda1a43SJohn Baldwin { N_DIVCBINFO, -1, 1, protopr, 216feda1a43SJohn Baldwin NULL, NULL, "divert", 1, IPPROTO_DIVERT }, 217feda1a43SJohn Baldwin { N_RIPCBINFO, N_IPSTAT, 1, protopr, 218feda1a43SJohn Baldwin ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 219feda1a43SJohn Baldwin { N_RIPCBINFO, N_ICMPSTAT, 1, protopr, 220feda1a43SJohn Baldwin icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 221feda1a43SJohn Baldwin { N_RIPCBINFO, N_IGMPSTAT, 1, protopr, 222feda1a43SJohn Baldwin igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 223cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 2248409aedfSGeorge V. Neville-Neil { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */ 225feda1a43SJohn Baldwin ipsec_stats, NULL, "ipsec", 0, 0}, 2268409aedfSGeorge V. Neville-Neil { -1, N_AHSTAT, 1, NULL, 227feda1a43SJohn Baldwin ah_stats, NULL, "ah", 0, 0}, 2288409aedfSGeorge V. Neville-Neil { -1, N_ESPSTAT, 1, NULL, 229feda1a43SJohn Baldwin esp_stats, NULL, "esp", 0, 0}, 2308409aedfSGeorge V. Neville-Neil { -1, N_IPCOMPSTAT, 1, NULL, 231feda1a43SJohn Baldwin ipcomp_stats, NULL, "ipcomp", 0, 0}, 232100b98dbSKelly Yancey #endif 233feda1a43SJohn Baldwin { N_RIPCBINFO, N_PIMSTAT, 1, protopr, 234feda1a43SJohn Baldwin pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 235feda1a43SJohn Baldwin { -1, N_CARPSTAT, 1, NULL, 236feda1a43SJohn Baldwin carp_stats, NULL, "carp", 1, 0 }, 237feda1a43SJohn Baldwin { -1, N_PFSYNCSTAT, 1, NULL, 238feda1a43SJohn Baldwin pfsync_stats, NULL, "pfsync", 1, 0 }, 23954fc657dSGeorge V. Neville-Neil { -1, N_ARPSTAT, 1, NULL, 24054fc657dSGeorge V. Neville-Neil arp_stats, NULL, "arp", 1, 0 }, 2416bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 242feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 2439b50d902SRodney W. Grimes }; 2449b50d902SRodney W. Grimes 245cfa1ca9dSYoshinobu Inoue #ifdef INET6 246cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = { 247feda1a43SJohn Baldwin { N_TCBINFO, N_TCPSTAT, 1, protopr, 248feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 249feda1a43SJohn Baldwin { N_UDBINFO, N_UDPSTAT, 1, protopr, 250feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 251feda1a43SJohn Baldwin { N_RIPCBINFO, N_IP6STAT, 1, protopr, 252feda1a43SJohn Baldwin ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 253feda1a43SJohn Baldwin { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, 254feda1a43SJohn Baldwin icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 255aa0a1e58SJeff Roberson #ifdef SDP 256aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 257aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 258aa0a1e58SJeff Roberson #endif 259cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 2606bb3f207SRuslan Ermilov { -1, N_IPSEC6STAT, 1, NULL, 261feda1a43SJohn Baldwin ipsec_stats, NULL, "ipsec6", 0, 0 }, 262cfa1ca9dSYoshinobu Inoue #endif 263cfa1ca9dSYoshinobu Inoue #ifdef notyet 2646bb3f207SRuslan Ermilov { -1, N_PIM6STAT, 1, NULL, 265feda1a43SJohn Baldwin pim6_stats, NULL, "pim6", 1, 0 }, 266cfa1ca9dSYoshinobu Inoue #endif 267feda1a43SJohn Baldwin { -1, N_RIP6STAT, 1, NULL, 268feda1a43SJohn Baldwin rip6_stats, NULL, "rip6", 1, 0 }, 2696bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 270feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 271cfa1ca9dSYoshinobu Inoue }; 272cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 273cfa1ca9dSYoshinobu Inoue 2743b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 2753b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = { 2766bb3f207SRuslan Ermilov { -1, N_PFKEYSTAT, 1, NULL, 277feda1a43SJohn Baldwin pfkey_stats, NULL, "pfkey", 0, 0 }, 2786bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 279feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 2803b8a8567SJun-ichiro itojun Hagino }; 2813b8a8567SJun-ichiro itojun Hagino #endif 2823b8a8567SJun-ichiro itojun Hagino 28363bf4575SJulian Elischer struct protox atalkprotox[] = { 28463bf4575SJulian Elischer { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 285feda1a43SJohn Baldwin ddp_stats, NULL, "ddp", 0, 0 }, 2866bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 287feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 28863bf4575SJulian Elischer }; 289690f477dSSam Leffler #ifdef NETGRAPH 2904cf49a43SJulian Elischer struct protox netgraphprotox[] = { 2914cf49a43SJulian Elischer { N_NGSOCKS, -1, 1, netgraphprotopr, 292feda1a43SJohn Baldwin NULL, NULL, "ctrl", 0, 0 }, 2934cf49a43SJulian Elischer { N_NGSOCKS, -1, 1, netgraphprotopr, 294feda1a43SJohn Baldwin NULL, NULL, "data", 0, 0 }, 2956bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 296feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 2974cf49a43SJulian Elischer }; 298690f477dSSam Leffler #endif 2999cc22e5cSPoul-Henning Kamp #ifdef IPX 300cc6a66f2SJulian Elischer struct protox ipxprotox[] = { 301cc6a66f2SJulian Elischer { N_IPX, N_IPXSTAT, 1, ipxprotopr, 302feda1a43SJohn Baldwin ipx_stats, NULL, "ipx", 0, 0 }, 303cc6a66f2SJulian Elischer { N_IPX, N_SPXSTAT, 1, ipxprotopr, 304feda1a43SJohn Baldwin spx_stats, NULL, "spx", 0, 0 }, 3056bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 306feda1a43SJohn Baldwin NULL, NULL, 0, 0, 0 } 307cc6a66f2SJulian Elischer }; 3089cc22e5cSPoul-Henning Kamp #endif 309cc6a66f2SJulian Elischer 310cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = { 311cfa1ca9dSYoshinobu Inoue protox, 312cfa1ca9dSYoshinobu Inoue #ifdef INET6 313cfa1ca9dSYoshinobu Inoue ip6protox, 314cfa1ca9dSYoshinobu Inoue #endif 3153b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 3163b8a8567SJun-ichiro itojun Hagino pfkeyprotox, 3173b8a8567SJun-ichiro itojun Hagino #endif 3189cc22e5cSPoul-Henning Kamp #ifdef IPX 3199cc22e5cSPoul-Henning Kamp ipxprotox, 3209cc22e5cSPoul-Henning Kamp #endif 3219cc22e5cSPoul-Henning Kamp atalkprotox, NULL }; 3229b50d902SRodney W. Grimes 323fa6d48c0SMark Murray static void printproto(struct protox *, const char *); 3245e051718SAssar Westerlund static void usage(void); 325096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *); 326096146f8SYaroslav Tykhiy static struct protox *knownname(const char *); 3279b50d902SRodney W. Grimes 328c6620a13SPoul-Henning Kamp static kvm_t *kvmd; 329080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL; 330080b7f49SDag-Erling Smørgrav 331080b7f49SDag-Erling Smørgrav int Aflag; /* show addresses of protocol control block */ 332080b7f49SDag-Erling Smørgrav int aflag; /* show all sockets (including servers) */ 3336b463eedSChristian S.J. Peron int Bflag; /* show information about bpf consumers */ 334080b7f49SDag-Erling Smørgrav int bflag; /* show i/f total bytes in/out */ 335080b7f49SDag-Erling Smørgrav int dflag; /* show i/f dropped packets */ 336080b7f49SDag-Erling Smørgrav int gflag; /* show group (multicast) routing or stats */ 337c2dfd19fSGleb Smirnoff int hflag; /* show counters in human readable format */ 338080b7f49SDag-Erling Smørgrav int iflag; /* show interfaces */ 339080b7f49SDag-Erling Smørgrav int Lflag; /* show size of listen queues */ 340080b7f49SDag-Erling Smørgrav int mflag; /* show memory stats */ 341bf10ffe1SXin LI int noutputs = 0; /* how much outputs before we exit */ 34265ea0024SAssar Westerlund int numeric_addr; /* show addresses numerically */ 34365ea0024SAssar Westerlund int numeric_port; /* show ports numerically */ 344cf5e44f8SRuslan Ermilov static int pflag; /* show given protocol */ 3450153eb66SRobert Watson int Qflag; /* show netisr information */ 346080b7f49SDag-Erling Smørgrav int rflag; /* show routing tables (or routing stats) */ 347080b7f49SDag-Erling Smørgrav int sflag; /* show protocol statistics */ 348080b7f49SDag-Erling Smørgrav int Wflag; /* wide display */ 349f5d34df5SGeorge V. Neville-Neil int Tflag; /* TCP Information */ 35049f287f8SGeorge V. Neville-Neil int xflag; /* extra information, includes all socket buffer info */ 351c73d99b5SRuslan Ermilov int zflag; /* zero stats */ 352080b7f49SDag-Erling Smørgrav 353080b7f49SDag-Erling Smørgrav int interval; /* repeat interval for i/f stats */ 354080b7f49SDag-Erling Smørgrav 355080b7f49SDag-Erling Smørgrav char *interface; /* desired i/f for stats, or NULL for all i/fs */ 356080b7f49SDag-Erling Smørgrav int unit; /* unit number for above */ 357080b7f49SDag-Erling Smørgrav 358080b7f49SDag-Erling Smørgrav int af; /* address family */ 359feda1a43SJohn Baldwin int live; /* true if we are examining a live system */ 3609b50d902SRodney W. Grimes 3619b50d902SRodney W. Grimes int 362a01e3379SDavid Malone main(int argc, char *argv[]) 3639b50d902SRodney W. Grimes { 364a01e3379SDavid Malone struct protox *tp = NULL; /* for printing cblocks & stats */ 3659b50d902SRodney W. Grimes int ch; 366*3fddef95SHiroki Sato int fib = -1; 367*3fddef95SHiroki Sato char *endptr; 3689b50d902SRodney W. Grimes 3699b50d902SRodney W. Grimes af = AF_UNSPEC; 3709b50d902SRodney W. Grimes 371*3fddef95SHiroki Sato while ((ch = getopt(argc, argv, "AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz")) 3720153eb66SRobert Watson != -1) 3739b50d902SRodney W. Grimes switch(ch) { 3749b50d902SRodney W. Grimes case 'A': 3759b50d902SRodney W. Grimes Aflag = 1; 3769b50d902SRodney W. Grimes break; 3779b50d902SRodney W. Grimes case 'a': 3789b50d902SRodney W. Grimes aflag = 1; 3799b50d902SRodney W. Grimes break; 3806b463eedSChristian S.J. Peron case 'B': 3816b463eedSChristian S.J. Peron Bflag = 1; 3826b463eedSChristian S.J. Peron break; 383e1e293a5SDavid Greenman case 'b': 384e1e293a5SDavid Greenman bflag = 1; 385e1e293a5SDavid Greenman break; 3869b50d902SRodney W. Grimes case 'd': 3879b50d902SRodney W. Grimes dflag = 1; 3889b50d902SRodney W. Grimes break; 389*3fddef95SHiroki Sato case 'F': 390*3fddef95SHiroki Sato fib = strtol(optarg, &endptr, 0); 391*3fddef95SHiroki Sato if (*endptr != '\0' || 392*3fddef95SHiroki Sato (fib == 0 && (errno == EINVAL || errno == ERANGE))) 393*3fddef95SHiroki Sato errx(1, "%s: invalid fib", optarg); 394*3fddef95SHiroki Sato break; 3959b50d902SRodney W. Grimes case 'f': 396cbc17e71SGarrett Wollman if (strcmp(optarg, "ipx") == 0) 397cc6a66f2SJulian Elischer af = AF_IPX; 3989b50d902SRodney W. Grimes else if (strcmp(optarg, "inet") == 0) 3999b50d902SRodney W. Grimes af = AF_INET; 400cfa1ca9dSYoshinobu Inoue #ifdef INET6 401cfa1ca9dSYoshinobu Inoue else if (strcmp(optarg, "inet6") == 0) 402cfa1ca9dSYoshinobu Inoue af = AF_INET6; 403e5134d2eSMax Laier #endif 404e5134d2eSMax Laier #ifdef IPSEC 4053b8a8567SJun-ichiro itojun Hagino else if (strcmp(optarg, "pfkey") == 0) 4063b8a8567SJun-ichiro itojun Hagino af = PF_KEY; 407e5134d2eSMax Laier #endif 4089b50d902SRodney W. Grimes else if (strcmp(optarg, "unix") == 0) 4099b50d902SRodney W. Grimes af = AF_UNIX; 41063bf4575SJulian Elischer else if (strcmp(optarg, "atalk") == 0) 41163bf4575SJulian Elischer af = AF_APPLETALK; 412690f477dSSam Leffler #ifdef NETGRAPH 4134cf49a43SJulian Elischer else if (strcmp(optarg, "ng") == 0 4144cf49a43SJulian Elischer || strcmp(optarg, "netgraph") == 0) 4154cf49a43SJulian Elischer af = AF_NETGRAPH; 416690f477dSSam Leffler #endif 417d44ddba9SRuslan Ermilov else if (strcmp(optarg, "link") == 0) 418d44ddba9SRuslan Ermilov af = AF_LINK; 4199b50d902SRodney W. Grimes else { 42051e7d42cSGarrett Wollman errx(1, "%s: unknown address family", optarg); 4219b50d902SRodney W. Grimes } 4229b50d902SRodney W. Grimes break; 4239b50d902SRodney W. Grimes case 'g': 4249b50d902SRodney W. Grimes gflag = 1; 4259b50d902SRodney W. Grimes break; 426c2dfd19fSGleb Smirnoff case 'h': 427c2dfd19fSGleb Smirnoff hflag = 1; 428c2dfd19fSGleb Smirnoff break; 4299b50d902SRodney W. Grimes case 'I': { 4309b50d902SRodney W. Grimes char *cp; 4319b50d902SRodney W. Grimes 4329b50d902SRodney W. Grimes iflag = 1; 4339b50d902SRodney W. Grimes for (cp = interface = optarg; isalpha(*cp); cp++) 4349b50d902SRodney W. Grimes continue; 4359b50d902SRodney W. Grimes unit = atoi(cp); 4369b50d902SRodney W. Grimes break; 4379b50d902SRodney W. Grimes } 4389b50d902SRodney W. Grimes case 'i': 4399b50d902SRodney W. Grimes iflag = 1; 4409b50d902SRodney W. Grimes break; 441ac55add0SGuido van Rooij case 'L': 442ac55add0SGuido van Rooij Lflag = 1; 443ac55add0SGuido van Rooij break; 4449b50d902SRodney W. Grimes case 'M': 4459b50d902SRodney W. Grimes memf = optarg; 4469b50d902SRodney W. Grimes break; 4479b50d902SRodney W. Grimes case 'm': 4489b50d902SRodney W. Grimes mflag = 1; 4499b50d902SRodney W. Grimes break; 4509b50d902SRodney W. Grimes case 'N': 4519b50d902SRodney W. Grimes nlistf = optarg; 4529b50d902SRodney W. Grimes break; 4539b50d902SRodney W. Grimes case 'n': 45465ea0024SAssar Westerlund numeric_addr = numeric_port = 1; 4559b50d902SRodney W. Grimes break; 4569b50d902SRodney W. Grimes case 'p': 4579b50d902SRodney W. Grimes if ((tp = name2protox(optarg)) == NULL) { 45851e7d42cSGarrett Wollman errx(1, 45951e7d42cSGarrett Wollman "%s: unknown or uninstrumented protocol", 46051e7d42cSGarrett Wollman optarg); 4619b50d902SRodney W. Grimes } 4629b50d902SRodney W. Grimes pflag = 1; 4639b50d902SRodney W. Grimes break; 4640153eb66SRobert Watson case 'Q': 4650153eb66SRobert Watson Qflag = 1; 4660153eb66SRobert Watson break; 467bf10ffe1SXin LI case 'q': 468bf10ffe1SXin LI noutputs = atoi(optarg); 469bf10ffe1SXin LI if (noutputs != 0) 470bf10ffe1SXin LI noutputs++; 471bf10ffe1SXin LI break; 4729b50d902SRodney W. Grimes case 'r': 4739b50d902SRodney W. Grimes rflag = 1; 4749b50d902SRodney W. Grimes break; 4759b50d902SRodney W. Grimes case 's': 4769b50d902SRodney W. Grimes ++sflag; 4779b50d902SRodney W. Grimes break; 47865ea0024SAssar Westerlund case 'S': 47965ea0024SAssar Westerlund numeric_addr = 1; 48065ea0024SAssar Westerlund break; 4819b50d902SRodney W. Grimes case 'u': 4829b50d902SRodney W. Grimes af = AF_UNIX; 4839b50d902SRodney W. Grimes break; 484080b7f49SDag-Erling Smørgrav case 'W': 48570057abfSRuslan Ermilov case 'l': 486080b7f49SDag-Erling Smørgrav Wflag = 1; 487080b7f49SDag-Erling Smørgrav break; 4889b50d902SRodney W. Grimes case 'w': 4899b50d902SRodney W. Grimes interval = atoi(optarg); 4909b50d902SRodney W. Grimes iflag = 1; 4919b50d902SRodney W. Grimes break; 492f5d34df5SGeorge V. Neville-Neil case 'T': 493f5d34df5SGeorge V. Neville-Neil Tflag = 1; 494f5d34df5SGeorge V. Neville-Neil break; 49549f287f8SGeorge V. Neville-Neil case 'x': 49649f287f8SGeorge V. Neville-Neil xflag = 1; 49749f287f8SGeorge V. Neville-Neil break; 498c73d99b5SRuslan Ermilov case 'z': 499c73d99b5SRuslan Ermilov zflag = 1; 500c73d99b5SRuslan Ermilov break; 5019b50d902SRodney W. Grimes case '?': 5029b50d902SRodney W. Grimes default: 5039b50d902SRodney W. Grimes usage(); 5049b50d902SRodney W. Grimes } 5059b50d902SRodney W. Grimes argv += optind; 5069b50d902SRodney W. Grimes argc -= optind; 5079b50d902SRodney W. Grimes 5089b50d902SRodney W. Grimes #define BACKWARD_COMPATIBILITY 5099b50d902SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 5109b50d902SRodney W. Grimes if (*argv) { 5119b50d902SRodney W. Grimes if (isdigit(**argv)) { 5129b50d902SRodney W. Grimes interval = atoi(*argv); 5139b50d902SRodney W. Grimes if (interval <= 0) 5149b50d902SRodney W. Grimes usage(); 5159b50d902SRodney W. Grimes ++argv; 5169b50d902SRodney W. Grimes iflag = 1; 5179b50d902SRodney W. Grimes } 5189b50d902SRodney W. Grimes if (*argv) { 5199b50d902SRodney W. Grimes nlistf = *argv; 5209b50d902SRodney W. Grimes if (*++argv) 5219b50d902SRodney W. Grimes memf = *argv; 5229b50d902SRodney W. Grimes } 5239b50d902SRodney W. Grimes } 5249b50d902SRodney W. Grimes #endif 5259b50d902SRodney W. Grimes 5269b50d902SRodney W. Grimes /* 5279b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 5289b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 5299b50d902SRodney W. Grimes */ 530feda1a43SJohn Baldwin live = (nlistf == NULL && memf == NULL); 531feda1a43SJohn Baldwin if (!live) 5329b50d902SRodney W. Grimes setgid(getgid()); 5339b50d902SRodney W. Grimes 534f5d34df5SGeorge V. Neville-Neil if (xflag && Tflag) 535f5d34df5SGeorge V. Neville-Neil errx(1, "-x and -T are incompatible, pick one."); 536f5d34df5SGeorge V. Neville-Neil 5376b463eedSChristian S.J. Peron if (Bflag) { 538feda1a43SJohn Baldwin if (!live) 539feda1a43SJohn Baldwin usage(); 5406b463eedSChristian S.J. Peron bpf_stats(interface); 5416b463eedSChristian S.J. Peron exit(0); 5426b463eedSChristian S.J. Peron } 5439b50d902SRodney W. Grimes if (mflag) { 54483708764SRuslan Ermilov if (!live) { 545feda1a43SJohn Baldwin if (kread(0, NULL, 0) == 0) 546d4426f28SRobert Watson mbpr(kvmd, nl[N_MBSTAT].n_value); 547d15c5f56SRuslan Ermilov } else 548d4426f28SRobert Watson mbpr(NULL, 0); 5499b50d902SRodney W. Grimes exit(0); 5509b50d902SRodney W. Grimes } 5510153eb66SRobert Watson if (Qflag) { 55288737be2SRobert Watson if (!live) { 55388737be2SRobert Watson if (kread(0, NULL, 0) == 0) 55488737be2SRobert Watson netisr_stats(kvmd); 55588737be2SRobert Watson } else 55688737be2SRobert Watson netisr_stats(NULL); 5570153eb66SRobert Watson exit(0); 5580153eb66SRobert Watson } 559cc63cd56SPeter Wemm #if 0 5609b50d902SRodney W. Grimes /* 5619b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 5629b50d902SRodney W. Grimes * of open/close on each call to get* routines. 5639b50d902SRodney W. Grimes */ 5649b50d902SRodney W. Grimes sethostent(1); 5659b50d902SRodney W. Grimes setnetent(1); 566cc63cd56SPeter Wemm #else 567cc63cd56SPeter Wemm /* 568cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 569cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 570cc63cd56SPeter Wemm * used for the queries, which is slower. 571cc63cd56SPeter Wemm */ 572cc63cd56SPeter Wemm #endif 573feda1a43SJohn Baldwin kread(0, NULL, 0); 574cf5e44f8SRuslan Ermilov if (iflag && !sflag) { 575cfa1ca9dSYoshinobu Inoue intpr(interval, nl[N_IFNET].n_value, NULL); 5769b50d902SRodney W. Grimes exit(0); 5779b50d902SRodney W. Grimes } 5789b50d902SRodney W. Grimes if (rflag) { 5799b50d902SRodney W. Grimes if (sflag) 5801093cc31SRuslan Ermilov rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value); 5819b50d902SRodney W. Grimes else 582*3fddef95SHiroki Sato routepr(nl[N_RTREE].n_value, fib); 5839b50d902SRodney W. Grimes exit(0); 5849b50d902SRodney W. Grimes } 5859b50d902SRodney W. Grimes if (gflag) { 586cfa1ca9dSYoshinobu Inoue if (sflag) { 587cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 58867a451ccSBill Fenner mrt_stats(nl[N_MRTSTAT].n_value); 589cfa1ca9dSYoshinobu Inoue #ifdef INET6 590cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 591cfa1ca9dSYoshinobu Inoue mrt6_stats(nl[N_MRT6STAT].n_value); 592cfa1ca9dSYoshinobu Inoue #endif 593cfa1ca9dSYoshinobu Inoue } else { 594cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 595443fc317SBruce M Simpson mroutepr(nl[N_MFCHASHTBL].n_value, 596443fc317SBruce M Simpson nl[N_MFCTABLESIZE].n_value, 5979b50d902SRodney W. Grimes nl[N_VIFTABLE].n_value); 598cfa1ca9dSYoshinobu Inoue #ifdef INET6 599cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 600cfa1ca9dSYoshinobu Inoue mroute6pr(nl[N_MF6CTABLE].n_value, 601cfa1ca9dSYoshinobu Inoue nl[N_MIF6TABLE].n_value); 602cfa1ca9dSYoshinobu Inoue #endif 603cfa1ca9dSYoshinobu Inoue } 6049b50d902SRodney W. Grimes exit(0); 6059b50d902SRodney W. Grimes } 606cfa1ca9dSYoshinobu Inoue 607cf5e44f8SRuslan Ermilov if (tp) { 608cf5e44f8SRuslan Ermilov printproto(tp, tp->pr_name); 609cf5e44f8SRuslan Ermilov exit(0); 610cf5e44f8SRuslan Ermilov } 611cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 6129b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 613cfa1ca9dSYoshinobu Inoue printproto(tp, tp->pr_name); 614cfa1ca9dSYoshinobu Inoue #ifdef INET6 615cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 616cfa1ca9dSYoshinobu Inoue for (tp = ip6protox; tp->pr_name; tp++) 617cfa1ca9dSYoshinobu Inoue printproto(tp, tp->pr_name); 618cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 6193b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 6203b8a8567SJun-ichiro itojun Hagino if (af == PF_KEY || af == AF_UNSPEC) 6213b8a8567SJun-ichiro itojun Hagino for (tp = pfkeyprotox; tp->pr_name; tp++) 6223b8a8567SJun-ichiro itojun Hagino printproto(tp, tp->pr_name); 6233b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/ 6249cc22e5cSPoul-Henning Kamp #ifdef IPX 625e30e913cSJohn Hay if (af == AF_IPX || af == AF_UNSPEC) { 626cc6a66f2SJulian Elischer for (tp = ipxprotox; tp->pr_name; tp++) 627cc6a66f2SJulian Elischer printproto(tp, tp->pr_name); 628e30e913cSJohn Hay } 6299cc22e5cSPoul-Henning Kamp #endif /* IPX */ 63063bf4575SJulian Elischer if (af == AF_APPLETALK || af == AF_UNSPEC) 63163bf4575SJulian Elischer for (tp = atalkprotox; tp->pr_name; tp++) 63263bf4575SJulian Elischer printproto(tp, tp->pr_name); 633690f477dSSam Leffler #ifdef NETGRAPH 6344cf49a43SJulian Elischer if (af == AF_NETGRAPH || af == AF_UNSPEC) 6354cf49a43SJulian Elischer for (tp = netgraphprotox; tp->pr_name; tp++) 6364cf49a43SJulian Elischer printproto(tp, tp->pr_name); 637690f477dSSam Leffler #endif /* NETGRAPH */ 638f1c0a78dSMaxim Konovalov if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 639feda1a43SJohn Baldwin unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 640963b7ccdSRobert Watson nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 641963b7ccdSRobert Watson nl[N_UNP_SPHEAD].n_value); 6429b50d902SRodney W. Grimes exit(0); 6439b50d902SRodney W. Grimes } 6449b50d902SRodney W. Grimes 6459b50d902SRodney W. Grimes /* 6469b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 6479b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 6489b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 6499b50d902SRodney W. Grimes */ 6509b50d902SRodney W. Grimes static void 651321ae07fSPhilippe Charnier printproto(struct protox *tp, const char *name) 6529b50d902SRodney W. Grimes { 653feda1a43SJohn Baldwin void (*pr)(u_long, const char *, int, int); 6549b50d902SRodney W. Grimes u_long off; 6559b50d902SRodney W. Grimes 6569b50d902SRodney W. Grimes if (sflag) { 657cfa1ca9dSYoshinobu Inoue if (iflag) { 658cfa1ca9dSYoshinobu Inoue if (tp->pr_istats) 659cfa1ca9dSYoshinobu Inoue intpr(interval, nl[N_IFNET].n_value, 660cfa1ca9dSYoshinobu Inoue tp->pr_istats); 661cf5e44f8SRuslan Ermilov else if (pflag) 662cf5e44f8SRuslan Ermilov printf("%s: no per-interface stats routine\n", 663cf5e44f8SRuslan Ermilov tp->pr_name); 664cfa1ca9dSYoshinobu Inoue return; 665feda1a43SJohn Baldwin } else { 6669b50d902SRodney W. Grimes pr = tp->pr_stats; 667cf5e44f8SRuslan Ermilov if (!pr) { 668cf5e44f8SRuslan Ermilov if (pflag) 669cf5e44f8SRuslan Ermilov printf("%s: no stats routine\n", 670cf5e44f8SRuslan Ermilov tp->pr_name); 671cf5e44f8SRuslan Ermilov return; 672cf5e44f8SRuslan Ermilov } 673feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 674feda1a43SJohn Baldwin off = 0; 675feda1a43SJohn Baldwin else if (tp->pr_sindex < 0) { 676feda1a43SJohn Baldwin if (pflag) 677feda1a43SJohn Baldwin printf( 678feda1a43SJohn Baldwin "%s: stats routine doesn't work on cores\n", 679feda1a43SJohn Baldwin tp->pr_name); 680feda1a43SJohn Baldwin return; 681feda1a43SJohn Baldwin } else 682feda1a43SJohn Baldwin off = nl[tp->pr_sindex].n_value; 683cfa1ca9dSYoshinobu Inoue } 6849b50d902SRodney W. Grimes } else { 6859b50d902SRodney W. Grimes pr = tp->pr_cblocks; 686cf5e44f8SRuslan Ermilov if (!pr) { 687cf5e44f8SRuslan Ermilov if (pflag) 688cf5e44f8SRuslan Ermilov printf("%s: no PCB routine\n", tp->pr_name); 689cf5e44f8SRuslan Ermilov return; 690cf5e44f8SRuslan Ermilov } 691feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 692feda1a43SJohn Baldwin off = 0; 693feda1a43SJohn Baldwin else if (tp->pr_index < 0) { 694feda1a43SJohn Baldwin if (pflag) 695feda1a43SJohn Baldwin printf( 696feda1a43SJohn Baldwin "%s: PCB routine doesn't work on cores\n", 697feda1a43SJohn Baldwin tp->pr_name); 698feda1a43SJohn Baldwin return; 699feda1a43SJohn Baldwin } else 700feda1a43SJohn Baldwin off = nl[tp->pr_index].n_value; 7019b50d902SRodney W. Grimes } 702feda1a43SJohn Baldwin if (pr != NULL && (off || (live && tp->pr_usesysctl) || 703feda1a43SJohn Baldwin af != AF_UNSPEC)) 704feda1a43SJohn Baldwin (*pr)(off, name, af, tp->pr_protocol); 7059b50d902SRodney W. Grimes } 7069b50d902SRodney W. Grimes 70729dde48dSGleb Smirnoff static int 70829dde48dSGleb Smirnoff kvmd_init(void) 7099b50d902SRodney W. Grimes { 710feda1a43SJohn Baldwin char errbuf[_POSIX2_LINE_MAX]; 711feda1a43SJohn Baldwin 71229dde48dSGleb Smirnoff if (kvmd != NULL) 71329dde48dSGleb Smirnoff return (0); 71429dde48dSGleb Smirnoff 715feda1a43SJohn Baldwin kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 7163b7e5cccSRuslan Ermilov setgid(getgid()); 71729dde48dSGleb Smirnoff 71829dde48dSGleb Smirnoff if (kvmd == NULL) { 71929dde48dSGleb Smirnoff warnx("kvm not available: %s", errbuf); 72029dde48dSGleb Smirnoff return (-1); 72129dde48dSGleb Smirnoff } 72229dde48dSGleb Smirnoff 72399453c6aSPoul-Henning Kamp if (kvm_nlist(kvmd, nl) < 0) { 72499453c6aSPoul-Henning Kamp if (nlistf) 72599453c6aSPoul-Henning Kamp errx(1, "%s: kvm_nlist: %s", nlistf, 72699453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 72799453c6aSPoul-Henning Kamp else 72899453c6aSPoul-Henning Kamp errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 72999453c6aSPoul-Henning Kamp } 73099453c6aSPoul-Henning Kamp 73199453c6aSPoul-Henning Kamp if (nl[0].n_type == 0) { 73299453c6aSPoul-Henning Kamp if (nlistf) 73399453c6aSPoul-Henning Kamp errx(1, "%s: no namelist", nlistf); 73499453c6aSPoul-Henning Kamp else 73599453c6aSPoul-Henning Kamp errx(1, "no namelist"); 73699453c6aSPoul-Henning Kamp } 73729dde48dSGleb Smirnoff 73829dde48dSGleb Smirnoff return (0); 73929dde48dSGleb Smirnoff } 74029dde48dSGleb Smirnoff 74129dde48dSGleb Smirnoff /* 74229dde48dSGleb Smirnoff * Read kernel memory, return 0 on success. 74329dde48dSGleb Smirnoff */ 74429dde48dSGleb Smirnoff int 74529dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size) 74629dde48dSGleb Smirnoff { 74729dde48dSGleb Smirnoff 74829dde48dSGleb Smirnoff if (kvmd_init() < 0) 74999453c6aSPoul-Henning Kamp return (-1); 75029dde48dSGleb Smirnoff 751c6620a13SPoul-Henning Kamp if (!buf) 752c6620a13SPoul-Henning Kamp return (0); 753feda1a43SJohn Baldwin if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 7543aa80b1dSDavid Greenman warnx("%s", kvm_geterr(kvmd)); 7559b50d902SRodney W. Grimes return (-1); 7569b50d902SRodney W. Grimes } 7579b50d902SRodney W. Grimes return (0); 7589b50d902SRodney W. Grimes } 7599b50d902SRodney W. Grimes 76029dde48dSGleb Smirnoff /* 76129dde48dSGleb Smirnoff * Read an array of N counters in kernel memory into array of N uint64_t's. 76229dde48dSGleb Smirnoff */ 76329dde48dSGleb Smirnoff int 7645da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size) 76529dde48dSGleb Smirnoff { 7665da0521fSAndrey V. Elsukov uint64_t *c = buf; 76729dde48dSGleb Smirnoff 76829dde48dSGleb Smirnoff if (kvmd_init() < 0) 76929dde48dSGleb Smirnoff return (-1); 77029dde48dSGleb Smirnoff 7715da0521fSAndrey V. Elsukov if (kread(addr, buf, size) < 0) 7725da0521fSAndrey V. Elsukov return (-1); 77329dde48dSGleb Smirnoff 7745da0521fSAndrey V. Elsukov while (size != 0) { 7755da0521fSAndrey V. Elsukov *c = kvm_counter_u64_fetch(kvmd, *c); 7765da0521fSAndrey V. Elsukov size -= sizeof(*c); 7775da0521fSAndrey V. Elsukov c++; 7785da0521fSAndrey V. Elsukov } 77929dde48dSGleb Smirnoff return (0); 78029dde48dSGleb Smirnoff } 78129dde48dSGleb Smirnoff 782a01e3379SDavid Malone const char * 7837b95a1ebSYaroslav Tykhiy plural(uintmax_t n) 7849b50d902SRodney W. Grimes { 7859b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 7869b50d902SRodney W. Grimes } 7879b50d902SRodney W. Grimes 788a01e3379SDavid Malone const char * 7897b95a1ebSYaroslav Tykhiy plurales(uintmax_t n) 7909b50d902SRodney W. Grimes { 7919b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 7929b50d902SRodney W. Grimes } 7939b50d902SRodney W. Grimes 794f99a4046SMike Makonnen const char * 7957b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n) 796f99a4046SMike Makonnen { 797f99a4046SMike Makonnen return (n != 1 ? "ies" : "y"); 798f99a4046SMike Makonnen } 799f99a4046SMike Makonnen 8009b50d902SRodney W. Grimes /* 8019b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 8029b50d902SRodney W. Grimes */ 8039b50d902SRodney W. Grimes static struct protox * 804096146f8SYaroslav Tykhiy knownname(const char *name) 8059b50d902SRodney W. Grimes { 8069b50d902SRodney W. Grimes struct protox **tpp, *tp; 8079b50d902SRodney W. Grimes 8089b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 8099b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 8109b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 8119b50d902SRodney W. Grimes return (tp); 8129b50d902SRodney W. Grimes return (NULL); 8139b50d902SRodney W. Grimes } 8149b50d902SRodney W. Grimes 8159b50d902SRodney W. Grimes /* 8169b50d902SRodney W. Grimes * Find the protox corresponding to name. 8179b50d902SRodney W. Grimes */ 8189b50d902SRodney W. Grimes static struct protox * 819096146f8SYaroslav Tykhiy name2protox(const char *name) 8209b50d902SRodney W. Grimes { 8219b50d902SRodney W. Grimes struct protox *tp; 8229b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 8239b50d902SRodney W. Grimes struct protoent *p; 8249b50d902SRodney W. Grimes 8259b50d902SRodney W. Grimes /* 8269b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 8279b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 8289b50d902SRodney W. Grimes */ 829cfa1ca9dSYoshinobu Inoue if ((tp = knownname(name)) != NULL) 8309b50d902SRodney W. Grimes return (tp); 8319b50d902SRodney W. Grimes 8329b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 833cfa1ca9dSYoshinobu Inoue while ((p = getprotoent()) != NULL) { 8349b50d902SRodney W. Grimes /* assert: name not same as p->name */ 8359b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 8369b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 8379b50d902SRodney W. Grimes endprotoent(); 8389b50d902SRodney W. Grimes return (knownname(p->p_name)); 8399b50d902SRodney W. Grimes } 8409b50d902SRodney W. Grimes } 8419b50d902SRodney W. Grimes endprotoent(); 8429b50d902SRodney W. Grimes return (NULL); 8439b50d902SRodney W. Grimes } 8449b50d902SRodney W. Grimes 8459b50d902SRodney W. Grimes static void 8465e051718SAssar Westerlund usage(void) 8479b50d902SRodney W. Grimes { 8480153eb66SRobert Watson (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 849f5d34df5SGeorge V. Neville-Neil "usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n" 8501cb467b1SRuslan Ermilov " [-M core] [-N system]", 8513ee7e0d4SJohn Baldwin " netstat -i | -I interface [-abdhnW] [-f address_family]\n" 852d44ddba9SRuslan Ermilov " [-M core] [-N system]", 853bf10ffe1SXin LI " netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]", 85482d383bcSRuslan Ermilov " netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n" 85582d383bcSRuslan Ermilov " [-M core] [-N system]", 8561cb467b1SRuslan Ermilov " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n" 8571cb467b1SRuslan Ermilov " [-M core] [-N system]", 85849c2dc64SMaxim Konovalov " netstat -m [-M core] [-N system]", 8595dbd2da1SChristian S.J. Peron " netstat -B [-I interface]", 86082d383bcSRuslan Ermilov " netstat -r [-AanW] [-f address_family] [-M core] [-N system]", 8611cb467b1SRuslan Ermilov " netstat -rs [-s] [-M core] [-N system]", 8621cb467b1SRuslan Ermilov " netstat -g [-W] [-f address_family] [-M core] [-N system]", 8630153eb66SRobert Watson " netstat -gs [-s] [-f address_family] [-M core] [-N system]", 8640153eb66SRobert Watson " netstat -Q"); 8659b50d902SRodney W. Grimes exit(1); 8669b50d902SRodney W. Grimes } 867