Home | History | Annotate | Line # | Download | only in netstat
inet6.c revision 1.55
      1 /*	$NetBSD: inet6.c,v 1.55 2011/05/03 23:36:26 dyoung 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.55 2011/05/03 23:36:26 dyoung 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( "%-5.5s %-6.6s %-6.6s  %*.*s %*.*s %-13.13s%s\n",
    177 	    "Proto", "Recv-Q", "Send-Q",
    178 	    -width, width, "Local Address",
    179 	    -width, width, "Foreign Address",
    180 	    "(state)", Vflag ? " Expires" : "");
    181 }
    182 
    183 static void
    184 ip6protopr0(intptr_t ppcb, u_long rcv_sb_cc, u_long snd_sb_cc,
    185 	const struct in6_addr *laddr, u_int16_t lport,
    186 	const struct in6_addr *faddr, u_int16_t fport,
    187 	short t_state, const char *name, const struct timeval *expires)
    188 {
    189 	static const char *shorttcpstates[] = {
    190 		"CLOSED",       "LISTEN",       "SYNSEN",       "SYSRCV",
    191 		"ESTABL",       "CLWAIT",       "FWAIT1",       "CLOSNG",
    192 		"LASTAK",       "FWAIT2",       "TMWAIT",
    193 	};
    194 	int istcp;
    195 
    196 	istcp = strcmp(name, "tcp6") == 0;
    197 	if (Aflag)
    198 		printf("%8" PRIxPTR " ", ppcb);
    199 
    200 	printf("%-5.5s %6ld %6ld%s", name, rcv_sb_cc, snd_sb_cc,
    201 	    compact ? "" : " ");
    202 
    203 	inet6print(laddr, (int)lport, name);
    204 	inet6print(faddr, (int)fport, name);
    205 	if (istcp) {
    206 #ifdef TCP6
    207 		if (t_state < 0 || t_state >= TCP6_NSTATES)
    208 			printf(" %d", t_state);
    209 		else
    210 			printf(" %s", tcp6states[t_state]);
    211 #else
    212 		if (t_state < 0 || t_state >= TCP_NSTATES)
    213 			printf(" %d", t_state);
    214 		else
    215 			printf(" %s", compact ? shorttcpstates[t_state] :
    216 			    tcpstates[t_state]);
    217 #endif
    218 	}
    219 	if (Vflag && expires != NULL) {
    220 		if (expires->tv_sec == 0 && expires->tv_usec == -1)
    221 			printf(" reclaimed");
    222 		else {
    223 			struct timeval delta;
    224 
    225 			timersub(expires, &now, &delta);
    226 			printf(" %.3fms",
    227 			    delta.tv_sec * 1000.0 + delta.tv_usec / 1000.0);
    228 		}
    229 	}
    230 	putchar('\n');
    231 }
    232 
    233 static void
    234 dbg_printf(const char *fmt, ...)
    235 {
    236 	return;
    237 }
    238 
    239 void
    240 print_vtw_v6(const vtw_t *vtw)
    241 {
    242 	const vtw_v6_t *v6 = (const vtw_v6_t *)vtw;
    243 	struct timeval delta;
    244 	char buf[2][128];
    245 	static const struct timeval zero = {.tv_sec = 0, .tv_usec = 0};
    246 
    247 	inet_ntop(AF_INET6, &v6->laddr, buf[0], sizeof(buf[0]));
    248 	inet_ntop(AF_INET6, &v6->faddr, buf[1], sizeof(buf[1]));
    249 
    250 	timersub(&vtw->expire, &now, &delta);
    251 
    252 	if (vtw->expire.tv_sec == 0 && vtw->expire.tv_usec == -1) {
    253 		dbg_printf("%15.15s:%d %15.15s:%d reclaimed\n"
    254 		    ,buf[0], ntohs(v6->lport)
    255 		    ,buf[1], ntohs(v6->fport));
    256 		if (!(Vflag && vflag))
    257 			return;
    258 	} else if (vtw->expire.tv_sec == 0)
    259 		return;
    260 	else if (timercmp(&delta, &zero, <) && !(Vflag && vflag)) {
    261 		dbg_printf("%15.15s:%d %15.15s:%d expired\n"
    262 		    ,buf[0], ntohs(v6->lport)
    263 		    ,buf[1], ntohs(v6->fport));
    264 		return;
    265 	} else {
    266 		dbg_printf("%15.15s:%d %15.15s:%d expires in %.3fms\n"
    267 		    ,buf[0], ntohs(v6->lport)
    268 		    ,buf[1], ntohs(v6->fport)
    269 		    ,delta.tv_sec * 1000.0 + delta.tv_usec / 1000.0);
    270 	}
    271 	ip6protopr0(0, 0, 0,
    272 		 &v6->laddr, v6->lport,
    273 		 &v6->faddr, v6->fport,
    274 		 TCPS_TIME_WAIT, "tcp6", &vtw->expire);
    275 }
    276 
    277 void
    278 ip6protopr(u_long off, const char *name)
    279 {
    280 	struct inpcbtable table;
    281 	struct in6pcb *head, *prev, *next;
    282 	int istcp = strcmp(name, "tcp6") == 0;
    283 	static int first = 1;
    284 
    285 	compact = 0;
    286 	if (Aflag) {
    287 		if (!numeric_addr)
    288 			width = 18;
    289 		else {
    290 			width = 21;
    291 			compact = 1;
    292 		}
    293 	} else
    294 		width = 22;
    295 
    296 	if (use_sysctl) {
    297 		struct kinfo_pcb *pcblist;
    298 		int mib[8];
    299 		size_t namelen = 0, size = 0, i;
    300 		char *mibname = NULL;
    301 
    302 		memset(mib, 0, sizeof(mib));
    303 
    304 		if (asprintf(&mibname, "net.inet6.%s.pcblist", name) == -1)
    305 			err(1, "asprintf");
    306 
    307 		/* get dynamic pcblist node */
    308 		if (sysctlnametomib(mibname, mib, &namelen) == -1) {
    309 			if (errno == ENOENT)
    310 				return;
    311 
    312 			err(1, "sysctlnametomib: %s", mibname);
    313 		}
    314 
    315 		if (prog_sysctl(mib, __arraycount(mib),
    316 		    NULL, &size, NULL, 0) == -1)
    317 			err(1, "sysctl (query)");
    318 
    319 		if ((pcblist = malloc(size)) == NULL)
    320 			err(1, "malloc");
    321 		memset(pcblist, 0, size);
    322 
    323 		mib[6] = sizeof(*pcblist);
    324 		mib[7] = size / sizeof(*pcblist);
    325 
    326 		if (prog_sysctl(mib, __arraycount(mib), pcblist,
    327 		    &size, NULL, 0) == -1)
    328 			err(1, "sysctl (copy)");
    329 
    330 		for (i = 0; i < size / sizeof(*pcblist); i++) {
    331 			struct sockaddr_in6 src, dst;
    332 
    333 			memcpy(&src, &pcblist[i].ki_s, sizeof(src));
    334 			memcpy(&dst, &pcblist[i].ki_d, sizeof(dst));
    335 
    336 			if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&dst.sin6_addr))
    337 				continue;
    338 
    339 			if (first) {
    340 				ip6protoprhdr();
    341 				first = 0;
    342 			}
    343 
    344 			ip6protopr0((intptr_t) pcblist[i].ki_ppcbaddr,
    345 			    pcblist[i].ki_rcvq, pcblist[i].ki_sndq,
    346 			    &src.sin6_addr, src.sin6_port,
    347 			    &dst.sin6_addr, dst.sin6_port,
    348 			    pcblist[i].ki_tstate, name, NULL);
    349 		}
    350 
    351 		free(pcblist);
    352 		goto end;
    353 	}
    354 
    355 	if (off == 0)
    356 		return;
    357 	kread(off, (char *)&table, sizeof (table));
    358 	head = prev =
    359 	    (struct in6pcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
    360 	next = (struct in6pcb *)table.inpt_queue.cqh_first;
    361 
    362 	while (next != head) {
    363 		kread((u_long)next, (char *)&in6pcb, sizeof in6pcb);
    364 		if ((struct in6pcb *)in6pcb.in6p_queue.cqe_prev != prev) {
    365 			printf("???\n");
    366 			break;
    367 		}
    368 		prev = next;
    369 		next = (struct in6pcb *)in6pcb.in6p_queue.cqe_next;
    370 
    371 		if (in6pcb.in6p_af != AF_INET6)
    372 			continue;
    373 
    374 		if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr))
    375 			continue;
    376 		kread((u_long)in6pcb.in6p_socket, (char *)&sockb,
    377 		    sizeof (sockb));
    378 		if (istcp) {
    379 #ifdef TCP6
    380 			kread((u_long)in6pcb.in6p_ppcb,
    381 			    (char *)&tcp6cb, sizeof (tcp6cb));
    382 #else
    383 			kread((u_long)in6pcb.in6p_ppcb,
    384 			    (char *)&tcpcb, sizeof (tcpcb));
    385 #endif
    386 		}
    387 		if (first) {
    388 			ip6protoprhdr();
    389 			first = 0;
    390 		}
    391 		ip6protopr0(istcp ? (intptr_t) in6pcb.in6p_ppcb :
    392 		    (intptr_t) prev, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc,
    393 		    &in6pcb.in6p_laddr, in6pcb.in6p_lport,
    394 		    &in6pcb.in6p_faddr, in6pcb.in6p_fport,
    395 		    tcpcb.t_state, name, NULL);
    396 
    397 	}
    398 end:
    399 	if (istcp) {
    400 		gettimeofday(&now, NULL);
    401 		show_vtw_v6(print_vtw_v6);
    402 	}
    403 }
    404 
    405 #ifdef TCP6
    406 /*
    407  * Dump TCP6 statistics structure.
    408  */
    409 void
    410 tcp6_stats(u_long off, const char *name)
    411 {
    412 	struct tcp6stat tcp6stat;
    413 
    414 	if (use_sysctl) {
    415 		size_t size = sizeof(tcp6stat);
    416 
    417 		if (sysctlbyname("net.inet6.tcp6.stats", &tcp6stat, &size,
    418 		    NULL, 0) == -1)
    419 			return;
    420 	} else {
    421 		warnx("%s stats not available via KVM.", name);
    422 		return;
    423 	}
    424 
    425 	printf ("%s:\n", name);
    426 
    427 #define	p(f, m) if (tcp6stat.f || sflag <= 1) \
    428     printf(m, tcp6stat.f, plural(tcp6stat.f))
    429 #define	p2(f1, f2, m) if (tcp6stat.f1 || tcp6stat.f2 || sflag <= 1) \
    430     printf(m, tcp6stat.f1, plural(tcp6stat.f1), tcp6stat.f2, plural(tcp6stat.f2))
    431 #define	p3(f, m) if (tcp6stat.f || sflag <= 1) \
    432     printf(m, tcp6stat.f, plurales(tcp6stat.f))
    433 
    434 	p(tcp6s_sndtotal, "\t%ld packet%s sent\n");
    435 	p2(tcp6s_sndpack,tcp6s_sndbyte,
    436 		"\t\t%ld data packet%s (%ld byte%s)\n");
    437 	p2(tcp6s_sndrexmitpack, tcp6s_sndrexmitbyte,
    438 		"\t\t%ld data packet%s (%ld byte%s) retransmitted\n");
    439 	p2(tcp6s_sndacks, tcp6s_delack,
    440 		"\t\t%ld ack-only packet%s (%ld packet%s delayed)\n");
    441 	p(tcp6s_sndurg, "\t\t%ld URG only packet%s\n");
    442 	p(tcp6s_sndprobe, "\t\t%ld window probe packet%s\n");
    443 	p(tcp6s_sndwinup, "\t\t%ld window update packet%s\n");
    444 	p(tcp6s_sndctrl, "\t\t%ld control packet%s\n");
    445 	p(tcp6s_rcvtotal, "\t%ld packet%s received\n");
    446 	p2(tcp6s_rcvackpack, tcp6s_rcvackbyte, "\t\t%ld ack%s (for %ld byte%s)\n");
    447 	p(tcp6s_rcvdupack, "\t\t%ld duplicate ack%s\n");
    448 	p(tcp6s_rcvacktoomuch, "\t\t%ld ack%s for unsent data\n");
    449 	p2(tcp6s_rcvpack, tcp6s_rcvbyte,
    450 		"\t\t%ld packet%s (%ld byte%s) received in-sequence\n");
    451 	p2(tcp6s_rcvduppack, tcp6s_rcvdupbyte,
    452 		"\t\t%ld completely duplicate packet%s (%ld byte%s)\n");
    453 	p(tcp6s_pawsdrop, "\t\t%ld old duplicate packet%s\n");
    454 	p2(tcp6s_rcvpartduppack, tcp6s_rcvpartdupbyte,
    455 		"\t\t%ld packet%s with some dup. data (%ld byte%s duped)\n");
    456 	p2(tcp6s_rcvoopack, tcp6s_rcvoobyte,
    457 		"\t\t%ld out-of-order packet%s (%ld byte%s)\n");
    458 	p2(tcp6s_rcvpackafterwin, tcp6s_rcvbyteafterwin,
    459 		"\t\t%ld packet%s (%ld byte%s) of data after window\n");
    460 	p(tcp6s_rcvwinprobe, "\t\t%ld window probe%s\n");
    461 	p(tcp6s_rcvwinupd, "\t\t%ld window update packet%s\n");
    462 	p(tcp6s_rcvafterclose, "\t\t%ld packet%s received after close\n");
    463 	p(tcp6s_rcvbadsum, "\t\t%ld discarded for bad checksum%s\n");
    464 	p(tcp6s_rcvbadoff, "\t\t%ld discarded for bad header offset field%s\n");
    465 	p(tcp6s_rcvshort, "\t\t%ld discarded because packet%s too short\n");
    466 	p(tcp6s_connattempt, "\t%ld connection request%s\n");
    467 	p(tcp6s_accepts, "\t%ld connection accept%s\n");
    468 	p(tcp6s_badsyn, "\t%ld bad connection attempt%s\n");
    469 	p(tcp6s_connects, "\t%ld connection%s established (including accepts)\n");
    470 	p2(tcp6s_closed, tcp6s_drops,
    471 		"\t%ld connection%s closed (including %ld drop%s)\n");
    472 	p(tcp6s_conndrops, "\t%ld embryonic connection%s dropped\n");
    473 	p2(tcp6s_rttupdated, tcp6s_segstimed,
    474 		"\t%ld segment%s updated rtt (of %ld attempt%s)\n");
    475 	p(tcp6s_rexmttimeo, "\t%ld retransmit timeout%s\n");
    476 	p(tcp6s_timeoutdrop, "\t\t%ld connection%s dropped by rexmit timeout\n");
    477 	p(tcp6s_persisttimeo, "\t%ld persist timeout%s\n");
    478 	p(tcp6s_persistdrop, "\t%ld connection%s timed out in persist\n");
    479 	p(tcp6s_keeptimeo, "\t%ld keepalive timeout%s\n");
    480 	p(tcp6s_keepprobe, "\t\t%ld keepalive probe%s sent\n");
    481 	p(tcp6s_keepdrops, "\t\t%ld connection%s dropped by keepalive\n");
    482 	p(tcp6s_predack, "\t%ld correct ACK header prediction%s\n");
    483 	p(tcp6s_preddat, "\t%ld correct data packet header prediction%s\n");
    484 	p3(tcp6s_pcbcachemiss, "\t%ld PCB cache miss%s\n");
    485 #undef p
    486 #undef p2
    487 #undef p3
    488 }
    489 #endif
    490 
    491 /*
    492  * Dump UDP6 statistics structure.
    493  */
    494 void
    495 udp6_stats(u_long off, const char *name)
    496 {
    497 	uint64_t udp6stat[UDP6_NSTATS];
    498 	u_quad_t delivered;
    499 
    500 	if (use_sysctl) {
    501 		size_t size = sizeof(udp6stat);
    502 
    503 		if (sysctlbyname("net.inet6.udp6.stats", udp6stat, &size,
    504 		    NULL, 0) == -1)
    505 			return;
    506 	} else {
    507 		warnx("%s stats not available via KVM.", name);
    508 		return;
    509 	}
    510 	printf("%s:\n", name);
    511 #define	p(f, m) if (udp6stat[f] || sflag <= 1) \
    512     printf(m, (unsigned long long)udp6stat[f], plural(udp6stat[f]))
    513 #define	p1(f, m) if (udp6stat[f] || sflag <= 1) \
    514     printf(m, (unsigned long long)udp6stat[f])
    515 	p(UDP6_STAT_IPACKETS, "\t%llu datagram%s received\n");
    516 	p1(UDP6_STAT_HDROPS, "\t%llu with incomplete header\n");
    517 	p1(UDP6_STAT_BADLEN, "\t%llu with bad data length field\n");
    518 	p1(UDP6_STAT_BADSUM, "\t%llu with bad checksum\n");
    519 	p1(UDP6_STAT_NOSUM, "\t%llu with no checksum\n");
    520 	p1(UDP6_STAT_NOPORT, "\t%llu dropped due to no socket\n");
    521 	p(UDP6_STAT_NOPORTMCAST,
    522 	    "\t%llu multicast datagram%s dropped due to no socket\n");
    523 	p1(UDP6_STAT_FULLSOCK, "\t%llu dropped due to full socket buffers\n");
    524 	delivered = udp6stat[UDP6_STAT_IPACKETS] -
    525 		    udp6stat[UDP6_STAT_HDROPS] -
    526 		    udp6stat[UDP6_STAT_BADLEN] -
    527 		    udp6stat[UDP6_STAT_BADSUM] -
    528 		    udp6stat[UDP6_STAT_NOPORT] -
    529 		    udp6stat[UDP6_STAT_NOPORTMCAST] -
    530 		    udp6stat[UDP6_STAT_FULLSOCK];
    531 	if (delivered || sflag <= 1)
    532 		printf("\t%llu delivered\n", (unsigned long long)delivered);
    533 	p(UDP6_STAT_OPACKETS, "\t%llu datagram%s output\n");
    534 #undef p
    535 #undef p1
    536 }
    537 
    538 static	const char *ip6nh[] = {
    539 /*0*/	"hop by hop",
    540 	"ICMP",
    541 	"IGMP",
    542 	NULL,
    543 	"IP",
    544 /*5*/	NULL,
    545 	"TCP",
    546 	NULL,
    547 	NULL,
    548 	NULL,
    549 /*10*/	NULL, NULL, NULL, NULL, NULL,
    550 /*15*/	NULL,
    551 	NULL,
    552 	"UDP",
    553 	NULL,
    554 	NULL,
    555 /*20*/	NULL,
    556 	NULL,
    557 	"IDP",
    558 	NULL,
    559 	NULL,
    560 /*25*/	NULL,
    561 	NULL,
    562 	NULL,
    563 	NULL,
    564 	"TP",
    565 /*30*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    566 /*40*/	NULL,
    567 	"IP6",
    568 	NULL,
    569 	"routing",
    570 	"fragment",
    571 /*45*/	NULL, NULL, NULL, NULL, NULL,
    572 /*50*/	"ESP",
    573 	"AH",
    574 	NULL,
    575 	NULL,
    576 	NULL,
    577 /*55*/	NULL,
    578 	NULL,
    579 	NULL,
    580 	"ICMP6",
    581 	"no next header",
    582 /*60*/	"destination option",
    583 	NULL,
    584 	NULL,
    585 	NULL,
    586 	NULL,
    587 /*65*/	NULL, NULL, NULL, NULL, NULL,
    588 /*70*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    589 /*80*/	"ISOIP",
    590 	NULL,
    591 	NULL,
    592 	NULL,
    593 	NULL,
    594 	NULL,
    595 	NULL,
    596 	NULL,
    597 	NULL,
    598 	"OSPF",
    599 /*90*/	NULL, NULL, NULL, NULL, NULL,
    600 /*95*/	NULL,
    601 	NULL,
    602 	"Ethernet",
    603 	NULL,
    604 	NULL,
    605 /*100*/	NULL,
    606 	NULL,
    607 	NULL,
    608 	"PIM",
    609 	NULL,
    610 /*105*/	NULL, NULL, NULL, NULL, NULL,
    611 /*110*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    612 /*120*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    613 /*130*/	NULL,
    614 	NULL,
    615 	"SCTP",
    616 	NULL,
    617 	NULL,
    618 /*135*/	NULL, NULL, NULL, NULL, NULL,
    619 /*140*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    620 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    621 /*160*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    622 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    623 /*180*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    624 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    625 /*200*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    626 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    627 /*220*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    628 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    629 /*240*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    630 	NULL, NULL, NULL, NULL, NULL, NULL,
    631 };
    632 
    633 /*
    634  * Dump IP6 statistics structure.
    635  */
    636 void
    637 ip6_stats(u_long off, const char *name)
    638 {
    639 	uint64_t ip6stat[IP6_NSTATS];
    640 	int first, i;
    641 	struct protoent *ep;
    642 	const char *n;
    643 
    644 	if (use_sysctl) {
    645 		size_t size = sizeof(ip6stat);
    646 
    647 		if (sysctlbyname("net.inet6.ip6.stats", ip6stat, &size,
    648 		    NULL, 0) == -1)
    649 			return;
    650 	} else {
    651 		warnx("%s stats not available via KVM.", name);
    652 		return;
    653 	}
    654 	printf("%s:\n", name);
    655 
    656 #define	p(f, m) if (ip6stat[f] || sflag <= 1) \
    657     printf(m, (unsigned long long)ip6stat[f], plural(ip6stat[f]))
    658 #define	p1(f, m) if (ip6stat[f] || sflag <= 1) \
    659     printf(m, (unsigned long long)ip6stat[f])
    660 
    661 	p(IP6_STAT_TOTAL, "\t%llu total packet%s received\n");
    662 	p1(IP6_STAT_TOOSMALL, "\t%llu with size smaller than minimum\n");
    663 	p1(IP6_STAT_TOOSHORT, "\t%llu with data size < data length\n");
    664 	p1(IP6_STAT_BADOPTIONS, "\t%llu with bad options\n");
    665 	p1(IP6_STAT_BADVERS, "\t%llu with incorrect version number\n");
    666 	p(IP6_STAT_FRAGMENTS, "\t%llu fragment%s received\n");
    667 	p(IP6_STAT_FRAGDROPPED,
    668 	    "\t%llu fragment%s dropped (dup or out of space)\n");
    669 	p(IP6_STAT_FRAGTIMEOUT, "\t%llu fragment%s dropped after timeout\n");
    670 	p(IP6_STAT_FRAGOVERFLOW, "\t%llu fragment%s that exceeded limit\n");
    671 	p(IP6_STAT_REASSEMBLED, "\t%llu packet%s reassembled ok\n");
    672 	p(IP6_STAT_DELIVERED, "\t%llu packet%s for this host\n");
    673 	p(IP6_STAT_FORWARD, "\t%llu packet%s forwarded\n");
    674 	p(IP6_STAT_FASTFORWARD, "\t%llu packet%s fast forwarded\n");
    675 	p1(IP6_STAT_FASTFORWARDFLOWS, "\t%llu fast forward flows\n");
    676 	p(IP6_STAT_CANTFORWARD, "\t%llu packet%s not forwardable\n");
    677 	p(IP6_STAT_REDIRECTSENT, "\t%llu redirect%s sent\n");
    678 	p(IP6_STAT_LOCALOUT, "\t%llu packet%s sent from this host\n");
    679 	p(IP6_STAT_RAWOUT, "\t%llu packet%s sent with fabricated ip header\n");
    680 	p(IP6_STAT_ODROPPED,
    681 	    "\t%llu output packet%s dropped due to no bufs, etc.\n");
    682 	p(IP6_STAT_NOROUTE, "\t%llu output packet%s discarded due to no route\n");
    683 	p(IP6_STAT_FRAGMENTED, "\t%llu output datagram%s fragmented\n");
    684 	p(IP6_STAT_OFRAGMENTS, "\t%llu fragment%s created\n");
    685 	p(IP6_STAT_CANTFRAG, "\t%llu datagram%s that can't be fragmented\n");
    686 	p(IP6_STAT_BADSCOPE, "\t%llu packet%s that violated scope rules\n");
    687 	p(IP6_STAT_NOTMEMBER, "\t%llu multicast packet%s which we don't join\n");
    688 	for (first = 1, i = 0; i < 256; i++)
    689 		if (ip6stat[IP6_STAT_NXTHIST + i] != 0) {
    690 			if (first) {
    691 				printf("\tInput packet histogram:\n");
    692 				first = 0;
    693 			}
    694 			n = NULL;
    695 			if (ip6nh[i])
    696 				n = ip6nh[i];
    697 			else if ((ep = getprotobynumber(i)) != NULL)
    698 				n = ep->p_name;
    699 			if (n)
    700 				printf("\t\t%s: %llu\n", n,
    701 				    (unsigned long long)ip6stat[IP6_STAT_NXTHIST + i]);
    702 			else
    703 				printf("\t\t#%d: %llu\n", i,
    704 				    (unsigned long long)ip6stat[IP6_STAT_NXTHIST + i]);
    705 		}
    706 	printf("\tMbuf statistics:\n");
    707 	p(IP6_STAT_M1, "\t\t%llu one mbuf%s\n");
    708 	for (first = 1, i = 0; i < 32; i++) {
    709 		char ifbuf[IFNAMSIZ];
    710 		if (ip6stat[IP6_STAT_M2M + i] != 0) {
    711 			if (first) {
    712 				printf("\t\ttwo or more mbuf:\n");
    713 				first = 0;
    714 			}
    715 			printf("\t\t\t%s = %llu\n",
    716 			       if_indextoname(i, ifbuf),
    717 			       (unsigned long long)ip6stat[IP6_STAT_M2M + i]);
    718 		}
    719 	}
    720 	p(IP6_STAT_MEXT1, "\t\t%llu one ext mbuf%s\n");
    721 	p(IP6_STAT_MEXT2M, "\t\t%llu two or more ext mbuf%s\n");
    722 	p(IP6_STAT_EXTHDRTOOLONG,
    723 	    "\t%llu packet%s whose headers are not continuous\n");
    724 	p(IP6_STAT_NOGIF, "\t%llu tunneling packet%s that can't find gif\n");
    725 	p(IP6_STAT_TOOMANYHDR,
    726 	    "\t%llu packet%s discarded due to too many headers\n");
    727 
    728 	/* for debugging source address selection */
    729 #define PRINT_SCOPESTAT(s,i) do {\
    730 		switch(i) { /* XXX hardcoding in each case */\
    731 		case 1:\
    732 			p(s, "\t\t%llu node-local%s\n");\
    733 			break;\
    734 		case 2:\
    735 			p(s, "\t\t%llu link-local%s\n");\
    736 			break;\
    737 		case 5:\
    738 			p(s, "\t\t%llu site-local%s\n");\
    739 			break;\
    740 		case 14:\
    741 			p(s, "\t\t%llu global%s\n");\
    742 			break;\
    743 		default:\
    744 			printf("\t\t%llu addresses scope=%x\n",\
    745 			       (unsigned long long)ip6stat[s], i);\
    746 		}\
    747 	} while(/*CONSTCOND*/0);
    748 
    749 	p(IP6_STAT_SOURCES_NONE,
    750 	  "\t%llu failure%s of source address selection\n");
    751 	for (first = 1, i = 0; i < 16; i++) {
    752 		if (ip6stat[IP6_STAT_SOURCES_SAMEIF + i]) {
    753 			if (first) {
    754 				printf("\tsource addresses on an outgoing I/F\n");
    755 				first = 0;
    756 			}
    757 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_SAMEIF + i, i);
    758 		}
    759 	}
    760 	for (first = 1, i = 0; i < 16; i++) {
    761 		if (ip6stat[IP6_STAT_SOURCES_OTHERIF + i]) {
    762 			if (first) {
    763 				printf("\tsource addresses on a non-outgoing I/F\n");
    764 				first = 0;
    765 			}
    766 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_OTHERIF + i, i);
    767 		}
    768 	}
    769 	for (first = 1, i = 0; i < 16; i++) {
    770 		if (ip6stat[IP6_STAT_SOURCES_SAMESCOPE + i]) {
    771 			if (first) {
    772 				printf("\tsource addresses of same scope\n");
    773 				first = 0;
    774 			}
    775 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_SAMESCOPE + i, i);
    776 		}
    777 	}
    778 	for (first = 1, i = 0; i < 16; i++) {
    779 		if (ip6stat[IP6_STAT_SOURCES_OTHERSCOPE + i]) {
    780 			if (first) {
    781 				printf("\tsource addresses of a different scope\n");
    782 				first = 0;
    783 			}
    784 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_OTHERSCOPE + i, i);
    785 		}
    786 	}
    787 	for (first = 1, i = 0; i < 16; i++) {
    788 		if (ip6stat[IP6_STAT_SOURCES_DEPRECATED + i]) {
    789 			if (first) {
    790 				printf("\tdeprecated source addresses\n");
    791 				first = 0;
    792 			}
    793 			PRINT_SCOPESTAT(IP6_STAT_SOURCES_DEPRECATED + i, i);
    794 		}
    795 	}
    796 
    797 	p1(IP6_STAT_FORWARD_CACHEHIT, "\t%llu forward cache hit\n");
    798 	p1(IP6_STAT_FORWARD_CACHEMISS, "\t%llu forward cache miss\n");
    799 #undef p
    800 #undef p1
    801 }
    802 
    803 /*
    804  * Dump IPv6 per-interface statistics based on RFC 2465.
    805  */
    806 void
    807 ip6_ifstats(const char *ifname)
    808 {
    809 	struct in6_ifreq ifr;
    810 	int s;
    811 #define	p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
    812     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, \
    813 	plural(ifr.ifr_ifru.ifru_stat.f))
    814 #define	p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
    815     printf(m, (unsigned long long)ip6stat.f)
    816 
    817 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    818 		perror("Warning: socket(AF_INET6)");
    819 		return;
    820 	}
    821 
    822 	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    823 	printf("ip6 on %s:\n", ifname);
    824 
    825 	if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
    826 		perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
    827 		goto end;
    828 	}
    829 
    830 	p(ifs6_in_receive, "\t%llu total input datagram%s\n");
    831 	p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
    832 	p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
    833 	p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
    834 	p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
    835 	p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
    836 	p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
    837 	p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
    838 	p(ifs6_in_deliver,
    839 	  "\t%llu datagram%s delivered to an upper layer protocol\n");
    840 	p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
    841 	p(ifs6_out_request,
    842 	  "\t%llu datagram%s sent from an upper layer protocol\n");
    843 	p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
    844 	p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
    845 	p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
    846 	p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
    847 	p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
    848 	p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
    849 	p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
    850 	p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
    851 	p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
    852 
    853   end:
    854 	close(s);
    855 
    856 #undef p
    857 #undef p_5
    858 }
    859 
    860 static	const char *icmp6names[] = {
    861 	"#0",
    862 	"unreach",
    863 	"packet too big",
    864 	"time exceed",
    865 	"parameter problem",
    866 	"#5",
    867 	"#6",
    868 	"#7",
    869 	"#8",
    870 	"#9",
    871 	"#10",
    872 	"#11",
    873 	"#12",
    874 	"#13",
    875 	"#14",
    876 	"#15",
    877 	"#16",
    878 	"#17",
    879 	"#18",
    880 	"#19",
    881 	"#20",
    882 	"#21",
    883 	"#22",
    884 	"#23",
    885 	"#24",
    886 	"#25",
    887 	"#26",
    888 	"#27",
    889 	"#28",
    890 	"#29",
    891 	"#30",
    892 	"#31",
    893 	"#32",
    894 	"#33",
    895 	"#34",
    896 	"#35",
    897 	"#36",
    898 	"#37",
    899 	"#38",
    900 	"#39",
    901 	"#40",
    902 	"#41",
    903 	"#42",
    904 	"#43",
    905 	"#44",
    906 	"#45",
    907 	"#46",
    908 	"#47",
    909 	"#48",
    910 	"#49",
    911 	"#50",
    912 	"#51",
    913 	"#52",
    914 	"#53",
    915 	"#54",
    916 	"#55",
    917 	"#56",
    918 	"#57",
    919 	"#58",
    920 	"#59",
    921 	"#60",
    922 	"#61",
    923 	"#62",
    924 	"#63",
    925 	"#64",
    926 	"#65",
    927 	"#66",
    928 	"#67",
    929 	"#68",
    930 	"#69",
    931 	"#70",
    932 	"#71",
    933 	"#72",
    934 	"#73",
    935 	"#74",
    936 	"#75",
    937 	"#76",
    938 	"#77",
    939 	"#78",
    940 	"#79",
    941 	"#80",
    942 	"#81",
    943 	"#82",
    944 	"#83",
    945 	"#84",
    946 	"#85",
    947 	"#86",
    948 	"#87",
    949 	"#88",
    950 	"#89",
    951 	"#80",
    952 	"#91",
    953 	"#92",
    954 	"#93",
    955 	"#94",
    956 	"#95",
    957 	"#96",
    958 	"#97",
    959 	"#98",
    960 	"#99",
    961 	"#100",
    962 	"#101",
    963 	"#102",
    964 	"#103",
    965 	"#104",
    966 	"#105",
    967 	"#106",
    968 	"#107",
    969 	"#108",
    970 	"#109",
    971 	"#110",
    972 	"#111",
    973 	"#112",
    974 	"#113",
    975 	"#114",
    976 	"#115",
    977 	"#116",
    978 	"#117",
    979 	"#118",
    980 	"#119",
    981 	"#120",
    982 	"#121",
    983 	"#122",
    984 	"#123",
    985 	"#124",
    986 	"#125",
    987 	"#126",
    988 	"#127",
    989 	"echo",
    990 	"echo reply",
    991 	"multicast listener query",
    992 	"multicast listener report",
    993 	"multicast listener done",
    994 	"router solicitation",
    995 	"router advertisement",
    996 	"neighbor solicitation",
    997 	"neighbor advertisement",
    998 	"redirect",
    999 	"router renumbering",
   1000 	"node information request",
   1001 	"node information reply",
   1002 	"#141",
   1003 	"#142",
   1004 	"#143",
   1005 	"#144",
   1006 	"#145",
   1007 	"#146",
   1008 	"#147",
   1009 	"#148",
   1010 	"#149",
   1011 	"#150",
   1012 	"#151",
   1013 	"#152",
   1014 	"#153",
   1015 	"#154",
   1016 	"#155",
   1017 	"#156",
   1018 	"#157",
   1019 	"#158",
   1020 	"#159",
   1021 	"#160",
   1022 	"#161",
   1023 	"#162",
   1024 	"#163",
   1025 	"#164",
   1026 	"#165",
   1027 	"#166",
   1028 	"#167",
   1029 	"#168",
   1030 	"#169",
   1031 	"#170",
   1032 	"#171",
   1033 	"#172",
   1034 	"#173",
   1035 	"#174",
   1036 	"#175",
   1037 	"#176",
   1038 	"#177",
   1039 	"#178",
   1040 	"#179",
   1041 	"#180",
   1042 	"#181",
   1043 	"#182",
   1044 	"#183",
   1045 	"#184",
   1046 	"#185",
   1047 	"#186",
   1048 	"#187",
   1049 	"#188",
   1050 	"#189",
   1051 	"#180",
   1052 	"#191",
   1053 	"#192",
   1054 	"#193",
   1055 	"#194",
   1056 	"#195",
   1057 	"#196",
   1058 	"#197",
   1059 	"#198",
   1060 	"#199",
   1061 	"#200",
   1062 	"#201",
   1063 	"#202",
   1064 	"#203",
   1065 	"#204",
   1066 	"#205",
   1067 	"#206",
   1068 	"#207",
   1069 	"#208",
   1070 	"#209",
   1071 	"#210",
   1072 	"#211",
   1073 	"#212",
   1074 	"#213",
   1075 	"#214",
   1076 	"#215",
   1077 	"#216",
   1078 	"#217",
   1079 	"#218",
   1080 	"#219",
   1081 	"#220",
   1082 	"#221",
   1083 	"#222",
   1084 	"#223",
   1085 	"#224",
   1086 	"#225",
   1087 	"#226",
   1088 	"#227",
   1089 	"#228",
   1090 	"#229",
   1091 	"#230",
   1092 	"#231",
   1093 	"#232",
   1094 	"#233",
   1095 	"#234",
   1096 	"#235",
   1097 	"#236",
   1098 	"#237",
   1099 	"#238",
   1100 	"#239",
   1101 	"#240",
   1102 	"#241",
   1103 	"#242",
   1104 	"#243",
   1105 	"#244",
   1106 	"#245",
   1107 	"#246",
   1108 	"#247",
   1109 	"#248",
   1110 	"#249",
   1111 	"#250",
   1112 	"#251",
   1113 	"#252",
   1114 	"#253",
   1115 	"#254",
   1116 	"#255",
   1117 };
   1118 
   1119 /*
   1120  * Dump ICMPv6 statistics.
   1121  */
   1122 void
   1123 icmp6_stats(u_long off, const char *name)
   1124 {
   1125 	uint64_t icmp6stat[ICMP6_NSTATS];
   1126 	register int i, first;
   1127 
   1128 	if (use_sysctl) {
   1129 		size_t size = sizeof(icmp6stat);
   1130 
   1131 		if (sysctlbyname("net.inet6.icmp6.stats", icmp6stat, &size,
   1132 		    NULL, 0) == -1)
   1133 			return;
   1134 	} else {
   1135 		warnx("%s stats not available via KVM.", name);
   1136 		return;
   1137 	}
   1138 
   1139 	printf("%s:\n", name);
   1140 
   1141 #define	p(f, m) if (icmp6stat[f] || sflag <= 1) \
   1142     printf(m, (unsigned long long)icmp6stat[f], plural(icmp6stat[f]))
   1143 #define p_oerr(f, m) if (icmp6stat[ICMP6_STAT_OUTERRHIST + f] || sflag <= 1) \
   1144     printf(m, (unsigned long long)icmp6stat[ICMP6_STAT_OUTERRHIST + f])
   1145 
   1146 	p(ICMP6_STAT_ERROR, "\t%llu call%s to icmp6_error\n");
   1147 	p(ICMP6_STAT_CANTERROR,
   1148 	    "\t%llu error%s not generated because old message was icmp6 or so\n");
   1149 	p(ICMP6_STAT_TOOFREQ,
   1150 	    "\t%llu error%s not generated because of rate limitation\n");
   1151 	for (first = 1, i = 0; i < 256; i++)
   1152 		if (icmp6stat[ICMP6_STAT_OUTHIST + i] != 0) {
   1153 			if (first) {
   1154 				printf("\tOutput packet histogram:\n");
   1155 				first = 0;
   1156 			}
   1157 			printf("\t\t%s: %llu\n", icmp6names[i],
   1158 			 (unsigned long long)icmp6stat[ICMP6_STAT_OUTHIST + i]);
   1159 		}
   1160 	p(ICMP6_STAT_BADCODE, "\t%llu message%s with bad code fields\n");
   1161 	p(ICMP6_STAT_TOOSHORT, "\t%llu message%s < minimum length\n");
   1162 	p(ICMP6_STAT_CHECKSUM, "\t%llu bad checksum%s\n");
   1163 	p(ICMP6_STAT_BADLEN, "\t%llu message%s with bad length\n");
   1164 	for (first = 1, i = 0; i < ICMP6_MAXTYPE; i++)
   1165 		if (icmp6stat[ICMP6_STAT_INHIST + i] != 0) {
   1166 			if (first) {
   1167 				printf("\tInput packet histogram:\n");
   1168 				first = 0;
   1169 			}
   1170 			printf("\t\t%s: %llu\n", icmp6names[i],
   1171 			  (unsigned long long)icmp6stat[ICMP6_STAT_INHIST + i]);
   1172 		}
   1173 	printf("\tHistogram of error messages to be generated:\n");
   1174 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_NOROUTE, "\t\t%llu no route\n");
   1175 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_ADMIN, "\t\t%llu administratively prohibited\n");
   1176 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_BEYONDSCOPE, "\t\t%llu beyond scope\n");
   1177 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_ADDR, "\t\t%llu address unreachable\n");
   1178 	p_oerr(ICMP6_ERRSTAT_DST_UNREACH_NOPORT, "\t\t%llu port unreachable\n");
   1179 	p_oerr(ICMP6_ERRSTAT_PACKET_TOO_BIG, "\t\t%llu packet too big\n");
   1180 	p_oerr(ICMP6_ERRSTAT_TIME_EXCEED_TRANSIT, "\t\t%llu time exceed transit\n");
   1181 	p_oerr(ICMP6_ERRSTAT_TIME_EXCEED_REASSEMBLY, "\t\t%llu time exceed reassembly\n");
   1182 	p_oerr(ICMP6_ERRSTAT_PARAMPROB_HEADER, "\t\t%llu erroneous header field\n");
   1183 	p_oerr(ICMP6_ERRSTAT_PARAMPROB_NEXTHEADER, "\t\t%llu unrecognized next header\n");
   1184 	p_oerr(ICMP6_ERRSTAT_PARAMPROB_OPTION, "\t\t%llu unrecognized option\n");
   1185 	p_oerr(ICMP6_ERRSTAT_REDIRECT, "\t\t%llu redirect\n");
   1186 	p_oerr(ICMP6_ERRSTAT_UNKNOWN, "\t\t%llu unknown\n");
   1187 
   1188 	p(ICMP6_STAT_REFLECT, "\t%llu message response%s generated\n");
   1189 	p(ICMP6_STAT_ND_TOOMANYOPT, "\t%llu message%s with too many ND options\n");
   1190 	p(ICMP6_STAT_ND_BADOPT, "\t%llu message%s with bad ND options\n");
   1191 	p(ICMP6_STAT_BADNS, "\t%llu bad neighbor solicitation message%s\n");
   1192 	p(ICMP6_STAT_BADNA, "\t%llu bad neighbor advertisement message%s\n");
   1193 	p(ICMP6_STAT_BADRS, "\t%llu bad router solicitation message%s\n");
   1194 	p(ICMP6_STAT_BADRA, "\t%llu bad router advertisement message%s\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 	register 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 
   1453 	kread(pcbaddr, (char *)&tcp6cb, sizeof(tcp6cb));
   1454 
   1455 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
   1456 
   1457 	printf("Timers:\n");
   1458 	for (i = 0; i < TCP6T_NTIMERS; i++)
   1459 		printf("\t%s: %u", tcp6timers[i], tcp6cb.t_timer[i]);
   1460 	printf("\n\n");
   1461 
   1462 	if (tcp6cb.t_state < 0 || tcp6cb.t_state >= TCP6_NSTATES)
   1463 		printf("State: %d", tcp6cb.t_state);
   1464 	else
   1465 		printf("State: %s", tcp6states[tcp6cb.t_state]);
   1466 	printf(", flags 0x%x, in6pcb 0x%lx\n\n", tcp6cb.t_flags,
   1467 	    (u_long)tcp6cb.t_in6pcb);
   1468 
   1469 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcp6cb.t_rxtshift,
   1470 	    tcp6cb.t_rxtcur, tcp6cb.t_dupacks);
   1471 	printf("peermaxseg %u, maxseg %u, force %d\n\n", tcp6cb.t_peermaxseg,
   1472 	    tcp6cb.t_maxseg, tcp6cb.t_force);
   1473 
   1474 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
   1475 	    tcp6cb.snd_una, tcp6cb.snd_nxt, tcp6cb.snd_up);
   1476 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %llu\n\n",
   1477 	    tcp6cb.snd_wl1, tcp6cb.snd_wl2, tcp6cb.iss,
   1478 	    (unsigned long long)tcp6cb.snd_wnd);
   1479 
   1480 	printf("rcv_wnd %llu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
   1481 	    (unsigned long long)cp6cb.rcv_wnd, tcp6cb.rcv_nxt,
   1482 	    tcp6cb.rcv_up, tcp6cb.irs);
   1483 
   1484 	printf("rcv_adv %u, snd_max %u, snd_cwnd %llu, snd_ssthresh %llu\n",
   1485 	    tcp6cb.rcv_adv, tcp6cb.snd_max, (unsigned long long)tcp6cb.snd_cwnd,
   1486 	    (unsigned long long)tcp6cb.snd_ssthresh);
   1487 
   1488 	printf("idle %d, rtt %d, rtseq %u, srtt %d, rttvar %d, rttmin %d, "
   1489 	    "max_sndwnd %llu\n\n", tcp6cb.t_idle, tcp6cb.t_rtt, tcp6cb.t_rtseq,
   1490 	    tcp6cb.t_srtt, tcp6cb.t_rttvar, tcp6cb.t_rttmin,
   1491 	    (unsigned long long)tcp6cb.max_sndwnd);
   1492 
   1493 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcp6cb.t_oobflags,
   1494 	    tcp6cb.t_iobc, tcp6cb.t_softerror);
   1495 
   1496 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
   1497 	    tcp6cb.snd_scale, tcp6cb.rcv_scale, tcp6cb.request_r_scale,
   1498 	    tcp6cb.requested_s_scale);
   1499 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
   1500 	    tcp6cb.ts_recent, tcp6cb.ts_recent_age, tcp6cb.last_ack_sent);
   1501 }
   1502 #endif
   1503 
   1504 #endif /*INET6*/
   1505