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