diff options
author | Eric Dumazet <edumazet@google.com> | 2015-03-18 17:05:35 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2015-03-18 22:00:34 -0400 |
commit | d1e559d0b1b0d02f76a6bd5b768a99dc834ae926 (patch) | |
tree | 283048c1921aeec1c2dfb25e72e6e67cddffcb23 | |
parent | 5b441f76f1b83591e8cd9d60ba1df3a2aacde27f (diff) |
inet: add IPv6 support to sk_ehashfn()
Intent is to converge IPv4 & IPv6 inet_hash functions to
factorize code.
IPv4 sockets initialize sk_rcv_saddr and sk_v6_daddr
in this patch, thanks to new sk_daddr_set() and sk_rcv_saddr_set()
helpers.
__inet6_hash can now use sk_ehashfn() instead of a private
inet6_sk_ehashfn() and will simply use __inet_hash() in a
following patch.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | include/net/inet_hashtables.h | 19 | ||||
-rw-r--r-- | net/dccp/ipv4.c | 9 | ||||
-rw-r--r-- | net/dccp/ipv6.c | 10 | ||||
-rw-r--r-- | net/ipv4/inet_hashtables.c | 11 | ||||
-rw-r--r-- | net/ipv4/tcp_ipv4.c | 8 | ||||
-rw-r--r-- | net/ipv6/inet6_hashtables.c | 22 | ||||
-rw-r--r-- | net/ipv6/tcp_ipv6.c | 11 |
7 files changed, 45 insertions, 45 deletions
diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h index ef993ef571ea..06ad42182ec2 100644 --- a/include/net/inet_hashtables.h +++ b/include/net/inet_hashtables.h | |||
@@ -384,6 +384,25 @@ static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo, | |||
384 | } | 384 | } |
385 | 385 | ||
386 | u32 sk_ehashfn(const struct sock *sk); | 386 | u32 sk_ehashfn(const struct sock *sk); |
387 | u32 inet6_ehashfn(const struct net *net, | ||
388 | const struct in6_addr *laddr, const u16 lport, | ||
389 | const struct in6_addr *faddr, const __be16 fport); | ||
390 | |||
391 | static inline void sk_daddr_set(struct sock *sk, __be32 addr) | ||
392 | { | ||
393 | sk->sk_daddr = addr; /* alias of inet_daddr */ | ||
394 | #if IS_ENABLED(CONFIG_IPV6) | ||
395 | ipv6_addr_set_v4mapped(addr, &sk->sk_v6_daddr); | ||
396 | #endif | ||
397 | } | ||
398 | |||
399 | static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr) | ||
400 | { | ||
401 | sk->sk_rcv_saddr = addr; /* alias of inet_rcv_saddr */ | ||
402 | #if IS_ENABLED(CONFIG_IPV6) | ||
403 | ipv6_addr_set_v4mapped(addr, &sk->sk_v6_rcv_saddr); | ||
404 | #endif | ||
405 | } | ||
387 | 406 | ||
388 | int __inet_hash_connect(struct inet_timewait_death_row *death_row, | 407 | int __inet_hash_connect(struct inet_timewait_death_row *death_row, |
389 | struct sock *sk, u32 port_offset, | 408 | struct sock *sk, u32 port_offset, |
diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c index bf897829f4f0..f3f8906f482e 100644 --- a/net/dccp/ipv4.c +++ b/net/dccp/ipv4.c | |||
@@ -89,10 +89,9 @@ int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |||
89 | 89 | ||
90 | if (inet->inet_saddr == 0) | 90 | if (inet->inet_saddr == 0) |
91 | inet->inet_saddr = fl4->saddr; | 91 | inet->inet_saddr = fl4->saddr; |
92 | inet->inet_rcv_saddr = inet->inet_saddr; | 92 | sk_rcv_saddr_set(sk, inet->inet_saddr); |
93 | |||
94 | inet->inet_dport = usin->sin_port; | 93 | inet->inet_dport = usin->sin_port; |
95 | inet->inet_daddr = daddr; | 94 | sk_daddr_set(sk, daddr); |
96 | 95 | ||
97 | inet_csk(sk)->icsk_ext_hdr_len = 0; | 96 | inet_csk(sk)->icsk_ext_hdr_len = 0; |
98 | if (inet_opt) | 97 | if (inet_opt) |
@@ -408,8 +407,8 @@ struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb, | |||
408 | 407 | ||
409 | newinet = inet_sk(newsk); | 408 | newinet = inet_sk(newsk); |
410 | ireq = inet_rsk(req); | 409 | ireq = inet_rsk(req); |
411 | newinet->inet_daddr = ireq->ir_rmt_addr; | 410 | sk_daddr_set(newsk, ireq->ir_rmt_addr); |
412 | newinet->inet_rcv_saddr = ireq->ir_loc_addr; | 411 | sk_rcv_saddr_set(newsk, ireq->ir_loc_addr); |
413 | newinet->inet_saddr = ireq->ir_loc_addr; | 412 | newinet->inet_saddr = ireq->ir_loc_addr; |
414 | newinet->inet_opt = ireq->opt; | 413 | newinet->inet_opt = ireq->opt; |
415 | ireq->opt = NULL; | 414 | ireq->opt = NULL; |
diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c index d7e7c7b0a3f1..9216d173dd5f 100644 --- a/net/dccp/ipv6.c +++ b/net/dccp/ipv6.c | |||
@@ -470,11 +470,7 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk, | |||
470 | 470 | ||
471 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); | 471 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); |
472 | 472 | ||
473 | ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr); | 473 | newnp->saddr = newsk->sk_v6_rcv_saddr; |
474 | |||
475 | ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr); | ||
476 | |||
477 | newsk->sk_v6_rcv_saddr = newnp->saddr; | ||
478 | 474 | ||
479 | inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped; | 475 | inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped; |
480 | newsk->sk_backlog_rcv = dccp_v4_do_rcv; | 476 | newsk->sk_backlog_rcv = dccp_v4_do_rcv; |
@@ -917,9 +913,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr, | |||
917 | sk->sk_backlog_rcv = dccp_v6_do_rcv; | 913 | sk->sk_backlog_rcv = dccp_v6_do_rcv; |
918 | goto failure; | 914 | goto failure; |
919 | } | 915 | } |
920 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); | 916 | np->saddr = sk->sk_v6_rcv_saddr; |
921 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &sk->sk_v6_rcv_saddr); | ||
922 | |||
923 | return err; | 917 | return err; |
924 | } | 918 | } |
925 | 919 | ||
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 3a86dfd7ae33..ab7f677a97db 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c | |||
@@ -36,9 +36,18 @@ static u32 inet_ehashfn(const struct net *net, const __be32 laddr, | |||
36 | inet_ehash_secret + net_hash_mix(net)); | 36 | inet_ehash_secret + net_hash_mix(net)); |
37 | } | 37 | } |
38 | 38 | ||
39 | 39 | /* This function handles inet_sock, but also timewait and request sockets | |
40 | * for IPv4/IPv6. | ||
41 | */ | ||
40 | u32 sk_ehashfn(const struct sock *sk) | 42 | u32 sk_ehashfn(const struct sock *sk) |
41 | { | 43 | { |
44 | #if IS_ENABLED(CONFIG_IPV6) | ||
45 | if (sk->sk_family == AF_INET6 && | ||
46 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) | ||
47 | return inet6_ehashfn(sock_net(sk), | ||
48 | &sk->sk_v6_rcv_saddr, sk->sk_num, | ||
49 | &sk->sk_v6_daddr, sk->sk_dport); | ||
50 | #endif | ||
42 | return inet_ehashfn(sock_net(sk), | 51 | return inet_ehashfn(sock_net(sk), |
43 | sk->sk_rcv_saddr, sk->sk_num, | 52 | sk->sk_rcv_saddr, sk->sk_num, |
44 | sk->sk_daddr, sk->sk_dport); | 53 | sk->sk_daddr, sk->sk_dport); |
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 80067d5858b4..ca207df4af1c 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c | |||
@@ -189,7 +189,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |||
189 | 189 | ||
190 | if (!inet->inet_saddr) | 190 | if (!inet->inet_saddr) |
191 | inet->inet_saddr = fl4->saddr; | 191 | inet->inet_saddr = fl4->saddr; |
192 | inet->inet_rcv_saddr = inet->inet_saddr; | 192 | sk_rcv_saddr_set(sk, inet->inet_saddr); |
193 | 193 | ||
194 | if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) { | 194 | if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) { |
195 | /* Reset inherited state */ | 195 | /* Reset inherited state */ |
@@ -204,7 +204,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) | |||
204 | tcp_fetch_timewait_stamp(sk, &rt->dst); | 204 | tcp_fetch_timewait_stamp(sk, &rt->dst); |
205 | 205 | ||
206 | inet->inet_dport = usin->sin_port; | 206 | inet->inet_dport = usin->sin_port; |
207 | inet->inet_daddr = daddr; | 207 | sk_daddr_set(sk, daddr); |
208 | 208 | ||
209 | inet_csk(sk)->icsk_ext_hdr_len = 0; | 209 | inet_csk(sk)->icsk_ext_hdr_len = 0; |
210 | if (inet_opt) | 210 | if (inet_opt) |
@@ -1319,8 +1319,8 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb, | |||
1319 | newtp = tcp_sk(newsk); | 1319 | newtp = tcp_sk(newsk); |
1320 | newinet = inet_sk(newsk); | 1320 | newinet = inet_sk(newsk); |
1321 | ireq = inet_rsk(req); | 1321 | ireq = inet_rsk(req); |
1322 | newinet->inet_daddr = ireq->ir_rmt_addr; | 1322 | sk_daddr_set(newsk, ireq->ir_rmt_addr); |
1323 | newinet->inet_rcv_saddr = ireq->ir_loc_addr; | 1323 | sk_rcv_saddr_set(newsk, ireq->ir_loc_addr); |
1324 | newinet->inet_saddr = ireq->ir_loc_addr; | 1324 | newinet->inet_saddr = ireq->ir_loc_addr; |
1325 | inet_opt = ireq->opt; | 1325 | inet_opt = ireq->opt; |
1326 | rcu_assign_pointer(newinet->inet_opt, inet_opt); | 1326 | rcu_assign_pointer(newinet->inet_opt, inet_opt); |
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c index df7df99d1d7e..ed5787b20192 100644 --- a/net/ipv6/inet6_hashtables.c +++ b/net/ipv6/inet6_hashtables.c | |||
@@ -23,11 +23,9 @@ | |||
23 | #include <net/secure_seq.h> | 23 | #include <net/secure_seq.h> |
24 | #include <net/ip.h> | 24 | #include <net/ip.h> |
25 | 25 | ||
26 | static u32 inet6_ehashfn(const struct net *net, | 26 | u32 inet6_ehashfn(const struct net *net, |
27 | const struct in6_addr *laddr, | 27 | const struct in6_addr *laddr, const u16 lport, |
28 | const u16 lport, | 28 | const struct in6_addr *faddr, const __be16 fport) |
29 | const struct in6_addr *faddr, | ||
30 | const __be16 fport) | ||
31 | { | 29 | { |
32 | static u32 inet6_ehash_secret __read_mostly; | 30 | static u32 inet6_ehash_secret __read_mostly; |
33 | static u32 ipv6_hash_secret __read_mostly; | 31 | static u32 ipv6_hash_secret __read_mostly; |
@@ -44,18 +42,6 @@ static u32 inet6_ehashfn(const struct net *net, | |||
44 | inet6_ehash_secret + net_hash_mix(net)); | 42 | inet6_ehash_secret + net_hash_mix(net)); |
45 | } | 43 | } |
46 | 44 | ||
47 | static int inet6_sk_ehashfn(const struct sock *sk) | ||
48 | { | ||
49 | const struct inet_sock *inet = inet_sk(sk); | ||
50 | const struct in6_addr *laddr = &sk->sk_v6_rcv_saddr; | ||
51 | const struct in6_addr *faddr = &sk->sk_v6_daddr; | ||
52 | const __u16 lport = inet->inet_num; | ||
53 | const __be16 fport = inet->inet_dport; | ||
54 | struct net *net = sock_net(sk); | ||
55 | |||
56 | return inet6_ehashfn(net, laddr, lport, faddr, fport); | ||
57 | } | ||
58 | |||
59 | int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw) | 45 | int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw) |
60 | { | 46 | { |
61 | struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; | 47 | struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; |
@@ -75,7 +61,7 @@ int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw) | |||
75 | struct hlist_nulls_head *list; | 61 | struct hlist_nulls_head *list; |
76 | spinlock_t *lock; | 62 | spinlock_t *lock; |
77 | 63 | ||
78 | sk->sk_hash = hash = inet6_sk_ehashfn(sk); | 64 | sk->sk_hash = hash = sk_ehashfn(sk); |
79 | list = &inet_ehash_bucket(hashinfo, hash)->chain; | 65 | list = &inet_ehash_bucket(hashinfo, hash)->chain; |
80 | lock = inet_ehash_lockp(hashinfo, hash); | 66 | lock = inet_ehash_lockp(hashinfo, hash); |
81 | spin_lock(lock); | 67 | spin_lock(lock); |
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index e4761b22307b..5546df074583 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c | |||
@@ -233,11 +233,8 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, | |||
233 | tp->af_specific = &tcp_sock_ipv6_specific; | 233 | tp->af_specific = &tcp_sock_ipv6_specific; |
234 | #endif | 234 | #endif |
235 | goto failure; | 235 | goto failure; |
236 | } else { | ||
237 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); | ||
238 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, | ||
239 | &sk->sk_v6_rcv_saddr); | ||
240 | } | 236 | } |
237 | np->saddr = sk->sk_v6_rcv_saddr; | ||
241 | 238 | ||
242 | return err; | 239 | return err; |
243 | } | 240 | } |
@@ -1078,11 +1075,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb, | |||
1078 | 1075 | ||
1079 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); | 1076 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); |
1080 | 1077 | ||
1081 | ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr); | 1078 | newnp->saddr = newsk->sk_v6_rcv_saddr; |
1082 | |||
1083 | ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr); | ||
1084 | |||
1085 | newsk->sk_v6_rcv_saddr = newnp->saddr; | ||
1086 | 1079 | ||
1087 | inet_csk(newsk)->icsk_af_ops = &ipv6_mapped; | 1080 | inet_csk(newsk)->icsk_af_ops = &ipv6_mapped; |
1088 | newsk->sk_backlog_rcv = tcp_v4_do_rcv; | 1081 | newsk->sk_backlog_rcv = tcp_v4_do_rcv; |