inet: add IPv6 support to sk_ehashfn()
authorEric Dumazet <edumazet@google.com>
Wed, 18 Mar 2015 21:05:35 +0000 (14:05 -0700)
committerDavid S. Miller <davem@davemloft.net>
Thu, 19 Mar 2015 02:00:34 +0000 (22:00 -0400)
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>
include/net/inet_hashtables.h
net/dccp/ipv4.c
net/dccp/ipv6.c
net/ipv4/inet_hashtables.c
net/ipv4/tcp_ipv4.c
net/ipv6/inet6_hashtables.c
net/ipv6/tcp_ipv6.c

index ef993ef571ea9637b65247a8deec028106efad05..06ad42182ec2b8e1f20d5f89fd913d9e9190cc1f 100644 (file)
@@ -384,6 +384,25 @@ static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo,
 }
 
 u32 sk_ehashfn(const struct sock *sk);
+u32 inet6_ehashfn(const struct net *net,
+                 const struct in6_addr *laddr, const u16 lport,
+                 const struct in6_addr *faddr, const __be16 fport);
+
+static inline void sk_daddr_set(struct sock *sk, __be32 addr)
+{
+       sk->sk_daddr = addr; /* alias of inet_daddr */
+#if IS_ENABLED(CONFIG_IPV6)
+       ipv6_addr_set_v4mapped(addr, &sk->sk_v6_daddr);
+#endif
+}
+
+static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
+{
+       sk->sk_rcv_saddr = addr; /* alias of inet_rcv_saddr */
+#if IS_ENABLED(CONFIG_IPV6)
+       ipv6_addr_set_v4mapped(addr, &sk->sk_v6_rcv_saddr);
+#endif
+}
 
 int __inet_hash_connect(struct inet_timewait_death_row *death_row,
                        struct sock *sk, u32 port_offset,
index bf897829f4f0784da13ecff5d739eac13a18524f..f3f8906f482e69fd77f855a75a970472ee9da7ad 100644 (file)
@@ -89,10 +89,9 @@ int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 
        if (inet->inet_saddr == 0)
                inet->inet_saddr = fl4->saddr;
-       inet->inet_rcv_saddr = inet->inet_saddr;
-
+       sk_rcv_saddr_set(sk, inet->inet_saddr);
        inet->inet_dport = usin->sin_port;
-       inet->inet_daddr = daddr;
+       sk_daddr_set(sk, daddr);
 
        inet_csk(sk)->icsk_ext_hdr_len = 0;
        if (inet_opt)
@@ -408,8 +407,8 @@ struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
 
        newinet            = inet_sk(newsk);
        ireq               = inet_rsk(req);
-       newinet->inet_daddr     = ireq->ir_rmt_addr;
-       newinet->inet_rcv_saddr = ireq->ir_loc_addr;
+       sk_daddr_set(newsk, ireq->ir_rmt_addr);
+       sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
        newinet->inet_saddr     = ireq->ir_loc_addr;
        newinet->inet_opt       = ireq->opt;
        ireq->opt          = NULL;
index d7e7c7b0a3f1e1c919e6084de368b7a811f86be3..9216d173dd5fce8a592a06324dcb0b49d8c33806 100644 (file)
@@ -470,11 +470,7 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
 
                memcpy(newnp, np, sizeof(struct ipv6_pinfo));
 
-               ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
-
-               ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
-
-               newsk->sk_v6_rcv_saddr = newnp->saddr;
+               newnp->saddr = newsk->sk_v6_rcv_saddr;
 
                inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
                newsk->sk_backlog_rcv = dccp_v4_do_rcv;
@@ -917,9 +913,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
                        sk->sk_backlog_rcv = dccp_v6_do_rcv;
                        goto failure;
                }
-               ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
-               ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &sk->sk_v6_rcv_saddr);
-
+               np->saddr = sk->sk_v6_rcv_saddr;
                return err;
        }
 
index 3a86dfd7ae33191d62a79210aa555c2048d3d5e3..ab7f677a97dbcbc8a03285301d4ae11c847cf665 100644 (file)
@@ -36,9 +36,18 @@ static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
                              inet_ehash_secret + net_hash_mix(net));
 }
 
-
+/* This function handles inet_sock, but also timewait and request sockets
+ * for IPv4/IPv6.
+ */
 u32 sk_ehashfn(const struct sock *sk)
 {
+#if IS_ENABLED(CONFIG_IPV6)
+       if (sk->sk_family == AF_INET6 &&
+           !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
+               return inet6_ehashfn(sock_net(sk),
+                                    &sk->sk_v6_rcv_saddr, sk->sk_num,
+                                    &sk->sk_v6_daddr, sk->sk_dport);
+#endif
        return inet_ehashfn(sock_net(sk),
                            sk->sk_rcv_saddr, sk->sk_num,
                            sk->sk_daddr, sk->sk_dport);
index 80067d5858b4adf6d56926989139add1fe217684..ca207df4af1c40d54234328d8253cf08c315c4f0 100644 (file)
@@ -189,7 +189,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 
        if (!inet->inet_saddr)
                inet->inet_saddr = fl4->saddr;
-       inet->inet_rcv_saddr = inet->inet_saddr;
+       sk_rcv_saddr_set(sk, inet->inet_saddr);
 
        if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
                /* Reset inherited state */
@@ -204,7 +204,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                tcp_fetch_timewait_stamp(sk, &rt->dst);
 
        inet->inet_dport = usin->sin_port;
-       inet->inet_daddr = daddr;
+       sk_daddr_set(sk, daddr);
 
        inet_csk(sk)->icsk_ext_hdr_len = 0;
        if (inet_opt)
@@ -1319,8 +1319,8 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        newtp                 = tcp_sk(newsk);
        newinet               = inet_sk(newsk);
        ireq                  = inet_rsk(req);
-       newinet->inet_daddr   = ireq->ir_rmt_addr;
-       newinet->inet_rcv_saddr = ireq->ir_loc_addr;
+       sk_daddr_set(newsk, ireq->ir_rmt_addr);
+       sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
        newinet->inet_saddr           = ireq->ir_loc_addr;
        inet_opt              = ireq->opt;
        rcu_assign_pointer(newinet->inet_opt, inet_opt);
index df7df99d1d7eb4f7b1a8acda2e4d44d3bac24992..ed5787b20192ea6361dd1624d1761d1b18b3ae63 100644 (file)
 #include <net/secure_seq.h>
 #include <net/ip.h>
 
-static u32 inet6_ehashfn(const struct net *net,
-                        const struct in6_addr *laddr,
-                        const u16 lport,
-                        const struct in6_addr *faddr,
-                        const __be16 fport)
+u32 inet6_ehashfn(const struct net *net,
+                 const struct in6_addr *laddr, const u16 lport,
+                 const struct in6_addr *faddr, const __be16 fport)
 {
        static u32 inet6_ehash_secret __read_mostly;
        static u32 ipv6_hash_secret __read_mostly;
@@ -44,18 +42,6 @@ static u32 inet6_ehashfn(const struct net *net,
                               inet6_ehash_secret + net_hash_mix(net));
 }
 
-static int inet6_sk_ehashfn(const struct sock *sk)
-{
-       const struct inet_sock *inet = inet_sk(sk);
-       const struct in6_addr *laddr = &sk->sk_v6_rcv_saddr;
-       const struct in6_addr *faddr = &sk->sk_v6_daddr;
-       const __u16 lport = inet->inet_num;
-       const __be16 fport = inet->inet_dport;
-       struct net *net = sock_net(sk);
-
-       return inet6_ehashfn(net, laddr, lport, faddr, fport);
-}
-
 int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw)
 {
        struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
@@ -75,7 +61,7 @@ int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw)
                struct hlist_nulls_head *list;
                spinlock_t *lock;
 
-               sk->sk_hash = hash = inet6_sk_ehashfn(sk);
+               sk->sk_hash = hash = sk_ehashfn(sk);
                list = &inet_ehash_bucket(hashinfo, hash)->chain;
                lock = inet_ehash_lockp(hashinfo, hash);
                spin_lock(lock);
index e4761b22307b2543e004da4027a5ebd8bc13668b..5546df0745832a92584876406c5b3c3c6caf7960 100644 (file)
@@ -233,11 +233,8 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
                        tp->af_specific = &tcp_sock_ipv6_specific;
 #endif
                        goto failure;
-               } else {
-                       ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
-                       ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
-                                              &sk->sk_v6_rcv_saddr);
                }
+               np->saddr = sk->sk_v6_rcv_saddr;
 
                return err;
        }
@@ -1078,11 +1075,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
 
                memcpy(newnp, np, sizeof(struct ipv6_pinfo));
 
-               ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
-
-               ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
-
-               newsk->sk_v6_rcv_saddr = newnp->saddr;
+               newnp->saddr = newsk->sk_v6_rcv_saddr;
 
                inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
                newsk->sk_backlog_rcv = tcp_v4_do_rcv;