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