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