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