xref: /freebsd/usr.bin/netstat/main.c (revision 95968ea7ec8f1cd3c1d03cc56c704cb2392d0d25)
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 #include <sys/param.h>
339b50d902SRodney W. Grimes #include <sys/file.h>
34182e8ae2SDoug Rabson #ifdef JAIL
35182e8ae2SDoug Rabson #include <sys/jail.h>
36182e8ae2SDoug Rabson #endif
379b50d902SRodney W. Grimes #include <sys/protosw.h>
389b50d902SRodney W. Grimes #include <sys/socket.h>
39feda1a43SJohn Baldwin #include <sys/socketvar.h>
409eddb899SMark Johnston #include <sys/sysctl.h>
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <netinet/in.h>
439b50d902SRodney W. Grimes 
44690f477dSSam Leffler #ifdef NETGRAPH
454cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
46690f477dSSam Leffler #endif
474cf49a43SJulian Elischer 
489b50d902SRodney W. Grimes #include <ctype.h>
49821df508SXin LI #include <errno.h>
50182e8ae2SDoug Rabson #ifdef JAIL
51182e8ae2SDoug Rabson #include <jail.h>
52182e8ae2SDoug Rabson #endif
539b50d902SRodney W. Grimes #include <kvm.h>
549b50d902SRodney W. Grimes #include <limits.h>
559b50d902SRodney W. Grimes #include <netdb.h>
569b50d902SRodney W. Grimes #include <nlist.h>
57821df508SXin LI #include <paths.h>
587b95a1ebSYaroslav Tykhiy #include <stdint.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <stdlib.h>
61ade9ccfeSMarcel Moolenaar #include <stdbool.h>
629b50d902SRodney W. Grimes #include <string.h>
63*95968ea7SYan-Hao Wang #include <sysexits.h>
649b50d902SRodney W. Grimes #include <unistd.h>
659b50d902SRodney W. Grimes #include "netstat.h"
6681dacd8bSHiroki Sato #include "nl_defs.h"
67ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
689b50d902SRodney W. Grimes 
6981dacd8bSHiroki Sato static struct protox {
70feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
71feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
729b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
73feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
745e051718SAssar Westerlund 					/* control blocks printing routine */
75feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
765e051718SAssar Westerlund 					/* statistics printing routine */
775e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
78fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
7955fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
80feda1a43SJohn Baldwin 	int	pr_protocol;
819b50d902SRodney W. Grimes } protox[] = {
82ddf24a50SMichael Tuexen 	{ -1	,	N_TCPSTAT,	1,	protopr,
83feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
84ddf24a50SMichael Tuexen 	{ -1	,	N_UDPSTAT,	1,	protopr,
85feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
8674fd40c9SRandall Stewart #ifdef SCTP
87feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
88feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
8974fd40c9SRandall Stewart #endif
90aa0a1e58SJeff Roberson #ifdef SDP
91aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
92aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
93aa0a1e58SJeff Roberson #endif
94ddf24a50SMichael Tuexen 	{ -1	,	-1,		1,	protopr,
952b1c7217SGleb Smirnoff 	  divert_stats,	NULL,		"divert", 1,	0 },
96ddf24a50SMichael Tuexen 	{ -1	,	N_IPSTAT,	1,	protopr,
97feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
98ddf24a50SMichael Tuexen 	{ -1	,	N_ICMPSTAT,	1,	protopr,
99feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
100ddf24a50SMichael Tuexen 	{ -1	,	N_IGMPSTAT,	1,	protopr,
101feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
102cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
10381dacd8bSHiroki Sato 	{ -1,		N_IPSEC4STAT,	1,	NULL,	/* keep as compat */
1049d2d8e7bSGeorge V. Neville-Neil 	  ipsec_stats,	NULL,		"ipsec", 1,	0},
1058409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
1069d2d8e7bSGeorge V. Neville-Neil 	  ah_stats,	NULL,		"ah",	1,	0},
1078409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
1089d2d8e7bSGeorge V. Neville-Neil 	  esp_stats,	NULL,		"esp",	1,	0},
1098409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
1109d2d8e7bSGeorge V. Neville-Neil 	  ipcomp_stats,	NULL,		"ipcomp", 1,	0},
111100b98dbSKelly Yancey #endif
112ddf24a50SMichael Tuexen 	{ -1	,	N_PIMSTAT,	1,	protopr,
113feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
11481dacd8bSHiroki Sato 	{ -1,		N_CARPSTATS,	1,	NULL,
115feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
1163e4d5cd3SGleb Smirnoff #ifdef PF
11781dacd8bSHiroki Sato 	{ -1,		N_PFSYNCSTATS,	1,	NULL,
118feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
1195dea523bSKristof Provost 	{ -1,		N_PFLOWSTATS,	1,	NULL,
1205dea523bSKristof Provost 	  pflow_stats,	NULL,		"pflow", 1,	0 },
1213e4d5cd3SGleb Smirnoff #endif
12254fc657dSGeorge V. Neville-Neil 	{ -1,		N_ARPSTAT,	1,	NULL,
12354fc657dSGeorge V. Neville-Neil 	  arp_stats,	NULL,		"arp", 1,	0 },
1246bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
125feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1269b50d902SRodney W. Grimes };
1279b50d902SRodney W. Grimes 
128cfa1ca9dSYoshinobu Inoue #ifdef INET6
12981dacd8bSHiroki Sato static struct protox ip6protox[] = {
130ddf24a50SMichael Tuexen 	{ -1	,	N_TCPSTAT,	1,	protopr,
131feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
132ddf24a50SMichael Tuexen 	{ -1	,	N_UDPSTAT,	1,	protopr,
133feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
134ddf24a50SMichael Tuexen 	{ -1	,	N_IP6STAT,	1,	protopr,
135feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
136ddf24a50SMichael Tuexen 	{ -1	,	N_ICMP6STAT,	1,	protopr,
137feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
138aa0a1e58SJeff Roberson #ifdef SDP
139aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
140aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
141aa0a1e58SJeff Roberson #endif
142cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
1436bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
1449d2d8e7bSGeorge V. Neville-Neil 	  ipsec_stats,	NULL,		"ipsec6", 1,	0 },
145cfa1ca9dSYoshinobu Inoue #endif
146cfa1ca9dSYoshinobu Inoue #ifdef notyet
1476bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
148feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
149cfa1ca9dSYoshinobu Inoue #endif
150feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
151feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
1526bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
153feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
154cfa1ca9dSYoshinobu Inoue };
155cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
156cfa1ca9dSYoshinobu Inoue 
1573b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
15881dacd8bSHiroki Sato static struct protox pfkeyprotox[] = {
1596bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
160feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
1616bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
162feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1633b8a8567SJun-ichiro itojun Hagino };
1643b8a8567SJun-ichiro itojun Hagino #endif
1653b8a8567SJun-ichiro itojun Hagino 
166690f477dSSam Leffler #ifdef NETGRAPH
16781dacd8bSHiroki Sato static struct protox netgraphprotox[] = {
16881dacd8bSHiroki Sato 	{ N_NGSOCKLIST,	-1,		1,	netgraphprotopr,
169feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
17081dacd8bSHiroki Sato 	{ N_NGSOCKLIST,	-1,		1,	netgraphprotopr,
171feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
1726bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
173feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
1744cf49a43SJulian Elischer };
175690f477dSSam Leffler #endif
176cc6a66f2SJulian Elischer 
17781dacd8bSHiroki Sato static struct protox *protoprotox[] = {
178cfa1ca9dSYoshinobu Inoue 					 protox,
179cfa1ca9dSYoshinobu Inoue #ifdef INET6
180cfa1ca9dSYoshinobu Inoue 					 ip6protox,
181cfa1ca9dSYoshinobu Inoue #endif
1823b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
1833b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
1843b8a8567SJun-ichiro itojun Hagino #endif
18545c203fcSGleb Smirnoff 					 NULL };
1869b50d902SRodney W. Grimes 
187ade9ccfeSMarcel Moolenaar static void printproto(struct protox *, const char *, bool *);
1881a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
189096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
190096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
1919b50d902SRodney W. Grimes 
19281dacd8bSHiroki Sato static int kresolve_list(struct nlist *_nl);
19381dacd8bSHiroki Sato 
194c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
195080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
196080b7f49SDag-Erling Smørgrav 
197080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
198080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
19981dacd8bSHiroki Sato static int	Bflag;		/* show information about bpf consumers */
200080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
2010e5e35e3SRichard Scheffenegger int	cflag;		/* show TCP congestion control stack */
2020e5e35e3SRichard Scheffenegger int	Cflag;		/* show congestion control algo and vars */
203080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
204080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
205c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
206080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
207080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
208080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
209bf10ffe1SXin LI int	noutputs = 0;	/* how much outputs before we exit */
21065ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
21165ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
212fedeb08bSAlexander V. Chernikov int	Oflag;		/* show nhgrp objects*/
213a6663252SAlexander V. Chernikov int	oflag;		/* show nexthop objects*/
2142529f56eSJonathan T. Looney int	Pflag;		/* show TCP log ID */
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 */
229182e8ae2SDoug Rabson #ifdef JAIL
230182e8ae2SDoug Rabson char	*jail_name;	/* desired jail to operate in */
231182e8ae2SDoug Rabson #endif
232080b7f49SDag-Erling Smørgrav 
23381dacd8bSHiroki Sato static int	af;		/* address family */
234feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
2359b50d902SRodney W. Grimes 
2369b50d902SRodney W. Grimes int
237a01e3379SDavid Malone main(int argc, char *argv[])
2389b50d902SRodney W. Grimes {
239a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
2409b50d902SRodney W. Grimes 	int ch;
2413fddef95SHiroki Sato 	int fib = -1;
2423fddef95SHiroki Sato 	char *endptr;
243ade9ccfeSMarcel Moolenaar 	bool first = true;
244182e8ae2SDoug Rabson #ifdef JAIL
245182e8ae2SDoug Rabson 	int jid;
246182e8ae2SDoug Rabson #endif
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	af = AF_UNSPEC;
2499b50d902SRodney W. Grimes 
250ade9ccfeSMarcel Moolenaar 	argc = xo_parse_args(argc, argv);
25106ff7ccbSHajimu UMEMOTO 	if (argc < 0)
25206ff7ccbSHajimu UMEMOTO 		exit(EXIT_FAILURE);
253ade9ccfeSMarcel Moolenaar 
254182e8ae2SDoug Rabson 	while ((ch = getopt(argc, argv, "46AaBbCcdF:f:ghI:ij:LlM:mN:nOoPp:Qq:RrSTsuWw:xz"))
2550153eb66SRobert Watson 	    != -1)
2569b50d902SRodney W. Grimes 		switch(ch) {
25717ed2e8eSAlexander V. Chernikov 		case '4':
25817ed2e8eSAlexander V. Chernikov #ifdef INET
25917ed2e8eSAlexander V. Chernikov 			af = AF_INET;
26017ed2e8eSAlexander V. Chernikov #else
261*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "IPv4 support is not compiled in");
26217ed2e8eSAlexander V. Chernikov #endif
26317ed2e8eSAlexander V. Chernikov 			break;
26417ed2e8eSAlexander V. Chernikov 		case '6':
26517ed2e8eSAlexander V. Chernikov #ifdef INET6
26617ed2e8eSAlexander V. Chernikov 			af = AF_INET6;
26717ed2e8eSAlexander V. Chernikov #else
268*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "IPv6 support is not compiled in");
26917ed2e8eSAlexander V. Chernikov #endif
27017ed2e8eSAlexander V. Chernikov 			break;
2719b50d902SRodney W. Grimes 		case 'A':
2729b50d902SRodney W. Grimes 			Aflag = 1;
2739b50d902SRodney W. Grimes 			break;
2749b50d902SRodney W. Grimes 		case 'a':
2759b50d902SRodney W. Grimes 			aflag = 1;
2769b50d902SRodney W. Grimes 			break;
2776b463eedSChristian S.J. Peron 		case 'B':
2786b463eedSChristian S.J. Peron 			Bflag = 1;
2796b463eedSChristian S.J. Peron 			break;
280e1e293a5SDavid Greenman 		case 'b':
281e1e293a5SDavid Greenman 			bflag = 1;
282e1e293a5SDavid Greenman 			break;
2830e5e35e3SRichard Scheffenegger 		case 'c':
2840e5e35e3SRichard Scheffenegger 			cflag = 1;
2850e5e35e3SRichard Scheffenegger 			break;
286b98a21f6SMichael Tuexen 		case 'C':
287b98a21f6SMichael Tuexen 			Cflag = 1;
288b98a21f6SMichael Tuexen 			break;
2899b50d902SRodney W. Grimes 		case 'd':
2909b50d902SRodney W. Grimes 			dflag = 1;
2919b50d902SRodney W. Grimes 			break;
2923fddef95SHiroki Sato 		case 'F':
2933fddef95SHiroki Sato 			fib = strtol(optarg, &endptr, 0);
2943fddef95SHiroki Sato 			if (*endptr != '\0' ||
2953fddef95SHiroki Sato 			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
296*95968ea7SYan-Hao Wang 				xo_errx(EX_DATAERR, "%s: invalid fib", optarg);
2973fddef95SHiroki Sato 			break;
2989b50d902SRodney W. Grimes 		case 'f':
2992c284d93SGleb Smirnoff 			if (strcmp(optarg, "inet") == 0)
3009b50d902SRodney W. Grimes 				af = AF_INET;
301cfa1ca9dSYoshinobu Inoue #ifdef INET6
302cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
303cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
304e5134d2eSMax Laier #endif
305e5134d2eSMax Laier #ifdef IPSEC
3063b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3073b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
308e5134d2eSMax Laier #endif
30910d5269fSHiroki Sato 			else if (strcmp(optarg, "unix") == 0 ||
31010d5269fSHiroki Sato 				 strcmp(optarg, "local") == 0)
3119b50d902SRodney W. Grimes 				af = AF_UNIX;
312690f477dSSam Leffler #ifdef NETGRAPH
3134cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3144cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3154cf49a43SJulian Elischer 				af = AF_NETGRAPH;
316690f477dSSam Leffler #endif
317d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
318d44ddba9SRuslan Ermilov 				af = AF_LINK;
3199b50d902SRodney W. Grimes 			else {
320*95968ea7SYan-Hao Wang 				xo_errx(EX_DATAERR, "%s: unknown address family",
321ade9ccfeSMarcel Moolenaar 				    optarg);
3229b50d902SRodney W. Grimes 			}
3239b50d902SRodney W. Grimes 			break;
3249b50d902SRodney W. Grimes 		case 'g':
3259b50d902SRodney W. Grimes 			gflag = 1;
3269b50d902SRodney W. Grimes 			break;
327c2dfd19fSGleb Smirnoff 		case 'h':
328c2dfd19fSGleb Smirnoff 			hflag = 1;
329c2dfd19fSGleb Smirnoff 			break;
3309b50d902SRodney W. Grimes 		case 'I': {
3319b50d902SRodney W. Grimes 			char *cp;
3329b50d902SRodney W. Grimes 
3339b50d902SRodney W. Grimes 			iflag = 1;
3349b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3359b50d902SRodney W. Grimes 				continue;
3369b50d902SRodney W. Grimes 			unit = atoi(cp);
3379b50d902SRodney W. Grimes 			break;
3389b50d902SRodney W. Grimes 		}
3399b50d902SRodney W. Grimes 		case 'i':
3409b50d902SRodney W. Grimes 			iflag = 1;
3419b50d902SRodney W. Grimes 			break;
342182e8ae2SDoug Rabson 		case 'j':
343182e8ae2SDoug Rabson #ifdef JAIL
344182e8ae2SDoug Rabson 			if (optarg == NULL)
345182e8ae2SDoug Rabson 				usage();
346182e8ae2SDoug Rabson 			jail_name = optarg;
347182e8ae2SDoug Rabson #else
348*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "Jail support is not compiled in");
349182e8ae2SDoug Rabson #endif
350182e8ae2SDoug Rabson 			break;
351ac55add0SGuido van Rooij 		case 'L':
352ac55add0SGuido van Rooij 			Lflag = 1;
353ac55add0SGuido van Rooij 			break;
3549b50d902SRodney W. Grimes 		case 'M':
3559b50d902SRodney W. Grimes 			memf = optarg;
3569b50d902SRodney W. Grimes 			break;
3579b50d902SRodney W. Grimes 		case 'm':
3589b50d902SRodney W. Grimes 			mflag = 1;
3599b50d902SRodney W. Grimes 			break;
3609b50d902SRodney W. Grimes 		case 'N':
3619b50d902SRodney W. Grimes 			nlistf = optarg;
3629b50d902SRodney W. Grimes 			break;
3639b50d902SRodney W. Grimes 		case 'n':
36465ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
3659b50d902SRodney W. Grimes 			break;
366a6663252SAlexander V. Chernikov 		case 'o':
367a6663252SAlexander V. Chernikov 			oflag = 1;
368a6663252SAlexander V. Chernikov 			break;
369fedeb08bSAlexander V. Chernikov 		case 'O':
370fedeb08bSAlexander V. Chernikov 			Oflag = 1;
371fedeb08bSAlexander V. Chernikov 			break;
3722529f56eSJonathan T. Looney 		case 'P':
3732529f56eSJonathan T. Looney 			Pflag = 1;
3742529f56eSJonathan T. Looney 			break;
3759b50d902SRodney W. Grimes 		case 'p':
3769b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
377*95968ea7SYan-Hao Wang 				xo_errx(EX_DATAERR, "%s: unknown or uninstrumented "
378ade9ccfeSMarcel Moolenaar 				    "protocol", optarg);
3799b50d902SRodney W. Grimes 			}
3809b50d902SRodney W. Grimes 			pflag = 1;
3819b50d902SRodney W. Grimes 			break;
3820153eb66SRobert Watson 		case 'Q':
3830153eb66SRobert Watson 			Qflag = 1;
3840153eb66SRobert Watson 			break;
385bf10ffe1SXin LI 		case 'q':
386bf10ffe1SXin LI 			noutputs = atoi(optarg);
387bf10ffe1SXin LI 			if (noutputs != 0)
388bf10ffe1SXin LI 				noutputs++;
389bf10ffe1SXin LI 			break;
3909b50d902SRodney W. Grimes 		case 'r':
3919b50d902SRodney W. Grimes 			rflag = 1;
3929b50d902SRodney W. Grimes 			break;
39385b0f0f3SAdrian Chadd 		case 'R':
39485b0f0f3SAdrian Chadd 			Rflag = 1;
39585b0f0f3SAdrian Chadd 			break;
3969b50d902SRodney W. Grimes 		case 's':
3979b50d902SRodney W. Grimes 			++sflag;
3989b50d902SRodney W. Grimes 			break;
39965ea0024SAssar Westerlund 		case 'S':
40065ea0024SAssar Westerlund 			numeric_addr = 1;
40165ea0024SAssar Westerlund 			break;
4029b50d902SRodney W. Grimes 		case 'u':
4039b50d902SRodney W. Grimes 			af = AF_UNIX;
4049b50d902SRodney W. Grimes 			break;
405080b7f49SDag-Erling Smørgrav 		case 'W':
40670057abfSRuslan Ermilov 		case 'l':
407080b7f49SDag-Erling Smørgrav 			Wflag = 1;
408080b7f49SDag-Erling Smørgrav 			break;
4099b50d902SRodney W. Grimes 		case 'w':
4109b50d902SRodney W. Grimes 			interval = atoi(optarg);
4119b50d902SRodney W. Grimes 			iflag = 1;
4129b50d902SRodney W. Grimes 			break;
413f5d34df5SGeorge V. Neville-Neil 		case 'T':
414f5d34df5SGeorge V. Neville-Neil 			Tflag = 1;
415f5d34df5SGeorge V. Neville-Neil 			break;
41649f287f8SGeorge V. Neville-Neil 		case 'x':
41749f287f8SGeorge V. Neville-Neil 			xflag = 1;
41849f287f8SGeorge V. Neville-Neil 			break;
419c73d99b5SRuslan Ermilov 		case 'z':
420c73d99b5SRuslan Ermilov 			zflag = 1;
421c73d99b5SRuslan Ermilov 			break;
4229b50d902SRodney W. Grimes 		case '?':
4239b50d902SRodney W. Grimes 		default:
4249b50d902SRodney W. Grimes 			usage();
4259b50d902SRodney W. Grimes 		}
4269b50d902SRodney W. Grimes 	argv += optind;
4279b50d902SRodney W. Grimes 	argc -= optind;
4289b50d902SRodney W. Grimes 
4299b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4309b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4319b50d902SRodney W. Grimes 	if (*argv) {
4329b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4339b50d902SRodney W. Grimes 			interval = atoi(*argv);
4349b50d902SRodney W. Grimes 			if (interval <= 0)
4359b50d902SRodney W. Grimes 				usage();
4369b50d902SRodney W. Grimes 			++argv;
4379b50d902SRodney W. Grimes 			iflag = 1;
4389b50d902SRodney W. Grimes 		}
4399b50d902SRodney W. Grimes 		if (*argv) {
4409b50d902SRodney W. Grimes 			nlistf = *argv;
4419b50d902SRodney W. Grimes 			if (*++argv)
4429b50d902SRodney W. Grimes 				memf = *argv;
4439b50d902SRodney W. Grimes 		}
4449b50d902SRodney W. Grimes 	}
4459b50d902SRodney W. Grimes #endif
4469b50d902SRodney W. Grimes 
447182e8ae2SDoug Rabson #ifdef JAIL
448182e8ae2SDoug Rabson 	if (jail_name != NULL) {
449182e8ae2SDoug Rabson 		jid = jail_getid(jail_name);
450182e8ae2SDoug Rabson 		if (jid == -1)
451*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "Jail not found");
452182e8ae2SDoug Rabson 		if (jail_attach(jid) != 0)
453*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "Cannot attach to jail");
454182e8ae2SDoug Rabson 	}
455182e8ae2SDoug Rabson #endif
456182e8ae2SDoug Rabson 
4579b50d902SRodney W. Grimes 	/*
4589b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4599b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4609b50d902SRodney W. Grimes 	 */
461feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
462a4a889a0SXin LI 	if (!live) {
463a4a889a0SXin LI 		if (setgid(getgid()) != 0)
464*95968ea7SYan-Hao Wang 			xo_err(EX_OSERR, "setgid");
4651caaf3eaSBaptiste Daroussin 		/* Load all necessary kvm symbols */
4661caaf3eaSBaptiste Daroussin 		kresolve_list(nl);
467a4a889a0SXin LI 	}
4689b50d902SRodney W. Grimes 
469f5d34df5SGeorge V. Neville-Neil 	if (xflag && Tflag)
470*95968ea7SYan-Hao Wang 		xo_errx(EX_USAGE, "-x and -T are incompatible, pick one.");
471f5d34df5SGeorge V. Neville-Neil 
4726b463eedSChristian S.J. Peron 	if (Bflag) {
473feda1a43SJohn Baldwin 		if (!live)
474feda1a43SJohn Baldwin 			usage();
4756b463eedSChristian S.J. Peron 		bpf_stats(interface);
476*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
477*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
478*95968ea7SYan-Hao Wang 		exit(EX_OK);
4796b463eedSChristian S.J. Peron 	}
4809b50d902SRodney W. Grimes 	if (mflag) {
48183708764SRuslan Ermilov 		if (!live) {
482feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
48305d1f5bcSAndrey V. Elsukov 				mbpr(kvmd, nl[N_SFSTAT].n_value);
484d15c5f56SRuslan Ermilov 		} else
485d4426f28SRobert Watson 			mbpr(NULL, 0);
486*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
487*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
488*95968ea7SYan-Hao Wang 		exit(EX_OK);
4899b50d902SRodney W. Grimes 	}
4900153eb66SRobert Watson 	if (Qflag) {
49188737be2SRobert Watson 		if (!live) {
49288737be2SRobert Watson 			if (kread(0, NULL, 0) == 0)
49381dacd8bSHiroki Sato 				netisr_stats();
49488737be2SRobert Watson 		} else
49581dacd8bSHiroki Sato 			netisr_stats();
496*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
497*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
498*95968ea7SYan-Hao Wang 		exit(EX_OK);
4990153eb66SRobert Watson 	}
500cc63cd56SPeter Wemm #if 0
5019b50d902SRodney W. Grimes 	/*
5029b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5039b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5049b50d902SRodney W. Grimes 	 */
5059b50d902SRodney W. Grimes 	sethostent(1);
5069b50d902SRodney W. Grimes 	setnetent(1);
507cc63cd56SPeter Wemm #else
508cc63cd56SPeter Wemm 	/*
509cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
510cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
511cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
512cc63cd56SPeter Wemm 	 */
513cc63cd56SPeter Wemm #endif
514cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
515ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5165c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
51710d5269fSHiroki Sato 		intpr(NULL, af);
518ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
519*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
520*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
521*95968ea7SYan-Hao Wang 		exit(EX_OK);
5229b50d902SRodney W. Grimes 	}
5239b50d902SRodney W. Grimes 	if (rflag) {
524ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5255c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
5265d6d7e75SGleb Smirnoff 		if (sflag) {
5272ccdd4b3SBaptiste Daroussin 			if (live) {
5282ccdd4b3SBaptiste Daroussin 				kresolve_list(nl);
5292ccdd4b3SBaptiste Daroussin 			}
530fc47e028SAlexander V. Chernikov 			rt_stats();
5315d6d7e75SGleb Smirnoff 		} else
532fc47e028SAlexander V. Chernikov 			routepr(fib, af);
533ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
534*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
535*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
536*95968ea7SYan-Hao Wang 		exit(EX_OK);
5379b50d902SRodney W. Grimes 	}
538a6663252SAlexander V. Chernikov 	if (oflag) {
539a6663252SAlexander V. Chernikov 		xo_open_container("statistics");
5405c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
541a6663252SAlexander V. Chernikov 		nhops_print(fib, af);
542a6663252SAlexander V. Chernikov 		xo_close_container("statistics");
543*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
544*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
545*95968ea7SYan-Hao Wang 		exit(EX_OK);
546a6663252SAlexander V. Chernikov 	}
547fedeb08bSAlexander V. Chernikov 	if (Oflag) {
548fedeb08bSAlexander V. Chernikov 		xo_open_container("statistics");
5495c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
550fedeb08bSAlexander V. Chernikov 		nhgrp_print(fib, af);
551fedeb08bSAlexander V. Chernikov 		xo_close_container("statistics");
552*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
553*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
554*95968ea7SYan-Hao Wang 		exit(EX_OK);
555fedeb08bSAlexander V. Chernikov 	}
556fedeb08bSAlexander V. Chernikov 
557a6663252SAlexander V. Chernikov 
558fc47e028SAlexander V. Chernikov 
5599b50d902SRodney W. Grimes 	if (gflag) {
560ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5615c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
562cfa1ca9dSYoshinobu Inoue 		if (sflag) {
563cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
564fc47e028SAlexander V. Chernikov 				mrt_stats();
565cfa1ca9dSYoshinobu Inoue #ifdef INET6
566cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
567fc47e028SAlexander V. Chernikov 				mrt6_stats();
568cfa1ca9dSYoshinobu Inoue #endif
569cfa1ca9dSYoshinobu Inoue 		} else {
570cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
571fc47e028SAlexander V. Chernikov 				mroutepr();
572cfa1ca9dSYoshinobu Inoue #ifdef INET6
573cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
574fc47e028SAlexander V. Chernikov 				mroute6pr();
575cfa1ca9dSYoshinobu Inoue #endif
576cfa1ca9dSYoshinobu Inoue 		}
577ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
578*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
579*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
580*95968ea7SYan-Hao Wang 		exit(EX_OK);
5819b50d902SRodney W. Grimes 	}
582cfa1ca9dSYoshinobu Inoue 
583cf5e44f8SRuslan Ermilov 	if (tp) {
584ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5855c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
586ade9ccfeSMarcel Moolenaar 		printproto(tp, tp->pr_name, &first);
587ade9ccfeSMarcel Moolenaar 		if (!first)
588ade9ccfeSMarcel Moolenaar 			xo_close_list("socket");
589ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
590*95968ea7SYan-Hao Wang 		if (xo_finish() < 0)
591*95968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
592*95968ea7SYan-Hao Wang 		exit(EX_OK);
593cf5e44f8SRuslan Ermilov 	}
594ade9ccfeSMarcel Moolenaar 
595ade9ccfeSMarcel Moolenaar 	xo_open_container("statistics");
5965c4f64bdSBram 	xo_set_version(NETSTAT_XO_VERSION);
597cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5989b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
599ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
600cfa1ca9dSYoshinobu Inoue #ifdef INET6
601cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
602cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
603ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
604cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
6053b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
6063b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
6073b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
608ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
6093b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
610690f477dSSam Leffler #ifdef NETGRAPH
6114cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
6124cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
613ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
614690f477dSSam Leffler #endif /* NETGRAPH */
615f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
616feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
617963b7ccdSRobert Watson 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
618ade9ccfeSMarcel Moolenaar 		    nl[N_UNP_SPHEAD].n_value, &first);
619ade9ccfeSMarcel Moolenaar 
620ade9ccfeSMarcel Moolenaar 	if (!first)
621ade9ccfeSMarcel Moolenaar 		xo_close_list("socket");
622ade9ccfeSMarcel Moolenaar 	xo_close_container("statistics");
623*95968ea7SYan-Hao Wang 	if (xo_finish() < 0)
624*95968ea7SYan-Hao Wang 		xo_err(EX_IOERR, "stdout");
625*95968ea7SYan-Hao Wang 	exit(EX_OK);
6269b50d902SRodney W. Grimes }
6279b50d902SRodney W. Grimes 
628dbfd8708SGleb Smirnoff static int
629dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
630dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn, int zero)
6319eddb899SMark Johnston {
6329eddb899SMark Johnston 	int error;
6339eddb899SMark Johnston 
6349eddb899SMark Johnston 	if (live) {
6359eddb899SMark Johnston 		memset(stats, 0, len);
636dbfd8708SGleb Smirnoff 		if (zero)
6379eddb899SMark Johnston 			error = sysctlbyname(sysctlname, NULL, NULL, stats,
6389eddb899SMark Johnston 			    len);
6399eddb899SMark Johnston 		else
6409eddb899SMark Johnston 			error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
6419eddb899SMark Johnston 		if (error == -1 && errno != ENOENT)
6429eddb899SMark Johnston 			xo_warn("sysctl %s", sysctlname);
6439eddb899SMark Johnston 	} else {
6449eddb899SMark Johnston 		if (off == 0)
6459eddb899SMark Johnston 			return (1);
6469eddb899SMark Johnston 		error = kreadfn(off, stats, len);
6479eddb899SMark Johnston 	}
6489eddb899SMark Johnston 	return (error);
6499eddb899SMark Johnston }
6509eddb899SMark Johnston 
651dbfd8708SGleb Smirnoff int
652dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats,
653dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
654dbfd8708SGleb Smirnoff {
655dbfd8708SGleb Smirnoff 
656dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
657dbfd8708SGleb Smirnoff     zflag));
658dbfd8708SGleb Smirnoff }
659dbfd8708SGleb Smirnoff 
660dbfd8708SGleb Smirnoff int
661dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
662dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
663dbfd8708SGleb Smirnoff {
664dbfd8708SGleb Smirnoff 
665dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
666dbfd8708SGleb Smirnoff }
667dbfd8708SGleb Smirnoff 
6689b50d902SRodney W. Grimes /*
6699b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6709b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6719b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6729b50d902SRodney W. Grimes  */
6739b50d902SRodney W. Grimes static void
674ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first)
6759b50d902SRodney W. Grimes {
676feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6779b50d902SRodney W. Grimes 	u_long off;
678ade9ccfeSMarcel Moolenaar 	bool doingdblocks = false;
6799b50d902SRodney W. Grimes 
6809b50d902SRodney W. Grimes 	if (sflag) {
681cfa1ca9dSYoshinobu Inoue 		if (iflag) {
682cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
68310d5269fSHiroki Sato 				intpr(tp->pr_istats, af);
684cf5e44f8SRuslan Ermilov 			else if (pflag)
685ade9ccfeSMarcel Moolenaar 				xo_message("%s: no per-interface stats routine",
686cf5e44f8SRuslan Ermilov 				    tp->pr_name);
687cfa1ca9dSYoshinobu Inoue 			return;
688feda1a43SJohn Baldwin 		} else {
6899b50d902SRodney W. Grimes 			pr = tp->pr_stats;
690cf5e44f8SRuslan Ermilov 			if (!pr) {
691cf5e44f8SRuslan Ermilov 				if (pflag)
692ade9ccfeSMarcel Moolenaar 					xo_message("%s: no stats routine",
693cf5e44f8SRuslan Ermilov 					    tp->pr_name);
694cf5e44f8SRuslan Ermilov 				return;
695cf5e44f8SRuslan Ermilov 			}
696feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
697feda1a43SJohn Baldwin 				off = 0;
698feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
699feda1a43SJohn Baldwin 				if (pflag)
700ade9ccfeSMarcel Moolenaar 					xo_message("%s: stats routine doesn't "
701ade9ccfeSMarcel Moolenaar 					    "work on cores", tp->pr_name);
702feda1a43SJohn Baldwin 				return;
703feda1a43SJohn Baldwin 			} else
704feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
705cfa1ca9dSYoshinobu Inoue 		}
7069b50d902SRodney W. Grimes 	} else {
707ade9ccfeSMarcel Moolenaar 		doingdblocks = true;
7089b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
709cf5e44f8SRuslan Ermilov 		if (!pr) {
710cf5e44f8SRuslan Ermilov 			if (pflag)
711ade9ccfeSMarcel Moolenaar 				xo_message("%s: no PCB routine", tp->pr_name);
712cf5e44f8SRuslan Ermilov 			return;
713cf5e44f8SRuslan Ermilov 		}
714feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
715feda1a43SJohn Baldwin 			off = 0;
716feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
717feda1a43SJohn Baldwin 			if (pflag)
718ade9ccfeSMarcel Moolenaar 				xo_message("%s: PCB routine doesn't work on "
719ade9ccfeSMarcel Moolenaar 				    "cores", tp->pr_name);
720feda1a43SJohn Baldwin 			return;
721feda1a43SJohn Baldwin 		} else
722feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
7239b50d902SRodney W. Grimes 	}
724feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
725ade9ccfeSMarcel Moolenaar 	    af != AF_UNSPEC)) {
726ade9ccfeSMarcel Moolenaar 		if (doingdblocks && *first) {
727ade9ccfeSMarcel Moolenaar 			xo_open_list("socket");
728ade9ccfeSMarcel Moolenaar 			*first = false;
729ade9ccfeSMarcel Moolenaar 		}
730ade9ccfeSMarcel Moolenaar 
731feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
7329b50d902SRodney W. Grimes 	}
733ade9ccfeSMarcel Moolenaar }
7349b50d902SRodney W. Grimes 
73529dde48dSGleb Smirnoff static int
73629dde48dSGleb Smirnoff kvmd_init(void)
7379b50d902SRodney W. Grimes {
738feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
739feda1a43SJohn Baldwin 
74029dde48dSGleb Smirnoff 	if (kvmd != NULL)
74129dde48dSGleb Smirnoff 		return (0);
74229dde48dSGleb Smirnoff 
743feda1a43SJohn Baldwin 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
744a4a889a0SXin LI 	if (setgid(getgid()) != 0)
745*95968ea7SYan-Hao Wang 		xo_err(EX_OSERR, "setgid");
74629dde48dSGleb Smirnoff 
74729dde48dSGleb Smirnoff 	if (kvmd == NULL) {
748ade9ccfeSMarcel Moolenaar 		xo_warnx("kvm not available: %s", errbuf);
74929dde48dSGleb Smirnoff 		return (-1);
75029dde48dSGleb Smirnoff 	}
75129dde48dSGleb Smirnoff 
752fc47e028SAlexander V. Chernikov 	return (0);
753fc47e028SAlexander V. Chernikov }
754fc47e028SAlexander V. Chernikov 
755fc47e028SAlexander V. Chernikov /*
756fc47e028SAlexander V. Chernikov  * Resolve symbol list, return 0 on success.
757fc47e028SAlexander V. Chernikov  */
75881dacd8bSHiroki Sato static int
759fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl)
760fc47e028SAlexander V. Chernikov {
761fc47e028SAlexander V. Chernikov 
762fc47e028SAlexander V. Chernikov 	if ((kvmd == NULL) && (kvmd_init() != 0))
763fc47e028SAlexander V. Chernikov 		return (-1);
764fc47e028SAlexander V. Chernikov 
765fc47e028SAlexander V. Chernikov 	if (_nl[0].n_type != 0)
766fc47e028SAlexander V. Chernikov 		return (0);
767fc47e028SAlexander V. Chernikov 
768fc47e028SAlexander V. Chernikov 	if (kvm_nlist(kvmd, _nl) < 0) {
76999453c6aSPoul-Henning Kamp 		if (nlistf)
770*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "%s: kvm_nlist: %s", nlistf,
77199453c6aSPoul-Henning Kamp 			    kvm_geterr(kvmd));
77299453c6aSPoul-Henning Kamp 		else
773*95968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "kvm_nlist: %s", kvm_geterr(kvmd));
77499453c6aSPoul-Henning Kamp 	}
77599453c6aSPoul-Henning Kamp 
77629dde48dSGleb Smirnoff 	return (0);
77729dde48dSGleb Smirnoff }
77829dde48dSGleb Smirnoff 
77929dde48dSGleb Smirnoff /*
78081dacd8bSHiroki Sato  * Wrapper of kvm_dpcpu_setcpu().
78181dacd8bSHiroki Sato  */
78281dacd8bSHiroki Sato void
78381dacd8bSHiroki Sato kset_dpcpu(u_int cpuid)
78481dacd8bSHiroki Sato {
78581dacd8bSHiroki Sato 
78681dacd8bSHiroki Sato 	if ((kvmd == NULL) && (kvmd_init() != 0))
787*95968ea7SYan-Hao Wang 		xo_errx(EX_UNAVAILABLE, "%s: kvm is not available", __func__);
78881dacd8bSHiroki Sato 
78981dacd8bSHiroki Sato 	if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
790*95968ea7SYan-Hao Wang 		xo_errx(EX_UNAVAILABLE, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
79181dacd8bSHiroki Sato 		    cpuid, kvm_geterr(kvmd));
79281dacd8bSHiroki Sato 	return;
79381dacd8bSHiroki Sato }
79481dacd8bSHiroki Sato 
79581dacd8bSHiroki Sato /*
79629dde48dSGleb Smirnoff  * Read kernel memory, return 0 on success.
79729dde48dSGleb Smirnoff  */
79829dde48dSGleb Smirnoff int
79929dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size)
80029dde48dSGleb Smirnoff {
80129dde48dSGleb Smirnoff 
80229dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
80399453c6aSPoul-Henning Kamp 		return (-1);
80429dde48dSGleb Smirnoff 
805c6620a13SPoul-Henning Kamp 	if (!buf)
806c6620a13SPoul-Henning Kamp 		return (0);
807feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
808ade9ccfeSMarcel Moolenaar 		xo_warnx("%s", kvm_geterr(kvmd));
8099b50d902SRodney W. Grimes 		return (-1);
8109b50d902SRodney W. Grimes 	}
8119b50d902SRodney W. Grimes 	return (0);
8129b50d902SRodney W. Grimes }
8139b50d902SRodney W. Grimes 
81429dde48dSGleb Smirnoff /*
815e3a7aa6fSGleb Smirnoff  * Read single counter(9).
816e3a7aa6fSGleb Smirnoff  */
817e3a7aa6fSGleb Smirnoff uint64_t
818e3a7aa6fSGleb Smirnoff kread_counter(u_long addr)
819e3a7aa6fSGleb Smirnoff {
820e3a7aa6fSGleb Smirnoff 
821e3a7aa6fSGleb Smirnoff 	if (kvmd_init() < 0)
822e3a7aa6fSGleb Smirnoff 		return (-1);
823e3a7aa6fSGleb Smirnoff 
824e3a7aa6fSGleb Smirnoff 	return (kvm_counter_u64_fetch(kvmd, addr));
825e3a7aa6fSGleb Smirnoff }
826e3a7aa6fSGleb Smirnoff 
827e3a7aa6fSGleb Smirnoff /*
82829dde48dSGleb Smirnoff  * Read an array of N counters in kernel memory into array of N uint64_t's.
82929dde48dSGleb Smirnoff  */
83029dde48dSGleb Smirnoff int
8315da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size)
83229dde48dSGleb Smirnoff {
833794c57d3SMark Johnston 	uint64_t *c;
834794c57d3SMark Johnston 	u_long *counters;
835794c57d3SMark Johnston 	size_t i, n;
83629dde48dSGleb Smirnoff 
83729dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
83829dde48dSGleb Smirnoff 		return (-1);
83929dde48dSGleb Smirnoff 
840794c57d3SMark Johnston 	if (size % sizeof(uint64_t) != 0) {
841794c57d3SMark Johnston 		xo_warnx("kread_counters: invalid counter set size");
8425da0521fSAndrey V. Elsukov 		return (-1);
8435da0521fSAndrey V. Elsukov 	}
844794c57d3SMark Johnston 
845794c57d3SMark Johnston 	n = size / sizeof(uint64_t);
846794c57d3SMark Johnston 	if ((counters = malloc(n * sizeof(u_long))) == NULL)
847*95968ea7SYan-Hao Wang 		xo_err(EX_OSERR, "malloc");
848794c57d3SMark Johnston 	if (kread(addr, counters, n * sizeof(u_long)) < 0) {
849794c57d3SMark Johnston 		free(counters);
850794c57d3SMark Johnston 		return (-1);
851794c57d3SMark Johnston 	}
852794c57d3SMark Johnston 
853794c57d3SMark Johnston 	c = buf;
854794c57d3SMark Johnston 	for (i = 0; i < n; i++)
855794c57d3SMark Johnston 		c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
856794c57d3SMark Johnston 
857794c57d3SMark Johnston 	free(counters);
85829dde48dSGleb Smirnoff 	return (0);
85929dde48dSGleb Smirnoff }
86029dde48dSGleb Smirnoff 
861a01e3379SDavid Malone const char *
8627b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
8639b50d902SRodney W. Grimes {
8649b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
8659b50d902SRodney W. Grimes }
8669b50d902SRodney W. Grimes 
867a01e3379SDavid Malone const char *
8687b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
8699b50d902SRodney W. Grimes {
8709b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
8719b50d902SRodney W. Grimes }
8729b50d902SRodney W. Grimes 
873f99a4046SMike Makonnen const char *
8747b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
875f99a4046SMike Makonnen {
876f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
877f99a4046SMike Makonnen }
878f99a4046SMike Makonnen 
8799b50d902SRodney W. Grimes /*
8809b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
8819b50d902SRodney W. Grimes  */
8829b50d902SRodney W. Grimes static struct protox *
883096146f8SYaroslav Tykhiy knownname(const char *name)
8849b50d902SRodney W. Grimes {
8859b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
8869b50d902SRodney W. Grimes 
8879b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
8889b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
8899b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
8909b50d902SRodney W. Grimes 				return (tp);
8919b50d902SRodney W. Grimes 	return (NULL);
8929b50d902SRodney W. Grimes }
8939b50d902SRodney W. Grimes 
8949b50d902SRodney W. Grimes /*
8959b50d902SRodney W. Grimes  * Find the protox corresponding to name.
8969b50d902SRodney W. Grimes  */
8979b50d902SRodney W. Grimes static struct protox *
898096146f8SYaroslav Tykhiy name2protox(const char *name)
8999b50d902SRodney W. Grimes {
9009b50d902SRodney W. Grimes 	struct protox *tp;
9019b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
9029b50d902SRodney W. Grimes 	struct protoent *p;
9039b50d902SRodney W. Grimes 
9049b50d902SRodney W. Grimes 	/*
9059b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
9069b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
9079b50d902SRodney W. Grimes 	 */
908cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
9099b50d902SRodney W. Grimes 		return (tp);
9109b50d902SRodney W. Grimes 
9119b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
912cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
9139b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
9149b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
9159b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
9169b50d902SRodney W. Grimes 				endprotoent();
9179b50d902SRodney W. Grimes 				return (knownname(p->p_name));
9189b50d902SRodney W. Grimes 			}
9199b50d902SRodney W. Grimes 	}
9209b50d902SRodney W. Grimes 	endprotoent();
9219b50d902SRodney W. Grimes 	return (NULL);
9229b50d902SRodney W. Grimes }
9239b50d902SRodney W. Grimes 
9249b50d902SRodney W. Grimes static void
9255e051718SAssar Westerlund usage(void)
9269b50d902SRodney W. Grimes {
927*95968ea7SYan-Hao Wang 	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",
928182e8ae2SDoug Rabson "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n"
9291cb467b1SRuslan Ermilov "               [-M core] [-N system]",
930182e8ae2SDoug Rabson "       netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n"
931d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
932182e8ae2SDoug Rabson "       netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n"
933fd2c6bc9SAllan Jude "               [-q howmany]",
934182e8ae2SDoug Rabson "       netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n"
93582d383bcSRuslan Ermilov "               [-M core] [-N system]",
936182e8ae2SDoug Rabson "       netstat [-j jail] -i | -I interface -s [-46s]\n"
937fd2c6bc9SAllan Jude "               [-f protocol_family | -p protocol] [-M core] [-N system]",
938182e8ae2SDoug Rabson "       netstat [-j jail] -m [-M core] [-N system]",
939182e8ae2SDoug Rabson "       netstat [-j jail] -B [-z] [-I interface]",
940182e8ae2SDoug Rabson "       netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n"
941fd2c6bc9SAllan Jude "               [-M core] [-N system]",
942182e8ae2SDoug Rabson "       netstat [-j jail] -rs [-s] [-M core] [-N system]",
943182e8ae2SDoug Rabson "       netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]",
944182e8ae2SDoug Rabson "       netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]",
945182e8ae2SDoug Rabson "       netstat [-j jail] -Q");
946*95968ea7SYan-Hao Wang 	exit(EX_USAGE);
9479b50d902SRodney W. Grimes }
948