xref: /freebsd/usr.bin/netstat/main.c (revision 65ea0024ba6f8aeac9b9f28532a63bc7ac6174ae)
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" },
1567d56c0eeSAlexander Langer 	{ "" },
1579b50d902SRodney W. Grimes };
1589b50d902SRodney W. Grimes 
1599b50d902SRodney W. Grimes struct protox {
1609b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1619b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1629b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
1639b50d902SRodney W. Grimes 	void	(*pr_cblocks)();	/* control blocks printing routine */
1649b50d902SRodney W. Grimes 	void	(*pr_stats)();		/* statistics printing routine */
165cfa1ca9dSYoshinobu Inoue 	void	(*pr_istats)();		/* per/if statistics printing routine */
1669b50d902SRodney W. Grimes 	char	*pr_name;		/* well-known name */
1674f81ef50SGarrett Wollman 	int	pr_usesysctl;		/* true if we use sysctl, not kvm */
1689b50d902SRodney W. Grimes } protox[] = {
1694f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
170cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1714f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
172cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
1734f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
174cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1754f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
176cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1774f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
178cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1794f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
180cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
181cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
182cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSECSTAT,	1,	0,
183cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
184cfa1ca9dSYoshinobu Inoue #endif
18561793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
186cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
1879b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
188cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
1899b50d902SRodney W. Grimes };
1909b50d902SRodney W. Grimes 
191cfa1ca9dSYoshinobu Inoue #ifdef INET6
192cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
193cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
194cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
195cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
196cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
197cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IP6STAT,	1,	0,
198cfa1ca9dSYoshinobu Inoue 	  ip6_stats,	ip6_ifstats,	"ip6",	0 },
199cfa1ca9dSYoshinobu Inoue 	{ -1,		N_ICMP6STAT,	1,	0,
200cfa1ca9dSYoshinobu Inoue 	  icmp6_stats,	icmp6_ifstats,	"icmp6",0 },
201cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
202cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSEC6STAT,	1,	0,
203cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
204cfa1ca9dSYoshinobu Inoue #endif
205cfa1ca9dSYoshinobu Inoue #ifdef notyet
206cfa1ca9dSYoshinobu Inoue 	{ -1,		N_PIM6STAT,	1,	0,
207cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
208cfa1ca9dSYoshinobu Inoue #endif
20961793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
21033841545SHajimu UMEMOTO 	  rip6_stats,	NULL,		"rip6",	0 },
21133841545SHajimu UMEMOTO 	{ -1,		-1,		1,	0,
212cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
213cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		0,	0,
214cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
215cfa1ca9dSYoshinobu Inoue };
216cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
217cfa1ca9dSYoshinobu Inoue 
2183b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2193b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2203b8a8567SJun-ichiro itojun Hagino 	{ -1,		N_PFKEYSTAT,	1,	0,
2213b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2223b8a8567SJun-ichiro itojun Hagino 	{ -1,		-1,		0,	0,
2233b8a8567SJun-ichiro itojun Hagino 	  0,		NULL,		0,	0 }
2243b8a8567SJun-ichiro itojun Hagino };
2253b8a8567SJun-ichiro itojun Hagino #endif
2263b8a8567SJun-ichiro itojun Hagino 
22763bf4575SJulian Elischer struct protox atalkprotox[] = {
22863bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
229cfa1ca9dSYoshinobu Inoue 	  ddp_stats,	NULL,		"ddp" },
23063bf4575SJulian Elischer 	{ -1,		-1,		0,	0,
231cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
23263bf4575SJulian Elischer };
23363bf4575SJulian Elischer 
2344cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2354cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
236cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"ctrl" },
2374cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
238cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"data" },
239cfa1ca9dSYoshinobu Inoue 	{ -1,		NULL,		0,	0,
240cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2414cf49a43SJulian Elischer };
2424cf49a43SJulian Elischer 
243cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
244cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
245cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
246cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
247cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
248cc6a66f2SJulian Elischer 	{ -1,		-1,		0,	0,
249cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
250cc6a66f2SJulian Elischer };
251cc6a66f2SJulian Elischer 
252cbc17e71SGarrett Wollman #ifdef NS
2539b50d902SRodney W. Grimes struct protox nsprotox[] = {
2549b50d902SRodney W. Grimes 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
255cfa1ca9dSYoshinobu Inoue 	  idp_stats,	NULL,		"idp" },
2569b50d902SRodney W. Grimes 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
257cfa1ca9dSYoshinobu Inoue 	  spp_stats,	NULL,		"spp" },
2589b50d902SRodney W. Grimes 	{ -1,		N_NSERR,	1,	0,
259cfa1ca9dSYoshinobu Inoue 	  nserr_stats,	NULL,		"ns_err" },
2609b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
261cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2629b50d902SRodney W. Grimes };
263cbc17e71SGarrett Wollman #endif
2649b50d902SRodney W. Grimes 
2650761cb29SGarrett Wollman #ifdef ISO
2669b50d902SRodney W. Grimes struct protox isoprotox[] = {
2679b50d902SRodney W. Grimes 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
268cfa1ca9dSYoshinobu Inoue 	  tp_stats,	NULL,		"tp" },
2699b50d902SRodney W. Grimes 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
270cfa1ca9dSYoshinobu Inoue 	  cltp_stats,	NULL,		"cltp" },
2719b50d902SRodney W. Grimes 	{ -1,		N_CLNPSTAT,	1,	 0,
272cfa1ca9dSYoshinobu Inoue 	  clnp_stats,	NULL,		"clnp"},
2739b50d902SRodney W. Grimes 	{ -1,		N_ESISSTAT,	1,	 0,
274cfa1ca9dSYoshinobu Inoue 	  esis_stats,	NULL,		"esis"},
2759b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
276cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2779b50d902SRodney W. Grimes };
2780761cb29SGarrett Wollman #endif
2799b50d902SRodney W. Grimes 
280cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
281cfa1ca9dSYoshinobu Inoue 					 protox,
282cfa1ca9dSYoshinobu Inoue #ifdef INET6
283cfa1ca9dSYoshinobu Inoue 					 ip6protox,
284cfa1ca9dSYoshinobu Inoue #endif
2853b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2863b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2873b8a8567SJun-ichiro itojun Hagino #endif
288cfa1ca9dSYoshinobu Inoue 					 ipxprotox, atalkprotox,
289cbc17e71SGarrett Wollman #ifdef NS
290cbc17e71SGarrett Wollman 					 nsprotox,
291cbc17e71SGarrett Wollman #endif
2920761cb29SGarrett Wollman #ifdef ISO
2930761cb29SGarrett Wollman 					 isoprotox,
2940761cb29SGarrett Wollman #endif
2950761cb29SGarrett Wollman 					 NULL };
2969b50d902SRodney W. Grimes 
2979b50d902SRodney W. Grimes static void printproto __P((struct protox *, char *));
2989b50d902SRodney W. Grimes static void usage __P((void));
2999b50d902SRodney W. Grimes static struct protox *name2protox __P((char *));
3009b50d902SRodney W. Grimes static struct protox *knownname __P((char *));
3019b50d902SRodney W. Grimes 
302c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
303080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
304080b7f49SDag-Erling Smørgrav 
305080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
306080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
307080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
308080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
309080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
310080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
311080b7f49SDag-Erling Smørgrav int	lflag;		/* show routing table with use and ref */
312080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
313080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
31465ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
31565ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
316080b7f49SDag-Erling Smørgrav int	pflag;		/* show given protocol */
317080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
318080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
319080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
320080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
321080b7f49SDag-Erling Smørgrav 
322080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
323080b7f49SDag-Erling Smørgrav 
324080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
325080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
326080b7f49SDag-Erling Smørgrav 
327080b7f49SDag-Erling Smørgrav int	af;		/* address family */
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes int
3309b50d902SRodney W. Grimes main(argc, argv)
3319b50d902SRodney W. Grimes 	int argc;
3329b50d902SRodney W. Grimes 	char *argv[];
3339b50d902SRodney W. Grimes {
3344cf49a43SJulian Elischer 	register struct protox *tp = NULL;  /* for printing cblocks & stats */
3359b50d902SRodney W. Grimes 	int ch;
3369b50d902SRodney W. Grimes 
3379b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3389b50d902SRodney W. Grimes 
33965ea0024SAssar Westerlund 	while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rsStuWw:")) != -1)
3409b50d902SRodney W. Grimes 		switch(ch) {
3419b50d902SRodney W. Grimes 		case 'A':
3429b50d902SRodney W. Grimes 			Aflag = 1;
3439b50d902SRodney W. Grimes 			break;
3449b50d902SRodney W. Grimes 		case 'a':
3459b50d902SRodney W. Grimes 			aflag = 1;
3469b50d902SRodney W. Grimes 			break;
347e1e293a5SDavid Greenman 		case 'b':
348e1e293a5SDavid Greenman 			bflag = 1;
349e1e293a5SDavid Greenman 			break;
3509b50d902SRodney W. Grimes 		case 'd':
3519b50d902SRodney W. Grimes 			dflag = 1;
3529b50d902SRodney W. Grimes 			break;
3539b50d902SRodney W. Grimes 		case 'f':
354cbc17e71SGarrett Wollman #ifdef NS
3559b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
3569b50d902SRodney W. Grimes 				af = AF_NS;
357cbc17e71SGarrett Wollman 			else
358cbc17e71SGarrett Wollman #endif
359cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
360cc6a66f2SJulian Elischer 				af = AF_IPX;
3619b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3629b50d902SRodney W. Grimes 				af = AF_INET;
363cfa1ca9dSYoshinobu Inoue #ifdef INET6
364cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
365cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
366cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
3673b8a8567SJun-ichiro itojun Hagino #ifdef INET6
3683b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3693b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
3703b8a8567SJun-ichiro itojun Hagino #endif /*INET6*/
3719b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3729b50d902SRodney W. Grimes 				af = AF_UNIX;
37363bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
37463bf4575SJulian Elischer 				af = AF_APPLETALK;
3754cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3764cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3774cf49a43SJulian Elischer 				af = AF_NETGRAPH;
3780761cb29SGarrett Wollman #ifdef ISO
3799b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
3809b50d902SRodney W. Grimes 				af = AF_ISO;
3810761cb29SGarrett Wollman #endif
3829b50d902SRodney W. Grimes 			else {
38351e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3849b50d902SRodney W. Grimes 			}
3859b50d902SRodney W. Grimes 			break;
3869b50d902SRodney W. Grimes 		case 'g':
3879b50d902SRodney W. Grimes 			gflag = 1;
3889b50d902SRodney W. Grimes 			break;
3899b50d902SRodney W. Grimes 		case 'I': {
3909b50d902SRodney W. Grimes 			char *cp;
3919b50d902SRodney W. Grimes 
3929b50d902SRodney W. Grimes 			iflag = 1;
3939b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3949b50d902SRodney W. Grimes 				continue;
3959b50d902SRodney W. Grimes 			unit = atoi(cp);
3969b50d902SRodney W. Grimes 			break;
3979b50d902SRodney W. Grimes 		}
3989b50d902SRodney W. Grimes 		case 'i':
3999b50d902SRodney W. Grimes 			iflag = 1;
4009b50d902SRodney W. Grimes 			break;
401cfa1ca9dSYoshinobu Inoue 		case 'l':
402cfa1ca9dSYoshinobu Inoue 			lflag = 1;
403cfa1ca9dSYoshinobu Inoue 			break;
404ac55add0SGuido van Rooij 		case 'L':
405ac55add0SGuido van Rooij 			Lflag = 1;
406ac55add0SGuido van Rooij 			break;
4079b50d902SRodney W. Grimes 		case 'M':
4089b50d902SRodney W. Grimes 			memf = optarg;
4099b50d902SRodney W. Grimes 			break;
4109b50d902SRodney W. Grimes 		case 'm':
4119b50d902SRodney W. Grimes 			mflag = 1;
4129b50d902SRodney W. Grimes 			break;
4139b50d902SRodney W. Grimes 		case 'N':
4149b50d902SRodney W. Grimes 			nlistf = optarg;
4159b50d902SRodney W. Grimes 			break;
4169b50d902SRodney W. Grimes 		case 'n':
41765ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4189b50d902SRodney W. Grimes 			break;
4199b50d902SRodney W. Grimes 		case 'p':
4209b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
42151e7d42cSGarrett Wollman 				errx(1,
42251e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
42351e7d42cSGarrett Wollman 				     optarg);
4249b50d902SRodney W. Grimes 			}
4259b50d902SRodney W. Grimes 			pflag = 1;
4269b50d902SRodney W. Grimes 			break;
4279b50d902SRodney W. Grimes 		case 'r':
4289b50d902SRodney W. Grimes 			rflag = 1;
4299b50d902SRodney W. Grimes 			break;
4309b50d902SRodney W. Grimes 		case 's':
4319b50d902SRodney W. Grimes 			++sflag;
4329b50d902SRodney W. Grimes 			break;
43365ea0024SAssar Westerlund 		case 'S':
43465ea0024SAssar Westerlund 			numeric_addr = 1;
43565ea0024SAssar Westerlund 			break;
4369b50d902SRodney W. Grimes 		case 't':
4379b50d902SRodney W. Grimes 			tflag = 1;
4389b50d902SRodney W. Grimes 			break;
4399b50d902SRodney W. Grimes 		case 'u':
4409b50d902SRodney W. Grimes 			af = AF_UNIX;
4419b50d902SRodney W. Grimes 			break;
442080b7f49SDag-Erling Smørgrav 		case 'W':
443080b7f49SDag-Erling Smørgrav 			Wflag = 1;
444080b7f49SDag-Erling Smørgrav 			break;
4459b50d902SRodney W. Grimes 		case 'w':
4469b50d902SRodney W. Grimes 			interval = atoi(optarg);
4479b50d902SRodney W. Grimes 			iflag = 1;
4489b50d902SRodney W. Grimes 			break;
4499b50d902SRodney W. Grimes 		case '?':
4509b50d902SRodney W. Grimes 		default:
4519b50d902SRodney W. Grimes 			usage();
4529b50d902SRodney W. Grimes 		}
4539b50d902SRodney W. Grimes 	argv += optind;
4549b50d902SRodney W. Grimes 	argc -= optind;
4559b50d902SRodney W. Grimes 
4569b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4579b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4589b50d902SRodney W. Grimes 	if (*argv) {
4599b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4609b50d902SRodney W. Grimes 			interval = atoi(*argv);
4619b50d902SRodney W. Grimes 			if (interval <= 0)
4629b50d902SRodney W. Grimes 				usage();
4639b50d902SRodney W. Grimes 			++argv;
4649b50d902SRodney W. Grimes 			iflag = 1;
4659b50d902SRodney W. Grimes 		}
4669b50d902SRodney W. Grimes 		if (*argv) {
4679b50d902SRodney W. Grimes 			nlistf = *argv;
4689b50d902SRodney W. Grimes 			if (*++argv)
4699b50d902SRodney W. Grimes 				memf = *argv;
4709b50d902SRodney W. Grimes 		}
4719b50d902SRodney W. Grimes 	}
4729b50d902SRodney W. Grimes #endif
4739b50d902SRodney W. Grimes 
4749b50d902SRodney W. Grimes 	/*
4759b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4769b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4779b50d902SRodney W. Grimes 	 */
4789b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4799b50d902SRodney W. Grimes 		setgid(getgid());
4809b50d902SRodney W. Grimes 
4819b50d902SRodney W. Grimes 	if (mflag) {
482d15c5f56SRuslan Ermilov 		if (memf != NULL) {
483d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
484d15c5f56SRuslan Ermilov 				mbpr(nl[N_MBSTAT].n_value,
485d15c5f56SRuslan Ermilov 				    nl[N_MBTYPES].n_value,
486d15c5f56SRuslan Ermilov 				    nl[N_NMBCLUSTERS].n_value,
487d15c5f56SRuslan Ermilov 				    nl[N_NMBUFS].n_value);
488d15c5f56SRuslan Ermilov 		} else
489d15c5f56SRuslan Ermilov 			mbpr(0, 0, 0, 0);
4909b50d902SRodney W. Grimes 		exit(0);
4919b50d902SRodney W. Grimes 	}
4929b50d902SRodney W. Grimes 	if (pflag) {
493cfa1ca9dSYoshinobu Inoue 		if (iflag && tp->pr_istats) {
494cfa1ca9dSYoshinobu Inoue 			kread(0, 0, 0);
495cfa1ca9dSYoshinobu Inoue 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
496cfa1ca9dSYoshinobu Inoue 			exit(0);
497cfa1ca9dSYoshinobu Inoue 		}
498c6620a13SPoul-Henning Kamp 		if (!tp->pr_stats) {
4999b50d902SRodney W. Grimes 			printf("%s: no stats routine\n", tp->pr_name);
5009b50d902SRodney W. Grimes 			exit(0);
5019b50d902SRodney W. Grimes 		}
502c6620a13SPoul-Henning Kamp 		if (tp->pr_usesysctl) {
503c6620a13SPoul-Henning Kamp 			(*tp->pr_stats)(tp->pr_usesysctl, tp->pr_name);
504c6620a13SPoul-Henning Kamp 		} else {
505c6620a13SPoul-Henning Kamp 			kread(0, 0, 0);
506c6620a13SPoul-Henning Kamp 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
507c6620a13SPoul-Henning Kamp 					tp->pr_name);
508c6620a13SPoul-Henning Kamp 		}
509c6620a13SPoul-Henning Kamp 		exit(0);
510c6620a13SPoul-Henning Kamp 	}
511cc63cd56SPeter Wemm #if 0
5129b50d902SRodney W. Grimes 	/*
5139b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5149b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5159b50d902SRodney W. Grimes 	 */
5169b50d902SRodney W. Grimes 	sethostent(1);
5179b50d902SRodney W. Grimes 	setnetent(1);
518cc63cd56SPeter Wemm #else
519cc63cd56SPeter Wemm 	/*
520cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
521cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
522cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
523cc63cd56SPeter Wemm 	 */
524cc63cd56SPeter Wemm #endif
5259b50d902SRodney W. Grimes 	if (iflag) {
526fc60ab7bSYoshinobu Inoue 		if (sflag && af != AF_UNSPEC)
527cfa1ca9dSYoshinobu Inoue 			goto protostat;
528cfa1ca9dSYoshinobu Inoue 
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)
5369b50d902SRodney W. Grimes 			rt_stats(nl[N_RTSTAT].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   protostat:
564cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
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*/
578e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
579e30e913cSJohn Hay 		kread(0, 0, 0);
580cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
581cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
582e30e913cSJohn Hay 	}
58363bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
58463bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
58563bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5864cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5874cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5884cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
589cbc17e71SGarrett Wollman #ifdef NS
5909b50d902SRodney W. Grimes 	if (af == AF_NS || af == AF_UNSPEC)
5919b50d902SRodney W. Grimes 		for (tp = nsprotox; tp->pr_name; tp++)
5929b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
593cbc17e71SGarrett Wollman #endif
5940761cb29SGarrett Wollman #ifdef ISO
5959b50d902SRodney W. Grimes 	if (af == AF_ISO || af == AF_UNSPEC)
5969b50d902SRodney W. Grimes 		for (tp = isoprotox; tp->pr_name; tp++)
5979b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
5980761cb29SGarrett Wollman #endif
599ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
6004f81ef50SGarrett Wollman 		unixpr();
6019b50d902SRodney W. Grimes 	exit(0);
6029b50d902SRodney W. Grimes }
6039b50d902SRodney W. Grimes 
6049b50d902SRodney W. Grimes /*
6059b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6069b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6079b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6089b50d902SRodney W. Grimes  */
6099b50d902SRodney W. Grimes static void
6109b50d902SRodney W. Grimes printproto(tp, name)
6119b50d902SRodney W. Grimes 	register struct protox *tp;
6129b50d902SRodney W. Grimes 	char *name;
6139b50d902SRodney W. Grimes {
6149b50d902SRodney W. Grimes 	void (*pr)();
6159b50d902SRodney W. Grimes 	u_long off;
6169b50d902SRodney W. Grimes 
6179b50d902SRodney W. Grimes 	if (sflag) {
618cfa1ca9dSYoshinobu Inoue 		if (iflag) {
619cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
620cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
621cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
622cfa1ca9dSYoshinobu Inoue 			return;
623cfa1ca9dSYoshinobu Inoue 		}
624cfa1ca9dSYoshinobu Inoue 		else {
6259b50d902SRodney W. Grimes 			pr = tp->pr_stats;
6264f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
6274f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
628cfa1ca9dSYoshinobu Inoue 		}
6299b50d902SRodney W. Grimes 	} else {
6309b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
6314f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
6324f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6339b50d902SRodney W. Grimes 	}
6349b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
635cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6369b50d902SRodney W. Grimes }
6379b50d902SRodney W. Grimes 
6389b50d902SRodney W. Grimes /*
6399b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6409b50d902SRodney W. Grimes  */
6419b50d902SRodney W. Grimes int
6429b50d902SRodney W. Grimes kread(addr, buf, size)
6439b50d902SRodney W. Grimes 	u_long addr;
6449b50d902SRodney W. Grimes 	char *buf;
6459b50d902SRodney W. Grimes 	int size;
6469b50d902SRodney W. Grimes {
6474f81ef50SGarrett Wollman 	if (kvmd == 0) {
64899453c6aSPoul-Henning Kamp 		/*
64999453c6aSPoul-Henning Kamp 		 * XXX.
65099453c6aSPoul-Henning Kamp 		 */
65199453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
65299453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
65399453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
65499453c6aSPoul-Henning Kamp 				if(nlistf)
65599453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
65699453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
65799453c6aSPoul-Henning Kamp 				else
65899453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
65999453c6aSPoul-Henning Kamp 			}
66099453c6aSPoul-Henning Kamp 
66199453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
66299453c6aSPoul-Henning Kamp 				if(nlistf)
66399453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
66499453c6aSPoul-Henning Kamp 				else
66599453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
66699453c6aSPoul-Henning Kamp 			}
66799453c6aSPoul-Henning Kamp 		} else {
66899453c6aSPoul-Henning Kamp 			warnx("kvm not available");
66999453c6aSPoul-Henning Kamp 			return(-1);
67099453c6aSPoul-Henning Kamp 		}
6714f81ef50SGarrett Wollman 	}
672c6620a13SPoul-Henning Kamp 	if (!buf)
673c6620a13SPoul-Henning Kamp 		return (0);
6749b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
6753aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6769b50d902SRodney W. Grimes 		return (-1);
6779b50d902SRodney W. Grimes 	}
6789b50d902SRodney W. Grimes 	return (0);
6799b50d902SRodney W. Grimes }
6809b50d902SRodney W. Grimes 
6819b50d902SRodney W. Grimes char *
6829b50d902SRodney W. Grimes plural(n)
6839b50d902SRodney W. Grimes 	int n;
6849b50d902SRodney W. Grimes {
6859b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
6869b50d902SRodney W. Grimes }
6879b50d902SRodney W. Grimes 
6889b50d902SRodney W. Grimes char *
6899b50d902SRodney W. Grimes plurales(n)
6909b50d902SRodney W. Grimes 	int n;
6919b50d902SRodney W. Grimes {
6929b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
6939b50d902SRodney W. Grimes }
6949b50d902SRodney W. Grimes 
6959b50d902SRodney W. Grimes /*
6969b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
6979b50d902SRodney W. Grimes  */
6989b50d902SRodney W. Grimes static struct protox *
6999b50d902SRodney W. Grimes knownname(name)
7009b50d902SRodney W. Grimes 	char *name;
7019b50d902SRodney W. Grimes {
7029b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7039b50d902SRodney W. Grimes 
7049b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7059b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7069b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7079b50d902SRodney W. Grimes 				return (tp);
7089b50d902SRodney W. Grimes 	return (NULL);
7099b50d902SRodney W. Grimes }
7109b50d902SRodney W. Grimes 
7119b50d902SRodney W. Grimes /*
7129b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7139b50d902SRodney W. Grimes  */
7149b50d902SRodney W. Grimes static struct protox *
7159b50d902SRodney W. Grimes name2protox(name)
7169b50d902SRodney W. Grimes 	char *name;
7179b50d902SRodney W. Grimes {
7189b50d902SRodney W. Grimes 	struct protox *tp;
7199b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7209b50d902SRodney W. Grimes 	struct protoent *p;
7219b50d902SRodney W. Grimes 
7229b50d902SRodney W. Grimes 	/*
7239b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7249b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7259b50d902SRodney W. Grimes 	 */
726cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7279b50d902SRodney W. Grimes 		return (tp);
7289b50d902SRodney W. Grimes 
7299b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
730cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7319b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7329b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7339b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7349b50d902SRodney W. Grimes 				endprotoent();
7359b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7369b50d902SRodney W. Grimes 			}
7379b50d902SRodney W. Grimes 	}
7389b50d902SRodney W. Grimes 	endprotoent();
7399b50d902SRodney W. Grimes 	return (NULL);
7409b50d902SRodney W. Grimes }
7419b50d902SRodney W. Grimes 
7429b50d902SRodney W. Grimes static void
7439b50d902SRodney W. Grimes usage()
7449b50d902SRodney W. Grimes {
745d15c5f56SRuslan Ermilov 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
746080b7f49SDag-Erling Smørgrav "usage: netstat [-AaLlnW] [-f address_family] [-M core] [-N system]",
74765ea0024SAssar Westerlund "       netstat [-abdgilnrsS] [-f address_family] [-M core] [-N system]",
7485d422d6aSPhilippe Charnier "       netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
749d15c5f56SRuslan Ermilov "       netstat -m [-M core] [-N system]",
7505d422d6aSPhilippe Charnier "       netstat [-M core] [-N system] [-p protocol]");
7519b50d902SRodney W. Grimes 	exit(1);
7529b50d902SRodney W. Grimes }
753