xref: /freebsd/usr.bin/netstat/inet6.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
1 /*	BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp	*/
2 /*
3  * Copyright (c) 1983, 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD$
35  */
36 
37 #ifndef lint
38 /*
39 static char sccsid[] = "@(#)inet6.c	8.4 (Berkeley) 4/20/94";
40 */
41 #endif /* not lint */
42 
43 #ifdef INET6
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/ioctl.h>
48 #include <sys/mbuf.h>
49 #include <sys/protosw.h>
50 
51 #include <net/route.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <netinet/in.h>
55 #include <netinet/ip6.h>
56 #include <netinet/icmp6.h>
57 #include <netinet/in_systm.h>
58 #include <netinet6/in6_pcb.h>
59 #include <netinet6/in6_var.h>
60 #include <netinet6/ip6_var.h>
61 #include <netinet6/pim6_var.h>
62 
63 #include <arpa/inet.h>
64 #include <netdb.h>
65 
66 #include <stdio.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include "netstat.h"
70 
71 struct	socket sockb;
72 
73 char	*inet6name __P((struct in6_addr *));
74 void	inet6print __P((struct in6_addr *, int, char *, int));
75 
76 static char ntop_buf[INET6_ADDRSTRLEN];
77 
78 static	char *ip6nh[] = {
79 	"hop by hop",
80 	"ICMP",
81 	"IGMP",
82 	"#3",
83 	"IP",
84 	"#5",
85 	"TCP",
86 	"#7",
87 	"#8",
88 	"#9",
89 	"#10",
90 	"#11",
91 	"#12",
92 	"#13",
93 	"#14",
94 	"#15",
95 	"#16",
96 	"UDP",
97 	"#18",
98 	"#19",
99 	"#20",
100 	"#21",
101 	"IDP",
102 	"#23",
103 	"#24",
104 	"#25",
105 	"#26",
106 	"#27",
107 	"#28",
108 	"TP",
109 	"#30",
110 	"#31",
111 	"#32",
112 	"#33",
113 	"#34",
114 	"#35",
115 	"#36",
116 	"#37",
117 	"#38",
118 	"#39",
119 	"#40",
120 	"IP6",
121 	"#42",
122 	"routing",
123 	"fragment",
124 	"#45",
125 	"#46",
126 	"#47",
127 	"#48",
128 	"#49",
129 	"ESP",
130 	"AH",
131 	"#52",
132 	"#53",
133 	"#54",
134 	"#55",
135 	"#56",
136 	"#57",
137 	"ICMP6",
138 	"no next header",
139 	"destination option",
140 	"#61",
141 	"#62",
142 	"#63",
143 	"#64",
144 	"#65",
145 	"#66",
146 	"#67",
147 	"#68",
148 	"#69",
149 	"#70",
150 	"#71",
151 	"#72",
152 	"#73",
153 	"#74",
154 	"#75",
155 	"#76",
156 	"#77",
157 	"#78",
158 	"#79",
159 	"ISOIP",
160 	"#81",
161 	"#82",
162 	"#83",
163 	"#84",
164 	"#85",
165 	"#86",
166 	"#87",
167 	"#88",
168 	"OSPF",
169 	"#80",
170 	"#91",
171 	"#92",
172 	"#93",
173 	"#94",
174 	"#95",
175 	"#96",
176 	"Ethernet",
177 	"#98",
178 	"#99",
179 	"#100",
180 	"#101",
181 	"#102",
182 	"PIM",
183 	"#104",
184 	"#105",
185 	"#106",
186 	"#107",
187 	"#108",
188 	"#109",
189 	"#110",
190 	"#111",
191 	"#112",
192 	"#113",
193 	"#114",
194 	"#115",
195 	"#116",
196 	"#117",
197 	"#118",
198 	"#119",
199 	"#120",
200 	"#121",
201 	"#122",
202 	"#123",
203 	"#124",
204 	"#125",
205 	"#126",
206 	"#127",
207 	"#128",
208 	"#129",
209 	"#130",
210 	"#131",
211 	"#132",
212 	"#133",
213 	"#134",
214 	"#135",
215 	"#136",
216 	"#137",
217 	"#138",
218 	"#139",
219 	"#140",
220 	"#141",
221 	"#142",
222 	"#143",
223 	"#144",
224 	"#145",
225 	"#146",
226 	"#147",
227 	"#148",
228 	"#149",
229 	"#150",
230 	"#151",
231 	"#152",
232 	"#153",
233 	"#154",
234 	"#155",
235 	"#156",
236 	"#157",
237 	"#158",
238 	"#159",
239 	"#160",
240 	"#161",
241 	"#162",
242 	"#163",
243 	"#164",
244 	"#165",
245 	"#166",
246 	"#167",
247 	"#168",
248 	"#169",
249 	"#170",
250 	"#171",
251 	"#172",
252 	"#173",
253 	"#174",
254 	"#175",
255 	"#176",
256 	"#177",
257 	"#178",
258 	"#179",
259 	"#180",
260 	"#181",
261 	"#182",
262 	"#183",
263 	"#184",
264 	"#185",
265 	"#186",
266 	"#187",
267 	"#188",
268 	"#189",
269 	"#180",
270 	"#191",
271 	"#192",
272 	"#193",
273 	"#194",
274 	"#195",
275 	"#196",
276 	"#197",
277 	"#198",
278 	"#199",
279 	"#200",
280 	"#201",
281 	"#202",
282 	"#203",
283 	"#204",
284 	"#205",
285 	"#206",
286 	"#207",
287 	"#208",
288 	"#209",
289 	"#210",
290 	"#211",
291 	"#212",
292 	"#213",
293 	"#214",
294 	"#215",
295 	"#216",
296 	"#217",
297 	"#218",
298 	"#219",
299 	"#220",
300 	"#221",
301 	"#222",
302 	"#223",
303 	"#224",
304 	"#225",
305 	"#226",
306 	"#227",
307 	"#228",
308 	"#229",
309 	"#230",
310 	"#231",
311 	"#232",
312 	"#233",
313 	"#234",
314 	"#235",
315 	"#236",
316 	"#237",
317 	"#238",
318 	"#239",
319 	"#240",
320 	"#241",
321 	"#242",
322 	"#243",
323 	"#244",
324 	"#245",
325 	"#246",
326 	"#247",
327 	"#248",
328 	"#249",
329 	"#250",
330 	"#251",
331 	"#252",
332 	"#253",
333 	"#254",
334 	"#255",
335 };
336 
337 /*
338  * Dump IP6 statistics structure.
339  */
340 void
341 ip6_stats(off, name)
342 	u_long off;
343 	char *name;
344 {
345 	struct ip6stat ip6stat;
346 	int first, i;
347 
348 	if (off == 0)
349 		return;
350 
351 	if (kread(off, (char *)&ip6stat, sizeof (ip6stat)))
352 		return;
353 	printf("%s:\n", name);
354 
355 #define	p(f, m) if (ip6stat.f || sflag <= 1) \
356     printf(m, (unsigned long long)ip6stat.f, plural(ip6stat.f))
357 #define	p1a(f, m) if (ip6stat.f || sflag <= 1) \
358     printf(m, (unsigned long long)ip6stat.f)
359 
360 	p(ip6s_total, "\t%llu total packet%s received\n");
361 	p1a(ip6s_toosmall, "\t%llu with size smaller than minimum\n");
362 	p1a(ip6s_tooshort, "\t%llu with data size < data length\n");
363 	p1a(ip6s_badoptions, "\t%llu with bad options\n");
364 	p1a(ip6s_badvers, "\t%llu with incorrect version number\n");
365 	p(ip6s_fragments, "\t%llu fragment%s received\n");
366 	p(ip6s_fragdropped, "\t%llu fragment%s dropped (dup or out of space)\n");
367 	p(ip6s_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
368 	p(ip6s_fragoverflow, "\t%llu fragment%s that exceeded limit\n");
369 	p(ip6s_reassembled, "\t%llu packet%s reassembled ok\n");
370 	p(ip6s_delivered, "\t%llu packet%s for this host\n");
371 	p(ip6s_forward, "\t%llu packet%s forwarded\n");
372 	p(ip6s_cantforward, "\t%llu packet%s not forwardable\n");
373 	p(ip6s_redirectsent, "\t%llu redirect%s sent\n");
374 	p(ip6s_localout, "\t%llu packet%s sent from this host\n");
375 	p(ip6s_rawout, "\t%llu packet%s sent with fabricated ip header\n");
376 	p(ip6s_odropped, "\t%llu output packet%s dropped due to no bufs, etc.\n");
377 	p(ip6s_noroute, "\t%llu output packet%s discarded due to no route\n");
378 	p(ip6s_fragmented, "\t%llu output datagram%s fragmented\n");
379 	p(ip6s_ofragments, "\t%llu fragment%s created\n");
380 	p(ip6s_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
381 	p(ip6s_badscope, "\t%llu packet%s that violated scope rules\n");
382 	p(ip6s_notmember, "\t%llu multicast packet%s which we don't join\n");
383 	for (first = 1, i = 0; i < 256; i++)
384 		if (ip6stat.ip6s_nxthist[i] != 0) {
385 			if (first) {
386 				printf("\tInput histogram:\n");
387 				first = 0;
388 			}
389 			printf("\t\t%s: %llu\n", ip6nh[i],
390 			    (unsigned long long)ip6stat.ip6s_nxthist[i]);
391 		}
392 	printf("\tMbuf statistics:\n");
393 	printf("\t\t%llu one mbuf\n", (unsigned long long)ip6stat.ip6s_m1);
394 	for (first = 1, i = 0; i < 32; i++) {
395 		char ifbuf[IFNAMSIZ];
396 		if (ip6stat.ip6s_m2m[i] != 0) {
397 			if (first) {
398 				printf("\t\ttwo or more mbuf:\n");
399 				first = 0;
400 			}
401 			printf("\t\t\t%s= %llu\n",
402 			    if_indextoname(i, ifbuf),
403 			    (unsigned long long)ip6stat.ip6s_m2m[i]);
404 		}
405 	}
406 	printf("\t\t%llu one ext mbuf\n",
407 	    (unsigned long long)ip6stat.ip6s_mext1);
408 	printf("\t\t%llu two or more ext mbuf\n",
409 	    (unsigned long long)ip6stat.ip6s_mext2m);
410 	p(ip6s_exthdrtoolong,
411 	    "\t%llu packet%s whose headers are not continuous\n");
412 	p(ip6s_nogif, "\t%llu tunneling packet%s that can't find gif\n");
413 	p(ip6s_toomanyhdr,
414 	    "\t%llu packet%s discarded due to too may headers\n");
415 
416 	/* for debugging source address selection */
417 #define PRINT_SCOPESTAT(s,i) do {\
418 		switch(i) { /* XXX hardcoding in each case */\
419 		case 1:\
420 			p(s, "\t\t%llu node-local%s\n");\
421 			break;\
422 		case 2:\
423 			p(s,"\t\t%llu link-local%s\n");\
424 			break;\
425 		case 5:\
426 			p(s,"\t\t%llu site-local%s\n");\
427 			break;\
428 		case 14:\
429 			p(s,"\t\t%llu global%s\n");\
430 			break;\
431 		default:\
432 			printf("\t\t%llu addresses scope=%x\n",\
433 			    (unsigned long long)ip6stat.s, i);\
434 		}\
435 	} while (0);
436 
437 	p(ip6s_sources_none,
438 	  "\t%llu failure%s of source address selection\n");
439 	for (first = 1, i = 0; i < 16; i++) {
440 		if (ip6stat.ip6s_sources_sameif[i]) {
441 			if (first) {
442 				printf("\tsource addresses on an outgoing I/F\n");
443 				first = 0;
444 			}
445 			PRINT_SCOPESTAT(ip6s_sources_sameif[i], i);
446 		}
447 	}
448 	for (first = 1, i = 0; i < 16; i++) {
449 		if (ip6stat.ip6s_sources_otherif[i]) {
450 			if (first) {
451 				printf("\tsource addresses on a non-outgoing I/F\n");
452 				first = 0;
453 			}
454 			PRINT_SCOPESTAT(ip6s_sources_otherif[i], i);
455 		}
456 	}
457 	for (first = 1, i = 0; i < 16; i++) {
458 		if (ip6stat.ip6s_sources_samescope[i]) {
459 			if (first) {
460 				printf("\tsource addresses of same scope\n");
461 				first = 0;
462 			}
463 			PRINT_SCOPESTAT(ip6s_sources_samescope[i], i);
464 		}
465 	}
466 	for (first = 1, i = 0; i < 16; i++) {
467 		if (ip6stat.ip6s_sources_otherscope[i]) {
468 			if (first) {
469 				printf("\tsource addresses of a different scope\n");
470 				first = 0;
471 			}
472 			PRINT_SCOPESTAT(ip6s_sources_otherscope[i], i);
473 		}
474 	}
475 	for (first = 1, i = 0; i < 16; i++) {
476 		if (ip6stat.ip6s_sources_deprecated[i]) {
477 			if (first) {
478 				printf("\tdeprecated source addresses\n");
479 				first = 0;
480 			}
481 			PRINT_SCOPESTAT(ip6s_sources_deprecated[i], i);
482 		}
483 	}
484 
485 	p1a(ip6s_forward_cachehit, "\t%llu forward cache hit\n");
486 	p1a(ip6s_forward_cachemiss, "\t%llu forward cache miss\n");
487 #undef p
488 #undef p1a
489 }
490 
491 /*
492  * Dump IPv6 per-interface statistics based on RFC 2465.
493  */
494 void
495 ip6_ifstats(ifname)
496 	char *ifname;
497 {
498 	struct in6_ifreq ifr;
499 	int s;
500 #define	p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
501     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, plural(ifr.ifr_ifru.ifru_stat.f))
502 #define	p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
503     printf(m, (unsigned long long)ip6stat.f)
504 
505 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
506 		perror("Warning: socket(AF_INET6)");
507 		return;
508 	}
509 
510 	strcpy(ifr.ifr_name, ifname);
511 	printf("ip6 on %s:\n", ifr.ifr_name);
512 
513 	if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
514 		perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
515 		goto end;
516 	}
517 
518 	p(ifs6_in_receive, "\t%llu total input datagram%s\n");
519 	p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
520 	p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
521 	p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
522 	p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
523 	p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
524 	p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
525 	p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
526 	p(ifs6_in_deliver,
527 	  "\t%llu datagram%s delivered to an upper layer protocol\n");
528 	p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
529 	p(ifs6_out_request,
530 	  "\t%llu datagram%s sent from an upper layer protocol\n");
531 	p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
532 	p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
533 	p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
534 	p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
535 	p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
536 	p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
537 	p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
538 	p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
539 	p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
540 
541   end:
542 	close(s);
543 
544 #undef p
545 #undef p_5
546 }
547 
548 static	char *icmp6names[] = {
549 	"#0",
550 	"unreach",
551 	"packet too big",
552 	"time exceed",
553 	"parameter problem",
554 	"#5",
555 	"#6",
556 	"#7",
557 	"#8",
558 	"#9",
559 	"#10",
560 	"#11",
561 	"#12",
562 	"#13",
563 	"#14",
564 	"#15",
565 	"#16",
566 	"#17",
567 	"#18",
568 	"#19",
569 	"#20",
570 	"#21",
571 	"#22",
572 	"#23",
573 	"#24",
574 	"#25",
575 	"#26",
576 	"#27",
577 	"#28",
578 	"#29",
579 	"#30",
580 	"#31",
581 	"#32",
582 	"#33",
583 	"#34",
584 	"#35",
585 	"#36",
586 	"#37",
587 	"#38",
588 	"#39",
589 	"#40",
590 	"#41",
591 	"#42",
592 	"#43",
593 	"#44",
594 	"#45",
595 	"#46",
596 	"#47",
597 	"#48",
598 	"#49",
599 	"#50",
600 	"#51",
601 	"#52",
602 	"#53",
603 	"#54",
604 	"#55",
605 	"#56",
606 	"#57",
607 	"#58",
608 	"#59",
609 	"#60",
610 	"#61",
611 	"#62",
612 	"#63",
613 	"#64",
614 	"#65",
615 	"#66",
616 	"#67",
617 	"#68",
618 	"#69",
619 	"#70",
620 	"#71",
621 	"#72",
622 	"#73",
623 	"#74",
624 	"#75",
625 	"#76",
626 	"#77",
627 	"#78",
628 	"#79",
629 	"#80",
630 	"#81",
631 	"#82",
632 	"#83",
633 	"#84",
634 	"#85",
635 	"#86",
636 	"#87",
637 	"#88",
638 	"#89",
639 	"#80",
640 	"#91",
641 	"#92",
642 	"#93",
643 	"#94",
644 	"#95",
645 	"#96",
646 	"#97",
647 	"#98",
648 	"#99",
649 	"#100",
650 	"#101",
651 	"#102",
652 	"#103",
653 	"#104",
654 	"#105",
655 	"#106",
656 	"#107",
657 	"#108",
658 	"#109",
659 	"#110",
660 	"#111",
661 	"#112",
662 	"#113",
663 	"#114",
664 	"#115",
665 	"#116",
666 	"#117",
667 	"#118",
668 	"#119",
669 	"#120",
670 	"#121",
671 	"#122",
672 	"#123",
673 	"#124",
674 	"#125",
675 	"#126",
676 	"#127",
677 	"echo",
678 	"echo reply",
679 	"multicast listener query",
680 	"multicast listener report",
681 	"multicast listener done",
682 	"router solicitation",
683 	"router advertisment",
684 	"neighbor solicitation",
685 	"neighbor advertisment",
686 	"redirect",
687 	"router renumbering",
688 	"node information request",
689 	"node information reply",
690 	"#141",
691 	"#142",
692 	"#143",
693 	"#144",
694 	"#145",
695 	"#146",
696 	"#147",
697 	"#148",
698 	"#149",
699 	"#150",
700 	"#151",
701 	"#152",
702 	"#153",
703 	"#154",
704 	"#155",
705 	"#156",
706 	"#157",
707 	"#158",
708 	"#159",
709 	"#160",
710 	"#161",
711 	"#162",
712 	"#163",
713 	"#164",
714 	"#165",
715 	"#166",
716 	"#167",
717 	"#168",
718 	"#169",
719 	"#170",
720 	"#171",
721 	"#172",
722 	"#173",
723 	"#174",
724 	"#175",
725 	"#176",
726 	"#177",
727 	"#178",
728 	"#179",
729 	"#180",
730 	"#181",
731 	"#182",
732 	"#183",
733 	"#184",
734 	"#185",
735 	"#186",
736 	"#187",
737 	"#188",
738 	"#189",
739 	"#180",
740 	"#191",
741 	"#192",
742 	"#193",
743 	"#194",
744 	"#195",
745 	"#196",
746 	"#197",
747 	"#198",
748 	"#199",
749 	"#200",
750 	"#201",
751 	"#202",
752 	"#203",
753 	"#204",
754 	"#205",
755 	"#206",
756 	"#207",
757 	"#208",
758 	"#209",
759 	"#210",
760 	"#211",
761 	"#212",
762 	"#213",
763 	"#214",
764 	"#215",
765 	"#216",
766 	"#217",
767 	"#218",
768 	"#219",
769 	"#220",
770 	"#221",
771 	"#222",
772 	"#223",
773 	"#224",
774 	"#225",
775 	"#226",
776 	"#227",
777 	"#228",
778 	"#229",
779 	"#230",
780 	"#231",
781 	"#232",
782 	"#233",
783 	"#234",
784 	"#235",
785 	"#236",
786 	"#237",
787 	"#238",
788 	"#239",
789 	"#240",
790 	"#241",
791 	"#242",
792 	"#243",
793 	"#244",
794 	"#245",
795 	"#246",
796 	"#247",
797 	"#248",
798 	"#249",
799 	"#250",
800 	"#251",
801 	"#252",
802 	"#253",
803 	"#254",
804 	"#255",
805 };
806 
807 /*
808  * Dump ICMP6 statistics.
809  */
810 void
811 icmp6_stats(off, name)
812 	u_long off;
813 	char *name;
814 {
815 	struct icmp6stat icmp6stat;
816 	register int i, first;
817 
818 	if (off == 0)
819 		return;
820 	kread(off, (char *)&icmp6stat, sizeof (icmp6stat));
821 	printf("%s:\n", name);
822 
823 #define	p(f, m) if (icmp6stat.f || sflag <= 1) \
824     printf(m, (unsigned long long)icmp6stat.f, plural(icmp6stat.f))
825 #define p_5(f, m) printf(m, (unsigned long long)icmp6stat.f)
826 
827 	p(icp6s_error, "\t%llu call%s to icmp_error\n");
828 	p(icp6s_canterror,
829 	    "\t%llu error%s not generated because old message was icmp error or so\n");
830 	p(icp6s_toofreq,
831 	  "\t%llu error%s not generated because rate limitation\n");
832 	for (first = 1, i = 0; i < 256; i++)
833 		if (icmp6stat.icp6s_outhist[i] != 0) {
834 			if (first) {
835 				printf("\tOutput histogram:\n");
836 				first = 0;
837 			}
838 			printf("\t\t%s: %llu\n", icmp6names[i],
839 			    (unsigned long long)icmp6stat.icp6s_outhist[i]);
840 		}
841 	p(icp6s_badcode, "\t%llu message%s with bad code fields\n");
842 	p(icp6s_tooshort, "\t%llu message%s < minimum length\n");
843 	p(icp6s_checksum, "\t%llu bad checksum%s\n");
844 	p(icp6s_badlen, "\t%llu message%s with bad length\n");
845 	for (first = 1, i = 0; i < ICMP6_MAXTYPE; i++)
846 		if (icmp6stat.icp6s_inhist[i] != 0) {
847 			if (first) {
848 				printf("\tInput histogram:\n");
849 				first = 0;
850 			}
851 			printf("\t\t%s: %llu\n", icmp6names[i],
852 			    (unsigned long long)icmp6stat.icp6s_inhist[i]);
853 		}
854 	printf("\tHistgram of error messages to be generated:\n");
855 	p_5(icp6s_odst_unreach_noroute, "\t\t%llu no route\n");
856 	p_5(icp6s_odst_unreach_admin, "\t\t%llu administratively prohibited\n");
857 	p_5(icp6s_odst_unreach_beyondscope, "\t\t%llu beyond scope\n");
858 	p_5(icp6s_odst_unreach_addr, "\t\t%llu address unreachable\n");
859 	p_5(icp6s_odst_unreach_noport, "\t\t%llu port unreachable\n");
860 	p_5(icp6s_opacket_too_big, "\t\t%llu packet too big\n");
861 	p_5(icp6s_otime_exceed_transit, "\t\t%llu time exceed transit\n");
862 	p_5(icp6s_otime_exceed_reassembly, "\t\t%llu time exceed reassembly\n");
863 	p_5(icp6s_oparamprob_header, "\t\t%llu erroneous header field\n");
864 	p_5(icp6s_oparamprob_nextheader, "\t\t%llu unrecognized next header\n");
865 	p_5(icp6s_oparamprob_option, "\t\t%llu unrecognized option\n");
866 	p_5(icp6s_oredirect, "\t\t%llu redirect\n");
867 	p_5(icp6s_ounknown, "\t\t%llu unknown\n");
868 
869 	p(icp6s_reflect, "\t%llu message response%s generated\n");
870 	p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n");
871 #undef p
872 #undef p_5
873 }
874 
875 /*
876  * Dump ICMPv6 per-interface statistics based on RFC 2466.
877  */
878 void
879 icmp6_ifstats(ifname)
880 	char *ifname;
881 {
882 	struct in6_ifreq ifr;
883 	int s;
884 #define	p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
885     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, plural(ifr.ifr_ifru.ifru_icmp6stat.f))
886 
887 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
888 		perror("Warning: socket(AF_INET6)");
889 		return;
890 	}
891 
892 	strcpy(ifr.ifr_name, ifname);
893 	printf("icmp6 on %s:\n", ifr.ifr_name);
894 
895 	if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
896 		perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
897 		goto end;
898 	}
899 
900 	p(ifs6_in_msg, "\t%llu total input message%s\n");
901 	p(ifs6_in_error, "\t%llu total input error message%s\n");
902 	p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
903 	p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
904 	p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
905 	p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
906 	p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
907 	p(ifs6_in_echo, "\t%llu input echo request%s\n");
908 	p(ifs6_in_echoreply, "\t%llu input echo reply%s\n");
909 	p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
910 	p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
911 	p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
912 	p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
913 	p(ifs6_in_redirect, "\t%llu input redirect%s\n");
914 	p(ifs6_in_mldquery, "\t%llu input MLD query%s\n");
915 	p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
916 	p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
917 
918 	p(ifs6_out_msg, "\t%llu total output message%s\n");
919 	p(ifs6_out_error, "\t%llu total output error message%s\n");
920 	p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
921 	p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
922 	p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
923 	p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
924 	p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
925 	p(ifs6_out_echo, "\t%llu output echo request%s\n");
926 	p(ifs6_out_echoreply, "\t%llu output echo reply%s\n");
927 	p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
928 	p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
929 	p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
930 	p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
931 	p(ifs6_out_redirect, "\t%llu output redirect%s\n");
932 	p(ifs6_out_mldquery, "\t%llu output MLD query%s\n");
933 	p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
934 	p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
935 
936   end:
937 	close(s);
938 #undef p
939 }
940 
941 /*
942  * Dump PIM statistics structure.
943  */
944 void
945 pim6_stats(off, name)
946 	u_long off;
947 	char *name;
948 {
949 	struct pim6stat pim6stat;
950 
951 	if (off == 0)
952 		return;
953 	kread(off, (char *)&pim6stat, sizeof(pim6stat));
954 	printf("%s:\n", name);
955 
956 #define	p(f, m) if (pim6stat.f || sflag <= 1) \
957     printf(m, (unsigned long long)pim6stat.f, plural(pim6stat.f))
958 	p(pim6s_rcv_total, "\t%llu message%s received\n");
959 	p(pim6s_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
960 	p(pim6s_rcv_badsum, "\t%llu message%s received with bad checksum\n");
961 	p(pim6s_rcv_badversion, "\t%llu message%s received with bad version\n");
962 	p(pim6s_rcv_registers, "\t%llu register%s received\n");
963 	p(pim6s_rcv_badregisters, "\t%llu bad register%s received\n");
964 	p(pim6s_snd_registers, "\t%llu register%s sent\n");
965 #undef p
966 }
967 
968 /*
969  * Pretty print an Internet address (net address + port).
970  * If the nflag was specified, use numbers instead of names.
971  */
972 #define GETSERVBYPORT6(port, proto, ret)\
973 {\
974 	if (strcmp((proto), "tcp6") == 0)\
975 		(ret) = getservbyport((int)(port), "tcp");\
976 	else if (strcmp((proto), "udp6") == 0)\
977 		(ret) = getservbyport((int)(port), "udp");\
978 	else\
979 		(ret) = getservbyport((int)(port), (proto));\
980 };
981 
982 void
983 inet6print(in6, port, proto, numeric)
984 	register struct in6_addr *in6;
985 	int port;
986 	char *proto;
987 	int numeric;
988 {
989 	struct servent *sp = 0;
990 	char line[80], *cp;
991 	int width;
992 
993 	sprintf(line, "%.*s.", lflag ? 39 :
994 		(Aflag && !numeric) ? 12 : 16, inet6name(in6));
995 	cp = index(line, '\0');
996 	if (!numeric && port)
997 		GETSERVBYPORT6(port, proto, sp);
998 	if (sp || port == 0)
999 		sprintf(cp, "%.8s", sp ? sp->s_name : "*");
1000 	else
1001 		sprintf(cp, "%d", ntohs((u_short)port));
1002 	width = lflag ? 45 : Aflag ? 18 : 22;
1003 	printf("%-*.*s ", width, width, line);
1004 }
1005 
1006 /*
1007  * Construct an Internet address representation.
1008  * If the nflag has been supplied, give
1009  * numeric value, otherwise try for symbolic name.
1010  */
1011 
1012 char *
1013 inet6name(in6p)
1014 	struct in6_addr *in6p;
1015 {
1016 	register char *cp;
1017 	static char line[50];
1018 	struct hostent *hp;
1019 	static char domain[MAXHOSTNAMELEN];
1020 	static int first = 1;
1021 
1022 	if (first && !nflag) {
1023 		first = 0;
1024 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1025 		    (cp = index(domain, '.')))
1026 			(void) strcpy(domain, cp + 1);
1027 		else
1028 			domain[0] = 0;
1029 	}
1030 	cp = 0;
1031 	if (!nflag && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1032 		hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
1033 		if (hp) {
1034 			if ((cp = index(hp->h_name, '.')) &&
1035 			    !strcmp(cp + 1, domain))
1036 				*cp = 0;
1037 			cp = hp->h_name;
1038 		}
1039 	}
1040 	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
1041 		strcpy(line, "*");
1042 	else if (cp)
1043 		strcpy(line, cp);
1044 	else
1045 		sprintf(line, "%s",
1046 			inet_ntop(AF_INET6, (void *)in6p, ntop_buf,
1047 				sizeof(ntop_buf)));
1048 	return (line);
1049 }
1050 #endif /*INET6*/
1051