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