2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp */
31 * Copyright (c) 1983, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * $FreeBSD: src/usr.bin/netstat/inet6.c,v 1.3.2.9 2001/08/10 09:07:09 ru Exp $
67 static char sccsid[] = "@(#)inet6.c 8.4 (Berkeley) 4/20/94";
72 #include <sys/param.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/ioctl.h>
76 #include <sys/sysctl.h>
78 #include <net/route.h>
80 #include <net/if_var.h>
81 #include <netinet/in.h>
82 #include <netinet/ip6.h>
83 #include <netinet/icmp6.h>
84 #include <netinet/in_systm.h>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/ip6_var.h>
88 #include <netinet6/pim6_var.h>
89 #include <netinet6/raw_ip6.h>
91 #include <arpa/inet.h>
99 #if defined(__APPLE__) && !defined(__unused)
103 char *inet6name (struct in6_addr
*);
104 void inet6print (struct in6_addr
*, int, char *, int);
106 static char *ip6nh
[] = {
167 "destination option",
366 * Dump IP6 statistics structure.
369 ip6_stats(uint32_t off __unused
, char *name
, int af __unused
)
371 static struct ip6stat pip6stat
;
372 struct ip6stat ip6stat
;
379 mib
[2] = IPPROTO_IPV6
;
380 mib
[3] = IPV6CTL_STATS
;
382 len
= sizeof ip6stat
;
383 memset(&ip6stat
, 0, len
);
384 if (sysctl(mib
, 4, &ip6stat
, &len
, (void *)0, 0) < 0)
386 if (interval
&& vflag
> 0)
388 printf("%s:\n", name
);
390 #define IP6DIFF(f) (ip6stat.f - pip6stat.f)
391 #define p(f, m) if (IP6DIFF(f) || sflag <= 1) \
392 printf(m, (unsigned long long)IP6DIFF(f), plural(IP6DIFF(f)))
393 #define p1a(f, m) if (IP6DIFF(f) || sflag <= 1) \
394 printf(m, (unsigned long long)IP6DIFF(f))
396 p(ip6s_total
, "\t%llu total packet%s received\n");
397 p1a(ip6s_toosmall
, "\t\t%llu with size smaller than minimum\n");
398 p1a(ip6s_tooshort
, "\t\t%llu with data size < data length\n");
399 p1a(ip6s_adj
, "\t\t%llu with data size > data length\n");
400 p(ip6s_adj_hwcsum_clr
,
401 "\t\t\t%llu packet%s forced to software checksum\n");
402 p1a(ip6s_badoptions
, "\t\t%llu with bad options\n");
403 p1a(ip6s_badvers
, "\t\t%llu with incorrect version number\n");
404 p(ip6s_fragments
, "\t\t%llu fragment%s received\n");
405 p1a(ip6s_fragdropped
,
406 "\t\t\t%llu dropped (dup or out of space)\n");
407 p1a(ip6s_fragtimeout
, "\t\t\t%llu dropped after timeout\n");
408 p1a(ip6s_fragoverflow
, "\t\t\t%llu exceeded limit\n");
409 p1a(ip6s_reassembled
, "\t\t\t%llu reassembled ok\n");
410 p(ip6s_delivered
, "\t\t%llu packet%s for this host\n");
411 p(ip6s_forward
, "\t\t%llu packet%s forwarded\n");
412 p(ip6s_cantforward
, "\t\t%llu packet%s not forwardable\n");
413 p(ip6s_redirectsent
, "\t\t%llu redirect%s sent\n");
414 p(ip6s_notmember
, "\t\t%llu multicast packet%s which we don't join\n");
415 p(ip6s_exthdrtoolong
,
416 "\t\t%llu packet%s whose headers are not continuous\n");
417 p(ip6s_nogif
, "\t\t%llu tunneling packet%s that can't find gif\n");
419 "\t\t%llu packet%s discarded due to too may headers\n");
420 p1a(ip6s_forward_cachehit
, "\t\t%llu forward cache hit\n");
421 p1a(ip6s_forward_cachemiss
, "\t\t%llu forward cache miss\n");
423 "\t\t%llu packet%s dropped due to no bufs for control data\n");
424 p(ip6s_localout
, "\t%llu packet%s sent from this host\n");
425 p(ip6s_rawout
, "\t\t%llu packet%s sent with fabricated ip header\n");
427 "\t\t%llu output packet%s dropped due to no bufs, etc.\n");
428 p(ip6s_noroute
, "\t\t%llu output packet%s discarded due to no route\n");
429 p(ip6s_fragmented
, "\t\t%llu output datagram%s fragmented\n");
430 p(ip6s_ofragments
, "\t\t%llu fragment%s created\n");
431 p(ip6s_cantfrag
, "\t\t%llu datagram%s that can't be fragmented\n");
432 p(ip6s_badscope
, "\t\t%llu packet%s that violated scope rules\n");
433 for (first
= 1, i
= 0; i
< 256; i
++)
434 if (IP6DIFF(ip6s_nxthist
[i
]) != 0) {
436 printf("\tInput histogram:\n");
439 printf("\t\t%s: %llu\n", ip6nh
[i
],
440 (unsigned long long)IP6DIFF(ip6s_nxthist
[i
]));
442 printf("\tMbuf statistics:\n");
443 printf("\t\t%llu one mbuf\n", (unsigned long long)IP6DIFF(ip6s_m1
));
444 for (first
= 1, i
= 0; i
< 32; i
++) {
445 char ifbuf
[IFNAMSIZ
];
446 if (IP6DIFF(ip6s_m2m
[i
]) != 0) {
448 printf("\t\ttwo or more mbuf:\n");
451 printf("\t\t\t%s= %llu\n",
452 if_indextoname(i
, ifbuf
),
453 (unsigned long long)IP6DIFF(ip6s_m2m
[i
]));
456 printf("\t\t%llu one ext mbuf\n",
457 (unsigned long long)IP6DIFF(ip6s_mext1
));
458 printf("\t\t%llu two or more ext mbuf\n",
459 (unsigned long long)IP6DIFF(ip6s_mext2m
));
461 /* for debugging source address selection */
462 #define PRINT_SCOPESTAT(s,i) do {\
463 switch(i) { /* XXX hardcoding in each case */\
465 p(s, "\t\t\t%llu node-local%s\n");\
468 p(s,"\t\t\t%llu link-local%s\n");\
471 p(s,"\t\t\t%llu site-local%s\n");\
474 p(s,"\t\t\t%llu global%s\n");\
477 printf("\t\t\t%llu addresses scope=%x\n",\
478 (unsigned long long)IP6DIFF(s), i);\
483 "\t\t%llu failure%s of source address selection\n");
484 for (first
= 1, i
= 0; i
< 16; i
++) {
485 if (IP6DIFF(ip6s_sources_sameif
[i
])) {
487 printf("\t\tsource addresses on an outgoing I/F\n");
490 PRINT_SCOPESTAT(ip6s_sources_sameif
[i
], i
);
493 for (first
= 1, i
= 0; i
< 16; i
++) {
494 if (IP6DIFF(ip6s_sources_otherif
[i
])) {
496 printf("\t\tsource addresses on a non-outgoing I/F\n");
499 PRINT_SCOPESTAT(ip6s_sources_otherif
[i
], i
);
502 for (first
= 1, i
= 0; i
< 16; i
++) {
503 if (IP6DIFF(ip6s_sources_samescope
[i
])) {
505 printf("\t\tsource addresses of same scope\n");
508 PRINT_SCOPESTAT(ip6s_sources_samescope
[i
], i
);
511 for (first
= 1, i
= 0; i
< 16; i
++) {
512 if (IP6DIFF(ip6s_sources_otherscope
[i
])) {
514 printf("\t\tsource addresses of a different scope\n");
517 PRINT_SCOPESTAT(ip6s_sources_otherscope
[i
], i
);
520 for (first
= 1, i
= 0; i
< 16; i
++) {
521 if (IP6DIFF(ip6s_sources_deprecated
[i
])) {
523 printf("\t\tdeprecated source addresses\n");
526 PRINT_SCOPESTAT(ip6s_sources_deprecated
[i
], i
);
531 bcopy(&ip6stat
, &pip6stat
, len
);
539 * Dump IPv6 per-interface statistics based on RFC 2465.
542 ip6_ifstats(char *ifname
)
544 struct in6_ifreq ifr
;
546 #define p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
547 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, plural(ifr.ifr_ifru.ifru_stat.f))
548 #define p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
549 printf(m, (unsigned long long)ip6stat.f)
551 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
552 perror("Warning: socket(AF_INET6)");
556 if (interval
&& vflag
> 0)
558 strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
559 printf("ip6 on %s:\n", ifr
.ifr_name
);
561 if (ioctl(s
, SIOCGIFSTAT_IN6
, (char *)&ifr
) < 0) {
562 perror("Warning: ioctl(SIOCGIFSTAT_IN6)");
566 p(ifs6_in_receive
, "\t%llu total input datagram%s\n");
567 p(ifs6_in_hdrerr
, "\t%llu datagram%s with invalid header received\n");
568 p(ifs6_in_toobig
, "\t%llu datagram%s exceeded MTU received\n");
569 p(ifs6_in_noroute
, "\t%llu datagram%s with no route received\n");
570 p(ifs6_in_addrerr
, "\t%llu datagram%s with invalid dst received\n");
571 p(ifs6_in_protounknown
, "\t%llu datagram%s with unknown proto received\n");
572 p(ifs6_in_truncated
, "\t%llu truncated datagram%s received\n");
573 p(ifs6_in_discard
, "\t%llu input datagram%s discarded\n");
575 "\t%llu datagram%s delivered to an upper layer protocol\n");
576 p(ifs6_out_forward
, "\t%llu datagram%s forwarded to this interface\n");
578 "\t%llu datagram%s sent from an upper layer protocol\n");
579 p(ifs6_out_discard
, "\t%llu total discarded output datagram%s\n");
580 p(ifs6_out_fragok
, "\t%llu output datagram%s fragmented\n");
581 p(ifs6_out_fragfail
, "\t%llu output datagram%s failed on fragment\n");
582 p(ifs6_out_fragcreat
, "\t%llu output datagram%s succeeded on fragment\n");
583 p(ifs6_reass_reqd
, "\t%llu incoming datagram%s fragmented\n");
584 p(ifs6_reass_ok
, "\t%llu datagram%s reassembled\n");
585 p(ifs6_reass_fail
, "\t%llu datagram%s failed on reassembling\n");
586 p(ifs6_in_mcast
, "\t%llu multicast datagram%s received\n");
587 p(ifs6_out_mcast
, "\t%llu multicast datagram%s sent\n");
596 static char *icmp6names
[] = {
727 "multicast listener query",
728 "MLDv1 listener report",
729 "MLDv1 listener done",
730 "router solicitation",
731 "router advertisement",
732 "neighbor solicitation",
733 "neighbor advertisement",
735 "router renumbering",
736 "node information request",
737 "node information reply",
738 "inverse neighbor solicitation",
739 "inverse neighbor advertisement",
740 "MLDv2 listener report",
856 * Dump ICMP6 statistics.
859 icmp6_stats(uint32_t off __unused
, char *name
, int af __unused
)
861 static struct icmp6stat picmp6stat
;
862 struct icmp6stat icmp6stat
;
863 register int i
, first
;
869 mib
[2] = IPPROTO_ICMPV6
;
870 mib
[3] = ICMPV6CTL_STATS
;
872 len
= sizeof icmp6stat
;
873 memset(&icmp6stat
, 0, len
);
874 if (sysctl(mib
, 4, &icmp6stat
, &len
, (void *)0, 0) < 0)
876 if (interval
&& vflag
> 0)
878 printf("%s:\n", name
);
880 #define ICMP6DIFF(f) (icmp6stat.f - picmp6stat.f)
881 #define p(f, m) if (ICMP6DIFF(f) || sflag <= 1) \
882 printf(m, (unsigned long long)ICMP6DIFF(f), plural(ICMP6DIFF(f)))
883 #define p_5(f, m) printf(m, (unsigned long long)ICMP6DIFF(f))
885 p(icp6s_error
, "\t%llu call%s to icmp_error\n");
887 "\t%llu error%s not generated because old message was icmp error or so\n");
889 "\t%llu error%s not generated because rate limitation\n");
890 #define NELEM (sizeof(icmp6stat.icp6s_outhist)/sizeof(icmp6stat.icp6s_outhist[0]))
891 for (first
= 1, i
= 0; i
< NELEM
; i
++)
892 if (ICMP6DIFF(icp6s_outhist
[i
]) != 0) {
894 printf("\tOutput histogram:\n");
897 printf("\t\t%s: %llu\n", icmp6names
[i
],
898 (unsigned long long)ICMP6DIFF(icp6s_outhist
[i
]));
901 p(icp6s_badcode
, "\t%llu message%s with bad code fields\n");
902 p(icp6s_tooshort
, "\t%llu message%s < minimum length\n");
903 p(icp6s_checksum
, "\t%llu bad checksum%s\n");
904 p(icp6s_badlen
, "\t%llu message%s with bad length\n");
905 #define NELEM (sizeof(icmp6stat.icp6s_inhist)/sizeof(icmp6stat.icp6s_inhist[0]))
906 for (first
= 1, i
= 0; i
< NELEM
; i
++)
907 if (ICMP6DIFF(icp6s_inhist
[i
]) != 0) {
909 printf("\tInput histogram:\n");
912 printf("\t\t%s: %llu\n", icmp6names
[i
],
913 (unsigned long long)ICMP6DIFF(icp6s_inhist
[i
]));
916 printf("\tHistogram of error messages to be generated:\n");
917 p_5(icp6s_odst_unreach_noroute
, "\t\t%llu no route\n");
918 p_5(icp6s_odst_unreach_admin
, "\t\t%llu administratively prohibited\n");
919 p_5(icp6s_odst_unreach_beyondscope
, "\t\t%llu beyond scope\n");
920 p_5(icp6s_odst_unreach_addr
, "\t\t%llu address unreachable\n");
921 p_5(icp6s_odst_unreach_noport
, "\t\t%llu port unreachable\n");
922 p_5(icp6s_opacket_too_big
, "\t\t%llu packet too big\n");
923 p_5(icp6s_otime_exceed_transit
, "\t\t%llu time exceed transit\n");
924 p_5(icp6s_otime_exceed_reassembly
, "\t\t%llu time exceed reassembly\n");
925 p_5(icp6s_oparamprob_header
, "\t\t%llu erroneous header field\n");
926 p_5(icp6s_oparamprob_nextheader
, "\t\t%llu unrecognized next header\n");
927 p_5(icp6s_oparamprob_option
, "\t\t%llu unrecognized option\n");
928 p_5(icp6s_oredirect
, "\t\t%llu redirect\n");
929 p_5(icp6s_ounknown
, "\t\t%llu unknown\n");
931 p(icp6s_reflect
, "\t%llu message response%s generated\n");
932 p(icp6s_nd_toomanyopt
, "\t%llu message%s with too many ND options\n");
933 p(icp6s_nd_badopt
, "\t%qu message%s with bad ND options\n");
934 p(icp6s_badns
, "\t%qu bad neighbor solicitation message%s\n");
935 p(icp6s_badna
, "\t%qu bad neighbor advertisement message%s\n");
936 p(icp6s_badrs
, "\t%qu bad router solicitation message%s\n");
937 p(icp6s_badra
, "\t%qu bad router advertisement message%s\n");
938 p(icp6s_badredirect
, "\t%qu bad redirect message%s\n");
939 p(icp6s_pmtuchg
, "\t%llu path MTU change%s\n");
942 bcopy(&icmp6stat
, &picmp6stat
, len
);
950 * Dump ICMPv6 per-interface statistics based on RFC 2466.
953 icmp6_ifstats(char *ifname
)
955 struct in6_ifreq ifr
;
957 #define p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
958 printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, plural(ifr.ifr_ifru.ifru_icmp6stat.f))
960 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
961 perror("Warning: socket(AF_INET6)");
965 if (interval
&& vflag
> 0)
967 strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
968 printf("icmp6 on %s:\n", ifr
.ifr_name
);
970 if (ioctl(s
, SIOCGIFSTAT_ICMP6
, (char *)&ifr
) < 0) {
971 perror("Warning: ioctl(SIOCGIFSTAT_ICMP6)");
975 p(ifs6_in_msg
, "\t%llu total input message%s\n");
976 p(ifs6_in_error
, "\t%llu total input error message%s\n");
977 p(ifs6_in_dstunreach
, "\t%llu input destination unreachable error%s\n");
978 p(ifs6_in_adminprohib
, "\t%llu input administratively prohibited error%s\n");
979 p(ifs6_in_timeexceed
, "\t%llu input time exceeded error%s\n");
980 p(ifs6_in_paramprob
, "\t%llu input parameter problem error%s\n");
981 p(ifs6_in_pkttoobig
, "\t%llu input packet too big error%s\n");
982 p(ifs6_in_echo
, "\t%llu input echo request%s\n");
983 p(ifs6_in_echoreply
, "\t%llu input echo reply%s\n");
984 p(ifs6_in_routersolicit
, "\t%llu input router solicitation%s\n");
985 p(ifs6_in_routeradvert
, "\t%llu input router advertisement%s\n");
986 p(ifs6_in_neighborsolicit
, "\t%llu input neighbor solicitation%s\n");
987 p(ifs6_in_neighboradvert
, "\t%llu input neighbor advertisement%s\n");
988 p(ifs6_in_redirect
, "\t%llu input redirect%s\n");
989 p(ifs6_in_mldquery
, "\t%llu input MLD query%s\n");
990 p(ifs6_in_mldreport
, "\t%llu input MLD report%s\n");
991 p(ifs6_in_mlddone
, "\t%llu input MLD done%s\n");
993 p(ifs6_out_msg
, "\t%llu total output message%s\n");
994 p(ifs6_out_error
, "\t%llu total output error message%s\n");
995 p(ifs6_out_dstunreach
, "\t%llu output destination unreachable error%s\n");
996 p(ifs6_out_adminprohib
, "\t%llu output administratively prohibited error%s\n");
997 p(ifs6_out_timeexceed
, "\t%llu output time exceeded error%s\n");
998 p(ifs6_out_paramprob
, "\t%llu output parameter problem error%s\n");
999 p(ifs6_out_pkttoobig
, "\t%llu output packet too big error%s\n");
1000 p(ifs6_out_echo
, "\t%llu output echo request%s\n");
1001 p(ifs6_out_echoreply
, "\t%llu output echo reply%s\n");
1002 p(ifs6_out_routersolicit
, "\t%llu output router solicitation%s\n");
1003 p(ifs6_out_routeradvert
, "\t%llu output router advertisement%s\n");
1004 p(ifs6_out_neighborsolicit
, "\t%llu output neighbor solicitation%s\n");
1005 p(ifs6_out_neighboradvert
, "\t%llu output neighbor advertisement%s\n");
1006 p(ifs6_out_redirect
, "\t%llu output redirect%s\n");
1007 p(ifs6_out_mldquery
, "\t%llu output MLD query%s\n");
1008 p(ifs6_out_mldreport
, "\t%llu output MLD report%s\n");
1009 p(ifs6_out_mlddone
, "\t%llu output MLD done%s\n");
1017 * Dump PIM statistics structure.
1023 static struct pim6stat ppim6stat
;
1024 struct pim6stat pim6stat
;
1025 size_t len
= sizeof(struct pim6stat
);
1027 if (sysctlbyname("net.inet6.ip6.pim6stat", &pim6stat
, &len
, 0, 0) == -1)
1029 if (interval
&& vflag
> 0)
1031 printf("%s:\n", name
);
1033 #define PIM6DIFF(f) (pim6stat.f - ppim6stat.f)
1034 #define p(f, m) if (PIM6DIFF(f) || sflag <= 1) \
1035 printf(m, (unsigned long long)PIM6DIFF(f), plural(PIM6DIFF(f)))
1036 p(pim6s_rcv_total
, "\t%llu message%s received\n");
1037 p(pim6s_rcv_tooshort
, "\t%llu message%s received with too few bytes\n");
1038 p(pim6s_rcv_badsum
, "\t%llu message%s received with bad checksum\n");
1039 p(pim6s_rcv_badversion
, "\t%llu message%s received with bad version\n");
1040 p(pim6s_rcv_registers
, "\t%llu register%s received\n");
1041 p(pim6s_rcv_badregisters
, "\t%llu bad register%s received\n");
1042 p(pim6s_snd_registers
, "\t%llu register%s sent\n");
1045 bcopy(&pim6stat
, &ppim6stat
, len
);
1053 * Dump raw ip6 statistics structure.
1056 rip6_stats(uint32_t off __unused
, char *name
, int af __unused
)
1058 static struct rip6stat prip6stat
;
1059 struct rip6stat rip6stat
;
1066 mib
[2] = IPPROTO_IPV6
;
1067 mib
[3] = IPV6CTL_RIP6STATS
;
1068 l
= sizeof(rip6stat
);
1069 if (sysctl(mib
, 4, &rip6stat
, &l
, NULL
, 0) < 0) {
1070 perror("Warning: sysctl(net.inet6.ip6.rip6stats)");
1074 if (interval
&& vflag
> 0)
1076 printf("%s:\n", name
);
1078 #define RIP6DIFF(f) (rip6stat.f - prip6stat.f)
1079 #define p(f, m) if (RIP6DIFF(f) || sflag <= 1) \
1080 printf(m, (unsigned long long)RIP6DIFF(f), plural(RIP6DIFF(f)))
1081 p(rip6s_ipackets
, "\t%llu message%s received\n");
1082 p(rip6s_isum
, "\t%llu checksum calcuration%s on inbound\n");
1083 p(rip6s_badsum
, "\t%llu message%s with bad checksum\n");
1084 p(rip6s_nosock
, "\t%llu message%s dropped due to no socket\n");
1085 p(rip6s_nosockmcast
,
1086 "\t%llu multicast message%s dropped due to no socket\n");
1088 "\t%llu message%s dropped due to full socket buffers\n");
1089 delivered
= RIP6DIFF(rip6s_ipackets
) -
1090 RIP6DIFF(rip6s_badsum
) -
1091 RIP6DIFF(rip6s_nosock
) -
1092 RIP6DIFF(rip6s_nosockmcast
) -
1093 RIP6DIFF(rip6s_fullsock
);
1094 if (delivered
|| sflag
<= 1)
1095 printf("\t%llu delivered\n", (unsigned long long)delivered
);
1096 p(rip6s_opackets
, "\t%llu datagram%s output\n");
1099 bcopy(&rip6stat
, &prip6stat
, l
);
1106 * Pretty print an Internet address (net address + port).
1107 * If the nflag was specified, use numbers instead of names.
1110 extern struct servent
* _serv_cache_getservbyport(int port
, char *proto
);
1112 #define GETSERVBYPORT6(port, proto, ret)\
1114 if (strcmp((proto), "tcp6") == 0)\
1115 (ret) = _serv_cache_getservbyport((int)(port), "tcp");\
1116 else if (strcmp((proto), "udp6") == 0)\
1117 (ret) = _serv_cache_getservbyport((int)(port), "udp");\
1119 (ret) = _serv_cache_getservbyport((int)(port), (proto));\
1122 #define GETSERVBYPORT6(port, proto, ret)\
1124 if (strcmp((proto), "tcp6") == 0)\
1125 (ret) = getservbyport((int)(port), "tcp");\
1126 else if (strcmp((proto), "udp6") == 0)\
1127 (ret) = getservbyport((int)(port), "udp");\
1129 (ret) = getservbyport((int)(port), (proto));\
1133 inet6print(struct in6_addr
*in6
, int port
, char *proto
, int numeric
)
1135 struct servent
*sp
= 0;
1139 snprintf(line
, sizeof(line
), "%.*s.", lflag
? 39 :
1140 (Aflag
&& !numeric
) ? 12 : 16, inet6name(in6
));
1141 cp
= index(line
, '\0');
1142 if (!numeric
&& port
)
1143 GETSERVBYPORT6(port
, proto
, sp
);
1144 if (sp
|| port
== 0)
1145 snprintf(cp
, sizeof(line
) - (cp
- line
), "%.8s", sp
? sp
->s_name
: "*");
1147 snprintf(cp
, sizeof(line
) - (cp
- line
), "%d", ntohs((u_short
)port
));
1148 width
= lflag
? 45 : Aflag
? 18 : 22;
1149 printf("%-*.*s ", width
, width
, line
);
1153 * Construct an Internet address representation.
1154 * If the nflag has been supplied, give
1155 * numeric value, otherwise try for symbolic name.
1159 inet6name(struct in6_addr
*in6p
)
1162 static char line
[50];
1164 static char domain
[MAXHOSTNAMELEN
];
1165 static int first
= 1;
1166 char hbuf
[NI_MAXHOST
];
1167 struct sockaddr_in6 sin6
;
1168 const int niflag
= NI_NUMERICHOST
;
1170 if (first
&& !nflag
) {
1172 if (gethostname(domain
, MAXHOSTNAMELEN
) == 0 &&
1173 (cp
= index(domain
, '.')))
1174 (void) memmove(domain
, cp
+ 1, strlen(cp
+ 1) + 1);
1179 if (!nflag
&& !IN6_IS_ADDR_UNSPECIFIED(in6p
)) {
1180 hp
= gethostbyaddr((char *)in6p
, sizeof(*in6p
), AF_INET6
);
1182 if ((cp
= index(hp
->h_name
, '.')) &&
1183 !strcmp(cp
+ 1, domain
))
1188 if (IN6_IS_ADDR_UNSPECIFIED(in6p
))
1189 strlcpy(line
, "*", sizeof(line
));
1191 strlcpy(line
, cp
, sizeof(line
));
1193 memset(&sin6
, 0, sizeof(sin6
));
1194 sin6
.sin6_len
= sizeof(sin6
);
1195 sin6
.sin6_family
= AF_INET6
;
1196 sin6
.sin6_addr
= *in6p
;
1198 if (IN6_IS_ADDR_LINKLOCAL(in6p
) ||
1199 IN6_IS_ADDR_MC_NODELOCAL(in6p
) ||
1200 IN6_IS_ADDR_MC_LINKLOCAL(in6p
)) {
1201 sin6
.sin6_scope_id
=
1202 ntohs(*(u_int16_t
*)&in6p
->s6_addr
[2]);
1203 sin6
.sin6_addr
.s6_addr
[2] = 0;
1204 sin6
.sin6_addr
.s6_addr
[3] = 0;
1207 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
1208 hbuf
, sizeof(hbuf
), NULL
, 0, niflag
) != 0)
1209 strlcpy(hbuf
, "?", sizeof(hbuf
));
1210 strlcpy(line
, hbuf
, sizeof(line
));