main.c revision 286099
1/*-
2 * Copyright (c) 1983, 1988, 1993
3 *	Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31char const copyright[] =
32"@(#) Copyright (c) 1983, 1988, 1993\n\
33	Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#if 0
37#ifndef lint
38static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
39#endif /* not lint */
40#endif
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: stable/10/usr.bin/netstat/main.c 286099 2015-07-31 00:21:40Z markj $");
44
45#include <sys/param.h>
46#include <sys/file.h>
47#include <sys/protosw.h>
48#include <sys/socket.h>
49#include <sys/socketvar.h>
50
51#include <netinet/in.h>
52
53#ifdef NETGRAPH
54#include <netgraph/ng_socket.h>
55#endif
56
57#include <ctype.h>
58#include <err.h>
59#include <errno.h>
60#include <kvm.h>
61#include <limits.h>
62#include <netdb.h>
63#include <nlist.h>
64#include <paths.h>
65#include <stdint.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <unistd.h>
70#include "netstat.h"
71
72static struct nlist nl[] = {
73#define	N_IFNET		0
74	{ .n_name = "_ifnet" },		/* XXXGL: can be deleted */
75#define	N_RTSTAT	1
76	{ .n_name = "_rtstat" },
77#define	N_RTREE		2
78	{ .n_name = "_rt_tables"},
79#define	N_MRTSTAT	3
80	{ .n_name = "_mrtstat" },
81#define	N_MFCHASHTBL	4
82	{ .n_name = "_mfchashtbl" },
83#define	N_VIFTABLE	5
84	{ .n_name = "_viftable" },
85#define	N_IPX		6
86	{ .n_name = "_ipxpcb_list"},
87#define	N_IPXSTAT	7
88	{ .n_name = "_ipxstat"},
89#define	N_SPXSTAT	8
90	{ .n_name = "_spx_istat"},
91#define	N_DDPSTAT	9
92	{ .n_name = "_ddpstat"},
93#define	N_DDPCB		10
94	{ .n_name = "_ddpcb"},
95#define	N_NGSOCKS	11
96	{ .n_name = "_ngsocklist"},
97#define	N_IP6STAT	12
98	{ .n_name = "_ip6stat" },
99#define	N_ICMP6STAT	13
100	{ .n_name = "_icmp6stat" },
101#define	N_IPSECSTAT	14
102	{ .n_name = "_ipsec4stat" },
103#define	N_IPSEC6STAT	15
104	{ .n_name = "_ipsec6stat" },
105#define	N_PIM6STAT	16
106	{ .n_name = "_pim6stat" },
107#define	N_MRT6STAT	17
108	{ .n_name = "_mrt6stat" },
109#define	N_MF6CTABLE	18
110	{ .n_name = "_mf6ctable" },
111#define	N_MIF6TABLE	19
112	{ .n_name = "_mif6table" },
113#define	N_PFKEYSTAT	20
114	{ .n_name = "_pfkeystat" },
115#define	N_RTTRASH	21
116	{ .n_name = "_rttrash" },
117#define	N_CARPSTAT	22
118	{ .n_name = "_carpstats" },
119#define	N_PFSYNCSTAT	23
120	{ .n_name = "_pfsyncstats" },
121#define	N_AHSTAT	24
122	{ .n_name = "_ahstat" },
123#define	N_ESPSTAT	25
124	{ .n_name = "_espstat" },
125#define	N_IPCOMPSTAT	26
126	{ .n_name = "_ipcompstat" },
127#define	N_TCPSTAT	27
128	{ .n_name = "_tcpstat" },
129#define	N_UDPSTAT	28
130	{ .n_name = "_udpstat" },
131#define	N_IPSTAT	29
132	{ .n_name = "_ipstat" },
133#define	N_ICMPSTAT	30
134	{ .n_name = "_icmpstat" },
135#define	N_IGMPSTAT	31
136	{ .n_name = "_igmpstat" },
137#define	N_PIMSTAT	32
138	{ .n_name = "_pimstat" },
139#define	N_TCBINFO	33
140	{ .n_name = "_tcbinfo" },
141#define	N_UDBINFO	34
142	{ .n_name = "_udbinfo" },
143#define	N_DIVCBINFO	35
144	{ .n_name = "_divcbinfo" },
145#define	N_RIPCBINFO	36
146	{ .n_name = "_ripcbinfo" },
147#define	N_UNP_COUNT	37
148	{ .n_name = "_unp_count" },
149#define	N_UNP_GENCNT	38
150	{ .n_name = "_unp_gencnt" },
151#define	N_UNP_DHEAD	39
152	{ .n_name = "_unp_dhead" },
153#define	N_UNP_SHEAD	40
154	{ .n_name = "_unp_shead" },
155#define	N_RIP6STAT	41
156	{ .n_name = "_rip6stat" },
157#define	N_SCTPSTAT	42
158	{ .n_name = "_sctpstat" },
159#define	N_MFCTABLESIZE	43
160	{ .n_name = "_mfctablesize" },
161#define	N_ARPSTAT       44
162	{ .n_name = "_arpstat" },
163#define	N_UNP_SPHEAD	45
164	{ .n_name = "unp_sphead" },
165#define	N_SFSTAT	46
166	{ .n_name = "_sfstat"},
167	{ .n_name = NULL },
168};
169
170struct protox {
171	int	pr_index;		/* index into nlist of cb head */
172	int	pr_sindex;		/* index into nlist of stat block */
173	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
174	void	(*pr_cblocks)(u_long, const char *, int, int);
175					/* control blocks printing routine */
176	void	(*pr_stats)(u_long, const char *, int, int);
177					/* statistics printing routine */
178	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
179	const char	*pr_name;		/* well-known name */
180	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
181	int	pr_protocol;
182} protox[] = {
183	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
184	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
185	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
186	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
187#ifdef SCTP
188	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
189	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
190#endif
191#ifdef SDP
192	{ -1,		-1,		1,	protopr,
193	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
194#endif
195	{ N_DIVCBINFO,	-1,		1,	protopr,
196	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
197	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
198	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
199	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
200	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
201	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
202	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
203#ifdef IPSEC
204	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
205	  ipsec_stats,	NULL,		"ipsec", 0,	0},
206	{ -1,		N_AHSTAT,	1,	NULL,
207	  ah_stats,	NULL,		"ah",	0,	0},
208	{ -1,		N_ESPSTAT,	1,	NULL,
209	  esp_stats,	NULL,		"esp",	0,	0},
210	{ -1,		N_IPCOMPSTAT,	1,	NULL,
211	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
212#endif
213	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
214	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
215	{ -1,		N_CARPSTAT,	1,	NULL,
216	  carp_stats,	NULL,		"carp",	1,	0 },
217#ifdef PF
218	{ -1,		N_PFSYNCSTAT,	1,	NULL,
219	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
220#endif
221	{ -1,		N_ARPSTAT,	1,	NULL,
222	  arp_stats,	NULL,		"arp", 1,	0 },
223	{ -1,		-1,		0,	NULL,
224	  NULL,		NULL,		NULL,	0,	0 }
225};
226
227#ifdef INET6
228struct protox ip6protox[] = {
229	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
230	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
231	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
232	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
233	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
234	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
235	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
236	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
237#ifdef SDP
238	{ -1,		-1,		1,	protopr,
239	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
240#endif
241#ifdef IPSEC
242	{ -1,		N_IPSEC6STAT,	1,	NULL,
243	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
244#endif
245#ifdef notyet
246	{ -1,		N_PIM6STAT,	1,	NULL,
247	  pim6_stats,	NULL,		"pim6",	1,	0 },
248#endif
249	{ -1,		N_RIP6STAT,	1,	NULL,
250	  rip6_stats,	NULL,		"rip6",	1,	0 },
251	{ -1,		-1,		0,	NULL,
252	  NULL,		NULL,		NULL,	0,	0 }
253};
254#endif /*INET6*/
255
256#ifdef IPSEC
257struct protox pfkeyprotox[] = {
258	{ -1,		N_PFKEYSTAT,	1,	NULL,
259	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
260	{ -1,		-1,		0,	NULL,
261	  NULL,		NULL,		NULL,	0,	0 }
262};
263#endif
264
265struct protox atalkprotox[] = {
266	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
267	  ddp_stats,	NULL,		"ddp",	0,	0 },
268	{ -1,		-1,		0,	NULL,
269	  NULL,		NULL,		NULL,	0,	0 }
270};
271#ifdef NETGRAPH
272struct protox netgraphprotox[] = {
273	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
274	  NULL,		NULL,		"ctrl",	0,	0 },
275	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
276	  NULL,		NULL,		"data",	0,	0 },
277	{ -1,		-1,		0,	NULL,
278	  NULL,		NULL,		NULL,	0,	0 }
279};
280#endif
281#ifdef IPX
282struct protox ipxprotox[] = {
283	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
284	  ipx_stats,	NULL,		"ipx",	0,	0 },
285	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
286	  spx_stats,	NULL,		"spx",	0,	0 },
287	{ -1,		-1,		0,	NULL,
288	  NULL,		NULL,		0,	0,	0 }
289};
290#endif
291
292struct protox *protoprotox[] = {
293					 protox,
294#ifdef INET6
295					 ip6protox,
296#endif
297#ifdef IPSEC
298					 pfkeyprotox,
299#endif
300#ifdef IPX
301					 ipxprotox,
302#endif
303					 atalkprotox, NULL };
304
305static void printproto(struct protox *, const char *);
306static void usage(void);
307static struct protox *name2protox(const char *);
308static struct protox *knownname(const char *);
309
310static kvm_t *kvmd;
311static char *nlistf = NULL, *memf = NULL;
312
313int	Aflag;		/* show addresses of protocol control block */
314int	aflag;		/* show all sockets (including servers) */
315int	Bflag;		/* show information about bpf consumers */
316int	bflag;		/* show i/f total bytes in/out */
317int	dflag;		/* show i/f dropped packets */
318int	gflag;		/* show group (multicast) routing or stats */
319int	hflag;		/* show counters in human readable format */
320int	iflag;		/* show interfaces */
321int	Lflag;		/* show size of listen queues */
322int	mflag;		/* show memory stats */
323int	noutputs = 0;	/* how much outputs before we exit */
324int	numeric_addr;	/* show addresses numerically */
325int	numeric_port;	/* show ports numerically */
326static int pflag;	/* show given protocol */
327int	Qflag;		/* show netisr information */
328int	rflag;		/* show routing tables (or routing stats) */
329int	Rflag;		/* show flow / RSS statistics */
330int	sflag;		/* show protocol statistics */
331int	Wflag;		/* wide display */
332int	Tflag;		/* TCP Information */
333int	xflag;		/* extra information, includes all socket buffer info */
334int	zflag;		/* zero stats */
335
336int	interval;	/* repeat interval for i/f stats */
337
338char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
339int	unit;		/* unit number for above */
340
341int	af;		/* address family */
342int	live;		/* true if we are examining a live system */
343
344int
345main(int argc, char *argv[])
346{
347	struct protox *tp = NULL;  /* for printing cblocks & stats */
348	int ch;
349	int fib = -1;
350	char *endptr;
351
352	af = AF_UNSPEC;
353
354	while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
355	    != -1)
356		switch(ch) {
357		case '4':
358#ifdef INET
359			af = AF_INET;
360#else
361			errx(1, "IPv4 support is not compiled in");
362#endif
363			break;
364		case '6':
365#ifdef INET6
366			af = AF_INET6;
367#else
368			errx(1, "IPv6 support is not compiled in");
369#endif
370			break;
371		case 'A':
372			Aflag = 1;
373			break;
374		case 'a':
375			aflag = 1;
376			break;
377		case 'B':
378			Bflag = 1;
379			break;
380		case 'b':
381			bflag = 1;
382			break;
383		case 'd':
384			dflag = 1;
385			break;
386		case 'F':
387			fib = strtol(optarg, &endptr, 0);
388			if (*endptr != '\0' ||
389			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
390				errx(1, "%s: invalid fib", optarg);
391			break;
392		case 'f':
393			if (strcmp(optarg, "ipx") == 0)
394				af = AF_IPX;
395			else if (strcmp(optarg, "inet") == 0)
396				af = AF_INET;
397#ifdef INET6
398			else if (strcmp(optarg, "inet6") == 0)
399				af = AF_INET6;
400#endif
401#ifdef IPSEC
402			else if (strcmp(optarg, "pfkey") == 0)
403				af = PF_KEY;
404#endif
405			else if (strcmp(optarg, "unix") == 0)
406				af = AF_UNIX;
407			else if (strcmp(optarg, "atalk") == 0)
408				af = AF_APPLETALK;
409#ifdef NETGRAPH
410			else if (strcmp(optarg, "ng") == 0
411			    || strcmp(optarg, "netgraph") == 0)
412				af = AF_NETGRAPH;
413#endif
414			else if (strcmp(optarg, "link") == 0)
415				af = AF_LINK;
416			else {
417				errx(1, "%s: unknown address family", optarg);
418			}
419			break;
420		case 'g':
421			gflag = 1;
422			break;
423		case 'h':
424			hflag = 1;
425			break;
426		case 'I': {
427			char *cp;
428
429			iflag = 1;
430			for (cp = interface = optarg; isalpha(*cp); cp++)
431				continue;
432			unit = atoi(cp);
433			break;
434		}
435		case 'i':
436			iflag = 1;
437			break;
438		case 'L':
439			Lflag = 1;
440			break;
441		case 'M':
442			memf = optarg;
443			break;
444		case 'm':
445			mflag = 1;
446			break;
447		case 'N':
448			nlistf = optarg;
449			break;
450		case 'n':
451			numeric_addr = numeric_port = 1;
452			break;
453		case 'p':
454			if ((tp = name2protox(optarg)) == NULL) {
455				errx(1,
456				     "%s: unknown or uninstrumented protocol",
457				     optarg);
458			}
459			pflag = 1;
460			break;
461		case 'Q':
462			Qflag = 1;
463			break;
464		case 'q':
465			noutputs = atoi(optarg);
466			if (noutputs != 0)
467				noutputs++;
468			break;
469		case 'r':
470			rflag = 1;
471			break;
472		case 'R':
473			Rflag = 1;
474			break;
475		case 's':
476			++sflag;
477			break;
478		case 'S':
479			numeric_addr = 1;
480			break;
481		case 'u':
482			af = AF_UNIX;
483			break;
484		case 'W':
485		case 'l':
486			Wflag = 1;
487			break;
488		case 'w':
489			interval = atoi(optarg);
490			iflag = 1;
491			break;
492		case 'T':
493			Tflag = 1;
494			break;
495		case 'x':
496			xflag = 1;
497			break;
498		case 'z':
499			zflag = 1;
500			break;
501		case '?':
502		default:
503			usage();
504		}
505	argv += optind;
506	argc -= optind;
507
508#define	BACKWARD_COMPATIBILITY
509#ifdef	BACKWARD_COMPATIBILITY
510	if (*argv) {
511		if (isdigit(**argv)) {
512			interval = atoi(*argv);
513			if (interval <= 0)
514				usage();
515			++argv;
516			iflag = 1;
517		}
518		if (*argv) {
519			nlistf = *argv;
520			if (*++argv)
521				memf = *argv;
522		}
523	}
524#endif
525
526	/*
527	 * Discard setgid privileges if not the running kernel so that bad
528	 * guys can't print interesting stuff from kernel memory.
529	 */
530	live = (nlistf == NULL && memf == NULL);
531	if (!live)
532		setgid(getgid());
533
534	if (xflag && Tflag)
535		errx(1, "-x and -T are incompatible, pick one.");
536
537	if (Bflag) {
538		if (!live)
539			usage();
540		bpf_stats(interface);
541		exit(0);
542	}
543	if (mflag) {
544		if (!live) {
545			if (kread(0, NULL, 0) == 0)
546				mbpr(kvmd, nl[N_SFSTAT].n_value);
547		} else
548			mbpr(NULL, 0);
549		exit(0);
550	}
551	if (Qflag) {
552		if (!live) {
553			if (kread(0, NULL, 0) == 0)
554				netisr_stats(kvmd);
555		} else
556			netisr_stats(NULL);
557		exit(0);
558	}
559#if 0
560	/*
561	 * Keep file descriptors open to avoid overhead
562	 * of open/close on each call to get* routines.
563	 */
564	sethostent(1);
565	setnetent(1);
566#else
567	/*
568	 * This does not make sense any more with DNS being default over
569	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
570	 * used for the queries, which is slower.
571	 */
572#endif
573	if (iflag && !sflag) {
574		intpr(interval, NULL, af);
575		exit(0);
576	}
577	if (rflag) {
578		if (sflag) {
579			rt_stats();
580			flowtable_stats();
581		} else
582			routepr(fib, af);
583		exit(0);
584	}
585
586	if (gflag) {
587		if (sflag) {
588			if (af == AF_INET || af == AF_UNSPEC)
589				mrt_stats();
590#ifdef INET6
591			if (af == AF_INET6 || af == AF_UNSPEC)
592				mrt6_stats();
593#endif
594		} else {
595			if (af == AF_INET || af == AF_UNSPEC)
596				mroutepr();
597#ifdef INET6
598			if (af == AF_INET6 || af == AF_UNSPEC)
599				mroute6pr();
600#endif
601		}
602		exit(0);
603	}
604
605	/* Load all necessary kvm symbols */
606	kresolve_list(nl);
607
608	if (tp) {
609		printproto(tp, tp->pr_name);
610		exit(0);
611	}
612	if (af == AF_INET || af == AF_UNSPEC)
613		for (tp = protox; tp->pr_name; tp++)
614			printproto(tp, tp->pr_name);
615#ifdef INET6
616	if (af == AF_INET6 || af == AF_UNSPEC)
617		for (tp = ip6protox; tp->pr_name; tp++)
618			printproto(tp, tp->pr_name);
619#endif /*INET6*/
620#ifdef IPSEC
621	if (af == PF_KEY || af == AF_UNSPEC)
622		for (tp = pfkeyprotox; tp->pr_name; tp++)
623			printproto(tp, tp->pr_name);
624#endif /*IPSEC*/
625#ifdef IPX
626	if (af == AF_IPX || af == AF_UNSPEC) {
627		for (tp = ipxprotox; tp->pr_name; tp++)
628			printproto(tp, tp->pr_name);
629	}
630#endif /* IPX */
631	if (af == AF_APPLETALK || af == AF_UNSPEC)
632		for (tp = atalkprotox; tp->pr_name; tp++)
633			printproto(tp, tp->pr_name);
634#ifdef NETGRAPH
635	if (af == AF_NETGRAPH || af == AF_UNSPEC)
636		for (tp = netgraphprotox; tp->pr_name; tp++)
637			printproto(tp, tp->pr_name);
638#endif /* NETGRAPH */
639	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
640		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
641		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
642		    nl[N_UNP_SPHEAD].n_value);
643	exit(0);
644}
645
646/*
647 * Print out protocol statistics or control blocks (per sflag).
648 * If the interface was not specifically requested, and the symbol
649 * is not in the namelist, ignore this one.
650 */
651static void
652printproto(struct protox *tp, const char *name)
653{
654	void (*pr)(u_long, const char *, int, int);
655	u_long off;
656
657	if (sflag) {
658		if (iflag) {
659			if (tp->pr_istats)
660				intpr(interval, tp->pr_istats, af);
661			else if (pflag)
662				printf("%s: no per-interface stats routine\n",
663				    tp->pr_name);
664			return;
665		} else {
666			pr = tp->pr_stats;
667			if (!pr) {
668				if (pflag)
669					printf("%s: no stats routine\n",
670					    tp->pr_name);
671				return;
672			}
673			if (tp->pr_usesysctl && live)
674				off = 0;
675			else if (tp->pr_sindex < 0) {
676				if (pflag)
677					printf(
678				    "%s: stats routine doesn't work on cores\n",
679					    tp->pr_name);
680				return;
681			} else
682				off = nl[tp->pr_sindex].n_value;
683		}
684	} else {
685		pr = tp->pr_cblocks;
686		if (!pr) {
687			if (pflag)
688				printf("%s: no PCB routine\n", tp->pr_name);
689			return;
690		}
691		if (tp->pr_usesysctl && live)
692			off = 0;
693		else if (tp->pr_index < 0) {
694			if (pflag)
695				printf(
696				    "%s: PCB routine doesn't work on cores\n",
697				    tp->pr_name);
698			return;
699		} else
700			off = nl[tp->pr_index].n_value;
701	}
702	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
703	    af != AF_UNSPEC))
704		(*pr)(off, name, af, tp->pr_protocol);
705}
706
707static int
708kvmd_init(void)
709{
710	char errbuf[_POSIX2_LINE_MAX];
711
712	if (kvmd != NULL)
713		return (0);
714
715	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
716	setgid(getgid());
717
718	if (kvmd == NULL) {
719		warnx("kvm not available: %s", errbuf);
720		return (-1);
721	}
722
723	return (0);
724}
725
726/*
727 * Resolve symbol list, return 0 on success.
728 */
729int
730kresolve_list(struct nlist *_nl)
731{
732
733	if ((kvmd == NULL) && (kvmd_init() != 0))
734		return (-1);
735
736	if (_nl[0].n_type != 0)
737		return (0);
738
739	if (kvm_nlist(kvmd, _nl) < 0) {
740		if (nlistf)
741			errx(1, "%s: kvm_nlist: %s", nlistf,
742			     kvm_geterr(kvmd));
743		else
744			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
745	}
746
747	return (0);
748}
749
750/*
751 * Read kernel memory, return 0 on success.
752 */
753int
754kread(u_long addr, void *buf, size_t size)
755{
756
757	if (kvmd_init() < 0)
758		return (-1);
759
760	if (!buf)
761		return (0);
762	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
763		warnx("%s", kvm_geterr(kvmd));
764		return (-1);
765	}
766	return (0);
767}
768
769/*
770 * Read single counter(9).
771 */
772uint64_t
773kread_counter(u_long addr)
774{
775
776	if (kvmd_init() < 0)
777		return (-1);
778
779	return (kvm_counter_u64_fetch(kvmd, addr));
780}
781
782/*
783 * Read an array of N counters in kernel memory into array of N uint64_t's.
784 */
785int
786kread_counters(u_long addr, void *buf, size_t size)
787{
788	uint64_t *c;
789	u_long *counters;
790	size_t i, n;
791
792	if (kvmd_init() < 0)
793		return (-1);
794
795	if (size % sizeof(uint64_t) != 0) {
796		warnx("kread_counters: invalid counter set size");
797		return (-1);
798	}
799
800	n = size / sizeof(uint64_t);
801	if ((counters = malloc(n * sizeof(u_long))) == NULL)
802		err(-1, "malloc");
803	if (kread(addr, counters, n * sizeof(u_long)) < 0) {
804		free(counters);
805		return (-1);
806	}
807
808	c = buf;
809	for (i = 0; i < n; i++)
810		c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
811
812	free(counters);
813	return (0);
814}
815
816const char *
817plural(uintmax_t n)
818{
819	return (n != 1 ? "s" : "");
820}
821
822const char *
823plurales(uintmax_t n)
824{
825	return (n != 1 ? "es" : "");
826}
827
828const char *
829pluralies(uintmax_t n)
830{
831	return (n != 1 ? "ies" : "y");
832}
833
834/*
835 * Find the protox for the given "well-known" name.
836 */
837static struct protox *
838knownname(const char *name)
839{
840	struct protox **tpp, *tp;
841
842	for (tpp = protoprotox; *tpp; tpp++)
843		for (tp = *tpp; tp->pr_name; tp++)
844			if (strcmp(tp->pr_name, name) == 0)
845				return (tp);
846	return (NULL);
847}
848
849/*
850 * Find the protox corresponding to name.
851 */
852static struct protox *
853name2protox(const char *name)
854{
855	struct protox *tp;
856	char **alias;			/* alias from p->aliases */
857	struct protoent *p;
858
859	/*
860	 * Try to find the name in the list of "well-known" names. If that
861	 * fails, check if name is an alias for an Internet protocol.
862	 */
863	if ((tp = knownname(name)) != NULL)
864		return (tp);
865
866	setprotoent(1);			/* make protocol lookup cheaper */
867	while ((p = getprotoent()) != NULL) {
868		/* assert: name not same as p->name */
869		for (alias = p->p_aliases; *alias; alias++)
870			if (strcmp(name, *alias) == 0) {
871				endprotoent();
872				return (knownname(p->p_name));
873			}
874	}
875	endprotoent();
876	return (NULL);
877}
878
879static void
880usage(void)
881{
882	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
883"usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
884"               [-M core] [-N system]",
885"       netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
886"               [-M core] [-N system]",
887"       netstat -w wait [-I interface] [-46d] [-M core] [-N system] [-q howmany]",
888"       netstat -s [-s] [-46z] [-f protocol_family | -p protocol]\n"
889"               [-M core] [-N system]",
890"       netstat -i | -I interface [-46s] [-f protocol_family | -p protocol]\n"
891"               [-M core] [-N system]",
892"       netstat -m [-M core] [-N system]",
893"       netstat -B [-I interface]",
894"       netstat -r [-46AanW] [-f address_family] [-M core] [-N system]",
895"       netstat -rs [-s] [-M core] [-N system]",
896"       netstat -g [-46W] [-f address_family] [-M core] [-N system]",
897"       netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
898"       netstat -Q");
899	exit(1);
900}
901