xref: /netbsd-src/usr.bin/netstat/main.c (revision 62f324d0121177eaf2e0384f92fd9ca2a751c795)
1 /*	$NetBSD: main.c,v 1.85 2013/03/01 18:26:11 joerg 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.85 2013/03/01 18:26:11 joerg 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 #include "prog_ops.h"
68 
69 struct nlist nl[] = {
70 #define	N_MBSTAT	0
71 	{ "_mbstat", 0, 0, 0, 0 },
72 #define	N_IPSTAT	1
73 	{ "_ipstat", 0, 0, 0, 0 },	/* not available via kvm */
74 #define	N_TCBTABLE	2
75 	{ "_tcbtable", 0, 0, 0, 0 },
76 #define	N_TCPSTAT	3
77 	{ "_tcpstat", 0, 0, 0, 0 },	/* not available via kvm */
78 #define	N_UDBTABLE	4
79 	{ "_udbtable", 0, 0, 0, 0 },
80 #define	N_UDPSTAT	5
81 	{ "_udpstat", 0, 0, 0, 0 },	/* not available via kvm */
82 #define	N_IFNET		6
83 	{ "_ifnet", 0, 0, 0, 0 },
84 #define	N_ICMPSTAT	7
85 	{ "_icmpstat", 0, 0, 0, 0 },	/* not available via kvm */
86 #define	N_RTSTAT	8
87 	{ "_rtstat", 0, 0, 0, 0 },
88 #define	N_UNIXSW	9
89 	{ "_unixsw", 0, 0, 0, 0 },
90 #define N_RTREE		10
91 	{ "_rt_tables", 0, 0, 0, 0 },
92 #define	N_NFILE		11
93 	{ "_nfile", 0, 0, 0, 0 },
94 #define N_IGMPSTAT	12
95 	{ "_igmpstat", 0, 0, 0, 0 },	/* not available via kvm */
96 #define N_MRTPROTO	13
97 	{ "_ip_mrtproto", 0, 0, 0, 0 },
98 #define N_MRTSTAT	14
99 	{ "_mrtstat", 0, 0, 0, 0 },
100 #define N_MFCHASHTBL	15
101 	{ "_mfchashtbl", 0, 0, 0, 0 },
102 #define	N_MFCHASH	16
103 	{ "_mfchash", 0, 0, 0, 0 },
104 #define N_VIFTABLE	17
105 	{ "_viftable", 0, 0, 0, 0 },
106 #define N_MSIZE		18
107 	{ "_msize", 0, 0, 0, 0 },
108 #define N_MCLBYTES	19
109 	{ "_mclbytes", 0, 0, 0, 0 },
110 #define N_DDPSTAT	20
111 	{ "_ddpstat", 0, 0, 0, 0 },	/* not available via kvm */
112 #define N_DDPCB		21
113 	{ "_ddpcb", 0, 0, 0, 0 },
114 #define N_MBPOOL	22
115 	{ "_mbpool", 0, 0, 0, 0 },
116 #define N_MCLPOOL	23
117 	{ "_mclpool", 0, 0, 0, 0 },
118 #define N_IP6STAT	24
119 	{ "_ip6stat", 0, 0, 0, 0 },	/* not available via kvm */
120 #define N_TCP6STAT	25
121 	{ "_tcp6stat", 0, 0, 0, 0 },	/* not available via kvm */
122 #define N_UDP6STAT	26
123 	{ "_udp6stat", 0, 0, 0, 0 },	/* not available via kvm */
124 #define N_ICMP6STAT	27
125 	{ "_icmp6stat", 0, 0, 0, 0 },	/* not available via kvm */
126 #define N_IPSECSTAT	28
127 	{ "_ipsecstat", 0, 0, 0, 0 },	/* not available via kvm */
128 #define N_IPSEC6STAT	29
129 	{ "_ipsec6stat", 0, 0, 0, 0 },	/* not available via kvm */
130 #define N_PIM6STAT	30
131 	{ "_pim6stat", 0, 0, 0, 0 },	/* not available via kvm */
132 #define N_MRT6PROTO	31
133 	{ "_ip6_mrtproto", 0, 0, 0, 0 },
134 #define N_MRT6STAT	32
135 	{ "_mrt6stat", 0, 0, 0, 0 },
136 #define N_MF6CTABLE	33
137 	{ "_mf6ctable", 0, 0, 0, 0 },
138 #define N_MIF6TABLE	34
139 	{ "_mif6table", 0, 0, 0, 0 },
140 #define N_PFKEYSTAT	35
141 	{ "_pfkeystat", 0, 0, 0, 0 },	/* not available via kvm */
142 #define N_ARPSTAT	36
143 	{ "_arpstat", 0, 0, 0, 0 },	/* not available via kvm */
144 #define N_RIP6STAT	37
145 	{ "_rip6stat", 0, 0, 0, 0 },	/* not available via kvm */
146 #define	N_ARPINTRQ	38
147 	{ "_arpintrq", 0, 0, 0, 0 },
148 #define	N_IPINTRQ	39
149 	{ "_ipintrq", 0, 0, 0, 0 },
150 #define	N_IP6INTRQ	40
151 	{ "_ip6intrq", 0, 0, 0, 0 },
152 #define	N_ATINTRQ1	41
153 	{ "_atintrq1", 0, 0, 0, 0 },
154 #define	N_ATINTRQ2	42
155 	{ "_atintrq2", 0, 0, 0, 0 },
156 #define	N_NSINTRQ	43
157 	{ "_nsintrq", 0, 0, 0, 0 },
158 #define	N_LLCINTRQ	44
159 	{ "_llcintrq", 0, 0, 0, 0 },
160 #define	N_HDINTRQ	45
161 	{ "_hdintrq", 0, 0, 0, 0 },
162 #define	N_NATMINTRQ	46
163 	{ "_natmintrq", 0, 0, 0, 0 },
164 #define	N_PPPOEDISCINQ	47
165 	{ "_ppoediscinq", 0, 0, 0, 0 },
166 #define	N_PPPOEINQ	48
167 	{ "_ppoeinq", 0, 0, 0, 0 },
168 #define	N_PKINTRQ	49
169 	{ "_pkintrq", 0, 0, 0, 0 },
170 #define	N_HARDCLOCK_TICKS 50
171 	{ "_hardclock_ticks", 0, 0, 0, 0 },
172 #define N_PIMSTAT	51
173 	{ "_pimstat", 0, 0, 0, 0 },
174 #define N_CARPSTAT	52
175 	{ "_carpstats", 0, 0, 0, 0 },	/* not available via kvm */
176 #define N_PFSYNCSTAT	53
177 	{ "_pfsyncstats", 0, 0, 0, 0},  /* not available via kvm */
178 	{ "", 0, 0, 0, 0 },
179 };
180 
181 struct protox {
182 	u_char	pr_index;		/* index into nlist of cb head */
183 	u_char	pr_sindex;		/* index into nlist of stat block */
184 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
185 	void	(*pr_cblocks)		/* control blocks printing routine */
186 			__P((u_long, const char *));
187 	void	(*pr_stats)		/* statistics printing routine */
188 			__P((u_long, const char *));
189 	void	(*pr_istats)
190 			__P((const char *));	/* per/if statistics printing routine */
191 	void	(*pr_dump)		/* PCB state dump routine */
192 			__P((u_long));
193 	const char *pr_name;		/* well-known name */
194 } protox[] = {
195 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
196 	  tcp_stats,	NULL,		tcp_dump,	"tcp" },
197 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
198 	  udp_stats,	NULL,		0,	"udp" },
199 	{ -1,		N_IPSTAT,	1,	0,
200 	  ip_stats,	NULL,		0,	"ip" },
201 	{ -1,		N_ICMPSTAT,	1,	0,
202 	  icmp_stats,	NULL,		0,	"icmp" },
203 	{ -1,		N_IGMPSTAT,	1,	0,
204 	  igmp_stats,	NULL,		0,	"igmp" },
205 	{ -1,		N_CARPSTAT,	1,	0,
206 	  carp_stats,	NULL,		0,	"carp" },
207 #ifdef IPSEC
208 	{ -1,		N_IPSECSTAT,	1,	0,
209 	  fast_ipsec_stats, NULL,	0,	"ipsec" },
210 #endif
211 	{ -1,		N_PIMSTAT,	1,	0,
212 	  pim_stats,	NULL,		0,	"pim" },
213 	{ -1,		N_PFSYNCSTAT,  1,  0,
214 	  pfsync_stats,  NULL,		0,  "pfsync" },
215 	{ -1,		-1,		0,	0,
216 	  0,		NULL,		0,	0 }
217 };
218 
219 #ifdef INET6
220 struct protox ip6protox[] = {
221 	{ -1,		N_IP6STAT,	1,	0,
222 	  ip6_stats,	ip6_ifstats,	0,	"ip6" },
223 	{ -1,		N_ICMP6STAT,	1,	0,
224 	  icmp6_stats,	icmp6_ifstats,	0,	"icmp6" },
225 #ifdef TCP6
226 	{ N_TCBTABLE,	N_TCP6STAT,	1,	ip6protopr,
227 	  tcp6_stats,	NULL,		tcp6_dump,	"tcp6" },
228 #else
229 	{ N_TCBTABLE,	N_TCP6STAT,	1,	ip6protopr,
230 	  tcp_stats,	NULL,		tcp_dump,	"tcp6" },
231 #endif
232 	{ N_UDBTABLE,	N_UDP6STAT,	1,	ip6protopr,
233 	  udp6_stats,	NULL,		0,	"udp6" },
234 #ifdef IPSEC
235 	{ -1,		N_IPSEC6STAT,	1,	0,
236 	  fast_ipsec_stats, NULL,	0,	"ipsec6" },
237 #endif
238 	{ -1,		N_PIM6STAT,	1,	0,
239 	  pim6_stats,	NULL,		0,	"pim6" },
240 	{ -1,		N_RIP6STAT,	1,	0,
241 	  rip6_stats,	NULL,		0,	"rip6" },
242 	{ -1,		-1,		0,	0,
243 	  0,		NULL,		0,	0 }
244 };
245 #endif
246 
247 struct protox arpprotox[] = {
248 	{ -1,		N_ARPSTAT,	1,	0,
249 	  arp_stats,	NULL,		0,	"arp" },
250 	{ -1,		-1,		0,	0,
251 	  0,		NULL,		0,	0 }
252 };
253 
254 #ifdef IPSEC
255 struct protox pfkeyprotox[] = {
256 	{ -1,		N_PFKEYSTAT,	1,	0,
257 	  pfkey_stats,	NULL,		0,	"pfkey" },
258 	{ -1,		-1,		0,	0,
259 	  0,		NULL,		0,	0 }
260 };
261 #endif
262 
263 #ifndef SMALL
264 struct protox atalkprotox[] = {
265 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
266 	  ddp_stats,	NULL,		0,	"ddp" },
267 	{ -1,		-1,		0,	0,
268 	  0,		NULL,		0,	NULL }
269 };
270 #endif
271 
272 struct protox *protoprotox[] = { protox,
273 #ifdef INET6
274 				 ip6protox,
275 #endif
276 				 arpprotox,
277 #ifdef IPSEC
278 				 pfkeyprotox,
279 #endif
280 #ifndef SMALL
281 				 atalkprotox,
282 #endif
283 				 NULL };
284 
285 const struct softintrq {
286 	const char *siq_name;
287 	int siq_index;
288 } softintrq[] = {
289 	{ "arpintrq", N_ARPINTRQ },
290 	{ "ipintrq", N_IPINTRQ },
291 	{ "ip6intrq", N_IP6INTRQ },
292 	{ "atintrq1", N_ATINTRQ1 },
293 	{ "atintrq2", N_ATINTRQ2 },
294 	{ "llcintrq", N_LLCINTRQ },
295 	{ "hdintrq", N_HDINTRQ },
296 	{ "natmintrq", N_NATMINTRQ },
297 	{ "ppoediscinq", N_PPPOEDISCINQ },
298 	{ "ppoeinq", N_PPPOEINQ },
299 	{ "pkintrq", N_PKINTRQ },
300 	{ NULL, -1 },
301 };
302 
303 int main __P((int, char *[]));
304 static void printproto __P((struct protox *, const char *));
305 static void print_softintrq __P((void));
306 __dead static void usage(void);
307 static struct protox *name2protox __P((const char *));
308 static struct protox *knownname __P((const char *));
309 static void prepare(const char *, const char *, struct protox *tp);
310 static kvm_t *prepare_kvmd(const char *, const char *, char *);
311 
312 static kvm_t *kvmd = NULL;
313 gid_t egid;
314 int interval;	/* repeat interval for i/f stats */
315 static const char *nlistf = NULL, *memf = NULL;
316 
317 kvm_t *
318 get_kvmd(void)
319 {
320 	char buf[_POSIX2_LINE_MAX];
321 
322 	if (kvmd != NULL)
323 		return kvmd;
324 	if ((kvmd = prepare_kvmd(nlistf, memf, buf)) == NULL)
325 		errx(1, "kvm error: %s", buf);
326 	return kvmd;
327 }
328 
329 static kvm_t *
330 prepare_kvmd(const char *nf, const char *mf, char *errbuf)
331 {
332 	kvm_t *k;
333 
334 	(void)setegid(egid);
335 	k = kvm_openfiles(nf, mf, NULL, O_RDONLY, errbuf);
336 	(void)setgid(getgid());
337 	return k;
338 }
339 
340 void
341 prepare(const char *nf, const char *mf, struct protox *tp)
342 {
343 	char buf[_POSIX2_LINE_MAX];
344 	/*
345 	 * Try to figure out if we can use sysctl or not.
346 	 */
347 	if (nf != NULL && mf != NULL) {
348 		/* Of course, we can't use sysctl with dumps. */
349 		if (force_sysctl)
350 			errx(EXIT_FAILURE, "can't use sysctl with dumps");
351 
352 		/* If we have -M and -N, we're not dealing with live memory. */
353 		use_sysctl = 0;
354 	} else if (qflag ||
355 		   iflag ||
356 #ifndef SMALL
357 		   gflag ||
358 #endif
359 		   (pflag && tp->pr_sindex == N_PIMSTAT) ||
360 		   Pflag) {
361 		/* These flags are not yet supported via sysctl(3). */
362 		use_sysctl = 0;
363 	} else {
364 		/* We can use sysctl(3). */
365 		use_sysctl = 1;
366 	}
367 
368 	if (force_sysctl && !use_sysctl) {
369 		/* Let the user know what's about to happen. */
370 		warnx("forcing sysctl usage even though it might not be "\
371 		    "supported");
372 		use_sysctl = 1;
373 	}
374 
375 	kvmd = prepare_kvmd(nf, mf, buf);
376 
377 	if (!use_sysctl) {
378 
379 		if (kvmd == NULL)
380 			errx(1, "kvm error: %s", buf);
381 		if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
382 			if (nf)
383 				errx(1, "%s: no namelist", nf);
384 			else
385 				errx(1, "no namelist");
386 		}
387 	} else
388 		(void)setgid(getgid());
389 }
390 
391 int
392 main(int argc, char *argv[])
393 {
394 	struct protoent *p;
395 	struct protox *tp;	/* for printing cblocks & stats */
396 	int ch;
397 	char *cp;
398 	u_long pcbaddr;
399 
400 	if (prog_init) {
401 		if (prog_init() == -1)
402 			err(1, "init failed");
403 		force_sysctl = 1; /* cheap trick */
404 	}
405 
406 	egid = getegid();
407 	(void)setegid(getgid());
408 	tp = NULL;
409 	af = AF_UNSPEC;
410 	pcbaddr = 0;
411 
412 	while ((ch = getopt(argc, argv,
413 	    "AabBdf:ghI:LliM:mN:nP:p:qrsStTuVvw:X")) != -1)
414 		switch (ch) {
415 		case 'A':
416 			Aflag = 1;
417 			break;
418 		case 'a':
419 			aflag = 1;
420 			break;
421 		case 'b':
422 			bflag = 1;
423 			break;
424 		case 'B':
425 			Bflag = 1;
426 			break;
427 		case 'd':
428 			dflag = 1;
429 			break;
430 		case 'f':
431 			if (strcmp(optarg, "inet") == 0)
432 				af = AF_INET;
433 			else if (strcmp(optarg, "inet6") == 0)
434 				af = AF_INET6;
435 			else if (strcmp(optarg, "arp") == 0)
436 				af = AF_ARP;
437 			else if (strcmp(optarg, "pfkey") == 0)
438 				af = PF_KEY;
439 			else if (strcmp(optarg, "unix") == 0
440 			    || strcmp(optarg, "local") == 0)
441 				af = AF_LOCAL;
442 			else if (strcmp(optarg, "atalk") == 0)
443 				af = AF_APPLETALK;
444 			else if (strcmp(optarg, "mpls") == 0)
445 				af = AF_MPLS;
446 			else
447 				errx(1, "%s: unknown address family",
448 				    optarg);
449 			break;
450 #ifndef SMALL
451 		case 'g':
452 			gflag = 1;
453 			break;
454 #endif
455 		case 'h':
456 			hflag = 1;
457 			break;
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 'T':
513 			tagflag = 1;
514 			break;
515 		case 'u':
516 			af = AF_LOCAL;
517 			break;
518 		case 'V':
519 			Vflag++;
520 			break;
521 		case 'v':
522 			vflag++;
523 			break;
524 		case 'w':
525 			interval = atoi(optarg);
526 			iflag = 1;
527 			break;
528 		case 'X':
529 			force_sysctl = 1;
530 			break;
531 		case '?':
532 		default:
533 			usage();
534 		}
535 	argv += optind;
536 	argc -= optind;
537 
538 #define	BACKWARD_COMPATIBILITY
539 #ifdef	BACKWARD_COMPATIBILITY
540 	if (*argv) {
541 		if (isdigit((unsigned char)**argv)) {
542 			interval = atoi(*argv);
543 			if (interval <= 0)
544 				usage();
545 			++argv;
546 			iflag = 1;
547 		}
548 		if (*argv) {
549 			nlistf = *argv;
550 			if (*++argv)
551 				memf = *argv;
552 		}
553 	}
554 #endif
555 
556 	prepare(nlistf, memf, tp);
557 
558 #ifndef SMALL
559 	if (Bflag) {
560 		if (sflag)
561 			bpf_stats();
562 		else
563 			bpf_dump(interface);
564 		exit(0);
565 	}
566 #endif
567 
568 	if (mflag) {
569 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
570 		    nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
571 		    nl[N_MCLPOOL].n_value);
572 		exit(0);
573 	}
574 	if (Pflag) {
575 		if (tp == NULL) {
576 			/* Default to TCP. */
577 			tp = name2protox("tcp");
578 		}
579 		if (tp->pr_dump)
580 			(*tp->pr_dump)(pcbaddr);
581 		else
582 			printf("%s: no PCB dump routine\n", tp->pr_name);
583 		exit(0);
584 	}
585 	if (pflag) {
586 		if (iflag && tp->pr_istats)
587 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
588 		else if (tp->pr_stats)
589 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
590 				tp->pr_name);
591 		else
592 			printf("%s: no stats routine\n", tp->pr_name);
593 		exit(0);
594 	}
595 	if (qflag) {
596 		print_softintrq();
597 		exit(0);
598 	}
599 	/*
600 	 * Keep file descriptors open to avoid overhead
601 	 * of open/close on each call to get* routines.
602 	 */
603 	sethostent(1);
604 	setnetent(1);
605 	if (iflag) {
606 		if (af != AF_UNSPEC)
607 			goto protostat;
608 
609 		intpr(interval, nl[N_IFNET].n_value, NULL);
610 		exit(0);
611 	}
612 	if (rflag) {
613 		if (sflag)
614 			rt_stats(use_sysctl ? 0 : nl[N_RTSTAT].n_value);
615 		else {
616 			if (!use_sysctl)
617 				err(1, "-r is not supported "
618 				    "for post-mortem analysis.");
619 			p_rttables(af);
620 		}
621 		exit(0);
622 	}
623 #ifndef SMALL
624 	if (gflag) {
625 		if (sflag) {
626 			if (af == AF_INET || af == AF_UNSPEC)
627 				mrt_stats(nl[N_MRTPROTO].n_value,
628 					  nl[N_MRTSTAT].n_value);
629 #ifdef INET6
630 			if (af == AF_INET6 || af == AF_UNSPEC)
631 				mrt6_stats(nl[N_MRT6PROTO].n_value,
632 					   nl[N_MRT6STAT].n_value);
633 #endif
634 		}
635 		else {
636 			if (af == AF_INET || af == AF_UNSPEC)
637 				mroutepr(nl[N_MRTPROTO].n_value,
638 					 nl[N_MFCHASHTBL].n_value,
639 					 nl[N_MFCHASH].n_value,
640 					 nl[N_VIFTABLE].n_value);
641 #ifdef INET6
642 			if (af == AF_INET6 || af == AF_UNSPEC)
643 				mroute6pr(nl[N_MRT6PROTO].n_value,
644 					  nl[N_MF6CTABLE].n_value,
645 					  nl[N_MIF6TABLE].n_value);
646 #endif
647 		}
648 		exit(0);
649 	}
650 #endif
651   protostat:
652 	if (af == AF_INET || af == AF_UNSPEC) {
653 		setprotoent(1);
654 		setservent(1);
655 		/* ugh, this is O(MN) ... why do we do this? */
656 		while ((p = getprotoent()) != NULL) {
657 			for (tp = protox; tp->pr_name; tp++)
658 				if (strcmp(tp->pr_name, p->p_name) == 0)
659 					break;
660 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
661 				continue;
662 			printproto(tp, p->p_name);
663 			tp->pr_wanted = 0;
664 		}
665 		endprotoent();
666 		for (tp = protox; tp->pr_name; tp++)
667 			if (tp->pr_wanted)
668 				printproto(tp, tp->pr_name);
669 	}
670 #ifdef INET6
671 	if (af == AF_INET6 || af == AF_UNSPEC)
672 		for (tp = ip6protox; tp->pr_name; tp++)
673 			printproto(tp, tp->pr_name);
674 #endif
675 	if (af == AF_ARP || af == AF_UNSPEC)
676 		for (tp = arpprotox; tp->pr_name; tp++)
677 			printproto(tp, tp->pr_name);
678 #ifdef IPSEC
679 	if (af == PF_KEY || af == AF_UNSPEC)
680 		for (tp = pfkeyprotox; tp->pr_name; tp++)
681 			printproto(tp, tp->pr_name);
682 #endif
683 #ifndef SMALL
684 	if (af == AF_APPLETALK || af == AF_UNSPEC)
685 		for (tp = atalkprotox; tp->pr_name; tp++)
686 			printproto(tp, tp->pr_name);
687 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
688 		unixpr(nl[N_UNIXSW].n_value);
689 #endif
690 	exit(0);
691 }
692 
693 /*
694  * Print out protocol statistics or control blocks (per sflag).
695  * If the interface was not specifically requested, and the symbol
696  * is not in the namelist, ignore this one.
697  */
698 static void
699 printproto(struct protox *tp, const char *name)
700 {
701 	void (*pr) __P((u_long, const char *));
702 	u_long off;
703 
704 	if (sflag) {
705 		if (iflag) {
706 			if (tp->pr_istats)
707 				intpr(interval, nl[N_IFNET].n_value,
708 				      tp->pr_istats);
709 			return;
710 		}
711 		else {
712 			pr = tp->pr_stats;
713 			off = nl[tp->pr_sindex].n_value;
714 		}
715 	} else {
716 		pr = tp->pr_cblocks;
717 		off = nl[tp->pr_index].n_value;
718 	}
719 	if (pr != NULL && ((off || af != AF_UNSPEC) || use_sysctl)) {
720 		(*pr)(off, name);
721 	}
722 }
723 
724 /*
725  * Print softintrq status.
726  */
727 void
728 print_softintrq(void)
729 {
730 	struct ifqueue intrq, *ifq = &intrq;
731 	const struct softintrq *siq;
732 	u_long off;
733 
734 	for (siq = softintrq; siq->siq_name != NULL; siq++) {
735 		off = nl[siq->siq_index].n_value;
736 		if (off == 0)
737 			continue;
738 
739 		kread(off, (char *)ifq, sizeof(*ifq));
740 		printf("%s:\n", siq->siq_name);
741 		printf("\tqueue length: %d\n", ifq->ifq_len);
742 		printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen);
743 		printf("\tpackets dropped: %d\n", ifq->ifq_drops);
744 	}
745 }
746 
747 /*
748  * Read kernel memory, return 0 on success.
749  */
750 int
751 kread(u_long addr, char *buf, int size)
752 {
753 
754 	if (kvm_read(kvmd, addr, buf, size) != size) {
755 		warnx("%s", kvm_geterr(kvmd));
756 		return (-1);
757 	}
758 	return (0);
759 }
760 
761 const char *
762 plural(int n)
763 {
764 
765 	return (n != 1 ? "s" : "");
766 }
767 
768 const char *
769 plurales(int n)
770 {
771 
772 	return (n != 1 ? "es" : "");
773 }
774 
775 int
776 get_hardticks(void)
777 {
778 	int hardticks;
779 
780 	kread(nl[N_HARDCLOCK_TICKS].n_value, (char *)&hardticks,
781 	    sizeof(hardticks));
782 	return (hardticks);
783 }
784 
785 /*
786  * Find the protox for the given "well-known" name.
787  */
788 static struct protox *
789 knownname(const char *name)
790 {
791 	struct protox **tpp, *tp;
792 
793 	for (tpp = protoprotox; *tpp; tpp++)
794 		for (tp = *tpp; tp->pr_name; tp++)
795 			if (strcmp(tp->pr_name, name) == 0)
796 				return (tp);
797 	return (NULL);
798 }
799 
800 /*
801  * Find the protox corresponding to name.
802  */
803 static struct protox *
804 name2protox(const char *name)
805 {
806 	struct protox *tp;
807 	char **alias;			/* alias from p->aliases */
808 	struct protoent *p;
809 
810 	/*
811 	 * Try to find the name in the list of "well-known" names. If that
812 	 * fails, check if name is an alias for an Internet protocol.
813 	 */
814 	if ((tp = knownname(name)) != NULL)
815 		return (tp);
816 
817 	setprotoent(1);			/* make protocol lookup cheaper */
818 	while ((p = getprotoent()) != NULL) {
819 		/* assert: name not same as p->name */
820 		for (alias = p->p_aliases; *alias; alias++)
821 			if (strcmp(name, *alias) == 0) {
822 				endprotoent();
823 				return (knownname(p->p_name));
824 			}
825 	}
826 	endprotoent();
827 	return (NULL);
828 }
829 
830 static void
831 usage(void)
832 {
833 	const char *progname = getprogname();
834 
835 	(void)fprintf(stderr,
836 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
837 	(void)fprintf(stderr,
838 "       %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n",
839 	progname);
840 	(void)fprintf(stderr,
841 "       %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
842 	(void)fprintf(stderr,
843 "       %s [-p protocol] [-M core] [-N system]\n", progname);
844 	(void)fprintf(stderr,
845 "       %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
846 	(void)fprintf(stderr,
847 "       %s [-p protocol] [-i] [-I Interface] \n", progname);
848 	(void)fprintf(stderr,
849 "       %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
850 	(void)fprintf(stderr,
851 "       %s [-s] [-B] [-I interface]\n", progname);
852 	exit(1);
853 }
854