ipv4: Use inet_csk_route_child_sock() in DCCP and TCP.
[deliverable/linux.git] / net / ipv4 / tcp_ipv4.c
index 310454c2f4d1352bdd35cc42421f41cff3bff8ab..374de3c98d5e3900d77f8e9a60dd9f1bd25cb640 100644 (file)
@@ -151,9 +151,10 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        struct tcp_sock *tp = tcp_sk(sk);
        __be16 orig_sport, orig_dport;
        __be32 daddr, nexthop;
-       struct flowi4 fl4;
+       struct flowi4 *fl4;
        struct rtable *rt;
        int err;
+       struct ip_options_rcu *inet_opt;
 
        if (addr_len < sizeof(struct sockaddr_in))
                return -EINVAL;
@@ -162,15 +163,18 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                return -EAFNOSUPPORT;
 
        nexthop = daddr = usin->sin_addr.s_addr;
-       if (inet->opt && inet->opt->srr) {
+       inet_opt = rcu_dereference_protected(inet->inet_opt,
+                                            sock_owned_by_user(sk));
+       if (inet_opt && inet_opt->opt.srr) {
                if (!daddr)
                        return -EINVAL;
-               nexthop = inet->opt->faddr;
+               nexthop = inet_opt->opt.faddr;
        }
 
        orig_sport = inet->inet_sport;
        orig_dport = usin->sin_port;
-       rt = ip_route_connect(&fl4, nexthop, inet->inet_saddr,
+       fl4 = &inet->cork.fl.u.ip4;
+       rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
                              RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
                              IPPROTO_TCP,
                              orig_sport, orig_dport, sk, true);
@@ -186,11 +190,11 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                return -ENETUNREACH;
        }
 
-       if (!inet->opt || !inet->opt->srr)
-               daddr = rt->rt_dst;
+       if (!inet_opt || !inet_opt->opt.srr)
+               daddr = fl4->daddr;
 
        if (!inet->inet_saddr)
-               inet->inet_saddr = rt->rt_src;
+               inet->inet_saddr = fl4->saddr;
        inet->inet_rcv_saddr = inet->inet_saddr;
 
        if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
@@ -201,7 +205,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        }
 
        if (tcp_death_row.sysctl_tw_recycle &&
-           !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
+           !tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr) {
                struct inet_peer *peer = rt_get_peer(rt);
                /*
                 * VJ's idea. We save last timestamp seen from
@@ -222,8 +226,8 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        inet->inet_daddr = daddr;
 
        inet_csk(sk)->icsk_ext_hdr_len = 0;
-       if (inet->opt)
-               inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
+       if (inet_opt)
+               inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
 
        tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
 
@@ -237,7 +241,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        if (err)
                goto failure;
 
-       rt = ip_route_newports(&fl4, rt, orig_sport, orig_dport,
+       rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
                               inet->inet_sport, inet->inet_dport, sk);
        if (IS_ERR(rt)) {
                err = PTR_ERR(rt);
@@ -820,17 +824,18 @@ static void syn_flood_warning(const struct sk_buff *skb)
 /*
  * Save and compile IPv4 options into the request_sock if needed.
  */
-static struct ip_options *tcp_v4_save_options(struct sock *sk,
-                                             struct sk_buff *skb)
+static struct ip_options_rcu *tcp_v4_save_options(struct sock *sk,
+                                                 struct sk_buff *skb)
 {
-       struct ip_options *opt = &(IPCB(skb)->opt);
-       struct ip_options *dopt = NULL;
+       const struct ip_options *opt = &(IPCB(skb)->opt);
+       struct ip_options_rcu *dopt = NULL;
 
        if (opt && opt->optlen) {
-               int opt_size = optlength(opt);
+               int opt_size = sizeof(*dopt) + opt->optlen;
+
                dopt = kmalloc(opt_size, GFP_ATOMIC);
                if (dopt) {
-                       if (ip_options_echo(dopt, skb)) {
+                       if (ip_options_echo(&dopt->opt, skb)) {
                                kfree(dopt);
                                dopt = NULL;
                        }
@@ -1411,19 +1416,16 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
 #ifdef CONFIG_TCP_MD5SIG
        struct tcp_md5sig_key *key;
 #endif
+       struct ip_options_rcu *inet_opt;
 
        if (sk_acceptq_is_full(sk))
                goto exit_overflow;
 
-       if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
-               goto exit;
-
        newsk = tcp_create_openreq_child(sk, req, skb);
        if (!newsk)
                goto exit_nonewsk;
 
        newsk->sk_gso_type = SKB_GSO_TCPV4;
-       sk_setup_caps(newsk, dst);
 
        newtp                 = tcp_sk(newsk);
        newinet               = inet_sk(newsk);
@@ -1431,15 +1433,21 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        newinet->inet_daddr   = ireq->rmt_addr;
        newinet->inet_rcv_saddr = ireq->loc_addr;
        newinet->inet_saddr           = ireq->loc_addr;
-       newinet->opt          = ireq->opt;
+       inet_opt              = ireq->opt;
+       rcu_assign_pointer(newinet->inet_opt, inet_opt);
        ireq->opt             = NULL;
        newinet->mc_index     = inet_iif(skb);
        newinet->mc_ttl       = ip_hdr(skb)->ttl;
        inet_csk(newsk)->icsk_ext_hdr_len = 0;
-       if (newinet->opt)
-               inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
+       if (inet_opt)
+               inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
        newinet->inet_id = newtp->write_seq ^ jiffies;
 
+       if (!dst && (dst = inet_csk_route_child_sock(sk, newsk, req)) == NULL)
+               goto put_and_exit;
+
+       sk_setup_caps(newsk, dst);
+
        tcp_mtup_init(newsk);
        tcp_sync_mss(newsk, dst_mtu(dst));
        newtp->advmss = dst_metric_advmss(dst);
@@ -1467,10 +1475,8 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        }
 #endif
 
-       if (__inet_inherit_port(sk, newsk) < 0) {
-               sock_put(newsk);
-               goto exit;
-       }
+       if (__inet_inherit_port(sk, newsk) < 0)
+               goto put_and_exit;
        __inet_hash_nolisten(newsk, NULL);
 
        return newsk;
@@ -1482,6 +1488,9 @@ exit_nonewsk:
 exit:
        NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
        return NULL;
+put_and_exit:
+       sock_put(newsk);
+       goto exit;
 }
 EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
 
This page took 0.030222 seconds and 5 git commands to generate.