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