History

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.

Eric Dumazet committed 4 years ago

Commit 5d368f01 parent ff009404 files · +24 −14Browse files

Unified · Split

4 files
  1. include/net/inet6_hashtables.h +19 −9
  2. net/ipv4/inet_hashtables.c +1 −1
  3. net/ipv6/inet6_hashtables.c +3 −3
  4. net/ipv6/udp.c +1 −1
include/net/inet6_hashtables.h+19−9
@@ -103,15 +103,25 @@ struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, · 102 unmodified lines
103103 const int dif);
104104
105105 int inet6_hash(struct sock *sk);
106−#endif /* IS_ENABLED(CONFIG_IPV6) */
107106
108−#define INET6_MATCH(__sk, __net, __saddr, __daddr, __ports, __dif, __sdif) \
109− (((__sk)->sk_portpair == (__ports)) && \
110− ((__sk)->sk_family == AF_INET6) && \
111− ipv6_addr_equal(&(__sk)->sk_v6_daddr, (__saddr)) && \
112− ipv6_addr_equal(&(__sk)->sk_v6_rcv_saddr, (__daddr)) && \
113− (((__sk)->sk_bound_dev_if == (__dif)) || \
114− ((__sk)->sk_bound_dev_if == (__sdif))) && \
115− net_eq(sock_net(__sk), (__net)))
107+static inline bool inet6_match(struct net *net, const struct sock *sk,
108+ const struct in6_addr *saddr,
109+ const struct in6_addr *daddr,
110+ const __portpair ports,
111+ const int dif, const int sdif)
112+{
113+ int bound_dev_if;
114+
115+ if (!net_eq(sock_net(sk), net) ||
116+ sk->sk_family != AF_INET6 ||
117+ sk->sk_portpair != ports ||
118+ !ipv6_addr_equal(&sk->sk_v6_daddr, saddr) ||
119+ !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
120+ return false;
121+
122+ bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
123+ return bound_dev_if == dif || bound_dev_if == sdif;
124+}
125+#endif /* IS_ENABLED(CONFIG_IPV6) */
116126
117127 #endif /* _INET6_HASHTABLES_H */
net/ipv4/inet_hashtables.c+1−1
@@ -499,7 +499,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk, · 498 unmodified lines
499499 }
500500 #if IS_ENABLED(CONFIG_IPV6)
501501 else if (sk->sk_family == AF_INET6) {
502− if (unlikely(INET6_MATCH(esk, net,
502+ if (unlikely(inet6_match(net, esk,
503503 &sk->sk_v6_daddr,
504504 &sk->sk_v6_rcv_saddr,
505505 ports, dif, sdif))) {
net/ipv6/inet6_hashtables.c+3−3
@@ -71,12 +71,12 @@ begin: · 70 unmodified lines
7171 sk_nulls_for_each_rcu(sk, node, &head->chain) {
7272 if (sk->sk_hash != hash)
7373 continue;
74− if (!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif))
74+ if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
7575 continue;
7676 if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
7777 goto out;
7878
79− if (unlikely(!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif))) {
79+ if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) {
8080 sock_gen_put(sk);
8181 goto begin;
8282 }
@@ -268,7 +268,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row, · 185 unmodified lines
268268 if (sk2->sk_hash != hash)
269269 continue;
270270
271− if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports,
271+ if (likely(inet6_match(net, sk2, saddr, daddr, ports,
272272 dif, sdif))) {
273273 if (sk2->sk_state == TCP_TIME_WAIT) {
274274 tw = inet_twsk(sk2);
net/ipv6/udp.c+1−1
@@ -1044,7 +1044,7 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net, · 1043 unmodified lines
10441044
10451045 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
10461046 if (sk->sk_state == TCP_ESTABLISHED &&
1047− INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
1047+ inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
10481048 return sk;
10491049 /* Only check first socket in chain */
10501050 break;