xref: /freebsd/usr.bin/netstat/main.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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 <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 	{ "_ifnet" },
75 #define	N_RTSTAT	1
76 	{ "_rtstat" },
77 #define N_RTREE		2
78 	{ "_rt_tables"},
79 #define N_MRTSTAT	3
80 	{ "_mrtstat" },
81 #define N_MFCTABLE	4
82 	{ "_mfctable" },
83 #define N_VIFTABLE	5
84 	{ "_viftable" },
85 #define N_IPX		6
86 	{ "_ipxpcb_list"},
87 #define N_IPXSTAT	7
88 	{ "_ipxstat"},
89 #define N_SPXSTAT	8
90 	{ "_spx_istat"},
91 #define N_DDPSTAT	9
92 	{ "_ddpstat"},
93 #define N_DDPCB		10
94 	{ "_ddpcb"},
95 #define N_NGSOCKS	11
96 	{ "_ngsocklist"},
97 #define N_IP6STAT	12
98 	{ "_ip6stat" },
99 #define N_ICMP6STAT	13
100 	{ "_icmp6stat" },
101 #define N_IPSECSTAT	14
102 	{ "_ipsecstat" },
103 #define N_IPSEC6STAT	15
104 	{ "_ipsec6stat" },
105 #define N_PIM6STAT	16
106 	{ "_pim6stat" },
107 #define N_MRT6STAT	17
108 	{ "_mrt6stat" },
109 #define N_MF6CTABLE	18
110 	{ "_mf6ctable" },
111 #define N_MIF6TABLE	19
112 	{ "_mif6table" },
113 #define N_PFKEYSTAT	20
114 	{ "_pfkeystat" },
115 #define N_MBSTAT	21
116 	{ "_mbstat" },
117 #define N_MBTYPES	22
118 	{ "_mbtypes" },
119 #define N_NMBCLUSTERS	23
120 	{ "_nmbclusters" },
121 #define N_NMBUFS	24
122 	{ "_nmbufs" },
123 #define	N_MBHI		25
124 	{ "_mbuf_hiwm" },
125 #define	N_CLHI		26
126 	{ "_clust_hiwm" },
127 #define	N_NCPUS		27
128 	{ "_smp_cpus" },
129 #define	N_PAGESZ	28
130 	{ "_pagesize" },
131 #define	N_MBPSTAT	29
132 	{ "_mb_statpcpu" },
133 #define	N_RTTRASH	30
134 	{ "_rttrash" },
135 #define	N_MBLO		31
136 	{ "_mbuf_lowm" },
137 #define	N_CLLO		32
138 	{ "_clust_lowm" },
139 #define N_CARPSTAT	33
140 	{ "_carpstats" },
141 #define N_PFSYNCSTAT	34
142 	{ "_pfsyncstats" },
143 #define	N_FAST_IPSECSTAT 35
144 	{ "_newipsecstat" },
145 #define	N_AHSTAT	36
146 	{ "_ahstat" },
147 #define	N_ESPSTAT	37
148 	{ "_espstat" },
149 #define	N_IPCOMPSTAT	38
150 	{ "_ipcompstat" },
151 	{ "" },
152 };
153 
154 struct protox {
155 	u_char	pr_index;		/* index into nlist of cb head */
156 	u_char	pr_sindex;		/* index into nlist of stat block */
157 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
158 	void	(*pr_cblocks)(u_long, const char *, int);
159 					/* control blocks printing routine */
160 	void	(*pr_stats)(u_long, const char *, int);
161 					/* statistics printing routine */
162 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
163 	const char	*pr_name;		/* well-known name */
164 	u_long	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
165 } protox[] = {
166 	{ -1,		-1,		1,	protopr,
167 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
168 	{ -1,		-1,		1,	protopr,
169 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
170 	{ -1,		-1,		1,	protopr,
171 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
172 	{ -1,		-1,		1,	protopr,
173 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
174 	{ -1,		-1,		1,	protopr,
175 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
176 	{ -1,		-1,		1,	protopr,
177 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
178 #ifdef IPSEC
179 	{ -1,		N_IPSECSTAT,	1,	NULL,
180 	  ipsec_stats,	NULL,		"ipsec",	0},
181 #ifdef FAST_IPSEC
182 	{ -1,		N_FAST_IPSECSTAT, 1,	0,
183 	  ipsec_stats_new, NULL,	"fastipsec",	0},
184 	{ -1,		N_AHSTAT,	1,	0,
185 	  ah_stats,	NULL,		"ah",		0},
186 	{ -1,		N_ESPSTAT,	1,	0,
187 	  esp_stats,	NULL,		"esp",		0},
188 	{ -1,		N_IPCOMPSTAT,	1,	0,
189 	  ipcomp_stats,	NULL,		"ipcomp",	0},
190 #endif
191 #endif
192 	{ -1,		-1,		1,	protopr,
193 	  pim_stats,	NULL,		"pim",	IPPROTO_PIM },
194 	{ -1,		N_CARPSTAT,	1,	0,
195 	  carp_stats,	NULL,		"carp",		0},
196 	{ -1,		-1,		1,	NULL,
197 	  pfsync_stats,	NULL,		"pfsync",	1},
198 	{ -1,		-1,		0,	NULL,
199 	  NULL,		NULL,		NULL,	0 }
200 };
201 
202 #ifdef INET6
203 struct protox ip6protox[] = {
204 	{ -1,		-1,		1,	protopr,
205 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
206 	{ -1,		-1,		1,	protopr,
207 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
208 	{ -1,		N_IP6STAT,	1,	protopr,
209 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
210 	{ -1,		N_ICMP6STAT,	1,	protopr,
211 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
212 #ifdef IPSEC
213 	{ -1,		N_IPSEC6STAT,	1,	NULL,
214 	  ipsec_stats,	NULL,		"ipsec6",0 },
215 #endif
216 #ifdef notyet
217 	{ -1,		N_PIM6STAT,	1,	NULL,
218 	  pim6_stats,	NULL,		"pim6",	0 },
219 #endif
220 	{ -1,		-1,		1,	NULL,
221 	  rip6_stats,	NULL,		"rip6",	0 },
222 	{ -1,		-1,		0,	NULL,
223 	  NULL,		NULL,		NULL,	0 }
224 };
225 #endif /*INET6*/
226 
227 #ifdef IPSEC
228 struct protox pfkeyprotox[] = {
229 	{ -1,		N_PFKEYSTAT,	1,	NULL,
230 	  pfkey_stats,	NULL,		"pfkey", 0 },
231 	{ -1,		-1,		0,	NULL,
232 	  NULL,		NULL,		NULL,	0 }
233 };
234 #endif
235 
236 struct protox atalkprotox[] = {
237 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
238 	  ddp_stats,	NULL,		"ddp",	0 },
239 	{ -1,		-1,		0,	NULL,
240 	  NULL,		NULL,		NULL,	0 }
241 };
242 
243 struct protox netgraphprotox[] = {
244 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
245 	  NULL,		NULL,		"ctrl",	0 },
246 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
247 	  NULL,		NULL,		"data",	0 },
248 	{ -1,		-1,		0,	NULL,
249 	  NULL,		NULL,		NULL,	0 }
250 };
251 
252 #ifdef IPX
253 struct protox ipxprotox[] = {
254 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
255 	  ipx_stats,	NULL,		"ipx",	0 },
256 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
257 	  spx_stats,	NULL,		"spx",	0 },
258 	{ -1,		-1,		0,	NULL,
259 	  NULL,		NULL,		0,	0 }
260 };
261 #endif
262 
263 struct protox *protoprotox[] = {
264 					 protox,
265 #ifdef INET6
266 					 ip6protox,
267 #endif
268 #ifdef IPSEC
269 					 pfkeyprotox,
270 #endif
271 #ifdef IPX
272 					 ipxprotox,
273 #endif
274 					 atalkprotox, NULL };
275 
276 const char *pluralies(int);
277 static void printproto(struct protox *, const char *);
278 static void usage(void);
279 static struct protox *name2protox(char *);
280 static struct protox *knownname(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(int n)
661 {
662 	return (n != 1 ? "s" : "");
663 }
664 
665 const char *
666 plurales(int n)
667 {
668 	return (n != 1 ? "es" : "");
669 }
670 
671 const char *
672 pluralies(int 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(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(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