]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - net/ipv4/tcp_output.c
tcp: Restore ordering of TCP options for the sake of inter-operability
[linux-2.6-omap-h63xx.git] / net / ipv4 / tcp_output.c
index 8f9793a37b618540d27ec965b5489a3d1ff7a171..e4c5ac9fe89bf6c693690f22e5e0e4160443acd4 100644 (file)
@@ -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;
                }
        }
 }
@@ -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;
@@ -1012,7 +1029,7 @@ 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
+ * LARGESEND note: !tcp_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.
  */
@@ -1029,7 +1046,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) && !tcp_urg_mode(tp))
                doing_tso = 1;
 
        if (dst) {
@@ -1193,7 +1210,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;
 
@@ -1932,8 +1949,8 @@ int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
        /* 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)) &&
+           (tcp_write_queue_next(sk, skb) != tcp_send_head(sk)) &&
            (skb_shinfo(skb)->nr_frags == 0 &&
             skb_shinfo(tcp_write_queue_next(sk, skb))->nr_frags == 0) &&
            (tcp_skb_pcount(skb) == 1 &&
@@ -2232,6 +2249,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)
@@ -2242,13 +2260,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,
@@ -2258,8 +2280,7 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
 
        memset(&opts, 0, sizeof(opts));
        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);
 
@@ -2271,7 +2292,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)
@@ -2333,6 +2354,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),
@@ -2351,6 +2375,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;
@@ -2560,8 +2585,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);
        }