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