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