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