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