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