Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[pandora-kernel.git] / net / ipv4 / tcp_ipv4.c
index 4300bcf..91a8cfd 100644 (file)
@@ -87,9 +87,8 @@ int sysctl_tcp_low_latency __read_mostly;
 #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, 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)
@@ -175,7 +174,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
                               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;
        }
 
@@ -343,16 +342,17 @@ void tcp_v4_err(struct sk_buff *skb, u32 info)
        struct sock *sk;
        __u32 seq;
        int err;
+       struct net *net = dev_net(skb->dev);
 
        if (skb->len < (iph->ihl << 2) + 8) {
-               ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
+               ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
                return;
        }
 
-       sk = inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->daddr, th->dest,
+       sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest,
                        iph->saddr, th->source, inet_iif(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) {
@@ -365,7 +365,7 @@ 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;
@@ -374,7 +374,7 @@ void tcp_v4_err(struct sk_buff *skb, u32 info)
        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;
        }
 
@@ -418,10 +418,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;
                }
 
@@ -543,6 +543,7 @@ 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)
@@ -581,11 +582,9 @@ 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, arg.iov[0].iov_len);
+               tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
+                                    key, ip_hdr(skb)->daddr,
+                                    ip_hdr(skb)->saddr, &rep.th);
        }
 #endif
        arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
@@ -593,11 +592,12 @@ static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
                                      sizeof(struct tcphdr), IPPROTO_TCP, 0);
        arg.csumoffset = offsetof(struct tcphdr, check) / 2;
 
-       ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb,
+       net = dev_net(skb->dst->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
@@ -618,6 +618,7 @@ static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
                        ];
        } rep;
        struct ip_reply_arg arg;
+       struct net *net = dev_net(skb->dev);
 
        memset(&rep.th, 0, sizeof(struct tcphdr));
        memset(&arg, 0, sizeof(arg));
@@ -653,11 +654,9 @@ static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
                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, 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.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
@@ -667,10 +666,10 @@ static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
        if (oif)
                arg.bound_dev_if = oif;
 
-       ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb,
+       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)
@@ -985,28 +984,16 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
                                 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,
-                                  unsigned int tcplen)
+static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
+                                       __be32 daddr, __be32 saddr, int nbytes)
 {
-       struct tcp_md5sig_pool *hp;
        struct tcp4_pseudohdr *bp;
-       int err;
-
-       /*
-        * 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;
 
        /*
-        * The TCP pseudo-header (in the order: source IP address,
+        * 1. the TCP pseudo-header (in the order: source IP address,
         * destination IP address, zero-padded protocol number, and
         * segment length)
         */
@@ -1014,48 +1001,95 @@ static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
        bp->daddr = daddr;
        bp->pad = 0;
        bp->protocol = IPPROTO_TCP;
-       bp->len = htons(tcplen);
+       bp->len = cpu_to_be16(nbytes);
 
-       err = tcp_calc_md5_hash(md5_hash, key, sizeof(*bp),
-                               th, tcplen, hp);
-       if (err)
+       sg_init_one(&sg, bp, sizeof(*bp));
+       return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
+}
+
+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;
+
+       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, th->doff << 2))
+               goto clear_hash;
+       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;
 
-       /* Free up the crypto pool */
        tcp_put_md5sig_pool();
-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,
-                        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;
+       } 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, 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)
 {
@@ -1082,28 +1116,21 @@ static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
                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, 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()) {
@@ -1247,7 +1274,7 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
                        if (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;
                        }
                }
@@ -1352,6 +1379,7 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
                if (newkey != NULL)
                        tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
                                          newkey, key->keylen);
+               newsk->sk_route_caps &= ~NETIF_F_GSO_MASK;
        }
 #endif
 
@@ -1361,9 +1389,9 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
        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;
 }
@@ -1490,7 +1518,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;
 }
 
@@ -1504,12 +1532,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;
@@ -1538,7 +1567,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
        TCP_SKB_CB(skb)->flags   = iph->tos;
        TCP_SKB_CB(skb)->sacked  = 0;
 
-       sk = __inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->saddr,
+       sk = __inet_lookup(net, &tcp_hashinfo, iph->saddr,
                        th->source, iph->daddr, th->dest, inet_iif(skb));
        if (!sk)
                goto no_tcp_socket;
@@ -1585,7 +1614,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);
        }
@@ -1606,7 +1635,7 @@ 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;
        }
@@ -1714,7 +1743,7 @@ struct inet_connection_sock_af_ops ipv4_specific = {
 #ifdef CONFIG_TCP_MD5SIG
 static 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,
 };
@@ -1857,8 +1886,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 &&
-                                   net_eq(sock_net(req->sk), net)) {
+                               if (req->rsk_ops->family == st->family) {
                                        cur = req;
                                        goto out;
                                }