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