net: spread __net_init, __net_exit
[safe/jmp/linux-2.6] / net / ipv4 / tcp_ipv4.c
index 649d00a..c3588b4 100644 (file)
@@ -5,8 +5,6 @@
  *
  *             Implementation of the Transmission Control Protocol(TCP).
  *
- * Version:    $Id: tcp_ipv4.c,v 1.240 2002/02/01 22:01:04 davem Exp $
- *
  *             IPv4 specific functions
  *
  *
@@ -53,6 +51,7 @@
  */
 
 
+#include <linux/bottom_half.h>
 #include <linux/types.h>
 #include <linux/fcntl.h>
 #include <linux/module.h>
 int sysctl_tcp_tw_reuse __read_mostly;
 int sysctl_tcp_low_latency __read_mostly;
 
-/* Check TCP sequence numbers in ICMP packets. */
-#define ICMP_MIN_LENGTH 8
-
-/* Socket used for sending RSTs */
-static struct socket *tcp_socket __read_mostly;
-
-void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
 
 #ifdef CONFIG_TCP_MD5SIG
 static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
                                                   __be32 addr);
-static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
-                                  __be32 saddr, __be32 daddr,
-                                  struct tcphdr *th, int protocol,
-                                  unsigned int tcplen);
+static int tcp_v4_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
+                              __be32 daddr, __be32 saddr, struct tcphdr *th);
+#else
+static inline
+struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
+{
+       return NULL;
+}
 #endif
 
-struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
-       .lhash_lock  = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
-       .lhash_users = ATOMIC_INIT(0),
-       .lhash_wait  = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
-};
+struct inet_hashinfo tcp_hashinfo;
 
 static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
 {
@@ -173,13 +165,13 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                nexthop = inet->opt->faddr;
        }
 
-       tmp = ip_route_connect(&rt, nexthop, inet->saddr,
+       tmp = ip_route_connect(&rt, nexthop, inet->inet_saddr,
                               RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
                               IPPROTO_TCP,
-                              inet->sport, usin->sin_port, sk, 1);
+                              inet->inet_sport, usin->sin_port, sk, 1);
        if (tmp < 0) {
                if (tmp == -ENETUNREACH)
-                       IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
+                       IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
                return tmp;
        }
 
@@ -191,11 +183,11 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        if (!inet->opt || !inet->opt->srr)
                daddr = rt->rt_dst;
 
-       if (!inet->saddr)
-               inet->saddr = rt->rt_src;
-       inet->rcv_saddr = inet->saddr;
+       if (!inet->inet_saddr)
+               inet->inet_saddr = rt->rt_src;
+       inet->inet_rcv_saddr = inet->inet_saddr;
 
-       if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
+       if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
                /* Reset inherited state */
                tp->rx_opt.ts_recent       = 0;
                tp->rx_opt.ts_recent_stamp = 0;
@@ -212,20 +204,20 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                 * when trying new connection.
                 */
                if (peer != NULL &&
-                   peer->tcp_ts_stamp + TCP_PAWS_MSL >= get_seconds()) {
+                   (u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
                        tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
                        tp->rx_opt.ts_recent = peer->tcp_ts;
                }
        }
 
-       inet->dport = usin->sin_port;
-       inet->daddr = daddr;
+       inet->inet_dport = usin->sin_port;
+       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;
 
-       tp->rx_opt.mss_clamp = 536;
+       tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
 
        /* Socket identity is still unknown (sport may be zero).
         * However we set state to SYN-SENT and not releasing socket
@@ -238,7 +230,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                goto failure;
 
        err = ip_route_newports(&rt, IPPROTO_TCP,
-                               inet->sport, inet->dport, sk);
+                               inet->inet_sport, inet->inet_dport, sk);
        if (err)
                goto failure;
 
@@ -247,12 +239,12 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
        sk_setup_caps(sk, &rt->u.dst);
 
        if (!tp->write_seq)
-               tp->write_seq = secure_tcp_sequence_number(inet->saddr,
-                                                          inet->daddr,
-                                                          inet->sport,
+               tp->write_seq = secure_tcp_sequence_number(inet->inet_saddr,
+                                                          inet->inet_daddr,
+                                                          inet->inet_sport,
                                                           usin->sin_port);
 
-       inet->id = tp->write_seq ^ jiffies;
+       inet->inet_id = tp->write_seq ^ jiffies;
 
        err = tcp_connect(sk);
        rt = NULL;
@@ -269,7 +261,7 @@ failure:
        tcp_set_state(sk, TCP_CLOSE);
        ip_rt_put(rt);
        sk->sk_route_caps = 0;
-       inet->dport = 0;
+       inet->inet_dport = 0;
        return err;
 }
 
@@ -336,27 +328,31 @@ static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
  *
  */
 
-void tcp_v4_err(struct sk_buff *skb, u32 info)
+void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
 {
-       struct iphdr *iph = (struct iphdr *)skb->data;
-       struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
+       struct iphdr *iph = (struct iphdr *)icmp_skb->data;
+       struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
+       struct inet_connection_sock *icsk;
        struct tcp_sock *tp;
        struct inet_sock *inet;
-       const int type = icmp_hdr(skb)->type;
-       const int code = icmp_hdr(skb)->code;
+       const int type = icmp_hdr(icmp_skb)->type;
+       const int code = icmp_hdr(icmp_skb)->code;
        struct sock *sk;
+       struct sk_buff *skb;
        __u32 seq;
+       __u32 remaining;
        int err;
+       struct net *net = dev_net(icmp_skb->dev);
 
-       if (skb->len < (iph->ihl << 2) + 8) {
-               ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
+       if (icmp_skb->len < (iph->ihl << 2) + 8) {
+               ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
                return;
        }
 
-       sk = inet_lookup(skb->dev->nd_net, &tcp_hashinfo, iph->daddr, th->dest,
-                       iph->saddr, th->source, inet_iif(skb));
+       sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest,
+                       iph->saddr, th->source, inet_iif(icmp_skb));
        if (!sk) {
-               ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
+               ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
                return;
        }
        if (sk->sk_state == TCP_TIME_WAIT) {
@@ -369,16 +365,17 @@ void tcp_v4_err(struct sk_buff *skb, u32 info)
         * servers this needs to be solved differently.
         */
        if (sock_owned_by_user(sk))
-               NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
+               NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
 
        if (sk->sk_state == TCP_CLOSE)
                goto out;
 
+       icsk = inet_csk(sk);
        tp = tcp_sk(sk);
        seq = ntohl(th->seq);
        if (sk->sk_state != TCP_LISTEN &&
            !between(seq, tp->snd_una, tp->snd_nxt)) {
-               NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
+               NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
                goto out;
        }
 
@@ -400,6 +397,39 @@ void tcp_v4_err(struct sk_buff *skb, u32 info)
                }
 
                err = icmp_err_convert[code].errno;
+               /* check if icmp_skb allows revert of backoff
+                * (see draft-zimmermann-tcp-lcd) */
+               if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
+                       break;
+               if (seq != tp->snd_una  || !icsk->icsk_retransmits ||
+                   !icsk->icsk_backoff)
+                       break;
+
+               icsk->icsk_backoff--;
+               inet_csk(sk)->icsk_rto = __tcp_set_rto(tp) <<
+                                        icsk->icsk_backoff;
+               tcp_bound_rto(sk);
+
+               skb = tcp_write_queue_head(sk);
+               BUG_ON(!skb);
+
+               remaining = icsk->icsk_rto - min(icsk->icsk_rto,
+                               tcp_time_stamp - TCP_SKB_CB(skb)->when);
+
+               if (remaining) {
+                       inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
+                                                 remaining, TCP_RTO_MAX);
+               } else if (sock_owned_by_user(sk)) {
+                       /* RTO revert clocked out retransmission,
+                        * but socket is locked. Will defer. */
+                       inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
+                                                 HZ/20, TCP_RTO_MAX);
+               } else {
+                       /* RTO revert clocked out retransmission.
+                        * Will retransmit now */
+                       tcp_retransmit_timer(sk);
+               }
+
                break;
        case ICMP_TIME_EXCEEDED:
                err = EHOSTUNREACH;
@@ -422,10 +452,10 @@ void tcp_v4_err(struct sk_buff *skb, u32 info)
                /* ICMPs are not backlogged, hence we cannot get
                   an established socket here.
                 */
-               BUG_TRAP(!req->sk);
+               WARN_ON(req->sk);
 
                if (seq != tcp_rsk(req)->snt_isn) {
-                       NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
+                       NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
                        goto out;
                }
 
@@ -490,13 +520,14 @@ void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
        struct tcphdr *th = tcp_hdr(skb);
 
        if (skb->ip_summed == CHECKSUM_PARTIAL) {
-               th->check = ~tcp_v4_check(len, inet->saddr,
-                                         inet->daddr, 0);
+               th->check = ~tcp_v4_check(len, inet->inet_saddr,
+                                         inet->inet_daddr, 0);
                skb->csum_start = skb_transport_header(skb) - skb->head;
                skb->csum_offset = offsetof(struct tcphdr, check);
        } else {
-               th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
-                                        csum_partial((char *)th,
+               th->check = tcp_v4_check(len, inet->inet_saddr,
+                                        inet->inet_daddr,
+                                        csum_partial(th,
                                                      th->doff << 2,
                                                      skb->csum));
        }
@@ -547,12 +578,13 @@ static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
 #ifdef CONFIG_TCP_MD5SIG
        struct tcp_md5sig_key *key;
 #endif
+       struct net *net;
 
        /* Never send a reset in response to a reset. */
        if (th->rst)
                return;
 
-       if (skb->rtable->rt_type != RTN_LOCAL)
+       if (skb_rtable(skb)->rt_type != RTN_LOCAL)
                return;
 
        /* Swap the send and the receive. */
@@ -585,32 +617,33 @@ static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
                arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
                rep.th.doff = arg.iov[0].iov_len / 4;
 
-               tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
-                                       key,
-                                       ip_hdr(skb)->daddr,
-                                       ip_hdr(skb)->saddr,
-                                       &rep.th, IPPROTO_TCP,
-                                       arg.iov[0].iov_len);
+               tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
+                                    key, ip_hdr(skb)->saddr,
+                                    ip_hdr(skb)->daddr, &rep.th);
        }
 #endif
        arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
                                      ip_hdr(skb)->saddr, /* XXX */
-                                     sizeof(struct tcphdr), IPPROTO_TCP, 0);
+                                     arg.iov[0].iov_len, IPPROTO_TCP, 0);
        arg.csumoffset = offsetof(struct tcphdr, check) / 2;
+       arg.flags = (sk && inet_sk(sk)->transparent) ? IP_REPLY_ARG_NOSRCCHECK : 0;
 
-       ip_send_reply(tcp_socket->sk, skb, &arg, arg.iov[0].iov_len);
+       net = dev_net(skb_dst(skb)->dev);
+       ip_send_reply(net->ipv4.tcp_sock, skb,
+                     &arg, arg.iov[0].iov_len);
 
-       TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
-       TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
+       TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
+       TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
 }
 
 /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
    outside socket context is ugly, certainly. What can I do?
  */
 
-static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
-                           struct sk_buff *skb, u32 seq, u32 ack,
-                           u32 win, u32 ts)
+static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
+                           u32 win, u32 ts, int oif,
+                           struct tcp_md5sig_key *key,
+                           int reply_flags)
 {
        struct tcphdr *th = tcp_hdr(skb);
        struct {
@@ -622,10 +655,7 @@ static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
                        ];
        } rep;
        struct ip_reply_arg arg;
-#ifdef CONFIG_TCP_MD5SIG
-       struct tcp_md5sig_key *key;
-       struct tcp_md5sig_key tw_key;
-#endif
+       struct net *net = dev_net(skb_dst(skb)->dev);
 
        memset(&rep.th, 0, sizeof(struct tcphdr));
        memset(&arg, 0, sizeof(arg));
@@ -651,23 +681,6 @@ static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
        rep.th.window  = htons(win);
 
 #ifdef CONFIG_TCP_MD5SIG
-       /*
-        * The SKB holds an imcoming packet, but may not have a valid ->sk
-        * pointer. This is especially the case when we're dealing with a
-        * TIME_WAIT ack, because the sk structure is long gone, and only
-        * the tcp_timewait_sock remains. So the md5 key is stashed in that
-        * structure, and we use it in preference.  I believe that (twsk ||
-        * skb->sk) holds true, but we program defensively.
-        */
-       if (!twsk && skb->sk) {
-               key = tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr);
-       } else if (twsk && twsk->tw_md5_keylen) {
-               tw_key.key = twsk->tw_md5_key;
-               tw_key.keylen = twsk->tw_md5_keylen;
-               key = &tw_key;
-       } else
-               key = NULL;
-
        if (key) {
                int offset = (ts) ? 3 : 0;
 
@@ -678,24 +691,23 @@ static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
                arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
                rep.th.doff = arg.iov[0].iov_len/4;
 
-               tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
-                                       key,
-                                       ip_hdr(skb)->daddr,
-                                       ip_hdr(skb)->saddr,
-                                       &rep.th, IPPROTO_TCP,
-                                       arg.iov[0].iov_len);
+               tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
+                                   key, ip_hdr(skb)->saddr,
+                                   ip_hdr(skb)->daddr, &rep.th);
        }
 #endif
+       arg.flags = reply_flags;
        arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
                                      ip_hdr(skb)->saddr, /* XXX */
                                      arg.iov[0].iov_len, IPPROTO_TCP, 0);
        arg.csumoffset = offsetof(struct tcphdr, check) / 2;
-       if (twsk)
-               arg.bound_dev_if = twsk->tw_sk.tw_bound_dev_if;
+       if (oif)
+               arg.bound_dev_if = oif;
 
-       ip_send_reply(tcp_socket->sk, skb, &arg, arg.iov[0].iov_len);
+       ip_send_reply(net->ipv4.tcp_sock, skb,
+                     &arg, arg.iov[0].iov_len);
 
-       TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
+       TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
 }
 
 static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
@@ -703,19 +715,26 @@ static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
        struct inet_timewait_sock *tw = inet_twsk(sk);
        struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
 
-       tcp_v4_send_ack(tcptw, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
+       tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
                        tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
-                       tcptw->tw_ts_recent);
+                       tcptw->tw_ts_recent,
+                       tw->tw_bound_dev_if,
+                       tcp_twsk_md5_key(tcptw),
+                       tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0
+                       );
 
        inet_twsk_put(tw);
 }
 
-static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
+static void tcp_v4_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
                                  struct request_sock *req)
 {
-       tcp_v4_send_ack(NULL, skb, tcp_rsk(req)->snt_isn + 1,
+       tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1,
                        tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
-                       req->ts_recent);
+                       req->ts_recent,
+                       0,
+                       tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr),
+                       inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0);
 }
 
 /*
@@ -723,8 +742,9 @@ static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
  *     This still operates on a request_sock only, not on a big
  *     socket.
  */
-static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
-                               struct dst_entry *dst)
+static int tcp_v4_send_synack(struct sock *sk, struct dst_entry *dst,
+                             struct request_sock *req,
+                             struct request_values *rvp)
 {
        const struct inet_request_sock *ireq = inet_rsk(req);
        int err = -1;
@@ -734,7 +754,7 @@ static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
        if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
                return -1;
 
-       skb = tcp_make_synack(sk, dst, req);
+       skb = tcp_make_synack(sk, dst, req, rvp);
 
        if (skb) {
                struct tcphdr *th = tcp_hdr(skb);
@@ -742,7 +762,7 @@ static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
                th->check = tcp_v4_check(skb->len,
                                         ireq->loc_addr,
                                         ireq->rmt_addr,
-                                        csum_partial((char *)th, skb->len,
+                                        csum_partial(th, skb->len,
                                                      skb->csum));
 
                err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
@@ -755,9 +775,11 @@ static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
        return err;
 }
 
-static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req)
+static int tcp_v4_rtx_synack(struct sock *sk, struct request_sock *req,
+                             struct request_values *rvp)
 {
-       return __tcp_v4_send_synack(sk, req, NULL);
+       TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
+       return tcp_v4_send_synack(sk, NULL, req, rvp);
 }
 
 /*
@@ -830,7 +852,7 @@ static struct tcp_md5sig_key *
 struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
                                         struct sock *addr_sk)
 {
-       return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
+       return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->inet_daddr);
 }
 
 EXPORT_SYMBOL(tcp_v4_md5_lookup);
@@ -868,7 +890,7 @@ int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
                        }
                        sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
                }
-               if (tcp_alloc_md5sig_pool() == NULL) {
+               if (tcp_alloc_md5sig_pool(sk) == NULL) {
                        kfree(newkey);
                        return -ENOMEM;
                }
@@ -905,7 +927,7 @@ EXPORT_SYMBOL(tcp_v4_md5_do_add);
 static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
                               u8 *newkey, u8 newkeylen)
 {
-       return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
+       return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->inet_daddr,
                                 newkey, newkeylen);
 }
 
@@ -989,8 +1011,9 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
 
        if (!tcp_sk(sk)->md5sig_info) {
                struct tcp_sock *tp = tcp_sk(sk);
-               struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
+               struct tcp_md5sig_info *p;
 
+               p = kzalloc(sizeof(*p), sk->sk_allocation);
                if (!p)
                        return -EINVAL;
 
@@ -998,39 +1021,20 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
                sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
        }
 
-       newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
+       newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, sk->sk_allocation);
        if (!newkey)
                return -ENOMEM;
        return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
                                 newkey, cmd.tcpm_keylen);
 }
 
-static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
-                                  __be32 saddr, __be32 daddr,
-                                  struct tcphdr *th, int protocol,
-                                  unsigned int tcplen)
+static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
+                                       __be32 daddr, __be32 saddr, int nbytes)
 {
-       struct scatterlist sg[4];
-       __u16 data_len;
-       int block = 0;
-       __sum16 old_checksum;
-       struct tcp_md5sig_pool *hp;
        struct tcp4_pseudohdr *bp;
-       struct hash_desc *desc;
-       int err;
-       unsigned int nbytes = 0;
-
-       /*
-        * Okay, so RFC2385 is turned on for this connection,
-        * so we need to generate the MD5 hash for the packet now.
-        */
-
-       hp = tcp_get_md5sig_pool();
-       if (!hp)
-               goto clear_hash_noput;
+       struct scatterlist sg;
 
        bp = &hp->md5_blk.ip4;
-       desc = &hp->md5_desc;
 
        /*
         * 1. the TCP pseudo-header (in the order: source IP address,
@@ -1040,86 +1044,96 @@ static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
        bp->saddr = saddr;
        bp->daddr = daddr;
        bp->pad = 0;
-       bp->protocol = protocol;
-       bp->len = htons(tcplen);
+       bp->protocol = IPPROTO_TCP;
+       bp->len = cpu_to_be16(nbytes);
 
-       sg_init_table(sg, 4);
-
-       sg_set_buf(&sg[block++], bp, sizeof(*bp));
-       nbytes += sizeof(*bp);
-
-       /* 2. the TCP header, excluding options, and assuming a
-        * checksum of zero/
-        */
-       old_checksum = th->check;
-       th->check = 0;
-       sg_set_buf(&sg[block++], th, sizeof(struct tcphdr));
-       nbytes += sizeof(struct tcphdr);
-
-       /* 3. the TCP segment data (if any) */
-       data_len = tcplen - (th->doff << 2);
-       if (data_len > 0) {
-               unsigned char *data = (unsigned char *)th + (th->doff << 2);
-               sg_set_buf(&sg[block++], data, data_len);
-               nbytes += data_len;
-       }
+       sg_init_one(&sg, bp, sizeof(*bp));
+       return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
+}
 
-       /* 4. an independently-specified key or password, known to both
-        * TCPs and presumably connection-specific
-        */
-       sg_set_buf(&sg[block++], key->key, key->keylen);
-       nbytes += key->keylen;
+static int tcp_v4_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
+                              __be32 daddr, __be32 saddr, struct tcphdr *th)
+{
+       struct tcp_md5sig_pool *hp;
+       struct hash_desc *desc;
 
-       sg_mark_end(&sg[block - 1]);
+       hp = tcp_get_md5sig_pool();
+       if (!hp)
+               goto clear_hash_noput;
+       desc = &hp->md5_desc;
 
-       /* Now store the Hash into the packet */
-       err = crypto_hash_init(desc);
-       if (err)
+       if (crypto_hash_init(desc))
                goto clear_hash;
-       err = crypto_hash_update(desc, sg, nbytes);
-       if (err)
+       if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
                goto clear_hash;
-       err = crypto_hash_final(desc, md5_hash);
-       if (err)
+       if (tcp_md5_hash_header(hp, th))
+               goto clear_hash;
+       if (tcp_md5_hash_key(hp, key))
+               goto clear_hash;
+       if (crypto_hash_final(desc, md5_hash))
                goto clear_hash;
 
-       /* Reset header, and free up the crypto */
        tcp_put_md5sig_pool();
-       th->check = old_checksum;
-
-out:
        return 0;
+
 clear_hash:
        tcp_put_md5sig_pool();
 clear_hash_noput:
        memset(md5_hash, 0, 16);
-       goto out;
+       return 1;
 }
 
-int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
-                        struct sock *sk,
-                        struct dst_entry *dst,
-                        struct request_sock *req,
-                        struct tcphdr *th, int protocol,
-                        unsigned int tcplen)
+int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
+                       struct sock *sk, struct request_sock *req,
+                       struct sk_buff *skb)
 {
+       struct tcp_md5sig_pool *hp;
+       struct hash_desc *desc;
+       struct tcphdr *th = tcp_hdr(skb);
        __be32 saddr, daddr;
 
        if (sk) {
-               saddr = inet_sk(sk)->saddr;
-               daddr = inet_sk(sk)->daddr;
+               saddr = inet_sk(sk)->inet_saddr;
+               daddr = inet_sk(sk)->inet_daddr;
+       } else if (req) {
+               saddr = inet_rsk(req)->loc_addr;
+               daddr = inet_rsk(req)->rmt_addr;
        } else {
-               struct rtable *rt = (struct rtable *)dst;
-               BUG_ON(!rt);
-               saddr = rt->rt_src;
-               daddr = rt->rt_dst;
+               const struct iphdr *iph = ip_hdr(skb);
+               saddr = iph->saddr;
+               daddr = iph->daddr;
        }
-       return tcp_v4_do_calc_md5_hash(md5_hash, key,
-                                      saddr, daddr,
-                                      th, protocol, tcplen);
+
+       hp = tcp_get_md5sig_pool();
+       if (!hp)
+               goto clear_hash_noput;
+       desc = &hp->md5_desc;
+
+       if (crypto_hash_init(desc))
+               goto clear_hash;
+
+       if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
+               goto clear_hash;
+       if (tcp_md5_hash_header(hp, th))
+               goto clear_hash;
+       if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
+               goto clear_hash;
+       if (tcp_md5_hash_key(hp, key))
+               goto clear_hash;
+       if (crypto_hash_final(desc, md5_hash))
+               goto clear_hash;
+
+       tcp_put_md5sig_pool();
+       return 0;
+
+clear_hash:
+       tcp_put_md5sig_pool();
+clear_hash_noput:
+       memset(md5_hash, 0, 16);
+       return 1;
 }
 
-EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
+EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
 
 static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
 {
@@ -1135,87 +1149,38 @@ static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
        struct tcp_md5sig_key *hash_expected;
        const struct iphdr *iph = ip_hdr(skb);
        struct tcphdr *th = tcp_hdr(skb);
-       int length = (th->doff << 2) - sizeof(struct tcphdr);
        int genhash;
-       unsigned char *ptr;
        unsigned char newhash[16];
 
        hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
+       hash_location = tcp_parse_md5sig_option(th);
 
-       /*
-        * If the TCP option length is less than the TCP_MD5SIG
-        * option length, then we can shortcut
-        */
-       if (length < TCPOLEN_MD5SIG) {
-               if (hash_expected)
-                       return 1;
-               else
-                       return 0;
-       }
-
-       /* Okay, we can't shortcut - we have to grub through the options */
-       ptr = (unsigned char *)(th + 1);
-       while (length > 0) {
-               int opcode = *ptr++;
-               int opsize;
-
-               switch (opcode) {
-               case TCPOPT_EOL:
-                       goto done_opts;
-               case TCPOPT_NOP:
-                       length--;
-                       continue;
-               default:
-                       opsize = *ptr++;
-                       if (opsize < 2)
-                               goto done_opts;
-                       if (opsize > length)
-                               goto done_opts;
-
-                       if (opcode == TCPOPT_MD5SIG) {
-                               hash_location = ptr;
-                               goto done_opts;
-                       }
-               }
-               ptr += opsize-2;
-               length -= opsize;
-       }
-done_opts:
        /* We've parsed the options - do we have a hash? */
        if (!hash_expected && !hash_location)
                return 0;
 
        if (hash_expected && !hash_location) {
-               LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
-                              "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
-                              NIPQUAD(iph->saddr), ntohs(th->source),
-                              NIPQUAD(iph->daddr), ntohs(th->dest));
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
                return 1;
        }
 
        if (!hash_expected && hash_location) {
-               LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
-                              "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
-                              NIPQUAD(iph->saddr), ntohs(th->source),
-                              NIPQUAD(iph->daddr), ntohs(th->dest));
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
                return 1;
        }
 
        /* Okay, so this is hash_expected and hash_location -
         * so we need to calculate the checksum.
         */
-       genhash = tcp_v4_do_calc_md5_hash(newhash,
-                                         hash_expected,
-                                         iph->saddr, iph->daddr,
-                                         th, sk->sk_protocol,
-                                         skb->len);
+       genhash = tcp_v4_md5_hash_skb(newhash,
+                                     hash_expected,
+                                     NULL, NULL, skb);
 
        if (genhash || memcmp(hash_location, newhash, 16) != 0) {
                if (net_ratelimit()) {
-                       printk(KERN_INFO "MD5 Hash failed for "
-                              "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
-                              NIPQUAD(iph->saddr), ntohs(th->source),
-                              NIPQUAD(iph->daddr), ntohs(th->dest),
+                       printk(KERN_INFO "MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
+                              &iph->saddr, ntohs(th->source),
+                              &iph->daddr, ntohs(th->dest),
                               genhash ? " tcp_v4_calc_md5_hash failed" : "");
                }
                return 1;
@@ -1228,15 +1193,17 @@ done_opts:
 struct request_sock_ops tcp_request_sock_ops __read_mostly = {
        .family         =       PF_INET,
        .obj_size       =       sizeof(struct tcp_request_sock),
-       .rtx_syn_ack    =       tcp_v4_send_synack,
+       .rtx_syn_ack    =       tcp_v4_rtx_synack,
        .send_ack       =       tcp_v4_reqsk_send_ack,
        .destructor     =       tcp_v4_reqsk_destructor,
        .send_reset     =       tcp_v4_send_reset,
+       .syn_ack_timeout =      tcp_syn_ack_timeout,
 };
 
 #ifdef CONFIG_TCP_MD5SIG
-static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
+static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
        .md5_lookup     =       tcp_v4_reqsk_md5_lookup,
+       .calc_md5_hash  =       tcp_v4_md5_hash_skb,
 };
 #endif
 
@@ -1248,13 +1215,16 @@ static struct timewait_sock_ops tcp_timewait_sock_ops = {
 
 int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 {
-       struct inet_request_sock *ireq;
+       struct tcp_extend_values tmp_ext;
        struct tcp_options_received tmp_opt;
+       u8 *hash_location;
        struct request_sock *req;
+       struct inet_request_sock *ireq;
+       struct tcp_sock *tp = tcp_sk(sk);
+       struct dst_entry *dst = NULL;
        __be32 saddr = ip_hdr(skb)->saddr;
        __be32 daddr = ip_hdr(skb)->daddr;
        __u32 isn = TCP_SKB_CB(skb)->when;
-       struct dst_entry *dst = NULL;
 #ifdef CONFIG_SYN_COOKIES
        int want_cookie = 0;
 #else
@@ -1262,7 +1232,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 #endif
 
        /* Never answer to SYNs send to broadcast or multicast */
-       if (skb->rtable->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
+       if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
                goto drop;
 
        /* TW buckets are converted to open requests without
@@ -1286,7 +1256,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
        if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
                goto drop;
 
-       req = reqsk_alloc(&tcp_request_sock_ops);
+       req = inet_reqsk_alloc(&tcp_request_sock_ops);
        if (!req)
                goto drop;
 
@@ -1295,42 +1265,68 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
 #endif
 
        tcp_clear_options(&tmp_opt);
-       tmp_opt.mss_clamp = 536;
-       tmp_opt.user_mss  = tcp_sk(sk)->rx_opt.user_mss;
+       tmp_opt.mss_clamp = TCP_MSS_DEFAULT;
+       tmp_opt.user_mss  = tp->rx_opt.user_mss;
+       tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
+
+       if (tmp_opt.cookie_plus > 0 &&
+           tmp_opt.saw_tstamp &&
+           !tp->rx_opt.cookie_out_never &&
+           (sysctl_tcp_cookie_size > 0 ||
+            (tp->cookie_values != NULL &&
+             tp->cookie_values->cookie_desired > 0))) {
+               u8 *c;
+               u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
+               int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
+
+               if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
+                       goto drop_and_release;
 
-       tcp_parse_options(skb, &tmp_opt, 0);
+               /* Secret recipe starts with IP addresses */
+               *mess++ ^= daddr;
+               *mess++ ^= saddr;
 
-       if (want_cookie) {
-               tcp_clear_options(&tmp_opt);
-               tmp_opt.saw_tstamp = 0;
-       }
+               /* plus variable length Initiator Cookie */
+               c = (u8 *)mess;
+               while (l-- > 0)
+                       *c++ ^= *hash_location++;
 
-       if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
-               /* Some OSes (unknown ones, but I see them on web server, which
-                * contains information interesting only for windows'
-                * users) do not send their stamp in SYN. It is easy case.
-                * We simply do not advertise TS support.
-                */
-               tmp_opt.saw_tstamp = 0;
-               tmp_opt.tstamp_ok  = 0;
+#ifdef CONFIG_SYN_COOKIES
+               want_cookie = 0;        /* not our kind of cookie */
+#endif
+               tmp_ext.cookie_out_never = 0; /* false */
+               tmp_ext.cookie_plus = tmp_opt.cookie_plus;
+       } else if (!tp->rx_opt.cookie_in_always) {
+               /* redundant indications, but ensure initialization. */
+               tmp_ext.cookie_out_never = 1; /* true */
+               tmp_ext.cookie_plus = 0;
+       } else {
+               goto drop_and_release;
        }
-       tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
+       tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
 
-       tcp_openreq_init(req, &tmp_opt, skb);
+       if (want_cookie && !tmp_opt.saw_tstamp)
+               tcp_clear_options(&tmp_opt);
 
-       if (security_inet_conn_request(sk, skb, req))
-               goto drop_and_free;
+       tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
+       tcp_openreq_init(req, &tmp_opt, skb);
 
        ireq = inet_rsk(req);
        ireq->loc_addr = daddr;
        ireq->rmt_addr = saddr;
+       ireq->no_srccheck = inet_sk(sk)->transparent;
        ireq->opt = tcp_v4_save_options(sk, skb);
+
+       if (security_inet_conn_request(sk, skb, req))
+               goto drop_and_free;
+
        if (!want_cookie)
                TCP_ECN_create_request(req, tcp_hdr(skb));
 
        if (want_cookie) {
 #ifdef CONFIG_SYN_COOKIES
                syn_flood_warning(skb);
+               req->cookie_ts = tmp_opt.tstamp_ok;
 #endif
                isn = cookie_v4_init_sequence(sk, skb, &req->mss);
        } else if (!isn) {
@@ -1350,10 +1346,10 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
                    (dst = inet_csk_route_req(sk, req)) != NULL &&
                    (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
                    peer->v4daddr == saddr) {
-                       if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
+                       if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
                            (s32)(peer->tcp_ts - req->ts_recent) >
                                                        TCP_PAWS_WINDOW) {
-                               NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
+                               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
                                goto drop_and_release;
                        }
                }
@@ -1370,10 +1366,8 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
                         * to destinations, already remembered
                         * to the moment of synflood.
                         */
-                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
-                                      "request from %u.%u.%u.%u/%u\n",
-                                      NIPQUAD(saddr),
-                                      ntohs(tcp_hdr(skb)->source));
+                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI4/%u\n",
+                                      &saddr, ntohs(tcp_hdr(skb)->source));
                        goto drop_and_release;
                }
 
@@ -1381,7 +1375,9 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
        }
        tcp_rsk(req)->snt_isn = isn;
 
-       if (__tcp_v4_send_synack(sk, req, dst) || want_cookie)
+       if (tcp_v4_send_synack(sk, dst, req,
+                              (struct request_values *)&tmp_ext) ||
+           want_cookie)
                goto drop_and_free;
 
        inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
@@ -1428,9 +1424,9 @@ 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->daddr        = ireq->rmt_addr;
-       newinet->rcv_saddr    = ireq->loc_addr;
-       newinet->saddr        = ireq->loc_addr;
+       newinet->inet_daddr   = ireq->rmt_addr;
+       newinet->inet_rcv_saddr = ireq->loc_addr;
+       newinet->inet_saddr           = ireq->loc_addr;
        newinet->opt          = ireq->opt;
        ireq->opt             = NULL;
        newinet->mc_index     = inet_iif(skb);
@@ -1438,16 +1434,21 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        inet_csk(newsk)->icsk_ext_hdr_len = 0;
        if (newinet->opt)
                inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
-       newinet->id = newtp->write_seq ^ jiffies;
+       newinet->inet_id = newtp->write_seq ^ jiffies;
 
        tcp_mtup_init(newsk);
        tcp_sync_mss(newsk, dst_mtu(dst));
        newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
+       if (tcp_sk(sk)->rx_opt.user_mss &&
+           tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
+               newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
+
        tcp_initialize_rcv_mss(newsk);
 
 #ifdef CONFIG_TCP_MD5SIG
        /* Copy over the MD5 key from the original socket */
-       if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
+       key = tcp_v4_md5_do_lookup(sk, newinet->inet_daddr);
+       if (key != NULL) {
                /*
                 * We're using one, so create a matching key
                 * on the newsk structure. If we fail to get
@@ -1456,20 +1457,21 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
                 */
                char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
                if (newkey != NULL)
-                       tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
+                       tcp_v4_md5_do_add(newsk, newinet->inet_daddr,
                                          newkey, key->keylen);
+               newsk->sk_route_caps &= ~NETIF_F_GSO_MASK;
        }
 #endif
 
-       __inet_hash_nolisten(newsk);
+       __inet_hash_nolisten(newsk, NULL);
        __inet_inherit_port(sk, newsk);
 
        return newsk;
 
 exit_overflow:
-       NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
+       NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
 exit:
-       NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
+       NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
        dst_release(dst);
        return NULL;
 }
@@ -1486,7 +1488,7 @@ static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
        if (req)
                return tcp_check_req(sk, skb, req, prev);
 
-       nsk = inet_lookup_established(sk->sk_net, &tcp_hashinfo, iph->saddr,
+       nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
                        th->source, iph->daddr, th->dest, inet_iif(skb));
 
        if (nsk) {
@@ -1596,7 +1598,7 @@ discard:
        return 0;
 
 csum_err:
-       TCP_INC_STATS_BH(TCP_MIB_INERRS);
+       TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
        goto discard;
 }
 
@@ -1610,12 +1612,13 @@ int tcp_v4_rcv(struct sk_buff *skb)
        struct tcphdr *th;
        struct sock *sk;
        int ret;
+       struct net *net = dev_net(skb->dev);
 
        if (skb->pkt_type != PACKET_HOST)
                goto discard_it;
 
        /* Count it even if it's bad */
-       TCP_INC_STATS_BH(TCP_MIB_INSEGS);
+       TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
 
        if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
                goto discard_it;
@@ -1644,11 +1647,13 @@ int tcp_v4_rcv(struct sk_buff *skb)
        TCP_SKB_CB(skb)->flags   = iph->tos;
        TCP_SKB_CB(skb)->sacked  = 0;
 
-       sk = __inet_lookup(skb->dev->nd_net, &tcp_hashinfo, iph->saddr,
-                       th->source, iph->daddr, th->dest, inet_iif(skb));
+       sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
        if (!sk)
                goto no_tcp_socket;
 
+       if (iph->ttl < inet_sk(sk)->min_ttl)
+               goto discard_and_relse;
+
 process:
        if (sk->sk_state == TCP_TIME_WAIT)
                goto do_time_wait;
@@ -1668,14 +1673,14 @@ process:
 #ifdef CONFIG_NET_DMA
                struct tcp_sock *tp = tcp_sk(sk);
                if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
-                       tp->ucopy.dma_chan = get_softnet_dma();
+                       tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
                if (tp->ucopy.dma_chan)
                        ret = tcp_v4_do_rcv(sk, skb);
                else
 #endif
                {
                        if (!tcp_prequeue(sk, skb))
-                       ret = tcp_v4_do_rcv(sk, skb);
+                               ret = tcp_v4_do_rcv(sk, skb);
                }
        } else
                sk_add_backlog(sk, skb);
@@ -1691,7 +1696,7 @@ no_tcp_socket:
 
        if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
 bad_packet:
-               TCP_INC_STATS_BH(TCP_MIB_INERRS);
+               TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
        } else {
                tcp_v4_send_reset(NULL, skb);
        }
@@ -1712,13 +1717,13 @@ do_time_wait:
        }
 
        if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
-               TCP_INC_STATS_BH(TCP_MIB_INERRS);
+               TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
                inet_twsk_put(inet_twsk(sk));
                goto discard_it;
        }
        switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
        case TCP_TW_SYN: {
-               struct sock *sk2 = inet_lookup_listener(skb->dev->nd_net,
+               struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
                                                        &tcp_hashinfo,
                                                        iph->daddr, th->dest,
                                                        inet_iif(skb));
@@ -1754,8 +1759,8 @@ int tcp_v4_remember_stamp(struct sock *sk)
        struct inet_peer *peer = NULL;
        int release_it = 0;
 
-       if (!rt || rt->rt_dst != inet->daddr) {
-               peer = inet_getpeer(inet->daddr, 1);
+       if (!rt || rt->rt_dst != inet->inet_daddr) {
+               peer = inet_getpeer(inet->inet_daddr, 1);
                release_it = 1;
        } else {
                if (!rt->peer)
@@ -1765,9 +1770,9 @@ int tcp_v4_remember_stamp(struct sock *sk)
 
        if (peer) {
                if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
-                   (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
-                    peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
-                       peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
+                   ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
+                    peer->tcp_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
+                       peer->tcp_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
                        peer->tcp_ts = tp->rx_opt.ts_recent;
                }
                if (release_it)
@@ -1786,9 +1791,9 @@ int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
                const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
 
                if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
-                   (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
-                    peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
-                       peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
+                   ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
+                    peer->tcp_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
+                       peer->tcp_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
                        peer->tcp_ts       = tcptw->tw_ts_recent;
                }
                inet_putpeer(peer);
@@ -1798,7 +1803,7 @@ int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
        return 0;
 }
 
-struct inet_connection_sock_af_ops ipv4_specific = {
+const struct inet_connection_sock_af_ops ipv4_specific = {
        .queue_xmit        = ip_queue_xmit,
        .send_check        = tcp_v4_send_check,
        .rebuild_header    = inet_sk_rebuild_header,
@@ -1818,9 +1823,9 @@ struct inet_connection_sock_af_ops ipv4_specific = {
 };
 
 #ifdef CONFIG_TCP_MD5SIG
-static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
+static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
        .md5_lookup             = tcp_v4_md5_lookup,
-       .calc_md5_hash          = tcp_v4_calc_md5_hash,
+       .calc_md5_hash          = tcp_v4_md5_hash_skb,
        .md5_add                = tcp_v4_md5_add_func,
        .md5_parse              = tcp_v4_parse_md5_keys,
 };
@@ -1851,9 +1856,9 @@ static int tcp_v4_init_sock(struct sock *sk)
        /* See draft-stevens-tcpca-spec-01 for discussion of the
         * initialization of these values.
         */
-       tp->snd_ssthresh = 0x7fffffff;  /* Infinity */
+       tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
        tp->snd_cwnd_clamp = ~0;
-       tp->mss_cache = 536;
+       tp->mss_cache = TCP_MSS_DEFAULT;
 
        tp->reordering = sysctl_tcp_reordering;
        icsk->icsk_ca_ops = &tcp_init_congestion_ops;
@@ -1869,15 +1874,30 @@ static int tcp_v4_init_sock(struct sock *sk)
        tp->af_specific = &tcp_sock_ipv4_specific;
 #endif
 
+       /* TCP Cookie Transactions */
+       if (sysctl_tcp_cookie_size > 0) {
+               /* Default, cookies without s_data_payload. */
+               tp->cookie_values =
+                       kzalloc(sizeof(*tp->cookie_values),
+                               sk->sk_allocation);
+               if (tp->cookie_values != NULL)
+                       kref_init(&tp->cookie_values->kref);
+       }
+       /* Presumed zeroed, in order of appearance:
+        *      cookie_in_always, cookie_out_never,
+        *      s_data_constant, s_data_in, s_data_out
+        */
        sk->sk_sndbuf = sysctl_tcp_wmem[1];
        sk->sk_rcvbuf = sysctl_tcp_rmem[1];
 
-       atomic_inc(&tcp_sockets_allocated);
+       local_bh_disable();
+       percpu_counter_inc(&tcp_sockets_allocated);
+       local_bh_enable();
 
        return 0;
 }
 
-int tcp_v4_destroy_sock(struct sock *sk)
+void tcp_v4_destroy_sock(struct sock *sk)
 {
        struct tcp_sock *tp = tcp_sk(sk);
 
@@ -1920,17 +1940,14 @@ int tcp_v4_destroy_sock(struct sock *sk)
                sk->sk_sndmsg_page = NULL;
        }
 
-       if (tp->defer_tcp_accept.request) {
-               reqsk_free(tp->defer_tcp_accept.request);
-               sock_put(tp->defer_tcp_accept.listen_sk);
-               sock_put(sk);
-               tp->defer_tcp_accept.listen_sk = NULL;
-               tp->defer_tcp_accept.request = NULL;
+       /* TCP Cookie Transactions */
+       if (tp->cookie_values != NULL) {
+               kref_put(&tp->cookie_values->kref,
+                        tcp_cookie_values_release);
+               tp->cookie_values = NULL;
        }
 
-       atomic_dec(&tcp_sockets_allocated);
-
-       return 0;
+       percpu_counter_dec(&tcp_sockets_allocated);
 }
 
 EXPORT_SYMBOL(tcp_v4_destroy_sock);
@@ -1938,32 +1955,35 @@ EXPORT_SYMBOL(tcp_v4_destroy_sock);
 #ifdef CONFIG_PROC_FS
 /* Proc filesystem TCP sock list dumping. */
 
-static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
+static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head)
 {
-       return hlist_empty(head) ? NULL :
+       return hlist_nulls_empty(head) ? NULL :
                list_entry(head->first, struct inet_timewait_sock, tw_node);
 }
 
 static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
 {
-       return tw->tw_node.next ?
-               hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
+       return !is_a_nulls(tw->tw_node.next) ?
+               hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
 }
 
 static void *listening_get_next(struct seq_file *seq, void *cur)
 {
        struct inet_connection_sock *icsk;
-       struct hlist_node *node;
+       struct hlist_nulls_node *node;
        struct sock *sk = cur;
-       struct tcp_iter_state* st = seq->private;
-       struct net *net = st->net;
+       struct inet_listen_hashbucket *ilb;
+       struct tcp_iter_state *st = seq->private;
+       struct net *net = seq_file_net(seq);
 
        if (!sk) {
                st->bucket = 0;
-               sk = sk_head(&tcp_hashinfo.listening_hash[0]);
+               ilb = &tcp_hashinfo.listening_hash[0];
+               spin_lock_bh(&ilb->lock);
+               sk = sk_nulls_head(&ilb->head);
                goto get_sk;
        }
-
+       ilb = &tcp_hashinfo.listening_hash[st->bucket];
        ++st->num;
 
        if (st->state == TCP_SEQ_STATE_OPENREQ) {
@@ -1973,8 +1993,7 @@ static void *listening_get_next(struct seq_file *seq, void *cur)
                req = req->dl_next;
                while (1) {
                        while (req) {
-                               if (req->rsk_ops->family == st->family &&
-                                   req->sk->sk_net == net) {
+                               if (req->rsk_ops->family == st->family) {
                                        cur = req;
                                        goto out;
                                }
@@ -1997,8 +2016,8 @@ get_req:
                sk = sk_next(sk);
        }
 get_sk:
-       sk_for_each_from(sk, node) {
-               if (sk->sk_family == st->family && sk->sk_net == net) {
+       sk_nulls_for_each_from(sk, node) {
+               if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
                        cur = sk;
                        goto out;
                }
@@ -2014,8 +2033,11 @@ start_req:
                }
                read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
        }
+       spin_unlock_bh(&ilb->lock);
        if (++st->bucket < INET_LHTABLE_SIZE) {
-               sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
+               ilb = &tcp_hashinfo.listening_hash[st->bucket];
+               spin_lock_bh(&ilb->lock);
+               sk = sk_nulls_head(&ilb->head);
                goto get_sk;
        }
        cur = NULL;
@@ -2034,22 +2056,32 @@ static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
        return rc;
 }
 
+static inline int empty_bucket(struct tcp_iter_state *st)
+{
+       return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) &&
+               hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain);
+}
+
 static void *established_get_first(struct seq_file *seq)
 {
-       struct tcp_iter_statest = seq->private;
-       struct net *net = st->net;
+       struct tcp_iter_state *st = seq->private;
+       struct net *net = seq_file_net(seq);
        void *rc = NULL;
 
-       for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
+       for (st->bucket = 0; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
                struct sock *sk;
-               struct hlist_node *node;
+               struct hlist_nulls_node *node;
                struct inet_timewait_sock *tw;
-               rwlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
+               spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
 
-               read_lock_bh(lock);
-               sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
+               /* Lockless fast path for the common case of empty buckets */
+               if (empty_bucket(st))
+                       continue;
+
+               spin_lock_bh(lock);
+               sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
                        if (sk->sk_family != st->family ||
-                           sk->sk_net != net) {
+                           !net_eq(sock_net(sk), net)) {
                                continue;
                        }
                        rc = sk;
@@ -2059,13 +2091,13 @@ static void *established_get_first(struct seq_file *seq)
                inet_twsk_for_each(tw, node,
                                   &tcp_hashinfo.ehash[st->bucket].twchain) {
                        if (tw->tw_family != st->family ||
-                           tw->tw_net != net) {
+                           !net_eq(twsk_net(tw), net)) {
                                continue;
                        }
                        rc = tw;
                        goto out;
                }
-               read_unlock_bh(lock);
+               spin_unlock_bh(lock);
                st->state = TCP_SEQ_STATE_ESTABLISHED;
        }
 out:
@@ -2076,9 +2108,9 @@ static void *established_get_next(struct seq_file *seq, void *cur)
 {
        struct sock *sk = cur;
        struct inet_timewait_sock *tw;
-       struct hlist_node *node;
-       struct tcp_iter_statest = seq->private;
-       struct net *net = st->net;
+       struct hlist_nulls_node *node;
+       struct tcp_iter_state *st = seq->private;
+       struct net *net = seq_file_net(seq);
 
        ++st->num;
 
@@ -2086,28 +2118,30 @@ static void *established_get_next(struct seq_file *seq, void *cur)
                tw = cur;
                tw = tw_next(tw);
 get_tw:
-               while (tw && (tw->tw_family != st->family || tw->tw_net != net)) {
+               while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
                        tw = tw_next(tw);
                }
                if (tw) {
                        cur = tw;
                        goto out;
                }
-               read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
+               spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
                st->state = TCP_SEQ_STATE_ESTABLISHED;
 
-               if (++st->bucket < tcp_hashinfo.ehash_size) {
-                       read_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
-                       sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
-               } else {
-                       cur = NULL;
-                       goto out;
-               }
+               /* Look for next non empty bucket */
+               while (++st->bucket <= tcp_hashinfo.ehash_mask &&
+                               empty_bucket(st))
+                       ;
+               if (st->bucket > tcp_hashinfo.ehash_mask)
+                       return NULL;
+
+               spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
+               sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain);
        } else
-               sk = sk_next(sk);
+               sk = sk_nulls_next(sk);
 
-       sk_for_each_from(sk, node) {
-               if (sk->sk_family == st->family && sk->sk_net == net)
+       sk_nulls_for_each_from(sk, node) {
+               if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
                        goto found;
        }
 
@@ -2134,14 +2168,12 @@ static void *established_get_idx(struct seq_file *seq, loff_t pos)
 static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
 {
        void *rc;
-       struct tcp_iter_statest = seq->private;
+       struct tcp_iter_state *st = seq->private;
 
-       inet_listen_lock(&tcp_hashinfo);
        st->state = TCP_SEQ_STATE_LISTENING;
        rc        = listening_get_idx(seq, &pos);
 
        if (!rc) {
-               inet_listen_unlock(&tcp_hashinfo);
                st->state = TCP_SEQ_STATE_ESTABLISHED;
                rc        = established_get_idx(seq, pos);
        }
@@ -2151,7 +2183,7 @@ static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
 
 static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
 {
-       struct tcp_iter_statest = seq->private;
+       struct tcp_iter_state *st = seq->private;
        st->state = TCP_SEQ_STATE_LISTENING;
        st->num = 0;
        return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
@@ -2160,7 +2192,7 @@ static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
 static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
        void *rc = NULL;
-       struct tcp_iter_statest;
+       struct tcp_iter_state *st;
 
        if (v == SEQ_START_TOKEN) {
                rc = tcp_get_idx(seq, 0);
@@ -2173,7 +2205,6 @@ static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
        case TCP_SEQ_STATE_LISTENING:
                rc = listening_get_next(seq, v);
                if (!rc) {
-                       inet_listen_unlock(&tcp_hashinfo);
                        st->state = TCP_SEQ_STATE_ESTABLISHED;
                        rc        = established_get_first(seq);
                }
@@ -2190,7 +2221,7 @@ out:
 
 static void tcp_seq_stop(struct seq_file *seq, void *v)
 {
-       struct tcp_iter_statest = seq->private;
+       struct tcp_iter_state *st = seq->private;
 
        switch (st->state) {
        case TCP_SEQ_STATE_OPENREQ:
@@ -2200,12 +2231,12 @@ static void tcp_seq_stop(struct seq_file *seq, void *v)
                }
        case TCP_SEQ_STATE_LISTENING:
                if (v != SEQ_START_TOKEN)
-                       inet_listen_unlock(&tcp_hashinfo);
+                       spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
                break;
        case TCP_SEQ_STATE_TIME_WAIT:
        case TCP_SEQ_STATE_ESTABLISHED:
                if (v)
-                       read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
+                       spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
                break;
        }
 }
@@ -2213,51 +2244,16 @@ static void tcp_seq_stop(struct seq_file *seq, void *v)
 static int tcp_seq_open(struct inode *inode, struct file *file)
 {
        struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
-       struct seq_file *seq;
        struct tcp_iter_state *s;
-       struct net *net;
-       int rc;
-
-       if (unlikely(afinfo == NULL))
-               return -EINVAL;
-
-       s = kzalloc(sizeof(*s), GFP_KERNEL);
-       if (!s)
-               return -ENOMEM;
+       int err;
 
-       rc = -ENXIO;
-       net = get_proc_net(inode);
-       if (!net)
-               goto out_kfree;
+       err = seq_open_net(inode, file, &afinfo->seq_ops,
+                         sizeof(struct tcp_iter_state));
+       if (err < 0)
+               return err;
 
+       s = ((struct seq_file *)file->private_data)->private;
        s->family               = afinfo->family;
-       s->seq_ops.start        = tcp_seq_start;
-       s->seq_ops.next         = tcp_seq_next;
-       s->seq_ops.show         = afinfo->seq_show;
-       s->seq_ops.stop         = tcp_seq_stop;
-       s->net                  = net;
-
-       rc = seq_open(file, &s->seq_ops);
-       if (rc)
-               goto out_put_net;
-       seq = file->private_data;
-       seq->private = s;
-out:
-       return rc;
-out_put_net:
-       put_net(net);
-out_kfree:
-       kfree(s);
-       goto out;
-}
-
-static int tcp_seq_release(struct inode *inode, struct file *file)
-{
-       struct seq_file *seq = file->private_data;
-       struct tcp_iter_state *s = seq->private;
-
-       put_net(s->net);
-       seq_release_private(inode, file);
        return 0;
 }
 
@@ -2266,41 +2262,38 @@ int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
        int rc = 0;
        struct proc_dir_entry *p;
 
-       if (!afinfo)
-               return -EINVAL;
-       afinfo->seq_fops->owner         = afinfo->owner;
-       afinfo->seq_fops->open          = tcp_seq_open;
-       afinfo->seq_fops->read          = seq_read;
-       afinfo->seq_fops->llseek        = seq_lseek;
-       afinfo->seq_fops->release       = tcp_seq_release;
-
-       p = proc_net_fops_create(net, afinfo->name, S_IRUGO, afinfo->seq_fops);
-       if (p)
-               p->data = afinfo;
-       else
+       afinfo->seq_fops.open           = tcp_seq_open;
+       afinfo->seq_fops.read           = seq_read;
+       afinfo->seq_fops.llseek         = seq_lseek;
+       afinfo->seq_fops.release        = seq_release_net;
+
+       afinfo->seq_ops.start           = tcp_seq_start;
+       afinfo->seq_ops.next            = tcp_seq_next;
+       afinfo->seq_ops.stop            = tcp_seq_stop;
+
+       p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
+                            &afinfo->seq_fops, afinfo);
+       if (!p)
                rc = -ENOMEM;
        return rc;
 }
 
 void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
 {
-       if (!afinfo)
-               return;
        proc_net_remove(net, afinfo->name);
-       memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
 }
 
 static void get_openreq4(struct sock *sk, struct request_sock *req,
-                        char *tmpbuf, int i, int uid)
+                        struct seq_file *f, int i, int uid, int *len)
 {
        const struct inet_request_sock *ireq = inet_rsk(req);
        int ttd = req->expires - jiffies;
 
-       sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
-               " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p",
+       seq_printf(f, "%4d: %08X:%04X %08X:%04X"
+               " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
                i,
                ireq->loc_addr,
-               ntohs(inet_sk(sk)->sport),
+               ntohs(inet_sk(sk)->inet_sport),
                ireq->rmt_addr,
                ntohs(ireq->rmt_port),
                TCP_SYN_RECV,
@@ -2312,20 +2305,22 @@ static void get_openreq4(struct sock *sk, struct request_sock *req,
                0,  /* non standard timer */
                0, /* open_requests have no inode */
                atomic_read(&sk->sk_refcnt),
-               req);
+               req,
+               len);
 }
 
-static void get_tcp4_sock(struct sock *sk, char *tmpbuf, int i)
+static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
 {
        int timer_active;
        unsigned long timer_expires;
        struct tcp_sock *tp = tcp_sk(sk);
        const struct inet_connection_sock *icsk = inet_csk(sk);
        struct inet_sock *inet = inet_sk(sk);
-       __be32 dest = inet->daddr;
-       __be32 src = inet->rcv_saddr;
-       __u16 destp = ntohs(inet->dport);
-       __u16 srcp = ntohs(inet->sport);
+       __be32 dest = inet->inet_daddr;
+       __be32 src = inet->inet_rcv_saddr;
+       __u16 destp = ntohs(inet->inet_dport);
+       __u16 srcp = ntohs(inet->inet_sport);
+       int rx_queue;
 
        if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
                timer_active    = 1;
@@ -2341,12 +2336,19 @@ static void get_tcp4_sock(struct sock *sk, char *tmpbuf, int i)
                timer_expires = jiffies;
        }
 
-       sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
-                       "%08X %5d %8d %lu %d %p %u %u %u %u %d",
+       if (sk->sk_state == TCP_LISTEN)
+               rx_queue = sk->sk_ack_backlog;
+       else
+               /*
+                * because we dont lock socket, we might find a transient negative value
+                */
+               rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
+
+       seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
+                       "%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
                i, src, srcp, dest, destp, sk->sk_state,
                tp->write_seq - tp->snd_una,
-               sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog :
-                                            (tp->rcv_nxt - tp->copied_seq),
+               rx_queue,
                timer_active,
                jiffies_to_clock_t(timer_expires - jiffies),
                icsk->icsk_retransmits,
@@ -2354,15 +2356,16 @@ static void get_tcp4_sock(struct sock *sk, char *tmpbuf, int i)
                icsk->icsk_probes_out,
                sock_i_ino(sk),
                atomic_read(&sk->sk_refcnt), sk,
-               icsk->icsk_rto,
-               icsk->icsk_ack.ato,
+               jiffies_to_clock_t(icsk->icsk_rto),
+               jiffies_to_clock_t(icsk->icsk_ack.ato),
                (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
                tp->snd_cwnd,
-               tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh);
+               tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh,
+               len);
 }
 
 static void get_timewait4_sock(struct inet_timewait_sock *tw,
-                              char *tmpbuf, int i)
+                              struct seq_file *f, int i, int *len)
 {
        __be32 dest, src;
        __u16 destp, srcp;
@@ -2376,19 +2379,19 @@ static void get_timewait4_sock(struct inet_timewait_sock *tw,
        destp = ntohs(tw->tw_dport);
        srcp  = ntohs(tw->tw_sport);
 
-       sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
-               " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p",
+       seq_printf(f, "%4d: %08X:%04X %08X:%04X"
+               " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
                i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
                3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
-               atomic_read(&tw->tw_refcnt), tw);
+               atomic_read(&tw->tw_refcnt), tw, len);
 }
 
 #define TMPSZ 150
 
 static int tcp4_seq_show(struct seq_file *seq, void *v)
 {
-       struct tcp_iter_statest;
-       char tmpbuf[TMPSZ + 1];
+       struct tcp_iter_state *st;
+       int len;
 
        if (v == SEQ_START_TOKEN) {
                seq_printf(seq, "%-*s\n", TMPSZ - 1,
@@ -2402,35 +2405,37 @@ static int tcp4_seq_show(struct seq_file *seq, void *v)
        switch (st->state) {
        case TCP_SEQ_STATE_LISTENING:
        case TCP_SEQ_STATE_ESTABLISHED:
-               get_tcp4_sock(v, tmpbuf, st->num);
+               get_tcp4_sock(v, seq, st->num, &len);
                break;
        case TCP_SEQ_STATE_OPENREQ:
-               get_openreq4(st->syn_wait_sk, v, tmpbuf, st->num, st->uid);
+               get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
                break;
        case TCP_SEQ_STATE_TIME_WAIT:
-               get_timewait4_sock(v, tmpbuf, st->num);
+               get_timewait4_sock(v, seq, st->num, &len);
                break;
        }
-       seq_printf(seq, "%-*s\n", TMPSZ - 1, tmpbuf);
+       seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
 out:
        return 0;
 }
 
-static struct file_operations tcp4_seq_fops;
 static struct tcp_seq_afinfo tcp4_seq_afinfo = {
-       .owner          = THIS_MODULE,
        .name           = "tcp",
        .family         = AF_INET,
-       .seq_show       = tcp4_seq_show,
-       .seq_fops       = &tcp4_seq_fops,
+       .seq_fops       = {
+               .owner          = THIS_MODULE,
+       },
+       .seq_ops        = {
+               .show           = tcp4_seq_show,
+       },
 };
 
-static int tcp4_proc_init_net(struct net *net)
+static int __net_init tcp4_proc_init_net(struct net *net)
 {
        return tcp_proc_register(net, &tcp4_seq_afinfo);
 }
 
-static void tcp4_proc_exit_net(struct net *net)
+static void __net_exit tcp4_proc_exit_net(struct net *net)
 {
        tcp_proc_unregister(net, &tcp4_seq_afinfo);
 }
@@ -2451,7 +2456,40 @@ void tcp4_proc_exit(void)
 }
 #endif /* CONFIG_PROC_FS */
 
-DEFINE_PROTO_INUSE(tcp)
+struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
+{
+       struct iphdr *iph = skb_gro_network_header(skb);
+
+       switch (skb->ip_summed) {
+       case CHECKSUM_COMPLETE:
+               if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
+                                 skb->csum)) {
+                       skb->ip_summed = CHECKSUM_UNNECESSARY;
+                       break;
+               }
+
+               /* fall through */
+       case CHECKSUM_NONE:
+               NAPI_GRO_CB(skb)->flush = 1;
+               return NULL;
+       }
+
+       return tcp_gro_receive(head, skb);
+}
+EXPORT_SYMBOL(tcp4_gro_receive);
+
+int tcp4_gro_complete(struct sk_buff *skb)
+{
+       struct iphdr *iph = ip_hdr(skb);
+       struct tcphdr *th = tcp_hdr(skb);
+
+       th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
+                                 iph->saddr, iph->daddr, 0);
+       skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
+
+       return tcp_gro_complete(skb);
+}
+EXPORT_SYMBOL(tcp4_gro_complete);
 
 struct proto tcp_prot = {
        .name                   = "TCP",
@@ -2481,6 +2519,7 @@ struct proto tcp_prot = {
        .sysctl_rmem            = sysctl_tcp_rmem,
        .max_header             = MAX_TCP_HEADER,
        .obj_size               = sizeof(struct tcp_sock),
+       .slab_flags             = SLAB_DESTROY_BY_RCU,
        .twsk_prot              = &tcp_timewait_sock_ops,
        .rsk_prot               = &tcp_request_sock_ops,
        .h.hashinfo             = &tcp_hashinfo,
@@ -2488,13 +2527,35 @@ struct proto tcp_prot = {
        .compat_setsockopt      = compat_tcp_setsockopt,
        .compat_getsockopt      = compat_tcp_getsockopt,
 #endif
-       REF_PROTO_INUSE(tcp)
+};
+
+
+static int __net_init tcp_sk_init(struct net *net)
+{
+       return inet_ctl_sock_create(&net->ipv4.tcp_sock,
+                                   PF_INET, SOCK_RAW, IPPROTO_TCP, net);
+}
+
+static void __net_exit tcp_sk_exit(struct net *net)
+{
+       inet_ctl_sock_destroy(net->ipv4.tcp_sock);
+}
+
+static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
+{
+       inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
+}
+
+static struct pernet_operations __net_initdata tcp_sk_ops = {
+       .init      = tcp_sk_init,
+       .exit      = tcp_sk_exit,
+       .exit_batch = tcp_sk_exit_batch,
 };
 
 void __init tcp_v4_init(void)
 {
-       if (inet_csk_ctl_sock_create(&tcp_socket, PF_INET, SOCK_RAW,
-                                    IPPROTO_TCP) < 0)
+       inet_hashinfo_init(&tcp_hashinfo);
+       if (register_pernet_subsys(&tcp_sk_ops))
                panic("Failed to create the TCP control socket.\n");
 }