xref: /freebsd/usr.bin/netstat/main.c (revision d44ddba96c0c98edac3e1f29341a7b64fed81965)
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 */
1755e051718SAssar Westerlund 	void	(*pr_cblocks)(u_long, char *, int);
1765e051718SAssar Westerlund 					/* control blocks printing routine */
1775e051718SAssar Westerlund 	void	(*pr_stats)(u_long, char *, int);
1785e051718SAssar Westerlund 					/* statistics printing routine */
1795e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
1809b50d902SRodney W. Grimes 	char	*pr_name;		/* well-known name */
1814f81ef50SGarrett Wollman 	int	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,
202cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		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,
243cfa1ca9dSYoshinobu Inoue 	  ddp_stats,	NULL,		"ddp" },
24463bf4575SJulian Elischer 	{ -1,		-1,		0,	0,
245cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
24663bf4575SJulian Elischer };
24763bf4575SJulian Elischer 
2484cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2494cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
250cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"ctrl" },
2514cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
252cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"data" },
253cfa1ca9dSYoshinobu Inoue 	{ -1,		NULL,		0,	0,
254cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		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 
3115e051718SAssar Westerlund static void printproto (struct protox *, 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
3449b50d902SRodney W. Grimes main(argc, argv)
3459b50d902SRodney W. Grimes 	int argc;
3469b50d902SRodney W. Grimes 	char *argv[];
3479b50d902SRodney W. Grimes {
3484cf49a43SJulian Elischer 	register 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 
353c73d99b5SRuslan Ermilov 	while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -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;
361e1e293a5SDavid Greenman 		case 'b':
362e1e293a5SDavid Greenman 			bflag = 1;
363e1e293a5SDavid Greenman 			break;
3649b50d902SRodney W. Grimes 		case 'd':
3659b50d902SRodney W. Grimes 			dflag = 1;
3669b50d902SRodney W. Grimes 			break;
3679b50d902SRodney W. Grimes 		case 'f':
368cbc17e71SGarrett Wollman #ifdef NS
3699b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
3709b50d902SRodney W. Grimes 				af = AF_NS;
371cbc17e71SGarrett Wollman 			else
372cbc17e71SGarrett Wollman #endif
373cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
374cc6a66f2SJulian Elischer 				af = AF_IPX;
3759b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3769b50d902SRodney W. Grimes 				af = AF_INET;
377cfa1ca9dSYoshinobu Inoue #ifdef INET6
378cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
379cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
380cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
3813b8a8567SJun-ichiro itojun Hagino #ifdef INET6
3823b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3833b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
3843b8a8567SJun-ichiro itojun Hagino #endif /*INET6*/
3859b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3869b50d902SRodney W. Grimes 				af = AF_UNIX;
38763bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
38863bf4575SJulian Elischer 				af = AF_APPLETALK;
3894cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3904cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3914cf49a43SJulian Elischer 				af = AF_NETGRAPH;
3920761cb29SGarrett Wollman #ifdef ISO
3939b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
3949b50d902SRodney W. Grimes 				af = AF_ISO;
3950761cb29SGarrett Wollman #endif
396d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
397d44ddba9SRuslan Ermilov 				af = AF_LINK;
3989b50d902SRodney W. Grimes 			else {
39951e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
4009b50d902SRodney W. Grimes 			}
4019b50d902SRodney W. Grimes 			break;
4029b50d902SRodney W. Grimes 		case 'g':
4039b50d902SRodney W. Grimes 			gflag = 1;
4049b50d902SRodney W. Grimes 			break;
4059b50d902SRodney W. Grimes 		case 'I': {
4069b50d902SRodney W. Grimes 			char *cp;
4079b50d902SRodney W. Grimes 
4089b50d902SRodney W. Grimes 			iflag = 1;
4099b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4109b50d902SRodney W. Grimes 				continue;
4119b50d902SRodney W. Grimes 			unit = atoi(cp);
4129b50d902SRodney W. Grimes 			break;
4139b50d902SRodney W. Grimes 		}
4149b50d902SRodney W. Grimes 		case 'i':
4159b50d902SRodney W. Grimes 			iflag = 1;
4169b50d902SRodney W. Grimes 			break;
417ac55add0SGuido van Rooij 		case 'L':
418ac55add0SGuido van Rooij 			Lflag = 1;
419ac55add0SGuido van Rooij 			break;
4209b50d902SRodney W. Grimes 		case 'M':
4219b50d902SRodney W. Grimes 			memf = optarg;
4229b50d902SRodney W. Grimes 			break;
4239b50d902SRodney W. Grimes 		case 'm':
4249b50d902SRodney W. Grimes 			mflag = 1;
4259b50d902SRodney W. Grimes 			break;
4269b50d902SRodney W. Grimes 		case 'N':
4279b50d902SRodney W. Grimes 			nlistf = optarg;
4289b50d902SRodney W. Grimes 			break;
4299b50d902SRodney W. Grimes 		case 'n':
43065ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4319b50d902SRodney W. Grimes 			break;
4329b50d902SRodney W. Grimes 		case 'p':
4339b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
43451e7d42cSGarrett Wollman 				errx(1,
43551e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
43651e7d42cSGarrett Wollman 				     optarg);
4379b50d902SRodney W. Grimes 			}
4389b50d902SRodney W. Grimes 			pflag = 1;
4399b50d902SRodney W. Grimes 			break;
4409b50d902SRodney W. Grimes 		case 'r':
4419b50d902SRodney W. Grimes 			rflag = 1;
4429b50d902SRodney W. Grimes 			break;
4439b50d902SRodney W. Grimes 		case 's':
4449b50d902SRodney W. Grimes 			++sflag;
4459b50d902SRodney W. Grimes 			break;
44665ea0024SAssar Westerlund 		case 'S':
44765ea0024SAssar Westerlund 			numeric_addr = 1;
44865ea0024SAssar Westerlund 			break;
4499b50d902SRodney W. Grimes 		case 't':
4509b50d902SRodney W. Grimes 			tflag = 1;
4519b50d902SRodney W. Grimes 			break;
4529b50d902SRodney W. Grimes 		case 'u':
4539b50d902SRodney W. Grimes 			af = AF_UNIX;
4549b50d902SRodney W. Grimes 			break;
455080b7f49SDag-Erling Smørgrav 		case 'W':
45670057abfSRuslan Ermilov 		case 'l':
457080b7f49SDag-Erling Smørgrav 			Wflag = 1;
458080b7f49SDag-Erling Smørgrav 			break;
4599b50d902SRodney W. Grimes 		case 'w':
4609b50d902SRodney W. Grimes 			interval = atoi(optarg);
4619b50d902SRodney W. Grimes 			iflag = 1;
4629b50d902SRodney W. Grimes 			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 	 */
4959b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4969b50d902SRodney W. Grimes 		setgid(getgid());
4979b50d902SRodney W. Grimes 
4989b50d902SRodney W. Grimes 	if (mflag) {
499d15c5f56SRuslan Ermilov 		if (memf != NULL) {
500d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
501d15c5f56SRuslan Ermilov 				mbpr(nl[N_MBSTAT].n_value,
502d15c5f56SRuslan Ermilov 				    nl[N_MBTYPES].n_value,
503d15c5f56SRuslan Ermilov 				    nl[N_NMBCLUSTERS].n_value,
50408442f8aSBosko Milekic 				    nl[N_NMBUFS].n_value,
50508442f8aSBosko Milekic 				    nl[N_MBLIM].n_value,
50608442f8aSBosko Milekic 				    nl[N_CLLIM].n_value,
50708442f8aSBosko Milekic 				    nl[N_NCPUS].n_value,
50808442f8aSBosko Milekic 				    nl[N_PAGESZ].n_value,
50908442f8aSBosko Milekic 				    nl[N_MBPSTAT].n_value);
510d15c5f56SRuslan Ermilov 		} else
51108442f8aSBosko Milekic 			mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0);
5129b50d902SRodney W. Grimes 		exit(0);
5139b50d902SRodney W. Grimes 	}
514cc63cd56SPeter Wemm #if 0
5159b50d902SRodney W. Grimes 	/*
5169b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5179b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5189b50d902SRodney W. Grimes 	 */
5199b50d902SRodney W. Grimes 	sethostent(1);
5209b50d902SRodney W. Grimes 	setnetent(1);
521cc63cd56SPeter Wemm #else
522cc63cd56SPeter Wemm 	/*
523cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
524cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
525cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
526cc63cd56SPeter Wemm 	 */
527cc63cd56SPeter Wemm #endif
528cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
529c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
530cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5319b50d902SRodney W. Grimes 		exit(0);
5329b50d902SRodney W. Grimes 	}
5339b50d902SRodney W. Grimes 	if (rflag) {
534c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
5359b50d902SRodney W. Grimes 		if (sflag)
5361093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
5379b50d902SRodney W. Grimes 		else
5389b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5399b50d902SRodney W. Grimes 		exit(0);
5409b50d902SRodney W. Grimes 	}
5419b50d902SRodney W. Grimes 	if (gflag) {
542c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
543cfa1ca9dSYoshinobu Inoue 		if (sflag) {
544cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54567a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
546cfa1ca9dSYoshinobu Inoue #ifdef INET6
547cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
548cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
549cfa1ca9dSYoshinobu Inoue #endif
550cfa1ca9dSYoshinobu Inoue 		} else {
551cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
55267a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5539b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
554cfa1ca9dSYoshinobu Inoue #ifdef INET6
555cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
556cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
557cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
558cfa1ca9dSYoshinobu Inoue #endif
559cfa1ca9dSYoshinobu Inoue 		}
5609b50d902SRodney W. Grimes 		exit(0);
5619b50d902SRodney W. Grimes 	}
562cfa1ca9dSYoshinobu Inoue 
563cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
564cf5e44f8SRuslan Ermilov 	if (tp) {
565cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
566cf5e44f8SRuslan Ermilov 		exit(0);
567cf5e44f8SRuslan Ermilov 	}
568cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5699b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
570cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
571cfa1ca9dSYoshinobu Inoue #ifdef INET6
572cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
573cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
574cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
575cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5763b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5773b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5783b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5793b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5803b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
581e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
582e30e913cSJohn Hay 		kread(0, 0, 0);
583cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
584cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
585e30e913cSJohn Hay 	}
58663bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
58763bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
58863bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5894cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5904cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5914cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
592cbc17e71SGarrett Wollman #ifdef NS
5939b50d902SRodney W. Grimes 	if (af == AF_NS || af == AF_UNSPEC)
5949b50d902SRodney W. Grimes 		for (tp = nsprotox; tp->pr_name; tp++)
5959b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
596cbc17e71SGarrett Wollman #endif
5970761cb29SGarrett Wollman #ifdef ISO
5989b50d902SRodney W. Grimes 	if (af == AF_ISO || af == AF_UNSPEC)
5999b50d902SRodney W. Grimes 		for (tp = isoprotox; tp->pr_name; tp++)
6009b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
6010761cb29SGarrett Wollman #endif
602ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
6034f81ef50SGarrett Wollman 		unixpr();
6049b50d902SRodney W. Grimes 	exit(0);
6059b50d902SRodney W. Grimes }
6069b50d902SRodney W. Grimes 
6079b50d902SRodney W. Grimes /*
6089b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6099b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6109b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6119b50d902SRodney W. Grimes  */
6129b50d902SRodney W. Grimes static void
6139b50d902SRodney W. Grimes printproto(tp, name)
6149b50d902SRodney W. Grimes 	register struct protox *tp;
6159b50d902SRodney W. Grimes 	char *name;
6169b50d902SRodney W. Grimes {
617f964d60dSAssar Westerlund 	void (*pr)(u_long, char *, int);
6189b50d902SRodney W. Grimes 	u_long off;
6199b50d902SRodney W. Grimes 
6209b50d902SRodney W. Grimes 	if (sflag) {
621cfa1ca9dSYoshinobu Inoue 		if (iflag) {
622cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
623cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
624cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
625cf5e44f8SRuslan Ermilov 			else if (pflag)
626cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
627cf5e44f8SRuslan Ermilov 				    tp->pr_name);
628cfa1ca9dSYoshinobu Inoue 			return;
629cfa1ca9dSYoshinobu Inoue 		}
630cfa1ca9dSYoshinobu Inoue 		else {
6319b50d902SRodney W. Grimes 			pr = tp->pr_stats;
632cf5e44f8SRuslan Ermilov 			if (!pr) {
633cf5e44f8SRuslan Ermilov 				if (pflag)
634cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
635cf5e44f8SRuslan Ermilov 					    tp->pr_name);
636cf5e44f8SRuslan Ermilov 				return;
637cf5e44f8SRuslan Ermilov 			}
6384f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
6394f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
640cfa1ca9dSYoshinobu Inoue 		}
6419b50d902SRodney W. Grimes 	} else {
6429b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
643cf5e44f8SRuslan Ermilov 		if (!pr) {
644cf5e44f8SRuslan Ermilov 			if (pflag)
645cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
646cf5e44f8SRuslan Ermilov 			return;
647cf5e44f8SRuslan Ermilov 		}
6484f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
6494f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6509b50d902SRodney W. Grimes 	}
6519b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
652cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6539b50d902SRodney W. Grimes }
6549b50d902SRodney W. Grimes 
6559b50d902SRodney W. Grimes /*
6569b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6579b50d902SRodney W. Grimes  */
6589b50d902SRodney W. Grimes int
6595e051718SAssar Westerlund kread(u_long addr, char *buf, int size)
6609b50d902SRodney W. Grimes {
6614f81ef50SGarrett Wollman 	if (kvmd == 0) {
66299453c6aSPoul-Henning Kamp 		/*
66399453c6aSPoul-Henning Kamp 		 * XXX.
66499453c6aSPoul-Henning Kamp 		 */
66599453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
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 {
68399453c6aSPoul-Henning Kamp 			warnx("kvm not available");
68499453c6aSPoul-Henning Kamp 			return(-1);
68599453c6aSPoul-Henning Kamp 		}
6864f81ef50SGarrett Wollman 	}
687c6620a13SPoul-Henning Kamp 	if (!buf)
688c6620a13SPoul-Henning Kamp 		return (0);
6899b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != 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 
6969b50d902SRodney W. Grimes char *
6975e051718SAssar Westerlund plural(int n)
6989b50d902SRodney W. Grimes {
6999b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
7009b50d902SRodney W. Grimes }
7019b50d902SRodney W. Grimes 
7029b50d902SRodney W. Grimes char *
7035e051718SAssar Westerlund plurales(int n)
7049b50d902SRodney W. Grimes {
7059b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7069b50d902SRodney W. Grimes }
7079b50d902SRodney W. Grimes 
7089b50d902SRodney W. Grimes /*
7099b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7109b50d902SRodney W. Grimes  */
7119b50d902SRodney W. Grimes static struct protox *
7125e051718SAssar Westerlund knownname(char *name)
7139b50d902SRodney W. Grimes {
7149b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7159b50d902SRodney W. Grimes 
7169b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7179b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7189b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7199b50d902SRodney W. Grimes 				return (tp);
7209b50d902SRodney W. Grimes 	return (NULL);
7219b50d902SRodney W. Grimes }
7229b50d902SRodney W. Grimes 
7239b50d902SRodney W. Grimes /*
7249b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7259b50d902SRodney W. Grimes  */
7269b50d902SRodney W. Grimes static struct protox *
7275e051718SAssar Westerlund name2protox(char *name)
7289b50d902SRodney W. Grimes {
7299b50d902SRodney W. Grimes 	struct protox *tp;
7309b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7319b50d902SRodney W. Grimes 	struct protoent *p;
7329b50d902SRodney W. Grimes 
7339b50d902SRodney W. Grimes 	/*
7349b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7359b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7369b50d902SRodney W. Grimes 	 */
737cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7389b50d902SRodney W. Grimes 		return (tp);
7399b50d902SRodney W. Grimes 
7409b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
741cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7429b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7439b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7449b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7459b50d902SRodney W. Grimes 				endprotoent();
7469b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7479b50d902SRodney W. Grimes 			}
7489b50d902SRodney W. Grimes 	}
7499b50d902SRodney W. Grimes 	endprotoent();
7509b50d902SRodney W. Grimes 	return (NULL);
7519b50d902SRodney W. Grimes }
7529b50d902SRodney W. Grimes 
7539b50d902SRodney W. Grimes static void
7545e051718SAssar Westerlund usage(void)
7559b50d902SRodney W. Grimes {
7561cb467b1SRuslan Ermilov 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
7571cb467b1SRuslan Ermilov "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
7581cb467b1SRuslan Ermilov "               [-M core] [-N system]",
759d44ddba9SRuslan Ermilov "       netstat -i | -I interface [-abdnt] [-f address_family]\n"
760d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7611cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
7621cb467b1SRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
7631cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7641cb467b1SRuslan Ermilov "               [-M core] [-N system]",
765d15c5f56SRuslan Ermilov "       netstat -m [-M core] [-N system]",
7661cb467b1SRuslan Ermilov "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
7671cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7681cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7691cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7709b50d902SRodney W. Grimes 	exit(1);
7719b50d902SRodney W. Grimes }
772