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" }, 8345c203fcSGleb Smirnoff #define N_NGSOCKS 5 84096146f8SYaroslav Tykhiy { .n_name = "_ngsocklist"}, 8545c203fcSGleb Smirnoff #define N_IP6STAT 6 86096146f8SYaroslav Tykhiy { .n_name = "_ip6stat" }, 8745c203fcSGleb Smirnoff #define N_ICMP6STAT 7 88096146f8SYaroslav Tykhiy { .n_name = "_icmp6stat" }, 8945c203fcSGleb Smirnoff #define N_IPSECSTAT 8 908409aedfSGeorge V. Neville-Neil { .n_name = "_ipsec4stat" }, 9145c203fcSGleb Smirnoff #define N_IPSEC6STAT 9 92096146f8SYaroslav Tykhiy { .n_name = "_ipsec6stat" }, 9345c203fcSGleb Smirnoff #define N_PIM6STAT 10 94096146f8SYaroslav Tykhiy { .n_name = "_pim6stat" }, 9545c203fcSGleb Smirnoff #define N_MRT6STAT 11 96096146f8SYaroslav Tykhiy { .n_name = "_mrt6stat" }, 9745c203fcSGleb Smirnoff #define N_MF6CTABLE 12 98096146f8SYaroslav Tykhiy { .n_name = "_mf6ctable" }, 9945c203fcSGleb Smirnoff #define N_MIF6TABLE 13 100096146f8SYaroslav Tykhiy { .n_name = "_mif6table" }, 10145c203fcSGleb Smirnoff #define N_PFKEYSTAT 14 102096146f8SYaroslav Tykhiy { .n_name = "_pfkeystat" }, 10345c203fcSGleb Smirnoff #define N_RTTRASH 15 104096146f8SYaroslav Tykhiy { .n_name = "_rttrash" }, 10545c203fcSGleb Smirnoff #define N_CARPSTAT 16 106096146f8SYaroslav Tykhiy { .n_name = "_carpstats" }, 10745c203fcSGleb Smirnoff #define N_PFSYNCSTAT 17 108096146f8SYaroslav Tykhiy { .n_name = "_pfsyncstats" }, 10945c203fcSGleb Smirnoff #define N_AHSTAT 18 110096146f8SYaroslav Tykhiy { .n_name = "_ahstat" }, 11145c203fcSGleb Smirnoff #define N_ESPSTAT 19 112096146f8SYaroslav Tykhiy { .n_name = "_espstat" }, 11345c203fcSGleb Smirnoff #define N_IPCOMPSTAT 20 114096146f8SYaroslav Tykhiy { .n_name = "_ipcompstat" }, 11545c203fcSGleb Smirnoff #define N_TCPSTAT 21 1165da0521fSAndrey V. Elsukov { .n_name = "_tcpstat" }, 11745c203fcSGleb Smirnoff #define N_UDPSTAT 22 118feda1a43SJohn Baldwin { .n_name = "_udpstat" }, 11945c203fcSGleb Smirnoff #define N_IPSTAT 23 1205da0521fSAndrey V. Elsukov { .n_name = "_ipstat" }, 12145c203fcSGleb Smirnoff #define N_ICMPSTAT 24 122feda1a43SJohn Baldwin { .n_name = "_icmpstat" }, 12345c203fcSGleb Smirnoff #define N_IGMPSTAT 25 124feda1a43SJohn Baldwin { .n_name = "_igmpstat" }, 12545c203fcSGleb Smirnoff #define N_PIMSTAT 26 126feda1a43SJohn Baldwin { .n_name = "_pimstat" }, 12745c203fcSGleb Smirnoff #define N_TCBINFO 27 128feda1a43SJohn Baldwin { .n_name = "_tcbinfo" }, 12945c203fcSGleb Smirnoff #define N_UDBINFO 28 130feda1a43SJohn Baldwin { .n_name = "_udbinfo" }, 13145c203fcSGleb Smirnoff #define N_DIVCBINFO 29 132feda1a43SJohn Baldwin { .n_name = "_divcbinfo" }, 13345c203fcSGleb Smirnoff #define N_RIPCBINFO 30 134feda1a43SJohn Baldwin { .n_name = "_ripcbinfo" }, 13545c203fcSGleb Smirnoff #define N_UNP_COUNT 31 136feda1a43SJohn Baldwin { .n_name = "_unp_count" }, 13745c203fcSGleb Smirnoff #define N_UNP_GENCNT 32 138feda1a43SJohn Baldwin { .n_name = "_unp_gencnt" }, 13945c203fcSGleb Smirnoff #define N_UNP_DHEAD 33 140feda1a43SJohn Baldwin { .n_name = "_unp_dhead" }, 14145c203fcSGleb Smirnoff #define N_UNP_SHEAD 34 142feda1a43SJohn Baldwin { .n_name = "_unp_shead" }, 14345c203fcSGleb Smirnoff #define N_RIP6STAT 36 144feda1a43SJohn Baldwin { .n_name = "_rip6stat" }, 14545c203fcSGleb Smirnoff #define N_SCTPSTAT 36 146feda1a43SJohn Baldwin { .n_name = "_sctpstat" }, 14745c203fcSGleb Smirnoff #define N_MFCTABLESIZE 37 148443fc317SBruce M Simpson { .n_name = "_mfctablesize" }, 14945c203fcSGleb Smirnoff #define N_ARPSTAT 38 15054fc657dSGeorge V. Neville-Neil { .n_name = "_arpstat" }, 15145c203fcSGleb Smirnoff #define N_UNP_SPHEAD 39 152963b7ccdSRobert Watson { .n_name = "unp_sphead" }, 15345c203fcSGleb 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 27245c203fcSGleb 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) */ 298*85b0f0f3SAdrian Chadd int Rflag; /* show flow / RSS statistics */ 299080b7f49SDag-Erling Smørgrav int sflag; /* show protocol statistics */ 300080b7f49SDag-Erling Smørgrav int Wflag; /* wide display */ 301f5d34df5SGeorge V. Neville-Neil int Tflag; /* TCP Information */ 30249f287f8SGeorge V. Neville-Neil int xflag; /* extra information, includes all socket buffer info */ 303c73d99b5SRuslan Ermilov int zflag; /* zero stats */ 304080b7f49SDag-Erling Smørgrav 305080b7f49SDag-Erling Smørgrav int interval; /* repeat interval for i/f stats */ 306080b7f49SDag-Erling Smørgrav 307080b7f49SDag-Erling Smørgrav char *interface; /* desired i/f for stats, or NULL for all i/fs */ 308080b7f49SDag-Erling Smørgrav int unit; /* unit number for above */ 309080b7f49SDag-Erling Smørgrav 310080b7f49SDag-Erling Smørgrav int af; /* address family */ 311feda1a43SJohn Baldwin int live; /* true if we are examining a live system */ 3129b50d902SRodney W. Grimes 3139b50d902SRodney W. Grimes int 314a01e3379SDavid Malone main(int argc, char *argv[]) 3159b50d902SRodney W. Grimes { 316a01e3379SDavid Malone struct protox *tp = NULL; /* for printing cblocks & stats */ 3179b50d902SRodney W. Grimes int ch; 3183fddef95SHiroki Sato int fib = -1; 3193fddef95SHiroki Sato char *endptr; 3209b50d902SRodney W. Grimes 3219b50d902SRodney W. Grimes af = AF_UNSPEC; 3229b50d902SRodney W. Grimes 323*85b0f0f3SAdrian Chadd while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz")) 3240153eb66SRobert Watson != -1) 3259b50d902SRodney W. Grimes switch(ch) { 32617ed2e8eSAlexander V. Chernikov case '4': 32717ed2e8eSAlexander V. Chernikov #ifdef INET 32817ed2e8eSAlexander V. Chernikov af = AF_INET; 32917ed2e8eSAlexander V. Chernikov #else 33017ed2e8eSAlexander V. Chernikov errx(1, "IPv4 support is not compiled in"); 33117ed2e8eSAlexander V. Chernikov #endif 33217ed2e8eSAlexander V. Chernikov break; 33317ed2e8eSAlexander V. Chernikov case '6': 33417ed2e8eSAlexander V. Chernikov #ifdef INET6 33517ed2e8eSAlexander V. Chernikov af = AF_INET6; 33617ed2e8eSAlexander V. Chernikov #else 33717ed2e8eSAlexander V. Chernikov errx(1, "IPv6 support is not compiled in"); 33817ed2e8eSAlexander V. Chernikov #endif 33917ed2e8eSAlexander V. Chernikov break; 3409b50d902SRodney W. Grimes case 'A': 3419b50d902SRodney W. Grimes Aflag = 1; 3429b50d902SRodney W. Grimes break; 3439b50d902SRodney W. Grimes case 'a': 3449b50d902SRodney W. Grimes aflag = 1; 3459b50d902SRodney W. Grimes break; 3466b463eedSChristian S.J. Peron case 'B': 3476b463eedSChristian S.J. Peron Bflag = 1; 3486b463eedSChristian S.J. Peron break; 349e1e293a5SDavid Greenman case 'b': 350e1e293a5SDavid Greenman bflag = 1; 351e1e293a5SDavid Greenman break; 3529b50d902SRodney W. Grimes case 'd': 3539b50d902SRodney W. Grimes dflag = 1; 3549b50d902SRodney W. Grimes break; 3553fddef95SHiroki Sato case 'F': 3563fddef95SHiroki Sato fib = strtol(optarg, &endptr, 0); 3573fddef95SHiroki Sato if (*endptr != '\0' || 3583fddef95SHiroki Sato (fib == 0 && (errno == EINVAL || errno == ERANGE))) 3593fddef95SHiroki Sato errx(1, "%s: invalid fib", optarg); 3603fddef95SHiroki Sato break; 3619b50d902SRodney W. Grimes case 'f': 3622c284d93SGleb Smirnoff if (strcmp(optarg, "inet") == 0) 3639b50d902SRodney W. Grimes af = AF_INET; 364cfa1ca9dSYoshinobu Inoue #ifdef INET6 365cfa1ca9dSYoshinobu Inoue else if (strcmp(optarg, "inet6") == 0) 366cfa1ca9dSYoshinobu Inoue af = AF_INET6; 367e5134d2eSMax Laier #endif 368e5134d2eSMax Laier #ifdef IPSEC 3693b8a8567SJun-ichiro itojun Hagino else if (strcmp(optarg, "pfkey") == 0) 3703b8a8567SJun-ichiro itojun Hagino af = PF_KEY; 371e5134d2eSMax Laier #endif 3729b50d902SRodney W. Grimes else if (strcmp(optarg, "unix") == 0) 3739b50d902SRodney W. Grimes af = AF_UNIX; 374690f477dSSam Leffler #ifdef NETGRAPH 3754cf49a43SJulian Elischer else if (strcmp(optarg, "ng") == 0 3764cf49a43SJulian Elischer || strcmp(optarg, "netgraph") == 0) 3774cf49a43SJulian Elischer af = AF_NETGRAPH; 378690f477dSSam Leffler #endif 379d44ddba9SRuslan Ermilov else if (strcmp(optarg, "link") == 0) 380d44ddba9SRuslan Ermilov af = AF_LINK; 3819b50d902SRodney W. Grimes else { 38251e7d42cSGarrett Wollman errx(1, "%s: unknown address family", optarg); 3839b50d902SRodney W. Grimes } 3849b50d902SRodney W. Grimes break; 3859b50d902SRodney W. Grimes case 'g': 3869b50d902SRodney W. Grimes gflag = 1; 3879b50d902SRodney W. Grimes break; 388c2dfd19fSGleb Smirnoff case 'h': 389c2dfd19fSGleb Smirnoff hflag = 1; 390c2dfd19fSGleb Smirnoff break; 3919b50d902SRodney W. Grimes case 'I': { 3929b50d902SRodney W. Grimes char *cp; 3939b50d902SRodney W. Grimes 3949b50d902SRodney W. Grimes iflag = 1; 3959b50d902SRodney W. Grimes for (cp = interface = optarg; isalpha(*cp); cp++) 3969b50d902SRodney W. Grimes continue; 3979b50d902SRodney W. Grimes unit = atoi(cp); 3989b50d902SRodney W. Grimes break; 3999b50d902SRodney W. Grimes } 4009b50d902SRodney W. Grimes case 'i': 4019b50d902SRodney W. Grimes iflag = 1; 4029b50d902SRodney W. Grimes break; 403ac55add0SGuido van Rooij case 'L': 404ac55add0SGuido van Rooij Lflag = 1; 405ac55add0SGuido van Rooij break; 4069b50d902SRodney W. Grimes case 'M': 4079b50d902SRodney W. Grimes memf = optarg; 4089b50d902SRodney W. Grimes break; 4099b50d902SRodney W. Grimes case 'm': 4109b50d902SRodney W. Grimes mflag = 1; 4119b50d902SRodney W. Grimes break; 4129b50d902SRodney W. Grimes case 'N': 4139b50d902SRodney W. Grimes nlistf = optarg; 4149b50d902SRodney W. Grimes break; 4159b50d902SRodney W. Grimes case 'n': 41665ea0024SAssar Westerlund numeric_addr = numeric_port = 1; 4179b50d902SRodney W. Grimes break; 4189b50d902SRodney W. Grimes case 'p': 4199b50d902SRodney W. Grimes if ((tp = name2protox(optarg)) == NULL) { 42051e7d42cSGarrett Wollman errx(1, 42151e7d42cSGarrett Wollman "%s: unknown or uninstrumented protocol", 42251e7d42cSGarrett Wollman optarg); 4239b50d902SRodney W. Grimes } 4249b50d902SRodney W. Grimes pflag = 1; 4259b50d902SRodney W. Grimes break; 4260153eb66SRobert Watson case 'Q': 4270153eb66SRobert Watson Qflag = 1; 4280153eb66SRobert Watson break; 429bf10ffe1SXin LI case 'q': 430bf10ffe1SXin LI noutputs = atoi(optarg); 431bf10ffe1SXin LI if (noutputs != 0) 432bf10ffe1SXin LI noutputs++; 433bf10ffe1SXin LI break; 4349b50d902SRodney W. Grimes case 'r': 4359b50d902SRodney W. Grimes rflag = 1; 4369b50d902SRodney W. Grimes break; 437*85b0f0f3SAdrian Chadd case 'R': 438*85b0f0f3SAdrian Chadd Rflag = 1; 439*85b0f0f3SAdrian Chadd break; 4409b50d902SRodney W. Grimes case 's': 4419b50d902SRodney W. Grimes ++sflag; 4429b50d902SRodney W. Grimes break; 44365ea0024SAssar Westerlund case 'S': 44465ea0024SAssar Westerlund numeric_addr = 1; 44565ea0024SAssar Westerlund break; 4469b50d902SRodney W. Grimes case 'u': 4479b50d902SRodney W. Grimes af = AF_UNIX; 4489b50d902SRodney W. Grimes break; 449080b7f49SDag-Erling Smørgrav case 'W': 45070057abfSRuslan Ermilov case 'l': 451080b7f49SDag-Erling Smørgrav Wflag = 1; 452080b7f49SDag-Erling Smørgrav break; 4539b50d902SRodney W. Grimes case 'w': 4549b50d902SRodney W. Grimes interval = atoi(optarg); 4559b50d902SRodney W. Grimes iflag = 1; 4569b50d902SRodney W. Grimes break; 457f5d34df5SGeorge V. Neville-Neil case 'T': 458f5d34df5SGeorge V. Neville-Neil Tflag = 1; 459f5d34df5SGeorge V. Neville-Neil break; 46049f287f8SGeorge V. Neville-Neil case 'x': 46149f287f8SGeorge V. Neville-Neil xflag = 1; 46249f287f8SGeorge V. Neville-Neil break; 463c73d99b5SRuslan Ermilov case 'z': 464c73d99b5SRuslan Ermilov zflag = 1; 465c73d99b5SRuslan Ermilov break; 4669b50d902SRodney W. Grimes case '?': 4679b50d902SRodney W. Grimes default: 4689b50d902SRodney W. Grimes usage(); 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes argv += optind; 4719b50d902SRodney W. Grimes argc -= optind; 4729b50d902SRodney W. Grimes 4739b50d902SRodney W. Grimes #define BACKWARD_COMPATIBILITY 4749b50d902SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 4759b50d902SRodney W. Grimes if (*argv) { 4769b50d902SRodney W. Grimes if (isdigit(**argv)) { 4779b50d902SRodney W. Grimes interval = atoi(*argv); 4789b50d902SRodney W. Grimes if (interval <= 0) 4799b50d902SRodney W. Grimes usage(); 4809b50d902SRodney W. Grimes ++argv; 4819b50d902SRodney W. Grimes iflag = 1; 4829b50d902SRodney W. Grimes } 4839b50d902SRodney W. Grimes if (*argv) { 4849b50d902SRodney W. Grimes nlistf = *argv; 4859b50d902SRodney W. Grimes if (*++argv) 4869b50d902SRodney W. Grimes memf = *argv; 4879b50d902SRodney W. Grimes } 4889b50d902SRodney W. Grimes } 4899b50d902SRodney W. Grimes #endif 4909b50d902SRodney W. Grimes 4919b50d902SRodney W. Grimes /* 4929b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 4939b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 4949b50d902SRodney W. Grimes */ 495feda1a43SJohn Baldwin live = (nlistf == NULL && memf == NULL); 496feda1a43SJohn Baldwin if (!live) 4979b50d902SRodney W. Grimes setgid(getgid()); 4989b50d902SRodney W. Grimes 499f5d34df5SGeorge V. Neville-Neil if (xflag && Tflag) 500f5d34df5SGeorge V. Neville-Neil errx(1, "-x and -T are incompatible, pick one."); 501f5d34df5SGeorge V. Neville-Neil 5026b463eedSChristian S.J. Peron if (Bflag) { 503feda1a43SJohn Baldwin if (!live) 504feda1a43SJohn Baldwin usage(); 5056b463eedSChristian S.J. Peron bpf_stats(interface); 5066b463eedSChristian S.J. Peron exit(0); 5076b463eedSChristian S.J. Peron } 5089b50d902SRodney W. Grimes if (mflag) { 50983708764SRuslan Ermilov if (!live) { 510feda1a43SJohn Baldwin if (kread(0, NULL, 0) == 0) 51105d1f5bcSAndrey V. Elsukov mbpr(kvmd, nl[N_SFSTAT].n_value); 512d15c5f56SRuslan Ermilov } else 513d4426f28SRobert Watson mbpr(NULL, 0); 5149b50d902SRodney W. Grimes exit(0); 5159b50d902SRodney W. Grimes } 5160153eb66SRobert Watson if (Qflag) { 51788737be2SRobert Watson if (!live) { 51888737be2SRobert Watson if (kread(0, NULL, 0) == 0) 51988737be2SRobert Watson netisr_stats(kvmd); 52088737be2SRobert Watson } else 52188737be2SRobert Watson netisr_stats(NULL); 5220153eb66SRobert Watson exit(0); 5230153eb66SRobert Watson } 524cc63cd56SPeter Wemm #if 0 5259b50d902SRodney W. Grimes /* 5269b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 5279b50d902SRodney W. Grimes * of open/close on each call to get* routines. 5289b50d902SRodney W. Grimes */ 5299b50d902SRodney W. Grimes sethostent(1); 5309b50d902SRodney W. Grimes setnetent(1); 531cc63cd56SPeter Wemm #else 532cc63cd56SPeter Wemm /* 533cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 534cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 535cc63cd56SPeter Wemm * used for the queries, which is slower. 536cc63cd56SPeter Wemm */ 537cc63cd56SPeter Wemm #endif 538cf5e44f8SRuslan Ermilov if (iflag && !sflag) { 539fc47e028SAlexander V. Chernikov intpr(interval, NULL, af); 5409b50d902SRodney W. Grimes exit(0); 5419b50d902SRodney W. Grimes } 5429b50d902SRodney W. Grimes if (rflag) { 5435d6d7e75SGleb Smirnoff if (sflag) { 544fc47e028SAlexander V. Chernikov rt_stats(); 5455d6d7e75SGleb Smirnoff flowtable_stats(); 5465d6d7e75SGleb Smirnoff } else 547fc47e028SAlexander V. Chernikov routepr(fib, af); 5489b50d902SRodney W. Grimes exit(0); 5499b50d902SRodney W. Grimes } 550fc47e028SAlexander V. Chernikov 5519b50d902SRodney W. Grimes if (gflag) { 552cfa1ca9dSYoshinobu Inoue if (sflag) { 553cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 554fc47e028SAlexander V. Chernikov mrt_stats(); 555cfa1ca9dSYoshinobu Inoue #ifdef INET6 556cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 557fc47e028SAlexander V. Chernikov mrt6_stats(); 558cfa1ca9dSYoshinobu Inoue #endif 559cfa1ca9dSYoshinobu Inoue } else { 560cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 561fc47e028SAlexander V. Chernikov mroutepr(); 562cfa1ca9dSYoshinobu Inoue #ifdef INET6 563cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 564fc47e028SAlexander V. Chernikov mroute6pr(); 565cfa1ca9dSYoshinobu Inoue #endif 566cfa1ca9dSYoshinobu Inoue } 5679b50d902SRodney W. Grimes exit(0); 5689b50d902SRodney W. Grimes } 569cfa1ca9dSYoshinobu Inoue 570fc47e028SAlexander V. Chernikov /* Load all necessary kvm symbols */ 571fc47e028SAlexander V. Chernikov kresolve_list(nl); 572fc47e028SAlexander V. Chernikov 573cf5e44f8SRuslan Ermilov if (tp) { 574cf5e44f8SRuslan Ermilov printproto(tp, tp->pr_name); 575cf5e44f8SRuslan Ermilov exit(0); 576cf5e44f8SRuslan Ermilov } 577cfa1ca9dSYoshinobu Inoue if (af == AF_INET || af == AF_UNSPEC) 5789b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 579cfa1ca9dSYoshinobu Inoue printproto(tp, tp->pr_name); 580cfa1ca9dSYoshinobu Inoue #ifdef INET6 581cfa1ca9dSYoshinobu Inoue if (af == AF_INET6 || af == AF_UNSPEC) 582cfa1ca9dSYoshinobu Inoue for (tp = ip6protox; tp->pr_name; tp++) 583cfa1ca9dSYoshinobu Inoue printproto(tp, tp->pr_name); 584cfa1ca9dSYoshinobu Inoue #endif /*INET6*/ 5853b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC 5863b8a8567SJun-ichiro itojun Hagino if (af == PF_KEY || af == AF_UNSPEC) 5873b8a8567SJun-ichiro itojun Hagino for (tp = pfkeyprotox; tp->pr_name; tp++) 5883b8a8567SJun-ichiro itojun Hagino printproto(tp, tp->pr_name); 5893b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/ 590690f477dSSam Leffler #ifdef NETGRAPH 5914cf49a43SJulian Elischer if (af == AF_NETGRAPH || af == AF_UNSPEC) 5924cf49a43SJulian Elischer for (tp = netgraphprotox; tp->pr_name; tp++) 5934cf49a43SJulian Elischer printproto(tp, tp->pr_name); 594690f477dSSam Leffler #endif /* NETGRAPH */ 595f1c0a78dSMaxim Konovalov if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 596feda1a43SJohn Baldwin unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value, 597963b7ccdSRobert Watson nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value, 598963b7ccdSRobert Watson nl[N_UNP_SPHEAD].n_value); 5999b50d902SRodney W. Grimes exit(0); 6009b50d902SRodney W. Grimes } 6019b50d902SRodney W. Grimes 6029b50d902SRodney W. Grimes /* 6039b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 6049b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 6059b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 6069b50d902SRodney W. Grimes */ 6079b50d902SRodney W. Grimes static void 608321ae07fSPhilippe Charnier printproto(struct protox *tp, const char *name) 6099b50d902SRodney W. Grimes { 610feda1a43SJohn Baldwin void (*pr)(u_long, const char *, int, int); 6119b50d902SRodney W. Grimes u_long off; 6129b50d902SRodney W. Grimes 6139b50d902SRodney W. Grimes if (sflag) { 614cfa1ca9dSYoshinobu Inoue if (iflag) { 615cfa1ca9dSYoshinobu Inoue if (tp->pr_istats) 616fc47e028SAlexander V. Chernikov intpr(interval, tp->pr_istats, af); 617cf5e44f8SRuslan Ermilov else if (pflag) 618cf5e44f8SRuslan Ermilov printf("%s: no per-interface stats routine\n", 619cf5e44f8SRuslan Ermilov tp->pr_name); 620cfa1ca9dSYoshinobu Inoue return; 621feda1a43SJohn Baldwin } else { 6229b50d902SRodney W. Grimes pr = tp->pr_stats; 623cf5e44f8SRuslan Ermilov if (!pr) { 624cf5e44f8SRuslan Ermilov if (pflag) 625cf5e44f8SRuslan Ermilov printf("%s: no stats routine\n", 626cf5e44f8SRuslan Ermilov tp->pr_name); 627cf5e44f8SRuslan Ermilov return; 628cf5e44f8SRuslan Ermilov } 629feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 630feda1a43SJohn Baldwin off = 0; 631feda1a43SJohn Baldwin else if (tp->pr_sindex < 0) { 632feda1a43SJohn Baldwin if (pflag) 633feda1a43SJohn Baldwin printf( 634feda1a43SJohn Baldwin "%s: stats routine doesn't work on cores\n", 635feda1a43SJohn Baldwin tp->pr_name); 636feda1a43SJohn Baldwin return; 637feda1a43SJohn Baldwin } else 638feda1a43SJohn Baldwin off = nl[tp->pr_sindex].n_value; 639cfa1ca9dSYoshinobu Inoue } 6409b50d902SRodney W. Grimes } else { 6419b50d902SRodney W. Grimes pr = tp->pr_cblocks; 642cf5e44f8SRuslan Ermilov if (!pr) { 643cf5e44f8SRuslan Ermilov if (pflag) 644cf5e44f8SRuslan Ermilov printf("%s: no PCB routine\n", tp->pr_name); 645cf5e44f8SRuslan Ermilov return; 646cf5e44f8SRuslan Ermilov } 647feda1a43SJohn Baldwin if (tp->pr_usesysctl && live) 648feda1a43SJohn Baldwin off = 0; 649feda1a43SJohn Baldwin else if (tp->pr_index < 0) { 650feda1a43SJohn Baldwin if (pflag) 651feda1a43SJohn Baldwin printf( 652feda1a43SJohn Baldwin "%s: PCB routine doesn't work on cores\n", 653feda1a43SJohn Baldwin tp->pr_name); 654feda1a43SJohn Baldwin return; 655feda1a43SJohn Baldwin } else 656feda1a43SJohn Baldwin off = nl[tp->pr_index].n_value; 6579b50d902SRodney W. Grimes } 658feda1a43SJohn Baldwin if (pr != NULL && (off || (live && tp->pr_usesysctl) || 659feda1a43SJohn Baldwin af != AF_UNSPEC)) 660feda1a43SJohn Baldwin (*pr)(off, name, af, tp->pr_protocol); 6619b50d902SRodney W. Grimes } 6629b50d902SRodney W. Grimes 66329dde48dSGleb Smirnoff static int 66429dde48dSGleb Smirnoff kvmd_init(void) 6659b50d902SRodney W. Grimes { 666feda1a43SJohn Baldwin char errbuf[_POSIX2_LINE_MAX]; 667feda1a43SJohn Baldwin 66829dde48dSGleb Smirnoff if (kvmd != NULL) 66929dde48dSGleb Smirnoff return (0); 67029dde48dSGleb Smirnoff 671feda1a43SJohn Baldwin kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 6723b7e5cccSRuslan Ermilov setgid(getgid()); 67329dde48dSGleb Smirnoff 67429dde48dSGleb Smirnoff if (kvmd == NULL) { 67529dde48dSGleb Smirnoff warnx("kvm not available: %s", errbuf); 67629dde48dSGleb Smirnoff return (-1); 67729dde48dSGleb Smirnoff } 67829dde48dSGleb Smirnoff 679fc47e028SAlexander V. Chernikov return (0); 680fc47e028SAlexander V. Chernikov } 681fc47e028SAlexander V. Chernikov 682fc47e028SAlexander V. Chernikov /* 683fc47e028SAlexander V. Chernikov * Resolve symbol list, return 0 on success. 684fc47e028SAlexander V. Chernikov */ 685fc47e028SAlexander V. Chernikov int 686fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl) 687fc47e028SAlexander V. Chernikov { 688fc47e028SAlexander V. Chernikov 689fc47e028SAlexander V. Chernikov if ((kvmd == NULL) && (kvmd_init() != 0)) 690fc47e028SAlexander V. Chernikov return (-1); 691fc47e028SAlexander V. Chernikov 692fc47e028SAlexander V. Chernikov if (_nl[0].n_type != 0) 693fc47e028SAlexander V. Chernikov return (0); 694fc47e028SAlexander V. Chernikov 695fc47e028SAlexander V. Chernikov if (kvm_nlist(kvmd, _nl) < 0) { 69699453c6aSPoul-Henning Kamp if (nlistf) 69799453c6aSPoul-Henning Kamp errx(1, "%s: kvm_nlist: %s", nlistf, 69899453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 69999453c6aSPoul-Henning Kamp else 70099453c6aSPoul-Henning Kamp errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 70199453c6aSPoul-Henning Kamp } 70299453c6aSPoul-Henning Kamp 70329dde48dSGleb Smirnoff return (0); 70429dde48dSGleb Smirnoff } 70529dde48dSGleb Smirnoff 70629dde48dSGleb Smirnoff /* 70729dde48dSGleb Smirnoff * Read kernel memory, return 0 on success. 70829dde48dSGleb Smirnoff */ 70929dde48dSGleb Smirnoff int 71029dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size) 71129dde48dSGleb Smirnoff { 71229dde48dSGleb Smirnoff 71329dde48dSGleb Smirnoff if (kvmd_init() < 0) 71499453c6aSPoul-Henning Kamp return (-1); 71529dde48dSGleb Smirnoff 716c6620a13SPoul-Henning Kamp if (!buf) 717c6620a13SPoul-Henning Kamp return (0); 718feda1a43SJohn Baldwin if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) { 7193aa80b1dSDavid Greenman warnx("%s", kvm_geterr(kvmd)); 7209b50d902SRodney W. Grimes return (-1); 7219b50d902SRodney W. Grimes } 7229b50d902SRodney W. Grimes return (0); 7239b50d902SRodney W. Grimes } 7249b50d902SRodney W. Grimes 72529dde48dSGleb Smirnoff /* 726e3a7aa6fSGleb Smirnoff * Read single counter(9). 727e3a7aa6fSGleb Smirnoff */ 728e3a7aa6fSGleb Smirnoff uint64_t 729e3a7aa6fSGleb Smirnoff kread_counter(u_long addr) 730e3a7aa6fSGleb Smirnoff { 731e3a7aa6fSGleb Smirnoff 732e3a7aa6fSGleb Smirnoff if (kvmd_init() < 0) 733e3a7aa6fSGleb Smirnoff return (-1); 734e3a7aa6fSGleb Smirnoff 735e3a7aa6fSGleb Smirnoff return (kvm_counter_u64_fetch(kvmd, addr)); 736e3a7aa6fSGleb Smirnoff } 737e3a7aa6fSGleb Smirnoff 738e3a7aa6fSGleb Smirnoff /* 73929dde48dSGleb Smirnoff * Read an array of N counters in kernel memory into array of N uint64_t's. 74029dde48dSGleb Smirnoff */ 74129dde48dSGleb Smirnoff int 7425da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size) 74329dde48dSGleb Smirnoff { 7445da0521fSAndrey V. Elsukov uint64_t *c = buf; 74529dde48dSGleb Smirnoff 74629dde48dSGleb Smirnoff if (kvmd_init() < 0) 74729dde48dSGleb Smirnoff return (-1); 74829dde48dSGleb Smirnoff 7495da0521fSAndrey V. Elsukov if (kread(addr, buf, size) < 0) 7505da0521fSAndrey V. Elsukov return (-1); 75129dde48dSGleb Smirnoff 7525da0521fSAndrey V. Elsukov while (size != 0) { 7535da0521fSAndrey V. Elsukov *c = kvm_counter_u64_fetch(kvmd, *c); 7545da0521fSAndrey V. Elsukov size -= sizeof(*c); 7555da0521fSAndrey V. Elsukov c++; 7565da0521fSAndrey V. Elsukov } 75729dde48dSGleb Smirnoff return (0); 75829dde48dSGleb Smirnoff } 75929dde48dSGleb Smirnoff 760a01e3379SDavid Malone const char * 7617b95a1ebSYaroslav Tykhiy plural(uintmax_t n) 7629b50d902SRodney W. Grimes { 7639b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 7649b50d902SRodney W. Grimes } 7659b50d902SRodney W. Grimes 766a01e3379SDavid Malone const char * 7677b95a1ebSYaroslav Tykhiy plurales(uintmax_t n) 7689b50d902SRodney W. Grimes { 7699b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 7709b50d902SRodney W. Grimes } 7719b50d902SRodney W. Grimes 772f99a4046SMike Makonnen const char * 7737b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n) 774f99a4046SMike Makonnen { 775f99a4046SMike Makonnen return (n != 1 ? "ies" : "y"); 776f99a4046SMike Makonnen } 777f99a4046SMike Makonnen 7789b50d902SRodney W. Grimes /* 7799b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 7809b50d902SRodney W. Grimes */ 7819b50d902SRodney W. Grimes static struct protox * 782096146f8SYaroslav Tykhiy knownname(const char *name) 7839b50d902SRodney W. Grimes { 7849b50d902SRodney W. Grimes struct protox **tpp, *tp; 7859b50d902SRodney W. Grimes 7869b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 7879b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 7889b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 7899b50d902SRodney W. Grimes return (tp); 7909b50d902SRodney W. Grimes return (NULL); 7919b50d902SRodney W. Grimes } 7929b50d902SRodney W. Grimes 7939b50d902SRodney W. Grimes /* 7949b50d902SRodney W. Grimes * Find the protox corresponding to name. 7959b50d902SRodney W. Grimes */ 7969b50d902SRodney W. Grimes static struct protox * 797096146f8SYaroslav Tykhiy name2protox(const char *name) 7989b50d902SRodney W. Grimes { 7999b50d902SRodney W. Grimes struct protox *tp; 8009b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 8019b50d902SRodney W. Grimes struct protoent *p; 8029b50d902SRodney W. Grimes 8039b50d902SRodney W. Grimes /* 8049b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 8059b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 8069b50d902SRodney W. Grimes */ 807cfa1ca9dSYoshinobu Inoue if ((tp = knownname(name)) != NULL) 8089b50d902SRodney W. Grimes return (tp); 8099b50d902SRodney W. Grimes 8109b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 811cfa1ca9dSYoshinobu Inoue while ((p = getprotoent()) != NULL) { 8129b50d902SRodney W. Grimes /* assert: name not same as p->name */ 8139b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 8149b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 8159b50d902SRodney W. Grimes endprotoent(); 8169b50d902SRodney W. Grimes return (knownname(p->p_name)); 8179b50d902SRodney W. Grimes } 8189b50d902SRodney W. Grimes } 8199b50d902SRodney W. Grimes endprotoent(); 8209b50d902SRodney W. Grimes return (NULL); 8219b50d902SRodney W. Grimes } 8229b50d902SRodney W. Grimes 8239b50d902SRodney W. Grimes static void 8245e051718SAssar Westerlund usage(void) 8259b50d902SRodney W. Grimes { 8260153eb66SRobert 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", 827*85b0f0f3SAdrian Chadd "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n" 8281cb467b1SRuslan Ermilov " [-M core] [-N system]", 82917ed2e8eSAlexander V. Chernikov " netstat -i | -I interface [-46abdhnW] [-f address_family]\n" 830d44ddba9SRuslan Ermilov " [-M core] [-N system]", 83117ed2e8eSAlexander V. Chernikov " netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]", 83217ed2e8eSAlexander V. Chernikov " netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n" 83382d383bcSRuslan Ermilov " [-M core] [-N system]", 83417ed2e8eSAlexander V. Chernikov " netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n" 8351cb467b1SRuslan Ermilov " [-M core] [-N system]", 83649c2dc64SMaxim Konovalov " netstat -m [-M core] [-N system]", 8375dbd2da1SChristian S.J. Peron " netstat -B [-I interface]", 83817ed2e8eSAlexander V. Chernikov " netstat -r [-46AanW] [-f address_family] [-M core] [-N system]", 8391cb467b1SRuslan Ermilov " netstat -rs [-s] [-M core] [-N system]", 84017ed2e8eSAlexander V. Chernikov " netstat -g [-46W] [-f address_family] [-M core] [-N system]", 84117ed2e8eSAlexander V. Chernikov " netstat -gs [-46s] [-f address_family] [-M core] [-N system]", 8420153eb66SRobert Watson " netstat -Q"); 8439b50d902SRodney W. Grimes exit(1); 8449b50d902SRodney W. Grimes } 845