Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / net / ipv4 / tcp_output.c
index 957c4e3..da2c3b8 100644 (file)
@@ -42,7 +42,7 @@
 /* People can turn this off for buggy TCP's found in printers etc. */
 int sysctl_tcp_retrans_collapse __read_mostly = 1;
 
-/* People can turn this on to  work with those rare, broken TCPs that
+/* People can turn this on to work with those rare, broken TCPs that
  * interpret the window field as a signed quantity.
  */
 int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
@@ -345,6 +345,11 @@ static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
        TCP_SKB_CB(skb)->end_seq = seq;
 }
 
+static inline int tcp_urg_mode(const struct tcp_sock *tp)
+{
+       return tp->snd_una != tp->snd_up;
+}
+
 #define OPTION_SACK_ADVERTISE  (1 << 0)
 #define OPTION_TS              (1 << 1)
 #define OPTION_MD5             (1 << 2)
@@ -357,6 +362,17 @@ struct tcp_out_options {
        __u32 tsval, tsecr;     /* need to include OPTION_TS */
 };
 
+/* Beware: Something in the Internet is very sensitive to the ordering of
+ * TCP options, we learned this through the hard way, so be careful here.
+ * Luckily we can at least blame others for their non-compliance but from
+ * inter-operatibility perspective it seems that we're somewhat stuck with
+ * the ordering which we have been using if we want to keep working with
+ * those broken things (not that it currently hurts anybody as there isn't
+ * particular reason why the ordering would need to be changed).
+ *
+ * At least SACK_PERM as the first option is known to lead to a disaster
+ * (but it may well be that other scenarios fail similarly).
+ */
 static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
                              const struct tcp_out_options *opts,
                              __u8 **md5_hash) {
@@ -371,6 +387,12 @@ static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
                *md5_hash = NULL;
        }
 
+       if (unlikely(opts->mss)) {
+               *ptr++ = htonl((TCPOPT_MSS << 24) |
+                              (TCPOLEN_MSS << 16) |
+                              opts->mss);
+       }
+
        if (likely(OPTION_TS & opts->options)) {
                if (unlikely(OPTION_SACK_ADVERTISE & opts->options)) {
                        *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
@@ -387,12 +409,6 @@ static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
                *ptr++ = htonl(opts->tsecr);
        }
 
-       if (unlikely(opts->mss)) {
-               *ptr++ = htonl((TCPOPT_MSS << 24) |
-                              (TCPOLEN_MSS << 16) |
-                              opts->mss);
-       }
-
        if (unlikely(OPTION_SACK_ADVERTISE & opts->options &&
                     !(OPTION_TS & opts->options))) {
                *ptr++ = htonl((TCPOPT_NOP << 24) |
@@ -427,7 +443,7 @@ static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
 
                if (tp->rx_opt.dsack) {
                        tp->rx_opt.dsack = 0;
-                       tp->rx_opt.eff_sacks--;
+                       tp->rx_opt.eff_sacks = tp->rx_opt.num_sacks;
                }
        }
 }
@@ -468,7 +484,7 @@ static unsigned tcp_syn_options(struct sock *sk, struct sk_buff *skb,
        }
        if (likely(sysctl_tcp_window_scaling)) {
                opts->ws = tp->rx_opt.rcv_wscale;
-               if(likely(opts->ws))
+               if (likely(opts->ws))
                        size += TCPOLEN_WSCALE_ALIGNED;
        }
        if (likely(sysctl_tcp_sack)) {
@@ -510,7 +526,7 @@ static unsigned tcp_synack_options(struct sock *sk,
 
        if (likely(ireq->wscale_ok)) {
                opts->ws = ireq->rcv_wscale;
-               if(likely(opts->ws))
+               if (likely(opts->ws))
                        size += TCPOLEN_WSCALE_ALIGNED;
        }
        if (likely(doing_ts)) {
@@ -646,7 +662,8 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
        th->check               = 0;
        th->urg_ptr             = 0;
 
-       if (unlikely(tp->urg_mode &&
+       /* The urg_mode check is necessary during a below snd_una win probe */
+       if (unlikely(tcp_urg_mode(tp) &&
                     between(tp->snd_up, tcb->seq + 1, tcb->seq + 0xFFFF))) {
                th->urg_ptr             = htons(tp->snd_up - tcb->seq);
                th->urg                 = 1;
@@ -750,7 +767,7 @@ int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
 
        BUG_ON(len > skb->len);
 
-       tcp_clear_all_retrans_hints(tp);
+       tcp_clear_retrans_hints_partial(tp);
        nsize = skb_headlen(skb) - len;
        if (nsize < 0)
                nsize = 0;
@@ -1011,10 +1028,6 @@ unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
 
 /* Compute the current effective MSS, taking SACKs and IP options,
  * and even PMTU discovery events into account.
- *
- * LARGESEND note: !urg_mode is overkill, only frames up to snd_up
- * cannot be large. However, taking into account rare use of URG, this
- * is not a big flaw.
  */
 unsigned int tcp_current_mss(struct sock *sk, int large_allowed)
 {
@@ -1029,7 +1042,7 @@ unsigned int tcp_current_mss(struct sock *sk, int large_allowed)
 
        mss_now = tp->mss_cache;
 
-       if (large_allowed && sk_can_gso(sk) && !tp->urg_mode)
+       if (large_allowed && sk_can_gso(sk))
                doing_tso = 1;
 
        if (dst) {
@@ -1155,7 +1168,7 @@ static int tcp_init_tso_segs(struct sock *sk, struct sk_buff *skb,
 
 static inline int tcp_minshall_check(const struct tcp_sock *tp)
 {
-       return after(tp->snd_sml,tp->snd_una) &&
+       return after(tp->snd_sml, tp->snd_una) &&
                !after(tp->snd_sml, tp->snd_nxt);
 }
 
@@ -1193,7 +1206,7 @@ static inline int tcp_nagle_test(struct tcp_sock *tp, struct sk_buff *skb,
        /* Don't use the nagle rule for urgent data (or for the final FIN).
         * Nagle can be ignored during F-RTO too (see RFC4138).
         */
-       if (tp->urg_mode || (tp->frto_counter == 2) ||
+       if (tcp_urg_mode(tp) || (tp->frto_counter == 2) ||
            (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN))
                return 1;
 
@@ -1321,7 +1334,7 @@ static int tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb)
 
        /* Defer for less than two clock ticks. */
        if (tp->tso_deferred &&
-           ((jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
+           (((u32)jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
                goto send_now;
 
        in_flight = tcp_packets_in_flight(tp);
@@ -1499,10 +1512,15 @@ static int tcp_mtu_probe(struct sock *sk)
  * send_head.  This happens as incoming acks open up the remote
  * window for us.
  *
+ * LARGESEND note: !tcp_urg_mode is overkill, only frames between
+ * snd_up-64k-mss .. snd_up cannot be large. However, taking into
+ * account rare use of URG, this is not a big flaw.
+ *
  * Returns 1, if no segments are in flight and we have queued segments, but
  * cannot send anything now because of SWS or another problem.
  */
-static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
+static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
+                         int push_one, gfp_t gfp)
 {
        struct tcp_sock *tp = tcp_sk(sk);
        struct sk_buff *skb;
@@ -1510,20 +1528,16 @@ static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
        int cwnd_quota;
        int result;
 
-       /* If we are closed, the bytes will have to remain here.
-        * In time closedown will finish, we empty the write queue and all
-        * will be happy.
-        */
-       if (unlikely(sk->sk_state == TCP_CLOSE))
-               return 0;
-
        sent_pkts = 0;
 
-       /* Do MTU probing. */
-       if ((result = tcp_mtu_probe(sk)) == 0) {
-               return 0;
-       } else if (result > 0) {
-               sent_pkts = 1;
+       if (!push_one) {
+               /* Do MTU probing. */
+               result = tcp_mtu_probe(sk);
+               if (!result) {
+                       return 0;
+               } else if (result > 0) {
+                       sent_pkts = 1;
+               }
        }
 
        while ((skb = tcp_send_head(sk))) {
@@ -1545,12 +1559,12 @@ static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
                                                      nonagle : TCP_NAGLE_PUSH))))
                                break;
                } else {
-                       if (tcp_tso_should_defer(sk, skb))
+                       if (!push_one && tcp_tso_should_defer(sk, skb))
                                break;
                }
 
                limit = mss_now;
-               if (tso_segs > 1)
+               if (tso_segs > 1 && !tcp_urg_mode(tp))
                        limit = tcp_mss_split_point(sk, skb, mss_now,
                                                    cwnd_quota);
 
@@ -1560,7 +1574,7 @@ static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
 
                TCP_SKB_CB(skb)->when = tcp_time_stamp;
 
-               if (unlikely(tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC)))
+               if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
                        break;
 
                /* Advance the send_head.  This one is sent out.
@@ -1570,6 +1584,9 @@ static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
 
                tcp_minshall_update(tp, mss_now, skb);
                sent_pkts++;
+
+               if (push_one)
+                       break;
        }
 
        if (likely(sent_pkts)) {
@@ -1588,10 +1605,18 @@ void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
 {
        struct sk_buff *skb = tcp_send_head(sk);
 
-       if (skb) {
-               if (tcp_write_xmit(sk, cur_mss, nonagle))
-                       tcp_check_probe_timer(sk);
-       }
+       if (!skb)
+               return;
+
+       /* If we are closed, the bytes will have to remain here.
+        * In time closedown will finish, we empty the write queue and
+        * all will be happy.
+        */
+       if (unlikely(sk->sk_state == TCP_CLOSE))
+               return;
+
+       if (tcp_write_xmit(sk, cur_mss, nonagle, 0, GFP_ATOMIC))
+               tcp_check_probe_timer(sk);
 }
 
 /* Send _single_ skb sitting at the send head. This function requires
@@ -1600,36 +1625,10 @@ void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
 void tcp_push_one(struct sock *sk, unsigned int mss_now)
 {
        struct sk_buff *skb = tcp_send_head(sk);
-       unsigned int tso_segs, cwnd_quota;
 
        BUG_ON(!skb || skb->len < mss_now);
 
-       tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
-       cwnd_quota = tcp_snd_test(sk, skb, mss_now, TCP_NAGLE_PUSH);
-
-       if (likely(cwnd_quota)) {
-               unsigned int limit;
-
-               BUG_ON(!tso_segs);
-
-               limit = mss_now;
-               if (tso_segs > 1)
-                       limit = tcp_mss_split_point(sk, skb, mss_now,
-                                                   cwnd_quota);
-
-               if (skb->len > limit &&
-                   unlikely(tso_fragment(sk, skb, limit, mss_now)))
-                       return;
-
-               /* Send it out now. */
-               TCP_SKB_CB(skb)->when = tcp_time_stamp;
-
-               if (likely(!tcp_transmit_skb(sk, skb, 1, sk->sk_allocation))) {
-                       tcp_event_new_data_sent(sk, skb);
-                       tcp_cwnd_validate(sk);
-                       return;
-               }
-       }
+       tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation);
 }
 
 /* This function returns the amount that we can raise the
@@ -1749,46 +1748,22 @@ u32 __tcp_select_window(struct sock *sk)
        return window;
 }
 
-/* Attempt to collapse two adjacent SKB's during retransmission. */
-static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb,
-                                    int mss_now)
+/* Collapses two adjacent SKB's during retransmission. */
+static void tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb)
 {
        struct tcp_sock *tp = tcp_sk(sk);
        struct sk_buff *next_skb = tcp_write_queue_next(sk, skb);
        int skb_size, next_skb_size;
        u16 flags;
 
-       /* The first test we must make is that neither of these two
-        * SKB's are still referenced by someone else.
-        */
-       if (skb_cloned(skb) || skb_cloned(next_skb))
-               return;
-
        skb_size = skb->len;
        next_skb_size = next_skb->len;
        flags = TCP_SKB_CB(skb)->flags;
 
-       /* Also punt if next skb has been SACK'd. */
-       if (TCP_SKB_CB(next_skb)->sacked & TCPCB_SACKED_ACKED)
-               return;
-
-       /* Next skb is out of window. */
-       if (after(TCP_SKB_CB(next_skb)->end_seq, tcp_wnd_end(tp)))
-               return;
-
-       /* Punt if not enough space exists in the first SKB for
-        * the data in the second, or the total combined payload
-        * would exceed the MSS.
-        */
-       if ((next_skb_size > skb_tailroom(skb)) ||
-           ((skb_size + next_skb_size) > mss_now))
-               return;
-
        BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1);
 
        tcp_highest_sack_combine(sk, next_skb, skb);
 
-       /* Ok.  We will be able to collapse the packet. */
        tcp_unlink_write_queue(next_skb, sk);
 
        skb_copy_from_linear_data(next_skb, skb_put(skb, next_skb_size),
@@ -1823,59 +1798,67 @@ static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb,
        tp->packets_out -= tcp_skb_pcount(next_skb);
 
        /* changed transmit queue under us so clear hints */
-       tcp_clear_all_retrans_hints(tp);
+       tcp_clear_retrans_hints_partial(tp);
+       if (next_skb == tp->retransmit_skb_hint)
+               tp->retransmit_skb_hint = skb;
 
        sk_wmem_free_skb(sk, next_skb);
 }
 
-/* Do a simple retransmit without using the backoff mechanisms in
- * tcp_timer. This is used for path mtu discovery.
- * The socket is already locked here.
- */
-void tcp_simple_retransmit(struct sock *sk)
+static int tcp_can_collapse(struct sock *sk, struct sk_buff *skb)
+{
+       if (tcp_skb_pcount(skb) > 1)
+               return 0;
+       /* TODO: SACK collapsing could be used to remove this condition */
+       if (skb_shinfo(skb)->nr_frags != 0)
+               return 0;
+       if (skb_cloned(skb))
+               return 0;
+       if (skb == tcp_send_head(sk))
+               return 0;
+       /* Some heurestics for collapsing over SACK'd could be invented */
+       if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
+               return 0;
+
+       return 1;
+}
+
+static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to,
+                                    int space)
 {
-       const struct inet_connection_sock *icsk = inet_csk(sk);
        struct tcp_sock *tp = tcp_sk(sk);
-       struct sk_buff *skb;
-       unsigned int mss = tcp_current_mss(sk, 0);
-       u32 prior_lost = tp->lost_out;
+       struct sk_buff *skb = to, *tmp;
+       int first = 1;
 
-       tcp_for_write_queue(skb, sk) {
-               if (skb == tcp_send_head(sk))
+       if (!sysctl_tcp_retrans_collapse)
+               return;
+       if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN)
+               return;
+
+       tcp_for_write_queue_from_safe(skb, tmp, sk) {
+               if (!tcp_can_collapse(sk, skb))
                        break;
-               if (skb->len > mss &&
-                   !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
-                       if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
-                               TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
-                               tp->retrans_out -= tcp_skb_pcount(skb);
-                       }
-                       tcp_skb_mark_lost_uncond_verify(tp, skb);
-               }
-       }
 
-       tcp_clear_all_retrans_hints(tp);
+               space -= skb->len;
 
-       if (prior_lost == tp->lost_out)
-               return;
+               if (first) {
+                       first = 0;
+                       continue;
+               }
 
-       if (tcp_is_reno(tp))
-               tcp_limit_reno_sacked(tp);
+               if (space < 0)
+                       break;
+               /* Punt if not enough space exists in the first SKB for
+                * the data in the second
+                */
+               if (skb->len > skb_tailroom(to))
+                       break;
 
-       tcp_verify_left_out(tp);
+               if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp)))
+                       break;
 
-       /* Don't muck with the congestion window here.
-        * Reason is that we do not increase amount of _data_
-        * in network, but units changed and effective
-        * cwnd/ssthresh really reduced now.
-        */
-       if (icsk->icsk_ca_state != TCP_CA_Loss) {
-               tp->high_seq = tp->snd_nxt;
-               tp->snd_ssthresh = tcp_current_ssthresh(sk);
-               tp->prior_ssthresh = 0;
-               tp->undo_marker = 0;
-               tcp_set_ca_state(sk, TCP_CA_Loss);
+               tcp_collapse_retrans(sk, to);
        }
-       tcp_xmit_retransmit_queue(sk);
 }
 
 /* This retransmits one SKB.  Policy decisions and retransmit queue
@@ -1927,17 +1910,7 @@ int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
                        return -ENOMEM; /* We'll try again later. */
        }
 
-       /* Collapse two adjacent packets if worthwhile and we can. */
-       if (!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN) &&
-           (skb->len < (cur_mss >> 1)) &&
-           (tcp_write_queue_next(sk, skb) != tcp_send_head(sk)) &&
-           (!tcp_skb_is_last(sk, skb)) &&
-           (skb_shinfo(skb)->nr_frags == 0 &&
-            skb_shinfo(tcp_write_queue_next(sk, skb))->nr_frags == 0) &&
-           (tcp_skb_pcount(skb) == 1 &&
-            tcp_skb_pcount(tcp_write_queue_next(sk, skb)) == 1) &&
-           (sysctl_tcp_retrans_collapse != 0))
-               tcp_retrans_try_collapse(sk, skb, cur_mss);
+       tcp_retrans_try_collapse(sk, skb, cur_mss);
 
        /* Some Solaris stacks overoptimize and ignore the FIN on a
         * retransmit when old data is attached.  So strip it off
@@ -2032,96 +2005,85 @@ void tcp_xmit_retransmit_queue(struct sock *sk)
        const struct inet_connection_sock *icsk = inet_csk(sk);
        struct tcp_sock *tp = tcp_sk(sk);
        struct sk_buff *skb;
+       struct sk_buff *hole = NULL;
+       u32 last_lost;
+       int mib_idx;
+       int fwd_rexmitting = 0;
+
+       if (!tp->lost_out)
+               tp->retransmit_high = tp->snd_una;
 
-       if (tp->retransmit_skb_hint)
+       if (tp->retransmit_skb_hint) {
                skb = tp->retransmit_skb_hint;
-       else
+               last_lost = TCP_SKB_CB(skb)->end_seq;
+               if (after(last_lost, tp->retransmit_high))
+                       last_lost = tp->retransmit_high;
+       } else {
                skb = tcp_write_queue_head(sk);
+               last_lost = tp->snd_una;
+       }
 
-       /* First pass: retransmit lost packets. */
-       if (tp->lost_out) {
-               tcp_for_write_queue_from(skb, sk) {
-                       __u8 sacked = TCP_SKB_CB(skb)->sacked;
+       tcp_for_write_queue_from(skb, sk) {
+               __u8 sacked = TCP_SKB_CB(skb)->sacked;
 
-                       if (skb == tcp_send_head(sk))
-                               break;
-                       /* we could do better than to assign each time */
+               if (skb == tcp_send_head(sk))
+                       break;
+               /* we could do better than to assign each time */
+               if (hole == NULL)
                        tp->retransmit_skb_hint = skb;
 
-                       /* Assume this retransmit will generate
-                        * only one packet for congestion window
-                        * calculation purposes.  This works because
-                        * tcp_retransmit_skb() will chop up the
-                        * packet to be MSS sized and all the
-                        * packet counting works out.
-                        */
-                       if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
-                               return;
-                       if (!before(TCP_SKB_CB(skb)->seq, tp->retransmit_high))
+               /* Assume this retransmit will generate
+                * only one packet for congestion window
+                * calculation purposes.  This works because
+                * tcp_retransmit_skb() will chop up the
+                * packet to be MSS sized and all the
+                * packet counting works out.
+                */
+               if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
+                       return;
+
+               if (fwd_rexmitting) {
+begin_fwd:
+                       if (!before(TCP_SKB_CB(skb)->seq, tcp_highest_sack_seq(tp)))
                                break;
+                       mib_idx = LINUX_MIB_TCPFORWARDRETRANS;
 
-                       if (sacked & TCPCB_LOST) {
-                               if (!(sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))) {
-                                       int mib_idx;
-
-                                       if (tcp_retransmit_skb(sk, skb)) {
-                                               tp->retransmit_skb_hint = NULL;
-                                               return;
-                                       }
-                                       if (icsk->icsk_ca_state != TCP_CA_Loss)
-                                               mib_idx = LINUX_MIB_TCPFASTRETRANS;
-                                       else
-                                               mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS;
-                                       NET_INC_STATS_BH(sock_net(sk), mib_idx);
-
-                                       if (skb == tcp_write_queue_head(sk))
-                                               inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
-                                                                         inet_csk(sk)->icsk_rto,
-                                                                         TCP_RTO_MAX);
-                               }
+               } else if (!before(TCP_SKB_CB(skb)->seq, tp->retransmit_high)) {
+                       tp->retransmit_high = last_lost;
+                       if (!tcp_can_forward_retransmit(sk))
+                               break;
+                       /* Backtrack if necessary to non-L'ed skb */
+                       if (hole != NULL) {
+                               skb = hole;
+                               hole = NULL;
                        }
-               }
-       }
-
-       /* OK, demanded retransmission is finished. */
-       if (!tcp_can_forward_retransmit(sk))
-               return;
-
-       /* If nothing is SACKed, highest_sack in the loop won't be valid */
-       if (!tp->sacked_out)
-               return;
-
-       if (tp->forward_skb_hint)
-               skb = tp->forward_skb_hint;
-       else
-               skb = tcp_write_queue_head(sk);
-
-       tcp_for_write_queue_from(skb, sk) {
-               if (skb == tcp_send_head(sk))
-                       break;
-               tp->forward_skb_hint = skb;
+                       fwd_rexmitting = 1;
+                       goto begin_fwd;
 
-               if (!before(TCP_SKB_CB(skb)->seq, tcp_highest_sack_seq(tp)))
-                       break;
+               } else if (!(sacked & TCPCB_LOST)) {
+                       if (hole == NULL && !(sacked & TCPCB_SACKED_RETRANS))
+                               hole = skb;
+                       continue;
 
-               if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
-                       break;
+               } else {
+                       last_lost = TCP_SKB_CB(skb)->end_seq;
+                       if (icsk->icsk_ca_state != TCP_CA_Loss)
+                               mib_idx = LINUX_MIB_TCPFASTRETRANS;
+                       else
+                               mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS;
+               }
 
-               if (TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS)
+               if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))
                        continue;
 
-               /* Ok, retransmit it. */
-               if (tcp_retransmit_skb(sk, skb)) {
-                       tp->forward_skb_hint = NULL;
-                       break;
-               }
+               if (tcp_retransmit_skb(sk, skb))
+                       return;
+               NET_INC_STATS_BH(sock_net(sk), mib_idx);
 
                if (skb == tcp_write_queue_head(sk))
                        inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
                                                  inet_csk(sk)->icsk_rto,
                                                  TCP_RTO_MAX);
-
-               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFORWARDRETRANS);
        }
 }
 
@@ -2240,6 +2202,7 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
        struct sk_buff *skb;
        struct tcp_md5sig_key *md5;
        __u8 *md5_hash_location;
+       int mss;
 
        skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
        if (skb == NULL)
@@ -2250,13 +2213,17 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
 
        skb->dst = dst_clone(dst);
 
+       mss = dst_metric(dst, RTAX_ADVMSS);
+       if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss)
+               mss = tp->rx_opt.user_mss;
+
        if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
                __u8 rcv_wscale;
                /* Set this up on the first call only */
                req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
                /* tcp_full_space because it is guaranteed to be the first packet */
                tcp_select_initial_window(tcp_full_space(sk),
-                       dst_metric(dst, RTAX_ADVMSS) - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
+                       mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
                        &req->rcv_wnd,
                        &req->window_clamp,
                        ireq->wscale_ok,
@@ -2265,9 +2232,13 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
        }
 
        memset(&opts, 0, sizeof(opts));
+#ifdef CONFIG_SYN_COOKIES
+       if (unlikely(req->cookie_ts))
+               TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
+       else
+#endif
        TCP_SKB_CB(skb)->when = tcp_time_stamp;
-       tcp_header_size = tcp_synack_options(sk, req,
-                                            dst_metric(dst, RTAX_ADVMSS),
+       tcp_header_size = tcp_synack_options(sk, req, mss,
                                             skb, &opts, &md5) +
                          sizeof(struct tcphdr);
 
@@ -2279,7 +2250,7 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
        th->syn = 1;
        th->ack = 1;
        TCP_ECN_make_synack(req, th);
-       th->source = inet_sk(sk)->sport;
+       th->source = ireq->loc_port;
        th->dest = ireq->rmt_port;
        /* Setting of flags are superfluous here for callers (and ECE is
         * not even correctly set)
@@ -2291,11 +2262,6 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
 
        /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
        th->window = htons(min(req->rcv_wnd, 65535U));
-#ifdef CONFIG_SYN_COOKIES
-       if (unlikely(req->cookie_ts))
-               TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
-       else
-#endif
        tcp_options_write((__be32 *)(th + 1), tp, &opts, &md5_hash_location);
        th->doff = (tcp_header_size >> 2);
        TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS);
@@ -2341,6 +2307,9 @@ static void tcp_connect_init(struct sock *sk)
        if (!tp->window_clamp)
                tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
        tp->advmss = dst_metric(dst, RTAX_ADVMSS);
+       if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->advmss)
+               tp->advmss = tp->rx_opt.user_mss;
+
        tcp_initialize_rcv_mss(sk);
 
        tcp_select_initial_window(tcp_full_space(sk),
@@ -2359,6 +2328,7 @@ static void tcp_connect_init(struct sock *sk)
        tcp_init_wl(tp, tp->write_seq, 0);
        tp->snd_una = tp->write_seq;
        tp->snd_sml = tp->write_seq;
+       tp->snd_up = tp->write_seq;
        tp->rcv_nxt = 0;
        tp->rcv_wup = 0;
        tp->copied_seq = 0;
@@ -2568,8 +2538,7 @@ int tcp_write_wakeup(struct sock *sk)
                        tcp_event_new_data_sent(sk, skb);
                return err;
        } else {
-               if (tp->urg_mode &&
-                   between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF))
+               if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF))
                        tcp_xmit_probe_skb(sk, 1);
                return tcp_xmit_probe_skb(sk, 0);
        }