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