xref: /freebsd/usr.bin/netstat/main.c (revision 49f287f8c58cd72f62ae5047baf110ec91b6acf7)
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 
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 */
33449f287f8SGeorge V. Neville-Neil int     xflag;          /* extra information, includes all socket buffer info */
335c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
336080b7f49SDag-Erling Smørgrav 
337080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
338080b7f49SDag-Erling Smørgrav 
339080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
340080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
341080b7f49SDag-Erling Smørgrav 
342080b7f49SDag-Erling Smørgrav int	af;		/* address family */
343feda1a43SJohn Baldwin int	live;		/* true if we are examining a live system */
3449b50d902SRodney W. Grimes 
3459b50d902SRodney W. Grimes int
346a01e3379SDavid Malone main(int argc, char *argv[])
3479b50d902SRodney W. Grimes {
348a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3499b50d902SRodney W. Grimes 	int ch;
3509b50d902SRodney W. Grimes 
3519b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3529b50d902SRodney W. Grimes 
35349f287f8SGeorge V. Neville-Neil 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:xz")) != -1)
3549b50d902SRodney W. Grimes 		switch(ch) {
3559b50d902SRodney W. Grimes 		case 'A':
3569b50d902SRodney W. Grimes 			Aflag = 1;
3579b50d902SRodney W. Grimes 			break;
3589b50d902SRodney W. Grimes 		case 'a':
3599b50d902SRodney W. Grimes 			aflag = 1;
3609b50d902SRodney W. Grimes 			break;
3616b463eedSChristian S.J. Peron 		case 'B':
3626b463eedSChristian S.J. Peron 			Bflag = 1;
3636b463eedSChristian S.J. Peron 			break;
364e1e293a5SDavid Greenman 		case 'b':
365e1e293a5SDavid Greenman 			bflag = 1;
366e1e293a5SDavid Greenman 			break;
3679b50d902SRodney W. Grimes 		case 'd':
3689b50d902SRodney W. Grimes 			dflag = 1;
3699b50d902SRodney W. Grimes 			break;
3709b50d902SRodney W. Grimes 		case 'f':
371cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
372cc6a66f2SJulian Elischer 				af = AF_IPX;
3739b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3749b50d902SRodney W. Grimes 				af = AF_INET;
375cfa1ca9dSYoshinobu Inoue #ifdef INET6
376cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
377cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
378e5134d2eSMax Laier #endif
379e5134d2eSMax Laier #ifdef IPSEC
3803b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3813b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
382e5134d2eSMax Laier #endif
3839b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3849b50d902SRodney W. Grimes 				af = AF_UNIX;
38563bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
38663bf4575SJulian Elischer 				af = AF_APPLETALK;
3874cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3884cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3894cf49a43SJulian Elischer 				af = AF_NETGRAPH;
390d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
391d44ddba9SRuslan Ermilov 				af = AF_LINK;
3929b50d902SRodney W. Grimes 			else {
39351e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3949b50d902SRodney W. Grimes 			}
3959b50d902SRodney W. Grimes 			break;
3969b50d902SRodney W. Grimes 		case 'g':
3979b50d902SRodney W. Grimes 			gflag = 1;
3989b50d902SRodney W. Grimes 			break;
399c2dfd19fSGleb Smirnoff 		case 'h':
400c2dfd19fSGleb Smirnoff 			hflag = 1;
401c2dfd19fSGleb Smirnoff 			break;
4029b50d902SRodney W. Grimes 		case 'I': {
4039b50d902SRodney W. Grimes 			char *cp;
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 			iflag = 1;
4069b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4079b50d902SRodney W. Grimes 				continue;
4089b50d902SRodney W. Grimes 			unit = atoi(cp);
4099b50d902SRodney W. Grimes 			break;
4109b50d902SRodney W. Grimes 		}
4119b50d902SRodney W. Grimes 		case 'i':
4129b50d902SRodney W. Grimes 			iflag = 1;
4139b50d902SRodney W. Grimes 			break;
414ac55add0SGuido van Rooij 		case 'L':
415ac55add0SGuido van Rooij 			Lflag = 1;
416ac55add0SGuido van Rooij 			break;
4179b50d902SRodney W. Grimes 		case 'M':
4189b50d902SRodney W. Grimes 			memf = optarg;
4199b50d902SRodney W. Grimes 			break;
4209b50d902SRodney W. Grimes 		case 'm':
4219b50d902SRodney W. Grimes 			mflag = 1;
4229b50d902SRodney W. Grimes 			break;
4239b50d902SRodney W. Grimes 		case 'N':
4249b50d902SRodney W. Grimes 			nlistf = optarg;
4259b50d902SRodney W. Grimes 			break;
4269b50d902SRodney W. Grimes 		case 'n':
42765ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4289b50d902SRodney W. Grimes 			break;
4299b50d902SRodney W. Grimes 		case 'p':
4309b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
43151e7d42cSGarrett Wollman 				errx(1,
43251e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
43351e7d42cSGarrett Wollman 				     optarg);
4349b50d902SRodney W. Grimes 			}
4359b50d902SRodney W. Grimes 			pflag = 1;
4369b50d902SRodney W. Grimes 			break;
4379b50d902SRodney W. Grimes 		case 'r':
4389b50d902SRodney W. Grimes 			rflag = 1;
4399b50d902SRodney W. Grimes 			break;
4409b50d902SRodney W. Grimes 		case 's':
4419b50d902SRodney W. Grimes 			++sflag;
4429b50d902SRodney W. Grimes 			break;
44365ea0024SAssar Westerlund 		case 'S':
44465ea0024SAssar Westerlund 			numeric_addr = 1;
44565ea0024SAssar Westerlund 			break;
4469b50d902SRodney W. Grimes 		case 't':
4479b50d902SRodney W. Grimes 			tflag = 1;
4489b50d902SRodney W. Grimes 			break;
4499b50d902SRodney W. Grimes 		case 'u':
4509b50d902SRodney W. Grimes 			af = AF_UNIX;
4519b50d902SRodney W. Grimes 			break;
452080b7f49SDag-Erling Smørgrav 		case 'W':
45370057abfSRuslan Ermilov 		case 'l':
454080b7f49SDag-Erling Smørgrav 			Wflag = 1;
455080b7f49SDag-Erling Smørgrav 			break;
4569b50d902SRodney W. Grimes 		case 'w':
4579b50d902SRodney W. Grimes 			interval = atoi(optarg);
4589b50d902SRodney W. Grimes 			iflag = 1;
4599b50d902SRodney W. Grimes 			break;
46049f287f8SGeorge V. Neville-Neil 		case 'x':
46149f287f8SGeorge V. Neville-Neil 			xflag = 1;
46249f287f8SGeorge V. Neville-Neil 			break;
463c73d99b5SRuslan Ermilov 		case 'z':
464c73d99b5SRuslan Ermilov 			zflag = 1;
465c73d99b5SRuslan Ermilov 			break;
4669b50d902SRodney W. Grimes 		case '?':
4679b50d902SRodney W. Grimes 		default:
4689b50d902SRodney W. Grimes 			usage();
4699b50d902SRodney W. Grimes 		}
4709b50d902SRodney W. Grimes 	argv += optind;
4719b50d902SRodney W. Grimes 	argc -= optind;
4729b50d902SRodney W. Grimes 
4739b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4749b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4759b50d902SRodney W. Grimes 	if (*argv) {
4769b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4779b50d902SRodney W. Grimes 			interval = atoi(*argv);
4789b50d902SRodney W. Grimes 			if (interval <= 0)
4799b50d902SRodney W. Grimes 				usage();
4809b50d902SRodney W. Grimes 			++argv;
4819b50d902SRodney W. Grimes 			iflag = 1;
4829b50d902SRodney W. Grimes 		}
4839b50d902SRodney W. Grimes 		if (*argv) {
4849b50d902SRodney W. Grimes 			nlistf = *argv;
4859b50d902SRodney W. Grimes 			if (*++argv)
4869b50d902SRodney W. Grimes 				memf = *argv;
4879b50d902SRodney W. Grimes 		}
4889b50d902SRodney W. Grimes 	}
4899b50d902SRodney W. Grimes #endif
4909b50d902SRodney W. Grimes 
4919b50d902SRodney W. Grimes 	/*
4929b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4939b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4949b50d902SRodney W. Grimes 	 */
495feda1a43SJohn Baldwin 	live = (nlistf == NULL && memf == NULL);
496feda1a43SJohn Baldwin 	if (!live)
4979b50d902SRodney W. Grimes 		setgid(getgid());
4989b50d902SRodney W. Grimes 
4996b463eedSChristian S.J. Peron 	if (Bflag) {
500feda1a43SJohn Baldwin 		if (!live)
501feda1a43SJohn Baldwin 			usage();
5026b463eedSChristian S.J. Peron 		bpf_stats(interface);
5036b463eedSChristian S.J. Peron 		exit(0);
5046b463eedSChristian S.J. Peron 	}
5059b50d902SRodney W. Grimes 	if (mflag) {
506d15c5f56SRuslan Ermilov 		if (memf != NULL) {
507feda1a43SJohn Baldwin 			if (kread(0, NULL, 0) == 0)
508d4426f28SRobert Watson 				mbpr(kvmd, nl[N_MBSTAT].n_value);
509d15c5f56SRuslan Ermilov 		} else
510d4426f28SRobert Watson 			mbpr(NULL, 0);
5119b50d902SRodney W. Grimes 		exit(0);
5129b50d902SRodney W. Grimes 	}
513cc63cd56SPeter Wemm #if 0
5149b50d902SRodney W. Grimes 	/*
5159b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5169b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5179b50d902SRodney W. Grimes 	 */
5189b50d902SRodney W. Grimes 	sethostent(1);
5199b50d902SRodney W. Grimes 	setnetent(1);
520cc63cd56SPeter Wemm #else
521cc63cd56SPeter Wemm 	/*
522cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
523cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
524cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
525cc63cd56SPeter Wemm 	 */
526cc63cd56SPeter Wemm #endif
527feda1a43SJohn Baldwin 	kread(0, NULL, 0);
528cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
529cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5309b50d902SRodney W. Grimes 		exit(0);
5319b50d902SRodney W. Grimes 	}
5329b50d902SRodney W. Grimes 	if (rflag) {
5339b50d902SRodney W. Grimes 		if (sflag)
5341093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
5359b50d902SRodney W. Grimes 		else
5369b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5379b50d902SRodney W. Grimes 		exit(0);
5389b50d902SRodney W. Grimes 	}
5399b50d902SRodney W. Grimes 	if (gflag) {
540cfa1ca9dSYoshinobu Inoue 		if (sflag) {
541cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54267a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
543cfa1ca9dSYoshinobu Inoue #ifdef INET6
544cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
545cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
546cfa1ca9dSYoshinobu Inoue #endif
547cfa1ca9dSYoshinobu Inoue 		} else {
548cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54967a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5509b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
551cfa1ca9dSYoshinobu Inoue #ifdef INET6
552cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
553cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
554cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
555cfa1ca9dSYoshinobu Inoue #endif
556cfa1ca9dSYoshinobu Inoue 		}
5579fcc066dSBruce M Simpson 		ifmalist_dump();
5589b50d902SRodney W. Grimes 		exit(0);
5599b50d902SRodney W. Grimes 	}
560cfa1ca9dSYoshinobu Inoue 
561cf5e44f8SRuslan Ermilov 	if (tp) {
562cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
563cf5e44f8SRuslan Ermilov 		exit(0);
564cf5e44f8SRuslan Ermilov 	}
565cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5669b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
567cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
568cfa1ca9dSYoshinobu Inoue #ifdef INET6
569cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
570cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
571cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
572cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5733b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5743b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5753b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5763b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5773b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
5789cc22e5cSPoul-Henning Kamp #ifdef IPX
579e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
580cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
581cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
582e30e913cSJohn Hay 	}
5839cc22e5cSPoul-Henning Kamp #endif /* IPX */
58463bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
58563bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
58663bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5874cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5884cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5894cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
590ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
591feda1a43SJohn Baldwin 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
592feda1a43SJohn Baldwin 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
5939b50d902SRodney W. Grimes 	exit(0);
5949b50d902SRodney W. Grimes }
5959b50d902SRodney W. Grimes 
5969b50d902SRodney W. Grimes /*
5979b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
5989b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
5999b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6009b50d902SRodney W. Grimes  */
6019b50d902SRodney W. Grimes static void
6029b50d902SRodney W. Grimes printproto(tp, name)
603a01e3379SDavid Malone 	struct protox *tp;
604fa6d48c0SMark Murray 	const char *name;
6059b50d902SRodney W. Grimes {
606feda1a43SJohn Baldwin 	void (*pr)(u_long, const char *, int, int);
6079b50d902SRodney W. Grimes 	u_long off;
6089b50d902SRodney W. Grimes 
6099b50d902SRodney W. Grimes 	if (sflag) {
610cfa1ca9dSYoshinobu Inoue 		if (iflag) {
611cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
612cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
613cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
614cf5e44f8SRuslan Ermilov 			else if (pflag)
615cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
616cf5e44f8SRuslan Ermilov 				    tp->pr_name);
617cfa1ca9dSYoshinobu Inoue 			return;
618feda1a43SJohn Baldwin 		} else {
6199b50d902SRodney W. Grimes 			pr = tp->pr_stats;
620cf5e44f8SRuslan Ermilov 			if (!pr) {
621cf5e44f8SRuslan Ermilov 				if (pflag)
622cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
623cf5e44f8SRuslan Ermilov 					    tp->pr_name);
624cf5e44f8SRuslan Ermilov 				return;
625cf5e44f8SRuslan Ermilov 			}
626feda1a43SJohn Baldwin 			if (tp->pr_usesysctl && live)
627feda1a43SJohn Baldwin 				off = 0;
628feda1a43SJohn Baldwin 			else if (tp->pr_sindex < 0) {
629feda1a43SJohn Baldwin 				if (pflag)
630feda1a43SJohn Baldwin 					printf(
631feda1a43SJohn Baldwin 				    "%s: stats routine doesn't work on cores\n",
632feda1a43SJohn Baldwin 					    tp->pr_name);
633feda1a43SJohn Baldwin 				return;
634feda1a43SJohn Baldwin 			} else
635feda1a43SJohn Baldwin 				off = nl[tp->pr_sindex].n_value;
636cfa1ca9dSYoshinobu Inoue 		}
6379b50d902SRodney W. Grimes 	} else {
6389b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
639cf5e44f8SRuslan Ermilov 		if (!pr) {
640cf5e44f8SRuslan Ermilov 			if (pflag)
641cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
642cf5e44f8SRuslan Ermilov 			return;
643cf5e44f8SRuslan Ermilov 		}
644feda1a43SJohn Baldwin 		if (tp->pr_usesysctl && live)
645feda1a43SJohn Baldwin 			off = 0;
646feda1a43SJohn Baldwin 		else if (tp->pr_index < 0) {
647feda1a43SJohn Baldwin 			if (pflag)
648feda1a43SJohn Baldwin 				printf(
649feda1a43SJohn Baldwin 				    "%s: PCB routine doesn't work on cores\n",
650feda1a43SJohn Baldwin 				    tp->pr_name);
651feda1a43SJohn Baldwin 			return;
652feda1a43SJohn Baldwin 		} else
653feda1a43SJohn Baldwin 			off = nl[tp->pr_index].n_value;
6549b50d902SRodney W. Grimes 	}
655feda1a43SJohn Baldwin 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
656feda1a43SJohn Baldwin 	    af != AF_UNSPEC))
657feda1a43SJohn Baldwin 		(*pr)(off, name, af, tp->pr_protocol);
6589b50d902SRodney W. Grimes }
6599b50d902SRodney W. Grimes 
6609b50d902SRodney W. Grimes /*
6619b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6629b50d902SRodney W. Grimes  */
6639b50d902SRodney W. Grimes int
664feda1a43SJohn Baldwin kread(u_long addr, void *buf, size_t size)
6659b50d902SRodney W. Grimes {
666feda1a43SJohn Baldwin 	char errbuf[_POSIX2_LINE_MAX];
667feda1a43SJohn Baldwin 
668feda1a43SJohn Baldwin 	if (kvmd == NULL) {
669feda1a43SJohn Baldwin 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
6703b7e5cccSRuslan Ermilov 		setgid(getgid());
67199453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
67299453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
67399453c6aSPoul-Henning Kamp 				if (nlistf)
67499453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
67599453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
67699453c6aSPoul-Henning Kamp 				else
67799453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
67899453c6aSPoul-Henning Kamp 			}
67999453c6aSPoul-Henning Kamp 
68099453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
68199453c6aSPoul-Henning Kamp 				if (nlistf)
68299453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
68399453c6aSPoul-Henning Kamp 				else
68499453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
68599453c6aSPoul-Henning Kamp 			}
68699453c6aSPoul-Henning Kamp 		} else {
687feda1a43SJohn Baldwin 			warnx("kvm not available: %s", errbuf);
68899453c6aSPoul-Henning Kamp 			return(-1);
68999453c6aSPoul-Henning Kamp 		}
6904f81ef50SGarrett Wollman 	}
691c6620a13SPoul-Henning Kamp 	if (!buf)
692c6620a13SPoul-Henning Kamp 		return (0);
693feda1a43SJohn Baldwin 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
6943aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6959b50d902SRodney W. Grimes 		return (-1);
6969b50d902SRodney W. Grimes 	}
6979b50d902SRodney W. Grimes 	return (0);
6989b50d902SRodney W. Grimes }
6999b50d902SRodney W. Grimes 
700a01e3379SDavid Malone const char *
7017b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
7029b50d902SRodney W. Grimes {
7039b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes 
706a01e3379SDavid Malone const char *
7077b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
7089b50d902SRodney W. Grimes {
7099b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7109b50d902SRodney W. Grimes }
7119b50d902SRodney W. Grimes 
712f99a4046SMike Makonnen const char *
7137b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
714f99a4046SMike Makonnen {
715f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
716f99a4046SMike Makonnen }
717f99a4046SMike Makonnen 
7189b50d902SRodney W. Grimes /*
7199b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7209b50d902SRodney W. Grimes  */
7219b50d902SRodney W. Grimes static struct protox *
722096146f8SYaroslav Tykhiy knownname(const char *name)
7239b50d902SRodney W. Grimes {
7249b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7259b50d902SRodney W. Grimes 
7269b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7279b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7289b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7299b50d902SRodney W. Grimes 				return (tp);
7309b50d902SRodney W. Grimes 	return (NULL);
7319b50d902SRodney W. Grimes }
7329b50d902SRodney W. Grimes 
7339b50d902SRodney W. Grimes /*
7349b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7359b50d902SRodney W. Grimes  */
7369b50d902SRodney W. Grimes static struct protox *
737096146f8SYaroslav Tykhiy name2protox(const char *name)
7389b50d902SRodney W. Grimes {
7399b50d902SRodney W. Grimes 	struct protox *tp;
7409b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7419b50d902SRodney W. Grimes 	struct protoent *p;
7429b50d902SRodney W. Grimes 
7439b50d902SRodney W. Grimes 	/*
7449b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7459b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7469b50d902SRodney W. Grimes 	 */
747cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7489b50d902SRodney W. Grimes 		return (tp);
7499b50d902SRodney W. Grimes 
7509b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
751cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7529b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7539b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7549b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7559b50d902SRodney W. Grimes 				endprotoent();
7569b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7579b50d902SRodney W. Grimes 			}
7589b50d902SRodney W. Grimes 	}
7599b50d902SRodney W. Grimes 	endprotoent();
7609b50d902SRodney W. Grimes 	return (NULL);
7619b50d902SRodney W. Grimes }
7629b50d902SRodney W. Grimes 
7639b50d902SRodney W. Grimes static void
7645e051718SAssar Westerlund usage(void)
7659b50d902SRodney W. Grimes {
7665dbd2da1SChristian 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",
7671cb467b1SRuslan Ermilov "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
7681cb467b1SRuslan Ermilov "               [-M core] [-N system]",
769c2dfd19fSGleb Smirnoff "       netstat -i | -I interface [-abdhnt] [-f address_family]\n"
770d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7711cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
7721cb467b1SRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
7731cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7741cb467b1SRuslan Ermilov "               [-M core] [-N system]",
77549c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
7765dbd2da1SChristian S.J. Peron "       netstat -B [ -I interface]",
777dbe0253aSAlfred Perlstein "       netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
7781cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7791cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7801cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7819b50d902SRodney W. Grimes 	exit(1);
7829b50d902SRodney W. Grimes }
783