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