Commit d986f521 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

ipv6: lockless IPV6_MULTICAST_LOOP implementation

Add inet6_{test|set|clear|assign}_bit() helpers.

Note that I am using bits from inet->inet_flags,
this might change in the future if we need more flags.

While solving data-races accessing np->mc_loop,
this patch also allows to implement lockless accesses
to np->mcast_hops in the following patch.

Also constify sk_mc_loop() argument.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reviewed-by: default avatarDavid Ahern <dsahern@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent b0adfba7
...@@ -218,11 +218,9 @@ struct ipv6_pinfo { ...@@ -218,11 +218,9 @@ struct ipv6_pinfo {
#if defined(__BIG_ENDIAN_BITFIELD) #if defined(__BIG_ENDIAN_BITFIELD)
/* Packed in 16bits. */ /* Packed in 16bits. */
__s16 mcast_hops:9; __s16 mcast_hops:9;
__u16 __unused_2:6, __u16 __unused_2:7,
mc_loop:1;
#else #else
__u16 mc_loop:1, __u16 __unused_2:7;
__unused_2:6;
__s16 mcast_hops:9; __s16 mcast_hops:9;
#endif #endif
int ucast_oif; int ucast_oif;
...@@ -283,6 +281,18 @@ struct ipv6_pinfo { ...@@ -283,6 +281,18 @@ struct ipv6_pinfo {
struct inet6_cork cork; struct inet6_cork cork;
}; };
/* We currently use available bits from inet_sk(sk)->inet_flags,
* this could change in the future.
*/
#define inet6_test_bit(nr, sk) \
test_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
#define inet6_set_bit(nr, sk) \
set_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
#define inet6_clear_bit(nr, sk) \
clear_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
#define inet6_assign_bit(nr, sk, val) \
assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val)
/* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */ /* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */
struct raw6_sock { struct raw6_sock {
/* inet_sock has to be the first member of raw6_sock */ /* inet_sock has to be the first member of raw6_sock */
......
...@@ -268,6 +268,7 @@ enum { ...@@ -268,6 +268,7 @@ enum {
INET_FLAGS_NODEFRAG = 17, INET_FLAGS_NODEFRAG = 17,
INET_FLAGS_BIND_ADDRESS_NO_PORT = 18, INET_FLAGS_BIND_ADDRESS_NO_PORT = 18,
INET_FLAGS_DEFER_CONNECT = 19, INET_FLAGS_DEFER_CONNECT = 19,
INET_FLAGS_MC6_LOOP = 20,
}; };
/* cmsg flags for inet */ /* cmsg flags for inet */
......
...@@ -2238,7 +2238,7 @@ static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n) ...@@ -2238,7 +2238,7 @@ static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n)
} }
} }
bool sk_mc_loop(struct sock *sk); bool sk_mc_loop(const struct sock *sk);
static inline bool sk_can_gso(const struct sock *sk) static inline bool sk_can_gso(const struct sock *sk)
{ {
......
...@@ -759,7 +759,7 @@ static int sock_getbindtodevice(struct sock *sk, sockptr_t optval, ...@@ -759,7 +759,7 @@ static int sock_getbindtodevice(struct sock *sk, sockptr_t optval,
return ret; return ret;
} }
bool sk_mc_loop(struct sock *sk) bool sk_mc_loop(const struct sock *sk)
{ {
if (dev_recursion_level()) if (dev_recursion_level())
return false; return false;
...@@ -771,7 +771,7 @@ bool sk_mc_loop(struct sock *sk) ...@@ -771,7 +771,7 @@ bool sk_mc_loop(struct sock *sk)
return inet_test_bit(MC_LOOP, sk); return inet_test_bit(MC_LOOP, sk);
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
case AF_INET6: case AF_INET6:
return inet6_sk(sk)->mc_loop; return inet6_test_bit(MC6_LOOP, sk);
#endif #endif
} }
WARN_ON_ONCE(1); WARN_ON_ONCE(1);
......
...@@ -217,7 +217,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, ...@@ -217,7 +217,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,
inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk); inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
np->hop_limit = -1; np->hop_limit = -1;
np->mcast_hops = IPV6_DEFAULT_MCASTHOPS; np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
np->mc_loop = 1; inet6_set_bit(MC6_LOOP, sk);
np->mc_all = 1; np->mc_all = 1;
np->pmtudisc = IPV6_PMTUDISC_WANT; np->pmtudisc = IPV6_PMTUDISC_WANT;
np->repflow = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED; np->repflow = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
......
...@@ -424,6 +424,13 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname, ...@@ -424,6 +424,13 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
return -EINVAL; return -EINVAL;
WRITE_ONCE(np->hop_limit, val); WRITE_ONCE(np->hop_limit, val);
return 0; return 0;
case IPV6_MULTICAST_LOOP:
if (optlen < sizeof(int))
return -EINVAL;
if (val != valbool)
return -EINVAL;
inet6_assign_bit(MC6_LOOP, sk, valbool);
return 0;
} }
if (needs_rtnl) if (needs_rtnl)
rtnl_lock(); rtnl_lock();
...@@ -755,15 +762,6 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname, ...@@ -755,15 +762,6 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
retv = 0; retv = 0;
break; break;
case IPV6_MULTICAST_LOOP:
if (optlen < sizeof(int))
goto e_inval;
if (val != valbool)
goto e_inval;
np->mc_loop = valbool;
retv = 0;
break;
case IPV6_UNICAST_IF: case IPV6_UNICAST_IF:
{ {
struct net_device *dev = NULL; struct net_device *dev = NULL;
...@@ -1367,7 +1365,7 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname, ...@@ -1367,7 +1365,7 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
} }
case IPV6_MULTICAST_LOOP: case IPV6_MULTICAST_LOOP:
val = np->mc_loop; val = inet6_test_bit(MC6_LOOP, sk);
break; break;
case IPV6_MULTICAST_IF: case IPV6_MULTICAST_IF:
......
...@@ -1996,7 +1996,7 @@ static int __net_init ndisc_net_init(struct net *net) ...@@ -1996,7 +1996,7 @@ static int __net_init ndisc_net_init(struct net *net)
np = inet6_sk(sk); np = inet6_sk(sk);
np->hop_limit = 255; np->hop_limit = 255;
/* Do not loopback ndisc messages */ /* Do not loopback ndisc messages */
np->mc_loop = 0; inet6_clear_bit(MC6_LOOP, sk);
return 0; return 0;
} }
......
...@@ -1298,17 +1298,13 @@ static void set_sock_size(struct sock *sk, int mode, int val) ...@@ -1298,17 +1298,13 @@ static void set_sock_size(struct sock *sk, int mode, int val)
static void set_mcast_loop(struct sock *sk, u_char loop) static void set_mcast_loop(struct sock *sk, u_char loop)
{ {
/* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */ /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
lock_sock(sk);
inet_assign_bit(MC_LOOP, sk, loop); inet_assign_bit(MC_LOOP, sk, loop);
#ifdef CONFIG_IP_VS_IPV6 #ifdef CONFIG_IP_VS_IPV6
if (sk->sk_family == AF_INET6) { if (READ_ONCE(sk->sk_family) == AF_INET6) {
struct ipv6_pinfo *np = inet6_sk(sk);
/* IPV6_MULTICAST_LOOP */ /* IPV6_MULTICAST_LOOP */
np->mc_loop = loop ? 1 : 0; inet6_assign_bit(MC6_LOOP, sk, loop);
} }
#endif #endif
release_sock(sk);
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment