xref: /freebsd/usr.bin/netstat/main.c (revision c032fb855766d25d91b190af554e1ba86bbe7856)
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>
6395968ea7SYan-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 
197*c032fb85SEd Maste bool	Aflag;		/* show addresses of protocol control block */
198*c032fb85SEd Maste bool	aflag;		/* show all sockets (including servers) */
199*c032fb85SEd Maste static bool	Bflag;		/* show information about bpf consumers */
200*c032fb85SEd Maste bool	bflag;		/* show i/f total bytes in/out */
201*c032fb85SEd Maste bool	cflag;		/* show TCP congestion control stack */
202*c032fb85SEd Maste bool	Cflag;		/* show congestion control algo and vars */
203*c032fb85SEd Maste bool	dflag;		/* show i/f dropped packets */
204*c032fb85SEd Maste bool	gflag;		/* show group (multicast) routing or stats */
205*c032fb85SEd Maste bool	hflag;		/* show counters in human readable format */
206*c032fb85SEd Maste bool	iflag;		/* show interfaces */
207*c032fb85SEd Maste bool	Lflag;		/* show size of listen queues */
208*c032fb85SEd Maste bool	mflag;		/* show memory stats */
209bf10ffe1SXin LI int	noutputs = 0;	/* how much outputs before we exit */
210c2aa9174SGleb Smirnoff u_int	numeric_addr = 0; /* show addresses numerically */
211c2aa9174SGleb Smirnoff bool	numeric_port;	/* show ports numerically */
212*c032fb85SEd Maste bool	Oflag;		/* show nhgrp objects*/
213*c032fb85SEd Maste bool	oflag;		/* show nexthop objects*/
214*c032fb85SEd Maste bool	Pflag;		/* show TCP log ID */
215*c032fb85SEd Maste static bool pflag;	/* show given protocol */
216*c032fb85SEd Maste static bool Qflag;	/* show netisr information */
217*c032fb85SEd Maste bool	rflag;		/* show routing tables (or routing stats) */
218*c032fb85SEd Maste bool	Rflag;		/* show flow / RSS statistics */
219080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
220*c032fb85SEd Maste bool	Wflag;		/* wide display */
221*c032fb85SEd Maste bool	Tflag;		/* TCP Information */
222*c032fb85SEd Maste bool	xflag;		/* extra information, includes all socket buffer info */
223*c032fb85SEd Maste bool	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
main(int argc,char * argv[])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
26195968ea7SYan-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
26895968ea7SYan-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':
272*c032fb85SEd Maste 			Aflag = true;
2739b50d902SRodney W. Grimes 			break;
2749b50d902SRodney W. Grimes 		case 'a':
275*c032fb85SEd Maste 			aflag = true;
2769b50d902SRodney W. Grimes 			break;
2776b463eedSChristian S.J. Peron 		case 'B':
278*c032fb85SEd Maste 			Bflag = true;
2796b463eedSChristian S.J. Peron 			break;
280e1e293a5SDavid Greenman 		case 'b':
281*c032fb85SEd Maste 			bflag = true;
282e1e293a5SDavid Greenman 			break;
2830e5e35e3SRichard Scheffenegger 		case 'c':
284*c032fb85SEd Maste 			cflag = true;
2850e5e35e3SRichard Scheffenegger 			break;
286b98a21f6SMichael Tuexen 		case 'C':
287*c032fb85SEd Maste 			Cflag = true;
288b98a21f6SMichael Tuexen 			break;
2899b50d902SRodney W. Grimes 		case 'd':
290*c032fb85SEd Maste 			dflag = true;
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)))
29695968ea7SYan-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 {
32095968ea7SYan-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':
325*c032fb85SEd Maste 			gflag = true;
3269b50d902SRodney W. Grimes 			break;
327c2dfd19fSGleb Smirnoff 		case 'h':
328*c032fb85SEd Maste 			hflag = true;
329c2dfd19fSGleb Smirnoff 			break;
3309b50d902SRodney W. Grimes 		case 'I': {
3319b50d902SRodney W. Grimes 			char *cp;
3329b50d902SRodney W. Grimes 
333*c032fb85SEd Maste 			iflag = true;
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':
340*c032fb85SEd Maste 			iflag = true;
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
34895968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "Jail support is not compiled in");
349182e8ae2SDoug Rabson #endif
350182e8ae2SDoug Rabson 			break;
351ac55add0SGuido van Rooij 		case 'L':
352*c032fb85SEd Maste 			Lflag = true;
353ac55add0SGuido van Rooij 			break;
3549b50d902SRodney W. Grimes 		case 'M':
3559b50d902SRodney W. Grimes 			memf = optarg;
3569b50d902SRodney W. Grimes 			break;
3579b50d902SRodney W. Grimes 		case 'm':
358*c032fb85SEd Maste 			mflag = true;
3599b50d902SRodney W. Grimes 			break;
3609b50d902SRodney W. Grimes 		case 'N':
3619b50d902SRodney W. Grimes 			nlistf = optarg;
3629b50d902SRodney W. Grimes 			break;
3639b50d902SRodney W. Grimes 		case 'n':
364c2aa9174SGleb Smirnoff 			numeric_addr++;
365c2aa9174SGleb Smirnoff 			numeric_port = true;
3669b50d902SRodney W. Grimes 			break;
367a6663252SAlexander V. Chernikov 		case 'o':
368*c032fb85SEd Maste 			oflag = true;
369a6663252SAlexander V. Chernikov 			break;
370fedeb08bSAlexander V. Chernikov 		case 'O':
371*c032fb85SEd Maste 			Oflag = true;
372fedeb08bSAlexander V. Chernikov 			break;
3732529f56eSJonathan T. Looney 		case 'P':
374*c032fb85SEd Maste 			Pflag = true;
3752529f56eSJonathan T. Looney 			break;
3769b50d902SRodney W. Grimes 		case 'p':
3779b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
37895968ea7SYan-Hao Wang 				xo_errx(EX_DATAERR, "%s: unknown or uninstrumented "
379ade9ccfeSMarcel Moolenaar 				    "protocol", optarg);
3809b50d902SRodney W. Grimes 			}
381*c032fb85SEd Maste 			pflag = true;
3829b50d902SRodney W. Grimes 			break;
3830153eb66SRobert Watson 		case 'Q':
384*c032fb85SEd Maste 			Qflag = true;
3850153eb66SRobert Watson 			break;
386bf10ffe1SXin LI 		case 'q':
387bf10ffe1SXin LI 			noutputs = atoi(optarg);
388bf10ffe1SXin LI 			if (noutputs != 0)
389bf10ffe1SXin LI 				noutputs++;
390bf10ffe1SXin LI 			break;
3919b50d902SRodney W. Grimes 		case 'r':
392*c032fb85SEd Maste 			rflag = true;
3939b50d902SRodney W. Grimes 			break;
39485b0f0f3SAdrian Chadd 		case 'R':
395*c032fb85SEd Maste 			Rflag = true;
39685b0f0f3SAdrian Chadd 			break;
3979b50d902SRodney W. Grimes 		case 's':
3989b50d902SRodney W. Grimes 			++sflag;
3999b50d902SRodney W. Grimes 			break;
40065ea0024SAssar Westerlund 		case 'S':
40165ea0024SAssar Westerlund 			numeric_addr = 1;
40265ea0024SAssar Westerlund 			break;
4039b50d902SRodney W. Grimes 		case 'u':
4049b50d902SRodney W. Grimes 			af = AF_UNIX;
4059b50d902SRodney W. Grimes 			break;
406080b7f49SDag-Erling Smørgrav 		case 'W':
40770057abfSRuslan Ermilov 		case 'l':
408*c032fb85SEd Maste 			Wflag = true;
409080b7f49SDag-Erling Smørgrav 			break;
4109b50d902SRodney W. Grimes 		case 'w':
4119b50d902SRodney W. Grimes 			interval = atoi(optarg);
412*c032fb85SEd Maste 			iflag = true;
4139b50d902SRodney W. Grimes 			break;
414f5d34df5SGeorge V. Neville-Neil 		case 'T':
415*c032fb85SEd Maste 			Tflag = true;
416f5d34df5SGeorge V. Neville-Neil 			break;
41749f287f8SGeorge V. Neville-Neil 		case 'x':
418*c032fb85SEd Maste 			xflag = true;
41949f287f8SGeorge V. Neville-Neil 			break;
420c73d99b5SRuslan Ermilov 		case 'z':
421*c032fb85SEd Maste 			zflag = true;
422c73d99b5SRuslan Ermilov 			break;
4239b50d902SRodney W. Grimes 		case '?':
4249b50d902SRodney W. Grimes 		default:
4259b50d902SRodney W. Grimes 			usage();
4269b50d902SRodney W. Grimes 		}
4279b50d902SRodney W. Grimes 	argv += optind;
4289b50d902SRodney W. Grimes 	argc -= optind;
4299b50d902SRodney W. Grimes 
4309b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4319b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4329b50d902SRodney W. Grimes 	if (*argv) {
4339b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4349b50d902SRodney W. Grimes 			interval = atoi(*argv);
4359b50d902SRodney W. Grimes 			if (interval <= 0)
4369b50d902SRodney W. Grimes 				usage();
4379b50d902SRodney W. Grimes 			++argv;
438*c032fb85SEd Maste 			iflag = true;
4399b50d902SRodney W. Grimes 		}
4409b50d902SRodney W. Grimes 		if (*argv) {
4419b50d902SRodney W. Grimes 			nlistf = *argv;
4429b50d902SRodney W. Grimes 			if (*++argv)
4439b50d902SRodney W. Grimes 				memf = *argv;
4449b50d902SRodney W. Grimes 		}
4459b50d902SRodney W. Grimes 	}
4469b50d902SRodney W. Grimes #endif
4479b50d902SRodney W. Grimes 
448182e8ae2SDoug Rabson #ifdef JAIL
449182e8ae2SDoug Rabson 	if (jail_name != NULL) {
450182e8ae2SDoug Rabson 		jid = jail_getid(jail_name);
451182e8ae2SDoug Rabson 		if (jid == -1)
45295968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "Jail not found");
453182e8ae2SDoug Rabson 		if (jail_attach(jid) != 0)
45495968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "Cannot attach to jail");
455182e8ae2SDoug Rabson 	}
456182e8ae2SDoug Rabson #endif
457182e8ae2SDoug Rabson 
4589b50d902SRodney W. Grimes 	/*
4599b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4609b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4619b50d902SRodney W. Grimes 	 */
462feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
463a4a889a0SXin LI 	if (!live) {
464a4a889a0SXin LI 		if (setgid(getgid()) != 0)
46595968ea7SYan-Hao Wang 			xo_err(EX_OSERR, "setgid");
4661caaf3eaSBaptiste Daroussin 		/* Load all necessary kvm symbols */
4671caaf3eaSBaptiste Daroussin 		kresolve_list(nl);
468a4a889a0SXin LI 	}
4699b50d902SRodney W. Grimes 
470f5d34df5SGeorge V. Neville-Neil 	if (xflag && Tflag)
47195968ea7SYan-Hao Wang 		xo_errx(EX_USAGE, "-x and -T are incompatible, pick one.");
472f5d34df5SGeorge V. Neville-Neil 
4736b463eedSChristian S.J. Peron 	if (Bflag) {
474feda1a43SJohn Baldwin 		if (!live)
475feda1a43SJohn Baldwin 			usage();
4766b463eedSChristian S.J. Peron 		bpf_stats(interface);
47795968ea7SYan-Hao Wang 		if (xo_finish() < 0)
47895968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
47995968ea7SYan-Hao Wang 		exit(EX_OK);
4806b463eedSChristian S.J. Peron 	}
4819b50d902SRodney W. Grimes 	if (mflag) {
48283708764SRuslan Ermilov 		if (!live) {
483feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
48405d1f5bcSAndrey V. Elsukov 				mbpr(kvmd, nl[N_SFSTAT].n_value);
485d15c5f56SRuslan Ermilov 		} else
486d4426f28SRobert Watson 			mbpr(NULL, 0);
48795968ea7SYan-Hao Wang 		if (xo_finish() < 0)
48895968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
48995968ea7SYan-Hao Wang 		exit(EX_OK);
4909b50d902SRodney W. Grimes 	}
4910153eb66SRobert Watson 	if (Qflag) {
49288737be2SRobert Watson 		if (!live) {
49388737be2SRobert Watson 			if (kread(0, NULL, 0) == 0)
49481dacd8bSHiroki Sato 				netisr_stats();
49588737be2SRobert Watson 		} else
49681dacd8bSHiroki Sato 			netisr_stats();
49795968ea7SYan-Hao Wang 		if (xo_finish() < 0)
49895968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
49995968ea7SYan-Hao Wang 		exit(EX_OK);
5000153eb66SRobert Watson 	}
501cc63cd56SPeter Wemm #if 0
5029b50d902SRodney W. Grimes 	/*
5039b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5049b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5059b50d902SRodney W. Grimes 	 */
5069b50d902SRodney W. Grimes 	sethostent(1);
5079b50d902SRodney W. Grimes 	setnetent(1);
508cc63cd56SPeter Wemm #else
509cc63cd56SPeter Wemm 	/*
510cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
511cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
512cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
513cc63cd56SPeter Wemm 	 */
514cc63cd56SPeter Wemm #endif
515cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
516ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5175c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
51810d5269fSHiroki Sato 		intpr(NULL, af);
519ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
52095968ea7SYan-Hao Wang 		if (xo_finish() < 0)
52195968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
52295968ea7SYan-Hao Wang 		exit(EX_OK);
5239b50d902SRodney W. Grimes 	}
5249b50d902SRodney W. Grimes 	if (rflag) {
525ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5265c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
527f3a097d0SKyle Evans 		if (sflag)
528fc47e028SAlexander V. Chernikov 			rt_stats();
529f3a097d0SKyle Evans 		else
530fc47e028SAlexander V. Chernikov 			routepr(fib, af);
531ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
53295968ea7SYan-Hao Wang 		if (xo_finish() < 0)
53395968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
53495968ea7SYan-Hao Wang 		exit(EX_OK);
5359b50d902SRodney W. Grimes 	}
536a6663252SAlexander V. Chernikov 	if (oflag) {
537a6663252SAlexander V. Chernikov 		xo_open_container("statistics");
5385c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
539a6663252SAlexander V. Chernikov 		nhops_print(fib, af);
540a6663252SAlexander V. Chernikov 		xo_close_container("statistics");
54195968ea7SYan-Hao Wang 		if (xo_finish() < 0)
54295968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
54395968ea7SYan-Hao Wang 		exit(EX_OK);
544a6663252SAlexander V. Chernikov 	}
545fedeb08bSAlexander V. Chernikov 	if (Oflag) {
546fedeb08bSAlexander V. Chernikov 		xo_open_container("statistics");
5475c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
548fedeb08bSAlexander V. Chernikov 		nhgrp_print(fib, af);
549fedeb08bSAlexander V. Chernikov 		xo_close_container("statistics");
55095968ea7SYan-Hao Wang 		if (xo_finish() < 0)
55195968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
55295968ea7SYan-Hao Wang 		exit(EX_OK);
553fedeb08bSAlexander V. Chernikov 	}
554fedeb08bSAlexander V. Chernikov 
555a6663252SAlexander V. Chernikov 
556fc47e028SAlexander V. Chernikov 
5579b50d902SRodney W. Grimes 	if (gflag) {
558ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5595c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
560cfa1ca9dSYoshinobu Inoue 		if (sflag) {
561cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
562fc47e028SAlexander V. Chernikov 				mrt_stats();
563cfa1ca9dSYoshinobu Inoue #ifdef INET6
564cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
565fc47e028SAlexander V. Chernikov 				mrt6_stats();
566cfa1ca9dSYoshinobu Inoue #endif
567cfa1ca9dSYoshinobu Inoue 		} else {
568cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
569fc47e028SAlexander V. Chernikov 				mroutepr();
570cfa1ca9dSYoshinobu Inoue #ifdef INET6
571cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
572fc47e028SAlexander V. Chernikov 				mroute6pr();
573cfa1ca9dSYoshinobu Inoue #endif
574cfa1ca9dSYoshinobu Inoue 		}
575ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
57695968ea7SYan-Hao Wang 		if (xo_finish() < 0)
57795968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
57895968ea7SYan-Hao Wang 		exit(EX_OK);
5799b50d902SRodney W. Grimes 	}
580cfa1ca9dSYoshinobu Inoue 
581cf5e44f8SRuslan Ermilov 	if (tp) {
582ade9ccfeSMarcel Moolenaar 		xo_open_container("statistics");
5835c4f64bdSBram 		xo_set_version(NETSTAT_XO_VERSION);
584ade9ccfeSMarcel Moolenaar 		printproto(tp, tp->pr_name, &first);
585ade9ccfeSMarcel Moolenaar 		if (!first)
586ade9ccfeSMarcel Moolenaar 			xo_close_list("socket");
587ade9ccfeSMarcel Moolenaar 		xo_close_container("statistics");
58895968ea7SYan-Hao Wang 		if (xo_finish() < 0)
58995968ea7SYan-Hao Wang 			xo_err(EX_IOERR, "stdout");
59095968ea7SYan-Hao Wang 		exit(EX_OK);
591cf5e44f8SRuslan Ermilov 	}
592ade9ccfeSMarcel Moolenaar 
593ade9ccfeSMarcel Moolenaar 	xo_open_container("statistics");
5945c4f64bdSBram 	xo_set_version(NETSTAT_XO_VERSION);
595cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5969b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
597ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
598cfa1ca9dSYoshinobu Inoue #ifdef INET6
599cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
600cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
601ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
602cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
6033b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
6043b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
6053b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
606ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
6073b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
608690f477dSSam Leffler #ifdef NETGRAPH
6094cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
6104cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
611ade9ccfeSMarcel Moolenaar 			printproto(tp, tp->pr_name, &first);
612690f477dSSam Leffler #endif /* NETGRAPH */
613f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
614feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
615963b7ccdSRobert Watson 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
616ade9ccfeSMarcel Moolenaar 		    nl[N_UNP_SPHEAD].n_value, &first);
617ade9ccfeSMarcel Moolenaar 
618ade9ccfeSMarcel Moolenaar 	if (!first)
619ade9ccfeSMarcel Moolenaar 		xo_close_list("socket");
620ade9ccfeSMarcel Moolenaar 	xo_close_container("statistics");
62195968ea7SYan-Hao Wang 	if (xo_finish() < 0)
62295968ea7SYan-Hao Wang 		xo_err(EX_IOERR, "stdout");
62395968ea7SYan-Hao Wang 	exit(EX_OK);
6249b50d902SRodney W. Grimes }
6259b50d902SRodney W. Grimes 
626dbfd8708SGleb Smirnoff static int
fetch_stats_internal(const char * sysctlname,u_long off,void * stats,size_t len,kreadfn_t kreadfn,int zero)627dbfd8708SGleb Smirnoff fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
628dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn, int zero)
6299eddb899SMark Johnston {
6309eddb899SMark Johnston 	int error;
6319eddb899SMark Johnston 
6329eddb899SMark Johnston 	if (live) {
6339eddb899SMark Johnston 		memset(stats, 0, len);
634dbfd8708SGleb Smirnoff 		if (zero)
6359eddb899SMark Johnston 			error = sysctlbyname(sysctlname, NULL, NULL, stats,
6369eddb899SMark Johnston 			    len);
6379eddb899SMark Johnston 		else
6389eddb899SMark Johnston 			error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
6399eddb899SMark Johnston 		if (error == -1 && errno != ENOENT)
6409eddb899SMark Johnston 			xo_warn("sysctl %s", sysctlname);
6419eddb899SMark Johnston 	} else {
6429eddb899SMark Johnston 		if (off == 0)
6439eddb899SMark Johnston 			return (1);
6449eddb899SMark Johnston 		error = kreadfn(off, stats, len);
6459eddb899SMark Johnston 	}
6469eddb899SMark Johnston 	return (error);
6479eddb899SMark Johnston }
6489eddb899SMark Johnston 
649dbfd8708SGleb Smirnoff int
fetch_stats(const char * sysctlname,u_long off,void * stats,size_t len,kreadfn_t kreadfn)650dbfd8708SGleb Smirnoff fetch_stats(const char *sysctlname, u_long off, void *stats,
651dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
652dbfd8708SGleb Smirnoff {
653dbfd8708SGleb Smirnoff 
654dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
655dbfd8708SGleb Smirnoff     zflag));
656dbfd8708SGleb Smirnoff }
657dbfd8708SGleb Smirnoff 
658dbfd8708SGleb Smirnoff int
fetch_stats_ro(const char * sysctlname,u_long off,void * stats,size_t len,kreadfn_t kreadfn)659dbfd8708SGleb Smirnoff fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
660dbfd8708SGleb Smirnoff     size_t len, kreadfn_t kreadfn)
661dbfd8708SGleb Smirnoff {
662dbfd8708SGleb Smirnoff 
663dbfd8708SGleb Smirnoff 	return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
664dbfd8708SGleb Smirnoff }
665dbfd8708SGleb Smirnoff 
6669b50d902SRodney W. Grimes /*
6679b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6689b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6699b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6709b50d902SRodney W. Grimes  */
6719b50d902SRodney W. Grimes static void
printproto(struct protox * tp,const char * name,bool * first)672ade9ccfeSMarcel Moolenaar printproto(struct protox *tp, const char *name, bool *first)
6739b50d902SRodney W. Grimes {
674feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6759b50d902SRodney W. Grimes 	u_long off;
676ade9ccfeSMarcel Moolenaar 	bool doingdblocks = false;
6779b50d902SRodney W. Grimes 
6789b50d902SRodney W. Grimes 	if (sflag) {
679cfa1ca9dSYoshinobu Inoue 		if (iflag) {
680cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
68110d5269fSHiroki Sato 				intpr(tp->pr_istats, af);
682cf5e44f8SRuslan Ermilov 			else if (pflag)
683ade9ccfeSMarcel Moolenaar 				xo_message("%s: no per-interface stats routine",
684cf5e44f8SRuslan Ermilov 				    tp->pr_name);
685cfa1ca9dSYoshinobu Inoue 			return;
686feda1a43SJohn Baldwin 		} else {
6879b50d902SRodney W. Grimes 			pr = tp->pr_stats;
688cf5e44f8SRuslan Ermilov 			if (!pr) {
689cf5e44f8SRuslan Ermilov 				if (pflag)
690ade9ccfeSMarcel Moolenaar 					xo_message("%s: no stats routine",
691cf5e44f8SRuslan Ermilov 					    tp->pr_name);
692cf5e44f8SRuslan Ermilov 				return;
693cf5e44f8SRuslan Ermilov 			}
694feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
695feda1a43SJohn Baldwin 				off = 0;
696feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
697feda1a43SJohn Baldwin 				if (pflag)
698ade9ccfeSMarcel Moolenaar 					xo_message("%s: stats routine doesn't "
699ade9ccfeSMarcel Moolenaar 					    "work on cores", tp->pr_name);
700feda1a43SJohn Baldwin 				return;
701feda1a43SJohn Baldwin 			} else
702feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
703cfa1ca9dSYoshinobu Inoue 		}
7049b50d902SRodney W. Grimes 	} else {
705ade9ccfeSMarcel Moolenaar 		doingdblocks = true;
7069b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
707cf5e44f8SRuslan Ermilov 		if (!pr) {
708cf5e44f8SRuslan Ermilov 			if (pflag)
709ade9ccfeSMarcel Moolenaar 				xo_message("%s: no PCB routine", tp->pr_name);
710cf5e44f8SRuslan Ermilov 			return;
711cf5e44f8SRuslan Ermilov 		}
712feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
713feda1a43SJohn Baldwin 			off = 0;
714feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
715feda1a43SJohn Baldwin 			if (pflag)
716ade9ccfeSMarcel Moolenaar 				xo_message("%s: PCB routine doesn't work on "
717ade9ccfeSMarcel Moolenaar 				    "cores", tp->pr_name);
718feda1a43SJohn Baldwin 			return;
719feda1a43SJohn Baldwin 		} else
720feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
7219b50d902SRodney W. Grimes 	}
722feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
723ade9ccfeSMarcel Moolenaar 	    af != AF_UNSPEC)) {
724ade9ccfeSMarcel Moolenaar 		if (doingdblocks && *first) {
725ade9ccfeSMarcel Moolenaar 			xo_open_list("socket");
726ade9ccfeSMarcel Moolenaar 			*first = false;
727ade9ccfeSMarcel Moolenaar 		}
728ade9ccfeSMarcel Moolenaar 
729feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
7309b50d902SRodney W. Grimes 	}
731ade9ccfeSMarcel Moolenaar }
7329b50d902SRodney W. Grimes 
73329dde48dSGleb Smirnoff static int
kvmd_init(void)73429dde48dSGleb Smirnoff kvmd_init(void)
7359b50d902SRodney W. Grimes {
736feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
737feda1a43SJohn Baldwin 
73829dde48dSGleb Smirnoff 	if (kvmd != NULL)
73929dde48dSGleb Smirnoff 		return (0);
74029dde48dSGleb Smirnoff 
741feda1a43SJohn Baldwin 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
742a4a889a0SXin LI 	if (setgid(getgid()) != 0)
74395968ea7SYan-Hao Wang 		xo_err(EX_OSERR, "setgid");
74429dde48dSGleb Smirnoff 
74529dde48dSGleb Smirnoff 	if (kvmd == NULL) {
746ade9ccfeSMarcel Moolenaar 		xo_warnx("kvm not available: %s", errbuf);
74729dde48dSGleb Smirnoff 		return (-1);
74829dde48dSGleb Smirnoff 	}
74929dde48dSGleb Smirnoff 
750fc47e028SAlexander V. Chernikov 	return (0);
751fc47e028SAlexander V. Chernikov }
752fc47e028SAlexander V. Chernikov 
753fc47e028SAlexander V. Chernikov /*
754fc47e028SAlexander V. Chernikov  * Resolve symbol list, return 0 on success.
755fc47e028SAlexander V. Chernikov  */
75681dacd8bSHiroki Sato static int
kresolve_list(struct nlist * _nl)757fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl)
758fc47e028SAlexander V. Chernikov {
759fc47e028SAlexander V. Chernikov 
760fc47e028SAlexander V. Chernikov 	if ((kvmd == NULL) && (kvmd_init() != 0))
761fc47e028SAlexander V. Chernikov 		return (-1);
762fc47e028SAlexander V. Chernikov 
763fc47e028SAlexander V. Chernikov 	if (_nl[0].n_type != 0)
764fc47e028SAlexander V. Chernikov 		return (0);
765fc47e028SAlexander V. Chernikov 
766fc47e028SAlexander V. Chernikov 	if (kvm_nlist(kvmd, _nl) < 0) {
76799453c6aSPoul-Henning Kamp 		if (nlistf)
76895968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "%s: kvm_nlist: %s", nlistf,
76999453c6aSPoul-Henning Kamp 			    kvm_geterr(kvmd));
77099453c6aSPoul-Henning Kamp 		else
77195968ea7SYan-Hao Wang 			xo_errx(EX_UNAVAILABLE, "kvm_nlist: %s", kvm_geterr(kvmd));
77299453c6aSPoul-Henning Kamp 	}
77399453c6aSPoul-Henning Kamp 
77429dde48dSGleb Smirnoff 	return (0);
77529dde48dSGleb Smirnoff }
77629dde48dSGleb Smirnoff 
77729dde48dSGleb Smirnoff /*
77881dacd8bSHiroki Sato  * Wrapper of kvm_dpcpu_setcpu().
77981dacd8bSHiroki Sato  */
78081dacd8bSHiroki Sato void
kset_dpcpu(u_int cpuid)78181dacd8bSHiroki Sato kset_dpcpu(u_int cpuid)
78281dacd8bSHiroki Sato {
78381dacd8bSHiroki Sato 
78481dacd8bSHiroki Sato 	if ((kvmd == NULL) && (kvmd_init() != 0))
78595968ea7SYan-Hao Wang 		xo_errx(EX_UNAVAILABLE, "%s: kvm is not available", __func__);
78681dacd8bSHiroki Sato 
78781dacd8bSHiroki Sato 	if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
78895968ea7SYan-Hao Wang 		xo_errx(EX_UNAVAILABLE, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
78981dacd8bSHiroki Sato 		    cpuid, kvm_geterr(kvmd));
79081dacd8bSHiroki Sato 	return;
79181dacd8bSHiroki Sato }
79281dacd8bSHiroki Sato 
79381dacd8bSHiroki Sato /*
79429dde48dSGleb Smirnoff  * Read kernel memory, return 0 on success.
79529dde48dSGleb Smirnoff  */
79629dde48dSGleb Smirnoff int
kread(u_long addr,void * buf,size_t size)79729dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size)
79829dde48dSGleb Smirnoff {
79929dde48dSGleb Smirnoff 
80029dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
80199453c6aSPoul-Henning Kamp 		return (-1);
80229dde48dSGleb Smirnoff 
803c6620a13SPoul-Henning Kamp 	if (!buf)
804c6620a13SPoul-Henning Kamp 		return (0);
805feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
806ade9ccfeSMarcel Moolenaar 		xo_warnx("%s", kvm_geterr(kvmd));
8079b50d902SRodney W. Grimes 		return (-1);
8089b50d902SRodney W. Grimes 	}
8099b50d902SRodney W. Grimes 	return (0);
8109b50d902SRodney W. Grimes }
8119b50d902SRodney W. Grimes 
81229dde48dSGleb Smirnoff /*
813e3a7aa6fSGleb Smirnoff  * Read single counter(9).
814e3a7aa6fSGleb Smirnoff  */
815e3a7aa6fSGleb Smirnoff uint64_t
kread_counter(u_long addr)816e3a7aa6fSGleb Smirnoff kread_counter(u_long addr)
817e3a7aa6fSGleb Smirnoff {
818e3a7aa6fSGleb Smirnoff 
819e3a7aa6fSGleb Smirnoff 	if (kvmd_init() < 0)
820e3a7aa6fSGleb Smirnoff 		return (-1);
821e3a7aa6fSGleb Smirnoff 
822e3a7aa6fSGleb Smirnoff 	return (kvm_counter_u64_fetch(kvmd, addr));
823e3a7aa6fSGleb Smirnoff }
824e3a7aa6fSGleb Smirnoff 
825e3a7aa6fSGleb Smirnoff /*
82629dde48dSGleb Smirnoff  * Read an array of N counters in kernel memory into array of N uint64_t's.
82729dde48dSGleb Smirnoff  */
82829dde48dSGleb Smirnoff int
kread_counters(u_long addr,void * buf,size_t size)8295da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size)
83029dde48dSGleb Smirnoff {
831794c57d3SMark Johnston 	uint64_t *c;
832794c57d3SMark Johnston 	u_long *counters;
833794c57d3SMark Johnston 	size_t i, n;
83429dde48dSGleb Smirnoff 
83529dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
83629dde48dSGleb Smirnoff 		return (-1);
83729dde48dSGleb Smirnoff 
838794c57d3SMark Johnston 	if (size % sizeof(uint64_t) != 0) {
839794c57d3SMark Johnston 		xo_warnx("kread_counters: invalid counter set size");
8405da0521fSAndrey V. Elsukov 		return (-1);
8415da0521fSAndrey V. Elsukov 	}
842794c57d3SMark Johnston 
843794c57d3SMark Johnston 	n = size / sizeof(uint64_t);
844794c57d3SMark Johnston 	if ((counters = malloc(n * sizeof(u_long))) == NULL)
84595968ea7SYan-Hao Wang 		xo_err(EX_OSERR, "malloc");
846794c57d3SMark Johnston 	if (kread(addr, counters, n * sizeof(u_long)) < 0) {
847794c57d3SMark Johnston 		free(counters);
848794c57d3SMark Johnston 		return (-1);
849794c57d3SMark Johnston 	}
850794c57d3SMark Johnston 
851794c57d3SMark Johnston 	c = buf;
852794c57d3SMark Johnston 	for (i = 0; i < n; i++)
853794c57d3SMark Johnston 		c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
854794c57d3SMark Johnston 
855794c57d3SMark Johnston 	free(counters);
85629dde48dSGleb Smirnoff 	return (0);
85729dde48dSGleb Smirnoff }
85829dde48dSGleb Smirnoff 
859a01e3379SDavid Malone const char *
plural(uintmax_t n)8607b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
8619b50d902SRodney W. Grimes {
8629b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
8639b50d902SRodney W. Grimes }
8649b50d902SRodney W. Grimes 
865a01e3379SDavid Malone const char *
plurales(uintmax_t n)8667b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
8679b50d902SRodney W. Grimes {
8689b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
8699b50d902SRodney W. Grimes }
8709b50d902SRodney W. Grimes 
871f99a4046SMike Makonnen const char *
pluralies(uintmax_t n)8727b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
873f99a4046SMike Makonnen {
874f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
875f99a4046SMike Makonnen }
876f99a4046SMike Makonnen 
8779b50d902SRodney W. Grimes /*
8789b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
8799b50d902SRodney W. Grimes  */
8809b50d902SRodney W. Grimes static struct protox *
knownname(const char * name)881096146f8SYaroslav Tykhiy knownname(const char *name)
8829b50d902SRodney W. Grimes {
8839b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
8849b50d902SRodney W. Grimes 
8859b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
8869b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
8879b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
8889b50d902SRodney W. Grimes 				return (tp);
8899b50d902SRodney W. Grimes 	return (NULL);
8909b50d902SRodney W. Grimes }
8919b50d902SRodney W. Grimes 
8929b50d902SRodney W. Grimes /*
8939b50d902SRodney W. Grimes  * Find the protox corresponding to name.
8949b50d902SRodney W. Grimes  */
8959b50d902SRodney W. Grimes static struct protox *
name2protox(const char * name)896096146f8SYaroslav Tykhiy name2protox(const char *name)
8979b50d902SRodney W. Grimes {
8989b50d902SRodney W. Grimes 	struct protox *tp;
8999b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
9009b50d902SRodney W. Grimes 	struct protoent *p;
9019b50d902SRodney W. Grimes 
9029b50d902SRodney W. Grimes 	/*
9039b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
9049b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
9059b50d902SRodney W. Grimes 	 */
906cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
9079b50d902SRodney W. Grimes 		return (tp);
9089b50d902SRodney W. Grimes 
9099b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
910cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
9119b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
9129b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
9139b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
9149b50d902SRodney W. Grimes 				endprotoent();
9159b50d902SRodney W. Grimes 				return (knownname(p->p_name));
9169b50d902SRodney W. Grimes 			}
9179b50d902SRodney W. Grimes 	}
9189b50d902SRodney W. Grimes 	endprotoent();
9199b50d902SRodney W. Grimes 	return (NULL);
9209b50d902SRodney W. Grimes }
9219b50d902SRodney W. Grimes 
9229b50d902SRodney W. Grimes static void
usage(void)9235e051718SAssar Westerlund usage(void)
9249b50d902SRodney W. Grimes {
92595968ea7SYan-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",
926182e8ae2SDoug Rabson "usage: netstat [-j jail] [-46AaCcLnRSTWx] [-f protocol_family | -p protocol]\n"
9271cb467b1SRuslan Ermilov "               [-M core] [-N system]",
928182e8ae2SDoug Rabson "       netstat [-j jail] -i | -I interface [-46abdhnW] [-f address_family]\n"
929d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
930182e8ae2SDoug Rabson "       netstat [-j jail] -w wait [-I interface] [-46d] [-M core] [-N system]\n"
931fd2c6bc9SAllan Jude "               [-q howmany]",
932182e8ae2SDoug Rabson "       netstat [-j jail] -s [-46sz] [-f protocol_family | -p protocol]\n"
93382d383bcSRuslan Ermilov "               [-M core] [-N system]",
934182e8ae2SDoug Rabson "       netstat [-j jail] -i | -I interface -s [-46s]\n"
935fd2c6bc9SAllan Jude "               [-f protocol_family | -p protocol] [-M core] [-N system]",
936182e8ae2SDoug Rabson "       netstat [-j jail] -m [-M core] [-N system]",
937182e8ae2SDoug Rabson "       netstat [-j jail] -B [-z] [-I interface]",
938182e8ae2SDoug Rabson "       netstat [-j jail] -r [-46AnW] [-F fibnum] [-f address_family]\n"
939fd2c6bc9SAllan Jude "               [-M core] [-N system]",
940182e8ae2SDoug Rabson "       netstat [-j jail] -rs [-s] [-M core] [-N system]",
941182e8ae2SDoug Rabson "       netstat [-j jail] -g [-46W] [-f address_family] [-M core] [-N system]",
942182e8ae2SDoug Rabson "       netstat [-j jail] -gs [-46s] [-f address_family] [-M core] [-N system]",
943182e8ae2SDoug Rabson "       netstat [-j jail] -Q");
94495968ea7SYan-Hao Wang 	exit(EX_USAGE);
9459b50d902SRodney W. Grimes }
946