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

inet: add READ_ONCE(sk->sk_bound_dev_if) in INET_MATCH()

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

We probably need to annotate most reads.

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

v2:

We remove the 32bit version of it, as modern compilers
should generate the same code really, no need to
try to be smarter.

Also make 'struct net *net' the first argument.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 49bb39bd
...@@ -255,7 +255,6 @@ static inline struct sock *inet_lookup_listener(struct net *net, ...@@ -255,7 +255,6 @@ static inline struct sock *inet_lookup_listener(struct net *net,
((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport))) ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
#endif #endif
#if (BITS_PER_LONG == 64)
#ifdef __BIG_ENDIAN #ifdef __BIG_ENDIAN
#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
const __addrpair __name = (__force __addrpair) ( \ const __addrpair __name = (__force __addrpair) ( \
...@@ -267,24 +266,22 @@ static inline struct sock *inet_lookup_listener(struct net *net, ...@@ -267,24 +266,22 @@ static inline struct sock *inet_lookup_listener(struct net *net,
(((__force __u64)(__be32)(__daddr)) << 32) | \ (((__force __u64)(__be32)(__daddr)) << 32) | \
((__force __u64)(__be32)(__saddr))) ((__force __u64)(__be32)(__saddr)))
#endif /* __BIG_ENDIAN */ #endif /* __BIG_ENDIAN */
#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif, __sdif) \
(((__sk)->sk_portpair == (__ports)) && \ static inline bool INET_MATCH(struct net *net, const struct sock *sk,
((__sk)->sk_addrpair == (__cookie)) && \ const __addrpair cookie, const __portpair ports,
(((__sk)->sk_bound_dev_if == (__dif)) || \ int dif, int sdif)
((__sk)->sk_bound_dev_if == (__sdif))) && \ {
net_eq(sock_net(__sk), (__net))) int bound_dev_if;
#else /* 32-bit arch */
#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ if (!net_eq(sock_net(sk), net) ||
const int __name __deprecated __attribute__((unused)) sk->sk_portpair != ports ||
sk->sk_addrpair != cookie)
#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif, __sdif) \ return false;
(((__sk)->sk_portpair == (__ports)) && \
((__sk)->sk_daddr == (__saddr)) && \ /* Paired with WRITE_ONCE() from sock_bindtoindex_locked() */
((__sk)->sk_rcv_saddr == (__daddr)) && \ bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
(((__sk)->sk_bound_dev_if == (__dif)) || \ return bound_dev_if == dif || bound_dev_if == sdif;
((__sk)->sk_bound_dev_if == (__sdif))) && \ }
net_eq(sock_net(__sk), (__net)))
#endif /* 64-bit arch */
/* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
* not check it for lookups anymore, thanks Alexey. -DaveM * not check it for lookups anymore, thanks Alexey. -DaveM
......
...@@ -161,9 +161,6 @@ typedef __u64 __bitwise __addrpair; ...@@ -161,9 +161,6 @@ typedef __u64 __bitwise __addrpair;
* for struct sock and struct inet_timewait_sock. * for struct sock and struct inet_timewait_sock.
*/ */
struct sock_common { struct sock_common {
/* skc_daddr and skc_rcv_saddr must be grouped on a 8 bytes aligned
* address on 64bit arches : cf INET_MATCH()
*/
union { union {
__addrpair skc_addrpair; __addrpair skc_addrpair;
struct { struct {
......
...@@ -373,13 +373,11 @@ struct sock *__inet_lookup_established(struct net *net, ...@@ -373,13 +373,11 @@ struct sock *__inet_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 (likely(INET_MATCH(sk, net, acookie, if (likely(INET_MATCH(net, sk, acookie, ports, dif, sdif))) {
saddr, daddr, ports, dif, sdif))) {
if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
goto out; goto out;
if (unlikely(!INET_MATCH(sk, net, acookie, if (unlikely(!INET_MATCH(net, sk, acookie,
saddr, daddr, ports, ports, dif, sdif))) {
dif, sdif))) {
sock_gen_put(sk); sock_gen_put(sk);
goto begin; goto begin;
} }
...@@ -428,8 +426,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row, ...@@ -428,8 +426,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
if (sk2->sk_hash != hash) if (sk2->sk_hash != hash)
continue; continue;
if (likely(INET_MATCH(sk2, net, acookie, if (likely(INET_MATCH(net, sk2, acookie, ports, dif, sdif))) {
saddr, daddr, ports, dif, sdif))) {
if (sk2->sk_state == TCP_TIME_WAIT) { if (sk2->sk_state == TCP_TIME_WAIT) {
tw = inet_twsk(sk2); tw = inet_twsk(sk2);
if (twsk_unique(sk, sk2, twp)) if (twsk_unique(sk, sk2, twp))
...@@ -495,9 +492,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk, ...@@ -495,9 +492,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk,
if (esk->sk_hash != sk->sk_hash) if (esk->sk_hash != sk->sk_hash)
continue; continue;
if (sk->sk_family == AF_INET) { if (sk->sk_family == AF_INET) {
if (unlikely(INET_MATCH(esk, net, acookie, if (unlikely(INET_MATCH(net, esk, acookie,
sk->sk_daddr,
sk->sk_rcv_saddr,
ports, dif, sdif))) { ports, dif, sdif))) {
return true; return true;
} }
......
...@@ -2563,8 +2563,7 @@ static struct sock *__udp4_lib_demux_lookup(struct net *net, ...@@ -2563,8 +2563,7 @@ static struct sock *__udp4_lib_demux_lookup(struct net *net,
struct sock *sk; struct sock *sk;
udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
if (INET_MATCH(sk, net, acookie, rmt_addr, if (INET_MATCH(net, sk, acookie, ports, dif, sdif))
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