blob: b35802af3c4c45dc97ee8813a38df7cad8c67c30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* People can turn this off for buggy TCP's found in printers etc. */
Brian Haleyab32ea52006-09-22 14:15:41 -070043int sysctl_tcp_retrans_collapse __read_mostly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jianjun Kong09cb1052008-11-03 00:27:11 -080045/* People can turn this on to work with those rare, broken TCPs that
Rick Jones15d99e02006-03-20 22:40:29 -080046 * interpret the window field as a signed quantity.
47 */
Brian Haleyab32ea52006-09-22 14:15:41 -070048int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
Rick Jones15d99e02006-03-20 22:40:29 -080049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* 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 */
Brian Haleyab32ea52006-09-22 14:15:41 -070054int sysctl_tcp_tso_win_divisor __read_mostly = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Brian Haleyab32ea52006-09-22 14:15:41 -070056int sysctl_tcp_mtu_probing __read_mostly = 0;
57int sysctl_tcp_base_mss __read_mostly = 512;
John Heffner5d424d52006-03-20 17:53:41 -080058
David S. Miller35089bb2006-06-13 22:33:04 -070059/* By default, RFC2861 behavior. */
Brian Haleyab32ea52006-09-22 14:15:41 -070060int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
David S. Miller35089bb2006-06-13 22:33:04 -070061
Andi Kleen67edfef2009-07-21 23:00:40 +000062/* Account for new data that has been sent to the network. */
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -080063static void tcp_event_new_data_sent(struct sock *sk, struct sk_buff *skb)
Ilpo Järvinen6ff03ac2007-08-24 22:44:06 -070064{
65 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -080066 unsigned int prior_packets = tp->packets_out;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070067
David S. Millerfe067e82007-03-07 12:12:44 -080068 tcp_advance_send_head(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
Ilpo Järvinen85124302007-11-26 20:17:38 +080070
71 /* Don't override Nagle indefinately with F-RTO */
72 if (tp->frto_counter == 2)
73 tp->frto_counter = 3;
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -080074
75 tp->packets_out += tcp_skb_pcount(skb);
76 if (!prior_packets)
77 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
78 inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81/* SND.NXT, if window was not shrunk.
82 * If window has been shrunk, what should we make? It is not clear at all.
83 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
84 * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
85 * invalid. OK, let's make this for now:
86 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070087static inline __u32 tcp_acceptable_seq(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070089 struct tcp_sock *tp = tcp_sk(sk);
90
Ilpo Järvinen90840de2007-12-31 04:48:41 -080091 if (!before(tcp_wnd_end(tp), tp->snd_nxt))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return tp->snd_nxt;
93 else
Ilpo Järvinen90840de2007-12-31 04:48:41 -080094 return tcp_wnd_end(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/* Calculate mss to advertise in SYN segment.
98 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
99 *
100 * 1. It is independent of path mtu.
101 * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
102 * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
103 * attached devices, because some buggy hosts are confused by
104 * large MSS.
105 * 4. We do not make 3, we advertise MSS, calculated from first
106 * hop device mtu, but allow to raise it to ip_rt_min_advmss.
107 * This may be overridden via information stored in routing table.
108 * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
109 * probably even Jumbo".
110 */
111static __u16 tcp_advertise_mss(struct sock *sk)
112{
113 struct tcp_sock *tp = tcp_sk(sk);
114 struct dst_entry *dst = __sk_dst_get(sk);
115 int mss = tp->advmss;
116
117 if (dst && dst_metric(dst, RTAX_ADVMSS) < mss) {
118 mss = dst_metric(dst, RTAX_ADVMSS);
119 tp->advmss = mss;
120 }
121
122 return (__u16)mss;
123}
124
125/* RFC2861. Reset CWND after idle period longer RTO to "restart window".
126 * This is the first part of cwnd validation mechanism. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700127static void tcp_cwnd_restart(struct sock *sk, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700129 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 s32 delta = tcp_time_stamp - tp->lsndtime;
131 u32 restart_cwnd = tcp_init_cwnd(tp, dst);
132 u32 cwnd = tp->snd_cwnd;
133
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300134 tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300136 tp->snd_ssthresh = tcp_current_ssthresh(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 restart_cwnd = min(restart_cwnd, cwnd);
138
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700139 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 cwnd >>= 1;
141 tp->snd_cwnd = max(cwnd, restart_cwnd);
142 tp->snd_cwnd_stamp = tcp_time_stamp;
143 tp->snd_cwnd_used = 0;
144}
145
Andi Kleen67edfef2009-07-21 23:00:40 +0000146/* Congestion state accounting after a packet has been sent. */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800147static void tcp_event_data_sent(struct tcp_sock *tp,
148 struct sk_buff *skb, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700150 struct inet_connection_sock *icsk = inet_csk(sk);
151 const u32 now = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David S. Miller35089bb2006-06-13 22:33:04 -0700153 if (sysctl_tcp_slow_start_after_idle &&
154 (!tp->packets_out && (s32)(now - tp->lsndtime) > icsk->icsk_rto))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700155 tcp_cwnd_restart(sk, __sk_dst_get(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 tp->lsndtime = now;
158
159 /* If it is a reply for ato after last received
160 * packet, enter pingpong mode.
161 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700162 if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
163 icsk->icsk_ack.pingpong = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Andi Kleen67edfef2009-07-21 23:00:40 +0000166/* Account for an ACK we sent. */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800167static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700169 tcp_dec_quickack_mode(sk, pkts);
170 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/* Determine a window scaling and initial window to offer.
174 * Based on the assumption that the given amount of space
175 * will be offered. Store the results in the tp structure.
176 * NOTE: for smooth operation initial space offering should
177 * be a multiple of mss if possible. We assume here that mss >= 1.
178 * This MUST be enforced by all callers.
179 */
180void tcp_select_initial_window(int __space, __u32 mss,
181 __u32 *rcv_wnd, __u32 *window_clamp,
182 int wscale_ok, __u8 *rcv_wscale)
183{
184 unsigned int space = (__space < 0 ? 0 : __space);
185
186 /* If no clamp set the clamp to the max possible scaled window */
187 if (*window_clamp == 0)
188 (*window_clamp) = (65535 << 14);
189 space = min(*window_clamp, space);
190
191 /* Quantize space offering to a multiple of mss if possible. */
192 if (space > mss)
193 space = (space / mss) * mss;
194
195 /* NOTE: offering an initial window larger than 32767
Rick Jones15d99e02006-03-20 22:40:29 -0800196 * will break some buggy TCP stacks. If the admin tells us
197 * it is likely we could be speaking with such a buggy stack
198 * we will truncate our initial window offering to 32K-1
199 * unless the remote has sent us a window scaling option,
200 * which we interpret as a sign the remote TCP is not
201 * misinterpreting the window field as a signed quantity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Rick Jones15d99e02006-03-20 22:40:29 -0800203 if (sysctl_tcp_workaround_signed_windows)
204 (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
205 else
206 (*rcv_wnd) = space;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 (*rcv_wscale) = 0;
209 if (wscale_ok) {
210 /* Set window scaling on max possible window
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900211 * See RFC1323 for an explanation of the limit to 14
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
213 space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
Stephen Hemminger316c1592006-08-22 00:06:11 -0700214 space = min_t(u32, space, *window_clamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 while (space > 65535 && (*rcv_wscale) < 14) {
216 space >>= 1;
217 (*rcv_wscale)++;
218 }
219 }
220
221 /* Set initial window to value enough for senders,
David S. Miller6b251852005-09-28 16:31:48 -0700222 * following RFC2414. Senders, not following this RFC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * will be satisfied with 2.
224 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800225 if (mss > (1 << *rcv_wscale)) {
David S. Miller01ff3672005-09-29 17:07:20 -0700226 int init_cwnd = 4;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800227 if (mss > 1460 * 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 init_cwnd = 2;
David S. Miller01ff3672005-09-29 17:07:20 -0700229 else if (mss > 1460)
230 init_cwnd = 3;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800231 if (*rcv_wnd > init_cwnd * mss)
232 *rcv_wnd = init_cwnd * mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 /* Set the clamp no higher than max representable value */
236 (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
237}
238
239/* Chose a new window to advertise, update state in tcp_sock for the
240 * socket, and return result with RFC1323 scaling applied. The return
241 * value can be stuffed directly into th->window for an outgoing
242 * frame.
243 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800244static u16 tcp_select_window(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct tcp_sock *tp = tcp_sk(sk);
247 u32 cur_win = tcp_receive_window(tp);
248 u32 new_win = __tcp_select_window(sk);
249
250 /* Never shrink the offered window */
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800251 if (new_win < cur_win) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Danger Will Robinson!
253 * Don't update rcv_wup/rcv_wnd here or else
254 * we will not be able to advertise a zero
255 * window in time. --DaveM
256 *
257 * Relax Will Robinson.
258 */
Patrick McHardy607bfbf2008-03-20 16:11:27 -0700259 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 tp->rcv_wnd = new_win;
262 tp->rcv_wup = tp->rcv_nxt;
263
264 /* Make sure we do not exceed the maximum possible
265 * scaled window.
266 */
Rick Jones15d99e02006-03-20 22:40:29 -0800267 if (!tp->rx_opt.rcv_wscale && sysctl_tcp_workaround_signed_windows)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 new_win = min(new_win, MAX_TCP_WINDOW);
269 else
270 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
271
272 /* RFC1323 scaling applied */
273 new_win >>= tp->rx_opt.rcv_wscale;
274
275 /* If we advertise zero window, disable fast path. */
276 if (new_win == 0)
277 tp->pred_flags = 0;
278
279 return new_win;
280}
281
Andi Kleen67edfef2009-07-21 23:00:40 +0000282/* Packet ECN state for a SYN-ACK */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800283static inline void TCP_ECN_send_synack(struct tcp_sock *tp, struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700284{
285 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_CWR;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800286 if (!(tp->ecn_flags & TCP_ECN_OK))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700287 TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_ECE;
288}
289
Andi Kleen67edfef2009-07-21 23:00:40 +0000290/* Packet ECN state for a SYN. */
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700291static inline void TCP_ECN_send_syn(struct sock *sk, struct sk_buff *skb)
292{
293 struct tcp_sock *tp = tcp_sk(sk);
294
295 tp->ecn_flags = 0;
Ilpo Järvinen255cac92009-05-04 11:07:36 -0700296 if (sysctl_tcp_ecn == 1) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800297 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ECE | TCPCB_FLAG_CWR;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700298 tp->ecn_flags = TCP_ECN_OK;
299 }
300}
301
302static __inline__ void
303TCP_ECN_make_synack(struct request_sock *req, struct tcphdr *th)
304{
305 if (inet_rsk(req)->ecn_ok)
306 th->ece = 1;
307}
308
Andi Kleen67edfef2009-07-21 23:00:40 +0000309/* Set up ECN state for a packet on a ESTABLISHED socket that is about to
310 * be sent.
311 */
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700312static inline void TCP_ECN_send(struct sock *sk, struct sk_buff *skb,
313 int tcp_header_len)
314{
315 struct tcp_sock *tp = tcp_sk(sk);
316
317 if (tp->ecn_flags & TCP_ECN_OK) {
318 /* Not-retransmitted data segment: set ECT and inject CWR. */
319 if (skb->len != tcp_header_len &&
320 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {
321 INET_ECN_xmit(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800322 if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) {
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700323 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
324 tcp_hdr(skb)->cwr = 1;
325 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
326 }
327 } else {
328 /* ACK or retransmitted segment: clear ECT|CE */
329 INET_ECN_dontxmit(sk);
330 }
331 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR)
332 tcp_hdr(skb)->ece = 1;
333 }
334}
335
Ilpo Järvinene870a8e2008-01-03 20:39:01 -0800336/* Constructs common control bits of non-data skb. If SYN/FIN is present,
337 * auto increment end seqno.
338 */
339static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
340{
341 skb->csum = 0;
342
343 TCP_SKB_CB(skb)->flags = flags;
344 TCP_SKB_CB(skb)->sacked = 0;
345
346 skb_shinfo(skb)->gso_segs = 1;
347 skb_shinfo(skb)->gso_size = 0;
348 skb_shinfo(skb)->gso_type = 0;
349
350 TCP_SKB_CB(skb)->seq = seq;
351 if (flags & (TCPCB_FLAG_SYN | TCPCB_FLAG_FIN))
352 seq++;
353 TCP_SKB_CB(skb)->end_seq = seq;
354}
355
Ilpo Järvinen33f5f572008-10-07 14:43:06 -0700356static inline int tcp_urg_mode(const struct tcp_sock *tp)
357{
358 return tp->snd_una != tp->snd_up;
359}
360
Adam Langley33ad7982008-07-19 00:04:31 -0700361#define OPTION_SACK_ADVERTISE (1 << 0)
362#define OPTION_TS (1 << 1)
363#define OPTION_MD5 (1 << 2)
Ori Finkelman89e95a62009-10-01 06:41:59 +0000364#define OPTION_WSCALE (1 << 3)
Adam Langley33ad7982008-07-19 00:04:31 -0700365
366struct tcp_out_options {
367 u8 options; /* bit field of OPTION_* */
368 u8 ws; /* window scale, 0 to disable */
369 u8 num_sack_blocks; /* number of SACK blocks to include */
370 u16 mss; /* 0 to disable */
371 __u32 tsval, tsecr; /* need to include OPTION_TS */
372};
373
Andi Kleen67edfef2009-07-21 23:00:40 +0000374/* Write previously computed TCP options to the packet.
375 *
376 * Beware: Something in the Internet is very sensitive to the ordering of
Ilpo Järvinenfd6149d2008-10-23 14:06:35 -0700377 * TCP options, we learned this through the hard way, so be careful here.
378 * Luckily we can at least blame others for their non-compliance but from
379 * inter-operatibility perspective it seems that we're somewhat stuck with
380 * the ordering which we have been using if we want to keep working with
381 * those broken things (not that it currently hurts anybody as there isn't
382 * particular reason why the ordering would need to be changed).
383 *
384 * At least SACK_PERM as the first option is known to lead to a disaster
385 * (but it may well be that other scenarios fail similarly).
386 */
Adam Langley33ad7982008-07-19 00:04:31 -0700387static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
388 const struct tcp_out_options *opts,
389 __u8 **md5_hash) {
390 if (unlikely(OPTION_MD5 & opts->options)) {
YOSHIFUJI Hideaki496c98d2006-10-10 19:41:21 -0700391 *ptr++ = htonl((TCPOPT_NOP << 24) |
392 (TCPOPT_NOP << 16) |
Adam Langley33ad7982008-07-19 00:04:31 -0700393 (TCPOPT_MD5SIG << 8) |
394 TCPOLEN_MD5SIG);
395 *md5_hash = (__u8 *)ptr;
396 ptr += 4;
397 } else {
398 *md5_hash = NULL;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800399 }
Adam Langley33ad7982008-07-19 00:04:31 -0700400
Ilpo Järvinenfd6149d2008-10-23 14:06:35 -0700401 if (unlikely(opts->mss)) {
402 *ptr++ = htonl((TCPOPT_MSS << 24) |
403 (TCPOLEN_MSS << 16) |
404 opts->mss);
405 }
406
Adam Langley33ad7982008-07-19 00:04:31 -0700407 if (likely(OPTION_TS & opts->options)) {
408 if (unlikely(OPTION_SACK_ADVERTISE & opts->options)) {
409 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
410 (TCPOLEN_SACK_PERM << 16) |
411 (TCPOPT_TIMESTAMP << 8) |
412 TCPOLEN_TIMESTAMP);
413 } else {
414 *ptr++ = htonl((TCPOPT_NOP << 24) |
415 (TCPOPT_NOP << 16) |
416 (TCPOPT_TIMESTAMP << 8) |
417 TCPOLEN_TIMESTAMP);
418 }
419 *ptr++ = htonl(opts->tsval);
420 *ptr++ = htonl(opts->tsecr);
421 }
422
Adam Langley33ad7982008-07-19 00:04:31 -0700423 if (unlikely(OPTION_SACK_ADVERTISE & opts->options &&
424 !(OPTION_TS & opts->options))) {
425 *ptr++ = htonl((TCPOPT_NOP << 24) |
426 (TCPOPT_NOP << 16) |
427 (TCPOPT_SACK_PERM << 8) |
428 TCPOLEN_SACK_PERM);
429 }
430
Ori Finkelman89e95a62009-10-01 06:41:59 +0000431 if (unlikely(OPTION_WSCALE & opts->options)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700432 *ptr++ = htonl((TCPOPT_NOP << 24) |
433 (TCPOPT_WINDOW << 16) |
434 (TCPOLEN_WINDOW << 8) |
435 opts->ws);
436 }
437
438 if (unlikely(opts->num_sack_blocks)) {
439 struct tcp_sack_block *sp = tp->rx_opt.dsack ?
440 tp->duplicate_sack : tp->selective_acks;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800441 int this_sack;
442
443 *ptr++ = htonl((TCPOPT_NOP << 24) |
444 (TCPOPT_NOP << 16) |
445 (TCPOPT_SACK << 8) |
Adam Langley33ad7982008-07-19 00:04:31 -0700446 (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800447 TCPOLEN_SACK_PERBLOCK)));
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800448
Adam Langley33ad7982008-07-19 00:04:31 -0700449 for (this_sack = 0; this_sack < opts->num_sack_blocks;
450 ++this_sack) {
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800451 *ptr++ = htonl(sp[this_sack].start_seq);
452 *ptr++ = htonl(sp[this_sack].end_seq);
453 }
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800454
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +0000455 tp->rx_opt.dsack = 0;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800456 }
457}
458
Andi Kleen67edfef2009-07-21 23:00:40 +0000459/* Compute TCP options for SYN packets. This is not the final
460 * network wire format yet.
461 */
Adam Langley33ad7982008-07-19 00:04:31 -0700462static unsigned tcp_syn_options(struct sock *sk, struct sk_buff *skb,
463 struct tcp_out_options *opts,
464 struct tcp_md5sig_key **md5) {
465 struct tcp_sock *tp = tcp_sk(sk);
466 unsigned size = 0;
Gilad Ben-Yossef1aba7212009-10-28 04:15:24 +0000467 struct dst_entry *dst = __sk_dst_get(sk);
Adam Langley33ad7982008-07-19 00:04:31 -0700468
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800469#ifdef CONFIG_TCP_MD5SIG
Adam Langley33ad7982008-07-19 00:04:31 -0700470 *md5 = tp->af_specific->md5_lookup(sk, sk);
471 if (*md5) {
472 opts->options |= OPTION_MD5;
473 size += TCPOLEN_MD5SIG_ALIGNED;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800474 }
Adam Langley33ad7982008-07-19 00:04:31 -0700475#else
476 *md5 = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800477#endif
Adam Langley33ad7982008-07-19 00:04:31 -0700478
479 /* We always get an MSS option. The option bytes which will be seen in
480 * normal data packets should timestamps be used, must be in the MSS
481 * advertised. But we subtract them from tp->mss_cache so that
482 * calculations in tcp_sendmsg are simpler etc. So account for this
483 * fact here if necessary. If we don't do this correctly, as a
484 * receiver we won't recognize data packets as being full sized when we
485 * should, and thus we won't abide by the delayed ACK rules correctly.
486 * SACKs don't matter, we never delay an ACK when we have any of those
487 * going out. */
488 opts->mss = tcp_advertise_mss(sk);
489 size += TCPOLEN_MSS_ALIGNED;
490
491 if (likely(sysctl_tcp_timestamps && *md5 == NULL)) {
492 opts->options |= OPTION_TS;
493 opts->tsval = TCP_SKB_CB(skb)->when;
494 opts->tsecr = tp->rx_opt.ts_recent;
495 size += TCPOLEN_TSTAMP_ALIGNED;
496 }
497 if (likely(sysctl_tcp_window_scaling)) {
498 opts->ws = tp->rx_opt.rcv_wscale;
Ori Finkelman89e95a62009-10-01 06:41:59 +0000499 opts->options |= OPTION_WSCALE;
500 size += TCPOLEN_WSCALE_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700501 }
Gilad Ben-Yossef1aba7212009-10-28 04:15:24 +0000502 if (likely(sysctl_tcp_sack &&
503 !dst_feature(dst, RTAX_FEATURE_NO_SACK))) {
Adam Langley33ad7982008-07-19 00:04:31 -0700504 opts->options |= OPTION_SACK_ADVERTISE;
David S. Millerb32d1312008-07-21 18:45:34 -0700505 if (unlikely(!(OPTION_TS & opts->options)))
Adam Langley33ad7982008-07-19 00:04:31 -0700506 size += TCPOLEN_SACKPERM_ALIGNED;
507 }
508
509 return size;
510}
511
Andi Kleen67edfef2009-07-21 23:00:40 +0000512/* Set up TCP options for SYN-ACKs. */
Adam Langley33ad7982008-07-19 00:04:31 -0700513static unsigned tcp_synack_options(struct sock *sk,
514 struct request_sock *req,
515 unsigned mss, struct sk_buff *skb,
516 struct tcp_out_options *opts,
517 struct tcp_md5sig_key **md5) {
518 unsigned size = 0;
519 struct inet_request_sock *ireq = inet_rsk(req);
520 char doing_ts;
521
522#ifdef CONFIG_TCP_MD5SIG
523 *md5 = tcp_rsk(req)->af_specific->md5_lookup(sk, req);
524 if (*md5) {
525 opts->options |= OPTION_MD5;
526 size += TCPOLEN_MD5SIG_ALIGNED;
527 }
528#else
529 *md5 = NULL;
530#endif
531
532 /* we can't fit any SACK blocks in a packet with MD5 + TS
533 options. There was discussion about disabling SACK rather than TS in
534 order to fit in better with old, buggy kernels, but that was deemed
535 to be unnecessary. */
536 doing_ts = ireq->tstamp_ok && !(*md5 && ireq->sack_ok);
537
538 opts->mss = mss;
539 size += TCPOLEN_MSS_ALIGNED;
540
541 if (likely(ireq->wscale_ok)) {
542 opts->ws = ireq->rcv_wscale;
Ori Finkelman89e95a62009-10-01 06:41:59 +0000543 opts->options |= OPTION_WSCALE;
544 size += TCPOLEN_WSCALE_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700545 }
546 if (likely(doing_ts)) {
547 opts->options |= OPTION_TS;
548 opts->tsval = TCP_SKB_CB(skb)->when;
549 opts->tsecr = req->ts_recent;
550 size += TCPOLEN_TSTAMP_ALIGNED;
551 }
552 if (likely(ireq->sack_ok)) {
553 opts->options |= OPTION_SACK_ADVERTISE;
554 if (unlikely(!doing_ts))
555 size += TCPOLEN_SACKPERM_ALIGNED;
556 }
557
558 return size;
559}
560
Andi Kleen67edfef2009-07-21 23:00:40 +0000561/* Compute TCP options for ESTABLISHED sockets. This is not the
562 * final wire format yet.
563 */
Adam Langley33ad7982008-07-19 00:04:31 -0700564static unsigned tcp_established_options(struct sock *sk, struct sk_buff *skb,
565 struct tcp_out_options *opts,
566 struct tcp_md5sig_key **md5) {
567 struct tcp_skb_cb *tcb = skb ? TCP_SKB_CB(skb) : NULL;
568 struct tcp_sock *tp = tcp_sk(sk);
569 unsigned size = 0;
Ilpo Järvinencabeccb2009-02-28 04:44:38 +0000570 unsigned int eff_sacks;
Adam Langley33ad7982008-07-19 00:04:31 -0700571
572#ifdef CONFIG_TCP_MD5SIG
573 *md5 = tp->af_specific->md5_lookup(sk, sk);
574 if (unlikely(*md5)) {
575 opts->options |= OPTION_MD5;
576 size += TCPOLEN_MD5SIG_ALIGNED;
577 }
578#else
579 *md5 = NULL;
580#endif
581
582 if (likely(tp->rx_opt.tstamp_ok)) {
583 opts->options |= OPTION_TS;
584 opts->tsval = tcb ? tcb->when : 0;
585 opts->tsecr = tp->rx_opt.ts_recent;
586 size += TCPOLEN_TSTAMP_ALIGNED;
587 }
588
Ilpo Järvinencabeccb2009-02-28 04:44:38 +0000589 eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;
590 if (unlikely(eff_sacks)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700591 const unsigned remaining = MAX_TCP_OPTION_SPACE - size;
592 opts->num_sack_blocks =
Ilpo Järvinencabeccb2009-02-28 04:44:38 +0000593 min_t(unsigned, eff_sacks,
Adam Langley33ad7982008-07-19 00:04:31 -0700594 (remaining - TCPOLEN_SACK_BASE_ALIGNED) /
595 TCPOLEN_SACK_PERBLOCK);
596 size += TCPOLEN_SACK_BASE_ALIGNED +
597 opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK;
598 }
599
600 return size;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/* This routine actually transmits TCP packets queued in by
604 * tcp_do_sendmsg(). This is used by both the initial
605 * transmission and possible later retransmissions.
606 * All SKB's seen here are completely headerless. It is our
607 * job to build the TCP header, and pass the packet down to
608 * IP so it can do the same plus pass the packet off to the
609 * device.
610 *
611 * We are working here with either a clone of the original
612 * SKB, or a fresh unique copy made by the retransmit engine.
613 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800614static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
615 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800617 const struct inet_connection_sock *icsk = inet_csk(sk);
618 struct inet_sock *inet;
619 struct tcp_sock *tp;
620 struct tcp_skb_cb *tcb;
Adam Langley33ad7982008-07-19 00:04:31 -0700621 struct tcp_out_options opts;
622 unsigned tcp_options_size, tcp_header_size;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800623 struct tcp_md5sig_key *md5;
624 __u8 *md5_hash_location;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800625 struct tcphdr *th;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800626 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800628 BUG_ON(!skb || !tcp_skb_pcount(skb));
629
630 /* If congestion control is doing timestamping, we must
631 * take such a timestamp before we potentially clone/copy.
632 */
Stephen Hemminger164891a2007-04-23 22:26:16 -0700633 if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800634 __net_timestamp(skb);
635
636 if (likely(clone_it)) {
637 if (unlikely(skb_cloned(skb)))
638 skb = pskb_copy(skb, gfp_mask);
639 else
640 skb = skb_clone(skb, gfp_mask);
641 if (unlikely(!skb))
642 return -ENOBUFS;
643 }
644
645 inet = inet_sk(sk);
646 tp = tcp_sk(sk);
647 tcb = TCP_SKB_CB(skb);
Adam Langley33ad7982008-07-19 00:04:31 -0700648 memset(&opts, 0, sizeof(opts));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Adam Langley33ad7982008-07-19 00:04:31 -0700650 if (unlikely(tcb->flags & TCPCB_FLAG_SYN))
651 tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5);
652 else
653 tcp_options_size = tcp_established_options(sk, skb, &opts,
654 &md5);
655 tcp_header_size = tcp_options_size + sizeof(struct tcphdr);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900656
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800657 if (tcp_packets_in_flight(tp) == 0)
658 tcp_ca_event(sk, CA_EVENT_TX_START);
659
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700660 skb_push(skb, tcp_header_size);
661 skb_reset_transport_header(skb);
David S. Millere89862f2007-01-26 01:04:55 -0800662 skb_set_owner_w(skb, sk);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800663
664 /* Build TCP header and checksum it. */
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700665 th = tcp_hdr(skb);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000666 th->source = inet->inet_sport;
667 th->dest = inet->inet_dport;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800668 th->seq = htonl(tcb->seq);
669 th->ack_seq = htonl(tp->rcv_nxt);
Al Virodf7a3b02006-09-27 18:38:52 -0700670 *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) |
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800671 tcb->flags);
672
673 if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
674 /* RFC1323: The window in SYN & SYN/ACK segments
675 * is never scaled.
676 */
Ilpo Järvinen600ff0c2007-02-13 12:42:11 -0800677 th->window = htons(min(tp->rcv_wnd, 65535U));
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800678 } else {
679 th->window = htons(tcp_select_window(sk));
680 }
681 th->check = 0;
682 th->urg_ptr = 0;
683
Ilpo Järvinen33f5f572008-10-07 14:43:06 -0700684 /* The urg_mode check is necessary during a below snd_una win probe */
Herbert Xu76913672009-02-21 23:52:29 -0800685 if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) {
686 if (before(tp->snd_up, tcb->seq + 0x10000)) {
687 th->urg_ptr = htons(tp->snd_up - tcb->seq);
688 th->urg = 1;
689 } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) {
690 th->urg_ptr = 0xFFFF;
691 th->urg = 1;
692 }
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800693 }
694
Adam Langley33ad7982008-07-19 00:04:31 -0700695 tcp_options_write((__be32 *)(th + 1), tp, &opts, &md5_hash_location);
696 if (likely((tcb->flags & TCPCB_FLAG_SYN) == 0))
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700697 TCP_ECN_send(sk, skb, tcp_header_size);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800698
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800699#ifdef CONFIG_TCP_MD5SIG
700 /* Calculate the MD5 hash, as we have all we need now */
701 if (md5) {
Adam Langley33ad7982008-07-19 00:04:31 -0700702 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800703 tp->af_specific->calc_md5_hash(md5_hash_location,
Adam Langley49a72df2008-07-19 00:01:42 -0700704 md5, sk, NULL, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800705 }
706#endif
707
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800708 icsk->icsk_af_ops->send_check(sk, skb->len, skb);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800709
710 if (likely(tcb->flags & TCPCB_FLAG_ACK))
711 tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
712
713 if (skb->len != tcp_header_size)
714 tcp_event_data_sent(tp, skb, sk);
715
Wei Yongjunbd37a082006-08-07 21:04:15 -0700716 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -0700717 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800718
David S. Millere89862f2007-01-26 01:04:55 -0800719 err = icsk->icsk_af_ops->queue_xmit(skb, 0);
Hua Zhong83de47c2006-04-28 15:26:50 -0700720 if (likely(err <= 0))
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800721 return err;
722
Ilpo Järvinen3cfe3ba2007-02-27 10:09:49 -0800723 tcp_enter_cwr(sk, 1);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800724
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200725 return net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Andi Kleen67edfef2009-07-21 23:00:40 +0000728/* This routine just queues the buffer for sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
730 * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
731 * otherwise socket can stall.
732 */
733static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
734{
735 struct tcp_sock *tp = tcp_sk(sk);
736
737 /* Advance write_seq and place onto the write_queue. */
738 tp->write_seq = TCP_SKB_CB(skb)->end_seq;
739 skb_header_release(skb);
David S. Millerfe067e82007-03-07 12:12:44 -0800740 tcp_add_write_queue_tail(sk, skb);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800741 sk->sk_wmem_queued += skb->truesize;
742 sk_mem_charge(sk, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Andi Kleen67edfef2009-07-21 23:00:40 +0000745/* Initialize TSO segments for a packet. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800746static void tcp_set_skb_tso_segs(struct sock *sk, struct sk_buff *skb,
747 unsigned int mss_now)
David S. Millerf6302d12005-07-05 15:18:03 -0700748{
Herbert Xu8e5b9dd2009-06-28 18:03:30 +0000749 if (skb->len <= mss_now || !sk_can_gso(sk) ||
750 skb->ip_summed == CHECKSUM_NONE) {
David S. Millerf6302d12005-07-05 15:18:03 -0700751 /* Avoid the costly divide in the normal
752 * non-TSO case.
753 */
Herbert Xu79671682006-06-22 02:40:14 -0700754 skb_shinfo(skb)->gso_segs = 1;
755 skb_shinfo(skb)->gso_size = 0;
756 skb_shinfo(skb)->gso_type = 0;
David S. Millerf6302d12005-07-05 15:18:03 -0700757 } else {
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700758 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss_now);
Herbert Xu79671682006-06-22 02:40:14 -0700759 skb_shinfo(skb)->gso_size = mss_now;
Herbert Xubcd76112006-06-30 13:36:35 -0700760 skb_shinfo(skb)->gso_type = sk->sk_gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762}
763
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700764/* When a modification to fackets out becomes necessary, we need to check
Ilpo Järvinen68f83532007-11-15 19:50:37 -0800765 * skb is counted to fackets_out or not.
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700766 */
Ilpo Järvinena47e5a92007-11-15 19:41:46 -0800767static void tcp_adjust_fackets_out(struct sock *sk, struct sk_buff *skb,
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700768 int decr)
769{
Ilpo Järvinena47e5a92007-11-15 19:41:46 -0800770 struct tcp_sock *tp = tcp_sk(sk);
771
Ilpo Järvinendc869672007-10-01 15:27:19 -0700772 if (!tp->sacked_out || tcp_is_reno(tp))
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700773 return;
774
Ilpo Järvinen6859d492007-12-02 00:48:06 +0200775 if (after(tcp_highest_sack_seq(tp), TCP_SKB_CB(skb)->seq))
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700776 tp->fackets_out -= decr;
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700777}
778
Ilpo Järvinen797108d2009-04-01 23:15:17 +0000779/* Pcount in the middle of the write queue got changed, we need to do various
780 * tweaks to fix counters
781 */
782static void tcp_adjust_pcount(struct sock *sk, struct sk_buff *skb, int decr)
783{
784 struct tcp_sock *tp = tcp_sk(sk);
785
786 tp->packets_out -= decr;
787
788 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
789 tp->sacked_out -= decr;
790 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
791 tp->retrans_out -= decr;
792 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
793 tp->lost_out -= decr;
794
795 /* Reno case is special. Sigh... */
796 if (tcp_is_reno(tp) && decr > 0)
797 tp->sacked_out -= min_t(u32, tp->sacked_out, decr);
798
799 tcp_adjust_fackets_out(sk, skb, decr);
800
801 if (tp->lost_skb_hint &&
802 before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) &&
Ilpo Järvinen52cf3cc2009-04-18 05:48:48 +0000803 (tcp_is_fack(tp) || (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)))
Ilpo Järvinen797108d2009-04-01 23:15:17 +0000804 tp->lost_cnt_hint -= decr;
805
806 tcp_verify_left_out(tp);
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/* Function to create two new TCP segments. Shrinks the given segment
810 * to the specified size and appends a new segment with the rest of the
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900811 * packet to the list. This won't be called frequently, I hope.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * Remember, these are still headerless SKBs at this point.
813 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800814int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
815 unsigned int mss_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct tcp_sock *tp = tcp_sk(sk);
818 struct sk_buff *buff;
David S. Miller6475be12005-09-01 22:47:01 -0700819 int nsize, old_factor;
Herbert Xub60b49e2006-04-19 21:35:00 -0700820 int nlen;
Ilpo Järvinen9ce01462009-02-28 04:44:42 +0000821 u8 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Herbert Xub2cc99f02005-10-20 17:13:13 -0200823 BUG_ON(len > skb->len);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -0800824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 nsize = skb_headlen(skb) - len;
826 if (nsize < 0)
827 nsize = 0;
828
829 if (skb_cloned(skb) &&
830 skb_is_nonlinear(skb) &&
831 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
832 return -ENOMEM;
833
834 /* Get a new skb... force flag on. */
835 buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
836 if (buff == NULL)
837 return -ENOMEM; /* We'll just try again later. */
Herbert Xuef5cb972006-04-18 13:24:14 -0700838
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800839 sk->sk_wmem_queued += buff->truesize;
840 sk_mem_charge(sk, buff->truesize);
Herbert Xub60b49e2006-04-19 21:35:00 -0700841 nlen = skb->len - len - nsize;
842 buff->truesize += nlen;
843 skb->truesize -= nlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* Correct the sequence numbers. */
846 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
847 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
848 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
849
850 /* PSH and FIN should only be set in the second packet. */
851 flags = TCP_SKB_CB(skb)->flags;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800852 TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN | TCPCB_FLAG_PSH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 TCP_SKB_CB(buff)->flags = flags;
Herbert Xue14c3caf2005-09-19 18:18:38 -0700854 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Patrick McHardy84fa7932006-08-29 16:44:56 -0700856 if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* Copy and checksum data tail into the new buffer. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800858 buff->csum = csum_partial_copy_nocheck(skb->data + len,
859 skb_put(buff, nsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 nsize, 0);
861
862 skb_trim(skb, len);
863
864 skb->csum = csum_block_sub(skb->csum, buff->csum, len);
865 } else {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700866 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 skb_split(skb, buff, len);
868 }
869
870 buff->ip_summed = skb->ip_summed;
871
872 /* Looks stupid, but our code really uses when of
873 * skbs, which it never sent before. --ANK
874 */
875 TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700876 buff->tstamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
David S. Miller6475be12005-09-01 22:47:01 -0700878 old_factor = tcp_skb_pcount(skb);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Fix up tso_factor for both original and new SKB. */
David S. Miller846998a2005-08-04 19:52:01 -0700881 tcp_set_skb_tso_segs(sk, skb, mss_now);
882 tcp_set_skb_tso_segs(sk, buff, mss_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
David S. Miller6475be12005-09-01 22:47:01 -0700884 /* If this packet has been sent out already, we must
885 * adjust the various packet counters.
886 */
Herbert Xucf0b4502005-09-08 15:10:52 -0700887 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
David S. Miller6475be12005-09-01 22:47:01 -0700888 int diff = old_factor - tcp_skb_pcount(skb) -
889 tcp_skb_pcount(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Ilpo Järvinen797108d2009-04-01 23:15:17 +0000891 if (diff)
892 tcp_adjust_pcount(sk, skb, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
895 /* Link BUFF into the send queue. */
David S. Millerf44b5272005-07-05 15:18:34 -0700896 skb_header_release(buff);
David S. Millerfe067e82007-03-07 12:12:44 -0800897 tcp_insert_write_queue_after(skb, buff, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return 0;
900}
901
902/* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
903 * eventually). The difference is that pulled data not copied, but
904 * immediately discarded.
905 */
Herbert Xu ~{PmVHI~}f2911962006-06-05 15:03:37 -0700906static void __pskb_trim_head(struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 int i, k, eat;
909
910 eat = len;
911 k = 0;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800912 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (skb_shinfo(skb)->frags[i].size <= eat) {
914 put_page(skb_shinfo(skb)->frags[i].page);
915 eat -= skb_shinfo(skb)->frags[i].size;
916 } else {
917 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
918 if (eat) {
919 skb_shinfo(skb)->frags[k].page_offset += eat;
920 skb_shinfo(skb)->frags[k].size -= eat;
921 eat = 0;
922 }
923 k++;
924 }
925 }
926 skb_shinfo(skb)->nr_frags = k;
927
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700928 skb_reset_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 skb->data_len -= len;
930 skb->len = skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Andi Kleen67edfef2009-07-21 23:00:40 +0000933/* Remove acked data from a packet in the transmit queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
935{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800936 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -ENOMEM;
938
Herbert Xu ~{PmVHI~}f2911962006-06-05 15:03:37 -0700939 /* If len == headlen, we avoid __skb_pull to preserve alignment. */
940 if (unlikely(len < skb_headlen(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 __skb_pull(skb, len);
Herbert Xu ~{PmVHI~}f2911962006-06-05 15:03:37 -0700942 else
943 __pskb_trim_head(skb, len - skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 TCP_SKB_CB(skb)->seq += len;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700946 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 skb->truesize -= len;
949 sk->sk_wmem_queued -= len;
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800950 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
952
953 /* Any change of skb->len requires recalculation of tso
954 * factor and mss.
955 */
956 if (tcp_skb_pcount(skb) > 1)
Ilpo Järvinen0c54b852009-03-14 14:23:05 +0000957 tcp_set_skb_tso_segs(sk, skb, tcp_current_mss(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 return 0;
960}
961
Andi Kleen67edfef2009-07-21 23:00:40 +0000962/* Calculate MSS. Not accounting for SACKs here. */
John Heffner5d424d52006-03-20 17:53:41 -0800963int tcp_mtu_to_mss(struct sock *sk, int pmtu)
964{
965 struct tcp_sock *tp = tcp_sk(sk);
966 struct inet_connection_sock *icsk = inet_csk(sk);
967 int mss_now;
968
969 /* Calculate base mss without TCP options:
970 It is MMS_S - sizeof(tcphdr) of rfc1122
971 */
972 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
973
974 /* Clamp it (mss_clamp does not include tcp options) */
975 if (mss_now > tp->rx_opt.mss_clamp)
976 mss_now = tp->rx_opt.mss_clamp;
977
978 /* Now subtract optional transport overhead */
979 mss_now -= icsk->icsk_ext_hdr_len;
980
981 /* Then reserve room for full set of TCP options and 8 bytes of data */
982 if (mss_now < 48)
983 mss_now = 48;
984
985 /* Now subtract TCP options size, not including SACKs */
986 mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
987
988 return mss_now;
989}
990
991/* Inverse of above */
992int tcp_mss_to_mtu(struct sock *sk, int mss)
993{
994 struct tcp_sock *tp = tcp_sk(sk);
995 struct inet_connection_sock *icsk = inet_csk(sk);
996 int mtu;
997
998 mtu = mss +
999 tp->tcp_header_len +
1000 icsk->icsk_ext_hdr_len +
1001 icsk->icsk_af_ops->net_header_len;
1002
1003 return mtu;
1004}
1005
Andi Kleen67edfef2009-07-21 23:00:40 +00001006/* MTU probing init per socket */
John Heffner5d424d52006-03-20 17:53:41 -08001007void tcp_mtup_init(struct sock *sk)
1008{
1009 struct tcp_sock *tp = tcp_sk(sk);
1010 struct inet_connection_sock *icsk = inet_csk(sk);
1011
1012 icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
1013 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001014 icsk->icsk_af_ops->net_header_len;
John Heffner5d424d52006-03-20 17:53:41 -08001015 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
1016 icsk->icsk_mtup.probe_size = 0;
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/* This function synchronize snd mss to current pmtu/exthdr set.
1020
1021 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
1022 for TCP options, but includes only bare TCP header.
1023
1024 tp->rx_opt.mss_clamp is mss negotiated at connection setup.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001025 It is minimum of user_mss and mss received with SYN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 It also does not include TCP options.
1027
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001028 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 tp->mss_cache is current effective sending mss, including
1031 all tcp options except for SACKs. It is evaluated,
1032 taking into account current pmtu, but never exceeds
1033 tp->rx_opt.mss_clamp.
1034
1035 NOTE1. rfc1122 clearly states that advertised MSS
1036 DOES NOT include either tcp or ip options.
1037
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001038 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
1039 are READ ONLY outside this function. --ANK (980731)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
1042{
1043 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001044 struct inet_connection_sock *icsk = inet_csk(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001045 int mss_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
John Heffner5d424d52006-03-20 17:53:41 -08001047 if (icsk->icsk_mtup.search_high > pmtu)
1048 icsk->icsk_mtup.search_high = pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
John Heffner5d424d52006-03-20 17:53:41 -08001050 mss_now = tcp_mtu_to_mss(sk, pmtu);
Ilpo Järvinen409d22b2007-12-31 14:57:40 -08001051 mss_now = tcp_bound_to_half_wnd(tp, mss_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* And store cached results */
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001054 icsk->icsk_pmtu_cookie = pmtu;
John Heffner5d424d52006-03-20 17:53:41 -08001055 if (icsk->icsk_mtup.enabled)
1056 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001057 tp->mss_cache = mss_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 return mss_now;
1060}
1061
1062/* Compute the current effective MSS, taking SACKs and IP options,
1063 * and even PMTU discovery events into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 */
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001065unsigned int tcp_current_mss(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct tcp_sock *tp = tcp_sk(sk);
1068 struct dst_entry *dst = __sk_dst_get(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001069 u32 mss_now;
Adam Langley33ad7982008-07-19 00:04:31 -07001070 unsigned header_len;
1071 struct tcp_out_options opts;
1072 struct tcp_md5sig_key *md5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001074 mss_now = tp->mss_cache;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (dst) {
1077 u32 mtu = dst_mtu(dst);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001078 if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 mss_now = tcp_sync_mss(sk, mtu);
1080 }
1081
Adam Langley33ad7982008-07-19 00:04:31 -07001082 header_len = tcp_established_options(sk, NULL, &opts, &md5) +
1083 sizeof(struct tcphdr);
1084 /* The mss_cache is sized based on tp->tcp_header_len, which assumes
1085 * some common options. If this is an odd packet (because we have SACK
1086 * blocks etc) then our calculated header_len will be different, and
1087 * we have to adjust mss_now correspondingly */
1088 if (header_len != tp->tcp_header_len) {
1089 int delta = (int) header_len - tp->tcp_header_len;
1090 mss_now -= delta;
1091 }
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return mss_now;
1094}
1095
David S. Millera762a982005-07-05 15:18:51 -07001096/* Congestion window validation. (RFC2861) */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001097static void tcp_cwnd_validate(struct sock *sk)
David S. Millera762a982005-07-05 15:18:51 -07001098{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001099 struct tcp_sock *tp = tcp_sk(sk);
David S. Millera762a982005-07-05 15:18:51 -07001100
Ilpo Järvinend436d682007-12-31 14:58:00 -08001101 if (tp->packets_out >= tp->snd_cwnd) {
David S. Millera762a982005-07-05 15:18:51 -07001102 /* Network is feed fully. */
1103 tp->snd_cwnd_used = 0;
1104 tp->snd_cwnd_stamp = tcp_time_stamp;
1105 } else {
1106 /* Network starves. */
1107 if (tp->packets_out > tp->snd_cwnd_used)
1108 tp->snd_cwnd_used = tp->packets_out;
1109
David S. Miller15d33c02007-04-09 13:23:14 -07001110 if (sysctl_tcp_slow_start_after_idle &&
1111 (s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
David S. Millera762a982005-07-05 15:18:51 -07001112 tcp_cwnd_application_limited(sk);
1113 }
1114}
1115
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001116/* Returns the portion of skb which can be sent right away without
1117 * introducing MSS oddities to segment boundaries. In rare cases where
1118 * mss_now != mss_cache, we will request caller to create a small skb
1119 * per input skb which could be mostly avoided here (if desired).
Ilpo Järvinen5ea3a742008-03-11 17:55:27 -07001120 *
1121 * We explicitly want to create a request for splitting write queue tail
1122 * to a small skb for Nagle purposes while avoiding unnecessary modulos,
1123 * thus all the complexity (cwnd_len is always MSS multiple which we
1124 * return whenever allowed by the other factors). Basically we need the
1125 * modulo only when the receiver window alone is the limiting factor or
1126 * when we would be allowed to send the split-due-to-Nagle skb fully.
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001127 */
1128static unsigned int tcp_mss_split_point(struct sock *sk, struct sk_buff *skb,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001129 unsigned int mss_now, unsigned int cwnd)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001130{
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001131 struct tcp_sock *tp = tcp_sk(sk);
1132 u32 needed, window, cwnd_len;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001133
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001134 window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001135 cwnd_len = mss_now * cwnd;
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001136
1137 if (likely(cwnd_len <= window && skb != tcp_write_queue_tail(sk)))
1138 return cwnd_len;
1139
Ilpo Järvinen5ea3a742008-03-11 17:55:27 -07001140 needed = min(skb->len, window);
1141
Ilpo Järvinen17515402008-04-15 20:36:55 -07001142 if (cwnd_len <= needed)
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001143 return cwnd_len;
1144
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001145 return needed - needed % mss_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001146}
1147
1148/* Can at least one segment of SKB be sent right now, according to the
1149 * congestion window rules? If so, return how many segments are allowed.
1150 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001151static inline unsigned int tcp_cwnd_test(struct tcp_sock *tp,
1152 struct sk_buff *skb)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001153{
1154 u32 in_flight, cwnd;
1155
1156 /* Don't be strict about the congestion window for the final FIN. */
John Heffner104439a2007-02-05 17:53:11 -08001157 if ((TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
1158 tcp_skb_pcount(skb) == 1)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001159 return 1;
1160
1161 in_flight = tcp_packets_in_flight(tp);
1162 cwnd = tp->snd_cwnd;
1163 if (in_flight < cwnd)
1164 return (cwnd - in_flight);
1165
1166 return 0;
1167}
1168
Andi Kleen67edfef2009-07-21 23:00:40 +00001169/* Intialize TSO state of a skb.
1170 * This must be invoked the first time we consider transmitting
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001171 * SKB onto the wire.
1172 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001173static int tcp_init_tso_segs(struct sock *sk, struct sk_buff *skb,
1174 unsigned int mss_now)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001175{
1176 int tso_segs = tcp_skb_pcount(skb);
1177
Ilpo Järvinenf8269a42008-12-03 21:24:48 -08001178 if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) {
David S. Miller846998a2005-08-04 19:52:01 -07001179 tcp_set_skb_tso_segs(sk, skb, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001180 tso_segs = tcp_skb_pcount(skb);
1181 }
1182 return tso_segs;
1183}
1184
Andi Kleen67edfef2009-07-21 23:00:40 +00001185/* Minshall's variant of the Nagle send check. */
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001186static inline int tcp_minshall_check(const struct tcp_sock *tp)
1187{
Jianjun Kong09cb1052008-11-03 00:27:11 -08001188 return after(tp->snd_sml, tp->snd_una) &&
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001189 !after(tp->snd_sml, tp->snd_nxt);
1190}
1191
1192/* Return 0, if packet can be sent now without violation Nagle's rules:
1193 * 1. It is full sized.
1194 * 2. Or it contains FIN. (already checked by caller)
1195 * 3. Or TCP_NODELAY was set.
1196 * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
1197 * With Minshall's modification: all sent small packets are ACKed.
1198 */
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001199static inline int tcp_nagle_check(const struct tcp_sock *tp,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001200 const struct sk_buff *skb,
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001201 unsigned mss_now, int nonagle)
1202{
1203 return (skb->len < mss_now &&
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001204 ((nonagle & TCP_NAGLE_CORK) ||
1205 (!nonagle && tp->packets_out && tcp_minshall_check(tp))));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001206}
1207
1208/* Return non-zero if the Nagle test allows this packet to be
1209 * sent now.
1210 */
1211static inline int tcp_nagle_test(struct tcp_sock *tp, struct sk_buff *skb,
1212 unsigned int cur_mss, int nonagle)
1213{
1214 /* Nagle rule does not apply to frames, which sit in the middle of the
1215 * write_queue (they have no chances to get new data).
1216 *
1217 * This is implemented in the callers, where they modify the 'nonagle'
1218 * argument based upon the location of SKB in the send queue.
1219 */
1220 if (nonagle & TCP_NAGLE_PUSH)
1221 return 1;
1222
Ilpo Järvinend551e452007-04-30 00:42:20 -07001223 /* Don't use the nagle rule for urgent data (or for the final FIN).
1224 * Nagle can be ignored during F-RTO too (see RFC4138).
1225 */
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07001226 if (tcp_urg_mode(tp) || (tp->frto_counter == 2) ||
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001227 (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN))
1228 return 1;
1229
1230 if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
1231 return 1;
1232
1233 return 0;
1234}
1235
1236/* Does at least the first segment of SKB fit into the send window? */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001237static inline int tcp_snd_wnd_test(struct tcp_sock *tp, struct sk_buff *skb,
1238 unsigned int cur_mss)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001239{
1240 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
1241
1242 if (skb->len > cur_mss)
1243 end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
1244
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001245 return !after(end_seq, tcp_wnd_end(tp));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001246}
1247
David S. Millerfe067e82007-03-07 12:12:44 -08001248/* This checks if the data bearing packet SKB (usually tcp_send_head(sk))
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001249 * should be put on the wire right now. If so, it returns the number of
1250 * packets allowed by the congestion window.
1251 */
1252static unsigned int tcp_snd_test(struct sock *sk, struct sk_buff *skb,
1253 unsigned int cur_mss, int nonagle)
1254{
1255 struct tcp_sock *tp = tcp_sk(sk);
1256 unsigned int cwnd_quota;
1257
David S. Miller846998a2005-08-04 19:52:01 -07001258 tcp_init_tso_segs(sk, skb, cur_mss);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001259
1260 if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
1261 return 0;
1262
1263 cwnd_quota = tcp_cwnd_test(tp, skb);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001264 if (cwnd_quota && !tcp_snd_wnd_test(tp, skb, cur_mss))
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001265 cwnd_quota = 0;
1266
1267 return cwnd_quota;
1268}
1269
Andi Kleen67edfef2009-07-21 23:00:40 +00001270/* Test if sending is allowed right now. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001271int tcp_may_send_now(struct sock *sk)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001272{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001273 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08001274 struct sk_buff *skb = tcp_send_head(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001275
1276 return (skb &&
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001277 tcp_snd_test(sk, skb, tcp_current_mss(sk),
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001278 (tcp_skb_is_last(sk, skb) ?
Ilpo Järvinen4e67d872007-12-05 02:25:32 -08001279 tp->nonagle : TCP_NAGLE_PUSH)));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001280}
1281
1282/* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
1283 * which is put after SKB on the list. It is very much like
1284 * tcp_fragment() except that it may make several kinds of assumptions
1285 * in order to speed up the splitting operation. In particular, we
1286 * know that all the data is in scatter-gather pages, and that the
1287 * packet has never been sent out before (and thus is not cloned).
1288 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001289static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
1290 unsigned int mss_now)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001291{
1292 struct sk_buff *buff;
1293 int nlen = skb->len - len;
Ilpo Järvinen9ce01462009-02-28 04:44:42 +00001294 u8 flags;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001295
1296 /* All of a TSO frame must be composed of paged data. */
Herbert Xuc8ac3772005-08-16 20:43:40 -07001297 if (skb->len != skb->data_len)
1298 return tcp_fragment(sk, skb, len, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001299
Pavel Emelyanovdf97c702007-11-29 21:22:33 +11001300 buff = sk_stream_alloc_skb(sk, 0, GFP_ATOMIC);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001301 if (unlikely(buff == NULL))
1302 return -ENOMEM;
1303
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001304 sk->sk_wmem_queued += buff->truesize;
1305 sk_mem_charge(sk, buff->truesize);
Herbert Xub60b49e2006-04-19 21:35:00 -07001306 buff->truesize += nlen;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001307 skb->truesize -= nlen;
1308
1309 /* Correct the sequence numbers. */
1310 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
1311 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
1312 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
1313
1314 /* PSH and FIN should only be set in the second packet. */
1315 flags = TCP_SKB_CB(skb)->flags;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001316 TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN | TCPCB_FLAG_PSH);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001317 TCP_SKB_CB(buff)->flags = flags;
1318
1319 /* This packet was never sent out yet, so no SACK bits. */
1320 TCP_SKB_CB(buff)->sacked = 0;
1321
Patrick McHardy84fa7932006-08-29 16:44:56 -07001322 buff->ip_summed = skb->ip_summed = CHECKSUM_PARTIAL;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001323 skb_split(skb, buff, len);
1324
1325 /* Fix up tso_factor for both original and new SKB. */
David S. Miller846998a2005-08-04 19:52:01 -07001326 tcp_set_skb_tso_segs(sk, skb, mss_now);
1327 tcp_set_skb_tso_segs(sk, buff, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001328
1329 /* Link BUFF into the send queue. */
1330 skb_header_release(buff);
David S. Millerfe067e82007-03-07 12:12:44 -08001331 tcp_insert_write_queue_after(skb, buff, sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001332
1333 return 0;
1334}
1335
1336/* Try to defer sending, if possible, in order to minimize the amount
1337 * of TSO splitting we do. View it as a kind of TSO Nagle test.
1338 *
1339 * This algorithm is from John Heffner.
1340 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001341static int tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001342{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001343 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001344 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001345 u32 send_win, cong_win, limit, in_flight;
1346
1347 if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)
John Heffnerae8064a2006-10-18 20:36:48 -07001348 goto send_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001349
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001350 if (icsk->icsk_ca_state != TCP_CA_Open)
John Heffnerae8064a2006-10-18 20:36:48 -07001351 goto send_now;
1352
1353 /* Defer for less than two clock ticks. */
Ilpo Järvinenbd515c32007-12-20 20:36:03 -08001354 if (tp->tso_deferred &&
Ilpo Järvinena2acde02008-12-05 22:49:37 -08001355 (((u32)jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
John Heffnerae8064a2006-10-18 20:36:48 -07001356 goto send_now;
David S. Miller908a75c2005-07-05 15:43:58 -07001357
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001358 in_flight = tcp_packets_in_flight(tp);
1359
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001360 BUG_ON(tcp_skb_pcount(skb) <= 1 || (tp->snd_cwnd <= in_flight));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001361
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001362 send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001363
1364 /* From in_flight test above, we know that cwnd > in_flight. */
1365 cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
1366
1367 limit = min(send_win, cong_win);
1368
David S. Millerba244fe2006-03-11 18:51:49 -08001369 /* If a full-sized TSO skb can be sent, do it. */
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07001370 if (limit >= sk->sk_gso_max_size)
John Heffnerae8064a2006-10-18 20:36:48 -07001371 goto send_now;
David S. Millerba244fe2006-03-11 18:51:49 -08001372
Ilpo Järvinen62ad2762009-02-28 04:44:29 +00001373 /* Middle in queue won't get any more data, full sendable already? */
1374 if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len))
1375 goto send_now;
1376
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001377 if (sysctl_tcp_tso_win_divisor) {
1378 u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
1379
1380 /* If at least some fraction of a window is available,
1381 * just use it.
1382 */
1383 chunk /= sysctl_tcp_tso_win_divisor;
1384 if (limit >= chunk)
John Heffnerae8064a2006-10-18 20:36:48 -07001385 goto send_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001386 } else {
1387 /* Different approach, try not to defer past a single
1388 * ACK. Receiver should ACK every other full sized
1389 * frame, so if we have space for more than 3 frames
1390 * then send now.
1391 */
1392 if (limit > tcp_max_burst(tp) * tp->mss_cache)
John Heffnerae8064a2006-10-18 20:36:48 -07001393 goto send_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001394 }
1395
1396 /* Ok, it looks like it is advisable to defer. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001397 tp->tso_deferred = 1 | (jiffies << 1);
John Heffnerae8064a2006-10-18 20:36:48 -07001398
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001399 return 1;
John Heffnerae8064a2006-10-18 20:36:48 -07001400
1401send_now:
1402 tp->tso_deferred = 0;
1403 return 0;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001404}
1405
John Heffner5d424d52006-03-20 17:53:41 -08001406/* Create a new MTU probe if we are ready.
Andi Kleen67edfef2009-07-21 23:00:40 +00001407 * MTU probe is regularly attempting to increase the path MTU by
1408 * deliberately sending larger packets. This discovers routing
1409 * changes resulting in larger path MTUs.
1410 *
John Heffner5d424d52006-03-20 17:53:41 -08001411 * Returns 0 if we should wait to probe (no cwnd available),
1412 * 1 if a probe was sent,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001413 * -1 otherwise
1414 */
John Heffner5d424d52006-03-20 17:53:41 -08001415static int tcp_mtu_probe(struct sock *sk)
1416{
1417 struct tcp_sock *tp = tcp_sk(sk);
1418 struct inet_connection_sock *icsk = inet_csk(sk);
1419 struct sk_buff *skb, *nskb, *next;
1420 int len;
1421 int probe_size;
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001422 int size_needed;
John Heffner5d424d52006-03-20 17:53:41 -08001423 int copy;
1424 int mss_now;
1425
1426 /* Not currently probing/verifying,
1427 * not in recovery,
1428 * have enough cwnd, and
1429 * not SACKing (the variable headers throw things off) */
1430 if (!icsk->icsk_mtup.enabled ||
1431 icsk->icsk_mtup.probe_size ||
1432 inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
1433 tp->snd_cwnd < 11 ||
Ilpo Järvinencabeccb2009-02-28 04:44:38 +00001434 tp->rx_opt.num_sacks || tp->rx_opt.dsack)
John Heffner5d424d52006-03-20 17:53:41 -08001435 return -1;
1436
1437 /* Very simple search strategy: just double the MSS. */
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001438 mss_now = tcp_current_mss(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001439 probe_size = 2 * tp->mss_cache;
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001440 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
John Heffner5d424d52006-03-20 17:53:41 -08001441 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) {
1442 /* TODO: set timer for probe_converge_event */
1443 return -1;
1444 }
1445
1446 /* Have enough data in the send queue to probe? */
Ilpo Järvinen7f9c33e2007-11-23 19:10:56 +08001447 if (tp->write_seq - tp->snd_nxt < size_needed)
John Heffner5d424d52006-03-20 17:53:41 -08001448 return -1;
1449
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001450 if (tp->snd_wnd < size_needed)
1451 return -1;
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001452 if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp)))
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001453 return 0;
John Heffner5d424d52006-03-20 17:53:41 -08001454
Ilpo Järvinend67c58e2007-12-02 00:48:01 +02001455 /* Do we need to wait to drain cwnd? With none in flight, don't stall */
1456 if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) {
1457 if (!tcp_packets_in_flight(tp))
John Heffner5d424d52006-03-20 17:53:41 -08001458 return -1;
1459 else
1460 return 0;
1461 }
1462
1463 /* We're allowed to probe. Build it now. */
1464 if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL)
1465 return -1;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001466 sk->sk_wmem_queued += nskb->truesize;
1467 sk_mem_charge(sk, nskb->truesize);
John Heffner5d424d52006-03-20 17:53:41 -08001468
David S. Millerfe067e82007-03-07 12:12:44 -08001469 skb = tcp_send_head(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001470
1471 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
1472 TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
1473 TCP_SKB_CB(nskb)->flags = TCPCB_FLAG_ACK;
1474 TCP_SKB_CB(nskb)->sacked = 0;
1475 nskb->csum = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001476 nskb->ip_summed = skb->ip_summed;
John Heffner5d424d52006-03-20 17:53:41 -08001477
Ilpo Järvinen50c48172007-12-02 00:48:00 +02001478 tcp_insert_write_queue_before(nskb, skb, sk);
1479
John Heffner5d424d52006-03-20 17:53:41 -08001480 len = 0;
Ilpo Järvinen234b6862007-12-02 00:48:02 +02001481 tcp_for_write_queue_from_safe(skb, next, sk) {
John Heffner5d424d52006-03-20 17:53:41 -08001482 copy = min_t(int, skb->len, probe_size - len);
1483 if (nskb->ip_summed)
1484 skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
1485 else
1486 nskb->csum = skb_copy_and_csum_bits(skb, 0,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001487 skb_put(nskb, copy),
1488 copy, nskb->csum);
John Heffner5d424d52006-03-20 17:53:41 -08001489
1490 if (skb->len <= copy) {
1491 /* We've eaten all the data from this skb.
1492 * Throw it away. */
1493 TCP_SKB_CB(nskb)->flags |= TCP_SKB_CB(skb)->flags;
David S. Millerfe067e82007-03-07 12:12:44 -08001494 tcp_unlink_write_queue(skb, sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001495 sk_wmem_free_skb(sk, skb);
John Heffner5d424d52006-03-20 17:53:41 -08001496 } else {
1497 TCP_SKB_CB(nskb)->flags |= TCP_SKB_CB(skb)->flags &
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001498 ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
John Heffner5d424d52006-03-20 17:53:41 -08001499 if (!skb_shinfo(skb)->nr_frags) {
1500 skb_pull(skb, copy);
Patrick McHardy84fa7932006-08-29 16:44:56 -07001501 if (skb->ip_summed != CHECKSUM_PARTIAL)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001502 skb->csum = csum_partial(skb->data,
1503 skb->len, 0);
John Heffner5d424d52006-03-20 17:53:41 -08001504 } else {
1505 __pskb_trim_head(skb, copy);
1506 tcp_set_skb_tso_segs(sk, skb, mss_now);
1507 }
1508 TCP_SKB_CB(skb)->seq += copy;
1509 }
1510
1511 len += copy;
Ilpo Järvinen234b6862007-12-02 00:48:02 +02001512
1513 if (len >= probe_size)
1514 break;
John Heffner5d424d52006-03-20 17:53:41 -08001515 }
1516 tcp_init_tso_segs(sk, nskb, nskb->len);
1517
1518 /* We're ready to send. If this fails, the probe will
1519 * be resegmented into mss-sized pieces by tcp_write_xmit(). */
1520 TCP_SKB_CB(nskb)->when = tcp_time_stamp;
1521 if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
1522 /* Decrement cwnd here because we are sending
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001523 * effectively two packets. */
John Heffner5d424d52006-03-20 17:53:41 -08001524 tp->snd_cwnd--;
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -08001525 tcp_event_new_data_sent(sk, nskb);
John Heffner5d424d52006-03-20 17:53:41 -08001526
1527 icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
John Heffner0e7b1362006-03-20 21:32:58 -08001528 tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
1529 tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
John Heffner5d424d52006-03-20 17:53:41 -08001530
1531 return 1;
1532 }
1533
1534 return -1;
1535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/* This routine writes packets to the network. It advances the
1538 * send_head. This happens as incoming acks open up the remote
1539 * window for us.
1540 *
Ilpo Järvinenf8269a42008-12-03 21:24:48 -08001541 * LARGESEND note: !tcp_urg_mode is overkill, only frames between
1542 * snd_up-64k-mss .. snd_up cannot be large. However, taking into
1543 * account rare use of URG, this is not a big flaw.
1544 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * Returns 1, if no segments are in flight and we have queued segments, but
1546 * cannot send anything now because of SWS or another problem.
1547 */
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001548static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
1549 int push_one, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct tcp_sock *tp = tcp_sk(sk);
David S. Miller92df7b52005-07-05 15:19:06 -07001552 struct sk_buff *skb;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001553 unsigned int tso_segs, sent_pkts;
1554 int cwnd_quota;
John Heffner5d424d52006-03-20 17:53:41 -08001555 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
David S. Miller92df7b52005-07-05 15:19:06 -07001557 sent_pkts = 0;
John Heffner5d424d52006-03-20 17:53:41 -08001558
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001559 if (!push_one) {
1560 /* Do MTU probing. */
1561 result = tcp_mtu_probe(sk);
1562 if (!result) {
1563 return 0;
1564 } else if (result > 0) {
1565 sent_pkts = 1;
1566 }
John Heffner5d424d52006-03-20 17:53:41 -08001567 }
1568
David S. Millerfe067e82007-03-07 12:12:44 -08001569 while ((skb = tcp_send_head(sk))) {
Herbert Xuc8ac3772005-08-16 20:43:40 -07001570 unsigned int limit;
1571
Herbert Xub68e9f82005-08-04 19:52:02 -07001572 tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001573 BUG_ON(!tso_segs);
David S. Milleraa934662005-07-05 15:20:09 -07001574
Herbert Xub68e9f82005-08-04 19:52:02 -07001575 cwnd_quota = tcp_cwnd_test(tp, skb);
1576 if (!cwnd_quota)
1577 break;
1578
1579 if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
1580 break;
1581
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001582 if (tso_segs == 1) {
1583 if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
1584 (tcp_skb_is_last(sk, skb) ?
1585 nonagle : TCP_NAGLE_PUSH))))
1586 break;
1587 } else {
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001588 if (!push_one && tcp_tso_should_defer(sk, skb))
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001589 break;
1590 }
David S. Milleraa934662005-07-05 15:20:09 -07001591
Herbert Xuc8ac3772005-08-16 20:43:40 -07001592 limit = mss_now;
Ilpo Järvinenf8269a42008-12-03 21:24:48 -08001593 if (tso_segs > 1 && !tcp_urg_mode(tp))
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001594 limit = tcp_mss_split_point(sk, skb, mss_now,
1595 cwnd_quota);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Herbert Xuc8ac3772005-08-16 20:43:40 -07001597 if (skb->len > limit &&
1598 unlikely(tso_fragment(sk, skb, limit, mss_now)))
1599 break;
1600
David S. Miller92df7b52005-07-05 15:19:06 -07001601 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001602
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001603 if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
David S. Miller92df7b52005-07-05 15:19:06 -07001604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
David S. Miller92df7b52005-07-05 15:19:06 -07001606 /* Advance the send_head. This one is sent out.
1607 * This call will increment packets_out.
1608 */
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -08001609 tcp_event_new_data_sent(sk, skb);
David S. Miller92df7b52005-07-05 15:19:06 -07001610
1611 tcp_minshall_update(tp, mss_now, skb);
David S. Milleraa934662005-07-05 15:20:09 -07001612 sent_pkts++;
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001613
1614 if (push_one)
1615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
David S. Miller92df7b52005-07-05 15:19:06 -07001617
David S. Milleraa934662005-07-05 15:20:09 -07001618 if (likely(sent_pkts)) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001619 tcp_cwnd_validate(sk);
David S. Miller92df7b52005-07-05 15:19:06 -07001620 return 0;
1621 }
David S. Millerfe067e82007-03-07 12:12:44 -08001622 return !tp->packets_out && tcp_send_head(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
David S. Millera762a982005-07-05 15:18:51 -07001625/* Push out any pending frames which were held back due to
1626 * TCP_CORK or attempt at coalescing tiny packets.
1627 * The socket must be locked by the caller.
1628 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001629void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
1630 int nonagle)
David S. Millera762a982005-07-05 15:18:51 -07001631{
David S. Millerfe067e82007-03-07 12:12:44 -08001632 struct sk_buff *skb = tcp_send_head(sk);
David S. Millera762a982005-07-05 15:18:51 -07001633
Ilpo Järvinen726e07a2008-12-05 22:43:56 -08001634 if (!skb)
1635 return;
1636
1637 /* If we are closed, the bytes will have to remain here.
1638 * In time closedown will finish, we empty the write queue and
1639 * all will be happy.
1640 */
1641 if (unlikely(sk->sk_state == TCP_CLOSE))
1642 return;
1643
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001644 if (tcp_write_xmit(sk, cur_mss, nonagle, 0, GFP_ATOMIC))
Ilpo Järvinen726e07a2008-12-05 22:43:56 -08001645 tcp_check_probe_timer(sk);
David S. Millera762a982005-07-05 15:18:51 -07001646}
1647
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001648/* Send _single_ skb sitting at the send head. This function requires
1649 * true push pending frames to setup probe timer etc.
1650 */
1651void tcp_push_one(struct sock *sk, unsigned int mss_now)
1652{
David S. Millerfe067e82007-03-07 12:12:44 -08001653 struct sk_buff *skb = tcp_send_head(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001654
1655 BUG_ON(!skb || skb->len < mss_now);
1656
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001657 tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/* This function returns the amount that we can raise the
1661 * usable window based on the following constraints
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001662 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * 1. The window can never be shrunk once it is offered (RFC 793)
1664 * 2. We limit memory per socket
1665 *
1666 * RFC 1122:
1667 * "the suggested [SWS] avoidance algorithm for the receiver is to keep
1668 * RECV.NEXT + RCV.WIN fixed until:
1669 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
1670 *
1671 * i.e. don't raise the right edge of the window until you can raise
1672 * it at least MSS bytes.
1673 *
1674 * Unfortunately, the recommended algorithm breaks header prediction,
1675 * since header prediction assumes th->window stays fixed.
1676 *
1677 * Strictly speaking, keeping th->window fixed violates the receiver
1678 * side SWS prevention criteria. The problem is that under this rule
1679 * a stream of single byte packets will cause the right side of the
1680 * window to always advance by a single byte.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001681 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * Of course, if the sender implements sender side SWS prevention
1683 * then this will not be a problem.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001684 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * BSD seems to make the following compromise:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001686 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * If the free space is less than the 1/4 of the maximum
1688 * space available and the free space is less than 1/2 mss,
1689 * then set the window to 0.
1690 * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
1691 * Otherwise, just prevent the window from shrinking
1692 * and from being larger than the largest representable value.
1693 *
1694 * This prevents incremental opening of the window in the regime
1695 * where TCP is limited by the speed of the reader side taking
1696 * data out of the TCP receive queue. It does nothing about
1697 * those cases where the window is constrained on the sender side
1698 * because the pipeline is full.
1699 *
1700 * BSD also seems to "accidentally" limit itself to windows that are a
1701 * multiple of MSS, at least until the free space gets quite small.
1702 * This would appear to be a side effect of the mbuf implementation.
1703 * Combining these two algorithms results in the observed behavior
1704 * of having a fixed window size at almost all times.
1705 *
1706 * Below we obtain similar behavior by forcing the offered window to
1707 * a multiple of the mss when it is feasible to do so.
1708 *
1709 * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
1710 * Regular options like TIMESTAMP are taken into account.
1711 */
1712u32 __tcp_select_window(struct sock *sk)
1713{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001714 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001716 /* MSS for the peer's data. Previous versions used mss_clamp
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * here. I don't know if the value based on our guesses
1718 * of peer's MSS is better for the performance. It's more correct
1719 * but may be worse for the performance because of rcv_mss
1720 * fluctuations. --SAW 1998/11/1
1721 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001722 int mss = icsk->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int free_space = tcp_space(sk);
1724 int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
1725 int window;
1726
1727 if (mss > full_space)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001728 mss = full_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Eric Dumazetb92edbe2007-12-20 21:48:32 -08001730 if (free_space < (full_space >> 1)) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001731 icsk->icsk_ack.quick = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (tcp_memory_pressure)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001734 tp->rcv_ssthresh = min(tp->rcv_ssthresh,
1735 4U * tp->advmss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 if (free_space < mss)
1738 return 0;
1739 }
1740
1741 if (free_space > tp->rcv_ssthresh)
1742 free_space = tp->rcv_ssthresh;
1743
1744 /* Don't do rounding if we are using window scaling, since the
1745 * scaled window will not line up with the MSS boundary anyway.
1746 */
1747 window = tp->rcv_wnd;
1748 if (tp->rx_opt.rcv_wscale) {
1749 window = free_space;
1750
1751 /* Advertise enough space so that it won't get scaled away.
1752 * Import case: prevent zero window announcement if
1753 * 1<<rcv_wscale > mss.
1754 */
1755 if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
1756 window = (((window >> tp->rx_opt.rcv_wscale) + 1)
1757 << tp->rx_opt.rcv_wscale);
1758 } else {
1759 /* Get the largest window that is a nice multiple of mss.
1760 * Window clamp already applied above.
1761 * If our current window offering is within 1 mss of the
1762 * free space we just keep it. This prevents the divide
1763 * and multiply from happening most of the time.
1764 * We also don't do any window rounding when the free space
1765 * is too small.
1766 */
1767 if (window <= free_space - mss || window > free_space)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001768 window = (free_space / mss) * mss;
John Heffner84565072007-04-02 13:56:32 -07001769 else if (mss == full_space &&
Eric Dumazetb92edbe2007-12-20 21:48:32 -08001770 free_space > window + (full_space >> 1))
John Heffner84565072007-04-02 13:56:32 -07001771 window = free_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 return window;
1775}
1776
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08001777/* Collapses two adjacent SKB's during retransmission. */
1778static void tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08001781 struct sk_buff *next_skb = tcp_write_queue_next(sk, skb);
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001782 int skb_size, next_skb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001784 skb_size = skb->len;
1785 next_skb_size = next_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001787 BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1);
Ilpo Järvinena6963a62007-09-25 22:44:14 -07001788
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001789 tcp_highest_sack_combine(sk, next_skb, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001791 tcp_unlink_write_queue(next_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001793 skb_copy_from_linear_data(next_skb, skb_put(skb, next_skb_size),
1794 next_skb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001796 if (next_skb->ip_summed == CHECKSUM_PARTIAL)
1797 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001799 if (skb->ip_summed != CHECKSUM_PARTIAL)
1800 skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001802 /* Update sequence range on original skb. */
1803 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Ilpo Järvinene6c7d082009-02-28 04:44:35 +00001805 /* Merge over control information. This moves PSH/FIN etc. over */
1806 TCP_SKB_CB(skb)->flags |= TCP_SKB_CB(next_skb)->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001808 /* All done, get rid of second SKB and account for it so
1809 * packet counting does not break.
1810 */
1811 TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS;
Ilpo Järvinenb7689202007-09-20 11:37:19 -07001812
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001813 /* changed transmit queue under us so clear hints */
Ilpo Järvinenef9da472008-09-20 21:25:15 -07001814 tcp_clear_retrans_hints_partial(tp);
1815 if (next_skb == tp->retransmit_skb_hint)
1816 tp->retransmit_skb_hint = skb;
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001817
Ilpo Järvinen797108d2009-04-01 23:15:17 +00001818 tcp_adjust_pcount(sk, next_skb, tcp_skb_pcount(next_skb));
1819
Ilpo Järvinen058dc332007-12-31 04:51:11 -08001820 sk_wmem_free_skb(sk, next_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Andi Kleen67edfef2009-07-21 23:00:40 +00001823/* Check if coalescing SKBs is legal. */
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08001824static int tcp_can_collapse(struct sock *sk, struct sk_buff *skb)
1825{
1826 if (tcp_skb_pcount(skb) > 1)
1827 return 0;
1828 /* TODO: SACK collapsing could be used to remove this condition */
1829 if (skb_shinfo(skb)->nr_frags != 0)
1830 return 0;
1831 if (skb_cloned(skb))
1832 return 0;
1833 if (skb == tcp_send_head(sk))
1834 return 0;
1835 /* Some heurestics for collapsing over SACK'd could be invented */
1836 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1837 return 0;
1838
1839 return 1;
1840}
1841
Andi Kleen67edfef2009-07-21 23:00:40 +00001842/* Collapse packets in the retransmit queue to make to create
1843 * less packets on the wire. This is only done on retransmission.
1844 */
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08001845static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to,
1846 int space)
1847{
1848 struct tcp_sock *tp = tcp_sk(sk);
1849 struct sk_buff *skb = to, *tmp;
1850 int first = 1;
1851
1852 if (!sysctl_tcp_retrans_collapse)
1853 return;
1854 if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN)
1855 return;
1856
1857 tcp_for_write_queue_from_safe(skb, tmp, sk) {
1858 if (!tcp_can_collapse(sk, skb))
1859 break;
1860
1861 space -= skb->len;
1862
1863 if (first) {
1864 first = 0;
1865 continue;
1866 }
1867
1868 if (space < 0)
1869 break;
1870 /* Punt if not enough space exists in the first SKB for
1871 * the data in the second
1872 */
1873 if (skb->len > skb_tailroom(to))
1874 break;
1875
1876 if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp)))
1877 break;
1878
1879 tcp_collapse_retrans(sk, to);
1880 }
1881}
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883/* This retransmits one SKB. Policy decisions and retransmit queue
1884 * state updates are done by the caller. Returns non-zero if an
1885 * error occurred which prevented the send.
1886 */
1887int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
1888{
1889 struct tcp_sock *tp = tcp_sk(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001890 struct inet_connection_sock *icsk = inet_csk(sk);
Sridhar Samudrala7d227cd22008-05-21 16:42:20 -07001891 unsigned int cur_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int err;
1893
John Heffner5d424d52006-03-20 17:53:41 -08001894 /* Inconslusive MTU probe */
1895 if (icsk->icsk_mtup.probe_size) {
1896 icsk->icsk_mtup.probe_size = 0;
1897 }
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* Do not sent more than we queued. 1/4 is reserved for possible
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001900 * copying overhead: fragmentation, tunneling, mangling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
1902 if (atomic_read(&sk->sk_wmem_alloc) >
1903 min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
1904 return -EAGAIN;
1905
1906 if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
1907 if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1908 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
1910 return -ENOMEM;
1911 }
1912
Sridhar Samudrala7d227cd22008-05-21 16:42:20 -07001913 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
1914 return -EHOSTUNREACH; /* Routing failure or similar. */
1915
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001916 cur_mss = tcp_current_mss(sk);
Sridhar Samudrala7d227cd22008-05-21 16:42:20 -07001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /* If receiver has shrunk his window, and skb is out of
1919 * new window, do not retransmit it. The exception is the
1920 * case, when window is shrunk to zero. In this case
1921 * our retransmit serves as a zero window probe.
1922 */
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001923 if (!before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 && TCP_SKB_CB(skb)->seq != tp->snd_una)
1925 return -EAGAIN;
1926
1927 if (skb->len > cur_mss) {
David S. Miller846998a2005-08-04 19:52:01 -07001928 if (tcp_fragment(sk, skb, cur_mss, cur_mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return -ENOMEM; /* We'll try again later. */
Ilpo Järvinen02276f32009-02-28 04:44:31 +00001930 } else {
Ilpo Järvinen9eb93622009-04-01 23:18:20 +00001931 int oldpcount = tcp_skb_pcount(skb);
1932
1933 if (unlikely(oldpcount > 1)) {
1934 tcp_init_tso_segs(sk, skb, cur_mss);
1935 tcp_adjust_pcount(sk, skb, oldpcount - tcp_skb_pcount(skb));
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08001939 tcp_retrans_try_collapse(sk, skb, cur_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* Some Solaris stacks overoptimize and ignore the FIN on a
1942 * retransmit when old data is attached. So strip it off
1943 * since it is cheap to do so and saves bytes on the network.
1944 */
Stephen Hemminger2de979b2007-03-08 20:45:19 -08001945 if (skb->len > 0 &&
1946 (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
1947 tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (!pskb_trim(skb, 0)) {
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08001949 /* Reuse, even though it does some unnecessary work */
1950 tcp_init_nondata_skb(skb, TCP_SKB_CB(skb)->end_seq - 1,
1951 TCP_SKB_CB(skb)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 }
1955
1956 /* Make a copy, if the first transmission SKB clone we made
1957 * is still in somebody's hands, else make a clone.
1958 */
1959 TCP_SKB_CB(skb)->when = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
David S. Millerdfb4b9d2005-12-06 16:24:52 -08001961 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (err == 0) {
1964 /* Update global TCP statistics. */
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07001965 TCP_INC_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 tp->total_retrans++;
1968
1969#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001970 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (net_ratelimit())
1972 printk(KERN_DEBUG "retrans_out leaked.\n");
1973 }
1974#endif
Ilpo Järvinenb08d6cb2007-10-11 17:36:13 -07001975 if (!tp->retrans_out)
1976 tp->lost_retrans_low = tp->snd_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
1978 tp->retrans_out += tcp_skb_pcount(skb);
1979
1980 /* Save stamp of the first retransmit. */
1981 if (!tp->retrans_stamp)
1982 tp->retrans_stamp = TCP_SKB_CB(skb)->when;
1983
1984 tp->undo_retrans++;
1985
1986 /* snd_nxt is stored to detect loss of retransmitted segment,
1987 * see tcp_input.c tcp_sacktag_write_queue().
1988 */
1989 TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
1990 }
1991 return err;
1992}
1993
Andi Kleen67edfef2009-07-21 23:00:40 +00001994/* Check if we forward retransmits are possible in the current
1995 * window/congestion state.
1996 */
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07001997static int tcp_can_forward_retransmit(struct sock *sk)
1998{
1999 const struct inet_connection_sock *icsk = inet_csk(sk);
2000 struct tcp_sock *tp = tcp_sk(sk);
2001
2002 /* Forward retransmissions are possible only during Recovery. */
2003 if (icsk->icsk_ca_state != TCP_CA_Recovery)
2004 return 0;
2005
2006 /* No forward retransmissions in Reno are possible. */
2007 if (tcp_is_reno(tp))
2008 return 0;
2009
2010 /* Yeah, we have to make difficult choice between forward transmission
2011 * and retransmission... Both ways have their merits...
2012 *
2013 * For now we do not retransmit anything, while we have some new
2014 * segments to send. In the other cases, follow rule 3 for
2015 * NextSeg() specified in RFC3517.
2016 */
2017
2018 if (tcp_may_send_now(sk))
2019 return 0;
2020
2021 return 1;
2022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024/* This gets called after a retransmit timeout, and the initially
2025 * retransmitted data is acknowledged. It tries to continue
2026 * resending the rest of the retransmit queue, until either
2027 * we've sent it all or the congestion window limit is reached.
2028 * If doing SACK, the first ACK which comes back for a timeout
2029 * based retransmit packet might feed us FACK information again.
2030 * If so, we use it to avoid unnecessarily retransmissions.
2031 */
2032void tcp_xmit_retransmit_queue(struct sock *sk)
2033{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002034 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct tcp_sock *tp = tcp_sk(sk);
2036 struct sk_buff *skb;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002037 struct sk_buff *hole = NULL;
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002038 u32 last_lost;
Ilpo Järvinen61eb55f2008-09-20 21:22:59 -07002039 int mib_idx;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002040 int fwd_rexmitting = 0;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002041
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002042 if (!tp->lost_out)
2043 tp->retransmit_high = tp->snd_una;
2044
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002045 if (tp->retransmit_skb_hint) {
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002046 skb = tp->retransmit_skb_hint;
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002047 last_lost = TCP_SKB_CB(skb)->end_seq;
2048 if (after(last_lost, tp->retransmit_high))
2049 last_lost = tp->retransmit_high;
2050 } else {
David S. Millerfe067e82007-03-07 12:12:44 -08002051 skb = tcp_write_queue_head(sk);
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002052 last_lost = tp->snd_una;
2053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002055 tcp_for_write_queue_from(skb, sk) {
2056 __u8 sacked = TCP_SKB_CB(skb)->sacked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002058 if (skb == tcp_send_head(sk))
2059 break;
2060 /* we could do better than to assign each time */
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002061 if (hole == NULL)
2062 tp->retransmit_skb_hint = skb;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002063
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002064 /* Assume this retransmit will generate
2065 * only one packet for congestion window
2066 * calculation purposes. This works because
2067 * tcp_retransmit_skb() will chop up the
2068 * packet to be MSS sized and all the
2069 * packet counting works out.
2070 */
2071 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
2072 return;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002073
2074 if (fwd_rexmitting) {
2075begin_fwd:
2076 if (!before(TCP_SKB_CB(skb)->seq, tcp_highest_sack_seq(tp)))
2077 break;
2078 mib_idx = LINUX_MIB_TCPFORWARDRETRANS;
2079
2080 } else if (!before(TCP_SKB_CB(skb)->seq, tp->retransmit_high)) {
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002081 tp->retransmit_high = last_lost;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002082 if (!tcp_can_forward_retransmit(sk))
2083 break;
2084 /* Backtrack if necessary to non-L'ed skb */
2085 if (hole != NULL) {
2086 skb = hole;
2087 hole = NULL;
2088 }
2089 fwd_rexmitting = 1;
2090 goto begin_fwd;
2091
2092 } else if (!(sacked & TCPCB_LOST)) {
Ilpo Järvinenac11ba72009-02-28 04:44:27 +00002093 if (hole == NULL && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED)))
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002094 hole = skb;
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002095 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002097 } else {
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002098 last_lost = TCP_SKB_CB(skb)->end_seq;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002099 if (icsk->icsk_ca_state != TCP_CA_Loss)
2100 mib_idx = LINUX_MIB_TCPFASTRETRANS;
2101 else
2102 mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS;
2103 }
2104
2105 if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002106 continue;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002107
Ilpo Järvinenf0ceb0e2008-09-20 21:24:49 -07002108 if (tcp_retransmit_skb(sk, skb))
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002109 return;
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002110 NET_INC_STATS_BH(sock_net(sk), mib_idx);
2111
2112 if (skb == tcp_write_queue_head(sk))
2113 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
2114 inet_csk(sk)->icsk_rto,
2115 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119/* Send a fin. The caller locks the socket for us. This cannot be
2120 * allowed to fail queueing a FIN frame under any circumstances.
2121 */
2122void tcp_send_fin(struct sock *sk)
2123{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002124 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08002125 struct sk_buff *skb = tcp_write_queue_tail(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int mss_now;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* Optimization, tack on the FIN if we have a queue of
2129 * unsent frames. But be careful about outgoing SACKS
2130 * and IP options.
2131 */
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002132 mss_now = tcp_current_mss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David S. Millerfe067e82007-03-07 12:12:44 -08002134 if (tcp_send_head(sk) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_FIN;
2136 TCP_SKB_CB(skb)->end_seq++;
2137 tp->write_seq++;
2138 } else {
2139 /* Socket is locked, keep trying until memory is available. */
2140 for (;;) {
Wu Fengguangaa133072009-09-02 23:45:45 -07002141 skb = alloc_skb_fclone(MAX_TCP_HEADER,
2142 sk->sk_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (skb)
2144 break;
2145 yield();
2146 }
2147
2148 /* Reserve space for headers and prepare control bits. */
2149 skb_reserve(skb, MAX_TCP_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002151 tcp_init_nondata_skb(skb, tp->write_seq,
2152 TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 tcp_queue_skb(sk, skb);
2154 }
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002155 __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158/* We get here when a process closes a file descriptor (either due to
2159 * an explicit close() or as a byproduct of exit()'ing) and there
2160 * was unread data in the receive queue. This behavior is recommended
Gerrit Renker65bb7232007-04-28 21:21:46 -07002161 * by RFC 2525, section 2.17. -DaveM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 */
Al Virodd0fc662005-10-07 07:46:04 +01002163void tcp_send_active_reset(struct sock *sk, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct sk_buff *skb;
2166
2167 /* NOTE: No TCP options attached and we never retransmit this. */
2168 skb = alloc_skb(MAX_TCP_HEADER, priority);
2169 if (!skb) {
Pavel Emelyanov4e673442008-07-16 20:30:14 -07002170 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return;
2172 }
2173
2174 /* Reserve space for headers and prepare control bits. */
2175 skb_reserve(skb, MAX_TCP_HEADER);
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002176 tcp_init_nondata_skb(skb, tcp_acceptable_seq(sk),
2177 TCPCB_FLAG_ACK | TCPCB_FLAG_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* Send it off. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerdfb4b9d2005-12-06 16:24:52 -08002180 if (tcp_transmit_skb(sk, skb, 0, priority))
Pavel Emelyanov4e673442008-07-16 20:30:14 -07002181 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
Sridhar Samudrala26af65c2008-06-04 15:19:35 -07002182
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07002183 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTRSTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Andi Kleen67edfef2009-07-21 23:00:40 +00002186/* Send a crossed SYN-ACK during socket establishment.
2187 * WARNING: This routine must only be called when we have already sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 * a SYN packet that crossed the incoming SYN that caused this routine
2189 * to get called. If this assumption fails then the initial rcv_wnd
2190 * and rcv_wscale values will not be correct.
2191 */
2192int tcp_send_synack(struct sock *sk)
2193{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002194 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
David S. Millerfe067e82007-03-07 12:12:44 -08002196 skb = tcp_write_queue_head(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002197 if (skb == NULL || !(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 printk(KERN_DEBUG "tcp_send_synack: wrong queue state\n");
2199 return -EFAULT;
2200 }
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002201 if (!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_ACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (skb_cloned(skb)) {
2203 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
2204 if (nskb == NULL)
2205 return -ENOMEM;
David S. Millerfe067e82007-03-07 12:12:44 -08002206 tcp_unlink_write_queue(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 skb_header_release(nskb);
David S. Millerfe067e82007-03-07 12:12:44 -08002208 __tcp_add_write_queue_head(sk, nskb);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002209 sk_wmem_free_skb(sk, skb);
2210 sk->sk_wmem_queued += nskb->truesize;
2211 sk_mem_charge(sk, nskb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 skb = nskb;
2213 }
2214
2215 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;
2216 TCP_ECN_send_synack(tcp_sk(sk), skb);
2217 }
2218 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerdfb4b9d2005-12-06 16:24:52 -08002219 return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Andi Kleen67edfef2009-07-21 23:00:40 +00002222/* Prepare a SYN-ACK. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002223struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2224 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002226 struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct tcp_sock *tp = tcp_sk(sk);
2228 struct tcphdr *th;
2229 int tcp_header_size;
Adam Langley33ad7982008-07-19 00:04:31 -07002230 struct tcp_out_options opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct sk_buff *skb;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002232 struct tcp_md5sig_key *md5;
2233 __u8 *md5_hash_location;
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002234 int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
2237 if (skb == NULL)
2238 return NULL;
2239
2240 /* Reserve space for headers. */
2241 skb_reserve(skb, MAX_TCP_HEADER);
2242
Eric Dumazetadf30902009-06-02 05:19:30 +00002243 skb_dst_set(skb, dst_clone(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002245 mss = dst_metric(dst, RTAX_ADVMSS);
2246 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss)
2247 mss = tp->rx_opt.user_mss;
2248
Adam Langley33ad7982008-07-19 00:04:31 -07002249 if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
2250 __u8 rcv_wscale;
2251 /* Set this up on the first call only */
2252 req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
2253 /* tcp_full_space because it is guaranteed to be the first packet */
2254 tcp_select_initial_window(tcp_full_space(sk),
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002255 mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
Adam Langley33ad7982008-07-19 00:04:31 -07002256 &req->rcv_wnd,
2257 &req->window_clamp,
2258 ireq->wscale_ok,
2259 &rcv_wscale);
2260 ireq->rcv_wscale = rcv_wscale;
2261 }
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002262
Adam Langley33ad7982008-07-19 00:04:31 -07002263 memset(&opts, 0, sizeof(opts));
Florian Westphal8b5f12d2008-10-26 23:10:12 -07002264#ifdef CONFIG_SYN_COOKIES
2265 if (unlikely(req->cookie_ts))
2266 TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
2267 else
2268#endif
Adam Langley33ad7982008-07-19 00:04:31 -07002269 TCP_SKB_CB(skb)->when = tcp_time_stamp;
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002270 tcp_header_size = tcp_synack_options(sk, req, mss,
Adam Langley33ad7982008-07-19 00:04:31 -07002271 skb, &opts, &md5) +
2272 sizeof(struct tcphdr);
2273
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002274 skb_push(skb, tcp_header_size);
2275 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002277 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 memset(th, 0, sizeof(struct tcphdr));
2279 th->syn = 1;
2280 th->ack = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 TCP_ECN_make_synack(req, th);
KOVACS Krisztiana3116ac2008-10-01 07:46:49 -07002282 th->source = ireq->loc_port;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002283 th->dest = ireq->rmt_port;
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002284 /* Setting of flags are superfluous here for callers (and ECE is
2285 * not even correctly set)
2286 */
2287 tcp_init_nondata_skb(skb, tcp_rsk(req)->snt_isn,
2288 TCPCB_FLAG_SYN | TCPCB_FLAG_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 th->seq = htonl(TCP_SKB_CB(skb)->seq);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002290 th->ack_seq = htonl(tcp_rsk(req)->rcv_isn + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
Ilpo Järvinen600ff0c2007-02-13 12:42:11 -08002293 th->window = htons(min(req->rcv_wnd, 65535U));
Adam Langley33ad7982008-07-19 00:04:31 -07002294 tcp_options_write((__be32 *)(th + 1), tp, &opts, &md5_hash_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 th->doff = (tcp_header_size >> 2);
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07002296 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002297
2298#ifdef CONFIG_TCP_MD5SIG
2299 /* Okay, we have all we need - do the md5 hash if needed */
2300 if (md5) {
John Dykstrae3afe7b2009-07-16 05:04:51 +00002301 tcp_rsk(req)->af_specific->calc_md5_hash(md5_hash_location,
Adam Langley49a72df2008-07-19 00:01:42 -07002302 md5, NULL, req, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002303 }
2304#endif
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return skb;
2307}
2308
Andi Kleen67edfef2009-07-21 23:00:40 +00002309/* Do all connect socket setups that can be done AF independent. */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08002310static void tcp_connect_init(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 struct dst_entry *dst = __sk_dst_get(sk);
2313 struct tcp_sock *tp = tcp_sk(sk);
2314 __u8 rcv_wscale;
2315
2316 /* We'll fix this up when we get a response from the other end.
2317 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
2318 */
2319 tp->tcp_header_len = sizeof(struct tcphdr) +
2320 (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
2321
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002322#ifdef CONFIG_TCP_MD5SIG
2323 if (tp->af_specific->md5_lookup(sk, sk) != NULL)
2324 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
2325#endif
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /* If user gave his TCP_MAXSEG, record it to clamp */
2328 if (tp->rx_opt.user_mss)
2329 tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
2330 tp->max_window = 0;
John Heffner5d424d52006-03-20 17:53:41 -08002331 tcp_mtup_init(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 tcp_sync_mss(sk, dst_mtu(dst));
2333
2334 if (!tp->window_clamp)
2335 tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
2336 tp->advmss = dst_metric(dst, RTAX_ADVMSS);
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002337 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->advmss)
2338 tp->advmss = tp->rx_opt.user_mss;
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 tcp_initialize_rcv_mss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 tcp_select_initial_window(tcp_full_space(sk),
2343 tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
2344 &tp->rcv_wnd,
2345 &tp->window_clamp,
2346 sysctl_tcp_window_scaling,
2347 &rcv_wscale);
2348
2349 tp->rx_opt.rcv_wscale = rcv_wscale;
2350 tp->rcv_ssthresh = tp->rcv_wnd;
2351
2352 sk->sk_err = 0;
2353 sock_reset_flag(sk, SOCK_DONE);
2354 tp->snd_wnd = 0;
Hantzis Fotisee7537b2009-03-02 22:42:02 -08002355 tcp_init_wl(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 tp->snd_una = tp->write_seq;
2357 tp->snd_sml = tp->write_seq;
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07002358 tp->snd_up = tp->write_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 tp->rcv_nxt = 0;
2360 tp->rcv_wup = 0;
2361 tp->copied_seq = 0;
2362
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002363 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
2364 inet_csk(sk)->icsk_retransmits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 tcp_clear_retrans(tp);
2366}
2367
Andi Kleen67edfef2009-07-21 23:00:40 +00002368/* Build a SYN and send it off. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369int tcp_connect(struct sock *sk)
2370{
2371 struct tcp_sock *tp = tcp_sk(sk);
2372 struct sk_buff *buff;
2373
2374 tcp_connect_init(sk);
2375
David S. Millerd179cd12005-08-17 14:57:30 -07002376 buff = alloc_skb_fclone(MAX_TCP_HEADER + 15, sk->sk_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (unlikely(buff == NULL))
2378 return -ENOBUFS;
2379
2380 /* Reserve space for headers. */
2381 skb_reserve(buff, MAX_TCP_HEADER);
2382
Wei Yongjunbd37a082006-08-07 21:04:15 -07002383 tp->snd_nxt = tp->write_seq;
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002384 tcp_init_nondata_skb(buff, tp->write_seq++, TCPCB_FLAG_SYN);
2385 TCP_ECN_send_syn(sk, buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /* Send it off. */
2388 TCP_SKB_CB(buff)->when = tcp_time_stamp;
2389 tp->retrans_stamp = TCP_SKB_CB(buff)->when;
2390 skb_header_release(buff);
David S. Millerfe067e82007-03-07 12:12:44 -08002391 __tcp_add_write_queue_tail(sk, buff);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002392 sk->sk_wmem_queued += buff->truesize;
2393 sk_mem_charge(sk, buff->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 tp->packets_out += tcp_skb_pcount(buff);
Wu Fengguangaa133072009-09-02 23:45:45 -07002395 tcp_transmit_skb(sk, buff, 1, sk->sk_allocation);
Wei Yongjunbd37a082006-08-07 21:04:15 -07002396
2397 /* We change tp->snd_nxt after the tcp_transmit_skb() call
2398 * in order to make this packet get counted in tcpOutSegs.
2399 */
2400 tp->snd_nxt = tp->write_seq;
2401 tp->pushed_seq = tp->write_seq;
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07002402 TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /* Timer for repeating the SYN until an answer. */
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07002405 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
2406 inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return 0;
2408}
2409
2410/* Send out a delayed ack, the caller does the policy checking
2411 * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check()
2412 * for details.
2413 */
2414void tcp_send_delayed_ack(struct sock *sk)
2415{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002416 struct inet_connection_sock *icsk = inet_csk(sk);
2417 int ato = icsk->icsk_ack.ato;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 unsigned long timeout;
2419
2420 if (ato > TCP_DELACK_MIN) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002421 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002422 int max_ato = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002424 if (icsk->icsk_ack.pingpong ||
2425 (icsk->icsk_ack.pending & ICSK_ACK_PUSHED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 max_ato = TCP_DELACK_MAX;
2427
2428 /* Slow path, intersegment interval is "high". */
2429
2430 /* If some rtt estimate is known, use it to bound delayed ack.
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002431 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 * directly.
2433 */
2434 if (tp->srtt) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002435 int rtt = max(tp->srtt >> 3, TCP_DELACK_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (rtt < max_ato)
2438 max_ato = rtt;
2439 }
2440
2441 ato = min(ato, max_ato);
2442 }
2443
2444 /* Stay within the limit we were given */
2445 timeout = jiffies + ato;
2446
2447 /* Use new timeout only if there wasn't a older one earlier. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002448 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /* If delack timer was blocked or is about to expire,
2450 * send ACK now.
2451 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002452 if (icsk->icsk_ack.blocked ||
2453 time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 tcp_send_ack(sk);
2455 return;
2456 }
2457
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002458 if (!time_before(timeout, icsk->icsk_ack.timeout))
2459 timeout = icsk->icsk_ack.timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002461 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
2462 icsk->icsk_ack.timeout = timeout;
2463 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
2466/* This routine sends an ack and also updates the window. */
2467void tcp_send_ack(struct sock *sk)
2468{
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002469 struct sk_buff *buff;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* If we have been reset, we may not send again. */
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002472 if (sk->sk_state == TCP_CLOSE)
2473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002475 /* We are not putting this on the write queue, so
2476 * tcp_transmit_skb() will set the ownership to this
2477 * sock.
2478 */
2479 buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
2480 if (buff == NULL) {
2481 inet_csk_schedule_ack(sk);
2482 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
2483 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
2484 TCP_DELACK_MAX, TCP_RTO_MAX);
2485 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002487
2488 /* Reserve space for headers and prepare control bits. */
2489 skb_reserve(buff, MAX_TCP_HEADER);
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002490 tcp_init_nondata_skb(buff, tcp_acceptable_seq(sk), TCPCB_FLAG_ACK);
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002491
2492 /* Send it off, this clears delayed acks for us. */
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002493 TCP_SKB_CB(buff)->when = tcp_time_stamp;
2494 tcp_transmit_skb(sk, buff, 0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497/* This routine sends a packet with an out of date sequence
2498 * number. It assumes the other end will try to ack it.
2499 *
2500 * Question: what should we make while urgent mode?
2501 * 4.4BSD forces sending single byte of data. We cannot send
2502 * out of window data, because we have SND.NXT==SND.MAX...
2503 *
2504 * Current solution: to send TWO zero-length segments in urgent mode:
2505 * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
2506 * out-of-date with SND.UNA-1 to probe window.
2507 */
2508static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
2509{
2510 struct tcp_sock *tp = tcp_sk(sk);
2511 struct sk_buff *skb;
2512
2513 /* We don't queue it, tcp_transmit_skb() sets ownership. */
2514 skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002515 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return -1;
2517
2518 /* Reserve space for headers and set control bits. */
2519 skb_reserve(skb, MAX_TCP_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /* Use a previous sequence. This should cause the other
2521 * end to send an ack. Don't queue or clone SKB, just
2522 * send it.
2523 */
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002524 tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPCB_FLAG_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerdfb4b9d2005-12-06 16:24:52 -08002526 return tcp_transmit_skb(sk, skb, 0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Andi Kleen67edfef2009-07-21 23:00:40 +00002529/* Initiate keepalive or window probe from timer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530int tcp_write_wakeup(struct sock *sk)
2531{
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002532 struct tcp_sock *tp = tcp_sk(sk);
2533 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002535 if (sk->sk_state == TCP_CLOSE)
2536 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002538 if ((skb = tcp_send_head(sk)) != NULL &&
2539 before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) {
2540 int err;
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002541 unsigned int mss = tcp_current_mss(sk);
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002542 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002544 if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
2545 tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002547 /* We are probing the opening of a window
2548 * but the window size is != 0
2549 * must have been a result SWS avoidance ( sender )
2550 */
2551 if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
2552 skb->len > mss) {
2553 seg_size = min(seg_size, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002555 if (tcp_fragment(sk, skb, seg_size, mss))
2556 return -1;
2557 } else if (!tcp_skb_pcount(skb))
2558 tcp_set_skb_tso_segs(sk, skb, mss);
2559
2560 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
2561 TCP_SKB_CB(skb)->when = tcp_time_stamp;
2562 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
2563 if (!err)
2564 tcp_event_new_data_sent(sk, skb);
2565 return err;
2566 } else {
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07002567 if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF))
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002568 tcp_xmit_probe_skb(sk, 1);
2569 return tcp_xmit_probe_skb(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573/* A window probe timeout has occurred. If window is not closed send
2574 * a partial packet else a zero probe.
2575 */
2576void tcp_send_probe0(struct sock *sk)
2577{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002578 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct tcp_sock *tp = tcp_sk(sk);
2580 int err;
2581
2582 err = tcp_write_wakeup(sk);
2583
David S. Millerfe067e82007-03-07 12:12:44 -08002584 if (tp->packets_out || !tcp_send_head(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /* Cancel probe timer, if it is not required. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002586 icsk->icsk_probes_out = 0;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002587 icsk->icsk_backoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return;
2589 }
2590
2591 if (err <= 0) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002592 if (icsk->icsk_backoff < sysctl_tcp_retries2)
2593 icsk->icsk_backoff++;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002594 icsk->icsk_probes_out++;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002595 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07002596 min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
2597 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 } else {
2599 /* If packet was not sent due to local congestion,
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002600 * do not backoff and do not remember icsk_probes_out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * Let local senders to fight for local resources.
2602 *
2603 * Use accumulated backoff yet.
2604 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002605 if (!icsk->icsk_probes_out)
2606 icsk->icsk_probes_out = 1;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002607 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002608 min(icsk->icsk_rto << icsk->icsk_backoff,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07002609 TCP_RESOURCE_PROBE_INTERVAL),
2610 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612}
2613
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08002614EXPORT_SYMBOL(tcp_select_initial_window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615EXPORT_SYMBOL(tcp_connect);
2616EXPORT_SYMBOL(tcp_make_synack);
2617EXPORT_SYMBOL(tcp_simple_retransmit);
2618EXPORT_SYMBOL(tcp_sync_mss);
John Heffner5d424d52006-03-20 17:53:41 -08002619EXPORT_SYMBOL(tcp_mtup_init);