xref: /dragonfly/usr.bin/netstat/main.c (revision dc71b7ab)
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. 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  * @(#) Copyright (c) 1983, 1988, 1993 Regents of the University of California.  All rights reserved.
30  * @(#)main.c	8.4 (Berkeley) 3/1/94
31  * $FreeBSD: src/usr.bin/netstat/main.c,v 1.34.2.12 2001/09/17 15:17:46 ru Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 
39 #include <netinet/in.h>
40 
41 #include <netgraph/socket/ng_socket.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <kvm.h>
47 #include <limits.h>
48 #include <netdb.h>
49 #include <nlist.h>
50 #include <paths.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include "netstat.h"
56 
57 static struct nlist nl[] = {
58 #define	N_IFNET		0
59 	{ .n_name = "_ifnet" },
60 #define	N_IMP		1
61 	{ .n_name = "_imp_softc" },
62 #define	N_RTSTAT	2
63 	{ .n_name = "_rtstat" },
64 #define	N_UNIXSW	3
65 	{ .n_name = "_localsw" },
66 #define N_IDP		4
67 	{ .n_name = "_nspcb"},
68 #define N_IDPSTAT	5
69 	{ .n_name = "_idpstat"},
70 #define N_SPPSTAT	6
71 	{ .n_name = "_spp_istat"},
72 #define N_NSERR		7
73 	{ .n_name = "_ns_errstat"},
74 #define	N_CLNPSTAT	8
75 	{ .n_name = "_clnp_stat"},
76 #define	IN_NOTUSED	9
77 	{ .n_name = "_tp_inpcb" },
78 #define	ISO_TP		10
79 	{ .n_name = "_tp_refinfo" },
80 #define	N_TPSTAT	11
81 	{ .n_name = "_tp_stat" },
82 #define	N_ESISSTAT	12
83 	{ .n_name = "_esis_stat"},
84 #define N_NIMP		13
85 	{ .n_name = "_nimp"},
86 #define N_RTREE		14
87 	{ .n_name = "_rt_tables"},
88 #define N_CLTP		15
89 	{ .n_name = "_cltb"},
90 #define N_CLTPSTAT	16
91 	{ .n_name = "_cltpstat"},
92 #define	N_NFILE		17
93 	{ .n_name = "_nfile" },
94 #define	N_FILE		18
95 	{ .n_name = "_file" },
96 #define N_MRTSTAT	19
97 	{ .n_name = "_mrtstat" },
98 #define N_MFCTABLE	20
99 	{ .n_name = "_mfctable" },
100 #define N_VIFTABLE	21
101 	{ .n_name = "_viftable" },
102 #define N_IPX		22
103 	{ .n_name = "_ipxpcb"},
104 #define N_IPXSTAT	23
105 	{ .n_name = "_ipxstat"},
106 #define N_SPXSTAT	24
107 	{ .n_name = "_spx_istat"},
108 #define N_NGSOCKS	25
109 	{ .n_name = "_ngsocklist"},
110 #define N_IP6STAT	26
111 	{ .n_name = "_ip6stat" },
112 #define N_ICMP6STAT	27
113 	{ .n_name = "_icmp6stat" },
114 #define N_IPSECSTAT	28
115 	{ .n_name = "_ipsecstat" },
116 #define N_IPSEC6STAT	29
117 	{ .n_name = "_ipsec6stat" },
118 #define N_PIM6STAT	30
119 	{ .n_name = "_pim6stat" },
120 #define N_MRT6PROTO	31
121 	{ .n_name = "_ip6_mrtproto" },
122 #define N_MRT6STAT	32
123 	{ .n_name = "_mrt6stat" },
124 #define N_MF6CTABLE	33
125 	{ .n_name = "_mf6ctable" },
126 #define N_MIF6TABLE	34
127 	{ .n_name = "_mif6table" },
128 #define N_PFKEYSTAT	35
129 	{ .n_name = "_pfkeystat" },
130 #define N_MBSTAT	36
131 	{ .n_name = "_mbstat" },
132 #define N_MBTYPES	37
133 	{ .n_name = "_mbtypes" },
134 #define N_NMBCLUSTERS	38
135 	{ .n_name = "_nmbclusters" },
136 #define N_NMBUFS	39
137 	{ .n_name = "_nmbufs" },
138 #define	N_RTTRASH	40
139 	{ .n_name = "_rttrash" },
140 #define	N_NCPUS		41
141 	{ .n_name = "_ncpus" },
142 #define	N_CARPSTAT	42
143 	{ .n_name = "_carpstats" },
144 	{ .n_name = NULL },
145 };
146 
147 struct protox {
148 	u_char	pr_index;		/* index into nlist of cb head */
149 	u_char	pr_sindex;		/* index into nlist of stat block */
150 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
151 	void	(*pr_cblocks)(u_long, const char *, int);
152 					/* control blocks printing routine */
153 	void	(*pr_stats)(u_long, const char *, int);
154 					/* statistics printing routine */
155 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
156 	const char *pr_name;		/* well-known name */
157 	u_int	pr_usesysctl;		/* true if we use sysctl, not kvm */
158 } protox[] = {
159 	{ -1,		-1,		1,	protopr,
160 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
161 	{ -1,		-1,		1,	protopr,
162 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
163 	{ -1,		-1,		1,	protopr,
164 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
165 	{ -1,		-1,		1,	protopr,
166 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
167 	{ -1,		-1,		1,	protopr,
168 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
169 	{ -1,		-1,		1,	protopr,
170 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
171 #ifdef IPSEC
172 	{ -1,		N_IPSECSTAT,	1,	0,
173 	  ipsec_stats,	NULL,		"ipsec",	0},
174 #endif
175         { -1,           N_CARPSTAT,     1,      0,
176           carp_stats,   NULL,           "carp",         0},
177 	{ -1,		-1,		0,	0,
178 	  0,		NULL,		NULL,		0}
179 };
180 
181 #ifdef INET6
182 struct protox ip6protox[] = {
183 	{ -1,		-1,		1,	protopr,
184 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
185 	{ -1,		-1,		1,	protopr,
186 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
187 	{ -1,		N_IP6STAT,	1,	protopr,
188 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
189 	{ -1,		N_ICMP6STAT,	1,	protopr,
190 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
191 #ifdef IPSEC
192 	{ -1,		N_IPSEC6STAT,	1,	0,
193 	  ipsec_stats,	NULL,		"ipsec6",0 },
194 #endif
195 #ifdef notyet
196 	{ -1,		N_PIM6STAT,	1,	0,
197 	  pim6_stats,	NULL,		"pim6",	0 },
198 #endif
199 	{ -1,		-1,		1,	0,
200 	  rip6_stats,	NULL,		"rip6",	0 },
201 	{ -1,		-1,		1,	protopr,
202 	  pim_stats,	NULL,		"pim",	IPPROTO_PIM },
203 	{ -1,		-1,		0,	0,
204 	  0,		NULL,		0,	0 }
205 };
206 #endif /*INET6*/
207 
208 #ifdef IPSEC
209 struct protox pfkeyprotox[] = {
210 	{ -1,		N_PFKEYSTAT,	1,	0,
211 	  pfkey_stats,	NULL,		"pfkey", 0 },
212 	{ -1,		-1,		0,	0,
213 	  0,		NULL,		0,	0 }
214 };
215 #endif
216 
217 struct protox netgraphprotox[] = {
218 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
219 	  NULL,		NULL,		"ctrl",	0 },
220 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
221 	  NULL,		NULL,		"data",	0 },
222 	{ -1,		-1,		0,	0,
223 	  0,		NULL,		NULL,	0 }
224 };
225 
226 struct protox ipxprotox[] = {
227 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
228 	  ipx_stats,	NULL,		"ipx",	0 },
229 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
230 	  spx_stats,	NULL,		"spx",	0 },
231 	{ -1,		-1,		0,	0,
232 	  0,		NULL,		0,	0 }
233 };
234 
235 #ifdef ISO
236 struct protox isoprotox[] = {
237 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
238 	  tp_stats,	NULL,		"tp" },
239 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
240 	  cltp_stats,	NULL,		"cltp" },
241 	{ -1,		N_CLNPSTAT,	1,	 0,
242 	  clnp_stats,	NULL,		"clnp"},
243 	{ -1,		N_ESISSTAT,	1,	 0,
244 	  esis_stats,	NULL,		"esis"},
245 	{ -1,		-1,		0,	0,
246 	  0,		NULL,		0 }
247 };
248 #endif
249 
250 struct protox *protoprotox[] = {
251 					 protox,
252 #ifdef INET6
253 					 ip6protox,
254 #endif
255 #ifdef IPSEC
256 					 pfkeyprotox,
257 #endif
258 					 ipxprotox,
259 #ifdef ISO
260 					 isoprotox,
261 #endif
262 					 NULL };
263 
264 static void printproto (struct protox *, const char *, u_long);
265 static void usage (void);
266 static struct protox *name2protox (char *);
267 static struct protox *knownname (char *);
268 
269 static kvm_t *kvmd;
270 static char *nlistf = NULL, *memf = NULL;
271 
272 int	Aflag;		/* show addresses of protocol control block */
273 int	aflag;		/* show all sockets (including servers) */
274 int	bflag;		/* show i/f total bytes in/out */
275 int	cpuflag = -1;	/* dump route table from specific cpu */
276 int	dflag;		/* show i/f dropped packets */
277 int	gflag;		/* show group (multicast) routing or stats */
278 int	iflag;		/* show interfaces */
279 int	Lflag;		/* show size of listen queues */
280 int	mflag;		/* show memory stats */
281 int	Pflag;		/* show more protocol info (go past 80 columns) */
282 int	numeric_addr;	/* show addresses numerically */
283 int	numeric_port;	/* show ports numerically */
284 static int pflag;	/* show given protocol */
285 int	rflag;		/* show routing tables (or routing stats) */
286 int	sflag;		/* show protocol statistics */
287 int	tflag;		/* show i/f watchdog timers */
288 int	Bflag;		/* show buffer limit instead of buffer use */
289 int	Wflag;		/* wide display */
290 int	zflag;		/* zero stats */
291 
292 int	interval;	/* repeat interval for i/f stats */
293 
294 char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
295 int	unit;		/* unit number for above */
296 
297 int	af;		/* address family */
298 
299 int
300 main(int argc, char **argv)
301 {
302 	struct protox *tp = NULL;  /* for printing cblocks & stats */
303 	int ch;
304 	int n;
305 
306 	af = AF_UNSPEC;
307 
308 	while ((ch = getopt(argc, argv, "Aabc:df:gI:iLlM:mN:nPp:rSsBtuWw:z")) != -1)
309 		switch(ch) {
310 		case 'A':
311 			Aflag = 1;
312 			break;
313 		case 'a':
314 			aflag = 1;
315 			break;
316 		case 'b':
317 			bflag = 1;
318 			break;
319 		case 'c':
320 			kread(0, 0, 0);
321 			kread(nl[N_NCPUS].n_value, (char *)&n, sizeof(n));
322 			cpuflag = strtol(optarg, NULL, 0);
323 			if (cpuflag < 0 || cpuflag >= n)
324 			    errx(1, "cpu %d does not exist", cpuflag);
325 			break;
326 		case 'd':
327 			dflag = 1;
328 			break;
329 		case 'f':
330 			if (strcmp(optarg, "ipx") == 0)
331 				af = AF_IPX;
332 			else if (strcmp(optarg, "inet") == 0)
333 				af = AF_INET;
334 #ifdef INET6
335 			else if (strcmp(optarg, "inet6") == 0)
336 				af = AF_INET6;
337 #endif /*INET6*/
338 #ifdef INET6
339 			else if (strcmp(optarg, "pfkey") == 0)
340 				af = PF_KEY;
341 #endif /*INET6*/
342 			else if (strcmp(optarg, "unix") == 0)
343 				af = AF_UNIX;
344 			else if (strcmp(optarg, "ng") == 0
345 			    || strcmp(optarg, "netgraph") == 0)
346 				af = AF_NETGRAPH;
347 #ifdef ISO
348 			else if (strcmp(optarg, "iso") == 0)
349 				af = AF_ISO;
350 #endif
351 			else if (strcmp(optarg, "link") == 0)
352 				af = AF_LINK;
353 			else if (strcmp(optarg, "mpls") == 0)
354 				af = AF_MPLS;
355 			else {
356 				errx(1, "%s: unknown address family", optarg);
357 			}
358 			break;
359 		case 'g':
360 			gflag = 1;
361 			break;
362 		case 'I': {
363 			char *cp;
364 
365 			iflag = 1;
366 			for (cp = interface = optarg; isalpha(*cp); cp++)
367 				continue;
368 			unit = atoi(cp);
369 			break;
370 		}
371 		case 'i':
372 			iflag = 1;
373 			break;
374 		case 'L':
375 			Lflag = 1;
376 			break;
377 		case 'M':
378 			memf = optarg;
379 			break;
380 		case 'm':
381 			mflag = 1;
382 			break;
383 		case 'N':
384 			nlistf = optarg;
385 			break;
386 		case 'n':
387 			numeric_addr = numeric_port = 1;
388 			break;
389 		case 'P':
390 			Pflag = 1;
391 			break;
392 		case 'p':
393 			if ((tp = name2protox(optarg)) == NULL) {
394 				errx(1,
395 				     "%s: unknown or uninstrumented protocol",
396 				     optarg);
397 			}
398 			pflag = 1;
399 			break;
400 		case 'r':
401 			rflag = 1;
402 			break;
403 		case 's':
404 			++sflag;
405 			break;
406 		case 'S':
407 			numeric_addr = 1;
408 			break;
409 		case 'B':
410 			Bflag = 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 (mflag) {
462 		if (memf != NULL) {
463 			if (kread(0, 0, 0) == 0)
464 				mbpr(nl[N_MBSTAT].n_value,
465 				    nl[N_MBTYPES].n_value,
466 				    nl[N_NMBCLUSTERS].n_value,
467 				    nl[N_NMBUFS].n_value,
468 				    nl[N_NCPUS].n_value);
469 		} else {
470 			mbpr(0, 0, 0, 0, 0);
471 		}
472 		exit(0);
473 	}
474 #if 0
475 	/*
476 	 * Keep file descriptors open to avoid overhead
477 	 * of open/close on each call to get* routines.
478 	 */
479 	sethostent(1);
480 	setnetent(1);
481 #else
482 	/*
483 	 * This does not make sense any more with DNS being default over
484 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
485 	 * used for the queries, which is slower.
486 	 */
487 #endif
488 	if (iflag && !sflag) {
489 		kread(0, 0, 0);
490 		intpr(interval, nl[N_IFNET].n_value, NULL, nl[N_NCPUS].n_value);
491 		exit(0);
492 	}
493 	if (rflag) {
494 		kread(0, 0, 0);
495 		if (sflag)
496 			rt_stats();
497 		else
498 			routepr(nl[N_RTREE].n_value);
499 		exit(0);
500 	}
501 	if (gflag) {
502 		kread(0, 0, 0);
503 		if (sflag) {
504 			if (af == AF_INET || af == AF_UNSPEC)
505 				mrt_stats(nl[N_MRTSTAT].n_value);
506 #ifdef INET6
507 			if (af == AF_INET6 || af == AF_UNSPEC)
508 				mrt6_stats(nl[N_MRT6STAT].n_value);
509 #endif
510 		} else {
511 			if (af == AF_INET || af == AF_UNSPEC)
512 				mroutepr(nl[N_MFCTABLE].n_value,
513 					 nl[N_VIFTABLE].n_value);
514 #ifdef INET6
515 			if (af == AF_INET6 || af == AF_UNSPEC)
516 				mroute6pr(nl[N_MF6CTABLE].n_value,
517 					  nl[N_MIF6TABLE].n_value);
518 #endif
519 		}
520 		exit(0);
521 	}
522 
523 	kread(0, 0, 0);
524 	if (tp) {
525 		printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
526 		exit(0);
527 	}
528 	if (af == AF_INET || af == AF_UNSPEC)
529 		for (tp = protox; tp->pr_name; tp++)
530 			printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
531 #ifdef INET6
532 	if (af == AF_INET6 || af == AF_UNSPEC)
533 		for (tp = ip6protox; tp->pr_name; tp++)
534 			printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
535 #endif /*INET6*/
536 #ifdef IPSEC
537 	if (af == PF_KEY || af == AF_UNSPEC)
538 		for (tp = pfkeyprotox; tp->pr_name; tp++)
539 			printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
540 #endif /*IPSEC*/
541 	if (af == AF_IPX || af == AF_UNSPEC) {
542 		kread(0, 0, 0);
543 		for (tp = ipxprotox; tp->pr_name; tp++)
544 			printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
545 	}
546 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
547 		for (tp = netgraphprotox; tp->pr_name; tp++)
548 			printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
549 #ifdef ISO
550 	if (af == AF_ISO || af == AF_UNSPEC)
551 		for (tp = isoprotox; tp->pr_name; tp++)
552 			printproto(tp, tp->pr_name, nl[N_NCPUS].n_value);
553 #endif
554 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
555 		unixpr();
556 	exit(0);
557 }
558 
559 /*
560  * Print out protocol statistics or control blocks (per sflag).
561  * If the interface was not specifically requested, and the symbol
562  * is not in the namelist, ignore this one.
563  */
564 static void
565 printproto(struct protox *tp, const char *name, u_long ncpusaddr)
566 {
567 	void (*pr)(u_long, const char *, int);
568 	u_long off;
569 
570 	if (sflag) {
571 		if (iflag) {
572 			if (tp->pr_istats)
573 				intpr(interval, nl[N_IFNET].n_value,
574 				      tp->pr_istats, ncpusaddr);
575 			else if (pflag)
576 				printf("%s: no per-interface stats routine\n",
577 				    tp->pr_name);
578 			return;
579 		}
580 		else {
581 			pr = tp->pr_stats;
582 			if (!pr) {
583 				if (pflag)
584 					printf("%s: no stats routine\n",
585 					    tp->pr_name);
586 				return;
587 			}
588 			off = tp->pr_usesysctl ? tp->pr_usesysctl
589 				: nl[tp->pr_sindex].n_value;
590 		}
591 	} else {
592 		pr = tp->pr_cblocks;
593 		if (!pr) {
594 			if (pflag)
595 				printf("%s: no PCB routine\n", tp->pr_name);
596 			return;
597 		}
598 		off = tp->pr_usesysctl ? tp->pr_usesysctl
599 			: nl[tp->pr_index].n_value;
600 	}
601 	if (pr != NULL && (off || af != AF_UNSPEC))
602 		(*pr)(off, name, af);
603 }
604 
605 /*
606  * Read kernel memory, return 0 on success.
607  */
608 int
609 kread(u_long addr, char *buf, int size)
610 {
611 	if (kvmd == NULL) {
612 		/*
613 		 * XXX.
614 		 */
615 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
616 		if (kvmd != NULL) {
617 			if (kvm_nlist(kvmd, nl) < 0) {
618 				if(nlistf)
619 					errx(1, "%s: kvm_nlist: %s", nlistf,
620 					     kvm_geterr(kvmd));
621 				else
622 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
623 			}
624 
625 			if (nl[0].n_type == 0) {
626 				if(nlistf)
627 					errx(1, "%s: no namelist", nlistf);
628 				else
629 					errx(1, "no namelist");
630 			}
631 		} else {
632 			warnx("kvm not available");
633 			return(-1);
634 		}
635 	}
636 	if (!buf)
637 		return (0);
638 	if (kvm_read(kvmd, addr, buf, size) != size) {
639 		warnx("%s", kvm_geterr(kvmd));
640 		return (-1);
641 	}
642 	return (0);
643 }
644 
645 const char *
646 plural(int n)
647 {
648 	return (n != 1 ? "s" : "");
649 }
650 
651 const char *
652 plurales(int n)
653 {
654 	return (n != 1 ? "es" : "");
655 }
656 
657 /*
658  * Find the protox for the given "well-known" name.
659  */
660 static struct protox *
661 knownname(char *name)
662 {
663 	struct protox **tpp, *tp;
664 
665 	for (tpp = protoprotox; *tpp; tpp++)
666 		for (tp = *tpp; tp->pr_name; tp++)
667 			if (strcmp(tp->pr_name, name) == 0)
668 				return (tp);
669 	return (NULL);
670 }
671 
672 /*
673  * Find the protox corresponding to name.
674  */
675 static struct protox *
676 name2protox(char *name)
677 {
678 	struct protox *tp;
679 	char **alias;			/* alias from p->aliases */
680 	struct protoent *p;
681 
682 	/*
683 	 * Try to find the name in the list of "well-known" names. If that
684 	 * fails, check if name is an alias for an Internet protocol.
685 	 */
686 	if ((tp = knownname(name)) != NULL)
687 		return (tp);
688 
689 	setprotoent(1);			/* make protocol lookup cheaper */
690 	while ((p = getprotoent()) != NULL) {
691 		/* assert: name not same as p->name */
692 		for (alias = p->p_aliases; *alias; alias++)
693 			if (strcmp(name, *alias) == 0) {
694 				endprotoent();
695 				return (knownname(p->p_name));
696 			}
697 	}
698 	endprotoent();
699 	return (NULL);
700 }
701 
702 static void
703 usage(void)
704 {
705 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
706 "usage: netstat [-AaLnPSW] [-c cpu] [-f protocol_family | -p protocol]\n"
707 "               [-M core] [-N system]",
708 "       netstat -i | -I interface [-aBbdnt] [-f address_family]\n"
709 "               [-M core] [-N system]",
710 "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
711 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
712 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
713 "               [-M core] [-N system]",
714 "       netstat -m [-M core] [-N system]",
715 "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
716 "       netstat -rs [-s] [-M core] [-N system]",
717 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
718 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
719 	exit(1);
720 }
721