Skip to content

Commit

Permalink
ipv6: lockless IPV6_ADDR_PREFERENCES implementation
Browse files Browse the repository at this point in the history
We have data-races while reading np->srcprefs

Switch the field to a plain byte, add READ_ONCE()
and WRITE_ONCE() annotations where needed,
and IPV6_ADDR_PREFERENCES setsockopt() can now be lockless.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Link: https://lore.kernel.org/r/20230918142321.1794107-1-edumazet@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Eric Dumazet authored and Paolo Abeni committed Sep 19, 2023
1 parent 6a23c55 commit fa17a6d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion include/linux/ipv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ struct ipv6_pinfo {
} rxopt;

/* sockopt flags */
__u8 srcprefs:3; /* 001: prefer temporary address
__u8 srcprefs; /* 001: prefer temporary address
* 010: prefer public address
* 100: prefer care-of address
*/
Expand Down
5 changes: 2 additions & 3 deletions include/net/ip6_route.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ struct route_info {
*/
static inline int rt6_srcprefs2flags(unsigned int srcprefs)
{
/* No need to bitmask because srcprefs have only 3 bits. */
return srcprefs << 3;
return (srcprefs & IPV6_PREFER_SRC_MASK) << 3;
}

static inline unsigned int rt6_flags2srcprefs(int flags)
{
return (flags >> 3) & 7;
return (flags >> 3) & IPV6_PREFER_SRC_MASK;
}

static inline bool rt6_need_strict(const struct in6_addr *daddr)
Expand Down
20 changes: 7 additions & 13 deletions include/net/ipv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,13 @@ static inline void ip6_sock_set_recverr(struct sock *sk)
inet6_set_bit(RECVERR6, sk);
}

static inline int __ip6_sock_set_addr_preferences(struct sock *sk, int val)
#define IPV6_PREFER_SRC_MASK (IPV6_PREFER_SRC_TMP | IPV6_PREFER_SRC_PUBLIC | \
IPV6_PREFER_SRC_COA)

static inline int ip6_sock_set_addr_preferences(struct sock *sk, int val)
{
unsigned int prefmask = ~IPV6_PREFER_SRC_MASK;
unsigned int pref = 0;
unsigned int prefmask = ~0;

/* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
switch (val & (IPV6_PREFER_SRC_PUBLIC |
Expand Down Expand Up @@ -1359,20 +1362,11 @@ static inline int __ip6_sock_set_addr_preferences(struct sock *sk, int val)
return -EINVAL;
}

inet6_sk(sk)->srcprefs = (inet6_sk(sk)->srcprefs & prefmask) | pref;
WRITE_ONCE(inet6_sk(sk)->srcprefs,
(READ_ONCE(inet6_sk(sk)->srcprefs) & prefmask) | pref);
return 0;
}

static inline int ip6_sock_set_addr_preferences(struct sock *sk, int val)
{
int ret;

lock_sock(sk);
ret = __ip6_sock_set_addr_preferences(sk, val);
release_sock(sk);
return ret;
}

static inline void ip6_sock_set_recvpktinfo(struct sock *sk)
{
lock_sock(sk);
Expand Down
2 changes: 1 addition & 1 deletion net/ipv6/ip6_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
rcu_read_lock();
from = rt ? rcu_dereference(rt->from) : NULL;
err = ip6_route_get_saddr(net, from, &fl6->daddr,
sk ? inet6_sk(sk)->srcprefs : 0,
sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0,
&fl6->saddr);
rcu_read_unlock();

Expand Down
19 changes: 10 additions & 9 deletions net/ipv6/ipv6_sockglue.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
return -EINVAL;
inet6_assign_bit(SNDFLOW, sk, valbool);
return 0;
case IPV6_ADDR_PREFERENCES:
if (optlen < sizeof(int))
return -EINVAL;
return ip6_sock_set_addr_preferences(sk, val);
}
if (needs_rtnl)
rtnl_lock();
Expand Down Expand Up @@ -964,11 +968,6 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
retv = xfrm_user_policy(sk, optname, optval, optlen);
break;

case IPV6_ADDR_PREFERENCES:
if (optlen < sizeof(int))
goto e_inval;
retv = __ip6_sock_set_addr_preferences(sk, val);
break;
case IPV6_RECVFRAGSIZE:
np->rxopt.bits.recvfragsize = valbool;
retv = 0;
Expand Down Expand Up @@ -1415,23 +1414,25 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
}

case IPV6_ADDR_PREFERENCES:
{
u8 srcprefs = READ_ONCE(np->srcprefs);
val = 0;

if (np->srcprefs & IPV6_PREFER_SRC_TMP)
if (srcprefs & IPV6_PREFER_SRC_TMP)
val |= IPV6_PREFER_SRC_TMP;
else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
else if (srcprefs & IPV6_PREFER_SRC_PUBLIC)
val |= IPV6_PREFER_SRC_PUBLIC;
else {
/* XXX: should we return system default? */
val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
}

if (np->srcprefs & IPV6_PREFER_SRC_COA)
if (srcprefs & IPV6_PREFER_SRC_COA)
val |= IPV6_PREFER_SRC_COA;
else
val |= IPV6_PREFER_SRC_HOME;
break;

}
case IPV6_MINHOPCOUNT:
val = READ_ONCE(np->min_hopcount);
break;
Expand Down
2 changes: 1 addition & 1 deletion net/ipv6/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ static struct dst_entry *ip6_route_output_flags_noref(struct net *net,
if (!any_src)
flags |= RT6_LOOKUP_F_HAS_SADDR;
else if (sk)
flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
flags |= rt6_srcprefs2flags(READ_ONCE(inet6_sk(sk)->srcprefs));

return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
}
Expand Down

0 comments on commit fa17a6d

Please sign in to comment.