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