main.c revision 216370
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: head/usr.bin/netstat/main.c 216370 2010-12-11 08:32:16Z joel $");
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" },
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_MBSTAT	21
116	{ .n_name = "_mbstat" },
117#define	N_MBTYPES	22
118	{ .n_name = "_mbtypes" },
119#define	N_NMBCLUSTERS	23
120	{ .n_name = "_nmbclusters" },
121#define	N_NMBUFS	24
122	{ .n_name = "_nmbufs" },
123#define	N_MBHI		25
124	{ .n_name = "_mbuf_hiwm" },
125#define	N_CLHI		26
126	{ .n_name = "_clust_hiwm" },
127#define	N_NCPUS		27
128	{ .n_name = "_smp_cpus" },
129#define	N_PAGESZ	28
130	{ .n_name = "_pagesize" },
131#define	N_MBPSTAT	29
132	{ .n_name = "_mb_statpcpu" },
133#define	N_RTTRASH	30
134	{ .n_name = "_rttrash" },
135#define	N_MBLO		31
136	{ .n_name = "_mbuf_lowm" },
137#define	N_CLLO		32
138	{ .n_name = "_clust_lowm" },
139#define	N_CARPSTAT	33
140	{ .n_name = "_carpstats" },
141#define	N_PFSYNCSTAT	34
142	{ .n_name = "_pfsyncstats" },
143#define	N_AHSTAT	35
144	{ .n_name = "_ahstat" },
145#define	N_ESPSTAT	36
146	{ .n_name = "_espstat" },
147#define	N_IPCOMPSTAT	37
148	{ .n_name = "_ipcompstat" },
149#define	N_TCPSTAT	38
150	{ .n_name = "_tcpstat" },
151#define	N_UDPSTAT	39
152	{ .n_name = "_udpstat" },
153#define	N_IPSTAT	40
154	{ .n_name = "_ipstat" },
155#define	N_ICMPSTAT	41
156	{ .n_name = "_icmpstat" },
157#define	N_IGMPSTAT	42
158	{ .n_name = "_igmpstat" },
159#define	N_PIMSTAT	43
160	{ .n_name = "_pimstat" },
161#define	N_TCBINFO	44
162	{ .n_name = "_tcbinfo" },
163#define	N_UDBINFO	45
164	{ .n_name = "_udbinfo" },
165#define	N_DIVCBINFO	46
166	{ .n_name = "_divcbinfo" },
167#define	N_RIPCBINFO	47
168	{ .n_name = "_ripcbinfo" },
169#define	N_UNP_COUNT	48
170	{ .n_name = "_unp_count" },
171#define	N_UNP_GENCNT	49
172	{ .n_name = "_unp_gencnt" },
173#define	N_UNP_DHEAD	50
174	{ .n_name = "_unp_dhead" },
175#define	N_UNP_SHEAD	51
176	{ .n_name = "_unp_shead" },
177#define	N_RIP6STAT	52
178	{ .n_name = "_rip6stat" },
179#define	N_SCTPSTAT	53
180	{ .n_name = "_sctpstat" },
181#define	N_MFCTABLESIZE	54
182	{ .n_name = "_mfctablesize" },
183#define N_ARPSTAT       55
184	{ .n_name = "_arpstat" },
185#define	N_UNP_SPHEAD	56
186	{ .n_name = "unp_sphead" },
187	{ .n_name = NULL },
188};
189
190struct protox {
191	int	pr_index;		/* index into nlist of cb head */
192	int	pr_sindex;		/* index into nlist of stat block */
193	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
194	void	(*pr_cblocks)(u_long, const char *, int, int);
195					/* control blocks printing routine */
196	void	(*pr_stats)(u_long, const char *, int, int);
197					/* statistics printing routine */
198	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
199	const char	*pr_name;		/* well-known name */
200	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
201	int	pr_protocol;
202} protox[] = {
203	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
204	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
205	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
206	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
207#ifdef SCTP
208	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
209	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
210#endif
211	{ N_DIVCBINFO,	-1,		1,	protopr,
212	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
213	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
214	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
215	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
216	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
217	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
218	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
219#ifdef IPSEC
220	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
221	  ipsec_stats,	NULL,		"ipsec", 0,	0},
222	{ -1,		N_AHSTAT,	1,	NULL,
223	  ah_stats,	NULL,		"ah",	0,	0},
224	{ -1,		N_ESPSTAT,	1,	NULL,
225	  esp_stats,	NULL,		"esp",	0,	0},
226	{ -1,		N_IPCOMPSTAT,	1,	NULL,
227	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
228#endif
229	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
230	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
231	{ -1,		N_CARPSTAT,	1,	NULL,
232	  carp_stats,	NULL,		"carp",	1,	0 },
233	{ -1,		N_PFSYNCSTAT,	1,	NULL,
234	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
235	{ -1,		N_ARPSTAT,	1,	NULL,
236	  arp_stats,	NULL,		"arp", 1,	0 },
237	{ -1,		-1,		0,	NULL,
238	  NULL,		NULL,		NULL,	0,	0 }
239};
240
241#ifdef INET6
242struct protox ip6protox[] = {
243	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
244	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
245	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
246	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
247	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
248	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
249	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
250	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
251#ifdef IPSEC
252	{ -1,		N_IPSEC6STAT,	1,	NULL,
253	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
254#endif
255#ifdef notyet
256	{ -1,		N_PIM6STAT,	1,	NULL,
257	  pim6_stats,	NULL,		"pim6",	1,	0 },
258#endif
259	{ -1,		N_RIP6STAT,	1,	NULL,
260	  rip6_stats,	NULL,		"rip6",	1,	0 },
261	{ -1,		-1,		0,	NULL,
262	  NULL,		NULL,		NULL,	0,	0 }
263};
264#endif /*INET6*/
265
266#ifdef IPSEC
267struct protox pfkeyprotox[] = {
268	{ -1,		N_PFKEYSTAT,	1,	NULL,
269	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
270	{ -1,		-1,		0,	NULL,
271	  NULL,		NULL,		NULL,	0,	0 }
272};
273#endif
274
275struct protox atalkprotox[] = {
276	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
277	  ddp_stats,	NULL,		"ddp",	0,	0 },
278	{ -1,		-1,		0,	NULL,
279	  NULL,		NULL,		NULL,	0,	0 }
280};
281#ifdef NETGRAPH
282struct protox netgraphprotox[] = {
283	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
284	  NULL,		NULL,		"ctrl",	0,	0 },
285	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
286	  NULL,		NULL,		"data",	0,	0 },
287	{ -1,		-1,		0,	NULL,
288	  NULL,		NULL,		NULL,	0,	0 }
289};
290#endif
291#ifdef IPX
292struct protox ipxprotox[] = {
293	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
294	  ipx_stats,	NULL,		"ipx",	0,	0 },
295	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
296	  spx_stats,	NULL,		"spx",	0,	0 },
297	{ -1,		-1,		0,	NULL,
298	  NULL,		NULL,		0,	0,	0 }
299};
300#endif
301
302struct protox *protoprotox[] = {
303					 protox,
304#ifdef INET6
305					 ip6protox,
306#endif
307#ifdef IPSEC
308					 pfkeyprotox,
309#endif
310#ifdef IPX
311					 ipxprotox,
312#endif
313					 atalkprotox, NULL };
314
315static void printproto(struct protox *, const char *);
316static void usage(void);
317static struct protox *name2protox(const char *);
318static struct protox *knownname(const char *);
319
320static kvm_t *kvmd;
321static char *nlistf = NULL, *memf = NULL;
322
323int	Aflag;		/* show addresses of protocol control block */
324int	aflag;		/* show all sockets (including servers) */
325int	Bflag;		/* show information about bpf consumers */
326int	bflag;		/* show i/f total bytes in/out */
327int	dflag;		/* show i/f dropped packets */
328int	gflag;		/* show group (multicast) routing or stats */
329int	hflag;		/* show counters in human readable format */
330int	iflag;		/* show interfaces */
331int	Lflag;		/* show size of listen queues */
332int	mflag;		/* show memory stats */
333int	noutputs = 0;	/* how much outputs before we exit */
334int	numeric_addr;	/* show addresses numerically */
335int	numeric_port;	/* show ports numerically */
336static int pflag;	/* show given protocol */
337int	Qflag;		/* show netisr information */
338int	rflag;		/* show routing tables (or routing stats) */
339int	sflag;		/* show protocol statistics */
340int	Wflag;		/* wide display */
341int	Tflag;		/* TCP Information */
342int	xflag;		/* extra information, includes all socket buffer info */
343int	zflag;		/* zero stats */
344
345int	interval;	/* repeat interval for i/f stats */
346
347char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
348int	unit;		/* unit number for above */
349
350int	af;		/* address family */
351int	live;		/* true if we are examining a live system */
352
353int
354main(int argc, char *argv[])
355{
356	struct protox *tp = NULL;  /* for printing cblocks & stats */
357	int ch;
358
359	af = AF_UNSPEC;
360
361	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:Qq:rSTsuWw:xz"))
362	    != -1)
363		switch(ch) {
364		case 'A':
365			Aflag = 1;
366			break;
367		case 'a':
368			aflag = 1;
369			break;
370		case 'B':
371			Bflag = 1;
372			break;
373		case 'b':
374			bflag = 1;
375			break;
376		case 'd':
377			dflag = 1;
378			break;
379		case 'f':
380			if (strcmp(optarg, "ipx") == 0)
381				af = AF_IPX;
382			else if (strcmp(optarg, "inet") == 0)
383				af = AF_INET;
384#ifdef INET6
385			else if (strcmp(optarg, "inet6") == 0)
386				af = AF_INET6;
387#endif
388#ifdef IPSEC
389			else if (strcmp(optarg, "pfkey") == 0)
390				af = PF_KEY;
391#endif
392			else if (strcmp(optarg, "unix") == 0)
393				af = AF_UNIX;
394			else if (strcmp(optarg, "atalk") == 0)
395				af = AF_APPLETALK;
396#ifdef NETGRAPH
397			else if (strcmp(optarg, "ng") == 0
398			    || strcmp(optarg, "netgraph") == 0)
399				af = AF_NETGRAPH;
400#endif
401			else if (strcmp(optarg, "link") == 0)
402				af = AF_LINK;
403			else {
404				errx(1, "%s: unknown address family", optarg);
405			}
406			break;
407		case 'g':
408			gflag = 1;
409			break;
410		case 'h':
411			hflag = 1;
412			break;
413		case 'I': {
414			char *cp;
415
416			iflag = 1;
417			for (cp = interface = optarg; isalpha(*cp); cp++)
418				continue;
419			unit = atoi(cp);
420			break;
421		}
422		case 'i':
423			iflag = 1;
424			break;
425		case 'L':
426			Lflag = 1;
427			break;
428		case 'M':
429			memf = optarg;
430			break;
431		case 'm':
432			mflag = 1;
433			break;
434		case 'N':
435			nlistf = optarg;
436			break;
437		case 'n':
438			numeric_addr = numeric_port = 1;
439			break;
440		case 'p':
441			if ((tp = name2protox(optarg)) == NULL) {
442				errx(1,
443				     "%s: unknown or uninstrumented protocol",
444				     optarg);
445			}
446			pflag = 1;
447			break;
448		case 'Q':
449			Qflag = 1;
450			break;
451		case 'q':
452			noutputs = atoi(optarg);
453			if (noutputs != 0)
454				noutputs++;
455			break;
456		case 'r':
457			rflag = 1;
458			break;
459		case 's':
460			++sflag;
461			break;
462		case 'S':
463			numeric_addr = 1;
464			break;
465		case 'u':
466			af = AF_UNIX;
467			break;
468		case 'W':
469		case 'l':
470			Wflag = 1;
471			break;
472		case 'w':
473			interval = atoi(optarg);
474			iflag = 1;
475			break;
476		case 'T':
477			Tflag = 1;
478			break;
479		case 'x':
480			xflag = 1;
481			break;
482		case 'z':
483			zflag = 1;
484			break;
485		case '?':
486		default:
487			usage();
488		}
489	argv += optind;
490	argc -= optind;
491
492#define	BACKWARD_COMPATIBILITY
493#ifdef	BACKWARD_COMPATIBILITY
494	if (*argv) {
495		if (isdigit(**argv)) {
496			interval = atoi(*argv);
497			if (interval <= 0)
498				usage();
499			++argv;
500			iflag = 1;
501		}
502		if (*argv) {
503			nlistf = *argv;
504			if (*++argv)
505				memf = *argv;
506		}
507	}
508#endif
509
510	/*
511	 * Discard setgid privileges if not the running kernel so that bad
512	 * guys can't print interesting stuff from kernel memory.
513	 */
514	live = (nlistf == NULL && memf == NULL);
515	if (!live)
516		setgid(getgid());
517
518	if (xflag && Tflag)
519		errx(1, "-x and -T are incompatible, pick one.");
520
521	if (Bflag) {
522		if (!live)
523			usage();
524		bpf_stats(interface);
525		exit(0);
526	}
527	if (mflag) {
528		if (!live) {
529			if (kread(0, NULL, 0) == 0)
530				mbpr(kvmd, nl[N_MBSTAT].n_value);
531		} else
532			mbpr(NULL, 0);
533		exit(0);
534	}
535	if (Qflag) {
536		if (!live) {
537			if (kread(0, NULL, 0) == 0)
538				netisr_stats(kvmd);
539		} else
540			netisr_stats(NULL);
541		exit(0);
542	}
543#if 0
544	/*
545	 * Keep file descriptors open to avoid overhead
546	 * of open/close on each call to get* routines.
547	 */
548	sethostent(1);
549	setnetent(1);
550#else
551	/*
552	 * This does not make sense any more with DNS being default over
553	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
554	 * used for the queries, which is slower.
555	 */
556#endif
557	kread(0, NULL, 0);
558	if (iflag && !sflag) {
559		intpr(interval, nl[N_IFNET].n_value, NULL);
560		exit(0);
561	}
562	if (rflag) {
563		if (sflag)
564			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
565		else
566			routepr(nl[N_RTREE].n_value);
567		exit(0);
568	}
569	if (gflag) {
570		if (sflag) {
571			if (af == AF_INET || af == AF_UNSPEC)
572				mrt_stats(nl[N_MRTSTAT].n_value);
573#ifdef INET6
574			if (af == AF_INET6 || af == AF_UNSPEC)
575				mrt6_stats(nl[N_MRT6STAT].n_value);
576#endif
577		} else {
578			if (af == AF_INET || af == AF_UNSPEC)
579				mroutepr(nl[N_MFCHASHTBL].n_value,
580					 nl[N_MFCTABLESIZE].n_value,
581					 nl[N_VIFTABLE].n_value);
582#ifdef INET6
583			if (af == AF_INET6 || af == AF_UNSPEC)
584				mroute6pr(nl[N_MF6CTABLE].n_value,
585					  nl[N_MIF6TABLE].n_value);
586#endif
587		}
588		exit(0);
589	}
590
591	if (tp) {
592		printproto(tp, tp->pr_name);
593		exit(0);
594	}
595	if (af == AF_INET || af == AF_UNSPEC)
596		for (tp = protox; tp->pr_name; tp++)
597			printproto(tp, tp->pr_name);
598#ifdef INET6
599	if (af == AF_INET6 || af == AF_UNSPEC)
600		for (tp = ip6protox; tp->pr_name; tp++)
601			printproto(tp, tp->pr_name);
602#endif /*INET6*/
603#ifdef IPSEC
604	if (af == PF_KEY || af == AF_UNSPEC)
605		for (tp = pfkeyprotox; tp->pr_name; tp++)
606			printproto(tp, tp->pr_name);
607#endif /*IPSEC*/
608#ifdef IPX
609	if (af == AF_IPX || af == AF_UNSPEC) {
610		for (tp = ipxprotox; tp->pr_name; tp++)
611			printproto(tp, tp->pr_name);
612	}
613#endif /* IPX */
614	if (af == AF_APPLETALK || af == AF_UNSPEC)
615		for (tp = atalkprotox; tp->pr_name; tp++)
616			printproto(tp, tp->pr_name);
617#ifdef NETGRAPH
618	if (af == AF_NETGRAPH || af == AF_UNSPEC)
619		for (tp = netgraphprotox; tp->pr_name; tp++)
620			printproto(tp, tp->pr_name);
621#endif /* NETGRAPH */
622	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
623		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
624		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
625		    nl[N_UNP_SPHEAD].n_value);
626	exit(0);
627}
628
629/*
630 * Print out protocol statistics or control blocks (per sflag).
631 * If the interface was not specifically requested, and the symbol
632 * is not in the namelist, ignore this one.
633 */
634static void
635printproto(tp, name)
636	struct protox *tp;
637	const char *name;
638{
639	void (*pr)(u_long, const char *, int, int);
640	u_long off;
641
642	if (sflag) {
643		if (iflag) {
644			if (tp->pr_istats)
645				intpr(interval, nl[N_IFNET].n_value,
646				      tp->pr_istats);
647			else if (pflag)
648				printf("%s: no per-interface stats routine\n",
649				    tp->pr_name);
650			return;
651		} else {
652			pr = tp->pr_stats;
653			if (!pr) {
654				if (pflag)
655					printf("%s: no stats routine\n",
656					    tp->pr_name);
657				return;
658			}
659			if (tp->pr_usesysctl && live)
660				off = 0;
661			else if (tp->pr_sindex < 0) {
662				if (pflag)
663					printf(
664				    "%s: stats routine doesn't work on cores\n",
665					    tp->pr_name);
666				return;
667			} else
668				off = nl[tp->pr_sindex].n_value;
669		}
670	} else {
671		pr = tp->pr_cblocks;
672		if (!pr) {
673			if (pflag)
674				printf("%s: no PCB routine\n", tp->pr_name);
675			return;
676		}
677		if (tp->pr_usesysctl && live)
678			off = 0;
679		else if (tp->pr_index < 0) {
680			if (pflag)
681				printf(
682				    "%s: PCB routine doesn't work on cores\n",
683				    tp->pr_name);
684			return;
685		} else
686			off = nl[tp->pr_index].n_value;
687	}
688	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
689	    af != AF_UNSPEC))
690		(*pr)(off, name, af, tp->pr_protocol);
691}
692
693/*
694 * Read kernel memory, return 0 on success.
695 */
696int
697kread(u_long addr, void *buf, size_t size)
698{
699	char errbuf[_POSIX2_LINE_MAX];
700
701	if (kvmd == NULL) {
702		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
703		setgid(getgid());
704		if (kvmd != NULL) {
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		} else {
720			warnx("kvm not available: %s", errbuf);
721			return(-1);
722		}
723	}
724	if (!buf)
725		return (0);
726	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
727		warnx("%s", kvm_geterr(kvmd));
728		return (-1);
729	}
730	return (0);
731}
732
733const char *
734plural(uintmax_t n)
735{
736	return (n != 1 ? "s" : "");
737}
738
739const char *
740plurales(uintmax_t n)
741{
742	return (n != 1 ? "es" : "");
743}
744
745const char *
746pluralies(uintmax_t n)
747{
748	return (n != 1 ? "ies" : "y");
749}
750
751/*
752 * Find the protox for the given "well-known" name.
753 */
754static struct protox *
755knownname(const char *name)
756{
757	struct protox **tpp, *tp;
758
759	for (tpp = protoprotox; *tpp; tpp++)
760		for (tp = *tpp; tp->pr_name; tp++)
761			if (strcmp(tp->pr_name, name) == 0)
762				return (tp);
763	return (NULL);
764}
765
766/*
767 * Find the protox corresponding to name.
768 */
769static struct protox *
770name2protox(const char *name)
771{
772	struct protox *tp;
773	char **alias;			/* alias from p->aliases */
774	struct protoent *p;
775
776	/*
777	 * Try to find the name in the list of "well-known" names. If that
778	 * fails, check if name is an alias for an Internet protocol.
779	 */
780	if ((tp = knownname(name)) != NULL)
781		return (tp);
782
783	setprotoent(1);			/* make protocol lookup cheaper */
784	while ((p = getprotoent()) != NULL) {
785		/* assert: name not same as p->name */
786		for (alias = p->p_aliases; *alias; alias++)
787			if (strcmp(name, *alias) == 0) {
788				endprotoent();
789				return (knownname(p->p_name));
790			}
791	}
792	endprotoent();
793	return (NULL);
794}
795
796static void
797usage(void)
798{
799	(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",
800"usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n"
801"               [-M core] [-N system]",
802"       netstat -i | -I interface [-abdhnW] [-f address_family]\n"
803"               [-M core] [-N system]",
804"       netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
805"       netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
806"               [-M core] [-N system]",
807"       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
808"               [-M core] [-N system]",
809"       netstat -m [-M core] [-N system]",
810"       netstat -B [-I interface]",
811"       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
812"       netstat -rs [-s] [-M core] [-N system]",
813"       netstat -g [-W] [-f address_family] [-M core] [-N system]",
814"       netstat -gs [-s] [-f address_family] [-M core] [-N system]",
815"       netstat -Q");
816	exit(1);
817}
818