Merge commit 'v2.6.34-rc2' into perf/core
[linux-2.6-block.git] / net / ipv4 / tcp_output.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: Pedro Roque : Retransmit queue handled by TCP.
23 * : Fragmentation on mtu decrease
24 * : Segment collapse on retransmit
25 * : AF independence
26 *
27 * Linus Torvalds : send_delayed_ack
28 * David S. Miller : Charge memory using the right skb
29 * during syn/ack processing.
30 * David S. Miller : Output engine completely rewritten.
31 * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
32 * Cacophonix Gaul : draft-minshall-nagle-01
33 * J Hadi Salim : ECN support
34 *
35 */
36
37#include <net/tcp.h>
38
39#include <linux/compiler.h>
40#include <linux/module.h>
1da177e4
LT
41
42/* People can turn this off for buggy TCP's found in printers etc. */
ab32ea5d 43int sysctl_tcp_retrans_collapse __read_mostly = 1;
1da177e4 44
09cb105e 45/* People can turn this on to work with those rare, broken TCPs that
15d99e02
RJ
46 * interpret the window field as a signed quantity.
47 */
ab32ea5d 48int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
15d99e02 49
1da177e4
LT
50/* This limits the percentage of the congestion window which we
51 * will allow a single TSO frame to consume. Building TSO frames
52 * which are too large can cause TCP streams to be bursty.
53 */
ab32ea5d 54int sysctl_tcp_tso_win_divisor __read_mostly = 3;
1da177e4 55
ab32ea5d
BH
56int sysctl_tcp_mtu_probing __read_mostly = 0;
57int sysctl_tcp_base_mss __read_mostly = 512;
5d424d5a 58
35089bb2 59/* By default, RFC2861 behavior. */
ab32ea5d 60int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
35089bb2 61
519855c5 62int sysctl_tcp_cookie_size __read_mostly = 0; /* TCP_COOKIE_MAX */
e6b09cca 63EXPORT_SYMBOL_GPL(sysctl_tcp_cookie_size);
519855c5
WAS
64
65
67edfef7 66/* Account for new data that has been sent to the network. */
66f5fe62 67static void tcp_event_new_data_sent(struct sock *sk, struct sk_buff *skb)
1da177e4 68{
9e412ba7 69 struct tcp_sock *tp = tcp_sk(sk);
66f5fe62 70 unsigned int prior_packets = tp->packets_out;
9e412ba7 71
fe067e8a 72 tcp_advance_send_head(sk, skb);
1da177e4 73 tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
8512430e
IJ
74
75 /* Don't override Nagle indefinately with F-RTO */
76 if (tp->frto_counter == 2)
77 tp->frto_counter = 3;
66f5fe62
IJ
78
79 tp->packets_out += tcp_skb_pcount(skb);
80 if (!prior_packets)
81 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
82 inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
1da177e4
LT
83}
84
85/* SND.NXT, if window was not shrunk.
86 * If window has been shrunk, what should we make? It is not clear at all.
87 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
88 * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
89 * invalid. OK, let's make this for now:
90 */
9e412ba7 91static inline __u32 tcp_acceptable_seq(struct sock *sk)
1da177e4 92{
9e412ba7
IJ
93 struct tcp_sock *tp = tcp_sk(sk);
94
90840def 95 if (!before(tcp_wnd_end(tp), tp->snd_nxt))
1da177e4
LT
96 return tp->snd_nxt;
97 else
90840def 98 return tcp_wnd_end(tp);
1da177e4
LT
99}
100
101/* Calculate mss to advertise in SYN segment.
102 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
103 *
104 * 1. It is independent of path mtu.
105 * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
106 * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
107 * attached devices, because some buggy hosts are confused by
108 * large MSS.
109 * 4. We do not make 3, we advertise MSS, calculated from first
110 * hop device mtu, but allow to raise it to ip_rt_min_advmss.
111 * This may be overridden via information stored in routing table.
112 * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
113 * probably even Jumbo".
114 */
115static __u16 tcp_advertise_mss(struct sock *sk)
116{
117 struct tcp_sock *tp = tcp_sk(sk);
118 struct dst_entry *dst = __sk_dst_get(sk);
119 int mss = tp->advmss;
120
121 if (dst && dst_metric(dst, RTAX_ADVMSS) < mss) {
122 mss = dst_metric(dst, RTAX_ADVMSS);
123 tp->advmss = mss;
124 }
125
126 return (__u16)mss;
127}
128
129/* RFC2861. Reset CWND after idle period longer RTO to "restart window".
130 * This is the first part of cwnd validation mechanism. */
463c84b9 131static void tcp_cwnd_restart(struct sock *sk, struct dst_entry *dst)
1da177e4 132{
463c84b9 133 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
134 s32 delta = tcp_time_stamp - tp->lsndtime;
135 u32 restart_cwnd = tcp_init_cwnd(tp, dst);
136 u32 cwnd = tp->snd_cwnd;
137
6687e988 138 tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
1da177e4 139
6687e988 140 tp->snd_ssthresh = tcp_current_ssthresh(sk);
1da177e4
LT
141 restart_cwnd = min(restart_cwnd, cwnd);
142
463c84b9 143 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
1da177e4
LT
144 cwnd >>= 1;
145 tp->snd_cwnd = max(cwnd, restart_cwnd);
146 tp->snd_cwnd_stamp = tcp_time_stamp;
147 tp->snd_cwnd_used = 0;
148}
149
67edfef7 150/* Congestion state accounting after a packet has been sent. */
40efc6fa
SH
151static void tcp_event_data_sent(struct tcp_sock *tp,
152 struct sk_buff *skb, struct sock *sk)
1da177e4 153{
463c84b9
ACM
154 struct inet_connection_sock *icsk = inet_csk(sk);
155 const u32 now = tcp_time_stamp;
1da177e4 156
35089bb2
DM
157 if (sysctl_tcp_slow_start_after_idle &&
158 (!tp->packets_out && (s32)(now - tp->lsndtime) > icsk->icsk_rto))
463c84b9 159 tcp_cwnd_restart(sk, __sk_dst_get(sk));
1da177e4
LT
160
161 tp->lsndtime = now;
162
163 /* If it is a reply for ato after last received
164 * packet, enter pingpong mode.
165 */
463c84b9
ACM
166 if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
167 icsk->icsk_ack.pingpong = 1;
1da177e4
LT
168}
169
67edfef7 170/* Account for an ACK we sent. */
40efc6fa 171static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
1da177e4 172{
463c84b9
ACM
173 tcp_dec_quickack_mode(sk, pkts);
174 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
1da177e4
LT
175}
176
177/* Determine a window scaling and initial window to offer.
178 * Based on the assumption that the given amount of space
179 * will be offered. Store the results in the tp structure.
180 * NOTE: for smooth operation initial space offering should
181 * be a multiple of mss if possible. We assume here that mss >= 1.
182 * This MUST be enforced by all callers.
183 */
184void tcp_select_initial_window(int __space, __u32 mss,
185 __u32 *rcv_wnd, __u32 *window_clamp,
31d12926 186 int wscale_ok, __u8 *rcv_wscale,
187 __u32 init_rcv_wnd)
1da177e4
LT
188{
189 unsigned int space = (__space < 0 ? 0 : __space);
190
191 /* If no clamp set the clamp to the max possible scaled window */
192 if (*window_clamp == 0)
193 (*window_clamp) = (65535 << 14);
194 space = min(*window_clamp, space);
195
196 /* Quantize space offering to a multiple of mss if possible. */
197 if (space > mss)
198 space = (space / mss) * mss;
199
200 /* NOTE: offering an initial window larger than 32767
15d99e02
RJ
201 * will break some buggy TCP stacks. If the admin tells us
202 * it is likely we could be speaking with such a buggy stack
203 * we will truncate our initial window offering to 32K-1
204 * unless the remote has sent us a window scaling option,
205 * which we interpret as a sign the remote TCP is not
206 * misinterpreting the window field as a signed quantity.
1da177e4 207 */
15d99e02
RJ
208 if (sysctl_tcp_workaround_signed_windows)
209 (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
210 else
211 (*rcv_wnd) = space;
212
1da177e4
LT
213 (*rcv_wscale) = 0;
214 if (wscale_ok) {
215 /* Set window scaling on max possible window
e905a9ed 216 * See RFC1323 for an explanation of the limit to 14
1da177e4
LT
217 */
218 space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
316c1592 219 space = min_t(u32, space, *window_clamp);
1da177e4
LT
220 while (space > 65535 && (*rcv_wscale) < 14) {
221 space >>= 1;
222 (*rcv_wscale)++;
223 }
224 }
225
226 /* Set initial window to value enough for senders,
6b251858 227 * following RFC2414. Senders, not following this RFC,
1da177e4
LT
228 * will be satisfied with 2.
229 */
056834d9 230 if (mss > (1 << *rcv_wscale)) {
01ff367e 231 int init_cwnd = 4;
056834d9 232 if (mss > 1460 * 3)
1da177e4 233 init_cwnd = 2;
01ff367e
DM
234 else if (mss > 1460)
235 init_cwnd = 3;
31d12926 236 /* when initializing use the value from init_rcv_wnd
237 * rather than the default from above
238 */
239 if (init_rcv_wnd &&
240 (*rcv_wnd > init_rcv_wnd * mss))
241 *rcv_wnd = init_rcv_wnd * mss;
242 else if (*rcv_wnd > init_cwnd * mss)
056834d9 243 *rcv_wnd = init_cwnd * mss;
1da177e4
LT
244 }
245
246 /* Set the clamp no higher than max representable value */
247 (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
248}
249
250/* Chose a new window to advertise, update state in tcp_sock for the
251 * socket, and return result with RFC1323 scaling applied. The return
252 * value can be stuffed directly into th->window for an outgoing
253 * frame.
254 */
40efc6fa 255static u16 tcp_select_window(struct sock *sk)
1da177e4
LT
256{
257 struct tcp_sock *tp = tcp_sk(sk);
258 u32 cur_win = tcp_receive_window(tp);
259 u32 new_win = __tcp_select_window(sk);
260
261 /* Never shrink the offered window */
2de979bd 262 if (new_win < cur_win) {
1da177e4
LT
263 /* Danger Will Robinson!
264 * Don't update rcv_wup/rcv_wnd here or else
265 * we will not be able to advertise a zero
266 * window in time. --DaveM
267 *
268 * Relax Will Robinson.
269 */
607bfbf2 270 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale);
1da177e4
LT
271 }
272 tp->rcv_wnd = new_win;
273 tp->rcv_wup = tp->rcv_nxt;
274
275 /* Make sure we do not exceed the maximum possible
276 * scaled window.
277 */
15d99e02 278 if (!tp->rx_opt.rcv_wscale && sysctl_tcp_workaround_signed_windows)
1da177e4
LT
279 new_win = min(new_win, MAX_TCP_WINDOW);
280 else
281 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
282
283 /* RFC1323 scaling applied */
284 new_win >>= tp->rx_opt.rcv_wscale;
285
286 /* If we advertise zero window, disable fast path. */
287 if (new_win == 0)
288 tp->pred_flags = 0;
289
290 return new_win;
291}
292
67edfef7 293/* Packet ECN state for a SYN-ACK */
056834d9 294static inline void TCP_ECN_send_synack(struct tcp_sock *tp, struct sk_buff *skb)
bdf1ee5d
IJ
295{
296 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_CWR;
056834d9 297 if (!(tp->ecn_flags & TCP_ECN_OK))
bdf1ee5d
IJ
298 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_ECE;
299}
300
67edfef7 301/* Packet ECN state for a SYN. */
bdf1ee5d
IJ
302static inline void TCP_ECN_send_syn(struct sock *sk, struct sk_buff *skb)
303{
304 struct tcp_sock *tp = tcp_sk(sk);
305
306 tp->ecn_flags = 0;
255cac91 307 if (sysctl_tcp_ecn == 1) {
056834d9 308 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ECE | TCPCB_FLAG_CWR;
bdf1ee5d
IJ
309 tp->ecn_flags = TCP_ECN_OK;
310 }
311}
312
313static __inline__ void
314TCP_ECN_make_synack(struct request_sock *req, struct tcphdr *th)
315{
316 if (inet_rsk(req)->ecn_ok)
317 th->ece = 1;
318}
319
67edfef7
AK
320/* Set up ECN state for a packet on a ESTABLISHED socket that is about to
321 * be sent.
322 */
bdf1ee5d
IJ
323static inline void TCP_ECN_send(struct sock *sk, struct sk_buff *skb,
324 int tcp_header_len)
325{
326 struct tcp_sock *tp = tcp_sk(sk);
327
328 if (tp->ecn_flags & TCP_ECN_OK) {
329 /* Not-retransmitted data segment: set ECT and inject CWR. */
330 if (skb->len != tcp_header_len &&
331 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {
332 INET_ECN_xmit(sk);
056834d9 333 if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) {
bdf1ee5d
IJ
334 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
335 tcp_hdr(skb)->cwr = 1;
336 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
337 }
338 } else {
339 /* ACK or retransmitted segment: clear ECT|CE */
340 INET_ECN_dontxmit(sk);
341 }
342 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR)
343 tcp_hdr(skb)->ece = 1;
344 }
345}
346
e870a8ef
IJ
347/* Constructs common control bits of non-data skb. If SYN/FIN is present,
348 * auto increment end seqno.
349 */
350static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
351{
352 skb->csum = 0;
353
354 TCP_SKB_CB(skb)->flags = flags;
355 TCP_SKB_CB(skb)->sacked = 0;
356
357 skb_shinfo(skb)->gso_segs = 1;
358 skb_shinfo(skb)->gso_size = 0;
359 skb_shinfo(skb)->gso_type = 0;
360
361 TCP_SKB_CB(skb)->seq = seq;
362 if (flags & (TCPCB_FLAG_SYN | TCPCB_FLAG_FIN))
363 seq++;
364 TCP_SKB_CB(skb)->end_seq = seq;
365}
366
33f5f57e
IJ
367static inline int tcp_urg_mode(const struct tcp_sock *tp)
368{
369 return tp->snd_una != tp->snd_up;
370}
371
33ad798c
AL
372#define OPTION_SACK_ADVERTISE (1 << 0)
373#define OPTION_TS (1 << 1)
374#define OPTION_MD5 (1 << 2)
89e95a61 375#define OPTION_WSCALE (1 << 3)
bd0388ae 376#define OPTION_COOKIE_EXTENSION (1 << 4)
33ad798c
AL
377
378struct tcp_out_options {
379 u8 options; /* bit field of OPTION_* */
380 u8 ws; /* window scale, 0 to disable */
381 u8 num_sack_blocks; /* number of SACK blocks to include */
bd0388ae 382 u8 hash_size; /* bytes in hash_location */
33ad798c
AL
383 u16 mss; /* 0 to disable */
384 __u32 tsval, tsecr; /* need to include OPTION_TS */
bd0388ae 385 __u8 *hash_location; /* temporary pointer, overloaded */
33ad798c
AL
386};
387
bd0388ae
WAS
388/* The sysctl int routines are generic, so check consistency here.
389 */
390static u8 tcp_cookie_size_check(u8 desired)
391{
392 if (desired > 0) {
393 /* previously specified */
394 return desired;
395 }
396 if (sysctl_tcp_cookie_size <= 0) {
397 /* no default specified */
398 return 0;
399 }
400 if (sysctl_tcp_cookie_size <= TCP_COOKIE_MIN) {
401 /* value too small, specify minimum */
402 return TCP_COOKIE_MIN;
403 }
404 if (sysctl_tcp_cookie_size >= TCP_COOKIE_MAX) {
405 /* value too large, specify maximum */
406 return TCP_COOKIE_MAX;
407 }
408 if (0x1 & sysctl_tcp_cookie_size) {
409 /* 8-bit multiple, illegal, fix it */
410 return (u8)(sysctl_tcp_cookie_size + 0x1);
411 }
412 return (u8)sysctl_tcp_cookie_size;
413}
414
67edfef7
AK
415/* Write previously computed TCP options to the packet.
416 *
417 * Beware: Something in the Internet is very sensitive to the ordering of
fd6149d3
IJ
418 * TCP options, we learned this through the hard way, so be careful here.
419 * Luckily we can at least blame others for their non-compliance but from
420 * inter-operatibility perspective it seems that we're somewhat stuck with
421 * the ordering which we have been using if we want to keep working with
422 * those broken things (not that it currently hurts anybody as there isn't
423 * particular reason why the ordering would need to be changed).
424 *
425 * At least SACK_PERM as the first option is known to lead to a disaster
426 * (but it may well be that other scenarios fail similarly).
427 */
33ad798c 428static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
bd0388ae
WAS
429 struct tcp_out_options *opts)
430{
431 u8 options = opts->options; /* mungable copy */
432
433 /* Having both authentication and cookies for security is redundant,
434 * and there's certainly not enough room. Instead, the cookie-less
435 * extension variant is proposed.
436 *
437 * Consider the pessimal case with authentication. The options
438 * could look like:
439 * COOKIE|MD5(20) + MSS(4) + SACK|TS(12) + WSCALE(4) == 40
440 */
441 if (unlikely(OPTION_MD5 & options)) {
442 if (unlikely(OPTION_COOKIE_EXTENSION & options)) {
443 *ptr++ = htonl((TCPOPT_COOKIE << 24) |
444 (TCPOLEN_COOKIE_BASE << 16) |
445 (TCPOPT_MD5SIG << 8) |
446 TCPOLEN_MD5SIG);
447 } else {
448 *ptr++ = htonl((TCPOPT_NOP << 24) |
449 (TCPOPT_NOP << 16) |
450 (TCPOPT_MD5SIG << 8) |
451 TCPOLEN_MD5SIG);
452 }
453 options &= ~OPTION_COOKIE_EXTENSION;
454 /* overload cookie hash location */
455 opts->hash_location = (__u8 *)ptr;
33ad798c 456 ptr += 4;
40efc6fa 457 }
33ad798c 458
fd6149d3
IJ
459 if (unlikely(opts->mss)) {
460 *ptr++ = htonl((TCPOPT_MSS << 24) |
461 (TCPOLEN_MSS << 16) |
462 opts->mss);
463 }
464
bd0388ae
WAS
465 if (likely(OPTION_TS & options)) {
466 if (unlikely(OPTION_SACK_ADVERTISE & options)) {
33ad798c
AL
467 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
468 (TCPOLEN_SACK_PERM << 16) |
469 (TCPOPT_TIMESTAMP << 8) |
470 TCPOLEN_TIMESTAMP);
bd0388ae 471 options &= ~OPTION_SACK_ADVERTISE;
33ad798c
AL
472 } else {
473 *ptr++ = htonl((TCPOPT_NOP << 24) |
474 (TCPOPT_NOP << 16) |
475 (TCPOPT_TIMESTAMP << 8) |
476 TCPOLEN_TIMESTAMP);
477 }
478 *ptr++ = htonl(opts->tsval);
479 *ptr++ = htonl(opts->tsecr);
480 }
481
bd0388ae
WAS
482 /* Specification requires after timestamp, so do it now.
483 *
484 * Consider the pessimal case without authentication. The options
485 * could look like:
486 * MSS(4) + SACK|TS(12) + COOKIE(20) + WSCALE(4) == 40
487 */
488 if (unlikely(OPTION_COOKIE_EXTENSION & options)) {
489 __u8 *cookie_copy = opts->hash_location;
490 u8 cookie_size = opts->hash_size;
491
492 /* 8-bit multiple handled in tcp_cookie_size_check() above,
493 * and elsewhere.
494 */
495 if (0x2 & cookie_size) {
496 __u8 *p = (__u8 *)ptr;
497
498 /* 16-bit multiple */
499 *p++ = TCPOPT_COOKIE;
500 *p++ = TCPOLEN_COOKIE_BASE + cookie_size;
501 *p++ = *cookie_copy++;
502 *p++ = *cookie_copy++;
503 ptr++;
504 cookie_size -= 2;
505 } else {
506 /* 32-bit multiple */
507 *ptr++ = htonl(((TCPOPT_NOP << 24) |
508 (TCPOPT_NOP << 16) |
509 (TCPOPT_COOKIE << 8) |
510 TCPOLEN_COOKIE_BASE) +
511 cookie_size);
512 }
513
514 if (cookie_size > 0) {
515 memcpy(ptr, cookie_copy, cookie_size);
516 ptr += (cookie_size / 4);
517 }
518 }
519
520 if (unlikely(OPTION_SACK_ADVERTISE & options)) {
33ad798c
AL
521 *ptr++ = htonl((TCPOPT_NOP << 24) |
522 (TCPOPT_NOP << 16) |
523 (TCPOPT_SACK_PERM << 8) |
524 TCPOLEN_SACK_PERM);
525 }
526
bd0388ae 527 if (unlikely(OPTION_WSCALE & options)) {
33ad798c
AL
528 *ptr++ = htonl((TCPOPT_NOP << 24) |
529 (TCPOPT_WINDOW << 16) |
530 (TCPOLEN_WINDOW << 8) |
531 opts->ws);
532 }
533
534 if (unlikely(opts->num_sack_blocks)) {
535 struct tcp_sack_block *sp = tp->rx_opt.dsack ?
536 tp->duplicate_sack : tp->selective_acks;
40efc6fa
SH
537 int this_sack;
538
539 *ptr++ = htonl((TCPOPT_NOP << 24) |
540 (TCPOPT_NOP << 16) |
541 (TCPOPT_SACK << 8) |
33ad798c 542 (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
40efc6fa 543 TCPOLEN_SACK_PERBLOCK)));
2de979bd 544
33ad798c
AL
545 for (this_sack = 0; this_sack < opts->num_sack_blocks;
546 ++this_sack) {
40efc6fa
SH
547 *ptr++ = htonl(sp[this_sack].start_seq);
548 *ptr++ = htonl(sp[this_sack].end_seq);
549 }
2de979bd 550
5861f8e5 551 tp->rx_opt.dsack = 0;
40efc6fa 552 }
33ad798c
AL
553}
554
67edfef7
AK
555/* Compute TCP options for SYN packets. This is not the final
556 * network wire format yet.
557 */
33ad798c
AL
558static unsigned tcp_syn_options(struct sock *sk, struct sk_buff *skb,
559 struct tcp_out_options *opts,
560 struct tcp_md5sig_key **md5) {
561 struct tcp_sock *tp = tcp_sk(sk);
bd0388ae 562 struct tcp_cookie_values *cvp = tp->cookie_values;
bd0388ae
WAS
563 unsigned remaining = MAX_TCP_OPTION_SPACE;
564 u8 cookie_size = (!tp->rx_opt.cookie_out_never && cvp != NULL) ?
565 tcp_cookie_size_check(cvp->cookie_desired) :
566 0;
33ad798c 567
cfb6eeb4 568#ifdef CONFIG_TCP_MD5SIG
33ad798c
AL
569 *md5 = tp->af_specific->md5_lookup(sk, sk);
570 if (*md5) {
571 opts->options |= OPTION_MD5;
bd0388ae 572 remaining -= TCPOLEN_MD5SIG_ALIGNED;
cfb6eeb4 573 }
33ad798c
AL
574#else
575 *md5 = NULL;
cfb6eeb4 576#endif
33ad798c
AL
577
578 /* We always get an MSS option. The option bytes which will be seen in
579 * normal data packets should timestamps be used, must be in the MSS
580 * advertised. But we subtract them from tp->mss_cache so that
581 * calculations in tcp_sendmsg are simpler etc. So account for this
582 * fact here if necessary. If we don't do this correctly, as a
583 * receiver we won't recognize data packets as being full sized when we
584 * should, and thus we won't abide by the delayed ACK rules correctly.
585 * SACKs don't matter, we never delay an ACK when we have any of those
586 * going out. */
587 opts->mss = tcp_advertise_mss(sk);
bd0388ae 588 remaining -= TCPOLEN_MSS_ALIGNED;
33ad798c 589
bb5b7c11 590 if (likely(sysctl_tcp_timestamps && *md5 == NULL)) {
33ad798c
AL
591 opts->options |= OPTION_TS;
592 opts->tsval = TCP_SKB_CB(skb)->when;
593 opts->tsecr = tp->rx_opt.ts_recent;
bd0388ae 594 remaining -= TCPOLEN_TSTAMP_ALIGNED;
33ad798c 595 }
bb5b7c11 596 if (likely(sysctl_tcp_window_scaling)) {
33ad798c 597 opts->ws = tp->rx_opt.rcv_wscale;
89e95a61 598 opts->options |= OPTION_WSCALE;
bd0388ae 599 remaining -= TCPOLEN_WSCALE_ALIGNED;
33ad798c 600 }
bb5b7c11 601 if (likely(sysctl_tcp_sack)) {
33ad798c 602 opts->options |= OPTION_SACK_ADVERTISE;
b32d1310 603 if (unlikely(!(OPTION_TS & opts->options)))
bd0388ae 604 remaining -= TCPOLEN_SACKPERM_ALIGNED;
33ad798c
AL
605 }
606
bd0388ae
WAS
607 /* Note that timestamps are required by the specification.
608 *
609 * Odd numbers of bytes are prohibited by the specification, ensuring
610 * that the cookie is 16-bit aligned, and the resulting cookie pair is
611 * 32-bit aligned.
612 */
613 if (*md5 == NULL &&
614 (OPTION_TS & opts->options) &&
615 cookie_size > 0) {
616 int need = TCPOLEN_COOKIE_BASE + cookie_size;
617
618 if (0x2 & need) {
619 /* 32-bit multiple */
620 need += 2; /* NOPs */
621
622 if (need > remaining) {
623 /* try shrinking cookie to fit */
624 cookie_size -= 2;
625 need -= 4;
626 }
627 }
628 while (need > remaining && TCP_COOKIE_MIN <= cookie_size) {
629 cookie_size -= 4;
630 need -= 4;
631 }
632 if (TCP_COOKIE_MIN <= cookie_size) {
633 opts->options |= OPTION_COOKIE_EXTENSION;
634 opts->hash_location = (__u8 *)&cvp->cookie_pair[0];
635 opts->hash_size = cookie_size;
636
637 /* Remember for future incarnations. */
638 cvp->cookie_desired = cookie_size;
639
640 if (cvp->cookie_desired != cvp->cookie_pair_size) {
641 /* Currently use random bytes as a nonce,
642 * assuming these are completely unpredictable
643 * by hostile users of the same system.
644 */
645 get_random_bytes(&cvp->cookie_pair[0],
646 cookie_size);
647 cvp->cookie_pair_size = cookie_size;
648 }
649
650 remaining -= need;
651 }
652 }
653 return MAX_TCP_OPTION_SPACE - remaining;
40efc6fa
SH
654}
655
67edfef7 656/* Set up TCP options for SYN-ACKs. */
33ad798c
AL
657static unsigned tcp_synack_options(struct sock *sk,
658 struct request_sock *req,
659 unsigned mss, struct sk_buff *skb,
660 struct tcp_out_options *opts,
4957faad
WAS
661 struct tcp_md5sig_key **md5,
662 struct tcp_extend_values *xvp)
663{
33ad798c 664 struct inet_request_sock *ireq = inet_rsk(req);
4957faad
WAS
665 unsigned remaining = MAX_TCP_OPTION_SPACE;
666 u8 cookie_plus = (xvp != NULL && !xvp->cookie_out_never) ?
667 xvp->cookie_plus :
668 0;
669 bool doing_ts = ireq->tstamp_ok;
33ad798c 670
cfb6eeb4 671#ifdef CONFIG_TCP_MD5SIG
33ad798c
AL
672 *md5 = tcp_rsk(req)->af_specific->md5_lookup(sk, req);
673 if (*md5) {
674 opts->options |= OPTION_MD5;
4957faad
WAS
675 remaining -= TCPOLEN_MD5SIG_ALIGNED;
676
677 /* We can't fit any SACK blocks in a packet with MD5 + TS
678 * options. There was discussion about disabling SACK
679 * rather than TS in order to fit in better with old,
680 * buggy kernels, but that was deemed to be unnecessary.
681 */
682 doing_ts &= !ireq->sack_ok;
cfb6eeb4 683 }
33ad798c
AL
684#else
685 *md5 = NULL;
cfb6eeb4 686#endif
33ad798c 687
4957faad 688 /* We always send an MSS option. */
33ad798c 689 opts->mss = mss;
4957faad 690 remaining -= TCPOLEN_MSS_ALIGNED;
33ad798c
AL
691
692 if (likely(ireq->wscale_ok)) {
693 opts->ws = ireq->rcv_wscale;
89e95a61 694 opts->options |= OPTION_WSCALE;
4957faad 695 remaining -= TCPOLEN_WSCALE_ALIGNED;
33ad798c
AL
696 }
697 if (likely(doing_ts)) {
698 opts->options |= OPTION_TS;
699 opts->tsval = TCP_SKB_CB(skb)->when;
700 opts->tsecr = req->ts_recent;
4957faad 701 remaining -= TCPOLEN_TSTAMP_ALIGNED;
33ad798c
AL
702 }
703 if (likely(ireq->sack_ok)) {
704 opts->options |= OPTION_SACK_ADVERTISE;
705 if (unlikely(!doing_ts))
4957faad 706 remaining -= TCPOLEN_SACKPERM_ALIGNED;
33ad798c
AL
707 }
708
4957faad
WAS
709 /* Similar rationale to tcp_syn_options() applies here, too.
710 * If the <SYN> options fit, the same options should fit now!
711 */
712 if (*md5 == NULL &&
713 doing_ts &&
714 cookie_plus > TCPOLEN_COOKIE_BASE) {
715 int need = cookie_plus; /* has TCPOLEN_COOKIE_BASE */
716
717 if (0x2 & need) {
718 /* 32-bit multiple */
719 need += 2; /* NOPs */
720 }
721 if (need <= remaining) {
722 opts->options |= OPTION_COOKIE_EXTENSION;
723 opts->hash_size = cookie_plus - TCPOLEN_COOKIE_BASE;
724 remaining -= need;
725 } else {
726 /* There's no error return, so flag it. */
727 xvp->cookie_out_never = 1; /* true */
728 opts->hash_size = 0;
729 }
730 }
731 return MAX_TCP_OPTION_SPACE - remaining;
33ad798c
AL
732}
733
67edfef7
AK
734/* Compute TCP options for ESTABLISHED sockets. This is not the
735 * final wire format yet.
736 */
33ad798c
AL
737static unsigned tcp_established_options(struct sock *sk, struct sk_buff *skb,
738 struct tcp_out_options *opts,
739 struct tcp_md5sig_key **md5) {
740 struct tcp_skb_cb *tcb = skb ? TCP_SKB_CB(skb) : NULL;
741 struct tcp_sock *tp = tcp_sk(sk);
742 unsigned size = 0;
cabeccbd 743 unsigned int eff_sacks;
33ad798c
AL
744
745#ifdef CONFIG_TCP_MD5SIG
746 *md5 = tp->af_specific->md5_lookup(sk, sk);
747 if (unlikely(*md5)) {
748 opts->options |= OPTION_MD5;
749 size += TCPOLEN_MD5SIG_ALIGNED;
750 }
751#else
752 *md5 = NULL;
753#endif
754
755 if (likely(tp->rx_opt.tstamp_ok)) {
756 opts->options |= OPTION_TS;
757 opts->tsval = tcb ? tcb->when : 0;
758 opts->tsecr = tp->rx_opt.ts_recent;
759 size += TCPOLEN_TSTAMP_ALIGNED;
760 }
761
cabeccbd
IJ
762 eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;
763 if (unlikely(eff_sacks)) {
33ad798c
AL
764 const unsigned remaining = MAX_TCP_OPTION_SPACE - size;
765 opts->num_sack_blocks =
cabeccbd 766 min_t(unsigned, eff_sacks,
33ad798c
AL
767 (remaining - TCPOLEN_SACK_BASE_ALIGNED) /
768 TCPOLEN_SACK_PERBLOCK);
769 size += TCPOLEN_SACK_BASE_ALIGNED +
770 opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK;
771 }
772
773 return size;
40efc6fa 774}
1da177e4
LT
775
776/* This routine actually transmits TCP packets queued in by
777 * tcp_do_sendmsg(). This is used by both the initial
778 * transmission and possible later retransmissions.
779 * All SKB's seen here are completely headerless. It is our
780 * job to build the TCP header, and pass the packet down to
781 * IP so it can do the same plus pass the packet off to the
782 * device.
783 *
784 * We are working here with either a clone of the original
785 * SKB, or a fresh unique copy made by the retransmit engine.
786 */
056834d9
IJ
787static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
788 gfp_t gfp_mask)
1da177e4 789{
dfb4b9dc
DM
790 const struct inet_connection_sock *icsk = inet_csk(sk);
791 struct inet_sock *inet;
792 struct tcp_sock *tp;
793 struct tcp_skb_cb *tcb;
33ad798c
AL
794 struct tcp_out_options opts;
795 unsigned tcp_options_size, tcp_header_size;
cfb6eeb4 796 struct tcp_md5sig_key *md5;
dfb4b9dc 797 struct tcphdr *th;
dfb4b9dc
DM
798 int err;
799
800 BUG_ON(!skb || !tcp_skb_pcount(skb));
801
802 /* If congestion control is doing timestamping, we must
803 * take such a timestamp before we potentially clone/copy.
804 */
164891aa 805 if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
dfb4b9dc
DM
806 __net_timestamp(skb);
807
808 if (likely(clone_it)) {
809 if (unlikely(skb_cloned(skb)))
810 skb = pskb_copy(skb, gfp_mask);
811 else
812 skb = skb_clone(skb, gfp_mask);
813 if (unlikely(!skb))
814 return -ENOBUFS;
815 }
1da177e4 816
dfb4b9dc
DM
817 inet = inet_sk(sk);
818 tp = tcp_sk(sk);
819 tcb = TCP_SKB_CB(skb);
33ad798c 820 memset(&opts, 0, sizeof(opts));
1da177e4 821
33ad798c
AL
822 if (unlikely(tcb->flags & TCPCB_FLAG_SYN))
823 tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5);
824 else
825 tcp_options_size = tcp_established_options(sk, skb, &opts,
826 &md5);
827 tcp_header_size = tcp_options_size + sizeof(struct tcphdr);
e905a9ed 828
dfb4b9dc
DM
829 if (tcp_packets_in_flight(tp) == 0)
830 tcp_ca_event(sk, CA_EVENT_TX_START);
831
aa8223c7
ACM
832 skb_push(skb, tcp_header_size);
833 skb_reset_transport_header(skb);
e89862f4 834 skb_set_owner_w(skb, sk);
dfb4b9dc
DM
835
836 /* Build TCP header and checksum it. */
aa8223c7 837 th = tcp_hdr(skb);
c720c7e8
ED
838 th->source = inet->inet_sport;
839 th->dest = inet->inet_dport;
dfb4b9dc
DM
840 th->seq = htonl(tcb->seq);
841 th->ack_seq = htonl(tp->rcv_nxt);
df7a3b07 842 *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) |
dfb4b9dc
DM
843 tcb->flags);
844
845 if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
846 /* RFC1323: The window in SYN & SYN/ACK segments
847 * is never scaled.
848 */
600ff0c2 849 th->window = htons(min(tp->rcv_wnd, 65535U));
dfb4b9dc
DM
850 } else {
851 th->window = htons(tcp_select_window(sk));
852 }
853 th->check = 0;
854 th->urg_ptr = 0;
1da177e4 855
33f5f57e 856 /* The urg_mode check is necessary during a below snd_una win probe */
7691367d
HX
857 if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) {
858 if (before(tp->snd_up, tcb->seq + 0x10000)) {
859 th->urg_ptr = htons(tp->snd_up - tcb->seq);
860 th->urg = 1;
861 } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) {
862 th->urg_ptr = 0xFFFF;
863 th->urg = 1;
864 }
dfb4b9dc 865 }
1da177e4 866
bd0388ae 867 tcp_options_write((__be32 *)(th + 1), tp, &opts);
33ad798c 868 if (likely((tcb->flags & TCPCB_FLAG_SYN) == 0))
9e412ba7 869 TCP_ECN_send(sk, skb, tcp_header_size);
1da177e4 870
cfb6eeb4
YH
871#ifdef CONFIG_TCP_MD5SIG
872 /* Calculate the MD5 hash, as we have all we need now */
873 if (md5) {
33ad798c 874 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
bd0388ae 875 tp->af_specific->calc_md5_hash(opts.hash_location,
49a72dfb 876 md5, sk, NULL, skb);
cfb6eeb4
YH
877 }
878#endif
879
8292a17a 880 icsk->icsk_af_ops->send_check(sk, skb->len, skb);
1da177e4 881
dfb4b9dc
DM
882 if (likely(tcb->flags & TCPCB_FLAG_ACK))
883 tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
1da177e4 884
dfb4b9dc
DM
885 if (skb->len != tcp_header_size)
886 tcp_event_data_sent(tp, skb, sk);
1da177e4 887
bd37a088 888 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
81cc8a75 889 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS);
1da177e4 890
e89862f4 891 err = icsk->icsk_af_ops->queue_xmit(skb, 0);
83de47cd 892 if (likely(err <= 0))
dfb4b9dc
DM
893 return err;
894
3cfe3baa 895 tcp_enter_cwr(sk, 1);
dfb4b9dc 896
b9df3cb8 897 return net_xmit_eval(err);
1da177e4
LT
898}
899
67edfef7 900/* This routine just queues the buffer for sending.
1da177e4
LT
901 *
902 * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
903 * otherwise socket can stall.
904 */
905static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
906{
907 struct tcp_sock *tp = tcp_sk(sk);
908
909 /* Advance write_seq and place onto the write_queue. */
910 tp->write_seq = TCP_SKB_CB(skb)->end_seq;
911 skb_header_release(skb);
fe067e8a 912 tcp_add_write_queue_tail(sk, skb);
3ab224be
HA
913 sk->sk_wmem_queued += skb->truesize;
914 sk_mem_charge(sk, skb->truesize);
1da177e4
LT
915}
916
67edfef7 917/* Initialize TSO segments for a packet. */
056834d9
IJ
918static void tcp_set_skb_tso_segs(struct sock *sk, struct sk_buff *skb,
919 unsigned int mss_now)
f6302d1d 920{
8e5b9dda
HX
921 if (skb->len <= mss_now || !sk_can_gso(sk) ||
922 skb->ip_summed == CHECKSUM_NONE) {
f6302d1d
DM
923 /* Avoid the costly divide in the normal
924 * non-TSO case.
925 */
7967168c
HX
926 skb_shinfo(skb)->gso_segs = 1;
927 skb_shinfo(skb)->gso_size = 0;
928 skb_shinfo(skb)->gso_type = 0;
f6302d1d 929 } else {
356f89e1 930 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss_now);
7967168c 931 skb_shinfo(skb)->gso_size = mss_now;
bcd76111 932 skb_shinfo(skb)->gso_type = sk->sk_gso_type;
1da177e4
LT
933 }
934}
935
91fed7a1 936/* When a modification to fackets out becomes necessary, we need to check
68f8353b 937 * skb is counted to fackets_out or not.
91fed7a1 938 */
a47e5a98 939static void tcp_adjust_fackets_out(struct sock *sk, struct sk_buff *skb,
91fed7a1
IJ
940 int decr)
941{
a47e5a98
IJ
942 struct tcp_sock *tp = tcp_sk(sk);
943
dc86967b 944 if (!tp->sacked_out || tcp_is_reno(tp))
91fed7a1
IJ
945 return;
946
6859d494 947 if (after(tcp_highest_sack_seq(tp), TCP_SKB_CB(skb)->seq))
91fed7a1 948 tp->fackets_out -= decr;
91fed7a1
IJ
949}
950
797108d1
IJ
951/* Pcount in the middle of the write queue got changed, we need to do various
952 * tweaks to fix counters
953 */
954static void tcp_adjust_pcount(struct sock *sk, struct sk_buff *skb, int decr)
955{
956 struct tcp_sock *tp = tcp_sk(sk);
957
958 tp->packets_out -= decr;
959
960 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
961 tp->sacked_out -= decr;
962 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
963 tp->retrans_out -= decr;
964 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
965 tp->lost_out -= decr;
966
967 /* Reno case is special. Sigh... */
968 if (tcp_is_reno(tp) && decr > 0)
969 tp->sacked_out -= min_t(u32, tp->sacked_out, decr);
970
971 tcp_adjust_fackets_out(sk, skb, decr);
972
973 if (tp->lost_skb_hint &&
974 before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) &&
52cf3cc8 975 (tcp_is_fack(tp) || (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)))
797108d1
IJ
976 tp->lost_cnt_hint -= decr;
977
978 tcp_verify_left_out(tp);
979}
980
1da177e4
LT
981/* Function to create two new TCP segments. Shrinks the given segment
982 * to the specified size and appends a new segment with the rest of the
e905a9ed 983 * packet to the list. This won't be called frequently, I hope.
1da177e4
LT
984 * Remember, these are still headerless SKBs at this point.
985 */
056834d9
IJ
986int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
987 unsigned int mss_now)
1da177e4
LT
988{
989 struct tcp_sock *tp = tcp_sk(sk);
990 struct sk_buff *buff;
6475be16 991 int nsize, old_factor;
b60b49ea 992 int nlen;
9ce01461 993 u8 flags;
1da177e4 994
b2cc99f0 995 BUG_ON(len > skb->len);
6a438bbe 996
1da177e4
LT
997 nsize = skb_headlen(skb) - len;
998 if (nsize < 0)
999 nsize = 0;
1000
1001 if (skb_cloned(skb) &&
1002 skb_is_nonlinear(skb) &&
1003 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1004 return -ENOMEM;
1005
1006 /* Get a new skb... force flag on. */
1007 buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
1008 if (buff == NULL)
1009 return -ENOMEM; /* We'll just try again later. */
ef5cb973 1010
3ab224be
HA
1011 sk->sk_wmem_queued += buff->truesize;
1012 sk_mem_charge(sk, buff->truesize);
b60b49ea
HX
1013 nlen = skb->len - len - nsize;
1014 buff->truesize += nlen;
1015 skb->truesize -= nlen;
1da177e4
LT
1016
1017 /* Correct the sequence numbers. */
1018 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
1019 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
1020 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
1021
1022 /* PSH and FIN should only be set in the second packet. */
1023 flags = TCP_SKB_CB(skb)->flags;
056834d9 1024 TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN | TCPCB_FLAG_PSH);
1da177e4 1025 TCP_SKB_CB(buff)->flags = flags;
e14c3caf 1026 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
1da177e4 1027
84fa7933 1028 if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
1da177e4 1029 /* Copy and checksum data tail into the new buffer. */
056834d9
IJ
1030 buff->csum = csum_partial_copy_nocheck(skb->data + len,
1031 skb_put(buff, nsize),
1da177e4
LT
1032 nsize, 0);
1033
1034 skb_trim(skb, len);
1035
1036 skb->csum = csum_block_sub(skb->csum, buff->csum, len);
1037 } else {
84fa7933 1038 skb->ip_summed = CHECKSUM_PARTIAL;
1da177e4
LT
1039 skb_split(skb, buff, len);
1040 }
1041
1042 buff->ip_summed = skb->ip_summed;
1043
1044 /* Looks stupid, but our code really uses when of
1045 * skbs, which it never sent before. --ANK
1046 */
1047 TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
a61bbcf2 1048 buff->tstamp = skb->tstamp;
1da177e4 1049
6475be16
DM
1050 old_factor = tcp_skb_pcount(skb);
1051
1da177e4 1052 /* Fix up tso_factor for both original and new SKB. */
846998ae
DM
1053 tcp_set_skb_tso_segs(sk, skb, mss_now);
1054 tcp_set_skb_tso_segs(sk, buff, mss_now);
1da177e4 1055
6475be16
DM
1056 /* If this packet has been sent out already, we must
1057 * adjust the various packet counters.
1058 */
cf0b450c 1059 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
6475be16
DM
1060 int diff = old_factor - tcp_skb_pcount(skb) -
1061 tcp_skb_pcount(buff);
1da177e4 1062
797108d1
IJ
1063 if (diff)
1064 tcp_adjust_pcount(sk, skb, diff);
1da177e4
LT
1065 }
1066
1067 /* Link BUFF into the send queue. */
f44b5271 1068 skb_header_release(buff);
fe067e8a 1069 tcp_insert_write_queue_after(skb, buff, sk);
1da177e4
LT
1070
1071 return 0;
1072}
1073
1074/* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
1075 * eventually). The difference is that pulled data not copied, but
1076 * immediately discarded.
1077 */
f2911969 1078static void __pskb_trim_head(struct sk_buff *skb, int len)
1da177e4
LT
1079{
1080 int i, k, eat;
1081
1082 eat = len;
1083 k = 0;
056834d9 1084 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1da177e4
LT
1085 if (skb_shinfo(skb)->frags[i].size <= eat) {
1086 put_page(skb_shinfo(skb)->frags[i].page);
1087 eat -= skb_shinfo(skb)->frags[i].size;
1088 } else {
1089 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1090 if (eat) {
1091 skb_shinfo(skb)->frags[k].page_offset += eat;
1092 skb_shinfo(skb)->frags[k].size -= eat;
1093 eat = 0;
1094 }
1095 k++;
1096 }
1097 }
1098 skb_shinfo(skb)->nr_frags = k;
1099
27a884dc 1100 skb_reset_tail_pointer(skb);
1da177e4
LT
1101 skb->data_len -= len;
1102 skb->len = skb->data_len;
1da177e4
LT
1103}
1104
67edfef7 1105/* Remove acked data from a packet in the transmit queue. */
1da177e4
LT
1106int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
1107{
056834d9 1108 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1da177e4
LT
1109 return -ENOMEM;
1110
f2911969
HXP
1111 /* If len == headlen, we avoid __skb_pull to preserve alignment. */
1112 if (unlikely(len < skb_headlen(skb)))
1da177e4 1113 __skb_pull(skb, len);
f2911969
HXP
1114 else
1115 __pskb_trim_head(skb, len - skb_headlen(skb));
1da177e4
LT
1116
1117 TCP_SKB_CB(skb)->seq += len;
84fa7933 1118 skb->ip_summed = CHECKSUM_PARTIAL;
1da177e4
LT
1119
1120 skb->truesize -= len;
1121 sk->sk_wmem_queued -= len;
3ab224be 1122 sk_mem_uncharge(sk, len);
1da177e4
LT
1123 sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
1124
1125 /* Any change of skb->len requires recalculation of tso
1126 * factor and mss.
1127 */
1128 if (tcp_skb_pcount(skb) > 1)
0c54b85f 1129 tcp_set_skb_tso_segs(sk, skb, tcp_current_mss(sk));
1da177e4
LT
1130
1131 return 0;
1132}
1133
67edfef7 1134/* Calculate MSS. Not accounting for SACKs here. */
5d424d5a
JH
1135int tcp_mtu_to_mss(struct sock *sk, int pmtu)
1136{
1137 struct tcp_sock *tp = tcp_sk(sk);
1138 struct inet_connection_sock *icsk = inet_csk(sk);
1139 int mss_now;
1140
1141 /* Calculate base mss without TCP options:
1142 It is MMS_S - sizeof(tcphdr) of rfc1122
1143 */
1144 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
1145
1146 /* Clamp it (mss_clamp does not include tcp options) */
1147 if (mss_now > tp->rx_opt.mss_clamp)
1148 mss_now = tp->rx_opt.mss_clamp;
1149
1150 /* Now subtract optional transport overhead */
1151 mss_now -= icsk->icsk_ext_hdr_len;
1152
1153 /* Then reserve room for full set of TCP options and 8 bytes of data */
1154 if (mss_now < 48)
1155 mss_now = 48;
1156
1157 /* Now subtract TCP options size, not including SACKs */
1158 mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
1159
1160 return mss_now;
1161}
1162
1163/* Inverse of above */
1164int tcp_mss_to_mtu(struct sock *sk, int mss)
1165{
1166 struct tcp_sock *tp = tcp_sk(sk);
1167 struct inet_connection_sock *icsk = inet_csk(sk);
1168 int mtu;
1169
1170 mtu = mss +
1171 tp->tcp_header_len +
1172 icsk->icsk_ext_hdr_len +
1173 icsk->icsk_af_ops->net_header_len;
1174
1175 return mtu;
1176}
1177
67edfef7 1178/* MTU probing init per socket */
5d424d5a
JH
1179void tcp_mtup_init(struct sock *sk)
1180{
1181 struct tcp_sock *tp = tcp_sk(sk);
1182 struct inet_connection_sock *icsk = inet_csk(sk);
1183
1184 icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
1185 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
e905a9ed 1186 icsk->icsk_af_ops->net_header_len;
5d424d5a
JH
1187 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
1188 icsk->icsk_mtup.probe_size = 0;
1189}
1190
1da177e4
LT
1191/* This function synchronize snd mss to current pmtu/exthdr set.
1192
1193 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
1194 for TCP options, but includes only bare TCP header.
1195
1196 tp->rx_opt.mss_clamp is mss negotiated at connection setup.
caa20d9a 1197 It is minimum of user_mss and mss received with SYN.
1da177e4
LT
1198 It also does not include TCP options.
1199
d83d8461 1200 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
1da177e4
LT
1201
1202 tp->mss_cache is current effective sending mss, including
1203 all tcp options except for SACKs. It is evaluated,
1204 taking into account current pmtu, but never exceeds
1205 tp->rx_opt.mss_clamp.
1206
1207 NOTE1. rfc1122 clearly states that advertised MSS
1208 DOES NOT include either tcp or ip options.
1209
d83d8461
ACM
1210 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
1211 are READ ONLY outside this function. --ANK (980731)
1da177e4 1212 */
1da177e4
LT
1213unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
1214{
1215 struct tcp_sock *tp = tcp_sk(sk);
d83d8461 1216 struct inet_connection_sock *icsk = inet_csk(sk);
5d424d5a 1217 int mss_now;
1da177e4 1218
5d424d5a
JH
1219 if (icsk->icsk_mtup.search_high > pmtu)
1220 icsk->icsk_mtup.search_high = pmtu;
1da177e4 1221
5d424d5a 1222 mss_now = tcp_mtu_to_mss(sk, pmtu);
409d22b4 1223 mss_now = tcp_bound_to_half_wnd(tp, mss_now);
1da177e4
LT
1224
1225 /* And store cached results */
d83d8461 1226 icsk->icsk_pmtu_cookie = pmtu;
5d424d5a
JH
1227 if (icsk->icsk_mtup.enabled)
1228 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
c1b4a7e6 1229 tp->mss_cache = mss_now;
1da177e4
LT
1230
1231 return mss_now;
1232}
1233
1234/* Compute the current effective MSS, taking SACKs and IP options,
1235 * and even PMTU discovery events into account.
1da177e4 1236 */
0c54b85f 1237unsigned int tcp_current_mss(struct sock *sk)
1da177e4
LT
1238{
1239 struct tcp_sock *tp = tcp_sk(sk);
1240 struct dst_entry *dst = __sk_dst_get(sk);
c1b4a7e6 1241 u32 mss_now;
33ad798c
AL
1242 unsigned header_len;
1243 struct tcp_out_options opts;
1244 struct tcp_md5sig_key *md5;
c1b4a7e6
DM
1245
1246 mss_now = tp->mss_cache;
1247
1da177e4
LT
1248 if (dst) {
1249 u32 mtu = dst_mtu(dst);
d83d8461 1250 if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
1da177e4
LT
1251 mss_now = tcp_sync_mss(sk, mtu);
1252 }
1253
33ad798c
AL
1254 header_len = tcp_established_options(sk, NULL, &opts, &md5) +
1255 sizeof(struct tcphdr);
1256 /* The mss_cache is sized based on tp->tcp_header_len, which assumes
1257 * some common options. If this is an odd packet (because we have SACK
1258 * blocks etc) then our calculated header_len will be different, and
1259 * we have to adjust mss_now correspondingly */
1260 if (header_len != tp->tcp_header_len) {
1261 int delta = (int) header_len - tp->tcp_header_len;
1262 mss_now -= delta;
1263 }
cfb6eeb4 1264
1da177e4
LT
1265 return mss_now;
1266}
1267
a762a980 1268/* Congestion window validation. (RFC2861) */
9e412ba7 1269static void tcp_cwnd_validate(struct sock *sk)
a762a980 1270{
9e412ba7 1271 struct tcp_sock *tp = tcp_sk(sk);
a762a980 1272
d436d686 1273 if (tp->packets_out >= tp->snd_cwnd) {
a762a980
DM
1274 /* Network is feed fully. */
1275 tp->snd_cwnd_used = 0;
1276 tp->snd_cwnd_stamp = tcp_time_stamp;
1277 } else {
1278 /* Network starves. */
1279 if (tp->packets_out > tp->snd_cwnd_used)
1280 tp->snd_cwnd_used = tp->packets_out;
1281
15d33c07
DM
1282 if (sysctl_tcp_slow_start_after_idle &&
1283 (s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
a762a980
DM
1284 tcp_cwnd_application_limited(sk);
1285 }
1286}
1287
0e3a4803
IJ
1288/* Returns the portion of skb which can be sent right away without
1289 * introducing MSS oddities to segment boundaries. In rare cases where
1290 * mss_now != mss_cache, we will request caller to create a small skb
1291 * per input skb which could be mostly avoided here (if desired).
5ea3a748
IJ
1292 *
1293 * We explicitly want to create a request for splitting write queue tail
1294 * to a small skb for Nagle purposes while avoiding unnecessary modulos,
1295 * thus all the complexity (cwnd_len is always MSS multiple which we
1296 * return whenever allowed by the other factors). Basically we need the
1297 * modulo only when the receiver window alone is the limiting factor or
1298 * when we would be allowed to send the split-due-to-Nagle skb fully.
0e3a4803
IJ
1299 */
1300static unsigned int tcp_mss_split_point(struct sock *sk, struct sk_buff *skb,
056834d9 1301 unsigned int mss_now, unsigned int cwnd)
c1b4a7e6 1302{
0e3a4803
IJ
1303 struct tcp_sock *tp = tcp_sk(sk);
1304 u32 needed, window, cwnd_len;
c1b4a7e6 1305
90840def 1306 window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
c1b4a7e6 1307 cwnd_len = mss_now * cwnd;
0e3a4803
IJ
1308
1309 if (likely(cwnd_len <= window && skb != tcp_write_queue_tail(sk)))
1310 return cwnd_len;
1311
5ea3a748
IJ
1312 needed = min(skb->len, window);
1313
17515408 1314 if (cwnd_len <= needed)
0e3a4803
IJ
1315 return cwnd_len;
1316
0e3a4803 1317 return needed - needed % mss_now;
c1b4a7e6
DM
1318}
1319
1320/* Can at least one segment of SKB be sent right now, according to the
1321 * congestion window rules? If so, return how many segments are allowed.
1322 */
056834d9
IJ
1323static inline unsigned int tcp_cwnd_test(struct tcp_sock *tp,
1324 struct sk_buff *skb)
c1b4a7e6
DM
1325{
1326 u32 in_flight, cwnd;
1327
1328 /* Don't be strict about the congestion window for the final FIN. */
104439a8
JH
1329 if ((TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
1330 tcp_skb_pcount(skb) == 1)
c1b4a7e6
DM
1331 return 1;
1332
1333 in_flight = tcp_packets_in_flight(tp);
1334 cwnd = tp->snd_cwnd;
1335 if (in_flight < cwnd)
1336 return (cwnd - in_flight);
1337
1338 return 0;
1339}
1340
67edfef7
AK
1341/* Intialize TSO state of a skb.
1342 * This must be invoked the first time we consider transmitting
c1b4a7e6
DM
1343 * SKB onto the wire.
1344 */
056834d9
IJ
1345static int tcp_init_tso_segs(struct sock *sk, struct sk_buff *skb,
1346 unsigned int mss_now)
c1b4a7e6
DM
1347{
1348 int tso_segs = tcp_skb_pcount(skb);
1349
f8269a49 1350 if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) {
846998ae 1351 tcp_set_skb_tso_segs(sk, skb, mss_now);
c1b4a7e6
DM
1352 tso_segs = tcp_skb_pcount(skb);
1353 }
1354 return tso_segs;
1355}
1356
67edfef7 1357/* Minshall's variant of the Nagle send check. */
c1b4a7e6
DM
1358static inline int tcp_minshall_check(const struct tcp_sock *tp)
1359{
09cb105e 1360 return after(tp->snd_sml, tp->snd_una) &&
c1b4a7e6
DM
1361 !after(tp->snd_sml, tp->snd_nxt);
1362}
1363
1364/* Return 0, if packet can be sent now without violation Nagle's rules:
1365 * 1. It is full sized.
1366 * 2. Or it contains FIN. (already checked by caller)
1367 * 3. Or TCP_NODELAY was set.
1368 * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
1369 * With Minshall's modification: all sent small packets are ACKed.
1370 */
c1b4a7e6 1371static inline int tcp_nagle_check(const struct tcp_sock *tp,
e905a9ed 1372 const struct sk_buff *skb,
c1b4a7e6
DM
1373 unsigned mss_now, int nonagle)
1374{
1375 return (skb->len < mss_now &&
056834d9
IJ
1376 ((nonagle & TCP_NAGLE_CORK) ||
1377 (!nonagle && tp->packets_out && tcp_minshall_check(tp))));
c1b4a7e6
DM
1378}
1379
1380/* Return non-zero if the Nagle test allows this packet to be
1381 * sent now.
1382 */
1383static inline int tcp_nagle_test(struct tcp_sock *tp, struct sk_buff *skb,
1384 unsigned int cur_mss, int nonagle)
1385{
1386 /* Nagle rule does not apply to frames, which sit in the middle of the
1387 * write_queue (they have no chances to get new data).
1388 *
1389 * This is implemented in the callers, where they modify the 'nonagle'
1390 * argument based upon the location of SKB in the send queue.
1391 */
1392 if (nonagle & TCP_NAGLE_PUSH)
1393 return 1;
1394
d551e454
IJ
1395 /* Don't use the nagle rule for urgent data (or for the final FIN).
1396 * Nagle can be ignored during F-RTO too (see RFC4138).
1397 */
33f5f57e 1398 if (tcp_urg_mode(tp) || (tp->frto_counter == 2) ||
c1b4a7e6
DM
1399 (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN))
1400 return 1;
1401
1402 if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
1403 return 1;
1404
1405 return 0;
1406}
1407
1408/* Does at least the first segment of SKB fit into the send window? */
056834d9
IJ
1409static inline int tcp_snd_wnd_test(struct tcp_sock *tp, struct sk_buff *skb,
1410 unsigned int cur_mss)
c1b4a7e6
DM
1411{
1412 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
1413
1414 if (skb->len > cur_mss)
1415 end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
1416
90840def 1417 return !after(end_seq, tcp_wnd_end(tp));
c1b4a7e6
DM
1418}
1419
fe067e8a 1420/* This checks if the data bearing packet SKB (usually tcp_send_head(sk))
c1b4a7e6
DM
1421 * should be put on the wire right now. If so, it returns the number of
1422 * packets allowed by the congestion window.
1423 */
1424static unsigned int tcp_snd_test(struct sock *sk, struct sk_buff *skb,
1425 unsigned int cur_mss, int nonagle)
1426{
1427 struct tcp_sock *tp = tcp_sk(sk);
1428 unsigned int cwnd_quota;
1429
846998ae 1430 tcp_init_tso_segs(sk, skb, cur_mss);
c1b4a7e6
DM
1431
1432 if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
1433 return 0;
1434
1435 cwnd_quota = tcp_cwnd_test(tp, skb);
056834d9 1436 if (cwnd_quota && !tcp_snd_wnd_test(tp, skb, cur_mss))
c1b4a7e6
DM
1437 cwnd_quota = 0;
1438
1439 return cwnd_quota;
1440}
1441
67edfef7 1442/* Test if sending is allowed right now. */
9e412ba7 1443int tcp_may_send_now(struct sock *sk)
c1b4a7e6 1444{
9e412ba7 1445 struct tcp_sock *tp = tcp_sk(sk);
fe067e8a 1446 struct sk_buff *skb = tcp_send_head(sk);
c1b4a7e6
DM
1447
1448 return (skb &&
0c54b85f 1449 tcp_snd_test(sk, skb, tcp_current_mss(sk),
c1b4a7e6 1450 (tcp_skb_is_last(sk, skb) ?