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