tcp_output.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Implementation of the Transmission Control Protocol(TCP).
  7. *
  8. * Version: $Id: tcp_output.c,v 1.146 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  13. * Corey Minyard <wf-rch!minyard@relay.EU.net>
  14. * Florian La Roche, <flla@stud.uni-sb.de>
  15. * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  16. * Linus Torvalds, <torvalds@cs.helsinki.fi>
  17. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  18. * Matthew Dillon, <dillon@apollo.west.oic.com>
  19. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  20. * Jorge Cwik, <jorge@laser.satlink.net>
  21. */
  22. /*
  23. * Changes: Pedro Roque : Retransmit queue handled by TCP.
  24. * : Fragmentation on mtu decrease
  25. * : Segment collapse on retransmit
  26. * : AF independence
  27. *
  28. * Linus Torvalds : send_delayed_ack
  29. * David S. Miller : Charge memory using the right skb
  30. * during syn/ack processing.
  31. * David S. Miller : Output engine completely rewritten.
  32. * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
  33. * Cacophonix Gaul : draft-minshall-nagle-01
  34. * J Hadi Salim : ECN support
  35. *
  36. */
  37. #include <net/tcp.h>
  38. #include <linux/compiler.h>
  39. #include <linux/module.h>
  40. #include <linux/smp_lock.h>
  41. /* People can turn this off for buggy TCP's found in printers etc. */
  42. int sysctl_tcp_retrans_collapse = 1;
  43. /* This limits the percentage of the congestion window which we
  44. * will allow a single TSO frame to consume. Building TSO frames
  45. * which are too large can cause TCP streams to be bursty.
  46. */
  47. int sysctl_tcp_tso_win_divisor = 3;
  48. static inline void update_send_head(struct sock *sk, struct tcp_sock *tp,
  49. struct sk_buff *skb)
  50. {
  51. sk->sk_send_head = skb->next;
  52. if (sk->sk_send_head == (struct sk_buff *)&sk->sk_write_queue)
  53. sk->sk_send_head = NULL;
  54. tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
  55. tcp_packets_out_inc(sk, tp, skb);
  56. }
  57. /* SND.NXT, if window was not shrunk.
  58. * If window has been shrunk, what should we make? It is not clear at all.
  59. * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
  60. * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
  61. * invalid. OK, let's make this for now:
  62. */
  63. static inline __u32 tcp_acceptable_seq(struct sock *sk, struct tcp_sock *tp)
  64. {
  65. if (!before(tp->snd_una+tp->snd_wnd, tp->snd_nxt))
  66. return tp->snd_nxt;
  67. else
  68. return tp->snd_una+tp->snd_wnd;
  69. }
  70. /* Calculate mss to advertise in SYN segment.
  71. * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
  72. *
  73. * 1. It is independent of path mtu.
  74. * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
  75. * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
  76. * attached devices, because some buggy hosts are confused by
  77. * large MSS.
  78. * 4. We do not make 3, we advertise MSS, calculated from first
  79. * hop device mtu, but allow to raise it to ip_rt_min_advmss.
  80. * This may be overridden via information stored in routing table.
  81. * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
  82. * probably even Jumbo".
  83. */
  84. static __u16 tcp_advertise_mss(struct sock *sk)
  85. {
  86. struct tcp_sock *tp = tcp_sk(sk);
  87. struct dst_entry *dst = __sk_dst_get(sk);
  88. int mss = tp->advmss;
  89. if (dst && dst_metric(dst, RTAX_ADVMSS) < mss) {
  90. mss = dst_metric(dst, RTAX_ADVMSS);
  91. tp->advmss = mss;
  92. }
  93. return (__u16)mss;
  94. }
  95. /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
  96. * This is the first part of cwnd validation mechanism. */
  97. static void tcp_cwnd_restart(struct tcp_sock *tp, struct dst_entry *dst)
  98. {
  99. s32 delta = tcp_time_stamp - tp->lsndtime;
  100. u32 restart_cwnd = tcp_init_cwnd(tp, dst);
  101. u32 cwnd = tp->snd_cwnd;
  102. tcp_ca_event(tp, CA_EVENT_CWND_RESTART);
  103. tp->snd_ssthresh = tcp_current_ssthresh(tp);
  104. restart_cwnd = min(restart_cwnd, cwnd);
  105. while ((delta -= tp->rto) > 0 && cwnd > restart_cwnd)
  106. cwnd >>= 1;
  107. tp->snd_cwnd = max(cwnd, restart_cwnd);
  108. tp->snd_cwnd_stamp = tcp_time_stamp;
  109. tp->snd_cwnd_used = 0;
  110. }
  111. static inline void tcp_event_data_sent(struct tcp_sock *tp,
  112. struct sk_buff *skb, struct sock *sk)
  113. {
  114. u32 now = tcp_time_stamp;
  115. if (!tp->packets_out && (s32)(now - tp->lsndtime) > tp->rto)
  116. tcp_cwnd_restart(tp, __sk_dst_get(sk));
  117. tp->lsndtime = now;
  118. /* If it is a reply for ato after last received
  119. * packet, enter pingpong mode.
  120. */
  121. if ((u32)(now - tp->ack.lrcvtime) < tp->ack.ato)
  122. tp->ack.pingpong = 1;
  123. }
  124. static __inline__ void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
  125. {
  126. struct tcp_sock *tp = tcp_sk(sk);
  127. tcp_dec_quickack_mode(tp, pkts);
  128. tcp_clear_xmit_timer(sk, TCP_TIME_DACK);
  129. }
  130. /* Determine a window scaling and initial window to offer.
  131. * Based on the assumption that the given amount of space
  132. * will be offered. Store the results in the tp structure.
  133. * NOTE: for smooth operation initial space offering should
  134. * be a multiple of mss if possible. We assume here that mss >= 1.
  135. * This MUST be enforced by all callers.
  136. */
  137. void tcp_select_initial_window(int __space, __u32 mss,
  138. __u32 *rcv_wnd, __u32 *window_clamp,
  139. int wscale_ok, __u8 *rcv_wscale)
  140. {
  141. unsigned int space = (__space < 0 ? 0 : __space);
  142. /* If no clamp set the clamp to the max possible scaled window */
  143. if (*window_clamp == 0)
  144. (*window_clamp) = (65535 << 14);
  145. space = min(*window_clamp, space);
  146. /* Quantize space offering to a multiple of mss if possible. */
  147. if (space > mss)
  148. space = (space / mss) * mss;
  149. /* NOTE: offering an initial window larger than 32767
  150. * will break some buggy TCP stacks. We try to be nice.
  151. * If we are not window scaling, then this truncates
  152. * our initial window offering to 32k. There should also
  153. * be a sysctl option to stop being nice.
  154. */
  155. (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
  156. (*rcv_wscale) = 0;
  157. if (wscale_ok) {
  158. /* Set window scaling on max possible window
  159. * See RFC1323 for an explanation of the limit to 14
  160. */
  161. space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
  162. while (space > 65535 && (*rcv_wscale) < 14) {
  163. space >>= 1;
  164. (*rcv_wscale)++;
  165. }
  166. }
  167. /* Set initial window to value enough for senders,
  168. * following RFC1414. Senders, not following this RFC,
  169. * will be satisfied with 2.
  170. */
  171. if (mss > (1<<*rcv_wscale)) {
  172. int init_cwnd = 4;
  173. if (mss > 1460*3)
  174. init_cwnd = 2;
  175. else if (mss > 1460)
  176. init_cwnd = 3;
  177. if (*rcv_wnd > init_cwnd*mss)
  178. *rcv_wnd = init_cwnd*mss;
  179. }
  180. /* Set the clamp no higher than max representable value */
  181. (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
  182. }
  183. /* Chose a new window to advertise, update state in tcp_sock for the
  184. * socket, and return result with RFC1323 scaling applied. The return
  185. * value can be stuffed directly into th->window for an outgoing
  186. * frame.
  187. */
  188. static __inline__ u16 tcp_select_window(struct sock *sk)
  189. {
  190. struct tcp_sock *tp = tcp_sk(sk);
  191. u32 cur_win = tcp_receive_window(tp);
  192. u32 new_win = __tcp_select_window(sk);
  193. /* Never shrink the offered window */
  194. if(new_win < cur_win) {
  195. /* Danger Will Robinson!
  196. * Don't update rcv_wup/rcv_wnd here or else
  197. * we will not be able to advertise a zero
  198. * window in time. --DaveM
  199. *
  200. * Relax Will Robinson.
  201. */
  202. new_win = cur_win;
  203. }
  204. tp->rcv_wnd = new_win;
  205. tp->rcv_wup = tp->rcv_nxt;
  206. /* Make sure we do not exceed the maximum possible
  207. * scaled window.
  208. */
  209. if (!tp->rx_opt.rcv_wscale)
  210. new_win = min(new_win, MAX_TCP_WINDOW);
  211. else
  212. new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
  213. /* RFC1323 scaling applied */
  214. new_win >>= tp->rx_opt.rcv_wscale;
  215. /* If we advertise zero window, disable fast path. */
  216. if (new_win == 0)
  217. tp->pred_flags = 0;
  218. return new_win;
  219. }
  220. /* This routine actually transmits TCP packets queued in by
  221. * tcp_do_sendmsg(). This is used by both the initial
  222. * transmission and possible later retransmissions.
  223. * All SKB's seen here are completely headerless. It is our
  224. * job to build the TCP header, and pass the packet down to
  225. * IP so it can do the same plus pass the packet off to the
  226. * device.
  227. *
  228. * We are working here with either a clone of the original
  229. * SKB, or a fresh unique copy made by the retransmit engine.
  230. */
  231. static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb)
  232. {
  233. if (skb != NULL) {
  234. struct inet_sock *inet = inet_sk(sk);
  235. struct tcp_sock *tp = tcp_sk(sk);
  236. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  237. int tcp_header_size = tp->tcp_header_len;
  238. struct tcphdr *th;
  239. int sysctl_flags;
  240. int err;
  241. BUG_ON(!tcp_skb_pcount(skb));
  242. #define SYSCTL_FLAG_TSTAMPS 0x1
  243. #define SYSCTL_FLAG_WSCALE 0x2
  244. #define SYSCTL_FLAG_SACK 0x4
  245. /* If congestion control is doing timestamping */
  246. if (tp->ca_ops->rtt_sample)
  247. do_gettimeofday(&skb->stamp);
  248. sysctl_flags = 0;
  249. if (tcb->flags & TCPCB_FLAG_SYN) {
  250. tcp_header_size = sizeof(struct tcphdr) + TCPOLEN_MSS;
  251. if(sysctl_tcp_timestamps) {
  252. tcp_header_size += TCPOLEN_TSTAMP_ALIGNED;
  253. sysctl_flags |= SYSCTL_FLAG_TSTAMPS;
  254. }
  255. if(sysctl_tcp_window_scaling) {
  256. tcp_header_size += TCPOLEN_WSCALE_ALIGNED;
  257. sysctl_flags |= SYSCTL_FLAG_WSCALE;
  258. }
  259. if(sysctl_tcp_sack) {
  260. sysctl_flags |= SYSCTL_FLAG_SACK;
  261. if(!(sysctl_flags & SYSCTL_FLAG_TSTAMPS))
  262. tcp_header_size += TCPOLEN_SACKPERM_ALIGNED;
  263. }
  264. } else if (tp->rx_opt.eff_sacks) {
  265. /* A SACK is 2 pad bytes, a 2 byte header, plus
  266. * 2 32-bit sequence numbers for each SACK block.
  267. */
  268. tcp_header_size += (TCPOLEN_SACK_BASE_ALIGNED +
  269. (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK));
  270. }
  271. if (tcp_packets_in_flight(tp) == 0)
  272. tcp_ca_event(tp, CA_EVENT_TX_START);
  273. th = (struct tcphdr *) skb_push(skb, tcp_header_size);
  274. skb->h.th = th;
  275. skb_set_owner_w(skb, sk);
  276. /* Build TCP header and checksum it. */
  277. th->source = inet->sport;
  278. th->dest = inet->dport;
  279. th->seq = htonl(tcb->seq);
  280. th->ack_seq = htonl(tp->rcv_nxt);
  281. *(((__u16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) | tcb->flags);
  282. if (tcb->flags & TCPCB_FLAG_SYN) {
  283. /* RFC1323: The window in SYN & SYN/ACK segments
  284. * is never scaled.
  285. */
  286. th->window = htons(tp->rcv_wnd);
  287. } else {
  288. th->window = htons(tcp_select_window(sk));
  289. }
  290. th->check = 0;
  291. th->urg_ptr = 0;
  292. if (tp->urg_mode &&
  293. between(tp->snd_up, tcb->seq+1, tcb->seq+0xFFFF)) {
  294. th->urg_ptr = htons(tp->snd_up-tcb->seq);
  295. th->urg = 1;
  296. }
  297. if (tcb->flags & TCPCB_FLAG_SYN) {
  298. tcp_syn_build_options((__u32 *)(th + 1),
  299. tcp_advertise_mss(sk),
  300. (sysctl_flags & SYSCTL_FLAG_TSTAMPS),
  301. (sysctl_flags & SYSCTL_FLAG_SACK),
  302. (sysctl_flags & SYSCTL_FLAG_WSCALE),
  303. tp->rx_opt.rcv_wscale,
  304. tcb->when,
  305. tp->rx_opt.ts_recent);
  306. } else {
  307. tcp_build_and_update_options((__u32 *)(th + 1),
  308. tp, tcb->when);
  309. TCP_ECN_send(sk, tp, skb, tcp_header_size);
  310. }
  311. tp->af_specific->send_check(sk, th, skb->len, skb);
  312. if (tcb->flags & TCPCB_FLAG_ACK)
  313. tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
  314. if (skb->len != tcp_header_size)
  315. tcp_event_data_sent(tp, skb, sk);
  316. TCP_INC_STATS(TCP_MIB_OUTSEGS);
  317. err = tp->af_specific->queue_xmit(skb, 0);
  318. if (err <= 0)
  319. return err;
  320. tcp_enter_cwr(tp);
  321. /* NET_XMIT_CN is special. It does not guarantee,
  322. * that this packet is lost. It tells that device
  323. * is about to start to drop packets or already
  324. * drops some packets of the same priority and
  325. * invokes us to send less aggressively.
  326. */
  327. return err == NET_XMIT_CN ? 0 : err;
  328. }
  329. return -ENOBUFS;
  330. #undef SYSCTL_FLAG_TSTAMPS
  331. #undef SYSCTL_FLAG_WSCALE
  332. #undef SYSCTL_FLAG_SACK
  333. }
  334. /* This routine just queue's the buffer
  335. *
  336. * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
  337. * otherwise socket can stall.
  338. */
  339. static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
  340. {
  341. struct tcp_sock *tp = tcp_sk(sk);
  342. /* Advance write_seq and place onto the write_queue. */
  343. tp->write_seq = TCP_SKB_CB(skb)->end_seq;
  344. skb_header_release(skb);
  345. __skb_queue_tail(&sk->sk_write_queue, skb);
  346. sk_charge_skb(sk, skb);
  347. /* Queue it, remembering where we must start sending. */
  348. if (sk->sk_send_head == NULL)
  349. sk->sk_send_head = skb;
  350. }
  351. static void tcp_set_skb_tso_segs(struct sock *sk, struct sk_buff *skb, unsigned int mss_now)
  352. {
  353. if (skb->len <= mss_now ||
  354. !(sk->sk_route_caps & NETIF_F_TSO)) {
  355. /* Avoid the costly divide in the normal
  356. * non-TSO case.
  357. */
  358. skb_shinfo(skb)->tso_segs = 1;
  359. skb_shinfo(skb)->tso_size = 0;
  360. } else {
  361. unsigned int factor;
  362. factor = skb->len + (mss_now - 1);
  363. factor /= mss_now;
  364. skb_shinfo(skb)->tso_segs = factor;
  365. skb_shinfo(skb)->tso_size = mss_now;
  366. }
  367. }
  368. /* Function to create two new TCP segments. Shrinks the given segment
  369. * to the specified size and appends a new segment with the rest of the
  370. * packet to the list. This won't be called frequently, I hope.
  371. * Remember, these are still headerless SKBs at this point.
  372. */
  373. static int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len, unsigned int mss_now)
  374. {
  375. struct tcp_sock *tp = tcp_sk(sk);
  376. struct sk_buff *buff;
  377. int nsize;
  378. u16 flags;
  379. nsize = skb_headlen(skb) - len;
  380. if (nsize < 0)
  381. nsize = 0;
  382. if (skb_cloned(skb) &&
  383. skb_is_nonlinear(skb) &&
  384. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  385. return -ENOMEM;
  386. /* Get a new skb... force flag on. */
  387. buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
  388. if (buff == NULL)
  389. return -ENOMEM; /* We'll just try again later. */
  390. sk_charge_skb(sk, buff);
  391. /* Correct the sequence numbers. */
  392. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  393. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  394. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  395. /* PSH and FIN should only be set in the second packet. */
  396. flags = TCP_SKB_CB(skb)->flags;
  397. TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
  398. TCP_SKB_CB(buff)->flags = flags;
  399. TCP_SKB_CB(buff)->sacked =
  400. (TCP_SKB_CB(skb)->sacked &
  401. (TCPCB_LOST | TCPCB_EVER_RETRANS | TCPCB_AT_TAIL));
  402. TCP_SKB_CB(skb)->sacked &= ~TCPCB_AT_TAIL;
  403. if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_HW) {
  404. /* Copy and checksum data tail into the new buffer. */
  405. buff->csum = csum_partial_copy_nocheck(skb->data + len, skb_put(buff, nsize),
  406. nsize, 0);
  407. skb_trim(skb, len);
  408. skb->csum = csum_block_sub(skb->csum, buff->csum, len);
  409. } else {
  410. skb->ip_summed = CHECKSUM_HW;
  411. skb_split(skb, buff, len);
  412. }
  413. buff->ip_summed = skb->ip_summed;
  414. /* Looks stupid, but our code really uses when of
  415. * skbs, which it never sent before. --ANK
  416. */
  417. TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
  418. buff->stamp = skb->stamp;
  419. if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) {
  420. tp->lost_out -= tcp_skb_pcount(skb);
  421. tp->left_out -= tcp_skb_pcount(skb);
  422. }
  423. /* Fix up tso_factor for both original and new SKB. */
  424. tcp_set_skb_tso_segs(sk, skb, mss_now);
  425. tcp_set_skb_tso_segs(sk, buff, mss_now);
  426. if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) {
  427. tp->lost_out += tcp_skb_pcount(skb);
  428. tp->left_out += tcp_skb_pcount(skb);
  429. }
  430. if (TCP_SKB_CB(buff)->sacked&TCPCB_LOST) {
  431. tp->lost_out += tcp_skb_pcount(buff);
  432. tp->left_out += tcp_skb_pcount(buff);
  433. }
  434. /* Link BUFF into the send queue. */
  435. skb_header_release(buff);
  436. __skb_append(skb, buff);
  437. return 0;
  438. }
  439. /* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
  440. * eventually). The difference is that pulled data not copied, but
  441. * immediately discarded.
  442. */
  443. static unsigned char *__pskb_trim_head(struct sk_buff *skb, int len)
  444. {
  445. int i, k, eat;
  446. eat = len;
  447. k = 0;
  448. for (i=0; i<skb_shinfo(skb)->nr_frags; i++) {
  449. if (skb_shinfo(skb)->frags[i].size <= eat) {
  450. put_page(skb_shinfo(skb)->frags[i].page);
  451. eat -= skb_shinfo(skb)->frags[i].size;
  452. } else {
  453. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  454. if (eat) {
  455. skb_shinfo(skb)->frags[k].page_offset += eat;
  456. skb_shinfo(skb)->frags[k].size -= eat;
  457. eat = 0;
  458. }
  459. k++;
  460. }
  461. }
  462. skb_shinfo(skb)->nr_frags = k;
  463. skb->tail = skb->data;
  464. skb->data_len -= len;
  465. skb->len = skb->data_len;
  466. return skb->tail;
  467. }
  468. int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
  469. {
  470. if (skb_cloned(skb) &&
  471. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  472. return -ENOMEM;
  473. if (len <= skb_headlen(skb)) {
  474. __skb_pull(skb, len);
  475. } else {
  476. if (__pskb_trim_head(skb, len-skb_headlen(skb)) == NULL)
  477. return -ENOMEM;
  478. }
  479. TCP_SKB_CB(skb)->seq += len;
  480. skb->ip_summed = CHECKSUM_HW;
  481. skb->truesize -= len;
  482. sk->sk_wmem_queued -= len;
  483. sk->sk_forward_alloc += len;
  484. sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
  485. /* Any change of skb->len requires recalculation of tso
  486. * factor and mss.
  487. */
  488. if (tcp_skb_pcount(skb) > 1)
  489. tcp_set_skb_tso_segs(sk, skb, tcp_current_mss(sk, 1));
  490. return 0;
  491. }
  492. /* This function synchronize snd mss to current pmtu/exthdr set.
  493. tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
  494. for TCP options, but includes only bare TCP header.
  495. tp->rx_opt.mss_clamp is mss negotiated at connection setup.
  496. It is minumum of user_mss and mss received with SYN.
  497. It also does not include TCP options.
  498. tp->pmtu_cookie is last pmtu, seen by this function.
  499. tp->mss_cache is current effective sending mss, including
  500. all tcp options except for SACKs. It is evaluated,
  501. taking into account current pmtu, but never exceeds
  502. tp->rx_opt.mss_clamp.
  503. NOTE1. rfc1122 clearly states that advertised MSS
  504. DOES NOT include either tcp or ip options.
  505. NOTE2. tp->pmtu_cookie and tp->mss_cache are READ ONLY outside
  506. this function. --ANK (980731)
  507. */
  508. unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
  509. {
  510. struct tcp_sock *tp = tcp_sk(sk);
  511. int mss_now;
  512. /* Calculate base mss without TCP options:
  513. It is MMS_S - sizeof(tcphdr) of rfc1122
  514. */
  515. mss_now = pmtu - tp->af_specific->net_header_len - sizeof(struct tcphdr);
  516. /* Clamp it (mss_clamp does not include tcp options) */
  517. if (mss_now > tp->rx_opt.mss_clamp)
  518. mss_now = tp->rx_opt.mss_clamp;
  519. /* Now subtract optional transport overhead */
  520. mss_now -= tp->ext_header_len;
  521. /* Then reserve room for full set of TCP options and 8 bytes of data */
  522. if (mss_now < 48)
  523. mss_now = 48;
  524. /* Now subtract TCP options size, not including SACKs */
  525. mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
  526. /* Bound mss with half of window */
  527. if (tp->max_window && mss_now > (tp->max_window>>1))
  528. mss_now = max((tp->max_window>>1), 68U - tp->tcp_header_len);
  529. /* And store cached results */
  530. tp->pmtu_cookie = pmtu;
  531. tp->mss_cache = mss_now;
  532. return mss_now;
  533. }
  534. /* Compute the current effective MSS, taking SACKs and IP options,
  535. * and even PMTU discovery events into account.
  536. *
  537. * LARGESEND note: !urg_mode is overkill, only frames up to snd_up
  538. * cannot be large. However, taking into account rare use of URG, this
  539. * is not a big flaw.
  540. */
  541. unsigned int tcp_current_mss(struct sock *sk, int large_allowed)
  542. {
  543. struct tcp_sock *tp = tcp_sk(sk);
  544. struct dst_entry *dst = __sk_dst_get(sk);
  545. u32 mss_now;
  546. u16 xmit_size_goal;
  547. int doing_tso = 0;
  548. mss_now = tp->mss_cache;
  549. if (large_allowed &&
  550. (sk->sk_route_caps & NETIF_F_TSO) &&
  551. !tp->urg_mode)
  552. doing_tso = 1;
  553. if (dst) {
  554. u32 mtu = dst_mtu(dst);
  555. if (mtu != tp->pmtu_cookie)
  556. mss_now = tcp_sync_mss(sk, mtu);
  557. }
  558. if (tp->rx_opt.eff_sacks)
  559. mss_now -= (TCPOLEN_SACK_BASE_ALIGNED +
  560. (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK));
  561. xmit_size_goal = mss_now;
  562. if (doing_tso) {
  563. xmit_size_goal = 65535 -
  564. tp->af_specific->net_header_len -
  565. tp->ext_header_len - tp->tcp_header_len;
  566. if (tp->max_window &&
  567. (xmit_size_goal > (tp->max_window >> 1)))
  568. xmit_size_goal = max((tp->max_window >> 1),
  569. 68U - tp->tcp_header_len);
  570. xmit_size_goal -= (xmit_size_goal % mss_now);
  571. }
  572. tp->xmit_size_goal = xmit_size_goal;
  573. return mss_now;
  574. }
  575. /* Congestion window validation. (RFC2861) */
  576. static inline void tcp_cwnd_validate(struct sock *sk, struct tcp_sock *tp)
  577. {
  578. __u32 packets_out = tp->packets_out;
  579. if (packets_out >= tp->snd_cwnd) {
  580. /* Network is feed fully. */
  581. tp->snd_cwnd_used = 0;
  582. tp->snd_cwnd_stamp = tcp_time_stamp;
  583. } else {
  584. /* Network starves. */
  585. if (tp->packets_out > tp->snd_cwnd_used)
  586. tp->snd_cwnd_used = tp->packets_out;
  587. if ((s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= tp->rto)
  588. tcp_cwnd_application_limited(sk);
  589. }
  590. }
  591. static unsigned int tcp_window_allows(struct tcp_sock *tp, struct sk_buff *skb, unsigned int mss_now, unsigned int cwnd)
  592. {
  593. u32 window, cwnd_len;
  594. window = (tp->snd_una + tp->snd_wnd - TCP_SKB_CB(skb)->seq);
  595. cwnd_len = mss_now * cwnd;
  596. return min(window, cwnd_len);
  597. }
  598. /* Can at least one segment of SKB be sent right now, according to the
  599. * congestion window rules? If so, return how many segments are allowed.
  600. */
  601. static inline unsigned int tcp_cwnd_test(struct tcp_sock *tp, struct sk_buff *skb)
  602. {
  603. u32 in_flight, cwnd;
  604. /* Don't be strict about the congestion window for the final FIN. */
  605. if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)
  606. return 1;
  607. in_flight = tcp_packets_in_flight(tp);
  608. cwnd = tp->snd_cwnd;
  609. if (in_flight < cwnd)
  610. return (cwnd - in_flight);
  611. return 0;
  612. }
  613. /* This must be invoked the first time we consider transmitting
  614. * SKB onto the wire.
  615. */
  616. static inline int tcp_init_tso_segs(struct sock *sk, struct sk_buff *skb, unsigned int mss_now)
  617. {
  618. int tso_segs = tcp_skb_pcount(skb);
  619. if (!tso_segs ||
  620. (tso_segs > 1 &&
  621. skb_shinfo(skb)->tso_size != mss_now)) {
  622. tcp_set_skb_tso_segs(sk, skb, mss_now);
  623. tso_segs = tcp_skb_pcount(skb);
  624. }
  625. return tso_segs;
  626. }
  627. static inline int tcp_minshall_check(const struct tcp_sock *tp)
  628. {
  629. return after(tp->snd_sml,tp->snd_una) &&
  630. !after(tp->snd_sml, tp->snd_nxt);
  631. }
  632. /* Return 0, if packet can be sent now without violation Nagle's rules:
  633. * 1. It is full sized.
  634. * 2. Or it contains FIN. (already checked by caller)
  635. * 3. Or TCP_NODELAY was set.
  636. * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
  637. * With Minshall's modification: all sent small packets are ACKed.
  638. */
  639. static inline int tcp_nagle_check(const struct tcp_sock *tp,
  640. const struct sk_buff *skb,
  641. unsigned mss_now, int nonagle)
  642. {
  643. return (skb->len < mss_now &&
  644. ((nonagle&TCP_NAGLE_CORK) ||
  645. (!nonagle &&
  646. tp->packets_out &&
  647. tcp_minshall_check(tp))));
  648. }
  649. /* Return non-zero if the Nagle test allows this packet to be
  650. * sent now.
  651. */
  652. static inline int tcp_nagle_test(struct tcp_sock *tp, struct sk_buff *skb,
  653. unsigned int cur_mss, int nonagle)
  654. {
  655. /* Nagle rule does not apply to frames, which sit in the middle of the
  656. * write_queue (they have no chances to get new data).
  657. *
  658. * This is implemented in the callers, where they modify the 'nonagle'
  659. * argument based upon the location of SKB in the send queue.
  660. */
  661. if (nonagle & TCP_NAGLE_PUSH)
  662. return 1;
  663. /* Don't use the nagle rule for urgent data (or for the final FIN). */
  664. if (tp->urg_mode ||
  665. (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN))
  666. return 1;
  667. if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
  668. return 1;
  669. return 0;
  670. }
  671. /* Does at least the first segment of SKB fit into the send window? */
  672. static inline int tcp_snd_wnd_test(struct tcp_sock *tp, struct sk_buff *skb, unsigned int cur_mss)
  673. {
  674. u32 end_seq = TCP_SKB_CB(skb)->end_seq;
  675. if (skb->len > cur_mss)
  676. end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
  677. return !after(end_seq, tp->snd_una + tp->snd_wnd);
  678. }
  679. /* This checks if the data bearing packet SKB (usually sk->sk_send_head)
  680. * should be put on the wire right now. If so, it returns the number of
  681. * packets allowed by the congestion window.
  682. */
  683. static unsigned int tcp_snd_test(struct sock *sk, struct sk_buff *skb,
  684. unsigned int cur_mss, int nonagle)
  685. {
  686. struct tcp_sock *tp = tcp_sk(sk);
  687. unsigned int cwnd_quota;
  688. tcp_init_tso_segs(sk, skb, cur_mss);
  689. if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
  690. return 0;
  691. cwnd_quota = tcp_cwnd_test(tp, skb);
  692. if (cwnd_quota &&
  693. !tcp_snd_wnd_test(tp, skb, cur_mss))
  694. cwnd_quota = 0;
  695. return cwnd_quota;
  696. }
  697. static inline int tcp_skb_is_last(const struct sock *sk,
  698. const struct sk_buff *skb)
  699. {
  700. return skb->next == (struct sk_buff *)&sk->sk_write_queue;
  701. }
  702. int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp)
  703. {
  704. struct sk_buff *skb = sk->sk_send_head;
  705. return (skb &&
  706. tcp_snd_test(sk, skb, tcp_current_mss(sk, 1),
  707. (tcp_skb_is_last(sk, skb) ?
  708. TCP_NAGLE_PUSH :
  709. tp->nonagle)));
  710. }
  711. /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
  712. * which is put after SKB on the list. It is very much like
  713. * tcp_fragment() except that it may make several kinds of assumptions
  714. * in order to speed up the splitting operation. In particular, we
  715. * know that all the data is in scatter-gather pages, and that the
  716. * packet has never been sent out before (and thus is not cloned).
  717. */
  718. static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len, unsigned int mss_now)
  719. {
  720. struct sk_buff *buff;
  721. int nlen = skb->len - len;
  722. u16 flags;
  723. /* All of a TSO frame must be composed of paged data. */
  724. BUG_ON(skb->len != skb->data_len);
  725. buff = sk_stream_alloc_pskb(sk, 0, 0, GFP_ATOMIC);
  726. if (unlikely(buff == NULL))
  727. return -ENOMEM;
  728. buff->truesize = nlen;
  729. skb->truesize -= nlen;
  730. /* Correct the sequence numbers. */
  731. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  732. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  733. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  734. /* PSH and FIN should only be set in the second packet. */
  735. flags = TCP_SKB_CB(skb)->flags;
  736. TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
  737. TCP_SKB_CB(buff)->flags = flags;
  738. /* This packet was never sent out yet, so no SACK bits. */
  739. TCP_SKB_CB(buff)->sacked = 0;
  740. buff->ip_summed = skb->ip_summed = CHECKSUM_HW;
  741. skb_split(skb, buff, len);
  742. /* Fix up tso_factor for both original and new SKB. */
  743. tcp_set_skb_tso_segs(sk, skb, mss_now);
  744. tcp_set_skb_tso_segs(sk, buff, mss_now);
  745. /* Link BUFF into the send queue. */
  746. skb_header_release(buff);
  747. __skb_append(skb, buff);
  748. return 0;
  749. }
  750. /* Try to defer sending, if possible, in order to minimize the amount
  751. * of TSO splitting we do. View it as a kind of TSO Nagle test.
  752. *
  753. * This algorithm is from John Heffner.
  754. */
  755. static int tcp_tso_should_defer(struct sock *sk, struct tcp_sock *tp, struct sk_buff *skb)
  756. {
  757. u32 send_win, cong_win, limit, in_flight;
  758. if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)
  759. return 0;
  760. if (tp->ca_state != TCP_CA_Open)
  761. return 0;
  762. in_flight = tcp_packets_in_flight(tp);
  763. BUG_ON(tcp_skb_pcount(skb) <= 1 ||
  764. (tp->snd_cwnd <= in_flight));
  765. send_win = (tp->snd_una + tp->snd_wnd) - TCP_SKB_CB(skb)->seq;
  766. /* From in_flight test above, we know that cwnd > in_flight. */
  767. cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
  768. limit = min(send_win, cong_win);
  769. /* If sk_send_head can be sent fully now, just do it. */
  770. if (skb->len <= limit)
  771. return 0;
  772. if (sysctl_tcp_tso_win_divisor) {
  773. u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
  774. /* If at least some fraction of a window is available,
  775. * just use it.
  776. */
  777. chunk /= sysctl_tcp_tso_win_divisor;
  778. if (limit >= chunk)
  779. return 0;
  780. } else {
  781. /* Different approach, try not to defer past a single
  782. * ACK. Receiver should ACK every other full sized
  783. * frame, so if we have space for more than 3 frames
  784. * then send now.
  785. */
  786. if (limit > tcp_max_burst(tp) * tp->mss_cache)
  787. return 0;
  788. }
  789. /* Ok, it looks like it is advisable to defer. */
  790. return 1;
  791. }
  792. /* This routine writes packets to the network. It advances the
  793. * send_head. This happens as incoming acks open up the remote
  794. * window for us.
  795. *
  796. * Returns 1, if no segments are in flight and we have queued segments, but
  797. * cannot send anything now because of SWS or another problem.
  798. */
  799. static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
  800. {
  801. struct tcp_sock *tp = tcp_sk(sk);
  802. struct sk_buff *skb;
  803. unsigned int tso_segs, sent_pkts;
  804. int cwnd_quota;
  805. /* If we are closed, the bytes will have to remain here.
  806. * In time closedown will finish, we empty the write queue and all
  807. * will be happy.
  808. */
  809. if (unlikely(sk->sk_state == TCP_CLOSE))
  810. return 0;
  811. sent_pkts = 0;
  812. while ((skb = sk->sk_send_head)) {
  813. tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
  814. BUG_ON(!tso_segs);
  815. cwnd_quota = tcp_cwnd_test(tp, skb);
  816. if (!cwnd_quota)
  817. break;
  818. if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
  819. break;
  820. if (tso_segs == 1) {
  821. if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
  822. (tcp_skb_is_last(sk, skb) ?
  823. nonagle : TCP_NAGLE_PUSH))))
  824. break;
  825. } else {
  826. if (tcp_tso_should_defer(sk, tp, skb))
  827. break;
  828. }
  829. if (tso_segs > 1) {
  830. u32 limit = tcp_window_allows(tp, skb,
  831. mss_now, cwnd_quota);
  832. if (skb->len < limit) {
  833. unsigned int trim = skb->len % mss_now;
  834. if (trim)
  835. limit = skb->len - trim;
  836. }
  837. if (skb->len > limit) {
  838. if (tso_fragment(sk, skb, limit, mss_now))
  839. break;
  840. }
  841. } else if (unlikely(skb->len > mss_now)) {
  842. if (unlikely(tcp_fragment(sk, skb, mss_now, mss_now)))
  843. break;
  844. }
  845. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  846. if (unlikely(tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC))))
  847. break;
  848. /* Advance the send_head. This one is sent out.
  849. * This call will increment packets_out.
  850. */
  851. update_send_head(sk, tp, skb);
  852. tcp_minshall_update(tp, mss_now, skb);
  853. sent_pkts++;
  854. }
  855. if (likely(sent_pkts)) {
  856. tcp_cwnd_validate(sk, tp);
  857. return 0;
  858. }
  859. return !tp->packets_out && sk->sk_send_head;
  860. }
  861. /* Push out any pending frames which were held back due to
  862. * TCP_CORK or attempt at coalescing tiny packets.
  863. * The socket must be locked by the caller.
  864. */
  865. void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
  866. unsigned int cur_mss, int nonagle)
  867. {
  868. struct sk_buff *skb = sk->sk_send_head;
  869. if (skb) {
  870. if (tcp_write_xmit(sk, cur_mss, nonagle))
  871. tcp_check_probe_timer(sk, tp);
  872. }
  873. }
  874. /* Send _single_ skb sitting at the send head. This function requires
  875. * true push pending frames to setup probe timer etc.
  876. */
  877. void tcp_push_one(struct sock *sk, unsigned int mss_now)
  878. {
  879. struct tcp_sock *tp = tcp_sk(sk);
  880. struct sk_buff *skb = sk->sk_send_head;
  881. unsigned int tso_segs, cwnd_quota;
  882. BUG_ON(!skb || skb->len < mss_now);
  883. tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
  884. cwnd_quota = tcp_snd_test(sk, skb, mss_now, TCP_NAGLE_PUSH);
  885. if (likely(cwnd_quota)) {
  886. BUG_ON(!tso_segs);
  887. if (tso_segs > 1) {
  888. u32 limit = tcp_window_allows(tp, skb,
  889. mss_now, cwnd_quota);
  890. if (skb->len < limit) {
  891. unsigned int trim = skb->len % mss_now;
  892. if (trim)
  893. limit = skb->len - trim;
  894. }
  895. if (skb->len > limit) {
  896. if (unlikely(tso_fragment(sk, skb, limit, mss_now)))
  897. return;
  898. }
  899. } else if (unlikely(skb->len > mss_now)) {
  900. if (unlikely(tcp_fragment(sk, skb, mss_now, mss_now)))
  901. return;
  902. }
  903. /* Send it out now. */
  904. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  905. if (likely(!tcp_transmit_skb(sk, skb_clone(skb, sk->sk_allocation)))) {
  906. update_send_head(sk, tp, skb);
  907. tcp_cwnd_validate(sk, tp);
  908. return;
  909. }
  910. }
  911. }
  912. /* This function returns the amount that we can raise the
  913. * usable window based on the following constraints
  914. *
  915. * 1. The window can never be shrunk once it is offered (RFC 793)
  916. * 2. We limit memory per socket
  917. *
  918. * RFC 1122:
  919. * "the suggested [SWS] avoidance algorithm for the receiver is to keep
  920. * RECV.NEXT + RCV.WIN fixed until:
  921. * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
  922. *
  923. * i.e. don't raise the right edge of the window until you can raise
  924. * it at least MSS bytes.
  925. *
  926. * Unfortunately, the recommended algorithm breaks header prediction,
  927. * since header prediction assumes th->window stays fixed.
  928. *
  929. * Strictly speaking, keeping th->window fixed violates the receiver
  930. * side SWS prevention criteria. The problem is that under this rule
  931. * a stream of single byte packets will cause the right side of the
  932. * window to always advance by a single byte.
  933. *
  934. * Of course, if the sender implements sender side SWS prevention
  935. * then this will not be a problem.
  936. *
  937. * BSD seems to make the following compromise:
  938. *
  939. * If the free space is less than the 1/4 of the maximum
  940. * space available and the free space is less than 1/2 mss,
  941. * then set the window to 0.
  942. * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
  943. * Otherwise, just prevent the window from shrinking
  944. * and from being larger than the largest representable value.
  945. *
  946. * This prevents incremental opening of the window in the regime
  947. * where TCP is limited by the speed of the reader side taking
  948. * data out of the TCP receive queue. It does nothing about
  949. * those cases where the window is constrained on the sender side
  950. * because the pipeline is full.
  951. *
  952. * BSD also seems to "accidentally" limit itself to windows that are a
  953. * multiple of MSS, at least until the free space gets quite small.
  954. * This would appear to be a side effect of the mbuf implementation.
  955. * Combining these two algorithms results in the observed behavior
  956. * of having a fixed window size at almost all times.
  957. *
  958. * Below we obtain similar behavior by forcing the offered window to
  959. * a multiple of the mss when it is feasible to do so.
  960. *
  961. * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
  962. * Regular options like TIMESTAMP are taken into account.
  963. */
  964. u32 __tcp_select_window(struct sock *sk)
  965. {
  966. struct tcp_sock *tp = tcp_sk(sk);
  967. /* MSS for the peer's data. Previous verions used mss_clamp
  968. * here. I don't know if the value based on our guesses
  969. * of peer's MSS is better for the performance. It's more correct
  970. * but may be worse for the performance because of rcv_mss
  971. * fluctuations. --SAW 1998/11/1
  972. */
  973. int mss = tp->ack.rcv_mss;
  974. int free_space = tcp_space(sk);
  975. int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
  976. int window;
  977. if (mss > full_space)
  978. mss = full_space;
  979. if (free_space < full_space/2) {
  980. tp->ack.quick = 0;
  981. if (tcp_memory_pressure)
  982. tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U*tp->advmss);
  983. if (free_space < mss)
  984. return 0;
  985. }
  986. if (free_space > tp->rcv_ssthresh)
  987. free_space = tp->rcv_ssthresh;
  988. /* Don't do rounding if we are using window scaling, since the
  989. * scaled window will not line up with the MSS boundary anyway.
  990. */
  991. window = tp->rcv_wnd;
  992. if (tp->rx_opt.rcv_wscale) {
  993. window = free_space;
  994. /* Advertise enough space so that it won't get scaled away.
  995. * Import case: prevent zero window announcement if
  996. * 1<<rcv_wscale > mss.
  997. */
  998. if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
  999. window = (((window >> tp->rx_opt.rcv_wscale) + 1)
  1000. << tp->rx_opt.rcv_wscale);
  1001. } else {
  1002. /* Get the largest window that is a nice multiple of mss.
  1003. * Window clamp already applied above.
  1004. * If our current window offering is within 1 mss of the
  1005. * free space we just keep it. This prevents the divide
  1006. * and multiply from happening most of the time.
  1007. * We also don't do any window rounding when the free space
  1008. * is too small.
  1009. */
  1010. if (window <= free_space - mss || window > free_space)
  1011. window = (free_space/mss)*mss;
  1012. }
  1013. return window;
  1014. }
  1015. /* Attempt to collapse two adjacent SKB's during retransmission. */
  1016. static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb, int mss_now)
  1017. {
  1018. struct tcp_sock *tp = tcp_sk(sk);
  1019. struct sk_buff *next_skb = skb->next;
  1020. /* The first test we must make is that neither of these two
  1021. * SKB's are still referenced by someone else.
  1022. */
  1023. if (!skb_cloned(skb) && !skb_cloned(next_skb)) {
  1024. int skb_size = skb->len, next_skb_size = next_skb->len;
  1025. u16 flags = TCP_SKB_CB(skb)->flags;
  1026. /* Also punt if next skb has been SACK'd. */
  1027. if(TCP_SKB_CB(next_skb)->sacked & TCPCB_SACKED_ACKED)
  1028. return;
  1029. /* Next skb is out of window. */
  1030. if (after(TCP_SKB_CB(next_skb)->end_seq, tp->snd_una+tp->snd_wnd))
  1031. return;
  1032. /* Punt if not enough space exists in the first SKB for
  1033. * the data in the second, or the total combined payload
  1034. * would exceed the MSS.
  1035. */
  1036. if ((next_skb_size > skb_tailroom(skb)) ||
  1037. ((skb_size + next_skb_size) > mss_now))
  1038. return;
  1039. BUG_ON(tcp_skb_pcount(skb) != 1 ||
  1040. tcp_skb_pcount(next_skb) != 1);
  1041. /* Ok. We will be able to collapse the packet. */
  1042. __skb_unlink(next_skb, next_skb->list);
  1043. memcpy(skb_put(skb, next_skb_size), next_skb->data, next_skb_size);
  1044. if (next_skb->ip_summed == CHECKSUM_HW)
  1045. skb->ip_summed = CHECKSUM_HW;
  1046. if (skb->ip_summed != CHECKSUM_HW)
  1047. skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
  1048. /* Update sequence range on original skb. */
  1049. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
  1050. /* Merge over control information. */
  1051. flags |= TCP_SKB_CB(next_skb)->flags; /* This moves PSH/FIN etc. over */
  1052. TCP_SKB_CB(skb)->flags = flags;
  1053. /* All done, get rid of second SKB and account for it so
  1054. * packet counting does not break.
  1055. */
  1056. TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked&(TCPCB_EVER_RETRANS|TCPCB_AT_TAIL);
  1057. if (TCP_SKB_CB(next_skb)->sacked&TCPCB_SACKED_RETRANS)
  1058. tp->retrans_out -= tcp_skb_pcount(next_skb);
  1059. if (TCP_SKB_CB(next_skb)->sacked&TCPCB_LOST) {
  1060. tp->lost_out -= tcp_skb_pcount(next_skb);
  1061. tp->left_out -= tcp_skb_pcount(next_skb);
  1062. }
  1063. /* Reno case is special. Sigh... */
  1064. if (!tp->rx_opt.sack_ok && tp->sacked_out) {
  1065. tcp_dec_pcount_approx(&tp->sacked_out, next_skb);
  1066. tp->left_out -= tcp_skb_pcount(next_skb);
  1067. }
  1068. /* Not quite right: it can be > snd.fack, but
  1069. * it is better to underestimate fackets.
  1070. */
  1071. tcp_dec_pcount_approx(&tp->fackets_out, next_skb);
  1072. tcp_packets_out_dec(tp, next_skb);
  1073. sk_stream_free_skb(sk, next_skb);
  1074. }
  1075. }
  1076. /* Do a simple retransmit without using the backoff mechanisms in
  1077. * tcp_timer. This is used for path mtu discovery.
  1078. * The socket is already locked here.
  1079. */
  1080. void tcp_simple_retransmit(struct sock *sk)
  1081. {
  1082. struct tcp_sock *tp = tcp_sk(sk);
  1083. struct sk_buff *skb;
  1084. unsigned int mss = tcp_current_mss(sk, 0);
  1085. int lost = 0;
  1086. sk_stream_for_retrans_queue(skb, sk) {
  1087. if (skb->len > mss &&
  1088. !(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) {
  1089. if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
  1090. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  1091. tp->retrans_out -= tcp_skb_pcount(skb);
  1092. }
  1093. if (!(TCP_SKB_CB(skb)->sacked&TCPCB_LOST)) {
  1094. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  1095. tp->lost_out += tcp_skb_pcount(skb);
  1096. lost = 1;
  1097. }
  1098. }
  1099. }
  1100. if (!lost)
  1101. return;
  1102. tcp_sync_left_out(tp);
  1103. /* Don't muck with the congestion window here.
  1104. * Reason is that we do not increase amount of _data_
  1105. * in network, but units changed and effective
  1106. * cwnd/ssthresh really reduced now.
  1107. */
  1108. if (tp->ca_state != TCP_CA_Loss) {
  1109. tp->high_seq = tp->snd_nxt;
  1110. tp->snd_ssthresh = tcp_current_ssthresh(tp);
  1111. tp->prior_ssthresh = 0;
  1112. tp->undo_marker = 0;
  1113. tcp_set_ca_state(tp, TCP_CA_Loss);
  1114. }
  1115. tcp_xmit_retransmit_queue(sk);
  1116. }
  1117. /* This retransmits one SKB. Policy decisions and retransmit queue
  1118. * state updates are done by the caller. Returns non-zero if an
  1119. * error occurred which prevented the send.
  1120. */
  1121. int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  1122. {
  1123. struct tcp_sock *tp = tcp_sk(sk);
  1124. unsigned int cur_mss = tcp_current_mss(sk, 0);
  1125. int err;
  1126. /* Do not sent more than we queued. 1/4 is reserved for possible
  1127. * copying overhead: frgagmentation, tunneling, mangling etc.
  1128. */
  1129. if (atomic_read(&sk->sk_wmem_alloc) >
  1130. min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
  1131. return -EAGAIN;
  1132. if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
  1133. if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
  1134. BUG();
  1135. if (sk->sk_route_caps & NETIF_F_TSO) {
  1136. sk->sk_route_caps &= ~NETIF_F_TSO;
  1137. sock_set_flag(sk, SOCK_NO_LARGESEND);
  1138. }
  1139. if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
  1140. return -ENOMEM;
  1141. }
  1142. /* If receiver has shrunk his window, and skb is out of
  1143. * new window, do not retransmit it. The exception is the
  1144. * case, when window is shrunk to zero. In this case
  1145. * our retransmit serves as a zero window probe.
  1146. */
  1147. if (!before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)
  1148. && TCP_SKB_CB(skb)->seq != tp->snd_una)
  1149. return -EAGAIN;
  1150. if (skb->len > cur_mss) {
  1151. int old_factor = tcp_skb_pcount(skb);
  1152. int diff;
  1153. if (tcp_fragment(sk, skb, cur_mss, cur_mss))
  1154. return -ENOMEM; /* We'll try again later. */
  1155. /* New SKB created, account for it. */
  1156. diff = old_factor - tcp_skb_pcount(skb) -
  1157. tcp_skb_pcount(skb->next);
  1158. tp->packets_out -= diff;
  1159. if (diff > 0) {
  1160. tp->fackets_out -= diff;
  1161. if ((int)tp->fackets_out < 0)
  1162. tp->fackets_out = 0;
  1163. }
  1164. }
  1165. /* Collapse two adjacent packets if worthwhile and we can. */
  1166. if(!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN) &&
  1167. (skb->len < (cur_mss >> 1)) &&
  1168. (skb->next != sk->sk_send_head) &&
  1169. (skb->next != (struct sk_buff *)&sk->sk_write_queue) &&
  1170. (skb_shinfo(skb)->nr_frags == 0 && skb_shinfo(skb->next)->nr_frags == 0) &&
  1171. (tcp_skb_pcount(skb) == 1 && tcp_skb_pcount(skb->next) == 1) &&
  1172. (sysctl_tcp_retrans_collapse != 0))
  1173. tcp_retrans_try_collapse(sk, skb, cur_mss);
  1174. if(tp->af_specific->rebuild_header(sk))
  1175. return -EHOSTUNREACH; /* Routing failure or similar. */
  1176. /* Some Solaris stacks overoptimize and ignore the FIN on a
  1177. * retransmit when old data is attached. So strip it off
  1178. * since it is cheap to do so and saves bytes on the network.
  1179. */
  1180. if(skb->len > 0 &&
  1181. (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
  1182. tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
  1183. if (!pskb_trim(skb, 0)) {
  1184. TCP_SKB_CB(skb)->seq = TCP_SKB_CB(skb)->end_seq - 1;
  1185. skb_shinfo(skb)->tso_segs = 1;
  1186. skb_shinfo(skb)->tso_size = 0;
  1187. skb->ip_summed = CHECKSUM_NONE;
  1188. skb->csum = 0;
  1189. }
  1190. }
  1191. /* Make a copy, if the first transmission SKB clone we made
  1192. * is still in somebody's hands, else make a clone.
  1193. */
  1194. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1195. err = tcp_transmit_skb(sk, (skb_cloned(skb) ?
  1196. pskb_copy(skb, GFP_ATOMIC):
  1197. skb_clone(skb, GFP_ATOMIC)));
  1198. if (err == 0) {
  1199. /* Update global TCP statistics. */
  1200. TCP_INC_STATS(TCP_MIB_RETRANSSEGS);
  1201. tp->total_retrans++;
  1202. #if FASTRETRANS_DEBUG > 0
  1203. if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
  1204. if (net_ratelimit())
  1205. printk(KERN_DEBUG "retrans_out leaked.\n");
  1206. }
  1207. #endif
  1208. TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
  1209. tp->retrans_out += tcp_skb_pcount(skb);
  1210. /* Save stamp of the first retransmit. */
  1211. if (!tp->retrans_stamp)
  1212. tp->retrans_stamp = TCP_SKB_CB(skb)->when;
  1213. tp->undo_retrans++;
  1214. /* snd_nxt is stored to detect loss of retransmitted segment,
  1215. * see tcp_input.c tcp_sacktag_write_queue().
  1216. */
  1217. TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
  1218. }
  1219. return err;
  1220. }
  1221. /* This gets called after a retransmit timeout, and the initially
  1222. * retransmitted data is acknowledged. It tries to continue
  1223. * resending the rest of the retransmit queue, until either
  1224. * we've sent it all or the congestion window limit is reached.
  1225. * If doing SACK, the first ACK which comes back for a timeout
  1226. * based retransmit packet might feed us FACK information again.
  1227. * If so, we use it to avoid unnecessarily retransmissions.
  1228. */
  1229. void tcp_xmit_retransmit_queue(struct sock *sk)
  1230. {
  1231. struct tcp_sock *tp = tcp_sk(sk);
  1232. struct sk_buff *skb;
  1233. int packet_cnt = tp->lost_out;
  1234. /* First pass: retransmit lost packets. */
  1235. if (packet_cnt) {
  1236. sk_stream_for_retrans_queue(skb, sk) {
  1237. __u8 sacked = TCP_SKB_CB(skb)->sacked;
  1238. /* Assume this retransmit will generate
  1239. * only one packet for congestion window
  1240. * calculation purposes. This works because
  1241. * tcp_retransmit_skb() will chop up the
  1242. * packet to be MSS sized and all the
  1243. * packet counting works out.
  1244. */
  1245. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  1246. return;
  1247. if (sacked&TCPCB_LOST) {
  1248. if (!(sacked&(TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))) {
  1249. if (tcp_retransmit_skb(sk, skb))
  1250. return;
  1251. if (tp->ca_state != TCP_CA_Loss)
  1252. NET_INC_STATS_BH(LINUX_MIB_TCPFASTRETRANS);
  1253. else
  1254. NET_INC_STATS_BH(LINUX_MIB_TCPSLOWSTARTRETRANS);
  1255. if (skb ==
  1256. skb_peek(&sk->sk_write_queue))
  1257. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  1258. }
  1259. packet_cnt -= tcp_skb_pcount(skb);
  1260. if (packet_cnt <= 0)
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. /* OK, demanded retransmission is finished. */
  1266. /* Forward retransmissions are possible only during Recovery. */
  1267. if (tp->ca_state != TCP_CA_Recovery)
  1268. return;
  1269. /* No forward retransmissions in Reno are possible. */
  1270. if (!tp->rx_opt.sack_ok)
  1271. return;
  1272. /* Yeah, we have to make difficult choice between forward transmission
  1273. * and retransmission... Both ways have their merits...
  1274. *
  1275. * For now we do not retransmit anything, while we have some new
  1276. * segments to send.
  1277. */
  1278. if (tcp_may_send_now(sk, tp))
  1279. return;
  1280. packet_cnt = 0;
  1281. sk_stream_for_retrans_queue(skb, sk) {
  1282. /* Similar to the retransmit loop above we
  1283. * can pretend that the retransmitted SKB
  1284. * we send out here will be composed of one
  1285. * real MSS sized packet because tcp_retransmit_skb()
  1286. * will fragment it if necessary.
  1287. */
  1288. if (++packet_cnt > tp->fackets_out)
  1289. break;
  1290. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  1291. break;
  1292. if (TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS)
  1293. continue;
  1294. /* Ok, retransmit it. */
  1295. if (tcp_retransmit_skb(sk, skb))
  1296. break;
  1297. if (skb == skb_peek(&sk->sk_write_queue))
  1298. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  1299. NET_INC_STATS_BH(LINUX_MIB_TCPFORWARDRETRANS);
  1300. }
  1301. }
  1302. /* Send a fin. The caller locks the socket for us. This cannot be
  1303. * allowed to fail queueing a FIN frame under any circumstances.
  1304. */
  1305. void tcp_send_fin(struct sock *sk)
  1306. {
  1307. struct tcp_sock *tp = tcp_sk(sk);
  1308. struct sk_buff *skb = skb_peek_tail(&sk->sk_write_queue);
  1309. int mss_now;
  1310. /* Optimization, tack on the FIN if we have a queue of
  1311. * unsent frames. But be careful about outgoing SACKS
  1312. * and IP options.
  1313. */
  1314. mss_now = tcp_current_mss(sk, 1);
  1315. if (sk->sk_send_head != NULL) {
  1316. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_FIN;
  1317. TCP_SKB_CB(skb)->end_seq++;
  1318. tp->write_seq++;
  1319. } else {
  1320. /* Socket is locked, keep trying until memory is available. */
  1321. for (;;) {
  1322. skb = alloc_skb(MAX_TCP_HEADER, GFP_KERNEL);
  1323. if (skb)
  1324. break;
  1325. yield();
  1326. }
  1327. /* Reserve space for headers and prepare control bits. */
  1328. skb_reserve(skb, MAX_TCP_HEADER);
  1329. skb->csum = 0;
  1330. TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);
  1331. TCP_SKB_CB(skb)->sacked = 0;
  1332. skb_shinfo(skb)->tso_segs = 1;
  1333. skb_shinfo(skb)->tso_size = 0;
  1334. /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
  1335. TCP_SKB_CB(skb)->seq = tp->write_seq;
  1336. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
  1337. tcp_queue_skb(sk, skb);
  1338. }
  1339. __tcp_push_pending_frames(sk, tp, mss_now, TCP_NAGLE_OFF);
  1340. }
  1341. /* We get here when a process closes a file descriptor (either due to
  1342. * an explicit close() or as a byproduct of exit()'ing) and there
  1343. * was unread data in the receive queue. This behavior is recommended
  1344. * by draft-ietf-tcpimpl-prob-03.txt section 3.10. -DaveM
  1345. */
  1346. void tcp_send_active_reset(struct sock *sk, unsigned int __nocast priority)
  1347. {
  1348. struct tcp_sock *tp = tcp_sk(sk);
  1349. struct sk_buff *skb;
  1350. /* NOTE: No TCP options attached and we never retransmit this. */
  1351. skb = alloc_skb(MAX_TCP_HEADER, priority);
  1352. if (!skb) {
  1353. NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
  1354. return;
  1355. }
  1356. /* Reserve space for headers and prepare control bits. */
  1357. skb_reserve(skb, MAX_TCP_HEADER);
  1358. skb->csum = 0;
  1359. TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_RST);
  1360. TCP_SKB_CB(skb)->sacked = 0;
  1361. skb_shinfo(skb)->tso_segs = 1;
  1362. skb_shinfo(skb)->tso_size = 0;
  1363. /* Send it off. */
  1364. TCP_SKB_CB(skb)->seq = tcp_acceptable_seq(sk, tp);
  1365. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
  1366. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1367. if (tcp_transmit_skb(sk, skb))
  1368. NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
  1369. }
  1370. /* WARNING: This routine must only be called when we have already sent
  1371. * a SYN packet that crossed the incoming SYN that caused this routine
  1372. * to get called. If this assumption fails then the initial rcv_wnd
  1373. * and rcv_wscale values will not be correct.
  1374. */
  1375. int tcp_send_synack(struct sock *sk)
  1376. {
  1377. struct sk_buff* skb;
  1378. skb = skb_peek(&sk->sk_write_queue);
  1379. if (skb == NULL || !(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_SYN)) {
  1380. printk(KERN_DEBUG "tcp_send_synack: wrong queue state\n");
  1381. return -EFAULT;
  1382. }
  1383. if (!(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_ACK)) {
  1384. if (skb_cloned(skb)) {
  1385. struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
  1386. if (nskb == NULL)
  1387. return -ENOMEM;
  1388. __skb_unlink(skb, &sk->sk_write_queue);
  1389. skb_header_release(nskb);
  1390. __skb_queue_head(&sk->sk_write_queue, nskb);
  1391. sk_stream_free_skb(sk, skb);
  1392. sk_charge_skb(sk, nskb);
  1393. skb = nskb;
  1394. }
  1395. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;
  1396. TCP_ECN_send_synack(tcp_sk(sk), skb);
  1397. }
  1398. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1399. return tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));
  1400. }
  1401. /*
  1402. * Prepare a SYN-ACK.
  1403. */
  1404. struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
  1405. struct request_sock *req)
  1406. {
  1407. struct inet_request_sock *ireq = inet_rsk(req);
  1408. struct tcp_sock *tp = tcp_sk(sk);
  1409. struct tcphdr *th;
  1410. int tcp_header_size;
  1411. struct sk_buff *skb;
  1412. skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
  1413. if (skb == NULL)
  1414. return NULL;
  1415. /* Reserve space for headers. */
  1416. skb_reserve(skb, MAX_TCP_HEADER);
  1417. skb->dst = dst_clone(dst);
  1418. tcp_header_size = (sizeof(struct tcphdr) + TCPOLEN_MSS +
  1419. (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0) +
  1420. (ireq->wscale_ok ? TCPOLEN_WSCALE_ALIGNED : 0) +
  1421. /* SACK_PERM is in the place of NOP NOP of TS */
  1422. ((ireq->sack_ok && !ireq->tstamp_ok) ? TCPOLEN_SACKPERM_ALIGNED : 0));
  1423. skb->h.th = th = (struct tcphdr *) skb_push(skb, tcp_header_size);
  1424. memset(th, 0, sizeof(struct tcphdr));
  1425. th->syn = 1;
  1426. th->ack = 1;
  1427. if (dst->dev->features&NETIF_F_TSO)
  1428. ireq->ecn_ok = 0;
  1429. TCP_ECN_make_synack(req, th);
  1430. th->source = inet_sk(sk)->sport;
  1431. th->dest = ireq->rmt_port;
  1432. TCP_SKB_CB(skb)->seq = tcp_rsk(req)->snt_isn;
  1433. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
  1434. TCP_SKB_CB(skb)->sacked = 0;
  1435. skb_shinfo(skb)->tso_segs = 1;
  1436. skb_shinfo(skb)->tso_size = 0;
  1437. th->seq = htonl(TCP_SKB_CB(skb)->seq);
  1438. th->ack_seq = htonl(tcp_rsk(req)->rcv_isn + 1);
  1439. if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
  1440. __u8 rcv_wscale;
  1441. /* Set this up on the first call only */
  1442. req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
  1443. /* tcp_full_space because it is guaranteed to be the first packet */
  1444. tcp_select_initial_window(tcp_full_space(sk),
  1445. dst_metric(dst, RTAX_ADVMSS) - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
  1446. &req->rcv_wnd,
  1447. &req->window_clamp,
  1448. ireq->wscale_ok,
  1449. &rcv_wscale);
  1450. ireq->rcv_wscale = rcv_wscale;
  1451. }
  1452. /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
  1453. th->window = htons(req->rcv_wnd);
  1454. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1455. tcp_syn_build_options((__u32 *)(th + 1), dst_metric(dst, RTAX_ADVMSS), ireq->tstamp_ok,
  1456. ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale,
  1457. TCP_SKB_CB(skb)->when,
  1458. req->ts_recent);
  1459. skb->csum = 0;
  1460. th->doff = (tcp_header_size >> 2);
  1461. TCP_INC_STATS(TCP_MIB_OUTSEGS);
  1462. return skb;
  1463. }
  1464. /*
  1465. * Do all connect socket setups that can be done AF independent.
  1466. */
  1467. static inline void tcp_connect_init(struct sock *sk)
  1468. {
  1469. struct dst_entry *dst = __sk_dst_get(sk);
  1470. struct tcp_sock *tp = tcp_sk(sk);
  1471. __u8 rcv_wscale;
  1472. /* We'll fix this up when we get a response from the other end.
  1473. * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
  1474. */
  1475. tp->tcp_header_len = sizeof(struct tcphdr) +
  1476. (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
  1477. /* If user gave his TCP_MAXSEG, record it to clamp */
  1478. if (tp->rx_opt.user_mss)
  1479. tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
  1480. tp->max_window = 0;
  1481. tcp_sync_mss(sk, dst_mtu(dst));
  1482. if (!tp->window_clamp)
  1483. tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
  1484. tp->advmss = dst_metric(dst, RTAX_ADVMSS);
  1485. tcp_initialize_rcv_mss(sk);
  1486. tcp_select_initial_window(tcp_full_space(sk),
  1487. tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
  1488. &tp->rcv_wnd,
  1489. &tp->window_clamp,
  1490. sysctl_tcp_window_scaling,
  1491. &rcv_wscale);
  1492. tp->rx_opt.rcv_wscale = rcv_wscale;
  1493. tp->rcv_ssthresh = tp->rcv_wnd;
  1494. sk->sk_err = 0;
  1495. sock_reset_flag(sk, SOCK_DONE);
  1496. tp->snd_wnd = 0;
  1497. tcp_init_wl(tp, tp->write_seq, 0);
  1498. tp->snd_una = tp->write_seq;
  1499. tp->snd_sml = tp->write_seq;
  1500. tp->rcv_nxt = 0;
  1501. tp->rcv_wup = 0;
  1502. tp->copied_seq = 0;
  1503. tp->rto = TCP_TIMEOUT_INIT;
  1504. tp->retransmits = 0;
  1505. tcp_clear_retrans(tp);
  1506. }
  1507. /*
  1508. * Build a SYN and send it off.
  1509. */
  1510. int tcp_connect(struct sock *sk)
  1511. {
  1512. struct tcp_sock *tp = tcp_sk(sk);
  1513. struct sk_buff *buff;
  1514. tcp_connect_init(sk);
  1515. buff = alloc_skb(MAX_TCP_HEADER + 15, sk->sk_allocation);
  1516. if (unlikely(buff == NULL))
  1517. return -ENOBUFS;
  1518. /* Reserve space for headers. */
  1519. skb_reserve(buff, MAX_TCP_HEADER);
  1520. TCP_SKB_CB(buff)->flags = TCPCB_FLAG_SYN;
  1521. TCP_ECN_send_syn(sk, tp, buff);
  1522. TCP_SKB_CB(buff)->sacked = 0;
  1523. skb_shinfo(buff)->tso_segs = 1;
  1524. skb_shinfo(buff)->tso_size = 0;
  1525. buff->csum = 0;
  1526. TCP_SKB_CB(buff)->seq = tp->write_seq++;
  1527. TCP_SKB_CB(buff)->end_seq = tp->write_seq;
  1528. tp->snd_nxt = tp->write_seq;
  1529. tp->pushed_seq = tp->write_seq;
  1530. /* Send it off. */
  1531. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  1532. tp->retrans_stamp = TCP_SKB_CB(buff)->when;
  1533. skb_header_release(buff);
  1534. __skb_queue_tail(&sk->sk_write_queue, buff);
  1535. sk_charge_skb(sk, buff);
  1536. tp->packets_out += tcp_skb_pcount(buff);
  1537. tcp_transmit_skb(sk, skb_clone(buff, GFP_KERNEL));
  1538. TCP_INC_STATS(TCP_MIB_ACTIVEOPENS);
  1539. /* Timer for repeating the SYN until an answer. */
  1540. tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
  1541. return 0;
  1542. }
  1543. /* Send out a delayed ack, the caller does the policy checking
  1544. * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check()
  1545. * for details.
  1546. */
  1547. void tcp_send_delayed_ack(struct sock *sk)
  1548. {
  1549. struct tcp_sock *tp = tcp_sk(sk);
  1550. int ato = tp->ack.ato;
  1551. unsigned long timeout;
  1552. if (ato > TCP_DELACK_MIN) {
  1553. int max_ato = HZ/2;
  1554. if (tp->ack.pingpong || (tp->ack.pending&TCP_ACK_PUSHED))
  1555. max_ato = TCP_DELACK_MAX;
  1556. /* Slow path, intersegment interval is "high". */
  1557. /* If some rtt estimate is known, use it to bound delayed ack.
  1558. * Do not use tp->rto here, use results of rtt measurements
  1559. * directly.
  1560. */
  1561. if (tp->srtt) {
  1562. int rtt = max(tp->srtt>>3, TCP_DELACK_MIN);
  1563. if (rtt < max_ato)
  1564. max_ato = rtt;
  1565. }
  1566. ato = min(ato, max_ato);
  1567. }
  1568. /* Stay within the limit we were given */
  1569. timeout = jiffies + ato;
  1570. /* Use new timeout only if there wasn't a older one earlier. */
  1571. if (tp->ack.pending&TCP_ACK_TIMER) {
  1572. /* If delack timer was blocked or is about to expire,
  1573. * send ACK now.
  1574. */
  1575. if (tp->ack.blocked || time_before_eq(tp->ack.timeout, jiffies+(ato>>2))) {
  1576. tcp_send_ack(sk);
  1577. return;
  1578. }
  1579. if (!time_before(timeout, tp->ack.timeout))
  1580. timeout = tp->ack.timeout;
  1581. }
  1582. tp->ack.pending |= TCP_ACK_SCHED|TCP_ACK_TIMER;
  1583. tp->ack.timeout = timeout;
  1584. sk_reset_timer(sk, &tp->delack_timer, timeout);
  1585. }
  1586. /* This routine sends an ack and also updates the window. */
  1587. void tcp_send_ack(struct sock *sk)
  1588. {
  1589. /* If we have been reset, we may not send again. */
  1590. if (sk->sk_state != TCP_CLOSE) {
  1591. struct tcp_sock *tp = tcp_sk(sk);
  1592. struct sk_buff *buff;
  1593. /* We are not putting this on the write queue, so
  1594. * tcp_transmit_skb() will set the ownership to this
  1595. * sock.
  1596. */
  1597. buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  1598. if (buff == NULL) {
  1599. tcp_schedule_ack(tp);
  1600. tp->ack.ato = TCP_ATO_MIN;
  1601. tcp_reset_xmit_timer(sk, TCP_TIME_DACK, TCP_DELACK_MAX);
  1602. return;
  1603. }
  1604. /* Reserve space for headers and prepare control bits. */
  1605. skb_reserve(buff, MAX_TCP_HEADER);
  1606. buff->csum = 0;
  1607. TCP_SKB_CB(buff)->flags = TCPCB_FLAG_ACK;
  1608. TCP_SKB_CB(buff)->sacked = 0;
  1609. skb_shinfo(buff)->tso_segs = 1;
  1610. skb_shinfo(buff)->tso_size = 0;
  1611. /* Send it off, this clears delayed acks for us. */
  1612. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(buff)->end_seq = tcp_acceptable_seq(sk, tp);
  1613. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  1614. tcp_transmit_skb(sk, buff);
  1615. }
  1616. }
  1617. /* This routine sends a packet with an out of date sequence
  1618. * number. It assumes the other end will try to ack it.
  1619. *
  1620. * Question: what should we make while urgent mode?
  1621. * 4.4BSD forces sending single byte of data. We cannot send
  1622. * out of window data, because we have SND.NXT==SND.MAX...
  1623. *
  1624. * Current solution: to send TWO zero-length segments in urgent mode:
  1625. * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
  1626. * out-of-date with SND.UNA-1 to probe window.
  1627. */
  1628. static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
  1629. {
  1630. struct tcp_sock *tp = tcp_sk(sk);
  1631. struct sk_buff *skb;
  1632. /* We don't queue it, tcp_transmit_skb() sets ownership. */
  1633. skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  1634. if (skb == NULL)
  1635. return -1;
  1636. /* Reserve space for headers and set control bits. */
  1637. skb_reserve(skb, MAX_TCP_HEADER);
  1638. skb->csum = 0;
  1639. TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
  1640. TCP_SKB_CB(skb)->sacked = urgent;
  1641. skb_shinfo(skb)->tso_segs = 1;
  1642. skb_shinfo(skb)->tso_size = 0;
  1643. /* Use a previous sequence. This should cause the other
  1644. * end to send an ack. Don't queue or clone SKB, just
  1645. * send it.
  1646. */
  1647. TCP_SKB_CB(skb)->seq = urgent ? tp->snd_una : tp->snd_una - 1;
  1648. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
  1649. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1650. return tcp_transmit_skb(sk, skb);
  1651. }
  1652. int tcp_write_wakeup(struct sock *sk)
  1653. {
  1654. if (sk->sk_state != TCP_CLOSE) {
  1655. struct tcp_sock *tp = tcp_sk(sk);
  1656. struct sk_buff *skb;
  1657. if ((skb = sk->sk_send_head) != NULL &&
  1658. before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)) {
  1659. int err;
  1660. unsigned int mss = tcp_current_mss(sk, 0);
  1661. unsigned int seg_size = tp->snd_una+tp->snd_wnd-TCP_SKB_CB(skb)->seq;
  1662. if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
  1663. tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
  1664. /* We are probing the opening of a window
  1665. * but the window size is != 0
  1666. * must have been a result SWS avoidance ( sender )
  1667. */
  1668. if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
  1669. skb->len > mss) {
  1670. seg_size = min(seg_size, mss);
  1671. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  1672. if (tcp_fragment(sk, skb, seg_size, mss))
  1673. return -1;
  1674. /* SWS override triggered forced fragmentation.
  1675. * Disable TSO, the connection is too sick. */
  1676. if (sk->sk_route_caps & NETIF_F_TSO) {
  1677. sock_set_flag(sk, SOCK_NO_LARGESEND);
  1678. sk->sk_route_caps &= ~NETIF_F_TSO;
  1679. }
  1680. } else if (!tcp_skb_pcount(skb))
  1681. tcp_set_skb_tso_segs(sk, skb, mss);
  1682. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  1683. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1684. err = tcp_transmit_skb(sk, skb_clone(skb, GFP_ATOMIC));
  1685. if (!err) {
  1686. update_send_head(sk, tp, skb);
  1687. }
  1688. return err;
  1689. } else {
  1690. if (tp->urg_mode &&
  1691. between(tp->snd_up, tp->snd_una+1, tp->snd_una+0xFFFF))
  1692. tcp_xmit_probe_skb(sk, TCPCB_URG);
  1693. return tcp_xmit_probe_skb(sk, 0);
  1694. }
  1695. }
  1696. return -1;
  1697. }
  1698. /* A window probe timeout has occurred. If window is not closed send
  1699. * a partial packet else a zero probe.
  1700. */
  1701. void tcp_send_probe0(struct sock *sk)
  1702. {
  1703. struct tcp_sock *tp = tcp_sk(sk);
  1704. int err;
  1705. err = tcp_write_wakeup(sk);
  1706. if (tp->packets_out || !sk->sk_send_head) {
  1707. /* Cancel probe timer, if it is not required. */
  1708. tp->probes_out = 0;
  1709. tp->backoff = 0;
  1710. return;
  1711. }
  1712. if (err <= 0) {
  1713. if (tp->backoff < sysctl_tcp_retries2)
  1714. tp->backoff++;
  1715. tp->probes_out++;
  1716. tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0,
  1717. min(tp->rto << tp->backoff, TCP_RTO_MAX));
  1718. } else {
  1719. /* If packet was not sent due to local congestion,
  1720. * do not backoff and do not remember probes_out.
  1721. * Let local senders to fight for local resources.
  1722. *
  1723. * Use accumulated backoff yet.
  1724. */
  1725. if (!tp->probes_out)
  1726. tp->probes_out=1;
  1727. tcp_reset_xmit_timer (sk, TCP_TIME_PROBE0,
  1728. min(tp->rto << tp->backoff, TCP_RESOURCE_PROBE_INTERVAL));
  1729. }
  1730. }
  1731. EXPORT_SYMBOL(tcp_connect);
  1732. EXPORT_SYMBOL(tcp_make_synack);
  1733. EXPORT_SYMBOL(tcp_simple_retransmit);
  1734. EXPORT_SYMBOL(tcp_sync_mss);