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