linux-bk/net/ipv4/tcp_input.c
<<
>>
Prefs
   1/*
   2 * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3 *              operating system.  INET is implemented using the  BSD Socket
   4 *              interface as the means of communication with the user level.
   5 *
   6 *              Implementation of the Transmission Control Protocol(TCP).
   7 *
   8 * Version:     $Id: tcp_input.c,v 1.243 2002/02/01 22:01:04 davem Exp $
   9 *
  10 * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12 *              Mark Evans, <evansmp@uhura.aston.ac.uk>
  13 *              Corey Minyard <wf-rch!minyard@relay.EU.net>
  14 *              Florian La Roche, <flla@stud.uni-sb.de>
  15 *              Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  16 *              Linus Torvalds, <torvalds@cs.helsinki.fi>
  17 *              Alan Cox, <gw4pts@gw4pts.ampr.org>
  18 *              Matthew Dillon, <dillon@apollo.west.oic.com>
  19 *              Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  20 *              Jorge Cwik, <jorge@laser.satlink.net>
  21 */
  22
  23/*
  24 * Changes:
  25 *              Pedro Roque     :       Fast Retransmit/Recovery.
  26 *                                      Two receive queues.
  27 *                                      Retransmit queue handled by TCP.
  28 *                                      Better retransmit timer handling.
  29 *                                      New congestion avoidance.
  30 *                                      Header prediction.
  31 *                                      Variable renaming.
  32 *
  33 *              Eric            :       Fast Retransmit.
  34 *              Randy Scott     :       MSS option defines.
  35 *              Eric Schenk     :       Fixes to slow start algorithm.
  36 *              Eric Schenk     :       Yet another double ACK bug.
  37 *              Eric Schenk     :       Delayed ACK bug fixes.
  38 *              Eric Schenk     :       Floyd style fast retrans war avoidance.
  39 *              David S. Miller :       Don't allow zero congestion window.
  40 *              Eric Schenk     :       Fix retransmitter so that it sends
  41 *                                      next packet on ack of previous packet.
  42 *              Andi Kleen      :       Moved open_request checking here
  43 *                                      and process RSTs for open_requests.
  44 *              Andi Kleen      :       Better prune_queue, and other fixes.
  45 *              Andrey Savochkin:       Fix RTT measurements in the presnce of
  46 *                                      timestamps.
  47 *              Andrey Savochkin:       Check sequence numbers correctly when
  48 *                                      removing SACKs due to in sequence incoming
  49 *                                      data segments.
  50 *              Andi Kleen:             Make sure we never ack data there is not
  51 *                                      enough room for. Also make this condition
  52 *                                      a fatal error if it might still happen.
  53 *              Andi Kleen:             Add tcp_measure_rcv_mss to make 
  54 *                                      connections with MSS<min(MTU,ann. MSS)
  55 *                                      work without delayed acks. 
  56 *              Andi Kleen:             Process packets with PSH set in the
  57 *                                      fast path.
  58 *              J Hadi Salim:           ECN support
  59 *              Andrei Gurtov,
  60 *              Pasi Sarolahti,
  61 *              Panu Kuhlberg:          Experimental audit of TCP (re)transmission
  62 *                                      engine. Lots of bugs are found.
  63 *              Pasi Sarolahti:         F-RTO for dealing with spurious RTOs
  64 */
  65
  66#include <linux/config.h>
  67#include <linux/mm.h>
  68#include <linux/module.h>
  69#include <linux/sysctl.h>
  70#include <net/tcp.h>
  71#include <net/inet_common.h>
  72#include <linux/ipsec.h>
  73
  74int sysctl_tcp_timestamps = 1;
  75int sysctl_tcp_window_scaling = 1;
  76int sysctl_tcp_sack = 1;
  77int sysctl_tcp_fack = 1;
  78int sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH;
  79#ifdef CONFIG_INET_ECN
  80int sysctl_tcp_ecn = 1;
  81#else
  82int sysctl_tcp_ecn;
  83#endif
  84int sysctl_tcp_dsack = 1;
  85int sysctl_tcp_app_win = 31;
  86int sysctl_tcp_adv_win_scale = 2;
  87
  88int sysctl_tcp_stdurg;
  89int sysctl_tcp_rfc1337;
  90int sysctl_tcp_max_orphans = NR_FILE;
  91int sysctl_tcp_frto;
  92
  93#define FLAG_DATA               0x01 /* Incoming frame contained data.          */
  94#define FLAG_WIN_UPDATE         0x02 /* Incoming ACK was a window update.       */
  95#define FLAG_DATA_ACKED         0x04 /* This ACK acknowledged new data.         */
  96#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted.  */
  97#define FLAG_SYN_ACKED          0x10 /* This ACK acknowledged SYN.              */
  98#define FLAG_DATA_SACKED        0x20 /* New SACK.                               */
  99#define FLAG_ECE                0x40 /* ECE in this ACK                         */
 100#define FLAG_DATA_LOST          0x80 /* SACK detected data lossage.             */
 101#define FLAG_SLOWPATH           0x100 /* Do not skip RFC checks for window update.*/
 102
 103#define FLAG_ACKED              (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
 104#define FLAG_NOT_DUP            (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
 105#define FLAG_CA_ALERT           (FLAG_DATA_SACKED|FLAG_ECE)
 106#define FLAG_FORWARD_PROGRESS   (FLAG_ACKED|FLAG_DATA_SACKED)
 107
 108#define IsReno(tp) ((tp)->sack_ok == 0)
 109#define IsFack(tp) ((tp)->sack_ok & 2)
 110#define IsDSack(tp) ((tp)->sack_ok & 4)
 111
 112#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
 113
 114/* Adapt the MSS value used to make delayed ack decision to the 
 115 * real world.
 116 */ 
 117static __inline__ void tcp_measure_rcv_mss(struct tcp_opt *tp, struct sk_buff *skb)
 118{
 119        unsigned int len, lss;
 120
 121        lss = tp->ack.last_seg_size; 
 122        tp->ack.last_seg_size = 0; 
 123
 124        /* skb->len may jitter because of SACKs, even if peer
 125         * sends good full-sized frames.
 126         */
 127        len = skb->len;
 128        if (len >= tp->ack.rcv_mss) {
 129                tp->ack.rcv_mss = len;
 130        } else {
 131                /* Otherwise, we make more careful check taking into account,
 132                 * that SACKs block is variable.
 133                 *
 134                 * "len" is invariant segment length, including TCP header.
 135                 */
 136                len += skb->data - skb->h.raw;
 137                if (len >= TCP_MIN_RCVMSS + sizeof(struct tcphdr) ||
 138                    /* If PSH is not set, packet should be
 139                     * full sized, provided peer TCP is not badly broken.
 140                     * This observation (if it is correct 8)) allows
 141                     * to handle super-low mtu links fairly.
 142                     */
 143                    (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
 144                     !(tcp_flag_word(skb->h.th)&TCP_REMNANT))) {
 145                        /* Subtract also invariant (if peer is RFC compliant),
 146                         * tcp header plus fixed timestamp option length.
 147                         * Resulting "len" is MSS free of SACK jitter.
 148                         */
 149                        len -= tp->tcp_header_len;
 150                        tp->ack.last_seg_size = len;
 151                        if (len == lss) {
 152                                tp->ack.rcv_mss = len;
 153                                return;
 154                        }
 155                }
 156                tp->ack.pending |= TCP_ACK_PUSHED;
 157        }
 158}
 159
 160static void tcp_incr_quickack(struct tcp_opt *tp)
 161{
 162        unsigned quickacks = tp->rcv_wnd/(2*tp->ack.rcv_mss);
 163
 164        if (quickacks==0)
 165                quickacks=2;
 166        if (quickacks > tp->ack.quick)
 167                tp->ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
 168}
 169
 170void tcp_enter_quickack_mode(struct tcp_opt *tp)
 171{
 172        tcp_incr_quickack(tp);
 173        tp->ack.pingpong = 0;
 174        tp->ack.ato = TCP_ATO_MIN;
 175}
 176
 177/* Send ACKs quickly, if "quick" count is not exhausted
 178 * and the session is not interactive.
 179 */
 180
 181static __inline__ int tcp_in_quickack_mode(struct tcp_opt *tp)
 182{
 183        return (tp->ack.quick && !tp->ack.pingpong);
 184}
 185
 186/* Buffer size and advertised window tuning.
 187 *
 188 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
 189 */
 190
 191static void tcp_fixup_sndbuf(struct sock *sk)
 192{
 193        int sndmem = tcp_sk(sk)->mss_clamp + MAX_TCP_HEADER + 16 +
 194                     sizeof(struct sk_buff);
 195
 196        if (sk->sk_sndbuf < 3 * sndmem)
 197                sk->sk_sndbuf = min(3 * sndmem, sysctl_tcp_wmem[2]);
 198}
 199
 200/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
 201 *
 202 * All tcp_full_space() is split to two parts: "network" buffer, allocated
 203 * forward and advertised in receiver window (tp->rcv_wnd) and
 204 * "application buffer", required to isolate scheduling/application
 205 * latencies from network.
 206 * window_clamp is maximal advertised window. It can be less than
 207 * tcp_full_space(), in this case tcp_full_space() - window_clamp
 208 * is reserved for "application" buffer. The less window_clamp is
 209 * the smoother our behaviour from viewpoint of network, but the lower
 210 * throughput and the higher sensitivity of the connection to losses. 8)
 211 *
 212 * rcv_ssthresh is more strict window_clamp used at "slow start"
 213 * phase to predict further behaviour of this connection.
 214 * It is used for two goals:
 215 * - to enforce header prediction at sender, even when application
 216 *   requires some significant "application buffer". It is check #1.
 217 * - to prevent pruning of receive queue because of misprediction
 218 *   of receiver window. Check #2.
 219 *
 220 * The scheme does not work when sender sends good segments opening
 221 * window and then starts to feed us spagetti. But it should work
 222 * in common situations. Otherwise, we have to rely on queue collapsing.
 223 */
 224
 225/* Slow part of check#2. */
 226static int
 227__tcp_grow_window(struct sock *sk, struct tcp_opt *tp, struct sk_buff *skb)
 228{
 229        /* Optimize this! */
 230        int truesize = tcp_win_from_space(skb->truesize)/2;
 231        int window = tcp_full_space(sk)/2;
 232
 233        while (tp->rcv_ssthresh <= window) {
 234                if (truesize <= skb->len)
 235                        return 2*tp->ack.rcv_mss;
 236
 237                truesize >>= 1;
 238                window >>= 1;
 239        }
 240        return 0;
 241}
 242
 243static __inline__ void
 244tcp_grow_window(struct sock *sk, struct tcp_opt *tp, struct sk_buff *skb)
 245{
 246        /* Check #1 */
 247        if (tp->rcv_ssthresh < tp->window_clamp &&
 248            (int)tp->rcv_ssthresh < tcp_space(sk) &&
 249            !tcp_memory_pressure) {
 250                int incr;
 251
 252                /* Check #2. Increase window, if skb with such overhead
 253                 * will fit to rcvbuf in future.
 254                 */
 255                if (tcp_win_from_space(skb->truesize) <= skb->len)
 256                        incr = 2*tp->advmss;
 257                else
 258                        incr = __tcp_grow_window(sk, tp, skb);
 259
 260                if (incr) {
 261                        tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr, tp->window_clamp);
 262                        tp->ack.quick |= 1;
 263                }
 264        }
 265}
 266
 267/* 3. Tuning rcvbuf, when connection enters established state. */
 268
 269static void tcp_fixup_rcvbuf(struct sock *sk)
 270{
 271        struct tcp_opt *tp = tcp_sk(sk);
 272        int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff);
 273
 274        /* Try to select rcvbuf so that 4 mss-sized segments
 275         * will fit to window and correspoding skbs will fit to our rcvbuf.
 276         * (was 3; 4 is minimum to allow fast retransmit to work.)
 277         */
 278        while (tcp_win_from_space(rcvmem) < tp->advmss)
 279                rcvmem += 128;
 280        if (sk->sk_rcvbuf < 4 * rcvmem)
 281                sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]);
 282}
 283
 284/* 4. Try to fixup all. It is made iimediately after connection enters
 285 *    established state.
 286 */
 287static void tcp_init_buffer_space(struct sock *sk)
 288{
 289        struct tcp_opt *tp = tcp_sk(sk);
 290        int maxwin;
 291
 292        if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
 293                tcp_fixup_rcvbuf(sk);
 294        if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
 295                tcp_fixup_sndbuf(sk);
 296
 297        maxwin = tcp_full_space(sk);
 298
 299        if (tp->window_clamp >= maxwin) {
 300                tp->window_clamp = maxwin;
 301
 302                if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
 303                        tp->window_clamp = max(maxwin -
 304                                               (maxwin >> sysctl_tcp_app_win),
 305                                               4 * tp->advmss);
 306        }
 307
 308        /* Force reservation of one segment. */
 309        if (sysctl_tcp_app_win &&
 310            tp->window_clamp > 2 * tp->advmss &&
 311            tp->window_clamp + tp->advmss > maxwin)
 312                tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
 313
 314        tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
 315        tp->snd_cwnd_stamp = tcp_time_stamp;
 316}
 317
 318/* 5. Recalculate window clamp after socket hit its memory bounds. */
 319static void tcp_clamp_window(struct sock *sk, struct tcp_opt *tp)
 320{
 321        struct sk_buff *skb;
 322        unsigned int app_win = tp->rcv_nxt - tp->copied_seq;
 323        int ofo_win = 0;
 324
 325        tp->ack.quick = 0;
 326
 327        skb_queue_walk(&tp->out_of_order_queue, skb) {
 328                ofo_win += skb->len;
 329        }
 330
 331        /* If overcommit is due to out of order segments,
 332         * do not clamp window. Try to expand rcvbuf instead.
 333         */
 334        if (ofo_win) {
 335                if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
 336                    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
 337                    !tcp_memory_pressure &&
 338                    atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0])
 339                        sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
 340                                            sysctl_tcp_rmem[2]);
 341        }
 342        if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) {
 343                app_win += ofo_win;
 344                if (atomic_read(&sk->sk_rmem_alloc) >= 2 * sk->sk_rcvbuf)
 345                        app_win >>= 1;
 346                if (app_win > tp->ack.rcv_mss)
 347                        app_win -= tp->ack.rcv_mss;
 348                app_win = max(app_win, 2U*tp->advmss);
 349
 350                if (!ofo_win)
 351                        tp->window_clamp = min(tp->window_clamp, app_win);
 352                tp->rcv_ssthresh = min(tp->window_clamp, 2U*tp->advmss);
 353        }
 354}
 355
 356/* There is something which you must keep in mind when you analyze the
 357 * behavior of the tp->ato delayed ack timeout interval.  When a
 358 * connection starts up, we want to ack as quickly as possible.  The
 359 * problem is that "good" TCP's do slow start at the beginning of data
 360 * transmission.  The means that until we send the first few ACK's the
 361 * sender will sit on his end and only queue most of his data, because
 362 * he can only send snd_cwnd unacked packets at any given time.  For
 363 * each ACK we send, he increments snd_cwnd and transmits more of his
 364 * queue.  -DaveM
 365 */
 366static void tcp_event_data_recv(struct sock *sk, struct tcp_opt *tp, struct sk_buff *skb)
 367{
 368        u32 now;
 369
 370        tcp_schedule_ack(tp);
 371
 372        tcp_measure_rcv_mss(tp, skb);
 373
 374        now = tcp_time_stamp;
 375
 376        if (!tp->ack.ato) {
 377                /* The _first_ data packet received, initialize
 378                 * delayed ACK engine.
 379                 */
 380                tcp_incr_quickack(tp);
 381                tp->ack.ato = TCP_ATO_MIN;
 382        } else {
 383                int m = now - tp->ack.lrcvtime;
 384
 385                if (m <= TCP_ATO_MIN/2) {
 386                        /* The fastest case is the first. */
 387                        tp->ack.ato = (tp->ack.ato>>1) + TCP_ATO_MIN/2;
 388                } else if (m < tp->ack.ato) {
 389                        tp->ack.ato = (tp->ack.ato>>1) + m;
 390                        if (tp->ack.ato > tp->rto)
 391                                tp->ack.ato = tp->rto;
 392                } else if (m > tp->rto) {
 393                        /* Too long gap. Apparently sender falled to
 394                         * restart window, so that we send ACKs quickly.
 395                         */
 396                        tcp_incr_quickack(tp);
 397                        tcp_mem_reclaim(sk);
 398                }
 399        }
 400        tp->ack.lrcvtime = now;
 401
 402        TCP_ECN_check_ce(tp, skb);
 403
 404        if (skb->len >= 128)
 405                tcp_grow_window(sk, tp, skb);
 406}
 407
 408/* Called to compute a smoothed rtt estimate. The data fed to this
 409 * routine either comes from timestamps, or from segments that were
 410 * known _not_ to have been retransmitted [see Karn/Partridge
 411 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
 412 * piece by Van Jacobson.
 413 * NOTE: the next three routines used to be one big routine.
 414 * To save cycles in the RFC 1323 implementation it was better to break
 415 * it up into three procedures. -- erics
 416 */
 417static void tcp_rtt_estimator(struct tcp_opt *tp, __u32 mrtt)
 418{
 419        long m = mrtt; /* RTT */
 420
 421        /*      The following amusing code comes from Jacobson's
 422         *      article in SIGCOMM '88.  Note that rtt and mdev
 423         *      are scaled versions of rtt and mean deviation.
 424         *      This is designed to be as fast as possible 
 425         *      m stands for "measurement".
 426         *
 427         *      On a 1990 paper the rto value is changed to:
 428         *      RTO = rtt + 4 * mdev
 429         *
 430         * Funny. This algorithm seems to be very broken.
 431         * These formulae increase RTO, when it should be decreased, increase
 432         * too slowly, when it should be incresed fastly, decrease too fastly
 433         * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
 434         * does not matter how to _calculate_ it. Seems, it was trap
 435         * that VJ failed to avoid. 8)
 436         */
 437        if(m == 0)
 438                m = 1;
 439        if (tp->srtt != 0) {
 440                m -= (tp->srtt >> 3);   /* m is now error in rtt est */
 441                tp->srtt += m;          /* rtt = 7/8 rtt + 1/8 new */
 442                if (m < 0) {
 443                        m = -m;         /* m is now abs(error) */
 444                        m -= (tp->mdev >> 2);   /* similar update on mdev */
 445                        /* This is similar to one of Eifel findings.
 446                         * Eifel blocks mdev updates when rtt decreases.
 447                         * This solution is a bit different: we use finer gain
 448                         * for mdev in this case (alpha*beta).
 449                         * Like Eifel it also prevents growth of rto,
 450                         * but also it limits too fast rto decreases,
 451                         * happening in pure Eifel.
 452                         */
 453                        if (m > 0)
 454                                m >>= 3;
 455                } else {
 456                        m -= (tp->mdev >> 2);   /* similar update on mdev */
 457                }
 458                tp->mdev += m;          /* mdev = 3/4 mdev + 1/4 new */
 459                if (tp->mdev > tp->mdev_max) {
 460                        tp->mdev_max = tp->mdev;
 461                        if (tp->mdev_max > tp->rttvar)
 462                                tp->rttvar = tp->mdev_max;
 463                }
 464                if (after(tp->snd_una, tp->rtt_seq)) {
 465                        if (tp->mdev_max < tp->rttvar)
 466                                tp->rttvar -= (tp->rttvar-tp->mdev_max)>>2;
 467                        tp->rtt_seq = tp->snd_nxt;
 468                        tp->mdev_max = TCP_RTO_MIN;
 469                }
 470        } else {
 471                /* no previous measure. */
 472                tp->srtt = m<<3;        /* take the measured time to be rtt */
 473                tp->mdev = m<<1;        /* make sure rto = 3*rtt */
 474                tp->mdev_max = tp->rttvar = max(tp->mdev, TCP_RTO_MIN);
 475                tp->rtt_seq = tp->snd_nxt;
 476        }
 477}
 478
 479/* Calculate rto without backoff.  This is the second half of Van Jacobson's
 480 * routine referred to above.
 481 */
 482static __inline__ void tcp_set_rto(struct tcp_opt *tp)
 483{
 484        /* Old crap is replaced with new one. 8)
 485         *
 486         * More seriously:
 487         * 1. If rtt variance happened to be less 50msec, it is hallucination.
 488         *    It cannot be less due to utterly erratic ACK generation made
 489         *    at least by solaris and freebsd. "Erratic ACKs" has _nothing_
 490         *    to do with delayed acks, because at cwnd>2 true delack timeout
 491         *    is invisible. Actually, Linux-2.4 also generates erratic
 492         *    ACKs in some curcumstances.
 493         */
 494        tp->rto = (tp->srtt >> 3) + tp->rttvar;
 495
 496        /* 2. Fixups made earlier cannot be right.
 497         *    If we do not estimate RTO correctly without them,
 498         *    all the algo is pure shit and should be replaced
 499         *    with correct one. It is exaclty, which we pretend to do.
 500         */
 501}
 502
 503/* NOTE: clamping at TCP_RTO_MIN is not required, current algo
 504 * guarantees that rto is higher.
 505 */
 506static __inline__ void tcp_bound_rto(struct tcp_opt *tp)
 507{
 508        if (tp->rto > TCP_RTO_MAX)
 509                tp->rto = TCP_RTO_MAX;
 510}
 511
 512/* Save metrics learned by this TCP session.
 513   This function is called only, when TCP finishes successfully
 514   i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE.
 515 */
 516void tcp_update_metrics(struct sock *sk)
 517{
 518        struct tcp_opt *tp = tcp_sk(sk);
 519        struct dst_entry *dst = __sk_dst_get(sk);
 520
 521        dst_confirm(dst);
 522
 523        if (dst && (dst->flags&DST_HOST)) {
 524                int m;
 525
 526                if (tp->backoff || !tp->srtt) {
 527                        /* This session failed to estimate rtt. Why?
 528                         * Probably, no packets returned in time.
 529                         * Reset our results.
 530                         */
 531                        if (!(dst_metric_locked(dst, RTAX_RTT)))
 532                                dst->metrics[RTAX_RTT-1] = 0;
 533                        return;
 534                }
 535
 536                m = dst_metric(dst, RTAX_RTT) - tp->srtt;
 537
 538                /* If newly calculated rtt larger than stored one,
 539                 * store new one. Otherwise, use EWMA. Remember,
 540                 * rtt overestimation is always better than underestimation.
 541                 */
 542                if (!(dst_metric_locked(dst, RTAX_RTT))) {
 543                        if (m <= 0)
 544                                dst->metrics[RTAX_RTT-1] = tp->srtt;
 545                        else
 546                                dst->metrics[RTAX_RTT-1] -= (m>>3);
 547                }
 548
 549                if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
 550                        if (m < 0)
 551                                m = -m;
 552
 553                        /* Scale deviation to rttvar fixed point */
 554                        m >>= 1;
 555                        if (m < tp->mdev)
 556                                m = tp->mdev;
 557
 558                        if (m >= dst_metric(dst, RTAX_RTTVAR))
 559                                dst->metrics[RTAX_RTTVAR-1] = m;
 560                        else
 561                                dst->metrics[RTAX_RTTVAR-1] -=
 562                                        (dst->metrics[RTAX_RTTVAR-1] - m)>>2;
 563                }
 564
 565                if (tp->snd_ssthresh >= 0xFFFF) {
 566                        /* Slow start still did not finish. */
 567                        if (dst_metric(dst, RTAX_SSTHRESH) &&
 568                            !dst_metric_locked(dst, RTAX_SSTHRESH) &&
 569                            (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
 570                                dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1;
 571                        if (!dst_metric_locked(dst, RTAX_CWND) &&
 572                            tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
 573                                dst->metrics[RTAX_CWND-1] = tp->snd_cwnd;
 574                } else if (tp->snd_cwnd > tp->snd_ssthresh &&
 575                           tp->ca_state == TCP_CA_Open) {
 576                        /* Cong. avoidance phase, cwnd is reliable. */
 577                        if (!dst_metric_locked(dst, RTAX_SSTHRESH))
 578                                dst->metrics[RTAX_SSTHRESH-1] =
 579                                        max(tp->snd_cwnd >> 1, tp->snd_ssthresh);
 580                        if (!dst_metric_locked(dst, RTAX_CWND))
 581                                dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_cwnd) >> 1;
 582                } else {
 583                        /* Else slow start did not finish, cwnd is non-sense,
 584                           ssthresh may be also invalid.
 585                         */
 586                        if (!dst_metric_locked(dst, RTAX_CWND))
 587                                dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_ssthresh) >> 1;
 588                        if (dst->metrics[RTAX_SSTHRESH-1] &&
 589                            !dst_metric_locked(dst, RTAX_SSTHRESH) &&
 590                            tp->snd_ssthresh > dst->metrics[RTAX_SSTHRESH-1])
 591                                dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh;
 592                }
 593
 594                if (!dst_metric_locked(dst, RTAX_REORDERING)) {
 595                        if (dst->metrics[RTAX_REORDERING-1] < tp->reordering &&
 596                            tp->reordering != sysctl_tcp_reordering)
 597                                dst->metrics[RTAX_REORDERING-1] = tp->reordering;
 598                }
 599        }
 600}
 601
 602/* Numbers are taken from RFC2414.  */
 603__u32 tcp_init_cwnd(struct tcp_opt *tp, struct dst_entry *dst)
 604{
 605        __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
 606
 607        if (!cwnd) {
 608                if (tp->mss_cache > 1460)
 609                        cwnd = 2;
 610                else
 611                        cwnd = (tp->mss_cache > 1095) ? 3 : 4;
 612        }
 613        return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
 614}
 615
 616/* Initialize metrics on socket. */
 617
 618static void tcp_init_metrics(struct sock *sk)
 619{
 620        struct tcp_opt *tp = tcp_sk(sk);
 621        struct dst_entry *dst = __sk_dst_get(sk);
 622
 623        if (dst == NULL)
 624                goto reset;
 625
 626        dst_confirm(dst);
 627
 628        if (dst_metric_locked(dst, RTAX_CWND))
 629                tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
 630        if (dst_metric(dst, RTAX_SSTHRESH)) {
 631                tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
 632                if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
 633                        tp->snd_ssthresh = tp->snd_cwnd_clamp;
 634        }
 635        if (dst_metric(dst, RTAX_REORDERING) &&
 636            tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
 637                tp->sack_ok &= ~2;
 638                tp->reordering = dst_metric(dst, RTAX_REORDERING);
 639        }
 640
 641        if (dst_metric(dst, RTAX_RTT) == 0)
 642                goto reset;
 643
 644        if (!tp->srtt && dst_metric(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3))
 645                goto reset;
 646
 647        /* Initial rtt is determined from SYN,SYN-ACK.
 648         * The segment is small and rtt may appear much
 649         * less than real one. Use per-dst memory
 650         * to make it more realistic.
 651         *
 652         * A bit of theory. RTT is time passed after "normal" sized packet
 653         * is sent until it is ACKed. In normal curcumstances sending small
 654         * packets force peer to delay ACKs and calculation is correct too.
 655         * The algorithm is adaptive and, provided we follow specs, it
 656         * NEVER underestimate RTT. BUT! If peer tries to make some clever
 657         * tricks sort of "quick acks" for time long enough to decrease RTT
 658         * to low value, and then abruptly stops to do it and starts to delay
 659         * ACKs, wait for troubles.
 660         */
 661        if (dst_metric(dst, RTAX_RTT) > tp->srtt)
 662                tp->srtt = dst_metric(dst, RTAX_RTT);
 663        if (dst_metric(dst, RTAX_RTTVAR) > tp->mdev) {
 664                tp->mdev = dst_metric(dst, RTAX_RTTVAR);
 665                tp->mdev_max = tp->rttvar = max(tp->mdev, TCP_RTO_MIN);
 666        }
 667        tcp_set_rto(tp);
 668        tcp_bound_rto(tp);
 669        if (tp->rto < TCP_TIMEOUT_INIT && !tp->saw_tstamp)
 670                goto reset;
 671        tp->snd_cwnd = tcp_init_cwnd(tp, dst);
 672        tp->snd_cwnd_stamp = tcp_time_stamp;
 673        return;
 674
 675reset:
 676        /* Play conservative. If timestamps are not
 677         * supported, TCP will fail to recalculate correct
 678         * rtt, if initial rto is too small. FORGET ALL AND RESET!
 679         */
 680        if (!tp->saw_tstamp && tp->srtt) {
 681                tp->srtt = 0;
 682                tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT;
 683                tp->rto = TCP_TIMEOUT_INIT;
 684        }
 685}
 686
 687static void tcp_update_reordering(struct tcp_opt *tp, int metric, int ts)
 688{
 689        if (metric > tp->reordering) {
 690                tp->reordering = min(TCP_MAX_REORDERING, metric);
 691
 692                /* This exciting event is worth to be remembered. 8) */
 693                if (ts)
 694                        NET_INC_STATS_BH(TCPTSReorder);
 695                else if (IsReno(tp))
 696                        NET_INC_STATS_BH(TCPRenoReorder);
 697                else if (IsFack(tp))
 698                        NET_INC_STATS_BH(TCPFACKReorder);
 699                else
 700                        NET_INC_STATS_BH(TCPSACKReorder);
 701#if FASTRETRANS_DEBUG > 1
 702                printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n",
 703                       tp->sack_ok, tp->ca_state,
 704                       tp->reordering, tp->fackets_out, tp->sacked_out,
 705                       tp->undo_marker ? tp->undo_retrans : 0);
 706#endif
 707                /* Disable FACK yet. */
 708                tp->sack_ok &= ~2;
 709        }
 710}
 711
 712/* This procedure tags the retransmission queue when SACKs arrive.
 713 *
 714 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
 715 * Packets in queue with these bits set are counted in variables
 716 * sacked_out, retrans_out and lost_out, correspondingly.
 717 *
 718 * Valid combinations are:
 719 * Tag  InFlight        Description
 720 * 0    1               - orig segment is in flight.
 721 * S    0               - nothing flies, orig reached receiver.
 722 * L    0               - nothing flies, orig lost by net.
 723 * R    2               - both orig and retransmit are in flight.
 724 * L|R  1               - orig is lost, retransmit is in flight.
 725 * S|R  1               - orig reached receiver, retrans is still in flight.
 726 * (L|S|R is logically valid, it could occur when L|R is sacked,
 727 *  but it is equivalent to plain S and code short-curcuits it to S.
 728 *  L|S is logically invalid, it would mean -1 packet in flight 8))
 729 *
 730 * These 6 states form finite state machine, controlled by the following events:
 731 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
 732 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
 733 * 3. Loss detection event of one of three flavors:
 734 *      A. Scoreboard estimator decided the packet is lost.
 735 *         A'. Reno "three dupacks" marks head of queue lost.
 736 *         A''. Its FACK modfication, head until snd.fack is lost.
 737 *      B. SACK arrives sacking data transmitted after never retransmitted
 738 *         hole was sent out.
 739 *      C. SACK arrives sacking SND.NXT at the moment, when the
 740 *         segment was retransmitted.
 741 * 4. D-SACK added new rule: D-SACK changes any tag to S.
 742 *
 743 * It is pleasant to note, that state diagram turns out to be commutative,
 744 * so that we are allowed not to be bothered by order of our actions,
 745 * when multiple events arrive simultaneously. (see the function below).
 746 *
 747 * Reordering detection.
 748 * --------------------
 749 * Reordering metric is maximal distance, which a packet can be displaced
 750 * in packet stream. With SACKs we can estimate it:
 751 *
 752 * 1. SACK fills old hole and the corresponding segment was not
 753 *    ever retransmitted -> reordering. Alas, we cannot use it
 754 *    when segment was retransmitted.
 755 * 2. The last flaw is solved with D-SACK. D-SACK arrives
 756 *    for retransmitted and already SACKed segment -> reordering..
 757 * Both of these heuristics are not used in Loss state, when we cannot
 758 * account for retransmits accurately.
 759 */
 760static int
 761tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb, u32 prior_snd_una)
 762{
 763        struct tcp_opt *tp = tcp_sk(sk);
 764        unsigned char *ptr = ack_skb->h.raw + TCP_SKB_CB(ack_skb)->sacked;
 765        struct tcp_sack_block *sp = (struct tcp_sack_block *)(ptr+2);
 766        int num_sacks = (ptr[1] - TCPOLEN_SACK_BASE)>>3;
 767        int reord = tp->packets_out;
 768        int prior_fackets;
 769        u32 lost_retrans = 0;
 770        int flag = 0;
 771        int i;
 772
 773        /* So, SACKs for already sent large segments will be lost.
 774         * Not good, but alternative is to resegment the queue. */
 775        if (sk->sk_route_caps & NETIF_F_TSO) {
 776                sk->sk_route_caps &= ~NETIF_F_TSO;
 777                sk->sk_no_largesend = 1;
 778                tp->mss_cache = tp->mss_cache_std;
 779        }
 780
 781        if (!tp->sacked_out)
 782                tp->fackets_out = 0;
 783        prior_fackets = tp->fackets_out;
 784
 785        for (i=0; i<num_sacks; i++, sp++) {
 786                struct sk_buff *skb;
 787                __u32 start_seq = ntohl(sp->start_seq);
 788                __u32 end_seq = ntohl(sp->end_seq);
 789                int fack_count = 0;
 790                int dup_sack = 0;
 791
 792                /* Check for D-SACK. */
 793                if (i == 0) {
 794                        u32 ack = TCP_SKB_CB(ack_skb)->ack_seq;
 795
 796                        if (before(start_seq, ack)) {
 797                                dup_sack = 1;
 798                                tp->sack_ok |= 4;
 799                                NET_INC_STATS_BH(TCPDSACKRecv);
 800                        } else if (num_sacks > 1 &&
 801                                   !after(end_seq, ntohl(sp[1].end_seq)) &&
 802                                   !before(start_seq, ntohl(sp[1].start_seq))) {
 803                                dup_sack = 1;
 804                                tp->sack_ok |= 4;
 805                                NET_INC_STATS_BH(TCPDSACKOfoRecv);
 806                        }
 807
 808                        /* D-SACK for already forgotten data...
 809                         * Do dumb counting. */
 810                        if (dup_sack &&
 811                            !after(end_seq, prior_snd_una) &&
 812                            after(end_seq, tp->undo_marker))
 813                                tp->undo_retrans--;
 814
 815                        /* Eliminate too old ACKs, but take into
 816                         * account more or less fresh ones, they can
 817                         * contain valid SACK info.
 818                         */
 819                        if (before(ack, prior_snd_una - tp->max_window))
 820                                return 0;
 821                }
 822
 823                /* Event "B" in the comment above. */
 824                if (after(end_seq, tp->high_seq))
 825                        flag |= FLAG_DATA_LOST;
 826
 827                for_retrans_queue(skb, sk, tp) {
 828                        u8 sacked = TCP_SKB_CB(skb)->sacked;
 829                        int in_sack;
 830
 831                        /* The retransmission queue is always in order, so
 832                         * we can short-circuit the walk early.
 833                         */
 834                        if(!before(TCP_SKB_CB(skb)->seq, end_seq))
 835                                break;
 836
 837                        fack_count++;
 838
 839                        in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
 840                                !before(end_seq, TCP_SKB_CB(skb)->end_seq);
 841
 842                        /* Account D-SACK for retransmitted packet. */
 843                        if ((dup_sack && in_sack) &&
 844                            (sacked & TCPCB_RETRANS) &&
 845                            after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker))
 846                                tp->undo_retrans--;
 847
 848                        /* The frame is ACKed. */
 849                        if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) {
 850                                if (sacked&TCPCB_RETRANS) {
 851                                        if ((dup_sack && in_sack) &&
 852                                            (sacked&TCPCB_SACKED_ACKED))
 853                                                reord = min(fack_count, reord);
 854                                } else {
 855                                        /* If it was in a hole, we detected reordering. */
 856                                        if (fack_count < prior_fackets &&
 857                                            !(sacked&TCPCB_SACKED_ACKED))
 858                                                reord = min(fack_count, reord);
 859                                }
 860
 861                                /* Nothing to do; acked frame is about to be dropped. */
 862                                continue;
 863                        }
 864
 865                        if ((sacked&TCPCB_SACKED_RETRANS) &&
 866                            after(end_seq, TCP_SKB_CB(skb)->ack_seq) &&
 867                            (!lost_retrans || after(end_seq, lost_retrans)))
 868                                lost_retrans = end_seq;
 869
 870                        if (!in_sack)
 871                                continue;
 872
 873                        if (!(sacked&TCPCB_SACKED_ACKED)) {
 874                                if (sacked & TCPCB_SACKED_RETRANS) {
 875                                        /* If the segment is not tagged as lost,
 876                                         * we do not clear RETRANS, believing
 877                                         * that retransmission is still in flight.
 878                                         */
 879                                        if (sacked & TCPCB_LOST) {
 880                                                TCP_SKB_CB(skb)->sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
 881                                                tp->lost_out--;
 882                                                tp->retrans_out--;
 883                                        }
 884                                } else {
 885                                        /* New sack for not retransmitted frame,
 886                                         * which was in hole. It is reordering.
 887                                         */
 888                                        if (!(sacked & TCPCB_RETRANS) &&
 889                                            fack_count < prior_fackets)
 890                                                reord = min(fack_count, reord);
 891
 892                                        if (sacked & TCPCB_LOST) {
 893                                                TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
 894                                                tp->lost_out--;
 895                                        }
 896                                }
 897
 898                                TCP_SKB_CB(skb)->sacked |= TCPCB_SACKED_ACKED;
 899                                flag |= FLAG_DATA_SACKED;
 900                                tp->sacked_out++;
 901
 902                                if (fack_count > tp->fackets_out)
 903                                        tp->fackets_out = fack_count;
 904                        } else {
 905                                if (dup_sack && (sacked&TCPCB_RETRANS))
 906                                        reord = min(fack_count, reord);
 907                        }
 908
 909                        /* D-SACK. We can detect redundant retransmission
 910                         * in S|R and plain R frames and clear it.
 911                         * undo_retrans is decreased above, L|R frames
 912                         * are accounted above as well.
 913                         */
 914                        if (dup_sack &&
 915                            (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS)) {
 916                                TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
 917                                tp->retrans_out--;
 918                        }
 919                }
 920        }
 921
 922        /* Check for lost retransmit. This superb idea is
 923         * borrowed from "ratehalving". Event "C".
 924         * Later note: FACK people cheated me again 8),
 925         * we have to account for reordering! Ugly,
 926         * but should help.
 927         */
 928        if (lost_retrans && tp->ca_state == TCP_CA_Recovery) {
 929                struct sk_buff *skb;
 930
 931                for_retrans_queue(skb, sk, tp) {
 932                        if (after(TCP_SKB_CB(skb)->seq, lost_retrans))
 933                                break;
 934                        if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
 935                                continue;
 936                        if ((TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) &&
 937                            after(lost_retrans, TCP_SKB_CB(skb)->ack_seq) &&
 938                            (IsFack(tp) ||
 939                             !before(lost_retrans,
 940                                     TCP_SKB_CB(skb)->ack_seq + tp->reordering *
 941                                     tp->mss_cache))) {
 942                                TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
 943                                tp->retrans_out--;
 944
 945                                if (!(TCP_SKB_CB(skb)->sacked&(TCPCB_LOST|TCPCB_SACKED_ACKED))) {
 946                                        tp->lost_out++;
 947                                        TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
 948                                        flag |= FLAG_DATA_SACKED;
 949                                        NET_INC_STATS_BH(TCPLostRetransmit);
 950                                }
 951                        }
 952                }
 953        }
 954
 955        tp->left_out = tp->sacked_out + tp->lost_out;
 956
 957        if (reord < tp->fackets_out && tp->ca_state != TCP_CA_Loss)
 958                tcp_update_reordering(tp, (tp->fackets_out + 1) - reord, 0);
 959
 960#if FASTRETRANS_DEBUG > 0
 961        BUG_TRAP((int)tp->sacked_out >= 0);
 962        BUG_TRAP((int)tp->lost_out >= 0);
 963        BUG_TRAP((int)tp->retrans_out >= 0);
 964        BUG_TRAP((int)tcp_packets_in_flight(tp) >= 0);
 965#endif
 966        return flag;
 967}
 968
 969/* RTO occurred, but do not yet enter loss state. Instead, transmit two new
 970 * segments to see from the next ACKs whether any data was really missing.
 971 * If the RTO was spurious, new ACKs should arrive.
 972 */
 973void tcp_enter_frto(struct sock *sk)
 974{
 975        struct tcp_opt *tp = tcp_sk(sk);
 976        struct sk_buff *skb;
 977
 978        tp->frto_counter = 1;
 979
 980        if (tp->ca_state <= TCP_CA_Disorder ||
 981            tp->snd_una == tp->high_seq ||
 982            (tp->ca_state == TCP_CA_Loss && !tp->retransmits)) {
 983                tp->prior_ssthresh = tcp_current_ssthresh(tp);
 984                tp->snd_ssthresh = tcp_recalc_ssthresh(tp);
 985        }
 986
 987        /* Have to clear retransmission markers here to keep the bookkeeping
 988         * in shape, even though we are not yet in Loss state.
 989         * If something was really lost, it is eventually caught up
 990         * in tcp_enter_frto_loss.
 991         */
 992        tp->retrans_out = 0;
 993        tp->undo_marker = tp->snd_una;
 994        tp->undo_retrans = 0;
 995
 996        for_retrans_queue(skb, sk, tp) {
 997                TCP_SKB_CB(skb)->sacked &= ~TCPCB_RETRANS;
 998        }
 999        tcp_sync_left_out(tp);
1000
1001        tp->ca_state = TCP_CA_Open;
1002        tp->frto_highmark = tp->snd_nxt;
1003}
1004
1005/* Enter Loss state after F-RTO was applied. Dupack arrived after RTO,
1006 * which indicates that we should follow the traditional RTO recovery,
1007 * i.e. mark everything lost and do go-back-N retransmission.
1008 */
1009void tcp_enter_frto_loss(struct sock *sk)
1010{
1011        struct tcp_opt *tp = tcp_sk(sk);
1012        struct sk_buff *skb;
1013        int cnt = 0;
1014
1015        tp->sacked_out = 0;
1016        tp->lost_out = 0;
1017        tp->fackets_out = 0;
1018
1019        for_retrans_queue(skb, sk, tp) {
1020                cnt++;
1021                TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
1022                if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) {
1023
1024                        /* Do not mark those segments lost that were
1025                         * forward transmitted after RTO
1026                         */
1027                        if(!after(TCP_SKB_CB(skb)->end_seq,
1028                                   tp->frto_highmark)) {
1029                                TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1030                                tp->lost_out++;
1031                        }
1032                } else {
1033                        tp->sacked_out++;
1034                        tp->fackets_out = cnt;
1035                }
1036        }
1037        tcp_sync_left_out(tp);
1038
1039        tp->snd_cwnd = tp->frto_counter + tcp_packets_in_flight(tp)+1;
1040        tp->snd_cwnd_cnt = 0;
1041        tp->snd_cwnd_stamp = tcp_time_stamp;
1042        tp->undo_marker = 0;
1043        tp->frto_counter = 0;
1044
1045        tp->reordering = min_t(unsigned int, tp->reordering,
1046                                             sysctl_tcp_reordering);
1047        tp->ca_state = TCP_CA_Loss;
1048        tp->high_seq = tp->frto_highmark;
1049        TCP_ECN_queue_cwr(tp);
1050}
1051
1052void tcp_clear_retrans(struct tcp_opt *tp)
1053{
1054        tp->left_out = 0;
1055        tp->retrans_out = 0;
1056
1057        tp->fackets_out = 0;
1058        tp->sacked_out = 0;
1059        tp->lost_out = 0;
1060
1061        tp->undo_marker = 0;
1062        tp->undo_retrans = 0;
1063}
1064
1065/* Enter Loss state. If "how" is not zero, forget all SACK information
1066 * and reset tags completely, otherwise preserve SACKs. If receiver
1067 * dropped its ofo queue, we will know this due to reneging detection.
1068 */
1069void tcp_enter_loss(struct sock *sk, int how)
1070{
1071        struct tcp_opt *tp = tcp_sk(sk);
1072        struct sk_buff *skb;
1073        int cnt = 0;
1074
1075        /* Reduce ssthresh if it has not yet been made inside this window. */
1076        if (tp->ca_state <= TCP_CA_Disorder || tp->snd_una == tp->high_seq ||
1077            (tp->ca_state == TCP_CA_Loss && !tp->retransmits)) {
1078                tp->prior_ssthresh = tcp_current_ssthresh(tp);
1079                tp->snd_ssthresh = tcp_recalc_ssthresh(tp);
1080        }
1081        tp->snd_cwnd       = 1;
1082        tp->snd_cwnd_cnt   = 0;
1083        tp->snd_cwnd_stamp = tcp_time_stamp;
1084
1085        tcp_clear_retrans(tp);
1086
1087        /* Push undo marker, if it was plain RTO and nothing
1088         * was retransmitted. */
1089        if (!how)
1090                tp->undo_marker = tp->snd_una;
1091
1092        for_retrans_queue(skb, sk, tp) {
1093                cnt++;
1094                if (TCP_SKB_CB(skb)->sacked&TCPCB_RETRANS)
1095                        tp->undo_marker = 0;
1096                TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
1097                if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || how) {
1098                        TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
1099                        TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1100                        tp->lost_out++;
1101                } else {
1102                        tp->sacked_out++;
1103                        tp->fackets_out = cnt;
1104                }
1105        }
1106        tcp_sync_left_out(tp);
1107
1108        tp->reordering = min_t(unsigned int, tp->reordering,
1109                                             sysctl_tcp_reordering);
1110        tp->ca_state = TCP_CA_Loss;
1111        tp->high_seq = tp->snd_nxt;
1112        TCP_ECN_queue_cwr(tp);
1113}
1114
1115static int tcp_check_sack_reneging(struct sock *sk, struct tcp_opt *tp)
1116{
1117        struct sk_buff *skb;
1118
1119        /* If ACK arrived pointing to a remembered SACK,
1120         * it means that our remembered SACKs do not reflect
1121         * real state of receiver i.e.
1122         * receiver _host_ is heavily congested (or buggy).
1123         * Do processing similar to RTO timeout.
1124         */
1125        if ((skb = skb_peek(&sk->sk_write_queue)) != NULL &&
1126            (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
1127                NET_INC_STATS_BH(TCPSACKReneging);
1128
1129                tcp_enter_loss(sk, 1);
1130                tp->retransmits++;
1131                tcp_retransmit_skb(sk, skb_peek(&sk->sk_write_queue));
1132                tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
1133                return 1;
1134        }
1135        return 0;
1136}
1137
1138static inline int tcp_fackets_out(struct tcp_opt *tp)
1139{
1140        return IsReno(tp) ? tp->sacked_out+1 : tp->fackets_out;
1141}
1142
1143static inline int tcp_skb_timedout(struct tcp_opt *tp, struct sk_buff *skb)
1144{
1145        return (tcp_time_stamp - TCP_SKB_CB(skb)->when > tp->rto);
1146}
1147
1148static inline int tcp_head_timedout(struct sock *sk, struct tcp_opt *tp)
1149{
1150        return tp->packets_out &&
1151               tcp_skb_timedout(tp, skb_peek(&sk->sk_write_queue));
1152}
1153
1154/* Linux NewReno/SACK/FACK/ECN state machine.
1155 * --------------------------------------
1156 *
1157 * "Open"       Normal state, no dubious events, fast path.
1158 * "Disorder"   In all the respects it is "Open",
1159 *              but requires a bit more attention. It is entered when
1160 *              we see some SACKs or dupacks. It is split of "Open"
1161 *              mainly to move some processing from fast path to slow one.
1162 * "CWR"        CWND was reduced due to some Congestion Notification event.
1163 *              It can be ECN, ICMP source quench, local device congestion.
1164 * "Recovery"   CWND was reduced, we are fast-retransmitting.
1165 * "Loss"       CWND was reduced due to RTO timeout or SACK reneging.
1166 *
1167 * tcp_fastretrans_alert() is entered:
1168 * - each incoming ACK, if state is not "Open"
1169 * - when arrived ACK is unusual, namely:
1170 *      * SACK
1171 *      * Duplicate ACK.
1172 *      * ECN ECE.
1173 *
1174 * Counting packets in flight is pretty simple.
1175 *
1176 *      in_flight = packets_out - left_out + retrans_out
1177 *
1178 *      packets_out is SND.NXT-SND.UNA counted in packets.
1179 *
1180 *      retrans_out is number of retransmitted segments.
1181 *
1182 *      left_out is number of segments left network, but not ACKed yet.
1183 *
1184 *              left_out = sacked_out + lost_out
1185 *
1186 *     sacked_out: Packets, which arrived to receiver out of order
1187 *                 and hence not ACKed. With SACKs this number is simply
1188 *                 amount of SACKed data. Even without SACKs
1189 *                 it is easy to give pretty reliable estimate of this number,
1190 *                 counting duplicate ACKs.
1191 *
1192 *       lost_out: Packets lost by network. TCP has no explicit
1193 *                 "loss notification" feedback from network (for now).
1194 *                 It means that this number can be only _guessed_.
1195 *                 Actually, it is the heuristics to predict lossage that
1196 *                 distinguishes different algorithms.
1197 *
1198 *      F.e. after RTO, when all the queue is considered as lost,
1199 *      lost_out = packets_out and in_flight = retrans_out.
1200 *
1201 *              Essentially, we have now two algorithms counting
1202 *              lost packets.
1203 *
1204 *              FACK: It is the simplest heuristics. As soon as we decided
1205 *              that something is lost, we decide that _all_ not SACKed
1206 *              packets until the most forward SACK are lost. I.e.
1207 *              lost_out = fackets_out - sacked_out and left_out = fackets_out.
1208 *              It is absolutely correct estimate, if network does not reorder
1209 *              packets. And it loses any connection to reality when reordering
1210 *              takes place. We use FACK by default until reordering
1211 *              is suspected on the path to this destination.
1212 *
1213 *              NewReno: when Recovery is entered, we assume that one segment
1214 *              is lost (classic Reno). While we are in Recovery and
1215 *              a partial ACK arrives, we assume that one more packet
1216 *              is lost (NewReno). This heuristics are the same in NewReno
1217 *              and SACK.
1218 *
1219 *  Imagine, that's all! Forget about all this shamanism about CWND inflation
1220 *  deflation etc. CWND is real congestion window, never inflated, changes
1221 *  only according to classic VJ rules.
1222 *
1223 * Really tricky (and requiring careful tuning) part of algorithm
1224 * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
1225 * The first determines the moment _when_ we should reduce CWND and,
1226 * hence, slow down forward transmission. In fact, it determines the moment
1227 * when we decide that hole is caused by loss, rather than by a reorder.
1228 *
1229 * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
1230 * holes, caused by lost packets.
1231 *
1232 * And the most logically complicated part of algorithm is undo
1233 * heuristics. We detect false retransmits due to both too early
1234 * fast retransmit (reordering) and underestimated RTO, analyzing
1235 * timestamps and D-SACKs. When we detect that some segments were
1236 * retransmitted by mistake and CWND reduction was wrong, we undo
1237 * window reduction and abort recovery phase. This logic is hidden
1238 * inside several functions named tcp_try_undo_<something>.
1239 */
1240
1241/* This function decides, when we should leave Disordered state
1242 * and enter Recovery phase, reducing congestion window.
1243 *
1244 * Main question: may we further continue forward transmission
1245 * with the same cwnd?
1246 */
1247static int
1248tcp_time_to_recover(struct sock *sk, struct tcp_opt *tp)
1249{
1250        /* Trick#1: The loss is proven. */
1251        if (tp->lost_out)
1252                return 1;
1253
1254        /* Not-A-Trick#2 : Classic rule... */
1255        if (tcp_fackets_out(tp) > tp->reordering)
1256                return 1;
1257
1258        /* Trick#3 : when we use RFC2988 timer restart, fast
1259         * retransmit can be triggered by timeout of queue head.
1260         */
1261        if (tcp_head_timedout(sk, tp))
1262                return 1;
1263
1264        /* Trick#4: It is still not OK... But will it be useful to delay
1265         * recovery more?
1266         */
1267        if (tp->packets_out <= tp->reordering &&
1268            tp->sacked_out >= max_t(__u32, tp->packets_out/2, sysctl_tcp_reordering) &&
1269            !tcp_may_send_now(sk, tp)) {
1270                /* We have nothing to send. This connection is limited
1271                 * either by receiver window or by application.
1272                 */
1273                return 1;
1274        }
1275
1276        return 0;
1277}
1278
1279/* If we receive more dupacks than we expected counting segments
1280 * in assumption of absent reordering, interpret this as reordering.
1281 * The only another reason could be bug in receiver TCP.
1282 */
1283static void tcp_check_reno_reordering(struct tcp_opt *tp, int addend)
1284{
1285        u32 holes;
1286
1287        holes = max(tp->lost_out, 1U);
1288        holes = min(holes, tp->packets_out);
1289
1290        if (tp->sacked_out + holes > tp->packets_out) {
1291                tp->sacked_out = tp->packets_out - holes;
1292                tcp_update_reordering(tp, tp->packets_out+addend, 0);
1293        }
1294}
1295
1296/* Emulate SACKs for SACKless connection: account for a new dupack. */
1297
1298static void tcp_add_reno_sack(struct tcp_opt *tp)
1299{
1300        ++tp->sacked_out;
1301        tcp_check_reno_reordering(tp, 0);
1302        tcp_sync_left_out(tp);
1303}
1304
1305/* Account for ACK, ACKing some data in Reno Recovery phase. */
1306
1307static void tcp_remove_reno_sacks(struct sock *sk, struct tcp_opt *tp, int acked)
1308{
1309        if (acked > 0) {
1310                /* One ACK acked hole. The rest eat duplicate ACKs. */
1311                if (acked-1 >= tp->sacked_out)
1312                        tp->sacked_out = 0;
1313                else
1314                        tp->sacked_out -= acked-1;
1315        }
1316        tcp_check_reno_reordering(tp, acked);
1317        tcp_sync_left_out(tp);
1318}
1319
1320static inline void tcp_reset_reno_sack(struct tcp_opt *tp)
1321{
1322        tp->sacked_out = 0;
1323        tp->left_out = tp->lost_out;
1324}
1325
1326/* Mark head of queue up as lost. */
1327static void
1328tcp_mark_head_lost(struct sock *sk, struct tcp_opt *tp, int packets, u32 high_seq)
1329{
1330        struct sk_buff *skb;
1331        int cnt = packets;
1332
1333        BUG_TRAP(cnt <= tp->packets_out);
1334
1335        for_retrans_queue(skb, sk, tp) {
1336                if (--cnt < 0 || after(TCP_SKB_CB(skb)->end_seq, high_seq))
1337                        break;
1338                if (!(TCP_SKB_CB(skb)->sacked&TCPCB_TAGBITS)) {
1339                        TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1340                        tp->lost_out++;
1341                }
1342        }
1343        tcp_sync_left_out(tp);
1344}
1345
1346/* Account newly detected lost packet(s) */
1347
1348static void tcp_update_scoreboard(struct sock *sk, struct tcp_opt *tp)
1349{
1350        if (IsFack(tp)) {
1351                int lost = tp->fackets_out - tp->reordering;
1352                if (lost <= 0)
1353                        lost = 1;
1354                tcp_mark_head_lost(sk, tp, lost, tp->high_seq);
1355        } else {
1356                tcp_mark_head_lost(sk, tp, 1, tp->high_seq);
1357        }
1358
1359        /* New heuristics: it is possible only after we switched
1360         * to restart timer each time when something is ACKed.
1361         * Hence, we can detect timed out packets during fast
1362         * retransmit without falling to slow start.
1363         */
1364        if (tcp_head_timedout(sk, tp)) {
1365                struct sk_buff *skb;
1366
1367                for_retrans_queue(skb, sk, tp) {
1368                        if (tcp_skb_timedout(tp, skb) &&
1369                            !(TCP_SKB_CB(skb)->sacked&TCPCB_TAGBITS)) {
1370                                TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1371                                tp->lost_out++;
1372                        }
1373                }
1374                tcp_sync_left_out(tp);
1375        }
1376}
1377
1378/* CWND moderation, preventing bursts due to too big ACKs
1379 * in dubious situations.
1380 */
1381static __inline__ void tcp_moderate_cwnd(struct tcp_opt *tp)
1382{
1383        tp->snd_cwnd = min(tp->snd_cwnd,
1384                           tcp_packets_in_flight(tp)+tcp_max_burst(tp));
1385        tp->snd_cwnd_stamp = tcp_time_stamp;
1386}
1387
1388/* Decrease cwnd each second ack. */
1389
1390static void tcp_cwnd_down(struct tcp_opt *tp)
1391{
1392        int decr = tp->snd_cwnd_cnt + 1;
1393
1394        tp->snd_cwnd_cnt = decr&1;
1395        decr >>= 1;
1396
1397        if (decr && tp->snd_cwnd > tp->snd_ssthresh/2)
1398                tp->snd_cwnd -= decr;
1399
1400        tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp)+1);
1401        tp->snd_cwnd_stamp = tcp_time_stamp;
1402}
1403
1404/* Nothing was retransmitted or returned timestamp is less
1405 * than timestamp of the first retransmission.
1406 */
1407static __inline__ int tcp_packet_delayed(struct tcp_opt *tp)
1408{
1409        return !tp->retrans_stamp ||
1410                (tp->saw_tstamp && tp->rcv_tsecr &&
1411                 (__s32)(tp->rcv_tsecr - tp->retrans_stamp) < 0);
1412}
1413
1414/* Undo procedures. */
1415
1416#if FASTRETRANS_DEBUG > 1
1417static void DBGUNDO(struct sock *sk, struct tcp_opt *tp, const char *msg)
1418{
1419        struct inet_opt *inet = inet_sk(sk);
1420        printk(KERN_DEBUG "Undo %s %u.%u.%u.%u/%u c%u l%u ss%u/%u p%u\n",
1421               msg,
1422               NIPQUAD(inet->daddr), ntohs(inet->dport),
1423               tp->snd_cwnd, tp->left_out,
1424               tp->snd_ssthresh, tp->prior_ssthresh, tp->packets_out);
1425}
1426#else
1427#define DBGUNDO(x...) do { } while (0)
1428#endif
1429
1430static void tcp_undo_cwr(struct tcp_opt *tp, int undo)
1431{
1432        if (tp->prior_ssthresh) {
1433                tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh<<1);
1434
1435                if (undo && tp->prior_ssthresh > tp->snd_ssthresh) {
1436                        tp->snd_ssthresh = tp->prior_ssthresh;
1437                        TCP_ECN_withdraw_cwr(tp);
1438                }
1439        } else {
1440                tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh);
1441        }
1442        tcp_moderate_cwnd(tp);
1443        tp->snd_cwnd_stamp = tcp_time_stamp;
1444}
1445
1446static inline int tcp_may_undo(struct tcp_opt *tp)
1447{
1448        return tp->undo_marker &&
1449                (!tp->undo_retrans || tcp_packet_delayed(tp));
1450}
1451
1452/* People celebrate: "We love our President!" */
1453static int tcp_try_undo_recovery(struct sock *sk, struct tcp_opt *tp)
1454{
1455        if (tcp_may_undo(tp)) {
1456                /* Happy end! We did not retransmit anything
1457                 * or our original transmission succeeded.
1458                 */
1459                DBGUNDO(sk, tp, tp->ca_state == TCP_CA_Loss ? "loss" : "retrans");
1460                tcp_undo_cwr(tp, 1);
1461                if (tp->ca_state == TCP_CA_Loss)
1462                        NET_INC_STATS_BH(TCPLossUndo);
1463                else
1464                        NET_INC_STATS_BH(TCPFullUndo);
1465                tp->undo_marker = 0;
1466        }
1467        if (tp->snd_una == tp->high_seq && IsReno(tp)) {
1468                /* Hold old state until something *above* high_seq
1469                 * is ACKed. For Reno it is MUST to prevent false
1470                 * fast retransmits (RFC2582). SACK TCP is safe. */
1471                tcp_moderate_cwnd(tp);
1472                return 1;
1473        }
1474        tp->ca_state = TCP_CA_Open;
1475        return 0;
1476}
1477
1478/* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
1479static void tcp_try_undo_dsack(struct sock *sk, struct tcp_opt *tp)
1480{
1481        if (tp->undo_marker && !tp->undo_retrans) {
1482                DBGUNDO(sk, tp, "D-SACK");
1483                tcp_undo_cwr(tp, 1);
1484                tp->undo_marker = 0;
1485                NET_INC_STATS_BH(TCPDSACKUndo);
1486        }
1487}
1488
1489/* Undo during fast recovery after partial ACK. */
1490
1491static int tcp_try_undo_partial(struct sock *sk, struct tcp_opt *tp, int acked)
1492{
1493        /* Partial ACK arrived. Force Hoe's retransmit. */
1494        int failed = IsReno(tp) || tp->fackets_out>tp->reordering;
1495
1496        if (tcp_may_undo(tp)) {
1497                /* Plain luck! Hole if filled with delayed
1498                 * packet, rather than with a retransmit.
1499                 */
1500                if (tp->retrans_out == 0)
1501                        tp->retrans_stamp = 0;
1502
1503                tcp_update_reordering(tp, tcp_fackets_out(tp)+acked, 1);
1504
1505                DBGUNDO(sk, tp, "Hoe");
1506                tcp_undo_cwr(tp, 0);
1507                NET_INC_STATS_BH(TCPPartialUndo);
1508
1509                /* So... Do not make Hoe's retransmit yet.
1510                 * If the first packet was delayed, the rest
1511                 * ones are most probably delayed as well.
1512                 */
1513                failed = 0;
1514        }
1515        return failed;
1516}
1517
1518/* Undo during loss recovery after partial ACK. */
1519static int tcp_try_undo_loss(struct sock *sk, struct tcp_opt *tp)
1520{
1521        if (tcp_may_undo(tp)) {
1522                struct sk_buff *skb;
1523                for_retrans_queue(skb, sk, tp) {
1524                        TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
1525                }
1526                DBGUNDO(sk, tp, "partial loss");
1527                tp->lost_out = 0;
1528                tp->left_out = tp->sacked_out;
1529                tcp_undo_cwr(tp, 1);
1530                NET_INC_STATS_BH(TCPLossUndo);
1531                tp->retransmits = 0;
1532                tp->undo_marker = 0;
1533                if (!IsReno(tp))
1534                        tp->ca_state = TCP_CA_Open;
1535                return 1;
1536        }
1537        return 0;
1538}
1539
1540static __inline__ void tcp_complete_cwr(struct tcp_opt *tp)
1541{
1542        tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh);
1543        tp->snd_cwnd_stamp = tcp_time_stamp;
1544}
1545
1546static void tcp_try_to_open(struct sock *sk, struct tcp_opt *tp, int flag)
1547{
1548        tp->left_out = tp->sacked_out;
1549
1550        if (tp->retrans_out == 0)
1551                tp->retrans_stamp = 0;
1552
1553        if (flag&FLAG_ECE)
1554                tcp_enter_cwr(tp);
1555
1556        if (tp->ca_state != TCP_CA_CWR) {
1557                int state = TCP_CA_Open;
1558
1559                if (tp->left_out ||
1560                    tp->retrans_out ||
1561                    tp->undo_marker)
1562                        state = TCP_CA_Disorder;
1563
1564                if (tp->ca_state != state) {
1565                        tp->ca_state = state;
1566                        tp->high_seq = tp->snd_nxt;
1567                }
1568                tcp_moderate_cwnd(tp);
1569        } else {
1570                tcp_cwnd_down(tp);
1571        }
1572}
1573
1574/* Process an event, which can update packets-in-flight not trivially.
1575 * Main goal of this function is to calculate new estimate for left_out,
1576 * taking into account both packets sitting in receiver's buffer and
1577 * packets lost by network.
1578 *
1579 * Besides that it does CWND reduction, when packet loss is detected
1580 * and changes state of machine.
1581 *
1582 * It does _not_ decide what to send, it is made in function
1583 * tcp_xmit_retransmit_queue().
1584 */
1585static void
1586tcp_fastretrans_alert(struct sock *sk, u32 prior_snd_una,
1587                      int prior_packets, int flag)
1588{
1589        struct tcp_opt *tp = tcp_sk(sk);
1590        int is_dupack = (tp->snd_una == prior_snd_una && !(flag&FLAG_NOT_DUP));
1591
1592        /* Some technical things:
1593         * 1. Reno does not count dupacks (sacked_out) automatically. */
1594        if (!tp->packets_out)
1595                tp->sacked_out = 0;
1596        /* 2. SACK counts snd_fack in packets inaccurately. */
1597        if (tp->sacked_out == 0)
1598                tp->fackets_out = 0;
1599
1600        /* Now state machine starts.
1601         * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
1602        if (flag&FLAG_ECE)
1603                tp->prior_ssthresh = 0;
1604
1605        /* B. In all the states check for reneging SACKs. */
1606        if (tp->sacked_out && tcp_check_sack_reneging(sk, tp))
1607                return;
1608
1609        /* C. Process data loss notification, provided it is valid. */
1610        if ((flag&FLAG_DATA_LOST) &&
1611            before(tp->snd_una, tp->high_seq) &&
1612            tp->ca_state != TCP_CA_Open &&
1613            tp->fackets_out > tp->reordering) {
1614                tcp_mark_head_lost(sk, tp, tp->fackets_out-tp->reordering, tp->high_seq);
1615                NET_INC_STATS_BH(TCPLoss);
1616        }
1617
1618        /* D. Synchronize left_out to current state. */
1619        tcp_sync_left_out(tp);
1620
1621        /* E. Check state exit conditions. State can be terminated
1622         *    when high_seq is ACKed. */
1623        if (tp->ca_state == TCP_CA_Open) {
1624                if (!sysctl_tcp_frto)
1625                        BUG_TRAP(tp->retrans_out == 0);
1626                tp->retrans_stamp = 0;
1627        } else if (!before(tp->snd_una, tp->high_seq)) {
1628                switch (tp->ca_state) {
1629                case TCP_CA_Loss:
1630                        tp->retransmits = 0;
1631                        if (tcp_try_undo_recovery(sk, tp))
1632                                return;
1633                        break;
1634
1635                case TCP_CA_CWR:
1636                        /* CWR is to be held something *above* high_seq
1637                         * is ACKed for CWR bit to reach receiver. */
1638                        if (tp->snd_una != tp->high_seq) {
1639                                tcp_complete_cwr(tp);
1640                                tp->ca_state = TCP_CA_Open;
1641                        }
1642                        break;
1643
1644                case TCP_CA_Disorder:
1645                        tcp_try_undo_dsack(sk, tp);
1646                        if (!tp->undo_marker ||
1647                            /* For SACK case do not Open to allow to undo
1648                             * catching for all duplicate ACKs. */
1649                            IsReno(tp) || tp->snd_una != tp->high_seq) {
1650                                tp->undo_marker = 0;
1651                                tp->ca_state = TCP_CA_Open;
1652                        }
1653                        break;
1654
1655                case TCP_CA_Recovery:
1656                        if (IsReno(tp))
1657                                tcp_reset_reno_sack(tp);
1658                        if (tcp_try_undo_recovery(sk, tp))
1659                                return;
1660                        tcp_complete_cwr(tp);
1661                        break;
1662                }
1663        }
1664
1665        /* F. Process state. */
1666        switch (tp->ca_state) {
1667        case TCP_CA_Recovery:
1668                if (prior_snd_una == tp->snd_una) {
1669                        if (IsReno(tp) && is_dupack)
1670                                tcp_add_reno_sack(tp);
1671                } else {
1672                        int acked = prior_packets - tp->packets_out;
1673                        if (IsReno(tp))
1674                                tcp_remove_reno_sacks(sk, tp, acked);
1675                        is_dupack = tcp_try_undo_partial(sk, tp, acked);
1676                }
1677                break;
1678        case TCP_CA_Loss:
1679                if (flag&FLAG_DATA_ACKED)
1680                        tp->retransmits = 0;
1681                if (!tcp_try_undo_loss(sk, tp)) {
1682                        tcp_moderate_cwnd(tp);
1683                        tcp_xmit_retransmit_queue(sk);
1684                        return;
1685                }
1686                if (tp->ca_state != TCP_CA_Open)
1687                        return;
1688                /* Loss is undone; fall through to processing in Open state. */
1689        default:
1690                if (IsReno(tp)) {
1691                        if (tp->snd_una != prior_snd_una)
1692                                tcp_reset_reno_sack(tp);
1693                        if (is_dupack)
1694                                tcp_add_reno_sack(tp);
1695                }
1696
1697                if (tp->ca_state == TCP_CA_Disorder)
1698                        tcp_try_undo_dsack(sk, tp);
1699
1700                if (!tcp_time_to_recover(sk, tp)) {
1701                        tcp_try_to_open(sk, tp, flag);
1702                        return;
1703                }
1704
1705                /* Otherwise enter Recovery state */
1706
1707                if (IsReno(tp))
1708                        NET_INC_STATS_BH(TCPRenoRecovery);
1709                else
1710                        NET_INC_STATS_BH(TCPSackRecovery);
1711
1712                tp->high_seq = tp->snd_nxt;
1713                tp->prior_ssthresh = 0;
1714                tp->undo_marker = tp->snd_una;
1715                tp->undo_retrans = tp->retrans_out;
1716
1717                if (tp->ca_state < TCP_CA_CWR) {
1718                        if (!(flag&FLAG_ECE))
1719                                tp->prior_ssthresh = tcp_current_ssthresh(tp);
1720                        tp->snd_ssthresh = tcp_recalc_ssthresh(tp);
1721                        TCP_ECN_queue_cwr(tp);
1722                }
1723
1724                tp->snd_cwnd_cnt = 0;
1725                tp->ca_state = TCP_CA_Recovery;
1726        }
1727
1728        if (is_dupack || tcp_head_timedout(sk, tp))
1729                tcp_update_scoreboard(sk, tp);
1730        tcp_cwnd_down(tp);
1731        tcp_xmit_retransmit_queue(sk);
1732}
1733
1734/* Read draft-ietf-tcplw-high-performance before mucking
1735 * with this code. (Superceeds RFC1323)
1736 */
1737static void tcp_ack_saw_tstamp(struct tcp_opt *tp, int flag)
1738{
1739        __u32 seq_rtt;
1740
1741        /* RTTM Rule: A TSecr value received in a segment is used to
1742         * update the averaged RTT measurement only if the segment
1743         * acknowledges some new data, i.e., only if it advances the
1744         * left edge of the send window.
1745         *
1746         * See draft-ietf-tcplw-high-performance-00, section 3.3.
1747         * 1998/04/10 Andrey V. Savochkin <saw@msu.ru>
1748         *
1749         * Changed: reset backoff as soon as we see the first valid sample.
1750         * If we do not, we get strongly overstimated rto. With timestamps
1751         * samples are accepted even from very old segments: f.e., when rtt=1
1752         * increases to 8, we retransmit 5 times and after 8 seconds delayed
1753         * answer arrives rto becomes 120 seconds! If at least one of segments
1754         * in window is lost... Voila.                          --ANK (010210)
1755         */
1756        seq_rtt = tcp_time_stamp - tp->rcv_tsecr;
1757        tcp_rtt_estimator(tp, seq_rtt);
1758        tcp_set_rto(tp);
1759        tp->backoff = 0;
1760        tcp_bound_rto(tp);
1761}
1762
1763static void tcp_ack_no_tstamp(struct tcp_opt *tp, u32 seq_rtt, int flag)
1764{
1765        /* We don't have a timestamp. Can only use
1766         * packets that are not retransmitted to determine
1767         * rtt estimates. Also, we must not reset the
1768         * backoff for rto until we get a non-retransmitted
1769         * packet. This allows us to deal with a situation
1770         * where the network delay has increased suddenly.
1771         * I.e. Karn's algorithm. (SIGCOMM '87, p5.)
1772         */
1773
1774        if (flag & FLAG_RETRANS_DATA_ACKED)
1775                return;
1776
1777        tcp_rtt_estimator(tp, seq_rtt);
1778        tcp_set_rto(tp);
1779        tp->backoff = 0;
1780        tcp_bound_rto(tp);
1781}
1782
1783static __inline__ void
1784tcp_ack_update_rtt(struct tcp_opt *tp, int flag, s32 seq_rtt)
1785{
1786        /* Note that peer MAY send zero echo. In this case it is ignored. (rfc1323) */
1787        if (tp->saw_tstamp && tp->rcv_tsecr)
1788                tcp_ack_saw_tstamp(tp, flag);
1789        else if (seq_rtt >= 0)
1790                tcp_ack_no_tstamp(tp, seq_rtt, flag);
1791}
1792
1793/* This is Jacobson's slow start and congestion avoidance. 
1794 * SIGCOMM '88, p. 328.
1795 */
1796static __inline__ void tcp_cong_avoid(struct tcp_opt *tp)
1797{
1798        if (tp->snd_cwnd <= tp->snd_ssthresh) {
1799                /* In "safe" area, increase. */
1800                if (tp->snd_cwnd < tp->snd_cwnd_clamp)
1801                        tp->snd_cwnd++;
1802        } else {
1803                /* In dangerous area, increase slowly.
1804                 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
1805                 */
1806                if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
1807                        if (tp->snd_cwnd < tp->snd_cwnd_clamp)
1808                                tp->snd_cwnd++;
1809                        tp->snd_cwnd_cnt=0;
1810                } else
1811                        tp->snd_cwnd_cnt++;
1812        }
1813        tp->snd_cwnd_stamp = tcp_time_stamp;
1814}
1815
1816/* Restart timer after forward progress on connection.
1817 * RFC2988 recommends to restart timer to now+rto.
1818 */
1819
1820static __inline__ void tcp_ack_packets_out(struct sock *sk, struct tcp_opt *tp)
1821{
1822        if (tp->packets_out==0) {
1823                tcp_clear_xmit_timer(sk, TCP_TIME_RETRANS);
1824        } else {
1825                tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
1826        }
1827}
1828
1829/* Remove acknowledged frames from the retransmission queue. */
1830static int tcp_clean_rtx_queue(struct sock *sk)
1831{
1832        struct tcp_opt *tp = tcp_sk(sk);
1833        struct sk_buff *skb;
1834        __u32 now = tcp_time_stamp;
1835        int acked = 0;
1836        __s32 seq_rtt = -1;
1837
1838        while ((skb = skb_peek(&sk->sk_write_queue)) && skb != tp->send_head) {
1839                struct tcp_skb_cb *scb = TCP_SKB_CB(skb); 
1840                __u8 sacked = scb->sacked;
1841
1842                /* If our packet is before the ack sequence we can
1843                 * discard it as it's confirmed to have arrived at
1844                 * the other end.
1845                 */
1846                if (after(scb->end_seq, tp->snd_una))
1847                        break;
1848
1849                /* Initial outgoing SYN's get put onto the write_queue
1850                 * just like anything else we transmit.  It is not
1851                 * true data, and if we misinform our callers that
1852                 * this ACK acks real data, we will erroneously exit
1853                 * connection startup slow start one packet too
1854                 * quickly.  This is severely frowned upon behavior.
1855                 */
1856                if(!(scb->flags & TCPCB_FLAG_SYN)) {
1857                        acked |= FLAG_DATA_ACKED;
1858                } else {
1859                        acked |= FLAG_SYN_ACKED;
1860                        tp->retrans_stamp = 0;
1861                }
1862
1863                if (sacked) {
1864                        if(sacked & TCPCB_RETRANS) {
1865                                if(sacked & TCPCB_SACKED_RETRANS)
1866                                        tp->retrans_out--;
1867                                acked |= FLAG_RETRANS_DATA_ACKED;
1868                                seq_rtt = -1;
1869                        } else if (seq_rtt < 0)
1870                                seq_rtt = now - scb->when;
1871                        if(sacked & TCPCB_SACKED_ACKED)
1872                                tp->sacked_out--;
1873                        if(sacked & TCPCB_LOST)
1874                                tp->lost_out--;
1875                        if(sacked & TCPCB_URG) {
1876                                if (tp->urg_mode &&
1877                                    !before(scb->end_seq, tp->snd_up))
1878                                        tp->urg_mode = 0;
1879                        }
1880                } else if (seq_rtt < 0)
1881                        seq_rtt = now - scb->when;
1882                if (tp->fackets_out)
1883                        tp->fackets_out--;
1884                tp->packets_out--;
1885                __skb_unlink(skb, skb->list);
1886                tcp_free_skb(sk, skb);
1887        }
1888
1889        if (acked&FLAG_ACKED) {
1890                tcp_ack_update_rtt(tp, acked, seq_rtt);
1891                tcp_ack_packets_out(sk, tp);
1892        }
1893
1894#if FASTRETRANS_DEBUG > 0
1895        BUG_TRAP((int)tp->sacked_out >= 0);
1896        BUG_TRAP((int)tp->lost_out >= 0);
1897        BUG_TRAP((int)tp->retrans_out >= 0);
1898        if (!tp->packets_out && tp->sack_ok) {
1899                if (tp->lost_out) {
1900                        printk(KERN_DEBUG "Leak l=%u %d\n", tp->lost_out,
1901                                                            tp->ca_state);
1902                        tp->lost_out = 0;
1903                }
1904                if (tp->sacked_out) {
1905                        printk(KERN_DEBUG "Leak s=%u %d\n", tp->sacked_out,
1906                                                            tp->ca_state);
1907                        tp->sacked_out = 0;
1908                }
1909                if (tp->retrans_out) {
1910                        printk(KERN_DEBUG "Leak r=%u %d\n", tp->retrans_out,
1911                                                            tp->ca_state);
1912                        tp->retrans_out = 0;
1913                }
1914        }
1915#endif
1916        return acked;
1917}
1918
1919static void tcp_ack_probe(struct sock *sk)
1920{
1921        struct tcp_opt *tp = tcp_sk(sk);
1922
1923        /* Was it a usable window open? */
1924
1925        if (!after(TCP_SKB_CB(tp->send_head)->end_seq,
1926                   tp->snd_una + tp->snd_wnd)) {
1927                tp->backoff = 0;
1928                tcp_clear_xmit_timer(sk, TCP_TIME_PROBE0);
1929                /* Socket must be waked up by subsequent tcp_data_snd_check().
1930                 * This function is not for random using!
1931                 */
1932        } else {
1933                tcp_reset_xmit_timer(sk, TCP_TIME_PROBE0,
1934                                     min(tp->rto << tp->backoff, TCP_RTO_MAX));
1935        }
1936}
1937
1938static __inline__ int tcp_ack_is_dubious(struct tcp_opt *tp, int flag)
1939{
1940        return (!(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
1941                tp->ca_state != TCP_CA_Open);
1942}
1943
1944static __inline__ int tcp_may_raise_cwnd(struct tcp_opt *tp, int flag)
1945{
1946        return (!(flag & FLAG_ECE) || tp->snd_cwnd < tp->snd_ssthresh) &&
1947                !((1<<tp->ca_state)&(TCPF_CA_Recovery|TCPF_CA_CWR));
1948}
1949
1950/* Check that window update is acceptable.
1951 * The function assumes that snd_una<=ack<=snd_next.
1952 */
1953static __inline__ int
1954tcp_may_update_window(struct tcp_opt *tp, u32 ack, u32 ack_seq, u32 nwin)
1955{
1956        return (after(ack, tp->snd_una) ||
1957                after(ack_seq, tp->snd_wl1) ||
1958                (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd));
1959}
1960
1961/* Update our send window.
1962 *
1963 * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
1964 * and in FreeBSD. NetBSD's one is even worse.) is wrong.
1965 */
1966static int tcp_ack_update_window(struct sock *sk, struct tcp_opt *tp,
1967                                 struct sk_buff *skb, u32 ack, u32 ack_seq)
1968{
1969        int flag = 0;
1970        u32 nwin = ntohs(skb->h.th->window);
1971
1972        if (likely(!skb->h.th->syn))
1973                nwin <<= tp->snd_wscale;
1974
1975        if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
1976                flag |= FLAG_WIN_UPDATE;
1977                tcp_update_wl(tp, ack, ack_seq);
1978
1979                if (tp->snd_wnd != nwin) {
1980                        tp->snd_wnd = nwin;
1981
1982                        /* Note, it is the only place, where
1983                         * fast path is recovered for sending TCP.
1984                         */
1985                        tcp_fast_path_check(sk, tp);
1986
1987                        if (nwin > tp->max_window) {
1988                                tp->max_window = nwin;
1989                                tcp_sync_mss(sk, tp->pmtu_cookie);
1990                        }
1991                }
1992        }
1993
1994        tp->snd_una = ack;
1995
1996        return flag;
1997}
1998
1999static void tcp_process_frto(struct sock *sk, u32 prior_snd_una)
2000{
2001        struct tcp_opt *tp = tcp_sk(sk);
2002        
2003        tcp_sync_left_out(tp);
2004        
2005        if (tp->snd_una == prior_snd_una ||
2006            !before(tp->snd_una, tp->frto_highmark)) {
2007                /* RTO was caused by loss, start retransmitting in
2008                 * go-back-N slow start
2009                 */
2010                tcp_enter_frto_loss(sk);
2011                return;
2012        }
2013
2014        if (tp->frto_counter == 1) {
2015                /* First ACK after RTO advances the window: allow two new
2016                 * segments out.
2017                 */
2018                tp->snd_cwnd = tcp_packets_in_flight(tp) + 2;
2019        } else {
2020                /* Also the second ACK after RTO advances the window.
2021                 * The RTO was likely spurious. Reduce cwnd and continue
2022                 * in congestion avoidance
2023                 */
2024                tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh);
2025                tcp_moderate_cwnd(tp);
2026        }
2027
2028        /* F-RTO affects on two new ACKs following RTO.
2029         * At latest on third ACK the TCP behavor is back to normal.
2030         */
2031        tp->frto_counter = (tp->frto_counter + 1) % 3;
2032}
2033
2034/* This routine deals with incoming acks, but not outgoing ones. */
2035static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag)
2036{
2037        struct tcp_opt *tp = tcp_sk(sk);
2038        u32 prior_snd_una = tp->snd_una;
2039        u32 ack_seq = TCP_SKB_CB(skb)->seq;
2040        u32 ack = TCP_SKB_CB(skb)->ack_seq;
2041        u32 prior_in_flight;
2042        int prior_packets;
2043
2044        /* If the ack is newer than sent or older than previous acks
2045         * then we can probably ignore it.
2046         */
2047        if (after(ack, tp->snd_nxt))
2048                goto uninteresting_ack;
2049
2050        if (before(ack, prior_snd_una))
2051                goto old_ack;
2052
2053        if (!(flag&FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
2054                /* Window is constant, pure forward advance.
2055                 * No more checks are required.
2056                 * Note, we use the fact that SND.UNA>=SND.WL2.
2057                 */
2058                tcp_update_wl(tp, ack, ack_seq);
2059                tp->snd_una = ack;
2060                flag |= FLAG_WIN_UPDATE;
2061
2062                NET_INC_STATS_BH(TCPHPAcks);
2063        } else {
2064                if (ack_seq != TCP_SKB_CB(skb)->end_seq)
2065                        flag |= FLAG_DATA;
2066                else
2067                        NET_INC_STATS_BH(TCPPureAcks);
2068
2069                flag |= tcp_ack_update_window(sk, tp, skb, ack, ack_seq);
2070
2071                if (TCP_SKB_CB(skb)->sacked)
2072                        flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una);
2073
2074                if (TCP_ECN_rcv_ecn_echo(tp, skb->h.th))
2075                        flag |= FLAG_ECE;
2076        }
2077
2078        /* We passed data and got it acked, remove any soft error
2079         * log. Something worked...
2080         */
2081        sk->sk_err_soft = 0;
2082        tp->rcv_tstamp = tcp_time_stamp;
2083        prior_packets = tp->packets_out;
2084        if (!prior_packets)
2085                goto no_queue;
2086
2087        prior_in_flight = tcp_packets_in_flight(tp);
2088
2089        /* See if we can take anything off of the retransmit queue. */
2090        flag |= tcp_clean_rtx_queue(sk);
2091
2092        if (tp->frto_counter)
2093                tcp_process_frto(sk, prior_snd_una);
2094
2095        if (tcp_ack_is_dubious(tp, flag)) {
2096                /* Advanve CWND, if state allows this. */
2097                if ((flag & FLAG_DATA_ACKED) &&
2098                    prior_in_flight >= tp->snd_cwnd &&
2099                    tcp_may_raise_cwnd(tp, flag))
2100                        tcp_cong_avoid(tp);
2101                tcp_fastretrans_alert(sk, prior_snd_una, prior_packets, flag);
2102        } else {
2103                if ((flag & FLAG_DATA_ACKED) && prior_in_flight >= tp->snd_cwnd)
2104                        tcp_cong_avoid(tp);
2105        }
2106
2107        if ((flag & FLAG_FORWARD_PROGRESS) || !(flag&FLAG_NOT_DUP))
2108                dst_confirm(sk->sk_dst_cache);
2109
2110        return 1;
2111
2112no_queue:
2113        tp->probes_out = 0;
2114
2115        /* If this ack opens up a zero window, clear backoff.  It was
2116         * being used to time the probes, and is probably far higher than
2117         * it needs to be for normal retransmission.
2118         */
2119        if (tp->send_head)
2120                tcp_ack_probe(sk);
2121        return 1;
2122
2123old_ack:
2124        if (TCP_SKB_CB(skb)->sacked)
2125                tcp_sacktag_write_queue(sk, skb, prior_snd_una);
2126
2127uninteresting_ack:
2128        SOCK_DEBUG(sk, "Ack %u out of %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
2129        return 0;
2130}
2131
2132
2133/* Look for tcp options. Normally only called on SYN and SYNACK packets.
2134 * But, this can also be called on packets in the established flow when
2135 * the fast version below fails.
2136 */
2137void tcp_parse_options(struct sk_buff *skb, struct tcp_opt *tp, int estab)
2138{
2139        unsigned char *ptr;
2140        struct tcphdr *th = skb->h.th;
2141        int length=(th->doff*4)-sizeof(struct tcphdr);
2142
2143        ptr = (unsigned char *)(th + 1);
2144        tp->saw_tstamp = 0;
2145
2146        while(length>0) {
2147                int opcode=*ptr++;
2148                int opsize;
2149
2150                switch (opcode) {
2151                        case TCPOPT_EOL:
2152                                return;
2153                        case TCPOPT_NOP:        /* Ref: RFC 793 section 3.1 */
2154                                length--;
2155                                continue;
2156                        default:
2157                                opsize=*ptr++;
2158                                if (opsize < 2) /* "silly options" */
2159                                        return;
2160                                if (opsize > length)
2161                                        return; /* don't parse partial options */
2162                                switch(opcode) {
2163                                case TCPOPT_MSS:
2164                                        if(opsize==TCPOLEN_MSS && th->syn && !estab) {
2165                                                u16 in_mss = ntohs(*(__u16 *)ptr);
2166                                                if (in_mss) {
2167                                                        if (tp->user_mss && tp->user_mss < in_mss)
2168                                                                in_mss = tp->user_mss;
2169                                                        tp->mss_clamp = in_mss;
2170                                                }
2171                                        }
2172                                        break;
2173                                case TCPOPT_WINDOW:
2174                                        if(opsize==TCPOLEN_WINDOW && th->syn && !estab)
2175                                                if (sysctl_tcp_window_scaling) {
2176                                                        tp->wscale_ok = 1;
2177                                                        tp->snd_wscale = *(__u8 *)ptr;
2178                                                        if(tp->snd_wscale > 14) {
2179                                                                if(net_ratelimit())
2180                                                                        printk("tcp_parse_options: Illegal window "
2181                                                                               "scaling value %d >14 received.",
2182                                                                               tp->snd_wscale);
2183                                                                tp->snd_wscale = 14;
2184                                                        }
2185                                                }
2186                                        break;
2187                                case TCPOPT_TIMESTAMP:
2188                                        if(opsize==TCPOLEN_TIMESTAMP) {
2189                                                if ((estab && tp->tstamp_ok) ||
2190                                                    (!estab && sysctl_tcp_timestamps)) {
2191                                                        tp->saw_tstamp = 1;
2192                                                        tp->rcv_tsval = ntohl(*(__u32 *)ptr);
2193                                                        tp->rcv_tsecr = ntohl(*(__u32 *)(ptr+4));
2194                                                }
2195                                        }
2196                                        break;
2197                                case TCPOPT_SACK_PERM:
2198                                        if(opsize==TCPOLEN_SACK_PERM && th->syn && !estab) {
2199                                                if (sysctl_tcp_sack) {
2200                                                        tp->sack_ok = 1;
2201                                                        tcp_sack_reset(tp);
2202                                                }
2203                                        }
2204                                        break;
2205
2206                                case TCPOPT_SACK:
2207                                        if((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
2208                                           !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
2209                                           tp->sack_ok) {
2210                                                TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
2211                                        }
2212                                };
2213                                ptr+=opsize-2;
2214                                length-=opsize;
2215                };
2216        }
2217}
2218
2219/* Fast parse options. This hopes to only see timestamps.
2220 * If it is wrong it falls back on tcp_parse_options().
2221 */
2222static __inline__ int tcp_fast_parse_options(struct sk_buff *skb, struct tcphdr *th, struct tcp_opt *tp)
2223{
2224        if (th->doff == sizeof(struct tcphdr)>>2) {
2225                tp->saw_tstamp = 0;
2226                return 0;
2227        } else if (tp->tstamp_ok &&
2228                   th->doff == (sizeof(struct tcphdr)>>2)+(TCPOLEN_TSTAMP_ALIGNED>>2)) {
2229                __u32 *ptr = (__u32 *)(th + 1);
2230                if (*ptr == ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
2231                                  | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
2232                        tp->saw_tstamp = 1;
2233                        ++ptr;
2234                        tp->rcv_tsval = ntohl(*ptr);
2235                        ++ptr;
2236                        tp->rcv_tsecr = ntohl(*ptr);
2237                        return 1;
2238                }
2239        }
2240        tcp_parse_options(skb, tp, 1);
2241        return 1;
2242}
2243
2244static __inline__ void
2245tcp_store_ts_recent(struct tcp_opt *tp)
2246{
2247        tp->ts_recent = tp->rcv_tsval;
2248        tp->ts_recent_stamp = xtime.tv_sec;
2249}
2250
2251static __inline__ void
2252tcp_replace_ts_recent(struct tcp_opt *tp, u32 seq)
2253{
2254        if (tp->saw_tstamp && !after(seq, tp->rcv_wup)) {
2255                /* PAWS bug workaround wrt. ACK frames, the PAWS discard
2256                 * extra check below makes sure this can only happen
2257                 * for pure ACK frames.  -DaveM
2258                 *
2259                 * Not only, also it occurs for expired timestamps.
2260                 */
2261
2262                if((s32)(tp->rcv_tsval - tp->ts_recent) >= 0 ||
2263                   xtime.tv_sec >= tp->ts_recent_stamp + TCP_PAWS_24DAYS)
2264                        tcp_store_ts_recent(tp);
2265        }
2266}
2267
2268/* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
2269 *
2270 * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
2271 * it can pass through stack. So, the following predicate verifies that
2272 * this segment is not used for anything but congestion avoidance or
2273 * fast retransmit. Moreover, we even are able to eliminate most of such
2274 * second order effects, if we apply some small "replay" window (~RTO)
2275 * to timestamp space.
2276 *
2277 * All these measures still do not guarantee that we reject wrapped ACKs
2278 * on networks with high bandwidth, when sequence space is recycled fastly,
2279 * but it guarantees that such events will be very rare and do not affect
2280 * connection seriously. This doesn't look nice, but alas, PAWS is really
2281 * buggy extension.
2282 *
2283 * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
2284 * states that events when retransmit arrives after original data are rare.
2285 * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
2286 * the biggest problem on large power networks even with minor reordering.
2287 * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
2288 * up to bandwidth of 18Gigabit/sec. 8) ]
2289 */
2290
2291static int tcp_disordered_ack(struct tcp_opt *tp, struct sk_buff *skb)
2292{
2293        struct tcphdr *th = skb->h.th;
2294        u32 seq = TCP_SKB_CB(skb)->seq;
2295        u32 ack = TCP_SKB_CB(skb)->ack_seq;
2296
2297        return (/* 1. Pure ACK with correct sequence number. */
2298                (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
2299
2300                /* 2. ... and duplicate ACK. */
2301                ack == tp->snd_una &&
2302
2303                /* 3. ... and does not update window. */
2304                !tcp_may_update_window(tp, ack, seq, ntohs(th->window)<<tp->snd_wscale) &&
2305
2306                /* 4. ... and sits in replay window. */
2307                (s32)(tp->ts_recent - tp->rcv_tsval) <= (tp->rto*1024)/HZ);
2308}
2309
2310static __inline__ int tcp_paws_discard(struct tcp_opt *tp, struct sk_buff *skb)
2311{
2312        return ((s32)(tp->ts_recent - tp->rcv_tsval) > TCP_PAWS_WINDOW &&
2313                xtime.tv_sec < tp->ts_recent_stamp + TCP_PAWS_24DAYS &&
2314                !tcp_disordered_ack(tp, skb));
2315}
2316
2317/* Check segment sequence number for validity.
2318 *
2319 * Segment controls are considered valid, if the segment
2320 * fits to the window after truncation to the window. Acceptability
2321 * of data (and SYN, FIN, of course) is checked separately.
2322 * See tcp_data_queue(), for example.
2323 *
2324 * Also, controls (RST is main one) are accepted using RCV.WUP instead
2325 * of RCV.NXT. Peer still did not advance his SND.UNA when we
2326 * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
2327 * (borrowed from freebsd)
2328 */
2329
2330static inline int tcp_sequence(struct tcp_opt *tp, u32 seq, u32 end_seq)
2331{
2332        return  !before(end_seq, tp->rcv_wup) &&
2333                !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
2334}
2335
2336/* When we get a reset we do this. */
2337static void tcp_reset(struct sock *sk)
2338{
2339        /* We want the right error as BSD sees it (and indeed as we do). */
2340        switch (sk->sk_state) {
2341                case TCP_SYN_SENT:
2342                        sk->sk_err = ECONNREFUSED;
2343                        break;
2344                case TCP_CLOSE_WAIT:
2345                        sk->sk_err = EPIPE;
2346                        break;
2347                case TCP_CLOSE:
2348                        return;
2349                default:
2350                        sk->sk_err = ECONNRESET;
2351        }
2352
2353        if (!sock_flag(sk, SOCK_DEAD))
2354                sk->sk_error_report(sk);
2355
2356        tcp_done(sk);
2357}
2358
2359/*
2360 *      Process the FIN bit. This now behaves as it is supposed to work
2361 *      and the FIN takes effect when it is validly part of sequence
2362 *      space. Not before when we get holes.
2363 *
2364 *      If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
2365 *      (and thence onto LAST-ACK and finally, CLOSE, we never enter
2366 *      TIME-WAIT)
2367 *
2368 *      If we are in FINWAIT-1, a received FIN indicates simultaneous
2369 *      close and we go into CLOSING (and later onto TIME-WAIT)
2370 *
2371 *      If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
2372 */
2373static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th)
2374{
2375        struct tcp_opt *tp = tcp_sk(sk);
2376
2377        tcp_schedule_ack(tp);
2378
2379        sk->sk_shutdown |= RCV_SHUTDOWN;
2380        sock_set_flag(sk, SOCK_DONE);
2381
2382        switch (sk->sk_state) {
2383                case TCP_SYN_RECV:
2384                case TCP_ESTABLISHED:
2385                        /* Move to CLOSE_WAIT */
2386                        tcp_set_state(sk, TCP_CLOSE_WAIT);
2387                        tp->ack.pingpong = 1;
2388                        break;
2389
2390                case TCP_CLOSE_WAIT:
2391                case TCP_CLOSING:
2392                        /* Received a retransmission of the FIN, do
2393                         * nothing.
2394                         */
2395                        break;
2396                case TCP_LAST_ACK:
2397                        /* RFC793: Remain in the LAST-ACK state. */
2398                        break;
2399
2400                case TCP_FIN_WAIT1:
2401                        /* This case occurs when a simultaneous close
2402                         * happens, we must ack the received FIN and
2403                         * enter the CLOSING state.
2404                         */
2405                        tcp_send_ack(sk);
2406                        tcp_set_state(sk, TCP_CLOSING);
2407                        break;
2408                case TCP_FIN_WAIT2:
2409                        /* Received a FIN -- send ACK and enter TIME_WAIT. */
2410                        tcp_send_ack(sk);
2411                        tcp_time_wait(sk, TCP_TIME_WAIT, 0);
2412                        break;
2413                default:
2414                        /* Only TCP_LISTEN and TCP_CLOSE are left, in these
2415                         * cases we should never reach this piece of code.
2416                         */
2417                        printk(KERN_ERR "%s: Impossible, sk->sk_state=%d\n",
2418                               __FUNCTION__, sk->sk_state);
2419                        break;
2420        };
2421
2422        /* It _is_ possible, that we have something out-of-order _after_ FIN.
2423         * Probably, we should reset in this case. For now drop them.
2424         */
2425        __skb_queue_purge(&tp->out_of_order_queue);
2426        if (tp->sack_ok)
2427                tcp_sack_reset(tp);
2428        tcp_mem_reclaim(sk);
2429
2430        if (!sock_flag(sk, SOCK_DEAD)) {
2431                sk->sk_state_change(sk);
2432
2433                /* Do not send POLL_HUP for half duplex close. */
2434                if (sk->sk_shutdown == SHUTDOWN_MASK ||
2435                    sk->sk_state == TCP_CLOSE)
2436                        sk_wake_async(sk, 1, POLL_HUP);
2437                else
2438                        sk_wake_async(sk, 1, POLL_IN);
2439        }
2440}
2441
2442static __inline__ int
2443tcp_sack_extend(struct tcp_sack_block *sp, u32 seq, u32 end_seq)
2444{
2445        if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
2446                if (before(seq, sp->start_seq))
2447                        sp->start_seq = seq;
2448                if (after(end_seq, sp->end_seq))
2449                        sp->end_seq = end_seq;
2450                return 1;
2451        }
2452        return 0;
2453}
2454
2455static __inline__ void tcp_dsack_set(struct tcp_opt *tp, u32 seq, u32 end_seq)
2456{
2457        if (tp->sack_ok && sysctl_tcp_dsack) {
2458                if (before(seq, tp->rcv_nxt))
2459                        NET_INC_STATS_BH(TCPDSACKOldSent);
2460                else
2461                        NET_INC_STATS_BH(TCPDSACKOfoSent);
2462
2463                tp->dsack = 1;
2464                tp->duplicate_sack[0].start_seq = seq;
2465                tp->duplicate_sack[0].end_seq = end_seq;
2466                tp->eff_sacks = min(tp->num_sacks+1, 4-tp->tstamp_ok);
2467        }
2468}
2469
2470static __inline__ void tcp_dsack_extend(struct tcp_opt *tp, u32 seq, u32 end_seq)
2471{
2472        if (!tp->dsack)
2473                tcp_dsack_set(tp, seq, end_seq);
2474        else
2475                tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
2476}
2477
2478static void tcp_send_dupack(struct sock *sk, struct sk_buff *skb)
2479{
2480        struct tcp_opt *tp = tcp_sk(sk);
2481
2482        if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
2483            before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
2484                NET_INC_STATS_BH(DelayedACKLost);
2485                tcp_enter_quickack_mode(tp);
2486
2487                if (tp->sack_ok && sysctl_tcp_dsack) {
2488                        u32 end_seq = TCP_SKB_CB(skb)->end_seq;
2489
2490                        if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
2491                                end_seq = tp->rcv_nxt;
2492                        tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, end_seq);
2493                }
2494        }
2495
2496        tcp_send_ack(sk);
2497}
2498
2499/* These routines update the SACK block as out-of-order packets arrive or
2500 * in-order packets close up the sequence space.
2501 */
2502static void tcp_sack_maybe_coalesce(struct tcp_opt *tp)
2503{
2504        int this_sack;
2505        struct tcp_sack_block *sp = &tp->selective_acks[0];
2506        struct tcp_sack_block *swalk = sp+1;
2507
2508        /* See if the recent change to the first SACK eats into
2509         * or hits the sequence space of other SACK blocks, if so coalesce.
2510         */
2511        for (this_sack = 1; this_sack < tp->num_sacks; ) {
2512                if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
2513                        int i;
2514
2515                        /* Zap SWALK, by moving every further SACK up by one slot.
2516                         * Decrease num_sacks.
2517                         */
2518                        tp->num_sacks--;
2519                        tp->eff_sacks = min(tp->num_sacks+tp->dsack, 4-tp->tstamp_ok);
2520                        for(i=this_sack; i < tp->num_sacks; i++)
2521                                sp[i] = sp[i+1];
2522                        continue;
2523                }
2524                this_sack++, swalk++;
2525        }
2526}
2527
2528static __inline__ void tcp_sack_swap(struct tcp_sack_block *sack1, struct tcp_sack_block *sack2)
2529{
2530        __u32 tmp;
2531
2532        tmp = sack1->start_seq;
2533        sack1->start_seq = sack2->start_seq;
2534        sack2->start_seq = tmp;
2535
2536        tmp = sack1->end_seq;
2537        sack1->end_seq = sack2->end_seq;
2538        sack2->end_seq = tmp;
2539}
2540
2541static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
2542{
2543        struct tcp_opt *tp = tcp_sk(sk);
2544        struct tcp_sack_block *sp = &tp->selective_acks[0];
2545        int cur_sacks = tp->num_sacks;
2546        int this_sack;
2547
2548        if (!cur_sacks)
2549                goto new_sack;
2550
2551        for (this_sack=0; this_sack<cur_sacks; this_sack++, sp++) {
2552                if (tcp_sack_extend(sp, seq, end_seq)) {
2553                        /* Rotate this_sack to the first one. */
2554                        for (; this_sack>0; this_sack--, sp--)
2555                                tcp_sack_swap(sp, sp-1);
2556                        if (cur_sacks > 1)
2557                                tcp_sack_maybe_coalesce(tp);
2558                        return;
2559                }
2560        }
2561
2562        /* Could not find an adjacent existing SACK, build a new one,
2563         * put it at the front, and shift everyone else down.  We
2564         * always know there is at least one SACK present already here.
2565         *
2566         * If the sack array is full, forget about the last one.
2567         */
2568        if (this_sack >= 4) {
2569                this_sack--;
2570                tp->num_sacks--;
2571                sp--;
2572        }
2573        for(; this_sack > 0; this_sack--, sp--)
2574                *sp = *(sp-1);
2575
2576new_sack:
2577        /* Build the new head SACK, and we're done. */
2578        sp->start_seq = seq;
2579        sp->end_seq = end_seq;
2580        tp->num_sacks++;
2581        tp->eff_sacks = min(tp->num_sacks + tp->dsack, 4 - tp->tstamp_ok);
2582}
2583
2584/* RCV.NXT advances, some SACKs should be eaten. */
2585
2586static void tcp_sack_remove(struct tcp_opt *tp)
2587{
2588        struct tcp_sack_block *sp = &tp->selective_acks[0];
2589        int num_sacks = tp->num_sacks;
2590        int this_sack;
2591
2592        /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
2593        if (skb_queue_len(&tp->out_of_order_queue) == 0) {
2594                tp->num_sacks = 0;
2595                tp->eff_sacks = tp->dsack;
2596                return;
2597        }
2598
2599        for(this_sack = 0; this_sack < num_sacks; ) {
2600                /* Check if the start of the sack is covered by RCV.NXT. */
2601                if (!before(tp->rcv_nxt, sp->start_seq)) {
2602                        int i;
2603
2604                        /* RCV.NXT must cover all the block! */
2605                        BUG_TRAP(!before(tp->rcv_nxt, sp->end_seq));
2606
2607                        /* Zap this SACK, by moving forward any other SACKS. */
2608                        for (i=this_sack+1; i < num_sacks; i++)
2609                                tp->selective_acks[i-1] = tp->selective_acks[i];
2610                        num_sacks--;
2611                        continue;
2612                }
2613                this_sack++;
2614                sp++;
2615        }
2616        if (num_sacks != tp->num_sacks) {
2617                tp->num_sacks = num_sacks;
2618                tp->eff_sacks = min(tp->num_sacks+tp->dsack, 4-tp->tstamp_ok);
2619        }
2620}
2621
2622/* This one checks to see if we can put data from the
2623 * out_of_order queue into the receive_queue.
2624 */
2625static void tcp_ofo_queue(struct sock *sk)
2626{
2627        struct tcp_opt *tp = tcp_sk(sk);
2628        __u32 dsack_high = tp->rcv_nxt;
2629        struct sk_buff *skb;
2630
2631        while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) {
2632                if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
2633                        break;
2634
2635                if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
2636                        __u32 dsack = dsack_high;
2637                        if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
2638                                dsack_high = TCP_SKB_CB(skb)->end_seq;
2639                        tcp_dsack_extend(tp, TCP_SKB_CB(skb)->seq, dsack);
2640                }
2641
2642                if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
2643                        SOCK_DEBUG(sk, "ofo packet was already received \n");
2644                        __skb_unlink(skb, skb->list);
2645                        __kfree_skb(skb);
2646                        continue;
2647                }
2648                SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
2649                           tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
2650                           TCP_SKB_CB(skb)->end_seq);
2651
2652                __skb_unlink(skb, skb->list);
2653                __skb_queue_tail(&sk->sk_receive_queue, skb);
2654                tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
2655                if(skb->h.th->fin)
2656                        tcp_fin(skb, sk, skb->h.th);
2657        }
2658}
2659
2660static inline int tcp_rmem_schedule(struct sock *sk, struct sk_buff *skb)
2661{
2662        return (int)skb->truesize <= sk->sk_forward_alloc ||
2663                tcp_mem_schedule(sk, skb->truesize, 1);
2664}
2665
2666static int tcp_prune_queue(struct sock *sk);
2667
2668static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
2669{
2670        struct tcphdr *th = skb->h.th;
2671        struct tcp_opt *tp = tcp_sk(sk);
2672        int eaten = -1;
2673
2674        if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq)
2675                goto drop;
2676
2677        th = skb->h.th;
2678        __skb_pull(skb, th->doff*4);
2679
2680        TCP_ECN_accept_cwr(tp, skb);
2681
2682        if (tp->dsack) {
2683                tp->dsack = 0;
2684                tp->eff_sacks = min_t(unsigned int, tp->num_sacks,
2685                                                    4 - tp->tstamp_ok);
2686        }
2687
2688        /*  Queue data for delivery to the user.
2689         *  Packets in sequence go to the receive queue.
2690         *  Out of sequence packets to the out_of_order_queue.
2691         */
2692        if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
2693                if (tcp_receive_window(tp) == 0)
2694                        goto out_of_window;
2695
2696                /* Ok. In sequence. In window. */
2697                if (tp->ucopy.task == current &&
2698                    tp->copied_seq == tp->rcv_nxt && tp->ucopy.len &&
2699                    sock_owned_by_user(sk) && !tp->urg_data) {
2700                        int chunk = min_t(unsigned int, skb->len,
2701                                                        tp->ucopy.len);
2702
2703                        __set_current_state(TASK_RUNNING);
2704
2705                        local_bh_enable();
2706                        if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) {
2707                                tp->ucopy.len -= chunk;
2708                                tp->copied_seq += chunk;
2709                                eaten = (chunk == skb->len && !th->fin);
2710                        }
2711                        local_bh_disable();
2712                }
2713
2714                if (eaten <= 0) {
2715queue_and_out:
2716                        if (eaten < 0 &&
2717                            (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
2718                             !tcp_rmem_schedule(sk, skb))) {
2719                                if (tcp_prune_queue(sk) < 0 || !tcp_rmem_schedule(sk, skb))
2720                                        goto drop;
2721                        }
2722                        tcp_set_owner_r(skb, sk);
2723                        __skb_queue_tail(&sk->sk_receive_queue, skb);
2724                }
2725                tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
2726                if(skb->len)
2727                        tcp_event_data_recv(sk, tp, skb);
2728                if(th->fin)
2729                        tcp_fin(skb, sk, th);
2730
2731                if (skb_queue_len(&tp->out_of_order_queue)) {
2732                        tcp_ofo_queue(sk);
2733
2734                        /* RFC2581. 4.2. SHOULD send immediate ACK, when
2735                         * gap in queue is filled.
2736                         */
2737                        if (!skb_queue_len(&tp->out_of_order_queue))
2738                                tp->ack.pingpong = 0;
2739                }
2740
2741                if (tp->num_sacks)
2742                        tcp_sack_remove(tp);
2743
2744                tcp_fast_path_check(sk, tp);
2745
2746                if (eaten > 0)
2747                        __kfree_skb(skb);
2748                else if (!sock_flag(sk, SOCK_DEAD))
2749                        sk->sk_data_ready(sk, 0);
2750                return;
2751        }
2752
2753        if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
2754                /* A retransmit, 2nd most common case.  Force an immediate ack. */
2755                NET_INC_STATS_BH(DelayedACKLost);
2756                tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
2757
2758out_of_window:
2759                tcp_enter_quickack_mode(tp);
2760                tcp_schedule_ack(tp);
2761drop:
2762                __kfree_skb(skb);
2763                return;
2764        }
2765
2766        /* Out of window. F.e. zero window probe. */
2767        if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
2768                goto out_of_window;
2769
2770        tcp_enter_quickack_mode(tp);
2771
2772        if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
2773                /* Partial packet, seq < rcv_next < end_seq */
2774                SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
2775                           tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
2776                           TCP_SKB_CB(skb)->end_seq);
2777
2778                tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
2779                
2780                /* If window is closed, drop tail of packet. But after
2781                 * remembering D-SACK for its head made in previous line.
2782                 */
2783                if (!tcp_receive_window(tp))
2784                        goto out_of_window;
2785                goto queue_and_out;
2786        }
2787
2788        TCP_ECN_check_ce(tp, skb);
2789
2790        if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
2791            !tcp_rmem_schedule(sk, skb)) {
2792                if (tcp_prune_queue(sk) < 0 || !tcp_rmem_schedule(sk, skb))
2793                        goto drop;
2794        }
2795
2796        /* Disable header prediction. */
2797        tp->pred_flags = 0;
2798        tcp_schedule_ack(tp);
2799
2800        SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
2801                   tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
2802
2803        tcp_set_owner_r(skb, sk);
2804
2805        if (!skb_peek(&tp->out_of_order_queue)) {
2806                /* Initial out of order segment, build 1 SACK. */
2807                if (tp->sack_ok) {
2808                        tp->num_sacks = 1;
2809                        tp->dsack     = 0;
2810                        tp->eff_sacks = 1;
2811                        tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq;
2812                        tp->selective_acks[0].end_seq =
2813                                                TCP_SKB_CB(skb)->end_seq;
2814                }
2815                __skb_queue_head(&tp->out_of_order_queue,skb);
2816        } else {
2817                struct sk_buff *skb1 = tp->out_of_order_queue.prev;
2818                u32 seq = TCP_SKB_CB(skb)->seq;
2819                u32 end_seq = TCP_SKB_CB(skb)->end_seq;
2820
2821                if (seq == TCP_SKB_CB(skb1)->end_seq) {
2822                        __skb_append(skb1, skb);
2823
2824                        if (!tp->num_sacks ||
2825                            tp->selective_acks[0].end_seq != seq)
2826                                goto add_sack;
2827
2828                        /* Common case: data arrive in order after hole. */
2829                        tp->selective_acks[0].end_seq = end_seq;
2830                        return;
2831                }
2832
2833                /* Find place to insert this segment. */
2834                do {
2835                        if (!after(TCP_SKB_CB(skb1)->seq, seq))
2836                                break;
2837                } while ((skb1 = skb1->prev) !=
2838                         (struct sk_buff*)&tp->out_of_order_queue);
2839
2840                /* Do skb overlap to previous one? */
2841                if (skb1 != (struct sk_buff*)&tp->out_of_order_queue &&
2842                    before(seq, TCP_SKB_CB(skb1)->end_seq)) {
2843                        if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
2844                                /* All the bits are present. Drop. */
2845                                __kfree_skb(skb);
2846                                tcp_dsack_set(tp, seq, end_seq);
2847                                goto add_sack;
2848                        }
2849                        if (after(seq, TCP_SKB_CB(skb1)->seq)) {
2850                                /* Partial overlap. */
2851                                tcp_dsack_set(tp, seq, TCP_SKB_CB(skb1)->end_seq);
2852                        } else {
2853                                skb1 = skb1->prev;
2854                        }
2855                }
2856                __skb_insert(skb, skb1, skb1->next, &tp->out_of_order_queue);
2857                
2858                /* And clean segments covered by new one as whole. */
2859                while ((skb1 = skb->next) !=
2860                       (struct sk_buff*)&tp->out_of_order_queue &&
2861                       after(end_seq, TCP_SKB_CB(skb1)->seq)) {
2862                       if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
2863                               tcp_dsack_extend(tp, TCP_SKB_CB(skb1)->seq, end_seq);
2864                               break;
2865                       }
2866                       __skb_unlink(skb1, skb1->list);
2867                       tcp_dsack_extend(tp, TCP_SKB_CB(skb1)->seq, TCP_SKB_CB(skb1)->end_seq);
2868                       __kfree_skb(skb1);
2869                }
2870
2871add_sack:
2872                if (tp->sack_ok)
2873                        tcp_sack_new_ofo_skb(sk, seq, end_seq);
2874        }
2875}
2876
2877/* Collapse contiguous sequence of skbs head..tail with
2878 * sequence numbers start..end.
2879 * Segments with FIN/SYN are not collapsed (only because this
2880 * simplifies code)
2881 */
2882static void
2883tcp_collapse(struct sock *sk, struct sk_buff *head,
2884             struct sk_buff *tail, u32 start, u32 end)
2885{
2886        struct sk_buff *skb;
2887
2888        /* First, check that queue is collapsable and find
2889         * the point where collapsing can be useful. */
2890        for (skb = head; skb != tail; ) {
2891                /* No new bits? It is possible on ofo queue. */
2892                if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
2893                        struct sk_buff *next = skb->next;
2894                        __skb_unlink(skb, skb->list);
2895                        __kfree_skb(skb);
2896                        NET_INC_STATS_BH(TCPRcvCollapsed);
2897                        skb = next;
2898                        continue;
2899                }
2900
2901                /* The first skb to collapse is:
2902                 * - not SYN/FIN and
2903                 * - bloated or contains data before "start" or
2904                 *   overlaps to the next one.
2905                 */
2906                if (!skb->h.th->syn && !skb->h.th->fin &&
2907                    (tcp_win_from_space(skb->truesize) > skb->len ||
2908                     before(TCP_SKB_CB(skb)->seq, start) ||
2909                     (skb->next != tail &&
2910                      TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb->next)->seq)))
2911                        break;
2912
2913                /* Decided to skip this, advance start seq. */
2914                start = TCP_SKB_CB(skb)->end_seq;
2915                skb = skb->next;
2916        }
2917        if (skb == tail || skb->h.th->syn || skb->h.th->fin)
2918                return;
2919
2920        while (before(start, end)) {
2921                struct sk_buff *nskb;
2922                int header = skb_headroom(skb);
2923                int copy = (PAGE_SIZE - sizeof(struct sk_buff) -
2924                            sizeof(struct skb_shared_info) - header - 31)&~15;
2925
2926                /* Too big header? This can happen with IPv6. */
2927                if (copy < 0)
2928                        return;
2929                if (end-start < copy)
2930                        copy = end-start;
2931                nskb = alloc_skb(copy+header, GFP_ATOMIC);
2932                if (!nskb)
2933                        return;
2934                skb_reserve(nskb, header);
2935                memcpy(nskb->head, skb->head, header);
2936                nskb->nh.raw = nskb->head + (skb->nh.raw-skb->head);
2937                nskb->h.raw = nskb->head + (skb->h.raw-skb->head);
2938                nskb->mac.raw = nskb->head + (skb->mac.raw-skb->head);
2939                memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2940                TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
2941                __skb_insert(nskb, skb->prev, skb, skb->list);
2942                tcp_set_owner_r(nskb, sk);
2943
2944                /* Copy data, releasing collapsed skbs. */
2945                while (copy > 0) {
2946                        int offset = start - TCP_SKB_CB(skb)->seq;
2947                        int size = TCP_SKB_CB(skb)->end_seq - start;
2948
2949                        if (offset < 0) BUG();
2950                        if (size > 0) {
2951                                size = min(copy, size);
2952                                if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
2953                                        BUG();
2954                                TCP_SKB_CB(nskb)->end_seq += size;
2955                                copy -= size;
2956                                start += size;
2957                        }
2958                        if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
2959                                struct sk_buff *next = skb->next;
2960                                __skb_unlink(skb, skb->list);
2961                                __kfree_skb(skb);
2962                                NET_INC_STATS_BH(TCPRcvCollapsed);
2963                                skb = next;
2964                                if (skb == tail || skb->h.th->syn || skb->h.th->fin)
2965                                        return;
2966                        }
2967                }
2968        }
2969}
2970
2971/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
2972 * and tcp_collapse() them until all the queue is collapsed.
2973 */
2974static void tcp_collapse_ofo_queue(struct sock *sk)
2975{
2976        struct tcp_opt *tp = tcp_sk(sk);
2977        struct sk_buff *skb = skb_peek(&tp->out_of_order_queue);
2978        struct sk_buff *head;
2979        u32 start, end;
2980
2981        if (skb == NULL)
2982                return;
2983
2984        start = TCP_SKB_CB(skb)->seq;
2985        end = TCP_SKB_CB(skb)->end_seq;
2986        head = skb;
2987
2988        for (;;) {
2989                skb = skb->next;
2990
2991                /* Segment is terminated when we see gap or when
2992                 * we are at the end of all the queue. */
2993                if (skb == (struct sk_buff *)&tp->out_of_order_queue ||
2994                    after(TCP_SKB_CB(skb)->seq, end) ||
2995                    before(TCP_SKB_CB(skb)->end_seq, start)) {
2996                        tcp_collapse(sk, head, skb, start, end);
2997                        head = skb;
2998                        if (skb == (struct sk_buff *)&tp->out_of_order_queue)
2999                                break;
3000                        /* Start new segment */
3001                        start = TCP_SKB_CB(skb)->seq;
3002                        end = TCP_SKB_CB(skb)->end_seq;
3003                } else {
3004                        if (before(TCP_SKB_CB(skb)->seq, start))
3005                                start = TCP_SKB_CB(skb)->seq;
3006                        if (after(TCP_SKB_CB(skb)->end_seq, end))
3007                                end = TCP_SKB_CB(skb)->end_seq;
3008                }
3009        }
3010}
3011
3012/* Reduce allocated memory if we can, trying to get
3013 * the socket within its memory limits again.
3014 *
3015 * Return less than zero if we should start dropping frames
3016 * until the socket owning process reads some of the data
3017 * to stabilize the situation.
3018 */
3019static int tcp_prune_queue(struct sock *sk)
3020{
3021        struct tcp_opt *tp = tcp_sk(sk); 
3022
3023        SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
3024
3025        NET_INC_STATS_BH(PruneCalled);
3026
3027        if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
3028                tcp_clamp_window(sk, tp);
3029        else if (tcp_memory_pressure)
3030                tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
3031
3032        tcp_collapse_ofo_queue(sk);
3033        tcp_collapse(sk, sk->sk_receive_queue.next,
3034                     (struct sk_buff*)&sk->sk_receive_queue,
3035                     tp->copied_seq, tp->rcv_nxt);
3036        tcp_mem_reclaim(sk);
3037
3038        if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3039                return 0;
3040
3041        /* Collapsing did not help, destructive actions follow.
3042         * This must not ever occur. */
3043
3044        /* First, purge the out_of_order queue. */
3045        if (skb_queue_len(&tp->out_of_order_queue)) {
3046                NET_ADD_STATS_BH(OfoPruned,
3047                                 skb_queue_len(&tp->out_of_order_queue));
3048                __skb_queue_purge(&tp->out_of_order_queue);
3049
3050                /* Reset SACK state.  A conforming SACK implementation will
3051                 * do the same at a timeout based retransmit.  When a connection
3052                 * is in a sad state like this, we care only about integrity
3053                 * of the connection not performance.
3054                 */
3055                if (tp->sack_ok)
3056                        tcp_sack_reset(tp);
3057                tcp_mem_reclaim(sk);
3058        }
3059
3060        if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3061                return 0;
3062
3063        /* If we are really being abused, tell the caller to silently
3064         * drop receive data on the floor.  It will get retransmitted
3065         * and hopefully then we'll have sufficient space.
3066         */
3067        NET_INC_STATS_BH(RcvPruned);
3068
3069        /* Massive buffer overcommit. */
3070        tp->pred_flags = 0;
3071        return -1;
3072}
3073
3074
3075/* RFC2861, slow part. Adjust cwnd, after it was not full during one rto.
3076 * As additional protections, we do not touch cwnd in retransmission phases,
3077 * and if application hit its sndbuf limit recently.
3078 */
3079void tcp_cwnd_application_limited(struct sock *sk)
3080{
3081        struct tcp_opt *tp = tcp_sk(sk);
3082
3083        if (tp->ca_state == TCP_CA_Open &&
3084            sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
3085                /* Limited by application or receiver window. */
3086                u32 win_used = max(tp->snd_cwnd_used, 2U);
3087                if (win_used < tp->snd_cwnd) {
3088                        tp->snd_ssthresh = tcp_current_ssthresh(tp);
3089                        tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1;
3090                }
3091                tp->snd_cwnd_used = 0;
3092        }
3093        tp->snd_cwnd_stamp = tcp_time_stamp;
3094}
3095
3096
3097/* When incoming ACK allowed to free some skb from write_queue,
3098 * we remember this event in flag tp->queue_shrunk and wake up socket
3099 * on the exit from tcp input handler.
3100 *
3101 * PROBLEM: sndbuf expansion does not work well with largesend.
3102 */
3103static void tcp_new_space(struct sock *sk)
3104{
3105        struct tcp_opt *tp = tcp_sk(sk);
3106
3107        if (tp->packets_out < tp->snd_cwnd &&
3108            !(sk->sk_userlocks & SOCK_SNDBUF_LOCK) &&
3109            !tcp_memory_pressure &&
3110            atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) {
3111                int sndmem = max_t(u32, tp->mss_clamp, tp->mss_cache) +
3112                        MAX_TCP_HEADER + 16 + sizeof(struct sk_buff),
3113                    demanded = max_t(unsigned int, tp->snd_cwnd,
3114                                                   tp->reordering + 1);
3115                sndmem *= 2*demanded;
3116                if (sndmem > sk->sk_sndbuf)
3117                        sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
3118                tp->snd_cwnd_stamp = tcp_time_stamp;
3119        }
3120
3121        sk->sk_write_space(sk);
3122}
3123
3124static inline void tcp_check_space(struct sock *sk)
3125{
3126        struct tcp_opt *tp = tcp_sk(sk);
3127
3128        if (tp->queue_shrunk) {
3129                tp->queue_shrunk = 0;
3130                if (sk->sk_socket &&
3131                    test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
3132                        tcp_new_space(sk);
3133        }
3134}
3135
3136static void __tcp_data_snd_check(struct sock *sk, struct sk_buff *skb)
3137{
3138        struct tcp_opt *tp = tcp_sk(sk);
3139
3140        if (after(TCP_SKB_CB(skb)->end_seq, tp->snd_una + tp->snd_wnd) ||
3141            tcp_packets_in_flight(tp) >= tp->snd_cwnd ||
3142            tcp_write_xmit(sk, tp->nonagle))
3143                tcp_check_probe_timer(sk, tp);
3144}
3145
3146static __inline__ void tcp_data_snd_check(struct sock *sk)
3147{
3148        struct tcp_opt *tp = tcp_sk(sk);
3149        struct sk_buff *skb = tp->send_head;
3150
3151        if (skb != NULL)
3152                __tcp_data_snd_check(sk, skb);
3153        tcp_check_space(sk);
3154}
3155
3156/*
3157 * Check if sending an ack is needed.
3158 */
3159static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
3160{
3161        struct tcp_opt *tp = tcp_sk(sk);
3162
3163            /* More than one full frame received... */
3164        if (((tp->rcv_nxt - tp->rcv_wup) > tp->ack.rcv_mss
3165             /* ... and right edge of window advances far enough.
3166              * (tcp_recvmsg() will send ACK otherwise). Or...
3167              */
3168             && __tcp_select_window(sk) >= tp->rcv_wnd) ||
3169            /* We ACK each frame or... */
3170            tcp_in_quickack_mode(tp) ||
3171            /* We have out of order data. */
3172            (ofo_possible &&
3173             skb_peek(&tp->out_of_order_queue))) {
3174                /* Then ack it now */
3175                tcp_send_ack(sk);
3176        } else {
3177                /* Else, send delayed ack. */
3178                tcp_send_delayed_ack(sk);
3179        }
3180}
3181
3182static __inline__ void tcp_ack_snd_check(struct sock *sk)
3183{
3184        struct tcp_opt *tp = tcp_sk(sk);
3185        if (!tcp_ack_scheduled(tp)) {
3186                /* We sent a data segment already. */
3187                return;
3188        }
3189        __tcp_ack_snd_check(sk, 1);
3190}
3191
3192/*
3193 *      This routine is only called when we have urgent data
3194 *      signalled. Its the 'slow' part of tcp_urg. It could be
3195 *      moved inline now as tcp_urg is only called from one
3196 *      place. We handle URGent data wrong. We have to - as
3197 *      BSD still doesn't use the correction from RFC961.
3198 *      For 1003.1g we should support a new option TCP_STDURG to permit
3199 *      either form (or just set the sysctl tcp_stdurg).
3200 */
3201 
3202static void tcp_check_urg(struct sock * sk, struct tcphdr * th)
3203{
3204        struct tcp_opt *tp = tcp_sk(sk);
3205        u32 ptr = ntohs(th->urg_ptr);
3206
3207        if (ptr && !sysctl_tcp_stdurg)
3208                ptr--;
3209        ptr += ntohl(th->seq);
3210
3211        /* Ignore urgent data that we've already seen and read. */
3212        if (after(tp->copied_seq, ptr))
3213                return;
3214
3215        /* Do not replay urg ptr.
3216         *
3217         * NOTE: interesting situation not covered by specs.
3218         * Misbehaving sender may send urg ptr, pointing to segment,
3219         * which we already have in ofo queue. We are not able to fetch
3220         * such data and will stay in TCP_URG_NOTYET until will be eaten
3221         * by recvmsg(). Seems, we are not obliged to handle such wicked
3222         * situations. But it is worth to think about possibility of some
3223         * DoSes using some hypothetical application level deadlock.
3224         */
3225        if (before(ptr, tp->rcv_nxt))
3226                return;
3227
3228        /* Do we already have a newer (or duplicate) urgent pointer? */
3229        if (tp->urg_data && !after(ptr, tp->urg_seq))
3230                return;
3231
3232        /* Tell the world about our new urgent pointer. */
3233        sk_send_sigurg(sk);
3234
3235        /* We may be adding urgent data when the last byte read was
3236         * urgent. To do this requires some care. We cannot just ignore
3237         * tp->copied_seq since we would read the last urgent byte again
3238         * as data, nor can we alter copied_seq until this data arrives
3239         * or we break the sematics of SIOCATMARK (and thus sockatmark())
3240         *
3241         * NOTE. Double Dutch. Rendering to plain English: author of comment
3242         * above did something sort of  send("A", MSG_OOB); send("B", MSG_OOB);
3243         * and expect that both A and B disappear from stream. This is _wrong_.
3244         * Though this happens in BSD with high probability, this is occasional.
3245         * Any application relying on this is buggy. Note also, that fix "works"
3246         * only in this artificial test. Insert some normal data between A and B and we will
3247         * decline of BSD again. Verdict: it is better to remove to trap
3248         * buggy users.
3249         */
3250        if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
3251            !sock_flag(sk, SOCK_URGINLINE) &&
3252            tp->copied_seq != tp->rcv_nxt) {
3253                struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
3254                tp->copied_seq++;
3255                if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
3256                        __skb_unlink(skb, skb->list);
3257                        __kfree_skb(skb);
3258                }
3259        }
3260
3261        tp->urg_data   = TCP_URG_NOTYET;
3262        tp->urg_seq    = ptr;
3263
3264        /* Disable header prediction. */
3265        tp->pred_flags = 0;
3266}
3267
3268/* This is the 'fast' part of urgent handling. */
3269static void tcp_urg(struct sock *sk, struct sk_buff *skb, struct tcphdr *th)
3270{
3271        struct tcp_opt *tp = tcp_sk(sk);
3272
3273        /* Check if we get a new urgent pointer - normally not. */
3274        if (th->urg)
3275                tcp_check_urg(sk,th);
3276
3277        /* Do we wait for any urgent data? - normally not... */
3278        if (tp->urg_data == TCP_URG_NOTYET) {
3279                u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
3280                          th->syn;
3281
3282                /* Is the urgent pointer pointing into this packet? */   
3283                if (ptr < skb->len) {
3284                        u8 tmp;
3285                        if (skb_copy_bits(skb, ptr, &tmp, 1))
3286                                BUG();
3287                        tp->urg_data = TCP_URG_VALID | tmp;
3288                        if (!sock_flag(sk, SOCK_DEAD))
3289                                sk->sk_data_ready(sk, 0);
3290                }
3291        }
3292}
3293
3294static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen)
3295{
3296        struct tcp_opt *tp = tcp_sk(sk);
3297        int chunk = skb->len - hlen;
3298        int err;
3299
3300        local_bh_enable();
3301        if (skb->ip_summed==CHECKSUM_UNNECESSARY)
3302                err = skb_copy_datagram_iovec(skb, hlen, tp->ucopy.iov, chunk);
3303        else
3304                err = skb_copy_and_csum_datagram_iovec(skb, hlen,
3305                                                       tp->ucopy.iov);
3306
3307        if (!err) {
3308                tp->ucopy.len -= chunk;
3309                tp->copied_seq += chunk;
3310        }
3311
3312        local_bh_disable();
3313        return err;
3314}
3315
3316static int __tcp_checksum_complete_user(struct sock *sk, struct sk_buff *skb)
3317{
3318        int result;
3319
3320        if (sock_owned_by_user(sk)) {
3321                local_bh_enable();
3322                result = __tcp_checksum_complete(skb);
3323                local_bh_disable();
3324        } else {
3325                result = __tcp_checksum_complete(skb);
3326        }
3327        return result;
3328}
3329
3330static __inline__ int
3331tcp_checksum_complete_user(struct sock *sk, struct sk_buff *skb)
3332{
3333        return skb->ip_summed != CHECKSUM_UNNECESSARY &&
3334                __tcp_checksum_complete_user(sk, skb);
3335}
3336
3337/*
3338 *      TCP receive function for the ESTABLISHED state. 
3339 *
3340 *      It is split into a fast path and a slow path. The fast path is 
3341 *      disabled when:
3342 *      - A zero window was announced from us - zero window probing
3343 *        is only handled properly in the slow path. 
3344 *      - Out of order segments arrived.
3345 *      - Urgent data is expected.
3346 *      - There is no buffer space left
3347 *      - Unexpected TCP flags/window values/header lengths are received
3348 *        (detected by checking the TCP header against pred_flags) 
3349 *      - Data is sent in both directions. Fast path only supports pure senders
3350 *        or pure receivers (this means either the sequence number or the ack
3351 *        value must stay constant)
3352 *      - Unexpected TCP option.
3353 *
3354 *      When these conditions are not satisfied it drops into a standard 
3355 *      receive procedure patterned after RFC793 to handle all cases.
3356 *      The first three cases are guaranteed by proper pred_flags setting,
3357 *      the rest is checked inline. Fast processing is turned on in 
3358 *      tcp_data_queue when everything is OK.
3359 */
3360int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
3361                        struct tcphdr *th, unsigned len)
3362{
3363        struct tcp_opt *tp = tcp_sk(sk);
3364
3365        /*
3366         *      Header prediction.
3367         *      The code loosely follows the one in the famous 
3368         *      "30 instruction TCP receive" Van Jacobson mail.
3369         *      
3370         *      Van's trick is to deposit buffers into socket queue 
3371         *      on a device interrupt, to call tcp_recv function
3372         *      on the receive process context and checksum and copy
3373         *      the buffer to user space. smart...
3374         *
3375         *      Our current scheme is not silly either but we take the 
3376         *      extra cost of the net_bh soft interrupt processing...
3377         *      We do checksum and copy also but from device to kernel.
3378         */
3379
3380        tp->saw_tstamp = 0;
3381
3382        /*      pred_flags is 0xS?10 << 16 + snd_wnd
3383         *      if header_predition is to be made
3384         *      'S' will always be tp->tcp_header_len >> 2
3385         *      '?' will be 0 for the fast path, otherwise pred_flags is 0 to
3386         *  turn it off (when there are holes in the receive 
3387         *       space for instance)
3388         *      PSH flag is ignored.
3389         */
3390
3391        if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
3392                TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
3393                int tcp_header_len = tp->tcp_header_len;
3394
3395                /* Timestamp header prediction: tcp_header_len
3396                 * is automatically equal to th->doff*4 due to pred_flags
3397                 * match.
3398                 */
3399
3400                /* Check timestamp */
3401                if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
3402                        __u32 *ptr = (__u32 *)(th + 1);
3403
3404                        /* No? Slow path! */
3405                        if (*ptr != ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
3406                                          | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP))
3407                                goto slow_path;
3408
3409                        tp->saw_tstamp = 1;
3410                        ++ptr; 
3411                        tp->rcv_tsval = ntohl(*ptr);
3412                        ++ptr;
3413                        tp->rcv_tsecr = ntohl(*ptr);
3414
3415                        /* If PAWS failed, check it more carefully in slow path */
3416                        if ((s32)(tp->rcv_tsval - tp->ts_recent) < 0)
3417                                goto slow_path;
3418
3419                        /* DO NOT update ts_recent here, if checksum fails
3420                         * and timestamp was corrupted part, it will result
3421                         * in a hung connection since we will drop all
3422                         * future packets due to the PAWS test.
3423                         */
3424                }
3425
3426                if (len <= tcp_header_len) {
3427                        /* Bulk data transfer: sender */
3428                        if (len == tcp_header_len) {
3429                                /* Predicted packet is in window by definition.
3430                                 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
3431                                 * Hence, check seq<=rcv_wup reduces to:
3432                                 */
3433                                if (tcp_header_len ==
3434                                    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
3435                                    tp->rcv_nxt == tp->rcv_wup)
3436                                        tcp_store_ts_recent(tp);
3437                                /* We know that such packets are checksummed
3438                                 * on entry.
3439                                 */
3440                                tcp_ack(sk, skb, 0);
3441                                __kfree_skb(skb); 
3442                                tcp_data_snd_check(sk);
3443                                return 0;
3444                        } else { /* Header too small */
3445                                TCP_INC_STATS_BH(TcpInErrs);
3446                                goto discard;
3447                        }
3448                } else {
3449                        int eaten = 0;
3450
3451                        if (tp->ucopy.task == current &&
3452                            tp->copied_seq == tp->rcv_nxt &&
3453                            len - tcp_header_len <= tp->ucopy.len &&
3454                            sock_owned_by_user(sk)) {
3455                                __set_current_state(TASK_RUNNING);
3456
3457                                if (!tcp_copy_to_iovec(sk, skb, tcp_header_len)) {
3458                                        /* Predicted packet is in window by definition.
3459                                         * seq == rcv_nxt and rcv_wup <= rcv_nxt.
3460                                         * Hence, check seq<=rcv_wup reduces to:
3461                                         */
3462                                        if (tcp_header_len ==
3463                                            (sizeof(struct tcphdr) +
3464                                             TCPOLEN_TSTAMP_ALIGNED) &&
3465                                            tp->rcv_nxt == tp->rcv_wup)
3466                                                tcp_store_ts_recent(tp);
3467
3468                                        __skb_pull(skb, tcp_header_len);
3469                                        tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
3470                                        NET_INC_STATS_BH(TCPHPHitsToUser);
3471                                        eaten = 1;
3472                                }
3473                        }
3474                        if (!eaten) {
3475                                if (tcp_checksum_complete_user(sk, skb))
3476                                        goto csum_error;
3477
3478                                /* Predicted packet is in window by definition.
3479                                 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
3480                                 * Hence, check seq<=rcv_wup reduces to:
3481                                 */
3482                                if (tcp_header_len ==
3483                                    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
3484                                    tp->rcv_nxt == tp->rcv_wup)
3485                                        tcp_store_ts_recent(tp);
3486
3487                                if ((int)skb->truesize > sk->sk_forward_alloc)
3488                                        goto step5;
3489
3490                                NET_INC_STATS_BH(TCPHPHits);
3491
3492                                /* Bulk data transfer: receiver */
3493                                __skb_pull(skb,tcp_header_len);
3494                                __skb_queue_tail(&sk->sk_receive_queue, skb);
3495                                tcp_set_owner_r(skb, sk);
3496                                tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
3497                        }
3498
3499                        tcp_event_data_recv(sk, tp, skb);
3500
3501                        if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
3502                                /* Well, only one small jumplet in fast path... */
3503                                tcp_ack(sk, skb, FLAG_DATA);
3504                                tcp_data_snd_check(sk);
3505                                if (!tcp_ack_scheduled(tp))
3506                                        goto no_ack;
3507                        }
3508
3509                        if (eaten) {
3510                                if (tcp_in_quickack_mode(tp)) {
3511                                        tcp_send_ack(sk);
3512                                } else {
3513                                        tcp_send_delayed_ack(sk);
3514                                }
3515                        } else {
3516                                __tcp_ack_snd_check(sk, 0);
3517                        }
3518
3519no_ack:
3520                        if (eaten)
3521                                __kfree_skb(skb);
3522                        else
3523                                sk->sk_data_ready(sk, 0);
3524                        return 0;
3525                }
3526        }
3527
3528slow_path:
3529        if (len < (th->doff<<2) || tcp_checksum_complete_user(sk, skb))
3530                goto csum_error;
3531
3532        /*
3533         * RFC1323: H1. Apply PAWS check first.
3534         */
3535        if (tcp_fast_parse_options(skb, th, tp) && tp->saw_tstamp &&
3536            tcp_paws_discard(tp, skb)) {
3537                if (!th->rst) {
3538                        NET_INC_STATS_BH(PAWSEstabRejected);
3539                        tcp_send_dupack(sk, skb);
3540                        goto discard;
3541                }
3542                /* Resets are accepted even if PAWS failed.
3543
3544                   ts_recent update must be made after we are sure
3545                   that the packet is in window.
3546                 */
3547        }
3548
3549        /*
3550         *      Standard slow path.
3551         */
3552
3553        if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
3554                /* RFC793, page 37: "In all states except SYN-SENT, all reset
3555                 * (RST) segments are validated by checking their SEQ-fields."
3556                 * And page 69: "If an incoming segment is not acceptable,
3557                 * an acknowledgment should be sent in reply (unless the RST bit
3558                 * is set, if so drop the segment and return)".
3559                 */
3560                if (!th->rst)
3561                        tcp_send_dupack(sk, skb);
3562                goto discard;
3563        }
3564
3565        if(th->rst) {
3566                tcp_reset(sk);
3567                goto discard;
3568        }
3569
3570        tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
3571
3572        if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
3573                TCP_INC_STATS_BH(TcpInErrs);
3574                NET_INC_STATS_BH(TCPAbortOnSyn);
3575                tcp_reset(sk);
3576                return 1;
3577        }
3578
3579step5:
3580        if(th->ack)
3581                tcp_ack(sk, skb, FLAG_SLOWPATH);
3582
3583        /* Process urgent data. */
3584        tcp_urg(sk, skb, th);
3585
3586        /* step 7: process the segment text */
3587        tcp_data_queue(sk, skb);
3588
3589        tcp_data_snd_check(sk);
3590        tcp_ack_snd_check(sk);
3591        return 0;
3592
3593csum_error:
3594        TCP_INC_STATS_BH(TcpInErrs);
3595
3596discard:
3597        __kfree_skb(skb);
3598        return 0;
3599}
3600
3601static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
3602                                         struct tcphdr *th, unsigned len)
3603{
3604        struct tcp_opt *tp = tcp_sk(sk);
3605        int saved_clamp = tp->mss_clamp;
3606
3607        tcp_parse_options(skb, tp, 0);
3608
3609        if (th->ack) {
3610                /* rfc793:
3611                 * "If the state is SYN-SENT then
3612                 *    first check the ACK bit
3613                 *      If the ACK bit is set
3614                 *        If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
3615                 *        a reset (unless the RST bit is set, if so drop
3616                 *        the segment and return)"
3617                 *
3618                 *  We do not send data with SYN, so that RFC-correct
3619                 *  test reduces to:
3620                 */
3621                if (TCP_SKB_CB(skb)->ack_seq != tp->snd_nxt)
3622                        goto reset_and_undo;
3623
3624                if (tp->saw_tstamp && tp->rcv_tsecr &&
3625                    !between(tp->rcv_tsecr, tp->retrans_stamp,
3626                             tcp_time_stamp)) {
3627                        NET_INC_STATS_BH(PAWSActiveRejected);
3628                        goto reset_and_undo;
3629                }
3630
3631                /* Now ACK is acceptable.
3632                 *
3633                 * "If the RST bit is set
3634                 *    If the ACK was acceptable then signal the user "error:
3635                 *    connection reset", drop the segment, enter CLOSED state,
3636                 *    delete TCB, and return."
3637                 */
3638
3639                if (th->rst) {
3640                        tcp_reset(sk);
3641                        goto discard;
3642                }
3643
3644                /* rfc793:
3645                 *   "fifth, if neither of the SYN or RST bits is set then
3646                 *    drop the segment and return."
3647                 *
3648                 *    See note below!
3649                 *                                        --ANK(990513)
3650                 */
3651                if (!th->syn)
3652                        goto discard_and_undo;
3653
3654                /* rfc793:
3655                 *   "If the SYN bit is on ...
3656                 *    are acceptable then ...
3657                 *    (our SYN has been ACKed), change the connection
3658                 *    state to ESTABLISHED..."
3659                 */
3660
3661                TCP_ECN_rcv_synack(tp, th);
3662                if (tp->ecn_flags&TCP_ECN_OK)
3663                        sk->sk_no_largesend = 1;
3664
3665                tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
3666                tcp_ack(sk, skb, FLAG_SLOWPATH);
3667
3668                /* Ok.. it's good. Set up sequence numbers and
3669                 * move to established.
3670                 */
3671                tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
3672                tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
3673
3674                /* RFC1323: The window in SYN & SYN/ACK segments is
3675                 * never scaled.
3676                 */
3677                tp->snd_wnd = ntohs(th->window);
3678                tcp_init_wl(tp, TCP_SKB_CB(skb)->ack_seq, TCP_SKB_CB(skb)->seq);
3679
3680                if (!tp->wscale_ok) {
3681                        tp->snd_wscale = tp->rcv_wscale = 0;
3682                        tp->window_clamp = min(tp->window_clamp, 65535U);
3683                }
3684
3685                if (tp->saw_tstamp) {
3686                        tp->tstamp_ok      = 1;
3687                        tp->tcp_header_len =
3688                                sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
3689                        tp->advmss          -= TCPOLEN_TSTAMP_ALIGNED;
3690                        tcp_store_ts_recent(tp);
3691                } else {
3692                        tp->tcp_header_len = sizeof(struct tcphdr);
3693                }
3694
3695                if (tp->sack_ok && sysctl_tcp_fack)
3696                        tp->sack_ok |= 2;
3697
3698                tcp_sync_mss(sk, tp->pmtu_cookie);
3699                tcp_initialize_rcv_mss(sk);
3700
3701                /* Remember, tcp_poll() does not lock socket!
3702                 * Change state from SYN-SENT only after copied_seq
3703                 * is initialized. */
3704                tp->copied_seq = tp->rcv_nxt;
3705                mb();
3706                tcp_set_state(sk, TCP_ESTABLISHED);
3707
3708                /* Make sure socket is routed, for correct metrics.  */
3709                tp->af_specific->rebuild_header(sk);
3710
3711                tcp_init_metrics(sk);
3712
3713                /* Prevent spurious tcp_cwnd_restart() on first data
3714                 * packet.
3715                 */
3716                tp->lsndtime = tcp_time_stamp;
3717
3718                tcp_init_buffer_space(sk);
3719
3720                if (sock_flag(sk, SOCK_KEEPOPEN))
3721                        tcp_reset_keepalive_timer(sk, keepalive_time_when(tp));
3722
3723                if (!tp->snd_wscale)
3724                        __tcp_fast_path_on(tp, tp->snd_wnd);
3725                else
3726                        tp->pred_flags = 0;
3727
3728                if (!sock_flag(sk, SOCK_DEAD)) {
3729                        sk->sk_state_change(sk);
3730                        sk_wake_async(sk, 0, POLL_OUT);
3731                }
3732
3733                if (tp->write_pending || tp->defer_accept || tp->ack.pingpong) {
3734                        /* Save one ACK. Data will be ready after
3735                         * several ticks, if write_pending is set.
3736                         *
3737                         * It may be deleted, but with this feature tcpdumps
3738                         * look so _wonderfully_ clever, that I was not able
3739                         * to stand against the temptation 8)     --ANK
3740                         */
3741                        tcp_schedule_ack(tp);
3742                        tp->ack.lrcvtime = tcp_time_stamp;
3743                        tp->ack.ato      = TCP_ATO_MIN;
3744                        tcp_incr_quickack(tp);
3745                        tcp_enter_quickack_mode(tp);
3746                        tcp_reset_xmit_timer(sk, TCP_TIME_DACK, TCP_DELACK_MAX);
3747
3748discard:
3749                        __kfree_skb(skb);
3750                        return 0;
3751                } else {
3752                        tcp_send_ack(sk);
3753                }
3754                return -1;
3755        }
3756
3757        /* No ACK in the segment */
3758
3759        if (th->rst) {
3760                /* rfc793:
3761                 * "If the RST bit is set
3762                 *
3763                 *      Otherwise (no ACK) drop the segment and return."
3764                 */
3765
3766                goto discard_and_undo;
3767        }
3768
3769        /* PAWS check. */
3770        if (tp->ts_recent_stamp && tp->saw_tstamp && tcp_paws_check(tp, 0))
3771                goto discard_and_undo;
3772
3773        if (th->syn) {
3774                /* We see SYN without ACK. It is attempt of
3775                 * simultaneous connect with crossed SYNs.
3776                 * Particularly, it can be connect to self.
3777                 */
3778                tcp_set_state(sk, TCP_SYN_RECV);
3779
3780                if (tp->saw_tstamp) {
3781                        tp->tstamp_ok = 1;
3782                        tcp_store_ts_recent(tp);
3783                        tp->tcp_header_len =
3784                                sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
3785                } else {
3786                        tp->tcp_header_len = sizeof(struct tcphdr);
3787                }
3788
3789                tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
3790                tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
3791
3792                /* RFC1323: The window in SYN & SYN/ACK segments is
3793                 * never scaled.
3794                 */
3795                tp->snd_wnd    = ntohs(th->window);
3796                tp->snd_wl1    = TCP_SKB_CB(skb)->seq;
3797                tp->max_window = tp->snd_wnd;
3798
3799                TCP_ECN_rcv_syn(tp, th);
3800                if (tp->ecn_flags&TCP_ECN_OK)
3801                        sk->sk_no_largesend = 1;
3802
3803                tcp_sync_mss(sk, tp->pmtu_cookie);
3804                tcp_initialize_rcv_mss(sk);
3805
3806
3807                tcp_send_synack(sk);
3808#if 0
3809                /* Note, we could accept data and URG from this segment.
3810                 * There are no obstacles to make this.
3811                 *
3812                 * However, if we ignore data in ACKless segments sometimes,
3813                 * we have no reasons to accept it sometimes.
3814                 * Also, seems the code doing it in step6 of tcp_rcv_state_process
3815                 * is not flawless. So, discard packet for sanity.
3816                 * Uncomment this return to process the data.
3817                 */
3818                return -1;
3819#else
3820                goto discard;
3821#endif
3822        }
3823        /* "fifth, if neither of the SYN or RST bits is set then
3824         * drop the segment and return."
3825         */
3826
3827discard_and_undo:
3828        tcp_clear_options(tp);
3829        tp->mss_clamp = saved_clamp;
3830        goto discard;
3831
3832reset_and_undo:
3833        tcp_clear_options(tp);
3834        tp->mss_clamp = saved_clamp;
3835        return 1;
3836}
3837
3838
3839/*
3840 *      This function implements the receiving procedure of RFC 793 for
3841 *      all states except ESTABLISHED and TIME_WAIT. 
3842 *      It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
3843 *      address independent.
3844 */
3845        
3846int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
3847                          struct tcphdr *th, unsigned len)
3848{
3849        struct tcp_opt *tp = tcp_sk(sk);
3850        int queued = 0;
3851
3852        tp->saw_tstamp = 0;
3853
3854        switch (sk->sk_state) {
3855        case TCP_CLOSE:
3856                goto discard;
3857
3858        case TCP_LISTEN:
3859                if(th->ack)
3860                        return 1;
3861
3862                if(th->rst)
3863                        goto discard;
3864
3865                if(th->syn) {
3866                        if(tp->af_specific->conn_request(sk, skb) < 0)
3867                                return 1;
3868
3869                        /* Now we have several options: In theory there is 
3870                         * nothing else in the frame. KA9Q has an option to 
3871                         * send data with the syn, BSD accepts data with the
3872                         * syn up to the [to be] advertised window and 
3873                         * Solaris 2.1 gives you a protocol error. For now 
3874                         * we just ignore it, that fits the spec precisely 
3875                         * and avoids incompatibilities. It would be nice in
3876                         * future to drop through and process the data.
3877                         *
3878                         * Now that TTCP is starting to be used we ought to 
3879                         * queue this data.
3880                         * But, this leaves one open to an easy denial of
3881                         * service attack, and SYN cookies can't defend
3882                         * against this problem. So, we drop the data
3883                         * in the interest of security over speed.
3884                         */
3885                        goto discard;
3886                }
3887                goto discard;
3888
3889        case TCP_SYN_SENT:
3890                queued = tcp_rcv_synsent_state_process(sk, skb, th, len);
3891                if (queued >= 0)
3892                        return queued;
3893
3894                /* Do step6 onward by hand. */
3895                tcp_urg(sk, skb, th);
3896                __kfree_skb(skb);
3897                tcp_data_snd_check(sk);
3898                return 0;
3899        }
3900
3901        if (tcp_fast_parse_options(skb, th, tp) && tp->saw_tstamp &&
3902            tcp_paws_discard(tp, skb)) {
3903                if (!th->rst) {
3904                        NET_INC_STATS_BH(PAWSEstabRejected);
3905                        tcp_send_dupack(sk, skb);
3906                        goto discard;
3907                }
3908                /* Reset is accepted even if it did not pass PAWS. */
3909        }
3910
3911        /* step 1: check sequence number */
3912        if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
3913                if (!th->rst)
3914                        tcp_send_dupack(sk, skb);
3915                goto discard;
3916        }
3917
3918        /* step 2: check RST bit */
3919        if(th->rst) {
3920                tcp_reset(sk);
3921                goto discard;
3922        }
3923
3924        tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
3925
3926        /* step 3: check security and precedence [ignored] */
3927
3928        /*      step 4:
3929         *
3930         *      Check for a SYN in window.
3931         */
3932        if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
3933                NET_INC_STATS_BH(TCPAbortOnSyn);
3934                tcp_reset(sk);
3935                return 1;
3936        }
3937
3938        /* step 5: check the ACK field */
3939        if (th->ack) {
3940                int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH);
3941
3942                switch(sk->sk_state) {
3943                case TCP_SYN_RECV:
3944                        if (acceptable) {
3945                                tp->copied_seq = tp->rcv_nxt;
3946                                mb();
3947                                tcp_set_state(sk, TCP_ESTABLISHED);
3948                                sk->sk_state_change(sk);
3949
3950                                /* Note, that this wakeup is only for marginal
3951                                 * crossed SYN case. Passively open sockets
3952                                 * are not waked up, because sk->sk_sleep ==
3953                                 * NULL and sk->sk_socket == NULL.
3954                                 */
3955                                if (sk->sk_socket) {
3956                                        sk_wake_async(sk,0,POLL_OUT);
3957                                }
3958
3959                                tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
3960                                tp->snd_wnd = ntohs(th->window) <<
3961                                              tp->snd_wscale;
3962                                tcp_init_wl(tp, TCP_SKB_CB(skb)->ack_seq,
3963                                            TCP_SKB_CB(skb)->seq);
3964
3965                                /* tcp_ack considers this ACK as duplicate
3966                                 * and does not calculate rtt.
3967                                 * Fix it at least with timestamps.
3968                                 */
3969                                if (tp->saw_tstamp && tp->rcv_tsecr &&
3970                                    !tp->srtt)
3971                                        tcp_ack_saw_tstamp(tp, 0);
3972
3973                                if (tp->tstamp_ok)
3974                                        tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
3975
3976                                /* Make sure socket is routed, for
3977                                 * correct metrics.
3978                                 */
3979                                tp->af_specific->rebuild_header(sk);
3980
3981                                tcp_init_metrics(sk);
3982
3983                                /* Prevent spurious tcp_cwnd_restart() on
3984                                 * first data packet.
3985                                 */
3986                                tp->lsndtime = tcp_time_stamp;
3987
3988                                tcp_initialize_rcv_mss(sk);
3989                                tcp_init_buffer_space(sk);
3990                                tcp_fast_path_on(tp);
3991                        } else {
3992                                return 1;
3993                        }
3994                        break;
3995
3996                case TCP_FIN_WAIT1:
3997                        if (tp->snd_una == tp->write_seq) {
3998                                tcp_set_state(sk, TCP_FIN_WAIT2);
3999                                sk->sk_shutdown |= SEND_SHUTDOWN;
4000                                dst_confirm(sk->sk_dst_cache);
4001
4002                                if (!sock_flag(sk, SOCK_DEAD))
4003                                        /* Wake up lingering close() */
4004                                        sk->sk_state_change(sk);
4005                                else {
4006                                        int tmo;
4007
4008                                        if (tp->linger2 < 0 ||
4009                                            (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
4010                                             after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
4011                                                tcp_done(sk);
4012                                                NET_INC_STATS_BH(TCPAbortOnData);
4013                                                return 1;
4014                                        }
4015
4016                                        tmo = tcp_fin_time(tp);
4017                                        if (tmo > TCP_TIMEWAIT_LEN) {
4018                                                tcp_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
4019                                        } else if (th->fin || sock_owned_by_user(sk)) {
4020                                                /* Bad case. We could lose such FIN otherwise.
4021                                                 * It is not a big problem, but it looks confusing
4022                                                 * and not so rare event. We still can lose it now,
4023                                                 * if it spins in bh_lock_sock(), but it is really
4024                                                 * marginal case.
4025                                                 */
4026                                                tcp_reset_keepalive_timer(sk, tmo);
4027                                        } else {
4028                                                tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
4029                                                goto discard;
4030                                        }
4031                                }
4032                        }
4033                        break;
4034
4035                case TCP_CLOSING:
4036                        if (tp->snd_una == tp->write_seq) {
4037                                tcp_time_wait(sk, TCP_TIME_WAIT, 0);
4038                                goto discard;
4039                        }
4040                        break;
4041
4042                case TCP_LAST_ACK:
4043                        if (tp->snd_una == tp->write_seq) {
4044                                tcp_update_metrics(sk);
4045                                tcp_done(sk);
4046                                goto discard;
4047                        }
4048                        break;
4049                }
4050        } else
4051                goto discard;
4052
4053        /* step 6: check the URG bit */
4054        tcp_urg(sk, skb, th);
4055
4056        /* step 7: process the segment text */
4057        switch (sk->sk_state) {
4058        case TCP_CLOSE_WAIT:
4059        case TCP_CLOSING:
4060        case TCP_LAST_ACK:
4061                if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
4062                        break;
4063        case TCP_FIN_WAIT1:
4064        case TCP_FIN_WAIT2:
4065                /* RFC 793 says to queue data in these states,
4066                 * RFC 1122 says we MUST send a reset. 
4067                 * BSD 4.4 also does reset.
4068                 */
4069                if (sk->sk_shutdown & RCV_SHUTDOWN) {
4070                        if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
4071                            after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
4072                                NET_INC_STATS_BH(TCPAbortOnData);
4073                                tcp_reset(sk);
4074                                return 1;
4075                        }
4076                }
4077                /* Fall through */
4078        case TCP_ESTABLISHED: 
4079                tcp_data_queue(sk, skb);
4080                queued = 1;
4081                break;
4082        }
4083
4084        /* tcp_data could move socket to TIME-WAIT */
4085        if (sk->sk_state != TCP_CLOSE) {
4086                tcp_data_snd_check(sk);
4087                tcp_ack_snd_check(sk);
4088        }
4089
4090        if (!queued) { 
4091discard:
4092                __kfree_skb(skb);
4093        }
4094        return 0;
4095}
4096
4097EXPORT_SYMBOL(sysctl_tcp_ecn);
4098EXPORT_SYMBOL(sysctl_tcp_reordering);
4099EXPORT_SYMBOL(tcp_cwnd_application_limited);
4100EXPORT_SYMBOL(tcp_parse_options);
4101EXPORT_SYMBOL(tcp_rcv_established);
4102EXPORT_SYMBOL(tcp_rcv_state_process);
4103
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.