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