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