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