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