1 // SPDX-License-Identifier: GPL-2.0-only
3 * Vxlan multicast group handling
6 #include <linux/kernel.h>
7 #include <net/net_namespace.h>
9 #include <linux/igmp.h>
10 #include <net/vxlan.h>
12 #include "vxlan_private.h"
14 /* Update multicast group membership when first VNI on
15 * multicast address is brought up
17 int vxlan_igmp_join(struct vxlan_dev *vxlan, union vxlan_addr *rip,
20 union vxlan_addr *ip = (rip ? : &vxlan->default_dst.remote_ip);
21 int ifindex = (rifindex ? : vxlan->default_dst.remote_ifindex);
25 if (ip->sa.sa_family == AF_INET) {
26 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
27 struct ip_mreqn mreq = {
28 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
29 .imr_ifindex = ifindex,
34 ret = ip_mc_join_group(sk, &mreq);
36 #if IS_ENABLED(CONFIG_IPV6)
38 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
42 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
51 int vxlan_igmp_leave(struct vxlan_dev *vxlan, union vxlan_addr *rip,
54 union vxlan_addr *ip = (rip ? : &vxlan->default_dst.remote_ip);
55 int ifindex = (rifindex ? : vxlan->default_dst.remote_ifindex);
59 if (ip->sa.sa_family == AF_INET) {
60 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
61 struct ip_mreqn mreq = {
62 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
63 .imr_ifindex = ifindex,
68 ret = ip_mc_leave_group(sk, &mreq);
70 #if IS_ENABLED(CONFIG_IPV6)
72 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
76 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
85 /* See if multicast group is already in use by other ID */
86 bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev,
87 union vxlan_addr *rip, int rifindex)
89 union vxlan_addr *ip = (rip ? : &dev->default_dst.remote_ip);
90 int ifindex = (rifindex ? : dev->default_dst.remote_ifindex);
91 struct vxlan_dev *vxlan;
92 struct vxlan_sock *sock4;
93 #if IS_ENABLED(CONFIG_IPV6)
94 struct vxlan_sock *sock6;
96 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
98 sock4 = rtnl_dereference(dev->vn4_sock);
100 /* The vxlan_sock is only used by dev, leaving group has
101 * no effect on other vxlan devices.
103 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
106 #if IS_ENABLED(CONFIG_IPV6)
107 sock6 = rtnl_dereference(dev->vn6_sock);
108 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
112 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
113 if (!netif_running(vxlan->dev) || vxlan == dev)
116 if (family == AF_INET &&
117 rtnl_dereference(vxlan->vn4_sock) != sock4)
119 #if IS_ENABLED(CONFIG_IPV6)
120 if (family == AF_INET6 &&
121 rtnl_dereference(vxlan->vn6_sock) != sock6)
124 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, ip))
127 if (vxlan->default_dst.remote_ifindex != ifindex)