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