xref: /freebsd/usr.bin/netstat/main.c (revision 82d383bc961d7835346fe7444d0ccaef256aba8c)
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>
639b50d902SRodney W. Grimes #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>
689b50d902SRodney W. Grimes #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" },
85ab54ea99SPeter Wemm #define	N_MFCTABLE	4
86096146f8SYaroslav Tykhiy 	{ .n_name = "_mfctable" },
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" },
185096146f8SYaroslav Tykhiy 	{ .n_name = NULL },
1869b50d902SRodney W. Grimes };
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes struct protox {
189feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
190feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
1919b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
192feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
1935e051718SAssar Westerlund 					/* control blocks printing routine */
194feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
1955e051718SAssar Westerlund 					/* statistics printing routine */
1965e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
197fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
19855fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
199feda1a43SJohn Baldwin 	int	pr_protocol;
2009b50d902SRodney W. Grimes } protox[] = {
201feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
202feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
203feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
204feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
20574fd40c9SRandall Stewart #ifdef SCTP
206feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
207feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
20874fd40c9SRandall Stewart #endif
209feda1a43SJohn Baldwin 	{ N_DIVCBINFO,	-1,		1,	protopr,
210feda1a43SJohn Baldwin 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
211feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
212feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
213feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
214feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
215feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
216feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
217cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2188409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
219feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
2208409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
221feda1a43SJohn Baldwin 	  ah_stats,	NULL,		"ah",	0,	0},
2228409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
223feda1a43SJohn Baldwin 	  esp_stats,	NULL,		"esp",	0,	0},
2248409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
225feda1a43SJohn Baldwin 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
226100b98dbSKelly Yancey #endif
227feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
228feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
229feda1a43SJohn Baldwin 	{ -1,		N_CARPSTAT,	1,	NULL,
230feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
231feda1a43SJohn Baldwin 	{ -1,		N_PFSYNCSTAT,	1,	NULL,
232feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
2336bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
234feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2359b50d902SRodney W. Grimes };
2369b50d902SRodney W. Grimes 
237cfa1ca9dSYoshinobu Inoue #ifdef INET6
238cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
239feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
240feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
241feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
242feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
243feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
244feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
245feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
246feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
247cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2486bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
249feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
250cfa1ca9dSYoshinobu Inoue #endif
251cfa1ca9dSYoshinobu Inoue #ifdef notyet
2526bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
253feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
254cfa1ca9dSYoshinobu Inoue #endif
255feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
256feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
2576bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
258feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
259cfa1ca9dSYoshinobu Inoue };
260cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
261cfa1ca9dSYoshinobu Inoue 
2623b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2633b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2646bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
265feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
2666bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
267feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2683b8a8567SJun-ichiro itojun Hagino };
2693b8a8567SJun-ichiro itojun Hagino #endif
2703b8a8567SJun-ichiro itojun Hagino 
27163bf4575SJulian Elischer struct protox atalkprotox[] = {
27263bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
273feda1a43SJohn Baldwin 	  ddp_stats,	NULL,		"ddp",	0,	0 },
2746bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
275feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
27663bf4575SJulian Elischer };
277690f477dSSam Leffler #ifdef NETGRAPH
2784cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2794cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
280feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
2814cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
282feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
2836bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
284feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2854cf49a43SJulian Elischer };
286690f477dSSam Leffler #endif
2879cc22e5cSPoul-Henning Kamp #ifdef IPX
288cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
289cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
290feda1a43SJohn Baldwin 	  ipx_stats,	NULL,		"ipx",	0,	0 },
291cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
292feda1a43SJohn Baldwin 	  spx_stats,	NULL,		"spx",	0,	0 },
2936bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
294feda1a43SJohn Baldwin 	  NULL,		NULL,		0,	0,	0 }
295cc6a66f2SJulian Elischer };
2969cc22e5cSPoul-Henning Kamp #endif
297cc6a66f2SJulian Elischer 
298cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
299cfa1ca9dSYoshinobu Inoue 					 protox,
300cfa1ca9dSYoshinobu Inoue #ifdef INET6
301cfa1ca9dSYoshinobu Inoue 					 ip6protox,
302cfa1ca9dSYoshinobu Inoue #endif
3033b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
3043b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
3053b8a8567SJun-ichiro itojun Hagino #endif
3069cc22e5cSPoul-Henning Kamp #ifdef IPX
3079cc22e5cSPoul-Henning Kamp 					 ipxprotox,
3089cc22e5cSPoul-Henning Kamp #endif
3099cc22e5cSPoul-Henning Kamp 					 atalkprotox, NULL };
3109b50d902SRodney W. Grimes 
311fa6d48c0SMark Murray static void printproto(struct protox *, const char *);
3125e051718SAssar Westerlund static void usage(void);
313096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
314096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
3159b50d902SRodney W. Grimes 
316c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
317080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
318080b7f49SDag-Erling Smørgrav 
319080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
320080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
3216b463eedSChristian S.J. Peron int	Bflag;		/* show information about bpf consumers */
322080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
323080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
324080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
325c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
326080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
327080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
328080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
32965ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
33065ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
331cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
332080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
333080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
334080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
335080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
33649f287f8SGeorge V. Neville-Neil int	xflag;		/* extra information, includes all socket buffer info */
337c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
338080b7f49SDag-Erling Smørgrav 
339080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
340080b7f49SDag-Erling Smørgrav 
341080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
342080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
343080b7f49SDag-Erling Smørgrav 
344080b7f49SDag-Erling Smørgrav int	af;		/* address family */
345feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
3469b50d902SRodney W. Grimes 
3479b50d902SRodney W. Grimes int
348a01e3379SDavid Malone main(int argc, char *argv[])
3499b50d902SRodney W. Grimes {
350a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3519b50d902SRodney W. Grimes 	int ch;
3529b50d902SRodney W. Grimes 
3539b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3549b50d902SRodney W. Grimes 
35549f287f8SGeorge V. Neville-Neil 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:xz")) != -1)
3569b50d902SRodney W. Grimes 		switch(ch) {
3579b50d902SRodney W. Grimes 		case 'A':
3589b50d902SRodney W. Grimes 			Aflag = 1;
3599b50d902SRodney W. Grimes 			break;
3609b50d902SRodney W. Grimes 		case 'a':
3619b50d902SRodney W. Grimes 			aflag = 1;
3629b50d902SRodney W. Grimes 			break;
3636b463eedSChristian S.J. Peron 		case 'B':
3646b463eedSChristian S.J. Peron 			Bflag = 1;
3656b463eedSChristian S.J. Peron 			break;
366e1e293a5SDavid Greenman 		case 'b':
367e1e293a5SDavid Greenman 			bflag = 1;
368e1e293a5SDavid Greenman 			break;
3699b50d902SRodney W. Grimes 		case 'd':
3709b50d902SRodney W. Grimes 			dflag = 1;
3719b50d902SRodney W. Grimes 			break;
3729b50d902SRodney W. Grimes 		case 'f':
373cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
374cc6a66f2SJulian Elischer 				af = AF_IPX;
3759b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3769b50d902SRodney W. Grimes 				af = AF_INET;
377cfa1ca9dSYoshinobu Inoue #ifdef INET6
378cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
379cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
380e5134d2eSMax Laier #endif
381e5134d2eSMax Laier #ifdef IPSEC
3823b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3833b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
384e5134d2eSMax Laier #endif
3859b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3869b50d902SRodney W. Grimes 				af = AF_UNIX;
38763bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
38863bf4575SJulian Elischer 				af = AF_APPLETALK;
389690f477dSSam Leffler #ifdef NETGRAPH
3904cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3914cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3924cf49a43SJulian Elischer 				af = AF_NETGRAPH;
393690f477dSSam Leffler #endif
394d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
395d44ddba9SRuslan Ermilov 				af = AF_LINK;
3969b50d902SRodney W. Grimes 			else {
39751e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3989b50d902SRodney W. Grimes 			}
3999b50d902SRodney W. Grimes 			break;
4009b50d902SRodney W. Grimes 		case 'g':
4019b50d902SRodney W. Grimes 			gflag = 1;
4029b50d902SRodney W. Grimes 			break;
403c2dfd19fSGleb Smirnoff 		case 'h':
404c2dfd19fSGleb Smirnoff 			hflag = 1;
405c2dfd19fSGleb Smirnoff 			break;
4069b50d902SRodney W. Grimes 		case 'I': {
4079b50d902SRodney W. Grimes 			char *cp;
4089b50d902SRodney W. Grimes 
4099b50d902SRodney W. Grimes 			iflag = 1;
4109b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4119b50d902SRodney W. Grimes 				continue;
4129b50d902SRodney W. Grimes 			unit = atoi(cp);
4139b50d902SRodney W. Grimes 			break;
4149b50d902SRodney W. Grimes 		}
4159b50d902SRodney W. Grimes 		case 'i':
4169b50d902SRodney W. Grimes 			iflag = 1;
4179b50d902SRodney W. Grimes 			break;
418ac55add0SGuido van Rooij 		case 'L':
419ac55add0SGuido van Rooij 			Lflag = 1;
420ac55add0SGuido van Rooij 			break;
4219b50d902SRodney W. Grimes 		case 'M':
4229b50d902SRodney W. Grimes 			memf = optarg;
4239b50d902SRodney W. Grimes 			break;
4249b50d902SRodney W. Grimes 		case 'm':
4259b50d902SRodney W. Grimes 			mflag = 1;
4269b50d902SRodney W. Grimes 			break;
4279b50d902SRodney W. Grimes 		case 'N':
4289b50d902SRodney W. Grimes 			nlistf = optarg;
4299b50d902SRodney W. Grimes 			break;
4309b50d902SRodney W. Grimes 		case 'n':
43165ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4329b50d902SRodney W. Grimes 			break;
4339b50d902SRodney W. Grimes 		case 'p':
4349b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
43551e7d42cSGarrett Wollman 				errx(1,
43651e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
43751e7d42cSGarrett Wollman 				     optarg);
4389b50d902SRodney W. Grimes 			}
4399b50d902SRodney W. Grimes 			pflag = 1;
4409b50d902SRodney W. Grimes 			break;
4419b50d902SRodney W. Grimes 		case 'r':
4429b50d902SRodney W. Grimes 			rflag = 1;
4439b50d902SRodney W. Grimes 			break;
4449b50d902SRodney W. Grimes 		case 's':
4459b50d902SRodney W. Grimes 			++sflag;
4469b50d902SRodney W. Grimes 			break;
44765ea0024SAssar Westerlund 		case 'S':
44865ea0024SAssar Westerlund 			numeric_addr = 1;
44965ea0024SAssar Westerlund 			break;
4509b50d902SRodney W. Grimes 		case 't':
4519b50d902SRodney W. Grimes 			tflag = 1;
4529b50d902SRodney W. Grimes 			break;
4539b50d902SRodney W. Grimes 		case 'u':
4549b50d902SRodney W. Grimes 			af = AF_UNIX;
4559b50d902SRodney W. Grimes 			break;
456080b7f49SDag-Erling Smørgrav 		case 'W':
45770057abfSRuslan Ermilov 		case 'l':
458080b7f49SDag-Erling Smørgrav 			Wflag = 1;
459080b7f49SDag-Erling Smørgrav 			break;
4609b50d902SRodney W. Grimes 		case 'w':
4619b50d902SRodney W. Grimes 			interval = atoi(optarg);
4629b50d902SRodney W. Grimes 			iflag = 1;
4639b50d902SRodney W. Grimes 			break;
46449f287f8SGeorge V. Neville-Neil 		case 'x':
46549f287f8SGeorge V. Neville-Neil 			xflag = 1;
46649f287f8SGeorge V. Neville-Neil 			break;
467c73d99b5SRuslan Ermilov 		case 'z':
468c73d99b5SRuslan Ermilov 			zflag = 1;
469c73d99b5SRuslan Ermilov 			break;
4709b50d902SRodney W. Grimes 		case '?':
4719b50d902SRodney W. Grimes 		default:
4729b50d902SRodney W. Grimes 			usage();
4739b50d902SRodney W. Grimes 		}
4749b50d902SRodney W. Grimes 	argv += optind;
4759b50d902SRodney W. Grimes 	argc -= optind;
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4789b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4799b50d902SRodney W. Grimes 	if (*argv) {
4809b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4819b50d902SRodney W. Grimes 			interval = atoi(*argv);
4829b50d902SRodney W. Grimes 			if (interval <= 0)
4839b50d902SRodney W. Grimes 				usage();
4849b50d902SRodney W. Grimes 			++argv;
4859b50d902SRodney W. Grimes 			iflag = 1;
4869b50d902SRodney W. Grimes 		}
4879b50d902SRodney W. Grimes 		if (*argv) {
4889b50d902SRodney W. Grimes 			nlistf = *argv;
4899b50d902SRodney W. Grimes 			if (*++argv)
4909b50d902SRodney W. Grimes 				memf = *argv;
4919b50d902SRodney W. Grimes 		}
4929b50d902SRodney W. Grimes 	}
4939b50d902SRodney W. Grimes #endif
4949b50d902SRodney W. Grimes 
4959b50d902SRodney W. Grimes 	/*
4969b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4979b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4989b50d902SRodney W. Grimes 	 */
499feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
500feda1a43SJohn Baldwin 	if (!live)
5019b50d902SRodney W. Grimes 		setgid(getgid());
5029b50d902SRodney W. Grimes 
5036b463eedSChristian S.J. Peron 	if (Bflag) {
504feda1a43SJohn Baldwin 		if (!live)
505feda1a43SJohn Baldwin 			usage();
5066b463eedSChristian S.J. Peron 		bpf_stats(interface);
5076b463eedSChristian S.J. Peron 		exit(0);
5086b463eedSChristian S.J. Peron 	}
5099b50d902SRodney W. Grimes 	if (mflag) {
51083708764SRuslan Ermilov 		if (!live) {
511feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
512d4426f28SRobert Watson 				mbpr(kvmd, nl[N_MBSTAT].n_value);
513d15c5f56SRuslan Ermilov 		} else
514d4426f28SRobert Watson 			mbpr(NULL, 0);
5159b50d902SRodney W. Grimes 		exit(0);
5169b50d902SRodney W. Grimes 	}
517cc63cd56SPeter Wemm #if 0
5189b50d902SRodney W. Grimes 	/*
5199b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5209b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5219b50d902SRodney W. Grimes 	 */
5229b50d902SRodney W. Grimes 	sethostent(1);
5239b50d902SRodney W. Grimes 	setnetent(1);
524cc63cd56SPeter Wemm #else
525cc63cd56SPeter Wemm 	/*
526cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
527cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
528cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
529cc63cd56SPeter Wemm 	 */
530cc63cd56SPeter Wemm #endif
531feda1a43SJohn Baldwin 	kread(0, NULL, 0);
532cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
533cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5349b50d902SRodney W. Grimes 		exit(0);
5359b50d902SRodney W. Grimes 	}
5369b50d902SRodney W. Grimes 	if (rflag) {
5379b50d902SRodney W. Grimes 		if (sflag)
5381093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
5399b50d902SRodney W. Grimes 		else
5409b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5419b50d902SRodney W. Grimes 		exit(0);
5429b50d902SRodney W. Grimes 	}
5439b50d902SRodney W. Grimes 	if (gflag) {
544cfa1ca9dSYoshinobu Inoue 		if (sflag) {
545cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54667a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
547cfa1ca9dSYoshinobu Inoue #ifdef INET6
548cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
549cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
550cfa1ca9dSYoshinobu Inoue #endif
551cfa1ca9dSYoshinobu Inoue 		} else {
552cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
55367a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5549b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
555cfa1ca9dSYoshinobu Inoue #ifdef INET6
556cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
557cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
558cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
559cfa1ca9dSYoshinobu Inoue #endif
560cfa1ca9dSYoshinobu Inoue 		}
5619fcc066dSBruce M Simpson 		ifmalist_dump();
5629b50d902SRodney W. Grimes 		exit(0);
5639b50d902SRodney W. Grimes 	}
564cfa1ca9dSYoshinobu Inoue 
565cf5e44f8SRuslan Ermilov 	if (tp) {
566cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
567cf5e44f8SRuslan Ermilov 		exit(0);
568cf5e44f8SRuslan Ermilov 	}
569cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5709b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
571cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
572cfa1ca9dSYoshinobu Inoue #ifdef INET6
573cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
574cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
575cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
576cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5773b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5783b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5793b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5803b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5813b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
5829cc22e5cSPoul-Henning Kamp #ifdef IPX
583e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
584cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
585cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
586e30e913cSJohn Hay 	}
5879cc22e5cSPoul-Henning Kamp #endif /* IPX */
58863bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
58963bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
59063bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
591690f477dSSam Leffler #ifdef NETGRAPH
5924cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5934cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5944cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
595690f477dSSam Leffler #endif /* NETGRAPH */
596f1c0a78dSMaxim Konovalov 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
597feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
598feda1a43SJohn Baldwin 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
5999b50d902SRodney W. Grimes 	exit(0);
6009b50d902SRodney W. Grimes }
6019b50d902SRodney W. Grimes 
6029b50d902SRodney W. Grimes /*
6039b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6049b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6059b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6069b50d902SRodney W. Grimes  */
6079b50d902SRodney W. Grimes static void
6089b50d902SRodney W. Grimes printproto(tp, name)
609a01e3379SDavid Malone 	struct protox *tp;
610fa6d48c0SMark Murray 	const char *name;
6119b50d902SRodney W. Grimes {
612feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6139b50d902SRodney W. Grimes 	u_long off;
6149b50d902SRodney W. Grimes 
6159b50d902SRodney W. Grimes 	if (sflag) {
616cfa1ca9dSYoshinobu Inoue 		if (iflag) {
617cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
618cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
619cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
620cf5e44f8SRuslan Ermilov 			else if (pflag)
621cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
622cf5e44f8SRuslan Ermilov 				    tp->pr_name);
623cfa1ca9dSYoshinobu Inoue 			return;
624feda1a43SJohn Baldwin 		} else {
6259b50d902SRodney W. Grimes 			pr = tp->pr_stats;
626cf5e44f8SRuslan Ermilov 			if (!pr) {
627cf5e44f8SRuslan Ermilov 				if (pflag)
628cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
629cf5e44f8SRuslan Ermilov 					    tp->pr_name);
630cf5e44f8SRuslan Ermilov 				return;
631cf5e44f8SRuslan Ermilov 			}
632feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
633feda1a43SJohn Baldwin 				off = 0;
634feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
635feda1a43SJohn Baldwin 				if (pflag)
636feda1a43SJohn Baldwin 					printf(
637feda1a43SJohn Baldwin 				    "%s: stats routine doesn't work on cores\n",
638feda1a43SJohn Baldwin 					    tp->pr_name);
639feda1a43SJohn Baldwin 				return;
640feda1a43SJohn Baldwin 			} else
641feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
642cfa1ca9dSYoshinobu Inoue 		}
6439b50d902SRodney W. Grimes 	} else {
6449b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
645cf5e44f8SRuslan Ermilov 		if (!pr) {
646cf5e44f8SRuslan Ermilov 			if (pflag)
647cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
648cf5e44f8SRuslan Ermilov 			return;
649cf5e44f8SRuslan Ermilov 		}
650feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
651feda1a43SJohn Baldwin 			off = 0;
652feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
653feda1a43SJohn Baldwin 			if (pflag)
654feda1a43SJohn Baldwin 				printf(
655feda1a43SJohn Baldwin 				    "%s: PCB routine doesn't work on cores\n",
656feda1a43SJohn Baldwin 				    tp->pr_name);
657feda1a43SJohn Baldwin 			return;
658feda1a43SJohn Baldwin 		} else
659feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6609b50d902SRodney W. Grimes 	}
661feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
662feda1a43SJohn Baldwin 	    af != AF_UNSPEC))
663feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
6649b50d902SRodney W. Grimes }
6659b50d902SRodney W. Grimes 
6669b50d902SRodney W. Grimes /*
6679b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6689b50d902SRodney W. Grimes  */
6699b50d902SRodney W. Grimes int
670feda1a43SJohn Baldwin kread(u_long addr, void *buf, size_t size)
6719b50d902SRodney W. Grimes {
672feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
673feda1a43SJohn Baldwin 
674feda1a43SJohn Baldwin 	if (kvmd == NULL) {
675feda1a43SJohn Baldwin 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
6763b7e5cccSRuslan Ermilov 		setgid(getgid());
67799453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
67899453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
67999453c6aSPoul-Henning Kamp 				if (nlistf)
68099453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
68199453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
68299453c6aSPoul-Henning Kamp 				else
68399453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
68499453c6aSPoul-Henning Kamp 			}
68599453c6aSPoul-Henning Kamp 
68699453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
68799453c6aSPoul-Henning Kamp 				if (nlistf)
68899453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
68999453c6aSPoul-Henning Kamp 				else
69099453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
69199453c6aSPoul-Henning Kamp 			}
69299453c6aSPoul-Henning Kamp 		} else {
693feda1a43SJohn Baldwin 			warnx("kvm not available: %s", errbuf);
69499453c6aSPoul-Henning Kamp 			return(-1);
69599453c6aSPoul-Henning Kamp 		}
6964f81ef50SGarrett Wollman 	}
697c6620a13SPoul-Henning Kamp 	if (!buf)
698c6620a13SPoul-Henning Kamp 		return (0);
699feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
7003aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
7019b50d902SRodney W. Grimes 		return (-1);
7029b50d902SRodney W. Grimes 	}
7039b50d902SRodney W. Grimes 	return (0);
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes 
706a01e3379SDavid Malone const char *
7077b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
7089b50d902SRodney W. Grimes {
7099b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7109b50d902SRodney W. Grimes }
7119b50d902SRodney W. Grimes 
712a01e3379SDavid Malone const char *
7137b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
7149b50d902SRodney W. Grimes {
7159b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7169b50d902SRodney W. Grimes }
7179b50d902SRodney W. Grimes 
718f99a4046SMike Makonnen const char *
7197b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
720f99a4046SMike Makonnen {
721f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
722f99a4046SMike Makonnen }
723f99a4046SMike Makonnen 
7249b50d902SRodney W. Grimes /*
7259b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7269b50d902SRodney W. Grimes  */
7279b50d902SRodney W. Grimes static struct protox *
728096146f8SYaroslav Tykhiy knownname(const char *name)
7299b50d902SRodney W. Grimes {
7309b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7319b50d902SRodney W. Grimes 
7329b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7339b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7349b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7359b50d902SRodney W. Grimes 				return (tp);
7369b50d902SRodney W. Grimes 	return (NULL);
7379b50d902SRodney W. Grimes }
7389b50d902SRodney W. Grimes 
7399b50d902SRodney W. Grimes /*
7409b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7419b50d902SRodney W. Grimes  */
7429b50d902SRodney W. Grimes static struct protox *
743096146f8SYaroslav Tykhiy name2protox(const char *name)
7449b50d902SRodney W. Grimes {
7459b50d902SRodney W. Grimes 	struct protox *tp;
7469b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7479b50d902SRodney W. Grimes 	struct protoent *p;
7489b50d902SRodney W. Grimes 
7499b50d902SRodney W. Grimes 	/*
7509b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7519b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7529b50d902SRodney W. Grimes 	 */
753cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7549b50d902SRodney W. Grimes 		return (tp);
7559b50d902SRodney W. Grimes 
7569b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
757cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7589b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7599b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7609b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7619b50d902SRodney W. Grimes 				endprotoent();
7629b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7639b50d902SRodney W. Grimes 			}
7649b50d902SRodney W. Grimes 	}
7659b50d902SRodney W. Grimes 	endprotoent();
7669b50d902SRodney W. Grimes 	return (NULL);
7679b50d902SRodney W. Grimes }
7689b50d902SRodney W. Grimes 
7699b50d902SRodney W. Grimes static void
7705e051718SAssar Westerlund usage(void)
7719b50d902SRodney W. Grimes {
7725dbd2da1SChristian S.J. Peron 	(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",
77382d383bcSRuslan Ermilov "usage: netstat [-AaLnSWx] [-f protocol_family | -p protocol]\n"
7741cb467b1SRuslan Ermilov "               [-M core] [-N system]",
77582d383bcSRuslan Ermilov "       netstat -i | -I interface [-abdhntW] [-f address_family]\n"
776d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7771cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
77882d383bcSRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
77982d383bcSRuslan Ermilov "               [-M core] [-N system]",
7801cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7811cb467b1SRuslan Ermilov "               [-M core] [-N system]",
78249c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
7835dbd2da1SChristian S.J. Peron "       netstat -B [-I interface]",
78482d383bcSRuslan Ermilov "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
7851cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7861cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7871cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7889b50d902SRodney W. Grimes 	exit(1);
7899b50d902SRodney W. Grimes }
790