xref: /netbsd-src/usr.bin/netstat/inet6.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: inet6.c,v 1.21 2001/06/13 02:50:25 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.21 2001/06/13 02:50:25 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 /*130*/	NULL,
433 	NULL,
434 	"SCTP",
435 	NULL,
436 	NULL,
437 /*135*/	NULL, NULL, NULL, NULL, NULL,
438 /*140*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
439 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
440 /*160*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
441 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
442 /*180*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
443 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
444 /*200*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
445 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
446 /*220*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
447 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
448 /*240*/	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
449 	NULL, NULL, NULL, NULL, NULL, NULL,
450 };
451 
452 /*
453  * Dump IP6 statistics structure.
454  */
455 void
456 ip6_stats(off, name)
457 	u_long off;
458 	char *name;
459 {
460 	struct ip6stat ip6stat;
461 	int first, i;
462 	struct protoent *ep;
463 	const char *n;
464 
465 	if (off == 0)
466 		return;
467 
468 	kread(off, (char *)&ip6stat, sizeof (ip6stat));
469 	printf("%s:\n", name);
470 
471 #define	p(f, m) if (ip6stat.f || sflag <= 1) \
472     printf(m, (unsigned long long)ip6stat.f, plural(ip6stat.f))
473 #define	p1(f, m) if (ip6stat.f || sflag <= 1) \
474     printf(m, (unsigned long long)ip6stat.f)
475 
476 	p(ip6s_total, "\t%llu total packet%s received\n");
477 	p1(ip6s_toosmall, "\t%llu with size smaller than minimum\n");
478 	p1(ip6s_tooshort, "\t%llu with data size < data length\n");
479 	p1(ip6s_badoptions, "\t%llu with bad options\n");
480 	p1(ip6s_badvers, "\t%llu with incorrect version number\n");
481 	p(ip6s_fragments, "\t%llu fragment%s received\n");
482 	p(ip6s_fragdropped,
483 	    "\t%llu fragment%s dropped (dup or out of space)\n");
484 	p(ip6s_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
485 	p(ip6s_fragoverflow, "\t%llu fragment%s that exceeded limit\n");
486 	p(ip6s_reassembled, "\t%llu packet%s reassembled ok\n");
487 	p(ip6s_delivered, "\t%llu packet%s for this host\n");
488 	p(ip6s_forward, "\t%llu packet%s forwarded\n");
489 	p(ip6s_cantforward, "\t%llu packet%s not forwardable\n");
490 	p(ip6s_redirectsent, "\t%llu redirect%s sent\n");
491 	p(ip6s_localout, "\t%llu packet%s sent from this host\n");
492 	p(ip6s_rawout, "\t%llu packet%s sent with fabricated ip header\n");
493 	p(ip6s_odropped,
494 	    "\t%llu output packet%s dropped due to no bufs, etc.\n");
495 	p(ip6s_noroute, "\t%llu output packet%s discarded due to no route\n");
496 	p(ip6s_fragmented, "\t%llu output datagram%s fragmented\n");
497 	p(ip6s_ofragments, "\t%llu fragment%s created\n");
498 	p(ip6s_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
499 	p(ip6s_badscope, "\t%llu packet%s that violated scope rules\n");
500 	p(ip6s_notmember, "\t%llu multicast packet%s which we don't join\n");
501 	for (first = 1, i = 0; i < 256; i++)
502 		if (ip6stat.ip6s_nxthist[i] != 0) {
503 			if (first) {
504 				printf("\tInput packet histogram:\n");
505 				first = 0;
506 			}
507 			n = NULL;
508 			if (ip6nh[i])
509 				n = ip6nh[i];
510 			else if ((ep = getprotobynumber(i)) != NULL)
511 				n = ep->p_name;
512 			if (n)
513 				printf("\t\t%s: %llu\n", n,
514 				    (unsigned long long)ip6stat.ip6s_nxthist[i]);
515 			else
516 				printf("\t\t#%d: %llu\n", i,
517 				    (unsigned long long)ip6stat.ip6s_nxthist[i]);
518 		}
519 	printf("\tMbuf statistics:\n");
520 	p(ip6s_m1, "\t\t%llu one mbuf%s\n");
521 	for (first = 1, i = 0; i < 32; i++) {
522 		char ifbuf[IFNAMSIZ];
523 		if (ip6stat.ip6s_m2m[i] != 0) {
524 			if (first) {
525 				printf("\t\ttwo or more mbuf:\n");
526 				first = 0;
527 			}
528 			printf("\t\t\t%s = %llu\n",
529 			       if_indextoname(i, ifbuf),
530 			       (unsigned long long)ip6stat.ip6s_m2m[i]);
531 		}
532 	}
533 	p(ip6s_mext1, "\t\t%llu one ext mbuf%s\n");
534 	p(ip6s_mext2m, "\t\t%llu two or more ext mbuf%s\n");
535 	p(ip6s_exthdrtoolong,
536 	    "\t%llu packet%s whose headers are not continuous\n");
537 	p(ip6s_nogif, "\t%llu tunneling packet%s that can't find gif\n");
538 	p(ip6s_toomanyhdr,
539 	    "\t%llu packet%s discarded due to too many headers\n");
540 
541 	/* for debugging source address selection */
542 #define PRINT_SCOPESTAT(s,i) do {\
543 		switch(i) { /* XXX hardcoding in each case */\
544 		case 1:\
545 			p(s, "\t\t%llu node-local%s\n");\
546 			break;\
547 		case 2:\
548 			p(s, "\t\t%llu link-local%s\n");\
549 			break;\
550 		case 5:\
551 			p(s, "\t\t%llu site-local%s\n");\
552 			break;\
553 		case 14:\
554 			p(s, "\t\t%llu global%s\n");\
555 			break;\
556 		default:\
557 			printf("\t\t%llu addresses scope=%x\n",\
558 			       (unsigned long long)ip6stat.s, i);\
559 		}\
560 	} while(0);
561 
562 	p(ip6s_sources_none,
563 	  "\t%llu failure%s of source address selection\n");
564 	for (first = 1, i = 0; i < 16; i++) {
565 		if (ip6stat.ip6s_sources_sameif[i]) {
566 			if (first) {
567 				printf("\tsource addresses on an outgoing I/F\n");
568 				first = 0;
569 			}
570 			PRINT_SCOPESTAT(ip6s_sources_sameif[i], i);
571 		}
572 	}
573 	for (first = 1, i = 0; i < 16; i++) {
574 		if (ip6stat.ip6s_sources_otherif[i]) {
575 			if (first) {
576 				printf("\tsource addresses on a non-outgoing I/F\n");
577 				first = 0;
578 			}
579 			PRINT_SCOPESTAT(ip6s_sources_otherif[i], i);
580 		}
581 	}
582 	for (first = 1, i = 0; i < 16; i++) {
583 		if (ip6stat.ip6s_sources_samescope[i]) {
584 			if (first) {
585 				printf("\tsource addresses of same scope\n");
586 				first = 0;
587 			}
588 			PRINT_SCOPESTAT(ip6s_sources_samescope[i], i);
589 		}
590 	}
591 	for (first = 1, i = 0; i < 16; i++) {
592 		if (ip6stat.ip6s_sources_otherscope[i]) {
593 			if (first) {
594 				printf("\tsource addresses of a different scope\n");
595 				first = 0;
596 			}
597 			PRINT_SCOPESTAT(ip6s_sources_otherscope[i], i);
598 		}
599 	}
600 	for (first = 1, i = 0; i < 16; i++) {
601 		if (ip6stat.ip6s_sources_deprecated[i]) {
602 			if (first) {
603 				printf("\tdeprecated source addresses\n");
604 				first = 0;
605 			}
606 			PRINT_SCOPESTAT(ip6s_sources_deprecated[i], i);
607 		}
608 	}
609 
610 	p1(ip6s_forward_cachehit, "\t%llu forward cache hit\n");
611 	p1(ip6s_forward_cachemiss, "\t%llu forward cache miss\n");
612 #undef p
613 #undef p1
614 }
615 
616 /*
617  * Dump IPv6 per-interface statistics based on RFC 2465.
618  */
619 void
620 ip6_ifstats(ifname)
621 	char *ifname;
622 {
623 	struct in6_ifreq ifr;
624 	int s;
625 #define	p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
626     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, \
627 	plural(ifr.ifr_ifru.ifru_stat.f))
628 #define	p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
629     printf(m, (unsigned long long)ip6stat.f)
630 
631 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
632 		perror("Warning: socket(AF_INET6)");
633 		return;
634 	}
635 
636 	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
637 	printf("ip6 on %s:\n", ifname);
638 
639 	if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
640 		perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
641 		goto end;
642 	}
643 
644 	p(ifs6_in_receive, "\t%llu total input datagram%s\n");
645 	p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
646 	p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
647 	p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
648 	p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
649 	p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
650 	p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
651 	p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
652 	p(ifs6_in_deliver,
653 	  "\t%llu datagram%s delivered to an upper layer protocol\n");
654 	p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
655 	p(ifs6_out_request,
656 	  "\t%llu datagram%s sent from an upper layer protocol\n");
657 	p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
658 	p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
659 	p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
660 	p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
661 	p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
662 	p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
663 	p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
664 	p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
665 	p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
666 
667   end:
668 	close(s);
669 
670 #undef p
671 #undef p_5
672 }
673 
674 static	char *icmp6names[] = {
675 	"#0",
676 	"unreach",
677 	"packet too big",
678 	"time exceed",
679 	"parameter problem",
680 	"#5",
681 	"#6",
682 	"#7",
683 	"#8",
684 	"#9",
685 	"#10",
686 	"#11",
687 	"#12",
688 	"#13",
689 	"#14",
690 	"#15",
691 	"#16",
692 	"#17",
693 	"#18",
694 	"#19",
695 	"#20",
696 	"#21",
697 	"#22",
698 	"#23",
699 	"#24",
700 	"#25",
701 	"#26",
702 	"#27",
703 	"#28",
704 	"#29",
705 	"#30",
706 	"#31",
707 	"#32",
708 	"#33",
709 	"#34",
710 	"#35",
711 	"#36",
712 	"#37",
713 	"#38",
714 	"#39",
715 	"#40",
716 	"#41",
717 	"#42",
718 	"#43",
719 	"#44",
720 	"#45",
721 	"#46",
722 	"#47",
723 	"#48",
724 	"#49",
725 	"#50",
726 	"#51",
727 	"#52",
728 	"#53",
729 	"#54",
730 	"#55",
731 	"#56",
732 	"#57",
733 	"#58",
734 	"#59",
735 	"#60",
736 	"#61",
737 	"#62",
738 	"#63",
739 	"#64",
740 	"#65",
741 	"#66",
742 	"#67",
743 	"#68",
744 	"#69",
745 	"#70",
746 	"#71",
747 	"#72",
748 	"#73",
749 	"#74",
750 	"#75",
751 	"#76",
752 	"#77",
753 	"#78",
754 	"#79",
755 	"#80",
756 	"#81",
757 	"#82",
758 	"#83",
759 	"#84",
760 	"#85",
761 	"#86",
762 	"#87",
763 	"#88",
764 	"#89",
765 	"#80",
766 	"#91",
767 	"#92",
768 	"#93",
769 	"#94",
770 	"#95",
771 	"#96",
772 	"#97",
773 	"#98",
774 	"#99",
775 	"#100",
776 	"#101",
777 	"#102",
778 	"#103",
779 	"#104",
780 	"#105",
781 	"#106",
782 	"#107",
783 	"#108",
784 	"#109",
785 	"#110",
786 	"#111",
787 	"#112",
788 	"#113",
789 	"#114",
790 	"#115",
791 	"#116",
792 	"#117",
793 	"#118",
794 	"#119",
795 	"#120",
796 	"#121",
797 	"#122",
798 	"#123",
799 	"#124",
800 	"#125",
801 	"#126",
802 	"#127",
803 	"echo",
804 	"echo reply",
805 	"multicast listener query",
806 	"multicast listener report",
807 	"multicast listener done",
808 	"router solicitation",
809 	"router advertisement",
810 	"neighbor solicitation",
811 	"neighbor advertisement",
812 	"redirect",
813 	"router renumbering",
814 	"node information request",
815 	"node information reply",
816 	"#141",
817 	"#142",
818 	"#143",
819 	"#144",
820 	"#145",
821 	"#146",
822 	"#147",
823 	"#148",
824 	"#149",
825 	"#150",
826 	"#151",
827 	"#152",
828 	"#153",
829 	"#154",
830 	"#155",
831 	"#156",
832 	"#157",
833 	"#158",
834 	"#159",
835 	"#160",
836 	"#161",
837 	"#162",
838 	"#163",
839 	"#164",
840 	"#165",
841 	"#166",
842 	"#167",
843 	"#168",
844 	"#169",
845 	"#170",
846 	"#171",
847 	"#172",
848 	"#173",
849 	"#174",
850 	"#175",
851 	"#176",
852 	"#177",
853 	"#178",
854 	"#179",
855 	"#180",
856 	"#181",
857 	"#182",
858 	"#183",
859 	"#184",
860 	"#185",
861 	"#186",
862 	"#187",
863 	"#188",
864 	"#189",
865 	"#180",
866 	"#191",
867 	"#192",
868 	"#193",
869 	"#194",
870 	"#195",
871 	"#196",
872 	"#197",
873 	"#198",
874 	"#199",
875 	"#200",
876 	"#201",
877 	"#202",
878 	"#203",
879 	"#204",
880 	"#205",
881 	"#206",
882 	"#207",
883 	"#208",
884 	"#209",
885 	"#210",
886 	"#211",
887 	"#212",
888 	"#213",
889 	"#214",
890 	"#215",
891 	"#216",
892 	"#217",
893 	"#218",
894 	"#219",
895 	"#220",
896 	"#221",
897 	"#222",
898 	"#223",
899 	"#224",
900 	"#225",
901 	"#226",
902 	"#227",
903 	"#228",
904 	"#229",
905 	"#230",
906 	"#231",
907 	"#232",
908 	"#233",
909 	"#234",
910 	"#235",
911 	"#236",
912 	"#237",
913 	"#238",
914 	"#239",
915 	"#240",
916 	"#241",
917 	"#242",
918 	"#243",
919 	"#244",
920 	"#245",
921 	"#246",
922 	"#247",
923 	"#248",
924 	"#249",
925 	"#250",
926 	"#251",
927 	"#252",
928 	"#253",
929 	"#254",
930 	"#255",
931 };
932 
933 /*
934  * Dump ICMPv6 statistics.
935  */
936 void
937 icmp6_stats(off, name)
938 	u_long off;
939 	char *name;
940 {
941 	struct icmp6stat icmp6stat;
942 	register int i, first;
943 
944 	if (off == 0)
945 		return;
946 	kread(off, (char *)&icmp6stat, sizeof (icmp6stat));
947 	printf("%s:\n", name);
948 
949 #define	p(f, m) if (icmp6stat.f || sflag <= 1) \
950     printf(m, (unsigned long long)icmp6stat.f, plural(icmp6stat.f))
951 #define p_5(f, m) if (icmp6stat.f || sflag <= 1) \
952     printf(m, (unsigned long long)icmp6stat.f)
953 
954 	p(icp6s_error, "\t%llu call%s to icmp6_error\n");
955 	p(icp6s_canterror,
956 	    "\t%llu error%s not generated because old message was icmp6 or so\n");
957 	p(icp6s_toofreq,
958 	    "\t%llu error%s not generated because of rate limitation\n");
959 	for (first = 1, i = 0; i < 256; i++)
960 		if (icmp6stat.icp6s_outhist[i] != 0) {
961 			if (first) {
962 				printf("\tOutput packet histogram:\n");
963 				first = 0;
964 			}
965 			printf("\t\t%s: %llu\n", icmp6names[i],
966 				(unsigned long long)icmp6stat.icp6s_outhist[i]);
967 		}
968 	p(icp6s_badcode, "\t%llu message%s with bad code fields\n");
969 	p(icp6s_tooshort, "\t%llu message%s < minimum length\n");
970 	p(icp6s_checksum, "\t%llu bad checksum%s\n");
971 	p(icp6s_badlen, "\t%llu message%s with bad length\n");
972 	for (first = 1, i = 0; i < ICMP6_MAXTYPE; i++)
973 		if (icmp6stat.icp6s_inhist[i] != 0) {
974 			if (first) {
975 				printf("\tInput packet histogram:\n");
976 				first = 0;
977 			}
978 			printf("\t\t%s: %llu\n", icmp6names[i],
979 				(unsigned long long)icmp6stat.icp6s_inhist[i]);
980 		}
981 	printf("\tHistogram of error messages to be generated:\n");
982 	p_5(icp6s_odst_unreach_noroute, "\t\t%llu no route\n");
983 	p_5(icp6s_odst_unreach_admin, "\t\t%llu administratively prohibited\n");
984 	p_5(icp6s_odst_unreach_beyondscope, "\t\t%llu beyond scope\n");
985 	p_5(icp6s_odst_unreach_addr, "\t\t%llu address unreachable\n");
986 	p_5(icp6s_odst_unreach_noport, "\t\t%llu port unreachable\n");
987 	p_5(icp6s_opacket_too_big, "\t\t%llu packet too big\n");
988 	p_5(icp6s_otime_exceed_transit, "\t\t%llu time exceed transit\n");
989 	p_5(icp6s_otime_exceed_reassembly, "\t\t%llu time exceed reassembly\n");
990 	p_5(icp6s_oparamprob_header, "\t\t%llu erroneous header field\n");
991 	p_5(icp6s_oparamprob_nextheader, "\t\t%llu unrecognized next header\n");
992 	p_5(icp6s_oparamprob_option, "\t\t%llu unrecognized option\n");
993 	p_5(icp6s_oredirect, "\t\t%llu redirect\n");
994 	p_5(icp6s_ounknown, "\t\t%llu unknown\n");
995 
996 	p(icp6s_reflect, "\t%llu message response%s generated\n");
997 	p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n");
998 	p(icp6s_nd_badopt, "\t%llu message%s with bad ND options\n");
999 	p(icp6s_badns, "\t%llu bad neighbor solicitation message%s\n");
1000 	p(icp6s_badna, "\t%llu bad neighbor advertisement message%s\n");
1001 	p(icp6s_badrs, "\t%llu bad router solicitation message%s\n");
1002 	p(icp6s_badra, "\t%llu bad router advertisement message%s\n");
1003 	p(icp6s_badredirect, "\t%llu bad redirect message%s\n");
1004 	p(icp6s_pmtuchg, "\t%llu path MTU change%s\n");
1005 #undef p
1006 #undef p_5
1007 }
1008 
1009 /*
1010  * Dump ICMPv6 per-interface statistics based on RFC 2466.
1011  */
1012 void
1013 icmp6_ifstats(ifname)
1014 	char *ifname;
1015 {
1016 	struct in6_ifreq ifr;
1017 	int s;
1018 #define	p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
1019     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, \
1020 	plural(ifr.ifr_ifru.ifru_icmp6stat.f))
1021 
1022 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1023 		perror("Warning: socket(AF_INET6)");
1024 		return;
1025 	}
1026 
1027 	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1028 	printf("icmp6 on %s:\n", ifname);
1029 
1030 	if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
1031 		perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
1032 		goto end;
1033 	}
1034 
1035 	p(ifs6_in_msg, "\t%llu total input message%s\n");
1036 	p(ifs6_in_error, "\t%llu total input error message%s\n");
1037 	p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
1038 	p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
1039 	p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
1040 	p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
1041 	p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
1042 	p(ifs6_in_echo, "\t%llu input echo request%s\n");
1043 	p(ifs6_in_echoreply, "\t%llu input echo reply%s\n");
1044 	p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
1045 	p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
1046 	p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
1047 	p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
1048 	p(ifs6_in_redirect, "\t%llu input redirect%s\n");
1049 	p(ifs6_in_mldquery, "\t%llu input MLD query%s\n");
1050 	p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
1051 	p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
1052 
1053 	p(ifs6_out_msg, "\t%llu total output message%s\n");
1054 	p(ifs6_out_error, "\t%llu total output error message%s\n");
1055 	p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
1056 	p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
1057 	p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
1058 	p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
1059 	p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
1060 	p(ifs6_out_echo, "\t%llu output echo request%s\n");
1061 	p(ifs6_out_echoreply, "\t%llu output echo reply%s\n");
1062 	p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
1063 	p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
1064 	p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
1065 	p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
1066 	p(ifs6_out_redirect, "\t%llu output redirect%s\n");
1067 	p(ifs6_out_mldquery, "\t%llu output MLD query%s\n");
1068 	p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
1069 	p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
1070 
1071   end:
1072 	close(s);
1073 #undef p
1074 }
1075 
1076 /*
1077  * Dump PIM statistics structure.
1078  */
1079 void
1080 pim6_stats(off, name)
1081 	u_long off;
1082 	char *name;
1083 {
1084 	struct pim6stat pim6stat;
1085 
1086 	if (off == 0)
1087 		return;
1088 	kread(off, (char *)&pim6stat, sizeof(pim6stat));
1089 	printf("%s:\n", name);
1090 
1091 #define	p(f, m) if (pim6stat.f || sflag <= 1) \
1092     printf(m, (unsigned long long)pim6stat.f, plural(pim6stat.f))
1093 	p(pim6s_rcv_total, "\t%llu message%s received\n");
1094 	p(pim6s_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
1095 	p(pim6s_rcv_badsum, "\t%llu message%s received with bad checksum\n");
1096 	p(pim6s_rcv_badversion, "\t%llu message%s received with bad version\n");
1097 	p(pim6s_rcv_registers, "\t%llu register%s received\n");
1098 	p(pim6s_rcv_badregisters, "\t%llu bad register%s received\n");
1099 	p(pim6s_snd_registers, "\t%llu register%s sent\n");
1100 #undef p
1101 }
1102 
1103 /*
1104  * Pretty print an Internet address (net address + port).
1105  * Take numeric_addr and numeric_port into consideration.
1106  */
1107 void
1108 inet6print(in6, port, proto)
1109 	register struct in6_addr *in6;
1110 	int port;
1111 	char *proto;
1112 {
1113 #define GETSERVBYPORT6(port, proto, ret)\
1114 do {\
1115 	if (strcmp((proto), "tcp6") == 0)\
1116 		(ret) = getservbyport((int)(port), "tcp");\
1117 	else if (strcmp((proto), "udp6") == 0)\
1118 		(ret) = getservbyport((int)(port), "udp");\
1119 	else\
1120 		(ret) = getservbyport((int)(port), (proto));\
1121 } while (0)
1122 	struct servent *sp = 0;
1123 	char line[80], *cp;
1124 	int width;
1125 
1126 	width = Aflag ? 12 : 16;
1127 	if (vflag && width < strlen(inet6name(in6)))
1128 		width = strlen(inet6name(in6));
1129 	snprintf(line, sizeof(line), "%.*s.", width, inet6name(in6));
1130 	cp = index(line, '\0');
1131 	if (!numeric_port && port)
1132 		GETSERVBYPORT6(port, proto, sp);
1133 	if (sp || port == 0)
1134 		snprintf(cp, sizeof(line) - (cp - line),
1135 		    "%.8s", sp ? sp->s_name : "*");
1136 	else
1137 		snprintf(cp, sizeof(line) - (cp - line),
1138 		    "%d", ntohs((u_short)port));
1139 	width = Aflag ? 18 : 22;
1140 	if (vflag && width < strlen(line))
1141 		width = strlen(line);
1142 	printf(" %-*.*s", width, width, line);
1143 }
1144 
1145 /*
1146  * Construct an Internet address representation.
1147  * If the numeric_addr has been supplied, give
1148  * numeric value, otherwise try for symbolic name.
1149  */
1150 
1151 char *
1152 inet6name(in6p)
1153 	struct in6_addr *in6p;
1154 {
1155 	register char *cp;
1156 	static char line[NI_MAXHOST];
1157 	struct hostent *hp;
1158 	static char domain[MAXHOSTNAMELEN + 1];
1159 	static int first = 1;
1160 	char hbuf[NI_MAXHOST];
1161 	struct sockaddr_in6 sin6;
1162 #ifdef NI_WITHSCOPEID
1163 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1164 #else
1165 	const int niflag = NI_NUMERICHOST;
1166 #endif
1167 
1168 	if (first && !numeric_addr) {
1169 		first = 0;
1170 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1171 		    (cp = index(domain, '.')))
1172 			(void) strlcpy(domain, cp + 1, sizeof(domain));
1173 		else
1174 			domain[0] = 0;
1175 	}
1176 	cp = 0;
1177 	if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1178 		hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
1179 		if (hp) {
1180 			if ((cp = index(hp->h_name, '.')) &&
1181 			    !strcmp(cp + 1, domain))
1182 				*cp = 0;
1183 			cp = hp->h_name;
1184 		}
1185 	}
1186 	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
1187 		strlcpy(line, "*", sizeof(line));
1188 	else if (cp)
1189 		strlcpy(line, cp, sizeof(line));
1190 	else {
1191 		memset(&sin6, 0, sizeof(sin6));
1192 		sin6.sin6_len = sizeof(sin6);
1193 		sin6.sin6_family = AF_INET6;
1194 		sin6.sin6_addr = *in6p;
1195 #ifdef KAME_SCOPEID
1196 		if (IN6_IS_ADDR_LINKLOCAL(in6p)) {
1197 			sin6.sin6_scope_id =
1198 				ntohs(*(u_int16_t *)&in6p->s6_addr[2]);
1199 			sin6.sin6_addr.s6_addr[2] = 0;
1200 			sin6.sin6_addr.s6_addr[3] = 0;
1201 		}
1202 #endif
1203 		if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1204 				hbuf, sizeof(hbuf), NULL, 0, niflag) != 0)
1205 			strlcpy(hbuf, "?", sizeof(hbuf));
1206 		strlcpy(line, hbuf, sizeof(line));
1207 	}
1208 	return (line);
1209 }
1210 
1211 #ifdef TCP6
1212 /*
1213  * Dump the contents of a TCP6 PCB.
1214  */
1215 void
1216 tcp6_dump(pcbaddr)
1217 	u_long pcbaddr;
1218 {
1219 	struct tcp6cb tcp6cb;
1220 	int i;
1221 
1222 	kread(pcbaddr, (char *)&tcp6cb, sizeof(tcp6cb));
1223 
1224 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
1225 
1226 	printf("Timers:\n");
1227 	for (i = 0; i < TCP6T_NTIMERS; i++)
1228 		printf("\t%s: %u", tcp6timers[i], tcp6cb.t_timer[i]);
1229 	printf("\n\n");
1230 
1231 	if (tcp6cb.t_state < 0 || tcp6cb.t_state >= TCP6_NSTATES)
1232 		printf("State: %d", tcp6cb.t_state);
1233 	else
1234 		printf("State: %s", tcp6states[tcp6cb.t_state]);
1235 	printf(", flags 0x%x, in6pcb 0x%lx\n\n", tcp6cb.t_flags,
1236 	    (u_long)tcp6cb.t_in6pcb);
1237 
1238 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcp6cb.t_rxtshift,
1239 	    tcp6cb.t_rxtcur, tcp6cb.t_dupacks);
1240 	printf("peermaxseg %u, maxseg %u, force %d\n\n", tcp6cb.t_peermaxseg,
1241 	    tcp6cb.t_maxseg, tcp6cb.t_force);
1242 
1243 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
1244 	    tcp6cb.snd_una, tcp6cb.snd_nxt, tcp6cb.snd_up);
1245 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %llu\n\n",
1246 	    tcp6cb.snd_wl1, tcp6cb.snd_wl2, tcp6cb.iss,
1247 	    (unsigned long long)tcp6cb.snd_wnd);
1248 
1249 	printf("rcv_wnd %llu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
1250 	    (unsigned long long)cp6cb.rcv_wnd, tcp6cb.rcv_nxt,
1251 	    tcp6cb.rcv_up, tcp6cb.irs);
1252 
1253 	printf("rcv_adv %u, snd_max %u, snd_cwnd %llu, snd_ssthresh %llu\n",
1254 	    tcp6cb.rcv_adv, tcp6cb.snd_max, (unsigned long long)tcp6cb.snd_cwnd,
1255 	    (unsigned long long)tcp6cb.snd_ssthresh);
1256 
1257 	printf("idle %d, rtt %d, rtseq %u, srtt %d, rttvar %d, rttmin %d, "
1258 	    "max_sndwnd %llu\n\n", tcp6cb.t_idle, tcp6cb.t_rtt, tcp6cb.t_rtseq,
1259 	    tcp6cb.t_srtt, tcp6cb.t_rttvar, tcp6cb.t_rttmin,
1260 	    (unsigned long long)tcp6cb.max_sndwnd);
1261 
1262 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcp6cb.t_oobflags,
1263 	    tcp6cb.t_iobc, tcp6cb.t_softerror);
1264 
1265 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
1266 	    tcp6cb.snd_scale, tcp6cb.rcv_scale, tcp6cb.request_r_scale,
1267 	    tcp6cb.requested_s_scale);
1268 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
1269 	    tcp6cb.ts_recent, tcp6cb.ts_recent_age, tcp6cb.last_ack_sent);
1270 }
1271 #endif
1272 
1273 #endif /*INET6*/
1274