xref: /dragonfly/usr.bin/netstat/inet6.c (revision 1de703da)
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: src/usr.bin/netstat/inet6.c,v 1.3.2.11 2001/09/17 14:53:17 ru Exp $
35  * $DragonFly: src/usr.bin/netstat/inet6.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
36  *
37  * @(#)inet6.c	8.4 (Berkeley) 4/20/94
38  */
39 
40 #ifdef INET6
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/ioctl.h>
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 #include <sys/sysctl.h>
48 
49 #include <net/route.h>
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
53 #include <netinet/ip6.h>
54 #include <netinet/icmp6.h>
55 #include <netinet/in_systm.h>
56 #include <netinet6/in6_pcb.h>
57 #include <netinet6/in6_var.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/pim6_var.h>
60 #include <netinet6/raw_ip6.h>
61 
62 #include <arpa/inet.h>
63 #include <netdb.h>
64 
65 #include <stdio.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include "netstat.h"
69 
70 struct	socket sockb;
71 
72 char	*inet6name (struct in6_addr *);
73 void	inet6print (struct in6_addr *, int, char *, int);
74 
75 static char ntop_buf[INET6_ADDRSTRLEN];
76 
77 static	char *ip6nh[] = {
78 	"hop by hop",
79 	"ICMP",
80 	"IGMP",
81 	"#3",
82 	"IP",
83 	"#5",
84 	"TCP",
85 	"#7",
86 	"#8",
87 	"#9",
88 	"#10",
89 	"#11",
90 	"#12",
91 	"#13",
92 	"#14",
93 	"#15",
94 	"#16",
95 	"UDP",
96 	"#18",
97 	"#19",
98 	"#20",
99 	"#21",
100 	"IDP",
101 	"#23",
102 	"#24",
103 	"#25",
104 	"#26",
105 	"#27",
106 	"#28",
107 	"TP",
108 	"#30",
109 	"#31",
110 	"#32",
111 	"#33",
112 	"#34",
113 	"#35",
114 	"#36",
115 	"#37",
116 	"#38",
117 	"#39",
118 	"#40",
119 	"IP6",
120 	"#42",
121 	"routing",
122 	"fragment",
123 	"#45",
124 	"#46",
125 	"#47",
126 	"#48",
127 	"#49",
128 	"ESP",
129 	"AH",
130 	"#52",
131 	"#53",
132 	"#54",
133 	"#55",
134 	"#56",
135 	"#57",
136 	"ICMP6",
137 	"no next header",
138 	"destination option",
139 	"#61",
140 	"#62",
141 	"#63",
142 	"#64",
143 	"#65",
144 	"#66",
145 	"#67",
146 	"#68",
147 	"#69",
148 	"#70",
149 	"#71",
150 	"#72",
151 	"#73",
152 	"#74",
153 	"#75",
154 	"#76",
155 	"#77",
156 	"#78",
157 	"#79",
158 	"ISOIP",
159 	"#81",
160 	"#82",
161 	"#83",
162 	"#84",
163 	"#85",
164 	"#86",
165 	"#87",
166 	"#88",
167 	"OSPF",
168 	"#80",
169 	"#91",
170 	"#92",
171 	"#93",
172 	"#94",
173 	"#95",
174 	"#96",
175 	"Ethernet",
176 	"#98",
177 	"#99",
178 	"#100",
179 	"#101",
180 	"#102",
181 	"PIM",
182 	"#104",
183 	"#105",
184 	"#106",
185 	"#107",
186 	"#108",
187 	"#109",
188 	"#110",
189 	"#111",
190 	"#112",
191 	"#113",
192 	"#114",
193 	"#115",
194 	"#116",
195 	"#117",
196 	"#118",
197 	"#119",
198 	"#120",
199 	"#121",
200 	"#122",
201 	"#123",
202 	"#124",
203 	"#125",
204 	"#126",
205 	"#127",
206 	"#128",
207 	"#129",
208 	"#130",
209 	"#131",
210 	"#132",
211 	"#133",
212 	"#134",
213 	"#135",
214 	"#136",
215 	"#137",
216 	"#138",
217 	"#139",
218 	"#140",
219 	"#141",
220 	"#142",
221 	"#143",
222 	"#144",
223 	"#145",
224 	"#146",
225 	"#147",
226 	"#148",
227 	"#149",
228 	"#150",
229 	"#151",
230 	"#152",
231 	"#153",
232 	"#154",
233 	"#155",
234 	"#156",
235 	"#157",
236 	"#158",
237 	"#159",
238 	"#160",
239 	"#161",
240 	"#162",
241 	"#163",
242 	"#164",
243 	"#165",
244 	"#166",
245 	"#167",
246 	"#168",
247 	"#169",
248 	"#170",
249 	"#171",
250 	"#172",
251 	"#173",
252 	"#174",
253 	"#175",
254 	"#176",
255 	"#177",
256 	"#178",
257 	"#179",
258 	"#180",
259 	"#181",
260 	"#182",
261 	"#183",
262 	"#184",
263 	"#185",
264 	"#186",
265 	"#187",
266 	"#188",
267 	"#189",
268 	"#180",
269 	"#191",
270 	"#192",
271 	"#193",
272 	"#194",
273 	"#195",
274 	"#196",
275 	"#197",
276 	"#198",
277 	"#199",
278 	"#200",
279 	"#201",
280 	"#202",
281 	"#203",
282 	"#204",
283 	"#205",
284 	"#206",
285 	"#207",
286 	"#208",
287 	"#209",
288 	"#210",
289 	"#211",
290 	"#212",
291 	"#213",
292 	"#214",
293 	"#215",
294 	"#216",
295 	"#217",
296 	"#218",
297 	"#219",
298 	"#220",
299 	"#221",
300 	"#222",
301 	"#223",
302 	"#224",
303 	"#225",
304 	"#226",
305 	"#227",
306 	"#228",
307 	"#229",
308 	"#230",
309 	"#231",
310 	"#232",
311 	"#233",
312 	"#234",
313 	"#235",
314 	"#236",
315 	"#237",
316 	"#238",
317 	"#239",
318 	"#240",
319 	"#241",
320 	"#242",
321 	"#243",
322 	"#244",
323 	"#245",
324 	"#246",
325 	"#247",
326 	"#248",
327 	"#249",
328 	"#250",
329 	"#251",
330 	"#252",
331 	"#253",
332 	"#254",
333 	"#255",
334 };
335 
336 /*
337  * Dump IP6 statistics structure.
338  */
339 void
340 ip6_stats(u_long off __unused, char *name, int af __unused)
341 {
342 	struct ip6stat ip6stat;
343 	int first, i;
344 	int mib[4];
345 	size_t len;
346 
347 	mib[0] = CTL_NET;
348 	mib[1] = PF_INET6;
349 	mib[2] = IPPROTO_IPV6;
350 	mib[3] = IPV6CTL_STATS;
351 
352 	len = sizeof ip6stat;
353 	memset(&ip6stat, 0, len);
354 	if (sysctl(mib, 4, &ip6stat, &len, (void *)0, 0) < 0)
355 		return;
356 	printf("%s:\n", name);
357 
358 #define	p(f, m) if (ip6stat.f || sflag <= 1) \
359     printf(m, (unsigned long long)ip6stat.f, plural(ip6stat.f))
360 #define	p1a(f, m) if (ip6stat.f || sflag <= 1) \
361     printf(m, (unsigned long long)ip6stat.f)
362 
363 	p(ip6s_total, "\t%llu total packet%s received\n");
364 	p1a(ip6s_toosmall, "\t%llu with size smaller than minimum\n");
365 	p1a(ip6s_tooshort, "\t%llu with data size < data length\n");
366 	p1a(ip6s_badoptions, "\t%llu with bad options\n");
367 	p1a(ip6s_badvers, "\t%llu with incorrect version number\n");
368 	p(ip6s_fragments, "\t%llu fragment%s received\n");
369 	p(ip6s_fragdropped, "\t%llu fragment%s dropped (dup or out of space)\n");
370 	p(ip6s_fragtimeout, "\t%llu fragment%s dropped after timeout\n");
371 	p(ip6s_fragoverflow, "\t%llu fragment%s that exceeded limit\n");
372 	p(ip6s_reassembled, "\t%llu packet%s reassembled ok\n");
373 	p(ip6s_delivered, "\t%llu packet%s for this host\n");
374 	p(ip6s_forward, "\t%llu packet%s forwarded\n");
375 	p(ip6s_cantforward, "\t%llu packet%s not forwardable\n");
376 	p(ip6s_redirectsent, "\t%llu redirect%s sent\n");
377 	p(ip6s_localout, "\t%llu packet%s sent from this host\n");
378 	p(ip6s_rawout, "\t%llu packet%s sent with fabricated ip header\n");
379 	p(ip6s_odropped, "\t%llu output packet%s dropped due to no bufs, etc.\n");
380 	p(ip6s_noroute, "\t%llu output packet%s discarded due to no route\n");
381 	p(ip6s_fragmented, "\t%llu output datagram%s fragmented\n");
382 	p(ip6s_ofragments, "\t%llu fragment%s created\n");
383 	p(ip6s_cantfrag, "\t%llu datagram%s that can't be fragmented\n");
384 	p(ip6s_badscope, "\t%llu packet%s that violated scope rules\n");
385 	p(ip6s_notmember, "\t%llu multicast packet%s which we don't join\n");
386 	for (first = 1, i = 0; i < 256; i++)
387 		if (ip6stat.ip6s_nxthist[i] != 0) {
388 			if (first) {
389 				printf("\tInput histogram:\n");
390 				first = 0;
391 			}
392 			printf("\t\t%s: %llu\n", ip6nh[i],
393 			    (unsigned long long)ip6stat.ip6s_nxthist[i]);
394 		}
395 	printf("\tMbuf statistics:\n");
396 	printf("\t\t%llu one mbuf\n", (unsigned long long)ip6stat.ip6s_m1);
397 	for (first = 1, i = 0; i < 32; i++) {
398 		char ifbuf[IFNAMSIZ];
399 		if (ip6stat.ip6s_m2m[i] != 0) {
400 			if (first) {
401 				printf("\t\ttwo or more mbuf:\n");
402 				first = 0;
403 			}
404 			printf("\t\t\t%s= %llu\n",
405 			    if_indextoname(i, ifbuf),
406 			    (unsigned long long)ip6stat.ip6s_m2m[i]);
407 		}
408 	}
409 	printf("\t\t%llu one ext mbuf\n",
410 	    (unsigned long long)ip6stat.ip6s_mext1);
411 	printf("\t\t%llu two or more ext mbuf\n",
412 	    (unsigned long long)ip6stat.ip6s_mext2m);
413 	p(ip6s_exthdrtoolong,
414 	    "\t%llu packet%s whose headers are not continuous\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 may 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 	p1a(ip6s_forward_cachehit, "\t%llu forward cache hit\n");
489 	p1a(ip6s_forward_cachemiss, "\t%llu forward cache miss\n");
490 #undef p
491 #undef p1a
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 #define	p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
503     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, plural(ifr.ifr_ifru.ifru_stat.f))
504 #define	p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
505     printf(m, (unsigned long long)ip6stat.f)
506 
507 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
508 		perror("Warning: socket(AF_INET6)");
509 		return;
510 	}
511 
512 	strcpy(ifr.ifr_name, ifname);
513 	printf("ip6 on %s:\n", ifr.ifr_name);
514 
515 	if (ioctl(s, SIOCGIFSTAT_IN6, (char *)&ifr) < 0) {
516 		perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
517 		goto end;
518 	}
519 
520 	p(ifs6_in_receive, "\t%llu total input datagram%s\n");
521 	p(ifs6_in_hdrerr, "\t%llu datagram%s with invalid header received\n");
522 	p(ifs6_in_toobig, "\t%llu datagram%s exceeded MTU received\n");
523 	p(ifs6_in_noroute, "\t%llu datagram%s with no route received\n");
524 	p(ifs6_in_addrerr, "\t%llu datagram%s with invalid dst received\n");
525 	p(ifs6_in_protounknown, "\t%llu datagram%s with unknown proto received\n");
526 	p(ifs6_in_truncated, "\t%llu truncated datagram%s received\n");
527 	p(ifs6_in_discard, "\t%llu input datagram%s discarded\n");
528 	p(ifs6_in_deliver,
529 	  "\t%llu datagram%s delivered to an upper layer protocol\n");
530 	p(ifs6_out_forward, "\t%llu datagram%s forwarded to this interface\n");
531 	p(ifs6_out_request,
532 	  "\t%llu datagram%s sent from an upper layer protocol\n");
533 	p(ifs6_out_discard, "\t%llu total discarded output datagram%s\n");
534 	p(ifs6_out_fragok, "\t%llu output datagram%s fragmented\n");
535 	p(ifs6_out_fragfail, "\t%llu output datagram%s failed on fragment\n");
536 	p(ifs6_out_fragcreat, "\t%llu output datagram%s succeeded on fragment\n");
537 	p(ifs6_reass_reqd, "\t%llu incoming datagram%s fragmented\n");
538 	p(ifs6_reass_ok, "\t%llu datagram%s reassembled\n");
539 	p(ifs6_reass_fail, "\t%llu datagram%s failed on reassembling\n");
540 	p(ifs6_in_mcast, "\t%llu multicast datagram%s received\n");
541 	p(ifs6_out_mcast, "\t%llu multicast datagram%s sent\n");
542 
543   end:
544 	close(s);
545 
546 #undef p
547 #undef p_5
548 }
549 
550 static	char *icmp6names[] = {
551 	"#0",
552 	"unreach",
553 	"packet too big",
554 	"time exceed",
555 	"parameter problem",
556 	"#5",
557 	"#6",
558 	"#7",
559 	"#8",
560 	"#9",
561 	"#10",
562 	"#11",
563 	"#12",
564 	"#13",
565 	"#14",
566 	"#15",
567 	"#16",
568 	"#17",
569 	"#18",
570 	"#19",
571 	"#20",
572 	"#21",
573 	"#22",
574 	"#23",
575 	"#24",
576 	"#25",
577 	"#26",
578 	"#27",
579 	"#28",
580 	"#29",
581 	"#30",
582 	"#31",
583 	"#32",
584 	"#33",
585 	"#34",
586 	"#35",
587 	"#36",
588 	"#37",
589 	"#38",
590 	"#39",
591 	"#40",
592 	"#41",
593 	"#42",
594 	"#43",
595 	"#44",
596 	"#45",
597 	"#46",
598 	"#47",
599 	"#48",
600 	"#49",
601 	"#50",
602 	"#51",
603 	"#52",
604 	"#53",
605 	"#54",
606 	"#55",
607 	"#56",
608 	"#57",
609 	"#58",
610 	"#59",
611 	"#60",
612 	"#61",
613 	"#62",
614 	"#63",
615 	"#64",
616 	"#65",
617 	"#66",
618 	"#67",
619 	"#68",
620 	"#69",
621 	"#70",
622 	"#71",
623 	"#72",
624 	"#73",
625 	"#74",
626 	"#75",
627 	"#76",
628 	"#77",
629 	"#78",
630 	"#79",
631 	"#80",
632 	"#81",
633 	"#82",
634 	"#83",
635 	"#84",
636 	"#85",
637 	"#86",
638 	"#87",
639 	"#88",
640 	"#89",
641 	"#80",
642 	"#91",
643 	"#92",
644 	"#93",
645 	"#94",
646 	"#95",
647 	"#96",
648 	"#97",
649 	"#98",
650 	"#99",
651 	"#100",
652 	"#101",
653 	"#102",
654 	"#103",
655 	"#104",
656 	"#105",
657 	"#106",
658 	"#107",
659 	"#108",
660 	"#109",
661 	"#110",
662 	"#111",
663 	"#112",
664 	"#113",
665 	"#114",
666 	"#115",
667 	"#116",
668 	"#117",
669 	"#118",
670 	"#119",
671 	"#120",
672 	"#121",
673 	"#122",
674 	"#123",
675 	"#124",
676 	"#125",
677 	"#126",
678 	"#127",
679 	"echo",
680 	"echo reply",
681 	"multicast listener query",
682 	"multicast listener report",
683 	"multicast listener done",
684 	"router solicitation",
685 	"router advertisement",
686 	"neighbor solicitation",
687 	"neighbor advertisement",
688 	"redirect",
689 	"router renumbering",
690 	"node information request",
691 	"node information reply",
692 	"inverse neighbor solicitation",
693 	"inverse neighbor advertisement",
694 	"#143",
695 	"#144",
696 	"#145",
697 	"#146",
698 	"#147",
699 	"#148",
700 	"#149",
701 	"#150",
702 	"#151",
703 	"#152",
704 	"#153",
705 	"#154",
706 	"#155",
707 	"#156",
708 	"#157",
709 	"#158",
710 	"#159",
711 	"#160",
712 	"#161",
713 	"#162",
714 	"#163",
715 	"#164",
716 	"#165",
717 	"#166",
718 	"#167",
719 	"#168",
720 	"#169",
721 	"#170",
722 	"#171",
723 	"#172",
724 	"#173",
725 	"#174",
726 	"#175",
727 	"#176",
728 	"#177",
729 	"#178",
730 	"#179",
731 	"#180",
732 	"#181",
733 	"#182",
734 	"#183",
735 	"#184",
736 	"#185",
737 	"#186",
738 	"#187",
739 	"#188",
740 	"#189",
741 	"#180",
742 	"#191",
743 	"#192",
744 	"#193",
745 	"#194",
746 	"#195",
747 	"#196",
748 	"#197",
749 	"#198",
750 	"#199",
751 	"#200",
752 	"#201",
753 	"#202",
754 	"#203",
755 	"#204",
756 	"#205",
757 	"#206",
758 	"#207",
759 	"#208",
760 	"#209",
761 	"#210",
762 	"#211",
763 	"#212",
764 	"#213",
765 	"#214",
766 	"#215",
767 	"#216",
768 	"#217",
769 	"#218",
770 	"#219",
771 	"#220",
772 	"#221",
773 	"#222",
774 	"#223",
775 	"#224",
776 	"#225",
777 	"#226",
778 	"#227",
779 	"#228",
780 	"#229",
781 	"#230",
782 	"#231",
783 	"#232",
784 	"#233",
785 	"#234",
786 	"#235",
787 	"#236",
788 	"#237",
789 	"#238",
790 	"#239",
791 	"#240",
792 	"#241",
793 	"#242",
794 	"#243",
795 	"#244",
796 	"#245",
797 	"#246",
798 	"#247",
799 	"#248",
800 	"#249",
801 	"#250",
802 	"#251",
803 	"#252",
804 	"#253",
805 	"#254",
806 	"#255",
807 };
808 
809 /*
810  * Dump ICMP6 statistics.
811  */
812 void
813 icmp6_stats(u_long off __unused, char *name, int af __unused)
814 {
815 	struct icmp6stat icmp6stat;
816 	register int i, first;
817 	int mib[4];
818 	size_t len;
819 
820 	mib[0] = CTL_NET;
821 	mib[1] = PF_INET6;
822 	mib[2] = IPPROTO_ICMPV6;
823 	mib[3] = ICMPV6CTL_STATS;
824 
825 	len = sizeof icmp6stat;
826 	memset(&icmp6stat, 0, len);
827 	if (sysctl(mib, 4, &icmp6stat, &len, (void *)0, 0) < 0)
828 		return;
829 	printf("%s:\n", name);
830 
831 #define	p(f, m) if (icmp6stat.f || sflag <= 1) \
832     printf(m, (unsigned long long)icmp6stat.f, plural(icmp6stat.f))
833 #define p_5(f, m) printf(m, (unsigned long long)icmp6stat.f)
834 
835 	p(icp6s_error, "\t%llu call%s to icmp_error\n");
836 	p(icp6s_canterror,
837 	    "\t%llu error%s not generated because old message was icmp error or so\n");
838 	p(icp6s_toofreq,
839 	  "\t%llu error%s not generated because rate limitation\n");
840 #define NELEM (sizeof(icmp6stat.icp6s_outhist)/sizeof(icmp6stat.icp6s_outhist[0]))
841 	for (first = 1, i = 0; i < NELEM; i++)
842 		if (icmp6stat.icp6s_outhist[i] != 0) {
843 			if (first) {
844 				printf("\tOutput histogram:\n");
845 				first = 0;
846 			}
847 			printf("\t\t%s: %llu\n", icmp6names[i],
848 			    (unsigned long long)icmp6stat.icp6s_outhist[i]);
849 		}
850 #undef NELEM
851 	p(icp6s_badcode, "\t%llu message%s with bad code fields\n");
852 	p(icp6s_tooshort, "\t%llu message%s < minimum length\n");
853 	p(icp6s_checksum, "\t%llu bad checksum%s\n");
854 	p(icp6s_badlen, "\t%llu message%s with bad length\n");
855 #define NELEM (sizeof(icmp6stat.icp6s_inhist)/sizeof(icmp6stat.icp6s_inhist[0]))
856 	for (first = 1, i = 0; i < NELEM; i++)
857 		if (icmp6stat.icp6s_inhist[i] != 0) {
858 			if (first) {
859 				printf("\tInput histogram:\n");
860 				first = 0;
861 			}
862 			printf("\t\t%s: %llu\n", icmp6names[i],
863 			    (unsigned long long)icmp6stat.icp6s_inhist[i]);
864 		}
865 #undef NELEM
866 	printf("\tHistogram of error messages to be generated:\n");
867 	p_5(icp6s_odst_unreach_noroute, "\t\t%llu no route\n");
868 	p_5(icp6s_odst_unreach_admin, "\t\t%llu administratively prohibited\n");
869 	p_5(icp6s_odst_unreach_beyondscope, "\t\t%llu beyond scope\n");
870 	p_5(icp6s_odst_unreach_addr, "\t\t%llu address unreachable\n");
871 	p_5(icp6s_odst_unreach_noport, "\t\t%llu port unreachable\n");
872 	p_5(icp6s_opacket_too_big, "\t\t%llu packet too big\n");
873 	p_5(icp6s_otime_exceed_transit, "\t\t%llu time exceed transit\n");
874 	p_5(icp6s_otime_exceed_reassembly, "\t\t%llu time exceed reassembly\n");
875 	p_5(icp6s_oparamprob_header, "\t\t%llu erroneous header field\n");
876 	p_5(icp6s_oparamprob_nextheader, "\t\t%llu unrecognized next header\n");
877 	p_5(icp6s_oparamprob_option, "\t\t%llu unrecognized option\n");
878 	p_5(icp6s_oredirect, "\t\t%llu redirect\n");
879 	p_5(icp6s_ounknown, "\t\t%llu unknown\n");
880 
881 	p(icp6s_reflect, "\t%llu message response%s generated\n");
882 	p(icp6s_nd_toomanyopt, "\t%llu message%s with too many ND options\n");
883 	p(icp6s_nd_badopt, "\t%qu message%s with bad ND options\n");
884 	p(icp6s_badns, "\t%qu bad neighbor solicitation message%s\n");
885 	p(icp6s_badna, "\t%qu bad neighbor advertisement message%s\n");
886 	p(icp6s_badrs, "\t%qu bad router solicitation message%s\n");
887 	p(icp6s_badra, "\t%qu bad router advertisement message%s\n");
888 	p(icp6s_badredirect, "\t%qu bad redirect message%s\n");
889 	p(icp6s_pmtuchg, "\t%llu path MTU change%s\n");
890 #undef p
891 #undef p_5
892 }
893 
894 /*
895  * Dump ICMPv6 per-interface statistics based on RFC 2466.
896  */
897 void
898 icmp6_ifstats(char *ifname)
899 {
900 	struct in6_ifreq ifr;
901 	int s;
902 #define	p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
903     printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, plural(ifr.ifr_ifru.ifru_icmp6stat.f))
904 
905 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
906 		perror("Warning: socket(AF_INET6)");
907 		return;
908 	}
909 
910 	strcpy(ifr.ifr_name, ifname);
911 	printf("icmp6 on %s:\n", ifr.ifr_name);
912 
913 	if (ioctl(s, SIOCGIFSTAT_ICMP6, (char *)&ifr) < 0) {
914 		perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
915 		goto end;
916 	}
917 
918 	p(ifs6_in_msg, "\t%llu total input message%s\n");
919 	p(ifs6_in_error, "\t%llu total input error message%s\n");
920 	p(ifs6_in_dstunreach, "\t%llu input destination unreachable error%s\n");
921 	p(ifs6_in_adminprohib, "\t%llu input administratively prohibited error%s\n");
922 	p(ifs6_in_timeexceed, "\t%llu input time exceeded error%s\n");
923 	p(ifs6_in_paramprob, "\t%llu input parameter problem error%s\n");
924 	p(ifs6_in_pkttoobig, "\t%llu input packet too big error%s\n");
925 	p(ifs6_in_echo, "\t%llu input echo request%s\n");
926 	p(ifs6_in_echoreply, "\t%llu input echo reply%s\n");
927 	p(ifs6_in_routersolicit, "\t%llu input router solicitation%s\n");
928 	p(ifs6_in_routeradvert, "\t%llu input router advertisement%s\n");
929 	p(ifs6_in_neighborsolicit, "\t%llu input neighbor solicitation%s\n");
930 	p(ifs6_in_neighboradvert, "\t%llu input neighbor advertisement%s\n");
931 	p(ifs6_in_redirect, "\t%llu input redirect%s\n");
932 	p(ifs6_in_mldquery, "\t%llu input MLD query%s\n");
933 	p(ifs6_in_mldreport, "\t%llu input MLD report%s\n");
934 	p(ifs6_in_mlddone, "\t%llu input MLD done%s\n");
935 
936 	p(ifs6_out_msg, "\t%llu total output message%s\n");
937 	p(ifs6_out_error, "\t%llu total output error message%s\n");
938 	p(ifs6_out_dstunreach, "\t%llu output destination unreachable error%s\n");
939 	p(ifs6_out_adminprohib, "\t%llu output administratively prohibited error%s\n");
940 	p(ifs6_out_timeexceed, "\t%llu output time exceeded error%s\n");
941 	p(ifs6_out_paramprob, "\t%llu output parameter problem error%s\n");
942 	p(ifs6_out_pkttoobig, "\t%llu output packet too big error%s\n");
943 	p(ifs6_out_echo, "\t%llu output echo request%s\n");
944 	p(ifs6_out_echoreply, "\t%llu output echo reply%s\n");
945 	p(ifs6_out_routersolicit, "\t%llu output router solicitation%s\n");
946 	p(ifs6_out_routeradvert, "\t%llu output router advertisement%s\n");
947 	p(ifs6_out_neighborsolicit, "\t%llu output neighbor solicitation%s\n");
948 	p(ifs6_out_neighboradvert, "\t%llu output neighbor advertisement%s\n");
949 	p(ifs6_out_redirect, "\t%llu output redirect%s\n");
950 	p(ifs6_out_mldquery, "\t%llu output MLD query%s\n");
951 	p(ifs6_out_mldreport, "\t%llu output MLD report%s\n");
952 	p(ifs6_out_mlddone, "\t%llu output MLD done%s\n");
953 
954   end:
955 	close(s);
956 #undef p
957 }
958 
959 /*
960  * Dump PIM statistics structure.
961  */
962 void
963 pim6_stats(u_long off __unused, char *name, int af __unused)
964 {
965 	struct pim6stat pim6stat;
966 
967 	if (off == 0)
968 		return;
969 	kread(off, (char *)&pim6stat, sizeof(pim6stat));
970 	printf("%s:\n", name);
971 
972 #define	p(f, m) if (pim6stat.f || sflag <= 1) \
973     printf(m, (unsigned long long)pim6stat.f, plural(pim6stat.f))
974 	p(pim6s_rcv_total, "\t%llu message%s received\n");
975 	p(pim6s_rcv_tooshort, "\t%llu message%s received with too few bytes\n");
976 	p(pim6s_rcv_badsum, "\t%llu message%s received with bad checksum\n");
977 	p(pim6s_rcv_badversion, "\t%llu message%s received with bad version\n");
978 	p(pim6s_rcv_registers, "\t%llu register%s received\n");
979 	p(pim6s_rcv_badregisters, "\t%llu bad register%s received\n");
980 	p(pim6s_snd_registers, "\t%llu register%s sent\n");
981 #undef p
982 }
983 
984 /*
985  * Dump raw ip6 statistics structure.
986  */
987 void
988 rip6_stats(u_long off __unused, char *name, int af __unused)
989 {
990 	struct rip6stat rip6stat;
991 	u_quad_t delivered;
992 	int mib[4];
993 	size_t l;
994 
995 	mib[0] = CTL_NET;
996 	mib[1] = PF_INET6;
997 	mib[2] = IPPROTO_IPV6;
998 	mib[3] = IPV6CTL_RIP6STATS;
999 	l = sizeof(rip6stat);
1000 	if (sysctl(mib, 4, &rip6stat, &l, NULL, 0) < 0) {
1001 		perror("Warning: sysctl(net.inet6.ip6.rip6stats)");
1002 		return;
1003 	}
1004 
1005 	printf("%s:\n", name);
1006 
1007 #define	p(f, m) if (rip6stat.f || sflag <= 1) \
1008     printf(m, (unsigned long long)rip6stat.f, plural(rip6stat.f))
1009 	p(rip6s_ipackets, "\t%llu message%s received\n");
1010 	p(rip6s_isum, "\t%llu checksum calcuration%s on inbound\n");
1011 	p(rip6s_badsum, "\t%llu message%s with bad checksum\n");
1012 	p(rip6s_nosock, "\t%llu message%s dropped due to no socket\n");
1013 	p(rip6s_nosockmcast,
1014 	    "\t%llu multicast message%s dropped due to no socket\n");
1015 	p(rip6s_fullsock,
1016 	    "\t%llu message%s dropped due to full socket buffers\n");
1017 	delivered = rip6stat.rip6s_ipackets -
1018 		    rip6stat.rip6s_badsum -
1019 		    rip6stat.rip6s_nosock -
1020 		    rip6stat.rip6s_nosockmcast -
1021 		    rip6stat.rip6s_fullsock;
1022 	if (delivered || sflag <= 1)
1023 		printf("\t%llu delivered\n", (unsigned long long)delivered);
1024 	p(rip6s_opackets, "\t%llu datagram%s output\n");
1025 #undef p
1026 }
1027 
1028 /*
1029  * Pretty print an Internet address (net address + port).
1030  * Take numeric_addr and numeric_port into consideration.
1031  */
1032 #define GETSERVBYPORT6(port, proto, ret)\
1033 {\
1034 	if (strcmp((proto), "tcp6") == 0)\
1035 		(ret) = getservbyport((int)(port), "tcp");\
1036 	else if (strcmp((proto), "udp6") == 0)\
1037 		(ret) = getservbyport((int)(port), "udp");\
1038 	else\
1039 		(ret) = getservbyport((int)(port), (proto));\
1040 };
1041 
1042 void
1043 inet6print(struct in6_addr *in6, int port, char *proto, int numeric)
1044 {
1045 	struct servent *sp = 0;
1046 	char line[80], *cp;
1047 	int width;
1048 
1049 	sprintf(line, "%.*s.", Wflag ? 39 :
1050 		(Aflag && !numeric) ? 12 : 16, inet6name(in6));
1051 	cp = index(line, '\0');
1052 	if (!numeric && port)
1053 		GETSERVBYPORT6(port, proto, sp);
1054 	if (sp || port == 0)
1055 		sprintf(cp, "%.8s", sp ? sp->s_name : "*");
1056 	else
1057 		sprintf(cp, "%d", ntohs((u_short)port));
1058 	width = Wflag ? 45 : Aflag ? 18 : 22;
1059 	printf("%-*.*s ", width, width, line);
1060 }
1061 
1062 /*
1063  * Construct an Internet address representation.
1064  * If the numeric_addr has been supplied, give
1065  * numeric value, otherwise try for symbolic name.
1066  */
1067 
1068 char *
1069 inet6name(struct in6_addr *in6p)
1070 {
1071 	register char *cp;
1072 	static char line[50];
1073 	struct hostent *hp;
1074 	static char domain[MAXHOSTNAMELEN];
1075 	static int first = 1;
1076 
1077 	if (first && !numeric_addr) {
1078 		first = 0;
1079 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1080 		    (cp = index(domain, '.')))
1081 			(void) strcpy(domain, cp + 1);
1082 		else
1083 			domain[0] = 0;
1084 	}
1085 	cp = 0;
1086 	if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) {
1087 		hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6);
1088 		if (hp) {
1089 			if ((cp = index(hp->h_name, '.')) &&
1090 			    !strcmp(cp + 1, domain))
1091 				*cp = 0;
1092 			cp = hp->h_name;
1093 		}
1094 	}
1095 	if (IN6_IS_ADDR_UNSPECIFIED(in6p))
1096 		strcpy(line, "*");
1097 	else if (cp)
1098 		strcpy(line, cp);
1099 	else
1100 		sprintf(line, "%s",
1101 			inet_ntop(AF_INET6, (void *)in6p, ntop_buf,
1102 				sizeof(ntop_buf)));
1103 	return (line);
1104 }
1105 #endif /*INET6*/
1106