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