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