main.c revision 263086
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 263086 2014-03-12 10:45:58Z glebius $");
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	sflag;		/* show protocol statistics */
330int	Wflag;		/* wide display */
331int	Tflag;		/* TCP Information */
332int	xflag;		/* extra information, includes all socket buffer info */
333int	zflag;		/* zero stats */
334
335int	interval;	/* repeat interval for i/f stats */
336
337char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
338int	unit;		/* unit number for above */
339
340int	af;		/* address family */
341int	live;		/* true if we are examining a live system */
342
343int
344main(int argc, char *argv[])
345{
346	struct protox *tp = NULL;  /* for printing cblocks & stats */
347	int ch;
348	int fib = -1;
349	char *endptr;
350
351	af = AF_UNSPEC;
352
353	while ((ch = getopt(argc, argv, "AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz"))
354	    != -1)
355		switch(ch) {
356		case 'A':
357			Aflag = 1;
358			break;
359		case 'a':
360			aflag = 1;
361			break;
362		case 'B':
363			Bflag = 1;
364			break;
365		case 'b':
366			bflag = 1;
367			break;
368		case 'd':
369			dflag = 1;
370			break;
371		case 'F':
372			fib = strtol(optarg, &endptr, 0);
373			if (*endptr != '\0' ||
374			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
375				errx(1, "%s: invalid fib", optarg);
376			break;
377		case 'f':
378			if (strcmp(optarg, "ipx") == 0)
379				af = AF_IPX;
380			else if (strcmp(optarg, "inet") == 0)
381				af = AF_INET;
382#ifdef INET6
383			else if (strcmp(optarg, "inet6") == 0)
384				af = AF_INET6;
385#endif
386#ifdef IPSEC
387			else if (strcmp(optarg, "pfkey") == 0)
388				af = PF_KEY;
389#endif
390			else if (strcmp(optarg, "unix") == 0)
391				af = AF_UNIX;
392			else if (strcmp(optarg, "atalk") == 0)
393				af = AF_APPLETALK;
394#ifdef NETGRAPH
395			else if (strcmp(optarg, "ng") == 0
396			    || strcmp(optarg, "netgraph") == 0)
397				af = AF_NETGRAPH;
398#endif
399			else if (strcmp(optarg, "link") == 0)
400				af = AF_LINK;
401			else {
402				errx(1, "%s: unknown address family", optarg);
403			}
404			break;
405		case 'g':
406			gflag = 1;
407			break;
408		case 'h':
409			hflag = 1;
410			break;
411		case 'I': {
412			char *cp;
413
414			iflag = 1;
415			for (cp = interface = optarg; isalpha(*cp); cp++)
416				continue;
417			unit = atoi(cp);
418			break;
419		}
420		case 'i':
421			iflag = 1;
422			break;
423		case 'L':
424			Lflag = 1;
425			break;
426		case 'M':
427			memf = optarg;
428			break;
429		case 'm':
430			mflag = 1;
431			break;
432		case 'N':
433			nlistf = optarg;
434			break;
435		case 'n':
436			numeric_addr = numeric_port = 1;
437			break;
438		case 'p':
439			if ((tp = name2protox(optarg)) == NULL) {
440				errx(1,
441				     "%s: unknown or uninstrumented protocol",
442				     optarg);
443			}
444			pflag = 1;
445			break;
446		case 'Q':
447			Qflag = 1;
448			break;
449		case 'q':
450			noutputs = atoi(optarg);
451			if (noutputs != 0)
452				noutputs++;
453			break;
454		case 'r':
455			rflag = 1;
456			break;
457		case 's':
458			++sflag;
459			break;
460		case 'S':
461			numeric_addr = 1;
462			break;
463		case 'u':
464			af = AF_UNIX;
465			break;
466		case 'W':
467		case 'l':
468			Wflag = 1;
469			break;
470		case 'w':
471			interval = atoi(optarg);
472			iflag = 1;
473			break;
474		case 'T':
475			Tflag = 1;
476			break;
477		case 'x':
478			xflag = 1;
479			break;
480		case 'z':
481			zflag = 1;
482			break;
483		case '?':
484		default:
485			usage();
486		}
487	argv += optind;
488	argc -= optind;
489
490#define	BACKWARD_COMPATIBILITY
491#ifdef	BACKWARD_COMPATIBILITY
492	if (*argv) {
493		if (isdigit(**argv)) {
494			interval = atoi(*argv);
495			if (interval <= 0)
496				usage();
497			++argv;
498			iflag = 1;
499		}
500		if (*argv) {
501			nlistf = *argv;
502			if (*++argv)
503				memf = *argv;
504		}
505	}
506#endif
507
508	/*
509	 * Discard setgid privileges if not the running kernel so that bad
510	 * guys can't print interesting stuff from kernel memory.
511	 */
512	live = (nlistf == NULL && memf == NULL);
513	if (!live)
514		setgid(getgid());
515
516	if (xflag && Tflag)
517		errx(1, "-x and -T are incompatible, pick one.");
518
519	if (Bflag) {
520		if (!live)
521			usage();
522		bpf_stats(interface);
523		exit(0);
524	}
525	if (mflag) {
526		if (!live) {
527			if (kread(0, NULL, 0) == 0)
528				mbpr(kvmd, nl[N_SFSTAT].n_value);
529		} else
530			mbpr(NULL, 0);
531		exit(0);
532	}
533	if (Qflag) {
534		if (!live) {
535			if (kread(0, NULL, 0) == 0)
536				netisr_stats(kvmd);
537		} else
538			netisr_stats(NULL);
539		exit(0);
540	}
541#if 0
542	/*
543	 * Keep file descriptors open to avoid overhead
544	 * of open/close on each call to get* routines.
545	 */
546	sethostent(1);
547	setnetent(1);
548#else
549	/*
550	 * This does not make sense any more with DNS being default over
551	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
552	 * used for the queries, which is slower.
553	 */
554#endif
555	kread(0, NULL, 0);
556	if (iflag && !sflag) {
557		intpr(interval, NULL);
558		exit(0);
559	}
560	if (rflag) {
561		if (sflag) {
562			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
563			flowtable_stats();
564		} else
565			routepr(nl[N_RTREE].n_value, fib);
566		exit(0);
567	}
568	if (gflag) {
569		if (sflag) {
570			if (af == AF_INET || af == AF_UNSPEC)
571				mrt_stats(nl[N_MRTSTAT].n_value);
572#ifdef INET6
573			if (af == AF_INET6 || af == AF_UNSPEC)
574				mrt6_stats(nl[N_MRT6STAT].n_value);
575#endif
576		} else {
577			if (af == AF_INET || af == AF_UNSPEC)
578				mroutepr(nl[N_MFCHASHTBL].n_value,
579					 nl[N_MFCTABLESIZE].n_value,
580					 nl[N_VIFTABLE].n_value);
581#ifdef INET6
582			if (af == AF_INET6 || af == AF_UNSPEC)
583				mroute6pr(nl[N_MF6CTABLE].n_value,
584					  nl[N_MIF6TABLE].n_value);
585#endif
586		}
587		exit(0);
588	}
589
590	if (tp) {
591		printproto(tp, tp->pr_name);
592		exit(0);
593	}
594	if (af == AF_INET || af == AF_UNSPEC)
595		for (tp = protox; tp->pr_name; tp++)
596			printproto(tp, tp->pr_name);
597#ifdef INET6
598	if (af == AF_INET6 || af == AF_UNSPEC)
599		for (tp = ip6protox; tp->pr_name; tp++)
600			printproto(tp, tp->pr_name);
601#endif /*INET6*/
602#ifdef IPSEC
603	if (af == PF_KEY || af == AF_UNSPEC)
604		for (tp = pfkeyprotox; tp->pr_name; tp++)
605			printproto(tp, tp->pr_name);
606#endif /*IPSEC*/
607#ifdef IPX
608	if (af == AF_IPX || af == AF_UNSPEC) {
609		for (tp = ipxprotox; tp->pr_name; tp++)
610			printproto(tp, tp->pr_name);
611	}
612#endif /* IPX */
613	if (af == AF_APPLETALK || af == AF_UNSPEC)
614		for (tp = atalkprotox; tp->pr_name; tp++)
615			printproto(tp, tp->pr_name);
616#ifdef NETGRAPH
617	if (af == AF_NETGRAPH || af == AF_UNSPEC)
618		for (tp = netgraphprotox; tp->pr_name; tp++)
619			printproto(tp, tp->pr_name);
620#endif /* NETGRAPH */
621	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
622		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
623		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
624		    nl[N_UNP_SPHEAD].n_value);
625	exit(0);
626}
627
628/*
629 * Print out protocol statistics or control blocks (per sflag).
630 * If the interface was not specifically requested, and the symbol
631 * is not in the namelist, ignore this one.
632 */
633static void
634printproto(struct protox *tp, const char *name)
635{
636	void (*pr)(u_long, const char *, int, int);
637	u_long off;
638
639	if (sflag) {
640		if (iflag) {
641			if (tp->pr_istats)
642				intpr(interval, tp->pr_istats);
643			else if (pflag)
644				printf("%s: no per-interface stats routine\n",
645				    tp->pr_name);
646			return;
647		} else {
648			pr = tp->pr_stats;
649			if (!pr) {
650				if (pflag)
651					printf("%s: no stats routine\n",
652					    tp->pr_name);
653				return;
654			}
655			if (tp->pr_usesysctl && live)
656				off = 0;
657			else if (tp->pr_sindex < 0) {
658				if (pflag)
659					printf(
660				    "%s: stats routine doesn't work on cores\n",
661					    tp->pr_name);
662				return;
663			} else
664				off = nl[tp->pr_sindex].n_value;
665		}
666	} else {
667		pr = tp->pr_cblocks;
668		if (!pr) {
669			if (pflag)
670				printf("%s: no PCB routine\n", tp->pr_name);
671			return;
672		}
673		if (tp->pr_usesysctl && live)
674			off = 0;
675		else if (tp->pr_index < 0) {
676			if (pflag)
677				printf(
678				    "%s: PCB routine doesn't work on cores\n",
679				    tp->pr_name);
680			return;
681		} else
682			off = nl[tp->pr_index].n_value;
683	}
684	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
685	    af != AF_UNSPEC))
686		(*pr)(off, name, af, tp->pr_protocol);
687}
688
689static int
690kvmd_init(void)
691{
692	char errbuf[_POSIX2_LINE_MAX];
693
694	if (kvmd != NULL)
695		return (0);
696
697	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
698	setgid(getgid());
699
700	if (kvmd == NULL) {
701		warnx("kvm not available: %s", errbuf);
702		return (-1);
703	}
704
705	if (kvm_nlist(kvmd, nl) < 0) {
706		if (nlistf)
707			errx(1, "%s: kvm_nlist: %s", nlistf,
708			     kvm_geterr(kvmd));
709		else
710			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
711	}
712
713	if (nl[0].n_type == 0) {
714		if (nlistf)
715			errx(1, "%s: no namelist", nlistf);
716		else
717			errx(1, "no namelist");
718	}
719
720	return (0);
721}
722
723/*
724 * Read kernel memory, return 0 on success.
725 */
726int
727kread(u_long addr, void *buf, size_t size)
728{
729
730	if (kvmd_init() < 0)
731		return (-1);
732
733	if (!buf)
734		return (0);
735	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
736		warnx("%s", kvm_geterr(kvmd));
737		return (-1);
738	}
739	return (0);
740}
741
742/*
743 * Read an array of N counters in kernel memory into array of N uint64_t's.
744 */
745int
746kread_counters(u_long addr, void *buf, size_t size)
747{
748	uint64_t *c = buf;
749
750	if (kvmd_init() < 0)
751		return (-1);
752
753	if (kread(addr, buf, size) < 0)
754		return (-1);
755
756	while (size != 0) {
757		*c = kvm_counter_u64_fetch(kvmd, *c);
758		size -= sizeof(*c);
759		c++;
760	}
761	return (0);
762}
763
764const char *
765plural(uintmax_t n)
766{
767	return (n != 1 ? "s" : "");
768}
769
770const char *
771plurales(uintmax_t n)
772{
773	return (n != 1 ? "es" : "");
774}
775
776const char *
777pluralies(uintmax_t n)
778{
779	return (n != 1 ? "ies" : "y");
780}
781
782/*
783 * Find the protox for the given "well-known" name.
784 */
785static struct protox *
786knownname(const char *name)
787{
788	struct protox **tpp, *tp;
789
790	for (tpp = protoprotox; *tpp; tpp++)
791		for (tp = *tpp; tp->pr_name; tp++)
792			if (strcmp(tp->pr_name, name) == 0)
793				return (tp);
794	return (NULL);
795}
796
797/*
798 * Find the protox corresponding to name.
799 */
800static struct protox *
801name2protox(const char *name)
802{
803	struct protox *tp;
804	char **alias;			/* alias from p->aliases */
805	struct protoent *p;
806
807	/*
808	 * Try to find the name in the list of "well-known" names. If that
809	 * fails, check if name is an alias for an Internet protocol.
810	 */
811	if ((tp = knownname(name)) != NULL)
812		return (tp);
813
814	setprotoent(1);			/* make protocol lookup cheaper */
815	while ((p = getprotoent()) != NULL) {
816		/* assert: name not same as p->name */
817		for (alias = p->p_aliases; *alias; alias++)
818			if (strcmp(name, *alias) == 0) {
819				endprotoent();
820				return (knownname(p->p_name));
821			}
822	}
823	endprotoent();
824	return (NULL);
825}
826
827static void
828usage(void)
829{
830	(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",
831"usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n"
832"               [-M core] [-N system]",
833"       netstat -i | -I interface [-abdhnW] [-f address_family]\n"
834"               [-M core] [-N system]",
835"       netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
836"       netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
837"               [-M core] [-N system]",
838"       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
839"               [-M core] [-N system]",
840"       netstat -m [-M core] [-N system]",
841"       netstat -B [-I interface]",
842"       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
843"       netstat -rs [-s] [-M core] [-N system]",
844"       netstat -g [-W] [-f address_family] [-M core] [-N system]",
845"       netstat -gs [-s] [-f address_family] [-M core] [-N system]",
846"       netstat -Q");
847	exit(1);
848}
849