Skip to content

Commit d1e559d

Browse files
edumazetdavem330
authored andcommitted
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 <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5b441f7 commit d1e559d

File tree

7 files changed

+45
-45
lines changed

7 files changed

+45
-45
lines changed

include/net/inet_hashtables.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,25 @@ static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
384384
}
385385

386386
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+
}
387406

388407
int __inet_hash_connect(struct inet_timewait_death_row *death_row,
389408
struct sock *sk, u32 port_offset,

net/dccp/ipv4.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
8989

9090
if (inet->inet_saddr == 0)
9191
inet->inet_saddr = fl4->saddr;
92-
inet->inet_rcv_saddr = inet->inet_saddr;
93-
92+
sk_rcv_saddr_set(sk, inet->inet_saddr);
9493
inet->inet_dport = usin->sin_port;
95-
inet->inet_daddr = daddr;
94+
sk_daddr_set(sk, daddr);
9695

9796
inet_csk(sk)->icsk_ext_hdr_len = 0;
9897
if (inet_opt)
@@ -408,8 +407,8 @@ struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
408407

409408
newinet = inet_sk(newsk);
410409
ireq = inet_rsk(req);
411-
newinet->inet_daddr = ireq->ir_rmt_addr;
412-
newinet->inet_rcv_saddr = ireq->ir_loc_addr;
410+
sk_daddr_set(newsk, ireq->ir_rmt_addr);
411+
sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
413412
newinet->inet_saddr = ireq->ir_loc_addr;
414413
newinet->inet_opt = ireq->opt;
415414
ireq->opt = NULL;

net/dccp/ipv6.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,7 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
470470

471471
memcpy(newnp, np, sizeof(struct ipv6_pinfo));
472472

473-
ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
474-
475-
ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
476-
477-
newsk->sk_v6_rcv_saddr = newnp->saddr;
473+
newnp->saddr = newsk->sk_v6_rcv_saddr;
478474

479475
inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
480476
newsk->sk_backlog_rcv = dccp_v4_do_rcv;
@@ -917,9 +913,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
917913
sk->sk_backlog_rcv = dccp_v6_do_rcv;
918914
goto failure;
919915
}
920-
ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
921-
ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &sk->sk_v6_rcv_saddr);
922-
916+
np->saddr = sk->sk_v6_rcv_saddr;
923917
return err;
924918
}
925919

net/ipv4/inet_hashtables.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
3636
inet_ehash_secret + net_hash_mix(net));
3737
}
3838

39-
39+
/* This function handles inet_sock, but also timewait and request sockets
40+
* for IPv4/IPv6.
41+
*/
4042
u32 sk_ehashfn(const struct sock *sk)
4143
{
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
4251
return inet_ehashfn(sock_net(sk),
4352
sk->sk_rcv_saddr, sk->sk_num,
4453
sk->sk_daddr, sk->sk_dport);

net/ipv4/tcp_ipv4.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
189189

190190
if (!inet->inet_saddr)
191191
inet->inet_saddr = fl4->saddr;
192-
inet->inet_rcv_saddr = inet->inet_saddr;
192+
sk_rcv_saddr_set(sk, inet->inet_saddr);
193193

194194
if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
195195
/* Reset inherited state */
@@ -204,7 +204,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
204204
tcp_fetch_timewait_stamp(sk, &rt->dst);
205205

206206
inet->inet_dport = usin->sin_port;
207-
inet->inet_daddr = daddr;
207+
sk_daddr_set(sk, daddr);
208208

209209
inet_csk(sk)->icsk_ext_hdr_len = 0;
210210
if (inet_opt)
@@ -1319,8 +1319,8 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
13191319
newtp = tcp_sk(newsk);
13201320
newinet = inet_sk(newsk);
13211321
ireq = inet_rsk(req);
1322-
newinet->inet_daddr = ireq->ir_rmt_addr;
1323-
newinet->inet_rcv_saddr = ireq->ir_loc_addr;
1322+
sk_daddr_set(newsk, ireq->ir_rmt_addr);
1323+
sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
13241324
newinet->inet_saddr = ireq->ir_loc_addr;
13251325
inet_opt = ireq->opt;
13261326
rcu_assign_pointer(newinet->inet_opt, inet_opt);

net/ipv6/inet6_hashtables.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
#include <net/secure_seq.h>
2424
#include <net/ip.h>
2525

26-
static u32 inet6_ehashfn(const struct net *net,
27-
const struct in6_addr *laddr,
28-
const u16 lport,
29-
const struct in6_addr *faddr,
30-
const __be16 fport)
26+
u32 inet6_ehashfn(const struct net *net,
27+
const struct in6_addr *laddr, const u16 lport,
28+
const struct in6_addr *faddr, const __be16 fport)
3129
{
3230
static u32 inet6_ehash_secret __read_mostly;
3331
static u32 ipv6_hash_secret __read_mostly;
@@ -44,18 +42,6 @@ static u32 inet6_ehashfn(const struct net *net,
4442
inet6_ehash_secret + net_hash_mix(net));
4543
}
4644

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-
5945
int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw)
6046
{
6147
struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
@@ -75,7 +61,7 @@ int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw)
7561
struct hlist_nulls_head *list;
7662
spinlock_t *lock;
7763

78-
sk->sk_hash = hash = inet6_sk_ehashfn(sk);
64+
sk->sk_hash = hash = sk_ehashfn(sk);
7965
list = &inet_ehash_bucket(hashinfo, hash)->chain;
8066
lock = inet_ehash_lockp(hashinfo, hash);
8167
spin_lock(lock);

net/ipv6/tcp_ipv6.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,8 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
233233
tp->af_specific = &tcp_sock_ipv6_specific;
234234
#endif
235235
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);
240236
}
237+
np->saddr = sk->sk_v6_rcv_saddr;
241238

242239
return err;
243240
}
@@ -1078,11 +1075,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
10781075

10791076
memcpy(newnp, np, sizeof(struct ipv6_pinfo));
10801077

1081-
ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
1082-
1083-
ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1084-
1085-
newsk->sk_v6_rcv_saddr = newnp->saddr;
1078+
newnp->saddr = newsk->sk_v6_rcv_saddr;
10861079

10871080
inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
10881081
newsk->sk_backlog_rcv = tcp_v4_do_rcv;

0 commit comments

Comments
 (0)