[Bug 265540] EADDRNOTAVAIL when leaving IPv6 multicast group and ipv6_mreq.ipv6mr_interface=0

From: <bugzilla-noreply_at_freebsd.org>
Date: Sun, 31 Jul 2022 19:29:36 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=265540

            Bug ID: 265540
           Summary: EADDRNOTAVAIL when leaving IPv6 multicast group and
                    ipv6_mreq.ipv6mr_interface=0
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: net@FreeBSD.org
          Reporter: dmgk@freebsd.org

Created attachment 235586
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=235586&action=edit
mcast.c

When porting some Linux code, I noticed that when ipv6_mreq.ipv6mr_interface=0,
it is possible to join an IPv6 multicast group, but setsockopts() returns
EADDRNOTAVAIL when leaving. Running attached mcast.c on 14.0-CURRENT:

$ ./mcast
mcast: addr: ff02::42
mcast: setsockopt: join ok
mcast: setsockopt: IPV6_LEAVE_GROUP: Can't assign requested address

I'm not sure whether this is a correct fix, but I think the lookup code in
in6p_leave_group() is missing an in6_setscope() call:

--- a/sys/netinet6/in6_mcast.c
+++ b/sys/netinet6/in6_mcast.c
@@ -2312,6 +2312,9 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
                            "ifp for group %s.", __func__,
                            ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
                        ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
+                       if (ifp == NULL)
+                               return (EADDRNOTAVAIL);
+                       (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
                } else {
                        NET_EPOCH_ENTER(et);
                        ifp = ifnet_byindex(ifindex);

in6p_join_group() calls in6_setscope() in in6_mcast.c:1996, but the lookup by
im6o_match_group() in in6_mcast.c:2336 is performed using unscoped address.

-- 
You are receiving this mail because:
You are the assignee for the bug.