Skip to content

Commit a498c59

Browse files
Roopa Prabhudavem330
Roopa Prabhu
authored andcommitted
vxlan_multicast: Move multicast helpers to a separate file
subsequent patches will add more helpers. Signed-off-by: Roopa Prabhu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 7b8135f commit a498c59

File tree

4 files changed

+142
-124
lines changed

4 files changed

+142
-124
lines changed

drivers/net/vxlan/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
obj-$(CONFIG_VXLAN) += vxlan.o
66

7-
vxlan-objs := vxlan_core.o
7+
vxlan-objs := vxlan_core.o vxlan_multicast.o

drivers/net/vxlan/vxlan_core.c

-123
Original file line numberDiff line numberDiff line change
@@ -1445,58 +1445,6 @@ static bool vxlan_snoop(struct net_device *dev,
14451445
return false;
14461446
}
14471447

1448-
/* See if multicast group is already in use by other ID */
1449-
static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev,
1450-
union vxlan_addr *rip, int rifindex)
1451-
{
1452-
union vxlan_addr *ip = (rip ? : &dev->default_dst.remote_ip);
1453-
int ifindex = (rifindex ? : dev->default_dst.remote_ifindex);
1454-
struct vxlan_dev *vxlan;
1455-
struct vxlan_sock *sock4;
1456-
#if IS_ENABLED(CONFIG_IPV6)
1457-
struct vxlan_sock *sock6;
1458-
#endif
1459-
unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1460-
1461-
sock4 = rtnl_dereference(dev->vn4_sock);
1462-
1463-
/* The vxlan_sock is only used by dev, leaving group has
1464-
* no effect on other vxlan devices.
1465-
*/
1466-
if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1467-
return false;
1468-
#if IS_ENABLED(CONFIG_IPV6)
1469-
sock6 = rtnl_dereference(dev->vn6_sock);
1470-
if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1471-
return false;
1472-
#endif
1473-
1474-
list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1475-
if (!netif_running(vxlan->dev) || vxlan == dev)
1476-
continue;
1477-
1478-
if (family == AF_INET &&
1479-
rtnl_dereference(vxlan->vn4_sock) != sock4)
1480-
continue;
1481-
#if IS_ENABLED(CONFIG_IPV6)
1482-
if (family == AF_INET6 &&
1483-
rtnl_dereference(vxlan->vn6_sock) != sock6)
1484-
continue;
1485-
#endif
1486-
1487-
if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1488-
ip))
1489-
continue;
1490-
1491-
if (vxlan->default_dst.remote_ifindex != ifindex)
1492-
continue;
1493-
1494-
return true;
1495-
}
1496-
1497-
return false;
1498-
}
1499-
15001448
static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
15011449
{
15021450
struct vxlan_net *vn;
@@ -1545,77 +1493,6 @@ static void vxlan_sock_release(struct vxlan_dev *vxlan)
15451493
#endif
15461494
}
15471495

1548-
/* Update multicast group membership when first VNI on
1549-
* multicast address is brought up
1550-
*/
1551-
static int vxlan_igmp_join(struct vxlan_dev *vxlan, union vxlan_addr *rip,
1552-
int rifindex)
1553-
{
1554-
union vxlan_addr *ip = (rip ? : &vxlan->default_dst.remote_ip);
1555-
int ifindex = (rifindex ? : vxlan->default_dst.remote_ifindex);
1556-
int ret = -EINVAL;
1557-
struct sock *sk;
1558-
1559-
if (ip->sa.sa_family == AF_INET) {
1560-
struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1561-
struct ip_mreqn mreq = {
1562-
.imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1563-
.imr_ifindex = ifindex,
1564-
};
1565-
1566-
sk = sock4->sock->sk;
1567-
lock_sock(sk);
1568-
ret = ip_mc_join_group(sk, &mreq);
1569-
release_sock(sk);
1570-
#if IS_ENABLED(CONFIG_IPV6)
1571-
} else {
1572-
struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1573-
1574-
sk = sock6->sock->sk;
1575-
lock_sock(sk);
1576-
ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1577-
&ip->sin6.sin6_addr);
1578-
release_sock(sk);
1579-
#endif
1580-
}
1581-
1582-
return ret;
1583-
}
1584-
1585-
static int vxlan_igmp_leave(struct vxlan_dev *vxlan, union vxlan_addr *rip,
1586-
int rifindex)
1587-
{
1588-
union vxlan_addr *ip = (rip ? : &vxlan->default_dst.remote_ip);
1589-
int ifindex = (rifindex ? : vxlan->default_dst.remote_ifindex);
1590-
int ret = -EINVAL;
1591-
struct sock *sk;
1592-
1593-
if (ip->sa.sa_family == AF_INET) {
1594-
struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1595-
struct ip_mreqn mreq = {
1596-
.imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1597-
.imr_ifindex = ifindex,
1598-
};
1599-
1600-
sk = sock4->sock->sk;
1601-
lock_sock(sk);
1602-
ret = ip_mc_leave_group(sk, &mreq);
1603-
release_sock(sk);
1604-
#if IS_ENABLED(CONFIG_IPV6)
1605-
} else {
1606-
struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1607-
1608-
sk = sock6->sock->sk;
1609-
lock_sock(sk);
1610-
ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1611-
&ip->sin6.sin6_addr);
1612-
release_sock(sk);
1613-
#endif
1614-
}
1615-
1616-
return ret;
1617-
}
1618-
16191496
static bool vxlan_remcsum(struct vxlanhdr *unparsed,
16201497
struct sk_buff *skb, u32 vxflags)
16211498
{

drivers/net/vxlan/vxlan_multicast.c

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Vxlan multicast group handling
4+
*
5+
*/
6+
#include <linux/kernel.h>
7+
#include <net/net_namespace.h>
8+
#include <net/sock.h>
9+
#include <linux/igmp.h>
10+
#include <net/vxlan.h>
11+
12+
#include "vxlan_private.h"
13+
14+
/* Update multicast group membership when first VNI on
15+
* multicast address is brought up
16+
*/
17+
int vxlan_igmp_join(struct vxlan_dev *vxlan, union vxlan_addr *rip,
18+
int rifindex)
19+
{
20+
union vxlan_addr *ip = (rip ? : &vxlan->default_dst.remote_ip);
21+
int ifindex = (rifindex ? : vxlan->default_dst.remote_ifindex);
22+
int ret = -EINVAL;
23+
struct sock *sk;
24+
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,
30+
};
31+
32+
sk = sock4->sock->sk;
33+
lock_sock(sk);
34+
ret = ip_mc_join_group(sk, &mreq);
35+
release_sock(sk);
36+
#if IS_ENABLED(CONFIG_IPV6)
37+
} else {
38+
struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
39+
40+
sk = sock6->sock->sk;
41+
lock_sock(sk);
42+
ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
43+
&ip->sin6.sin6_addr);
44+
release_sock(sk);
45+
#endif
46+
}
47+
48+
return ret;
49+
}
50+
51+
int vxlan_igmp_leave(struct vxlan_dev *vxlan, union vxlan_addr *rip,
52+
int rifindex)
53+
{
54+
union vxlan_addr *ip = (rip ? : &vxlan->default_dst.remote_ip);
55+
int ifindex = (rifindex ? : vxlan->default_dst.remote_ifindex);
56+
int ret = -EINVAL;
57+
struct sock *sk;
58+
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,
64+
};
65+
66+
sk = sock4->sock->sk;
67+
lock_sock(sk);
68+
ret = ip_mc_leave_group(sk, &mreq);
69+
release_sock(sk);
70+
#if IS_ENABLED(CONFIG_IPV6)
71+
} else {
72+
struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
73+
74+
sk = sock6->sock->sk;
75+
lock_sock(sk);
76+
ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
77+
&ip->sin6.sin6_addr);
78+
release_sock(sk);
79+
#endif
80+
}
81+
82+
return ret;
83+
}
84+
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)
88+
{
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;
95+
#endif
96+
unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
97+
98+
sock4 = rtnl_dereference(dev->vn4_sock);
99+
100+
/* The vxlan_sock is only used by dev, leaving group has
101+
* no effect on other vxlan devices.
102+
*/
103+
if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
104+
return false;
105+
106+
#if IS_ENABLED(CONFIG_IPV6)
107+
sock6 = rtnl_dereference(dev->vn6_sock);
108+
if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
109+
return false;
110+
#endif
111+
112+
list_for_each_entry(vxlan, &vn->vxlan_list, next) {
113+
if (!netif_running(vxlan->dev) || vxlan == dev)
114+
continue;
115+
116+
if (family == AF_INET &&
117+
rtnl_dereference(vxlan->vn4_sock) != sock4)
118+
continue;
119+
#if IS_ENABLED(CONFIG_IPV6)
120+
if (family == AF_INET6 &&
121+
rtnl_dereference(vxlan->vn6_sock) != sock6)
122+
continue;
123+
#endif
124+
if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, ip))
125+
continue;
126+
127+
if (vxlan->default_dst.remote_ifindex != ifindex)
128+
continue;
129+
130+
return true;
131+
}
132+
133+
return false;
134+
}

drivers/net/vxlan/vxlan_private.h

+7
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,11 @@ int vxlan_fdb_update(struct vxlan_dev *vxlan,
112112
__u32 ifindex, __u16 ndm_flags, u32 nhid,
113113
bool swdev_notify, struct netlink_ext_ack *extack);
114114

115+
/* vxlan_multicast.c */
116+
int vxlan_igmp_join(struct vxlan_dev *vxlan, union vxlan_addr *rip,
117+
int rifindex);
118+
int vxlan_igmp_leave(struct vxlan_dev *vxlan, union vxlan_addr *rip,
119+
int rifindex);
120+
bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev,
121+
union vxlan_addr *rip, int rifindex);
115122
#endif

0 commit comments

Comments
 (0)