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 *);
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));
465 * Print per-link MLD information, if available.
467 if (ifa.ifa_ifp != NULL) {
468 struct in6_ifextra ie;
469 struct mld_ifinfo mli;
471 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
472 KREAD(ifnet.if_afdata[AF_INET6], &ie,
474 if (ie.mld_ifinfo != NULL) {
475 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
480 ifap = ifa.ifa_link.tqe_next;
484 struct ifmultiaddr ifm, *ifmp = 0;
485 struct sockaddr_dl sdl;
487 KREAD(ifap0, &ifa, struct ifaddr);
488 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
489 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
490 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
492 KREAD(ifmp, &ifm, struct ifmultiaddr);
493 if (ifm.ifma_addr == NULL)
495 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
496 if (sa.sa_family != AF_INET6)
498 (void)in6_multientry((struct in6_multi *)
500 if (ifm.ifma_lladdr == 0)
502 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
503 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
504 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
507 ifmp = TAILQ_NEXT(&ifm, ifma_link);
512 static struct in6_multi *
513 in6_multientry(struct in6_multi *mc)
515 struct in6_multi multi;
517 KREAD(mc, &multi, struct in6_multi);
518 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
519 printf(" refcnt %u\n", multi.in6m_refcount);
521 return (multi.in6m_entry.le_next);
529 if_addrlist(struct ifaddr *ifap)
535 struct ifaddr *ifap0;
537 if (af && af != AF_INET)
541 KREAD(ifap, &ifa, struct ifaddr);
542 if (ifa.ifa_addr == NULL)
544 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
545 if (sa.sa_family != PF_INET)
547 KREAD(ifap, &ia, struct in_ifaddr);
548 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
550 * Print per-link IGMP information, if available.
552 if (ifa.ifa_ifp != NULL) {
554 struct igmp_ifinfo igi;
556 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
557 KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
558 if (ii.ii_igmp != NULL) {
559 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
564 ifap = ifa.ifa_link.tqe_next;
567 struct ifmultiaddr ifm, *ifmp = 0;
568 struct sockaddr_dl sdl;
570 KREAD(ifap0, &ifa, struct ifaddr);
571 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
572 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
573 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
575 KREAD(ifmp, &ifm, struct ifmultiaddr);
576 if (ifm.ifma_addr == NULL)
578 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
579 if (sa.sa_family != AF_INET)
581 (void)in_multientry((struct in_multi *)
583 if (ifm.ifma_lladdr == 0)
585 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
586 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
587 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
590 ifmp = TAILQ_NEXT(&ifm, ifma_link);
595 static const char *inm_states[] = {
608 inm_state(u_int state)
611 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
612 return (inm_states[state]);
617 static struct ip_msource *
618 ims_min_kvm(struct in_multi *pinm)
620 struct ip_msource ims0;
621 struct ip_msource *tmp, *parent;
624 tmp = RB_ROOT(&pinm->inm_srcs);
627 KREAD(tmp, &ims0, struct ip_msource);
628 tmp = RB_LEFT(&ims0, ims_link);
630 return (parent); /* kva */
633 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
634 static struct ip_msource *
635 ims_next_kvm(struct ip_msource *ims)
637 struct ip_msource ims0, ims1;
638 struct ip_msource *tmp;
640 KREAD(ims, &ims0, struct ip_msource);
641 if (RB_RIGHT(&ims0, ims_link)) {
642 ims = RB_RIGHT(&ims0, ims_link);
643 KREAD(ims, &ims1, struct ip_msource);
644 while ((tmp = RB_LEFT(&ims1, ims_link))) {
645 KREAD(tmp, &ims0, struct ip_msource);
646 ims = RB_LEFT(&ims0, ims_link);
649 tmp = RB_PARENT(&ims0, ims_link);
651 KREAD(tmp, &ims1, struct ip_msource);
652 if (ims == RB_LEFT(&ims1, ims_link))
655 while ((tmp = RB_PARENT(&ims0, ims_link))) {
656 KREAD(tmp, &ims1, struct ip_msource);
657 if (ims == RB_RIGHT(&ims1, ims_link)) {
659 KREAD(ims, &ims0, struct ip_msource);
663 ims = RB_PARENT(&ims0, ims_link);
666 return (ims); /* kva */
670 inm_print_sources_kvm(struct in_multi *pinm)
672 struct ip_msource ims0;
673 struct ip_msource *ims;
679 fmode = pinm->inm_st[1].iss_fmode;
680 if (fmode == MCAST_UNDEFINED)
682 for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
685 KREAD(ims, &ims0, struct ip_msource);
686 /* Only print sources in-mode at t1. */
687 if (fmode != ims_get_mode(pinm, ims, 1))
689 src.s_addr = htonl(ims0.ims_haddr);
690 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
695 static struct in_multi *
696 in_multientry(struct in_multi *pinm)
699 const char *state, *mode;
701 KREAD(pinm, &inm, struct in_multi);
702 printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
703 printf(" refcnt %u", inm.inm_refcount);
705 state = inm_state(inm.inm_state);
707 printf(" state %s", state);
709 printf(" state (%d)", inm.inm_state);
711 mode = inm_mode(inm.inm_st[1].iss_fmode);
713 printf(" mode %s", mode);
715 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
718 printf(" asm %u ex %u in %u rec %u",
719 (u_int)inm.inm_st[1].iss_asm,
720 (u_int)inm.inm_st[1].iss_ex,
721 (u_int)inm.inm_st[1].iss_in,
722 (u_int)inm.inm_st[1].iss_rec);
728 inm_print_sources_kvm(&inm);
737 #endif /* WITH_KVM */
742 in6_ifinfo(struct mld_ifinfo *mli)
746 switch (mli->mli_version) {
749 printf("mldv%d", mli->mli_version);
752 printf("mldv?(%d)", mli->mli_version);
756 printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
757 if (mli->mli_version == MLD_VERSION_2) {
758 printf(" rv %u qi %u qri %u uri %u",
759 mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
762 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
769 inet6_n2a(struct in6_addr *p)
771 static char buf[NI_MAXHOST];
772 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 if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p) ||
781 IN6_IS_ADDR_MC_NODELOCAL(p)) {
782 scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
784 sin6.sin6_scope_id = scopeid;
785 sin6.sin6_addr.s6_addr[2] = 0;
786 sin6.sin6_addr.s6_addr[3] = 0;
789 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
790 buf, sizeof(buf), NULL, 0, niflags) == 0) {
793 return ("(invalid)");
800 * Retrieve per-group source filter mode and lists via sysctl.
803 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
805 #define MAX_SYSCTL_TRY 5
814 struct in_addr *pina;
819 mibsize = sizeof(mib) / sizeof(mib[0]);
820 if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
821 perror("sysctlnametomib");
827 mib[6] = gina.s_addr; /* 32 bits wide */
828 mibsize = sizeof(mib) / sizeof(mib[0]);
830 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
831 perror("sysctl net.inet.ip.mcast.filters");
834 if ((buf = malloc(needed)) == NULL) {
838 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
839 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
846 } while (buf == NULL);
849 if (len < sizeof(uint32_t)) {
856 len -= sizeof(uint32_t);
858 modestr = inm_mode(fmode);
860 printf(" mode %s", modestr);
862 printf(" mode (%u)", fmode);
867 cnt = len / sizeof(struct in_addr);
868 pina = (struct in_addr *)p;
870 for (i = 0; i < cnt; i++) {
873 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
875 len -= sizeof(struct in_addr);
878 fprintf(stderr, "warning: %u trailing bytes from %s\n",
879 (unsigned int)len, "net.inet.ip.mcast.filters");
884 #undef MAX_SYSCTL_TRY
891 * Retrieve MLD per-group source filter mode and lists via sysctl.
893 * Note: The 128-bit IPv6 group addres needs to be segmented into
894 * 32-bit pieces for marshaling to sysctl. So the MIB name ends
895 * up looking like this:
896 * a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
897 * Assumes that pgroup originated from the kernel, so its components
898 * are already in network-byte order.
901 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
903 #define MAX_SYSCTL_TRY 5
904 char addrbuf[INET6_ADDRSTRLEN];
914 struct in6_addr *pina;
919 mibsize = sizeof(mib) / sizeof(mib[0]);
920 if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
922 perror("sysctlnametomib");
929 for (i = 0; i < 4; i++)
932 mibsize = sizeof(mib) / sizeof(mib[0]);
934 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
935 perror("sysctl net.inet6.ip6.mcast.filters");
938 if ((buf = malloc(needed)) == NULL) {
942 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
943 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
950 } while (buf == NULL);
953 if (len < sizeof(uint32_t)) {
960 len -= sizeof(uint32_t);
962 modestr = inm_mode(fmode);
964 printf(" mode %s", modestr);
966 printf(" mode (%u)", fmode);
971 cnt = len / sizeof(struct in6_addr);
972 pina = (struct in6_addr *)p;
974 for (i = 0; i < cnt; i++) {
977 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
979 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
980 len -= sizeof(struct in6_addr);
983 fprintf(stderr, "warning: %u trailing bytes from %s\n",
984 (unsigned int)len, "net.inet6.ip6.mcast.filters");
989 #undef MAX_SYSCTL_TRY
994 ifmcstat_getifmaddrs(void)
996 char thisifname[IFNAMSIZ];
997 char addrbuf[NI_MAXHOST];
998 struct ifaddrs *ifap, *ifa;
999 struct ifmaddrs *ifmap, *ifma;
1000 sockunion_t lastifasa;
1001 sockunion_t *psa, *pgsa, *pllsa, *pifasa;
1004 uint32_t lastifindex, thisifindex;
1012 lastifasa.ss.ss_family = AF_UNSPEC;
1014 if (getifaddrs(&ifap) != 0) {
1015 warn("getifmaddrs");
1019 if (getifmaddrs(&ifmap) != 0) {
1020 warn("getifmaddrs");
1025 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1027 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1030 psa = (sockunion_t *)ifma->ifma_name;
1031 if (psa->sa.sa_family != AF_LINK) {
1033 "WARNING: Kernel returned invalid data.\n");
1038 /* Filter on interface name. */
1039 thisifindex = psa->sdl.sdl_index;
1040 if (ifindex != 0 && thisifindex != ifindex)
1043 /* Filter on address family. */
1044 pgsa = (sockunion_t *)ifma->ifma_addr;
1045 if (af != 0 && pgsa->sa.sa_family != af)
1048 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1049 pcolon = strchr(thisifname, ':');
1053 /* Only print the banner for the first ifmaddrs entry. */
1054 if (lastifindex == 0 || lastifindex != thisifindex) {
1055 lastifindex = thisifindex;
1056 fprintf(stdout, "%s:\n", thisifname);
1060 * Currently, multicast joins only take place on the
1061 * primary IPv4 address, and only on the link-local IPv6
1062 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1063 * Therefore, we only look up the primary address on
1067 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1068 if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1069 (ifa->ifa_addr == NULL) ||
1070 (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1073 * For AF_INET6 only the link-local address should
1074 * be returned. If built without IPv6 support,
1075 * skip this address entirely.
1077 pifasa = (sockunion_t *)ifa->ifa_addr;
1078 if (pifasa->sa.sa_family == AF_INET6
1080 && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1089 continue; /* primary address not found */
1091 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1094 /* Parse and print primary address, if not already printed. */
1095 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1096 ((lastifasa.ss.ss_family == AF_LINK &&
1097 !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1098 !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1100 switch (pifasa->sa.sa_family) {
1111 pafname = "unknown";
1115 switch (pifasa->sa.sa_family) {
1120 inet6_n2a(&pifasa->sin6.sin6_addr);
1121 strlcpy(addrbuf, p, sizeof(addrbuf));
1129 error = getnameinfo(&pifasa->sa,
1131 addrbuf, sizeof(addrbuf), NULL, 0,
1134 perror("getnameinfo");
1141 fprintf(stdout, "\t%s %s\n", pafname, addrbuf);
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 strlcpy(addrbuf, p, sizeof(addrbuf));
1206 error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1207 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1209 perror("getnameinfo");
1212 fprintf(stdout, "\t\tgroup %s", addrbuf);
1214 if (pgsa->sa.sa_family == AF_INET) {
1215 inm_print_sources_sysctl(thisifindex,
1216 pgsa->sin.sin_addr);
1220 if (pgsa->sa.sa_family == AF_INET6) {
1221 in6m_print_sources_sysctl(thisifindex,
1222 &pgsa->sin6.sin6_addr);
1225 fprintf(stdout, "\n");
1227 /* Link-layer mapping, if present. */
1228 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1229 if (pllsa != NULL) {
1230 error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1231 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1232 fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1237 freeifmaddrs(ifmap);