1 /* $KAME: ifmcstat.c,v 1.48 2006/11/15 05:13:59 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2007-2009 Bruce Simpson.
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project 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.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
43 #include <net/if_types.h>
44 #include <net/if_dl.h>
45 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/igmp.h>
52 #include <netinet/if_ether.h>
53 #include <netinet/igmp_var.h>
56 #include <netinet/icmp6.h>
57 #include <netinet6/mld6_var.h>
60 #include <arpa/inet.h>
81 * Currently the KVM build is broken. To be fixed it requires uncovering
82 * large amount of _KERNEL code in include files, and it is also very
83 * tentative to internal kernel ABI changes. If anyone wishes to restore
84 * it, please move it out of src/usr.sbin to src/tools/tools.
90 /* XXX: This file currently assumes INET support in the base system. */
95 extern void printb(const char *, unsigned int, const char *);
98 struct sockaddr_storage ss;
100 struct sockaddr_dl sdl;
102 struct sockaddr_in sin;
105 struct sockaddr_in6 sin6;
108 typedef union sockunion sockunion_t;
110 uint32_t ifindex = 0;
117 #define sa_dl_equal(a1, a2) \
118 ((((struct sockaddr_dl *)(a1))->sdl_len == \
119 ((struct sockaddr_dl *)(a2))->sdl_len) && \
120 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
121 LLADDR((struct sockaddr_dl *)(a2)), \
122 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
125 * Most of the code in this utility is to support the use of KVM for
126 * post-mortem debugging of the multicast code.
131 static void if_addrlist(struct ifaddr *);
132 static struct in_multi *
133 in_multientry(struct in_multi *);
137 static void if6_addrlist(struct ifaddr *);
138 static struct in6_multi *
139 in6_multientry(struct in6_multi *);
142 static void kread(u_long, void *, int);
143 static void ll_addrlist(struct ifaddr *);
145 static int ifmcstat_kvm(const char *kernel, const char *core);
147 #define KREAD(addr, buf, type) \
148 kread((u_long)addr, (void *)buf, sizeof(type))
151 struct nlist nl[] = {
152 { "_ifnet", 0, 0, 0, 0, },
157 #endif /* WITH_KVM */
159 static int ifmcstat_getifmaddrs(void);
161 static void in_ifinfo(struct igmp_ifinfo *);
162 static const char * inm_mode(u_int mode);
165 static void in6_ifinfo(struct mld_ifinfo *);
166 static const char * inet6_n2a(struct in6_addr *, uint32_t);
168 int main(int, char **);
175 "usage: ifmcstat [-i interface] [-f address family]"
178 " [-K] [-M core] [-N system]"
184 static const char *options = "i:f:vM:N:"
191 main(int argc, char **argv)
195 const char *kernel = NULL;
196 const char *core = NULL;
199 while ((c = getopt(argc, argv, options)) != -1) {
202 if ((ifindex = if_nametoindex(optarg)) == 0) {
203 fprintf(stderr, "%s: unknown interface\n",
211 if (strcmp(optarg, "inet") == 0) {
217 if (strcmp(optarg, "inet6") == 0) {
222 if (strcmp(optarg, "link") == 0) {
226 fprintf(stderr, "%s: unknown address family\n", optarg);
243 core = strdup(optarg);
247 kernel = strdup(optarg);
258 if (af == AF_LINK && vflag)
263 error = ifmcstat_kvm(kernel, core);
265 * If KVM failed, and user did not explicitly specify a core file,
266 * or force KVM backend to be disabled, try the sysctl backend.
268 if (!Kflag || (error != 0 && (core == NULL && kernel == NULL)))
270 error = ifmcstat_getifmaddrs();
281 in_ifinfo(struct igmp_ifinfo *igi)
285 switch (igi->igi_version) {
289 printf("igmpv%d", igi->igi_version);
292 printf("igmpv?(%d)", igi->igi_version);
296 printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
297 if (igi->igi_version == IGMP_VERSION_3) {
298 printf(" rv %u qi %u qri %u uri %u",
299 igi->igi_rv, igi->igi_qi, igi->igi_qri, igi->igi_uri);
302 printf(" v1timer %u v2timer %u v3timer %u",
303 igi->igi_v1_timer, igi->igi_v2_timer, igi->igi_v3_timer);
308 static const char *inm_modes[] = {
318 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
319 return (inm_modes[mode]);
328 ifmcstat_kvm(const char *kernel, const char *core)
330 char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
331 struct ifnet *ifp, *nifp, ifnet;
333 if ((kvmd = kvm_openfiles(kernel, core, NULL, O_RDONLY, buf)) ==
335 perror("kvm_openfiles");
338 if (kvm_nlist(kvmd, nl) < 0) {
342 if (nl[N_IFNET].n_value == 0) {
343 printf("symbol %s not found\n", nl[N_IFNET].n_name);
346 KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
348 KREAD(ifp, &ifnet, struct ifnet);
349 nifp = ifnet.if_link.tqe_next;
350 if (ifindex && ifindex != ifnet.if_index)
353 printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
355 if_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
358 if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
361 ll_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
370 kread(u_long addr, void *buf, int len)
373 if (kvm_read(kvmd, addr, buf, len) != len) {
380 ll_addrlist(struct ifaddr *ifap)
382 char addrbuf[NI_MAXHOST];
385 struct sockaddr_dl sdl;
386 struct ifaddr *ifap0;
388 if (af && af != AF_LINK)
393 KREAD(ifap, &ifa, struct ifaddr);
394 if (ifa.ifa_addr == NULL)
396 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
397 if (sa.sa_family != PF_LINK)
399 KREAD(ifa.ifa_addr, &sdl, struct sockaddr_dl);
400 if (sdl.sdl_alen == 0)
403 getnameinfo((struct sockaddr *)&sdl, sdl.sdl_len,
404 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
405 printf("\tlink %s\n", addrbuf);
407 ifap = ifa.ifa_link.tqe_next;
411 struct ifmultiaddr ifm, *ifmp = 0;
413 KREAD(ifap0, &ifa, struct ifaddr);
414 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
415 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
416 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
418 KREAD(ifmp, &ifm, struct ifmultiaddr);
419 if (ifm.ifma_addr == NULL)
421 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
422 if (sa.sa_family != AF_LINK)
424 KREAD(ifm.ifma_addr, &sdl, struct sockaddr_dl);
426 getnameinfo((struct sockaddr *)&sdl,
427 sdl.sdl_len, addrbuf, sizeof(addrbuf),
428 NULL, 0, NI_NUMERICHOST);
429 printf("\t\tgroup %s refcnt %d\n",
430 addrbuf, ifm.ifma_refcount);
432 ifmp = TAILQ_NEXT(&ifm, ifma_link);
440 if6_addrlist(struct ifaddr *ifap)
445 struct in6_ifaddr if6a;
446 struct ifaddr *ifap0;
448 if (af && af != AF_INET6)
452 KREAD(ifap, &ifa, struct ifaddr);
453 if (ifa.ifa_addr == NULL)
455 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
456 if (sa.sa_family != PF_INET6)
458 KREAD(ifap, &if6a, struct in6_ifaddr);
459 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr,
460 if6a.ia_addr.sin6_scope_id));
462 * Print per-link MLD information, if available.
464 if (ifa.ifa_ifp != NULL) {
465 struct in6_ifextra ie;
466 struct mld_ifinfo mli;
468 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
469 KREAD(ifnet.if_afdata[AF_INET6], &ie,
471 if (ie.mld_ifinfo != NULL) {
472 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
477 ifap = ifa.ifa_link.tqe_next;
481 struct ifmultiaddr ifm, *ifmp = 0;
482 struct sockaddr_dl sdl;
484 KREAD(ifap0, &ifa, struct ifaddr);
485 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
486 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
487 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
489 KREAD(ifmp, &ifm, struct ifmultiaddr);
490 if (ifm.ifma_addr == NULL)
492 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
493 if (sa.sa_family != AF_INET6)
495 (void)in6_multientry((struct in6_multi *)
497 if (ifm.ifma_lladdr == 0)
499 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
500 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
501 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
504 ifmp = TAILQ_NEXT(&ifm, ifma_link);
509 static struct in6_multi *
510 in6_multientry(struct in6_multi *mc)
512 struct in6_multi multi;
514 KREAD(mc, &multi, struct in6_multi);
515 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr, 0));
516 printf(" refcnt %u\n", multi.in6m_refcount);
518 return (multi.in6m_entry.le_next);
526 if_addrlist(struct ifaddr *ifap)
532 struct ifaddr *ifap0;
534 if (af && af != AF_INET)
538 KREAD(ifap, &ifa, struct ifaddr);
539 if (ifa.ifa_addr == NULL)
541 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
542 if (sa.sa_family != PF_INET)
544 KREAD(ifap, &ia, struct in_ifaddr);
545 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
547 * Print per-link IGMP information, if available.
549 if (ifa.ifa_ifp != NULL) {
551 struct igmp_ifinfo igi;
553 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
554 KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
555 if (ii.ii_igmp != NULL) {
556 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
561 ifap = ifa.ifa_link.tqe_next;
564 struct ifmultiaddr ifm, *ifmp = 0;
565 struct sockaddr_dl sdl;
567 KREAD(ifap0, &ifa, struct ifaddr);
568 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
569 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
570 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
572 KREAD(ifmp, &ifm, struct ifmultiaddr);
573 if (ifm.ifma_addr == NULL)
575 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
576 if (sa.sa_family != AF_INET)
578 (void)in_multientry((struct in_multi *)
580 if (ifm.ifma_lladdr == 0)
582 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
583 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
584 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
587 ifmp = TAILQ_NEXT(&ifm, ifma_link);
592 static const char *inm_states[] = {
605 inm_state(u_int state)
608 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
609 return (inm_states[state]);
614 static struct ip_msource *
615 ims_min_kvm(struct in_multi *pinm)
617 struct ip_msource ims0;
618 struct ip_msource *tmp, *parent;
621 tmp = RB_ROOT(&pinm->inm_srcs);
624 KREAD(tmp, &ims0, struct ip_msource);
625 tmp = RB_LEFT(&ims0, ims_link);
627 return (parent); /* kva */
630 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
631 static struct ip_msource *
632 ims_next_kvm(struct ip_msource *ims)
634 struct ip_msource ims0, ims1;
635 struct ip_msource *tmp;
637 KREAD(ims, &ims0, struct ip_msource);
638 if (RB_RIGHT(&ims0, ims_link)) {
639 ims = RB_RIGHT(&ims0, ims_link);
640 KREAD(ims, &ims1, struct ip_msource);
641 while ((tmp = RB_LEFT(&ims1, ims_link))) {
642 KREAD(tmp, &ims0, struct ip_msource);
643 ims = RB_LEFT(&ims0, ims_link);
646 tmp = RB_PARENT(&ims0, ims_link);
648 KREAD(tmp, &ims1, struct ip_msource);
649 if (ims == RB_LEFT(&ims1, ims_link))
652 while ((tmp = RB_PARENT(&ims0, ims_link))) {
653 KREAD(tmp, &ims1, struct ip_msource);
654 if (ims == RB_RIGHT(&ims1, ims_link)) {
656 KREAD(ims, &ims0, struct ip_msource);
660 ims = RB_PARENT(&ims0, ims_link);
663 return (ims); /* kva */
667 inm_print_sources_kvm(struct in_multi *pinm)
669 struct ip_msource ims0;
670 struct ip_msource *ims;
676 fmode = pinm->inm_st[1].iss_fmode;
677 if (fmode == MCAST_UNDEFINED)
679 for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
682 KREAD(ims, &ims0, struct ip_msource);
683 /* Only print sources in-mode at t1. */
684 if (fmode != ims_get_mode(pinm, ims, 1))
686 src.s_addr = htonl(ims0.ims_haddr);
687 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
692 static struct in_multi *
693 in_multientry(struct in_multi *pinm)
696 const char *state, *mode;
698 KREAD(pinm, &inm, struct in_multi);
699 printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
700 printf(" refcnt %u", inm.inm_refcount);
702 state = inm_state(inm.inm_state);
704 printf(" state %s", state);
706 printf(" state (%d)", inm.inm_state);
708 mode = inm_mode(inm.inm_st[1].iss_fmode);
710 printf(" mode %s", mode);
712 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
715 printf(" asm %u ex %u in %u rec %u",
716 (u_int)inm.inm_st[1].iss_asm,
717 (u_int)inm.inm_st[1].iss_ex,
718 (u_int)inm.inm_st[1].iss_in,
719 (u_int)inm.inm_st[1].iss_rec);
725 inm_print_sources_kvm(&inm);
734 #endif /* WITH_KVM */
739 in6_ifinfo(struct mld_ifinfo *mli)
743 switch (mli->mli_version) {
746 printf("mldv%d", mli->mli_version);
749 printf("mldv?(%d)", mli->mli_version);
753 printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
754 if (mli->mli_version == MLD_VERSION_2) {
755 printf(" rv %u qi %u qri %u uri %u",
756 mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
759 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
766 inet6_n2a(struct in6_addr *p, uint32_t scope_id)
768 static char buf[NI_MAXHOST];
769 struct sockaddr_in6 sin6;
770 const int niflags = NI_NUMERICHOST;
772 memset(&sin6, 0, sizeof(sin6));
773 sin6.sin6_family = AF_INET6;
774 sin6.sin6_len = sizeof(struct sockaddr_in6);
776 sin6.sin6_scope_id = scope_id;
777 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
778 buf, sizeof(buf), NULL, 0, niflags) == 0) {
781 return ("(invalid)");
788 * Retrieve per-group source filter mode and lists via sysctl.
791 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
793 #define MAX_SYSCTL_TRY 5
802 struct in_addr *pina;
807 mibsize = nitems(mib);
808 if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
809 perror("sysctlnametomib");
815 mib[6] = gina.s_addr; /* 32 bits wide */
816 mibsize = nitems(mib);
818 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
819 perror("sysctl net.inet.ip.mcast.filters");
822 if ((buf = malloc(needed)) == NULL) {
826 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
827 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
834 } while (buf == NULL);
837 if (len < sizeof(uint32_t)) {
844 len -= sizeof(uint32_t);
846 modestr = inm_mode(fmode);
848 printf(" mode %s", modestr);
850 printf(" mode (%u)", fmode);
855 cnt = len / sizeof(struct in_addr);
856 pina = (struct in_addr *)p;
858 for (i = 0; i < cnt; i++) {
861 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
863 len -= sizeof(struct in_addr);
866 fprintf(stderr, "warning: %u trailing bytes from %s\n",
867 (unsigned int)len, "net.inet.ip.mcast.filters");
872 #undef MAX_SYSCTL_TRY
879 * Retrieve MLD per-group source filter mode and lists via sysctl.
881 * Note: The 128-bit IPv6 group address needs to be segmented into
882 * 32-bit pieces for marshaling to sysctl. So the MIB name ends
883 * up looking like this:
884 * a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
885 * Assumes that pgroup originated from the kernel, so its components
886 * are already in network-byte order.
889 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
891 #define MAX_SYSCTL_TRY 5
892 char addrbuf[INET6_ADDRSTRLEN];
902 struct in6_addr *pina;
907 mibsize = nitems(mib);
908 if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
910 perror("sysctlnametomib");
917 for (i = 0; i < 4; i++)
920 mibsize = nitems(mib);
922 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
923 perror("sysctl net.inet6.ip6.mcast.filters");
926 if ((buf = malloc(needed)) == NULL) {
930 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
931 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
938 } while (buf == NULL);
941 if (len < sizeof(uint32_t)) {
948 len -= sizeof(uint32_t);
950 modestr = inm_mode(fmode);
952 printf(" mode %s", modestr);
954 printf(" mode (%u)", fmode);
959 cnt = len / sizeof(struct in6_addr);
960 pina = (struct in6_addr *)p;
962 for (i = 0; i < cnt; i++) {
965 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
967 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
968 len -= sizeof(struct in6_addr);
971 fprintf(stderr, "warning: %u trailing bytes from %s\n",
972 (unsigned int)len, "net.inet6.ip6.mcast.filters");
977 #undef MAX_SYSCTL_TRY
982 ifmcstat_getifmaddrs(void)
984 char thisifname[IFNAMSIZ];
985 char addrbuf[NI_MAXHOST];
986 struct ifaddrs *ifap, *ifa;
987 struct ifmaddrs *ifmap, *ifma;
988 sockunion_t lastifasa;
989 sockunion_t *psa, *pgsa, *pllsa, *pifasa;
992 uint32_t lastifindex, thisifindex;
1000 lastifasa.ss.ss_family = AF_UNSPEC;
1002 if (getifaddrs(&ifap) != 0) {
1003 warn("getifmaddrs");
1007 if (getifmaddrs(&ifmap) != 0) {
1008 warn("getifmaddrs");
1013 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1015 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1018 psa = (sockunion_t *)ifma->ifma_name;
1019 if (psa->sa.sa_family != AF_LINK) {
1021 "WARNING: Kernel returned invalid data.\n");
1026 /* Filter on interface name. */
1027 thisifindex = psa->sdl.sdl_index;
1028 if (ifindex != 0 && thisifindex != ifindex)
1031 /* Filter on address family. */
1032 pgsa = (sockunion_t *)ifma->ifma_addr;
1033 if (af != 0 && pgsa->sa.sa_family != af)
1036 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1037 pcolon = strchr(thisifname, ':');
1041 /* Only print the banner for the first ifmaddrs entry. */
1042 if (lastifindex == 0 || lastifindex != thisifindex) {
1043 lastifindex = thisifindex;
1044 fprintf(stdout, "%s:\n", thisifname);
1048 * Currently, multicast joins only take place on the
1049 * primary IPv4 address, and only on the link-local IPv6
1050 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1051 * Therefore, we only look up the primary address on
1055 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1056 if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1057 (ifa->ifa_addr == NULL) ||
1058 (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1061 * For AF_INET6 only the link-local address should
1062 * be returned. If built without IPv6 support,
1063 * skip this address entirely.
1065 pifasa = (sockunion_t *)ifa->ifa_addr;
1066 if (pifasa->sa.sa_family == AF_INET6
1068 && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1077 continue; /* primary address not found */
1079 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1082 /* Parse and print primary address, if not already printed. */
1083 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1084 ((lastifasa.ss.ss_family == AF_LINK &&
1085 !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1086 !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1088 switch (pifasa->sa.sa_family) {
1099 pafname = "unknown";
1103 switch (pifasa->sa.sa_family) {
1108 inet6_n2a(&pifasa->sin6.sin6_addr,
1109 pifasa->sin6.sin6_scope_id);
1110 strlcpy(addrbuf, p, sizeof(addrbuf));
1118 error = getnameinfo(&pifasa->sa,
1120 addrbuf, sizeof(addrbuf), NULL, 0,
1123 perror("getnameinfo");
1130 fprintf(stdout, "\t%s %s", pafname, addrbuf);
1132 if (pifasa->sa.sa_family == AF_INET6 &&
1133 pifasa->sin6.sin6_scope_id)
1134 fprintf(stdout, " scopeid 0x%x",
1135 pifasa->sin6.sin6_scope_id);
1137 fprintf(stdout, "\n");
1140 * Print per-link IGMP information, if available.
1142 if (pifasa->sa.sa_family == AF_INET) {
1143 struct igmp_ifinfo igi;
1144 size_t mibsize, len;
1147 mibsize = nitems(mib);
1148 if (sysctlnametomib("net.inet.igmp.ifinfo",
1149 mib, &mibsize) == -1) {
1150 perror("sysctlnametomib");
1153 mib[mibsize] = thisifindex;
1154 len = sizeof(struct igmp_ifinfo);
1155 if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1157 perror("sysctl net.inet.igmp.ifinfo");
1165 * Print per-link MLD information, if available.
1167 if (pifasa->sa.sa_family == AF_INET6) {
1168 struct mld_ifinfo mli;
1169 size_t mibsize, len;
1172 mibsize = nitems(mib);
1173 if (sysctlnametomib("net.inet6.mld.ifinfo",
1174 mib, &mibsize) == -1) {
1175 perror("sysctlnametomib");
1178 mib[mibsize] = thisifindex;
1179 len = sizeof(struct mld_ifinfo);
1180 if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1182 perror("sysctl net.inet6.mld.ifinfo");
1188 #if defined(INET) || defined(INET6)
1191 lastifasa = *pifasa;
1194 /* Print this group address. */
1196 if (pgsa->sa.sa_family == AF_INET6) {
1197 const char *p = inet6_n2a(&pgsa->sin6.sin6_addr,
1198 pgsa->sin6.sin6_scope_id);
1199 strlcpy(addrbuf, p, sizeof(addrbuf));
1203 error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1204 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1206 perror("getnameinfo");
1209 fprintf(stdout, "\t\tgroup %s", addrbuf);
1211 if (pgsa->sa.sa_family == AF_INET6 &&
1212 pgsa->sin6.sin6_scope_id)
1213 fprintf(stdout, " scopeid 0x%x",
1214 pgsa->sin6.sin6_scope_id);
1217 if (pgsa->sa.sa_family == AF_INET) {
1218 inm_print_sources_sysctl(thisifindex,
1219 pgsa->sin.sin_addr);
1223 if (pgsa->sa.sa_family == AF_INET6) {
1224 in6m_print_sources_sysctl(thisifindex,
1225 &pgsa->sin6.sin6_addr);
1228 fprintf(stdout, "\n");
1230 /* Link-layer mapping, if present. */
1231 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1232 if (pllsa != NULL) {
1233 error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1234 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1235 fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1240 freeifmaddrs(ifmap);