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[] = { 732c284d93SGleb Smirnoff #define N_RTSTAT 0 74096146f8SYaroslav Tykhiy { .n_name = "_rtstat" }, 752c284d93SGleb Smirnoff #define N_RTREE 1 76096146f8SYaroslav Tykhiy { .n_name = "_rt_tables"}, 772c284d93SGleb Smirnoff #define N_MRTSTAT 2 78096146f8SYaroslav Tykhiy { .n_name = "_mrtstat" }, 792c284d93SGleb Smirnoff #define N_MFCHASHTBL 3 80443fc317SBruce M Simpson { .n_name = "_mfchashtbl" }, 812c284d93SGleb Smirnoff #define N_VIFTABLE 4 82096146f8SYaroslav Tykhiy { .n_name = "_viftable" }, 83*45c203fcSGleb Smirnoff #define N_NGSOCKS 5 84096146f8SYaroslav Tykhiy { .n_name = "_ngsocklist"}, 85*45c203fcSGleb Smirnoff #define N_IP6STAT 6 86096146f8SYaroslav Tykhiy { .n_name = "_ip6stat" }, 87*45c203fcSGleb Smirnoff #define N_ICMP6STAT 7 88096146f8SYaroslav Tykhiy { .n_name = "_icmp6stat" }, 89*45c203fcSGleb Smirnoff #define N_IPSECSTAT 8 908409aedfSGeorge V. Neville-Neil { .n_name = "_ipsec4stat" }, 91*45c203fcSGleb Smirnoff #define N_IPSEC6STAT 9 92096146f8SYaroslav Tykhiy { .n_name = "_ipsec6stat" }, 93*45c203fcSGleb Smirnoff #define N_PIM6STAT 10 94096146f8SYaroslav Tykhiy { .n_name = "_pim6stat" }, 95*45c203fcSGleb Smirnoff #define N_MRT6STAT 11 96096146f8SYaroslav Tykhiy { .n_name = "_mrt6stat" }, 97*45c203fcSGleb Smirnoff #define N_MF6CTABLE 12 98096146f8SYaroslav Tykhiy { .n_name = "_mf6ctable" }, 99*45c203fcSGleb Smirnoff #define N_MIF6TABLE 13 100096146f8SYaroslav Tykhiy { .n_name = "_mif6table" }, 101*45c203fcSGleb Smirnoff #define N_PFKEYSTAT 14 102096146f8SYaroslav Tykhiy { .n_name = "_pfkeystat" }, 103*45c203fcSGleb Smirnoff #define N_RTTRASH 15 104096146f8SYaroslav Tykhiy { .n_name = "_rttrash" }, 105*45c203fcSGleb Smirnoff #define N_CARPSTAT 16 106096146f8SYaroslav Tykhiy { .n_name = "_carpstats" }, 107*45c203fcSGleb Smirnoff #define N_PFSYNCSTAT 17 108096146f8SYaroslav Tykhiy { .n_name = "_pfsyncstats" }, 109*45c203fcSGleb Smirnoff #define N_AHSTAT 18 110096146f8SYaroslav Tykhiy { .n_name = "_ahstat" }, 111*45c203fcSGleb Smirnoff #define N_ESPSTAT 19 112096146f8SYaroslav Tykhiy { .n_name = "_espstat" }, 113*45c203fcSGleb Smirnoff #define N_IPCOMPSTAT 20 114096146f8SYaroslav Tykhiy { .n_name = "_ipcompstat" }, 115*45c203fcSGleb Smirnoff #define N_TCPSTAT 21 1165da0521fSAndrey V. Elsukov { .n_name = "_tcpstat" }, 117*45c203fcSGleb Smirnoff #define N_UDPSTAT 22 118feda1a43SJohn Baldwin { .n_name = "_udpstat" }, 119*45c203fcSGleb Smirnoff #define N_IPSTAT 23 1205da0521fSAndrey V. Elsukov { .n_name = "_ipstat" }, 121*45c203fcSGleb Smirnoff #define N_ICMPSTAT 24 122feda1a43SJohn Baldwin { .n_name = "_icmpstat" }, 123*45c203fcSGleb Smirnoff #define N_IGMPSTAT 25 124feda1a43SJohn Baldwin { .n_name = "_igmpstat" }, 125*45c203fcSGleb Smirnoff #define N_PIMSTAT 26 126feda1a43SJohn Baldwin { .n_name = "_pimstat" }, 127*45c203fcSGleb Smirnoff #define N_TCBINFO 27 128feda1a43SJohn Baldwin { .n_name = "_tcbinfo" }, 129*45c203fcSGleb Smirnoff #define N_UDBINFO 28 130feda1a43SJohn Baldwin { .n_name = "_udbinfo" }, 131*45c203fcSGleb Smirnoff #define N_DIVCBINFO 29 132feda1a43SJohn Baldwin { .n_name = "_divcbinfo" }, 133*45c203fcSGleb Smirnoff #define N_RIPCBINFO 30 134feda1a43SJohn Baldwin { .n_name = "_ripcbinfo" }, 135*45c203fcSGleb Smirnoff #define N_UNP_COUNT 31 136feda1a43SJohn Baldwin { .n_name = "_unp_count" }, 137*45c203fcSGleb Smirnoff #define N_UNP_GENCNT 32 138feda1a43SJohn Baldwin { .n_name = "_unp_gencnt" }, 139*45c203fcSGleb Smirnoff #define N_UNP_DHEAD 33 140feda1a43SJohn Baldwin { .n_name = "_unp_dhead" }, 141*45c203fcSGleb Smirnoff #define N_UNP_SHEAD 34 142feda1a43SJohn Baldwin { .n_name = "_unp_shead" }, 143*45c203fcSGleb Smirnoff #define N_RIP6STAT 36 144feda1a43SJohn Baldwin { .n_name = "_rip6stat" }, 145*45c203fcSGleb Smirnoff #define N_SCTPSTAT 36 146feda1a43SJohn Baldwin { .n_name = "_sctpstat" }, 147*45c203fcSGleb Smirnoff #define N_MFCTABLESIZE 37 148443fc317SBruce M Simpson { .n_name = "_mfctablesize" }, 149*45c203fcSGleb Smirnoff #define N_ARPSTAT 38 15054fc657dSGeorge V. Neville-Neil { .n_name = "_arpstat" }, 151*45c203fcSGleb Smirnoff #define N_UNP_SPHEAD 39 152963b7ccdSRobert Watson { .n_name = "unp_sphead" }, 153*45c203fcSGleb Smirnoff #define N_SFSTAT 40 15405d1f5bcSAndrey V. Elsukov { .n_name = "_sfstat"}, 155096146f8SYaroslav Tykhiy { .n_name = NULL }, 1569b50d902SRodney W. Grimes }; 1579b50d902SRodney W. Grimes 1589b50d902SRodney W. Grimes struct protox { 159feda1a43SJohn Baldwin int pr_index; /* index into nlist of cb head */ 160feda1a43SJohn Baldwin int pr_sindex; /* index into nlist of stat block */ 1619b50d902SRodney W. Grimes u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 162feda1a43SJohn Baldwin void (*pr_cblocks)(u_long, const char *, int, int); 1635e051718SAssar Westerlund /* control blocks printing routine */ 164feda1a43SJohn Baldwin void (*pr_stats)(u_long, const char *, int, int); 1655e051718SAssar Westerlund /* statistics printing routine */ 1665e051718SAssar Westerlund void (*pr_istats)(char *); /* per/if statistics printing routine */ 167fa6d48c0SMark Murray const char *pr_name; /* well-known name */ 16855fd53e2SJohn Baldwin int pr_usesysctl; /* non-zero if we use sysctl, not kvm */ 169feda1a43SJohn Baldwin int pr_protocol; 1709b50d902SRodney W. Grimes } protox[] = { 171feda1a43SJohn Baldwin { N_TCBINFO, N_TCPSTAT, 1, protopr, 172feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 173feda1a43SJohn Baldwin { N_UDBINFO, N_UDPSTAT, 1, protopr, 174feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 17574fd40c9SRandall Stewart #ifdef SCTP 176feda1a43SJohn Baldwin { -1, N_SCTPSTAT, 1, sctp_protopr, 177feda1a43SJohn Baldwin sctp_stats, NULL, "sctp", 1, IPPROTO_SCTP }, 17874fd40c9SRandall Stewart #endif 179aa0a1e58SJeff Roberson #ifdef SDP 180aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 181aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 182aa0a1e58SJeff Roberson #endif 183feda1a43SJohn Baldwin { N_DIVCBINFO, -1, 1, protopr, 184feda1a43SJohn Baldwin NULL, NULL, "divert", 1, IPPROTO_DIVERT }, 185feda1a43SJohn Baldwin { N_RIPCBINFO, N_IPSTAT, 1, protopr, 186feda1a43SJohn Baldwin ip_stats, NULL, "ip", 1, IPPROTO_RAW }, 187feda1a43SJohn Baldwin { N_RIPCBINFO, N_ICMPSTAT, 1, protopr, 188feda1a43SJohn Baldwin icmp_stats, NULL, "icmp", 1, IPPROTO_ICMP }, 189feda1a43SJohn Baldwin { N_RIPCBINFO, N_IGMPSTAT, 1, protopr, 190feda1a43SJohn Baldwin igmp_stats, NULL, "igmp", 1, IPPROTO_IGMP }, 191cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 1928409aedfSGeorge V. Neville-Neil { -1, N_IPSECSTAT, 1, NULL, /* keep as compat */ 193feda1a43SJohn Baldwin ipsec_stats, NULL, "ipsec", 0, 0}, 1948409aedfSGeorge V. Neville-Neil { -1, N_AHSTAT, 1, NULL, 195feda1a43SJohn Baldwin ah_stats, NULL, "ah", 0, 0}, 1968409aedfSGeorge V. Neville-Neil { -1, N_ESPSTAT, 1, NULL, 197feda1a43SJohn Baldwin esp_stats, NULL, "esp", 0, 0}, 1988409aedfSGeorge V. Neville-Neil { -1, N_IPCOMPSTAT, 1, NULL, 199feda1a43SJohn Baldwin ipcomp_stats, NULL, "ipcomp", 0, 0}, 200100b98dbSKelly Yancey #endif 201feda1a43SJohn Baldwin { N_RIPCBINFO, N_PIMSTAT, 1, protopr, 202feda1a43SJohn Baldwin pim_stats, NULL, "pim", 1, IPPROTO_PIM }, 203feda1a43SJohn Baldwin { -1, N_CARPSTAT, 1, NULL, 204feda1a43SJohn Baldwin carp_stats, NULL, "carp", 1, 0 }, 2053e4d5cd3SGleb Smirnoff #ifdef PF 206feda1a43SJohn Baldwin { -1, N_PFSYNCSTAT, 1, NULL, 207feda1a43SJohn Baldwin pfsync_stats, NULL, "pfsync", 1, 0 }, 2083e4d5cd3SGleb Smirnoff #endif 20954fc657dSGeorge V. Neville-Neil { -1, N_ARPSTAT, 1, NULL, 21054fc657dSGeorge V. Neville-Neil arp_stats, NULL, "arp", 1, 0 }, 2116bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 212feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 2139b50d902SRodney W. Grimes }; 2149b50d902SRodney W. Grimes 215cfa1ca9dSYoshinobu Inoue #ifdef INET6 216cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = { 217feda1a43SJohn Baldwin { N_TCBINFO, N_TCPSTAT, 1, protopr, 218feda1a43SJohn Baldwin tcp_stats, NULL, "tcp", 1, IPPROTO_TCP }, 219feda1a43SJohn Baldwin { N_UDBINFO, N_UDPSTAT, 1, protopr, 220feda1a43SJohn Baldwin udp_stats, NULL, "udp", 1, IPPROTO_UDP }, 221feda1a43SJohn Baldwin { N_RIPCBINFO, N_IP6STAT, 1, protopr, 222feda1a43SJohn Baldwin ip6_stats, ip6_ifstats, "ip6", 1, IPPROTO_RAW }, 223feda1a43SJohn Baldwin { N_RIPCBINFO, N_ICMP6STAT, 1, protopr, 224feda1a43SJohn Baldwin icmp6_stats, icmp6_ifstats, "icmp6", 1, IPPROTO_ICMPV6 }, 225aa0a1e58SJeff Roberson #ifdef SDP 226aa0a1e58SJeff Roberson { -1, -1, 1, protopr, 227aa0a1e58SJeff Roberson NULL, NULL, "sdp", 1, IPPROTO_TCP }, 228aa0a1e58SJeff Roberson #endif 229cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 2306bb3f207SRuslan Ermilov { -1, N_IPSEC6STAT, 1, NULL, 231feda1a43SJohn Baldwin ipsec_stats, NULL, "ipsec6", 0, 0 }, 232cfa1ca9dSYoshinobu Inoue #endif 233cfa1ca9dSYoshinobu Inoue #ifdef notyet 2346bb3f207SRuslan Ermilov { -1, N_PIM6STAT, 1, NULL, 235feda1a43SJohn Baldwin pim6_stats, NULL, "pim6", 1, 0 }, 236cfa1ca9dSYoshinobu Inoue #endif 237feda1a43SJohn Baldwin { -1, N_RIP6STAT, 1, NULL, 238feda1a43SJohn Baldwin rip6_stats, NULL, "rip6", 1, 0 }, 2396bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 240feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 241cfa1ca9dSYoshinobu Inoue }; 242cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 243cfa1ca9dSYoshinobu Inoue 2443b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 2453b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = { 2466bb3f207SRuslan Ermilov { -1, N_PFKEYSTAT, 1, NULL, 247feda1a43SJohn Baldwin pfkey_stats, NULL, "pfkey", 0, 0 }, 2486bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 249feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 2503b8a8567SJun-ichiro itojun Hagino }; 2513b8a8567SJun-ichiro itojun Hagino #endif 2523b8a8567SJun-ichiro itojun Hagino 253690f477dSSam Leffler #ifdef NETGRAPH 2544cf49a43SJulian Elischer struct protox netgraphprotox[] = { 2554cf49a43SJulian Elischer { N_NGSOCKS, -1, 1, netgraphprotopr, 256feda1a43SJohn Baldwin NULL, NULL, "ctrl", 0, 0 }, 2574cf49a43SJulian Elischer { N_NGSOCKS, -1, 1, netgraphprotopr, 258feda1a43SJohn Baldwin NULL, NULL, "data", 0, 0 }, 2596bb3f207SRuslan Ermilov { -1, -1, 0, NULL, 260feda1a43SJohn Baldwin NULL, NULL, NULL, 0, 0 } 2614cf49a43SJulian Elischer }; 262690f477dSSam Leffler #endif 263cc6a66f2SJulian Elischer 264cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = { 265cfa1ca9dSYoshinobu Inoue protox, 266cfa1ca9dSYoshinobu Inoue #ifdef INET6 267cfa1ca9dSYoshinobu Inoue ip6protox, 268cfa1ca9dSYoshinobu Inoue #endif 2693b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 2703b8a8567SJun-ichiro itojun Hagino pfkeyprotox, 2713b8a8567SJun-ichiro itojun Hagino #endif 272*45c203fcSGleb Smirnoff NULL }; 2739b50d902SRodney W. Grimes 274fa6d48c0SMark Murray static void printproto(struct protox *, const char *); 2755e051718SAssar Westerlund static void usage(void); 276096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *); 277096146f8SYaroslav Tykhiy static struct protox *knownname(const char *); 2789b50d902SRodney W. Grimes 279c6620a13SPoul-Henning Kamp static kvm_t *kvmd; 280080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL; 281080b7f49SDag-Erling Smørgrav 282080b7f49SDag-Erling Smørgrav int Aflag; /* show addresses of protocol control block */ 283080b7f49SDag-Erling Smørgrav int aflag; /* show all sockets (including servers) */ 2846b463eedSChristian S.J. Peron int Bflag; /* show information about bpf consumers */ 285080b7f49SDag-Erling Smørgrav int bflag; /* show i/f total bytes in/out */ 286080b7f49SDag-Erling Smørgrav int dflag; /* show i/f dropped packets */ 287080b7f49SDag-Erling Smørgrav int gflag; /* show group (multicast) routing or stats */ 288c2dfd19fSGleb Smirnoff int hflag; /* show counters in human readable format */ 289080b7f49SDag-Erling Smørgrav int iflag; /* show interfaces */ 290080b7f49SDag-Erling Smørgrav int Lflag; /* show size of listen queues */ 291080b7f49SDag-Erling Smørgrav int mflag; /* show memory stats */ 292bf10ffe1SXin LI int noutputs = 0; /* how much outputs before we exit */ 29365ea0024SAssar Westerlund int numeric_addr; /* show addresses numerically */ 29465ea0024SAssar Westerlund int numeric_port; /* show ports numerically */ 295cf5e44f8SRuslan Ermilov static int pflag; /* show given protocol */ 2960153eb66SRobert Watson int Qflag; /* show netisr information */ 297080b7f49SDag-Erling Smørgrav int rflag; /* show routing tables (or routing stats) */ 298080b7f49SDag-Erling Smørgrav int sflag; /* show protocol statistics */ 299080b7f49SDag-Erling Smørgrav int Wflag; /* wide display */ 300f5d34df5SGeorge V. Neville-Neil int Tflag; /* TCP Information */ 30149f287f8SGeorge V. Neville-Neil int xflag; /* extra information, includes all socket buffer info */ 302c73d99b5SRuslan Ermilov int zflag; /* zero stats */ 303080b7f49SDag-Erling Smørgrav 304080b7f49SDag-Erling Smørgrav int interval; /* repeat interval for i/f stats */ 305080b7f49SDag-Erling Smørgrav 306080b7f49SDag-Erling Smørgrav char *interface; /* desired i/f for stats, or NULL for all i/fs */ 307080b7f49SDag-Erling Smørgrav int unit; /* unit number for above */ 308080b7f49SDag-Erling Smørgrav 309080b7f49SDag-Erling Smørgrav int af; /* address family */ 310feda1a43SJohn Baldwin int live; /* true if we are examining a live system */ 3119b50d902SRodney W. Grimes 3129b50d902SRodney W. Grimes int 313a01e3379SDavid Malone main(int argc, char *argv[]) 3149b50d902SRodney W. Grimes { 315a01e3379SDavid Malone struct protox *tp = NULL; /* for printing cblocks & stats */ 3169b50d902SRodney W. Grimes int ch; 3173fddef95SHiroki Sato int fib = -1; 3183fddef95SHiroki Sato char *endptr; 3199b50d902SRodney W. Grimes 3209b50d902SRodney W. Grimes af = AF_UNSPEC; 3219b50d902SRodney W. Grimes 32217ed2e8eSAlexander V. Chernikov while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz")) 3230153eb66SRobert Watson != -1) 3249b50d902SRodney W. Grimes switch(ch) { 32517ed2e8eSAlexander V. Chernikov case '4': 32617ed2e8eSAlexander V. Chernikov #ifdef INET 32717ed2e8eSAlexander V. Chernikov af = AF_INET; 32817ed2e8eSAlexander V. Chernikov #else 32917ed2e8eSAlexander V. Chernikov errx(1, "IPv4 support is not compiled in"); 33017ed2e8eSAlexander V. Chernikov #endif 33117ed2e8eSAlexander V. Chernikov break; 33217ed2e8eSAlexander V. Chernikov case '6': 33317ed2e8eSAlexander V. Chernikov #ifdef INET6 33417ed2e8eSAlexander V. Chernikov af = AF_INET6; 33517ed2e8eSAlexander V. Chernikov #else 33617ed2e8eSAlexander V. Chernikov errx(1, "IPv6 support is not compiled in"); 33717ed2e8eSAlexander V. Chernikov #endif 33817ed2e8eSAlexander V. Chernikov break; 3399b50d902SRodney W. Grimes case 'A': 3409b50d902SRodney W. Grimes Aflag = 1; 3419b50d902SRodney W. Grimes break; 3429b50d902SRodney W. Grimes case 'a': 3439b50d902SRodney W. Grimes aflag = 1; 3449b50d902SRodney W. Grimes break; 3456b463eedSChristian S.J. Peron case 'B': 3466b463eedSChristian S.J. Peron Bflag = 1; 3476b463eedSChristian S.J. Peron break; 348e1e293a5SDavid Greenman case 'b': 349e1e293a5SDavid Greenman bflag = 1; 350e1e293a5SDavid Greenman break; 3519b50d902SRodney W. Grimes case 'd': 3529b50d902SRodney W. Grimes dflag = 1; 3539b50d902SRodney W. Grimes break; 3543fddef95SHiroki Sato case 'F': 3553fddef95SHiroki Sato fib = strtol(optarg, &endptr, 0); 3563fddef95SHiroki Sato if (*endptr != '\0' || 3573fddef95SHiroki Sato (fib == 0 && (errno == EINVAL || errno == ERANGE))) 3583fddef95SHiroki Sato errx(1, "%s: invalid fib", optarg); 3593fddef95SHiroki Sato break; 3609b50d902SRodney W. Grimes case 'f': 3612c284d93SGleb Smirnoff if (strcmp(optarg, "inet") == 0) 3629b50d902SRodney W. Grimes af = AF_INET; 363cfa1ca9dSYoshinobu Inoue #ifdef INET6 364cfa1ca9dSYoshinobu Inoue else if (strcmp(optarg, "inet6") == 0) 365cfa1ca9dSYoshinobu Inoue af = AF_INET6; 366e5134d2eSMax Laier #endif 367e5134d2eSMax Laier #ifdef IPSEC 3683b8a8567SJun-ichiro itojun Hagino else if (strcmp(optarg, "pfkey") == 0) 3693b8a8567SJun-ichiro itojun Hagino af = PF_KEY; 370e5134d2eSMax Laier #endif 3719b50d902SRodney W. Grimes else if (strcmp(optarg, "unix") == 0) 3729b50d902SRodney W. Grimes af = AF_UNIX; 373690f477dSSam Leffler #ifdef NETGRAPH 3744cf49a43SJulian Elischer else if (strcmp(optarg, "ng") == 0 3754cf49a43SJulian Elischer || strcmp(optarg, "netgraph") == 0) 3764cf49a43SJulian Elischer af = AF_NETGRAPH; 377690f477dSSam Leffler #endif 378d44ddba9SRuslan Ermilov else if (strcmp(optarg, "link") == 0) 379d44ddba9SRuslan Ermilov af = AF_LINK; 3809b50d902SRodney W. Grimes else { 38151e7d42cSGarrett Wollman errx(1, "%s: unknown address family", optarg); 3829b50d902SRodney W. Grimes } 3839b50d902SRodney W. Grimes break; 3849b50d902SRodney W. Grimes case 'g': 3859b50d902SRodney W. Grimes gflag = 1; 3869b50d902SRodney W. Grimes break; 387c2dfd19fSGleb Smirnoff case 'h': 388c2dfd19fSGleb Smirnoff hflag = 1; 389c2dfd19fSGleb Smirnoff break; 3909b50d902SRodney W. Grimes case 'I': { 3919b50d902SRodney W. Grimes char *cp; 3929b50d902SRodney W. Grimes 3939b50d902SRodney W. Grimes iflag = 1; 3949b50d902SRodney W. Grimes for (cp = interface = optarg; isalpha(*cp); cp++) 3959b50d902SRodney W. Grimes continue; 3969b50d902SRodney W. Grimes unit = atoi(cp); 3979b50d902SRodney W. Grimes break; 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes case 'i': 4009b50d902SRodney W. Grimes iflag = 1; 4019b50d902SRodney W. Grimes break; 402ac55add0SGuido van Rooij case 'L': 403ac55add0SGuido van Rooij Lflag = 1; 404ac55add0SGuido van Rooij break; 4059b50d902SRodney W. Grimes case 'M': 4069b50d902SRodney W. Grimes memf = optarg; 4079b50d902SRodney W. Grimes break; 4089b50d902SRodney W. Grimes case 'm': 4099b50d902SRodney W. Grimes mflag = 1; 4109b50d902SRodney W. Grimes break; 4119b50d902SRodney W. Grimes case 'N': 4129b50d902SRodney W. Grimes nlistf = optarg; 4139b50d902SRodney W. Grimes break; 4149b50d902SRodney W. Grimes case 'n': 41565ea0024SAssar Westerlund numeric_addr = numeric_port = 1; 4169b50d902SRodney W. Grimes break; 4179b50d902SRodney W. Grimes case 'p': 4189b50d902SRodney W. Grimes if ((tp = name2protox(optarg)) == NULL) { 41951e7d42cSGarrett Wollman errx(1, 42051e7d42cSGarrett Wollman "%s: unknown or uninstrumented protocol", 42151e7d42cSGarrett Wollman optarg); 4229b50d902SRodney W. Grimes } 4239b50d902SRodney W. Grimes pflag = 1; 4249b50d902SRodney W. Grimes break; 4250153eb66SRobert Watson case 'Q': 4260153eb66SRobert Watson Qflag = 1; 4270153eb66SRobert Watson break; 428bf10ffe1SXin LI case 'q': 429bf10ffe1SXin LI noutputs = atoi(optarg); 430bf10ffe1SXin LI if (noutputs != 0) 431bf10ffe1SXin LI noutputs++; 432bf10ffe1SXin LI break; 4339b50d902SRodney W. Grimes case 'r': 4349b50d902SRodney W. Grimes rflag = 1; 4359b50d902SRodney W. Grimes break; 4369b50d902SRodney W. Grimes case 's': 4379b50d902SRodney W. Grimes ++sflag; 4389b50d902SRodney W. Grimes break; 43965ea0024SAssar Westerlund case 'S': 44065ea0024SAssar Westerlund numeric_addr = 1; 44165ea0024SAssar Westerlund break; 4429b50d902SRodney W. Grimes case 'u': 4439b50d902SRodney W. Grimes af = AF_UNIX; 4449b50d902SRodney W. Grimes break; 445080b7f49SDag-Erling Smørgrav case 'W': 44670057abfSRuslan Ermilov case 'l': 447080b7f49SDag-Erling Smørgrav Wflag = 1; 448080b7f49SDag-Erling Smørgrav break; 4499b50d902SRodney W. Grimes case 'w': 4509b50d902SRodney W. Grimes interval = atoi(optarg); 4519b50d902SRodney W. Grimes iflag = 1; 4529b50d902SRodney W. Grimes break; 453f5d34df5SGeorge V. Neville-Neil case 'T': 454f5d34df5SGeorge V. Neville-Neil Tflag = 1; 455f5d34df5SGeorge V. Neville-Neil break; 45649f287f8SGeorge V. Neville-Neil case 'x': 45749f287f8SGeorge V. Neville-Neil xflag = 1; 45849f287f8SGeorge V. Neville-Neil break; 459c73d99b5SRuslan Ermilov case 'z': 460c73d99b5SRuslan Ermilov zflag = 1; 461c73d99b5SRuslan Ermilov break; 4629b50d902SRodney W. Grimes case '?': 4639b50d902SRodney W. Grimes default: 4649b50d902SRodney W. Grimes usage(); 4659b50d902SRodney W. Grimes } 4669b50d902SRodney W. Grimes argv += optind; 4679b50d902SRodney W. Grimes argc -= optind; 4689b50d902SRodney W. Grimes 4699b50d902SRodney W. Grimes #define BACKWARD_COMPATIBILITY 4709b50d902SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 4719b50d902SRodney W. Grimes if (*argv) { 4729b50d902SRodney W. Grimes if (isdigit(**argv)) { 4739b50d902SRodney W. Grimes interval = atoi(*argv); 4749b50d902SRodney W. Grimes if (interval <= 0) 4759b50d902SRodney W. Grimes usage(); 4769b50d902SRodney W. Grimes ++argv; 4779b50d902SRodney W. Grimes iflag = 1; 4789b50d902SRodney W. Grimes } 4799b50d902SRodney W. Grimes if (*argv) { 4809b50d902SRodney W. Grimes nlistf = *argv; 4819b50d902SRodney W. Grimes if (*++argv) 4829b50d902SRodney W. Grimes memf = *argv; 4839b50d902SRodney W. Grimes } 4849b50d902SRodney W. Grimes } 4859b50d902SRodney W. Grimes #endif 4869b50d902SRodney W. Grimes 4879b50d902SRodney W. Grimes /* 4889b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 4899b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 4909b50d902SRodney W. Grimes */ 491feda1a43SJohn Baldwin live = (nlistf == NULL && memf == NULL); 492feda1a43SJohn Baldwin if (!live) 4939b50d902SRodney W. Grimes setgid(getgid()); 4949b50d902SRodney W. Grimes 495f5d34df5SGeorge V. Neville-Neil if (xflag && Tflag) 496f5d34df5SGeorge V. Neville-Neil errx(1, "-x and -T are incompatible, pick one."); 497f5d34df5SGeorge V. Neville-Neil 4986b463eedSChristian S.J. Peron if (Bflag) { 499feda1a43SJohn Baldwin if (!live) 500feda1a43SJohn Baldwin usage(); 5016b463eedSChristian S.J. Peron bpf_stats(interface); 5026b463eedSChristian S.J. Peron exit(0); 5036b463eedSChristian S.J. Peron } 5049b50d902SRodney W. Grimes if (mflag) { 50583708764SRuslan Ermilov if (!live) { 506feda1a43SJohn Baldwin if (kread(0, NULL, 0) == 0) 50705d1f5bcSAndrey V. Elsukov mbpr(kvmd, nl[N_SFSTAT].n_value); 508d15c5f56SRuslan Ermilov } else 509d4426f28SRobert Watson mbpr(NULL, 0); 5109b50d902SRodney W. Grimes exit(0); 5119b50d902SRodney W. Grimes } 5120153eb66SRobert Watson if (Qflag) { 51388737be2SRobert Watson if (!live) { 51488737be2SRobert Watson if (kread(0, NULL, 0) == 0) 51588737be2SRobert Watson netisr_stats(kvmd); 51688737be2SRobert Watson } else 51788737be2SRobert Watson netisr_stats(NULL); 5180153eb66SRobert Watson exit(0); 5190153eb66SRobert Watson } 520cc63cd56SPeter Wemm #if 0 5219b50d902SRodney W. Grimes /* 5229b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 5239b50d902SRodney W. Grimes * of open/close on each call to get* routines. 5249b50d902SRodney W. Grimes */ 5259b50d902SRodney W. Grimes sethostent(1); 5269b50d902SRodney W. Grimes setnetent(1); 527cc63cd56SPeter Wemm #else 528cc63cd56SPeter Wemm /* 529cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 530cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 531cc63cd56SPeter Wemm * used for the queries, which is slower. 532cc63cd56SPeter Wemm */ 533cc63cd56SPeter Wemm #endif 534cf5e44f8SRuslan Ermilov if (iflag && !sflag) { 535fc47e028SAlexander V. Chernikov intpr(interval, NULL, af); 5369b50d902SRodney W. Grimes exit(0); 5379b50d902SRodney W. Grimes } 5389b50d902SRodney W. Grimes if (rflag) { 5395d6d7e75SGleb Smirnoff if (sflag) { 540fc47e028SAlexander V. Chernikov rt_stats(); 5415d6d7e75SGleb Smirnoff flowtable_stats(); 5425d6d7e75SGleb Smirnoff } else 543fc47e028SAlexander V. Chernikov routepr(fib, af); 5449b50d902SRodney W. Grimes exit(0); 5459b50d902SRodney W. Grimes } 546fc47e028SAlexander V. Chernikov 5479b50d902SRodney W. Grimes if (gflag) { 548cfa1ca9dSYoshinobu Inoue if (sflag) { 549cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 550fc47e028SAlexander V. Chernikov mrt_stats(); 551cfa1ca9dSYoshinobu Inoue #ifdef INET6 552cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 553fc47e028SAlexander V. Chernikov mrt6_stats(); 554cfa1ca9dSYoshinobu Inoue #endif 555cfa1ca9dSYoshinobu Inoue } else { 556cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 557fc47e028SAlexander V. Chernikov mroutepr(); 558cfa1ca9dSYoshinobu Inoue #ifdef INET6 559cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 560fc47e028SAlexander V. Chernikov mroute6pr(); 561cfa1ca9dSYoshinobu Inoue #endif 562cfa1ca9dSYoshinobu Inoue } 5639b50d902SRodney W. Grimes exit(0); 5649b50d902SRodney W. Grimes } 565cfa1ca9dSYoshinobu Inoue 566fc47e028SAlexander V. Chernikov /* Load all necessary kvm symbols */ 567fc47e028SAlexander V. Chernikov kresolve_list(nl); 568fc47e028SAlexander V. Chernikov 569cf5e44f8SRuslan Ermilov if (tp) { 570cf5e44f8SRuslan Ermilov printproto(tp, tp->pr_name); 571cf5e44f8SRuslan Ermilov exit(0); 572cf5e44f8SRuslan Ermilov } 573cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 5749b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 575cfa1ca9dSYoshinobu Inoue printproto(tp, tp->pr_name); 576cfa1ca9dSYoshinobu Inoue #ifdef INET6 577cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 578cfa1ca9dSYoshinobu Inoue for (tp = ip6protox; tp->pr_name; tp++) 579cfa1ca9dSYoshinobu Inoue printproto(tp, tp->pr_name); 580cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 5813b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 5823b8a8567SJun-ichiro itojun Hagino if (af == PF_KEY || af == AF_UNSPEC) 5833b8a8567SJun-ichiro itojun Hagino for (tp = pfkeyprotox; tp->pr_name; tp++) 5843b8a8567SJun-ichiro itojun Hagino printproto(tp, tp->pr_name); 5853b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/ 586690f477dSSam Leffler #ifdef NETGRAPH 5874cf49a43SJulian Elischer if (af == AF_NETGRAPH || af == AF_UNSPEC) 5884cf49a43SJulian Elischer for (tp = netgraphprotox; tp->pr_name; tp++) 5894cf49a43SJulian Elischer printproto(tp, tp->pr_name); 590690f477dSSam Leffler #endif /* NETGRAPH */ 591f1c0a78dSMaxim Konovalov if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 592feda1a43SJohn Baldwin unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 593963b7ccdSRobert Watson nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 594963b7ccdSRobert Watson nl[N_UNP_SPHEAD].n_value); 5959b50d902SRodney W. Grimes exit(0); 5969b50d902SRodney W. Grimes } 5979b50d902SRodney W. Grimes 5989b50d902SRodney W. Grimes /* 5999b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 6009b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 6019b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 6029b50d902SRodney W. Grimes */ 6039b50d902SRodney W. Grimes static void 604321ae07fSPhilippe Charnier printproto(struct protox *tp, const char *name) 6059b50d902SRodney W. Grimes { 606feda1a43SJohn Baldwin void (*pr)(u_long, const char *, int, int); 6079b50d902SRodney W. Grimes u_long off; 6089b50d902SRodney W. Grimes 6099b50d902SRodney W. Grimes if (sflag) { 610cfa1ca9dSYoshinobu Inoue if (iflag) { 611cfa1ca9dSYoshinobu Inoue if (tp->pr_istats) 612fc47e028SAlexander V. Chernikov intpr(interval, tp->pr_istats, af); 613cf5e44f8SRuslan Ermilov else if (pflag) 614cf5e44f8SRuslan Ermilov printf("%s: no per-interface stats routine\n", 615cf5e44f8SRuslan Ermilov tp->pr_name); 616cfa1ca9dSYoshinobu Inoue return; 617feda1a43SJohn Baldwin } else { 6189b50d902SRodney W. Grimes pr = tp->pr_stats; 619cf5e44f8SRuslan Ermilov if (!pr) { 620cf5e44f8SRuslan Ermilov if (pflag) 621cf5e44f8SRuslan Ermilov printf("%s: no stats routine\n", 622cf5e44f8SRuslan Ermilov tp->pr_name); 623cf5e44f8SRuslan Ermilov return; 624cf5e44f8SRuslan Ermilov } 625feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 626feda1a43SJohn Baldwin off = 0; 627feda1a43SJohn Baldwin else if (tp->pr_sindex < 0) { 628feda1a43SJohn Baldwin if (pflag) 629feda1a43SJohn Baldwin printf( 630feda1a43SJohn Baldwin "%s: stats routine doesn't work on cores\n", 631feda1a43SJohn Baldwin tp->pr_name); 632feda1a43SJohn Baldwin return; 633feda1a43SJohn Baldwin } else 634feda1a43SJohn Baldwin off = nl[tp->pr_sindex].n_value; 635cfa1ca9dSYoshinobu Inoue } 6369b50d902SRodney W. Grimes } else { 6379b50d902SRodney W. Grimes pr = tp->pr_cblocks; 638cf5e44f8SRuslan Ermilov if (!pr) { 639cf5e44f8SRuslan Ermilov if (pflag) 640cf5e44f8SRuslan Ermilov printf("%s: no PCB routine\n", tp->pr_name); 641cf5e44f8SRuslan Ermilov return; 642cf5e44f8SRuslan Ermilov } 643feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 644feda1a43SJohn Baldwin off = 0; 645feda1a43SJohn Baldwin else if (tp->pr_index < 0) { 646feda1a43SJohn Baldwin if (pflag) 647feda1a43SJohn Baldwin printf( 648feda1a43SJohn Baldwin "%s: PCB routine doesn't work on cores\n", 649feda1a43SJohn Baldwin tp->pr_name); 650feda1a43SJohn Baldwin return; 651feda1a43SJohn Baldwin } else 652feda1a43SJohn Baldwin off = nl[tp->pr_index].n_value; 6539b50d902SRodney W. Grimes } 654feda1a43SJohn Baldwin if (pr != NULL && (off || (live && tp->pr_usesysctl) || 655feda1a43SJohn Baldwin af != AF_UNSPEC)) 656feda1a43SJohn Baldwin (*pr)(off, name, af, tp->pr_protocol); 6579b50d902SRodney W. Grimes } 6589b50d902SRodney W. Grimes 65929dde48dSGleb Smirnoff static int 66029dde48dSGleb Smirnoff kvmd_init(void) 6619b50d902SRodney W. Grimes { 662feda1a43SJohn Baldwin char errbuf[_POSIX2_LINE_MAX]; 663feda1a43SJohn Baldwin 66429dde48dSGleb Smirnoff if (kvmd != NULL) 66529dde48dSGleb Smirnoff return (0); 66629dde48dSGleb Smirnoff 667feda1a43SJohn Baldwin kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 6683b7e5cccSRuslan Ermilov setgid(getgid()); 66929dde48dSGleb Smirnoff 67029dde48dSGleb Smirnoff if (kvmd == NULL) { 67129dde48dSGleb Smirnoff warnx("kvm not available: %s", errbuf); 67229dde48dSGleb Smirnoff return (-1); 67329dde48dSGleb Smirnoff } 67429dde48dSGleb Smirnoff 675fc47e028SAlexander V. Chernikov return (0); 676fc47e028SAlexander V. Chernikov } 677fc47e028SAlexander V. Chernikov 678fc47e028SAlexander V. Chernikov /* 679fc47e028SAlexander V. Chernikov * Resolve symbol list, return 0 on success. 680fc47e028SAlexander V. Chernikov */ 681fc47e028SAlexander V. Chernikov int 682fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl) 683fc47e028SAlexander V. Chernikov { 684fc47e028SAlexander V. Chernikov 685fc47e028SAlexander V. Chernikov if ((kvmd == NULL) && (kvmd_init() != 0)) 686fc47e028SAlexander V. Chernikov return (-1); 687fc47e028SAlexander V. Chernikov 688fc47e028SAlexander V. Chernikov if (_nl[0].n_type != 0) 689fc47e028SAlexander V. Chernikov return (0); 690fc47e028SAlexander V. Chernikov 691fc47e028SAlexander V. Chernikov if (kvm_nlist(kvmd, _nl) < 0) { 69299453c6aSPoul-Henning Kamp if (nlistf) 69399453c6aSPoul-Henning Kamp errx(1, "%s: kvm_nlist: %s", nlistf, 69499453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 69599453c6aSPoul-Henning Kamp else 69699453c6aSPoul-Henning Kamp errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 69799453c6aSPoul-Henning Kamp } 69899453c6aSPoul-Henning Kamp 69929dde48dSGleb Smirnoff return (0); 70029dde48dSGleb Smirnoff } 70129dde48dSGleb Smirnoff 70229dde48dSGleb Smirnoff /* 70329dde48dSGleb Smirnoff * Read kernel memory, return 0 on success. 70429dde48dSGleb Smirnoff */ 70529dde48dSGleb Smirnoff int 70629dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size) 70729dde48dSGleb Smirnoff { 70829dde48dSGleb Smirnoff 70929dde48dSGleb Smirnoff if (kvmd_init() < 0) 71099453c6aSPoul-Henning Kamp return (-1); 71129dde48dSGleb Smirnoff 712c6620a13SPoul-Henning Kamp if (!buf) 713c6620a13SPoul-Henning Kamp return (0); 714feda1a43SJohn Baldwin if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 7153aa80b1dSDavid Greenman warnx("%s", kvm_geterr(kvmd)); 7169b50d902SRodney W. Grimes return (-1); 7179b50d902SRodney W. Grimes } 7189b50d902SRodney W. Grimes return (0); 7199b50d902SRodney W. Grimes } 7209b50d902SRodney W. Grimes 72129dde48dSGleb Smirnoff /* 722e3a7aa6fSGleb Smirnoff * Read single counter(9). 723e3a7aa6fSGleb Smirnoff */ 724e3a7aa6fSGleb Smirnoff uint64_t 725e3a7aa6fSGleb Smirnoff kread_counter(u_long addr) 726e3a7aa6fSGleb Smirnoff { 727e3a7aa6fSGleb Smirnoff 728e3a7aa6fSGleb Smirnoff if (kvmd_init() < 0) 729e3a7aa6fSGleb Smirnoff return (-1); 730e3a7aa6fSGleb Smirnoff 731e3a7aa6fSGleb Smirnoff return (kvm_counter_u64_fetch(kvmd, addr)); 732e3a7aa6fSGleb Smirnoff } 733e3a7aa6fSGleb Smirnoff 734e3a7aa6fSGleb Smirnoff /* 73529dde48dSGleb Smirnoff * Read an array of N counters in kernel memory into array of N uint64_t's. 73629dde48dSGleb Smirnoff */ 73729dde48dSGleb Smirnoff int 7385da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size) 73929dde48dSGleb Smirnoff { 7405da0521fSAndrey V. Elsukov uint64_t *c = buf; 74129dde48dSGleb Smirnoff 74229dde48dSGleb Smirnoff if (kvmd_init() < 0) 74329dde48dSGleb Smirnoff return (-1); 74429dde48dSGleb Smirnoff 7455da0521fSAndrey V. Elsukov if (kread(addr, buf, size) < 0) 7465da0521fSAndrey V. Elsukov return (-1); 74729dde48dSGleb Smirnoff 7485da0521fSAndrey V. Elsukov while (size != 0) { 7495da0521fSAndrey V. Elsukov *c = kvm_counter_u64_fetch(kvmd, *c); 7505da0521fSAndrey V. Elsukov size -= sizeof(*c); 7515da0521fSAndrey V. Elsukov c++; 7525da0521fSAndrey V. Elsukov } 75329dde48dSGleb Smirnoff return (0); 75429dde48dSGleb Smirnoff } 75529dde48dSGleb Smirnoff 756a01e3379SDavid Malone const char * 7577b95a1ebSYaroslav Tykhiy plural(uintmax_t n) 7589b50d902SRodney W. Grimes { 7599b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 7609b50d902SRodney W. Grimes } 7619b50d902SRodney W. Grimes 762a01e3379SDavid Malone const char * 7637b95a1ebSYaroslav Tykhiy plurales(uintmax_t n) 7649b50d902SRodney W. Grimes { 7659b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 7669b50d902SRodney W. Grimes } 7679b50d902SRodney W. Grimes 768f99a4046SMike Makonnen const char * 7697b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n) 770f99a4046SMike Makonnen { 771f99a4046SMike Makonnen return (n != 1 ? "ies" : "y"); 772f99a4046SMike Makonnen } 773f99a4046SMike Makonnen 7749b50d902SRodney W. Grimes /* 7759b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 7769b50d902SRodney W. Grimes */ 7779b50d902SRodney W. Grimes static struct protox * 778096146f8SYaroslav Tykhiy knownname(const char *name) 7799b50d902SRodney W. Grimes { 7809b50d902SRodney W. Grimes struct protox **tpp, *tp; 7819b50d902SRodney W. Grimes 7829b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 7839b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 7849b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 7859b50d902SRodney W. Grimes return (tp); 7869b50d902SRodney W. Grimes return (NULL); 7879b50d902SRodney W. Grimes } 7889b50d902SRodney W. Grimes 7899b50d902SRodney W. Grimes /* 7909b50d902SRodney W. Grimes * Find the protox corresponding to name. 7919b50d902SRodney W. Grimes */ 7929b50d902SRodney W. Grimes static struct protox * 793096146f8SYaroslav Tykhiy name2protox(const char *name) 7949b50d902SRodney W. Grimes { 7959b50d902SRodney W. Grimes struct protox *tp; 7969b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 7979b50d902SRodney W. Grimes struct protoent *p; 7989b50d902SRodney W. Grimes 7999b50d902SRodney W. Grimes /* 8009b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 8019b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 8029b50d902SRodney W. Grimes */ 803cfa1ca9dSYoshinobu Inoue if ((tp = knownname(name)) != NULL) 8049b50d902SRodney W. Grimes return (tp); 8059b50d902SRodney W. Grimes 8069b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 807cfa1ca9dSYoshinobu Inoue while ((p = getprotoent()) != NULL) { 8089b50d902SRodney W. Grimes /* assert: name not same as p->name */ 8099b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 8109b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 8119b50d902SRodney W. Grimes endprotoent(); 8129b50d902SRodney W. Grimes return (knownname(p->p_name)); 8139b50d902SRodney W. Grimes } 8149b50d902SRodney W. Grimes } 8159b50d902SRodney W. Grimes endprotoent(); 8169b50d902SRodney W. Grimes return (NULL); 8179b50d902SRodney W. Grimes } 8189b50d902SRodney W. Grimes 8199b50d902SRodney W. Grimes static void 8205e051718SAssar Westerlund usage(void) 8219b50d902SRodney W. Grimes { 8220153eb66SRobert 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", 82317ed2e8eSAlexander V. Chernikov "usage: netstat [-46AaLnSTWx] [-f protocol_family | -p protocol]\n" 8241cb467b1SRuslan Ermilov " [-M core] [-N system]", 82517ed2e8eSAlexander V. Chernikov " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" 826d44ddba9SRuslan Ermilov " [-M core] [-N system]", 82717ed2e8eSAlexander V. Chernikov " netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]", 82817ed2e8eSAlexander V. Chernikov " netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n" 82982d383bcSRuslan Ermilov " [-M core] [-N system]", 83017ed2e8eSAlexander V. Chernikov " netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n" 8311cb467b1SRuslan Ermilov " [-M core] [-N system]", 83249c2dc64SMaxim Konovalov " netstat -m [-M core] [-N system]", 8335dbd2da1SChristian S.J. Peron " netstat -B [-I interface]", 83417ed2e8eSAlexander V. Chernikov " netstat -r [-46AanW] [-f address_family] [-M core] [-N system]", 8351cb467b1SRuslan Ermilov " netstat -rs [-s] [-M core] [-N system]", 83617ed2e8eSAlexander V. Chernikov " netstat -g [-46W] [-f address_family] [-M core] [-N system]", 83717ed2e8eSAlexander V. Chernikov " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", 8380153eb66SRobert Watson " netstat -Q"); 8399b50d902SRodney W. Grimes exit(1); 8409b50d902SRodney W. Grimes } 841