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>
45 #include <net/if_var.h>
46 #include <net/if_types.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/igmp.h>
56 # include <netinet/if_ether.h>
59 #define SYSCTL_DECL(x)
60 # include <netinet/igmp_var.h>
65 #include <netinet/icmp6.h>
67 # include <netinet6/mld6_var.h>
71 #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_dl_equal(a1, a2) \
120 ((((struct sockaddr_dl *)(a1))->sdl_len == \
121 ((struct sockaddr_dl *)(a2))->sdl_len) && \
122 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
123 LLADDR((struct sockaddr_dl *)(a2)), \
124 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
127 * Most of the code in this utility is to support the use of KVM for
128 * post-mortem debugging of the multicast code.
133 static void if_addrlist(struct ifaddr *);
134 static struct in_multi *
135 in_multientry(struct in_multi *);
139 static void if6_addrlist(struct ifaddr *);
140 static struct in6_multi *
141 in6_multientry(struct in6_multi *);
144 static void kread(u_long, void *, int);
145 static void ll_addrlist(struct ifaddr *);
147 static int ifmcstat_kvm(const char *kernel, const char *core);
149 #define KREAD(addr, buf, type) \
150 kread((u_long)addr, (void *)buf, sizeof(type))
153 struct nlist nl[] = {
154 { "_ifnet", 0, 0, 0, 0, },
159 #endif /* WITH_KVM */
161 static int ifmcstat_getifmaddrs(void);
163 static void in_ifinfo(struct igmp_ifinfo *);
164 static const char * inm_mode(u_int mode);
167 static void in6_ifinfo(struct mld_ifinfo *);
168 static const char * inet6_n2a(struct in6_addr *, uint32_t);
170 int main(int, char **);
177 "usage: ifmcstat [-i interface] [-f address family]"
180 " [-K] [-M core] [-N system]"
186 static const char *options = "i:f:vM:N:"
193 main(int argc, char **argv)
197 const char *kernel = NULL;
198 const char *core = NULL;
201 while ((c = getopt(argc, argv, options)) != -1) {
204 if ((ifindex = if_nametoindex(optarg)) == 0) {
205 fprintf(stderr, "%s: unknown interface\n",
213 if (strcmp(optarg, "inet") == 0) {
219 if (strcmp(optarg, "inet6") == 0) {
224 if (strcmp(optarg, "link") == 0) {
228 fprintf(stderr, "%s: unknown address family\n", optarg);
245 core = strdup(optarg);
249 kernel = strdup(optarg);
260 if (af == AF_LINK && vflag)
265 error = ifmcstat_kvm(kernel, core);
267 * If KVM failed, and user did not explicitly specify a core file,
268 * or force KVM backend to be disabled, try the sysctl backend.
270 if (!Kflag || (error != 0 && (core == NULL && kernel == NULL)))
272 error = ifmcstat_getifmaddrs();
283 in_ifinfo(struct igmp_ifinfo *igi)
287 switch (igi->igi_version) {
291 printf("igmpv%d", igi->igi_version);
294 printf("igmpv?(%d)", igi->igi_version);
298 printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
299 if (igi->igi_version == IGMP_VERSION_3) {
300 printf(" rv %u qi %u qri %u uri %u",
301 igi->igi_rv, igi->igi_qi, igi->igi_qri, igi->igi_uri);
304 printf(" v1timer %u v2timer %u v3timer %u",
305 igi->igi_v1_timer, igi->igi_v2_timer, igi->igi_v3_timer);
310 static const char *inm_modes[] = {
320 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
321 return (inm_modes[mode]);
330 ifmcstat_kvm(const char *kernel, const char *core)
332 char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
333 struct ifnet *ifp, *nifp, ifnet;
335 if ((kvmd = kvm_openfiles(kernel, core, NULL, O_RDONLY, buf)) ==
337 perror("kvm_openfiles");
340 if (kvm_nlist(kvmd, nl) < 0) {
344 if (nl[N_IFNET].n_value == 0) {
345 printf("symbol %s not found\n", nl[N_IFNET].n_name);
348 KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
350 KREAD(ifp, &ifnet, struct ifnet);
351 nifp = ifnet.if_link.tqe_next;
352 if (ifindex && ifindex != ifnet.if_index)
355 printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
357 if_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
360 if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
363 ll_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
372 kread(u_long addr, void *buf, int len)
375 if (kvm_read(kvmd, addr, buf, len) != len) {
382 ll_addrlist(struct ifaddr *ifap)
384 char addrbuf[NI_MAXHOST];
387 struct sockaddr_dl sdl;
388 struct ifaddr *ifap0;
390 if (af && af != AF_LINK)
395 KREAD(ifap, &ifa, struct ifaddr);
396 if (ifa.ifa_addr == NULL)
398 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
399 if (sa.sa_family != PF_LINK)
401 KREAD(ifa.ifa_addr, &sdl, struct sockaddr_dl);
402 if (sdl.sdl_alen == 0)
405 getnameinfo((struct sockaddr *)&sdl, sdl.sdl_len,
406 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
407 printf("\tlink %s\n", addrbuf);
409 ifap = ifa.ifa_link.tqe_next;
413 struct ifmultiaddr ifm, *ifmp = 0;
415 KREAD(ifap0, &ifa, struct ifaddr);
416 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
417 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
418 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
420 KREAD(ifmp, &ifm, struct ifmultiaddr);
421 if (ifm.ifma_addr == NULL)
423 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
424 if (sa.sa_family != AF_LINK)
426 KREAD(ifm.ifma_addr, &sdl, struct sockaddr_dl);
428 getnameinfo((struct sockaddr *)&sdl,
429 sdl.sdl_len, addrbuf, sizeof(addrbuf),
430 NULL, 0, NI_NUMERICHOST);
431 printf("\t\tgroup %s refcnt %d\n",
432 addrbuf, ifm.ifma_refcount);
434 ifmp = TAILQ_NEXT(&ifm, ifma_link);
442 if6_addrlist(struct ifaddr *ifap)
447 struct in6_ifaddr if6a;
448 struct ifaddr *ifap0;
450 if (af && af != AF_INET6)
454 KREAD(ifap, &ifa, struct ifaddr);
455 if (ifa.ifa_addr == NULL)
457 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
458 if (sa.sa_family != PF_INET6)
460 KREAD(ifap, &if6a, struct in6_ifaddr);
461 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr,
462 if6a.ia_addr.sin6_scope_id));
464 * Print per-link MLD information, if available.
466 if (ifa.ifa_ifp != NULL) {
467 struct in6_ifextra ie;
468 struct mld_ifinfo mli;
470 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
471 KREAD(ifnet.if_afdata[AF_INET6], &ie,
473 if (ie.mld_ifinfo != NULL) {
474 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
479 ifap = ifa.ifa_link.tqe_next;
483 struct ifmultiaddr ifm, *ifmp = 0;
484 struct sockaddr_dl sdl;
486 KREAD(ifap0, &ifa, struct ifaddr);
487 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
488 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
489 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
491 KREAD(ifmp, &ifm, struct ifmultiaddr);
492 if (ifm.ifma_addr == NULL)
494 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
495 if (sa.sa_family != AF_INET6)
497 (void)in6_multientry((struct in6_multi *)
499 if (ifm.ifma_lladdr == 0)
501 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
502 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
503 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
506 ifmp = TAILQ_NEXT(&ifm, ifma_link);
511 static struct in6_multi *
512 in6_multientry(struct in6_multi *mc)
514 struct in6_multi multi;
516 KREAD(mc, &multi, struct in6_multi);
517 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr, 0));
518 printf(" refcnt %u\n", multi.in6m_refcount);
520 return (multi.in6m_entry.le_next);
528 if_addrlist(struct ifaddr *ifap)
534 struct ifaddr *ifap0;
536 if (af && af != AF_INET)
540 KREAD(ifap, &ifa, struct ifaddr);
541 if (ifa.ifa_addr == NULL)
543 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
544 if (sa.sa_family != PF_INET)
546 KREAD(ifap, &ia, struct in_ifaddr);
547 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
549 * Print per-link IGMP information, if available.
551 if (ifa.ifa_ifp != NULL) {
553 struct igmp_ifinfo igi;
555 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
556 KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
557 if (ii.ii_igmp != NULL) {
558 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
563 ifap = ifa.ifa_link.tqe_next;
566 struct ifmultiaddr ifm, *ifmp = 0;
567 struct sockaddr_dl sdl;
569 KREAD(ifap0, &ifa, struct ifaddr);
570 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
571 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
572 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
574 KREAD(ifmp, &ifm, struct ifmultiaddr);
575 if (ifm.ifma_addr == NULL)
577 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
578 if (sa.sa_family != AF_INET)
580 (void)in_multientry((struct in_multi *)
582 if (ifm.ifma_lladdr == 0)
584 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
585 printf("\t\t\tmcast-macaddr %s refcnt %d\n",
586 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
589 ifmp = TAILQ_NEXT(&ifm, ifma_link);
594 static const char *inm_states[] = {
607 inm_state(u_int state)
610 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
611 return (inm_states[state]);
616 static struct ip_msource *
617 ims_min_kvm(struct in_multi *pinm)
619 struct ip_msource ims0;
620 struct ip_msource *tmp, *parent;
623 tmp = RB_ROOT(&pinm->inm_srcs);
626 KREAD(tmp, &ims0, struct ip_msource);
627 tmp = RB_LEFT(&ims0, ims_link);
629 return (parent); /* kva */
632 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
633 static struct ip_msource *
634 ims_next_kvm(struct ip_msource *ims)
636 struct ip_msource ims0, ims1;
637 struct ip_msource *tmp;
639 KREAD(ims, &ims0, struct ip_msource);
640 if (RB_RIGHT(&ims0, ims_link)) {
641 ims = RB_RIGHT(&ims0, ims_link);
642 KREAD(ims, &ims1, struct ip_msource);
643 while ((tmp = RB_LEFT(&ims1, ims_link))) {
644 KREAD(tmp, &ims0, struct ip_msource);
645 ims = RB_LEFT(&ims0, ims_link);
648 tmp = RB_PARENT(&ims0, ims_link);
650 KREAD(tmp, &ims1, struct ip_msource);
651 if (ims == RB_LEFT(&ims1, ims_link))
654 while ((tmp = RB_PARENT(&ims0, ims_link))) {
655 KREAD(tmp, &ims1, struct ip_msource);
656 if (ims == RB_RIGHT(&ims1, ims_link)) {
658 KREAD(ims, &ims0, struct ip_msource);
662 ims = RB_PARENT(&ims0, ims_link);
665 return (ims); /* kva */
669 inm_print_sources_kvm(struct in_multi *pinm)
671 struct ip_msource ims0;
672 struct ip_msource *ims;
678 fmode = pinm->inm_st[1].iss_fmode;
679 if (fmode == MCAST_UNDEFINED)
681 for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
684 KREAD(ims, &ims0, struct ip_msource);
685 /* Only print sources in-mode at t1. */
686 if (fmode != ims_get_mode(pinm, ims, 1))
688 src.s_addr = htonl(ims0.ims_haddr);
689 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
694 static struct in_multi *
695 in_multientry(struct in_multi *pinm)
698 const char *state, *mode;
700 KREAD(pinm, &inm, struct in_multi);
701 printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
702 printf(" refcnt %u", inm.inm_refcount);
704 state = inm_state(inm.inm_state);
706 printf(" state %s", state);
708 printf(" state (%d)", inm.inm_state);
710 mode = inm_mode(inm.inm_st[1].iss_fmode);
712 printf(" mode %s", mode);
714 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
717 printf(" asm %u ex %u in %u rec %u",
718 (u_int)inm.inm_st[1].iss_asm,
719 (u_int)inm.inm_st[1].iss_ex,
720 (u_int)inm.inm_st[1].iss_in,
721 (u_int)inm.inm_st[1].iss_rec);
727 inm_print_sources_kvm(&inm);
736 #endif /* WITH_KVM */
741 in6_ifinfo(struct mld_ifinfo *mli)
745 switch (mli->mli_version) {
748 printf("mldv%d", mli->mli_version);
751 printf("mldv?(%d)", mli->mli_version);
755 printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
756 if (mli->mli_version == MLD_VERSION_2) {
757 printf(" rv %u qi %u qri %u uri %u",
758 mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
761 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
768 inet6_n2a(struct in6_addr *p, uint32_t scope_id)
770 static char buf[NI_MAXHOST];
771 struct sockaddr_in6 sin6;
772 const int niflags = NI_NUMERICHOST;
774 memset(&sin6, 0, sizeof(sin6));
775 sin6.sin6_family = AF_INET6;
776 sin6.sin6_len = sizeof(struct sockaddr_in6);
778 sin6.sin6_scope_id = scope_id;
779 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
780 buf, sizeof(buf), NULL, 0, niflags) == 0) {
783 return ("(invalid)");
790 * Retrieve per-group source filter mode and lists via sysctl.
793 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
795 #define MAX_SYSCTL_TRY 5
804 struct in_addr *pina;
809 mibsize = sizeof(mib) / sizeof(mib[0]);
810 if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
811 perror("sysctlnametomib");
817 mib[6] = gina.s_addr; /* 32 bits wide */
818 mibsize = sizeof(mib) / sizeof(mib[0]);
820 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
821 perror("sysctl net.inet.ip.mcast.filters");
824 if ((buf = malloc(needed)) == NULL) {
828 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
829 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
836 } while (buf == NULL);
839 if (len < sizeof(uint32_t)) {
846 len -= sizeof(uint32_t);
848 modestr = inm_mode(fmode);
850 printf(" mode %s", modestr);
852 printf(" mode (%u)", fmode);
857 cnt = len / sizeof(struct in_addr);
858 pina = (struct in_addr *)p;
860 for (i = 0; i < cnt; i++) {
863 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
865 len -= sizeof(struct in_addr);
868 fprintf(stderr, "warning: %u trailing bytes from %s\n",
869 (unsigned int)len, "net.inet.ip.mcast.filters");
874 #undef MAX_SYSCTL_TRY
881 * Retrieve MLD per-group source filter mode and lists via sysctl.
883 * Note: The 128-bit IPv6 group address needs to be segmented into
884 * 32-bit pieces for marshaling to sysctl. So the MIB name ends
885 * up looking like this:
886 * a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
887 * Assumes that pgroup originated from the kernel, so its components
888 * are already in network-byte order.
891 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
893 #define MAX_SYSCTL_TRY 5
894 char addrbuf[INET6_ADDRSTRLEN];
904 struct in6_addr *pina;
909 mibsize = sizeof(mib) / sizeof(mib[0]);
910 if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
912 perror("sysctlnametomib");
919 for (i = 0; i < 4; i++)
922 mibsize = sizeof(mib) / sizeof(mib[0]);
924 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
925 perror("sysctl net.inet6.ip6.mcast.filters");
928 if ((buf = malloc(needed)) == NULL) {
932 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
933 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
940 } while (buf == NULL);
943 if (len < sizeof(uint32_t)) {
950 len -= sizeof(uint32_t);
952 modestr = inm_mode(fmode);
954 printf(" mode %s", modestr);
956 printf(" mode (%u)", fmode);
961 cnt = len / sizeof(struct in6_addr);
962 pina = (struct in6_addr *)p;
964 for (i = 0; i < cnt; i++) {
967 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
969 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
970 len -= sizeof(struct in6_addr);
973 fprintf(stderr, "warning: %u trailing bytes from %s\n",
974 (unsigned int)len, "net.inet6.ip6.mcast.filters");
979 #undef MAX_SYSCTL_TRY
984 ifmcstat_getifmaddrs(void)
986 char thisifname[IFNAMSIZ];
987 char addrbuf[NI_MAXHOST];
988 struct ifaddrs *ifap, *ifa;
989 struct ifmaddrs *ifmap, *ifma;
990 sockunion_t lastifasa;
991 sockunion_t *psa, *pgsa, *pllsa, *pifasa;
994 uint32_t lastifindex, thisifindex;
1002 lastifasa.ss.ss_family = AF_UNSPEC;
1004 if (getifaddrs(&ifap) != 0) {
1005 warn("getifmaddrs");
1009 if (getifmaddrs(&ifmap) != 0) {
1010 warn("getifmaddrs");
1015 for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1017 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1020 psa = (sockunion_t *)ifma->ifma_name;
1021 if (psa->sa.sa_family != AF_LINK) {
1023 "WARNING: Kernel returned invalid data.\n");
1028 /* Filter on interface name. */
1029 thisifindex = psa->sdl.sdl_index;
1030 if (ifindex != 0 && thisifindex != ifindex)
1033 /* Filter on address family. */
1034 pgsa = (sockunion_t *)ifma->ifma_addr;
1035 if (af != 0 && pgsa->sa.sa_family != af)
1038 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1039 pcolon = strchr(thisifname, ':');
1043 /* Only print the banner for the first ifmaddrs entry. */
1044 if (lastifindex == 0 || lastifindex != thisifindex) {
1045 lastifindex = thisifindex;
1046 fprintf(stdout, "%s:\n", thisifname);
1050 * Currently, multicast joins only take place on the
1051 * primary IPv4 address, and only on the link-local IPv6
1052 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1053 * Therefore, we only look up the primary address on
1057 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1058 if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1059 (ifa->ifa_addr == NULL) ||
1060 (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1063 * For AF_INET6 only the link-local address should
1064 * be returned. If built without IPv6 support,
1065 * skip this address entirely.
1067 pifasa = (sockunion_t *)ifa->ifa_addr;
1068 if (pifasa->sa.sa_family == AF_INET6
1070 && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1079 continue; /* primary address not found */
1081 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1084 /* Parse and print primary address, if not already printed. */
1085 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1086 ((lastifasa.ss.ss_family == AF_LINK &&
1087 !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1088 !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1090 switch (pifasa->sa.sa_family) {
1101 pafname = "unknown";
1105 switch (pifasa->sa.sa_family) {
1110 inet6_n2a(&pifasa->sin6.sin6_addr,
1111 pifasa->sin6.sin6_scope_id);
1112 strlcpy(addrbuf, p, sizeof(addrbuf));
1120 error = getnameinfo(&pifasa->sa,
1122 addrbuf, sizeof(addrbuf), NULL, 0,
1125 perror("getnameinfo");
1132 fprintf(stdout, "\t%s %s", pafname, addrbuf);
1134 if (pifasa->sa.sa_family == AF_INET6 &&
1135 pifasa->sin6.sin6_scope_id)
1136 fprintf(stdout, " scopeid 0x%x",
1137 pifasa->sin6.sin6_scope_id);
1139 fprintf(stdout, "\n");
1142 * Print per-link IGMP information, if available.
1144 if (pifasa->sa.sa_family == AF_INET) {
1145 struct igmp_ifinfo igi;
1146 size_t mibsize, len;
1149 mibsize = sizeof(mib) / sizeof(mib[0]);
1150 if (sysctlnametomib("net.inet.igmp.ifinfo",
1151 mib, &mibsize) == -1) {
1152 perror("sysctlnametomib");
1155 mib[mibsize] = thisifindex;
1156 len = sizeof(struct igmp_ifinfo);
1157 if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1159 perror("sysctl net.inet.igmp.ifinfo");
1167 * Print per-link MLD information, if available.
1169 if (pifasa->sa.sa_family == AF_INET6) {
1170 struct mld_ifinfo mli;
1171 size_t mibsize, len;
1174 mibsize = sizeof(mib) / sizeof(mib[0]);
1175 if (sysctlnametomib("net.inet6.mld.ifinfo",
1176 mib, &mibsize) == -1) {
1177 perror("sysctlnametomib");
1180 mib[mibsize] = thisifindex;
1181 len = sizeof(struct mld_ifinfo);
1182 if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1184 perror("sysctl net.inet6.mld.ifinfo");
1190 #if defined(INET) || defined(INET6)
1193 lastifasa = *pifasa;
1196 /* Print this group address. */
1198 if (pgsa->sa.sa_family == AF_INET6) {
1199 const char *p = inet6_n2a(&pgsa->sin6.sin6_addr,
1200 pgsa->sin6.sin6_scope_id);
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_INET6 &&
1214 pgsa->sin6.sin6_scope_id)
1215 fprintf(stdout, " scopeid 0x%x",
1216 pgsa->sin6.sin6_scope_id);
1219 if (pgsa->sa.sa_family == AF_INET) {
1220 inm_print_sources_sysctl(thisifindex,
1221 pgsa->sin.sin_addr);
1225 if (pgsa->sa.sa_family == AF_INET6) {
1226 in6m_print_sources_sysctl(thisifindex,
1227 &pgsa->sin6.sin6_addr);
1230 fprintf(stdout, "\n");
1232 /* Link-layer mapping, if present. */
1233 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1234 if (pllsa != NULL) {
1235 error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1236 addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1237 fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1242 freeifmaddrs(ifmap);