xref: /freebsd/usr.bin/netstat/main.c (revision 17ed2e8ea8d7432c4312ef81b07236cac96d965a)
165475bc8SDavid E. O'Brien /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
39b50d902SRodney W. Grimes  *	Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
315d422d6aSPhilippe Charnier char const copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\
339b50d902SRodney W. Grimes 	Regents of the University of California.  All rights reserved.\n";
349b50d902SRodney W. Grimes #endif /* not lint */
359b50d902SRodney W. Grimes 
365d422d6aSPhilippe Charnier #if 0
376cc6f122SPhilippe Charnier #ifndef lint
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
399b50d902SRodney W. Grimes #endif /* not lint */
406cc6f122SPhilippe Charnier #endif
416cc6f122SPhilippe Charnier 
426cc6f122SPhilippe Charnier #include <sys/cdefs.h>
436cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/param.h>
469b50d902SRodney W. Grimes #include <sys/file.h>
479b50d902SRodney W. Grimes #include <sys/protosw.h>
489b50d902SRodney W. Grimes #include <sys/socket.h>
49feda1a43SJohn Baldwin #include <sys/socketvar.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <netinet/in.h>
529b50d902SRodney W. Grimes 
53690f477dSSam Leffler #ifdef NETGRAPH
544cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
55690f477dSSam Leffler #endif
564cf49a43SJulian Elischer 
579b50d902SRodney W. Grimes #include <ctype.h>
585d422d6aSPhilippe Charnier #include <err.h>
59821df508SXin LI #include <errno.h>
609b50d902SRodney W. Grimes #include <kvm.h>
619b50d902SRodney W. Grimes #include <limits.h>
629b50d902SRodney W. Grimes #include <netdb.h>
639b50d902SRodney W. Grimes #include <nlist.h>
64821df508SXin LI #include <paths.h>
657b95a1ebSYaroslav Tykhiy #include <stdint.h>
669b50d902SRodney W. Grimes #include <stdio.h>
679b50d902SRodney W. Grimes #include <stdlib.h>
689b50d902SRodney W. Grimes #include <string.h>
699b50d902SRodney W. Grimes #include <unistd.h>
709b50d902SRodney W. Grimes #include "netstat.h"
719b50d902SRodney W. Grimes 
72c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
734f81ef50SGarrett Wollman #define	N_IFNET		0
7484c1edcbSGleb Smirnoff 	{ .n_name = "_ifnet" },		/* XXXGL: can be deleted */
75ab54ea99SPeter Wemm #define	N_RTSTAT	1
76096146f8SYaroslav Tykhiy 	{ .n_name = "_rtstat" },
77ab54ea99SPeter Wemm #define	N_RTREE		2
78096146f8SYaroslav Tykhiy 	{ .n_name = "_rt_tables"},
79ab54ea99SPeter Wemm #define	N_MRTSTAT	3
80096146f8SYaroslav Tykhiy 	{ .n_name = "_mrtstat" },
81443fc317SBruce M Simpson #define	N_MFCHASHTBL	4
82443fc317SBruce M Simpson 	{ .n_name = "_mfchashtbl" },
83ab54ea99SPeter Wemm #define	N_VIFTABLE	5
84096146f8SYaroslav Tykhiy 	{ .n_name = "_viftable" },
85ab54ea99SPeter Wemm #define	N_IPX		6
86096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxpcb_list"},
87ab54ea99SPeter Wemm #define	N_IPXSTAT	7
88096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxstat"},
89ab54ea99SPeter Wemm #define	N_SPXSTAT	8
90096146f8SYaroslav Tykhiy 	{ .n_name = "_spx_istat"},
91ab54ea99SPeter Wemm #define	N_DDPSTAT	9
92096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpstat"},
93ab54ea99SPeter Wemm #define	N_DDPCB		10
94096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpcb"},
95ab54ea99SPeter Wemm #define	N_NGSOCKS	11
96096146f8SYaroslav Tykhiy 	{ .n_name = "_ngsocklist"},
97ab54ea99SPeter Wemm #define	N_IP6STAT	12
98096146f8SYaroslav Tykhiy 	{ .n_name = "_ip6stat" },
99ab54ea99SPeter Wemm #define	N_ICMP6STAT	13
100096146f8SYaroslav Tykhiy 	{ .n_name = "_icmp6stat" },
101ab54ea99SPeter Wemm #define	N_IPSECSTAT	14
1028409aedfSGeorge V. Neville-Neil 	{ .n_name = "_ipsec4stat" },
103ab54ea99SPeter Wemm #define	N_IPSEC6STAT	15
104096146f8SYaroslav Tykhiy 	{ .n_name = "_ipsec6stat" },
105ab54ea99SPeter Wemm #define	N_PIM6STAT	16
106096146f8SYaroslav Tykhiy 	{ .n_name = "_pim6stat" },
107ab54ea99SPeter Wemm #define	N_MRT6STAT	17
108096146f8SYaroslav Tykhiy 	{ .n_name = "_mrt6stat" },
109ab54ea99SPeter Wemm #define	N_MF6CTABLE	18
110096146f8SYaroslav Tykhiy 	{ .n_name = "_mf6ctable" },
111ab54ea99SPeter Wemm #define	N_MIF6TABLE	19
112096146f8SYaroslav Tykhiy 	{ .n_name = "_mif6table" },
113ab54ea99SPeter Wemm #define	N_PFKEYSTAT	20
114096146f8SYaroslav Tykhiy 	{ .n_name = "_pfkeystat" },
115c780f378SGleb Smirnoff #define	N_RTTRASH	21
116096146f8SYaroslav Tykhiy 	{ .n_name = "_rttrash" },
117c780f378SGleb Smirnoff #define	N_CARPSTAT	22
118096146f8SYaroslav Tykhiy 	{ .n_name = "_carpstats" },
119c780f378SGleb Smirnoff #define	N_PFSYNCSTAT	23
120096146f8SYaroslav Tykhiy 	{ .n_name = "_pfsyncstats" },
121c780f378SGleb Smirnoff #define	N_AHSTAT	24
122096146f8SYaroslav Tykhiy 	{ .n_name = "_ahstat" },
123c780f378SGleb Smirnoff #define	N_ESPSTAT	25
124096146f8SYaroslav Tykhiy 	{ .n_name = "_espstat" },
125c780f378SGleb Smirnoff #define	N_IPCOMPSTAT	26
126096146f8SYaroslav Tykhiy 	{ .n_name = "_ipcompstat" },
127c780f378SGleb Smirnoff #define	N_TCPSTAT	27
1285da0521fSAndrey V. Elsukov 	{ .n_name = "_tcpstat" },
129c780f378SGleb Smirnoff #define	N_UDPSTAT	28
130feda1a43SJohn Baldwin 	{ .n_name = "_udpstat" },
131c780f378SGleb Smirnoff #define	N_IPSTAT	29
1325da0521fSAndrey V. Elsukov 	{ .n_name = "_ipstat" },
133c780f378SGleb Smirnoff #define	N_ICMPSTAT	30
134feda1a43SJohn Baldwin 	{ .n_name = "_icmpstat" },
135c780f378SGleb Smirnoff #define	N_IGMPSTAT	31
136feda1a43SJohn Baldwin 	{ .n_name = "_igmpstat" },
137c780f378SGleb Smirnoff #define	N_PIMSTAT	32
138feda1a43SJohn Baldwin 	{ .n_name = "_pimstat" },
139c780f378SGleb Smirnoff #define	N_TCBINFO	33
140feda1a43SJohn Baldwin 	{ .n_name = "_tcbinfo" },
141c780f378SGleb Smirnoff #define	N_UDBINFO	34
142feda1a43SJohn Baldwin 	{ .n_name = "_udbinfo" },
143c780f378SGleb Smirnoff #define	N_DIVCBINFO	35
144feda1a43SJohn Baldwin 	{ .n_name = "_divcbinfo" },
145c780f378SGleb Smirnoff #define	N_RIPCBINFO	36
146feda1a43SJohn Baldwin 	{ .n_name = "_ripcbinfo" },
147c780f378SGleb Smirnoff #define	N_UNP_COUNT	37
148feda1a43SJohn Baldwin 	{ .n_name = "_unp_count" },
149c780f378SGleb Smirnoff #define	N_UNP_GENCNT	38
150feda1a43SJohn Baldwin 	{ .n_name = "_unp_gencnt" },
151c780f378SGleb Smirnoff #define	N_UNP_DHEAD	39
152feda1a43SJohn Baldwin 	{ .n_name = "_unp_dhead" },
153c780f378SGleb Smirnoff #define	N_UNP_SHEAD	40
154feda1a43SJohn Baldwin 	{ .n_name = "_unp_shead" },
155c780f378SGleb Smirnoff #define	N_RIP6STAT	41
156feda1a43SJohn Baldwin 	{ .n_name = "_rip6stat" },
157c780f378SGleb Smirnoff #define	N_SCTPSTAT	42
158feda1a43SJohn Baldwin 	{ .n_name = "_sctpstat" },
159c780f378SGleb Smirnoff #define	N_MFCTABLESIZE	43
160443fc317SBruce M Simpson 	{ .n_name = "_mfctablesize" },
161c780f378SGleb Smirnoff #define	N_ARPSTAT       44
16254fc657dSGeorge V. Neville-Neil 	{ .n_name = "_arpstat" },
163c780f378SGleb Smirnoff #define	N_UNP_SPHEAD	45
164963b7ccdSRobert Watson 	{ .n_name = "unp_sphead" },
165c780f378SGleb Smirnoff #define	N_SFSTAT	46
16605d1f5bcSAndrey V. Elsukov 	{ .n_name = "_sfstat"},
167096146f8SYaroslav Tykhiy 	{ .n_name = NULL },
1689b50d902SRodney W. Grimes };
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes struct protox {
171feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
172feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
1739b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
174feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
1755e051718SAssar Westerlund 					/* control blocks printing routine */
176feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
1775e051718SAssar Westerlund 					/* statistics printing routine */
1785e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
179fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
18055fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
181feda1a43SJohn Baldwin 	int	pr_protocol;
1829b50d902SRodney W. Grimes } protox[] = {
183feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
184feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
185feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
186feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
18774fd40c9SRandall Stewart #ifdef SCTP
188feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
189feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
19074fd40c9SRandall Stewart #endif
191aa0a1e58SJeff Roberson #ifdef SDP
192aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
193aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
194aa0a1e58SJeff Roberson #endif
195feda1a43SJohn Baldwin 	{ N_DIVCBINFO,	-1,		1,	protopr,
196feda1a43SJohn Baldwin 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
197feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
198feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
199feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
200feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
201feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
202feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
203cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2048409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
205feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
2068409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
207feda1a43SJohn Baldwin 	  ah_stats,	NULL,		"ah",	0,	0},
2088409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
209feda1a43SJohn Baldwin 	  esp_stats,	NULL,		"esp",	0,	0},
2108409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
211feda1a43SJohn Baldwin 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
212100b98dbSKelly Yancey #endif
213feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
214feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
215feda1a43SJohn Baldwin 	{ -1,		N_CARPSTAT,	1,	NULL,
216feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
2173e4d5cd3SGleb Smirnoff #ifdef PF
218feda1a43SJohn Baldwin 	{ -1,		N_PFSYNCSTAT,	1,	NULL,
219feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
2203e4d5cd3SGleb Smirnoff #endif
22154fc657dSGeorge V. Neville-Neil 	{ -1,		N_ARPSTAT,	1,	NULL,
22254fc657dSGeorge V. Neville-Neil 	  arp_stats,	NULL,		"arp", 1,	0 },
2236bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
224feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2259b50d902SRodney W. Grimes };
2269b50d902SRodney W. Grimes 
227cfa1ca9dSYoshinobu Inoue #ifdef INET6
228cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
229feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
230feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
231feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
232feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
233feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
234feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
235feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
236feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
237aa0a1e58SJeff Roberson #ifdef SDP
238aa0a1e58SJeff Roberson 	{ -1,		-1,		1,	protopr,
239aa0a1e58SJeff Roberson 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
240aa0a1e58SJeff Roberson #endif
241cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2426bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
243feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
244cfa1ca9dSYoshinobu Inoue #endif
245cfa1ca9dSYoshinobu Inoue #ifdef notyet
2466bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
247feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
248cfa1ca9dSYoshinobu Inoue #endif
249feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
250feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
2516bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
252feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
253cfa1ca9dSYoshinobu Inoue };
254cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
255cfa1ca9dSYoshinobu Inoue 
2563b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2573b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2586bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
259feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
2606bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
261feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2623b8a8567SJun-ichiro itojun Hagino };
2633b8a8567SJun-ichiro itojun Hagino #endif
2643b8a8567SJun-ichiro itojun Hagino 
26563bf4575SJulian Elischer struct protox atalkprotox[] = {
26663bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
267feda1a43SJohn Baldwin 	  ddp_stats,	NULL,		"ddp",	0,	0 },
2686bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
269feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
27063bf4575SJulian Elischer };
271690f477dSSam Leffler #ifdef NETGRAPH
2724cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2734cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
274feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
2754cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
276feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
2776bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
278feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2794cf49a43SJulian Elischer };
280690f477dSSam Leffler #endif
2819cc22e5cSPoul-Henning Kamp #ifdef IPX
282cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
283cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
284feda1a43SJohn Baldwin 	  ipx_stats,	NULL,		"ipx",	0,	0 },
285cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
286feda1a43SJohn Baldwin 	  spx_stats,	NULL,		"spx",	0,	0 },
2876bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
288feda1a43SJohn Baldwin 	  NULL,		NULL,		0,	0,	0 }
289cc6a66f2SJulian Elischer };
2909cc22e5cSPoul-Henning Kamp #endif
291cc6a66f2SJulian Elischer 
292cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
293cfa1ca9dSYoshinobu Inoue 					 protox,
294cfa1ca9dSYoshinobu Inoue #ifdef INET6
295cfa1ca9dSYoshinobu Inoue 					 ip6protox,
296cfa1ca9dSYoshinobu Inoue #endif
2973b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2983b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2993b8a8567SJun-ichiro itojun Hagino #endif
3009cc22e5cSPoul-Henning Kamp #ifdef IPX
3019cc22e5cSPoul-Henning Kamp 					 ipxprotox,
3029cc22e5cSPoul-Henning Kamp #endif
3039cc22e5cSPoul-Henning Kamp 					 atalkprotox, NULL };
3049b50d902SRodney W. Grimes 
305fa6d48c0SMark Murray static void printproto(struct protox *, const char *);
3065e051718SAssar Westerlund static void usage(void);
307096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
308096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
3099b50d902SRodney W. Grimes 
310c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
311080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
312080b7f49SDag-Erling Smørgrav 
313080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
314080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
3156b463eedSChristian S.J. Peron int	Bflag;		/* show information about bpf consumers */
316080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
317080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
318080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
319c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
320080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
321080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
322080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
323bf10ffe1SXin LI int	noutputs = 0;	/* how much outputs before we exit */
32465ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
32565ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
326cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
3270153eb66SRobert Watson int	Qflag;		/* show netisr information */
328080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
329080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
330080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
331f5d34df5SGeorge V. Neville-Neil int	Tflag;		/* TCP Information */
33249f287f8SGeorge V. Neville-Neil int	xflag;		/* extra information, includes all socket buffer info */
333c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
334080b7f49SDag-Erling Smørgrav 
335080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
336080b7f49SDag-Erling Smørgrav 
337080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
338080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
339080b7f49SDag-Erling Smørgrav 
340080b7f49SDag-Erling Smørgrav int	af;		/* address family */
341feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
3429b50d902SRodney W. Grimes 
3439b50d902SRodney W. Grimes int
344a01e3379SDavid Malone main(int argc, char *argv[])
3459b50d902SRodney W. Grimes {
346a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3479b50d902SRodney W. Grimes 	int ch;
3483fddef95SHiroki Sato 	int fib = -1;
3493fddef95SHiroki Sato 	char *endptr;
3509b50d902SRodney W. Grimes 
3519b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3529b50d902SRodney W. Grimes 
353*17ed2e8eSAlexander V. Chernikov 	while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz"))
3540153eb66SRobert Watson 	    != -1)
3559b50d902SRodney W. Grimes 		switch(ch) {
356*17ed2e8eSAlexander V. Chernikov 		case '4':
357*17ed2e8eSAlexander V. Chernikov #ifdef INET
358*17ed2e8eSAlexander V. Chernikov 			af = AF_INET;
359*17ed2e8eSAlexander V. Chernikov #else
360*17ed2e8eSAlexander V. Chernikov 			errx(1, "IPv4 support is not compiled in");
361*17ed2e8eSAlexander V. Chernikov #endif
362*17ed2e8eSAlexander V. Chernikov 			break;
363*17ed2e8eSAlexander V. Chernikov 		case '6':
364*17ed2e8eSAlexander V. Chernikov #ifdef INET6
365*17ed2e8eSAlexander V. Chernikov 			af = AF_INET6;
366*17ed2e8eSAlexander V. Chernikov #else
367*17ed2e8eSAlexander V. Chernikov 			errx(1, "IPv6 support is not compiled in");
368*17ed2e8eSAlexander V. Chernikov #endif
369*17ed2e8eSAlexander V. Chernikov 			break;
3709b50d902SRodney W. Grimes 		case 'A':
3719b50d902SRodney W. Grimes 			Aflag = 1;
3729b50d902SRodney W. Grimes 			break;
3739b50d902SRodney W. Grimes 		case 'a':
3749b50d902SRodney W. Grimes 			aflag = 1;
3759b50d902SRodney W. Grimes 			break;
3766b463eedSChristian S.J. Peron 		case 'B':
3776b463eedSChristian S.J. Peron 			Bflag = 1;
3786b463eedSChristian S.J. Peron 			break;
379e1e293a5SDavid Greenman 		case 'b':
380e1e293a5SDavid Greenman 			bflag = 1;
381e1e293a5SDavid Greenman 			break;
3829b50d902SRodney W. Grimes 		case 'd':
3839b50d902SRodney W. Grimes 			dflag = 1;
3849b50d902SRodney W. Grimes 			break;
3853fddef95SHiroki Sato 		case 'F':
3863fddef95SHiroki Sato 			fib = strtol(optarg, &endptr, 0);
3873fddef95SHiroki Sato 			if (*endptr != '\0' ||
3883fddef95SHiroki Sato 			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
3893fddef95SHiroki Sato 				errx(1, "%s: invalid fib", optarg);
3903fddef95SHiroki Sato 			break;
3919b50d902SRodney W. Grimes 		case 'f':
392cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
393cc6a66f2SJulian Elischer 				af = AF_IPX;
3949b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3959b50d902SRodney W. Grimes 				af = AF_INET;
396cfa1ca9dSYoshinobu Inoue #ifdef INET6
397cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
398cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
399e5134d2eSMax Laier #endif
400e5134d2eSMax Laier #ifdef IPSEC
4013b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
4023b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
403e5134d2eSMax Laier #endif
4049b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
4059b50d902SRodney W. Grimes 				af = AF_UNIX;
40663bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
40763bf4575SJulian Elischer 				af = AF_APPLETALK;
408690f477dSSam Leffler #ifdef NETGRAPH
4094cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
4104cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
4114cf49a43SJulian Elischer 				af = AF_NETGRAPH;
412690f477dSSam Leffler #endif
413d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
414d44ddba9SRuslan Ermilov 				af = AF_LINK;
4159b50d902SRodney W. Grimes 			else {
41651e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
4179b50d902SRodney W. Grimes 			}
4189b50d902SRodney W. Grimes 			break;
4199b50d902SRodney W. Grimes 		case 'g':
4209b50d902SRodney W. Grimes 			gflag = 1;
4219b50d902SRodney W. Grimes 			break;
422c2dfd19fSGleb Smirnoff 		case 'h':
423c2dfd19fSGleb Smirnoff 			hflag = 1;
424c2dfd19fSGleb Smirnoff 			break;
4259b50d902SRodney W. Grimes 		case 'I': {
4269b50d902SRodney W. Grimes 			char *cp;
4279b50d902SRodney W. Grimes 
4289b50d902SRodney W. Grimes 			iflag = 1;
4299b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4309b50d902SRodney W. Grimes 				continue;
4319b50d902SRodney W. Grimes 			unit = atoi(cp);
4329b50d902SRodney W. Grimes 			break;
4339b50d902SRodney W. Grimes 		}
4349b50d902SRodney W. Grimes 		case 'i':
4359b50d902SRodney W. Grimes 			iflag = 1;
4369b50d902SRodney W. Grimes 			break;
437ac55add0SGuido van Rooij 		case 'L':
438ac55add0SGuido van Rooij 			Lflag = 1;
439ac55add0SGuido van Rooij 			break;
4409b50d902SRodney W. Grimes 		case 'M':
4419b50d902SRodney W. Grimes 			memf = optarg;
4429b50d902SRodney W. Grimes 			break;
4439b50d902SRodney W. Grimes 		case 'm':
4449b50d902SRodney W. Grimes 			mflag = 1;
4459b50d902SRodney W. Grimes 			break;
4469b50d902SRodney W. Grimes 		case 'N':
4479b50d902SRodney W. Grimes 			nlistf = optarg;
4489b50d902SRodney W. Grimes 			break;
4499b50d902SRodney W. Grimes 		case 'n':
45065ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4519b50d902SRodney W. Grimes 			break;
4529b50d902SRodney W. Grimes 		case 'p':
4539b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
45451e7d42cSGarrett Wollman 				errx(1,
45551e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
45651e7d42cSGarrett Wollman 				     optarg);
4579b50d902SRodney W. Grimes 			}
4589b50d902SRodney W. Grimes 			pflag = 1;
4599b50d902SRodney W. Grimes 			break;
4600153eb66SRobert Watson 		case 'Q':
4610153eb66SRobert Watson 			Qflag = 1;
4620153eb66SRobert Watson 			break;
463bf10ffe1SXin LI 		case 'q':
464bf10ffe1SXin LI 			noutputs = atoi(optarg);
465bf10ffe1SXin LI 			if (noutputs != 0)
466bf10ffe1SXin LI 				noutputs++;
467bf10ffe1SXin LI 			break;
4689b50d902SRodney W. Grimes 		case 'r':
4699b50d902SRodney W. Grimes 			rflag = 1;
4709b50d902SRodney W. Grimes 			break;
4719b50d902SRodney W. Grimes 		case 's':
4729b50d902SRodney W. Grimes 			++sflag;
4739b50d902SRodney W. Grimes 			break;
47465ea0024SAssar Westerlund 		case 'S':
47565ea0024SAssar Westerlund 			numeric_addr = 1;
47665ea0024SAssar Westerlund 			break;
4779b50d902SRodney W. Grimes 		case 'u':
4789b50d902SRodney W. Grimes 			af = AF_UNIX;
4799b50d902SRodney W. Grimes 			break;
480080b7f49SDag-Erling Smørgrav 		case 'W':
48170057abfSRuslan Ermilov 		case 'l':
482080b7f49SDag-Erling Smørgrav 			Wflag = 1;
483080b7f49SDag-Erling Smørgrav 			break;
4849b50d902SRodney W. Grimes 		case 'w':
4859b50d902SRodney W. Grimes 			interval = atoi(optarg);
4869b50d902SRodney W. Grimes 			iflag = 1;
4879b50d902SRodney W. Grimes 			break;
488f5d34df5SGeorge V. Neville-Neil 		case 'T':
489f5d34df5SGeorge V. Neville-Neil 			Tflag = 1;
490f5d34df5SGeorge V. Neville-Neil 			break;
49149f287f8SGeorge V. Neville-Neil 		case 'x':
49249f287f8SGeorge V. Neville-Neil 			xflag = 1;
49349f287f8SGeorge V. Neville-Neil 			break;
494c73d99b5SRuslan Ermilov 		case 'z':
495c73d99b5SRuslan Ermilov 			zflag = 1;
496c73d99b5SRuslan Ermilov 			break;
4979b50d902SRodney W. Grimes 		case '?':
4989b50d902SRodney W. Grimes 		default:
4999b50d902SRodney W. Grimes 			usage();
5009b50d902SRodney W. Grimes 		}
5019b50d902SRodney W. Grimes 	argv += optind;
5029b50d902SRodney W. Grimes 	argc -= optind;
5039b50d902SRodney W. Grimes 
5049b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
5059b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
5069b50d902SRodney W. Grimes 	if (*argv) {
5079b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
5089b50d902SRodney W. Grimes 			interval = atoi(*argv);
5099b50d902SRodney W. Grimes 			if (interval <= 0)
5109b50d902SRodney W. Grimes 				usage();
5119b50d902SRodney W. Grimes 			++argv;
5129b50d902SRodney W. Grimes 			iflag = 1;
5139b50d902SRodney W. Grimes 		}
5149b50d902SRodney W. Grimes 		if (*argv) {
5159b50d902SRodney W. Grimes 			nlistf = *argv;
5169b50d902SRodney W. Grimes 			if (*++argv)
5179b50d902SRodney W. Grimes 				memf = *argv;
5189b50d902SRodney W. Grimes 		}
5199b50d902SRodney W. Grimes 	}
5209b50d902SRodney W. Grimes #endif
5219b50d902SRodney W. Grimes 
5229b50d902SRodney W. Grimes 	/*
5239b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
5249b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
5259b50d902SRodney W. Grimes 	 */
526feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
527feda1a43SJohn Baldwin 	if (!live)
5289b50d902SRodney W. Grimes 		setgid(getgid());
5299b50d902SRodney W. Grimes 
530f5d34df5SGeorge V. Neville-Neil 	if (xflag && Tflag)
531f5d34df5SGeorge V. Neville-Neil 		errx(1, "-x and -T are incompatible, pick one.");
532f5d34df5SGeorge V. Neville-Neil 
5336b463eedSChristian S.J. Peron 	if (Bflag) {
534feda1a43SJohn Baldwin 		if (!live)
535feda1a43SJohn Baldwin 			usage();
5366b463eedSChristian S.J. Peron 		bpf_stats(interface);
5376b463eedSChristian S.J. Peron 		exit(0);
5386b463eedSChristian S.J. Peron 	}
5399b50d902SRodney W. Grimes 	if (mflag) {
54083708764SRuslan Ermilov 		if (!live) {
541feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
54205d1f5bcSAndrey V. Elsukov 				mbpr(kvmd, nl[N_SFSTAT].n_value);
543d15c5f56SRuslan Ermilov 		} else
544d4426f28SRobert Watson 			mbpr(NULL, 0);
5459b50d902SRodney W. Grimes 		exit(0);
5469b50d902SRodney W. Grimes 	}
5470153eb66SRobert Watson 	if (Qflag) {
54888737be2SRobert Watson 		if (!live) {
54988737be2SRobert Watson 			if (kread(0, NULL, 0) == 0)
55088737be2SRobert Watson 				netisr_stats(kvmd);
55188737be2SRobert Watson 		} else
55288737be2SRobert Watson 			netisr_stats(NULL);
5530153eb66SRobert Watson 		exit(0);
5540153eb66SRobert Watson 	}
555cc63cd56SPeter Wemm #if 0
5569b50d902SRodney W. Grimes 	/*
5579b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5589b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5599b50d902SRodney W. Grimes 	 */
5609b50d902SRodney W. Grimes 	sethostent(1);
5619b50d902SRodney W. Grimes 	setnetent(1);
562cc63cd56SPeter Wemm #else
563cc63cd56SPeter Wemm 	/*
564cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
565cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
566cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
567cc63cd56SPeter Wemm 	 */
568cc63cd56SPeter Wemm #endif
569cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
570fc47e028SAlexander V. Chernikov 		intpr(interval, NULL, af);
5719b50d902SRodney W. Grimes 		exit(0);
5729b50d902SRodney W. Grimes 	}
5739b50d902SRodney W. Grimes 	if (rflag) {
5749b50d902SRodney W. Grimes 		if (sflag)
575fc47e028SAlexander V. Chernikov 			rt_stats();
5769b50d902SRodney W. Grimes 		else
577fc47e028SAlexander V. Chernikov 			routepr(fib, af);
5789b50d902SRodney W. Grimes 		exit(0);
5799b50d902SRodney W. Grimes 	}
580fc47e028SAlexander V. Chernikov 
5819b50d902SRodney W. Grimes 	if (gflag) {
582cfa1ca9dSYoshinobu Inoue 		if (sflag) {
583cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
584fc47e028SAlexander V. Chernikov 				mrt_stats();
585cfa1ca9dSYoshinobu Inoue #ifdef INET6
586cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
587fc47e028SAlexander V. Chernikov 				mrt6_stats();
588cfa1ca9dSYoshinobu Inoue #endif
589cfa1ca9dSYoshinobu Inoue 		} else {
590cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
591fc47e028SAlexander V. Chernikov 				mroutepr();
592cfa1ca9dSYoshinobu Inoue #ifdef INET6
593cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
594fc47e028SAlexander V. Chernikov 				mroute6pr();
595cfa1ca9dSYoshinobu Inoue #endif
596cfa1ca9dSYoshinobu Inoue 		}
5979b50d902SRodney W. Grimes 		exit(0);
5989b50d902SRodney W. Grimes 	}
599cfa1ca9dSYoshinobu Inoue 
600fc47e028SAlexander V. Chernikov 	/* Load all necessary kvm symbols */
601fc47e028SAlexander V. Chernikov 	kresolve_list(nl);
602fc47e028SAlexander V. Chernikov 
603cf5e44f8SRuslan Ermilov 	if (tp) {
604cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
605cf5e44f8SRuslan Ermilov 		exit(0);
606cf5e44f8SRuslan Ermilov 	}
607cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
6089b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
609cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
610cfa1ca9dSYoshinobu Inoue #ifdef INET6
611cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
612cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
613cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
614cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
6153b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
6163b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
6173b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
6183b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
6193b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
6209cc22e5cSPoul-Henning Kamp #ifdef IPX
621e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
622cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
623cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
624e30e913cSJohn Hay 	}
6259cc22e5cSPoul-Henning Kamp #endif /* IPX */
62663bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
62763bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
62863bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
629690f477dSSam Leffler #ifdef NETGRAPH
6304cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
6314cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
6324cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
633690f477dSSam Leffler #endif /* NETGRAPH */
634f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
635feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
636963b7ccdSRobert Watson 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
637963b7ccdSRobert Watson 		    nl[N_UNP_SPHEAD].n_value);
6389b50d902SRodney W. Grimes 	exit(0);
6399b50d902SRodney W. Grimes }
6409b50d902SRodney W. Grimes 
6419b50d902SRodney W. Grimes /*
6429b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6439b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6449b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6459b50d902SRodney W. Grimes  */
6469b50d902SRodney W. Grimes static void
647321ae07fSPhilippe Charnier printproto(struct protox *tp, const char *name)
6489b50d902SRodney W. Grimes {
649feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6509b50d902SRodney W. Grimes 	u_long off;
6519b50d902SRodney W. Grimes 
6529b50d902SRodney W. Grimes 	if (sflag) {
653cfa1ca9dSYoshinobu Inoue 		if (iflag) {
654cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
655fc47e028SAlexander V. Chernikov 				intpr(interval, tp->pr_istats, af);
656cf5e44f8SRuslan Ermilov 			else if (pflag)
657cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
658cf5e44f8SRuslan Ermilov 				    tp->pr_name);
659cfa1ca9dSYoshinobu Inoue 			return;
660feda1a43SJohn Baldwin 		} else {
6619b50d902SRodney W. Grimes 			pr = tp->pr_stats;
662cf5e44f8SRuslan Ermilov 			if (!pr) {
663cf5e44f8SRuslan Ermilov 				if (pflag)
664cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
665cf5e44f8SRuslan Ermilov 					    tp->pr_name);
666cf5e44f8SRuslan Ermilov 				return;
667cf5e44f8SRuslan Ermilov 			}
668feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
669feda1a43SJohn Baldwin 				off = 0;
670feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
671feda1a43SJohn Baldwin 				if (pflag)
672feda1a43SJohn Baldwin 					printf(
673feda1a43SJohn Baldwin 				    "%s: stats routine doesn't work on cores\n",
674feda1a43SJohn Baldwin 					    tp->pr_name);
675feda1a43SJohn Baldwin 				return;
676feda1a43SJohn Baldwin 			} else
677feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
678cfa1ca9dSYoshinobu Inoue 		}
6799b50d902SRodney W. Grimes 	} else {
6809b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
681cf5e44f8SRuslan Ermilov 		if (!pr) {
682cf5e44f8SRuslan Ermilov 			if (pflag)
683cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
684cf5e44f8SRuslan Ermilov 			return;
685cf5e44f8SRuslan Ermilov 		}
686feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
687feda1a43SJohn Baldwin 			off = 0;
688feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
689feda1a43SJohn Baldwin 			if (pflag)
690feda1a43SJohn Baldwin 				printf(
691feda1a43SJohn Baldwin 				    "%s: PCB routine doesn't work on cores\n",
692feda1a43SJohn Baldwin 				    tp->pr_name);
693feda1a43SJohn Baldwin 			return;
694feda1a43SJohn Baldwin 		} else
695feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6969b50d902SRodney W. Grimes 	}
697feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
698feda1a43SJohn Baldwin 	    af != AF_UNSPEC))
699feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
7009b50d902SRodney W. Grimes }
7019b50d902SRodney W. Grimes 
70229dde48dSGleb Smirnoff static int
70329dde48dSGleb Smirnoff kvmd_init(void)
7049b50d902SRodney W. Grimes {
705feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
706feda1a43SJohn Baldwin 
70729dde48dSGleb Smirnoff 	if (kvmd != NULL)
70829dde48dSGleb Smirnoff 		return (0);
70929dde48dSGleb Smirnoff 
710feda1a43SJohn Baldwin 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
7113b7e5cccSRuslan Ermilov 	setgid(getgid());
71229dde48dSGleb Smirnoff 
71329dde48dSGleb Smirnoff 	if (kvmd == NULL) {
71429dde48dSGleb Smirnoff 		warnx("kvm not available: %s", errbuf);
71529dde48dSGleb Smirnoff 		return (-1);
71629dde48dSGleb Smirnoff 	}
71729dde48dSGleb Smirnoff 
718fc47e028SAlexander V. Chernikov 	return (0);
719fc47e028SAlexander V. Chernikov }
720fc47e028SAlexander V. Chernikov 
721fc47e028SAlexander V. Chernikov /*
722fc47e028SAlexander V. Chernikov  * Resolve symbol list, return 0 on success.
723fc47e028SAlexander V. Chernikov  */
724fc47e028SAlexander V. Chernikov int
725fc47e028SAlexander V. Chernikov kresolve_list(struct nlist *_nl)
726fc47e028SAlexander V. Chernikov {
727fc47e028SAlexander V. Chernikov 
728fc47e028SAlexander V. Chernikov 	if ((kvmd == NULL) && (kvmd_init() != 0))
729fc47e028SAlexander V. Chernikov 		return (-1);
730fc47e028SAlexander V. Chernikov 
731fc47e028SAlexander V. Chernikov 	if (_nl[0].n_type != 0)
732fc47e028SAlexander V. Chernikov 		return (0);
733fc47e028SAlexander V. Chernikov 
734fc47e028SAlexander V. Chernikov 	if (kvm_nlist(kvmd, _nl) < 0) {
73599453c6aSPoul-Henning Kamp 		if (nlistf)
73699453c6aSPoul-Henning Kamp 			errx(1, "%s: kvm_nlist: %s", nlistf,
73799453c6aSPoul-Henning Kamp 			     kvm_geterr(kvmd));
73899453c6aSPoul-Henning Kamp 		else
73999453c6aSPoul-Henning Kamp 			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
74099453c6aSPoul-Henning Kamp 	}
74199453c6aSPoul-Henning Kamp 
74229dde48dSGleb Smirnoff 	return (0);
74329dde48dSGleb Smirnoff }
74429dde48dSGleb Smirnoff 
74529dde48dSGleb Smirnoff /*
74629dde48dSGleb Smirnoff  * Read kernel memory, return 0 on success.
74729dde48dSGleb Smirnoff  */
74829dde48dSGleb Smirnoff int
74929dde48dSGleb Smirnoff kread(u_long addr, void *buf, size_t size)
75029dde48dSGleb Smirnoff {
75129dde48dSGleb Smirnoff 
75229dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
75399453c6aSPoul-Henning Kamp 		return (-1);
75429dde48dSGleb Smirnoff 
755c6620a13SPoul-Henning Kamp 	if (!buf)
756c6620a13SPoul-Henning Kamp 		return (0);
757feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
7583aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
7599b50d902SRodney W. Grimes 		return (-1);
7609b50d902SRodney W. Grimes 	}
7619b50d902SRodney W. Grimes 	return (0);
7629b50d902SRodney W. Grimes }
7639b50d902SRodney W. Grimes 
76429dde48dSGleb Smirnoff /*
76529dde48dSGleb Smirnoff  * Read an array of N counters in kernel memory into array of N uint64_t's.
76629dde48dSGleb Smirnoff  */
76729dde48dSGleb Smirnoff int
7685da0521fSAndrey V. Elsukov kread_counters(u_long addr, void *buf, size_t size)
76929dde48dSGleb Smirnoff {
7705da0521fSAndrey V. Elsukov 	uint64_t *c = buf;
77129dde48dSGleb Smirnoff 
77229dde48dSGleb Smirnoff 	if (kvmd_init() < 0)
77329dde48dSGleb Smirnoff 		return (-1);
77429dde48dSGleb Smirnoff 
7755da0521fSAndrey V. Elsukov 	if (kread(addr, buf, size) < 0)
7765da0521fSAndrey V. Elsukov 		return (-1);
77729dde48dSGleb Smirnoff 
7785da0521fSAndrey V. Elsukov 	while (size != 0) {
7795da0521fSAndrey V. Elsukov 		*c = kvm_counter_u64_fetch(kvmd, *c);
7805da0521fSAndrey V. Elsukov 		size -= sizeof(*c);
7815da0521fSAndrey V. Elsukov 		c++;
7825da0521fSAndrey V. Elsukov 	}
78329dde48dSGleb Smirnoff 	return (0);
78429dde48dSGleb Smirnoff }
78529dde48dSGleb Smirnoff 
786a01e3379SDavid Malone const char *
7877b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
7889b50d902SRodney W. Grimes {
7899b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7909b50d902SRodney W. Grimes }
7919b50d902SRodney W. Grimes 
792a01e3379SDavid Malone const char *
7937b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
7949b50d902SRodney W. Grimes {
7959b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7969b50d902SRodney W. Grimes }
7979b50d902SRodney W. Grimes 
798f99a4046SMike Makonnen const char *
7997b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
800f99a4046SMike Makonnen {
801f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
802f99a4046SMike Makonnen }
803f99a4046SMike Makonnen 
8049b50d902SRodney W. Grimes /*
8059b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
8069b50d902SRodney W. Grimes  */
8079b50d902SRodney W. Grimes static struct protox *
808096146f8SYaroslav Tykhiy knownname(const char *name)
8099b50d902SRodney W. Grimes {
8109b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
8119b50d902SRodney W. Grimes 
8129b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
8139b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
8149b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
8159b50d902SRodney W. Grimes 				return (tp);
8169b50d902SRodney W. Grimes 	return (NULL);
8179b50d902SRodney W. Grimes }
8189b50d902SRodney W. Grimes 
8199b50d902SRodney W. Grimes /*
8209b50d902SRodney W. Grimes  * Find the protox corresponding to name.
8219b50d902SRodney W. Grimes  */
8229b50d902SRodney W. Grimes static struct protox *
823096146f8SYaroslav Tykhiy name2protox(const char *name)
8249b50d902SRodney W. Grimes {
8259b50d902SRodney W. Grimes 	struct protox *tp;
8269b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
8279b50d902SRodney W. Grimes 	struct protoent *p;
8289b50d902SRodney W. Grimes 
8299b50d902SRodney W. Grimes 	/*
8309b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
8319b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
8329b50d902SRodney W. Grimes 	 */
833cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
8349b50d902SRodney W. Grimes 		return (tp);
8359b50d902SRodney W. Grimes 
8369b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
837cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
8389b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
8399b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
8409b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
8419b50d902SRodney W. Grimes 				endprotoent();
8429b50d902SRodney W. Grimes 				return (knownname(p->p_name));
8439b50d902SRodney W. Grimes 			}
8449b50d902SRodney W. Grimes 	}
8459b50d902SRodney W. Grimes 	endprotoent();
8469b50d902SRodney W. Grimes 	return (NULL);
8479b50d902SRodney W. Grimes }
8489b50d902SRodney W. Grimes 
8499b50d902SRodney W. Grimes static void
8505e051718SAssar Westerlund usage(void)
8519b50d902SRodney W. Grimes {
8520153eb66SRobert Watson 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
853*17ed2e8eSAlexander V. Chernikov "usage: netstat [-46AaLnSTWx] [-f protocol_family | -p protocol]\n"
8541cb467b1SRuslan Ermilov "               [-M core] [-N system]",
855*17ed2e8eSAlexander V. Chernikov "       netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
856d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
857*17ed2e8eSAlexander V. Chernikov "       netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]",
858*17ed2e8eSAlexander V. Chernikov "       netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n"
85982d383bcSRuslan Ermilov "               [-M core] [-N system]",
860*17ed2e8eSAlexander V. Chernikov "       netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n"
8611cb467b1SRuslan Ermilov "               [-M core] [-N system]",
86249c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
8635dbd2da1SChristian S.J. Peron "       netstat -B [-I interface]",
864*17ed2e8eSAlexander V. Chernikov "       netstat -r [-46AanW] [-f address_family] [-M core] [-N system]",
8651cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
866*17ed2e8eSAlexander V. Chernikov "       netstat -g [-46W] [-f address_family] [-M core] [-N system]",
867*17ed2e8eSAlexander V. Chernikov "       netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
8680153eb66SRobert Watson "       netstat -Q");
8699b50d902SRodney W. Grimes 	exit(1);
8709b50d902SRodney W. Grimes }
871