xref: /freebsd/usr.bin/netstat/main.c (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char const copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37 	Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
43 #endif /* not lint */
44 #endif
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 
54 #include <netinet/in.h>
55 
56 #include <netgraph/ng_socket.h>
57 
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <kvm.h>
62 #include <limits.h>
63 #include <netdb.h>
64 #include <nlist.h>
65 #include <paths.h>
66 #include <stdint.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include "netstat.h"
72 
73 static struct nlist nl[] = {
74 #define	N_IFNET		0
75 	{ .n_name = "_ifnet" },
76 #define	N_RTSTAT	1
77 	{ .n_name = "_rtstat" },
78 #define N_RTREE		2
79 	{ .n_name = "_rt_tables"},
80 #define N_MRTSTAT	3
81 	{ .n_name = "_mrtstat" },
82 #define N_MFCTABLE	4
83 	{ .n_name = "_mfctable" },
84 #define N_VIFTABLE	5
85 	{ .n_name = "_viftable" },
86 #define N_IPX		6
87 	{ .n_name = "_ipxpcb_list"},
88 #define N_IPXSTAT	7
89 	{ .n_name = "_ipxstat"},
90 #define N_SPXSTAT	8
91 	{ .n_name = "_spx_istat"},
92 #define N_DDPSTAT	9
93 	{ .n_name = "_ddpstat"},
94 #define N_DDPCB		10
95 	{ .n_name = "_ddpcb"},
96 #define N_NGSOCKS	11
97 	{ .n_name = "_ngsocklist"},
98 #define N_IP6STAT	12
99 	{ .n_name = "_ip6stat" },
100 #define N_ICMP6STAT	13
101 	{ .n_name = "_icmp6stat" },
102 #define N_IPSECSTAT	14
103 	{ .n_name = "_ipsecstat" },
104 #define N_IPSEC6STAT	15
105 	{ .n_name = "_ipsec6stat" },
106 #define N_PIM6STAT	16
107 	{ .n_name = "_pim6stat" },
108 #define N_MRT6STAT	17
109 	{ .n_name = "_mrt6stat" },
110 #define N_MF6CTABLE	18
111 	{ .n_name = "_mf6ctable" },
112 #define N_MIF6TABLE	19
113 	{ .n_name = "_mif6table" },
114 #define N_PFKEYSTAT	20
115 	{ .n_name = "_pfkeystat" },
116 #define N_MBSTAT	21
117 	{ .n_name = "_mbstat" },
118 #define N_MBTYPES	22
119 	{ .n_name = "_mbtypes" },
120 #define N_NMBCLUSTERS	23
121 	{ .n_name = "_nmbclusters" },
122 #define N_NMBUFS	24
123 	{ .n_name = "_nmbufs" },
124 #define	N_MBHI		25
125 	{ .n_name = "_mbuf_hiwm" },
126 #define	N_CLHI		26
127 	{ .n_name = "_clust_hiwm" },
128 #define	N_NCPUS		27
129 	{ .n_name = "_smp_cpus" },
130 #define	N_PAGESZ	28
131 	{ .n_name = "_pagesize" },
132 #define	N_MBPSTAT	29
133 	{ .n_name = "_mb_statpcpu" },
134 #define	N_RTTRASH	30
135 	{ .n_name = "_rttrash" },
136 #define	N_MBLO		31
137 	{ .n_name = "_mbuf_lowm" },
138 #define	N_CLLO		32
139 	{ .n_name = "_clust_lowm" },
140 #define N_CARPSTAT	33
141 	{ .n_name = "_carpstats" },
142 #define N_PFSYNCSTAT	34
143 	{ .n_name = "_pfsyncstats" },
144 #define	N_FAST_IPSECSTAT 35
145 	{ .n_name = "_newipsecstat" },
146 #define	N_AHSTAT	36
147 	{ .n_name = "_ahstat" },
148 #define	N_ESPSTAT	37
149 	{ .n_name = "_espstat" },
150 #define	N_IPCOMPSTAT	38
151 	{ .n_name = "_ipcompstat" },
152 	{ .n_name = NULL },
153 };
154 
155 struct protox {
156 	u_char	pr_index;		/* index into nlist of cb head */
157 	u_char	pr_sindex;		/* index into nlist of stat block */
158 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
159 	void	(*pr_cblocks)(u_long, const char *, int);
160 					/* control blocks printing routine */
161 	void	(*pr_stats)(u_long, const char *, int);
162 					/* statistics printing routine */
163 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
164 	const char	*pr_name;		/* well-known name */
165 	u_long	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
166 } protox[] = {
167 	{ -1,		-1,		1,	protopr,
168 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
169 	{ -1,		-1,		1,	protopr,
170 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
171 	{ -1,		-1,		1,	protopr,
172 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
173 	{ -1,		-1,		1,	protopr,
174 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
175 	{ -1,		-1,		1,	protopr,
176 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
177 	{ -1,		-1,		1,	protopr,
178 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
179 #ifdef IPSEC
180 	{ -1,		N_IPSECSTAT,	1,	NULL,
181 	  ipsec_stats,	NULL,		"ipsec",	0},
182 #ifdef FAST_IPSEC
183 	{ -1,		N_FAST_IPSECSTAT, 1,	0,
184 	  ipsec_stats_new, NULL,	"fastipsec",	0},
185 	{ -1,		N_AHSTAT,	1,	0,
186 	  ah_stats,	NULL,		"ah",		0},
187 	{ -1,		N_ESPSTAT,	1,	0,
188 	  esp_stats,	NULL,		"esp",		0},
189 	{ -1,		N_IPCOMPSTAT,	1,	0,
190 	  ipcomp_stats,	NULL,		"ipcomp",	0},
191 #endif
192 #endif
193 	{ -1,		-1,		1,	protopr,
194 	  pim_stats,	NULL,		"pim",	IPPROTO_PIM },
195 	{ -1,		N_CARPSTAT,	1,	0,
196 	  carp_stats,	NULL,		"carp",		0},
197 	{ -1,		-1,		1,	NULL,
198 	  pfsync_stats,	NULL,		"pfsync",	1},
199 	{ -1,		-1,		0,	NULL,
200 	  NULL,		NULL,		NULL,	0 }
201 };
202 
203 #ifdef INET6
204 struct protox ip6protox[] = {
205 	{ -1,		-1,		1,	protopr,
206 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
207 	{ -1,		-1,		1,	protopr,
208 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
209 	{ -1,		N_IP6STAT,	1,	protopr,
210 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
211 	{ -1,		N_ICMP6STAT,	1,	protopr,
212 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
213 #ifdef IPSEC
214 	{ -1,		N_IPSEC6STAT,	1,	NULL,
215 	  ipsec_stats,	NULL,		"ipsec6",0 },
216 #endif
217 #ifdef notyet
218 	{ -1,		N_PIM6STAT,	1,	NULL,
219 	  pim6_stats,	NULL,		"pim6",	0 },
220 #endif
221 	{ -1,		-1,		1,	NULL,
222 	  rip6_stats,	NULL,		"rip6",	0 },
223 	{ -1,		-1,		0,	NULL,
224 	  NULL,		NULL,		NULL,	0 }
225 };
226 #endif /*INET6*/
227 
228 #ifdef IPSEC
229 struct protox pfkeyprotox[] = {
230 	{ -1,		N_PFKEYSTAT,	1,	NULL,
231 	  pfkey_stats,	NULL,		"pfkey", 0 },
232 	{ -1,		-1,		0,	NULL,
233 	  NULL,		NULL,		NULL,	0 }
234 };
235 #endif
236 
237 struct protox atalkprotox[] = {
238 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
239 	  ddp_stats,	NULL,		"ddp",	0 },
240 	{ -1,		-1,		0,	NULL,
241 	  NULL,		NULL,		NULL,	0 }
242 };
243 
244 struct protox netgraphprotox[] = {
245 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
246 	  NULL,		NULL,		"ctrl",	0 },
247 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
248 	  NULL,		NULL,		"data",	0 },
249 	{ -1,		-1,		0,	NULL,
250 	  NULL,		NULL,		NULL,	0 }
251 };
252 
253 #ifdef IPX
254 struct protox ipxprotox[] = {
255 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
256 	  ipx_stats,	NULL,		"ipx",	0 },
257 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
258 	  spx_stats,	NULL,		"spx",	0 },
259 	{ -1,		-1,		0,	NULL,
260 	  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 #ifdef IPX
273 					 ipxprotox,
274 #endif
275 					 atalkprotox, NULL };
276 
277 static void printproto(struct protox *, const char *);
278 static void usage(void);
279 static struct protox *name2protox(const char *);
280 static struct protox *knownname(const char *);
281 
282 static kvm_t *kvmd;
283 static char *nlistf = NULL, *memf = NULL;
284 
285 int	Aflag;		/* show addresses of protocol control block */
286 int	aflag;		/* show all sockets (including servers) */
287 int	Bflag;		/* show information about bpf consumers */
288 int	bflag;		/* show i/f total bytes in/out */
289 int	dflag;		/* show i/f dropped packets */
290 int	gflag;		/* show group (multicast) routing or stats */
291 int	hflag;		/* show counters in human readable format */
292 int	iflag;		/* show interfaces */
293 int	Lflag;		/* show size of listen queues */
294 int	mflag;		/* show memory stats */
295 int	numeric_addr;	/* show addresses numerically */
296 int	numeric_port;	/* show ports numerically */
297 static int pflag;	/* show given protocol */
298 int	rflag;		/* show routing tables (or routing stats) */
299 int	sflag;		/* show protocol statistics */
300 int	tflag;		/* show i/f watchdog timers */
301 int	Wflag;		/* wide display */
302 int	zflag;		/* zero stats */
303 
304 int	interval;	/* repeat interval for i/f stats */
305 
306 char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
307 int	unit;		/* unit number for above */
308 
309 int	af;		/* address family */
310 
311 int
312 main(int argc, char *argv[])
313 {
314 	struct protox *tp = NULL;  /* for printing cblocks & stats */
315 	int ch;
316 
317 	af = AF_UNSPEC;
318 
319 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) != -1)
320 		switch(ch) {
321 		case 'A':
322 			Aflag = 1;
323 			break;
324 		case 'a':
325 			aflag = 1;
326 			break;
327 		case 'B':
328 			Bflag = 1;
329 			break;
330 		case 'b':
331 			bflag = 1;
332 			break;
333 		case 'd':
334 			dflag = 1;
335 			break;
336 		case 'f':
337 			if (strcmp(optarg, "ipx") == 0)
338 				af = AF_IPX;
339 			else if (strcmp(optarg, "inet") == 0)
340 				af = AF_INET;
341 #ifdef INET6
342 			else if (strcmp(optarg, "inet6") == 0)
343 				af = AF_INET6;
344 #endif
345 #ifdef IPSEC
346 			else if (strcmp(optarg, "pfkey") == 0)
347 				af = PF_KEY;
348 #endif
349 			else if (strcmp(optarg, "unix") == 0)
350 				af = AF_UNIX;
351 			else if (strcmp(optarg, "atalk") == 0)
352 				af = AF_APPLETALK;
353 			else if (strcmp(optarg, "ng") == 0
354 			    || strcmp(optarg, "netgraph") == 0)
355 				af = AF_NETGRAPH;
356 			else if (strcmp(optarg, "link") == 0)
357 				af = AF_LINK;
358 			else {
359 				errx(1, "%s: unknown address family", optarg);
360 			}
361 			break;
362 		case 'g':
363 			gflag = 1;
364 			break;
365 		case 'h':
366 			hflag = 1;
367 			break;
368 		case 'I': {
369 			char *cp;
370 
371 			iflag = 1;
372 			for (cp = interface = optarg; isalpha(*cp); cp++)
373 				continue;
374 			unit = atoi(cp);
375 			break;
376 		}
377 		case 'i':
378 			iflag = 1;
379 			break;
380 		case 'L':
381 			Lflag = 1;
382 			break;
383 		case 'M':
384 			memf = optarg;
385 			break;
386 		case 'm':
387 			mflag = 1;
388 			break;
389 		case 'N':
390 			nlistf = optarg;
391 			break;
392 		case 'n':
393 			numeric_addr = numeric_port = 1;
394 			break;
395 		case 'p':
396 			if ((tp = name2protox(optarg)) == NULL) {
397 				errx(1,
398 				     "%s: unknown or uninstrumented protocol",
399 				     optarg);
400 			}
401 			pflag = 1;
402 			break;
403 		case 'r':
404 			rflag = 1;
405 			break;
406 		case 's':
407 			++sflag;
408 			break;
409 		case 'S':
410 			numeric_addr = 1;
411 			break;
412 		case 't':
413 			tflag = 1;
414 			break;
415 		case 'u':
416 			af = AF_UNIX;
417 			break;
418 		case 'W':
419 		case 'l':
420 			Wflag = 1;
421 			break;
422 		case 'w':
423 			interval = atoi(optarg);
424 			iflag = 1;
425 			break;
426 		case 'z':
427 			zflag = 1;
428 			break;
429 		case '?':
430 		default:
431 			usage();
432 		}
433 	argv += optind;
434 	argc -= optind;
435 
436 #define	BACKWARD_COMPATIBILITY
437 #ifdef	BACKWARD_COMPATIBILITY
438 	if (*argv) {
439 		if (isdigit(**argv)) {
440 			interval = atoi(*argv);
441 			if (interval <= 0)
442 				usage();
443 			++argv;
444 			iflag = 1;
445 		}
446 		if (*argv) {
447 			nlistf = *argv;
448 			if (*++argv)
449 				memf = *argv;
450 		}
451 	}
452 #endif
453 
454 	/*
455 	 * Discard setgid privileges if not the running kernel so that bad
456 	 * guys can't print interesting stuff from kernel memory.
457 	 */
458 	if (nlistf != NULL || memf != NULL)
459 		setgid(getgid());
460 
461 	if (Bflag) {
462 		bpf_stats(interface);
463 		exit(0);
464 	}
465 	if (mflag) {
466 		if (memf != NULL) {
467 			if (kread(0, 0, 0) == 0)
468 				mbpr(kvmd, nl[N_MBSTAT].n_value);
469 		} else
470 			mbpr(NULL, 0);
471 		exit(0);
472 	}
473 #if 0
474 	/*
475 	 * Keep file descriptors open to avoid overhead
476 	 * of open/close on each call to get* routines.
477 	 */
478 	sethostent(1);
479 	setnetent(1);
480 #else
481 	/*
482 	 * This does not make sense any more with DNS being default over
483 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
484 	 * used for the queries, which is slower.
485 	 */
486 #endif
487 	if (iflag && !sflag) {
488 		kread(0, 0, 0);
489 		intpr(interval, nl[N_IFNET].n_value, NULL);
490 		exit(0);
491 	}
492 	if (rflag) {
493 		kread(0, 0, 0);
494 		if (sflag)
495 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
496 		else
497 			routepr(nl[N_RTREE].n_value);
498 		exit(0);
499 	}
500 	if (gflag) {
501 		kread(0, 0, 0);
502 		if (sflag) {
503 			if (af == AF_INET || af == AF_UNSPEC)
504 				mrt_stats(nl[N_MRTSTAT].n_value);
505 #ifdef INET6
506 			if (af == AF_INET6 || af == AF_UNSPEC)
507 				mrt6_stats(nl[N_MRT6STAT].n_value);
508 #endif
509 		} else {
510 			if (af == AF_INET || af == AF_UNSPEC)
511 				mroutepr(nl[N_MFCTABLE].n_value,
512 					 nl[N_VIFTABLE].n_value);
513 #ifdef INET6
514 			if (af == AF_INET6 || af == AF_UNSPEC)
515 				mroute6pr(nl[N_MF6CTABLE].n_value,
516 					  nl[N_MIF6TABLE].n_value);
517 #endif
518 		}
519 		ifmalist_dump();
520 		exit(0);
521 	}
522 
523 	kread(0, 0, 0);
524 	if (tp) {
525 #ifdef FAST_IPSEC
526 		/*
527 		 * HACK: fallback to printing the new FAST IPSEC stats
528 		 *	 if the kernel was built with FAST_IPSEC rather
529 		 *	 than the KAME IPSEC stack (the two are mutually
530 		 *	 exclusive).
531 		 */
532 		if (nl[tp->pr_sindex].n_value == 0 &&
533 		    strcmp(tp->pr_name, "ipsec") == 0)
534 			tp = name2protox("fastipsec");
535 #endif
536 		printproto(tp, tp->pr_name);
537 		exit(0);
538 	}
539 	if (af == AF_INET || af == AF_UNSPEC)
540 		for (tp = protox; tp->pr_name; tp++)
541 			printproto(tp, tp->pr_name);
542 #ifdef INET6
543 	if (af == AF_INET6 || af == AF_UNSPEC)
544 		for (tp = ip6protox; tp->pr_name; tp++)
545 			printproto(tp, tp->pr_name);
546 #endif /*INET6*/
547 #ifdef IPSEC
548 	if (af == PF_KEY || af == AF_UNSPEC)
549 		for (tp = pfkeyprotox; tp->pr_name; tp++)
550 			printproto(tp, tp->pr_name);
551 #endif /*IPSEC*/
552 #ifdef IPX
553 	if (af == AF_IPX || af == AF_UNSPEC) {
554 		kread(0, 0, 0);
555 		for (tp = ipxprotox; tp->pr_name; tp++)
556 			printproto(tp, tp->pr_name);
557 	}
558 #endif /* IPX */
559 	if (af == AF_APPLETALK || af == AF_UNSPEC)
560 		for (tp = atalkprotox; tp->pr_name; tp++)
561 			printproto(tp, tp->pr_name);
562 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
563 		for (tp = netgraphprotox; tp->pr_name; tp++)
564 			printproto(tp, tp->pr_name);
565 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
566 		unixpr();
567 	exit(0);
568 }
569 
570 /*
571  * Print out protocol statistics or control blocks (per sflag).
572  * If the interface was not specifically requested, and the symbol
573  * is not in the namelist, ignore this one.
574  */
575 static void
576 printproto(tp, name)
577 	struct protox *tp;
578 	const char *name;
579 {
580 	void (*pr)(u_long, const char *, int);
581 	u_long off;
582 
583 	if (sflag) {
584 		if (iflag) {
585 			if (tp->pr_istats)
586 				intpr(interval, nl[N_IFNET].n_value,
587 				      tp->pr_istats);
588 			else if (pflag)
589 				printf("%s: no per-interface stats routine\n",
590 				    tp->pr_name);
591 			return;
592 		}
593 		else {
594 			pr = tp->pr_stats;
595 			if (!pr) {
596 				if (pflag)
597 					printf("%s: no stats routine\n",
598 					    tp->pr_name);
599 				return;
600 			}
601 			off = tp->pr_usesysctl ? tp->pr_usesysctl
602 				: nl[tp->pr_sindex].n_value;
603 		}
604 	} else {
605 		pr = tp->pr_cblocks;
606 		if (!pr) {
607 			if (pflag)
608 				printf("%s: no PCB routine\n", tp->pr_name);
609 			return;
610 		}
611 		off = tp->pr_usesysctl ? tp->pr_usesysctl
612 			: nl[tp->pr_index].n_value;
613 	}
614 	if (pr != NULL && (off || af != AF_UNSPEC))
615 		(*pr)(off, name, af);
616 }
617 
618 /*
619  * Read kernel memory, return 0 on success.
620  */
621 int
622 kread(u_long addr, char *buf, int size)
623 {
624 	if (kvmd == 0) {
625 		/*
626 		 * XXX.
627 		 */
628 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
629 		setgid(getgid());
630 		if (kvmd != NULL) {
631 			if (kvm_nlist(kvmd, nl) < 0) {
632 				if(nlistf)
633 					errx(1, "%s: kvm_nlist: %s", nlistf,
634 					     kvm_geterr(kvmd));
635 				else
636 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
637 			}
638 
639 			if (nl[0].n_type == 0) {
640 				if(nlistf)
641 					errx(1, "%s: no namelist", nlistf);
642 				else
643 					errx(1, "no namelist");
644 			}
645 		} else {
646 			warnx("kvm not available");
647 			return(-1);
648 		}
649 	}
650 	if (!buf)
651 		return (0);
652 	if (kvm_read(kvmd, addr, buf, size) != size) {
653 		warnx("%s", kvm_geterr(kvmd));
654 		return (-1);
655 	}
656 	return (0);
657 }
658 
659 const char *
660 plural(uintmax_t n)
661 {
662 	return (n != 1 ? "s" : "");
663 }
664 
665 const char *
666 plurales(uintmax_t n)
667 {
668 	return (n != 1 ? "es" : "");
669 }
670 
671 const char *
672 pluralies(uintmax_t n)
673 {
674 	return (n != 1 ? "ies" : "y");
675 }
676 
677 /*
678  * Find the protox for the given "well-known" name.
679  */
680 static struct protox *
681 knownname(const char *name)
682 {
683 	struct protox **tpp, *tp;
684 
685 	for (tpp = protoprotox; *tpp; tpp++)
686 		for (tp = *tpp; tp->pr_name; tp++)
687 			if (strcmp(tp->pr_name, name) == 0)
688 				return (tp);
689 	return (NULL);
690 }
691 
692 /*
693  * Find the protox corresponding to name.
694  */
695 static struct protox *
696 name2protox(const char *name)
697 {
698 	struct protox *tp;
699 	char **alias;			/* alias from p->aliases */
700 	struct protoent *p;
701 
702 	/*
703 	 * Try to find the name in the list of "well-known" names. If that
704 	 * fails, check if name is an alias for an Internet protocol.
705 	 */
706 	if ((tp = knownname(name)) != NULL)
707 		return (tp);
708 
709 	setprotoent(1);			/* make protocol lookup cheaper */
710 	while ((p = getprotoent()) != NULL) {
711 		/* assert: name not same as p->name */
712 		for (alias = p->p_aliases; *alias; alias++)
713 			if (strcmp(name, *alias) == 0) {
714 				endprotoent();
715 				return (knownname(p->p_name));
716 			}
717 	}
718 	endprotoent();
719 	return (NULL);
720 }
721 
722 static void
723 usage(void)
724 {
725 	(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",
726 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
727 "               [-M core] [-N system]",
728 "       netstat -i | -I interface [-abdhnt] [-f address_family]\n"
729 "               [-M core] [-N system]",
730 "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
731 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
732 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
733 "               [-M core] [-N system]",
734 "       netstat -m [-M core] [-N system]",
735 "       netstat -B [ -I interface]",
736 "       netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
737 "       netstat -rs [-s] [-M core] [-N system]",
738 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
739 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
740 	exit(1);
741 }
742