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