xref: /freebsd/usr.bin/netstat/main.c (revision 55fd53e237087793de39914acf4fd3c801e2cd8c)
19b50d902SRodney W. Grimes /*
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 
574cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
584cf49a43SJulian Elischer 
599b50d902SRodney W. Grimes #include <ctype.h>
605d422d6aSPhilippe Charnier #include <err.h>
619b50d902SRodney W. Grimes #include <errno.h>
629b50d902SRodney W. Grimes #include <kvm.h>
639b50d902SRodney W. Grimes #include <limits.h>
649b50d902SRodney W. Grimes #include <netdb.h>
659b50d902SRodney W. Grimes #include <nlist.h>
669b50d902SRodney W. Grimes #include <paths.h>
677b95a1ebSYaroslav Tykhiy #include <stdint.h>
689b50d902SRodney W. Grimes #include <stdio.h>
699b50d902SRodney W. Grimes #include <stdlib.h>
709b50d902SRodney W. Grimes #include <string.h>
719b50d902SRodney W. Grimes #include <unistd.h>
729b50d902SRodney W. Grimes #include "netstat.h"
739b50d902SRodney W. Grimes 
74c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
754f81ef50SGarrett Wollman #define	N_IFNET		0
76096146f8SYaroslav Tykhiy 	{ .n_name = "_ifnet" },
77ab54ea99SPeter Wemm #define	N_RTSTAT	1
78096146f8SYaroslav Tykhiy 	{ .n_name = "_rtstat" },
79ab54ea99SPeter Wemm #define N_RTREE		2
80096146f8SYaroslav Tykhiy 	{ .n_name = "_rt_tables"},
81ab54ea99SPeter Wemm #define N_MRTSTAT	3
82096146f8SYaroslav Tykhiy 	{ .n_name = "_mrtstat" },
83ab54ea99SPeter Wemm #define N_MFCTABLE	4
84096146f8SYaroslav Tykhiy 	{ .n_name = "_mfctable" },
85ab54ea99SPeter Wemm #define N_VIFTABLE	5
86096146f8SYaroslav Tykhiy 	{ .n_name = "_viftable" },
87ab54ea99SPeter Wemm #define N_IPX		6
88096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxpcb_list"},
89ab54ea99SPeter Wemm #define N_IPXSTAT	7
90096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxstat"},
91ab54ea99SPeter Wemm #define N_SPXSTAT	8
92096146f8SYaroslav Tykhiy 	{ .n_name = "_spx_istat"},
93ab54ea99SPeter Wemm #define N_DDPSTAT	9
94096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpstat"},
95ab54ea99SPeter Wemm #define N_DDPCB		10
96096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpcb"},
97ab54ea99SPeter Wemm #define N_NGSOCKS	11
98096146f8SYaroslav Tykhiy 	{ .n_name = "_ngsocklist"},
99ab54ea99SPeter Wemm #define N_IP6STAT	12
100096146f8SYaroslav Tykhiy 	{ .n_name = "_ip6stat" },
101ab54ea99SPeter Wemm #define N_ICMP6STAT	13
102096146f8SYaroslav Tykhiy 	{ .n_name = "_icmp6stat" },
103ab54ea99SPeter Wemm #define N_IPSECSTAT	14
1048409aedfSGeorge V. Neville-Neil 	{ .n_name = "_ipsec4stat" },
105ab54ea99SPeter Wemm #define N_IPSEC6STAT	15
106096146f8SYaroslav Tykhiy 	{ .n_name = "_ipsec6stat" },
107ab54ea99SPeter Wemm #define N_PIM6STAT	16
108096146f8SYaroslav Tykhiy 	{ .n_name = "_pim6stat" },
109ab54ea99SPeter Wemm #define N_MRT6STAT	17
110096146f8SYaroslav Tykhiy 	{ .n_name = "_mrt6stat" },
111ab54ea99SPeter Wemm #define N_MF6CTABLE	18
112096146f8SYaroslav Tykhiy 	{ .n_name = "_mf6ctable" },
113ab54ea99SPeter Wemm #define N_MIF6TABLE	19
114096146f8SYaroslav Tykhiy 	{ .n_name = "_mif6table" },
115ab54ea99SPeter Wemm #define N_PFKEYSTAT	20
116096146f8SYaroslav Tykhiy 	{ .n_name = "_pfkeystat" },
117ab54ea99SPeter Wemm #define N_MBSTAT	21
118096146f8SYaroslav Tykhiy 	{ .n_name = "_mbstat" },
119ab54ea99SPeter Wemm #define N_MBTYPES	22
120096146f8SYaroslav Tykhiy 	{ .n_name = "_mbtypes" },
121ab54ea99SPeter Wemm #define N_NMBCLUSTERS	23
122096146f8SYaroslav Tykhiy 	{ .n_name = "_nmbclusters" },
123ab54ea99SPeter Wemm #define N_NMBUFS	24
124096146f8SYaroslav Tykhiy 	{ .n_name = "_nmbufs" },
125ab54ea99SPeter Wemm #define	N_MBHI		25
126096146f8SYaroslav Tykhiy 	{ .n_name = "_mbuf_hiwm" },
127ab54ea99SPeter Wemm #define	N_CLHI		26
128096146f8SYaroslav Tykhiy 	{ .n_name = "_clust_hiwm" },
129ab54ea99SPeter Wemm #define	N_NCPUS		27
130096146f8SYaroslav Tykhiy 	{ .n_name = "_smp_cpus" },
131ab54ea99SPeter Wemm #define	N_PAGESZ	28
132096146f8SYaroslav Tykhiy 	{ .n_name = "_pagesize" },
133ab54ea99SPeter Wemm #define	N_MBPSTAT	29
134096146f8SYaroslav Tykhiy 	{ .n_name = "_mb_statpcpu" },
135ab54ea99SPeter Wemm #define	N_RTTRASH	30
136096146f8SYaroslav Tykhiy 	{ .n_name = "_rttrash" },
137ab54ea99SPeter Wemm #define	N_MBLO		31
138096146f8SYaroslav Tykhiy 	{ .n_name = "_mbuf_lowm" },
139ab54ea99SPeter Wemm #define	N_CLLO		32
140096146f8SYaroslav Tykhiy 	{ .n_name = "_clust_lowm" },
141a9771948SGleb Smirnoff #define N_CARPSTAT	33
142096146f8SYaroslav Tykhiy 	{ .n_name = "_carpstats" },
1432e37c5a3SMax Laier #define N_PFSYNCSTAT	34
144096146f8SYaroslav Tykhiy 	{ .n_name = "_pfsyncstats" },
145feda1a43SJohn Baldwin #define	N_AHSTAT	35
146096146f8SYaroslav Tykhiy 	{ .n_name = "_ahstat" },
147feda1a43SJohn Baldwin #define	N_ESPSTAT	36
148096146f8SYaroslav Tykhiy 	{ .n_name = "_espstat" },
149feda1a43SJohn Baldwin #define	N_IPCOMPSTAT	37
150096146f8SYaroslav Tykhiy 	{ .n_name = "_ipcompstat" },
151feda1a43SJohn Baldwin #define	N_TCPSTAT	38
152feda1a43SJohn Baldwin 	{ .n_name = "_tcpstat" },
153feda1a43SJohn Baldwin #define	N_UDPSTAT	39
154feda1a43SJohn Baldwin 	{ .n_name = "_udpstat" },
155feda1a43SJohn Baldwin #define	N_IPSTAT	40
156feda1a43SJohn Baldwin 	{ .n_name = "_ipstat" },
157feda1a43SJohn Baldwin #define	N_ICMPSTAT	41
158feda1a43SJohn Baldwin 	{ .n_name = "_icmpstat" },
159feda1a43SJohn Baldwin #define	N_IGMPSTAT	42
160feda1a43SJohn Baldwin 	{ .n_name = "_igmpstat" },
161feda1a43SJohn Baldwin #define	N_PIMSTAT	43
162feda1a43SJohn Baldwin 	{ .n_name = "_pimstat" },
163feda1a43SJohn Baldwin #define	N_TCBINFO	44
164feda1a43SJohn Baldwin 	{ .n_name = "_tcbinfo" },
165feda1a43SJohn Baldwin #define	N_UDBINFO	45
166feda1a43SJohn Baldwin 	{ .n_name = "_udbinfo" },
167feda1a43SJohn Baldwin #define	N_DIVCBINFO	46
168feda1a43SJohn Baldwin 	{ .n_name = "_divcbinfo" },
169feda1a43SJohn Baldwin #define	N_RIPCBINFO	47
170feda1a43SJohn Baldwin 	{ .n_name = "_ripcbinfo" },
171feda1a43SJohn Baldwin #define	N_UNP_COUNT	48
172feda1a43SJohn Baldwin 	{ .n_name = "_unp_count" },
173feda1a43SJohn Baldwin #define	N_UNP_GENCNT	49
174feda1a43SJohn Baldwin 	{ .n_name = "_unp_gencnt" },
175feda1a43SJohn Baldwin #define	N_UNP_DHEAD	50
176feda1a43SJohn Baldwin 	{ .n_name = "_unp_dhead" },
177feda1a43SJohn Baldwin #define	N_UNP_SHEAD	51
178feda1a43SJohn Baldwin 	{ .n_name = "_unp_shead" },
179feda1a43SJohn Baldwin #define	N_RIP6STAT	52
180feda1a43SJohn Baldwin 	{ .n_name = "_rip6stat" },
181feda1a43SJohn Baldwin #define	N_SCTPSTAT	53
182feda1a43SJohn Baldwin 	{ .n_name = "_sctpstat" },
183096146f8SYaroslav Tykhiy 	{ .n_name = NULL },
1849b50d902SRodney W. Grimes };
1859b50d902SRodney W. Grimes 
1869b50d902SRodney W. Grimes struct protox {
187feda1a43SJohn Baldwin 	int	pr_index;		/* index into nlist of cb head */
188feda1a43SJohn Baldwin 	int	pr_sindex;		/* index into nlist of stat block */
1899b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
190feda1a43SJohn Baldwin 	void	(*pr_cblocks)(u_long, const char *, int, int);
1915e051718SAssar Westerlund 					/* control blocks printing routine */
192feda1a43SJohn Baldwin 	void	(*pr_stats)(u_long, const char *, int, int);
1935e051718SAssar Westerlund 					/* statistics printing routine */
1945e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
195fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
19655fd53e2SJohn Baldwin 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
197feda1a43SJohn Baldwin 	int	pr_protocol;
1989b50d902SRodney W. Grimes } protox[] = {
199feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
200feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
201feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
202feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
20374fd40c9SRandall Stewart #ifdef SCTP
204feda1a43SJohn Baldwin 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
205feda1a43SJohn Baldwin 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
20674fd40c9SRandall Stewart #endif
207feda1a43SJohn Baldwin 	{ N_DIVCBINFO,	-1,		1,	protopr,
208feda1a43SJohn Baldwin 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
209feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
210feda1a43SJohn Baldwin 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
211feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
212feda1a43SJohn Baldwin 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
213feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
214feda1a43SJohn Baldwin 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
215cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2168409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
217feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
2188409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
219feda1a43SJohn Baldwin 	  ah_stats,	NULL,		"ah",	0,	0},
2208409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
221feda1a43SJohn Baldwin 	  esp_stats,	NULL,		"esp",	0,	0},
2228409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
223feda1a43SJohn Baldwin 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
224100b98dbSKelly Yancey #endif
225feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
226feda1a43SJohn Baldwin 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
227feda1a43SJohn Baldwin 	{ -1,		N_CARPSTAT,	1,	NULL,
228feda1a43SJohn Baldwin 	  carp_stats,	NULL,		"carp",	1,	0 },
229feda1a43SJohn Baldwin 	{ -1,		N_PFSYNCSTAT,	1,	NULL,
230feda1a43SJohn Baldwin 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
2316bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
232feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2339b50d902SRodney W. Grimes };
2349b50d902SRodney W. Grimes 
235cfa1ca9dSYoshinobu Inoue #ifdef INET6
236cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
237feda1a43SJohn Baldwin 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
238feda1a43SJohn Baldwin 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
239feda1a43SJohn Baldwin 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
240feda1a43SJohn Baldwin 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
241feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
242feda1a43SJohn Baldwin 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
243feda1a43SJohn Baldwin 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
244feda1a43SJohn Baldwin 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
245cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2466bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
247feda1a43SJohn Baldwin 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
248cfa1ca9dSYoshinobu Inoue #endif
249cfa1ca9dSYoshinobu Inoue #ifdef notyet
2506bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
251feda1a43SJohn Baldwin 	  pim6_stats,	NULL,		"pim6",	1,	0 },
252cfa1ca9dSYoshinobu Inoue #endif
253feda1a43SJohn Baldwin 	{ -1,		N_RIP6STAT,	1,	NULL,
254feda1a43SJohn Baldwin 	  rip6_stats,	NULL,		"rip6",	1,	0 },
2556bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
256feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
257cfa1ca9dSYoshinobu Inoue };
258cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
259cfa1ca9dSYoshinobu Inoue 
2603b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2613b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2626bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
263feda1a43SJohn Baldwin 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
2646bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
265feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2663b8a8567SJun-ichiro itojun Hagino };
2673b8a8567SJun-ichiro itojun Hagino #endif
2683b8a8567SJun-ichiro itojun Hagino 
26963bf4575SJulian Elischer struct protox atalkprotox[] = {
27063bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
271feda1a43SJohn Baldwin 	  ddp_stats,	NULL,		"ddp",	0,	0 },
2726bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
273feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
27463bf4575SJulian Elischer };
27563bf4575SJulian Elischer 
2764cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2774cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
278feda1a43SJohn Baldwin 	  NULL,		NULL,		"ctrl",	0,	0 },
2794cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
280feda1a43SJohn Baldwin 	  NULL,		NULL,		"data",	0,	0 },
2816bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
282feda1a43SJohn Baldwin 	  NULL,		NULL,		NULL,	0,	0 }
2834cf49a43SJulian Elischer };
2844cf49a43SJulian Elischer 
2859cc22e5cSPoul-Henning Kamp #ifdef IPX
286cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
287cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
288feda1a43SJohn Baldwin 	  ipx_stats,	NULL,		"ipx",	0,	0 },
289cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
290feda1a43SJohn Baldwin 	  spx_stats,	NULL,		"spx",	0,	0 },
2916bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
292feda1a43SJohn Baldwin 	  NULL,		NULL,		0,	0,	0 }
293cc6a66f2SJulian Elischer };
2949cc22e5cSPoul-Henning Kamp #endif
295cc6a66f2SJulian Elischer 
296cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
297cfa1ca9dSYoshinobu Inoue 					 protox,
298cfa1ca9dSYoshinobu Inoue #ifdef INET6
299cfa1ca9dSYoshinobu Inoue 					 ip6protox,
300cfa1ca9dSYoshinobu Inoue #endif
3013b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
3023b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
3033b8a8567SJun-ichiro itojun Hagino #endif
3049cc22e5cSPoul-Henning Kamp #ifdef IPX
3059cc22e5cSPoul-Henning Kamp 					 ipxprotox,
3069cc22e5cSPoul-Henning Kamp #endif
3079cc22e5cSPoul-Henning Kamp 					 atalkprotox, NULL };
3089b50d902SRodney W. Grimes 
309fa6d48c0SMark Murray static void printproto(struct protox *, const char *);
3105e051718SAssar Westerlund static void usage(void);
311096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
312096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
3139b50d902SRodney W. Grimes 
314c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
315080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
316080b7f49SDag-Erling Smørgrav 
317080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
318080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
3196b463eedSChristian S.J. Peron int	Bflag;		/* show information about bpf consumers */
320080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
321080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
322080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
323c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
324080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
325080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
326080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
32765ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
32865ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
329cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
330080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
331080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
332080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
333080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
334c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
335080b7f49SDag-Erling Smørgrav 
336080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
337080b7f49SDag-Erling Smørgrav 
338080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
339080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
340080b7f49SDag-Erling Smørgrav 
341080b7f49SDag-Erling Smørgrav int	af;		/* address family */
342feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
3439b50d902SRodney W. Grimes 
3449b50d902SRodney W. Grimes int
345a01e3379SDavid Malone main(int argc, char *argv[])
3469b50d902SRodney W. Grimes {
347a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3489b50d902SRodney W. Grimes 	int ch;
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3519b50d902SRodney W. Grimes 
3526b463eedSChristian S.J. Peron 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) != -1)
3539b50d902SRodney W. Grimes 		switch(ch) {
3549b50d902SRodney W. Grimes 		case 'A':
3559b50d902SRodney W. Grimes 			Aflag = 1;
3569b50d902SRodney W. Grimes 			break;
3579b50d902SRodney W. Grimes 		case 'a':
3589b50d902SRodney W. Grimes 			aflag = 1;
3599b50d902SRodney W. Grimes 			break;
3606b463eedSChristian S.J. Peron 		case 'B':
3616b463eedSChristian S.J. Peron 			Bflag = 1;
3626b463eedSChristian S.J. Peron 			break;
363e1e293a5SDavid Greenman 		case 'b':
364e1e293a5SDavid Greenman 			bflag = 1;
365e1e293a5SDavid Greenman 			break;
3669b50d902SRodney W. Grimes 		case 'd':
3679b50d902SRodney W. Grimes 			dflag = 1;
3689b50d902SRodney W. Grimes 			break;
3699b50d902SRodney W. Grimes 		case 'f':
370cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
371cc6a66f2SJulian Elischer 				af = AF_IPX;
3729b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3739b50d902SRodney W. Grimes 				af = AF_INET;
374cfa1ca9dSYoshinobu Inoue #ifdef INET6
375cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
376cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
377e5134d2eSMax Laier #endif
378e5134d2eSMax Laier #ifdef IPSEC
3793b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3803b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
381e5134d2eSMax Laier #endif
3829b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3839b50d902SRodney W. Grimes 				af = AF_UNIX;
38463bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
38563bf4575SJulian Elischer 				af = AF_APPLETALK;
3864cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3874cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3884cf49a43SJulian Elischer 				af = AF_NETGRAPH;
389d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
390d44ddba9SRuslan Ermilov 				af = AF_LINK;
3919b50d902SRodney W. Grimes 			else {
39251e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3939b50d902SRodney W. Grimes 			}
3949b50d902SRodney W. Grimes 			break;
3959b50d902SRodney W. Grimes 		case 'g':
3969b50d902SRodney W. Grimes 			gflag = 1;
3979b50d902SRodney W. Grimes 			break;
398c2dfd19fSGleb Smirnoff 		case 'h':
399c2dfd19fSGleb Smirnoff 			hflag = 1;
400c2dfd19fSGleb Smirnoff 			break;
4019b50d902SRodney W. Grimes 		case 'I': {
4029b50d902SRodney W. Grimes 			char *cp;
4039b50d902SRodney W. Grimes 
4049b50d902SRodney W. Grimes 			iflag = 1;
4059b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4069b50d902SRodney W. Grimes 				continue;
4079b50d902SRodney W. Grimes 			unit = atoi(cp);
4089b50d902SRodney W. Grimes 			break;
4099b50d902SRodney W. Grimes 		}
4109b50d902SRodney W. Grimes 		case 'i':
4119b50d902SRodney W. Grimes 			iflag = 1;
4129b50d902SRodney W. Grimes 			break;
413ac55add0SGuido van Rooij 		case 'L':
414ac55add0SGuido van Rooij 			Lflag = 1;
415ac55add0SGuido van Rooij 			break;
4169b50d902SRodney W. Grimes 		case 'M':
4179b50d902SRodney W. Grimes 			memf = optarg;
4189b50d902SRodney W. Grimes 			break;
4199b50d902SRodney W. Grimes 		case 'm':
4209b50d902SRodney W. Grimes 			mflag = 1;
4219b50d902SRodney W. Grimes 			break;
4229b50d902SRodney W. Grimes 		case 'N':
4239b50d902SRodney W. Grimes 			nlistf = optarg;
4249b50d902SRodney W. Grimes 			break;
4259b50d902SRodney W. Grimes 		case 'n':
42665ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4279b50d902SRodney W. Grimes 			break;
4289b50d902SRodney W. Grimes 		case 'p':
4299b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
43051e7d42cSGarrett Wollman 				errx(1,
43151e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
43251e7d42cSGarrett Wollman 				     optarg);
4339b50d902SRodney W. Grimes 			}
4349b50d902SRodney W. Grimes 			pflag = 1;
4359b50d902SRodney W. Grimes 			break;
4369b50d902SRodney W. Grimes 		case 'r':
4379b50d902SRodney W. Grimes 			rflag = 1;
4389b50d902SRodney W. Grimes 			break;
4399b50d902SRodney W. Grimes 		case 's':
4409b50d902SRodney W. Grimes 			++sflag;
4419b50d902SRodney W. Grimes 			break;
44265ea0024SAssar Westerlund 		case 'S':
44365ea0024SAssar Westerlund 			numeric_addr = 1;
44465ea0024SAssar Westerlund 			break;
4459b50d902SRodney W. Grimes 		case 't':
4469b50d902SRodney W. Grimes 			tflag = 1;
4479b50d902SRodney W. Grimes 			break;
4489b50d902SRodney W. Grimes 		case 'u':
4499b50d902SRodney W. Grimes 			af = AF_UNIX;
4509b50d902SRodney W. Grimes 			break;
451080b7f49SDag-Erling Smørgrav 		case 'W':
45270057abfSRuslan Ermilov 		case 'l':
453080b7f49SDag-Erling Smørgrav 			Wflag = 1;
454080b7f49SDag-Erling Smørgrav 			break;
4559b50d902SRodney W. Grimes 		case 'w':
4569b50d902SRodney W. Grimes 			interval = atoi(optarg);
4579b50d902SRodney W. Grimes 			iflag = 1;
4589b50d902SRodney W. Grimes 			break;
459c73d99b5SRuslan Ermilov 		case 'z':
460c73d99b5SRuslan Ermilov 			zflag = 1;
461c73d99b5SRuslan Ermilov 			break;
4629b50d902SRodney W. Grimes 		case '?':
4639b50d902SRodney W. Grimes 		default:
4649b50d902SRodney W. Grimes 			usage();
4659b50d902SRodney W. Grimes 		}
4669b50d902SRodney W. Grimes 	argv += optind;
4679b50d902SRodney W. Grimes 	argc -= optind;
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4709b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4719b50d902SRodney W. Grimes 	if (*argv) {
4729b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4739b50d902SRodney W. Grimes 			interval = atoi(*argv);
4749b50d902SRodney W. Grimes 			if (interval <= 0)
4759b50d902SRodney W. Grimes 				usage();
4769b50d902SRodney W. Grimes 			++argv;
4779b50d902SRodney W. Grimes 			iflag = 1;
4789b50d902SRodney W. Grimes 		}
4799b50d902SRodney W. Grimes 		if (*argv) {
4809b50d902SRodney W. Grimes 			nlistf = *argv;
4819b50d902SRodney W. Grimes 			if (*++argv)
4829b50d902SRodney W. Grimes 				memf = *argv;
4839b50d902SRodney W. Grimes 		}
4849b50d902SRodney W. Grimes 	}
4859b50d902SRodney W. Grimes #endif
4869b50d902SRodney W. Grimes 
4879b50d902SRodney W. Grimes 	/*
4889b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4899b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4909b50d902SRodney W. Grimes 	 */
491feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
492feda1a43SJohn Baldwin 	if (!live)
4939b50d902SRodney W. Grimes 		setgid(getgid());
4949b50d902SRodney W. Grimes 
4956b463eedSChristian S.J. Peron 	if (Bflag) {
496feda1a43SJohn Baldwin 		if (!live)
497feda1a43SJohn Baldwin 			usage();
4986b463eedSChristian S.J. Peron 		bpf_stats(interface);
4996b463eedSChristian S.J. Peron 		exit(0);
5006b463eedSChristian S.J. Peron 	}
5019b50d902SRodney W. Grimes 	if (mflag) {
502d15c5f56SRuslan Ermilov 		if (memf != NULL) {
503feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
504d4426f28SRobert Watson 				mbpr(kvmd, nl[N_MBSTAT].n_value);
505d15c5f56SRuslan Ermilov 		} else
506d4426f28SRobert Watson 			mbpr(NULL, 0);
5079b50d902SRodney W. Grimes 		exit(0);
5089b50d902SRodney W. Grimes 	}
509cc63cd56SPeter Wemm #if 0
5109b50d902SRodney W. Grimes 	/*
5119b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5129b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5139b50d902SRodney W. Grimes 	 */
5149b50d902SRodney W. Grimes 	sethostent(1);
5159b50d902SRodney W. Grimes 	setnetent(1);
516cc63cd56SPeter Wemm #else
517cc63cd56SPeter Wemm 	/*
518cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
519cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
520cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
521cc63cd56SPeter Wemm 	 */
522cc63cd56SPeter Wemm #endif
523feda1a43SJohn Baldwin 	kread(0, NULL, 0);
524cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
525cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5269b50d902SRodney W. Grimes 		exit(0);
5279b50d902SRodney W. Grimes 	}
5289b50d902SRodney W. Grimes 	if (rflag) {
5299b50d902SRodney W. Grimes 		if (sflag)
5301093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
5319b50d902SRodney W. Grimes 		else
5329b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5339b50d902SRodney W. Grimes 		exit(0);
5349b50d902SRodney W. Grimes 	}
5359b50d902SRodney W. Grimes 	if (gflag) {
536cfa1ca9dSYoshinobu Inoue 		if (sflag) {
537cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
53867a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
539cfa1ca9dSYoshinobu Inoue #ifdef INET6
540cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
541cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
542cfa1ca9dSYoshinobu Inoue #endif
543cfa1ca9dSYoshinobu Inoue 		} else {
544cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54567a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5469b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
547cfa1ca9dSYoshinobu Inoue #ifdef INET6
548cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
549cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
550cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
551cfa1ca9dSYoshinobu Inoue #endif
552cfa1ca9dSYoshinobu Inoue 		}
5539fcc066dSBruce M Simpson 		ifmalist_dump();
5549b50d902SRodney W. Grimes 		exit(0);
5559b50d902SRodney W. Grimes 	}
556cfa1ca9dSYoshinobu Inoue 
557cf5e44f8SRuslan Ermilov 	if (tp) {
558cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
559cf5e44f8SRuslan Ermilov 		exit(0);
560cf5e44f8SRuslan Ermilov 	}
561cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5629b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
563cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
564cfa1ca9dSYoshinobu Inoue #ifdef INET6
565cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
566cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
567cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
568cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5693b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5703b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5713b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5723b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5733b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
5749cc22e5cSPoul-Henning Kamp #ifdef IPX
575e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
576cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
577cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
578e30e913cSJohn Hay 	}
5799cc22e5cSPoul-Henning Kamp #endif /* IPX */
58063bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
58163bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
58263bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5834cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5844cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5854cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
586ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
587feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
588feda1a43SJohn Baldwin 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
5899b50d902SRodney W. Grimes 	exit(0);
5909b50d902SRodney W. Grimes }
5919b50d902SRodney W. Grimes 
5929b50d902SRodney W. Grimes /*
5939b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
5949b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
5959b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
5969b50d902SRodney W. Grimes  */
5979b50d902SRodney W. Grimes static void
5989b50d902SRodney W. Grimes printproto(tp, name)
599a01e3379SDavid Malone 	struct protox *tp;
600fa6d48c0SMark Murray 	const char *name;
6019b50d902SRodney W. Grimes {
602feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6039b50d902SRodney W. Grimes 	u_long off;
6049b50d902SRodney W. Grimes 
6059b50d902SRodney W. Grimes 	if (sflag) {
606cfa1ca9dSYoshinobu Inoue 		if (iflag) {
607cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
608cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
609cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
610cf5e44f8SRuslan Ermilov 			else if (pflag)
611cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
612cf5e44f8SRuslan Ermilov 				    tp->pr_name);
613cfa1ca9dSYoshinobu Inoue 			return;
614feda1a43SJohn Baldwin 		} else {
6159b50d902SRodney W. Grimes 			pr = tp->pr_stats;
616cf5e44f8SRuslan Ermilov 			if (!pr) {
617cf5e44f8SRuslan Ermilov 				if (pflag)
618cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
619cf5e44f8SRuslan Ermilov 					    tp->pr_name);
620cf5e44f8SRuslan Ermilov 				return;
621cf5e44f8SRuslan Ermilov 			}
622feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
623feda1a43SJohn Baldwin 				off = 0;
624feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
625feda1a43SJohn Baldwin 				if (pflag)
626feda1a43SJohn Baldwin 					printf(
627feda1a43SJohn Baldwin 				    "%s: stats routine doesn't work on cores\n",
628feda1a43SJohn Baldwin 					    tp->pr_name);
629feda1a43SJohn Baldwin 				return;
630feda1a43SJohn Baldwin 			} else
631feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
632cfa1ca9dSYoshinobu Inoue 		}
6339b50d902SRodney W. Grimes 	} else {
6349b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
635cf5e44f8SRuslan Ermilov 		if (!pr) {
636cf5e44f8SRuslan Ermilov 			if (pflag)
637cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
638cf5e44f8SRuslan Ermilov 			return;
639cf5e44f8SRuslan Ermilov 		}
640feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
641feda1a43SJohn Baldwin 			off = 0;
642feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
643feda1a43SJohn Baldwin 			if (pflag)
644feda1a43SJohn Baldwin 				printf(
645feda1a43SJohn Baldwin 				    "%s: PCB routine doesn't work on cores\n",
646feda1a43SJohn Baldwin 				    tp->pr_name);
647feda1a43SJohn Baldwin 			return;
648feda1a43SJohn Baldwin 		} else
649feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6509b50d902SRodney W. Grimes 	}
651feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
652feda1a43SJohn Baldwin 	    af != AF_UNSPEC))
653feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
6549b50d902SRodney W. Grimes }
6559b50d902SRodney W. Grimes 
6569b50d902SRodney W. Grimes /*
6579b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6589b50d902SRodney W. Grimes  */
6599b50d902SRodney W. Grimes int
660feda1a43SJohn Baldwin kread(u_long addr, void *buf, size_t size)
6619b50d902SRodney W. Grimes {
662feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
663feda1a43SJohn Baldwin 
664feda1a43SJohn Baldwin 	if (kvmd == NULL) {
665feda1a43SJohn Baldwin 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
6663b7e5cccSRuslan Ermilov 		setgid(getgid());
66799453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
66899453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
66999453c6aSPoul-Henning Kamp 				if (nlistf)
67099453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
67199453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
67299453c6aSPoul-Henning Kamp 				else
67399453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
67499453c6aSPoul-Henning Kamp 			}
67599453c6aSPoul-Henning Kamp 
67699453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
67799453c6aSPoul-Henning Kamp 				if (nlistf)
67899453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
67999453c6aSPoul-Henning Kamp 				else
68099453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
68199453c6aSPoul-Henning Kamp 			}
68299453c6aSPoul-Henning Kamp 		} else {
683feda1a43SJohn Baldwin 			warnx("kvm not available: %s", errbuf);
68499453c6aSPoul-Henning Kamp 			return(-1);
68599453c6aSPoul-Henning Kamp 		}
6864f81ef50SGarrett Wollman 	}
687c6620a13SPoul-Henning Kamp 	if (!buf)
688c6620a13SPoul-Henning Kamp 		return (0);
689feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
6903aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6919b50d902SRodney W. Grimes 		return (-1);
6929b50d902SRodney W. Grimes 	}
6939b50d902SRodney W. Grimes 	return (0);
6949b50d902SRodney W. Grimes }
6959b50d902SRodney W. Grimes 
696a01e3379SDavid Malone const char *
6977b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
6989b50d902SRodney W. Grimes {
6999b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7009b50d902SRodney W. Grimes }
7019b50d902SRodney W. Grimes 
702a01e3379SDavid Malone const char *
7037b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
7049b50d902SRodney W. Grimes {
7059b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7069b50d902SRodney W. Grimes }
7079b50d902SRodney W. Grimes 
708f99a4046SMike Makonnen const char *
7097b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
710f99a4046SMike Makonnen {
711f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
712f99a4046SMike Makonnen }
713f99a4046SMike Makonnen 
7149b50d902SRodney W. Grimes /*
7159b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7169b50d902SRodney W. Grimes  */
7179b50d902SRodney W. Grimes static struct protox *
718096146f8SYaroslav Tykhiy knownname(const char *name)
7199b50d902SRodney W. Grimes {
7209b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7219b50d902SRodney W. Grimes 
7229b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7239b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7249b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7259b50d902SRodney W. Grimes 				return (tp);
7269b50d902SRodney W. Grimes 	return (NULL);
7279b50d902SRodney W. Grimes }
7289b50d902SRodney W. Grimes 
7299b50d902SRodney W. Grimes /*
7309b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7319b50d902SRodney W. Grimes  */
7329b50d902SRodney W. Grimes static struct protox *
733096146f8SYaroslav Tykhiy name2protox(const char *name)
7349b50d902SRodney W. Grimes {
7359b50d902SRodney W. Grimes 	struct protox *tp;
7369b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7379b50d902SRodney W. Grimes 	struct protoent *p;
7389b50d902SRodney W. Grimes 
7399b50d902SRodney W. Grimes 	/*
7409b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7419b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7429b50d902SRodney W. Grimes 	 */
743cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7449b50d902SRodney W. Grimes 		return (tp);
7459b50d902SRodney W. Grimes 
7469b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
747cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7489b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7499b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7509b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7519b50d902SRodney W. Grimes 				endprotoent();
7529b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7539b50d902SRodney W. Grimes 			}
7549b50d902SRodney W. Grimes 	}
7559b50d902SRodney W. Grimes 	endprotoent();
7569b50d902SRodney W. Grimes 	return (NULL);
7579b50d902SRodney W. Grimes }
7589b50d902SRodney W. Grimes 
7599b50d902SRodney W. Grimes static void
7605e051718SAssar Westerlund usage(void)
7619b50d902SRodney W. Grimes {
7625dbd2da1SChristian 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",
7631cb467b1SRuslan Ermilov "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
7641cb467b1SRuslan Ermilov "               [-M core] [-N system]",
765c2dfd19fSGleb Smirnoff "       netstat -i | -I interface [-abdhnt] [-f address_family]\n"
766d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7671cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
7681cb467b1SRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
7691cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7701cb467b1SRuslan Ermilov "               [-M core] [-N system]",
77149c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
7725dbd2da1SChristian S.J. Peron "       netstat -B [ -I interface]",
773dbe0253aSAlfred Perlstein "       netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
7741cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7751cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7761cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7779b50d902SRodney W. Grimes 	exit(1);
7789b50d902SRodney W. Grimes }
779