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_types.h>
45 #include <net/if_dl.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/igmp.h>
53 #include <netinet/if_ether.h>
54 #include <netinet/igmp_var.h>
57 #include <netinet/icmp6.h>
58 #include <netinet6/mld6_var.h>
61 #include <arpa/inet.h>
82 * Currently the KVM build is broken. To be fixed it requires uncovering
83 * large amount of _KERNEL code in include files, and it is also very
84 * tentative to internal kernel ABI changes. If anyone wishes to restore
85 * it, please move it out of src/usr.sbin to src/tools/tools.
91 /* XXX: This file currently assumes INET 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_dl_equal(a1, a2) \
119 ((((struct sockaddr_dl *)(a1))->sdl_len == \
120 ((struct sockaddr_dl *)(a2))->sdl_len) && \
121 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
122 LLADDR((struct sockaddr_dl *)(a2)), \
123 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
126 * Most of the code in this utility is to support the use of KVM for
127 * post-mortem debugging of the multicast code.
132 static void if_addrlist(struct ifaddr *);
133 static struct in_multi *
134 in_multientry(struct in_multi *);
138 static void if6_addrlist(struct ifaddr *);
139 static struct in6_multi *
140 in6_multientry(struct in6_multi *);
143 static void kread(u_long, void *, int);
144 static void ll_addrlist(struct ifaddr *);
146 static int ifmcstat_kvm(const char *kernel, const char *core);
148 #define KREAD(addr, buf, type) \
149 kread((u_long)addr, (void *)buf, sizeof(type))
152 struct nlist nl[] = {
153 { "_ifnet", 0, 0, 0, 0, },
158 #endif /* WITH_KVM */
160 static int ifmcstat_getifmaddrs(void);
162 static void in_ifinfo(struct igmp_ifinfo *);
163 static const char * inm_mode(u_int mode);
166 static void in6_ifinfo(struct mld_ifinfo *);
167 static const char * inet6_n2a(struct in6_addr *, uint32_t);
169 int main(int, char **);
176 "usage: ifmcstat [-i interface] [-f address family]"
179 " [-K] [-M core] [-N system]"
185 static const char *options = "i:f:vM:N:"
192 main(int argc, char **argv)
196 const char *kernel = NULL;
197 const char *core = NULL;
200 while ((c = getopt(argc, argv, options)) != -1) {
203 if ((ifindex = if_nametoindex(optarg)) == 0) {
204 fprintf(stderr, "%s: unknown interface\n",
212 if (strcmp(optarg, "inet") == 0) {
218 if (strcmp(optarg, "inet6") == 0) {
223 if (strcmp(optarg, "link") == 0) {
227 fprintf(stderr, "%s: unknown address family\n", optarg);
244 core = strdup(optarg);
248 kernel = strdup(optarg);
259 if (af == AF_LINK && vflag)
264 error = ifmcstat_kvm(kernel, core);
266 * If KVM failed, and user did not explicitly specify a core file,
267 * or force KVM backend to be disabled, try the sysctl backend.
269 if (!Kflag || (error != 0 && (core == NULL && kernel == NULL)))
271 error = ifmcstat_getifmaddrs();
282 in_ifinfo(struct igmp_ifinfo *igi)
286 switch (igi->igi_version) {
290 printf("igmpv%d", igi->igi_version);
293 printf("igmpv?(%d)", igi->igi_version);
297 printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
298 if (igi->igi_version == IGMP_VERSION_3) {
299 printf(" rv %u qi %u qri %u uri %u",
300 igi->igi_rv, igi->igi_qi, igi->igi_qri, igi->igi_uri);
303 printf(" v1timer %u v2timer %u v3timer %u",
304 igi->igi_v1_timer, igi->igi_v2_timer, igi->igi_v3_timer);
309 static const char *inm_modes[] = {
319 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
320 return (inm_modes[mode]);
329 ifmcstat_kvm(const char *kernel, const char *core)
331 char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
332 struct ifnet *ifp, *nifp, ifnet;
334 if ((kvmd = kvm_openfiles(kernel, core, NULL, O_RDONLY, buf)) ==
336 perror("kvm_openfiles");
339 if (kvm_nlist(kvmd, nl) < 0) {
343 if (nl[N_IFNET].n_value == 0) {
344 printf("symbol %s not found\n", nl[N_IFNET].n_name);
347 KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
349 KREAD(ifp, &ifnet, struct ifnet);
350 nifp = ifnet.if_link.tqe_next;
351 if (ifindex && ifindex != ifnet.if_index)
354 printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
356 if_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
359 if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
362 ll_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
371 kread(u_long addr, void *buf, int len)
374 if (kvm_read(kvmd, addr, buf, len) != len) {
381 ll_addrlist(struct ifaddr *ifap)
383 char addrbuf[NI_MAXHOST];
386 struct sockaddr_dl sdl;
387 struct ifaddr *ifap0;
389 if (af && af != AF_LINK)
394 KREAD(ifap, &ifa, struct ifaddr);
395 if (ifa.ifa_addr == NULL)
397 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
398 if (sa.sa_family != PF_LINK)
400 KREAD(ifa.ifa_addr, &sdl, struct sockaddr_dl);
401 if (sdl.sdl_alen == 0)
404 getnameinfo((struct sockaddr *)&sdl, sdl.sdl_len,
405 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
406 printf("\tlink %s\n", addrbuf);
408 ifap = ifa.ifa_link.tqe_next;
412 struct ifmultiaddr ifm, *ifmp = 0;
414 KREAD(ifap0, &ifa, struct ifaddr);
415 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
416 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
417 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
419 KREAD(ifmp, &ifm, struct ifmultiaddr);
420 if (ifm.ifma_addr == NULL)
422 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
423 if (sa.sa_family != AF_LINK)
425 KREAD(ifm.ifma_addr, &sdl, struct sockaddr_dl);
427 getnameinfo((struct sockaddr *)&sdl,
428 sdl.sdl_len, addrbuf, sizeof(addrbuf),
429 NULL, 0, NI_NUMERICHOST);
430 printf("\t\tgroup %s refcnt %d\n",
431 addrbuf, ifm.ifma_refcount);
433 ifmp = TAILQ_NEXT(&ifm, ifma_link);
441 if6_addrlist(struct ifaddr *ifap)
446 struct in6_ifaddr if6a;
447 struct ifaddr *ifap0;
449 if (af && af != AF_INET6)
453 KREAD(ifap, &ifa, struct ifaddr);
454 if (ifa.ifa_addr == NULL)
456 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
457 if (sa.sa_family != PF_INET6)
459 KREAD(ifap, &if6a, struct in6_ifaddr);
460 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr,
461 if6a.ia_addr.sin6_scope_id));
463 * Print per-link MLD information, if available.
465 if (ifa.ifa_ifp != NULL) {
466 struct in6_ifextra ie;
467 struct mld_ifinfo mli;
469 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
470 KREAD(ifnet.if_afdata[AF_INET6], &ie,
472 if (ie.mld_ifinfo != NULL) {
473 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
478 ifap = ifa.ifa_link.tqe_next;
482 struct ifmultiaddr ifm, *ifmp = 0;
483 struct sockaddr_dl sdl;
485 KREAD(ifap0, &ifa, struct ifaddr);
486 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
487 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
488 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
490 KREAD(ifmp, &ifm, struct ifmultiaddr);
491 if (ifm.ifma_addr == NULL)
493 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
494 if (sa.sa_family != AF_INET6)
496 (void)in6_multientry((struct in6_multi *)
498 if (ifm.ifma_lladdr == 0)
500 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
501 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
502 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
505 ifmp = TAILQ_NEXT(&ifm, ifma_link);
510 static struct in6_multi *
511 in6_multientry(struct in6_multi *mc)
513 struct in6_multi multi;
515 KREAD(mc, &multi, struct in6_multi);
516 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr, 0));
517 printf(" refcnt %u\n", multi.in6m_refcount);
519 return (multi.in6m_entry.le_next);
527 if_addrlist(struct ifaddr *ifap)
533 struct ifaddr *ifap0;
535 if (af && af != AF_INET)
539 KREAD(ifap, &ifa, struct ifaddr);
540 if (ifa.ifa_addr == NULL)
542 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
543 if (sa.sa_family != PF_INET)
545 KREAD(ifap, &ia, struct in_ifaddr);
546 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
548 * Print per-link IGMP information, if available.
550 if (ifa.ifa_ifp != NULL) {
552 struct igmp_ifinfo igi;
554 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
555 KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
556 if (ii.ii_igmp != NULL) {
557 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
562 ifap = ifa.ifa_link.tqe_next;
565 struct ifmultiaddr ifm, *ifmp = 0;
566 struct sockaddr_dl sdl;
568 KREAD(ifap0, &ifa, struct ifaddr);
569 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
570 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
571 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
573 KREAD(ifmp, &ifm, struct ifmultiaddr);
574 if (ifm.ifma_addr == NULL)
576 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
577 if (sa.sa_family != AF_INET)
579 (void)in_multientry((struct in_multi *)
581 if (ifm.ifma_lladdr == 0)
583 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
584 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
585 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
588 ifmp = TAILQ_NEXT(&ifm, ifma_link);
593 static const char *inm_states[] = {
606 inm_state(u_int state)
609 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
610 return (inm_states[state]);
615 static struct ip_msource *
616 ims_min_kvm(struct in_multi *pinm)
618 struct ip_msource ims0;
619 struct ip_msource *tmp, *parent;
622 tmp = RB_ROOT(&pinm->inm_srcs);
625 KREAD(tmp, &ims0, struct ip_msource);
626 tmp = RB_LEFT(&ims0, ims_link);
628 return (parent); /* kva */
631 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
632 static struct ip_msource *
633 ims_next_kvm(struct ip_msource *ims)
635 struct ip_msource ims0, ims1;
636 struct ip_msource *tmp;
638 KREAD(ims, &ims0, struct ip_msource);
639 if (RB_RIGHT(&ims0, ims_link)) {
640 ims = RB_RIGHT(&ims0, ims_link);
641 KREAD(ims, &ims1, struct ip_msource);
642 while ((tmp = RB_LEFT(&ims1, ims_link))) {
643 KREAD(tmp, &ims0, struct ip_msource);
644 ims = RB_LEFT(&ims0, ims_link);
647 tmp = RB_PARENT(&ims0, ims_link);
649 KREAD(tmp, &ims1, struct ip_msource);
650 if (ims == RB_LEFT(&ims1, ims_link))
653 while ((tmp = RB_PARENT(&ims0, ims_link))) {
654 KREAD(tmp, &ims1, struct ip_msource);
655 if (ims == RB_RIGHT(&ims1, ims_link)) {
657 KREAD(ims, &ims0, struct ip_msource);
661 ims = RB_PARENT(&ims0, ims_link);
664 return (ims); /* kva */
668 inm_print_sources_kvm(struct in_multi *pinm)
670 struct ip_msource ims0;
671 struct ip_msource *ims;
677 fmode = pinm->inm_st[1].iss_fmode;
678 if (fmode == MCAST_UNDEFINED)
680 for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
683 KREAD(ims, &ims0, struct ip_msource);
684 /* Only print sources in-mode at t1. */
685 if (fmode != ims_get_mode(pinm, ims, 1))
687 src.s_addr = htonl(ims0.ims_haddr);
688 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
693 static struct in_multi *
694 in_multientry(struct in_multi *pinm)
697 const char *state, *mode;
699 KREAD(pinm, &inm, struct in_multi);
700 printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
701 printf(" refcnt %u", inm.inm_refcount);
703 state = inm_state(inm.inm_state);
705 printf(" state %s", state);
707 printf(" state (%d)", inm.inm_state);
709 mode = inm_mode(inm.inm_st[1].iss_fmode);
711 printf(" mode %s", mode);
713 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
716 printf(" asm %u ex %u in %u rec %u",
717 (u_int)inm.inm_st[1].iss_asm,
718 (u_int)inm.inm_st[1].iss_ex,
719 (u_int)inm.inm_st[1].iss_in,
720 (u_int)inm.inm_st[1].iss_rec);
726 inm_print_sources_kvm(&inm);
735 #endif /* WITH_KVM */
740 in6_ifinfo(struct mld_ifinfo *mli)
744 switch (mli->mli_version) {
747 printf("mldv%d", mli->mli_version);
750 printf("mldv?(%d)", mli->mli_version);
754 printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
755 if (mli->mli_version == MLD_VERSION_2) {
756 printf(" rv %u qi %u qri %u uri %u",
757 mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
760 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
767 inet6_n2a(struct in6_addr *p, uint32_t scope_id)
769 static char buf[NI_MAXHOST];
770 struct sockaddr_in6 sin6;
771 const int niflags = NI_NUMERICHOST;
773 memset(&sin6, 0, sizeof(sin6));
774 sin6.sin6_family = AF_INET6;
775 sin6.sin6_len = sizeof(struct sockaddr_in6);
777 sin6.sin6_scope_id = scope_id;
778 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
779 buf, sizeof(buf), NULL, 0, niflags) == 0) {
782 return ("(invalid)");
789 * Retrieve per-group source filter mode and lists via sysctl.
792 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
794 #define MAX_SYSCTL_TRY 5
803 struct in_addr *pina;
808 mibsize = sizeof(mib) / sizeof(mib[0]);
809 if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
810 perror("sysctlnametomib");
816 mib[6] = gina.s_addr; /* 32 bits wide */
817 mibsize = sizeof(mib) / sizeof(mib[0]);
819 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
820 perror("sysctl net.inet.ip.mcast.filters");
823 if ((buf = malloc(needed)) == NULL) {
827 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
828 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
835 } while (buf == NULL);
838 if (len < sizeof(uint32_t)) {
845 len -= sizeof(uint32_t);
847 modestr = inm_mode(fmode);
849 printf(" mode %s", modestr);
851 printf(" mode (%u)", fmode);
856 cnt = len / sizeof(struct in_addr);
857 pina = (struct in_addr *)p;
859 for (i = 0; i < cnt; i++) {
862 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
864 len -= sizeof(struct in_addr);
867 fprintf(stderr, "warning: %u trailing bytes from %s\n",
868 (unsigned int)len, "net.inet.ip.mcast.filters");
873 #undef MAX_SYSCTL_TRY
880 * Retrieve MLD per-group source filter mode and lists via sysctl.
882 * Note: The 128-bit IPv6 group address needs to be segmented into
883 * 32-bit pieces for marshaling to sysctl. So the MIB name ends
884 * up looking like this:
885 * a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
886 * Assumes that pgroup originated from the kernel, so its components
887 * are already in network-byte order.
890 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
892 #define MAX_SYSCTL_TRY 5
893 char addrbuf[INET6_ADDRSTRLEN];
903 struct in6_addr *pina;
908 mibsize = sizeof(mib) / sizeof(mib[0]);
909 if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
911 perror("sysctlnametomib");
918 for (i = 0; i < 4; i++)
921 mibsize = sizeof(mib) / sizeof(mib[0]);
923 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
924 perror("sysctl net.inet6.ip6.mcast.filters");
927 if ((buf = malloc(needed)) == NULL) {
931 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
932 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
939 } while (buf == NULL);
942 if (len < sizeof(uint32_t)) {
949 len -= sizeof(uint32_t);
951 modestr = inm_mode(fmode);
953 printf(" mode %s", modestr);
955 printf(" mode (%u)", fmode);
960 cnt = len / sizeof(struct in6_addr);
961 pina = (struct in6_addr *)p;
963 for (i = 0; i < cnt; i++) {
966 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
968 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
969 len -= sizeof(struct in6_addr);
972 fprintf(stderr, "warning: %u trailing bytes from %s\n",
973 (unsigned int)len, "net.inet6.ip6.mcast.filters");
978 #undef MAX_SYSCTL_TRY
983 ifmcstat_getifmaddrs(void)
985 char thisifname[IFNAMSIZ];
986 char addrbuf[NI_MAXHOST];
987 struct ifaddrs *ifap, *ifa;
988 struct ifmaddrs *ifmap, *ifma;
989 sockunion_t lastifasa;
990 sockunion_t *psa, *pgsa, *pllsa, *pifasa;
993 uint32_t lastifindex, thisifindex;
1001 lastifasa.ss.ss_family = AF_UNSPEC;
1003 if (getifaddrs(&ifap) != 0) {
1004 warn("getifmaddrs");
1008 if (getifmaddrs(&ifmap) != 0) {
1009 warn("getifmaddrs");
1014 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1016 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1019 psa = (sockunion_t *)ifma->ifma_name;
1020 if (psa->sa.sa_family != AF_LINK) {
1022 "WARNING: Kernel returned invalid data.\n");
1027 /* Filter on interface name. */
1028 thisifindex = psa->sdl.sdl_index;
1029 if (ifindex != 0 && thisifindex != ifindex)
1032 /* Filter on address family. */
1033 pgsa = (sockunion_t *)ifma->ifma_addr;
1034 if (af != 0 && pgsa->sa.sa_family != af)
1037 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1038 pcolon = strchr(thisifname, ':');
1042 /* Only print the banner for the first ifmaddrs entry. */
1043 if (lastifindex == 0 || lastifindex != thisifindex) {
1044 lastifindex = thisifindex;
1045 fprintf(stdout, "%s:\n", thisifname);
1049 * Currently, multicast joins only take place on the
1050 * primary IPv4 address, and only on the link-local IPv6
1051 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1052 * Therefore, we only look up the primary address on
1056 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1057 if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1058 (ifa->ifa_addr == NULL) ||
1059 (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1062 * For AF_INET6 only the link-local address should
1063 * be returned. If built without IPv6 support,
1064 * skip this address entirely.
1066 pifasa = (sockunion_t *)ifa->ifa_addr;
1067 if (pifasa->sa.sa_family == AF_INET6
1069 && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1078 continue; /* primary address not found */
1080 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1083 /* Parse and print primary address, if not already printed. */
1084 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1085 ((lastifasa.ss.ss_family == AF_LINK &&
1086 !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1087 !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1089 switch (pifasa->sa.sa_family) {
1100 pafname = "unknown";
1104 switch (pifasa->sa.sa_family) {
1109 inet6_n2a(&pifasa->sin6.sin6_addr,
1110 pifasa->sin6.sin6_scope_id);
1111 strlcpy(addrbuf, p, sizeof(addrbuf));
1119 error = getnameinfo(&pifasa->sa,
1121 addrbuf, sizeof(addrbuf), NULL, 0,
1124 perror("getnameinfo");
1131 fprintf(stdout, "\t%s %s", pafname, addrbuf);
1133 if (pifasa->sa.sa_family == AF_INET6 &&
1134 pifasa->sin6.sin6_scope_id)
1135 fprintf(stdout, " scopeid 0x%x",
1136 pifasa->sin6.sin6_scope_id);
1138 fprintf(stdout, "\n");
1141 * Print per-link IGMP information, if available.
1143 if (pifasa->sa.sa_family == AF_INET) {
1144 struct igmp_ifinfo igi;
1145 size_t mibsize, len;
1148 mibsize = sizeof(mib) / sizeof(mib[0]);
1149 if (sysctlnametomib("net.inet.igmp.ifinfo",
1150 mib, &mibsize) == -1) {
1151 perror("sysctlnametomib");
1154 mib[mibsize] = thisifindex;
1155 len = sizeof(struct igmp_ifinfo);
1156 if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1158 perror("sysctl net.inet.igmp.ifinfo");
1166 * Print per-link MLD information, if available.
1168 if (pifasa->sa.sa_family == AF_INET6) {
1169 struct mld_ifinfo mli;
1170 size_t mibsize, len;
1173 mibsize = sizeof(mib) / sizeof(mib[0]);
1174 if (sysctlnametomib("net.inet6.mld.ifinfo",
1175 mib, &mibsize) == -1) {
1176 perror("sysctlnametomib");
1179 mib[mibsize] = thisifindex;
1180 len = sizeof(struct mld_ifinfo);
1181 if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1183 perror("sysctl net.inet6.mld.ifinfo");
1189 #if defined(INET) || defined(INET6)
1192 lastifasa = *pifasa;
1195 /* Print this group address. */
1197 if (pgsa->sa.sa_family == AF_INET6) {
1198 const char *p = inet6_n2a(&pgsa->sin6.sin6_addr,
1199 pgsa->sin6.sin6_scope_id);
1200 strlcpy(addrbuf, p, sizeof(addrbuf));
1204 error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1205 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1207 perror("getnameinfo");
1210 fprintf(stdout, "\t\tgroup %s", addrbuf);
1212 if (pgsa->sa.sa_family == AF_INET6 &&
1213 pgsa->sin6.sin6_scope_id)
1214 fprintf(stdout, " scopeid 0x%x",
1215 pgsa->sin6.sin6_scope_id);
1218 if (pgsa->sa.sa_family == AF_INET) {
1219 inm_print_sources_sysctl(thisifindex,
1220 pgsa->sin.sin_addr);
1224 if (pgsa->sa.sa_family == AF_INET6) {
1225 in6m_print_sources_sysctl(thisifindex,
1226 &pgsa->sin6.sin6_addr);
1229 fprintf(stdout, "\n");
1231 /* Link-layer mapping, if present. */
1232 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1233 if (pllsa != NULL) {
1234 error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1235 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1236 fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1241 freeifmaddrs(ifmap);