xref: /netbsd-src/usr.bin/netstat/main.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: main.c,v 1.70 2008/07/21 14:19:24 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
35  Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "from: @(#)main.c	8.4 (Berkeley) 3/1/94";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.70 2008/07/21 14:19:24 lukem Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 
51 #include <net/if.h>
52 #include <netinet/in.h>
53 
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <kvm.h>
58 #include <limits.h>
59 #include <netdb.h>
60 #include <nlist.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include "netstat.h"
67 
68 struct nlist nl[] = {
69 #define	N_MBSTAT	0
70 	{ "_mbstat" },
71 #define	N_IPSTAT	1
72 	{ "_ipstat" },		/* not available via kvm */
73 #define	N_TCBTABLE	2
74 	{ "_tcbtable" },
75 #define	N_TCPSTAT	3
76 	{ "_tcpstat" },		/* not available via kvm */
77 #define	N_UDBTABLE	4
78 	{ "_udbtable" },
79 #define	N_UDPSTAT	5
80 	{ "_udpstat" },		/* not available via kvm */
81 #define	N_IFNET		6
82 	{ "_ifnet" },
83 #define	N_IMP		7
84 	{ "_imp_softc" },
85 #define	N_ICMPSTAT	8
86 	{ "_icmpstat" },	/* not available via kvm */
87 #define	N_RTSTAT	9
88 	{ "_rtstat" },
89 #define	N_UNIXSW	10
90 	{ "_unixsw" },
91 #define	N_CLNPSTAT	11
92 	{ "_clnp_stat"},
93 #define	IN_NOTUSED	12
94 	{ "_tp_inpcb" },
95 #define	ISO_TP		13
96 	{ "_tp_refinfo" },
97 #define	N_TPSTAT	14
98 	{ "_tp_stat" },
99 #define	N_ESISSTAT	15
100 	{ "_esis_stat"},
101 #define N_NIMP		16
102 	{ "_nimp"},
103 #define N_RTREE		17
104 	{ "_rt_tables"},
105 #define N_CLTP		18
106 	{ "_cltb"},
107 #define N_CLTPSTAT	19
108 	{ "_cltpstat"},
109 #define	N_NFILE		20
110 	{ "_nfile" },
111 #define	N_FILE		21
112 	{ "_file" },
113 #define N_IGMPSTAT	22
114 	{ "_igmpstat" },	/* not available via kvm */
115 #define N_MRTPROTO	23
116 	{ "_ip_mrtproto" },
117 #define N_MRTSTAT	24
118 	{ "_mrtstat" },
119 #define N_MFCHASHTBL	25
120 	{ "_mfchashtbl" },
121 #define	N_MFCHASH	26
122 	{ "_mfchash" },
123 #define N_VIFTABLE	27
124 	{ "_viftable" },
125 #define N_MSIZE		28
126 	{ "_msize" },
127 #define N_MCLBYTES	29
128 	{ "_mclbytes" },
129 #define N_DDPSTAT	30
130 	{ "_ddpstat"},		/* not available via kvm */
131 #define N_DDPCB		31
132 	{ "_ddpcb"},
133 #define N_MBPOOL	32
134 	{ "_mbpool" },
135 #define N_MCLPOOL	33
136 	{ "_mclpool" },
137 #define N_DIVPCB	34
138 	{ "_divcb"},
139 #define N_DIVSTAT	35
140 	{ "_divstat"},
141 #define N_IP6STAT	36
142 	{ "_ip6stat" },		/* not available via kvm */
143 #define N_TCP6STAT	37
144 	{ "_tcp6stat" },	/* not available via kvm */
145 #define N_UDP6STAT	38
146 	{ "_udp6stat" },	/* not available via kvm */
147 #define N_ICMP6STAT	39
148 	{ "_icmp6stat" },	/* not available via kvm */
149 #define N_IPSECSTAT	40
150 	{ "_ipsecstat" },	/* not available via kvm */
151 #define N_IPSEC6STAT	41
152 	{ "_ipsec6stat" },	/* not available via kvm */
153 #define N_PIM6STAT	42
154 	{ "_pim6stat" },	/* not available via kvm */
155 #define N_MRT6PROTO	43
156 	{ "_ip6_mrtproto" },
157 #define N_MRT6STAT	44
158 	{ "_mrt6stat" },
159 #define N_MF6CTABLE	45
160 	{ "_mf6ctable" },
161 #define N_MIF6TABLE	46
162 	{ "_mif6table" },
163 #define N_PFKEYSTAT	47
164 	{ "_pfkeystat" },	/* not available via kvm */
165 #define N_ARPSTAT	48
166 	{ "_arpstat" },		/* not available via kvm */
167 #define N_RIP6STAT	49
168 	{ "_rip6stat" },	/* not available via kvm */
169 #define	N_ARPINTRQ	50
170 	{ "_arpintrq" },
171 #define	N_IPINTRQ	51
172 	{ "_ipintrq" },
173 #define	N_IP6INTRQ	52
174 	{ "_ip6intrq" },
175 #define	N_ATINTRQ1	53
176 	{ "_atintrq1" },
177 #define	N_ATINTRQ2	54
178 	{ "_atintrq2" },
179 #define	N_NSINTRQ	55
180 	{ "_nsintrq" },
181 #define	N_CLNLINTRQ	56
182 	{ "_clnlintrq" },
183 #define	N_LLCINTRQ	57
184 	{ "_llcintrq" },
185 #define	N_HDINTRQ	58
186 	{ "_hdintrq" },
187 #define	N_NATMINTRQ	59
188 	{ "_natmintrq" },
189 #define	N_PPPOEDISCINQ	61
190 	{ "_ppoediscinq" },
191 #define	N_PPPOEINQ	61
192 	{ "_ppoeinq" },
193 #define	N_PKINTRQ	62
194 	{ "_pkintrq" },
195 #define	N_HARDCLOCK_TICKS 63
196 	{ "_hardclock_ticks" },
197 #define N_PIMSTAT	64
198 	{ "_pimstat" },
199 #define N_CARPSTAT	65
200 	{ "_carpstats" },	/* not available via kvm */
201 	{ "" },
202 };
203 
204 struct protox {
205 	u_char	pr_index;		/* index into nlist of cb head */
206 	u_char	pr_sindex;		/* index into nlist of stat block */
207 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
208 	void	(*pr_cblocks)		/* control blocks printing routine */
209 			__P((u_long, char *));
210 	void	(*pr_stats)		/* statistics printing routine */
211 			__P((u_long, char *));
212 	void	(*pr_istats)
213 			__P((char *));	/* per/if statistics printing routine */
214 	void	(*pr_dump)		/* PCB state dump routine */
215 			__P((u_long));
216 	char	*pr_name;		/* well-known name */
217 } protox[] = {
218 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
219 	  tcp_stats,	NULL,		tcp_dump,	"tcp" },
220 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
221 	  udp_stats,	NULL,		0,	"udp" },
222 	{ -1,		N_IPSTAT,	1,	0,
223 	  ip_stats,	NULL,		0,	"ip" },
224 	{ -1,		N_ICMPSTAT,	1,	0,
225 	  icmp_stats,	NULL,		0,	"icmp" },
226 	{ -1,		N_IGMPSTAT,	1,	0,
227 	  igmp_stats,	NULL,		0,	"igmp" },
228 	{ -1,		N_CARPSTAT,	1,	0,
229 	  carp_stats,	NULL,		0,	"carp" },
230 #ifdef IPSEC
231 	{ -1,		N_IPSECSTAT,	1,	0,
232 	  ipsec_switch,	NULL,		0,	"ipsec" },
233 #endif
234 	{ -1,		N_PIMSTAT,	1,	0,
235 	  pim_stats,	NULL,		0,	"pim" },
236 	{ -1,		-1,		0,	0,
237 	  0,		NULL,		0,	0 }
238 };
239 
240 #ifdef INET6
241 struct protox ip6protox[] = {
242 	{ -1,		N_IP6STAT,	1,	0,
243 	  ip6_stats,	ip6_ifstats,	0,	"ip6" },
244 	{ -1,		N_ICMP6STAT,	1,	0,
245 	  icmp6_stats,	icmp6_ifstats,	0,	"icmp6" },
246 #ifdef TCP6
247 	{ N_TCBTABLE,	N_TCP6STAT,	1,	ip6protopr,
248 	  tcp6_stats,	NULL,		tcp6_dump,	"tcp6" },
249 #else
250 	{ N_TCBTABLE,	N_TCP6STAT,	1,	ip6protopr,
251 	  tcp_stats,	NULL,		tcp_dump,	"tcp6" },
252 #endif
253 	{ N_UDBTABLE,	N_UDP6STAT,	1,	ip6protopr,
254 	  udp6_stats,	NULL,		0,	"udp6" },
255 #ifdef IPSEC
256 	{ -1,		N_IPSEC6STAT,	1,	0,
257 	  ipsec_switch,	NULL,		0,	"ipsec6" },
258 #endif
259 	{ -1,		N_PIM6STAT,	1,	0,
260 	  pim6_stats,	NULL,		0,	"pim6" },
261 	{ -1,		N_RIP6STAT,	1,	0,
262 	  rip6_stats,	NULL,		0,	"rip6" },
263 	{ -1,		-1,		0,	0,
264 	  0,		NULL,		0,	0 }
265 };
266 #endif
267 
268 struct protox arpprotox[] = {
269 	{ -1,		N_ARPSTAT,	1,	0,
270 	  arp_stats,	NULL,		0,	"arp" },
271 	{ -1,		-1,		0,	0,
272 	  0,		NULL,		0,	0 }
273 };
274 
275 #ifdef IPSEC
276 struct protox pfkeyprotox[] = {
277 	{ -1,		N_PFKEYSTAT,	1,	0,
278 	  pfkey_stats,	NULL,		0,	"pfkey" },
279 	{ -1,		-1,		0,	0,
280 	  0,		NULL,		0,	0 }
281 };
282 #endif
283 
284 #ifndef SMALL
285 struct protox atalkprotox[] = {
286 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
287 	  ddp_stats,	NULL,		0,	"ddp" },
288 	{ -1,		-1,		0,	0,
289 	  0,		NULL,		0 }
290 };
291 
292 struct protox isoprotox[] = {
293 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
294 	  tp_stats,	NULL,		0,	"tp" },
295 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
296 	  cltp_stats,	NULL,		0,	"cltp" },
297 	{ -1,		N_CLNPSTAT,	1,	 0,
298 	  clnp_stats,	NULL,		0,	"clnp"},
299 	{ -1,		N_ESISSTAT,	1,	 0,
300 	  esis_stats,	NULL,		0,	"esis"},
301 	{ -1,		-1,		0,	0,
302 	  0,		NULL,		0,	0 }
303 };
304 #endif
305 
306 struct protox *protoprotox[] = { protox,
307 #ifdef INET6
308 				 ip6protox,
309 #endif
310 				 arpprotox,
311 #ifdef IPSEC
312 				 pfkeyprotox,
313 #endif
314 #ifndef SMALL
315 				 atalkprotox,
316 				 isoprotox,
317 #endif
318 				 NULL };
319 
320 const struct softintrq {
321 	const char *siq_name;
322 	int siq_index;
323 } softintrq[] = {
324 	{ "arpintrq", N_ARPINTRQ },
325 	{ "ipintrq", N_IPINTRQ },
326 	{ "ip6intrq", N_IP6INTRQ },
327 	{ "atintrq1", N_ATINTRQ1 },
328 	{ "atintrq2", N_ATINTRQ2 },
329 	{ "clnlintrq", N_CLNLINTRQ },
330 	{ "llcintrq", N_LLCINTRQ },
331 	{ "hdintrq", N_HDINTRQ },
332 	{ "natmintrq", N_NATMINTRQ },
333 	{ "ppoediscinq", N_PPPOEDISCINQ },
334 	{ "ppoeinq", N_PPPOEINQ },
335 	{ "pkintrq", N_PKINTRQ },
336 	{ NULL, -1 },
337 };
338 
339 int main __P((int, char *[]));
340 static void printproto __P((struct protox *, char *));
341 static void print_softintrq __P((void));
342 static void usage __P((void));
343 static struct protox *name2protox __P((char *));
344 static struct protox *knownname __P((char *));
345 static void prepare(char *, char *, struct protox *tp);
346 
347 kvm_t *kvmd;
348 gid_t egid;
349 
350 void
351 prepare(char *nlistf, char *memf, struct protox *tp)
352 {
353 	char buf[_POSIX2_LINE_MAX];
354 
355 	/*
356 	 * Try to figure out if we can use sysctl or not.
357 	 */
358 	if (nlistf != NULL && memf != NULL) {
359 		/* If we have -M and -N, we're not dealing with live memory. */
360 		use_sysctl = 0;
361 	} else if (qflag ||
362 		   rflag ||
363 		   iflag ||
364 #ifndef SMALL
365 		   gflag ||
366 		   (pflag && tp->pr_sindex == N_TPSTAT) ||
367 		   (pflag && tp->pr_sindex == N_CLTPSTAT) ||
368 		   (pflag && tp->pr_sindex == N_CLNPSTAT) ||
369 		   (pflag && tp->pr_sindex == N_ESISSTAT) ||
370 #endif
371 		   (pflag && tp->pr_sindex == N_PIMSTAT) ||
372 		   Pflag) {
373 		/* These flags are not yet supported via sysctl(3). */
374 		use_sysctl = 0;
375 	} else {
376 		/* We can use sysctl(3). */
377 		use_sysctl = 1;
378 	}
379 
380 	if (!use_sysctl) {
381 		(void)setegid(egid);
382 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
383 		(void)setgid(getgid());
384 		if (kvmd == NULL)
385 			err(1, "kvm error: %s", buf);
386 
387 		if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
388 			if (nlistf)
389 				errx(1, "%s: no namelist", nlistf);
390 			else
391 				errx(1, "no namelist");
392 		}
393 	} else
394 		(void)setgid(getgid());
395 }
396 
397 int
398 main(argc, argv)
399 	int argc;
400 	char *argv[];
401 {
402 	struct protoent *p;
403 	struct protox *tp;	/* for printing cblocks & stats */
404 	int ch;
405 	char *nlistf = NULL, *memf = NULL;
406 	char *cp;
407 	u_long pcbaddr;
408 
409 	egid = getegid();
410 	(void)setegid(getgid());
411 	tp = NULL;
412 	af = AF_UNSPEC;
413 	pcbaddr = 0;
414 
415 	while ((ch = getopt(argc, argv,
416 	    "AabBdf:ghI:LliM:mN:nP:p:qrsStuvw:")) != -1)
417 		switch (ch) {
418 		case 'A':
419 			Aflag = 1;
420 			break;
421 		case 'a':
422 			aflag = 1;
423 			break;
424 		case 'b':
425 			bflag = 1;
426 			break;
427 		case 'B':
428 			Bflag = 1;
429 			break;
430 		case 'd':
431 			dflag = 1;
432 			break;
433 		case 'f':
434 			if (strcmp(optarg, "inet") == 0)
435 				af = AF_INET;
436 			else if (strcmp(optarg, "inet6") == 0)
437 				af = AF_INET6;
438 			else if (strcmp(optarg, "arp") == 0)
439 				af = AF_ARP;
440 			else if (strcmp(optarg, "pfkey") == 0)
441 				af = PF_KEY;
442 			else if (strcmp(optarg, "unix") == 0
443 			    || strcmp(optarg, "local") == 0)
444 				af = AF_LOCAL;
445 			else if (strcmp(optarg, "iso") == 0)
446 				af = AF_ISO;
447 			else if (strcmp(optarg, "atalk") == 0)
448 				af = AF_APPLETALK;
449 			else
450 				errx(1, "%s: unknown address family",
451 				    optarg);
452 			break;
453 #ifndef SMALL
454 		case 'g':
455 			gflag = 1;
456 			break;
457 #endif
458 		case 'I':
459 			iflag = 1;
460 			interface = optarg;
461 			break;
462 		case 'i':
463 			iflag = 1;
464 			break;
465 		case 'L':
466 			Lflag = 1;
467 			break;
468 		case 'l':
469 			lflag = 1;
470 			break;
471 		case 'M':
472 			memf = optarg;
473 			break;
474 		case 'm':
475 			mflag = 1;
476 			break;
477 		case 'N':
478 			nlistf = optarg;
479 			break;
480 		case 'n':
481 			numeric_addr = numeric_port = nflag = 1;
482 			break;
483 		case 'P':
484 			errno = 0;
485 			pcbaddr = strtoul(optarg, &cp, 16);
486 			if (*cp != '\0' || errno == ERANGE)
487 				errx(1, "invalid PCB address %s",
488 				    optarg);
489 			Pflag = 1;
490 			break;
491 		case 'p':
492 			if ((tp = name2protox(optarg)) == NULL)
493 				errx(1, "%s: unknown or uninstrumented protocol",
494 				    optarg);
495 			pflag = 1;
496 			break;
497 		case 'q':
498 			qflag = 1;
499 			break;
500 		case 'r':
501 			rflag = 1;
502 			break;
503 		case 's':
504 			++sflag;
505 			break;
506 		case 'S':
507 			numeric_addr = 1;
508 			break;
509 		case 't':
510 			tflag = 1;
511 			break;
512 		case 'u':
513 			af = AF_LOCAL;
514 			break;
515 		case 'v':
516 			vflag++;
517 			break;
518 		case 'w':
519 			interval = atoi(optarg);
520 			iflag = 1;
521 			break;
522 		case '?':
523 		default:
524 			usage();
525 		}
526 	argv += optind;
527 	argc -= optind;
528 
529 #define	BACKWARD_COMPATIBILITY
530 #ifdef	BACKWARD_COMPATIBILITY
531 	if (*argv) {
532 		if (isdigit((unsigned char)**argv)) {
533 			interval = atoi(*argv);
534 			if (interval <= 0)
535 				usage();
536 			++argv;
537 			iflag = 1;
538 		}
539 		if (*argv) {
540 			nlistf = *argv;
541 			if (*++argv)
542 				memf = *argv;
543 		}
544 	}
545 #endif
546 
547 	prepare(nlistf, memf, tp);
548 
549 #ifndef SMALL
550 	if (Bflag) {
551 		if (sflag)
552 			bpf_stats();
553 		else
554 			bpf_dump(interface);
555 		exit(0);
556 	}
557 #endif
558 
559 	if (mflag) {
560 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
561 		    nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
562 		    nl[N_MCLPOOL].n_value);
563 		exit(0);
564 	}
565 	if (Pflag) {
566 		if (tp == NULL) {
567 			/* Default to TCP. */
568 			tp = name2protox("tcp");
569 		}
570 		if (tp->pr_dump)
571 			(*tp->pr_dump)(pcbaddr);
572 		else
573 			printf("%s: no PCB dump routine\n", tp->pr_name);
574 		exit(0);
575 	}
576 	if (pflag) {
577 		if (iflag && tp->pr_istats)
578 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
579 		else if (tp->pr_stats)
580 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
581 				tp->pr_name);
582 		else
583 			printf("%s: no stats routine\n", tp->pr_name);
584 		exit(0);
585 	}
586 	if (qflag) {
587 		print_softintrq();
588 		exit(0);
589 	}
590 	/*
591 	 * Keep file descriptors open to avoid overhead
592 	 * of open/close on each call to get* routines.
593 	 */
594 	sethostent(1);
595 	setnetent(1);
596 	if (iflag) {
597 		if (af != AF_UNSPEC)
598 			goto protostat;
599 
600 		intpr(interval, nl[N_IFNET].n_value, NULL);
601 		exit(0);
602 	}
603 	if (rflag) {
604 		if (sflag)
605 			rt_stats(use_sysctl ? 0 : nl[N_RTSTAT].n_value);
606 		else {
607 			if (use_sysctl)
608 				p_rttables(af);
609 			else
610 				routepr(nl[N_RTREE].n_value);
611 		}
612 		exit(0);
613 	}
614 #ifndef SMALL
615 	if (gflag) {
616 		if (sflag) {
617 			if (af == AF_INET || af == AF_UNSPEC)
618 				mrt_stats(nl[N_MRTPROTO].n_value,
619 					  nl[N_MRTSTAT].n_value);
620 #ifdef INET6
621 			if (af == AF_INET6 || af == AF_UNSPEC)
622 				mrt6_stats(nl[N_MRT6PROTO].n_value,
623 					   nl[N_MRT6STAT].n_value);
624 #endif
625 		}
626 		else {
627 			if (af == AF_INET || af == AF_UNSPEC)
628 				mroutepr(nl[N_MRTPROTO].n_value,
629 					 nl[N_MFCHASHTBL].n_value,
630 					 nl[N_MFCHASH].n_value,
631 					 nl[N_VIFTABLE].n_value);
632 #ifdef INET6
633 			if (af == AF_INET6 || af == AF_UNSPEC)
634 				mroute6pr(nl[N_MRT6PROTO].n_value,
635 					  nl[N_MF6CTABLE].n_value,
636 					  nl[N_MIF6TABLE].n_value);
637 #endif
638 		}
639 		exit(0);
640 	}
641 #endif
642   protostat:
643 	if (af == AF_INET || af == AF_UNSPEC) {
644 		setprotoent(1);
645 		setservent(1);
646 		/* ugh, this is O(MN) ... why do we do this? */
647 		while ((p = getprotoent()) != NULL) {
648 			for (tp = protox; tp->pr_name; tp++)
649 				if (strcmp(tp->pr_name, p->p_name) == 0)
650 					break;
651 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
652 				continue;
653 			printproto(tp, p->p_name);
654 			tp->pr_wanted = 0;
655 		}
656 		endprotoent();
657 		for (tp = protox; tp->pr_name; tp++)
658 			if (tp->pr_wanted)
659 				printproto(tp, tp->pr_name);
660 	}
661 #ifdef INET6
662 	if (af == AF_INET6 || af == AF_UNSPEC)
663 		for (tp = ip6protox; tp->pr_name; tp++)
664 			printproto(tp, tp->pr_name);
665 #endif
666 	if (af == AF_ARP || af == AF_UNSPEC)
667 		for (tp = arpprotox; tp->pr_name; tp++)
668 			printproto(tp, tp->pr_name);
669 #ifdef IPSEC
670 	if (af == PF_KEY || af == AF_UNSPEC)
671 		for (tp = pfkeyprotox; tp->pr_name; tp++)
672 			printproto(tp, tp->pr_name);
673 #endif
674 #ifndef SMALL
675 	if (af == AF_APPLETALK || af == AF_UNSPEC)
676 		for (tp = atalkprotox; tp->pr_name; tp++)
677 			printproto(tp, tp->pr_name);
678 	if (af == AF_ISO || af == AF_UNSPEC)
679 		for (tp = isoprotox; tp->pr_name; tp++)
680 			printproto(tp, tp->pr_name);
681 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
682 		unixpr(nl[N_UNIXSW].n_value);
683 #endif
684 	exit(0);
685 }
686 
687 /*
688  * Print out protocol statistics or control blocks (per sflag).
689  * If the interface was not specifically requested, and the symbol
690  * is not in the namelist, ignore this one.
691  */
692 static void
693 printproto(tp, name)
694 	struct protox *tp;
695 	char *name;
696 {
697 	void (*pr) __P((u_long, char *));
698 	u_long off;
699 
700 	if (sflag) {
701 		if (iflag) {
702 			if (tp->pr_istats)
703 				intpr(interval, nl[N_IFNET].n_value,
704 				      tp->pr_istats);
705 			return;
706 		}
707 		else {
708 			pr = tp->pr_stats;
709 			off = nl[tp->pr_sindex].n_value;
710 		}
711 	} else {
712 		pr = tp->pr_cblocks;
713 		off = nl[tp->pr_index].n_value;
714 	}
715 	if (pr != NULL && ((off || af != AF_UNSPEC) || use_sysctl)) {
716 		(*pr)(off, name);
717 	}
718 }
719 
720 /*
721  * Print softintrq status.
722  */
723 void
724 print_softintrq()
725 {
726 	struct ifqueue intrq, *ifq = &intrq;
727 	const struct softintrq *siq;
728 	u_long off;
729 
730 	for (siq = softintrq; siq->siq_name != NULL; siq++) {
731 		off = nl[siq->siq_index].n_value;
732 		if (off == 0)
733 			continue;
734 
735 		kread(off, (char *)ifq, sizeof(*ifq));
736 		printf("%s:\n", siq->siq_name);
737 		printf("\tqueue length: %d\n", ifq->ifq_len);
738 		printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen);
739 		printf("\tpackets dropped: %d\n", ifq->ifq_drops);
740 	}
741 }
742 
743 /*
744  * Read kernel memory, return 0 on success.
745  */
746 int
747 kread(addr, buf, size)
748 	u_long addr;
749 	char *buf;
750 	int size;
751 {
752 
753 	if (kvm_read(kvmd, addr, buf, size) != size) {
754 		warnx("%s", kvm_geterr(kvmd));
755 		return (-1);
756 	}
757 	return (0);
758 }
759 
760 char *
761 plural(n)
762 	int n;
763 {
764 
765 	return (n != 1 ? "s" : "");
766 }
767 
768 char *
769 plurales(n)
770 	int n;
771 {
772 
773 	return (n != 1 ? "es" : "");
774 }
775 
776 int
777 get_hardticks(void)
778 {
779 	int hardticks;
780 
781 	kread(nl[N_HARDCLOCK_TICKS].n_value, (char *)&hardticks,
782 	    sizeof(hardticks));
783 	return (hardticks);
784 }
785 
786 /*
787  * Find the protox for the given "well-known" name.
788  */
789 static struct protox *
790 knownname(name)
791 	char *name;
792 {
793 	struct protox **tpp, *tp;
794 
795 	for (tpp = protoprotox; *tpp; tpp++)
796 		for (tp = *tpp; tp->pr_name; tp++)
797 			if (strcmp(tp->pr_name, name) == 0)
798 				return (tp);
799 	return (NULL);
800 }
801 
802 /*
803  * Find the protox corresponding to name.
804  */
805 static struct protox *
806 name2protox(name)
807 	char *name;
808 {
809 	struct protox *tp;
810 	char **alias;			/* alias from p->aliases */
811 	struct protoent *p;
812 
813 	/*
814 	 * Try to find the name in the list of "well-known" names. If that
815 	 * fails, check if name is an alias for an Internet protocol.
816 	 */
817 	if ((tp = knownname(name)) != NULL)
818 		return (tp);
819 
820 	setprotoent(1);			/* make protocol lookup cheaper */
821 	while ((p = getprotoent()) != NULL) {
822 		/* assert: name not same as p->name */
823 		for (alias = p->p_aliases; *alias; alias++)
824 			if (strcmp(name, *alias) == 0) {
825 				endprotoent();
826 				return (knownname(p->p_name));
827 			}
828 	}
829 	endprotoent();
830 	return (NULL);
831 }
832 
833 static void
834 usage()
835 {
836 	const char *progname = getprogname();
837 
838 	(void)fprintf(stderr,
839 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
840 	(void)fprintf(stderr,
841 "       %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n",
842 	progname);
843 	(void)fprintf(stderr,
844 "       %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
845 	(void)fprintf(stderr,
846 "       %s [-p protocol] [-M core] [-N system]\n", progname);
847 	(void)fprintf(stderr,
848 "       %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
849 	(void)fprintf(stderr,
850 "       %s [-p protocol] [-i] [-I Interface] \n", progname);
851 	(void)fprintf(stderr,
852 "       %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
853 	(void)fprintf(stderr,
854 "       %s [-s] [-B] [-I interface]\n", progname);
855 	exit(1);
856 }
857