xref: /freebsd/usr.bin/netstat/main.c (revision f99a4046d83609160b00d895b2a6a91dfc57e9dd)
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 
409b50d902SRodney W. Grimes #ifndef lint
415d422d6aSPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
435d422d6aSPhilippe Charnier #endif
445d422d6aSPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/param.h>
499b50d902SRodney W. Grimes #include <sys/file.h>
509b50d902SRodney W. Grimes #include <sys/protosw.h>
519b50d902SRodney W. Grimes #include <sys/socket.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <netinet/in.h>
549b50d902SRodney W. Grimes 
554cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
564cf49a43SJulian Elischer 
579b50d902SRodney W. Grimes #include <ctype.h>
585d422d6aSPhilippe Charnier #include <err.h>
599b50d902SRodney W. Grimes #include <errno.h>
609b50d902SRodney W. Grimes #include <kvm.h>
619b50d902SRodney W. Grimes #include <limits.h>
629b50d902SRodney W. Grimes #include <netdb.h>
639b50d902SRodney W. Grimes #include <nlist.h>
649b50d902SRodney W. Grimes #include <paths.h>
659b50d902SRodney W. Grimes #include <stdio.h>
669b50d902SRodney W. Grimes #include <stdlib.h>
679b50d902SRodney W. Grimes #include <string.h>
689b50d902SRodney W. Grimes #include <unistd.h>
699b50d902SRodney W. Grimes #include "netstat.h"
709b50d902SRodney W. Grimes 
71c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
724f81ef50SGarrett Wollman #define	N_IFNET		0
739b50d902SRodney W. Grimes 	{ "_ifnet" },
744f81ef50SGarrett Wollman #define	N_IMP		1
759b50d902SRodney W. Grimes 	{ "_imp_softc" },
764f81ef50SGarrett Wollman #define	N_RTSTAT	2
779b50d902SRodney W. Grimes 	{ "_rtstat" },
784f81ef50SGarrett Wollman #define	N_UNIXSW	3
79f3117d66SPeter Wemm 	{ "_localsw" },
804f81ef50SGarrett Wollman #define N_IDP		4
819b50d902SRodney W. Grimes 	{ "_nspcb"},
824f81ef50SGarrett Wollman #define N_IDPSTAT	5
839b50d902SRodney W. Grimes 	{ "_idpstat"},
844f81ef50SGarrett Wollman #define N_SPPSTAT	6
859b50d902SRodney W. Grimes 	{ "_spp_istat"},
864f81ef50SGarrett Wollman #define N_NSERR		7
879b50d902SRodney W. Grimes 	{ "_ns_errstat"},
884f81ef50SGarrett Wollman #define	N_CLNPSTAT	8
899b50d902SRodney W. Grimes 	{ "_clnp_stat"},
904f81ef50SGarrett Wollman #define	IN_NOTUSED	9
919b50d902SRodney W. Grimes 	{ "_tp_inpcb" },
924f81ef50SGarrett Wollman #define	ISO_TP		10
939b50d902SRodney W. Grimes 	{ "_tp_refinfo" },
944f81ef50SGarrett Wollman #define	N_TPSTAT	11
959b50d902SRodney W. Grimes 	{ "_tp_stat" },
964f81ef50SGarrett Wollman #define	N_ESISSTAT	12
979b50d902SRodney W. Grimes 	{ "_esis_stat"},
984f81ef50SGarrett Wollman #define N_NIMP		13
999b50d902SRodney W. Grimes 	{ "_nimp"},
1004f81ef50SGarrett Wollman #define N_RTREE		14
1019b50d902SRodney W. Grimes 	{ "_rt_tables"},
1024f81ef50SGarrett Wollman #define N_CLTP		15
1039b50d902SRodney W. Grimes 	{ "_cltb"},
1044f81ef50SGarrett Wollman #define N_CLTPSTAT	16
1059b50d902SRodney W. Grimes 	{ "_cltpstat"},
1064f81ef50SGarrett Wollman #define	N_NFILE		17
1079b50d902SRodney W. Grimes 	{ "_nfile" },
1084f81ef50SGarrett Wollman #define	N_FILE		18
1099b50d902SRodney W. Grimes 	{ "_file" },
11067a451ccSBill Fenner #define N_MRTSTAT	19
1119b50d902SRodney W. Grimes 	{ "_mrtstat" },
11267a451ccSBill Fenner #define N_MFCTABLE	20
113ef105c25SGarrett Wollman 	{ "_mfctable" },
11467a451ccSBill Fenner #define N_VIFTABLE	21
1159b50d902SRodney W. Grimes 	{ "_viftable" },
11667a451ccSBill Fenner #define N_IPX		22
117cc6a66f2SJulian Elischer 	{ "_ipxpcb"},
11867a451ccSBill Fenner #define N_IPXSTAT	23
119cc6a66f2SJulian Elischer 	{ "_ipxstat"},
12067a451ccSBill Fenner #define N_SPXSTAT	24
121cc6a66f2SJulian Elischer 	{ "_spx_istat"},
12267a451ccSBill Fenner #define N_DDPSTAT	25
12363bf4575SJulian Elischer 	{ "_ddpstat"},
12467a451ccSBill Fenner #define N_DDPCB		26
12563bf4575SJulian Elischer 	{ "_ddpcb"},
1264cf49a43SJulian Elischer #define N_NGSOCKS	27
1274cf49a43SJulian Elischer 	{ "_ngsocklist"},
128cfa1ca9dSYoshinobu Inoue #define N_IP6STAT	28
129cfa1ca9dSYoshinobu Inoue 	{ "_ip6stat" },
130cfa1ca9dSYoshinobu Inoue #define N_ICMP6STAT	29
131cfa1ca9dSYoshinobu Inoue 	{ "_icmp6stat" },
132cfa1ca9dSYoshinobu Inoue #define N_IPSECSTAT	30
133cfa1ca9dSYoshinobu Inoue 	{ "_ipsecstat" },
134cfa1ca9dSYoshinobu Inoue #define N_IPSEC6STAT	31
135cfa1ca9dSYoshinobu Inoue 	{ "_ipsec6stat" },
136cfa1ca9dSYoshinobu Inoue #define N_PIM6STAT	32
137cfa1ca9dSYoshinobu Inoue 	{ "_pim6stat" },
138cfa1ca9dSYoshinobu Inoue #define N_MRT6PROTO	33
139cfa1ca9dSYoshinobu Inoue 	{ "_ip6_mrtproto" },
140cfa1ca9dSYoshinobu Inoue #define N_MRT6STAT	34
141cfa1ca9dSYoshinobu Inoue 	{ "_mrt6stat" },
142cfa1ca9dSYoshinobu Inoue #define N_MF6CTABLE	35
143cfa1ca9dSYoshinobu Inoue 	{ "_mf6ctable" },
144cfa1ca9dSYoshinobu Inoue #define N_MIF6TABLE	36
145cfa1ca9dSYoshinobu Inoue 	{ "_mif6table" },
1463b8a8567SJun-ichiro itojun Hagino #define N_PFKEYSTAT	37
1473b8a8567SJun-ichiro itojun Hagino 	{ "_pfkeystat" },
148d15c5f56SRuslan Ermilov #define N_MBSTAT	38
149d15c5f56SRuslan Ermilov 	{ "_mbstat" },
150d15c5f56SRuslan Ermilov #define N_MBTYPES	39
151d15c5f56SRuslan Ermilov 	{ "_mbtypes" },
152d15c5f56SRuslan Ermilov #define N_NMBCLUSTERS	40
153d15c5f56SRuslan Ermilov 	{ "_nmbclusters" },
154d15c5f56SRuslan Ermilov #define N_NMBUFS	41
155d15c5f56SRuslan Ermilov 	{ "_nmbufs" },
15608442f8aSBosko Milekic #define	N_MBLIM		42
15708442f8aSBosko Milekic 	{ "_mbuf_limit" },
15808442f8aSBosko Milekic #define	N_CLLIM		43
15908442f8aSBosko Milekic 	{ "_clust_limit" },
16008442f8aSBosko Milekic #define	N_NCPUS		44
16108442f8aSBosko Milekic 	{ "_smp_cpus" },
16208442f8aSBosko Milekic #define	N_PAGESZ	45
16308442f8aSBosko Milekic 	{ "_pagesize" },
16408442f8aSBosko Milekic #define	N_MBPSTAT	46
16508442f8aSBosko Milekic 	{ "_mb_statpcpu" },
1661093cc31SRuslan Ermilov #define	N_RTTRASH	47
1671093cc31SRuslan Ermilov 	{ "_rttrash" },
1687d56c0eeSAlexander Langer 	{ "" },
1699b50d902SRodney W. Grimes };
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes struct protox {
1729b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1739b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1749b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
175a01e3379SDavid Malone 	void	(*pr_cblocks)(u_long, const char *, int);
1765e051718SAssar Westerlund 					/* control blocks printing routine */
177a01e3379SDavid Malone 	void	(*pr_stats)(u_long, const char *, int);
1785e051718SAssar Westerlund 					/* statistics printing routine */
1795e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
180fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
181a01e3379SDavid Malone 	u_long	pr_usesysctl;		/* true if we use sysctl, not kvm */
1829b50d902SRodney W. Grimes } protox[] = {
1834f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
184cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1854f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
186cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
1874f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
188cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1894f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
190cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1914f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
192cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1934f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
194cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
195cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
196cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSECSTAT,	1,	0,
197cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
198cfa1ca9dSYoshinobu Inoue #endif
19961793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
200cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
2019b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
202a01e3379SDavid Malone 	  0,		NULL,		0,	0 }
2039b50d902SRodney W. Grimes };
2049b50d902SRodney W. Grimes 
205cfa1ca9dSYoshinobu Inoue #ifdef INET6
206cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
207cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
208cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
209cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
210cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
211785d213dSHajimu UMEMOTO 	{ -1,		N_IP6STAT,	1,	protopr,
212785d213dSHajimu UMEMOTO 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
213785d213dSHajimu UMEMOTO 	{ -1,		N_ICMP6STAT,	1,	protopr,
214785d213dSHajimu UMEMOTO 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
215cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
216cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSEC6STAT,	1,	0,
217cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
218cfa1ca9dSYoshinobu Inoue #endif
219cfa1ca9dSYoshinobu Inoue #ifdef notyet
220cfa1ca9dSYoshinobu Inoue 	{ -1,		N_PIM6STAT,	1,	0,
221cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
222cfa1ca9dSYoshinobu Inoue #endif
22361793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
22433841545SHajimu UMEMOTO 	  rip6_stats,	NULL,		"rip6",	0 },
22533841545SHajimu UMEMOTO 	{ -1,		-1,		1,	0,
226cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
227cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		0,	0,
228cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
229cfa1ca9dSYoshinobu Inoue };
230cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
231cfa1ca9dSYoshinobu Inoue 
2323b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2333b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2343b8a8567SJun-ichiro itojun Hagino 	{ -1,		N_PFKEYSTAT,	1,	0,
2353b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2363b8a8567SJun-ichiro itojun Hagino 	{ -1,		-1,		0,	0,
2373b8a8567SJun-ichiro itojun Hagino 	  0,		NULL,		0,	0 }
2383b8a8567SJun-ichiro itojun Hagino };
2393b8a8567SJun-ichiro itojun Hagino #endif
2403b8a8567SJun-ichiro itojun Hagino 
24163bf4575SJulian Elischer struct protox atalkprotox[] = {
24263bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
243a01e3379SDavid Malone 	  ddp_stats,	NULL,		"ddp",	0 },
24463bf4575SJulian Elischer 	{ -1,		-1,		0,	0,
245a01e3379SDavid Malone 	  0,		NULL,		0,	0 }
24663bf4575SJulian Elischer };
24763bf4575SJulian Elischer 
2484cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2494cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
250a01e3379SDavid Malone 	  NULL,		NULL,		"ctrl",	0 },
2514cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
252a01e3379SDavid Malone 	  NULL,		NULL,		"data",	0 },
253cfa1ca9dSYoshinobu Inoue 	{ -1,		NULL,		0,	0,
254a01e3379SDavid Malone 	  0,		NULL,		0,	0 }
2554cf49a43SJulian Elischer };
2564cf49a43SJulian Elischer 
257cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
258cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
259cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
260cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
261cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
262cc6a66f2SJulian Elischer 	{ -1,		-1,		0,	0,
263cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
264cc6a66f2SJulian Elischer };
265cc6a66f2SJulian Elischer 
266cbc17e71SGarrett Wollman #ifdef NS
2679b50d902SRodney W. Grimes struct protox nsprotox[] = {
2689b50d902SRodney W. Grimes 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
269cfa1ca9dSYoshinobu Inoue 	  idp_stats,	NULL,		"idp" },
2709b50d902SRodney W. Grimes 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
271cfa1ca9dSYoshinobu Inoue 	  spp_stats,	NULL,		"spp" },
2729b50d902SRodney W. Grimes 	{ -1,		N_NSERR,	1,	0,
273cfa1ca9dSYoshinobu Inoue 	  nserr_stats,	NULL,		"ns_err" },
2749b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
275cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2769b50d902SRodney W. Grimes };
277cbc17e71SGarrett Wollman #endif
2789b50d902SRodney W. Grimes 
2790761cb29SGarrett Wollman #ifdef ISO
2809b50d902SRodney W. Grimes struct protox isoprotox[] = {
2819b50d902SRodney W. Grimes 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
282cfa1ca9dSYoshinobu Inoue 	  tp_stats,	NULL,		"tp" },
2839b50d902SRodney W. Grimes 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
284cfa1ca9dSYoshinobu Inoue 	  cltp_stats,	NULL,		"cltp" },
2859b50d902SRodney W. Grimes 	{ -1,		N_CLNPSTAT,	1,	 0,
286cfa1ca9dSYoshinobu Inoue 	  clnp_stats,	NULL,		"clnp"},
2879b50d902SRodney W. Grimes 	{ -1,		N_ESISSTAT,	1,	 0,
288cfa1ca9dSYoshinobu Inoue 	  esis_stats,	NULL,		"esis"},
2899b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
290cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2919b50d902SRodney W. Grimes };
2920761cb29SGarrett Wollman #endif
2939b50d902SRodney W. Grimes 
294cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
295cfa1ca9dSYoshinobu Inoue 					 protox,
296cfa1ca9dSYoshinobu Inoue #ifdef INET6
297cfa1ca9dSYoshinobu Inoue 					 ip6protox,
298cfa1ca9dSYoshinobu Inoue #endif
2993b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
3003b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
3013b8a8567SJun-ichiro itojun Hagino #endif
302cfa1ca9dSYoshinobu Inoue 					 ipxprotox, atalkprotox,
303cbc17e71SGarrett Wollman #ifdef NS
304cbc17e71SGarrett Wollman 					 nsprotox,
305cbc17e71SGarrett Wollman #endif
3060761cb29SGarrett Wollman #ifdef ISO
3070761cb29SGarrett Wollman 					 isoprotox,
3080761cb29SGarrett Wollman #endif
3090761cb29SGarrett Wollman 					 NULL };
3109b50d902SRodney W. Grimes 
311fa6d48c0SMark Murray static void printproto (struct protox *, const char *);
3125e051718SAssar Westerlund static void usage (void);
3135e051718SAssar Westerlund static struct protox *name2protox (char *);
3145e051718SAssar Westerlund static struct protox *knownname (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) */
321080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
322080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
323080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
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 */
3429b50d902SRodney W. Grimes 
3439b50d902SRodney W. Grimes int
344a01e3379SDavid Malone main(int argc, char *argv[])
3459b50d902SRodney W. Grimes {
346a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3479b50d902SRodney W. Grimes 	int ch;
3489b50d902SRodney W. Grimes 
3499b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3509b50d902SRodney W. Grimes 
351c73d99b5SRuslan Ermilov 	while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
3529b50d902SRodney W. Grimes 		switch(ch) {
3539b50d902SRodney W. Grimes 		case 'A':
3549b50d902SRodney W. Grimes 			Aflag = 1;
3559b50d902SRodney W. Grimes 			break;
3569b50d902SRodney W. Grimes 		case 'a':
3579b50d902SRodney W. Grimes 			aflag = 1;
3589b50d902SRodney W. Grimes 			break;
359e1e293a5SDavid Greenman 		case 'b':
360e1e293a5SDavid Greenman 			bflag = 1;
361e1e293a5SDavid Greenman 			break;
3629b50d902SRodney W. Grimes 		case 'd':
3639b50d902SRodney W. Grimes 			dflag = 1;
3649b50d902SRodney W. Grimes 			break;
3659b50d902SRodney W. Grimes 		case 'f':
366cbc17e71SGarrett Wollman #ifdef NS
3679b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
3689b50d902SRodney W. Grimes 				af = AF_NS;
369cbc17e71SGarrett Wollman 			else
370cbc17e71SGarrett Wollman #endif
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;
378cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
3793b8a8567SJun-ichiro itojun Hagino #ifdef INET6
3803b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3813b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
3823b8a8567SJun-ichiro itojun Hagino #endif /*INET6*/
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;
3900761cb29SGarrett Wollman #ifdef ISO
3919b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
3929b50d902SRodney W. Grimes 				af = AF_ISO;
3930761cb29SGarrett Wollman #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;
4039b50d902SRodney W. Grimes 		case 'I': {
4049b50d902SRodney W. Grimes 			char *cp;
4059b50d902SRodney W. Grimes 
4069b50d902SRodney W. Grimes 			iflag = 1;
4079b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4089b50d902SRodney W. Grimes 				continue;
4099b50d902SRodney W. Grimes 			unit = atoi(cp);
4109b50d902SRodney W. Grimes 			break;
4119b50d902SRodney W. Grimes 		}
4129b50d902SRodney W. Grimes 		case 'i':
4139b50d902SRodney W. Grimes 			iflag = 1;
4149b50d902SRodney W. Grimes 			break;
415ac55add0SGuido van Rooij 		case 'L':
416ac55add0SGuido van Rooij 			Lflag = 1;
417ac55add0SGuido van Rooij 			break;
4189b50d902SRodney W. Grimes 		case 'M':
4199b50d902SRodney W. Grimes 			memf = optarg;
4209b50d902SRodney W. Grimes 			break;
4219b50d902SRodney W. Grimes 		case 'm':
4229b50d902SRodney W. Grimes 			mflag = 1;
4239b50d902SRodney W. Grimes 			break;
4249b50d902SRodney W. Grimes 		case 'N':
4259b50d902SRodney W. Grimes 			nlistf = optarg;
4269b50d902SRodney W. Grimes 			break;
4279b50d902SRodney W. Grimes 		case 'n':
42865ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4299b50d902SRodney W. Grimes 			break;
4309b50d902SRodney W. Grimes 		case 'p':
4319b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
43251e7d42cSGarrett Wollman 				errx(1,
43351e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
43451e7d42cSGarrett Wollman 				     optarg);
4359b50d902SRodney W. Grimes 			}
4369b50d902SRodney W. Grimes 			pflag = 1;
4379b50d902SRodney W. Grimes 			break;
4389b50d902SRodney W. Grimes 		case 'r':
4399b50d902SRodney W. Grimes 			rflag = 1;
4409b50d902SRodney W. Grimes 			break;
4419b50d902SRodney W. Grimes 		case 's':
4429b50d902SRodney W. Grimes 			++sflag;
4439b50d902SRodney W. Grimes 			break;
44465ea0024SAssar Westerlund 		case 'S':
44565ea0024SAssar Westerlund 			numeric_addr = 1;
44665ea0024SAssar Westerlund 			break;
4479b50d902SRodney W. Grimes 		case 't':
4489b50d902SRodney W. Grimes 			tflag = 1;
4499b50d902SRodney W. Grimes 			break;
4509b50d902SRodney W. Grimes 		case 'u':
4519b50d902SRodney W. Grimes 			af = AF_UNIX;
4529b50d902SRodney W. Grimes 			break;
453080b7f49SDag-Erling Smørgrav 		case 'W':
45470057abfSRuslan Ermilov 		case 'l':
455080b7f49SDag-Erling Smørgrav 			Wflag = 1;
456080b7f49SDag-Erling Smørgrav 			break;
4579b50d902SRodney W. Grimes 		case 'w':
4589b50d902SRodney W. Grimes 			interval = atoi(optarg);
4599b50d902SRodney W. Grimes 			iflag = 1;
4609b50d902SRodney W. Grimes 			break;
461c73d99b5SRuslan Ermilov 		case 'z':
462c73d99b5SRuslan Ermilov 			zflag = 1;
463c73d99b5SRuslan Ermilov 			break;
4649b50d902SRodney W. Grimes 		case '?':
4659b50d902SRodney W. Grimes 		default:
4669b50d902SRodney W. Grimes 			usage();
4679b50d902SRodney W. Grimes 		}
4689b50d902SRodney W. Grimes 	argv += optind;
4699b50d902SRodney W. Grimes 	argc -= optind;
4709b50d902SRodney W. Grimes 
4719b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4729b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4739b50d902SRodney W. Grimes 	if (*argv) {
4749b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4759b50d902SRodney W. Grimes 			interval = atoi(*argv);
4769b50d902SRodney W. Grimes 			if (interval <= 0)
4779b50d902SRodney W. Grimes 				usage();
4789b50d902SRodney W. Grimes 			++argv;
4799b50d902SRodney W. Grimes 			iflag = 1;
4809b50d902SRodney W. Grimes 		}
4819b50d902SRodney W. Grimes 		if (*argv) {
4829b50d902SRodney W. Grimes 			nlistf = *argv;
4839b50d902SRodney W. Grimes 			if (*++argv)
4849b50d902SRodney W. Grimes 				memf = *argv;
4859b50d902SRodney W. Grimes 		}
4869b50d902SRodney W. Grimes 	}
4879b50d902SRodney W. Grimes #endif
4889b50d902SRodney W. Grimes 
4899b50d902SRodney W. Grimes 	/*
4909b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4919b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4929b50d902SRodney W. Grimes 	 */
4939b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4949b50d902SRodney W. Grimes 		setgid(getgid());
4959b50d902SRodney W. Grimes 
4969b50d902SRodney W. Grimes 	if (mflag) {
497d15c5f56SRuslan Ermilov 		if (memf != NULL) {
498d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
499d15c5f56SRuslan Ermilov 				mbpr(nl[N_MBSTAT].n_value,
500d15c5f56SRuslan Ermilov 				    nl[N_MBTYPES].n_value,
501d15c5f56SRuslan Ermilov 				    nl[N_NMBCLUSTERS].n_value,
50208442f8aSBosko Milekic 				    nl[N_NMBUFS].n_value,
50308442f8aSBosko Milekic 				    nl[N_MBLIM].n_value,
50408442f8aSBosko Milekic 				    nl[N_CLLIM].n_value,
50508442f8aSBosko Milekic 				    nl[N_NCPUS].n_value,
50608442f8aSBosko Milekic 				    nl[N_PAGESZ].n_value,
50708442f8aSBosko Milekic 				    nl[N_MBPSTAT].n_value);
508d15c5f56SRuslan Ermilov 		} else
50908442f8aSBosko Milekic 			mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0);
5109b50d902SRodney W. Grimes 		exit(0);
5119b50d902SRodney W. Grimes 	}
512cc63cd56SPeter Wemm #if 0
5139b50d902SRodney W. Grimes 	/*
5149b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5159b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5169b50d902SRodney W. Grimes 	 */
5179b50d902SRodney W. Grimes 	sethostent(1);
5189b50d902SRodney W. Grimes 	setnetent(1);
519cc63cd56SPeter Wemm #else
520cc63cd56SPeter Wemm 	/*
521cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
522cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
523cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
524cc63cd56SPeter Wemm 	 */
525cc63cd56SPeter Wemm #endif
526cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
527c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
528cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5299b50d902SRodney W. Grimes 		exit(0);
5309b50d902SRodney W. Grimes 	}
5319b50d902SRodney W. Grimes 	if (rflag) {
532c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
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) {
540c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
541cfa1ca9dSYoshinobu Inoue 		if (sflag) {
542cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54367a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
544cfa1ca9dSYoshinobu Inoue #ifdef INET6
545cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
546cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
547cfa1ca9dSYoshinobu Inoue #endif
548cfa1ca9dSYoshinobu Inoue 		} else {
549cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
55067a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5519b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
552cfa1ca9dSYoshinobu Inoue #ifdef INET6
553cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
554cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
555cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
556cfa1ca9dSYoshinobu Inoue #endif
557cfa1ca9dSYoshinobu Inoue 		}
5589b50d902SRodney W. Grimes 		exit(0);
5599b50d902SRodney W. Grimes 	}
560cfa1ca9dSYoshinobu Inoue 
561cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
562cf5e44f8SRuslan Ermilov 	if (tp) {
563cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
564cf5e44f8SRuslan Ermilov 		exit(0);
565cf5e44f8SRuslan Ermilov 	}
566cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5679b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
568cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
569cfa1ca9dSYoshinobu Inoue #ifdef INET6
570cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
571cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
572cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
573cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5743b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5753b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5763b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5773b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5783b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
579e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
580e30e913cSJohn Hay 		kread(0, 0, 0);
581cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
582cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
583e30e913cSJohn Hay 	}
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);
590cbc17e71SGarrett Wollman #ifdef NS
5919b50d902SRodney W. Grimes 	if (af == AF_NS || af == AF_UNSPEC)
5929b50d902SRodney W. Grimes 		for (tp = nsprotox; tp->pr_name; tp++)
5939b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
594cbc17e71SGarrett Wollman #endif
5950761cb29SGarrett Wollman #ifdef ISO
5969b50d902SRodney W. Grimes 	if (af == AF_ISO || af == AF_UNSPEC)
5979b50d902SRodney W. Grimes 		for (tp = isoprotox; tp->pr_name; tp++)
5989b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
5990761cb29SGarrett Wollman #endif
600ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
6014f81ef50SGarrett Wollman 		unixpr();
6029b50d902SRodney W. Grimes 	exit(0);
6039b50d902SRodney W. Grimes }
6049b50d902SRodney W. Grimes 
6059b50d902SRodney W. Grimes /*
6069b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6079b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6089b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6099b50d902SRodney W. Grimes  */
6109b50d902SRodney W. Grimes static void
6119b50d902SRodney W. Grimes printproto(tp, name)
612a01e3379SDavid Malone 	struct protox *tp;
613fa6d48c0SMark Murray 	const char *name;
6149b50d902SRodney W. Grimes {
615a01e3379SDavid Malone 	void (*pr)(u_long, const char *, int);
6169b50d902SRodney W. Grimes 	u_long off;
6179b50d902SRodney W. Grimes 
6189b50d902SRodney W. Grimes 	if (sflag) {
619cfa1ca9dSYoshinobu Inoue 		if (iflag) {
620cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
621cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
622cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
623cf5e44f8SRuslan Ermilov 			else if (pflag)
624cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
625cf5e44f8SRuslan Ermilov 				    tp->pr_name);
626cfa1ca9dSYoshinobu Inoue 			return;
627cfa1ca9dSYoshinobu Inoue 		}
628cfa1ca9dSYoshinobu Inoue 		else {
6299b50d902SRodney W. Grimes 			pr = tp->pr_stats;
630cf5e44f8SRuslan Ermilov 			if (!pr) {
631cf5e44f8SRuslan Ermilov 				if (pflag)
632cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
633cf5e44f8SRuslan Ermilov 					    tp->pr_name);
634cf5e44f8SRuslan Ermilov 				return;
635cf5e44f8SRuslan Ermilov 			}
6364f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
6374f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
638cfa1ca9dSYoshinobu Inoue 		}
6399b50d902SRodney W. Grimes 	} else {
6409b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
641cf5e44f8SRuslan Ermilov 		if (!pr) {
642cf5e44f8SRuslan Ermilov 			if (pflag)
643cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
644cf5e44f8SRuslan Ermilov 			return;
645cf5e44f8SRuslan Ermilov 		}
6464f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
6474f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6489b50d902SRodney W. Grimes 	}
6499b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
650cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6519b50d902SRodney W. Grimes }
6529b50d902SRodney W. Grimes 
6539b50d902SRodney W. Grimes /*
6549b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6559b50d902SRodney W. Grimes  */
6569b50d902SRodney W. Grimes int
6575e051718SAssar Westerlund kread(u_long addr, char *buf, int size)
6589b50d902SRodney W. Grimes {
6594f81ef50SGarrett Wollman 	if (kvmd == 0) {
66099453c6aSPoul-Henning Kamp 		/*
66199453c6aSPoul-Henning Kamp 		 * XXX.
66299453c6aSPoul-Henning Kamp 		 */
66399453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
6643b7e5cccSRuslan Ermilov 		setgid(getgid());
66599453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
66699453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
66799453c6aSPoul-Henning Kamp 				if(nlistf)
66899453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
66999453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
67099453c6aSPoul-Henning Kamp 				else
67199453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
67299453c6aSPoul-Henning Kamp 			}
67399453c6aSPoul-Henning Kamp 
67499453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
67599453c6aSPoul-Henning Kamp 				if(nlistf)
67699453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
67799453c6aSPoul-Henning Kamp 				else
67899453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
67999453c6aSPoul-Henning Kamp 			}
68099453c6aSPoul-Henning Kamp 		} else {
68199453c6aSPoul-Henning Kamp 			warnx("kvm not available");
68299453c6aSPoul-Henning Kamp 			return(-1);
68399453c6aSPoul-Henning Kamp 		}
6844f81ef50SGarrett Wollman 	}
685c6620a13SPoul-Henning Kamp 	if (!buf)
686c6620a13SPoul-Henning Kamp 		return (0);
6879b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
6883aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6899b50d902SRodney W. Grimes 		return (-1);
6909b50d902SRodney W. Grimes 	}
6919b50d902SRodney W. Grimes 	return (0);
6929b50d902SRodney W. Grimes }
6939b50d902SRodney W. Grimes 
694a01e3379SDavid Malone const char *
6955e051718SAssar Westerlund plural(int n)
6969b50d902SRodney W. Grimes {
6979b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
6989b50d902SRodney W. Grimes }
6999b50d902SRodney W. Grimes 
700a01e3379SDavid Malone const char *
7015e051718SAssar Westerlund plurales(int n)
7029b50d902SRodney W. Grimes {
7039b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes 
706f99a4046SMike Makonnen const char *
707f99a4046SMike Makonnen pluralies(int n)
708f99a4046SMike Makonnen {
709f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
710f99a4046SMike Makonnen }
711f99a4046SMike Makonnen 
7129b50d902SRodney W. Grimes /*
7139b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7149b50d902SRodney W. Grimes  */
7159b50d902SRodney W. Grimes static struct protox *
7165e051718SAssar Westerlund knownname(char *name)
7179b50d902SRodney W. Grimes {
7189b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7199b50d902SRodney W. Grimes 
7209b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7219b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7229b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7239b50d902SRodney W. Grimes 				return (tp);
7249b50d902SRodney W. Grimes 	return (NULL);
7259b50d902SRodney W. Grimes }
7269b50d902SRodney W. Grimes 
7279b50d902SRodney W. Grimes /*
7289b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7299b50d902SRodney W. Grimes  */
7309b50d902SRodney W. Grimes static struct protox *
7315e051718SAssar Westerlund name2protox(char *name)
7329b50d902SRodney W. Grimes {
7339b50d902SRodney W. Grimes 	struct protox *tp;
7349b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7359b50d902SRodney W. Grimes 	struct protoent *p;
7369b50d902SRodney W. Grimes 
7379b50d902SRodney W. Grimes 	/*
7389b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7399b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7409b50d902SRodney W. Grimes 	 */
741cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7429b50d902SRodney W. Grimes 		return (tp);
7439b50d902SRodney W. Grimes 
7449b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
745cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7469b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7479b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7489b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7499b50d902SRodney W. Grimes 				endprotoent();
7509b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7519b50d902SRodney W. Grimes 			}
7529b50d902SRodney W. Grimes 	}
7539b50d902SRodney W. Grimes 	endprotoent();
7549b50d902SRodney W. Grimes 	return (NULL);
7559b50d902SRodney W. Grimes }
7569b50d902SRodney W. Grimes 
7579b50d902SRodney W. Grimes static void
7585e051718SAssar Westerlund usage(void)
7599b50d902SRodney W. Grimes {
7601cb467b1SRuslan Ermilov 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
7611cb467b1SRuslan Ermilov "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
7621cb467b1SRuslan Ermilov "               [-M core] [-N system]",
763d44ddba9SRuslan Ermilov "       netstat -i | -I interface [-abdnt] [-f address_family]\n"
764d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7651cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
7661cb467b1SRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
7671cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7681cb467b1SRuslan Ermilov "               [-M core] [-N system]",
769d15c5f56SRuslan Ermilov "       netstat -m [-M core] [-N system]",
7701cb467b1SRuslan Ermilov "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
7711cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7721cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7731cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7749b50d902SRodney W. Grimes 	exit(1);
7759b50d902SRodney W. Grimes }
776