Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-block.git] / net / ipv4 / tcp_input.c
CommitLineData
1da177e4
LT
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 *
02c30a84 8 * Authors: Ross Biro
1da177e4
LT
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21/*
22 * Changes:
23 * Pedro Roque : Fast Retransmit/Recovery.
24 * Two receive queues.
25 * Retransmit queue handled by TCP.
26 * Better retransmit timer handling.
27 * New congestion avoidance.
28 * Header prediction.
29 * Variable renaming.
30 *
31 * Eric : Fast Retransmit.
32 * Randy Scott : MSS option defines.
33 * Eric Schenk : Fixes to slow start algorithm.
34 * Eric Schenk : Yet another double ACK bug.
35 * Eric Schenk : Delayed ACK bug fixes.
36 * Eric Schenk : Floyd style fast retrans war avoidance.
37 * David S. Miller : Don't allow zero congestion window.
38 * Eric Schenk : Fix retransmitter so that it sends
39 * next packet on ack of previous packet.
40 * Andi Kleen : Moved open_request checking here
41 * and process RSTs for open_requests.
42 * Andi Kleen : Better prune_queue, and other fixes.
caa20d9a 43 * Andrey Savochkin: Fix RTT measurements in the presence of
1da177e4
LT
44 * timestamps.
45 * Andrey Savochkin: Check sequence numbers correctly when
46 * removing SACKs due to in sequence incoming
47 * data segments.
48 * Andi Kleen: Make sure we never ack data there is not
49 * enough room for. Also make this condition
50 * a fatal error if it might still happen.
e905a9ed 51 * Andi Kleen: Add tcp_measure_rcv_mss to make
1da177e4 52 * connections with MSS<min(MTU,ann. MSS)
e905a9ed 53 * work without delayed acks.
1da177e4
LT
54 * Andi Kleen: Process packets with PSH set in the
55 * fast path.
56 * J Hadi Salim: ECN support
57 * Andrei Gurtov,
58 * Pasi Sarolahti,
59 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
60 * engine. Lots of bugs are found.
61 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
1da177e4
LT
62 */
63
1da177e4
LT
64#include <linux/mm.h>
65#include <linux/module.h>
66#include <linux/sysctl.h>
a0bffffc 67#include <linux/kernel.h>
5ffc02a1 68#include <net/dst.h>
1da177e4
LT
69#include <net/tcp.h>
70#include <net/inet_common.h>
71#include <linux/ipsec.h>
72#include <asm/unaligned.h>
1a2449a8 73#include <net/netdma.h>
1da177e4 74
ab32ea5d
BH
75int sysctl_tcp_timestamps __read_mostly = 1;
76int sysctl_tcp_window_scaling __read_mostly = 1;
77int sysctl_tcp_sack __read_mostly = 1;
78int sysctl_tcp_fack __read_mostly = 1;
79int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;
255cac91 80int sysctl_tcp_ecn __read_mostly = 2;
ab32ea5d
BH
81int sysctl_tcp_dsack __read_mostly = 1;
82int sysctl_tcp_app_win __read_mostly = 31;
83int sysctl_tcp_adv_win_scale __read_mostly = 2;
1da177e4 84
ab32ea5d
BH
85int sysctl_tcp_stdurg __read_mostly;
86int sysctl_tcp_rfc1337 __read_mostly;
87int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
c96fd3d4 88int sysctl_tcp_frto __read_mostly = 2;
3cfe3baa 89int sysctl_tcp_frto_response __read_mostly;
ab32ea5d 90int sysctl_tcp_nometrics_save __read_mostly;
1da177e4 91
7e380175
AP
92int sysctl_tcp_thin_dupack __read_mostly;
93
ab32ea5d
BH
94int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
95int sysctl_tcp_abc __read_mostly;
1da177e4 96
1da177e4
LT
97#define FLAG_DATA 0x01 /* Incoming frame contained data. */
98#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
99#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
100#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
101#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
102#define FLAG_DATA_SACKED 0x20 /* New SACK. */
103#define FLAG_ECE 0x40 /* ECE in this ACK */
104#define FLAG_DATA_LOST 0x80 /* SACK detected data lossage. */
105#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
4dc2665e 106#define FLAG_ONLY_ORIG_SACKED 0x200 /* SACKs only non-rexmit sent before RTO */
2e605294 107#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
564262c1 108#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
009a2e3e 109#define FLAG_NONHEAD_RETRANS_ACKED 0x1000 /* Non-head rexmitted data was ACKed */
cadbd031 110#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
1da177e4
LT
111
112#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
113#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
114#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
115#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
2e605294 116#define FLAG_ANY_PROGRESS (FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED)
1da177e4 117
1da177e4 118#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
bdf1ee5d 119#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1da177e4 120
e905a9ed 121/* Adapt the MSS value used to make delayed ack decision to the
1da177e4 122 * real world.
e905a9ed 123 */
056834d9 124static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
1da177e4 125{
463c84b9 126 struct inet_connection_sock *icsk = inet_csk(sk);
e905a9ed 127 const unsigned int lss = icsk->icsk_ack.last_seg_size;
463c84b9 128 unsigned int len;
1da177e4 129
e905a9ed 130 icsk->icsk_ack.last_seg_size = 0;
1da177e4
LT
131
132 /* skb->len may jitter because of SACKs, even if peer
133 * sends good full-sized frames.
134 */
056834d9 135 len = skb_shinfo(skb)->gso_size ? : skb->len;
463c84b9
ACM
136 if (len >= icsk->icsk_ack.rcv_mss) {
137 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
138 } else {
139 /* Otherwise, we make more careful check taking into account,
140 * that SACKs block is variable.
141 *
142 * "len" is invariant segment length, including TCP header.
143 */
9c70220b 144 len += skb->data - skb_transport_header(skb);
bee7ca9e 145 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
1da177e4
LT
146 /* If PSH is not set, packet should be
147 * full sized, provided peer TCP is not badly broken.
148 * This observation (if it is correct 8)) allows
149 * to handle super-low mtu links fairly.
150 */
151 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
aa8223c7 152 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
1da177e4
LT
153 /* Subtract also invariant (if peer is RFC compliant),
154 * tcp header plus fixed timestamp option length.
155 * Resulting "len" is MSS free of SACK jitter.
156 */
463c84b9
ACM
157 len -= tcp_sk(sk)->tcp_header_len;
158 icsk->icsk_ack.last_seg_size = len;
1da177e4 159 if (len == lss) {
463c84b9 160 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
161 return;
162 }
163 }
1ef9696c
AK
164 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
165 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
463c84b9 166 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
1da177e4
LT
167 }
168}
169
463c84b9 170static void tcp_incr_quickack(struct sock *sk)
1da177e4 171{
463c84b9
ACM
172 struct inet_connection_sock *icsk = inet_csk(sk);
173 unsigned quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
1da177e4 174
056834d9
IJ
175 if (quickacks == 0)
176 quickacks = 2;
463c84b9
ACM
177 if (quickacks > icsk->icsk_ack.quick)
178 icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
1da177e4
LT
179}
180
463c84b9 181void tcp_enter_quickack_mode(struct sock *sk)
1da177e4 182{
463c84b9
ACM
183 struct inet_connection_sock *icsk = inet_csk(sk);
184 tcp_incr_quickack(sk);
185 icsk->icsk_ack.pingpong = 0;
186 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4
LT
187}
188
189/* Send ACKs quickly, if "quick" count is not exhausted
190 * and the session is not interactive.
191 */
192
463c84b9 193static inline int tcp_in_quickack_mode(const struct sock *sk)
1da177e4 194{
463c84b9
ACM
195 const struct inet_connection_sock *icsk = inet_csk(sk);
196 return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;
1da177e4
LT
197}
198
bdf1ee5d
IJ
199static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
200{
056834d9 201 if (tp->ecn_flags & TCP_ECN_OK)
bdf1ee5d
IJ
202 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
203}
204
205static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb)
206{
207 if (tcp_hdr(skb)->cwr)
208 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
209}
210
211static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
212{
213 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
214}
215
216static inline void TCP_ECN_check_ce(struct tcp_sock *tp, struct sk_buff *skb)
217{
056834d9 218 if (tp->ecn_flags & TCP_ECN_OK) {
bdf1ee5d
IJ
219 if (INET_ECN_is_ce(TCP_SKB_CB(skb)->flags))
220 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
221 /* Funny extension: if ECT is not set on a segment,
222 * it is surely retransmit. It is not in ECN RFC,
223 * but Linux follows this rule. */
224 else if (INET_ECN_is_not_ect((TCP_SKB_CB(skb)->flags)))
225 tcp_enter_quickack_mode((struct sock *)tp);
226 }
227}
228
229static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th)
230{
056834d9 231 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
bdf1ee5d
IJ
232 tp->ecn_flags &= ~TCP_ECN_OK;
233}
234
235static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th)
236{
056834d9 237 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
bdf1ee5d
IJ
238 tp->ecn_flags &= ~TCP_ECN_OK;
239}
240
241static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th)
242{
056834d9 243 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
bdf1ee5d
IJ
244 return 1;
245 return 0;
246}
247
1da177e4
LT
248/* Buffer size and advertised window tuning.
249 *
250 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
251 */
252
253static void tcp_fixup_sndbuf(struct sock *sk)
254{
255 int sndmem = tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER + 16 +
256 sizeof(struct sk_buff);
257
258 if (sk->sk_sndbuf < 3 * sndmem)
259 sk->sk_sndbuf = min(3 * sndmem, sysctl_tcp_wmem[2]);
260}
261
262/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
263 *
264 * All tcp_full_space() is split to two parts: "network" buffer, allocated
265 * forward and advertised in receiver window (tp->rcv_wnd) and
266 * "application buffer", required to isolate scheduling/application
267 * latencies from network.
268 * window_clamp is maximal advertised window. It can be less than
269 * tcp_full_space(), in this case tcp_full_space() - window_clamp
270 * is reserved for "application" buffer. The less window_clamp is
271 * the smoother our behaviour from viewpoint of network, but the lower
272 * throughput and the higher sensitivity of the connection to losses. 8)
273 *
274 * rcv_ssthresh is more strict window_clamp used at "slow start"
275 * phase to predict further behaviour of this connection.
276 * It is used for two goals:
277 * - to enforce header prediction at sender, even when application
278 * requires some significant "application buffer". It is check #1.
279 * - to prevent pruning of receive queue because of misprediction
280 * of receiver window. Check #2.
281 *
282 * The scheme does not work when sender sends good segments opening
caa20d9a 283 * window and then starts to feed us spaghetti. But it should work
1da177e4
LT
284 * in common situations. Otherwise, we have to rely on queue collapsing.
285 */
286
287/* Slow part of check#2. */
9e412ba7 288static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
1da177e4 289{
9e412ba7 290 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 291 /* Optimize this! */
dfd4f0ae
ED
292 int truesize = tcp_win_from_space(skb->truesize) >> 1;
293 int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
1da177e4
LT
294
295 while (tp->rcv_ssthresh <= window) {
296 if (truesize <= skb->len)
463c84b9 297 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
1da177e4
LT
298
299 truesize >>= 1;
300 window >>= 1;
301 }
302 return 0;
303}
304
056834d9 305static void tcp_grow_window(struct sock *sk, struct sk_buff *skb)
1da177e4 306{
9e412ba7
IJ
307 struct tcp_sock *tp = tcp_sk(sk);
308
1da177e4
LT
309 /* Check #1 */
310 if (tp->rcv_ssthresh < tp->window_clamp &&
311 (int)tp->rcv_ssthresh < tcp_space(sk) &&
312 !tcp_memory_pressure) {
313 int incr;
314
315 /* Check #2. Increase window, if skb with such overhead
316 * will fit to rcvbuf in future.
317 */
318 if (tcp_win_from_space(skb->truesize) <= skb->len)
056834d9 319 incr = 2 * tp->advmss;
1da177e4 320 else
9e412ba7 321 incr = __tcp_grow_window(sk, skb);
1da177e4
LT
322
323 if (incr) {
056834d9
IJ
324 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
325 tp->window_clamp);
463c84b9 326 inet_csk(sk)->icsk_ack.quick |= 1;
1da177e4
LT
327 }
328 }
329}
330
331/* 3. Tuning rcvbuf, when connection enters established state. */
332
333static void tcp_fixup_rcvbuf(struct sock *sk)
334{
335 struct tcp_sock *tp = tcp_sk(sk);
336 int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff);
337
338 /* Try to select rcvbuf so that 4 mss-sized segments
caa20d9a 339 * will fit to window and corresponding skbs will fit to our rcvbuf.
1da177e4
LT
340 * (was 3; 4 is minimum to allow fast retransmit to work.)
341 */
342 while (tcp_win_from_space(rcvmem) < tp->advmss)
343 rcvmem += 128;
344 if (sk->sk_rcvbuf < 4 * rcvmem)
345 sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]);
346}
347
caa20d9a 348/* 4. Try to fixup all. It is made immediately after connection enters
1da177e4
LT
349 * established state.
350 */
351static void tcp_init_buffer_space(struct sock *sk)
352{
353 struct tcp_sock *tp = tcp_sk(sk);
354 int maxwin;
355
356 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
357 tcp_fixup_rcvbuf(sk);
358 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
359 tcp_fixup_sndbuf(sk);
360
361 tp->rcvq_space.space = tp->rcv_wnd;
362
363 maxwin = tcp_full_space(sk);
364
365 if (tp->window_clamp >= maxwin) {
366 tp->window_clamp = maxwin;
367
368 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
369 tp->window_clamp = max(maxwin -
370 (maxwin >> sysctl_tcp_app_win),
371 4 * tp->advmss);
372 }
373
374 /* Force reservation of one segment. */
375 if (sysctl_tcp_app_win &&
376 tp->window_clamp > 2 * tp->advmss &&
377 tp->window_clamp + tp->advmss > maxwin)
378 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
379
380 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
381 tp->snd_cwnd_stamp = tcp_time_stamp;
382}
383
1da177e4 384/* 5. Recalculate window clamp after socket hit its memory bounds. */
9e412ba7 385static void tcp_clamp_window(struct sock *sk)
1da177e4 386{
9e412ba7 387 struct tcp_sock *tp = tcp_sk(sk);
6687e988 388 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 389
6687e988 390 icsk->icsk_ack.quick = 0;
1da177e4 391
326f36e9
JH
392 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
393 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
394 !tcp_memory_pressure &&
395 atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) {
396 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
397 sysctl_tcp_rmem[2]);
1da177e4 398 }
326f36e9 399 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
056834d9 400 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
1da177e4
LT
401}
402
40efc6fa
SH
403/* Initialize RCV_MSS value.
404 * RCV_MSS is an our guess about MSS used by the peer.
405 * We haven't any direct information about the MSS.
406 * It's better to underestimate the RCV_MSS rather than overestimate.
407 * Overestimations make us ACKing less frequently than needed.
408 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
409 */
410void tcp_initialize_rcv_mss(struct sock *sk)
411{
412 struct tcp_sock *tp = tcp_sk(sk);
413 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
414
056834d9 415 hint = min(hint, tp->rcv_wnd / 2);
bee7ca9e 416 hint = min(hint, TCP_MSS_DEFAULT);
40efc6fa
SH
417 hint = max(hint, TCP_MIN_MSS);
418
419 inet_csk(sk)->icsk_ack.rcv_mss = hint;
420}
421
1da177e4
LT
422/* Receiver "autotuning" code.
423 *
424 * The algorithm for RTT estimation w/o timestamps is based on
425 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
426 * <http://www.lanl.gov/radiant/website/pubs/drs/lacsi2001.ps>
427 *
428 * More detail on this code can be found at
429 * <http://www.psc.edu/~jheffner/senior_thesis.ps>,
430 * though this reference is out of date. A new paper
431 * is pending.
432 */
433static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
434{
435 u32 new_sample = tp->rcv_rtt_est.rtt;
436 long m = sample;
437
438 if (m == 0)
439 m = 1;
440
441 if (new_sample != 0) {
442 /* If we sample in larger samples in the non-timestamp
443 * case, we could grossly overestimate the RTT especially
444 * with chatty applications or bulk transfer apps which
445 * are stalled on filesystem I/O.
446 *
447 * Also, since we are only going for a minimum in the
31f34269 448 * non-timestamp case, we do not smooth things out
caa20d9a 449 * else with timestamps disabled convergence takes too
1da177e4
LT
450 * long.
451 */
452 if (!win_dep) {
453 m -= (new_sample >> 3);
454 new_sample += m;
455 } else if (m < new_sample)
456 new_sample = m << 3;
457 } else {
caa20d9a 458 /* No previous measure. */
1da177e4
LT
459 new_sample = m << 3;
460 }
461
462 if (tp->rcv_rtt_est.rtt != new_sample)
463 tp->rcv_rtt_est.rtt = new_sample;
464}
465
466static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
467{
468 if (tp->rcv_rtt_est.time == 0)
469 goto new_measure;
470 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
471 return;
056834d9 472 tcp_rcv_rtt_update(tp, jiffies - tp->rcv_rtt_est.time, 1);
1da177e4
LT
473
474new_measure:
475 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
476 tp->rcv_rtt_est.time = tcp_time_stamp;
477}
478
056834d9
IJ
479static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
480 const struct sk_buff *skb)
1da177e4 481{
463c84b9 482 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
483 if (tp->rx_opt.rcv_tsecr &&
484 (TCP_SKB_CB(skb)->end_seq -
463c84b9 485 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
1da177e4
LT
486 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
487}
488
489/*
490 * This function should be called every time data is copied to user space.
491 * It calculates the appropriate TCP receive buffer space.
492 */
493void tcp_rcv_space_adjust(struct sock *sk)
494{
495 struct tcp_sock *tp = tcp_sk(sk);
496 int time;
497 int space;
e905a9ed 498
1da177e4
LT
499 if (tp->rcvq_space.time == 0)
500 goto new_measure;
e905a9ed 501
1da177e4 502 time = tcp_time_stamp - tp->rcvq_space.time;
056834d9 503 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
1da177e4 504 return;
e905a9ed 505
1da177e4
LT
506 space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
507
508 space = max(tp->rcvq_space.space, space);
509
510 if (tp->rcvq_space.space != space) {
511 int rcvmem;
512
513 tp->rcvq_space.space = space;
514
6fcf9412
JH
515 if (sysctl_tcp_moderate_rcvbuf &&
516 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1da177e4
LT
517 int new_clamp = space;
518
519 /* Receive space grows, normalize in order to
520 * take into account packet headers and sk_buff
521 * structure overhead.
522 */
523 space /= tp->advmss;
524 if (!space)
525 space = 1;
526 rcvmem = (tp->advmss + MAX_TCP_HEADER +
527 16 + sizeof(struct sk_buff));
528 while (tcp_win_from_space(rcvmem) < tp->advmss)
529 rcvmem += 128;
530 space *= rcvmem;
531 space = min(space, sysctl_tcp_rmem[2]);
532 if (space > sk->sk_rcvbuf) {
533 sk->sk_rcvbuf = space;
534
535 /* Make the window clamp follow along. */
536 tp->window_clamp = new_clamp;
537 }
538 }
539 }
e905a9ed 540
1da177e4
LT
541new_measure:
542 tp->rcvq_space.seq = tp->copied_seq;
543 tp->rcvq_space.time = tcp_time_stamp;
544}
545
546/* There is something which you must keep in mind when you analyze the
547 * behavior of the tp->ato delayed ack timeout interval. When a
548 * connection starts up, we want to ack as quickly as possible. The
549 * problem is that "good" TCP's do slow start at the beginning of data
550 * transmission. The means that until we send the first few ACK's the
551 * sender will sit on his end and only queue most of his data, because
552 * he can only send snd_cwnd unacked packets at any given time. For
553 * each ACK we send, he increments snd_cwnd and transmits more of his
554 * queue. -DaveM
555 */
9e412ba7 556static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
1da177e4 557{
9e412ba7 558 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 559 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
560 u32 now;
561
463c84b9 562 inet_csk_schedule_ack(sk);
1da177e4 563
463c84b9 564 tcp_measure_rcv_mss(sk, skb);
1da177e4
LT
565
566 tcp_rcv_rtt_measure(tp);
e905a9ed 567
1da177e4
LT
568 now = tcp_time_stamp;
569
463c84b9 570 if (!icsk->icsk_ack.ato) {
1da177e4
LT
571 /* The _first_ data packet received, initialize
572 * delayed ACK engine.
573 */
463c84b9
ACM
574 tcp_incr_quickack(sk);
575 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4 576 } else {
463c84b9 577 int m = now - icsk->icsk_ack.lrcvtime;
1da177e4 578
056834d9 579 if (m <= TCP_ATO_MIN / 2) {
1da177e4 580 /* The fastest case is the first. */
463c84b9
ACM
581 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
582 } else if (m < icsk->icsk_ack.ato) {
583 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
584 if (icsk->icsk_ack.ato > icsk->icsk_rto)
585 icsk->icsk_ack.ato = icsk->icsk_rto;
586 } else if (m > icsk->icsk_rto) {
caa20d9a 587 /* Too long gap. Apparently sender failed to
1da177e4
LT
588 * restart window, so that we send ACKs quickly.
589 */
463c84b9 590 tcp_incr_quickack(sk);
3ab224be 591 sk_mem_reclaim(sk);
1da177e4
LT
592 }
593 }
463c84b9 594 icsk->icsk_ack.lrcvtime = now;
1da177e4
LT
595
596 TCP_ECN_check_ce(tp, skb);
597
598 if (skb->len >= 128)
9e412ba7 599 tcp_grow_window(sk, skb);
1da177e4
LT
600}
601
1da177e4
LT
602/* Called to compute a smoothed rtt estimate. The data fed to this
603 * routine either comes from timestamps, or from segments that were
604 * known _not_ to have been retransmitted [see Karn/Partridge
605 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
606 * piece by Van Jacobson.
607 * NOTE: the next three routines used to be one big routine.
608 * To save cycles in the RFC 1323 implementation it was better to break
609 * it up into three procedures. -- erics
610 */
2d2abbab 611static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)
1da177e4 612{
6687e988 613 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
614 long m = mrtt; /* RTT */
615
1da177e4
LT
616 /* The following amusing code comes from Jacobson's
617 * article in SIGCOMM '88. Note that rtt and mdev
618 * are scaled versions of rtt and mean deviation.
e905a9ed 619 * This is designed to be as fast as possible
1da177e4
LT
620 * m stands for "measurement".
621 *
622 * On a 1990 paper the rto value is changed to:
623 * RTO = rtt + 4 * mdev
624 *
625 * Funny. This algorithm seems to be very broken.
626 * These formulae increase RTO, when it should be decreased, increase
31f34269 627 * too slowly, when it should be increased quickly, decrease too quickly
1da177e4
LT
628 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
629 * does not matter how to _calculate_ it. Seems, it was trap
630 * that VJ failed to avoid. 8)
631 */
2de979bd 632 if (m == 0)
1da177e4
LT
633 m = 1;
634 if (tp->srtt != 0) {
635 m -= (tp->srtt >> 3); /* m is now error in rtt est */
636 tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */
637 if (m < 0) {
638 m = -m; /* m is now abs(error) */
639 m -= (tp->mdev >> 2); /* similar update on mdev */
640 /* This is similar to one of Eifel findings.
641 * Eifel blocks mdev updates when rtt decreases.
642 * This solution is a bit different: we use finer gain
643 * for mdev in this case (alpha*beta).
644 * Like Eifel it also prevents growth of rto,
645 * but also it limits too fast rto decreases,
646 * happening in pure Eifel.
647 */
648 if (m > 0)
649 m >>= 3;
650 } else {
651 m -= (tp->mdev >> 2); /* similar update on mdev */
652 }
653 tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */
654 if (tp->mdev > tp->mdev_max) {
655 tp->mdev_max = tp->mdev;
656 if (tp->mdev_max > tp->rttvar)
657 tp->rttvar = tp->mdev_max;
658 }
659 if (after(tp->snd_una, tp->rtt_seq)) {
660 if (tp->mdev_max < tp->rttvar)
056834d9 661 tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2;
1da177e4 662 tp->rtt_seq = tp->snd_nxt;
05bb1fad 663 tp->mdev_max = tcp_rto_min(sk);
1da177e4
LT
664 }
665 } else {
666 /* no previous measure. */
056834d9
IJ
667 tp->srtt = m << 3; /* take the measured time to be rtt */
668 tp->mdev = m << 1; /* make sure rto = 3*rtt */
05bb1fad 669 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4
LT
670 tp->rtt_seq = tp->snd_nxt;
671 }
1da177e4
LT
672}
673
674/* Calculate rto without backoff. This is the second half of Van Jacobson's
675 * routine referred to above.
676 */
463c84b9 677static inline void tcp_set_rto(struct sock *sk)
1da177e4 678{
463c84b9 679 const struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
680 /* Old crap is replaced with new one. 8)
681 *
682 * More seriously:
683 * 1. If rtt variance happened to be less 50msec, it is hallucination.
684 * It cannot be less due to utterly erratic ACK generation made
685 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
686 * to do with delayed acks, because at cwnd>2 true delack timeout
687 * is invisible. Actually, Linux-2.4 also generates erratic
caa20d9a 688 * ACKs in some circumstances.
1da177e4 689 */
f1ecd5d9 690 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
1da177e4
LT
691
692 /* 2. Fixups made earlier cannot be right.
693 * If we do not estimate RTO correctly without them,
694 * all the algo is pure shit and should be replaced
caa20d9a 695 * with correct one. It is exactly, which we pretend to do.
1da177e4 696 */
1da177e4 697
ee6aac59
IJ
698 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
699 * guarantees that rto is higher.
700 */
f1ecd5d9 701 tcp_bound_rto(sk);
1da177e4
LT
702}
703
704/* Save metrics learned by this TCP session.
705 This function is called only, when TCP finishes successfully
706 i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE.
707 */
708void tcp_update_metrics(struct sock *sk)
709{
710 struct tcp_sock *tp = tcp_sk(sk);
711 struct dst_entry *dst = __sk_dst_get(sk);
712
713 if (sysctl_tcp_nometrics_save)
714 return;
715
716 dst_confirm(dst);
717
056834d9 718 if (dst && (dst->flags & DST_HOST)) {
6687e988 719 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 720 int m;
c1e20f7c 721 unsigned long rtt;
1da177e4 722
6687e988 723 if (icsk->icsk_backoff || !tp->srtt) {
1da177e4
LT
724 /* This session failed to estimate rtt. Why?
725 * Probably, no packets returned in time.
726 * Reset our results.
727 */
728 if (!(dst_metric_locked(dst, RTAX_RTT)))
056834d9 729 dst->metrics[RTAX_RTT - 1] = 0;
1da177e4
LT
730 return;
731 }
732
c1e20f7c
SH
733 rtt = dst_metric_rtt(dst, RTAX_RTT);
734 m = rtt - tp->srtt;
1da177e4
LT
735
736 /* If newly calculated rtt larger than stored one,
737 * store new one. Otherwise, use EWMA. Remember,
738 * rtt overestimation is always better than underestimation.
739 */
740 if (!(dst_metric_locked(dst, RTAX_RTT))) {
741 if (m <= 0)
c1e20f7c 742 set_dst_metric_rtt(dst, RTAX_RTT, tp->srtt);
1da177e4 743 else
c1e20f7c 744 set_dst_metric_rtt(dst, RTAX_RTT, rtt - (m >> 3));
1da177e4
LT
745 }
746
747 if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
c1e20f7c 748 unsigned long var;
1da177e4
LT
749 if (m < 0)
750 m = -m;
751
752 /* Scale deviation to rttvar fixed point */
753 m >>= 1;
754 if (m < tp->mdev)
755 m = tp->mdev;
756
c1e20f7c
SH
757 var = dst_metric_rtt(dst, RTAX_RTTVAR);
758 if (m >= var)
759 var = m;
1da177e4 760 else
c1e20f7c
SH
761 var -= (var - m) >> 2;
762
763 set_dst_metric_rtt(dst, RTAX_RTTVAR, var);
1da177e4
LT
764 }
765
0b6a05c1 766 if (tcp_in_initial_slowstart(tp)) {
1da177e4
LT
767 /* Slow start still did not finish. */
768 if (dst_metric(dst, RTAX_SSTHRESH) &&
769 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
770 (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
771 dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1;
772 if (!dst_metric_locked(dst, RTAX_CWND) &&
773 tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
056834d9 774 dst->metrics[RTAX_CWND - 1] = tp->snd_cwnd;
1da177e4 775 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
6687e988 776 icsk->icsk_ca_state == TCP_CA_Open) {
1da177e4
LT
777 /* Cong. avoidance phase, cwnd is reliable. */
778 if (!dst_metric_locked(dst, RTAX_SSTHRESH))
779 dst->metrics[RTAX_SSTHRESH-1] =
780 max(tp->snd_cwnd >> 1, tp->snd_ssthresh);
781 if (!dst_metric_locked(dst, RTAX_CWND))
5ffc02a1 782 dst->metrics[RTAX_CWND-1] = (dst_metric(dst, RTAX_CWND) + tp->snd_cwnd) >> 1;
1da177e4
LT
783 } else {
784 /* Else slow start did not finish, cwnd is non-sense,
785 ssthresh may be also invalid.
786 */
787 if (!dst_metric_locked(dst, RTAX_CWND))
5ffc02a1
SS
788 dst->metrics[RTAX_CWND-1] = (dst_metric(dst, RTAX_CWND) + tp->snd_ssthresh) >> 1;
789 if (dst_metric(dst, RTAX_SSTHRESH) &&
1da177e4 790 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
5ffc02a1 791 tp->snd_ssthresh > dst_metric(dst, RTAX_SSTHRESH))
1da177e4
LT
792 dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh;
793 }
794
795 if (!dst_metric_locked(dst, RTAX_REORDERING)) {
5ffc02a1 796 if (dst_metric(dst, RTAX_REORDERING) < tp->reordering &&
1da177e4
LT
797 tp->reordering != sysctl_tcp_reordering)
798 dst->metrics[RTAX_REORDERING-1] = tp->reordering;
799 }
800 }
801}
802
410e27a4
GR
803/* Numbers are taken from RFC3390.
804 *
805 * John Heffner states:
806 *
807 * The RFC specifies a window of no more than 4380 bytes
808 * unless 2*MSS > 4380. Reading the pseudocode in the RFC
809 * is a bit misleading because they use a clamp at 4380 bytes
810 * rather than use a multiplier in the relevant range.
811 */
1da177e4
LT
812__u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst)
813{
814 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
815
410e27a4
GR
816 if (!cwnd) {
817 if (tp->mss_cache > 1460)
818 cwnd = 2;
819 else
820 cwnd = (tp->mss_cache > 1095) ? 3 : 4;
821 }
1da177e4
LT
822 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
823}
824
40efc6fa 825/* Set slow start threshold and cwnd not falling to slow start */
3cfe3baa 826void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
40efc6fa
SH
827{
828 struct tcp_sock *tp = tcp_sk(sk);
3cfe3baa 829 const struct inet_connection_sock *icsk = inet_csk(sk);
40efc6fa
SH
830
831 tp->prior_ssthresh = 0;
832 tp->bytes_acked = 0;
e01f9d77 833 if (icsk->icsk_ca_state < TCP_CA_CWR) {
40efc6fa 834 tp->undo_marker = 0;
3cfe3baa
IJ
835 if (set_ssthresh)
836 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
40efc6fa
SH
837 tp->snd_cwnd = min(tp->snd_cwnd,
838 tcp_packets_in_flight(tp) + 1U);
839 tp->snd_cwnd_cnt = 0;
840 tp->high_seq = tp->snd_nxt;
841 tp->snd_cwnd_stamp = tcp_time_stamp;
842 TCP_ECN_queue_cwr(tp);
843
844 tcp_set_ca_state(sk, TCP_CA_CWR);
845 }
846}
847
e60402d0
IJ
848/*
849 * Packet counting of FACK is based on in-order assumptions, therefore TCP
850 * disables it when reordering is detected
851 */
852static void tcp_disable_fack(struct tcp_sock *tp)
853{
85cc391c
IJ
854 /* RFC3517 uses different metric in lost marker => reset on change */
855 if (tcp_is_fack(tp))
856 tp->lost_skb_hint = NULL;
e60402d0
IJ
857 tp->rx_opt.sack_ok &= ~2;
858}
859
564262c1 860/* Take a notice that peer is sending D-SACKs */
e60402d0
IJ
861static void tcp_dsack_seen(struct tcp_sock *tp)
862{
863 tp->rx_opt.sack_ok |= 4;
864}
865
1da177e4
LT
866/* Initialize metrics on socket. */
867
868static void tcp_init_metrics(struct sock *sk)
869{
870 struct tcp_sock *tp = tcp_sk(sk);
871 struct dst_entry *dst = __sk_dst_get(sk);
872
873 if (dst == NULL)
874 goto reset;
875
876 dst_confirm(dst);
877
878 if (dst_metric_locked(dst, RTAX_CWND))
879 tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
880 if (dst_metric(dst, RTAX_SSTHRESH)) {
881 tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
882 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
883 tp->snd_ssthresh = tp->snd_cwnd_clamp;
884 }
885 if (dst_metric(dst, RTAX_REORDERING) &&
886 tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
e60402d0 887 tcp_disable_fack(tp);
1da177e4
LT
888 tp->reordering = dst_metric(dst, RTAX_REORDERING);
889 }
890
891 if (dst_metric(dst, RTAX_RTT) == 0)
892 goto reset;
893
c1e20f7c 894 if (!tp->srtt && dst_metric_rtt(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3))
1da177e4
LT
895 goto reset;
896
897 /* Initial rtt is determined from SYN,SYN-ACK.
898 * The segment is small and rtt may appear much
899 * less than real one. Use per-dst memory
900 * to make it more realistic.
901 *
902 * A bit of theory. RTT is time passed after "normal" sized packet
caa20d9a 903 * is sent until it is ACKed. In normal circumstances sending small
1da177e4
LT
904 * packets force peer to delay ACKs and calculation is correct too.
905 * The algorithm is adaptive and, provided we follow specs, it
906 * NEVER underestimate RTT. BUT! If peer tries to make some clever
907 * tricks sort of "quick acks" for time long enough to decrease RTT
908 * to low value, and then abruptly stops to do it and starts to delay
909 * ACKs, wait for troubles.
910 */
c1e20f7c
SH
911 if (dst_metric_rtt(dst, RTAX_RTT) > tp->srtt) {
912 tp->srtt = dst_metric_rtt(dst, RTAX_RTT);
1da177e4
LT
913 tp->rtt_seq = tp->snd_nxt;
914 }
c1e20f7c
SH
915 if (dst_metric_rtt(dst, RTAX_RTTVAR) > tp->mdev) {
916 tp->mdev = dst_metric_rtt(dst, RTAX_RTTVAR);
488faa2a 917 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4 918 }
463c84b9 919 tcp_set_rto(sk);
463c84b9 920 if (inet_csk(sk)->icsk_rto < TCP_TIMEOUT_INIT && !tp->rx_opt.saw_tstamp)
1da177e4 921 goto reset;
86bcebaf
IJ
922
923cwnd:
1da177e4
LT
924 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
925 tp->snd_cwnd_stamp = tcp_time_stamp;
926 return;
927
928reset:
929 /* Play conservative. If timestamps are not
930 * supported, TCP will fail to recalculate correct
931 * rtt, if initial rto is too small. FORGET ALL AND RESET!
932 */
933 if (!tp->rx_opt.saw_tstamp && tp->srtt) {
934 tp->srtt = 0;
935 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT;
463c84b9 936 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
1da177e4 937 }
86bcebaf 938 goto cwnd;
1da177e4
LT
939}
940
6687e988
ACM
941static void tcp_update_reordering(struct sock *sk, const int metric,
942 const int ts)
1da177e4 943{
6687e988 944 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 945 if (metric > tp->reordering) {
40b215e5
PE
946 int mib_idx;
947
1da177e4
LT
948 tp->reordering = min(TCP_MAX_REORDERING, metric);
949
950 /* This exciting event is worth to be remembered. 8) */
951 if (ts)
40b215e5 952 mib_idx = LINUX_MIB_TCPTSREORDER;
e60402d0 953 else if (tcp_is_reno(tp))
40b215e5 954 mib_idx = LINUX_MIB_TCPRENOREORDER;
e60402d0 955 else if (tcp_is_fack(tp))
40b215e5 956 mib_idx = LINUX_MIB_TCPFACKREORDER;
1da177e4 957 else
40b215e5
PE
958 mib_idx = LINUX_MIB_TCPSACKREORDER;
959
de0744af 960 NET_INC_STATS_BH(sock_net(sk), mib_idx);
1da177e4
LT
961#if FASTRETRANS_DEBUG > 1
962 printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n",
6687e988 963 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
1da177e4
LT
964 tp->reordering,
965 tp->fackets_out,
966 tp->sacked_out,
967 tp->undo_marker ? tp->undo_retrans : 0);
968#endif
e60402d0 969 tcp_disable_fack(tp);
1da177e4
LT
970 }
971}
972
006f582c 973/* This must be called before lost_out is incremented */
c8c213f2
IJ
974static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
975{
006f582c 976 if ((tp->retransmit_skb_hint == NULL) ||
c8c213f2
IJ
977 before(TCP_SKB_CB(skb)->seq,
978 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
006f582c
IJ
979 tp->retransmit_skb_hint = skb;
980
981 if (!tp->lost_out ||
982 after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
983 tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
c8c213f2
IJ
984}
985
41ea36e3
IJ
986static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
987{
988 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
989 tcp_verify_retransmit_hint(tp, skb);
990
991 tp->lost_out += tcp_skb_pcount(skb);
992 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
993 }
994}
995
e1aa680f
IJ
996static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,
997 struct sk_buff *skb)
006f582c
IJ
998{
999 tcp_verify_retransmit_hint(tp, skb);
1000
1001 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
1002 tp->lost_out += tcp_skb_pcount(skb);
1003 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1004 }
1005}
1006
1da177e4
LT
1007/* This procedure tags the retransmission queue when SACKs arrive.
1008 *
1009 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
1010 * Packets in queue with these bits set are counted in variables
1011 * sacked_out, retrans_out and lost_out, correspondingly.
1012 *
1013 * Valid combinations are:
1014 * Tag InFlight Description
1015 * 0 1 - orig segment is in flight.
1016 * S 0 - nothing flies, orig reached receiver.
1017 * L 0 - nothing flies, orig lost by net.
1018 * R 2 - both orig and retransmit are in flight.
1019 * L|R 1 - orig is lost, retransmit is in flight.
1020 * S|R 1 - orig reached receiver, retrans is still in flight.
1021 * (L|S|R is logically valid, it could occur when L|R is sacked,
1022 * but it is equivalent to plain S and code short-curcuits it to S.
1023 * L|S is logically invalid, it would mean -1 packet in flight 8))
1024 *
1025 * These 6 states form finite state machine, controlled by the following events:
1026 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
1027 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
1028 * 3. Loss detection event of one of three flavors:
1029 * A. Scoreboard estimator decided the packet is lost.
1030 * A'. Reno "three dupacks" marks head of queue lost.
1031 * A''. Its FACK modfication, head until snd.fack is lost.
1032 * B. SACK arrives sacking data transmitted after never retransmitted
1033 * hole was sent out.
1034 * C. SACK arrives sacking SND.NXT at the moment, when the
1035 * segment was retransmitted.
1036 * 4. D-SACK added new rule: D-SACK changes any tag to S.
1037 *
1038 * It is pleasant to note, that state diagram turns out to be commutative,
1039 * so that we are allowed not to be bothered by order of our actions,
1040 * when multiple events arrive simultaneously. (see the function below).
1041 *
1042 * Reordering detection.
1043 * --------------------
1044 * Reordering metric is maximal distance, which a packet can be displaced
1045 * in packet stream. With SACKs we can estimate it:
1046 *
1047 * 1. SACK fills old hole and the corresponding segment was not
1048 * ever retransmitted -> reordering. Alas, we cannot use it
1049 * when segment was retransmitted.
1050 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1051 * for retransmitted and already SACKed segment -> reordering..
1052 * Both of these heuristics are not used in Loss state, when we cannot
1053 * account for retransmits accurately.
5b3c9882
IJ
1054 *
1055 * SACK block validation.
1056 * ----------------------
1057 *
1058 * SACK block range validation checks that the received SACK block fits to
1059 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1060 * Note that SND.UNA is not included to the range though being valid because
0e835331
IJ
1061 * it means that the receiver is rather inconsistent with itself reporting
1062 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1063 * perfectly valid, however, in light of RFC2018 which explicitly states
1064 * that "SACK block MUST reflect the newest segment. Even if the newest
1065 * segment is going to be discarded ...", not that it looks very clever
1066 * in case of head skb. Due to potentional receiver driven attacks, we
1067 * choose to avoid immediate execution of a walk in write queue due to
1068 * reneging and defer head skb's loss recovery to standard loss recovery
1069 * procedure that will eventually trigger (nothing forbids us doing this).
5b3c9882
IJ
1070 *
1071 * Implements also blockage to start_seq wrap-around. Problem lies in the
1072 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1073 * there's no guarantee that it will be before snd_nxt (n). The problem
1074 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1075 * wrap (s_w):
1076 *
1077 * <- outs wnd -> <- wrapzone ->
1078 * u e n u_w e_w s n_w
1079 * | | | | | | |
1080 * |<------------+------+----- TCP seqno space --------------+---------->|
1081 * ...-- <2^31 ->| |<--------...
1082 * ...---- >2^31 ------>| |<--------...
1083 *
1084 * Current code wouldn't be vulnerable but it's better still to discard such
1085 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1086 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1087 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1088 * equal to the ideal case (infinite seqno space without wrap caused issues).
1089 *
1090 * With D-SACK the lower bound is extended to cover sequence space below
1091 * SND.UNA down to undo_marker, which is the last point of interest. Yet
564262c1 1092 * again, D-SACK block must not to go across snd_una (for the same reason as
5b3c9882
IJ
1093 * for the normal SACK blocks, explained above). But there all simplicity
1094 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1095 * fully below undo_marker they do not affect behavior in anyway and can
1096 * therefore be safely ignored. In rare cases (which are more or less
1097 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1098 * fragmentation and packet reordering past skb's retransmission. To consider
1099 * them correctly, the acceptable range must be extended even more though
1100 * the exact amount is rather hard to quantify. However, tp->max_window can
1101 * be used as an exaggerated estimate.
1da177e4 1102 */
5b3c9882
IJ
1103static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack,
1104 u32 start_seq, u32 end_seq)
1105{
1106 /* Too far in future, or reversed (interpretation is ambiguous) */
1107 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
1108 return 0;
1109
1110 /* Nasty start_seq wrap-around check (see comments above) */
1111 if (!before(start_seq, tp->snd_nxt))
1112 return 0;
1113
564262c1 1114 /* In outstanding window? ...This is valid exit for D-SACKs too.
5b3c9882
IJ
1115 * start_seq == snd_una is non-sensical (see comments above)
1116 */
1117 if (after(start_seq, tp->snd_una))
1118 return 1;
1119
1120 if (!is_dsack || !tp->undo_marker)
1121 return 0;
1122
1123 /* ...Then it's D-SACK, and must reside below snd_una completely */
1124 if (!after(end_seq, tp->snd_una))
1125 return 0;
1126
1127 if (!before(start_seq, tp->undo_marker))
1128 return 1;
1129
1130 /* Too old */
1131 if (!after(end_seq, tp->undo_marker))
1132 return 0;
1133
1134 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1135 * start_seq < undo_marker and end_seq >= undo_marker.
1136 */
1137 return !before(start_seq, end_seq - tp->max_window);
1138}
1139
1c1e87ed
IJ
1140/* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
1141 * Event "C". Later note: FACK people cheated me again 8), we have to account
1142 * for reordering! Ugly, but should help.
f785a8e2
IJ
1143 *
1144 * Search retransmitted skbs from write_queue that were sent when snd_nxt was
1145 * less than what is now known to be received by the other end (derived from
9f58f3b7
IJ
1146 * highest SACK block). Also calculate the lowest snd_nxt among the remaining
1147 * retransmitted skbs to avoid some costly processing per ACKs.
1c1e87ed 1148 */
407ef1de 1149static void tcp_mark_lost_retrans(struct sock *sk)
1c1e87ed 1150{
9f58f3b7 1151 const struct inet_connection_sock *icsk = inet_csk(sk);
1c1e87ed
IJ
1152 struct tcp_sock *tp = tcp_sk(sk);
1153 struct sk_buff *skb;
f785a8e2 1154 int cnt = 0;
df2e014b 1155 u32 new_low_seq = tp->snd_nxt;
6859d494 1156 u32 received_upto = tcp_highest_sack_seq(tp);
9f58f3b7
IJ
1157
1158 if (!tcp_is_fack(tp) || !tp->retrans_out ||
1159 !after(received_upto, tp->lost_retrans_low) ||
1160 icsk->icsk_ca_state != TCP_CA_Recovery)
407ef1de 1161 return;
1c1e87ed
IJ
1162
1163 tcp_for_write_queue(skb, sk) {
1164 u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
1165
1166 if (skb == tcp_send_head(sk))
1167 break;
f785a8e2 1168 if (cnt == tp->retrans_out)
1c1e87ed
IJ
1169 break;
1170 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1171 continue;
1172
f785a8e2
IJ
1173 if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS))
1174 continue;
1175
d0af4160
IJ
1176 /* TODO: We would like to get rid of tcp_is_fack(tp) only
1177 * constraint here (see above) but figuring out that at
1178 * least tp->reordering SACK blocks reside between ack_seq
1179 * and received_upto is not easy task to do cheaply with
1180 * the available datastructures.
1181 *
1182 * Whether FACK should check here for tp->reordering segs
1183 * in-between one could argue for either way (it would be
1184 * rather simple to implement as we could count fack_count
1185 * during the walk and do tp->fackets_out - fack_count).
1186 */
1187 if (after(received_upto, ack_seq)) {
1c1e87ed
IJ
1188 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
1189 tp->retrans_out -= tcp_skb_pcount(skb);
1190
006f582c 1191 tcp_skb_mark_lost_uncond_verify(tp, skb);
de0744af 1192 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT);
f785a8e2 1193 } else {
df2e014b 1194 if (before(ack_seq, new_low_seq))
b08d6cb2 1195 new_low_seq = ack_seq;
f785a8e2 1196 cnt += tcp_skb_pcount(skb);
1c1e87ed
IJ
1197 }
1198 }
b08d6cb2
IJ
1199
1200 if (tp->retrans_out)
1201 tp->lost_retrans_low = new_low_seq;
1c1e87ed 1202}
5b3c9882 1203
1ed83465 1204static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb,
d06e021d
DM
1205 struct tcp_sack_block_wire *sp, int num_sacks,
1206 u32 prior_snd_una)
1207{
1ed83465 1208 struct tcp_sock *tp = tcp_sk(sk);
d3e2ce3b
HH
1209 u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1210 u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
d06e021d
DM
1211 int dup_sack = 0;
1212
1213 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1214 dup_sack = 1;
e60402d0 1215 tcp_dsack_seen(tp);
de0744af 1216 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
d06e021d 1217 } else if (num_sacks > 1) {
d3e2ce3b
HH
1218 u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1219 u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
d06e021d
DM
1220
1221 if (!after(end_seq_0, end_seq_1) &&
1222 !before(start_seq_0, start_seq_1)) {
1223 dup_sack = 1;
e60402d0 1224 tcp_dsack_seen(tp);
de0744af
PE
1225 NET_INC_STATS_BH(sock_net(sk),
1226 LINUX_MIB_TCPDSACKOFORECV);
d06e021d
DM
1227 }
1228 }
1229
1230 /* D-SACK for already forgotten data... Do dumb counting. */
1231 if (dup_sack &&
1232 !after(end_seq_0, prior_snd_una) &&
1233 after(end_seq_0, tp->undo_marker))
1234 tp->undo_retrans--;
1235
1236 return dup_sack;
1237}
1238
a1197f5a
IJ
1239struct tcp_sacktag_state {
1240 int reord;
1241 int fack_count;
1242 int flag;
1243};
1244
d1935942
IJ
1245/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1246 * the incoming SACK may not exactly match but we can find smaller MSS
1247 * aligned portion of it that matches. Therefore we might need to fragment
1248 * which may fail and creates some hassle (caller must handle error case
1249 * returns).
832d11c5
IJ
1250 *
1251 * FIXME: this could be merged to shift decision code
d1935942 1252 */
0f79efdc
AB
1253static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1254 u32 start_seq, u32 end_seq)
d1935942
IJ
1255{
1256 int in_sack, err;
1257 unsigned int pkt_len;
adb92db8 1258 unsigned int mss;
d1935942
IJ
1259
1260 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1261 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1262
1263 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1264 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
adb92db8 1265 mss = tcp_skb_mss(skb);
d1935942
IJ
1266 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1267
adb92db8 1268 if (!in_sack) {
d1935942 1269 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
adb92db8
IJ
1270 if (pkt_len < mss)
1271 pkt_len = mss;
1272 } else {
d1935942 1273 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
adb92db8
IJ
1274 if (pkt_len < mss)
1275 return -EINVAL;
1276 }
1277
1278 /* Round if necessary so that SACKs cover only full MSSes
1279 * and/or the remaining small portion (if present)
1280 */
1281 if (pkt_len > mss) {
1282 unsigned int new_len = (pkt_len / mss) * mss;
1283 if (!in_sack && new_len < pkt_len) {
1284 new_len += mss;
1285 if (new_len > skb->len)
1286 return 0;
1287 }
1288 pkt_len = new_len;
1289 }
1290 err = tcp_fragment(sk, skb, pkt_len, mss);
d1935942
IJ
1291 if (err < 0)
1292 return err;
1293 }
1294
1295 return in_sack;
1296}
1297
a1197f5a
IJ
1298static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,
1299 struct tcp_sacktag_state *state,
1300 int dup_sack, int pcount)
9e10c47c 1301{
6859d494 1302 struct tcp_sock *tp = tcp_sk(sk);
9e10c47c 1303 u8 sacked = TCP_SKB_CB(skb)->sacked;
a1197f5a 1304 int fack_count = state->fack_count;
9e10c47c
IJ
1305
1306 /* Account D-SACK for retransmitted packet. */
1307 if (dup_sack && (sacked & TCPCB_RETRANS)) {
1308 if (after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker))
1309 tp->undo_retrans--;
ede9f3b1 1310 if (sacked & TCPCB_SACKED_ACKED)
a1197f5a 1311 state->reord = min(fack_count, state->reord);
9e10c47c
IJ
1312 }
1313
1314 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
1315 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
a1197f5a 1316 return sacked;
9e10c47c
IJ
1317
1318 if (!(sacked & TCPCB_SACKED_ACKED)) {
1319 if (sacked & TCPCB_SACKED_RETRANS) {
1320 /* If the segment is not tagged as lost,
1321 * we do not clear RETRANS, believing
1322 * that retransmission is still in flight.
1323 */
1324 if (sacked & TCPCB_LOST) {
a1197f5a 1325 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
f58b22fd
IJ
1326 tp->lost_out -= pcount;
1327 tp->retrans_out -= pcount;
9e10c47c
IJ
1328 }
1329 } else {
1330 if (!(sacked & TCPCB_RETRANS)) {
1331 /* New sack for not retransmitted frame,
1332 * which was in hole. It is reordering.
1333 */
1334 if (before(TCP_SKB_CB(skb)->seq,
1335 tcp_highest_sack_seq(tp)))
a1197f5a
IJ
1336 state->reord = min(fack_count,
1337 state->reord);
9e10c47c
IJ
1338
1339 /* SACK enhanced F-RTO (RFC4138; Appendix B) */
1340 if (!after(TCP_SKB_CB(skb)->end_seq, tp->frto_highmark))
a1197f5a 1341 state->flag |= FLAG_ONLY_ORIG_SACKED;
9e10c47c
IJ
1342 }
1343
1344 if (sacked & TCPCB_LOST) {
a1197f5a 1345 sacked &= ~TCPCB_LOST;
f58b22fd 1346 tp->lost_out -= pcount;
9e10c47c
IJ
1347 }
1348 }
1349
a1197f5a
IJ
1350 sacked |= TCPCB_SACKED_ACKED;
1351 state->flag |= FLAG_DATA_SACKED;
f58b22fd 1352 tp->sacked_out += pcount;
9e10c47c 1353
f58b22fd 1354 fack_count += pcount;
9e10c47c
IJ
1355
1356 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
1357 if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
1358 before(TCP_SKB_CB(skb)->seq,
1359 TCP_SKB_CB(tp->lost_skb_hint)->seq))
f58b22fd 1360 tp->lost_cnt_hint += pcount;
9e10c47c
IJ
1361
1362 if (fack_count > tp->fackets_out)
1363 tp->fackets_out = fack_count;
9e10c47c
IJ
1364 }
1365
1366 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1367 * frames and clear it. undo_retrans is decreased above, L|R frames
1368 * are accounted above as well.
1369 */
a1197f5a
IJ
1370 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1371 sacked &= ~TCPCB_SACKED_RETRANS;
f58b22fd 1372 tp->retrans_out -= pcount;
9e10c47c
IJ
1373 }
1374
a1197f5a 1375 return sacked;
9e10c47c
IJ
1376}
1377
50133161 1378static int tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
a1197f5a 1379 struct tcp_sacktag_state *state,
9ec06ff5
IJ
1380 unsigned int pcount, int shifted, int mss,
1381 int dup_sack)
832d11c5
IJ
1382{
1383 struct tcp_sock *tp = tcp_sk(sk);
50133161 1384 struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
832d11c5
IJ
1385
1386 BUG_ON(!pcount);
1387
92ee76b6
IJ
1388 /* Tweak before seqno plays */
1389 if (!tcp_is_fack(tp) && tcp_is_sack(tp) && tp->lost_skb_hint &&
1390 !before(TCP_SKB_CB(tp->lost_skb_hint)->seq, TCP_SKB_CB(skb)->seq))
1391 tp->lost_cnt_hint += pcount;
1392
832d11c5
IJ
1393 TCP_SKB_CB(prev)->end_seq += shifted;
1394 TCP_SKB_CB(skb)->seq += shifted;
1395
1396 skb_shinfo(prev)->gso_segs += pcount;
1397 BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
1398 skb_shinfo(skb)->gso_segs -= pcount;
1399
1400 /* When we're adding to gso_segs == 1, gso_size will be zero,
1401 * in theory this shouldn't be necessary but as long as DSACK
1402 * code can come after this skb later on it's better to keep
1403 * setting gso_size to something.
1404 */
1405 if (!skb_shinfo(prev)->gso_size) {
1406 skb_shinfo(prev)->gso_size = mss;
1407 skb_shinfo(prev)->gso_type = sk->sk_gso_type;
1408 }
1409
1410 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
1411 if (skb_shinfo(skb)->gso_segs <= 1) {
1412 skb_shinfo(skb)->gso_size = 0;
1413 skb_shinfo(skb)->gso_type = 0;
1414 }
1415
a1197f5a 1416 /* We discard results */
9ec06ff5 1417 tcp_sacktag_one(skb, sk, state, dup_sack, pcount);
832d11c5
IJ
1418
1419 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1420 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1421
832d11c5
IJ
1422 if (skb->len > 0) {
1423 BUG_ON(!tcp_skb_pcount(skb));
111cc8b9 1424 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
832d11c5
IJ
1425 return 0;
1426 }
1427
1428 /* Whole SKB was eaten :-) */
1429
92ee76b6
IJ
1430 if (skb == tp->retransmit_skb_hint)
1431 tp->retransmit_skb_hint = prev;
1432 if (skb == tp->scoreboard_skb_hint)
1433 tp->scoreboard_skb_hint = prev;
1434 if (skb == tp->lost_skb_hint) {
1435 tp->lost_skb_hint = prev;
1436 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1437 }
1438
832d11c5
IJ
1439 TCP_SKB_CB(skb)->flags |= TCP_SKB_CB(prev)->flags;
1440 if (skb == tcp_highest_sack(sk))
1441 tcp_advance_highest_sack(sk, skb);
1442
1443 tcp_unlink_write_queue(skb, sk);
1444 sk_wmem_free_skb(sk, skb);
1445
111cc8b9
IJ
1446 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
1447
832d11c5
IJ
1448 return 1;
1449}
1450
1451/* I wish gso_size would have a bit more sane initialization than
1452 * something-or-zero which complicates things
1453 */
775ffabf 1454static int tcp_skb_seglen(struct sk_buff *skb)
832d11c5 1455{
775ffabf 1456 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
832d11c5
IJ
1457}
1458
1459/* Shifting pages past head area doesn't work */
1460static int skb_can_shift(struct sk_buff *skb)
1461{
1462 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1463}
1464
1465/* Try collapsing SACK blocks spanning across multiple skbs to a single
1466 * skb.
1467 */
1468static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
a1197f5a 1469 struct tcp_sacktag_state *state,
832d11c5 1470 u32 start_seq, u32 end_seq,
a1197f5a 1471 int dup_sack)
832d11c5
IJ
1472{
1473 struct tcp_sock *tp = tcp_sk(sk);
1474 struct sk_buff *prev;
1475 int mss;
1476 int pcount = 0;
1477 int len;
1478 int in_sack;
1479
1480 if (!sk_can_gso(sk))
1481 goto fallback;
1482
1483 /* Normally R but no L won't result in plain S */
1484 if (!dup_sack &&
9969ca5f 1485 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
832d11c5
IJ
1486 goto fallback;
1487 if (!skb_can_shift(skb))
1488 goto fallback;
1489 /* This frame is about to be dropped (was ACKed). */
1490 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1491 goto fallback;
1492
1493 /* Can only happen with delayed DSACK + discard craziness */
1494 if (unlikely(skb == tcp_write_queue_head(sk)))
1495 goto fallback;
1496 prev = tcp_write_queue_prev(sk, skb);
1497
1498 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1499 goto fallback;
1500
1501 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1502 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1503
1504 if (in_sack) {
1505 len = skb->len;
1506 pcount = tcp_skb_pcount(skb);
775ffabf 1507 mss = tcp_skb_seglen(skb);
832d11c5
IJ
1508
1509 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1510 * drop this restriction as unnecessary
1511 */
775ffabf 1512 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1513 goto fallback;
1514 } else {
1515 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1516 goto noop;
1517 /* CHECKME: This is non-MSS split case only?, this will
1518 * cause skipped skbs due to advancing loop btw, original
1519 * has that feature too
1520 */
1521 if (tcp_skb_pcount(skb) <= 1)
1522 goto noop;
1523
1524 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1525 if (!in_sack) {
1526 /* TODO: head merge to next could be attempted here
1527 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1528 * though it might not be worth of the additional hassle
1529 *
1530 * ...we can probably just fallback to what was done
1531 * previously. We could try merging non-SACKed ones
1532 * as well but it probably isn't going to buy off
1533 * because later SACKs might again split them, and
1534 * it would make skb timestamp tracking considerably
1535 * harder problem.
1536 */
1537 goto fallback;
1538 }
1539
1540 len = end_seq - TCP_SKB_CB(skb)->seq;
1541 BUG_ON(len < 0);
1542 BUG_ON(len > skb->len);
1543
1544 /* MSS boundaries should be honoured or else pcount will
1545 * severely break even though it makes things bit trickier.
1546 * Optimize common case to avoid most of the divides
1547 */
1548 mss = tcp_skb_mss(skb);
1549
1550 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1551 * drop this restriction as unnecessary
1552 */
775ffabf 1553 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1554 goto fallback;
1555
1556 if (len == mss) {
1557 pcount = 1;
1558 } else if (len < mss) {
1559 goto noop;
1560 } else {
1561 pcount = len / mss;
1562 len = pcount * mss;
1563 }
1564 }
1565
1566 if (!skb_shift(prev, skb, len))
1567 goto fallback;
9ec06ff5 1568 if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
832d11c5
IJ
1569 goto out;
1570
1571 /* Hole filled allows collapsing with the next as well, this is very
1572 * useful when hole on every nth skb pattern happens
1573 */
1574 if (prev == tcp_write_queue_tail(sk))
1575 goto out;
1576 skb = tcp_write_queue_next(sk, prev);
1577
f0bc52f3
IJ
1578 if (!skb_can_shift(skb) ||
1579 (skb == tcp_send_head(sk)) ||
1580 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
775ffabf 1581 (mss != tcp_skb_seglen(skb)))
832d11c5
IJ
1582 goto out;
1583
1584 len = skb->len;
1585 if (skb_shift(prev, skb, len)) {
1586 pcount += tcp_skb_pcount(skb);
9ec06ff5 1587 tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
832d11c5
IJ
1588 }
1589
1590out:
a1197f5a 1591 state->fack_count += pcount;
832d11c5
IJ
1592 return prev;
1593
1594noop:
1595 return skb;
1596
1597fallback:
111cc8b9 1598 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
832d11c5
IJ
1599 return NULL;
1600}
1601
68f8353b
IJ
1602static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1603 struct tcp_sack_block *next_dup,
a1197f5a 1604 struct tcp_sacktag_state *state,
68f8353b 1605 u32 start_seq, u32 end_seq,
a1197f5a 1606 int dup_sack_in)
68f8353b 1607{
832d11c5
IJ
1608 struct tcp_sock *tp = tcp_sk(sk);
1609 struct sk_buff *tmp;
1610
68f8353b
IJ
1611 tcp_for_write_queue_from(skb, sk) {
1612 int in_sack = 0;
1613 int dup_sack = dup_sack_in;
1614
1615 if (skb == tcp_send_head(sk))
1616 break;
1617
1618 /* queue is in-order => we can short-circuit the walk early */
1619 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1620 break;
1621
1622 if ((next_dup != NULL) &&
1623 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1624 in_sack = tcp_match_skb_to_sack(sk, skb,
1625 next_dup->start_seq,
1626 next_dup->end_seq);
1627 if (in_sack > 0)
1628 dup_sack = 1;
1629 }
1630
832d11c5
IJ
1631 /* skb reference here is a bit tricky to get right, since
1632 * shifting can eat and free both this skb and the next,
1633 * so not even _safe variant of the loop is enough.
1634 */
1635 if (in_sack <= 0) {
a1197f5a
IJ
1636 tmp = tcp_shift_skb_data(sk, skb, state,
1637 start_seq, end_seq, dup_sack);
832d11c5
IJ
1638 if (tmp != NULL) {
1639 if (tmp != skb) {
1640 skb = tmp;
1641 continue;
1642 }
1643
1644 in_sack = 0;
1645 } else {
1646 in_sack = tcp_match_skb_to_sack(sk, skb,
1647 start_seq,
1648 end_seq);
1649 }
1650 }
1651
68f8353b
IJ
1652 if (unlikely(in_sack < 0))
1653 break;
1654
832d11c5 1655 if (in_sack) {
a1197f5a
IJ
1656 TCP_SKB_CB(skb)->sacked = tcp_sacktag_one(skb, sk,
1657 state,
1658 dup_sack,
1659 tcp_skb_pcount(skb));
68f8353b 1660
832d11c5
IJ
1661 if (!before(TCP_SKB_CB(skb)->seq,
1662 tcp_highest_sack_seq(tp)))
1663 tcp_advance_highest_sack(sk, skb);
1664 }
1665
a1197f5a 1666 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1667 }
1668 return skb;
1669}
1670
1671/* Avoid all extra work that is being done by sacktag while walking in
1672 * a normal way
1673 */
1674static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
a1197f5a
IJ
1675 struct tcp_sacktag_state *state,
1676 u32 skip_to_seq)
68f8353b
IJ
1677{
1678 tcp_for_write_queue_from(skb, sk) {
1679 if (skb == tcp_send_head(sk))
1680 break;
1681
e8bae275 1682 if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
68f8353b 1683 break;
d152a7d8 1684
a1197f5a 1685 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1686 }
1687 return skb;
1688}
1689
1690static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1691 struct sock *sk,
1692 struct tcp_sack_block *next_dup,
a1197f5a
IJ
1693 struct tcp_sacktag_state *state,
1694 u32 skip_to_seq)
68f8353b
IJ
1695{
1696 if (next_dup == NULL)
1697 return skb;
1698
1699 if (before(next_dup->start_seq, skip_to_seq)) {
a1197f5a
IJ
1700 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1701 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1702 next_dup->start_seq, next_dup->end_seq,
1703 1);
68f8353b
IJ
1704 }
1705
1706 return skb;
1707}
1708
1709static int tcp_sack_cache_ok(struct tcp_sock *tp, struct tcp_sack_block *cache)
1710{
1711 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1712}
1713
1da177e4 1714static int
056834d9
IJ
1715tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb,
1716 u32 prior_snd_una)
1da177e4 1717{
6687e988 1718 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 1719 struct tcp_sock *tp = tcp_sk(sk);
9c70220b
ACM
1720 unsigned char *ptr = (skb_transport_header(ack_skb) +
1721 TCP_SKB_CB(ack_skb)->sacked);
fd6dad61 1722 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
4389dded 1723 struct tcp_sack_block sp[TCP_NUM_SACKS];
68f8353b 1724 struct tcp_sack_block *cache;
a1197f5a 1725 struct tcp_sacktag_state state;
68f8353b 1726 struct sk_buff *skb;
4389dded 1727 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
fd6dad61 1728 int used_sacks;
7769f406 1729 int found_dup_sack = 0;
68f8353b 1730 int i, j;
fda03fbb 1731 int first_sack_index;
1da177e4 1732
a1197f5a
IJ
1733 state.flag = 0;
1734 state.reord = tp->packets_out;
1735
d738cd8f 1736 if (!tp->sacked_out) {
de83c058
IJ
1737 if (WARN_ON(tp->fackets_out))
1738 tp->fackets_out = 0;
6859d494 1739 tcp_highest_sack_reset(sk);
d738cd8f 1740 }
1da177e4 1741
1ed83465 1742 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
d06e021d
DM
1743 num_sacks, prior_snd_una);
1744 if (found_dup_sack)
a1197f5a 1745 state.flag |= FLAG_DSACKING_ACK;
6f74651a
BE
1746
1747 /* Eliminate too old ACKs, but take into
1748 * account more or less fresh ones, they can
1749 * contain valid SACK info.
1750 */
1751 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1752 return 0;
1753