1 /* $KAME: ifmcstat.c,v 1.48 2006/11/15 05:13:59 itojun Exp $ */
4 * Copyright (c) 2007-2009 Bruce Simpson.
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/socket.h>
40 #include <sys/queue.h>
44 #include <net/if_var.h>
45 #include <net/if_types.h>
46 #include <net/if_dl.h>
47 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/igmp.h>
55 # include <netinet/if_ether.h>
58 #define SYSCTL_DECL(x)
59 # include <netinet/igmp_var.h>
64 #include <netinet/icmp6.h>
66 # include <netinet6/mld6_var.h>
70 #include <arpa/inet.h>
91 /* XXX: This file currently assumes INET and KVM support in the base system. */
96 extern void printb(const char *, unsigned int, const char *);
99 struct sockaddr_storage ss;
101 struct sockaddr_dl sdl;
103 struct sockaddr_in sin;
106 struct sockaddr_in6 sin6;
109 typedef union sockunion sockunion_t;
111 uint32_t ifindex = 0;
118 #define sa_equal(a1, a2) \
119 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
121 #define sa_dl_equal(a1, a2) \
122 ((((struct sockaddr_dl *)(a1))->sdl_len == \
123 ((struct sockaddr_dl *)(a2))->sdl_len) && \
124 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
125 LLADDR((struct sockaddr_dl *)(a2)), \
126 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
129 * Most of the code in this utility is to support the use of KVM for
130 * post-mortem debugging of the multicast code.
135 static void if_addrlist(struct ifaddr *);
136 static struct in_multi *
137 in_multientry(struct in_multi *);
141 static void if6_addrlist(struct ifaddr *);
142 static struct in6_multi *
143 in6_multientry(struct in6_multi *);
146 static void kread(u_long, void *, int);
147 static void ll_addrlist(struct ifaddr *);
149 static int ifmcstat_kvm(const char *kernel, const char *core);
151 #define KREAD(addr, buf, type) \
152 kread((u_long)addr, (void *)buf, sizeof(type))
155 struct nlist nl[] = {
156 { "_ifnet", 0, 0, 0, 0, },
161 #endif /* WITH_KVM */
163 static int ifmcstat_getifmaddrs(void);
165 static void in_ifinfo(struct igmp_ifinfo *);
166 static const char * inm_mode(u_int mode);
169 static void in6_ifinfo(struct mld_ifinfo *);
170 static const char * inet6_n2a(struct in6_addr *, uint32_t);
172 int main(int, char **);
179 "usage: ifmcstat [-i interface] [-f address family]"
182 " [-K] [-M core] [-N system]"
188 static const char *options = "i:f:vM:N:"
195 main(int argc, char **argv)
199 const char *kernel = NULL;
200 const char *core = NULL;
203 while ((c = getopt(argc, argv, options)) != -1) {
206 if ((ifindex = if_nametoindex(optarg)) == 0) {
207 fprintf(stderr, "%s: unknown interface\n",
215 if (strcmp(optarg, "inet") == 0) {
221 if (strcmp(optarg, "inet6") == 0) {
226 if (strcmp(optarg, "link") == 0) {
230 fprintf(stderr, "%s: unknown address family\n", optarg);
247 core = strdup(optarg);
251 kernel = strdup(optarg);
262 if (af == AF_LINK && vflag)
267 error = ifmcstat_kvm(kernel, core);
269 * If KVM failed, and user did not explicitly specify a core file,
270 * or force KVM backend to be disabled, try the sysctl backend.
272 if (!Kflag || (error != 0 && (core == NULL && kernel == NULL)))
274 error = ifmcstat_getifmaddrs();
285 in_ifinfo(struct igmp_ifinfo *igi)
289 switch (igi->igi_version) {
293 printf("igmpv%d", igi->igi_version);
296 printf("igmpv?(%d)", igi->igi_version);
300 printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
301 if (igi->igi_version == IGMP_VERSION_3) {
302 printf(" rv %u qi %u qri %u uri %u",
303 igi->igi_rv, igi->igi_qi, igi->igi_qri, igi->igi_uri);
306 printf(" v1timer %u v2timer %u v3timer %u",
307 igi->igi_v1_timer, igi->igi_v2_timer, igi->igi_v3_timer);
312 static const char *inm_modes[] = {
322 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
323 return (inm_modes[mode]);
332 ifmcstat_kvm(const char *kernel, const char *core)
334 char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
335 struct ifnet *ifp, *nifp, ifnet;
337 if ((kvmd = kvm_openfiles(kernel, core, NULL, O_RDONLY, buf)) ==
339 perror("kvm_openfiles");
342 if (kvm_nlist(kvmd, nl) < 0) {
346 if (nl[N_IFNET].n_value == 0) {
347 printf("symbol %s not found\n", nl[N_IFNET].n_name);
350 KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
352 KREAD(ifp, &ifnet, struct ifnet);
353 nifp = ifnet.if_link.tqe_next;
354 if (ifindex && ifindex != ifnet.if_index)
357 printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
359 if_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
362 if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
365 ll_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
374 kread(u_long addr, void *buf, int len)
377 if (kvm_read(kvmd, addr, buf, len) != len) {
384 ll_addrlist(struct ifaddr *ifap)
386 char addrbuf[NI_MAXHOST];
389 struct sockaddr_dl sdl;
390 struct ifaddr *ifap0;
392 if (af && af != AF_LINK)
397 KREAD(ifap, &ifa, struct ifaddr);
398 if (ifa.ifa_addr == NULL)
400 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
401 if (sa.sa_family != PF_LINK)
403 KREAD(ifa.ifa_addr, &sdl, struct sockaddr_dl);
404 if (sdl.sdl_alen == 0)
407 getnameinfo((struct sockaddr *)&sdl, sdl.sdl_len,
408 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
409 printf("\tlink %s\n", addrbuf);
411 ifap = ifa.ifa_link.tqe_next;
415 struct ifmultiaddr ifm, *ifmp = 0;
417 KREAD(ifap0, &ifa, struct ifaddr);
418 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
419 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
420 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
422 KREAD(ifmp, &ifm, struct ifmultiaddr);
423 if (ifm.ifma_addr == NULL)
425 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
426 if (sa.sa_family != AF_LINK)
428 KREAD(ifm.ifma_addr, &sdl, struct sockaddr_dl);
430 getnameinfo((struct sockaddr *)&sdl,
431 sdl.sdl_len, addrbuf, sizeof(addrbuf),
432 NULL, 0, NI_NUMERICHOST);
433 printf("\t\tgroup %s refcnt %d\n",
434 addrbuf, ifm.ifma_refcount);
436 ifmp = TAILQ_NEXT(&ifm, ifma_link);
444 if6_addrlist(struct ifaddr *ifap)
449 struct in6_ifaddr if6a;
450 struct ifaddr *ifap0;
452 if (af && af != AF_INET6)
456 KREAD(ifap, &ifa, struct ifaddr);
457 if (ifa.ifa_addr == NULL)
459 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
460 if (sa.sa_family != PF_INET6)
462 KREAD(ifap, &if6a, struct in6_ifaddr);
463 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr,
464 if6a.ia_addr.sin6_scope_id));
466 * Print per-link MLD information, if available.
468 if (ifa.ifa_ifp != NULL) {
469 struct in6_ifextra ie;
470 struct mld_ifinfo mli;
472 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
473 KREAD(ifnet.if_afdata[AF_INET6], &ie,
475 if (ie.mld_ifinfo != NULL) {
476 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
481 ifap = ifa.ifa_link.tqe_next;
485 struct ifmultiaddr ifm, *ifmp = 0;
486 struct sockaddr_dl sdl;
488 KREAD(ifap0, &ifa, struct ifaddr);
489 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
490 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
491 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
493 KREAD(ifmp, &ifm, struct ifmultiaddr);
494 if (ifm.ifma_addr == NULL)
496 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
497 if (sa.sa_family != AF_INET6)
499 (void)in6_multientry((struct in6_multi *)
501 if (ifm.ifma_lladdr == 0)
503 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
504 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
505 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
508 ifmp = TAILQ_NEXT(&ifm, ifma_link);
513 static struct in6_multi *
514 in6_multientry(struct in6_multi *mc)
516 struct in6_multi multi;
518 KREAD(mc, &multi, struct in6_multi);
519 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr, 0));
520 printf(" refcnt %u\n", multi.in6m_refcount);
522 return (multi.in6m_entry.le_next);
530 if_addrlist(struct ifaddr *ifap)
536 struct ifaddr *ifap0;
538 if (af && af != AF_INET)
542 KREAD(ifap, &ifa, struct ifaddr);
543 if (ifa.ifa_addr == NULL)
545 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
546 if (sa.sa_family != PF_INET)
548 KREAD(ifap, &ia, struct in_ifaddr);
549 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
551 * Print per-link IGMP information, if available.
553 if (ifa.ifa_ifp != NULL) {
555 struct igmp_ifinfo igi;
557 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
558 KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
559 if (ii.ii_igmp != NULL) {
560 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
565 ifap = ifa.ifa_link.tqe_next;
568 struct ifmultiaddr ifm, *ifmp = 0;
569 struct sockaddr_dl sdl;
571 KREAD(ifap0, &ifa, struct ifaddr);
572 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
573 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
574 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
576 KREAD(ifmp, &ifm, struct ifmultiaddr);
577 if (ifm.ifma_addr == NULL)
579 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
580 if (sa.sa_family != AF_INET)
582 (void)in_multientry((struct in_multi *)
584 if (ifm.ifma_lladdr == 0)
586 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
587 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
588 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
591 ifmp = TAILQ_NEXT(&ifm, ifma_link);
596 static const char *inm_states[] = {
609 inm_state(u_int state)
612 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
613 return (inm_states[state]);
618 static struct ip_msource *
619 ims_min_kvm(struct in_multi *pinm)
621 struct ip_msource ims0;
622 struct ip_msource *tmp, *parent;
625 tmp = RB_ROOT(&pinm->inm_srcs);
628 KREAD(tmp, &ims0, struct ip_msource);
629 tmp = RB_LEFT(&ims0, ims_link);
631 return (parent); /* kva */
634 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
635 static struct ip_msource *
636 ims_next_kvm(struct ip_msource *ims)
638 struct ip_msource ims0, ims1;
639 struct ip_msource *tmp;
641 KREAD(ims, &ims0, struct ip_msource);
642 if (RB_RIGHT(&ims0, ims_link)) {
643 ims = RB_RIGHT(&ims0, ims_link);
644 KREAD(ims, &ims1, struct ip_msource);
645 while ((tmp = RB_LEFT(&ims1, ims_link))) {
646 KREAD(tmp, &ims0, struct ip_msource);
647 ims = RB_LEFT(&ims0, ims_link);
650 tmp = RB_PARENT(&ims0, ims_link);
652 KREAD(tmp, &ims1, struct ip_msource);
653 if (ims == RB_LEFT(&ims1, ims_link))
656 while ((tmp = RB_PARENT(&ims0, ims_link))) {
657 KREAD(tmp, &ims1, struct ip_msource);
658 if (ims == RB_RIGHT(&ims1, ims_link)) {
660 KREAD(ims, &ims0, struct ip_msource);
664 ims = RB_PARENT(&ims0, ims_link);
667 return (ims); /* kva */
671 inm_print_sources_kvm(struct in_multi *pinm)
673 struct ip_msource ims0;
674 struct ip_msource *ims;
680 fmode = pinm->inm_st[1].iss_fmode;
681 if (fmode == MCAST_UNDEFINED)
683 for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
686 KREAD(ims, &ims0, struct ip_msource);
687 /* Only print sources in-mode at t1. */
688 if (fmode != ims_get_mode(pinm, ims, 1))
690 src.s_addr = htonl(ims0.ims_haddr);
691 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
696 static struct in_multi *
697 in_multientry(struct in_multi *pinm)
700 const char *state, *mode;
702 KREAD(pinm, &inm, struct in_multi);
703 printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
704 printf(" refcnt %u", inm.inm_refcount);
706 state = inm_state(inm.inm_state);
708 printf(" state %s", state);
710 printf(" state (%d)", inm.inm_state);
712 mode = inm_mode(inm.inm_st[1].iss_fmode);
714 printf(" mode %s", mode);
716 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
719 printf(" asm %u ex %u in %u rec %u",
720 (u_int)inm.inm_st[1].iss_asm,
721 (u_int)inm.inm_st[1].iss_ex,
722 (u_int)inm.inm_st[1].iss_in,
723 (u_int)inm.inm_st[1].iss_rec);
729 inm_print_sources_kvm(&inm);
738 #endif /* WITH_KVM */
743 in6_ifinfo(struct mld_ifinfo *mli)
747 switch (mli->mli_version) {
750 printf("mldv%d", mli->mli_version);
753 printf("mldv?(%d)", mli->mli_version);
757 printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
758 if (mli->mli_version == MLD_VERSION_2) {
759 printf(" rv %u qi %u qri %u uri %u",
760 mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
763 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
770 inet6_n2a(struct in6_addr *p, uint32_t scope_id)
772 static char buf[NI_MAXHOST];
773 struct sockaddr_in6 sin6;
774 const int niflags = NI_NUMERICHOST;
776 memset(&sin6, 0, sizeof(sin6));
777 sin6.sin6_family = AF_INET6;
778 sin6.sin6_len = sizeof(struct sockaddr_in6);
780 sin6.sin6_scope_id = scope_id;
781 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
782 buf, sizeof(buf), NULL, 0, niflags) == 0) {
785 return ("(invalid)");
792 * Retrieve per-group source filter mode and lists via sysctl.
795 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
797 #define MAX_SYSCTL_TRY 5
806 struct in_addr *pina;
811 mibsize = sizeof(mib) / sizeof(mib[0]);
812 if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
813 perror("sysctlnametomib");
819 mib[6] = gina.s_addr; /* 32 bits wide */
820 mibsize = sizeof(mib) / sizeof(mib[0]);
822 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
823 perror("sysctl net.inet.ip.mcast.filters");
826 if ((buf = malloc(needed)) == NULL) {
830 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
831 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
838 } while (buf == NULL);
841 if (len < sizeof(uint32_t)) {
848 len -= sizeof(uint32_t);
850 modestr = inm_mode(fmode);
852 printf(" mode %s", modestr);
854 printf(" mode (%u)", fmode);
859 cnt = len / sizeof(struct in_addr);
860 pina = (struct in_addr *)p;
862 for (i = 0; i < cnt; i++) {
865 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
867 len -= sizeof(struct in_addr);
870 fprintf(stderr, "warning: %u trailing bytes from %s\n",
871 (unsigned int)len, "net.inet.ip.mcast.filters");
876 #undef MAX_SYSCTL_TRY
883 * Retrieve MLD per-group source filter mode and lists via sysctl.
885 * Note: The 128-bit IPv6 group address needs to be segmented into
886 * 32-bit pieces for marshaling to sysctl. So the MIB name ends
887 * up looking like this:
888 * a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
889 * Assumes that pgroup originated from the kernel, so its components
890 * are already in network-byte order.
893 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
895 #define MAX_SYSCTL_TRY 5
896 char addrbuf[INET6_ADDRSTRLEN];
906 struct in6_addr *pina;
911 mibsize = sizeof(mib) / sizeof(mib[0]);
912 if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
914 perror("sysctlnametomib");
921 for (i = 0; i < 4; i++)
924 mibsize = sizeof(mib) / sizeof(mib[0]);
926 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
927 perror("sysctl net.inet6.ip6.mcast.filters");
930 if ((buf = malloc(needed)) == NULL) {
934 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
935 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
942 } while (buf == NULL);
945 if (len < sizeof(uint32_t)) {
952 len -= sizeof(uint32_t);
954 modestr = inm_mode(fmode);
956 printf(" mode %s", modestr);
958 printf(" mode (%u)", fmode);
963 cnt = len / sizeof(struct in6_addr);
964 pina = (struct in6_addr *)p;
966 for (i = 0; i < cnt; i++) {
969 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
971 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
972 len -= sizeof(struct in6_addr);
975 fprintf(stderr, "warning: %u trailing bytes from %s\n",
976 (unsigned int)len, "net.inet6.ip6.mcast.filters");
981 #undef MAX_SYSCTL_TRY
986 ifmcstat_getifmaddrs(void)
988 char thisifname[IFNAMSIZ];
989 char addrbuf[NI_MAXHOST];
990 struct ifaddrs *ifap, *ifa;
991 struct ifmaddrs *ifmap, *ifma;
992 sockunion_t lastifasa;
993 sockunion_t *psa, *pgsa, *pllsa, *pifasa;
996 uint32_t lastifindex, thisifindex;
1004 lastifasa.ss.ss_family = AF_UNSPEC;
1006 if (getifaddrs(&ifap) != 0) {
1007 warn("getifmaddrs");
1011 if (getifmaddrs(&ifmap) != 0) {
1012 warn("getifmaddrs");
1017 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1019 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1022 psa = (sockunion_t *)ifma->ifma_name;
1023 if (psa->sa.sa_family != AF_LINK) {
1025 "WARNING: Kernel returned invalid data.\n");
1030 /* Filter on interface name. */
1031 thisifindex = psa->sdl.sdl_index;
1032 if (ifindex != 0 && thisifindex != ifindex)
1035 /* Filter on address family. */
1036 pgsa = (sockunion_t *)ifma->ifma_addr;
1037 if (af != 0 && pgsa->sa.sa_family != af)
1040 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1041 pcolon = strchr(thisifname, ':');
1045 /* Only print the banner for the first ifmaddrs entry. */
1046 if (lastifindex == 0 || lastifindex != thisifindex) {
1047 lastifindex = thisifindex;
1048 fprintf(stdout, "%s:\n", thisifname);
1052 * Currently, multicast joins only take place on the
1053 * primary IPv4 address, and only on the link-local IPv6
1054 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1055 * Therefore, we only look up the primary address on
1059 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1060 if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1061 (ifa->ifa_addr == NULL) ||
1062 (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1065 * For AF_INET6 only the link-local address should
1066 * be returned. If built without IPv6 support,
1067 * skip this address entirely.
1069 pifasa = (sockunion_t *)ifa->ifa_addr;
1070 if (pifasa->sa.sa_family == AF_INET6
1072 && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1081 continue; /* primary address not found */
1083 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1086 /* Parse and print primary address, if not already printed. */
1087 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1088 ((lastifasa.ss.ss_family == AF_LINK &&
1089 !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1090 !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1092 switch (pifasa->sa.sa_family) {
1103 pafname = "unknown";
1107 switch (pifasa->sa.sa_family) {
1112 inet6_n2a(&pifasa->sin6.sin6_addr,
1113 pifasa->sin6.sin6_scope_id);
1114 strlcpy(addrbuf, p, sizeof(addrbuf));
1122 error = getnameinfo(&pifasa->sa,
1124 addrbuf, sizeof(addrbuf), NULL, 0,
1127 perror("getnameinfo");
1134 fprintf(stdout, "\t%s %s", pafname, addrbuf);
1136 if (pifasa->sa.sa_family == AF_INET6 &&
1137 pifasa->sin6.sin6_scope_id)
1138 fprintf(stdout, " scopeid 0x%x",
1139 pifasa->sin6.sin6_scope_id);
1141 fprintf(stdout, "\n");
1144 * Print per-link IGMP information, if available.
1146 if (pifasa->sa.sa_family == AF_INET) {
1147 struct igmp_ifinfo igi;
1148 size_t mibsize, len;
1151 mibsize = sizeof(mib) / sizeof(mib[0]);
1152 if (sysctlnametomib("net.inet.igmp.ifinfo",
1153 mib, &mibsize) == -1) {
1154 perror("sysctlnametomib");
1157 mib[mibsize] = thisifindex;
1158 len = sizeof(struct igmp_ifinfo);
1159 if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1161 perror("sysctl net.inet.igmp.ifinfo");
1169 * Print per-link MLD information, if available.
1171 if (pifasa->sa.sa_family == AF_INET6) {
1172 struct mld_ifinfo mli;
1173 size_t mibsize, len;
1176 mibsize = sizeof(mib) / sizeof(mib[0]);
1177 if (sysctlnametomib("net.inet6.mld.ifinfo",
1178 mib, &mibsize) == -1) {
1179 perror("sysctlnametomib");
1182 mib[mibsize] = thisifindex;
1183 len = sizeof(struct mld_ifinfo);
1184 if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1186 perror("sysctl net.inet6.mld.ifinfo");
1192 #if defined(INET) || defined(INET6)
1195 lastifasa = *pifasa;
1198 /* Print this group address. */
1200 if (pgsa->sa.sa_family == AF_INET6) {
1201 const char *p = inet6_n2a(&pgsa->sin6.sin6_addr,
1202 pgsa->sin6.sin6_scope_id);
1203 strlcpy(addrbuf, p, sizeof(addrbuf));
1207 error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1208 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1210 perror("getnameinfo");
1213 fprintf(stdout, "\t\tgroup %s", addrbuf);
1215 if (pgsa->sa.sa_family == AF_INET6 &&
1216 pgsa->sin6.sin6_scope_id)
1217 fprintf(stdout, " scopeid 0x%x",
1218 pgsa->sin6.sin6_scope_id);
1221 if (pgsa->sa.sa_family == AF_INET) {
1222 inm_print_sources_sysctl(thisifindex,
1223 pgsa->sin.sin_addr);
1227 if (pgsa->sa.sa_family == AF_INET6) {
1228 in6m_print_sources_sysctl(thisifindex,
1229 &pgsa->sin6.sin6_addr);
1232 fprintf(stdout, "\n");
1234 /* Link-layer mapping, if present. */
1235 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1236 if (pllsa != NULL) {
1237 error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1238 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1239 fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1244 freeifmaddrs(ifmap);