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

ipv6: add READ_ONCE(sk->sk_bound_dev_if) in INET6_MATCH()

INET6_MATCH() runs without holding a lock on the socket.

We probably need to annotate most reads.

This patch makes INET6_MATCH() an inline function
to ease our changes.

v2: inline function only defined if IS_ENABLED(CONFIG_IPV6)
    Change the name to inet6_match(), this is no longer a macro.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent ff009403
...@@ -103,15 +103,25 @@ struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, ...@@ -103,15 +103,25 @@ struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
const int dif); const int dif);
int inet6_hash(struct sock *sk); int inet6_hash(struct sock *sk);
#endif /* IS_ENABLED(CONFIG_IPV6) */
#define INET6_MATCH(__sk, __net, __saddr, __daddr, __ports, __dif, __sdif) \ static inline bool inet6_match(struct net *net, const struct sock *sk,
(((__sk)->sk_portpair == (__ports)) && \ const struct in6_addr *saddr,
((__sk)->sk_family == AF_INET6) && \ const struct in6_addr *daddr,
ipv6_addr_equal(&(__sk)->sk_v6_daddr, (__saddr)) && \ const __portpair ports,
ipv6_addr_equal(&(__sk)->sk_v6_rcv_saddr, (__daddr)) && \ const int dif, const int sdif)
(((__sk)->sk_bound_dev_if == (__dif)) || \ {
((__sk)->sk_bound_dev_if == (__sdif))) && \ int bound_dev_if;
net_eq(sock_net(__sk), (__net)))
if (!net_eq(sock_net(sk), net) ||
sk->sk_family != AF_INET6 ||
sk->sk_portpair != ports ||
!ipv6_addr_equal(&sk->sk_v6_daddr, saddr) ||
!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
return false;
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
return bound_dev_if == dif || bound_dev_if == sdif;
}
#endif /* IS_ENABLED(CONFIG_IPV6) */
#endif /* _INET6_HASHTABLES_H */ #endif /* _INET6_HASHTABLES_H */
...@@ -499,7 +499,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk, ...@@ -499,7 +499,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk,
} }
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
else if (sk->sk_family == AF_INET6) { else if (sk->sk_family == AF_INET6) {
if (unlikely(INET6_MATCH(esk, net, if (unlikely(inet6_match(net, esk,
&sk->sk_v6_daddr, &sk->sk_v6_daddr,
&sk->sk_v6_rcv_saddr, &sk->sk_v6_rcv_saddr,
ports, dif, sdif))) { ports, dif, sdif))) {
......
...@@ -71,12 +71,12 @@ struct sock *__inet6_lookup_established(struct net *net, ...@@ -71,12 +71,12 @@ struct sock *__inet6_lookup_established(struct net *net,
sk_nulls_for_each_rcu(sk, node, &head->chain) { sk_nulls_for_each_rcu(sk, node, &head->chain) {
if (sk->sk_hash != hash) if (sk->sk_hash != hash)
continue; continue;
if (!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif)) if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
continue; continue;
if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
goto out; goto out;
if (unlikely(!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif))) { if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) {
sock_gen_put(sk); sock_gen_put(sk);
goto begin; goto begin;
} }
...@@ -268,7 +268,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row, ...@@ -268,7 +268,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
if (sk2->sk_hash != hash) if (sk2->sk_hash != hash)
continue; continue;
if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports, if (likely(inet6_match(net, sk2, saddr, daddr, ports,
dif, sdif))) { dif, sdif))) {
if (sk2->sk_state == TCP_TIME_WAIT) { if (sk2->sk_state == TCP_TIME_WAIT) {
tw = inet_twsk(sk2); tw = inet_twsk(sk2);
......
...@@ -1044,7 +1044,7 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net, ...@@ -1044,7 +1044,7 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net,
udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
if (sk->sk_state == TCP_ESTABLISHED && if (sk->sk_state == TCP_ESTABLISHED &&
INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif)) inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
return sk; return sk;
/* Only check first socket in chain */ /* Only check first socket in chain */
break; break;
......
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