xref: /freebsd/usr.bin/netstat/inet6.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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 #include <sys/sysctl.h>
51 
52 #include <net/route.h>
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <netinet/in.h>
56 #include <netinet/ip6.h>
57 #include <netinet/icmp6.h>
58 #include <netinet/in_systm.h>
59 #include <netinet6/in6_pcb.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/pim6_var.h>
63 #include <netinet6/raw_ip6.h>
64 
65 #include <arpa/inet.h>
66 #include <netdb.h>
67 
68 #include <stdio.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include "netstat.h"
72 
73 struct	socket sockb;
74 
75 char	*inet6name (struct in6_addr *);
76 
77 static char ntop_buf[INET6_ADDRSTRLEN];
78 
79 static	const char *ip6nh[] = {
80 	"hop by hop",
81 	"ICMP",
82 	"IGMP",
83 	"#3",
84 	"IP",
85 	"#5",
86 	"TCP",
87 	"#7",
88 	"#8",
89 	"#9",
90 	"#10",
91 	"#11",
92 	"#12",
93 	"#13",
94 	"#14",
95 	"#15",
96 	"#16",
97 	"UDP",
98 	"#18",
99 	"#19",
100 	"#20",
101 	"#21",
102 	"IDP",
103 	"#23",
104 	"#24",
105 	"#25",
106 	"#26",
107 	"#27",
108 	"#28",
109 	"TP",
110 	"#30",
111 	"#31",
112 	"#32",
113 	"#33",
114 	"#34",
115 	"#35",
116 	"#36",
117 	"#37",
118 	"#38",
119 	"#39",
120 	"#40",
121 	"IP6",
122 	"#42",
123 	"routing",
124 	"fragment",
125 	"#45",
126 	"#46",
127 	"#47",
128 	"#48",
129 	"#49",
130 	"ESP",
131 	"AH",
132 	"#52",
133 	"#53",
134 	"#54",
135 	"#55",
136 	"#56",
137 	"#57",
138 	"ICMP6",
139 	"no next header",
140 	"destination option",
141 	"#61",
142 	"#62",
143 	"#63",
144 	"#64",
145 	"#65",
146 	"#66",
147 	"#67",
148 	"#68",
149 	"#69",
150 	"#70",
151 	"#71",
152 	"#72",
153 	"#73",
154 	"#74",
155 	"#75",
156 	"#76",
157 	"#77",
158 	"#78",
159 	"#79",
160 	"ISOIP",
161 	"#81",
162 	"#82",
163 	"#83",
164 	"#84",
165 	"#85",
166 	"#86",
167 	"#87",
168 	"#88",
169 	"OSPF",
170 	"#80",
171 	"#91",
172 	"#92",
173 	"#93",
174 	"#94",
175 	"#95",
176 	"#96",
177 	"Ethernet",
178 	"#98",
179 	"#99",
180 	"#100",
181 	"#101",
182 	"#102",
183 	"PIM",
184 	"#104",
185 	"#105",
186 	"#106",
187 	"#107",
188 	"#108",
189 	"#109",
190 	"#110",
191 	"#111",
192 	"#112",
193 	"#113",
194 	"#114",
195 	"#115",
196 	"#116",
197 	"#117",
198 	"#118",
199 	"#119",
200 	"#120",
201 	"#121",
202 	"#122",
203 	"#123",
204 	"#124",
205 	"#125",
206 	"#126",
207 	"#127",
208 	"#128",
209 	"#129",
210 	"#130",
211 	"#131",
212 	"#132",
213 	"#133",
214 	"#134",
215 	"#135",
216 	"#136",
217 	"#137",
218 	"#138",
219 	"#139",
220 	"#140",
221 	"#141",
222 	"#142",
223 	"#143",
224 	"#144",
225 	"#145",
226 	"#146",
227 	"#147",
228 	"#148",
229 	"#149",
230 	"#150",
231 	"#151",
232 	"#152",
233 	"#153",
234 	"#154",
235 	"#155",
236 	"#156",
237 	"#157",
238 	"#158",
239 	"#159",
240 	"#160",
241 	"#161",
242 	"#162",
243 	"#163",
244 	"#164",
245 	"#165",
246 	"#166",
247 	"#167",
248 	"#168",
249 	"#169",
250 	"#170",
251 	"#171",
252 	"#172",
253 	"#173",
254 	"#174",
255 	"#175",
256 	"#176",
257 	"#177",
258 	"#178",
259 	"#179",
260 	"#180",
261 	"#181",
262 	"#182",
263 	"#183",
264 	"#184",
265 	"#185",
266 	"#186",
267 	"#187",
268 	"#188",
269 	"#189",
270 	"#180",
271 	"#191",
272 	"#192",
273 	"#193",
274 	"#194",
275 	"#195",
276 	"#196",
277 	"#197",
278 	"#198",
279 	"#199",
280 	"#200",
281 	"#201",
282 	"#202",
283 	"#203",
284 	"#204",
285 	"#205",
286 	"#206",
287 	"#207",
288 	"#208",
289 	"#209",
290 	"#210",
291 	"#211",
292 	"#212",
293 	"#213",
294 	"#214",
295 	"#215",
296 	"#216",
297 	"#217",
298 	"#218",
299 	"#219",
300 	"#220",
301 	"#221",
302 	"#222",
303 	"#223",
304 	"#224",
305 	"#225",
306 	"#226",
307 	"#227",
308 	"#228",
309 	"#229",
310 	"#230",
311 	"#231",
312 	"#232",
313 	"#233",
314 	"#234",
315 	"#235",
316 	"#236",
317 	"#237",
318 	"#238",
319 	"#239",
320 	"#240",
321 	"#241",
322 	"#242",
323 	"#243",
324 	"#244",
325 	"#245",
326 	"#246",
327 	"#247",
328 	"#248",
329 	"#249",
330 	"#250",
331 	"#251",
332 	"#252",
333 	"#253",
334 	"#254",
335 	"#255",
336 };
337 
338 /*
339  * Dump IP6 statistics structure.
340  */
341 void
342 ip6_stats(u_long off __unused, const char *name, int af1 __unused)
343 {
344 	struct ip6stat ip6stat;
345 	int first, i;
346 	int mib[4];
347 	size_t len;
348 
349 	mib[0] = CTL_NET;
350 	mib[1] = PF_INET6;
351 	mib[2] = IPPROTO_IPV6;
352 	mib[3] = IPV6CTL_STATS;
353 
354 	len = sizeof ip6stat;
355 	memset(&ip6stat, 0, len);
356 	if (sysctl(mib, 4, &ip6stat, &len, (void *)0, 0) < 0)
357 		return;
358 	printf("%s:\n", name);
359 
360 #define	p(f, m) if (ip6stat.f || sflag <= 1) \
361     printf(m, (unsigned long long)ip6stat.f, plural(ip6stat.f))
362 #define	p1a(f, m) if (ip6stat.f || sflag <= 1) \
363     printf(m, (unsigned long long)ip6stat.f)
364 
365 	p(ip6s_total, "\t%llu total packet%s received\n");
366 	p1a(ip6s_toosmall, "\t%llu with size smaller than minimum\n");
367 	p1a(ip6s_tooshort, "\t%llu with data size < data length\n");
368 	p1a(ip6s_badoptions, "\t%llu with bad options\n");
369 	p1a(ip6s_badvers, "\t%llu with incorrect version number\n");
370 	p(ip6s_fragments, "\t%llu fragment%s received\n");
371 	p(ip6s_fragdropped, "\t%llu fragment%s dropped (dup or out of space)\n");
372 	p(ip6s_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
373 	p(ip6s_fragoverflow, "\t%llu fragment%s that exceeded limit\n");
374 	p(ip6s_reassembled, "\t%llu packet%s reassembled ok\n");
375 	p(ip6s_delivered, "\t%llu packet%s for this host\n");
376 	p(ip6s_forward, "\t%llu packet%s forwarded\n");
377 	p(ip6s_cantforward, "\t%llu packet%s not forwardable\n");
378 	p(ip6s_redirectsent, "\t%llu redirect%s sent\n");
379 	p(ip6s_localout, "\t%llu packet%s sent from this host\n");
380 	p(ip6s_rawout, "\t%llu packet%s sent with fabricated ip header\n");
381 	p(ip6s_odropped, "\t%llu output packet%s dropped due to no bufs, etc.\n");
382 	p(ip6s_noroute, "\t%llu output packet%s discarded due to no route\n");
383 	p(ip6s_fragmented, "\t%llu output datagram%s fragmented\n");
384 	p(ip6s_ofragments, "\t%llu fragment%s created\n");
385 	p(ip6s_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
386 	p(ip6s_badscope, "\t%llu packet%s that violated scope rules\n");
387 	p(ip6s_notmember, "\t%llu multicast packet%s which we don't join\n");
388 	for (first = 1, i = 0; i < 256; i++)
389 		if (ip6stat.ip6s_nxthist[i] != 0) {
390 			if (first) {
391 				printf("\tInput histogram:\n");
392 				first = 0;
393 			}
394 			printf("\t\t%s: %llu\n", ip6nh[i],
395 			    (unsigned long long)ip6stat.ip6s_nxthist[i]);
396 		}
397 	printf("\tMbuf statistics:\n");
398 	printf("\t\t%llu one mbuf\n", (unsigned long long)ip6stat.ip6s_m1);
399 	for (first = 1, i = 0; i < 32; i++) {
400 		char ifbuf[IFNAMSIZ];
401 		if (ip6stat.ip6s_m2m[i] != 0) {
402 			if (first) {
403 				printf("\t\ttwo or more mbuf:\n");
404 				first = 0;
405 			}
406 			printf("\t\t\t%s= %llu\n",
407 			    if_indextoname(i, ifbuf),
408 			    (unsigned long long)ip6stat.ip6s_m2m[i]);
409 		}
410 	}
411 	printf("\t\t%llu one ext mbuf\n",
412 	    (unsigned long long)ip6stat.ip6s_mext1);
413 	printf("\t\t%llu two or more ext mbuf\n",
414 	    (unsigned long long)ip6stat.ip6s_mext2m);
415 	p(ip6s_exthdrtoolong,
416 	    "\t%llu packet%s whose headers are not continuous\n");
417 	p(ip6s_nogif, "\t%llu tunneling packet%s that can't find gif\n");
418 	p(ip6s_toomanyhdr,
419 	    "\t%llu packet%s discarded because of too many headers\n");
420 
421 	/* for debugging source address selection */
422 #define PRINT_SCOPESTAT(s,i) do {\
423 		switch(i) { /* XXX hardcoding in each case */\
424 		case 1:\
425 			p(s, "\t\t%llu node-local%s\n");\
426 			break;\
427 		case 2:\
428 			p(s,"\t\t%llu link-local%s\n");\
429 			break;\
430 		case 5:\
431 			p(s,"\t\t%llu site-local%s\n");\
432 			break;\
433 		case 14:\
434 			p(s,"\t\t%llu global%s\n");\
435 			break;\
436 		default:\
437 			printf("\t\t%llu addresses scope=%x\n",\
438 			    (unsigned long long)ip6stat.s, i);\
439 		}\
440 	} while (0);
441 
442 	p(ip6s_sources_none,
443 	  "\t%llu failure%s of source address selection\n");
444 	for (first = 1, i = 0; i < 16; i++) {
445 		if (ip6stat.ip6s_sources_sameif[i]) {
446 			if (first) {
447 				printf("\tsource addresses on an outgoing I/F\n");
448 				first = 0;
449 			}
450 			PRINT_SCOPESTAT(ip6s_sources_sameif[i], i);
451 		}
452 	}
453 	for (first = 1, i = 0; i < 16; i++) {
454 		if (ip6stat.ip6s_sources_otherif[i]) {
455 			if (first) {
456 				printf("\tsource addresses on a non-outgoing I/F\n");
457 				first = 0;
458 			}
459 			PRINT_SCOPESTAT(ip6s_sources_otherif[i], i);
460 		}
461 	}
462 	for (first = 1, i = 0; i < 16; i++) {
463 		if (ip6stat.ip6s_sources_samescope[i]) {
464 			if (first) {
465 				printf("\tsource addresses of same scope\n");
466 				first = 0;
467 			}
468 			PRINT_SCOPESTAT(ip6s_sources_samescope[i], i);
469 		}
470 	}
471 	for (first = 1, i = 0; i < 16; i++) {
472 		if (ip6stat.ip6s_sources_otherscope[i]) {
473 			if (first) {
474 				printf("\tsource addresses of a different scope\n");
475 				first = 0;
476 			}
477 			PRINT_SCOPESTAT(ip6s_sources_otherscope[i], i);
478 		}
479 	}
480 	for (first = 1, i = 0; i < 16; i++) {
481 		if (ip6stat.ip6s_sources_deprecated[i]) {
482 			if (first) {
483 				printf("\tdeprecated source addresses\n");
484 				first = 0;
485 			}
486 			PRINT_SCOPESTAT(ip6s_sources_deprecated[i], i);
487 		}
488 	}
489 
490 	p1a(ip6s_forward_cachehit, "\t%llu forward cache hit\n");
491 	p1a(ip6s_forward_cachemiss, "\t%llu forward cache miss\n");
492 #undef p
493 #undef p1a
494 }
495 
496 /*
497  * Dump IPv6 per-interface statistics based on RFC 2465.
498  */
499 void
500 ip6_ifstats(char *ifname)
501 {
502 	struct in6_ifreq ifr;
503 	int s;
504 #define	p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
505     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, 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 	strcpy(ifr.ifr_name, ifname);
515 	printf("ip6 on %s:\n", ifr.ifr_name);
516 
517 	if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&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_protounknown, "\t%llu datagram%s with unknown proto received\n");
528 	p(ifs6_in_truncated, "\t%llu truncated datagram%s 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 reassembly\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	const 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 	"inverse neighbor solicitation",
695 	"inverse neighbor advertisement",
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 ICMP6 statistics.
813  */
814 void
815 icmp6_stats(u_long off __unused, const char *name, int af1 __unused)
816 {
817 	struct icmp6stat icmp6stat;
818 	int i, first;
819 	int mib[4];
820 	size_t len;
821 
822 	mib[0] = CTL_NET;
823 	mib[1] = PF_INET6;
824 	mib[2] = IPPROTO_ICMPV6;
825 	mib[3] = ICMPV6CTL_STATS;
826 
827 	len = sizeof icmp6stat;
828 	memset(&icmp6stat, 0, len);
829 	if (sysctl(mib, 4, &icmp6stat, &len, (void *)0, 0) < 0)
830 		return;
831 	printf("%s:\n", name);
832 
833 #define	p(f, m) if (icmp6stat.f || sflag <= 1) \
834     printf(m, (unsigned long long)icmp6stat.f, plural(icmp6stat.f))
835 #define p_5(f, m) printf(m, (unsigned long long)icmp6stat.f)
836 
837 	p(icp6s_error, "\t%llu call%s to icmp6_error\n");
838 	p(icp6s_canterror,
839 	    "\t%llu error%s not generated in response to an icmp6 message\n");
840 	p(icp6s_toofreq,
841 	  "\t%llu error%s not generated because of rate limitation\n");
842 #define NELEM (int)(sizeof(icmp6stat.icp6s_outhist)/sizeof(icmp6stat.icp6s_outhist[0]))
843 	for (first = 1, i = 0; i < NELEM; i++)
844 		if (icmp6stat.icp6s_outhist[i] != 0) {
845 			if (first) {
846 				printf("\tOutput histogram:\n");
847 				first = 0;
848 			}
849 			printf("\t\t%s: %llu\n", icmp6names[i],
850 			    (unsigned long long)icmp6stat.icp6s_outhist[i]);
851 		}
852 #undef NELEM
853 	p(icp6s_badcode, "\t%llu message%s with bad code fields\n");
854 	p(icp6s_tooshort, "\t%llu message%s < minimum length\n");
855 	p(icp6s_checksum, "\t%llu bad checksum%s\n");
856 	p(icp6s_badlen, "\t%llu message%s with bad length\n");
857 #define NELEM (int)(sizeof(icmp6stat.icp6s_inhist)/sizeof(icmp6stat.icp6s_inhist[0]))
858 	for (first = 1, i = 0; i < NELEM; i++)
859 		if (icmp6stat.icp6s_inhist[i] != 0) {
860 			if (first) {
861 				printf("\tInput histogram:\n");
862 				first = 0;
863 			}
864 			printf("\t\t%s: %llu\n", icmp6names[i],
865 			    (unsigned long long)icmp6stat.icp6s_inhist[i]);
866 		}
867 #undef NELEM
868 	printf("\tHistogram of error messages to be generated:\n");
869 	p_5(icp6s_odst_unreach_noroute, "\t\t%llu no route\n");
870 	p_5(icp6s_odst_unreach_admin, "\t\t%llu administratively prohibited\n");
871 	p_5(icp6s_odst_unreach_beyondscope, "\t\t%llu beyond scope\n");
872 	p_5(icp6s_odst_unreach_addr, "\t\t%llu address unreachable\n");
873 	p_5(icp6s_odst_unreach_noport, "\t\t%llu port unreachable\n");
874 	p_5(icp6s_opacket_too_big, "\t\t%llu packet too big\n");
875 	p_5(icp6s_otime_exceed_transit, "\t\t%llu time exceed transit\n");
876 	p_5(icp6s_otime_exceed_reassembly, "\t\t%llu time exceed reassembly\n");
877 	p_5(icp6s_oparamprob_header, "\t\t%llu erroneous header field\n");
878 	p_5(icp6s_oparamprob_nextheader, "\t\t%llu unrecognized next header\n");
879 	p_5(icp6s_oparamprob_option, "\t\t%llu unrecognized option\n");
880 	p_5(icp6s_oredirect, "\t\t%llu redirect\n");
881 	p_5(icp6s_ounknown, "\t\t%llu unknown\n");
882 
883 	p(icp6s_reflect, "\t%llu message response%s generated\n");
884 	p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n");
885 	p(icp6s_nd_badopt, "\t%qu message%s with bad ND options\n");
886 	p(icp6s_badns, "\t%qu bad neighbor solicitation message%s\n");
887 	p(icp6s_badna, "\t%qu bad neighbor advertisement message%s\n");
888 	p(icp6s_badrs, "\t%qu bad router solicitation message%s\n");
889 	p(icp6s_badra, "\t%qu bad router advertisement message%s\n");
890 	p(icp6s_badredirect, "\t%qu bad redirect message%s\n");
891 	p(icp6s_pmtuchg, "\t%llu path MTU change%s\n");
892 #undef p
893 #undef p_5
894 }
895 
896 /*
897  * Dump ICMPv6 per-interface statistics based on RFC 2466.
898  */
899 void
900 icmp6_ifstats(char *ifname)
901 {
902 	struct in6_ifreq ifr;
903 	int s;
904 #define	p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
905     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, plural(ifr.ifr_ifru.ifru_icmp6stat.f))
906 #define	p2(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
907     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, pluralies(ifr.ifr_ifru.ifru_icmp6stat.f))
908 
909 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
910 		perror("Warning: socket(AF_INET6)");
911 		return;
912 	}
913 
914 	strcpy(ifr.ifr_name, ifname);
915 	printf("icmp6 on %s:\n", ifr.ifr_name);
916 
917 	if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
918 		perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
919 		goto end;
920 	}
921 
922 	p(ifs6_in_msg, "\t%llu total input message%s\n");
923 	p(ifs6_in_error, "\t%llu total input error message%s\n");
924 	p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
925 	p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
926 	p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
927 	p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
928 	p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
929 	p(ifs6_in_echo, "\t%llu input echo request%s\n");
930 	p2(ifs6_in_echoreply, "\t%llu input echo repl%s\n");
931 	p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
932 	p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
933 	p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
934 	p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
935 	p(ifs6_in_redirect, "\t%llu input redirect%s\n");
936 	p2(ifs6_in_mldquery, "\t%llu input MLD quer%s\n");
937 	p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
938 	p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
939 
940 	p(ifs6_out_msg, "\t%llu total output message%s\n");
941 	p(ifs6_out_error, "\t%llu total output error message%s\n");
942 	p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
943 	p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
944 	p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
945 	p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
946 	p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
947 	p(ifs6_out_echo, "\t%llu output echo request%s\n");
948 	p2(ifs6_out_echoreply, "\t%llu output echo repl%s\n");
949 	p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
950 	p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
951 	p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
952 	p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
953 	p(ifs6_out_redirect, "\t%llu output redirect%s\n");
954 	p2(ifs6_out_mldquery, "\t%llu output MLD quer%s\n");
955 	p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
956 	p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
957 
958   end:
959 	close(s);
960 #undef p
961 }
962 
963 /*
964  * Dump PIM statistics structure.
965  */
966 void
967 pim6_stats(u_long off __unused, const char *name, int af1 __unused)
968 {
969 	struct pim6stat pim6stat;
970 
971 	if (off == 0)
972 		return;
973 	kread(off, (char *)&pim6stat, sizeof(pim6stat));
974 	printf("%s:\n", name);
975 
976 #define	p(f, m) if (pim6stat.f || sflag <= 1) \
977     printf(m, (unsigned long long)pim6stat.f, plural(pim6stat.f))
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(u_long off __unused, const char *name, int af1 __unused)
993 {
994 	struct rip6stat rip6stat;
995 	u_quad_t delivered;
996 	int mib[4];
997 	size_t l;
998 
999 	mib[0] = CTL_NET;
1000 	mib[1] = PF_INET6;
1001 	mib[2] = IPPROTO_IPV6;
1002 	mib[3] = IPV6CTL_RIP6STATS;
1003 	l = sizeof(rip6stat);
1004 	if (sysctl(mib, 4, &rip6stat, &l, NULL, 0) < 0) {
1005 		perror("Warning: sysctl(net.inet6.ip6.rip6stats)");
1006 		return;
1007 	}
1008 
1009 	printf("%s:\n", name);
1010 
1011 #define	p(f, m) if (rip6stat.f || sflag <= 1) \
1012     printf(m, (unsigned long long)rip6stat.f, plural(rip6stat.f))
1013 	p(rip6s_ipackets, "\t%llu message%s received\n");
1014 	p(rip6s_isum, "\t%llu checksum calcuration%s on inbound\n");
1015 	p(rip6s_badsum, "\t%llu message%s with bad checksum\n");
1016 	p(rip6s_nosock, "\t%llu message%s dropped due to no socket\n");
1017 	p(rip6s_nosockmcast,
1018 	    "\t%llu multicast message%s dropped due to no socket\n");
1019 	p(rip6s_fullsock,
1020 	    "\t%llu message%s dropped due to full socket buffers\n");
1021 	delivered = rip6stat.rip6s_ipackets -
1022 		    rip6stat.rip6s_badsum -
1023 		    rip6stat.rip6s_nosock -
1024 		    rip6stat.rip6s_nosockmcast -
1025 		    rip6stat.rip6s_fullsock;
1026 	if (delivered || sflag <= 1)
1027 		printf("\t%llu delivered\n", (unsigned long long)delivered);
1028 	p(rip6s_opackets, "\t%llu datagram%s output\n");
1029 #undef p
1030 }
1031 
1032 /*
1033  * Pretty print an Internet address (net address + port).
1034  * Take numeric_addr and numeric_port into consideration.
1035  */
1036 #define GETSERVBYPORT6(port, proto, ret)\
1037 {\
1038 	if (strcmp((proto), "tcp6") == 0)\
1039 		(ret) = getservbyport((int)(port), "tcp");\
1040 	else if (strcmp((proto), "udp6") == 0)\
1041 		(ret) = getservbyport((int)(port), "udp");\
1042 	else\
1043 		(ret) = getservbyport((int)(port), (proto));\
1044 };
1045 
1046 void
1047 inet6print(struct in6_addr *in6, int port, const char *proto, int numeric)
1048 {
1049 	struct servent *sp = 0;
1050 	char line[80], *cp;
1051 	int width;
1052 
1053 	sprintf(line, "%.*s.", Wflag ? 39 :
1054 		(Aflag && !numeric) ? 12 : 16, inet6name(in6));
1055 	cp = index(line, '\0');
1056 	if (!numeric && port)
1057 		GETSERVBYPORT6(port, proto, sp);
1058 	if (sp || port == 0)
1059 		sprintf(cp, "%.8s", sp ? sp->s_name : "*");
1060 	else
1061 		sprintf(cp, "%d", ntohs((u_short)port));
1062 	width = Wflag ? 45 : Aflag ? 18 : 22;
1063 	printf("%-*.*s ", width, width, line);
1064 }
1065 
1066 /*
1067  * Construct an Internet address representation.
1068  * If the numeric_addr has been supplied, give
1069  * numeric value, otherwise try for symbolic name.
1070  */
1071 
1072 char *
1073 inet6name(struct in6_addr *in6p)
1074 {
1075 	char *cp;
1076 	static char line[50];
1077 	struct hostent *hp;
1078 	static char domain[MAXHOSTNAMELEN];
1079 	static int first = 1;
1080 
1081 	if (first && !numeric_addr) {
1082 		first = 0;
1083 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1084 		    (cp = index(domain, '.')))
1085 			(void) strcpy(domain, cp + 1);
1086 		else
1087 			domain[0] = 0;
1088 	}
1089 	cp = 0;
1090 	if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1091 		hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
1092 		if (hp) {
1093 			if ((cp = index(hp->h_name, '.')) &&
1094 			    !strcmp(cp + 1, domain))
1095 				*cp = 0;
1096 			cp = hp->h_name;
1097 		}
1098 	}
1099 	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
1100 		strcpy(line, "*");
1101 	else if (cp)
1102 		strcpy(line, cp);
1103 	else
1104 		sprintf(line, "%s",
1105 			inet_ntop(AF_INET6, (void *)in6p, ntop_buf,
1106 				sizeof(ntop_buf)));
1107 	return (line);
1108 }
1109 #endif /*INET6*/
1110