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