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