xref: /freebsd/usr.bin/netstat/main.c (revision b98a21f64ab05014988abf7f375fceb61b5cd2e6)
165475bc8SDavid E. O'Brien /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
59b50d902SRodney W. Grimes  *	Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #ifndef lint
3381dacd8bSHiroki Sato static char const copyright[] =
349b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\
359b50d902SRodney W. Grimes 	Regents of the University of California.  All rights reserved.\n";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
385d422d6aSPhilippe Charnier #if 0
396cc6f122SPhilippe Charnier #ifndef lint
409b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
419b50d902SRodney W. Grimes #endif /* not lint */
426cc6f122SPhilippe Charnier #endif
436cc6f122SPhilippe Charnier 
446cc6f122SPhilippe Charnier #include <sys/cdefs.h>
456cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <sys/param.h>
489b50d902SRodney W. Grimes #include <sys/file.h>
499b50d902SRodney W. Grimes #include <sys/protosw.h>
509b50d902SRodney W. Grimes #include <sys/socket.h>
51feda1a43SJohn Baldwin #include <sys/socketvar.h>
529eddb899SMark Johnston #include <sys/sysctl.h>
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes #include <netinet/in.h>
559b50d902SRodney W. Grimes 
56690f477dSSam Leffler #ifdef NETGRAPH
574cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
58690f477dSSam Leffler #endif
594cf49a43SJulian Elischer 
609b50d902SRodney W. Grimes #include <ctype.h>
615d422d6aSPhilippe Charnier #include <err.h>
62821df508SXin LI #include <errno.h>
639b50d902SRodney W. Grimes #include <kvm.h>
649b50d902SRodney W. Grimes #include <limits.h>
659b50d902SRodney W. Grimes #include <netdb.h>
669b50d902SRodney W. Grimes #include <nlist.h>
67821df508SXin LI #include <paths.h>
687b95a1ebSYaroslav Tykhiy #include <stdint.h>
699b50d902SRodney W. Grimes #include <stdio.h>
709b50d902SRodney W. Grimes #include <stdlib.h>
71ade9ccfeSMarcel Moolenaar #include <stdbool.h>
729b50d902SRodney W. Grimes #include <string.h>
739b50d902SRodney W. Grimes #include <unistd.h>
749b50d902SRodney W. Grimes #include "netstat.h"
7581dacd8bSHiroki Sato #include "nl_defs.h"
76ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
779b50d902SRodney W. Grimes 
7881dacd8bSHiroki Sato static struct protox {
79feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
80feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
819b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
82feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
835e051718SAssar Westerlund 					/* control blocks printing routine */
84feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
855e051718SAssar Westerlund 					/* statistics printing routine */
865e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
87fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
8855fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
89feda1a43SJohn Baldwin 	int	pr_protocol;
909b50d902SRodney W. Grimes } protox[] = {
91feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
92feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
93feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
94feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
9574fd40c9SRandall Stewart #ifdef SCTP
96feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
97feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
9874fd40c9SRandall Stewart #endif
99aa0a1e58SJeff Roberson #ifdef SDP
100aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
101aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
102aa0a1e58SJeff Roberson #endif
103feda1a43SJohn Baldwin 	{ N_DIVCBINFO,	-1,		1,	protopr,
104feda1a43SJohn Baldwin 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
105feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
106feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
107feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
108feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
109feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
110feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
111cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
11281dacd8bSHiroki Sato 	{ -1,		N_IPSEC4STAT,	1,	NULL,	/* keep as compat */
1139d2d8e7bSGeorge V. Neville-Neil 	  ipsec_stats,	NULL,		"ipsec", 1,	0},
1148409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
1159d2d8e7bSGeorge V. Neville-Neil 	  ah_stats,	NULL,		"ah",	1,	0},
1168409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
1179d2d8e7bSGeorge V. Neville-Neil 	  esp_stats,	NULL,		"esp",	1,	0},
1188409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
1199d2d8e7bSGeorge V. Neville-Neil 	  ipcomp_stats,	NULL,		"ipcomp", 1,	0},
120100b98dbSKelly Yancey #endif
121feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
122feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
12381dacd8bSHiroki Sato 	{ -1,		N_CARPSTATS,	1,	NULL,
124feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
1253e4d5cd3SGleb Smirnoff #ifdef PF
12681dacd8bSHiroki Sato 	{ -1,		N_PFSYNCSTATS,	1,	NULL,
127feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
1283e4d5cd3SGleb Smirnoff #endif
12954fc657dSGeorge V. Neville-Neil 	{ -1,		N_ARPSTAT,	1,	NULL,
13054fc657dSGeorge V. Neville-Neil 	  arp_stats,	NULL,		"arp", 1,	0 },
1316bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
132feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1339b50d902SRodney W. Grimes };
1349b50d902SRodney W. Grimes 
135cfa1ca9dSYoshinobu Inoue #ifdef INET6
13681dacd8bSHiroki Sato static struct protox ip6protox[] = {
137feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
138feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
139feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
140feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
141feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
142feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
143feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
144feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
145aa0a1e58SJeff Roberson #ifdef SDP
146aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
147aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
148aa0a1e58SJeff Roberson #endif
149cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
1506bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
1519d2d8e7bSGeorge V. Neville-Neil 	  ipsec_stats,	NULL,		"ipsec6", 1,	0 },
152cfa1ca9dSYoshinobu Inoue #endif
153cfa1ca9dSYoshinobu Inoue #ifdef notyet
1546bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
155feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
156cfa1ca9dSYoshinobu Inoue #endif
157feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
158feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
1596bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
160feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
161cfa1ca9dSYoshinobu Inoue };
162cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
163cfa1ca9dSYoshinobu Inoue 
1643b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
16581dacd8bSHiroki Sato static struct protox pfkeyprotox[] = {
1666bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
167feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
1686bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
169feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1703b8a8567SJun-ichiro itojun Hagino };
1713b8a8567SJun-ichiro itojun Hagino #endif
1723b8a8567SJun-ichiro itojun Hagino 
173690f477dSSam Leffler #ifdef NETGRAPH
17481dacd8bSHiroki Sato static struct protox netgraphprotox[] = {
17581dacd8bSHiroki Sato 	{ N_NGSOCKLIST,	-1,		1,	netgraphprotopr,
176feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
17781dacd8bSHiroki Sato 	{ N_NGSOCKLIST,	-1,		1,	netgraphprotopr,
178feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
1796bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
180feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1814cf49a43SJulian Elischer };
182690f477dSSam Leffler #endif
183cc6a66f2SJulian Elischer 
18481dacd8bSHiroki Sato static struct protox *protoprotox[] = {
185cfa1ca9dSYoshinobu Inoue 					 protox,
186cfa1ca9dSYoshinobu Inoue #ifdef INET6
187cfa1ca9dSYoshinobu Inoue 					 ip6protox,
188cfa1ca9dSYoshinobu Inoue #endif
1893b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
1903b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
1913b8a8567SJun-ichiro itojun Hagino #endif
19245c203fcSGleb Smirnoff 					 NULL };
1939b50d902SRodney W. Grimes 
194ade9ccfeSMarcel Moolenaar static void printproto(struct protox *, const char *, bool *);
1955e051718SAssar Westerlund static void usage(void);
196096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
197096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
1989b50d902SRodney W. Grimes 
19981dacd8bSHiroki Sato static int kresolve_list(struct nlist *_nl);
20081dacd8bSHiroki Sato 
201c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
202080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
203080b7f49SDag-Erling Smørgrav 
204080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
205080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
20681dacd8bSHiroki Sato static int	Bflag;		/* show information about bpf consumers */
207080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
208*b98a21f6SMichael Tuexen int	Cflag;		/* show congestion control */
209080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
210080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
211c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
212080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
213080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
214080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
215bf10ffe1SXin LI int	noutputs = 0;	/* how much outputs before we exit */
21665ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
21765ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
218a6663252SAlexander V. Chernikov int	oflag;		/* show nexthop objects*/
2192529f56eSJonathan T. Looney int	Pflag;		/* show TCP log ID */
220cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
22181dacd8bSHiroki Sato static int	Qflag;		/* show netisr information */
222080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
22385b0f0f3SAdrian Chadd int	Rflag;		/* show flow / RSS statistics */
224080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
225080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
226f5d34df5SGeorge V. Neville-Neil int	Tflag;		/* TCP Information */
22749f287f8SGeorge V. Neville-Neil int	xflag;		/* extra information, includes all socket buffer info */
228c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
229080b7f49SDag-Erling Smørgrav 
230080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
231080b7f49SDag-Erling Smørgrav 
232080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
233080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
234080b7f49SDag-Erling Smørgrav 
23581dacd8bSHiroki Sato static int	af;		/* address family */
236feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes int
239a01e3379SDavid Malone main(int argc, char *argv[])
2409b50d902SRodney W. Grimes {
241a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
2429b50d902SRodney W. Grimes 	int ch;
2433fddef95SHiroki Sato 	int fib = -1;
2443fddef95SHiroki Sato 	char *endptr;
245ade9ccfeSMarcel Moolenaar 	bool first = true;
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes 	af = AF_UNSPEC;
2489b50d902SRodney W. Grimes 
249ade9ccfeSMarcel Moolenaar 	argc = xo_parse_args(argc, argv);
25006ff7ccbSHajimu UMEMOTO 	if (argc < 0)
25106ff7ccbSHajimu UMEMOTO 		exit(EXIT_FAILURE);
252ade9ccfeSMarcel Moolenaar 
253*b98a21f6SMichael Tuexen 	while ((ch = getopt(argc, argv, "46AaBbCdF:f:ghI:iLlM:mN:noPp:Qq:RrSTsuWw:xz"))
2540153eb66SRobert Watson 	    != -1)
2559b50d902SRodney W. Grimes 		switch(ch) {
25617ed2e8eSAlexander V. Chernikov 		case '4':
25717ed2e8eSAlexander V. Chernikov #ifdef INET
25817ed2e8eSAlexander V. Chernikov 			af = AF_INET;
25917ed2e8eSAlexander V. Chernikov #else
26017ed2e8eSAlexander V. Chernikov 			errx(1, "IPv4 support is not compiled in");
26117ed2e8eSAlexander V. Chernikov #endif
26217ed2e8eSAlexander V. Chernikov 			break;
26317ed2e8eSAlexander V. Chernikov 		case '6':
26417ed2e8eSAlexander V. Chernikov #ifdef INET6
26517ed2e8eSAlexander V. Chernikov 			af = AF_INET6;
26617ed2e8eSAlexander V. Chernikov #else
26717ed2e8eSAlexander V. Chernikov 			errx(1, "IPv6 support is not compiled in");
26817ed2e8eSAlexander V. Chernikov #endif
26917ed2e8eSAlexander V. Chernikov 			break;
2709b50d902SRodney W. Grimes 		case 'A':
2719b50d902SRodney W. Grimes 			Aflag = 1;
2729b50d902SRodney W. Grimes 			break;
2739b50d902SRodney W. Grimes 		case 'a':
2749b50d902SRodney W. Grimes 			aflag = 1;
2759b50d902SRodney W. Grimes 			break;
2766b463eedSChristian S.J. Peron 		case 'B':
2776b463eedSChristian S.J. Peron 			Bflag = 1;
2786b463eedSChristian S.J. Peron 			break;
279e1e293a5SDavid Greenman 		case 'b':
280e1e293a5SDavid Greenman 			bflag = 1;
281e1e293a5SDavid Greenman 			break;
282*b98a21f6SMichael Tuexen 		case 'C':
283*b98a21f6SMichael Tuexen 			Cflag = 1;
284*b98a21f6SMichael Tuexen 			break;
2859b50d902SRodney W. Grimes 		case 'd':
2869b50d902SRodney W. Grimes 			dflag = 1;
2879b50d902SRodney W. Grimes 			break;
2883fddef95SHiroki Sato 		case 'F':
2893fddef95SHiroki Sato 			fib = strtol(optarg, &endptr, 0);
2903fddef95SHiroki Sato 			if (*endptr != '\0' ||
2913fddef95SHiroki Sato 			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
292ade9ccfeSMarcel Moolenaar 				xo_errx(1, "%s: invalid fib", optarg);
2933fddef95SHiroki Sato 			break;
2949b50d902SRodney W. Grimes 		case 'f':
2952c284d93SGleb Smirnoff 			if (strcmp(optarg, "inet") == 0)
2969b50d902SRodney W. Grimes 				af = AF_INET;
297cfa1ca9dSYoshinobu Inoue #ifdef INET6
298cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
299cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
300e5134d2eSMax Laier #endif
301e5134d2eSMax Laier #ifdef IPSEC
3023b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3033b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
304e5134d2eSMax Laier #endif
30510d5269fSHiroki Sato 			else if (strcmp(optarg, "unix") == 0 ||
30610d5269fSHiroki Sato 				 strcmp(optarg, "local") == 0)
3079b50d902SRodney W. Grimes 				af = AF_UNIX;
308690f477dSSam Leffler #ifdef NETGRAPH
3094cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3104cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3114cf49a43SJulian Elischer 				af = AF_NETGRAPH;
312690f477dSSam Leffler #endif
313d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
314d44ddba9SRuslan Ermilov 				af = AF_LINK;
3159b50d902SRodney W. Grimes 			else {
316ade9ccfeSMarcel Moolenaar 				xo_errx(1, "%s: unknown address family",
317ade9ccfeSMarcel Moolenaar 				    optarg);
3189b50d902SRodney W. Grimes 			}
3199b50d902SRodney W. Grimes 			break;
3209b50d902SRodney W. Grimes 		case 'g':
3219b50d902SRodney W. Grimes 			gflag = 1;
3229b50d902SRodney W. Grimes 			break;
323c2dfd19fSGleb Smirnoff 		case 'h':
324c2dfd19fSGleb Smirnoff 			hflag = 1;
325c2dfd19fSGleb Smirnoff 			break;
3269b50d902SRodney W. Grimes 		case 'I': {
3279b50d902SRodney W. Grimes 			char *cp;
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes 			iflag = 1;
3309b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3319b50d902SRodney W. Grimes 				continue;
3329b50d902SRodney W. Grimes 			unit = atoi(cp);
3339b50d902SRodney W. Grimes 			break;
3349b50d902SRodney W. Grimes 		}
3359b50d902SRodney W. Grimes 		case 'i':
3369b50d902SRodney W. Grimes 			iflag = 1;
3379b50d902SRodney W. Grimes 			break;
338ac55add0SGuido van Rooij 		case 'L':
339ac55add0SGuido van Rooij 			Lflag = 1;
340ac55add0SGuido van Rooij 			break;
3419b50d902SRodney W. Grimes 		case 'M':
3429b50d902SRodney W. Grimes 			memf = optarg;
3439b50d902SRodney W. Grimes 			break;
3449b50d902SRodney W. Grimes 		case 'm':
3459b50d902SRodney W. Grimes 			mflag = 1;
3469b50d902SRodney W. Grimes 			break;
3479b50d902SRodney W. Grimes 		case 'N':
3489b50d902SRodney W. Grimes 			nlistf = optarg;
3499b50d902SRodney W. Grimes 			break;
3509b50d902SRodney W. Grimes 		case 'n':
35165ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
3529b50d902SRodney W. Grimes 			break;
353a6663252SAlexander V. Chernikov 		case 'o':
354a6663252SAlexander V. Chernikov 			oflag = 1;
355a6663252SAlexander V. Chernikov 			break;
3562529f56eSJonathan T. Looney 		case 'P':
3572529f56eSJonathan T. Looney 			Pflag = 1;
3582529f56eSJonathan T. Looney 			break;
3599b50d902SRodney W. Grimes 		case 'p':
3609b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
361ade9ccfeSMarcel Moolenaar 				xo_errx(1, "%s: unknown or uninstrumented "
362ade9ccfeSMarcel Moolenaar 				    "protocol", optarg);
3639b50d902SRodney W. Grimes 			}
3649b50d902SRodney W. Grimes 			pflag = 1;
3659b50d902SRodney W. Grimes 			break;
3660153eb66SRobert Watson 		case 'Q':
3670153eb66SRobert Watson 			Qflag = 1;
3680153eb66SRobert Watson 			break;
369bf10ffe1SXin LI 		case 'q':
370bf10ffe1SXin LI 			noutputs = atoi(optarg);
371bf10ffe1SXin LI 			if (noutputs != 0)
372bf10ffe1SXin LI 				noutputs++;
373bf10ffe1SXin LI 			break;
3749b50d902SRodney W. Grimes 		case 'r':
3759b50d902SRodney W. Grimes 			rflag = 1;
3769b50d902SRodney W. Grimes 			break;
37785b0f0f3SAdrian Chadd 		case 'R':
37885b0f0f3SAdrian Chadd 			Rflag = 1;
37985b0f0f3SAdrian Chadd 			break;
3809b50d902SRodney W. Grimes 		case 's':
3819b50d902SRodney W. Grimes 			++sflag;
3829b50d902SRodney W. Grimes 			break;
38365ea0024SAssar Westerlund 		case 'S':
38465ea0024SAssar Westerlund 			numeric_addr = 1;
38565ea0024SAssar Westerlund 			break;
3869b50d902SRodney W. Grimes 		case 'u':
3879b50d902SRodney W. Grimes 			af = AF_UNIX;
3889b50d902SRodney W. Grimes 			break;
389080b7f49SDag-Erling Smørgrav 		case 'W':
39070057abfSRuslan Ermilov 		case 'l':
391080b7f49SDag-Erling Smørgrav 			Wflag = 1;
392080b7f49SDag-Erling Smørgrav 			break;
3939b50d902SRodney W. Grimes 		case 'w':
3949b50d902SRodney W. Grimes 			interval = atoi(optarg);
3959b50d902SRodney W. Grimes 			iflag = 1;
3969b50d902SRodney W. Grimes 			break;
397f5d34df5SGeorge V. Neville-Neil 		case 'T':
398f5d34df5SGeorge V. Neville-Neil 			Tflag = 1;
399f5d34df5SGeorge V. Neville-Neil 			break;
40049f287f8SGeorge V. Neville-Neil 		case 'x':
40149f287f8SGeorge V. Neville-Neil 			xflag = 1;
40249f287f8SGeorge V. Neville-Neil 			break;
403c73d99b5SRuslan Ermilov 		case 'z':
404c73d99b5SRuslan Ermilov 			zflag = 1;
405c73d99b5SRuslan Ermilov 			break;
4069b50d902SRodney W. Grimes 		case '?':
4079b50d902SRodney W. Grimes 		default:
4089b50d902SRodney W. Grimes 			usage();
4099b50d902SRodney W. Grimes 		}
4109b50d902SRodney W. Grimes 	argv += optind;
4119b50d902SRodney W. Grimes 	argc -= optind;
4129b50d902SRodney W. Grimes 
4139b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4149b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4159b50d902SRodney W. Grimes 	if (*argv) {
4169b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4179b50d902SRodney W. Grimes 			interval = atoi(*argv);
4189b50d902SRodney W. Grimes 			if (interval <= 0)
4199b50d902SRodney W. Grimes 				usage();
4209b50d902SRodney W. Grimes 			++argv;
4219b50d902SRodney W. Grimes 			iflag = 1;
4229b50d902SRodney W. Grimes 		}
4239b50d902SRodney W. Grimes 		if (*argv) {
4249b50d902SRodney W. Grimes 			nlistf = *argv;
4259b50d902SRodney W. Grimes 			if (*++argv)
4269b50d902SRodney W. Grimes 				memf = *argv;
4279b50d902SRodney W. Grimes 		}
4289b50d902SRodney W. Grimes 	}
4299b50d902SRodney W. Grimes #endif
4309b50d902SRodney W. Grimes 
4319b50d902SRodney W. Grimes 	/*
4329b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4339b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4349b50d902SRodney W. Grimes 	 */
435feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
436a4a889a0SXin LI 	if (!live) {
437a4a889a0SXin LI 		if (setgid(getgid()) != 0)
438a4a889a0SXin LI 			xo_err(-1, "setgid");
4391caaf3eaSBaptiste Daroussin 		/* Load all necessary kvm symbols */
4401caaf3eaSBaptiste Daroussin 		kresolve_list(nl);
441a4a889a0SXin LI 	}
4429b50d902SRodney W. Grimes 
443f5d34df5SGeorge V. Neville-Neil 	if (xflag && Tflag)
444ade9ccfeSMarcel Moolenaar 		xo_errx(1, "-x and -T are incompatible, pick one.");
445f5d34df5SGeorge V. Neville-Neil 
4466b463eedSChristian S.J. Peron 	if (Bflag) {
447feda1a43SJohn Baldwin 		if (!live)
448feda1a43SJohn Baldwin 			usage();
4496b463eedSChristian S.J. Peron 		bpf_stats(interface);
450ade9ccfeSMarcel Moolenaar 		xo_finish();
4516b463eedSChristian S.J. Peron 		exit(0);
4526b463eedSChristian S.J. Peron 	}
4539b50d902SRodney W. Grimes 	if (mflag) {
45483708764SRuslan Ermilov 		if (!live) {
455feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
45605d1f5bcSAndrey V. Elsukov 				mbpr(kvmd, nl[N_SFSTAT].n_value);
457d15c5f56SRuslan Ermilov 		} else
458d4426f28SRobert Watson 			mbpr(NULL, 0);
459ade9ccfeSMarcel Moolenaar 		xo_finish();
4609b50d902SRodney W. Grimes 		exit(0);
4619b50d902SRodney W. Grimes 	}
4620153eb66SRobert Watson 	if (Qflag) {
46388737be2SRobert Watson 		if (!live) {
46488737be2SRobert Watson 			if (kread(0, NULL, 0) == 0)
46581dacd8bSHiroki Sato 				netisr_stats();
46688737be2SRobert Watson 		} else
46781dacd8bSHiroki Sato 			netisr_stats();
468ade9ccfeSMarcel Moolenaar 		xo_finish();
4690153eb66SRobert Watson 		exit(0);
4700153eb66SRobert Watson 	}
471cc63cd56SPeter Wemm #if 0
4729b50d902SRodney W. Grimes 	/*
4739b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
4749b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
4759b50d902SRodney W. Grimes 	 */
4769b50d902SRodney W. Grimes 	sethostent(1);
4779b50d902SRodney W. Grimes 	setnetent(1);
478cc63cd56SPeter Wemm #else
479cc63cd56SPeter Wemm 	/*
480cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
481cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
482cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
483cc63cd56SPeter Wemm 	 */
484cc63cd56SPeter Wemm #endif
485cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
486ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
48710d5269fSHiroki Sato 		intpr(NULL, af);
488ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
489ade9ccfeSMarcel Moolenaar 		xo_finish();
4909b50d902SRodney W. Grimes 		exit(0);
4919b50d902SRodney W. Grimes 	}
4929b50d902SRodney W. Grimes 	if (rflag) {
493ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
4945d6d7e75SGleb Smirnoff 		if (sflag) {
4952ccdd4b3SBaptiste Daroussin 			if (live) {
4962ccdd4b3SBaptiste Daroussin 				kresolve_list(nl);
4972ccdd4b3SBaptiste Daroussin 			}
498fc47e028SAlexander V. Chernikov 			rt_stats();
4995d6d7e75SGleb Smirnoff 		} else
500fc47e028SAlexander V. Chernikov 			routepr(fib, af);
501ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
502ade9ccfeSMarcel Moolenaar 		xo_finish();
5039b50d902SRodney W. Grimes 		exit(0);
5049b50d902SRodney W. Grimes 	}
505a6663252SAlexander V. Chernikov 	if (oflag) {
506a6663252SAlexander V. Chernikov 		xo_open_container("statistics");
507a6663252SAlexander V. Chernikov 		nhops_print(fib, af);
508a6663252SAlexander V. Chernikov 		xo_close_container("statistics");
509a6663252SAlexander V. Chernikov 		xo_finish();
510a6663252SAlexander V. Chernikov 		exit(0);
511a6663252SAlexander V. Chernikov 	}
512a6663252SAlexander V. Chernikov 
513fc47e028SAlexander V. Chernikov 
5149b50d902SRodney W. Grimes 	if (gflag) {
515ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
516cfa1ca9dSYoshinobu Inoue 		if (sflag) {
517cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
518fc47e028SAlexander V. Chernikov 				mrt_stats();
519cfa1ca9dSYoshinobu Inoue #ifdef INET6
520cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
521fc47e028SAlexander V. Chernikov 				mrt6_stats();
522cfa1ca9dSYoshinobu Inoue #endif
523cfa1ca9dSYoshinobu Inoue 		} else {
524cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
525fc47e028SAlexander V. Chernikov 				mroutepr();
526cfa1ca9dSYoshinobu Inoue #ifdef INET6
527cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
528fc47e028SAlexander V. Chernikov 				mroute6pr();
529cfa1ca9dSYoshinobu Inoue #endif
530cfa1ca9dSYoshinobu Inoue 		}
531ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
532ade9ccfeSMarcel Moolenaar 		xo_finish();
5339b50d902SRodney W. Grimes 		exit(0);
5349b50d902SRodney W. Grimes 	}
535cfa1ca9dSYoshinobu Inoue 
536cf5e44f8SRuslan Ermilov 	if (tp) {
537ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
538ade9ccfeSMarcel Moolenaar 		printproto(tp, tp->pr_name, &first);
539ade9ccfeSMarcel Moolenaar 		if (!first)
540ade9ccfeSMarcel Moolenaar 			xo_close_list("socket");
541ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
542ade9ccfeSMarcel Moolenaar 		xo_finish();
543cf5e44f8SRuslan Ermilov 		exit(0);
544cf5e44f8SRuslan Ermilov 	}
545ade9ccfeSMarcel Moolenaar 
546ade9ccfeSMarcel Moolenaar 	xo_open_container("statistics");
547cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5489b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
549ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
550cfa1ca9dSYoshinobu Inoue #ifdef INET6
551cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
552cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
553ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
554cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5553b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5563b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5573b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
558ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
5593b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
560690f477dSSam Leffler #ifdef NETGRAPH
5614cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5624cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
563ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
564690f477dSSam Leffler #endif /* NETGRAPH */
565f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
566feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
567963b7ccdSRobert Watson 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
568ade9ccfeSMarcel Moolenaar 		    nl[N_UNP_SPHEAD].n_value, &first);
569ade9ccfeSMarcel Moolenaar 
570ade9ccfeSMarcel Moolenaar 	if (!first)
571ade9ccfeSMarcel Moolenaar 		xo_close_list("socket");
572ade9ccfeSMarcel Moolenaar 	xo_close_container("statistics");
573ade9ccfeSMarcel Moolenaar 	xo_finish();
5749b50d902SRodney W. Grimes 	exit(0);
5759b50d902SRodney W. Grimes }
5769b50d902SRodney W. Grimes 
577dbfd8708SGleb Smirnoff static int
578dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
579dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn, int zero)
5809eddb899SMark Johnston {
5819eddb899SMark Johnston 	int error;
5829eddb899SMark Johnston 
5839eddb899SMark Johnston 	if (live) {
5849eddb899SMark Johnston 		memset(stats, 0, len);
585dbfd8708SGleb Smirnoff 		if (zero)
5869eddb899SMark Johnston 			error = sysctlbyname(sysctlname, NULL, NULL, stats,
5879eddb899SMark Johnston 			    len);
5889eddb899SMark Johnston 		else
5899eddb899SMark Johnston 			error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
5909eddb899SMark Johnston 		if (error == -1 && errno != ENOENT)
5919eddb899SMark Johnston 			xo_warn("sysctl %s", sysctlname);
5929eddb899SMark Johnston 	} else {
5939eddb899SMark Johnston 		if (off == 0)
5949eddb899SMark Johnston 			return (1);
5959eddb899SMark Johnston 		error = kreadfn(off, stats, len);
5969eddb899SMark Johnston 	}
5979eddb899SMark Johnston 	return (error);
5989eddb899SMark Johnston }
5999eddb899SMark Johnston 
600dbfd8708SGleb Smirnoff int
601dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats,
602dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
603dbfd8708SGleb Smirnoff {
604dbfd8708SGleb Smirnoff 
605dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
606dbfd8708SGleb Smirnoff     zflag));
607dbfd8708SGleb Smirnoff }
608dbfd8708SGleb Smirnoff 
609dbfd8708SGleb Smirnoff int
610dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
611dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
612dbfd8708SGleb Smirnoff {
613dbfd8708SGleb Smirnoff 
614dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
615dbfd8708SGleb Smirnoff }
616dbfd8708SGleb Smirnoff 
6179b50d902SRodney W. Grimes /*
6189b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6199b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6209b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6219b50d902SRodney W. Grimes  */
6229b50d902SRodney W. Grimes static void
623ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first)
6249b50d902SRodney W. Grimes {
625feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6269b50d902SRodney W. Grimes 	u_long off;
627ade9ccfeSMarcel Moolenaar 	bool doingdblocks = false;
6289b50d902SRodney W. Grimes 
6299b50d902SRodney W. Grimes 	if (sflag) {
630cfa1ca9dSYoshinobu Inoue 		if (iflag) {
631cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
63210d5269fSHiroki Sato 				intpr(tp->pr_istats, af);
633cf5e44f8SRuslan Ermilov 			else if (pflag)
634ade9ccfeSMarcel Moolenaar 				xo_message("%s: no per-interface stats routine",
635cf5e44f8SRuslan Ermilov 				    tp->pr_name);
636cfa1ca9dSYoshinobu Inoue 			return;
637feda1a43SJohn Baldwin 		} else {
6389b50d902SRodney W. Grimes 			pr = tp->pr_stats;
639cf5e44f8SRuslan Ermilov 			if (!pr) {
640cf5e44f8SRuslan Ermilov 				if (pflag)
641ade9ccfeSMarcel Moolenaar 					xo_message("%s: no stats routine",
642cf5e44f8SRuslan Ermilov 					    tp->pr_name);
643cf5e44f8SRuslan Ermilov 				return;
644cf5e44f8SRuslan Ermilov 			}
645feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
646feda1a43SJohn Baldwin 				off = 0;
647feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
648feda1a43SJohn Baldwin 				if (pflag)
649ade9ccfeSMarcel Moolenaar 					xo_message("%s: stats routine doesn't "
650ade9ccfeSMarcel Moolenaar 					    "work on cores", tp->pr_name);
651feda1a43SJohn Baldwin 				return;
652feda1a43SJohn Baldwin 			} else
653feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
654cfa1ca9dSYoshinobu Inoue 		}
6559b50d902SRodney W. Grimes 	} else {
656ade9ccfeSMarcel Moolenaar 		doingdblocks = true;
6579b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
658cf5e44f8SRuslan Ermilov 		if (!pr) {
659cf5e44f8SRuslan Ermilov 			if (pflag)
660ade9ccfeSMarcel Moolenaar 				xo_message("%s: no PCB routine", tp->pr_name);
661cf5e44f8SRuslan Ermilov 			return;
662cf5e44f8SRuslan Ermilov 		}
663feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
664feda1a43SJohn Baldwin 			off = 0;
665feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
666feda1a43SJohn Baldwin 			if (pflag)
667ade9ccfeSMarcel Moolenaar 				xo_message("%s: PCB routine doesn't work on "
668ade9ccfeSMarcel Moolenaar 				    "cores", tp->pr_name);
669feda1a43SJohn Baldwin 			return;
670feda1a43SJohn Baldwin 		} else
671feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6729b50d902SRodney W. Grimes 	}
673feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
674ade9ccfeSMarcel Moolenaar 	    af != AF_UNSPEC)) {
675ade9ccfeSMarcel Moolenaar 		if (doingdblocks && *first) {
676ade9ccfeSMarcel Moolenaar 			xo_open_list("socket");
677ade9ccfeSMarcel Moolenaar 			*first = false;
678ade9ccfeSMarcel Moolenaar 		}
679ade9ccfeSMarcel Moolenaar 
680feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
6819b50d902SRodney W. Grimes 	}
682ade9ccfeSMarcel Moolenaar }
6839b50d902SRodney W. Grimes 
68429dde48dSGleb Smirnoff static int
68529dde48dSGleb Smirnoff kvmd_init(void)
6869b50d902SRodney W. Grimes {
687feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
688feda1a43SJohn Baldwin 
68929dde48dSGleb Smirnoff 	if (kvmd != NULL)
69029dde48dSGleb Smirnoff 		return (0);
69129dde48dSGleb Smirnoff 
692feda1a43SJohn Baldwin 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
693a4a889a0SXin LI 	if (setgid(getgid()) != 0)
694a4a889a0SXin LI 		xo_err(-1, "setgid");
69529dde48dSGleb Smirnoff 
69629dde48dSGleb Smirnoff 	if (kvmd == NULL) {
697ade9ccfeSMarcel Moolenaar 		xo_warnx("kvm not available: %s", errbuf);
69829dde48dSGleb Smirnoff 		return (-1);
69929dde48dSGleb Smirnoff 	}
70029dde48dSGleb Smirnoff 
701fc47e028SAlexander V. Chernikov 	return (0);
702fc47e028SAlexander V. Chernikov }
703fc47e028SAlexander V. Chernikov 
704fc47e028SAlexander V. Chernikov /*
705fc47e028SAlexander V. Chernikov  * Resolve symbol list, return 0 on success.
706fc47e028SAlexander V. Chernikov  */
70781dacd8bSHiroki Sato static int
708fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl)
709fc47e028SAlexander V. Chernikov {
710fc47e028SAlexander V. Chernikov 
711fc47e028SAlexander V. Chernikov 	if ((kvmd == NULL) && (kvmd_init() != 0))
712fc47e028SAlexander V. Chernikov 		return (-1);
713fc47e028SAlexander V. Chernikov 
714fc47e028SAlexander V. Chernikov 	if (_nl[0].n_type != 0)
715fc47e028SAlexander V. Chernikov 		return (0);
716fc47e028SAlexander V. Chernikov 
717fc47e028SAlexander V. Chernikov 	if (kvm_nlist(kvmd, _nl) < 0) {
71899453c6aSPoul-Henning Kamp 		if (nlistf)
719ade9ccfeSMarcel Moolenaar 			xo_errx(1, "%s: kvm_nlist: %s", nlistf,
72099453c6aSPoul-Henning Kamp 			    kvm_geterr(kvmd));
72199453c6aSPoul-Henning Kamp 		else
722ade9ccfeSMarcel Moolenaar 			xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
72399453c6aSPoul-Henning Kamp 	}
72499453c6aSPoul-Henning Kamp 
72529dde48dSGleb Smirnoff 	return (0);
72629dde48dSGleb Smirnoff }
72729dde48dSGleb Smirnoff 
72829dde48dSGleb Smirnoff /*
72981dacd8bSHiroki Sato  * Wrapper of kvm_dpcpu_setcpu().
73081dacd8bSHiroki Sato  */
73181dacd8bSHiroki Sato void
73281dacd8bSHiroki Sato kset_dpcpu(u_int cpuid)
73381dacd8bSHiroki Sato {
73481dacd8bSHiroki Sato 
73581dacd8bSHiroki Sato 	if ((kvmd == NULL) && (kvmd_init() != 0))
73681dacd8bSHiroki Sato 		xo_errx(-1, "%s: kvm is not available", __func__);
73781dacd8bSHiroki Sato 
73881dacd8bSHiroki Sato 	if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
73981dacd8bSHiroki Sato 		xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
74081dacd8bSHiroki Sato 		    cpuid, kvm_geterr(kvmd));
74181dacd8bSHiroki Sato 	return;
74281dacd8bSHiroki Sato }
74381dacd8bSHiroki Sato 
74481dacd8bSHiroki Sato /*
74529dde48dSGleb Smirnoff  * Read kernel memory, return 0 on success.
74629dde48dSGleb Smirnoff  */
74729dde48dSGleb Smirnoff int
74829dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size)
74929dde48dSGleb Smirnoff {
75029dde48dSGleb Smirnoff 
75129dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
75299453c6aSPoul-Henning Kamp 		return (-1);
75329dde48dSGleb Smirnoff 
754c6620a13SPoul-Henning Kamp 	if (!buf)
755c6620a13SPoul-Henning Kamp 		return (0);
756feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
757ade9ccfeSMarcel Moolenaar 		xo_warnx("%s", kvm_geterr(kvmd));
7589b50d902SRodney W. Grimes 		return (-1);
7599b50d902SRodney W. Grimes 	}
7609b50d902SRodney W. Grimes 	return (0);
7619b50d902SRodney W. Grimes }
7629b50d902SRodney W. Grimes 
76329dde48dSGleb Smirnoff /*
764e3a7aa6fSGleb Smirnoff  * Read single counter(9).
765e3a7aa6fSGleb Smirnoff  */
766e3a7aa6fSGleb Smirnoff uint64_t
767e3a7aa6fSGleb Smirnoff kread_counter(u_long addr)
768e3a7aa6fSGleb Smirnoff {
769e3a7aa6fSGleb Smirnoff 
770e3a7aa6fSGleb Smirnoff 	if (kvmd_init() < 0)
771e3a7aa6fSGleb Smirnoff 		return (-1);
772e3a7aa6fSGleb Smirnoff 
773e3a7aa6fSGleb Smirnoff 	return (kvm_counter_u64_fetch(kvmd, addr));
774e3a7aa6fSGleb Smirnoff }
775e3a7aa6fSGleb Smirnoff 
776e3a7aa6fSGleb Smirnoff /*
77729dde48dSGleb Smirnoff  * Read an array of N counters in kernel memory into array of N uint64_t's.
77829dde48dSGleb Smirnoff  */
77929dde48dSGleb Smirnoff int
7805da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size)
78129dde48dSGleb Smirnoff {
782794c57d3SMark Johnston 	uint64_t *c;
783794c57d3SMark Johnston 	u_long *counters;
784794c57d3SMark Johnston 	size_t i, n;
78529dde48dSGleb Smirnoff 
78629dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
78729dde48dSGleb Smirnoff 		return (-1);
78829dde48dSGleb Smirnoff 
789794c57d3SMark Johnston 	if (size % sizeof(uint64_t) != 0) {
790794c57d3SMark Johnston 		xo_warnx("kread_counters: invalid counter set size");
7915da0521fSAndrey V. Elsukov 		return (-1);
7925da0521fSAndrey V. Elsukov 	}
793794c57d3SMark Johnston 
794794c57d3SMark Johnston 	n = size / sizeof(uint64_t);
795794c57d3SMark Johnston 	if ((counters = malloc(n * sizeof(u_long))) == NULL)
796794c57d3SMark Johnston 		xo_err(-1, "malloc");
797794c57d3SMark Johnston 	if (kread(addr, counters, n * sizeof(u_long)) < 0) {
798794c57d3SMark Johnston 		free(counters);
799794c57d3SMark Johnston 		return (-1);
800794c57d3SMark Johnston 	}
801794c57d3SMark Johnston 
802794c57d3SMark Johnston 	c = buf;
803794c57d3SMark Johnston 	for (i = 0; i < n; i++)
804794c57d3SMark Johnston 		c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
805794c57d3SMark Johnston 
806794c57d3SMark Johnston 	free(counters);
80729dde48dSGleb Smirnoff 	return (0);
80829dde48dSGleb Smirnoff }
80929dde48dSGleb Smirnoff 
810a01e3379SDavid Malone const char *
8117b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
8129b50d902SRodney W. Grimes {
8139b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
8149b50d902SRodney W. Grimes }
8159b50d902SRodney W. Grimes 
816a01e3379SDavid Malone const char *
8177b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
8189b50d902SRodney W. Grimes {
8199b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
8209b50d902SRodney W. Grimes }
8219b50d902SRodney W. Grimes 
822f99a4046SMike Makonnen const char *
8237b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
824f99a4046SMike Makonnen {
825f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
826f99a4046SMike Makonnen }
827f99a4046SMike Makonnen 
8289b50d902SRodney W. Grimes /*
8299b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
8309b50d902SRodney W. Grimes  */
8319b50d902SRodney W. Grimes static struct protox *
832096146f8SYaroslav Tykhiy knownname(const char *name)
8339b50d902SRodney W. Grimes {
8349b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
8359b50d902SRodney W. Grimes 
8369b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
8379b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
8389b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
8399b50d902SRodney W. Grimes 				return (tp);
8409b50d902SRodney W. Grimes 	return (NULL);
8419b50d902SRodney W. Grimes }
8429b50d902SRodney W. Grimes 
8439b50d902SRodney W. Grimes /*
8449b50d902SRodney W. Grimes  * Find the protox corresponding to name.
8459b50d902SRodney W. Grimes  */
8469b50d902SRodney W. Grimes static struct protox *
847096146f8SYaroslav Tykhiy name2protox(const char *name)
8489b50d902SRodney W. Grimes {
8499b50d902SRodney W. Grimes 	struct protox *tp;
8509b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
8519b50d902SRodney W. Grimes 	struct protoent *p;
8529b50d902SRodney W. Grimes 
8539b50d902SRodney W. Grimes 	/*
8549b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
8559b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
8569b50d902SRodney W. Grimes 	 */
857cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
8589b50d902SRodney W. Grimes 		return (tp);
8599b50d902SRodney W. Grimes 
8609b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
861cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
8629b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
8639b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
8649b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
8659b50d902SRodney W. Grimes 				endprotoent();
8669b50d902SRodney W. Grimes 				return (knownname(p->p_name));
8679b50d902SRodney W. Grimes 			}
8689b50d902SRodney W. Grimes 	}
8699b50d902SRodney W. Grimes 	endprotoent();
8709b50d902SRodney W. Grimes 	return (NULL);
8719b50d902SRodney W. Grimes }
8729b50d902SRodney W. Grimes 
8739b50d902SRodney W. Grimes static void
8745e051718SAssar Westerlund usage(void)
8759b50d902SRodney W. Grimes {
876ade9ccfeSMarcel 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",
87785b0f0f3SAdrian Chadd "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
8781cb467b1SRuslan Ermilov "               [-M core] [-N system]",
87917ed2e8eSAlexander V. Chernikov "       netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
880d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
881fd2c6bc9SAllan Jude "       netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
882fd2c6bc9SAllan Jude "               [-q howmany]",
883fd2c6bc9SAllan Jude "       netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
88482d383bcSRuslan Ermilov "               [-M core] [-N system]",
885fd2c6bc9SAllan Jude "       netstat -i | -I interface -s [-46s]\n"
886fd2c6bc9SAllan Jude "               [-f protocol_family | -p protocol] [-M core] [-N system]",
88749c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
888fd2c6bc9SAllan Jude "       netstat -B [-z] [-I interface]",
889fd2c6bc9SAllan Jude "       netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
890fd2c6bc9SAllan Jude "               [-M core] [-N system]",
8911cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
89217ed2e8eSAlexander V. Chernikov "       netstat -g [-46W] [-f address_family] [-M core] [-N system]",
89317ed2e8eSAlexander V. Chernikov "       netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
8940153eb66SRobert Watson "       netstat -Q");
895ade9ccfeSMarcel Moolenaar 	xo_finish();
8969b50d902SRodney W. Grimes 	exit(1);
8979b50d902SRodney W. Grimes }
898