xref: /freebsd/usr.bin/netstat/main.c (revision dbfd87087bf7ea38323ee5aa24e26483eea30117)
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
3181dacd8bSHiroki Sato static 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>
509eddb899SMark Johnston #include <sys/sysctl.h>
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes #include <netinet/in.h>
539b50d902SRodney W. Grimes 
54690f477dSSam Leffler #ifdef NETGRAPH
554cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
56690f477dSSam Leffler #endif
574cf49a43SJulian Elischer 
589b50d902SRodney W. Grimes #include <ctype.h>
595d422d6aSPhilippe Charnier #include <err.h>
60821df508SXin LI #include <errno.h>
619b50d902SRodney W. Grimes #include <kvm.h>
629b50d902SRodney W. Grimes #include <limits.h>
639b50d902SRodney W. Grimes #include <netdb.h>
649b50d902SRodney W. Grimes #include <nlist.h>
65821df508SXin LI #include <paths.h>
667b95a1ebSYaroslav Tykhiy #include <stdint.h>
679b50d902SRodney W. Grimes #include <stdio.h>
689b50d902SRodney W. Grimes #include <stdlib.h>
69ade9ccfeSMarcel Moolenaar #include <stdbool.h>
709b50d902SRodney W. Grimes #include <string.h>
719b50d902SRodney W. Grimes #include <unistd.h>
729b50d902SRodney W. Grimes #include "netstat.h"
7381dacd8bSHiroki Sato #include "nl_defs.h"
74ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
759b50d902SRodney W. Grimes 
7681dacd8bSHiroki Sato static struct protox {
77feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
78feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
799b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
80feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
815e051718SAssar Westerlund 					/* control blocks printing routine */
82feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
835e051718SAssar Westerlund 					/* statistics printing routine */
845e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
85fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
8655fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
87feda1a43SJohn Baldwin 	int	pr_protocol;
889b50d902SRodney W. Grimes } protox[] = {
89feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
90feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
91feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
92feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
9374fd40c9SRandall Stewart #ifdef SCTP
94feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
95feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
9674fd40c9SRandall Stewart #endif
97aa0a1e58SJeff Roberson #ifdef SDP
98aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
99aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
100aa0a1e58SJeff Roberson #endif
101feda1a43SJohn Baldwin 	{ N_DIVCBINFO,	-1,		1,	protopr,
102feda1a43SJohn Baldwin 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
103feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
104feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
105feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
106feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
107feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
108feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
109cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
11081dacd8bSHiroki Sato 	{ -1,		N_IPSEC4STAT,	1,	NULL,	/* keep as compat */
1119d2d8e7bSGeorge V. Neville-Neil 	  ipsec_stats,	NULL,		"ipsec", 1,	0},
1128409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
1139d2d8e7bSGeorge V. Neville-Neil 	  ah_stats,	NULL,		"ah",	1,	0},
1148409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
1159d2d8e7bSGeorge V. Neville-Neil 	  esp_stats,	NULL,		"esp",	1,	0},
1168409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
1179d2d8e7bSGeorge V. Neville-Neil 	  ipcomp_stats,	NULL,		"ipcomp", 1,	0},
118100b98dbSKelly Yancey #endif
119feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
120feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
12181dacd8bSHiroki Sato 	{ -1,		N_CARPSTATS,	1,	NULL,
122feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
1233e4d5cd3SGleb Smirnoff #ifdef PF
12481dacd8bSHiroki Sato 	{ -1,		N_PFSYNCSTATS,	1,	NULL,
125feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
1263e4d5cd3SGleb Smirnoff #endif
12754fc657dSGeorge V. Neville-Neil 	{ -1,		N_ARPSTAT,	1,	NULL,
12854fc657dSGeorge V. Neville-Neil 	  arp_stats,	NULL,		"arp", 1,	0 },
1296bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
130feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1319b50d902SRodney W. Grimes };
1329b50d902SRodney W. Grimes 
133cfa1ca9dSYoshinobu Inoue #ifdef INET6
13481dacd8bSHiroki Sato static struct protox ip6protox[] = {
135feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
136feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
137feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
138feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
139feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
140feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
141feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
142feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
143aa0a1e58SJeff Roberson #ifdef SDP
144aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
145aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
146aa0a1e58SJeff Roberson #endif
147cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
1486bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
1499d2d8e7bSGeorge V. Neville-Neil 	  ipsec_stats,	NULL,		"ipsec6", 1,	0 },
150cfa1ca9dSYoshinobu Inoue #endif
151cfa1ca9dSYoshinobu Inoue #ifdef notyet
1526bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
153feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
154cfa1ca9dSYoshinobu Inoue #endif
155feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
156feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
1576bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
158feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
159cfa1ca9dSYoshinobu Inoue };
160cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
161cfa1ca9dSYoshinobu Inoue 
1623b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
16381dacd8bSHiroki Sato static struct protox pfkeyprotox[] = {
1646bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
165feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
1666bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
167feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1683b8a8567SJun-ichiro itojun Hagino };
1693b8a8567SJun-ichiro itojun Hagino #endif
1703b8a8567SJun-ichiro itojun Hagino 
171690f477dSSam Leffler #ifdef NETGRAPH
17281dacd8bSHiroki Sato static struct protox netgraphprotox[] = {
17381dacd8bSHiroki Sato 	{ N_NGSOCKLIST,	-1,		1,	netgraphprotopr,
174feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
17581dacd8bSHiroki Sato 	{ N_NGSOCKLIST,	-1,		1,	netgraphprotopr,
176feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
1776bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
178feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1794cf49a43SJulian Elischer };
180690f477dSSam Leffler #endif
181cc6a66f2SJulian Elischer 
18281dacd8bSHiroki Sato static struct protox *protoprotox[] = {
183cfa1ca9dSYoshinobu Inoue 					 protox,
184cfa1ca9dSYoshinobu Inoue #ifdef INET6
185cfa1ca9dSYoshinobu Inoue 					 ip6protox,
186cfa1ca9dSYoshinobu Inoue #endif
1873b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
1883b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
1893b8a8567SJun-ichiro itojun Hagino #endif
19045c203fcSGleb Smirnoff 					 NULL };
1919b50d902SRodney W. Grimes 
192ade9ccfeSMarcel Moolenaar static void printproto(struct protox *, const char *, bool *);
1935e051718SAssar Westerlund static void usage(void);
194096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
195096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
1969b50d902SRodney W. Grimes 
19781dacd8bSHiroki Sato static int kresolve_list(struct nlist *_nl);
19881dacd8bSHiroki Sato 
199c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
200080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
201080b7f49SDag-Erling Smørgrav 
202080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
203080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
20481dacd8bSHiroki Sato static int	Bflag;		/* show information about bpf consumers */
205080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
206080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
207080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
208c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
209080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
210080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
211080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
212bf10ffe1SXin LI int	noutputs = 0;	/* how much outputs before we exit */
21365ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
21465ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
215cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
21681dacd8bSHiroki Sato static int	Qflag;		/* show netisr information */
217080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
21885b0f0f3SAdrian Chadd int	Rflag;		/* show flow / RSS statistics */
219080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
220080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
221f5d34df5SGeorge V. Neville-Neil int	Tflag;		/* TCP Information */
22249f287f8SGeorge V. Neville-Neil int	xflag;		/* extra information, includes all socket buffer info */
223c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
224080b7f49SDag-Erling Smørgrav 
225080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
226080b7f49SDag-Erling Smørgrav 
227080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
228080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
229080b7f49SDag-Erling Smørgrav 
23081dacd8bSHiroki Sato static int	af;		/* address family */
231feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
2329b50d902SRodney W. Grimes 
2339b50d902SRodney W. Grimes int
234a01e3379SDavid Malone main(int argc, char *argv[])
2359b50d902SRodney W. Grimes {
236a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
2379b50d902SRodney W. Grimes 	int ch;
2383fddef95SHiroki Sato 	int fib = -1;
2393fddef95SHiroki Sato 	char *endptr;
240ade9ccfeSMarcel Moolenaar 	bool first = true;
2419b50d902SRodney W. Grimes 
2429b50d902SRodney W. Grimes 	af = AF_UNSPEC;
2439b50d902SRodney W. Grimes 
244ade9ccfeSMarcel Moolenaar 	argc = xo_parse_args(argc, argv);
24506ff7ccbSHajimu UMEMOTO 	if (argc < 0)
24606ff7ccbSHajimu UMEMOTO 		exit(EXIT_FAILURE);
247ade9ccfeSMarcel Moolenaar 
24885b0f0f3SAdrian Chadd 	while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
2490153eb66SRobert Watson 	    != -1)
2509b50d902SRodney W. Grimes 		switch(ch) {
25117ed2e8eSAlexander V. Chernikov 		case '4':
25217ed2e8eSAlexander V. Chernikov #ifdef INET
25317ed2e8eSAlexander V. Chernikov 			af = AF_INET;
25417ed2e8eSAlexander V. Chernikov #else
25517ed2e8eSAlexander V. Chernikov 			errx(1, "IPv4 support is not compiled in");
25617ed2e8eSAlexander V. Chernikov #endif
25717ed2e8eSAlexander V. Chernikov 			break;
25817ed2e8eSAlexander V. Chernikov 		case '6':
25917ed2e8eSAlexander V. Chernikov #ifdef INET6
26017ed2e8eSAlexander V. Chernikov 			af = AF_INET6;
26117ed2e8eSAlexander V. Chernikov #else
26217ed2e8eSAlexander V. Chernikov 			errx(1, "IPv6 support is not compiled in");
26317ed2e8eSAlexander V. Chernikov #endif
26417ed2e8eSAlexander V. Chernikov 			break;
2659b50d902SRodney W. Grimes 		case 'A':
2669b50d902SRodney W. Grimes 			Aflag = 1;
2679b50d902SRodney W. Grimes 			break;
2689b50d902SRodney W. Grimes 		case 'a':
2699b50d902SRodney W. Grimes 			aflag = 1;
2709b50d902SRodney W. Grimes 			break;
2716b463eedSChristian S.J. Peron 		case 'B':
2726b463eedSChristian S.J. Peron 			Bflag = 1;
2736b463eedSChristian S.J. Peron 			break;
274e1e293a5SDavid Greenman 		case 'b':
275e1e293a5SDavid Greenman 			bflag = 1;
276e1e293a5SDavid Greenman 			break;
2779b50d902SRodney W. Grimes 		case 'd':
2789b50d902SRodney W. Grimes 			dflag = 1;
2799b50d902SRodney W. Grimes 			break;
2803fddef95SHiroki Sato 		case 'F':
2813fddef95SHiroki Sato 			fib = strtol(optarg, &endptr, 0);
2823fddef95SHiroki Sato 			if (*endptr != '\0' ||
2833fddef95SHiroki Sato 			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
284ade9ccfeSMarcel Moolenaar 				xo_errx(1, "%s: invalid fib", optarg);
2853fddef95SHiroki Sato 			break;
2869b50d902SRodney W. Grimes 		case 'f':
2872c284d93SGleb Smirnoff 			if (strcmp(optarg, "inet") == 0)
2889b50d902SRodney W. Grimes 				af = AF_INET;
289cfa1ca9dSYoshinobu Inoue #ifdef INET6
290cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
291cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
292e5134d2eSMax Laier #endif
293e5134d2eSMax Laier #ifdef IPSEC
2943b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
2953b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
296e5134d2eSMax Laier #endif
29710d5269fSHiroki Sato 			else if (strcmp(optarg, "unix") == 0 ||
29810d5269fSHiroki Sato 				 strcmp(optarg, "local") == 0)
2999b50d902SRodney W. Grimes 				af = AF_UNIX;
300690f477dSSam Leffler #ifdef NETGRAPH
3014cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3024cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3034cf49a43SJulian Elischer 				af = AF_NETGRAPH;
304690f477dSSam Leffler #endif
305d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
306d44ddba9SRuslan Ermilov 				af = AF_LINK;
3079b50d902SRodney W. Grimes 			else {
308ade9ccfeSMarcel Moolenaar 				xo_errx(1, "%s: unknown address family",
309ade9ccfeSMarcel Moolenaar 				    optarg);
3109b50d902SRodney W. Grimes 			}
3119b50d902SRodney W. Grimes 			break;
3129b50d902SRodney W. Grimes 		case 'g':
3139b50d902SRodney W. Grimes 			gflag = 1;
3149b50d902SRodney W. Grimes 			break;
315c2dfd19fSGleb Smirnoff 		case 'h':
316c2dfd19fSGleb Smirnoff 			hflag = 1;
317c2dfd19fSGleb Smirnoff 			break;
3189b50d902SRodney W. Grimes 		case 'I': {
3199b50d902SRodney W. Grimes 			char *cp;
3209b50d902SRodney W. Grimes 
3219b50d902SRodney W. Grimes 			iflag = 1;
3229b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3239b50d902SRodney W. Grimes 				continue;
3249b50d902SRodney W. Grimes 			unit = atoi(cp);
3259b50d902SRodney W. Grimes 			break;
3269b50d902SRodney W. Grimes 		}
3279b50d902SRodney W. Grimes 		case 'i':
3289b50d902SRodney W. Grimes 			iflag = 1;
3299b50d902SRodney W. Grimes 			break;
330ac55add0SGuido van Rooij 		case 'L':
331ac55add0SGuido van Rooij 			Lflag = 1;
332ac55add0SGuido van Rooij 			break;
3339b50d902SRodney W. Grimes 		case 'M':
3349b50d902SRodney W. Grimes 			memf = optarg;
3359b50d902SRodney W. Grimes 			break;
3369b50d902SRodney W. Grimes 		case 'm':
3379b50d902SRodney W. Grimes 			mflag = 1;
3389b50d902SRodney W. Grimes 			break;
3399b50d902SRodney W. Grimes 		case 'N':
3409b50d902SRodney W. Grimes 			nlistf = optarg;
3419b50d902SRodney W. Grimes 			break;
3429b50d902SRodney W. Grimes 		case 'n':
34365ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
3449b50d902SRodney W. Grimes 			break;
3459b50d902SRodney W. Grimes 		case 'p':
3469b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
347ade9ccfeSMarcel Moolenaar 				xo_errx(1, "%s: unknown or uninstrumented "
348ade9ccfeSMarcel Moolenaar 				    "protocol", optarg);
3499b50d902SRodney W. Grimes 			}
3509b50d902SRodney W. Grimes 			pflag = 1;
3519b50d902SRodney W. Grimes 			break;
3520153eb66SRobert Watson 		case 'Q':
3530153eb66SRobert Watson 			Qflag = 1;
3540153eb66SRobert Watson 			break;
355bf10ffe1SXin LI 		case 'q':
356bf10ffe1SXin LI 			noutputs = atoi(optarg);
357bf10ffe1SXin LI 			if (noutputs != 0)
358bf10ffe1SXin LI 				noutputs++;
359bf10ffe1SXin LI 			break;
3609b50d902SRodney W. Grimes 		case 'r':
3619b50d902SRodney W. Grimes 			rflag = 1;
3629b50d902SRodney W. Grimes 			break;
36385b0f0f3SAdrian Chadd 		case 'R':
36485b0f0f3SAdrian Chadd 			Rflag = 1;
36585b0f0f3SAdrian Chadd 			break;
3669b50d902SRodney W. Grimes 		case 's':
3679b50d902SRodney W. Grimes 			++sflag;
3689b50d902SRodney W. Grimes 			break;
36965ea0024SAssar Westerlund 		case 'S':
37065ea0024SAssar Westerlund 			numeric_addr = 1;
37165ea0024SAssar Westerlund 			break;
3729b50d902SRodney W. Grimes 		case 'u':
3739b50d902SRodney W. Grimes 			af = AF_UNIX;
3749b50d902SRodney W. Grimes 			break;
375080b7f49SDag-Erling Smørgrav 		case 'W':
37670057abfSRuslan Ermilov 		case 'l':
377080b7f49SDag-Erling Smørgrav 			Wflag = 1;
378080b7f49SDag-Erling Smørgrav 			break;
3799b50d902SRodney W. Grimes 		case 'w':
3809b50d902SRodney W. Grimes 			interval = atoi(optarg);
3819b50d902SRodney W. Grimes 			iflag = 1;
3829b50d902SRodney W. Grimes 			break;
383f5d34df5SGeorge V. Neville-Neil 		case 'T':
384f5d34df5SGeorge V. Neville-Neil 			Tflag = 1;
385f5d34df5SGeorge V. Neville-Neil 			break;
38649f287f8SGeorge V. Neville-Neil 		case 'x':
38749f287f8SGeorge V. Neville-Neil 			xflag = 1;
38849f287f8SGeorge V. Neville-Neil 			break;
389c73d99b5SRuslan Ermilov 		case 'z':
390c73d99b5SRuslan Ermilov 			zflag = 1;
391c73d99b5SRuslan Ermilov 			break;
3929b50d902SRodney W. Grimes 		case '?':
3939b50d902SRodney W. Grimes 		default:
3949b50d902SRodney W. Grimes 			usage();
3959b50d902SRodney W. Grimes 		}
3969b50d902SRodney W. Grimes 	argv += optind;
3979b50d902SRodney W. Grimes 	argc -= optind;
3989b50d902SRodney W. Grimes 
3999b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4009b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4019b50d902SRodney W. Grimes 	if (*argv) {
4029b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4039b50d902SRodney W. Grimes 			interval = atoi(*argv);
4049b50d902SRodney W. Grimes 			if (interval <= 0)
4059b50d902SRodney W. Grimes 				usage();
4069b50d902SRodney W. Grimes 			++argv;
4079b50d902SRodney W. Grimes 			iflag = 1;
4089b50d902SRodney W. Grimes 		}
4099b50d902SRodney W. Grimes 		if (*argv) {
4109b50d902SRodney W. Grimes 			nlistf = *argv;
4119b50d902SRodney W. Grimes 			if (*++argv)
4129b50d902SRodney W. Grimes 				memf = *argv;
4139b50d902SRodney W. Grimes 		}
4149b50d902SRodney W. Grimes 	}
4159b50d902SRodney W. Grimes #endif
4169b50d902SRodney W. Grimes 
4179b50d902SRodney W. Grimes 	/*
4189b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4199b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4209b50d902SRodney W. Grimes 	 */
421feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
422a4a889a0SXin LI 	if (!live) {
423a4a889a0SXin LI 		if (setgid(getgid()) != 0)
424a4a889a0SXin LI 			xo_err(-1, "setgid");
425a4a889a0SXin LI 	}
4269b50d902SRodney W. Grimes 
427f5d34df5SGeorge V. Neville-Neil 	if (xflag && Tflag)
428ade9ccfeSMarcel Moolenaar 		xo_errx(1, "-x and -T are incompatible, pick one.");
429f5d34df5SGeorge V. Neville-Neil 
4306b463eedSChristian S.J. Peron 	if (Bflag) {
431feda1a43SJohn Baldwin 		if (!live)
432feda1a43SJohn Baldwin 			usage();
4336b463eedSChristian S.J. Peron 		bpf_stats(interface);
434ade9ccfeSMarcel Moolenaar 		xo_finish();
4356b463eedSChristian S.J. Peron 		exit(0);
4366b463eedSChristian S.J. Peron 	}
4379b50d902SRodney W. Grimes 	if (mflag) {
43883708764SRuslan Ermilov 		if (!live) {
439feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
44005d1f5bcSAndrey V. Elsukov 				mbpr(kvmd, nl[N_SFSTAT].n_value);
441d15c5f56SRuslan Ermilov 		} else
442d4426f28SRobert Watson 			mbpr(NULL, 0);
443ade9ccfeSMarcel Moolenaar 		xo_finish();
4449b50d902SRodney W. Grimes 		exit(0);
4459b50d902SRodney W. Grimes 	}
4460153eb66SRobert Watson 	if (Qflag) {
44788737be2SRobert Watson 		if (!live) {
44888737be2SRobert Watson 			if (kread(0, NULL, 0) == 0)
44981dacd8bSHiroki Sato 				netisr_stats();
45088737be2SRobert Watson 		} else
45181dacd8bSHiroki Sato 			netisr_stats();
452ade9ccfeSMarcel Moolenaar 		xo_finish();
4530153eb66SRobert Watson 		exit(0);
4540153eb66SRobert Watson 	}
455cc63cd56SPeter Wemm #if 0
4569b50d902SRodney W. Grimes 	/*
4579b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
4589b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
4599b50d902SRodney W. Grimes 	 */
4609b50d902SRodney W. Grimes 	sethostent(1);
4619b50d902SRodney W. Grimes 	setnetent(1);
462cc63cd56SPeter Wemm #else
463cc63cd56SPeter Wemm 	/*
464cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
465cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
466cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
467cc63cd56SPeter Wemm 	 */
468cc63cd56SPeter Wemm #endif
469cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
470ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
47110d5269fSHiroki Sato 		intpr(NULL, af);
472ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
473ade9ccfeSMarcel Moolenaar 		xo_finish();
4749b50d902SRodney W. Grimes 		exit(0);
4759b50d902SRodney W. Grimes 	}
4769b50d902SRodney W. Grimes 	if (rflag) {
477ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
4785d6d7e75SGleb Smirnoff 		if (sflag) {
479fc47e028SAlexander V. Chernikov 			rt_stats();
4805d6d7e75SGleb Smirnoff 			flowtable_stats();
4815d6d7e75SGleb Smirnoff 		} else
482fc47e028SAlexander V. Chernikov 			routepr(fib, af);
483ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
484ade9ccfeSMarcel Moolenaar 		xo_finish();
4859b50d902SRodney W. Grimes 		exit(0);
4869b50d902SRodney W. Grimes 	}
487fc47e028SAlexander V. Chernikov 
4889b50d902SRodney W. Grimes 	if (gflag) {
489ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
490cfa1ca9dSYoshinobu Inoue 		if (sflag) {
491cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
492fc47e028SAlexander V. Chernikov 				mrt_stats();
493cfa1ca9dSYoshinobu Inoue #ifdef INET6
494cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
495fc47e028SAlexander V. Chernikov 				mrt6_stats();
496cfa1ca9dSYoshinobu Inoue #endif
497cfa1ca9dSYoshinobu Inoue 		} else {
498cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
499fc47e028SAlexander V. Chernikov 				mroutepr();
500cfa1ca9dSYoshinobu Inoue #ifdef INET6
501cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
502fc47e028SAlexander V. Chernikov 				mroute6pr();
503cfa1ca9dSYoshinobu Inoue #endif
504cfa1ca9dSYoshinobu Inoue 		}
505ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
506ade9ccfeSMarcel Moolenaar 		xo_finish();
5079b50d902SRodney W. Grimes 		exit(0);
5089b50d902SRodney W. Grimes 	}
509cfa1ca9dSYoshinobu Inoue 
510fc47e028SAlexander V. Chernikov 	/* Load all necessary kvm symbols */
511fc47e028SAlexander V. Chernikov 	kresolve_list(nl);
512fc47e028SAlexander V. Chernikov 
513cf5e44f8SRuslan Ermilov 	if (tp) {
514ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
515ade9ccfeSMarcel Moolenaar 		printproto(tp, tp->pr_name, &first);
516ade9ccfeSMarcel Moolenaar 		if (!first)
517ade9ccfeSMarcel Moolenaar 			xo_close_list("socket");
518ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
519ade9ccfeSMarcel Moolenaar 		xo_finish();
520cf5e44f8SRuslan Ermilov 		exit(0);
521cf5e44f8SRuslan Ermilov 	}
522ade9ccfeSMarcel Moolenaar 
523ade9ccfeSMarcel Moolenaar 	xo_open_container("statistics");
524cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5259b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
526ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
527cfa1ca9dSYoshinobu Inoue #ifdef INET6
528cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
529cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
530ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
531cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5323b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5333b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5343b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
535ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
5363b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
537690f477dSSam Leffler #ifdef NETGRAPH
5384cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5394cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
540ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
541690f477dSSam Leffler #endif /* NETGRAPH */
542f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
543feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
544963b7ccdSRobert Watson 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
545ade9ccfeSMarcel Moolenaar 		    nl[N_UNP_SPHEAD].n_value, &first);
546ade9ccfeSMarcel Moolenaar 
547ade9ccfeSMarcel Moolenaar 	if (!first)
548ade9ccfeSMarcel Moolenaar 		xo_close_list("socket");
549ade9ccfeSMarcel Moolenaar 	xo_close_container("statistics");
550ade9ccfeSMarcel Moolenaar 	xo_finish();
5519b50d902SRodney W. Grimes 	exit(0);
5529b50d902SRodney W. Grimes }
5539b50d902SRodney W. Grimes 
554*dbfd8708SGleb Smirnoff static int
555*dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
556*dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn, int zero)
5579eddb899SMark Johnston {
5589eddb899SMark Johnston 	int error;
5599eddb899SMark Johnston 
5609eddb899SMark Johnston 	if (live) {
5619eddb899SMark Johnston 		memset(stats, 0, len);
562*dbfd8708SGleb Smirnoff 		if (zero)
5639eddb899SMark Johnston 			error = sysctlbyname(sysctlname, NULL, NULL, stats,
5649eddb899SMark Johnston 			    len);
5659eddb899SMark Johnston 		else
5669eddb899SMark Johnston 			error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
5679eddb899SMark Johnston 		if (error == -1 && errno != ENOENT)
5689eddb899SMark Johnston 			xo_warn("sysctl %s", sysctlname);
5699eddb899SMark Johnston 	} else {
5709eddb899SMark Johnston 		if (off == 0)
5719eddb899SMark Johnston 			return (1);
5729eddb899SMark Johnston 		error = kreadfn(off, stats, len);
5739eddb899SMark Johnston 	}
5749eddb899SMark Johnston 	return (error);
5759eddb899SMark Johnston }
5769eddb899SMark Johnston 
577*dbfd8708SGleb Smirnoff int
578*dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats,
579*dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
580*dbfd8708SGleb Smirnoff {
581*dbfd8708SGleb Smirnoff 
582*dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
583*dbfd8708SGleb Smirnoff     zflag));
584*dbfd8708SGleb Smirnoff }
585*dbfd8708SGleb Smirnoff 
586*dbfd8708SGleb Smirnoff int
587*dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
588*dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
589*dbfd8708SGleb Smirnoff {
590*dbfd8708SGleb Smirnoff 
591*dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
592*dbfd8708SGleb Smirnoff }
593*dbfd8708SGleb Smirnoff 
5949b50d902SRodney W. Grimes /*
5959b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
5969b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
5979b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
5989b50d902SRodney W. Grimes  */
5999b50d902SRodney W. Grimes static void
600ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first)
6019b50d902SRodney W. Grimes {
602feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6039b50d902SRodney W. Grimes 	u_long off;
604ade9ccfeSMarcel Moolenaar 	bool doingdblocks = false;
6059b50d902SRodney W. Grimes 
6069b50d902SRodney W. Grimes 	if (sflag) {
607cfa1ca9dSYoshinobu Inoue 		if (iflag) {
608cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
60910d5269fSHiroki Sato 				intpr(tp->pr_istats, af);
610cf5e44f8SRuslan Ermilov 			else if (pflag)
611ade9ccfeSMarcel Moolenaar 				xo_message("%s: no per-interface stats routine",
612cf5e44f8SRuslan Ermilov 				    tp->pr_name);
613cfa1ca9dSYoshinobu Inoue 			return;
614feda1a43SJohn Baldwin 		} else {
6159b50d902SRodney W. Grimes 			pr = tp->pr_stats;
616cf5e44f8SRuslan Ermilov 			if (!pr) {
617cf5e44f8SRuslan Ermilov 				if (pflag)
618ade9ccfeSMarcel Moolenaar 					xo_message("%s: no stats routine",
619cf5e44f8SRuslan Ermilov 					    tp->pr_name);
620cf5e44f8SRuslan Ermilov 				return;
621cf5e44f8SRuslan Ermilov 			}
622feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
623feda1a43SJohn Baldwin 				off = 0;
624feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
625feda1a43SJohn Baldwin 				if (pflag)
626ade9ccfeSMarcel Moolenaar 					xo_message("%s: stats routine doesn't "
627ade9ccfeSMarcel Moolenaar 					    "work on cores", tp->pr_name);
628feda1a43SJohn Baldwin 				return;
629feda1a43SJohn Baldwin 			} else
630feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
631cfa1ca9dSYoshinobu Inoue 		}
6329b50d902SRodney W. Grimes 	} else {
633ade9ccfeSMarcel Moolenaar 		doingdblocks = true;
6349b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
635cf5e44f8SRuslan Ermilov 		if (!pr) {
636cf5e44f8SRuslan Ermilov 			if (pflag)
637ade9ccfeSMarcel Moolenaar 				xo_message("%s: no PCB routine", tp->pr_name);
638cf5e44f8SRuslan Ermilov 			return;
639cf5e44f8SRuslan Ermilov 		}
640feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
641feda1a43SJohn Baldwin 			off = 0;
642feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
643feda1a43SJohn Baldwin 			if (pflag)
644ade9ccfeSMarcel Moolenaar 				xo_message("%s: PCB routine doesn't work on "
645ade9ccfeSMarcel Moolenaar 				    "cores", tp->pr_name);
646feda1a43SJohn Baldwin 			return;
647feda1a43SJohn Baldwin 		} else
648feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6499b50d902SRodney W. Grimes 	}
650feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
651ade9ccfeSMarcel Moolenaar 	    af != AF_UNSPEC)) {
652ade9ccfeSMarcel Moolenaar 		if (doingdblocks && *first) {
653ade9ccfeSMarcel Moolenaar 			xo_open_list("socket");
654ade9ccfeSMarcel Moolenaar 			*first = false;
655ade9ccfeSMarcel Moolenaar 		}
656ade9ccfeSMarcel Moolenaar 
657feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
6589b50d902SRodney W. Grimes 	}
659ade9ccfeSMarcel Moolenaar }
6609b50d902SRodney W. Grimes 
66129dde48dSGleb Smirnoff static int
66229dde48dSGleb Smirnoff kvmd_init(void)
6639b50d902SRodney W. Grimes {
664feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
665feda1a43SJohn Baldwin 
66629dde48dSGleb Smirnoff 	if (kvmd != NULL)
66729dde48dSGleb Smirnoff 		return (0);
66829dde48dSGleb Smirnoff 
669feda1a43SJohn Baldwin 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
670a4a889a0SXin LI 	if (setgid(getgid()) != 0)
671a4a889a0SXin LI 		xo_err(-1, "setgid");
67229dde48dSGleb Smirnoff 
67329dde48dSGleb Smirnoff 	if (kvmd == NULL) {
674ade9ccfeSMarcel Moolenaar 		xo_warnx("kvm not available: %s", errbuf);
67529dde48dSGleb Smirnoff 		return (-1);
67629dde48dSGleb Smirnoff 	}
67729dde48dSGleb Smirnoff 
678fc47e028SAlexander V. Chernikov 	return (0);
679fc47e028SAlexander V. Chernikov }
680fc47e028SAlexander V. Chernikov 
681fc47e028SAlexander V. Chernikov /*
682fc47e028SAlexander V. Chernikov  * Resolve symbol list, return 0 on success.
683fc47e028SAlexander V. Chernikov  */
68481dacd8bSHiroki Sato static int
685fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl)
686fc47e028SAlexander V. Chernikov {
687fc47e028SAlexander V. Chernikov 
688fc47e028SAlexander V. Chernikov 	if ((kvmd == NULL) && (kvmd_init() != 0))
689fc47e028SAlexander V. Chernikov 		return (-1);
690fc47e028SAlexander V. Chernikov 
691fc47e028SAlexander V. Chernikov 	if (_nl[0].n_type != 0)
692fc47e028SAlexander V. Chernikov 		return (0);
693fc47e028SAlexander V. Chernikov 
694fc47e028SAlexander V. Chernikov 	if (kvm_nlist(kvmd, _nl) < 0) {
69599453c6aSPoul-Henning Kamp 		if (nlistf)
696ade9ccfeSMarcel Moolenaar 			xo_errx(1, "%s: kvm_nlist: %s", nlistf,
69799453c6aSPoul-Henning Kamp 			    kvm_geterr(kvmd));
69899453c6aSPoul-Henning Kamp 		else
699ade9ccfeSMarcel Moolenaar 			xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
70099453c6aSPoul-Henning Kamp 	}
70199453c6aSPoul-Henning Kamp 
70229dde48dSGleb Smirnoff 	return (0);
70329dde48dSGleb Smirnoff }
70429dde48dSGleb Smirnoff 
70529dde48dSGleb Smirnoff /*
70681dacd8bSHiroki Sato  * Wrapper of kvm_dpcpu_setcpu().
70781dacd8bSHiroki Sato  */
70881dacd8bSHiroki Sato void
70981dacd8bSHiroki Sato kset_dpcpu(u_int cpuid)
71081dacd8bSHiroki Sato {
71181dacd8bSHiroki Sato 
71281dacd8bSHiroki Sato 	if ((kvmd == NULL) && (kvmd_init() != 0))
71381dacd8bSHiroki Sato 		xo_errx(-1, "%s: kvm is not available", __func__);
71481dacd8bSHiroki Sato 
71581dacd8bSHiroki Sato 	if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
71681dacd8bSHiroki Sato 		xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
71781dacd8bSHiroki Sato 		    cpuid, kvm_geterr(kvmd));
71881dacd8bSHiroki Sato 	return;
71981dacd8bSHiroki Sato }
72081dacd8bSHiroki Sato 
72181dacd8bSHiroki Sato /*
72229dde48dSGleb Smirnoff  * Read kernel memory, return 0 on success.
72329dde48dSGleb Smirnoff  */
72429dde48dSGleb Smirnoff int
72529dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size)
72629dde48dSGleb Smirnoff {
72729dde48dSGleb Smirnoff 
72829dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
72999453c6aSPoul-Henning Kamp 		return (-1);
73029dde48dSGleb Smirnoff 
731c6620a13SPoul-Henning Kamp 	if (!buf)
732c6620a13SPoul-Henning Kamp 		return (0);
733feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
734ade9ccfeSMarcel Moolenaar 		xo_warnx("%s", kvm_geterr(kvmd));
7359b50d902SRodney W. Grimes 		return (-1);
7369b50d902SRodney W. Grimes 	}
7379b50d902SRodney W. Grimes 	return (0);
7389b50d902SRodney W. Grimes }
7399b50d902SRodney W. Grimes 
74029dde48dSGleb Smirnoff /*
741e3a7aa6fSGleb Smirnoff  * Read single counter(9).
742e3a7aa6fSGleb Smirnoff  */
743e3a7aa6fSGleb Smirnoff uint64_t
744e3a7aa6fSGleb Smirnoff kread_counter(u_long addr)
745e3a7aa6fSGleb Smirnoff {
746e3a7aa6fSGleb Smirnoff 
747e3a7aa6fSGleb Smirnoff 	if (kvmd_init() < 0)
748e3a7aa6fSGleb Smirnoff 		return (-1);
749e3a7aa6fSGleb Smirnoff 
750e3a7aa6fSGleb Smirnoff 	return (kvm_counter_u64_fetch(kvmd, addr));
751e3a7aa6fSGleb Smirnoff }
752e3a7aa6fSGleb Smirnoff 
753e3a7aa6fSGleb Smirnoff /*
75429dde48dSGleb Smirnoff  * Read an array of N counters in kernel memory into array of N uint64_t's.
75529dde48dSGleb Smirnoff  */
75629dde48dSGleb Smirnoff int
7575da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size)
75829dde48dSGleb Smirnoff {
759794c57d3SMark Johnston 	uint64_t *c;
760794c57d3SMark Johnston 	u_long *counters;
761794c57d3SMark Johnston 	size_t i, n;
76229dde48dSGleb Smirnoff 
76329dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
76429dde48dSGleb Smirnoff 		return (-1);
76529dde48dSGleb Smirnoff 
766794c57d3SMark Johnston 	if (size % sizeof(uint64_t) != 0) {
767794c57d3SMark Johnston 		xo_warnx("kread_counters: invalid counter set size");
7685da0521fSAndrey V. Elsukov 		return (-1);
7695da0521fSAndrey V. Elsukov 	}
770794c57d3SMark Johnston 
771794c57d3SMark Johnston 	n = size / sizeof(uint64_t);
772794c57d3SMark Johnston 	if ((counters = malloc(n * sizeof(u_long))) == NULL)
773794c57d3SMark Johnston 		xo_err(-1, "malloc");
774794c57d3SMark Johnston 	if (kread(addr, counters, n * sizeof(u_long)) < 0) {
775794c57d3SMark Johnston 		free(counters);
776794c57d3SMark Johnston 		return (-1);
777794c57d3SMark Johnston 	}
778794c57d3SMark Johnston 
779794c57d3SMark Johnston 	c = buf;
780794c57d3SMark Johnston 	for (i = 0; i < n; i++)
781794c57d3SMark Johnston 		c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
782794c57d3SMark Johnston 
783794c57d3SMark Johnston 	free(counters);
78429dde48dSGleb Smirnoff 	return (0);
78529dde48dSGleb Smirnoff }
78629dde48dSGleb Smirnoff 
787a01e3379SDavid Malone const char *
7887b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
7899b50d902SRodney W. Grimes {
7909b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7919b50d902SRodney W. Grimes }
7929b50d902SRodney W. Grimes 
793a01e3379SDavid Malone const char *
7947b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
7959b50d902SRodney W. Grimes {
7969b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7979b50d902SRodney W. Grimes }
7989b50d902SRodney W. Grimes 
799f99a4046SMike Makonnen const char *
8007b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
801f99a4046SMike Makonnen {
802f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
803f99a4046SMike Makonnen }
804f99a4046SMike Makonnen 
8059b50d902SRodney W. Grimes /*
8069b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
8079b50d902SRodney W. Grimes  */
8089b50d902SRodney W. Grimes static struct protox *
809096146f8SYaroslav Tykhiy knownname(const char *name)
8109b50d902SRodney W. Grimes {
8119b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
8129b50d902SRodney W. Grimes 
8139b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
8149b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
8159b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
8169b50d902SRodney W. Grimes 				return (tp);
8179b50d902SRodney W. Grimes 	return (NULL);
8189b50d902SRodney W. Grimes }
8199b50d902SRodney W. Grimes 
8209b50d902SRodney W. Grimes /*
8219b50d902SRodney W. Grimes  * Find the protox corresponding to name.
8229b50d902SRodney W. Grimes  */
8239b50d902SRodney W. Grimes static struct protox *
824096146f8SYaroslav Tykhiy name2protox(const char *name)
8259b50d902SRodney W. Grimes {
8269b50d902SRodney W. Grimes 	struct protox *tp;
8279b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
8289b50d902SRodney W. Grimes 	struct protoent *p;
8299b50d902SRodney W. Grimes 
8309b50d902SRodney W. Grimes 	/*
8319b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
8329b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
8339b50d902SRodney W. Grimes 	 */
834cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
8359b50d902SRodney W. Grimes 		return (tp);
8369b50d902SRodney W. Grimes 
8379b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
838cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
8399b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
8409b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
8419b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
8429b50d902SRodney W. Grimes 				endprotoent();
8439b50d902SRodney W. Grimes 				return (knownname(p->p_name));
8449b50d902SRodney W. Grimes 			}
8459b50d902SRodney W. Grimes 	}
8469b50d902SRodney W. Grimes 	endprotoent();
8479b50d902SRodney W. Grimes 	return (NULL);
8489b50d902SRodney W. Grimes }
8499b50d902SRodney W. Grimes 
8509b50d902SRodney W. Grimes static void
8515e051718SAssar Westerlund usage(void)
8529b50d902SRodney W. Grimes {
853ade9ccfeSMarcel Moolenaar 	(void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
85485b0f0f3SAdrian Chadd "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
8551cb467b1SRuslan Ermilov "               [-M core] [-N system]",
85617ed2e8eSAlexander V. Chernikov "       netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
857d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
858fd2c6bc9SAllan Jude "       netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
859fd2c6bc9SAllan Jude "               [-q howmany]",
860fd2c6bc9SAllan Jude "       netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
86182d383bcSRuslan Ermilov "               [-M core] [-N system]",
862fd2c6bc9SAllan Jude "       netstat -i | -I interface -s [-46s]\n"
863fd2c6bc9SAllan Jude "               [-f protocol_family | -p protocol] [-M core] [-N system]",
86449c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
865fd2c6bc9SAllan Jude "       netstat -B [-z] [-I interface]",
866fd2c6bc9SAllan Jude "       netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
867fd2c6bc9SAllan Jude "               [-M core] [-N system]",
8681cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
86917ed2e8eSAlexander V. Chernikov "       netstat -g [-46W] [-f address_family] [-M core] [-N system]",
87017ed2e8eSAlexander V. Chernikov "       netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
8710153eb66SRobert Watson "       netstat -Q");
872ade9ccfeSMarcel Moolenaar 	xo_finish();
8739b50d902SRodney W. Grimes 	exit(1);
8749b50d902SRodney W. Grimes }
875