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