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