xref: /freebsd/usr.bin/netstat/main.c (revision 5b9c547c)
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 <stdbool.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include "netstat.h"
72 #include <libxo/xo.h>
73 
74 static struct nlist nl[] = {
75 #define	N_RTSTAT	0
76 	{ .n_name = "_rtstat" },
77 #define	N_RTREE		1
78 	{ .n_name = "_rt_tables"},
79 #define	N_MRTSTAT	2
80 	{ .n_name = "_mrtstat" },
81 #define	N_MFCHASHTBL	3
82 	{ .n_name = "_mfchashtbl" },
83 #define	N_VIFTABLE	4
84 	{ .n_name = "_viftable" },
85 #define	N_NGSOCKS	5
86 	{ .n_name = "_ngsocklist"},
87 #define	N_IP6STAT	6
88 	{ .n_name = "_ip6stat" },
89 #define	N_ICMP6STAT	7
90 	{ .n_name = "_icmp6stat" },
91 #define	N_IPSECSTAT	8
92 	{ .n_name = "_ipsec4stat" },
93 #define	N_IPSEC6STAT	9
94 	{ .n_name = "_ipsec6stat" },
95 #define	N_PIM6STAT	10
96 	{ .n_name = "_pim6stat" },
97 #define	N_MRT6STAT	11
98 	{ .n_name = "_mrt6stat" },
99 #define	N_MF6CTABLE	12
100 	{ .n_name = "_mf6ctable" },
101 #define	N_MIF6TABLE	13
102 	{ .n_name = "_mif6table" },
103 #define	N_PFKEYSTAT	14
104 	{ .n_name = "_pfkeystat" },
105 #define	N_RTTRASH	15
106 	{ .n_name = "_rttrash" },
107 #define	N_CARPSTAT	16
108 	{ .n_name = "_carpstats" },
109 #define	N_PFSYNCSTAT	17
110 	{ .n_name = "_pfsyncstats" },
111 #define	N_AHSTAT	18
112 	{ .n_name = "_ahstat" },
113 #define	N_ESPSTAT	19
114 	{ .n_name = "_espstat" },
115 #define	N_IPCOMPSTAT	20
116 	{ .n_name = "_ipcompstat" },
117 #define	N_TCPSTAT	21
118 	{ .n_name = "_tcpstat" },
119 #define	N_UDPSTAT	22
120 	{ .n_name = "_udpstat" },
121 #define	N_IPSTAT	23
122 	{ .n_name = "_ipstat" },
123 #define	N_ICMPSTAT	24
124 	{ .n_name = "_icmpstat" },
125 #define	N_IGMPSTAT	25
126 	{ .n_name = "_igmpstat" },
127 #define	N_PIMSTAT	26
128 	{ .n_name = "_pimstat" },
129 #define	N_TCBINFO	27
130 	{ .n_name = "_tcbinfo" },
131 #define	N_UDBINFO	28
132 	{ .n_name = "_udbinfo" },
133 #define	N_DIVCBINFO	29
134 	{ .n_name = "_divcbinfo" },
135 #define	N_RIPCBINFO	30
136 	{ .n_name = "_ripcbinfo" },
137 #define	N_UNP_COUNT	31
138 	{ .n_name = "_unp_count" },
139 #define	N_UNP_GENCNT	32
140 	{ .n_name = "_unp_gencnt" },
141 #define	N_UNP_DHEAD	33
142 	{ .n_name = "_unp_dhead" },
143 #define	N_UNP_SHEAD	34
144 	{ .n_name = "_unp_shead" },
145 #define	N_RIP6STAT	36
146 	{ .n_name = "_rip6stat" },
147 #define	N_SCTPSTAT	36
148 	{ .n_name = "_sctpstat" },
149 #define	N_MFCTABLESIZE	37
150 	{ .n_name = "_mfctablesize" },
151 #define	N_ARPSTAT	38
152 	{ .n_name = "_arpstat" },
153 #define	N_UNP_SPHEAD	39
154 	{ .n_name = "unp_sphead" },
155 #define	N_SFSTAT	40
156 	{ .n_name = "_sfstat"},
157 	{ .n_name = NULL },
158 };
159 
160 struct protox {
161 	int	pr_index;		/* index into nlist of cb head */
162 	int	pr_sindex;		/* index into nlist of stat block */
163 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
164 	void	(*pr_cblocks)(u_long, const char *, int, int);
165 					/* control blocks printing routine */
166 	void	(*pr_stats)(u_long, const char *, int, int);
167 					/* statistics printing routine */
168 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
169 	const char	*pr_name;		/* well-known name */
170 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
171 	int	pr_protocol;
172 } protox[] = {
173 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
174 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
175 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
176 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
177 #ifdef SCTP
178 	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
179 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
180 #endif
181 #ifdef SDP
182 	{ -1,		-1,		1,	protopr,
183 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
184 #endif
185 	{ N_DIVCBINFO,	-1,		1,	protopr,
186 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
187 	{ N_RIPCBINFO,	N_IPSTAT,	1,	protopr,
188 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
189 	{ N_RIPCBINFO,	N_ICMPSTAT,	1,	protopr,
190 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
191 	{ N_RIPCBINFO,	N_IGMPSTAT,	1,	protopr,
192 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
193 #ifdef IPSEC
194 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
195 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
196 	{ -1,		N_AHSTAT,	1,	NULL,
197 	  ah_stats,	NULL,		"ah",	0,	0},
198 	{ -1,		N_ESPSTAT,	1,	NULL,
199 	  esp_stats,	NULL,		"esp",	0,	0},
200 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
201 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
202 #endif
203 	{ N_RIPCBINFO,	N_PIMSTAT,	1,	protopr,
204 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
205 	{ -1,		N_CARPSTAT,	1,	NULL,
206 	  carp_stats,	NULL,		"carp",	1,	0 },
207 #ifdef PF
208 	{ -1,		N_PFSYNCSTAT,	1,	NULL,
209 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
210 #endif
211 	{ -1,		N_ARPSTAT,	1,	NULL,
212 	  arp_stats,	NULL,		"arp", 1,	0 },
213 	{ -1,		-1,		0,	NULL,
214 	  NULL,		NULL,		NULL,	0,	0 }
215 };
216 
217 #ifdef INET6
218 struct protox ip6protox[] = {
219 	{ N_TCBINFO,	N_TCPSTAT,	1,	protopr,
220 	  tcp_stats,	NULL,		"tcp",	1,	IPPROTO_TCP },
221 	{ N_UDBINFO,	N_UDPSTAT,	1,	protopr,
222 	  udp_stats,	NULL,		"udp",	1,	IPPROTO_UDP },
223 	{ N_RIPCBINFO,	N_IP6STAT,	1,	protopr,
224 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
225 	{ N_RIPCBINFO,	N_ICMP6STAT,	1,	protopr,
226 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
227 #ifdef SDP
228 	{ -1,		-1,		1,	protopr,
229 	 NULL,		NULL,		"sdp",	1,	IPPROTO_TCP },
230 #endif
231 #ifdef IPSEC
232 	{ -1,		N_IPSEC6STAT,	1,	NULL,
233 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
234 #endif
235 #ifdef notyet
236 	{ -1,		N_PIM6STAT,	1,	NULL,
237 	  pim6_stats,	NULL,		"pim6",	1,	0 },
238 #endif
239 	{ -1,		N_RIP6STAT,	1,	NULL,
240 	  rip6_stats,	NULL,		"rip6",	1,	0 },
241 	{ -1,		-1,		0,	NULL,
242 	  NULL,		NULL,		NULL,	0,	0 }
243 };
244 #endif /*INET6*/
245 
246 #ifdef IPSEC
247 struct protox pfkeyprotox[] = {
248 	{ -1,		N_PFKEYSTAT,	1,	NULL,
249 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
250 	{ -1,		-1,		0,	NULL,
251 	  NULL,		NULL,		NULL,	0,	0 }
252 };
253 #endif
254 
255 #ifdef NETGRAPH
256 struct protox netgraphprotox[] = {
257 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
258 	  NULL,		NULL,		"ctrl",	0,	0 },
259 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
260 	  NULL,		NULL,		"data",	0,	0 },
261 	{ -1,		-1,		0,	NULL,
262 	  NULL,		NULL,		NULL,	0,	0 }
263 };
264 #endif
265 
266 struct protox *protoprotox[] = {
267 					 protox,
268 #ifdef INET6
269 					 ip6protox,
270 #endif
271 #ifdef IPSEC
272 					 pfkeyprotox,
273 #endif
274 					 NULL };
275 
276 static void printproto(struct protox *, const char *, bool *);
277 static void usage(void);
278 static struct protox *name2protox(const char *);
279 static struct protox *knownname(const char *);
280 
281 static kvm_t *kvmd;
282 static char *nlistf = NULL, *memf = NULL;
283 
284 int	Aflag;		/* show addresses of protocol control block */
285 int	aflag;		/* show all sockets (including servers) */
286 int	Bflag;		/* show information about bpf consumers */
287 int	bflag;		/* show i/f total bytes in/out */
288 int	dflag;		/* show i/f dropped packets */
289 int	gflag;		/* show group (multicast) routing or stats */
290 int	hflag;		/* show counters in human readable format */
291 int	iflag;		/* show interfaces */
292 int	Lflag;		/* show size of listen queues */
293 int	mflag;		/* show memory stats */
294 int	noutputs = 0;	/* how much outputs before we exit */
295 int	numeric_addr;	/* show addresses numerically */
296 int	numeric_port;	/* show ports numerically */
297 static int pflag;	/* show given protocol */
298 int	Qflag;		/* show netisr information */
299 int	rflag;		/* show routing tables (or routing stats) */
300 int	Rflag;		/* show flow / RSS statistics */
301 int	sflag;		/* show protocol statistics */
302 int	Wflag;		/* wide display */
303 int	Tflag;		/* TCP Information */
304 int	xflag;		/* extra information, includes all socket buffer info */
305 int	zflag;		/* zero stats */
306 
307 int	interval;	/* repeat interval for i/f stats */
308 
309 char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
310 int	unit;		/* unit number for above */
311 
312 int	af;		/* address family */
313 int	live;		/* true if we are examining a live system */
314 
315 int
316 main(int argc, char *argv[])
317 {
318 	struct protox *tp = NULL;  /* for printing cblocks & stats */
319 	int ch;
320 	int fib = -1;
321 	char *endptr;
322 	bool first = true;
323 
324 	af = AF_UNSPEC;
325 
326 	argc = xo_parse_args(argc, argv);
327 
328 	while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
329 	    != -1)
330 		switch(ch) {
331 		case '4':
332 #ifdef INET
333 			af = AF_INET;
334 #else
335 			errx(1, "IPv4 support is not compiled in");
336 #endif
337 			break;
338 		case '6':
339 #ifdef INET6
340 			af = AF_INET6;
341 #else
342 			errx(1, "IPv6 support is not compiled in");
343 #endif
344 			break;
345 		case 'A':
346 			Aflag = 1;
347 			break;
348 		case 'a':
349 			aflag = 1;
350 			break;
351 		case 'B':
352 			Bflag = 1;
353 			break;
354 		case 'b':
355 			bflag = 1;
356 			break;
357 		case 'd':
358 			dflag = 1;
359 			break;
360 		case 'F':
361 			fib = strtol(optarg, &endptr, 0);
362 			if (*endptr != '\0' ||
363 			    (fib == 0 && (errno == EINVAL || errno == ERANGE)))
364 				xo_errx(1, "%s: invalid fib", optarg);
365 			break;
366 		case 'f':
367 			if (strcmp(optarg, "inet") == 0)
368 				af = AF_INET;
369 #ifdef INET6
370 			else if (strcmp(optarg, "inet6") == 0)
371 				af = AF_INET6;
372 #endif
373 #ifdef IPSEC
374 			else if (strcmp(optarg, "pfkey") == 0)
375 				af = PF_KEY;
376 #endif
377 			else if (strcmp(optarg, "unix") == 0)
378 				af = AF_UNIX;
379 #ifdef NETGRAPH
380 			else if (strcmp(optarg, "ng") == 0
381 			    || strcmp(optarg, "netgraph") == 0)
382 				af = AF_NETGRAPH;
383 #endif
384 			else if (strcmp(optarg, "link") == 0)
385 				af = AF_LINK;
386 			else {
387 				xo_errx(1, "%s: unknown address family",
388 				    optarg);
389 			}
390 			break;
391 		case 'g':
392 			gflag = 1;
393 			break;
394 		case 'h':
395 			hflag = 1;
396 			break;
397 		case 'I': {
398 			char *cp;
399 
400 			iflag = 1;
401 			for (cp = interface = optarg; isalpha(*cp); cp++)
402 				continue;
403 			unit = atoi(cp);
404 			break;
405 		}
406 		case 'i':
407 			iflag = 1;
408 			break;
409 		case 'L':
410 			Lflag = 1;
411 			break;
412 		case 'M':
413 			memf = optarg;
414 			break;
415 		case 'm':
416 			mflag = 1;
417 			break;
418 		case 'N':
419 			nlistf = optarg;
420 			break;
421 		case 'n':
422 			numeric_addr = numeric_port = 1;
423 			break;
424 		case 'p':
425 			if ((tp = name2protox(optarg)) == NULL) {
426 				xo_errx(1, "%s: unknown or uninstrumented "
427 				    "protocol", optarg);
428 			}
429 			pflag = 1;
430 			break;
431 		case 'Q':
432 			Qflag = 1;
433 			break;
434 		case 'q':
435 			noutputs = atoi(optarg);
436 			if (noutputs != 0)
437 				noutputs++;
438 			break;
439 		case 'r':
440 			rflag = 1;
441 			break;
442 		case 'R':
443 			Rflag = 1;
444 			break;
445 		case 's':
446 			++sflag;
447 			break;
448 		case 'S':
449 			numeric_addr = 1;
450 			break;
451 		case 'u':
452 			af = AF_UNIX;
453 			break;
454 		case 'W':
455 		case 'l':
456 			Wflag = 1;
457 			break;
458 		case 'w':
459 			interval = atoi(optarg);
460 			iflag = 1;
461 			break;
462 		case 'T':
463 			Tflag = 1;
464 			break;
465 		case 'x':
466 			xflag = 1;
467 			break;
468 		case 'z':
469 			zflag = 1;
470 			break;
471 		case '?':
472 		default:
473 			usage();
474 		}
475 	argv += optind;
476 	argc -= optind;
477 
478 #define	BACKWARD_COMPATIBILITY
479 #ifdef	BACKWARD_COMPATIBILITY
480 	if (*argv) {
481 		if (isdigit(**argv)) {
482 			interval = atoi(*argv);
483 			if (interval <= 0)
484 				usage();
485 			++argv;
486 			iflag = 1;
487 		}
488 		if (*argv) {
489 			nlistf = *argv;
490 			if (*++argv)
491 				memf = *argv;
492 		}
493 	}
494 #endif
495 
496 	/*
497 	 * Discard setgid privileges if not the running kernel so that bad
498 	 * guys can't print interesting stuff from kernel memory.
499 	 */
500 	live = (nlistf == NULL && memf == NULL);
501 	if (!live)
502 		setgid(getgid());
503 
504 	if (xflag && Tflag)
505 		xo_errx(1, "-x and -T are incompatible, pick one.");
506 
507 	if (Bflag) {
508 		if (!live)
509 			usage();
510 		bpf_stats(interface);
511 		xo_finish();
512 		exit(0);
513 	}
514 	if (mflag) {
515 		if (!live) {
516 			if (kread(0, NULL, 0) == 0)
517 				mbpr(kvmd, nl[N_SFSTAT].n_value);
518 		} else
519 			mbpr(NULL, 0);
520 		xo_finish();
521 		exit(0);
522 	}
523 	if (Qflag) {
524 		if (!live) {
525 			if (kread(0, NULL, 0) == 0)
526 				netisr_stats(kvmd);
527 		} else
528 			netisr_stats(NULL);
529 		xo_finish();
530 		exit(0);
531 	}
532 #if 0
533 	/*
534 	 * Keep file descriptors open to avoid overhead
535 	 * of open/close on each call to get* routines.
536 	 */
537 	sethostent(1);
538 	setnetent(1);
539 #else
540 	/*
541 	 * This does not make sense any more with DNS being default over
542 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
543 	 * used for the queries, which is slower.
544 	 */
545 #endif
546 	if (iflag && !sflag) {
547 		xo_open_container("statistics");
548 		intpr(interval, NULL, af);
549 		xo_close_container("statistics");
550 		xo_finish();
551 		exit(0);
552 	}
553 	if (rflag) {
554 		xo_open_container("statistics");
555 		if (sflag) {
556 			rt_stats();
557 			flowtable_stats();
558 		} else
559 			routepr(fib, af);
560 		xo_close_container("statistics");
561 		xo_finish();
562 		exit(0);
563 	}
564 
565 	if (gflag) {
566 		xo_open_container("statistics");
567 		if (sflag) {
568 			if (af == AF_INET || af == AF_UNSPEC)
569 				mrt_stats();
570 #ifdef INET6
571 			if (af == AF_INET6 || af == AF_UNSPEC)
572 				mrt6_stats();
573 #endif
574 		} else {
575 			if (af == AF_INET || af == AF_UNSPEC)
576 				mroutepr();
577 #ifdef INET6
578 			if (af == AF_INET6 || af == AF_UNSPEC)
579 				mroute6pr();
580 #endif
581 		}
582 		xo_close_container("statistics");
583 		xo_finish();
584 		exit(0);
585 	}
586 
587 	/* Load all necessary kvm symbols */
588 	kresolve_list(nl);
589 
590 	if (tp) {
591 		xo_open_container("statistics");
592 		printproto(tp, tp->pr_name, &first);
593 		if (!first)
594 			xo_close_list("socket");
595 		xo_close_container("statistics");
596 		xo_finish();
597 		exit(0);
598 	}
599 
600 	xo_open_container("statistics");
601 	if (af == AF_INET || af == AF_UNSPEC)
602 		for (tp = protox; tp->pr_name; tp++)
603 			printproto(tp, tp->pr_name, &first);
604 #ifdef INET6
605 	if (af == AF_INET6 || af == AF_UNSPEC)
606 		for (tp = ip6protox; tp->pr_name; tp++)
607 			printproto(tp, tp->pr_name, &first);
608 #endif /*INET6*/
609 #ifdef IPSEC
610 	if (af == PF_KEY || af == AF_UNSPEC)
611 		for (tp = pfkeyprotox; tp->pr_name; tp++)
612 			printproto(tp, tp->pr_name, &first);
613 #endif /*IPSEC*/
614 #ifdef NETGRAPH
615 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
616 		for (tp = netgraphprotox; tp->pr_name; tp++)
617 			printproto(tp, tp->pr_name, &first);
618 #endif /* NETGRAPH */
619 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
620 		unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
621 		    nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
622 		    nl[N_UNP_SPHEAD].n_value, &first);
623 
624 	if (!first)
625 		xo_close_list("socket");
626 	xo_close_container("statistics");
627 	xo_finish();
628 	exit(0);
629 }
630 
631 /*
632  * Print out protocol statistics or control blocks (per sflag).
633  * If the interface was not specifically requested, and the symbol
634  * is not in the namelist, ignore this one.
635  */
636 static void
637 printproto(struct protox *tp, const char *name, bool *first)
638 {
639 	void (*pr)(u_long, const char *, int, int);
640 	u_long off;
641 	bool doingdblocks = false;
642 
643 	if (sflag) {
644 		if (iflag) {
645 			if (tp->pr_istats)
646 				intpr(interval, tp->pr_istats, af);
647 			else if (pflag)
648 				xo_message("%s: no per-interface stats routine",
649 				    tp->pr_name);
650 			return;
651 		} else {
652 			pr = tp->pr_stats;
653 			if (!pr) {
654 				if (pflag)
655 					xo_message("%s: no stats routine",
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 					xo_message("%s: stats routine doesn't "
664 					    "work on cores", tp->pr_name);
665 				return;
666 			} else
667 				off = nl[tp->pr_sindex].n_value;
668 		}
669 	} else {
670 		doingdblocks = true;
671 		pr = tp->pr_cblocks;
672 		if (!pr) {
673 			if (pflag)
674 				xo_message("%s: no PCB routine", 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 				xo_message("%s: PCB routine doesn't work on "
682 				    "cores", tp->pr_name);
683 			return;
684 		} else
685 			off = nl[tp->pr_index].n_value;
686 	}
687 	if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
688 	    af != AF_UNSPEC)) {
689 		if (doingdblocks && *first) {
690 			xo_open_list("socket");
691 			*first = false;
692 		}
693 
694 		(*pr)(off, name, af, tp->pr_protocol);
695 	}
696 }
697 
698 static int
699 kvmd_init(void)
700 {
701 	char errbuf[_POSIX2_LINE_MAX];
702 
703 	if (kvmd != NULL)
704 		return (0);
705 
706 	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
707 	setgid(getgid());
708 
709 	if (kvmd == NULL) {
710 		xo_warnx("kvm not available: %s", errbuf);
711 		return (-1);
712 	}
713 
714 	return (0);
715 }
716 
717 /*
718  * Resolve symbol list, return 0 on success.
719  */
720 int
721 kresolve_list(struct nlist *_nl)
722 {
723 
724 	if ((kvmd == NULL) && (kvmd_init() != 0))
725 		return (-1);
726 
727 	if (_nl[0].n_type != 0)
728 		return (0);
729 
730 	if (kvm_nlist(kvmd, _nl) < 0) {
731 		if (nlistf)
732 			xo_errx(1, "%s: kvm_nlist: %s", nlistf,
733 			    kvm_geterr(kvmd));
734 		else
735 			xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
736 	}
737 
738 	return (0);
739 }
740 
741 /*
742  * Read kernel memory, return 0 on success.
743  */
744 int
745 kread(u_long addr, void *buf, size_t size)
746 {
747 
748 	if (kvmd_init() < 0)
749 		return (-1);
750 
751 	if (!buf)
752 		return (0);
753 	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
754 		xo_warnx("%s", kvm_geterr(kvmd));
755 		return (-1);
756 	}
757 	return (0);
758 }
759 
760 /*
761  * Read single counter(9).
762  */
763 uint64_t
764 kread_counter(u_long addr)
765 {
766 
767 	if (kvmd_init() < 0)
768 		return (-1);
769 
770 	return (kvm_counter_u64_fetch(kvmd, addr));
771 }
772 
773 /*
774  * Read an array of N counters in kernel memory into array of N uint64_t's.
775  */
776 int
777 kread_counters(u_long addr, void *buf, size_t size)
778 {
779 	uint64_t *c = buf;
780 
781 	if (kvmd_init() < 0)
782 		return (-1);
783 
784 	if (kread(addr, buf, size) < 0)
785 		return (-1);
786 
787 	while (size != 0) {
788 		*c = kvm_counter_u64_fetch(kvmd, *c);
789 		size -= sizeof(*c);
790 		c++;
791 	}
792 	return (0);
793 }
794 
795 const char *
796 plural(uintmax_t n)
797 {
798 	return (n != 1 ? "s" : "");
799 }
800 
801 const char *
802 plurales(uintmax_t n)
803 {
804 	return (n != 1 ? "es" : "");
805 }
806 
807 const char *
808 pluralies(uintmax_t n)
809 {
810 	return (n != 1 ? "ies" : "y");
811 }
812 
813 /*
814  * Find the protox for the given "well-known" name.
815  */
816 static struct protox *
817 knownname(const char *name)
818 {
819 	struct protox **tpp, *tp;
820 
821 	for (tpp = protoprotox; *tpp; tpp++)
822 		for (tp = *tpp; tp->pr_name; tp++)
823 			if (strcmp(tp->pr_name, name) == 0)
824 				return (tp);
825 	return (NULL);
826 }
827 
828 /*
829  * Find the protox corresponding to name.
830  */
831 static struct protox *
832 name2protox(const char *name)
833 {
834 	struct protox *tp;
835 	char **alias;			/* alias from p->aliases */
836 	struct protoent *p;
837 
838 	/*
839 	 * Try to find the name in the list of "well-known" names. If that
840 	 * fails, check if name is an alias for an Internet protocol.
841 	 */
842 	if ((tp = knownname(name)) != NULL)
843 		return (tp);
844 
845 	setprotoent(1);			/* make protocol lookup cheaper */
846 	while ((p = getprotoent()) != NULL) {
847 		/* assert: name not same as p->name */
848 		for (alias = p->p_aliases; *alias; alias++)
849 			if (strcmp(name, *alias) == 0) {
850 				endprotoent();
851 				return (knownname(p->p_name));
852 			}
853 	}
854 	endprotoent();
855 	return (NULL);
856 }
857 
858 static void
859 usage(void)
860 {
861 	(void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
862 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
863 "               [-M core] [-N system]",
864 "       netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
865 "               [-M core] [-N system]",
866 "       netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
867 "               [-q howmany]",
868 "       netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
869 "               [-M core] [-N system]",
870 "       netstat -i | -I interface -s [-46s]\n"
871 "               [-f protocol_family | -p protocol] [-M core] [-N system]",
872 "       netstat -m [-M core] [-N system]",
873 "       netstat -B [-z] [-I interface]",
874 "       netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
875 "               [-M core] [-N system]",
876 "       netstat -rs [-s] [-M core] [-N system]",
877 "       netstat -g [-46W] [-f address_family] [-M core] [-N system]",
878 "       netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
879 "       netstat -Q");
880 	xo_finish();
881 	exit(1);
882 }
883