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