xref: /freebsd/usr.bin/netstat/main.c (revision 88737be2ddd3e5de3365db49abfb4e84e191e3b6)
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  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
355d422d6aSPhilippe Charnier char const copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\
379b50d902SRodney W. Grimes 	Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
405d422d6aSPhilippe Charnier #if 0
416cc6f122SPhilippe Charnier #ifndef lint
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
439b50d902SRodney W. Grimes #endif /* not lint */
446cc6f122SPhilippe Charnier #endif
456cc6f122SPhilippe Charnier 
466cc6f122SPhilippe Charnier #include <sys/cdefs.h>
476cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <sys/param.h>
509b50d902SRodney W. Grimes #include <sys/file.h>
519b50d902SRodney W. Grimes #include <sys/protosw.h>
529b50d902SRodney W. Grimes #include <sys/socket.h>
53feda1a43SJohn Baldwin #include <sys/socketvar.h>
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes #include <netinet/in.h>
569b50d902SRodney W. Grimes 
57690f477dSSam Leffler #ifdef NETGRAPH
584cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
59690f477dSSam Leffler #endif
604cf49a43SJulian Elischer 
619b50d902SRodney W. Grimes #include <ctype.h>
625d422d6aSPhilippe Charnier #include <err.h>
63821df508SXin LI #include <errno.h>
649b50d902SRodney W. Grimes #include <kvm.h>
659b50d902SRodney W. Grimes #include <limits.h>
669b50d902SRodney W. Grimes #include <netdb.h>
679b50d902SRodney W. Grimes #include <nlist.h>
68821df508SXin LI #include <paths.h>
697b95a1ebSYaroslav Tykhiy #include <stdint.h>
709b50d902SRodney W. Grimes #include <stdio.h>
719b50d902SRodney W. Grimes #include <stdlib.h>
729b50d902SRodney W. Grimes #include <string.h>
739b50d902SRodney W. Grimes #include <unistd.h>
749b50d902SRodney W. Grimes #include "netstat.h"
759b50d902SRodney W. Grimes 
76c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
774f81ef50SGarrett Wollman #define	N_IFNET		0
78096146f8SYaroslav Tykhiy 	{ .n_name = "_ifnet" },
79ab54ea99SPeter Wemm #define	N_RTSTAT	1
80096146f8SYaroslav Tykhiy 	{ .n_name = "_rtstat" },
81ab54ea99SPeter Wemm #define	N_RTREE		2
82096146f8SYaroslav Tykhiy 	{ .n_name = "_rt_tables"},
83ab54ea99SPeter Wemm #define	N_MRTSTAT	3
84096146f8SYaroslav Tykhiy 	{ .n_name = "_mrtstat" },
85443fc317SBruce M Simpson #define	N_MFCHASHTBL	4
86443fc317SBruce M Simpson 	{ .n_name = "_mfchashtbl" },
87ab54ea99SPeter Wemm #define	N_VIFTABLE	5
88096146f8SYaroslav Tykhiy 	{ .n_name = "_viftable" },
89ab54ea99SPeter Wemm #define	N_IPX		6
90096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxpcb_list"},
91ab54ea99SPeter Wemm #define	N_IPXSTAT	7
92096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxstat"},
93ab54ea99SPeter Wemm #define	N_SPXSTAT	8
94096146f8SYaroslav Tykhiy 	{ .n_name = "_spx_istat"},
95ab54ea99SPeter Wemm #define	N_DDPSTAT	9
96096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpstat"},
97ab54ea99SPeter Wemm #define	N_DDPCB		10
98096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpcb"},
99ab54ea99SPeter Wemm #define	N_NGSOCKS	11
100096146f8SYaroslav Tykhiy 	{ .n_name = "_ngsocklist"},
101ab54ea99SPeter Wemm #define	N_IP6STAT	12
102096146f8SYaroslav Tykhiy 	{ .n_name = "_ip6stat" },
103ab54ea99SPeter Wemm #define	N_ICMP6STAT	13
104096146f8SYaroslav Tykhiy 	{ .n_name = "_icmp6stat" },
105ab54ea99SPeter Wemm #define	N_IPSECSTAT	14
1068409aedfSGeorge V. Neville-Neil 	{ .n_name = "_ipsec4stat" },
107ab54ea99SPeter Wemm #define	N_IPSEC6STAT	15
108096146f8SYaroslav Tykhiy 	{ .n_name = "_ipsec6stat" },
109ab54ea99SPeter Wemm #define	N_PIM6STAT	16
110096146f8SYaroslav Tykhiy 	{ .n_name = "_pim6stat" },
111ab54ea99SPeter Wemm #define	N_MRT6STAT	17
112096146f8SYaroslav Tykhiy 	{ .n_name = "_mrt6stat" },
113ab54ea99SPeter Wemm #define	N_MF6CTABLE	18
114096146f8SYaroslav Tykhiy 	{ .n_name = "_mf6ctable" },
115ab54ea99SPeter Wemm #define	N_MIF6TABLE	19
116096146f8SYaroslav Tykhiy 	{ .n_name = "_mif6table" },
117ab54ea99SPeter Wemm #define	N_PFKEYSTAT	20
118096146f8SYaroslav Tykhiy 	{ .n_name = "_pfkeystat" },
119ab54ea99SPeter Wemm #define	N_MBSTAT	21
120096146f8SYaroslav Tykhiy 	{ .n_name = "_mbstat" },
121ab54ea99SPeter Wemm #define	N_MBTYPES	22
122096146f8SYaroslav Tykhiy 	{ .n_name = "_mbtypes" },
123ab54ea99SPeter Wemm #define	N_NMBCLUSTERS	23
124096146f8SYaroslav Tykhiy 	{ .n_name = "_nmbclusters" },
125ab54ea99SPeter Wemm #define	N_NMBUFS	24
126096146f8SYaroslav Tykhiy 	{ .n_name = "_nmbufs" },
127ab54ea99SPeter Wemm #define	N_MBHI		25
128096146f8SYaroslav Tykhiy 	{ .n_name = "_mbuf_hiwm" },
129ab54ea99SPeter Wemm #define	N_CLHI		26
130096146f8SYaroslav Tykhiy 	{ .n_name = "_clust_hiwm" },
131ab54ea99SPeter Wemm #define	N_NCPUS		27
132096146f8SYaroslav Tykhiy 	{ .n_name = "_smp_cpus" },
133ab54ea99SPeter Wemm #define	N_PAGESZ	28
134096146f8SYaroslav Tykhiy 	{ .n_name = "_pagesize" },
135ab54ea99SPeter Wemm #define	N_MBPSTAT	29
136096146f8SYaroslav Tykhiy 	{ .n_name = "_mb_statpcpu" },
137ab54ea99SPeter Wemm #define	N_RTTRASH	30
138096146f8SYaroslav Tykhiy 	{ .n_name = "_rttrash" },
139ab54ea99SPeter Wemm #define	N_MBLO		31
140096146f8SYaroslav Tykhiy 	{ .n_name = "_mbuf_lowm" },
141ab54ea99SPeter Wemm #define	N_CLLO		32
142096146f8SYaroslav Tykhiy 	{ .n_name = "_clust_lowm" },
143a9771948SGleb Smirnoff #define	N_CARPSTAT	33
144096146f8SYaroslav Tykhiy 	{ .n_name = "_carpstats" },
1452e37c5a3SMax Laier #define	N_PFSYNCSTAT	34
146096146f8SYaroslav Tykhiy 	{ .n_name = "_pfsyncstats" },
147feda1a43SJohn Baldwin #define	N_AHSTAT	35
148096146f8SYaroslav Tykhiy 	{ .n_name = "_ahstat" },
149feda1a43SJohn Baldwin #define	N_ESPSTAT	36
150096146f8SYaroslav Tykhiy 	{ .n_name = "_espstat" },
151feda1a43SJohn Baldwin #define	N_IPCOMPSTAT	37
152096146f8SYaroslav Tykhiy 	{ .n_name = "_ipcompstat" },
153feda1a43SJohn Baldwin #define	N_TCPSTAT	38
154feda1a43SJohn Baldwin 	{ .n_name = "_tcpstat" },
155feda1a43SJohn Baldwin #define	N_UDPSTAT	39
156feda1a43SJohn Baldwin 	{ .n_name = "_udpstat" },
157feda1a43SJohn Baldwin #define	N_IPSTAT	40
158feda1a43SJohn Baldwin 	{ .n_name = "_ipstat" },
159feda1a43SJohn Baldwin #define	N_ICMPSTAT	41
160feda1a43SJohn Baldwin 	{ .n_name = "_icmpstat" },
161feda1a43SJohn Baldwin #define	N_IGMPSTAT	42
162feda1a43SJohn Baldwin 	{ .n_name = "_igmpstat" },
163feda1a43SJohn Baldwin #define	N_PIMSTAT	43
164feda1a43SJohn Baldwin 	{ .n_name = "_pimstat" },
165feda1a43SJohn Baldwin #define	N_TCBINFO	44
166feda1a43SJohn Baldwin 	{ .n_name = "_tcbinfo" },
167feda1a43SJohn Baldwin #define	N_UDBINFO	45
168feda1a43SJohn Baldwin 	{ .n_name = "_udbinfo" },
169feda1a43SJohn Baldwin #define	N_DIVCBINFO	46
170feda1a43SJohn Baldwin 	{ .n_name = "_divcbinfo" },
171feda1a43SJohn Baldwin #define	N_RIPCBINFO	47
172feda1a43SJohn Baldwin 	{ .n_name = "_ripcbinfo" },
173feda1a43SJohn Baldwin #define	N_UNP_COUNT	48
174feda1a43SJohn Baldwin 	{ .n_name = "_unp_count" },
175feda1a43SJohn Baldwin #define	N_UNP_GENCNT	49
176feda1a43SJohn Baldwin 	{ .n_name = "_unp_gencnt" },
177feda1a43SJohn Baldwin #define	N_UNP_DHEAD	50
178feda1a43SJohn Baldwin 	{ .n_name = "_unp_dhead" },
179feda1a43SJohn Baldwin #define	N_UNP_SHEAD	51
180feda1a43SJohn Baldwin 	{ .n_name = "_unp_shead" },
181feda1a43SJohn Baldwin #define	N_RIP6STAT	52
182feda1a43SJohn Baldwin 	{ .n_name = "_rip6stat" },
183feda1a43SJohn Baldwin #define	N_SCTPSTAT	53
184feda1a43SJohn Baldwin 	{ .n_name = "_sctpstat" },
185443fc317SBruce M Simpson #define	N_MFCTABLESIZE	54
186443fc317SBruce M Simpson 	{ .n_name = "_mfctablesize" },
18754fc657dSGeorge V. Neville-Neil #define N_ARPSTAT       55
18854fc657dSGeorge V. Neville-Neil 	{ .n_name = "_arpstat" },
189963b7ccdSRobert Watson #define	N_UNP_SPHEAD	56
190963b7ccdSRobert Watson 	{ .n_name = "unp_sphead" },
191096146f8SYaroslav Tykhiy 	{ .n_name = NULL },
1929b50d902SRodney W. Grimes };
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes struct protox {
195feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
196feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
1979b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
198feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
1995e051718SAssar Westerlund 					/* control blocks printing routine */
200feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
2015e051718SAssar Westerlund 					/* statistics printing routine */
2025e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
203fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
20455fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
205feda1a43SJohn Baldwin 	int	pr_protocol;
2069b50d902SRodney W. Grimes } protox[] = {
207feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
208feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
209feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
210feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
21174fd40c9SRandall Stewart #ifdef SCTP
212feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
213feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
21474fd40c9SRandall Stewart #endif
215feda1a43SJohn Baldwin 	{ N_DIVCBINFO,	-1,		1,	protopr,
216feda1a43SJohn Baldwin 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
217feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
218feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
219feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
220feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
221feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
222feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
223cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2248409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
225feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
2268409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
227feda1a43SJohn Baldwin 	  ah_stats,	NULL,		"ah",	0,	0},
2288409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
229feda1a43SJohn Baldwin 	  esp_stats,	NULL,		"esp",	0,	0},
2308409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
231feda1a43SJohn Baldwin 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
232100b98dbSKelly Yancey #endif
233feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
234feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
235feda1a43SJohn Baldwin 	{ -1,		N_CARPSTAT,	1,	NULL,
236feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
237feda1a43SJohn Baldwin 	{ -1,		N_PFSYNCSTAT,	1,	NULL,
238feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
23954fc657dSGeorge V. Neville-Neil 	{ -1,		N_ARPSTAT,	1,	NULL,
24054fc657dSGeorge V. Neville-Neil 	  arp_stats,	NULL,		"arp", 1,	0 },
2416bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
242feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2439b50d902SRodney W. Grimes };
2449b50d902SRodney W. Grimes 
245cfa1ca9dSYoshinobu Inoue #ifdef INET6
246cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
247feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
248feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
249feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
250feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
251feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
252feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
253feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
254feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
255cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2566bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
257feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
258cfa1ca9dSYoshinobu Inoue #endif
259cfa1ca9dSYoshinobu Inoue #ifdef notyet
2606bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
261feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
262cfa1ca9dSYoshinobu Inoue #endif
263feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
264feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
2656bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
266feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
267cfa1ca9dSYoshinobu Inoue };
268cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
269cfa1ca9dSYoshinobu Inoue 
2703b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2713b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2726bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
273feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
2746bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
275feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2763b8a8567SJun-ichiro itojun Hagino };
2773b8a8567SJun-ichiro itojun Hagino #endif
2783b8a8567SJun-ichiro itojun Hagino 
27963bf4575SJulian Elischer struct protox atalkprotox[] = {
28063bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
281feda1a43SJohn Baldwin 	  ddp_stats,	NULL,		"ddp",	0,	0 },
2826bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
283feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
28463bf4575SJulian Elischer };
285690f477dSSam Leffler #ifdef NETGRAPH
2864cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2874cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
288feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
2894cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
290feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
2916bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
292feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2934cf49a43SJulian Elischer };
294690f477dSSam Leffler #endif
2959cc22e5cSPoul-Henning Kamp #ifdef IPX
296cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
297cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
298feda1a43SJohn Baldwin 	  ipx_stats,	NULL,		"ipx",	0,	0 },
299cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
300feda1a43SJohn Baldwin 	  spx_stats,	NULL,		"spx",	0,	0 },
3016bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
302feda1a43SJohn Baldwin 	  NULL,		NULL,		0,	0,	0 }
303cc6a66f2SJulian Elischer };
3049cc22e5cSPoul-Henning Kamp #endif
305cc6a66f2SJulian Elischer 
306cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
307cfa1ca9dSYoshinobu Inoue 					 protox,
308cfa1ca9dSYoshinobu Inoue #ifdef INET6
309cfa1ca9dSYoshinobu Inoue 					 ip6protox,
310cfa1ca9dSYoshinobu Inoue #endif
3113b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
3123b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
3133b8a8567SJun-ichiro itojun Hagino #endif
3149cc22e5cSPoul-Henning Kamp #ifdef IPX
3159cc22e5cSPoul-Henning Kamp 					 ipxprotox,
3169cc22e5cSPoul-Henning Kamp #endif
3179cc22e5cSPoul-Henning Kamp 					 atalkprotox, NULL };
3189b50d902SRodney W. Grimes 
319fa6d48c0SMark Murray static void printproto(struct protox *, const char *);
3205e051718SAssar Westerlund static void usage(void);
321096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
322096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
3239b50d902SRodney W. Grimes 
324c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
325080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
326080b7f49SDag-Erling Smørgrav 
327080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
328080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
3296b463eedSChristian S.J. Peron int	Bflag;		/* show information about bpf consumers */
330080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
331080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
332080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
333c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
334080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
335080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
336080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
337bf10ffe1SXin LI int	noutputs = 0;	/* how much outputs before we exit */
33865ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
33965ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
340cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
3410153eb66SRobert Watson int	Qflag;		/* show netisr information */
342080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
343080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
344080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
34549f287f8SGeorge V. Neville-Neil int	xflag;		/* extra information, includes all socket buffer info */
346c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
347080b7f49SDag-Erling Smørgrav 
348080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
349080b7f49SDag-Erling Smørgrav 
350080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
351080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
352080b7f49SDag-Erling Smørgrav 
353080b7f49SDag-Erling Smørgrav int	af;		/* address family */
354feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
3559b50d902SRodney W. Grimes 
3569b50d902SRodney W. Grimes int
357a01e3379SDavid Malone main(int argc, char *argv[])
3589b50d902SRodney W. Grimes {
359a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3609b50d902SRodney W. Grimes 	int ch;
3619b50d902SRodney W. Grimes 
3629b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3639b50d902SRodney W. Grimes 
3640153eb66SRobert Watson 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:Qq:rSsuWw:xz"))
3650153eb66SRobert Watson 	    != -1)
3669b50d902SRodney W. Grimes 		switch(ch) {
3679b50d902SRodney W. Grimes 		case 'A':
3689b50d902SRodney W. Grimes 			Aflag = 1;
3699b50d902SRodney W. Grimes 			break;
3709b50d902SRodney W. Grimes 		case 'a':
3719b50d902SRodney W. Grimes 			aflag = 1;
3729b50d902SRodney W. Grimes 			break;
3736b463eedSChristian S.J. Peron 		case 'B':
3746b463eedSChristian S.J. Peron 			Bflag = 1;
3756b463eedSChristian S.J. Peron 			break;
376e1e293a5SDavid Greenman 		case 'b':
377e1e293a5SDavid Greenman 			bflag = 1;
378e1e293a5SDavid Greenman 			break;
3799b50d902SRodney W. Grimes 		case 'd':
3809b50d902SRodney W. Grimes 			dflag = 1;
3819b50d902SRodney W. Grimes 			break;
3829b50d902SRodney W. Grimes 		case 'f':
383cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
384cc6a66f2SJulian Elischer 				af = AF_IPX;
3859b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3869b50d902SRodney W. Grimes 				af = AF_INET;
387cfa1ca9dSYoshinobu Inoue #ifdef INET6
388cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
389cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
390e5134d2eSMax Laier #endif
391e5134d2eSMax Laier #ifdef IPSEC
3923b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3933b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
394e5134d2eSMax Laier #endif
3959b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3969b50d902SRodney W. Grimes 				af = AF_UNIX;
39763bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
39863bf4575SJulian Elischer 				af = AF_APPLETALK;
399690f477dSSam Leffler #ifdef NETGRAPH
4004cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
4014cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
4024cf49a43SJulian Elischer 				af = AF_NETGRAPH;
403690f477dSSam Leffler #endif
404d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
405d44ddba9SRuslan Ermilov 				af = AF_LINK;
4069b50d902SRodney W. Grimes 			else {
40751e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
4089b50d902SRodney W. Grimes 			}
4099b50d902SRodney W. Grimes 			break;
4109b50d902SRodney W. Grimes 		case 'g':
4119b50d902SRodney W. Grimes 			gflag = 1;
4129b50d902SRodney W. Grimes 			break;
413c2dfd19fSGleb Smirnoff 		case 'h':
414c2dfd19fSGleb Smirnoff 			hflag = 1;
415c2dfd19fSGleb Smirnoff 			break;
4169b50d902SRodney W. Grimes 		case 'I': {
4179b50d902SRodney W. Grimes 			char *cp;
4189b50d902SRodney W. Grimes 
4199b50d902SRodney W. Grimes 			iflag = 1;
4209b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4219b50d902SRodney W. Grimes 				continue;
4229b50d902SRodney W. Grimes 			unit = atoi(cp);
4239b50d902SRodney W. Grimes 			break;
4249b50d902SRodney W. Grimes 		}
4259b50d902SRodney W. Grimes 		case 'i':
4269b50d902SRodney W. Grimes 			iflag = 1;
4279b50d902SRodney W. Grimes 			break;
428ac55add0SGuido van Rooij 		case 'L':
429ac55add0SGuido van Rooij 			Lflag = 1;
430ac55add0SGuido van Rooij 			break;
4319b50d902SRodney W. Grimes 		case 'M':
4329b50d902SRodney W. Grimes 			memf = optarg;
4339b50d902SRodney W. Grimes 			break;
4349b50d902SRodney W. Grimes 		case 'm':
4359b50d902SRodney W. Grimes 			mflag = 1;
4369b50d902SRodney W. Grimes 			break;
4379b50d902SRodney W. Grimes 		case 'N':
4389b50d902SRodney W. Grimes 			nlistf = optarg;
4399b50d902SRodney W. Grimes 			break;
4409b50d902SRodney W. Grimes 		case 'n':
44165ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4429b50d902SRodney W. Grimes 			break;
4439b50d902SRodney W. Grimes 		case 'p':
4449b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
44551e7d42cSGarrett Wollman 				errx(1,
44651e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
44751e7d42cSGarrett Wollman 				     optarg);
4489b50d902SRodney W. Grimes 			}
4499b50d902SRodney W. Grimes 			pflag = 1;
4509b50d902SRodney W. Grimes 			break;
4510153eb66SRobert Watson 		case 'Q':
4520153eb66SRobert Watson 			Qflag = 1;
4530153eb66SRobert Watson 			break;
454bf10ffe1SXin LI 		case 'q':
455bf10ffe1SXin LI 			noutputs = atoi(optarg);
456bf10ffe1SXin LI 			if (noutputs != 0)
457bf10ffe1SXin LI 				noutputs++;
458bf10ffe1SXin LI 			break;
4599b50d902SRodney W. Grimes 		case 'r':
4609b50d902SRodney W. Grimes 			rflag = 1;
4619b50d902SRodney W. Grimes 			break;
4629b50d902SRodney W. Grimes 		case 's':
4639b50d902SRodney W. Grimes 			++sflag;
4649b50d902SRodney W. Grimes 			break;
46565ea0024SAssar Westerlund 		case 'S':
46665ea0024SAssar Westerlund 			numeric_addr = 1;
46765ea0024SAssar Westerlund 			break;
4689b50d902SRodney W. Grimes 		case 'u':
4699b50d902SRodney W. Grimes 			af = AF_UNIX;
4709b50d902SRodney W. Grimes 			break;
471080b7f49SDag-Erling Smørgrav 		case 'W':
47270057abfSRuslan Ermilov 		case 'l':
473080b7f49SDag-Erling Smørgrav 			Wflag = 1;
474080b7f49SDag-Erling Smørgrav 			break;
4759b50d902SRodney W. Grimes 		case 'w':
4769b50d902SRodney W. Grimes 			interval = atoi(optarg);
4779b50d902SRodney W. Grimes 			iflag = 1;
4789b50d902SRodney W. Grimes 			break;
47949f287f8SGeorge V. Neville-Neil 		case 'x':
48049f287f8SGeorge V. Neville-Neil 			xflag = 1;
48149f287f8SGeorge V. Neville-Neil 			break;
482c73d99b5SRuslan Ermilov 		case 'z':
483c73d99b5SRuslan Ermilov 			zflag = 1;
484c73d99b5SRuslan Ermilov 			break;
4859b50d902SRodney W. Grimes 		case '?':
4869b50d902SRodney W. Grimes 		default:
4879b50d902SRodney W. Grimes 			usage();
4889b50d902SRodney W. Grimes 		}
4899b50d902SRodney W. Grimes 	argv += optind;
4909b50d902SRodney W. Grimes 	argc -= optind;
4919b50d902SRodney W. Grimes 
4929b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4939b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4949b50d902SRodney W. Grimes 	if (*argv) {
4959b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4969b50d902SRodney W. Grimes 			interval = atoi(*argv);
4979b50d902SRodney W. Grimes 			if (interval <= 0)
4989b50d902SRodney W. Grimes 				usage();
4999b50d902SRodney W. Grimes 			++argv;
5009b50d902SRodney W. Grimes 			iflag = 1;
5019b50d902SRodney W. Grimes 		}
5029b50d902SRodney W. Grimes 		if (*argv) {
5039b50d902SRodney W. Grimes 			nlistf = *argv;
5049b50d902SRodney W. Grimes 			if (*++argv)
5059b50d902SRodney W. Grimes 				memf = *argv;
5069b50d902SRodney W. Grimes 		}
5079b50d902SRodney W. Grimes 	}
5089b50d902SRodney W. Grimes #endif
5099b50d902SRodney W. Grimes 
5109b50d902SRodney W. Grimes 	/*
5119b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
5129b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
5139b50d902SRodney W. Grimes 	 */
514feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
515feda1a43SJohn Baldwin 	if (!live)
5169b50d902SRodney W. Grimes 		setgid(getgid());
5179b50d902SRodney W. Grimes 
5186b463eedSChristian S.J. Peron 	if (Bflag) {
519feda1a43SJohn Baldwin 		if (!live)
520feda1a43SJohn Baldwin 			usage();
5216b463eedSChristian S.J. Peron 		bpf_stats(interface);
5226b463eedSChristian S.J. Peron 		exit(0);
5236b463eedSChristian S.J. Peron 	}
5249b50d902SRodney W. Grimes 	if (mflag) {
52583708764SRuslan Ermilov 		if (!live) {
526feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
527d4426f28SRobert Watson 				mbpr(kvmd, nl[N_MBSTAT].n_value);
528d15c5f56SRuslan Ermilov 		} else
529d4426f28SRobert Watson 			mbpr(NULL, 0);
5309b50d902SRodney W. Grimes 		exit(0);
5319b50d902SRodney W. Grimes 	}
5320153eb66SRobert Watson 	if (Qflag) {
53388737be2SRobert Watson 		if (!live) {
53488737be2SRobert Watson 			if (kread(0, NULL, 0) == 0)
53588737be2SRobert Watson 				netisr_stats(kvmd);
53688737be2SRobert Watson 		} else
53788737be2SRobert Watson 			netisr_stats(NULL);
5380153eb66SRobert Watson 		exit(0);
5390153eb66SRobert Watson 	}
540cc63cd56SPeter Wemm #if 0
5419b50d902SRodney W. Grimes 	/*
5429b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5439b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5449b50d902SRodney W. Grimes 	 */
5459b50d902SRodney W. Grimes 	sethostent(1);
5469b50d902SRodney W. Grimes 	setnetent(1);
547cc63cd56SPeter Wemm #else
548cc63cd56SPeter Wemm 	/*
549cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
550cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
551cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
552cc63cd56SPeter Wemm 	 */
553cc63cd56SPeter Wemm #endif
554feda1a43SJohn Baldwin 	kread(0, NULL, 0);
555cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
556cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5579b50d902SRodney W. Grimes 		exit(0);
5589b50d902SRodney W. Grimes 	}
5599b50d902SRodney W. Grimes 	if (rflag) {
5609b50d902SRodney W. Grimes 		if (sflag)
5611093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
5629b50d902SRodney W. Grimes 		else
5639b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5649b50d902SRodney W. Grimes 		exit(0);
5659b50d902SRodney W. Grimes 	}
5669b50d902SRodney W. Grimes 	if (gflag) {
567cfa1ca9dSYoshinobu Inoue 		if (sflag) {
568cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
56967a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
570cfa1ca9dSYoshinobu Inoue #ifdef INET6
571cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
572cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
573cfa1ca9dSYoshinobu Inoue #endif
574cfa1ca9dSYoshinobu Inoue 		} else {
575cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
576443fc317SBruce M Simpson 				mroutepr(nl[N_MFCHASHTBL].n_value,
577443fc317SBruce M Simpson 					 nl[N_MFCTABLESIZE].n_value,
5789b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
579cfa1ca9dSYoshinobu Inoue #ifdef INET6
580cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
581cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
582cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
583cfa1ca9dSYoshinobu Inoue #endif
584cfa1ca9dSYoshinobu Inoue 		}
5859b50d902SRodney W. Grimes 		exit(0);
5869b50d902SRodney W. Grimes 	}
587cfa1ca9dSYoshinobu Inoue 
588cf5e44f8SRuslan Ermilov 	if (tp) {
589cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
590cf5e44f8SRuslan Ermilov 		exit(0);
591cf5e44f8SRuslan Ermilov 	}
592cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5939b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
594cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
595cfa1ca9dSYoshinobu Inoue #ifdef INET6
596cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
597cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
598cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
599cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
6003b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
6013b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
6023b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
6033b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
6043b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
6059cc22e5cSPoul-Henning Kamp #ifdef IPX
606e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
607cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
608cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
609e30e913cSJohn Hay 	}
6109cc22e5cSPoul-Henning Kamp #endif /* IPX */
61163bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
61263bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
61363bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
614690f477dSSam Leffler #ifdef NETGRAPH
6154cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
6164cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
6174cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
618690f477dSSam Leffler #endif /* NETGRAPH */
619f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
620feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
621963b7ccdSRobert Watson 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
622963b7ccdSRobert Watson 		    nl[N_UNP_SPHEAD].n_value);
6239b50d902SRodney W. Grimes 	exit(0);
6249b50d902SRodney W. Grimes }
6259b50d902SRodney W. Grimes 
6269b50d902SRodney W. Grimes /*
6279b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6289b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6299b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6309b50d902SRodney W. Grimes  */
6319b50d902SRodney W. Grimes static void
6329b50d902SRodney W. Grimes printproto(tp, name)
633a01e3379SDavid Malone 	struct protox *tp;
634fa6d48c0SMark Murray 	const char *name;
6359b50d902SRodney W. Grimes {
636feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6379b50d902SRodney W. Grimes 	u_long off;
6389b50d902SRodney W. Grimes 
6399b50d902SRodney W. Grimes 	if (sflag) {
640cfa1ca9dSYoshinobu Inoue 		if (iflag) {
641cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
642cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
643cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
644cf5e44f8SRuslan Ermilov 			else if (pflag)
645cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
646cf5e44f8SRuslan Ermilov 				    tp->pr_name);
647cfa1ca9dSYoshinobu Inoue 			return;
648feda1a43SJohn Baldwin 		} else {
6499b50d902SRodney W. Grimes 			pr = tp->pr_stats;
650cf5e44f8SRuslan Ermilov 			if (!pr) {
651cf5e44f8SRuslan Ermilov 				if (pflag)
652cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
653cf5e44f8SRuslan Ermilov 					    tp->pr_name);
654cf5e44f8SRuslan Ermilov 				return;
655cf5e44f8SRuslan Ermilov 			}
656feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
657feda1a43SJohn Baldwin 				off = 0;
658feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
659feda1a43SJohn Baldwin 				if (pflag)
660feda1a43SJohn Baldwin 					printf(
661feda1a43SJohn Baldwin 				    "%s: stats routine doesn't work on cores\n",
662feda1a43SJohn Baldwin 					    tp->pr_name);
663feda1a43SJohn Baldwin 				return;
664feda1a43SJohn Baldwin 			} else
665feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
666cfa1ca9dSYoshinobu Inoue 		}
6679b50d902SRodney W. Grimes 	} else {
6689b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
669cf5e44f8SRuslan Ermilov 		if (!pr) {
670cf5e44f8SRuslan Ermilov 			if (pflag)
671cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
672cf5e44f8SRuslan Ermilov 			return;
673cf5e44f8SRuslan Ermilov 		}
674feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
675feda1a43SJohn Baldwin 			off = 0;
676feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
677feda1a43SJohn Baldwin 			if (pflag)
678feda1a43SJohn Baldwin 				printf(
679feda1a43SJohn Baldwin 				    "%s: PCB routine doesn't work on cores\n",
680feda1a43SJohn Baldwin 				    tp->pr_name);
681feda1a43SJohn Baldwin 			return;
682feda1a43SJohn Baldwin 		} else
683feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6849b50d902SRodney W. Grimes 	}
685feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
686feda1a43SJohn Baldwin 	    af != AF_UNSPEC))
687feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
6889b50d902SRodney W. Grimes }
6899b50d902SRodney W. Grimes 
6909b50d902SRodney W. Grimes /*
6919b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6929b50d902SRodney W. Grimes  */
6939b50d902SRodney W. Grimes int
694feda1a43SJohn Baldwin kread(u_long addr, void *buf, size_t size)
6959b50d902SRodney W. Grimes {
696feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
697feda1a43SJohn Baldwin 
698feda1a43SJohn Baldwin 	if (kvmd == NULL) {
699feda1a43SJohn Baldwin 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
7003b7e5cccSRuslan Ermilov 		setgid(getgid());
70199453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
70299453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
70399453c6aSPoul-Henning Kamp 				if (nlistf)
70499453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
70599453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
70699453c6aSPoul-Henning Kamp 				else
70799453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
70899453c6aSPoul-Henning Kamp 			}
70999453c6aSPoul-Henning Kamp 
71099453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
71199453c6aSPoul-Henning Kamp 				if (nlistf)
71299453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
71399453c6aSPoul-Henning Kamp 				else
71499453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
71599453c6aSPoul-Henning Kamp 			}
71699453c6aSPoul-Henning Kamp 		} else {
717feda1a43SJohn Baldwin 			warnx("kvm not available: %s", errbuf);
71899453c6aSPoul-Henning Kamp 			return(-1);
71999453c6aSPoul-Henning Kamp 		}
7204f81ef50SGarrett Wollman 	}
721c6620a13SPoul-Henning Kamp 	if (!buf)
722c6620a13SPoul-Henning Kamp 		return (0);
723feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
7243aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
7259b50d902SRodney W. Grimes 		return (-1);
7269b50d902SRodney W. Grimes 	}
7279b50d902SRodney W. Grimes 	return (0);
7289b50d902SRodney W. Grimes }
7299b50d902SRodney W. Grimes 
730a01e3379SDavid Malone const char *
7317b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
7329b50d902SRodney W. Grimes {
7339b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7349b50d902SRodney W. Grimes }
7359b50d902SRodney W. Grimes 
736a01e3379SDavid Malone const char *
7377b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
7389b50d902SRodney W. Grimes {
7399b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7409b50d902SRodney W. Grimes }
7419b50d902SRodney W. Grimes 
742f99a4046SMike Makonnen const char *
7437b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
744f99a4046SMike Makonnen {
745f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
746f99a4046SMike Makonnen }
747f99a4046SMike Makonnen 
7489b50d902SRodney W. Grimes /*
7499b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7509b50d902SRodney W. Grimes  */
7519b50d902SRodney W. Grimes static struct protox *
752096146f8SYaroslav Tykhiy knownname(const char *name)
7539b50d902SRodney W. Grimes {
7549b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7559b50d902SRodney W. Grimes 
7569b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7579b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7589b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7599b50d902SRodney W. Grimes 				return (tp);
7609b50d902SRodney W. Grimes 	return (NULL);
7619b50d902SRodney W. Grimes }
7629b50d902SRodney W. Grimes 
7639b50d902SRodney W. Grimes /*
7649b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7659b50d902SRodney W. Grimes  */
7669b50d902SRodney W. Grimes static struct protox *
767096146f8SYaroslav Tykhiy name2protox(const char *name)
7689b50d902SRodney W. Grimes {
7699b50d902SRodney W. Grimes 	struct protox *tp;
7709b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7719b50d902SRodney W. Grimes 	struct protoent *p;
7729b50d902SRodney W. Grimes 
7739b50d902SRodney W. Grimes 	/*
7749b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7759b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7769b50d902SRodney W. Grimes 	 */
777cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7789b50d902SRodney W. Grimes 		return (tp);
7799b50d902SRodney W. Grimes 
7809b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
781cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7829b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7839b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7849b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7859b50d902SRodney W. Grimes 				endprotoent();
7869b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7879b50d902SRodney W. Grimes 			}
7889b50d902SRodney W. Grimes 	}
7899b50d902SRodney W. Grimes 	endprotoent();
7909b50d902SRodney W. Grimes 	return (NULL);
7919b50d902SRodney W. Grimes }
7929b50d902SRodney W. Grimes 
7939b50d902SRodney W. Grimes static void
7945e051718SAssar Westerlund usage(void)
7959b50d902SRodney W. Grimes {
7960153eb66SRobert 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",
79782d383bcSRuslan Ermilov "usage: netstat [-AaLnSWx] [-f protocol_family | -p protocol]\n"
7981cb467b1SRuslan Ermilov "               [-M core] [-N system]",
7993ee7e0d4SJohn Baldwin "       netstat -i | -I interface [-abdhnW] [-f address_family]\n"
800d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
801bf10ffe1SXin LI "       netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
80282d383bcSRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
80382d383bcSRuslan Ermilov "               [-M core] [-N system]",
8041cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
8051cb467b1SRuslan Ermilov "               [-M core] [-N system]",
80649c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
8075dbd2da1SChristian S.J. Peron "       netstat -B [-I interface]",
80882d383bcSRuslan Ermilov "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
8091cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
8101cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
8110153eb66SRobert Watson "       netstat -gs [-s] [-f address_family] [-M core] [-N system]",
8120153eb66SRobert Watson "       netstat -Q");
8139b50d902SRodney W. Grimes 	exit(1);
8149b50d902SRodney W. Grimes }
815