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