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>
92 /* XXX: This file currently assumes INET and KVM support in the base system. */
97 extern void printb(const char *, unsigned int, const char *);
100 struct sockaddr_storage ss;
102 struct sockaddr_dl sdl;
104 struct sockaddr_in sin;
107 struct sockaddr_in6 sin6;
110 typedef union sockunion sockunion_t;
112 uint32_t ifindex = 0;
119 #define sa_equal(a1, a2) \
120 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
122 #define sa_dl_equal(a1, a2) \
123 ((((struct sockaddr_dl *)(a1))->sdl_len == \
124 ((struct sockaddr_dl *)(a2))->sdl_len) && \
125 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
126 LLADDR((struct sockaddr_dl *)(a2)), \
127 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
130 * Most of the code in this utility is to support the use of KVM for
131 * post-mortem debugging of the multicast code.
136 static void if_addrlist(struct ifaddr *);
137 static struct in_multi *
138 in_multientry(struct in_multi *);
142 static void if6_addrlist(struct ifaddr *);
143 static struct in6_multi *
144 in6_multientry(struct in6_multi *);
147 static void kread(u_long, void *, int);
148 static void ll_addrlist(struct ifaddr *);
150 static int ifmcstat_kvm(const char *kernel, const char *core);
152 #define KREAD(addr, buf, type) \
153 kread((u_long)addr, (void *)buf, sizeof(type))
156 struct nlist nl[] = {
157 { "_ifnet", 0, 0, 0, 0, },
162 #endif /* WITH_KVM */
164 static int ifmcstat_getifmaddrs(void);
166 static void in_ifinfo(struct igmp_ifinfo *);
167 static const char * inm_mode(u_int mode);
170 static void in6_ifinfo(struct mld_ifinfo *);
171 static const char * inet6_n2a(struct in6_addr *);
173 int main(int, char **);
180 "usage: ifmcstat [-i interface] [-f address family]"
183 " [-K] [-M core] [-N system]"
189 static const char *options = "i:f:vM:N:"
196 main(int argc, char **argv)
200 const char *kernel = NULL;
201 const char *core = NULL;
204 while ((c = getopt(argc, argv, options)) != -1) {
207 if ((ifindex = if_nametoindex(optarg)) == 0) {
208 fprintf(stderr, "%s: unknown interface\n",
216 if (strcmp(optarg, "inet") == 0) {
222 if (strcmp(optarg, "inet6") == 0) {
227 if (strcmp(optarg, "link") == 0) {
231 fprintf(stderr, "%s: unknown address family\n", optarg);
248 core = strdup(optarg);
252 kernel = strdup(optarg);
263 if (af == AF_LINK && vflag)
268 error = ifmcstat_kvm(kernel, core);
270 * If KVM failed, and user did not explicitly specify a core file,
271 * or force KVM backend to be disabled, try the sysctl backend.
273 if (!Kflag || (error != 0 && (core == NULL && kernel == NULL)))
275 error = ifmcstat_getifmaddrs();
286 in_ifinfo(struct igmp_ifinfo *igi)
290 switch (igi->igi_version) {
294 printf("igmpv%d", igi->igi_version);
297 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;
393 if (af && af != AF_LINK)
398 KREAD(ifap, &ifa, struct ifaddr);
399 if (ifa.ifa_addr == NULL)
401 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
402 if (sa.sa_family != PF_LINK)
404 KREAD(ifa.ifa_addr, &sdl, struct sockaddr_dl);
405 if (sdl.sdl_alen == 0)
408 error = getnameinfo((struct sockaddr *)&sdl, sdl.sdl_len,
409 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
410 printf("\tlink %s\n", addrbuf);
412 ifap = ifa.ifa_link.tqe_next;
416 struct ifmultiaddr ifm, *ifmp = 0;
418 KREAD(ifap0, &ifa, struct ifaddr);
419 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
420 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
421 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
423 KREAD(ifmp, &ifm, struct ifmultiaddr);
424 if (ifm.ifma_addr == NULL)
426 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
427 if (sa.sa_family != AF_LINK)
429 KREAD(ifm.ifma_addr, &sdl, struct sockaddr_dl);
431 error = getnameinfo((struct sockaddr *)&sdl,
432 sdl.sdl_len, addrbuf, sizeof(addrbuf),
433 NULL, 0, NI_NUMERICHOST);
434 printf("\t\tgroup %s refcnt %d\n",
435 addrbuf, ifm.ifma_refcount);
437 ifmp = TAILQ_NEXT(&ifm, ifma_link);
445 in6_ifinfo(struct mld_ifinfo *mli)
449 switch (mli->mli_version) {
452 printf("mldv%d", mli->mli_version);
455 printf("mldv?(%d)", mli->mli_version);
458 printb(" flags", mli->mli_flags, "\020\1SILENT");
459 if (mli->mli_version == MLD_VERSION_2) {
460 printf(" rv %u qi %u qri %u uri %u",
461 mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
464 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
471 if6_addrlist(struct ifaddr *ifap)
476 struct in6_ifaddr if6a;
477 struct ifaddr *ifap0;
479 if (af && af != AF_INET6)
483 KREAD(ifap, &ifa, struct ifaddr);
484 if (ifa.ifa_addr == NULL)
486 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
487 if (sa.sa_family != PF_INET6)
489 KREAD(ifap, &if6a, struct in6_ifaddr);
490 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
492 * Print per-link MLD information, if available.
494 if (ifa.ifa_ifp != NULL) {
495 struct in6_ifextra ie;
496 struct mld_ifinfo mli;
498 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
499 KREAD(ifnet.if_afdata[AF_INET6], &ie,
501 if (ie.mld_ifinfo != NULL) {
502 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
507 ifap = ifa.ifa_link.tqe_next;
511 struct ifmultiaddr ifm, *ifmp = 0;
512 struct sockaddr_dl sdl;
514 KREAD(ifap0, &ifa, struct ifaddr);
515 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
516 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
517 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
519 KREAD(ifmp, &ifm, struct ifmultiaddr);
520 if (ifm.ifma_addr == NULL)
522 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
523 if (sa.sa_family != AF_INET6)
525 (void)in6_multientry((struct in6_multi *)
527 if (ifm.ifma_lladdr == 0)
529 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
530 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
531 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
534 ifmp = TAILQ_NEXT(&ifm, ifma_link);
539 static struct in6_multi *
540 in6_multientry(struct in6_multi *mc)
542 struct in6_multi multi;
544 KREAD(mc, &multi, struct in6_multi);
545 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
546 printf(" refcnt %u\n", multi.in6m_refcount);
548 return (multi.in6m_entry.le_next);
556 if_addrlist(struct ifaddr *ifap)
562 struct ifaddr *ifap0;
564 if (af && af != AF_INET)
568 KREAD(ifap, &ifa, struct ifaddr);
569 if (ifa.ifa_addr == NULL)
571 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
572 if (sa.sa_family != PF_INET)
574 KREAD(ifap, &ia, struct in_ifaddr);
575 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
577 * Print per-link IGMP information, if available.
579 if (ifa.ifa_ifp != NULL) {
581 struct igmp_ifinfo igi;
583 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
584 KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
585 if (ii.ii_igmp != NULL) {
586 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
591 ifap = ifa.ifa_link.tqe_next;
594 struct ifmultiaddr ifm, *ifmp = 0;
595 struct sockaddr_dl sdl;
597 KREAD(ifap0, &ifa, struct ifaddr);
598 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
599 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
600 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
602 KREAD(ifmp, &ifm, struct ifmultiaddr);
603 if (ifm.ifma_addr == NULL)
605 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
606 if (sa.sa_family != AF_INET)
608 (void)in_multientry((struct in_multi *)
610 if (ifm.ifma_lladdr == 0)
612 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
613 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
614 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
617 ifmp = TAILQ_NEXT(&ifm, ifma_link);
622 static const char *inm_states[] = {
635 inm_state(u_int state)
638 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
639 return (inm_states[state]);
644 static struct ip_msource *
645 ims_min_kvm(struct in_multi *pinm)
647 struct ip_msource ims0;
648 struct ip_msource *tmp, *parent;
651 tmp = RB_ROOT(&pinm->inm_srcs);
654 KREAD(tmp, &ims0, struct ip_msource);
655 tmp = RB_LEFT(&ims0, ims_link);
657 return (parent); /* kva */
660 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
661 static struct ip_msource *
662 ims_next_kvm(struct ip_msource *ims)
664 struct ip_msource ims0, ims1;
665 struct ip_msource *tmp;
667 KREAD(ims, &ims0, struct ip_msource);
668 if (RB_RIGHT(&ims0, ims_link)) {
669 ims = RB_RIGHT(&ims0, ims_link);
670 KREAD(ims, &ims1, struct ip_msource);
671 while ((tmp = RB_LEFT(&ims1, ims_link))) {
672 KREAD(tmp, &ims0, struct ip_msource);
673 ims = RB_LEFT(&ims0, ims_link);
676 tmp = RB_PARENT(&ims0, ims_link);
678 KREAD(tmp, &ims1, struct ip_msource);
679 if (ims == RB_LEFT(&ims1, ims_link))
682 while ((tmp = RB_PARENT(&ims0, ims_link))) {
683 KREAD(tmp, &ims1, struct ip_msource);
684 if (ims == RB_RIGHT(&ims1, ims_link)) {
686 KREAD(ims, &ims0, struct ip_msource);
690 ims = RB_PARENT(&ims0, ims_link);
693 return (ims); /* kva */
697 inm_print_sources_kvm(struct in_multi *pinm)
699 struct ip_msource ims0;
700 struct ip_msource *ims;
706 fmode = pinm->inm_st[1].iss_fmode;
707 if (fmode == MCAST_UNDEFINED)
709 for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
712 KREAD(ims, &ims0, struct ip_msource);
713 /* Only print sources in-mode at t1. */
714 if (fmode != ims_get_mode(pinm, ims, 1))
716 src.s_addr = htonl(ims0.ims_haddr);
717 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
722 static struct in_multi *
723 in_multientry(struct in_multi *pinm)
726 const char *state, *mode;
728 KREAD(pinm, &inm, struct in_multi);
729 printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
730 printf(" refcnt %u", inm.inm_refcount);
732 state = inm_state(inm.inm_state);
734 printf(" state %s", state);
736 printf(" state (%d)", inm.inm_state);
738 mode = inm_mode(inm.inm_st[1].iss_fmode);
740 printf(" mode %s", mode);
742 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
745 printf(" asm %u ex %u in %u rec %u",
746 (u_int)inm.inm_st[1].iss_asm,
747 (u_int)inm.inm_st[1].iss_ex,
748 (u_int)inm.inm_st[1].iss_in,
749 (u_int)inm.inm_st[1].iss_rec);
755 inm_print_sources_kvm(&inm);
764 #endif /* WITH_KVM */
768 inet6_n2a(struct in6_addr *p)
770 static char buf[NI_MAXHOST];
771 struct sockaddr_in6 sin6;
773 const int niflags = NI_NUMERICHOST;
775 memset(&sin6, 0, sizeof(sin6));
776 sin6.sin6_family = AF_INET6;
777 sin6.sin6_len = sizeof(struct sockaddr_in6);
779 if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p) ||
780 IN6_IS_ADDR_MC_NODELOCAL(p)) {
781 scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
783 sin6.sin6_scope_id = scopeid;
784 sin6.sin6_addr.s6_addr[2] = 0;
785 sin6.sin6_addr.s6_addr[3] = 0;
788 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
789 buf, sizeof(buf), NULL, 0, niflags) == 0) {
792 return ("(invalid)");
799 * Retrieve per-group source filter mode and lists via sysctl.
802 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
804 #define MAX_SYSCTL_TRY 5
813 struct in_addr *pina;
818 mibsize = sizeof(mib) / sizeof(mib[0]);
819 if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
820 perror("sysctlnametomib");
826 mib[6] = gina.s_addr; /* 32 bits wide */
827 mibsize = sizeof(mib) / sizeof(mib[0]);
829 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
830 perror("sysctl net.inet.ip.mcast.filters");
833 if ((buf = malloc(needed)) == NULL) {
837 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
838 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
845 } while (buf == NULL);
848 if (len < sizeof(uint32_t)) {
855 len -= sizeof(uint32_t);
857 modestr = inm_mode(fmode);
859 printf(" mode %s", modestr);
861 printf(" mode (%u)", fmode);
866 cnt = len / sizeof(struct in_addr);
867 pina = (struct in_addr *)p;
869 for (i = 0; i < cnt; i++) {
872 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
874 len -= sizeof(struct in_addr);
877 fprintf(stderr, "warning: %u trailing bytes from %s\n",
878 (unsigned int)len, "net.inet.ip.mcast.filters");
883 #undef MAX_SYSCTL_TRY
890 * Retrieve MLD per-group source filter mode and lists via sysctl.
892 * Note: The 128-bit IPv6 group addres needs to be segmented into
893 * 32-bit pieces for marshaling to sysctl. So the MIB name ends
894 * up looking like this:
895 * a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
896 * Assumes that pgroup originated from the kernel, so its components
897 * are already in network-byte order.
900 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
902 #define MAX_SYSCTL_TRY 5
903 char addrbuf[INET6_ADDRSTRLEN];
913 struct in6_addr *pina;
918 mibsize = sizeof(mib) / sizeof(mib[0]);
919 if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
921 perror("sysctlnametomib");
928 for (i = 0; i < 4; i++)
931 mibsize = sizeof(mib) / sizeof(mib[0]);
933 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
934 perror("sysctl net.inet6.ip6.mcast.filters");
937 if ((buf = malloc(needed)) == NULL) {
941 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
942 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
949 } while (buf == NULL);
952 if (len < sizeof(uint32_t)) {
959 len -= sizeof(uint32_t);
961 modestr = inm_mode(fmode);
963 printf(" mode %s", modestr);
965 printf(" mode (%u)", fmode);
970 cnt = len / sizeof(struct in6_addr);
971 pina = (struct in6_addr *)p;
973 for (i = 0; i < cnt; i++) {
976 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
978 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
979 len -= sizeof(struct in6_addr);
982 fprintf(stderr, "warning: %u trailing bytes from %s\n",
983 (unsigned int)len, "net.inet6.ip6.mcast.filters");
988 #undef MAX_SYSCTL_TRY
993 ifmcstat_getifmaddrs(void)
995 char thisifname[IFNAMSIZ];
996 char addrbuf[NI_MAXHOST];
997 struct ifaddrs *ifap, *ifa;
998 struct ifmaddrs *ifmap, *ifma;
999 sockunion_t lastifasa;
1000 sockunion_t *psa, *pgsa, *pllsa, *pifasa;
1003 uint32_t lastifindex, thisifindex;
1011 lastifasa.ss.ss_family = AF_UNSPEC;
1013 if (getifaddrs(&ifap) != 0) {
1014 warn("getifmaddrs");
1018 if (getifmaddrs(&ifmap) != 0) {
1019 warn("getifmaddrs");
1024 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1026 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1029 psa = (sockunion_t *)ifma->ifma_name;
1030 if (psa->sa.sa_family != AF_LINK) {
1032 "WARNING: Kernel returned invalid data.\n");
1037 /* Filter on interface name. */
1038 thisifindex = psa->sdl.sdl_index;
1039 if (ifindex != 0 && thisifindex != ifindex)
1042 /* Filter on address family. */
1043 pgsa = (sockunion_t *)ifma->ifma_addr;
1044 if (af != 0 && pgsa->sa.sa_family != af)
1047 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1048 pcolon = strchr(thisifname, ':');
1052 /* Only print the banner for the first ifmaddrs entry. */
1053 if (lastifindex == 0 || lastifindex != thisifindex) {
1054 lastifindex = thisifindex;
1055 fprintf(stdout, "%s:\n", thisifname);
1059 * Currently, multicast joins only take place on the
1060 * primary IPv4 address, and only on the link-local IPv6
1061 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1062 * Therefore, we only look up the primary address on
1066 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1067 if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1068 (ifa->ifa_addr == NULL) ||
1069 (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1072 * For AF_INET6 only the link-local address should
1073 * be returned. If built without IPv6 support,
1074 * skip this address entirely.
1076 pifasa = (sockunion_t *)ifa->ifa_addr;
1077 if (pifasa->sa.sa_family == AF_INET6
1079 && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1088 continue; /* primary address not found */
1090 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1093 /* Parse and print primary address, if not already printed. */
1094 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1095 ((lastifasa.ss.ss_family == AF_LINK &&
1096 !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1097 !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1099 switch (pifasa->sa.sa_family) {
1110 pafname = "unknown";
1114 switch (pifasa->sa.sa_family) {
1119 inet6_n2a(&pifasa->sin6.sin6_addr);
1120 strlcpy(addrbuf, p, sizeof(addrbuf));
1128 error = getnameinfo(&pifasa->sa,
1130 addrbuf, sizeof(addrbuf), NULL, 0,
1133 perror("getnameinfo");
1140 fprintf(stdout, "\t%s %s\n", pafname, addrbuf);
1143 * Print per-link IGMP information, if available.
1145 if (pifasa->sa.sa_family == AF_INET) {
1146 struct igmp_ifinfo igi;
1147 size_t mibsize, len;
1150 mibsize = sizeof(mib) / sizeof(mib[0]);
1151 if (sysctlnametomib("net.inet.igmp.ifinfo",
1152 mib, &mibsize) == -1) {
1153 perror("sysctlnametomib");
1156 mib[mibsize] = thisifindex;
1157 len = sizeof(struct igmp_ifinfo);
1158 if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1160 perror("sysctl net.inet.igmp.ifinfo");
1168 * Print per-link MLD information, if available.
1170 if (pifasa->sa.sa_family == AF_INET6) {
1171 struct mld_ifinfo mli;
1172 size_t mibsize, len;
1175 mibsize = sizeof(mib) / sizeof(mib[0]);
1176 if (sysctlnametomib("net.inet6.mld.ifinfo",
1177 mib, &mibsize) == -1) {
1178 perror("sysctlnametomib");
1181 mib[mibsize] = thisifindex;
1182 len = sizeof(struct mld_ifinfo);
1183 if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1185 perror("sysctl net.inet6.mld.ifinfo");
1191 #if defined(INET) || defined(INET6)
1194 lastifasa = *pifasa;
1197 /* Print this group address. */
1199 if (pgsa->sa.sa_family == AF_INET6) {
1200 const char *p = inet6_n2a(&pgsa->sin6.sin6_addr);
1201 strlcpy(addrbuf, p, sizeof(addrbuf));
1205 error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1206 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1208 perror("getnameinfo");
1211 fprintf(stdout, "\t\tgroup %s", addrbuf);
1213 if (pgsa->sa.sa_family == AF_INET) {
1214 inm_print_sources_sysctl(thisifindex,
1215 pgsa->sin.sin_addr);
1219 if (pgsa->sa.sa_family == AF_INET6) {
1220 in6m_print_sources_sysctl(thisifindex,
1221 &pgsa->sin6.sin6_addr);
1224 fprintf(stdout, "\n");
1226 /* Link-layer mapping, if present. */
1227 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1228 if (pllsa != NULL) {
1229 error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1230 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1231 fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1236 freeifmaddrs(ifmap);