Home | History | Annotate | Line # | Download | only in netstat
inet6.c revision 1.66.4.1
      1 /*	$NetBSD: inet6.c,v 1.66.4.1 2015/02/11 08:29:56 snj Exp $	*/
      2 /*	BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1983, 1988, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 #ifndef lint
     64 #if 0
     65 static char sccsid[] = "@(#)inet.c	8.4 (Berkeley) 4/20/94";
     66 #else
     67 __RCSID("$NetBSD: inet6.c,v 1.66.4.1 2015/02/11 08:29:56 snj Exp $");
     68 #endif
     69 #endif /* not lint */
     70 
     71 #define _CALLOUT_PRIVATE
     72 
     73 #include <sys/param.h>
     74 #include <sys/socket.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/ioctl.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/protosw.h>
     79 #include <sys/sysctl.h>
     80 
     81 #include <net/route.h>
     82 #include <net/if.h>
     83 #include <netinet/in.h>
     84 #include <netinet/ip6.h>
     85 #include <netinet/icmp6.h>
     86 #include <netinet/in_systm.h>
     87 #ifndef TCP6
     88 #include <netinet/ip.h>
     89 #include <netinet/ip_var.h>
     90 #endif
     91 #include <netinet6/ip6_var.h>
     92 #include <netinet6/in6_pcb.h>
     93 #include <netinet6/in6_var.h>
     94 #ifdef TCP6
     95 #include <netinet/tcp6.h>
     96 #include <netinet/tcp6_seq.h>
     97 #define TCP6STATES
     98 #include <netinet/tcp6_fsm.h>
     99 #define TCP6TIMERS
    100 #include <netinet/tcp6_timer.h>
    101 #include <netinet/tcp6_var.h>
    102 #include <netinet/tcp6_debug.h>
    103 #else
    104 #define TCP6T_NTIMERS	TCPT_NTIMERS
    105 #define tcp6timers tcptimers
    106 #define tcp6states tcpstates
    107 #define TCP6_NSTATES	TCP_NSTATES
    108 #define tcp6cb tcpcb
    109 #include <netinet/tcp.h>
    110 #include <netinet/tcpip.h>
    111 #include <netinet/tcp_seq.h>
    112 #include <netinet/tcp_fsm.h>
    113 extern const char * const tcpstates[];
    114 extern const char * const tcptimers[];
    115 #include <netinet/tcp_timer.h>
    116 #include <netinet/tcp_var.h>
    117 #include <netinet/tcp_debug.h>
    118 #endif /*TCP6*/
    119 #include <netinet6/udp6.h>
    120 #include <netinet6/udp6_var.h>
    121 #include <netinet6/pim6_var.h>
    122 #include <netinet6/raw_ip6.h>
    123 #include <netinet/tcp_vtw.h>
    124 
    125 #include <arpa/inet.h>
    126 #if 0
    127 #include "gethostbyname2.h"
    128 #endif
    129 #include <netdb.h>
    130 
    131 #include <err.h>
    132 #include <errno.h>
    133 #include <kvm.h>
    134 #include <stdio.h>
    135 #include <stdlib.h>
    136 #include <string.h>
    137 #include <unistd.h>
    138 #include "netstat.h"
    139 #include "vtw.h"
    140 #include "prog_ops.h"
    141 
    142 #ifdef INET6
    143 
    144 struct	in6pcb in6pcb;
    145 #ifdef TCP6
    146 struct	tcp6cb tcp6cb;
    147 #else
    148 struct	tcpcb tcpcb;
    149 #endif
    150 struct	socket sockb;
    151 
    152 char	*inet6name(const struct in6_addr *);
    153 void	inet6print(const struct in6_addr *, int, const char *);
    154 void	print_vtw_v6(const vtw_t *);
    155 
    156 /*
    157  * Print a summary of connections related to an Internet
    158  * protocol.  For TCP, also give state of connection.
    159  * Listening processes (aflag) are suppressed unless the
    160  * -a (all) flag is specified.
    161  */
    162 static int width;
    163 static int compact;
    164 
    165 /* VTW-related variables. */
    166 static struct timeval now;
    167 
    168 static void
    169 ip6protoprhdr(void)
    170 {
    171 
    172 	printf("Active Internet6 connections");
    173 
    174 	if (aflag)
    175 		printf(" (including servers)");
    176 	putchar('\n');
    177 
    178 	if (Aflag) {
    179 		printf("%-8.8s ", "PCB");
    180 		width = 18;
    181 	}
    182 	printf(
    183 	    Vflag ? "%-5.5s %-6.6s %-6.6s  %*.*s %*.*s %-13.13s Expires\n"
    184 	          : "%-5.5s %-6.6s %-6.6s  %*.*s %*.*s %s\n",
    185 	    "Proto", "Recv-Q", "Send-Q",
    186 	    -width, width, "Local Address",
    187 	    -width, width, "Foreign Address", "(state)");
    188 }
    189 
    190 static void
    191 ip6protopr0(intptr_t ppcb, u_long rcv_sb_cc, u_long snd_sb_cc,
    192 	const struct in6_addr *laddr, u_int16_t lport,
    193 	const struct in6_addr *faddr, u_int16_t fport,
    194 	short t_state, const char *name, const struct timeval *expires)
    195 {
    196 	static const char *shorttcpstates[] = {
    197 		"CLOSED",       "LISTEN",       "SYNSEN",       "SYSRCV",
    198 		"ESTABL",       "CLWAIT",       "FWAIT1",       "CLOSNG",
    199 		"LASTAK",       "FWAIT2",       "TMWAIT",
    200 	};
    201 	int istcp;
    202 
    203 	istcp = strcmp(name, "tcp6") == 0;
    204 	if (Aflag)
    205 		printf("%8" PRIxPTR " ", ppcb);
    206 
    207 	printf("%-5.5s %6ld %6ld%s", name, rcv_sb_cc, snd_sb_cc,
    208 	    compact ? "" : " ");
    209 
    210 	inet6print(laddr, (int)lport, name);
    211 	inet6print(faddr, (int)fport, name);
    212 	if (istcp) {
    213 #ifdef TCP6
    214 		if (t_state < 0 || t_state >= TCP6_NSTATES)
    215 			printf(" %d", t_state);
    216 		else
    217 			printf(" %s", tcp6states[t_state]);
    218 #else
    219 		if (t_state < 0 || t_state >= TCP_NSTATES)
    220 			printf(" %d", t_state);
    221 		else
    222 			printf(" %s", compact ? shorttcpstates[t_state] :
    223 			    tcpstates[t_state]);
    224 #endif
    225 	}
    226 	if (Vflag && expires != NULL) {
    227 		if (expires->tv_sec == 0 && expires->tv_usec == -1)
    228 			printf(" reclaimed");
    229 		else {
    230 			struct timeval delta;
    231 
    232 			timersub(expires, &now, &delta);
    233 			printf(" %.3fms",
    234 			    delta.tv_sec * 1000.0 + delta.tv_usec / 1000.0);
    235 		}
    236 	}
    237 	putchar('\n');
    238 }
    239 
    240 static void
    241 dbg_printf(const char *fmt, ...)
    242 {
    243 	return;
    244 }
    245 
    246 void
    247 print_vtw_v6(const vtw_t *vtw)
    248 {
    249 	const vtw_v6_t *v6 = (const vtw_v6_t *)vtw;
    250 	struct timeval delta;
    251 	char buf[2][128];
    252 	static const struct timeval zero = {.tv_sec = 0, .tv_usec = 0};
    253 
    254 	inet_ntop(AF_INET6, &v6->laddr, buf[0], sizeof(buf[0]));
    255 	inet_ntop(AF_INET6, &v6->faddr, buf[1], sizeof(buf[1]));
    256 
    257 	timersub(&vtw->expire, &now, &delta);
    258 
    259 	if (vtw->expire.tv_sec == 0 && vtw->expire.tv_usec == -1) {
    260 		dbg_printf("%15.15s:%d %15.15s:%d reclaimed\n"
    261 		    ,buf[0], ntohs(v6->lport)
    262 		    ,buf[1], ntohs(v6->fport));
    263 		if (!(Vflag && vflag))
    264 			return;
    265 	} else if (vtw->expire.tv_sec == 0)
    266 		return;
    267 	else if (timercmp(&delta, &zero, <) && !(Vflag && vflag)) {
    268 		dbg_printf("%15.15s:%d %15.15s:%d expired\n"
    269 		    ,buf[0], ntohs(v6->lport)
    270 		    ,buf[1], ntohs(v6->fport));
    271 		return;
    272 	} else {
    273 		dbg_printf("%15.15s:%d %15.15s:%d expires in %.3fms\n"
    274 		    ,buf[0], ntohs(v6->lport)
    275 		    ,buf[1], ntohs(v6->fport)
    276 		    ,delta.tv_sec * 1000.0 + delta.tv_usec / 1000.0);
    277 	}
    278 	ip6protopr0(0, 0, 0,
    279 		 &v6->laddr, v6->lport,
    280 		 &v6->faddr, v6->fport,
    281 		 TCPS_TIME_WAIT, "tcp6", &vtw->expire);
    282 }
    283 
    284 
    285 static struct kinfo_pcb *
    286 getpcblist_kmem(u_long off, const char *name, size_t *len) {
    287 
    288 	struct inpcbtable table;
    289 	struct inpcb_hdr *next, *prev;
    290 	int istcp = strcmp(name, "tcp6") == 0;
    291 	struct kinfo_pcb *pcblist;
    292 	size_t size = 100, i;
    293 	struct sockaddr_in6 sin6;
    294 	struct inpcbqueue *head;
    295 
    296 	if (off == 0) {
    297 		*len = 0;
    298 		return NULL;
    299 	}
    300 	kread(off, (char *)&table, sizeof (table));
    301 	head = &table.inpt_queue;
    302 	next = TAILQ_FIRST(head);
    303 	prev = TAILQ_END(head);
    304 
    305 	if ((pcblist = malloc(size * sizeof(*pcblist))) == NULL)
    306 		err(1, "malloc");
    307 
    308 	i = 0;
    309 	while (next != TAILQ_END(head)) {
    310 		kread((u_long)next, (char *)&in6pcb, sizeof in6pcb);
    311 		next = TAILQ_NEXT(&in6pcb, in6p_queue);
    312 		prev = next;
    313 
    314 		if (in6pcb.in6p_af != AF_INET6)
    315 			continue;
    316 
    317 		kread((u_long)in6pcb.in6p_socket, (char *)&sockb,
    318 		    sizeof (sockb));
    319 		if (istcp) {
    320 #ifdef TCP6
    321 			kread((u_long)in6pcb.in6p_ppcb,
    322 			    (char *)&tcp6cb, sizeof (tcp6cb));
    323 #else
    324 			kread((u_long)in6pcb.in6p_ppcb,
    325 			    (char *)&tcpcb, sizeof (tcpcb));
    326 #endif
    327 		}
    328 		pcblist[i].ki_ppcbaddr =
    329 		    istcp ? (uintptr_t) in6pcb.in6p_ppcb : (uintptr_t) prev;
    330 		pcblist[i].ki_rcvq = (uint64_t)sockb.so_rcv.sb_cc;
    331 		pcblist[i].ki_sndq = (uint64_t)sockb.so_snd.sb_cc;
    332 		sin6.sin6_addr = in6pcb.in6p_laddr;
    333 		sin6.sin6_port = in6pcb.in6p_lport;
    334 		memcpy(&pcblist[i].ki_s, &sin6, sizeof(sin6));
    335 		sin6.sin6_addr = in6pcb.in6p_faddr;
    336 		sin6.sin6_port = in6pcb.in6p_fport;
    337 		memcpy(&pcblist[i].ki_d, &sin6, sizeof(sin6));
    338 		pcblist[i].ki_tstate = tcpcb.t_state;
    339 		if (i++ == size) {
    340 			size += 100;
    341 			struct kinfo_pcb *n = realloc(pcblist,
    342 			    size * sizeof(*pcblist));
    343 			if (n == NULL)
    344 				err(1, "realloc");
    345 			pcblist = n;
    346 		}
    347 	}
    348 	*len = i;
    349 	return pcblist;
    350 }
    351 
    352 void
    353 ip6protopr(u_long off, const char *name)
    354 {
    355 	struct kinfo_pcb *pcblist;
    356 	size_t i, len;
    357 	static int first = 1;
    358 
    359 	compact = 0;
    360 	if (Aflag) {
    361 		if (!numeric_addr)
    362 			width = 18;
    363 		else {
    364 			width = 21;
    365 			compact = 1;
    366 		}
    367 	} else
    368 		width = 22;
    369 
    370 	if (use_sysctl)
    371 		pcblist = getpcblist_sysctl(name, &len);
    372 	else
    373 		pcblist = getpcblist_kmem(off, name, &len);
    374 
    375 	for (i = 0; i < len; i++) {
    376 		struct sockaddr_in6 src, dst;
    377 
    378 		memcpy(&src, &pcblist[i].ki_s, sizeof(src));
    379 		memcpy(&dst, &pcblist[i].ki_d, sizeof(dst));
    380 
    381 		if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&dst.sin6_addr))
    382 			continue;
    383 
    384 		if (first) {
    385 			ip6protoprhdr();
    386 			first = 0;
    387 		}
    388 
    389 		ip6protopr0((intptr_t) pcblist[i].ki_ppcbaddr,
    390 		    pcblist[i].ki_rcvq, pcblist[i].ki_sndq,
    391 		    &src.sin6_addr, src.sin6_port,
    392 		    &dst.sin6_addr, dst.sin6_port,
    393 		    pcblist[i].ki_tstate, name, NULL);
    394 	}
    395 
    396 	free(pcblist);
    397 
    398 	if (strcmp(name, "tcp6") == 0) {
    399 		struct timeval t;
    400 		timebase(&t);
    401 		gettimeofday(&now, NULL);
    402 		timersub(&now, &t, &now);
    403 		show_vtw_v6(print_vtw_v6);
    404 	}
    405 }
    406 
    407 #ifdef TCP6
    408 /*
    409  * Dump TCP6 statistics structure.
    410  */
    411 void
    412 tcp6_stats(u_long off, const char *name)
    413 {
    414 	struct tcp6stat tcp6stat;
    415 
    416 	if (use_sysctl) {
    417 		size_t size = sizeof(tcp6stat);
    418 
    419 		if (sysctlbyname("net.inet6.tcp6.stats", &tcp6stat, &size,
    420 		    NULL, 0) == -1)
    421 			return;
    422 	} else {
    423 		warnx("%s stats not available via KVM.", name);
    424 		return;
    425 	}
    426 
    427 	printf ("%s:\n", name);
    428 
    429 #define	p(f, m) if (tcp6stat.f || sflag <= 1) \
    430     printf(m, tcp6stat.f, plural(tcp6stat.f))
    431 #define	p2(f1, f2, m) if (tcp6stat.f1 || tcp6stat.f2 || sflag <= 1) \
    432     printf(m, tcp6stat.f1, plural(tcp6stat.f1), tcp6stat.f2, plural(tcp6stat.f2))
    433 #define	p3(f, m) if (tcp6stat.f || sflag <= 1) \
    434     printf(m, tcp6stat.f, plurales(tcp6stat.f))
    435 
    436 	p(tcp6s_sndtotal, "\t%ld packet%s sent\n");
    437 	p2(tcp6s_sndpack,tcp6s_sndbyte,
    438 		"\t\t%ld data packet%s (%ld byte%s)\n");
    439 	p2(tcp6s_sndrexmitpack, tcp6s_sndrexmitbyte,
    440 		"\t\t%ld data packet%s (%ld byte%s) retransmitted\n");
    441 	p2(tcp6s_sndacks, tcp6s_delack,
    442 		"\t\t%ld ack-only packet%s (%ld packet%s delayed)\n");
    443 	p(tcp6s_sndurg, "\t\t%ld URG only packet%s\n");
    444 	p(tcp6s_sndprobe, "\t\t%ld window probe packet%s\n");
    445 	p(tcp6s_sndwinup, "\t\t%ld window update packet%s\n");
    446 	p(tcp6s_sndctrl, "\t\t%ld control packet%s\n");
    447 	p(tcp6s_rcvtotal, "\t%ld packet%s received\n");
    448 	p2(tcp6s_rcvackpack, tcp6s_rcvackbyte, "\t\t%ld ack%s (for %ld byte%s)\n");
    449 	p(tcp6s_rcvdupack, "\t\t%ld duplicate ack%s\n");
    450 	p(tcp6s_rcvacktoomuch, "\t\t%ld ack%s for unsent data\n");
    451 	p2(tcp6s_rcvpack, tcp6s_rcvbyte,
    452 		"\t\t%ld packet%s (%ld byte%s) received in-sequence\n");
    453 	p2(tcp6s_rcvduppack, tcp6s_rcvdupbyte,
    454 		"\t\t%ld completely duplicate packet%s (%ld byte%s)\n");
    455 	p(tcp6s_pawsdrop, "\t\t%ld old duplicate packet%s\n");
    456 	p2(tcp6s_rcvpartduppack, tcp6s_rcvpartdupbyte,
    457 		"\t\t%ld packet%s with some dup. data (%ld byte%s duped)\n");
    458 	p2(tcp6s_rcvoopack, tcp6s_rcvoobyte,
    459 		"\t\t%ld out-of-order packet%s (%ld byte%s)\n");
    460 	p2(tcp6s_rcvpackafterwin, tcp6s_rcvbyteafterwin,
    461 		"\t\t%ld packet%s (%ld byte%s) of data after window\n");
    462 	p(tcp6s_rcvwinprobe, "\t\t%ld window probe%s\n");
    463 	p(tcp6s_rcvwinupd, "\t\t%ld window update packet%s\n");
    464 	p(tcp6s_rcvafterclose, "\t\t%ld packet%s received after close\n");
    465 	p(tcp6s_rcvbadsum, "\t\t%ld discarded for bad checksum%s\n");
    466 	p(tcp6s_rcvbadoff, "\t\t%ld discarded for bad header offset field%s\n");
    467 	p(tcp6s_rcvshort, "\t\t%ld discarded because packet%s too short\n");
    468 	p(tcp6s_connattempt, "\t%ld connection request%s\n");
    469 	p(tcp6s_accepts, "\t%ld connection accept%s\n");
    470 	p(tcp6s_badsyn, "\t%ld bad connection attempt%s\n");
    471 	p(tcp6s_connects, "\t%ld connection%s established (including accepts)\n");
    472 	p2(tcp6s_closed, tcp6s_drops,
    473 		"\t%ld connection%s closed (including %ld drop%s)\n");
    474 	p(tcp6s_conndrops, "\t%ld embryonic connection%s dropped\n");
    475 	p2(tcp6s_rttupdated, tcp6s_segstimed,
    476 		"\t%ld segment%s updated rtt (of %ld attempt%s)\n");
    477 	p(tcp6s_rexmttimeo, "\t%ld retransmit timeout%s\n");
    478 	p(tcp6s_timeoutdrop, "\t\t%ld connection%s dropped by rexmit timeout\n");
    479 	p(tcp6s_persisttimeo, "\t%ld persist timeout%s\n");
    480 	p(tcp6s_persistdrop, "\t%ld connection%s timed out in persist\n");
    481 	p(tcp6s_keeptimeo, "\t%ld keepalive timeout%s\n");
    482 	p(tcp6s_keepprobe, "\t\t%ld keepalive probe%s sent\n");
    483 	p(tcp6s_keepdrops, "\t\t%ld connection%s dropped by keepalive\n");
    484 	p(tcp6s_predack, "\t%ld correct ACK header prediction%s\n");
    485 	p(tcp6s_preddat, "\t%ld correct data packet header prediction%s\n");
    486 	p3(tcp6s_pcbcachemiss, "\t%ld PCB cache miss%s\n");
    487 #undef p
    488 #undef p2
    489 #undef p3
    490 }
    491 #endif
    492 
    493 /*
    494  * Dump UDP6 statistics structure.
    495  */
    496 void
    497 udp6_stats(u_long off, const char *name)
    498 {
    499 	uint64_t udp6stat[UDP6_NSTATS];
    500 	u_quad_t delivered;
    501 
    502 	if (use_sysctl) {
    503 		size_t size = sizeof(udp6stat);
    504 
    505 		if (sysctlbyname("net.inet6.udp6.stats", udp6stat, &size,
    506 		    NULL, 0) == -1)
    507 			return;
    508 	} else {
    509 		warnx("%s stats not available via KVM.", name);
    510 		return;
    511 	}
    512 	printf("%s:\n", name);
    513 #define	p(f, m) if (udp6stat[f] || sflag <= 1) \
    514     printf(m, (unsigned long long)udp6stat[f], plural(udp6stat[f]))
    515 #define	p1(f, m) if (udp6stat[f] || sflag <= 1) \
    516     printf(m, (unsigned long long)udp6stat[f])
    517 	p(UDP6_STAT_IPACKETS, "\t%llu datagram%s received\n");
    518 	p1(UDP6_STAT_HDROPS, "\t%llu with incomplete header\n");
    519 	p1(UDP6_STAT_BADLEN, "\t%llu with bad data length field\n");
    520 	p1(UDP6_STAT_BADSUM, "\t%llu with bad checksum\n");
    521 	p1(UDP6_STAT_NOSUM, "\t%llu with no checksum\n");
    522 	p1(UDP6_STAT_NOPORT, "\t%llu dropped due to no socket\n");
    523 	p(UDP6_STAT_NOPORTMCAST,
    524 	    "\t%llu multicast datagram%s dropped due to no socket\n");
    525 	p1(UDP6_STAT_FULLSOCK, "\t%llu dropped due to full socket buffers\n");
    526 	delivered = udp6stat[UDP6_STAT_IPACKETS] -
    527 		    udp6stat[UDP6_STAT_HDROPS] -
    528 		    udp6stat[UDP6_STAT_BADLEN] -
    529 		    udp6stat[UDP6_STAT_BADSUM] -
    530 		    udp6stat[UDP6_STAT_NOPORT] -
    531 		    udp6stat[UDP6_STAT_NOPORTMCAST] -
    532 		    udp6stat[UDP6_STAT_FULLSOCK];
    533 	if (delivered || sflag <= 1)
    534 		printf("\t%llu delivered\n", (unsigned long long)delivered);
    535 	p(UDP6_STAT_OPACKETS, "\t%llu datagram%s output\n");
    536 #undef p
    537 #undef p1
    538 }
    539 
    540 static	const char *ip6nh[] = {
    541 /*0*/	"hop by hop",
    542 	"ICMP",
    543 	"IGMP",
    544 	NULL,
    545 	"IP",
    546 /*5*/	NULL,
    547 	"TCP",
    548 	NULL,
    549 	NULL,
    550 	NULL,
    551 /*10*/	NULL, NULL, NULL, NULL, NULL,
    552 /*15*/	NULL,
    553 	NULL,
    554 	"UDP",
    555 	NULL,
    556 	NULL,
    557 /*20*/	NULL,
    558 	NULL,
    559 	"IDP",
    560 	NULL,
    561 	NULL,
    562 /*25*/	NULL,
    563 	NULL,
    564 	NULL,
    565 	NULL,
    566 	NULL,
    567 /*30*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    568 /*40*/	NULL,
    569 	"IP6",
    570 	NULL,
    571 	"routing",
    572 	"fragment",
    573 /*45*/	NULL, NULL, NULL, NULL, NULL,
    574 /*50*/	"ESP",
    575 	"AH",
    576 	NULL,
    577 	NULL,
    578 	NULL,
    579 /*55*/	NULL,
    580 	NULL,
    581 	NULL,
    582 	"ICMP6",
    583 	"no next header",
    584 /*60*/	"destination option",
    585 	NULL,
    586 	NULL,
    587 	NULL,
    588 	NULL,
    589 /*65*/	NULL, NULL, NULL, NULL, NULL,
    590 /*70*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    591 /*80*/	NULL,
    592 	NULL,
    593 	NULL,
    594 	NULL,
    595 	NULL,
    596 	NULL,
    597 	NULL,
    598 	NULL,
    599 	NULL,
    600 	"OSPF",
    601 /*90*/	NULL, NULL, NULL, NULL, NULL,
    602 /*95*/	NULL,
    603 	NULL,
    604 	"Ethernet",
    605 	NULL,
    606 	NULL,
    607 /*100*/	NULL,
    608 	NULL,
    609 	NULL,
    610 	"PIM",
    611 	NULL,
    612 /*105*/	NULL, NULL, NULL, NULL, NULL,
    613 /*110*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    614 /*120*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    615 /*130*/	NULL,
    616 	NULL,
    617 	"SCTP",
    618 	NULL,
    619 	NULL,
    620 /*135*/	NULL, NULL, NULL, NULL, NULL,
    621 /*140*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    622 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    623 /*160*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    624 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    625 /*180*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    626 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    627 /*200*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    628 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    629 /*220*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    630 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    631 /*240*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    632 	NULL, NULL, NULL, NULL, NULL, NULL,
    633 };
    634 
    635 /*
    636  * Dump IP6 statistics structure.
    637  */
    638 void
    639 ip6_stats(u_long off, const char *name)
    640 {
    641 	uint64_t ip6stat[IP6_NSTATS];
    642 	int first, i;
    643 	struct protoent *ep;
    644 	const char *n;
    645 
    646 	if (use_sysctl) {
    647 		size_t size = sizeof(ip6stat);
    648 
    649 		if (sysctlbyname("net.inet6.ip6.stats", ip6stat, &size,
    650 		    NULL, 0) == -1)
    651 			return;
    652 	} else {
    653 		warnx("%s stats not available via KVM.", name);
    654 		return;
    655 	}
    656 	printf("%s:\n", name);
    657 
    658 #define	p(f, m) if (ip6stat[f] || sflag <= 1) \
    659     printf(m, (unsigned long long)ip6stat[f], plural(ip6stat[f]))
    660 #define	p1(f, m) if (ip6stat[f] || sflag <= 1) \
    661     printf(m, (unsigned long long)ip6stat[f])
    662 
    663 	p(IP6_STAT_TOTAL, "\t%llu total packet%s received\n");
    664 	p1(IP6_STAT_TOOSMALL, "\t%llu with size smaller than minimum\n");
    665 	p1(IP6_STAT_TOOSHORT, "\t%llu with data size < data length\n");
    666 	p1(IP6_STAT_BADOPTIONS, "\t%llu with bad options\n");
    667 	p1(IP6_STAT_BADVERS, "\t%llu with incorrect version number\n");
    668 	p(IP6_STAT_FRAGMENTS, "\t%llu fragment%s received\n");
    669 	p(IP6_STAT_FRAGDROPPED,
    670 	    "\t%llu fragment%s dropped (dup or out of space)\n");
    671 	p(IP6_STAT_FRAGTIMEOUT, "\t%llu fragment%s dropped after timeout\n");
    672 	p(IP6_STAT_FRAGOVERFLOW, "\t%llu fragment%s that exceeded limit\n");
    673 	p(IP6_STAT_REASSEMBLED, "\t%llu packet%s reassembled ok\n");
    674 	p(IP6_STAT_DELIVERED, "\t%llu packet%s for this host\n");
    675 	p(IP6_STAT_FORWARD, "\t%llu packet%s forwarded\n");
    676 	p(IP6_STAT_FASTFORWARD, "\t%llu packet%s fast forwarded\n");
    677 	p1(IP6_STAT_FASTFORWARDFLOWS, "\t%llu fast forward flows\n");
    678 	p(IP6_STAT_CANTFORWARD, "\t%llu packet%s not forwardable\n");
    679 	p(IP6_STAT_REDIRECTSENT, "\t%llu redirect%s sent\n");
    680 	p(IP6_STAT_LOCALOUT, "\t%llu packet%s sent from this host\n");
    681 	p(IP6_STAT_RAWOUT, "\t%llu packet%s sent with fabricated ip header\n");
    682 	p(IP6_STAT_ODROPPED,
    683 	    "\t%llu output packet%s dropped due to no bufs, etc.\n");
    684 	p(IP6_STAT_NOROUTE, "\t%llu output packet%s discarded due to no route\n");
    685 	p(IP6_STAT_FRAGMENTED, "\t%llu output datagram%s fragmented\n");
    686 	p(IP6_STAT_OFRAGMENTS, "\t%llu fragment%s created\n");
    687 	p(IP6_STAT_CANTFRAG, "\t%llu datagram%s that can't be fragmented\n");
    688 	p(IP6_STAT_BADSCOPE, "\t%llu packet%s that violated scope rules\n");
    689 	p(IP6_STAT_NOTMEMBER, "\t%llu multicast packet%s which we don't join\n");
    690 	for (first = 1, i = 0; i < 256; i++)
    691 		if (ip6stat[IP6_STAT_NXTHIST + i] != 0) {
    692 			if (first) {
    693 				printf("\tInput packet histogram:\n");
    694 				first = 0;
    695 			}
    696 			n = NULL;
    697 			if (ip6nh[i])
    698 				n = ip6nh[i];
    699 			else if ((ep = getprotobynumber(i)) != NULL)
    700 				n = ep->p_name;
    701 			if (n)
    702 				printf("\t\t%s: %llu\n", n,
    703 				    (unsigned long long)ip6stat[IP6_STAT_NXTHIST + i]);
    704 			else
    705 				printf("\t\t#%d: %llu\n", i,
    706 				    (unsigned long long)ip6stat[IP6_STAT_NXTHIST + i]);
    707 		}
    708 	printf("\tMbuf statistics:\n");
    709 	p(IP6_STAT_M1, "\t\t%llu one mbuf%s\n");
    710 	for (first = 1, i = 0; i < 32; i++) {
    711 		char ifbuf[IFNAMSIZ];
    712 		if (ip6stat[IP6_STAT_M2M + i] != 0) {
    713 			if (first) {
    714 				printf("\t\ttwo or more mbuf:\n");
    715 				first = 0;
    716 			}
    717 			printf("\t\t\t%s = %llu\n",
    718 			       if_indextoname(i, ifbuf),
    719 			       (unsigned long long)ip6stat[IP6_STAT_M2M + i]);
    720 		}
    721 	}
    722 	p(IP6_STAT_MEXT1, "\t\t%llu one ext mbuf%s\n");
    723 	p(IP6_STAT_MEXT2M, "\t\t%llu two or more ext mbuf%s\n");
    724 	p(IP6_STAT_EXTHDRTOOLONG,
    725 	    "\t%llu packet%s whose headers are not continuous\n");
    726 	p(IP6_STAT_NOGIF, "\t%llu tunneling packet%s that can't find gif\n");
    727 	p(IP6_STAT_TOOMANYHDR,
    728 	    "\t%llu packet%s discarded due to too many headers\n");
    729 
    730 	/* for debugging source address selection */
    731 #define PRINT_SCOPESTAT(s,i) do {\
    732 		switch(i) { /* XXX hardcoding in each case */\
    733 		case 1:\
    734 			p(s, "\t\t%llu node-local%s\n");\
    735 			break;\
    736 		case 2:\
    737 			p(s, "\t\t%llu link-local%s\n");\
    738 			break;\
    739 		case 5:\
    740 			p(s, "\t\t%llu site-local%s\n");\
    741 			break;\
    742 		case 14:\
    743 			p(s, "\t\t%llu global%s\n");\
    744 			break;\
    745 		default:\
    746 			printf("\t\t%llu addresses scope=%x\n",\
    747 			       (unsigned long long)ip6stat[s], i);\
    748 		}\
    749 	} while(/*CONSTCOND*/0);
    750 
    751 	p(IP6_STAT_SOURCES_NONE,
    752 	  "\t%llu failure%s of source address selection\n");
    753 	for (first = 1, i = 0; i < 16; i++) {
    754 		if (ip6stat[IP6_STAT_SOURCES_SAMEIF + i]) {
    755 			if (first) {
    756 				printf("\tsource addresses on an outgoing I/F\n");
    757 				first = 0;
    758 			}
    759 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_SAMEIF + i, i);
    760 		}
    761 	}
    762 	for (first = 1, i = 0; i < 16; i++) {
    763 		if (ip6stat[IP6_STAT_SOURCES_OTHERIF + i]) {
    764 			if (first) {
    765 				printf("\tsource addresses on a non-outgoing I/F\n");
    766 				first = 0;
    767 			}
    768 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_OTHERIF + i, i);
    769 		}
    770 	}
    771 	for (first = 1, i = 0; i < 16; i++) {
    772 		if (ip6stat[IP6_STAT_SOURCES_SAMESCOPE + i]) {
    773 			if (first) {
    774 				printf("\tsource addresses of same scope\n");
    775 				first = 0;
    776 			}
    777 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_SAMESCOPE + i, i);
    778 		}
    779 	}
    780 	for (first = 1, i = 0; i < 16; i++) {
    781 		if (ip6stat[IP6_STAT_SOURCES_OTHERSCOPE + i]) {
    782 			if (first) {
    783 				printf("\tsource addresses of a different scope\n");
    784 				first = 0;
    785 			}
    786 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_OTHERSCOPE + i, i);
    787 		}
    788 	}
    789 	for (first = 1, i = 0; i < 16; i++) {
    790 		if (ip6stat[IP6_STAT_SOURCES_DEPRECATED + i]) {
    791 			if (first) {
    792 				printf("\tdeprecated source addresses\n");
    793 				first = 0;
    794 			}
    795 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_DEPRECATED + i, i);
    796 		}
    797 	}
    798 
    799 	p1(IP6_STAT_FORWARD_CACHEHIT, "\t%llu forward cache hit\n");
    800 	p1(IP6_STAT_FORWARD_CACHEMISS, "\t%llu forward cache miss\n");
    801 #undef p
    802 #undef p1
    803 }
    804 
    805 /*
    806  * Dump IPv6 per-interface statistics based on RFC 2465.
    807  */
    808 void
    809 ip6_ifstats(const char *ifname)
    810 {
    811 	struct in6_ifreq ifr;
    812 	int s;
    813 #define	p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
    814     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, \
    815 	plural(ifr.ifr_ifru.ifru_stat.f))
    816 #define	p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
    817     printf(m, (unsigned long long)ip6stat.f)
    818 
    819 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    820 		perror("Warning: socket(AF_INET6)");
    821 		return;
    822 	}
    823 
    824 	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    825 	printf("ip6 on %s:\n", ifname);
    826 
    827 	if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
    828 		perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
    829 		goto end;
    830 	}
    831 
    832 	p(ifs6_in_receive, "\t%llu total input datagram%s\n");
    833 	p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
    834 	p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
    835 	p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
    836 	p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
    837 	p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
    838 	p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
    839 	p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
    840 	p(ifs6_in_deliver,
    841 	  "\t%llu datagram%s delivered to an upper layer protocol\n");
    842 	p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
    843 	p(ifs6_out_request,
    844 	  "\t%llu datagram%s sent from an upper layer protocol\n");
    845 	p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
    846 	p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
    847 	p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
    848 	p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
    849 	p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
    850 	p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
    851 	p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
    852 	p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
    853 	p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
    854 
    855   end:
    856 	close(s);
    857 
    858 #undef p
    859 #undef p_5
    860 }
    861 
    862 static	const char *icmp6names[] = {
    863 	"#0",
    864 	"unreach",
    865 	"packet too big",
    866 	"time exceed",
    867 	"parameter problem",
    868 	"#5",
    869 	"#6",
    870 	"#7",
    871 	"#8",
    872 	"#9",
    873 	"#10",
    874 	"#11",
    875 	"#12",
    876 	"#13",
    877 	"#14",
    878 	"#15",
    879 	"#16",
    880 	"#17",
    881 	"#18",
    882 	"#19",
    883 	"#20",
    884 	"#21",
    885 	"#22",
    886 	"#23",
    887 	"#24",
    888 	"#25",
    889 	"#26",
    890 	"#27",
    891 	"#28",
    892 	"#29",
    893 	"#30",
    894 	"#31",
    895 	"#32",
    896 	"#33",
    897 	"#34",
    898 	"#35",
    899 	"#36",
    900 	"#37",
    901 	"#38",
    902 	"#39",
    903 	"#40",
    904 	"#41",
    905 	"#42",
    906 	"#43",
    907 	"#44",
    908 	"#45",
    909 	"#46",
    910 	"#47",
    911 	"#48",
    912 	"#49",
    913 	"#50",
    914 	"#51",
    915 	"#52",
    916 	"#53",
    917 	"#54",
    918 	"#55",
    919 	"#56",
    920 	"#57",
    921 	"#58",
    922 	"#59",
    923 	"#60",
    924 	"#61",
    925 	"#62",
    926 	"#63",
    927 	"#64",
    928 	"#65",
    929 	"#66",
    930 	"#67",
    931 	"#68",
    932 	"#69",
    933 	"#70",
    934 	"#71",
    935 	"#72",
    936 	"#73",
    937 	"#74",
    938 	"#75",
    939 	"#76",
    940 	"#77",
    941 	"#78",
    942 	"#79",
    943 	"#80",
    944 	"#81",
    945 	"#82",
    946 	"#83",
    947 	"#84",
    948 	"#85",
    949 	"#86",
    950 	"#87",
    951 	"#88",
    952 	"#89",
    953 	"#80",
    954 	"#91",
    955 	"#92",
    956 	"#93",
    957 	"#94",
    958 	"#95",
    959 	"#96",
    960 	"#97",
    961 	"#98",
    962 	"#99",
    963 	"#100",
    964 	"#101",
    965 	"#102",
    966 	"#103",
    967 	"#104",
    968 	"#105",
    969 	"#106",
    970 	"#107",
    971 	"#108",
    972 	"#109",
    973 	"#110",
    974 	"#111",
    975 	"#112",
    976 	"#113",
    977 	"#114",
    978 	"#115",
    979 	"#116",
    980 	"#117",
    981 	"#118",
    982 	"#119",
    983 	"#120",
    984 	"#121",
    985 	"#122",
    986 	"#123",
    987 	"#124",
    988 	"#125",
    989 	"#126",
    990 	"#127",
    991 	"echo",
    992 	"echo reply",
    993 	"multicast listener query",
    994 	"multicast listener report",
    995 	"multicast listener done",
    996 	"router solicitation",
    997 	"router advertisement",
    998 	"neighbor solicitation",
    999 	"neighbor advertisement",
   1000 	"redirect",
   1001 	"router renumbering",
   1002 	"node information request",
   1003 	"node information reply",
   1004 	"#141",
   1005 	"#142",
   1006 	"#143",
   1007 	"#144",
   1008 	"#145",
   1009 	"#146",
   1010 	"#147",
   1011 	"#148",
   1012 	"#149",
   1013 	"#150",
   1014 	"#151",
   1015 	"#152",
   1016 	"#153",
   1017 	"#154",
   1018 	"#155",
   1019 	"#156",
   1020 	"#157",
   1021 	"#158",
   1022 	"#159",
   1023 	"#160",
   1024 	"#161",
   1025 	"#162",
   1026 	"#163",
   1027 	"#164",
   1028 	"#165",
   1029 	"#166",
   1030 	"#167",
   1031 	"#168",
   1032 	"#169",
   1033 	"#170",
   1034 	"#171",
   1035 	"#172",
   1036 	"#173",
   1037 	"#174",
   1038 	"#175",
   1039 	"#176",
   1040 	"#177",
   1041 	"#178",
   1042 	"#179",
   1043 	"#180",
   1044 	"#181",
   1045 	"#182",
   1046 	"#183",
   1047 	"#184",
   1048 	"#185",
   1049 	"#186",
   1050 	"#187",
   1051 	"#188",
   1052 	"#189",
   1053 	"#180",
   1054 	"#191",
   1055 	"#192",
   1056 	"#193",
   1057 	"#194",
   1058 	"#195",
   1059 	"#196",
   1060 	"#197",
   1061 	"#198",
   1062 	"#199",
   1063 	"#200",
   1064 	"#201",
   1065 	"#202",
   1066 	"#203",
   1067 	"#204",
   1068 	"#205",
   1069 	"#206",
   1070 	"#207",
   1071 	"#208",
   1072 	"#209",
   1073 	"#210",
   1074 	"#211",
   1075 	"#212",
   1076 	"#213",
   1077 	"#214",
   1078 	"#215",
   1079 	"#216",
   1080 	"#217",
   1081 	"#218",
   1082 	"#219",
   1083 	"#220",
   1084 	"#221",
   1085 	"#222",
   1086 	"#223",
   1087 	"#224",
   1088 	"#225",
   1089 	"#226",
   1090 	"#227",
   1091 	"#228",
   1092 	"#229",
   1093 	"#230",
   1094 	"#231",
   1095 	"#232",
   1096 	"#233",
   1097 	"#234",
   1098 	"#235",
   1099 	"#236",
   1100 	"#237",
   1101 	"#238",
   1102 	"#239",
   1103 	"#240",
   1104 	"#241",
   1105 	"#242",
   1106 	"#243",
   1107 	"#244",
   1108 	"#245",
   1109 	"#246",
   1110 	"#247",
   1111 	"#248",
   1112 	"#249",
   1113 	"#250",
   1114 	"#251",
   1115 	"#252",
   1116 	"#253",
   1117 	"#254",
   1118 	"#255",
   1119 };
   1120 
   1121 /*
   1122  * Dump ICMPv6 statistics.
   1123  */
   1124 void
   1125 icmp6_stats(u_long off, const char *name)
   1126 {
   1127 	uint64_t icmp6stat[ICMP6_NSTATS];
   1128 	int i, first;
   1129 
   1130 	if (use_sysctl) {
   1131 		size_t size = sizeof(icmp6stat);
   1132 
   1133 		if (sysctlbyname("net.inet6.icmp6.stats", icmp6stat, &size,
   1134 		    NULL, 0) == -1)
   1135 			return;
   1136 	} else {
   1137 		warnx("%s stats not available via KVM.", name);
   1138 		return;
   1139 	}
   1140 
   1141 	printf("%s:\n", name);
   1142 
   1143 #define	p(f, m) if (icmp6stat[f] || sflag <= 1) \
   1144     printf(m, (unsigned long long)icmp6stat[f], plural(icmp6stat[f]))
   1145 #define p_oerr(f, m) if (icmp6stat[ICMP6_STAT_OUTERRHIST + f] || sflag <= 1) \
   1146     printf(m, (unsigned long long)icmp6stat[ICMP6_STAT_OUTERRHIST + f])
   1147 
   1148 	p(ICMP6_STAT_ERROR, "\t%llu call%s to icmp6_error\n");
   1149 	p(ICMP6_STAT_CANTERROR,
   1150 	    "\t%llu error%s not generated because old message was icmp6 or so\n");
   1151 	p(ICMP6_STAT_TOOFREQ,
   1152 	    "\t%llu error%s not generated because of rate limitation\n");
   1153 	for (first = 1, i = 0; i < 256; i++)
   1154 		if (icmp6stat[ICMP6_STAT_OUTHIST + i] != 0) {
   1155 			if (first) {
   1156 				printf("\tOutput packet histogram:\n");
   1157 				first = 0;
   1158 			}
   1159 			printf("\t\t%s: %llu\n", icmp6names[i],
   1160 			 (unsigned long long)icmp6stat[ICMP6_STAT_OUTHIST + i]);
   1161 		}
   1162 	p(ICMP6_STAT_BADCODE, "\t%llu message%s with bad code fields\n");
   1163 	p(ICMP6_STAT_TOOSHORT, "\t%llu message%s < minimum length\n");
   1164 	p(ICMP6_STAT_CHECKSUM, "\t%llu bad checksum%s\n");
   1165 	p(ICMP6_STAT_BADLEN, "\t%llu message%s with bad length\n");
   1166 	for (first = 1, i = 0; i < ICMP6_MAXTYPE; i++)
   1167 		if (icmp6stat[ICMP6_STAT_INHIST + i] != 0) {
   1168 			if (first) {
   1169 				printf("\tInput packet histogram:\n");
   1170 				first = 0;
   1171 			}
   1172 			printf("\t\t%s: %llu\n", icmp6names[i],
   1173 			  (unsigned long long)icmp6stat[ICMP6_STAT_INHIST + i]);
   1174 		}
   1175 	printf("\tHistogram of error messages to be generated:\n");
   1176 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_NOROUTE, "\t\t%llu no route\n");
   1177 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_ADMIN, "\t\t%llu administratively prohibited\n");
   1178 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_BEYONDSCOPE, "\t\t%llu beyond scope\n");
   1179 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_ADDR, "\t\t%llu address unreachable\n");
   1180 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_NOPORT, "\t\t%llu port unreachable\n");
   1181 	p_oerr(ICMP6_ERRSTAT_PACKET_TOO_BIG, "\t\t%llu packet too big\n");
   1182 	p_oerr(ICMP6_ERRSTAT_TIME_EXCEED_TRANSIT, "\t\t%llu time exceed transit\n");
   1183 	p_oerr(ICMP6_ERRSTAT_TIME_EXCEED_REASSEMBLY, "\t\t%llu time exceed reassembly\n");
   1184 	p_oerr(ICMP6_ERRSTAT_PARAMPROB_HEADER, "\t\t%llu erroneous header field\n");
   1185 	p_oerr(ICMP6_ERRSTAT_PARAMPROB_NEXTHEADER, "\t\t%llu unrecognized next header\n");
   1186 	p_oerr(ICMP6_ERRSTAT_PARAMPROB_OPTION, "\t\t%llu unrecognized option\n");
   1187 	p_oerr(ICMP6_ERRSTAT_REDIRECT, "\t\t%llu redirect\n");
   1188 	p_oerr(ICMP6_ERRSTAT_UNKNOWN, "\t\t%llu unknown\n");
   1189 
   1190 	p(ICMP6_STAT_REFLECT, "\t%llu message response%s generated\n");
   1191 	p(ICMP6_STAT_ND_TOOMANYOPT, "\t%llu message%s with too many ND options\n");
   1192 	p(ICMP6_STAT_ND_BADOPT, "\t%llu message%s with bad ND options\n");
   1193 	p(ICMP6_STAT_BADNS, "\t%llu bad neighbor solicitation message%s\n");
   1194 	p(ICMP6_STAT_BADNA, "\t%llu bad neighbor advertisement message%s\n");
   1195 	p(ICMP6_STAT_BADRS, "\t%llu bad router solicitation message%s\n");
   1196 	p(ICMP6_STAT_BADRA, "\t%llu bad router advertisement message%s\n");
   1197 	p(ICMP6_STAT_DROPPED_RAROUTE, "\t%llu router advertisement route%s dropped\n");
   1198 	p(ICMP6_STAT_BADREDIRECT, "\t%llu bad redirect message%s\n");
   1199 	p(ICMP6_STAT_PMTUCHG, "\t%llu path MTU change%s\n");
   1200 #undef p
   1201 #undef p_oerr
   1202 }
   1203 
   1204 /*
   1205  * Dump ICMPv6 per-interface statistics based on RFC 2466.
   1206  */
   1207 void
   1208 icmp6_ifstats(const char *ifname)
   1209 {
   1210 	struct in6_ifreq ifr;
   1211 	int s;
   1212 #define	p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
   1213     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, \
   1214 	plural(ifr.ifr_ifru.ifru_icmp6stat.f))
   1215 
   1216 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
   1217 		perror("Warning: socket(AF_INET6)");
   1218 		return;
   1219 	}
   1220 
   1221 	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1222 	printf("icmp6 on %s:\n", ifname);
   1223 
   1224 	if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
   1225 		perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
   1226 		goto end;
   1227 	}
   1228 
   1229 	p(ifs6_in_msg, "\t%llu total input message%s\n");
   1230 	p(ifs6_in_error, "\t%llu total input error message%s\n");
   1231 	p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
   1232 	p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
   1233 	p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
   1234 	p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
   1235 	p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
   1236 	p(ifs6_in_echo, "\t%llu input echo request%s\n");
   1237 	p(ifs6_in_echoreply, "\t%llu input echo reply%s\n");
   1238 	p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
   1239 	p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
   1240 	p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
   1241 	p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
   1242 	p(ifs6_in_redirect, "\t%llu input redirect%s\n");
   1243 	p(ifs6_in_mldquery, "\t%llu input MLD query%s\n");
   1244 	p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
   1245 	p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
   1246 
   1247 	p(ifs6_out_msg, "\t%llu total output message%s\n");
   1248 	p(ifs6_out_error, "\t%llu total output error message%s\n");
   1249 	p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
   1250 	p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
   1251 	p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
   1252 	p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
   1253 	p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
   1254 	p(ifs6_out_echo, "\t%llu output echo request%s\n");
   1255 	p(ifs6_out_echoreply, "\t%llu output echo reply%s\n");
   1256 	p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
   1257 	p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
   1258 	p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
   1259 	p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
   1260 	p(ifs6_out_redirect, "\t%llu output redirect%s\n");
   1261 	p(ifs6_out_mldquery, "\t%llu output MLD query%s\n");
   1262 	p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
   1263 	p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
   1264 
   1265   end:
   1266 	close(s);
   1267 #undef p
   1268 }
   1269 
   1270 /*
   1271  * Dump PIM statistics structure.
   1272  */
   1273 void
   1274 pim6_stats(u_long off, const char *name)
   1275 {
   1276 	uint64_t pim6stat[PIM6_NSTATS];
   1277 
   1278 	if (use_sysctl) {
   1279 		size_t size = sizeof(pim6stat);
   1280 
   1281 		if (sysctlbyname("net.inet6.pim6.stats", pim6stat, &size,
   1282 		    NULL, 0) == -1)
   1283 			return;
   1284         } else {
   1285 		warnx("%s stats not available via KVM.", name);
   1286 		return;
   1287 	}
   1288 	printf("%s:\n", name);
   1289 
   1290 #define	p(f, m) if (pim6stat[f] || sflag <= 1) \
   1291     printf(m, (unsigned long long)pim6stat[f], plural(pim6stat[f]))
   1292 	p(PIM6_STAT_RCV_TOTAL, "\t%llu message%s received\n");
   1293 	p(PIM6_STAT_RCV_TOOSHORT, "\t%llu message%s received with too few bytes\n");
   1294 	p(PIM6_STAT_RCV_BADSUM, "\t%llu message%s received with bad checksum\n");
   1295 	p(PIM6_STAT_RCV_BADVERSION, "\t%llu message%s received with bad version\n");
   1296 	p(PIM6_STAT_RCV_REGISTERS, "\t%llu register%s received\n");
   1297 	p(PIM6_STAT_RCV_BADREGISTERS, "\t%llu bad register%s received\n");
   1298 	p(PIM6_STAT_SND_REGISTERS, "\t%llu register%s sent\n");
   1299 #undef p
   1300 }
   1301 
   1302 /*
   1303  * Dump raw ip6 statistics structure.
   1304  */
   1305 void
   1306 rip6_stats(u_long off, const char *name)
   1307 {
   1308 	uint64_t rip6stat[RIP6_NSTATS];
   1309 	u_quad_t delivered;
   1310 
   1311 	if (use_sysctl) {
   1312 		size_t size = sizeof(rip6stat);
   1313 
   1314 		if (sysctlbyname("net.inet6.raw6.stats", rip6stat, &size,
   1315 		    NULL, 0) == -1)
   1316 			return;
   1317 	} else {
   1318 		warnx("%s stats not available via KVM.", name);
   1319 		return;
   1320 	}
   1321 	printf("%s:\n", name);
   1322 
   1323 #define	p(f, m) if (rip6stat[f] || sflag <= 1) \
   1324     printf(m, (unsigned long long)rip6stat[f], plural(rip6stat[f]))
   1325 	p(RIP6_STAT_IPACKETS, "\t%llu message%s received\n");
   1326 	p(RIP6_STAT_ISUM, "\t%llu checksum calculation%s on inbound\n");
   1327 	p(RIP6_STAT_BADSUM, "\t%llu message%s with bad checksum\n");
   1328 	p(RIP6_STAT_NOSOCK, "\t%llu message%s dropped due to no socket\n");
   1329 	p(RIP6_STAT_NOSOCKMCAST,
   1330 	    "\t%llu multicast message%s dropped due to no socket\n");
   1331 	p(RIP6_STAT_FULLSOCK,
   1332 	    "\t%llu message%s dropped due to full socket buffers\n");
   1333 	delivered = rip6stat[RIP6_STAT_IPACKETS] -
   1334 		    rip6stat[RIP6_STAT_BADSUM] -
   1335 		    rip6stat[RIP6_STAT_NOSOCK] -
   1336 		    rip6stat[RIP6_STAT_NOSOCKMCAST] -
   1337 		    rip6stat[RIP6_STAT_FULLSOCK];
   1338 	if (delivered || sflag <= 1)
   1339 		printf("\t%llu delivered\n", (unsigned long long)delivered);
   1340 	p(RIP6_STAT_OPACKETS, "\t%llu datagram%s output\n");
   1341 #undef p
   1342 }
   1343 
   1344 /*
   1345  * Pretty print an Internet address (net address + port).
   1346  * Take numeric_addr and numeric_port into consideration.
   1347  */
   1348 void
   1349 inet6print(const struct in6_addr *in6, int port, const char *proto)
   1350 {
   1351 #define GETSERVBYPORT6(port, proto, ret)\
   1352 do {\
   1353 	if (strcmp((proto), "tcp6") == 0)\
   1354 		(ret) = getservbyport((int)(port), "tcp");\
   1355 	else if (strcmp((proto), "udp6") == 0)\
   1356 		(ret) = getservbyport((int)(port), "udp");\
   1357 	else\
   1358 		(ret) = getservbyport((int)(port), (proto));\
   1359 } while (0)
   1360 	struct servent *sp = 0;
   1361 	char line[80], *cp;
   1362 	int lwidth;
   1363 
   1364 	lwidth = Aflag ? 12 : 16;
   1365 	if (vflag && lwidth < (int)strlen(inet6name(in6)))
   1366 		lwidth = strlen(inet6name(in6));
   1367 	snprintf(line, sizeof(line), "%.*s.", lwidth, inet6name(in6));
   1368 	cp = strchr(line, '\0');
   1369 	if (!numeric_port && port)
   1370 		GETSERVBYPORT6(port, proto, sp);
   1371 	if (sp || port == 0)
   1372 		snprintf(cp, sizeof(line) - (cp - line),
   1373 		    "%s", sp ? sp->s_name : "*");
   1374 	else
   1375 		snprintf(cp, sizeof(line) - (cp - line),
   1376 		    "%d", ntohs((u_short)port));
   1377 	lwidth = Aflag ? 18 : 22;
   1378 	if (vflag && lwidth < (int)strlen(line))
   1379 		lwidth = strlen(line);
   1380 	printf(" %-*.*s", lwidth, lwidth, line);
   1381 }
   1382 
   1383 /*
   1384  * Construct an Internet address representation.
   1385  * If the numeric_addr has been supplied, give
   1386  * numeric value, otherwise try for symbolic name.
   1387  */
   1388 
   1389 char *
   1390 inet6name(const struct in6_addr *in6p)
   1391 {
   1392 	char *cp;
   1393 	static char line[NI_MAXHOST];
   1394 	struct hostent *hp;
   1395 	static char domain[MAXHOSTNAMELEN + 1];
   1396 	static int first = 1;
   1397 	char hbuf[NI_MAXHOST];
   1398 	struct sockaddr_in6 sin6;
   1399 	const int niflag = NI_NUMERICHOST;
   1400 
   1401 	if (first && !numeric_addr) {
   1402 		first = 0;
   1403 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
   1404 		    (cp = strchr(domain, '.')))
   1405 			(void) strlcpy(domain, cp + 1, sizeof(domain));
   1406 		else
   1407 			domain[0] = 0;
   1408 	}
   1409 	cp = 0;
   1410 	if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
   1411 		hp = gethostbyaddr((const char *)in6p, sizeof(*in6p), AF_INET6);
   1412 		if (hp) {
   1413 			if ((cp = strchr(hp->h_name, '.')) &&
   1414 			    !strcmp(cp + 1, domain))
   1415 				*cp = 0;
   1416 			cp = hp->h_name;
   1417 		}
   1418 	}
   1419 	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
   1420 		strlcpy(line, "*", sizeof(line));
   1421 	else if (cp)
   1422 		strlcpy(line, cp, sizeof(line));
   1423 	else {
   1424 		memset(&sin6, 0, sizeof(sin6));
   1425 		sin6.sin6_len = sizeof(sin6);
   1426 		sin6.sin6_family = AF_INET6;
   1427 		sin6.sin6_addr = *in6p;
   1428 		inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
   1429 		    INET6_IS_ADDR_MC_LINKLOCAL);
   1430 		if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
   1431 				hbuf, sizeof(hbuf), NULL, 0, niflag) != 0)
   1432 			strlcpy(hbuf, "?", sizeof(hbuf));
   1433 		strlcpy(line, hbuf, sizeof(line));
   1434 	}
   1435 	return (line);
   1436 }
   1437 
   1438 /*
   1439  * Dump the contents of a TCP6 PCB.
   1440  */
   1441 void
   1442 tcp6_dump(u_long off, const char *name, u_long pcbaddr)
   1443 {
   1444 	callout_impl_t *ci;
   1445 	int i, hardticks;
   1446 	struct kinfo_pcb *pcblist;
   1447 #ifdef TCP6
   1448 #define mypcb tcp6cb
   1449 #else
   1450 #define mypcb tcpcb
   1451 #endif
   1452 	size_t j, len;
   1453 
   1454 	if (use_sysctl)
   1455 		pcblist = getpcblist_sysctl(name, &len);
   1456 	else
   1457 		pcblist = getpcblist_kmem(off, name, &len);
   1458 
   1459 	for (j = 0; j < len; j++)
   1460 		if (pcblist[j].ki_ppcbaddr == pcbaddr)
   1461 			break;
   1462 	free(pcblist);
   1463 
   1464 	if (j == len)
   1465 		errx(1, "0x%lx is not a valid pcb address", pcbaddr);
   1466 
   1467 	kread(pcbaddr, (char *)&mypcb, sizeof(mypcb));
   1468 	hardticks = get_hardticks();
   1469 
   1470 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
   1471 	printf("Timers:\n");
   1472 	for (i = 0; i < TCP6T_NTIMERS; i++) {
   1473 		ci = (callout_impl_t *)&tcpcb.t_timer[i];
   1474 		printf("\t%s: %d", tcptimers[i],
   1475 		    (ci->c_flags & CALLOUT_PENDING) ?
   1476 		    ci->c_time - hardticks : 0);
   1477 	}
   1478 	printf("\n\n");
   1479 
   1480 	if (mypcb.t_state < 0 || mypcb.t_state >= TCP6_NSTATES)
   1481 		printf("State: %d", mypcb.t_state);
   1482 	else
   1483 		printf("State: %s", tcp6states[mypcb.t_state]);
   1484 	printf(", flags 0x%x, in6pcb 0x%lx\n\n", mypcb.t_flags,
   1485 	    (u_long)mypcb.t_in6pcb);
   1486 
   1487 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", mypcb.t_rxtshift,
   1488 	    mypcb.t_rxtcur, mypcb.t_dupacks);
   1489 #ifdef TCP6
   1490 	printf("peermaxseg %u, maxseg %u, force %d\n\n", mypcb.t_peermaxseg,
   1491 	    mypcb.t_maxseg, mypcb.t_force);
   1492 #endif
   1493 
   1494 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
   1495 	    mypcb.snd_una, mypcb.snd_nxt, mypcb.snd_up);
   1496 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %llu\n\n",
   1497 	    mypcb.snd_wl1, mypcb.snd_wl2, mypcb.iss,
   1498 	    (unsigned long long)mypcb.snd_wnd);
   1499 
   1500 	printf("rcv_wnd %llu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
   1501 	    (unsigned long long)mypcb.rcv_wnd, mypcb.rcv_nxt,
   1502 	    mypcb.rcv_up, mypcb.irs);
   1503 
   1504 	printf("rcv_adv %u, snd_max %u, snd_cwnd %llu, snd_ssthresh %llu\n",
   1505 	    mypcb.rcv_adv, mypcb.snd_max, (unsigned long long)mypcb.snd_cwnd,
   1506 	    (unsigned long long)mypcb.snd_ssthresh);
   1507 
   1508 #ifdef TCP6
   1509 	printf("idle %d, rtt %d, " mypcb.t_idle, mypcb.t_rtt)
   1510 #endif
   1511 	printf("rtseq %u, srtt %d, rttvar %d, rttmin %d, "
   1512 	    "max_sndwnd %llu\n\n", mypcb.t_rtseq,
   1513 	    mypcb.t_srtt, mypcb.t_rttvar, mypcb.t_rttmin,
   1514 	    (unsigned long long)mypcb.max_sndwnd);
   1515 
   1516 	printf("oobflags %d, iobc %d, softerror %d\n\n", mypcb.t_oobflags,
   1517 	    mypcb.t_iobc, mypcb.t_softerror);
   1518 
   1519 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
   1520 	    mypcb.snd_scale, mypcb.rcv_scale, mypcb.request_r_scale,
   1521 	    mypcb.requested_s_scale);
   1522 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
   1523 	    mypcb.ts_recent, mypcb.ts_recent_age, mypcb.last_ack_sent);
   1524 }
   1525 
   1526 #endif /*INET6*/
   1527