tcp_output.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164
  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. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  11. * Corey Minyard <wf-rch!minyard@relay.EU.net>
  12. * Florian La Roche, <flla@stud.uni-sb.de>
  13. * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  14. * Linus Torvalds, <torvalds@cs.helsinki.fi>
  15. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  16. * Matthew Dillon, <dillon@apollo.west.oic.com>
  17. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  18. * Jorge Cwik, <jorge@laser.satlink.net>
  19. */
  20. /*
  21. * Changes: Pedro Roque : Retransmit queue handled by TCP.
  22. * : Fragmentation on mtu decrease
  23. * : Segment collapse on retransmit
  24. * : AF independence
  25. *
  26. * Linus Torvalds : send_delayed_ack
  27. * David S. Miller : Charge memory using the right skb
  28. * during syn/ack processing.
  29. * David S. Miller : Output engine completely rewritten.
  30. * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
  31. * Cacophonix Gaul : draft-minshall-nagle-01
  32. * J Hadi Salim : ECN support
  33. *
  34. */
  35. #define pr_fmt(fmt) "TCP: " fmt
  36. #include <net/tcp.h>
  37. #include <linux/compiler.h>
  38. #include <linux/gfp.h>
  39. #include <linux/module.h>
  40. /* People can turn this off for buggy TCP's found in printers etc. */
  41. int sysctl_tcp_retrans_collapse __read_mostly = 1;
  42. /* People can turn this on to work with those rare, broken TCPs that
  43. * interpret the window field as a signed quantity.
  44. */
  45. int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
  46. /* Default TSQ limit of two TSO segments */
  47. int sysctl_tcp_limit_output_bytes __read_mostly = 131072;
  48. /* This limits the percentage of the congestion window which we
  49. * will allow a single TSO frame to consume. Building TSO frames
  50. * which are too large can cause TCP streams to be bursty.
  51. */
  52. int sysctl_tcp_tso_win_divisor __read_mostly = 3;
  53. int sysctl_tcp_mtu_probing __read_mostly = 0;
  54. int sysctl_tcp_base_mss __read_mostly = TCP_BASE_MSS;
  55. /* By default, RFC2861 behavior. */
  56. int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
  57. static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
  58. int push_one, gfp_t gfp);
  59. /* Account for new data that has been sent to the network. */
  60. static void tcp_event_new_data_sent(struct sock *sk, const struct sk_buff *skb)
  61. {
  62. struct inet_connection_sock *icsk = inet_csk(sk);
  63. struct tcp_sock *tp = tcp_sk(sk);
  64. unsigned int prior_packets = tp->packets_out;
  65. tcp_advance_send_head(sk, skb);
  66. tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
  67. tp->packets_out += tcp_skb_pcount(skb);
  68. if (!prior_packets || icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
  69. icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)
  70. tcp_rearm_rto(sk);
  71. }
  72. /* SND.NXT, if window was not shrunk.
  73. * If window has been shrunk, what should we make? It is not clear at all.
  74. * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
  75. * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
  76. * invalid. OK, let's make this for now:
  77. */
  78. static inline __u32 tcp_acceptable_seq(const struct sock *sk)
  79. {
  80. const struct tcp_sock *tp = tcp_sk(sk);
  81. if (!before(tcp_wnd_end(tp), tp->snd_nxt))
  82. return tp->snd_nxt;
  83. else
  84. return tcp_wnd_end(tp);
  85. }
  86. /* Calculate mss to advertise in SYN segment.
  87. * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
  88. *
  89. * 1. It is independent of path mtu.
  90. * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
  91. * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
  92. * attached devices, because some buggy hosts are confused by
  93. * large MSS.
  94. * 4. We do not make 3, we advertise MSS, calculated from first
  95. * hop device mtu, but allow to raise it to ip_rt_min_advmss.
  96. * This may be overridden via information stored in routing table.
  97. * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
  98. * probably even Jumbo".
  99. */
  100. static __u16 tcp_advertise_mss(struct sock *sk)
  101. {
  102. struct tcp_sock *tp = tcp_sk(sk);
  103. const struct dst_entry *dst = __sk_dst_get(sk);
  104. int mss = tp->advmss;
  105. if (dst) {
  106. unsigned int metric = dst_metric_advmss(dst);
  107. if (metric < mss) {
  108. mss = metric;
  109. tp->advmss = mss;
  110. }
  111. }
  112. return (__u16)mss;
  113. }
  114. /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
  115. * This is the first part of cwnd validation mechanism. */
  116. static void tcp_cwnd_restart(struct sock *sk, const struct dst_entry *dst)
  117. {
  118. struct tcp_sock *tp = tcp_sk(sk);
  119. s32 delta = tcp_time_stamp - tp->lsndtime;
  120. u32 restart_cwnd = tcp_init_cwnd(tp, dst);
  121. u32 cwnd = tp->snd_cwnd;
  122. tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
  123. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  124. restart_cwnd = min(restart_cwnd, cwnd);
  125. while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
  126. cwnd >>= 1;
  127. tp->snd_cwnd = max(cwnd, restart_cwnd);
  128. tp->snd_cwnd_stamp = tcp_time_stamp;
  129. tp->snd_cwnd_used = 0;
  130. }
  131. /* Congestion state accounting after a packet has been sent. */
  132. static void tcp_event_data_sent(struct tcp_sock *tp,
  133. struct sock *sk)
  134. {
  135. struct inet_connection_sock *icsk = inet_csk(sk);
  136. const u32 now = tcp_time_stamp;
  137. if (sysctl_tcp_slow_start_after_idle &&
  138. (!tp->packets_out && (s32)(now - tp->lsndtime) > icsk->icsk_rto))
  139. tcp_cwnd_restart(sk, __sk_dst_get(sk));
  140. tp->lsndtime = now;
  141. /* If it is a reply for ato after last received
  142. * packet, enter pingpong mode.
  143. */
  144. if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
  145. icsk->icsk_ack.pingpong = 1;
  146. }
  147. /* Account for an ACK we sent. */
  148. static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
  149. {
  150. tcp_dec_quickack_mode(sk, pkts);
  151. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  152. }
  153. /* Determine a window scaling and initial window to offer.
  154. * Based on the assumption that the given amount of space
  155. * will be offered. Store the results in the tp structure.
  156. * NOTE: for smooth operation initial space offering should
  157. * be a multiple of mss if possible. We assume here that mss >= 1.
  158. * This MUST be enforced by all callers.
  159. */
  160. void tcp_select_initial_window(int __space, __u32 mss,
  161. __u32 *rcv_wnd, __u32 *window_clamp,
  162. int wscale_ok, __u8 *rcv_wscale,
  163. __u32 init_rcv_wnd)
  164. {
  165. unsigned int space = (__space < 0 ? 0 : __space);
  166. /* If no clamp set the clamp to the max possible scaled window */
  167. if (*window_clamp == 0)
  168. (*window_clamp) = (65535 << 14);
  169. space = min(*window_clamp, space);
  170. /* Quantize space offering to a multiple of mss if possible. */
  171. if (space > mss)
  172. space = (space / mss) * mss;
  173. /* NOTE: offering an initial window larger than 32767
  174. * will break some buggy TCP stacks. If the admin tells us
  175. * it is likely we could be speaking with such a buggy stack
  176. * we will truncate our initial window offering to 32K-1
  177. * unless the remote has sent us a window scaling option,
  178. * which we interpret as a sign the remote TCP is not
  179. * misinterpreting the window field as a signed quantity.
  180. */
  181. if (sysctl_tcp_workaround_signed_windows)
  182. (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
  183. else
  184. (*rcv_wnd) = space;
  185. (*rcv_wscale) = 0;
  186. if (wscale_ok) {
  187. /* Set window scaling on max possible window
  188. * See RFC1323 for an explanation of the limit to 14
  189. */
  190. space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
  191. space = min_t(u32, space, *window_clamp);
  192. while (space > 65535 && (*rcv_wscale) < 14) {
  193. space >>= 1;
  194. (*rcv_wscale)++;
  195. }
  196. }
  197. /* Set initial window to a value enough for senders starting with
  198. * initial congestion window of TCP_DEFAULT_INIT_RCVWND. Place
  199. * a limit on the initial window when mss is larger than 1460.
  200. */
  201. if (mss > (1 << *rcv_wscale)) {
  202. int init_cwnd = TCP_DEFAULT_INIT_RCVWND;
  203. if (mss > 1460)
  204. init_cwnd =
  205. max_t(u32, (1460 * TCP_DEFAULT_INIT_RCVWND) / mss, 2);
  206. /* when initializing use the value from init_rcv_wnd
  207. * rather than the default from above
  208. */
  209. if (init_rcv_wnd)
  210. *rcv_wnd = min(*rcv_wnd, init_rcv_wnd * mss);
  211. else
  212. *rcv_wnd = min(*rcv_wnd, init_cwnd * mss);
  213. }
  214. /* Set the clamp no higher than max representable value */
  215. (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
  216. }
  217. EXPORT_SYMBOL(tcp_select_initial_window);
  218. /* Chose a new window to advertise, update state in tcp_sock for the
  219. * socket, and return result with RFC1323 scaling applied. The return
  220. * value can be stuffed directly into th->window for an outgoing
  221. * frame.
  222. */
  223. static u16 tcp_select_window(struct sock *sk)
  224. {
  225. struct tcp_sock *tp = tcp_sk(sk);
  226. u32 cur_win = tcp_receive_window(tp);
  227. u32 new_win = __tcp_select_window(sk);
  228. /* Never shrink the offered window */
  229. if (new_win < cur_win) {
  230. /* Danger Will Robinson!
  231. * Don't update rcv_wup/rcv_wnd here or else
  232. * we will not be able to advertise a zero
  233. * window in time. --DaveM
  234. *
  235. * Relax Will Robinson.
  236. */
  237. new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale);
  238. }
  239. tp->rcv_wnd = new_win;
  240. tp->rcv_wup = tp->rcv_nxt;
  241. /* Make sure we do not exceed the maximum possible
  242. * scaled window.
  243. */
  244. if (!tp->rx_opt.rcv_wscale && sysctl_tcp_workaround_signed_windows)
  245. new_win = min(new_win, MAX_TCP_WINDOW);
  246. else
  247. new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
  248. /* RFC1323 scaling applied */
  249. new_win >>= tp->rx_opt.rcv_wscale;
  250. /* If we advertise zero window, disable fast path. */
  251. if (new_win == 0)
  252. tp->pred_flags = 0;
  253. return new_win;
  254. }
  255. /* Packet ECN state for a SYN-ACK */
  256. static inline void TCP_ECN_send_synack(const struct tcp_sock *tp, struct sk_buff *skb)
  257. {
  258. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR;
  259. if (!(tp->ecn_flags & TCP_ECN_OK))
  260. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE;
  261. }
  262. /* Packet ECN state for a SYN. */
  263. static inline void TCP_ECN_send_syn(struct sock *sk, struct sk_buff *skb)
  264. {
  265. struct tcp_sock *tp = tcp_sk(sk);
  266. tp->ecn_flags = 0;
  267. if (sock_net(sk)->ipv4.sysctl_tcp_ecn == 1) {
  268. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR;
  269. tp->ecn_flags = TCP_ECN_OK;
  270. }
  271. }
  272. static __inline__ void
  273. TCP_ECN_make_synack(const struct request_sock *req, struct tcphdr *th)
  274. {
  275. if (inet_rsk(req)->ecn_ok)
  276. th->ece = 1;
  277. }
  278. /* Set up ECN state for a packet on a ESTABLISHED socket that is about to
  279. * be sent.
  280. */
  281. static inline void TCP_ECN_send(struct sock *sk, struct sk_buff *skb,
  282. int tcp_header_len)
  283. {
  284. struct tcp_sock *tp = tcp_sk(sk);
  285. if (tp->ecn_flags & TCP_ECN_OK) {
  286. /* Not-retransmitted data segment: set ECT and inject CWR. */
  287. if (skb->len != tcp_header_len &&
  288. !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {
  289. INET_ECN_xmit(sk);
  290. if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) {
  291. tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
  292. tcp_hdr(skb)->cwr = 1;
  293. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  294. }
  295. } else {
  296. /* ACK or retransmitted segment: clear ECT|CE */
  297. INET_ECN_dontxmit(sk);
  298. }
  299. if (tp->ecn_flags & TCP_ECN_DEMAND_CWR)
  300. tcp_hdr(skb)->ece = 1;
  301. }
  302. }
  303. /* Constructs common control bits of non-data skb. If SYN/FIN is present,
  304. * auto increment end seqno.
  305. */
  306. static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
  307. {
  308. skb->ip_summed = CHECKSUM_PARTIAL;
  309. skb->csum = 0;
  310. TCP_SKB_CB(skb)->tcp_flags = flags;
  311. TCP_SKB_CB(skb)->sacked = 0;
  312. skb_shinfo(skb)->gso_segs = 1;
  313. skb_shinfo(skb)->gso_size = 0;
  314. skb_shinfo(skb)->gso_type = 0;
  315. TCP_SKB_CB(skb)->seq = seq;
  316. if (flags & (TCPHDR_SYN | TCPHDR_FIN))
  317. seq++;
  318. TCP_SKB_CB(skb)->end_seq = seq;
  319. }
  320. static inline bool tcp_urg_mode(const struct tcp_sock *tp)
  321. {
  322. return tp->snd_una != tp->snd_up;
  323. }
  324. #define OPTION_SACK_ADVERTISE (1 << 0)
  325. #define OPTION_TS (1 << 1)
  326. #define OPTION_MD5 (1 << 2)
  327. #define OPTION_WSCALE (1 << 3)
  328. #define OPTION_FAST_OPEN_COOKIE (1 << 8)
  329. struct tcp_out_options {
  330. u16 options; /* bit field of OPTION_* */
  331. u16 mss; /* 0 to disable */
  332. u8 ws; /* window scale, 0 to disable */
  333. u8 num_sack_blocks; /* number of SACK blocks to include */
  334. u8 hash_size; /* bytes in hash_location */
  335. __u8 *hash_location; /* temporary pointer, overloaded */
  336. __u32 tsval, tsecr; /* need to include OPTION_TS */
  337. struct tcp_fastopen_cookie *fastopen_cookie; /* Fast open cookie */
  338. };
  339. /* Write previously computed TCP options to the packet.
  340. *
  341. * Beware: Something in the Internet is very sensitive to the ordering of
  342. * TCP options, we learned this through the hard way, so be careful here.
  343. * Luckily we can at least blame others for their non-compliance but from
  344. * inter-operatibility perspective it seems that we're somewhat stuck with
  345. * the ordering which we have been using if we want to keep working with
  346. * those broken things (not that it currently hurts anybody as there isn't
  347. * particular reason why the ordering would need to be changed).
  348. *
  349. * At least SACK_PERM as the first option is known to lead to a disaster
  350. * (but it may well be that other scenarios fail similarly).
  351. */
  352. static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
  353. struct tcp_out_options *opts)
  354. {
  355. u16 options = opts->options; /* mungable copy */
  356. if (unlikely(OPTION_MD5 & options)) {
  357. *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  358. (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  359. /* overload cookie hash location */
  360. opts->hash_location = (__u8 *)ptr;
  361. ptr += 4;
  362. }
  363. if (unlikely(opts->mss)) {
  364. *ptr++ = htonl((TCPOPT_MSS << 24) |
  365. (TCPOLEN_MSS << 16) |
  366. opts->mss);
  367. }
  368. if (likely(OPTION_TS & options)) {
  369. if (unlikely(OPTION_SACK_ADVERTISE & options)) {
  370. *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
  371. (TCPOLEN_SACK_PERM << 16) |
  372. (TCPOPT_TIMESTAMP << 8) |
  373. TCPOLEN_TIMESTAMP);
  374. options &= ~OPTION_SACK_ADVERTISE;
  375. } else {
  376. *ptr++ = htonl((TCPOPT_NOP << 24) |
  377. (TCPOPT_NOP << 16) |
  378. (TCPOPT_TIMESTAMP << 8) |
  379. TCPOLEN_TIMESTAMP);
  380. }
  381. *ptr++ = htonl(opts->tsval);
  382. *ptr++ = htonl(opts->tsecr);
  383. }
  384. if (unlikely(OPTION_SACK_ADVERTISE & options)) {
  385. *ptr++ = htonl((TCPOPT_NOP << 24) |
  386. (TCPOPT_NOP << 16) |
  387. (TCPOPT_SACK_PERM << 8) |
  388. TCPOLEN_SACK_PERM);
  389. }
  390. if (unlikely(OPTION_WSCALE & options)) {
  391. *ptr++ = htonl((TCPOPT_NOP << 24) |
  392. (TCPOPT_WINDOW << 16) |
  393. (TCPOLEN_WINDOW << 8) |
  394. opts->ws);
  395. }
  396. if (unlikely(opts->num_sack_blocks)) {
  397. struct tcp_sack_block *sp = tp->rx_opt.dsack ?
  398. tp->duplicate_sack : tp->selective_acks;
  399. int this_sack;
  400. *ptr++ = htonl((TCPOPT_NOP << 24) |
  401. (TCPOPT_NOP << 16) |
  402. (TCPOPT_SACK << 8) |
  403. (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
  404. TCPOLEN_SACK_PERBLOCK)));
  405. for (this_sack = 0; this_sack < opts->num_sack_blocks;
  406. ++this_sack) {
  407. *ptr++ = htonl(sp[this_sack].start_seq);
  408. *ptr++ = htonl(sp[this_sack].end_seq);
  409. }
  410. tp->rx_opt.dsack = 0;
  411. }
  412. if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) {
  413. struct tcp_fastopen_cookie *foc = opts->fastopen_cookie;
  414. *ptr++ = htonl((TCPOPT_EXP << 24) |
  415. ((TCPOLEN_EXP_FASTOPEN_BASE + foc->len) << 16) |
  416. TCPOPT_FASTOPEN_MAGIC);
  417. memcpy(ptr, foc->val, foc->len);
  418. if ((foc->len & 3) == 2) {
  419. u8 *align = ((u8 *)ptr) + foc->len;
  420. align[0] = align[1] = TCPOPT_NOP;
  421. }
  422. ptr += (foc->len + 3) >> 2;
  423. }
  424. }
  425. /* Compute TCP options for SYN packets. This is not the final
  426. * network wire format yet.
  427. */
  428. static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
  429. struct tcp_out_options *opts,
  430. struct tcp_md5sig_key **md5)
  431. {
  432. struct tcp_sock *tp = tcp_sk(sk);
  433. unsigned int remaining = MAX_TCP_OPTION_SPACE;
  434. struct tcp_fastopen_request *fastopen = tp->fastopen_req;
  435. #ifdef CONFIG_TCP_MD5SIG
  436. *md5 = tp->af_specific->md5_lookup(sk, sk);
  437. if (*md5) {
  438. opts->options |= OPTION_MD5;
  439. remaining -= TCPOLEN_MD5SIG_ALIGNED;
  440. }
  441. #else
  442. *md5 = NULL;
  443. #endif
  444. /* We always get an MSS option. The option bytes which will be seen in
  445. * normal data packets should timestamps be used, must be in the MSS
  446. * advertised. But we subtract them from tp->mss_cache so that
  447. * calculations in tcp_sendmsg are simpler etc. So account for this
  448. * fact here if necessary. If we don't do this correctly, as a
  449. * receiver we won't recognize data packets as being full sized when we
  450. * should, and thus we won't abide by the delayed ACK rules correctly.
  451. * SACKs don't matter, we never delay an ACK when we have any of those
  452. * going out. */
  453. opts->mss = tcp_advertise_mss(sk);
  454. remaining -= TCPOLEN_MSS_ALIGNED;
  455. if (likely(sysctl_tcp_timestamps && *md5 == NULL)) {
  456. opts->options |= OPTION_TS;
  457. opts->tsval = TCP_SKB_CB(skb)->when + tp->tsoffset;
  458. opts->tsecr = tp->rx_opt.ts_recent;
  459. remaining -= TCPOLEN_TSTAMP_ALIGNED;
  460. }
  461. if (likely(sysctl_tcp_window_scaling)) {
  462. opts->ws = tp->rx_opt.rcv_wscale;
  463. opts->options |= OPTION_WSCALE;
  464. remaining -= TCPOLEN_WSCALE_ALIGNED;
  465. }
  466. if (likely(sysctl_tcp_sack)) {
  467. opts->options |= OPTION_SACK_ADVERTISE;
  468. if (unlikely(!(OPTION_TS & opts->options)))
  469. remaining -= TCPOLEN_SACKPERM_ALIGNED;
  470. }
  471. if (fastopen && fastopen->cookie.len >= 0) {
  472. u32 need = TCPOLEN_EXP_FASTOPEN_BASE + fastopen->cookie.len;
  473. need = (need + 3) & ~3U; /* Align to 32 bits */
  474. if (remaining >= need) {
  475. opts->options |= OPTION_FAST_OPEN_COOKIE;
  476. opts->fastopen_cookie = &fastopen->cookie;
  477. remaining -= need;
  478. tp->syn_fastopen = 1;
  479. }
  480. }
  481. return MAX_TCP_OPTION_SPACE - remaining;
  482. }
  483. /* Set up TCP options for SYN-ACKs. */
  484. static unsigned int tcp_synack_options(struct sock *sk,
  485. struct request_sock *req,
  486. unsigned int mss, struct sk_buff *skb,
  487. struct tcp_out_options *opts,
  488. struct tcp_md5sig_key **md5,
  489. struct tcp_fastopen_cookie *foc)
  490. {
  491. struct inet_request_sock *ireq = inet_rsk(req);
  492. unsigned int remaining = MAX_TCP_OPTION_SPACE;
  493. #ifdef CONFIG_TCP_MD5SIG
  494. *md5 = tcp_rsk(req)->af_specific->md5_lookup(sk, req);
  495. if (*md5) {
  496. opts->options |= OPTION_MD5;
  497. remaining -= TCPOLEN_MD5SIG_ALIGNED;
  498. /* We can't fit any SACK blocks in a packet with MD5 + TS
  499. * options. There was discussion about disabling SACK
  500. * rather than TS in order to fit in better with old,
  501. * buggy kernels, but that was deemed to be unnecessary.
  502. */
  503. ireq->tstamp_ok &= !ireq->sack_ok;
  504. }
  505. #else
  506. *md5 = NULL;
  507. #endif
  508. /* We always send an MSS option. */
  509. opts->mss = mss;
  510. remaining -= TCPOLEN_MSS_ALIGNED;
  511. if (likely(ireq->wscale_ok)) {
  512. opts->ws = ireq->rcv_wscale;
  513. opts->options |= OPTION_WSCALE;
  514. remaining -= TCPOLEN_WSCALE_ALIGNED;
  515. }
  516. if (likely(ireq->tstamp_ok)) {
  517. opts->options |= OPTION_TS;
  518. opts->tsval = TCP_SKB_CB(skb)->when;
  519. opts->tsecr = req->ts_recent;
  520. remaining -= TCPOLEN_TSTAMP_ALIGNED;
  521. }
  522. if (likely(ireq->sack_ok)) {
  523. opts->options |= OPTION_SACK_ADVERTISE;
  524. if (unlikely(!ireq->tstamp_ok))
  525. remaining -= TCPOLEN_SACKPERM_ALIGNED;
  526. }
  527. if (foc != NULL) {
  528. u32 need = TCPOLEN_EXP_FASTOPEN_BASE + foc->len;
  529. need = (need + 3) & ~3U; /* Align to 32 bits */
  530. if (remaining >= need) {
  531. opts->options |= OPTION_FAST_OPEN_COOKIE;
  532. opts->fastopen_cookie = foc;
  533. remaining -= need;
  534. }
  535. }
  536. return MAX_TCP_OPTION_SPACE - remaining;
  537. }
  538. /* Compute TCP options for ESTABLISHED sockets. This is not the
  539. * final wire format yet.
  540. */
  541. static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb,
  542. struct tcp_out_options *opts,
  543. struct tcp_md5sig_key **md5)
  544. {
  545. struct tcp_skb_cb *tcb = skb ? TCP_SKB_CB(skb) : NULL;
  546. struct tcp_sock *tp = tcp_sk(sk);
  547. unsigned int size = 0;
  548. unsigned int eff_sacks;
  549. #ifdef CONFIG_TCP_MD5SIG
  550. *md5 = tp->af_specific->md5_lookup(sk, sk);
  551. if (unlikely(*md5)) {
  552. opts->options |= OPTION_MD5;
  553. size += TCPOLEN_MD5SIG_ALIGNED;
  554. }
  555. #else
  556. *md5 = NULL;
  557. #endif
  558. if (likely(tp->rx_opt.tstamp_ok)) {
  559. opts->options |= OPTION_TS;
  560. opts->tsval = tcb ? tcb->when + tp->tsoffset : 0;
  561. opts->tsecr = tp->rx_opt.ts_recent;
  562. size += TCPOLEN_TSTAMP_ALIGNED;
  563. }
  564. eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;
  565. if (unlikely(eff_sacks)) {
  566. const unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
  567. opts->num_sack_blocks =
  568. min_t(unsigned int, eff_sacks,
  569. (remaining - TCPOLEN_SACK_BASE_ALIGNED) /
  570. TCPOLEN_SACK_PERBLOCK);
  571. size += TCPOLEN_SACK_BASE_ALIGNED +
  572. opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK;
  573. }
  574. return size;
  575. }
  576. /* TCP SMALL QUEUES (TSQ)
  577. *
  578. * TSQ goal is to keep small amount of skbs per tcp flow in tx queues (qdisc+dev)
  579. * to reduce RTT and bufferbloat.
  580. * We do this using a special skb destructor (tcp_wfree).
  581. *
  582. * Its important tcp_wfree() can be replaced by sock_wfree() in the event skb
  583. * needs to be reallocated in a driver.
  584. * The invariant being skb->truesize substracted from sk->sk_wmem_alloc
  585. *
  586. * Since transmit from skb destructor is forbidden, we use a tasklet
  587. * to process all sockets that eventually need to send more skbs.
  588. * We use one tasklet per cpu, with its own queue of sockets.
  589. */
  590. struct tsq_tasklet {
  591. struct tasklet_struct tasklet;
  592. struct list_head head; /* queue of tcp sockets */
  593. };
  594. static DEFINE_PER_CPU(struct tsq_tasklet, tsq_tasklet);
  595. static void tcp_tsq_handler(struct sock *sk)
  596. {
  597. if ((1 << sk->sk_state) &
  598. (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_CLOSING |
  599. TCPF_CLOSE_WAIT | TCPF_LAST_ACK))
  600. tcp_write_xmit(sk, tcp_current_mss(sk), 0, 0, GFP_ATOMIC);
  601. }
  602. /*
  603. * One tasklest per cpu tries to send more skbs.
  604. * We run in tasklet context but need to disable irqs when
  605. * transfering tsq->head because tcp_wfree() might
  606. * interrupt us (non NAPI drivers)
  607. */
  608. static void tcp_tasklet_func(unsigned long data)
  609. {
  610. struct tsq_tasklet *tsq = (struct tsq_tasklet *)data;
  611. LIST_HEAD(list);
  612. unsigned long flags;
  613. struct list_head *q, *n;
  614. struct tcp_sock *tp;
  615. struct sock *sk;
  616. local_irq_save(flags);
  617. list_splice_init(&tsq->head, &list);
  618. local_irq_restore(flags);
  619. list_for_each_safe(q, n, &list) {
  620. tp = list_entry(q, struct tcp_sock, tsq_node);
  621. list_del(&tp->tsq_node);
  622. sk = (struct sock *)tp;
  623. bh_lock_sock(sk);
  624. if (!sock_owned_by_user(sk)) {
  625. tcp_tsq_handler(sk);
  626. } else {
  627. /* defer the work to tcp_release_cb() */
  628. set_bit(TCP_TSQ_DEFERRED, &tp->tsq_flags);
  629. }
  630. bh_unlock_sock(sk);
  631. clear_bit(TSQ_QUEUED, &tp->tsq_flags);
  632. sk_free(sk);
  633. }
  634. }
  635. #define TCP_DEFERRED_ALL ((1UL << TCP_TSQ_DEFERRED) | \
  636. (1UL << TCP_WRITE_TIMER_DEFERRED) | \
  637. (1UL << TCP_DELACK_TIMER_DEFERRED) | \
  638. (1UL << TCP_MTU_REDUCED_DEFERRED))
  639. /**
  640. * tcp_release_cb - tcp release_sock() callback
  641. * @sk: socket
  642. *
  643. * called from release_sock() to perform protocol dependent
  644. * actions before socket release.
  645. */
  646. void tcp_release_cb(struct sock *sk)
  647. {
  648. struct tcp_sock *tp = tcp_sk(sk);
  649. unsigned long flags, nflags;
  650. /* perform an atomic operation only if at least one flag is set */
  651. do {
  652. flags = tp->tsq_flags;
  653. if (!(flags & TCP_DEFERRED_ALL))
  654. return;
  655. nflags = flags & ~TCP_DEFERRED_ALL;
  656. } while (cmpxchg(&tp->tsq_flags, flags, nflags) != flags);
  657. if (flags & (1UL << TCP_TSQ_DEFERRED))
  658. tcp_tsq_handler(sk);
  659. if (flags & (1UL << TCP_WRITE_TIMER_DEFERRED)) {
  660. tcp_write_timer_handler(sk);
  661. __sock_put(sk);
  662. }
  663. if (flags & (1UL << TCP_DELACK_TIMER_DEFERRED)) {
  664. tcp_delack_timer_handler(sk);
  665. __sock_put(sk);
  666. }
  667. if (flags & (1UL << TCP_MTU_REDUCED_DEFERRED)) {
  668. sk->sk_prot->mtu_reduced(sk);
  669. __sock_put(sk);
  670. }
  671. }
  672. EXPORT_SYMBOL(tcp_release_cb);
  673. void __init tcp_tasklet_init(void)
  674. {
  675. int i;
  676. for_each_possible_cpu(i) {
  677. struct tsq_tasklet *tsq = &per_cpu(tsq_tasklet, i);
  678. INIT_LIST_HEAD(&tsq->head);
  679. tasklet_init(&tsq->tasklet,
  680. tcp_tasklet_func,
  681. (unsigned long)tsq);
  682. }
  683. }
  684. /*
  685. * Write buffer destructor automatically called from kfree_skb.
  686. * We cant xmit new skbs from this context, as we might already
  687. * hold qdisc lock.
  688. */
  689. static void tcp_wfree(struct sk_buff *skb)
  690. {
  691. struct sock *sk = skb->sk;
  692. struct tcp_sock *tp = tcp_sk(sk);
  693. if (test_and_clear_bit(TSQ_THROTTLED, &tp->tsq_flags) &&
  694. !test_and_set_bit(TSQ_QUEUED, &tp->tsq_flags)) {
  695. unsigned long flags;
  696. struct tsq_tasklet *tsq;
  697. /* Keep a ref on socket.
  698. * This last ref will be released in tcp_tasklet_func()
  699. */
  700. atomic_sub(skb->truesize - 1, &sk->sk_wmem_alloc);
  701. /* queue this socket to tasklet queue */
  702. local_irq_save(flags);
  703. tsq = &__get_cpu_var(tsq_tasklet);
  704. list_add(&tp->tsq_node, &tsq->head);
  705. tasklet_schedule(&tsq->tasklet);
  706. local_irq_restore(flags);
  707. } else {
  708. sock_wfree(skb);
  709. }
  710. }
  711. /* This routine actually transmits TCP packets queued in by
  712. * tcp_do_sendmsg(). This is used by both the initial
  713. * transmission and possible later retransmissions.
  714. * All SKB's seen here are completely headerless. It is our
  715. * job to build the TCP header, and pass the packet down to
  716. * IP so it can do the same plus pass the packet off to the
  717. * device.
  718. *
  719. * We are working here with either a clone of the original
  720. * SKB, or a fresh unique copy made by the retransmit engine.
  721. */
  722. static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
  723. gfp_t gfp_mask)
  724. {
  725. const struct inet_connection_sock *icsk = inet_csk(sk);
  726. struct inet_sock *inet;
  727. struct tcp_sock *tp;
  728. struct tcp_skb_cb *tcb;
  729. struct tcp_out_options opts;
  730. unsigned int tcp_options_size, tcp_header_size;
  731. struct tcp_md5sig_key *md5;
  732. struct tcphdr *th;
  733. int err;
  734. BUG_ON(!skb || !tcp_skb_pcount(skb));
  735. /* If congestion control is doing timestamping, we must
  736. * take such a timestamp before we potentially clone/copy.
  737. */
  738. if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
  739. __net_timestamp(skb);
  740. if (likely(clone_it)) {
  741. if (unlikely(skb_cloned(skb)))
  742. skb = pskb_copy(skb, gfp_mask);
  743. else
  744. skb = skb_clone(skb, gfp_mask);
  745. if (unlikely(!skb))
  746. return -ENOBUFS;
  747. }
  748. inet = inet_sk(sk);
  749. tp = tcp_sk(sk);
  750. tcb = TCP_SKB_CB(skb);
  751. memset(&opts, 0, sizeof(opts));
  752. if (unlikely(tcb->tcp_flags & TCPHDR_SYN))
  753. tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5);
  754. else
  755. tcp_options_size = tcp_established_options(sk, skb, &opts,
  756. &md5);
  757. tcp_header_size = tcp_options_size + sizeof(struct tcphdr);
  758. if (tcp_packets_in_flight(tp) == 0) {
  759. tcp_ca_event(sk, CA_EVENT_TX_START);
  760. skb->ooo_okay = 1;
  761. } else
  762. skb->ooo_okay = 0;
  763. skb_push(skb, tcp_header_size);
  764. skb_reset_transport_header(skb);
  765. skb_orphan(skb);
  766. skb->sk = sk;
  767. skb->destructor = (sysctl_tcp_limit_output_bytes > 0) ?
  768. tcp_wfree : sock_wfree;
  769. atomic_add(skb->truesize, &sk->sk_wmem_alloc);
  770. /* Build TCP header and checksum it. */
  771. th = tcp_hdr(skb);
  772. th->source = inet->inet_sport;
  773. th->dest = inet->inet_dport;
  774. th->seq = htonl(tcb->seq);
  775. th->ack_seq = htonl(tp->rcv_nxt);
  776. *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) |
  777. tcb->tcp_flags);
  778. if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
  779. /* RFC1323: The window in SYN & SYN/ACK segments
  780. * is never scaled.
  781. */
  782. th->window = htons(min(tp->rcv_wnd, 65535U));
  783. } else {
  784. th->window = htons(tcp_select_window(sk));
  785. }
  786. th->check = 0;
  787. th->urg_ptr = 0;
  788. /* The urg_mode check is necessary during a below snd_una win probe */
  789. if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) {
  790. if (before(tp->snd_up, tcb->seq + 0x10000)) {
  791. th->urg_ptr = htons(tp->snd_up - tcb->seq);
  792. th->urg = 1;
  793. } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) {
  794. th->urg_ptr = htons(0xFFFF);
  795. th->urg = 1;
  796. }
  797. }
  798. tcp_options_write((__be32 *)(th + 1), tp, &opts);
  799. if (likely((tcb->tcp_flags & TCPHDR_SYN) == 0))
  800. TCP_ECN_send(sk, skb, tcp_header_size);
  801. #ifdef CONFIG_TCP_MD5SIG
  802. /* Calculate the MD5 hash, as we have all we need now */
  803. if (md5) {
  804. sk_nocaps_add(sk, NETIF_F_GSO_MASK);
  805. tp->af_specific->calc_md5_hash(opts.hash_location,
  806. md5, sk, NULL, skb);
  807. }
  808. #endif
  809. icsk->icsk_af_ops->send_check(sk, skb);
  810. if (likely(tcb->tcp_flags & TCPHDR_ACK))
  811. tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
  812. if (skb->len != tcp_header_size)
  813. tcp_event_data_sent(tp, sk);
  814. if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
  815. TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS,
  816. tcp_skb_pcount(skb));
  817. err = icsk->icsk_af_ops->queue_xmit(skb, &inet->cork.fl);
  818. if (likely(err <= 0))
  819. return err;
  820. tcp_enter_cwr(sk, 1);
  821. return net_xmit_eval(err);
  822. }
  823. /* This routine just queues the buffer for sending.
  824. *
  825. * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
  826. * otherwise socket can stall.
  827. */
  828. static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
  829. {
  830. struct tcp_sock *tp = tcp_sk(sk);
  831. /* Advance write_seq and place onto the write_queue. */
  832. tp->write_seq = TCP_SKB_CB(skb)->end_seq;
  833. skb_header_release(skb);
  834. tcp_add_write_queue_tail(sk, skb);
  835. sk->sk_wmem_queued += skb->truesize;
  836. sk_mem_charge(sk, skb->truesize);
  837. }
  838. /* Initialize TSO segments for a packet. */
  839. static void tcp_set_skb_tso_segs(const struct sock *sk, struct sk_buff *skb,
  840. unsigned int mss_now)
  841. {
  842. if (skb->len <= mss_now || !sk_can_gso(sk) ||
  843. skb->ip_summed == CHECKSUM_NONE) {
  844. /* Avoid the costly divide in the normal
  845. * non-TSO case.
  846. */
  847. skb_shinfo(skb)->gso_segs = 1;
  848. skb_shinfo(skb)->gso_size = 0;
  849. skb_shinfo(skb)->gso_type = 0;
  850. } else {
  851. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss_now);
  852. skb_shinfo(skb)->gso_size = mss_now;
  853. skb_shinfo(skb)->gso_type = sk->sk_gso_type;
  854. }
  855. }
  856. /* When a modification to fackets out becomes necessary, we need to check
  857. * skb is counted to fackets_out or not.
  858. */
  859. static void tcp_adjust_fackets_out(struct sock *sk, const struct sk_buff *skb,
  860. int decr)
  861. {
  862. struct tcp_sock *tp = tcp_sk(sk);
  863. if (!tp->sacked_out || tcp_is_reno(tp))
  864. return;
  865. if (after(tcp_highest_sack_seq(tp), TCP_SKB_CB(skb)->seq))
  866. tp->fackets_out -= decr;
  867. }
  868. /* Pcount in the middle of the write queue got changed, we need to do various
  869. * tweaks to fix counters
  870. */
  871. static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr)
  872. {
  873. struct tcp_sock *tp = tcp_sk(sk);
  874. tp->packets_out -= decr;
  875. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
  876. tp->sacked_out -= decr;
  877. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
  878. tp->retrans_out -= decr;
  879. if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
  880. tp->lost_out -= decr;
  881. /* Reno case is special. Sigh... */
  882. if (tcp_is_reno(tp) && decr > 0)
  883. tp->sacked_out -= min_t(u32, tp->sacked_out, decr);
  884. tcp_adjust_fackets_out(sk, skb, decr);
  885. if (tp->lost_skb_hint &&
  886. before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) &&
  887. (tcp_is_fack(tp) || (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)))
  888. tp->lost_cnt_hint -= decr;
  889. tcp_verify_left_out(tp);
  890. }
  891. /* Function to create two new TCP segments. Shrinks the given segment
  892. * to the specified size and appends a new segment with the rest of the
  893. * packet to the list. This won't be called frequently, I hope.
  894. * Remember, these are still headerless SKBs at this point.
  895. */
  896. int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
  897. unsigned int mss_now)
  898. {
  899. struct tcp_sock *tp = tcp_sk(sk);
  900. struct sk_buff *buff;
  901. int nsize, old_factor;
  902. int nlen;
  903. u8 flags;
  904. if (WARN_ON(len > skb->len))
  905. return -EINVAL;
  906. nsize = skb_headlen(skb) - len;
  907. if (nsize < 0)
  908. nsize = 0;
  909. if (skb_cloned(skb) &&
  910. skb_is_nonlinear(skb) &&
  911. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  912. return -ENOMEM;
  913. /* Get a new skb... force flag on. */
  914. buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
  915. if (buff == NULL)
  916. return -ENOMEM; /* We'll just try again later. */
  917. sk->sk_wmem_queued += buff->truesize;
  918. sk_mem_charge(sk, buff->truesize);
  919. nlen = skb->len - len - nsize;
  920. buff->truesize += nlen;
  921. skb->truesize -= nlen;
  922. /* Correct the sequence numbers. */
  923. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  924. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  925. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  926. /* PSH and FIN should only be set in the second packet. */
  927. flags = TCP_SKB_CB(skb)->tcp_flags;
  928. TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
  929. TCP_SKB_CB(buff)->tcp_flags = flags;
  930. TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
  931. if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
  932. /* Copy and checksum data tail into the new buffer. */
  933. buff->csum = csum_partial_copy_nocheck(skb->data + len,
  934. skb_put(buff, nsize),
  935. nsize, 0);
  936. skb_trim(skb, len);
  937. skb->csum = csum_block_sub(skb->csum, buff->csum, len);
  938. } else {
  939. skb->ip_summed = CHECKSUM_PARTIAL;
  940. skb_split(skb, buff, len);
  941. }
  942. buff->ip_summed = skb->ip_summed;
  943. /* Looks stupid, but our code really uses when of
  944. * skbs, which it never sent before. --ANK
  945. */
  946. TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
  947. buff->tstamp = skb->tstamp;
  948. old_factor = tcp_skb_pcount(skb);
  949. /* Fix up tso_factor for both original and new SKB. */
  950. tcp_set_skb_tso_segs(sk, skb, mss_now);
  951. tcp_set_skb_tso_segs(sk, buff, mss_now);
  952. /* If this packet has been sent out already, we must
  953. * adjust the various packet counters.
  954. */
  955. if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
  956. int diff = old_factor - tcp_skb_pcount(skb) -
  957. tcp_skb_pcount(buff);
  958. if (diff)
  959. tcp_adjust_pcount(sk, skb, diff);
  960. }
  961. /* Link BUFF into the send queue. */
  962. skb_header_release(buff);
  963. tcp_insert_write_queue_after(skb, buff, sk);
  964. return 0;
  965. }
  966. /* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
  967. * eventually). The difference is that pulled data not copied, but
  968. * immediately discarded.
  969. */
  970. static void __pskb_trim_head(struct sk_buff *skb, int len)
  971. {
  972. int i, k, eat;
  973. eat = min_t(int, len, skb_headlen(skb));
  974. if (eat) {
  975. __skb_pull(skb, eat);
  976. len -= eat;
  977. if (!len)
  978. return;
  979. }
  980. eat = len;
  981. k = 0;
  982. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  983. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  984. if (size <= eat) {
  985. skb_frag_unref(skb, i);
  986. eat -= size;
  987. } else {
  988. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  989. if (eat) {
  990. skb_shinfo(skb)->frags[k].page_offset += eat;
  991. skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
  992. eat = 0;
  993. }
  994. k++;
  995. }
  996. }
  997. skb_shinfo(skb)->nr_frags = k;
  998. skb_reset_tail_pointer(skb);
  999. skb->data_len -= len;
  1000. skb->len = skb->data_len;
  1001. }
  1002. /* Remove acked data from a packet in the transmit queue. */
  1003. int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
  1004. {
  1005. if (skb_unclone(skb, GFP_ATOMIC))
  1006. return -ENOMEM;
  1007. __pskb_trim_head(skb, len);
  1008. TCP_SKB_CB(skb)->seq += len;
  1009. skb->ip_summed = CHECKSUM_PARTIAL;
  1010. skb->truesize -= len;
  1011. sk->sk_wmem_queued -= len;
  1012. sk_mem_uncharge(sk, len);
  1013. sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
  1014. /* Any change of skb->len requires recalculation of tso factor. */
  1015. if (tcp_skb_pcount(skb) > 1)
  1016. tcp_set_skb_tso_segs(sk, skb, tcp_skb_mss(skb));
  1017. return 0;
  1018. }
  1019. /* Calculate MSS not accounting any TCP options. */
  1020. static inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu)
  1021. {
  1022. const struct tcp_sock *tp = tcp_sk(sk);
  1023. const struct inet_connection_sock *icsk = inet_csk(sk);
  1024. int mss_now;
  1025. /* Calculate base mss without TCP options:
  1026. It is MMS_S - sizeof(tcphdr) of rfc1122
  1027. */
  1028. mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
  1029. /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
  1030. if (icsk->icsk_af_ops->net_frag_header_len) {
  1031. const struct dst_entry *dst = __sk_dst_get(sk);
  1032. if (dst && dst_allfrag(dst))
  1033. mss_now -= icsk->icsk_af_ops->net_frag_header_len;
  1034. }
  1035. /* Clamp it (mss_clamp does not include tcp options) */
  1036. if (mss_now > tp->rx_opt.mss_clamp)
  1037. mss_now = tp->rx_opt.mss_clamp;
  1038. /* Now subtract optional transport overhead */
  1039. mss_now -= icsk->icsk_ext_hdr_len;
  1040. /* Then reserve room for full set of TCP options and 8 bytes of data */
  1041. if (mss_now < 48)
  1042. mss_now = 48;
  1043. return mss_now;
  1044. }
  1045. /* Calculate MSS. Not accounting for SACKs here. */
  1046. int tcp_mtu_to_mss(struct sock *sk, int pmtu)
  1047. {
  1048. /* Subtract TCP options size, not including SACKs */
  1049. return __tcp_mtu_to_mss(sk, pmtu) -
  1050. (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr));
  1051. }
  1052. /* Inverse of above */
  1053. int tcp_mss_to_mtu(struct sock *sk, int mss)
  1054. {
  1055. const struct tcp_sock *tp = tcp_sk(sk);
  1056. const struct inet_connection_sock *icsk = inet_csk(sk);
  1057. int mtu;
  1058. mtu = mss +
  1059. tp->tcp_header_len +
  1060. icsk->icsk_ext_hdr_len +
  1061. icsk->icsk_af_ops->net_header_len;
  1062. /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
  1063. if (icsk->icsk_af_ops->net_frag_header_len) {
  1064. const struct dst_entry *dst = __sk_dst_get(sk);
  1065. if (dst && dst_allfrag(dst))
  1066. mtu += icsk->icsk_af_ops->net_frag_header_len;
  1067. }
  1068. return mtu;
  1069. }
  1070. /* MTU probing init per socket */
  1071. void tcp_mtup_init(struct sock *sk)
  1072. {
  1073. struct tcp_sock *tp = tcp_sk(sk);
  1074. struct inet_connection_sock *icsk = inet_csk(sk);
  1075. icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
  1076. icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
  1077. icsk->icsk_af_ops->net_header_len;
  1078. icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
  1079. icsk->icsk_mtup.probe_size = 0;
  1080. }
  1081. EXPORT_SYMBOL(tcp_mtup_init);
  1082. /* This function synchronize snd mss to current pmtu/exthdr set.
  1083. tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
  1084. for TCP options, but includes only bare TCP header.
  1085. tp->rx_opt.mss_clamp is mss negotiated at connection setup.
  1086. It is minimum of user_mss and mss received with SYN.
  1087. It also does not include TCP options.
  1088. inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
  1089. tp->mss_cache is current effective sending mss, including
  1090. all tcp options except for SACKs. It is evaluated,
  1091. taking into account current pmtu, but never exceeds
  1092. tp->rx_opt.mss_clamp.
  1093. NOTE1. rfc1122 clearly states that advertised MSS
  1094. DOES NOT include either tcp or ip options.
  1095. NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
  1096. are READ ONLY outside this function. --ANK (980731)
  1097. */
  1098. unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
  1099. {
  1100. struct tcp_sock *tp = tcp_sk(sk);
  1101. struct inet_connection_sock *icsk = inet_csk(sk);
  1102. int mss_now;
  1103. if (icsk->icsk_mtup.search_high > pmtu)
  1104. icsk->icsk_mtup.search_high = pmtu;
  1105. mss_now = tcp_mtu_to_mss(sk, pmtu);
  1106. mss_now = tcp_bound_to_half_wnd(tp, mss_now);
  1107. /* And store cached results */
  1108. icsk->icsk_pmtu_cookie = pmtu;
  1109. if (icsk->icsk_mtup.enabled)
  1110. mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
  1111. tp->mss_cache = mss_now;
  1112. return mss_now;
  1113. }
  1114. EXPORT_SYMBOL(tcp_sync_mss);
  1115. /* Compute the current effective MSS, taking SACKs and IP options,
  1116. * and even PMTU discovery events into account.
  1117. */
  1118. unsigned int tcp_current_mss(struct sock *sk)
  1119. {
  1120. const struct tcp_sock *tp = tcp_sk(sk);
  1121. const struct dst_entry *dst = __sk_dst_get(sk);
  1122. u32 mss_now;
  1123. unsigned int header_len;
  1124. struct tcp_out_options opts;
  1125. struct tcp_md5sig_key *md5;
  1126. mss_now = tp->mss_cache;
  1127. if (dst) {
  1128. u32 mtu = dst_mtu(dst);
  1129. if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
  1130. mss_now = tcp_sync_mss(sk, mtu);
  1131. }
  1132. header_len = tcp_established_options(sk, NULL, &opts, &md5) +
  1133. sizeof(struct tcphdr);
  1134. /* The mss_cache is sized based on tp->tcp_header_len, which assumes
  1135. * some common options. If this is an odd packet (because we have SACK
  1136. * blocks etc) then our calculated header_len will be different, and
  1137. * we have to adjust mss_now correspondingly */
  1138. if (header_len != tp->tcp_header_len) {
  1139. int delta = (int) header_len - tp->tcp_header_len;
  1140. mss_now -= delta;
  1141. }
  1142. return mss_now;
  1143. }
  1144. /* Congestion window validation. (RFC2861) */
  1145. static void tcp_cwnd_validate(struct sock *sk)
  1146. {
  1147. struct tcp_sock *tp = tcp_sk(sk);
  1148. if (tp->packets_out >= tp->snd_cwnd) {
  1149. /* Network is feed fully. */
  1150. tp->snd_cwnd_used = 0;
  1151. tp->snd_cwnd_stamp = tcp_time_stamp;
  1152. } else {
  1153. /* Network starves. */
  1154. if (tp->packets_out > tp->snd_cwnd_used)
  1155. tp->snd_cwnd_used = tp->packets_out;
  1156. if (sysctl_tcp_slow_start_after_idle &&
  1157. (s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
  1158. tcp_cwnd_application_limited(sk);
  1159. }
  1160. }
  1161. /* Returns the portion of skb which can be sent right away without
  1162. * introducing MSS oddities to segment boundaries. In rare cases where
  1163. * mss_now != mss_cache, we will request caller to create a small skb
  1164. * per input skb which could be mostly avoided here (if desired).
  1165. *
  1166. * We explicitly want to create a request for splitting write queue tail
  1167. * to a small skb for Nagle purposes while avoiding unnecessary modulos,
  1168. * thus all the complexity (cwnd_len is always MSS multiple which we
  1169. * return whenever allowed by the other factors). Basically we need the
  1170. * modulo only when the receiver window alone is the limiting factor or
  1171. * when we would be allowed to send the split-due-to-Nagle skb fully.
  1172. */
  1173. static unsigned int tcp_mss_split_point(const struct sock *sk, const struct sk_buff *skb,
  1174. unsigned int mss_now, unsigned int max_segs)
  1175. {
  1176. const struct tcp_sock *tp = tcp_sk(sk);
  1177. u32 needed, window, max_len;
  1178. window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
  1179. max_len = mss_now * max_segs;
  1180. if (likely(max_len <= window && skb != tcp_write_queue_tail(sk)))
  1181. return max_len;
  1182. needed = min(skb->len, window);
  1183. if (max_len <= needed)
  1184. return max_len;
  1185. return needed - needed % mss_now;
  1186. }
  1187. /* Can at least one segment of SKB be sent right now, according to the
  1188. * congestion window rules? If so, return how many segments are allowed.
  1189. */
  1190. static inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp,
  1191. const struct sk_buff *skb)
  1192. {
  1193. u32 in_flight, cwnd;
  1194. /* Don't be strict about the congestion window for the final FIN. */
  1195. if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
  1196. tcp_skb_pcount(skb) == 1)
  1197. return 1;
  1198. in_flight = tcp_packets_in_flight(tp);
  1199. cwnd = tp->snd_cwnd;
  1200. if (in_flight < cwnd)
  1201. return (cwnd - in_flight);
  1202. return 0;
  1203. }
  1204. /* Initialize TSO state of a skb.
  1205. * This must be invoked the first time we consider transmitting
  1206. * SKB onto the wire.
  1207. */
  1208. static int tcp_init_tso_segs(const struct sock *sk, struct sk_buff *skb,
  1209. unsigned int mss_now)
  1210. {
  1211. int tso_segs = tcp_skb_pcount(skb);
  1212. if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) {
  1213. tcp_set_skb_tso_segs(sk, skb, mss_now);
  1214. tso_segs = tcp_skb_pcount(skb);
  1215. }
  1216. return tso_segs;
  1217. }
  1218. /* Minshall's variant of the Nagle send check. */
  1219. static inline bool tcp_minshall_check(const struct tcp_sock *tp)
  1220. {
  1221. return after(tp->snd_sml, tp->snd_una) &&
  1222. !after(tp->snd_sml, tp->snd_nxt);
  1223. }
  1224. /* Return false, if packet can be sent now without violation Nagle's rules:
  1225. * 1. It is full sized.
  1226. * 2. Or it contains FIN. (already checked by caller)
  1227. * 3. Or TCP_CORK is not set, and TCP_NODELAY is set.
  1228. * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
  1229. * With Minshall's modification: all sent small packets are ACKed.
  1230. */
  1231. static inline bool tcp_nagle_check(const struct tcp_sock *tp,
  1232. const struct sk_buff *skb,
  1233. unsigned int mss_now, int nonagle)
  1234. {
  1235. return skb->len < mss_now &&
  1236. ((nonagle & TCP_NAGLE_CORK) ||
  1237. (!nonagle && tp->packets_out && tcp_minshall_check(tp)));
  1238. }
  1239. /* Return true if the Nagle test allows this packet to be
  1240. * sent now.
  1241. */
  1242. static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb,
  1243. unsigned int cur_mss, int nonagle)
  1244. {
  1245. /* Nagle rule does not apply to frames, which sit in the middle of the
  1246. * write_queue (they have no chances to get new data).
  1247. *
  1248. * This is implemented in the callers, where they modify the 'nonagle'
  1249. * argument based upon the location of SKB in the send queue.
  1250. */
  1251. if (nonagle & TCP_NAGLE_PUSH)
  1252. return true;
  1253. /* Don't use the nagle rule for urgent data (or for the final FIN). */
  1254. if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
  1255. return true;
  1256. if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
  1257. return true;
  1258. return false;
  1259. }
  1260. /* Does at least the first segment of SKB fit into the send window? */
  1261. static bool tcp_snd_wnd_test(const struct tcp_sock *tp,
  1262. const struct sk_buff *skb,
  1263. unsigned int cur_mss)
  1264. {
  1265. u32 end_seq = TCP_SKB_CB(skb)->end_seq;
  1266. if (skb->len > cur_mss)
  1267. end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
  1268. return !after(end_seq, tcp_wnd_end(tp));
  1269. }
  1270. /* This checks if the data bearing packet SKB (usually tcp_send_head(sk))
  1271. * should be put on the wire right now. If so, it returns the number of
  1272. * packets allowed by the congestion window.
  1273. */
  1274. static unsigned int tcp_snd_test(const struct sock *sk, struct sk_buff *skb,
  1275. unsigned int cur_mss, int nonagle)
  1276. {
  1277. const struct tcp_sock *tp = tcp_sk(sk);
  1278. unsigned int cwnd_quota;
  1279. tcp_init_tso_segs(sk, skb, cur_mss);
  1280. if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
  1281. return 0;
  1282. cwnd_quota = tcp_cwnd_test(tp, skb);
  1283. if (cwnd_quota && !tcp_snd_wnd_test(tp, skb, cur_mss))
  1284. cwnd_quota = 0;
  1285. return cwnd_quota;
  1286. }
  1287. /* Test if sending is allowed right now. */
  1288. bool tcp_may_send_now(struct sock *sk)
  1289. {
  1290. const struct tcp_sock *tp = tcp_sk(sk);
  1291. struct sk_buff *skb = tcp_send_head(sk);
  1292. return skb &&
  1293. tcp_snd_test(sk, skb, tcp_current_mss(sk),
  1294. (tcp_skb_is_last(sk, skb) ?
  1295. tp->nonagle : TCP_NAGLE_PUSH));
  1296. }
  1297. /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
  1298. * which is put after SKB on the list. It is very much like
  1299. * tcp_fragment() except that it may make several kinds of assumptions
  1300. * in order to speed up the splitting operation. In particular, we
  1301. * know that all the data is in scatter-gather pages, and that the
  1302. * packet has never been sent out before (and thus is not cloned).
  1303. */
  1304. static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
  1305. unsigned int mss_now, gfp_t gfp)
  1306. {
  1307. struct sk_buff *buff;
  1308. int nlen = skb->len - len;
  1309. u8 flags;
  1310. /* All of a TSO frame must be composed of paged data. */
  1311. if (skb->len != skb->data_len)
  1312. return tcp_fragment(sk, skb, len, mss_now);
  1313. buff = sk_stream_alloc_skb(sk, 0, gfp);
  1314. if (unlikely(buff == NULL))
  1315. return -ENOMEM;
  1316. sk->sk_wmem_queued += buff->truesize;
  1317. sk_mem_charge(sk, buff->truesize);
  1318. buff->truesize += nlen;
  1319. skb->truesize -= nlen;
  1320. /* Correct the sequence numbers. */
  1321. TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
  1322. TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
  1323. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
  1324. /* PSH and FIN should only be set in the second packet. */
  1325. flags = TCP_SKB_CB(skb)->tcp_flags;
  1326. TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
  1327. TCP_SKB_CB(buff)->tcp_flags = flags;
  1328. /* This packet was never sent out yet, so no SACK bits. */
  1329. TCP_SKB_CB(buff)->sacked = 0;
  1330. buff->ip_summed = skb->ip_summed = CHECKSUM_PARTIAL;
  1331. skb_split(skb, buff, len);
  1332. /* Fix up tso_factor for both original and new SKB. */
  1333. tcp_set_skb_tso_segs(sk, skb, mss_now);
  1334. tcp_set_skb_tso_segs(sk, buff, mss_now);
  1335. /* Link BUFF into the send queue. */
  1336. skb_header_release(buff);
  1337. tcp_insert_write_queue_after(skb, buff, sk);
  1338. return 0;
  1339. }
  1340. /* Try to defer sending, if possible, in order to minimize the amount
  1341. * of TSO splitting we do. View it as a kind of TSO Nagle test.
  1342. *
  1343. * This algorithm is from John Heffner.
  1344. */
  1345. static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb)
  1346. {
  1347. struct tcp_sock *tp = tcp_sk(sk);
  1348. const struct inet_connection_sock *icsk = inet_csk(sk);
  1349. u32 send_win, cong_win, limit, in_flight;
  1350. int win_divisor;
  1351. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  1352. goto send_now;
  1353. if (icsk->icsk_ca_state != TCP_CA_Open)
  1354. goto send_now;
  1355. /* Defer for less than two clock ticks. */
  1356. if (tp->tso_deferred &&
  1357. (((u32)jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
  1358. goto send_now;
  1359. in_flight = tcp_packets_in_flight(tp);
  1360. BUG_ON(tcp_skb_pcount(skb) <= 1 || (tp->snd_cwnd <= in_flight));
  1361. send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
  1362. /* From in_flight test above, we know that cwnd > in_flight. */
  1363. cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
  1364. limit = min(send_win, cong_win);
  1365. /* If a full-sized TSO skb can be sent, do it. */
  1366. if (limit >= min_t(unsigned int, sk->sk_gso_max_size,
  1367. sk->sk_gso_max_segs * tp->mss_cache))
  1368. goto send_now;
  1369. /* Middle in queue won't get any more data, full sendable already? */
  1370. if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len))
  1371. goto send_now;
  1372. win_divisor = ACCESS_ONCE(sysctl_tcp_tso_win_divisor);
  1373. if (win_divisor) {
  1374. u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
  1375. /* If at least some fraction of a window is available,
  1376. * just use it.
  1377. */
  1378. chunk /= win_divisor;
  1379. if (limit >= chunk)
  1380. goto send_now;
  1381. } else {
  1382. /* Different approach, try not to defer past a single
  1383. * ACK. Receiver should ACK every other full sized
  1384. * frame, so if we have space for more than 3 frames
  1385. * then send now.
  1386. */
  1387. if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache)
  1388. goto send_now;
  1389. }
  1390. /* Ok, it looks like it is advisable to defer.
  1391. * Do not rearm the timer if already set to not break TCP ACK clocking.
  1392. */
  1393. if (!tp->tso_deferred)
  1394. tp->tso_deferred = 1 | (jiffies << 1);
  1395. return true;
  1396. send_now:
  1397. tp->tso_deferred = 0;
  1398. return false;
  1399. }
  1400. /* Create a new MTU probe if we are ready.
  1401. * MTU probe is regularly attempting to increase the path MTU by
  1402. * deliberately sending larger packets. This discovers routing
  1403. * changes resulting in larger path MTUs.
  1404. *
  1405. * Returns 0 if we should wait to probe (no cwnd available),
  1406. * 1 if a probe was sent,
  1407. * -1 otherwise
  1408. */
  1409. static int tcp_mtu_probe(struct sock *sk)
  1410. {
  1411. struct tcp_sock *tp = tcp_sk(sk);
  1412. struct inet_connection_sock *icsk = inet_csk(sk);
  1413. struct sk_buff *skb, *nskb, *next;
  1414. int len;
  1415. int probe_size;
  1416. int size_needed;
  1417. int copy;
  1418. int mss_now;
  1419. /* Not currently probing/verifying,
  1420. * not in recovery,
  1421. * have enough cwnd, and
  1422. * not SACKing (the variable headers throw things off) */
  1423. if (!icsk->icsk_mtup.enabled ||
  1424. icsk->icsk_mtup.probe_size ||
  1425. inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
  1426. tp->snd_cwnd < 11 ||
  1427. tp->rx_opt.num_sacks || tp->rx_opt.dsack)
  1428. return -1;
  1429. /* Very simple search strategy: just double the MSS. */
  1430. mss_now = tcp_current_mss(sk);
  1431. probe_size = 2 * tp->mss_cache;
  1432. size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
  1433. if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) {
  1434. /* TODO: set timer for probe_converge_event */
  1435. return -1;
  1436. }
  1437. /* Have enough data in the send queue to probe? */
  1438. if (tp->write_seq - tp->snd_nxt < size_needed)
  1439. return -1;
  1440. if (tp->snd_wnd < size_needed)
  1441. return -1;
  1442. if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp)))
  1443. return 0;
  1444. /* Do we need to wait to drain cwnd? With none in flight, don't stall */
  1445. if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) {
  1446. if (!tcp_packets_in_flight(tp))
  1447. return -1;
  1448. else
  1449. return 0;
  1450. }
  1451. /* We're allowed to probe. Build it now. */
  1452. if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL)
  1453. return -1;
  1454. sk->sk_wmem_queued += nskb->truesize;
  1455. sk_mem_charge(sk, nskb->truesize);
  1456. skb = tcp_send_head(sk);
  1457. TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
  1458. TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
  1459. TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK;
  1460. TCP_SKB_CB(nskb)->sacked = 0;
  1461. nskb->csum = 0;
  1462. nskb->ip_summed = skb->ip_summed;
  1463. tcp_insert_write_queue_before(nskb, skb, sk);
  1464. len = 0;
  1465. tcp_for_write_queue_from_safe(skb, next, sk) {
  1466. copy = min_t(int, skb->len, probe_size - len);
  1467. if (nskb->ip_summed)
  1468. skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
  1469. else
  1470. nskb->csum = skb_copy_and_csum_bits(skb, 0,
  1471. skb_put(nskb, copy),
  1472. copy, nskb->csum);
  1473. if (skb->len <= copy) {
  1474. /* We've eaten all the data from this skb.
  1475. * Throw it away. */
  1476. TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
  1477. tcp_unlink_write_queue(skb, sk);
  1478. sk_wmem_free_skb(sk, skb);
  1479. } else {
  1480. TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags &
  1481. ~(TCPHDR_FIN|TCPHDR_PSH);
  1482. if (!skb_shinfo(skb)->nr_frags) {
  1483. skb_pull(skb, copy);
  1484. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1485. skb->csum = csum_partial(skb->data,
  1486. skb->len, 0);
  1487. } else {
  1488. __pskb_trim_head(skb, copy);
  1489. tcp_set_skb_tso_segs(sk, skb, mss_now);
  1490. }
  1491. TCP_SKB_CB(skb)->seq += copy;
  1492. }
  1493. len += copy;
  1494. if (len >= probe_size)
  1495. break;
  1496. }
  1497. tcp_init_tso_segs(sk, nskb, nskb->len);
  1498. /* We're ready to send. If this fails, the probe will
  1499. * be resegmented into mss-sized pieces by tcp_write_xmit(). */
  1500. TCP_SKB_CB(nskb)->when = tcp_time_stamp;
  1501. if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
  1502. /* Decrement cwnd here because we are sending
  1503. * effectively two packets. */
  1504. tp->snd_cwnd--;
  1505. tcp_event_new_data_sent(sk, nskb);
  1506. icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
  1507. tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
  1508. tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
  1509. return 1;
  1510. }
  1511. return -1;
  1512. }
  1513. /* This routine writes packets to the network. It advances the
  1514. * send_head. This happens as incoming acks open up the remote
  1515. * window for us.
  1516. *
  1517. * LARGESEND note: !tcp_urg_mode is overkill, only frames between
  1518. * snd_up-64k-mss .. snd_up cannot be large. However, taking into
  1519. * account rare use of URG, this is not a big flaw.
  1520. *
  1521. * Send at most one packet when push_one > 0. Temporarily ignore
  1522. * cwnd limit to force at most one packet out when push_one == 2.
  1523. * Returns true, if no segments are in flight and we have queued segments,
  1524. * but cannot send anything now because of SWS or another problem.
  1525. */
  1526. static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
  1527. int push_one, gfp_t gfp)
  1528. {
  1529. struct tcp_sock *tp = tcp_sk(sk);
  1530. struct sk_buff *skb;
  1531. unsigned int tso_segs, sent_pkts;
  1532. int cwnd_quota;
  1533. int result;
  1534. sent_pkts = 0;
  1535. if (!push_one) {
  1536. /* Do MTU probing. */
  1537. result = tcp_mtu_probe(sk);
  1538. if (!result) {
  1539. return false;
  1540. } else if (result > 0) {
  1541. sent_pkts = 1;
  1542. }
  1543. }
  1544. while ((skb = tcp_send_head(sk))) {
  1545. unsigned int limit;
  1546. tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
  1547. BUG_ON(!tso_segs);
  1548. if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE)
  1549. goto repair; /* Skip network transmission */
  1550. cwnd_quota = tcp_cwnd_test(tp, skb);
  1551. if (!cwnd_quota) {
  1552. if (push_one == 2)
  1553. /* Force out a loss probe pkt. */
  1554. cwnd_quota = 1;
  1555. else
  1556. break;
  1557. }
  1558. if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
  1559. break;
  1560. if (tso_segs == 1) {
  1561. if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
  1562. (tcp_skb_is_last(sk, skb) ?
  1563. nonagle : TCP_NAGLE_PUSH))))
  1564. break;
  1565. } else {
  1566. if (!push_one && tcp_tso_should_defer(sk, skb))
  1567. break;
  1568. }
  1569. /* TSQ : sk_wmem_alloc accounts skb truesize,
  1570. * including skb overhead. But thats OK.
  1571. */
  1572. if (atomic_read(&sk->sk_wmem_alloc) >= sysctl_tcp_limit_output_bytes) {
  1573. set_bit(TSQ_THROTTLED, &tp->tsq_flags);
  1574. break;
  1575. }
  1576. limit = mss_now;
  1577. if (tso_segs > 1 && !tcp_urg_mode(tp))
  1578. limit = tcp_mss_split_point(sk, skb, mss_now,
  1579. min_t(unsigned int,
  1580. cwnd_quota,
  1581. sk->sk_gso_max_segs));
  1582. if (skb->len > limit &&
  1583. unlikely(tso_fragment(sk, skb, limit, mss_now, gfp)))
  1584. break;
  1585. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1586. if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
  1587. break;
  1588. repair:
  1589. /* Advance the send_head. This one is sent out.
  1590. * This call will increment packets_out.
  1591. */
  1592. tcp_event_new_data_sent(sk, skb);
  1593. tcp_minshall_update(tp, mss_now, skb);
  1594. sent_pkts += tcp_skb_pcount(skb);
  1595. if (push_one)
  1596. break;
  1597. }
  1598. if (likely(sent_pkts)) {
  1599. if (tcp_in_cwnd_reduction(sk))
  1600. tp->prr_out += sent_pkts;
  1601. /* Send one loss probe per tail loss episode. */
  1602. if (push_one != 2)
  1603. tcp_schedule_loss_probe(sk);
  1604. tcp_cwnd_validate(sk);
  1605. return false;
  1606. }
  1607. return (push_one == 2) || (!tp->packets_out && tcp_send_head(sk));
  1608. }
  1609. bool tcp_schedule_loss_probe(struct sock *sk)
  1610. {
  1611. struct inet_connection_sock *icsk = inet_csk(sk);
  1612. struct tcp_sock *tp = tcp_sk(sk);
  1613. u32 timeout, tlp_time_stamp, rto_time_stamp;
  1614. u32 rtt = tp->srtt >> 3;
  1615. if (WARN_ON(icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS))
  1616. return false;
  1617. /* No consecutive loss probes. */
  1618. if (WARN_ON(icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)) {
  1619. tcp_rearm_rto(sk);
  1620. return false;
  1621. }
  1622. /* Don't do any loss probe on a Fast Open connection before 3WHS
  1623. * finishes.
  1624. */
  1625. if (sk->sk_state == TCP_SYN_RECV)
  1626. return false;
  1627. /* TLP is only scheduled when next timer event is RTO. */
  1628. if (icsk->icsk_pending != ICSK_TIME_RETRANS)
  1629. return false;
  1630. /* Schedule a loss probe in 2*RTT for SACK capable connections
  1631. * in Open state, that are either limited by cwnd or application.
  1632. */
  1633. if (sysctl_tcp_early_retrans < 3 || !rtt || !tp->packets_out ||
  1634. !tcp_is_sack(tp) || inet_csk(sk)->icsk_ca_state != TCP_CA_Open)
  1635. return false;
  1636. if ((tp->snd_cwnd > tcp_packets_in_flight(tp)) &&
  1637. tcp_send_head(sk))
  1638. return false;
  1639. /* Probe timeout is at least 1.5*rtt + TCP_DELACK_MAX to account
  1640. * for delayed ack when there's one outstanding packet.
  1641. */
  1642. timeout = rtt << 1;
  1643. if (tp->packets_out == 1)
  1644. timeout = max_t(u32, timeout,
  1645. (rtt + (rtt >> 1) + TCP_DELACK_MAX));
  1646. timeout = max_t(u32, timeout, msecs_to_jiffies(10));
  1647. /* If RTO is shorter, just schedule TLP in its place. */
  1648. tlp_time_stamp = tcp_time_stamp + timeout;
  1649. rto_time_stamp = (u32)inet_csk(sk)->icsk_timeout;
  1650. if ((s32)(tlp_time_stamp - rto_time_stamp) > 0) {
  1651. s32 delta = rto_time_stamp - tcp_time_stamp;
  1652. if (delta > 0)
  1653. timeout = delta;
  1654. }
  1655. inet_csk_reset_xmit_timer(sk, ICSK_TIME_LOSS_PROBE, timeout,
  1656. TCP_RTO_MAX);
  1657. return true;
  1658. }
  1659. /* When probe timeout (PTO) fires, send a new segment if one exists, else
  1660. * retransmit the last segment.
  1661. */
  1662. void tcp_send_loss_probe(struct sock *sk)
  1663. {
  1664. struct tcp_sock *tp = tcp_sk(sk);
  1665. struct sk_buff *skb;
  1666. int pcount;
  1667. int mss = tcp_current_mss(sk);
  1668. int err = -1;
  1669. if (tcp_send_head(sk) != NULL) {
  1670. err = tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC);
  1671. goto rearm_timer;
  1672. }
  1673. /* At most one outstanding TLP retransmission. */
  1674. if (tp->tlp_high_seq)
  1675. goto rearm_timer;
  1676. /* Retransmit last segment. */
  1677. skb = tcp_write_queue_tail(sk);
  1678. if (WARN_ON(!skb))
  1679. goto rearm_timer;
  1680. pcount = tcp_skb_pcount(skb);
  1681. if (WARN_ON(!pcount))
  1682. goto rearm_timer;
  1683. if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) {
  1684. if (unlikely(tcp_fragment(sk, skb, (pcount - 1) * mss, mss)))
  1685. goto rearm_timer;
  1686. skb = tcp_write_queue_tail(sk);
  1687. }
  1688. if (WARN_ON(!skb || !tcp_skb_pcount(skb)))
  1689. goto rearm_timer;
  1690. /* Probe with zero data doesn't trigger fast recovery. */
  1691. if (skb->len > 0)
  1692. err = __tcp_retransmit_skb(sk, skb);
  1693. /* Record snd_nxt for loss detection. */
  1694. if (likely(!err))
  1695. tp->tlp_high_seq = tp->snd_nxt;
  1696. rearm_timer:
  1697. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  1698. inet_csk(sk)->icsk_rto,
  1699. TCP_RTO_MAX);
  1700. if (likely(!err))
  1701. NET_INC_STATS_BH(sock_net(sk),
  1702. LINUX_MIB_TCPLOSSPROBES);
  1703. return;
  1704. }
  1705. /* Push out any pending frames which were held back due to
  1706. * TCP_CORK or attempt at coalescing tiny packets.
  1707. * The socket must be locked by the caller.
  1708. */
  1709. void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
  1710. int nonagle)
  1711. {
  1712. /* If we are closed, the bytes will have to remain here.
  1713. * In time closedown will finish, we empty the write queue and
  1714. * all will be happy.
  1715. */
  1716. if (unlikely(sk->sk_state == TCP_CLOSE))
  1717. return;
  1718. if (tcp_write_xmit(sk, cur_mss, nonagle, 0,
  1719. sk_gfp_atomic(sk, GFP_ATOMIC)))
  1720. tcp_check_probe_timer(sk);
  1721. }
  1722. /* Send _single_ skb sitting at the send head. This function requires
  1723. * true push pending frames to setup probe timer etc.
  1724. */
  1725. void tcp_push_one(struct sock *sk, unsigned int mss_now)
  1726. {
  1727. struct sk_buff *skb = tcp_send_head(sk);
  1728. BUG_ON(!skb || skb->len < mss_now);
  1729. tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation);
  1730. }
  1731. /* This function returns the amount that we can raise the
  1732. * usable window based on the following constraints
  1733. *
  1734. * 1. The window can never be shrunk once it is offered (RFC 793)
  1735. * 2. We limit memory per socket
  1736. *
  1737. * RFC 1122:
  1738. * "the suggested [SWS] avoidance algorithm for the receiver is to keep
  1739. * RECV.NEXT + RCV.WIN fixed until:
  1740. * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
  1741. *
  1742. * i.e. don't raise the right edge of the window until you can raise
  1743. * it at least MSS bytes.
  1744. *
  1745. * Unfortunately, the recommended algorithm breaks header prediction,
  1746. * since header prediction assumes th->window stays fixed.
  1747. *
  1748. * Strictly speaking, keeping th->window fixed violates the receiver
  1749. * side SWS prevention criteria. The problem is that under this rule
  1750. * a stream of single byte packets will cause the right side of the
  1751. * window to always advance by a single byte.
  1752. *
  1753. * Of course, if the sender implements sender side SWS prevention
  1754. * then this will not be a problem.
  1755. *
  1756. * BSD seems to make the following compromise:
  1757. *
  1758. * If the free space is less than the 1/4 of the maximum
  1759. * space available and the free space is less than 1/2 mss,
  1760. * then set the window to 0.
  1761. * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
  1762. * Otherwise, just prevent the window from shrinking
  1763. * and from being larger than the largest representable value.
  1764. *
  1765. * This prevents incremental opening of the window in the regime
  1766. * where TCP is limited by the speed of the reader side taking
  1767. * data out of the TCP receive queue. It does nothing about
  1768. * those cases where the window is constrained on the sender side
  1769. * because the pipeline is full.
  1770. *
  1771. * BSD also seems to "accidentally" limit itself to windows that are a
  1772. * multiple of MSS, at least until the free space gets quite small.
  1773. * This would appear to be a side effect of the mbuf implementation.
  1774. * Combining these two algorithms results in the observed behavior
  1775. * of having a fixed window size at almost all times.
  1776. *
  1777. * Below we obtain similar behavior by forcing the offered window to
  1778. * a multiple of the mss when it is feasible to do so.
  1779. *
  1780. * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
  1781. * Regular options like TIMESTAMP are taken into account.
  1782. */
  1783. u32 __tcp_select_window(struct sock *sk)
  1784. {
  1785. struct inet_connection_sock *icsk = inet_csk(sk);
  1786. struct tcp_sock *tp = tcp_sk(sk);
  1787. /* MSS for the peer's data. Previous versions used mss_clamp
  1788. * here. I don't know if the value based on our guesses
  1789. * of peer's MSS is better for the performance. It's more correct
  1790. * but may be worse for the performance because of rcv_mss
  1791. * fluctuations. --SAW 1998/11/1
  1792. */
  1793. int mss = icsk->icsk_ack.rcv_mss;
  1794. int free_space = tcp_space(sk);
  1795. int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
  1796. int window;
  1797. if (mss > full_space)
  1798. mss = full_space;
  1799. if (free_space < (full_space >> 1)) {
  1800. icsk->icsk_ack.quick = 0;
  1801. if (sk_under_memory_pressure(sk))
  1802. tp->rcv_ssthresh = min(tp->rcv_ssthresh,
  1803. 4U * tp->advmss);
  1804. if (free_space < mss)
  1805. return 0;
  1806. }
  1807. if (free_space > tp->rcv_ssthresh)
  1808. free_space = tp->rcv_ssthresh;
  1809. /* Don't do rounding if we are using window scaling, since the
  1810. * scaled window will not line up with the MSS boundary anyway.
  1811. */
  1812. window = tp->rcv_wnd;
  1813. if (tp->rx_opt.rcv_wscale) {
  1814. window = free_space;
  1815. /* Advertise enough space so that it won't get scaled away.
  1816. * Import case: prevent zero window announcement if
  1817. * 1<<rcv_wscale > mss.
  1818. */
  1819. if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
  1820. window = (((window >> tp->rx_opt.rcv_wscale) + 1)
  1821. << tp->rx_opt.rcv_wscale);
  1822. } else {
  1823. /* Get the largest window that is a nice multiple of mss.
  1824. * Window clamp already applied above.
  1825. * If our current window offering is within 1 mss of the
  1826. * free space we just keep it. This prevents the divide
  1827. * and multiply from happening most of the time.
  1828. * We also don't do any window rounding when the free space
  1829. * is too small.
  1830. */
  1831. if (window <= free_space - mss || window > free_space)
  1832. window = (free_space / mss) * mss;
  1833. else if (mss == full_space &&
  1834. free_space > window + (full_space >> 1))
  1835. window = free_space;
  1836. }
  1837. return window;
  1838. }
  1839. /* Collapses two adjacent SKB's during retransmission. */
  1840. static void tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb)
  1841. {
  1842. struct tcp_sock *tp = tcp_sk(sk);
  1843. struct sk_buff *next_skb = tcp_write_queue_next(sk, skb);
  1844. int skb_size, next_skb_size;
  1845. skb_size = skb->len;
  1846. next_skb_size = next_skb->len;
  1847. BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1);
  1848. tcp_highest_sack_combine(sk, next_skb, skb);
  1849. tcp_unlink_write_queue(next_skb, sk);
  1850. skb_copy_from_linear_data(next_skb, skb_put(skb, next_skb_size),
  1851. next_skb_size);
  1852. if (next_skb->ip_summed == CHECKSUM_PARTIAL)
  1853. skb->ip_summed = CHECKSUM_PARTIAL;
  1854. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1855. skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
  1856. /* Update sequence range on original skb. */
  1857. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
  1858. /* Merge over control information. This moves PSH/FIN etc. over */
  1859. TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags;
  1860. /* All done, get rid of second SKB and account for it so
  1861. * packet counting does not break.
  1862. */
  1863. TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS;
  1864. /* changed transmit queue under us so clear hints */
  1865. tcp_clear_retrans_hints_partial(tp);
  1866. if (next_skb == tp->retransmit_skb_hint)
  1867. tp->retransmit_skb_hint = skb;
  1868. tcp_adjust_pcount(sk, next_skb, tcp_skb_pcount(next_skb));
  1869. sk_wmem_free_skb(sk, next_skb);
  1870. }
  1871. /* Check if coalescing SKBs is legal. */
  1872. static bool tcp_can_collapse(const struct sock *sk, const struct sk_buff *skb)
  1873. {
  1874. if (tcp_skb_pcount(skb) > 1)
  1875. return false;
  1876. /* TODO: SACK collapsing could be used to remove this condition */
  1877. if (skb_shinfo(skb)->nr_frags != 0)
  1878. return false;
  1879. if (skb_cloned(skb))
  1880. return false;
  1881. if (skb == tcp_send_head(sk))
  1882. return false;
  1883. /* Some heurestics for collapsing over SACK'd could be invented */
  1884. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
  1885. return false;
  1886. return true;
  1887. }
  1888. /* Collapse packets in the retransmit queue to make to create
  1889. * less packets on the wire. This is only done on retransmission.
  1890. */
  1891. static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to,
  1892. int space)
  1893. {
  1894. struct tcp_sock *tp = tcp_sk(sk);
  1895. struct sk_buff *skb = to, *tmp;
  1896. bool first = true;
  1897. if (!sysctl_tcp_retrans_collapse)
  1898. return;
  1899. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)
  1900. return;
  1901. tcp_for_write_queue_from_safe(skb, tmp, sk) {
  1902. if (!tcp_can_collapse(sk, skb))
  1903. break;
  1904. space -= skb->len;
  1905. if (first) {
  1906. first = false;
  1907. continue;
  1908. }
  1909. if (space < 0)
  1910. break;
  1911. /* Punt if not enough space exists in the first SKB for
  1912. * the data in the second
  1913. */
  1914. if (skb->len > skb_availroom(to))
  1915. break;
  1916. if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp)))
  1917. break;
  1918. tcp_collapse_retrans(sk, to);
  1919. }
  1920. }
  1921. /* This retransmits one SKB. Policy decisions and retransmit queue
  1922. * state updates are done by the caller. Returns non-zero if an
  1923. * error occurred which prevented the send.
  1924. */
  1925. int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  1926. {
  1927. struct tcp_sock *tp = tcp_sk(sk);
  1928. struct inet_connection_sock *icsk = inet_csk(sk);
  1929. unsigned int cur_mss;
  1930. /* Inconslusive MTU probe */
  1931. if (icsk->icsk_mtup.probe_size) {
  1932. icsk->icsk_mtup.probe_size = 0;
  1933. }
  1934. /* Do not sent more than we queued. 1/4 is reserved for possible
  1935. * copying overhead: fragmentation, tunneling, mangling etc.
  1936. */
  1937. if (atomic_read(&sk->sk_wmem_alloc) >
  1938. min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
  1939. return -EAGAIN;
  1940. if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
  1941. if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
  1942. BUG();
  1943. if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
  1944. return -ENOMEM;
  1945. }
  1946. if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
  1947. return -EHOSTUNREACH; /* Routing failure or similar. */
  1948. cur_mss = tcp_current_mss(sk);
  1949. /* If receiver has shrunk his window, and skb is out of
  1950. * new window, do not retransmit it. The exception is the
  1951. * case, when window is shrunk to zero. In this case
  1952. * our retransmit serves as a zero window probe.
  1953. */
  1954. if (!before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp)) &&
  1955. TCP_SKB_CB(skb)->seq != tp->snd_una)
  1956. return -EAGAIN;
  1957. if (skb->len > cur_mss) {
  1958. if (tcp_fragment(sk, skb, cur_mss, cur_mss))
  1959. return -ENOMEM; /* We'll try again later. */
  1960. } else {
  1961. int oldpcount = tcp_skb_pcount(skb);
  1962. if (unlikely(oldpcount > 1)) {
  1963. tcp_init_tso_segs(sk, skb, cur_mss);
  1964. tcp_adjust_pcount(sk, skb, oldpcount - tcp_skb_pcount(skb));
  1965. }
  1966. }
  1967. tcp_retrans_try_collapse(sk, skb, cur_mss);
  1968. /* Some Solaris stacks overoptimize and ignore the FIN on a
  1969. * retransmit when old data is attached. So strip it off
  1970. * since it is cheap to do so and saves bytes on the network.
  1971. */
  1972. if (skb->len > 0 &&
  1973. (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
  1974. tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
  1975. if (!pskb_trim(skb, 0)) {
  1976. /* Reuse, even though it does some unnecessary work */
  1977. tcp_init_nondata_skb(skb, TCP_SKB_CB(skb)->end_seq - 1,
  1978. TCP_SKB_CB(skb)->tcp_flags);
  1979. skb->ip_summed = CHECKSUM_NONE;
  1980. }
  1981. }
  1982. /* Make a copy, if the first transmission SKB clone we made
  1983. * is still in somebody's hands, else make a clone.
  1984. */
  1985. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  1986. /* make sure skb->data is aligned on arches that require it */
  1987. if (unlikely(NET_IP_ALIGN && ((unsigned long)skb->data & 3))) {
  1988. struct sk_buff *nskb = __pskb_copy(skb, MAX_TCP_HEADER,
  1989. GFP_ATOMIC);
  1990. return nskb ? tcp_transmit_skb(sk, nskb, 0, GFP_ATOMIC) :
  1991. -ENOBUFS;
  1992. } else {
  1993. return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
  1994. }
  1995. }
  1996. int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  1997. {
  1998. struct tcp_sock *tp = tcp_sk(sk);
  1999. int err = __tcp_retransmit_skb(sk, skb);
  2000. if (err == 0) {
  2001. /* Update global TCP statistics. */
  2002. TCP_INC_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS);
  2003. tp->total_retrans++;
  2004. #if FASTRETRANS_DEBUG > 0
  2005. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
  2006. net_dbg_ratelimited("retrans_out leaked\n");
  2007. }
  2008. #endif
  2009. if (!tp->retrans_out)
  2010. tp->lost_retrans_low = tp->snd_nxt;
  2011. TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
  2012. tp->retrans_out += tcp_skb_pcount(skb);
  2013. /* Save stamp of the first retransmit. */
  2014. if (!tp->retrans_stamp)
  2015. tp->retrans_stamp = TCP_SKB_CB(skb)->when;
  2016. tp->undo_retrans += tcp_skb_pcount(skb);
  2017. /* snd_nxt is stored to detect loss of retransmitted segment,
  2018. * see tcp_input.c tcp_sacktag_write_queue().
  2019. */
  2020. TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
  2021. }
  2022. return err;
  2023. }
  2024. /* Check if we forward retransmits are possible in the current
  2025. * window/congestion state.
  2026. */
  2027. static bool tcp_can_forward_retransmit(struct sock *sk)
  2028. {
  2029. const struct inet_connection_sock *icsk = inet_csk(sk);
  2030. const struct tcp_sock *tp = tcp_sk(sk);
  2031. /* Forward retransmissions are possible only during Recovery. */
  2032. if (icsk->icsk_ca_state != TCP_CA_Recovery)
  2033. return false;
  2034. /* No forward retransmissions in Reno are possible. */
  2035. if (tcp_is_reno(tp))
  2036. return false;
  2037. /* Yeah, we have to make difficult choice between forward transmission
  2038. * and retransmission... Both ways have their merits...
  2039. *
  2040. * For now we do not retransmit anything, while we have some new
  2041. * segments to send. In the other cases, follow rule 3 for
  2042. * NextSeg() specified in RFC3517.
  2043. */
  2044. if (tcp_may_send_now(sk))
  2045. return false;
  2046. return true;
  2047. }
  2048. /* This gets called after a retransmit timeout, and the initially
  2049. * retransmitted data is acknowledged. It tries to continue
  2050. * resending the rest of the retransmit queue, until either
  2051. * we've sent it all or the congestion window limit is reached.
  2052. * If doing SACK, the first ACK which comes back for a timeout
  2053. * based retransmit packet might feed us FACK information again.
  2054. * If so, we use it to avoid unnecessarily retransmissions.
  2055. */
  2056. void tcp_xmit_retransmit_queue(struct sock *sk)
  2057. {
  2058. const struct inet_connection_sock *icsk = inet_csk(sk);
  2059. struct tcp_sock *tp = tcp_sk(sk);
  2060. struct sk_buff *skb;
  2061. struct sk_buff *hole = NULL;
  2062. u32 last_lost;
  2063. int mib_idx;
  2064. int fwd_rexmitting = 0;
  2065. if (!tp->packets_out)
  2066. return;
  2067. if (!tp->lost_out)
  2068. tp->retransmit_high = tp->snd_una;
  2069. if (tp->retransmit_skb_hint) {
  2070. skb = tp->retransmit_skb_hint;
  2071. last_lost = TCP_SKB_CB(skb)->end_seq;
  2072. if (after(last_lost, tp->retransmit_high))
  2073. last_lost = tp->retransmit_high;
  2074. } else {
  2075. skb = tcp_write_queue_head(sk);
  2076. last_lost = tp->snd_una;
  2077. }
  2078. tcp_for_write_queue_from(skb, sk) {
  2079. __u8 sacked = TCP_SKB_CB(skb)->sacked;
  2080. if (skb == tcp_send_head(sk))
  2081. break;
  2082. /* we could do better than to assign each time */
  2083. if (hole == NULL)
  2084. tp->retransmit_skb_hint = skb;
  2085. /* Assume this retransmit will generate
  2086. * only one packet for congestion window
  2087. * calculation purposes. This works because
  2088. * tcp_retransmit_skb() will chop up the
  2089. * packet to be MSS sized and all the
  2090. * packet counting works out.
  2091. */
  2092. if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
  2093. return;
  2094. if (fwd_rexmitting) {
  2095. begin_fwd:
  2096. if (!before(TCP_SKB_CB(skb)->seq, tcp_highest_sack_seq(tp)))
  2097. break;
  2098. mib_idx = LINUX_MIB_TCPFORWARDRETRANS;
  2099. } else if (!before(TCP_SKB_CB(skb)->seq, tp->retransmit_high)) {
  2100. tp->retransmit_high = last_lost;
  2101. if (!tcp_can_forward_retransmit(sk))
  2102. break;
  2103. /* Backtrack if necessary to non-L'ed skb */
  2104. if (hole != NULL) {
  2105. skb = hole;
  2106. hole = NULL;
  2107. }
  2108. fwd_rexmitting = 1;
  2109. goto begin_fwd;
  2110. } else if (!(sacked & TCPCB_LOST)) {
  2111. if (hole == NULL && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED)))
  2112. hole = skb;
  2113. continue;
  2114. } else {
  2115. last_lost = TCP_SKB_CB(skb)->end_seq;
  2116. if (icsk->icsk_ca_state != TCP_CA_Loss)
  2117. mib_idx = LINUX_MIB_TCPFASTRETRANS;
  2118. else
  2119. mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS;
  2120. }
  2121. if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))
  2122. continue;
  2123. if (tcp_retransmit_skb(sk, skb)) {
  2124. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL);
  2125. return;
  2126. }
  2127. NET_INC_STATS_BH(sock_net(sk), mib_idx);
  2128. if (tcp_in_cwnd_reduction(sk))
  2129. tp->prr_out += tcp_skb_pcount(skb);
  2130. if (skb == tcp_write_queue_head(sk))
  2131. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  2132. inet_csk(sk)->icsk_rto,
  2133. TCP_RTO_MAX);
  2134. }
  2135. }
  2136. /* Send a fin. The caller locks the socket for us. This cannot be
  2137. * allowed to fail queueing a FIN frame under any circumstances.
  2138. */
  2139. void tcp_send_fin(struct sock *sk)
  2140. {
  2141. struct tcp_sock *tp = tcp_sk(sk);
  2142. struct sk_buff *skb = tcp_write_queue_tail(sk);
  2143. int mss_now;
  2144. /* Optimization, tack on the FIN if we have a queue of
  2145. * unsent frames. But be careful about outgoing SACKS
  2146. * and IP options.
  2147. */
  2148. mss_now = tcp_current_mss(sk);
  2149. if (tcp_send_head(sk) != NULL) {
  2150. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_FIN;
  2151. TCP_SKB_CB(skb)->end_seq++;
  2152. tp->write_seq++;
  2153. } else {
  2154. /* Socket is locked, keep trying until memory is available. */
  2155. for (;;) {
  2156. skb = alloc_skb_fclone(MAX_TCP_HEADER,
  2157. sk->sk_allocation);
  2158. if (skb)
  2159. break;
  2160. yield();
  2161. }
  2162. /* Reserve space for headers and prepare control bits. */
  2163. skb_reserve(skb, MAX_TCP_HEADER);
  2164. /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
  2165. tcp_init_nondata_skb(skb, tp->write_seq,
  2166. TCPHDR_ACK | TCPHDR_FIN);
  2167. tcp_queue_skb(sk, skb);
  2168. }
  2169. __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_OFF);
  2170. }
  2171. /* We get here when a process closes a file descriptor (either due to
  2172. * an explicit close() or as a byproduct of exit()'ing) and there
  2173. * was unread data in the receive queue. This behavior is recommended
  2174. * by RFC 2525, section 2.17. -DaveM
  2175. */
  2176. void tcp_send_active_reset(struct sock *sk, gfp_t priority)
  2177. {
  2178. struct sk_buff *skb;
  2179. /* NOTE: No TCP options attached and we never retransmit this. */
  2180. skb = alloc_skb(MAX_TCP_HEADER, priority);
  2181. if (!skb) {
  2182. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
  2183. return;
  2184. }
  2185. /* Reserve space for headers and prepare control bits. */
  2186. skb_reserve(skb, MAX_TCP_HEADER);
  2187. tcp_init_nondata_skb(skb, tcp_acceptable_seq(sk),
  2188. TCPHDR_ACK | TCPHDR_RST);
  2189. /* Send it off. */
  2190. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  2191. if (tcp_transmit_skb(sk, skb, 0, priority))
  2192. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
  2193. TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTRSTS);
  2194. }
  2195. /* Send a crossed SYN-ACK during socket establishment.
  2196. * WARNING: This routine must only be called when we have already sent
  2197. * a SYN packet that crossed the incoming SYN that caused this routine
  2198. * to get called. If this assumption fails then the initial rcv_wnd
  2199. * and rcv_wscale values will not be correct.
  2200. */
  2201. int tcp_send_synack(struct sock *sk)
  2202. {
  2203. struct sk_buff *skb;
  2204. skb = tcp_write_queue_head(sk);
  2205. if (skb == NULL || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  2206. pr_debug("%s: wrong queue state\n", __func__);
  2207. return -EFAULT;
  2208. }
  2209. if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) {
  2210. if (skb_cloned(skb)) {
  2211. struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
  2212. if (nskb == NULL)
  2213. return -ENOMEM;
  2214. tcp_unlink_write_queue(skb, sk);
  2215. skb_header_release(nskb);
  2216. __tcp_add_write_queue_head(sk, nskb);
  2217. sk_wmem_free_skb(sk, skb);
  2218. sk->sk_wmem_queued += nskb->truesize;
  2219. sk_mem_charge(sk, nskb->truesize);
  2220. skb = nskb;
  2221. }
  2222. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK;
  2223. TCP_ECN_send_synack(tcp_sk(sk), skb);
  2224. }
  2225. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  2226. return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
  2227. }
  2228. /**
  2229. * tcp_make_synack - Prepare a SYN-ACK.
  2230. * sk: listener socket
  2231. * dst: dst entry attached to the SYNACK
  2232. * req: request_sock pointer
  2233. *
  2234. * Allocate one skb and build a SYNACK packet.
  2235. * @dst is consumed : Caller should not use it again.
  2236. */
  2237. struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
  2238. struct request_sock *req,
  2239. struct tcp_fastopen_cookie *foc)
  2240. {
  2241. struct tcp_out_options opts;
  2242. struct inet_request_sock *ireq = inet_rsk(req);
  2243. struct tcp_sock *tp = tcp_sk(sk);
  2244. struct tcphdr *th;
  2245. struct sk_buff *skb;
  2246. struct tcp_md5sig_key *md5;
  2247. int tcp_header_size;
  2248. int mss;
  2249. skb = alloc_skb(MAX_TCP_HEADER + 15, sk_gfp_atomic(sk, GFP_ATOMIC));
  2250. if (unlikely(!skb)) {
  2251. dst_release(dst);
  2252. return NULL;
  2253. }
  2254. /* Reserve space for headers. */
  2255. skb_reserve(skb, MAX_TCP_HEADER);
  2256. skb_dst_set(skb, dst);
  2257. mss = dst_metric_advmss(dst);
  2258. if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss)
  2259. mss = tp->rx_opt.user_mss;
  2260. if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
  2261. __u8 rcv_wscale;
  2262. /* Set this up on the first call only */
  2263. req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
  2264. /* limit the window selection if the user enforce a smaller rx buffer */
  2265. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
  2266. (req->window_clamp > tcp_full_space(sk) || req->window_clamp == 0))
  2267. req->window_clamp = tcp_full_space(sk);
  2268. /* tcp_full_space because it is guaranteed to be the first packet */
  2269. tcp_select_initial_window(tcp_full_space(sk),
  2270. mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
  2271. &req->rcv_wnd,
  2272. &req->window_clamp,
  2273. ireq->wscale_ok,
  2274. &rcv_wscale,
  2275. dst_metric(dst, RTAX_INITRWND));
  2276. ireq->rcv_wscale = rcv_wscale;
  2277. }
  2278. memset(&opts, 0, sizeof(opts));
  2279. #ifdef CONFIG_SYN_COOKIES
  2280. if (unlikely(req->cookie_ts))
  2281. TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
  2282. else
  2283. #endif
  2284. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  2285. tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, &md5,
  2286. foc) + sizeof(*th);
  2287. skb_push(skb, tcp_header_size);
  2288. skb_reset_transport_header(skb);
  2289. th = tcp_hdr(skb);
  2290. memset(th, 0, sizeof(struct tcphdr));
  2291. th->syn = 1;
  2292. th->ack = 1;
  2293. TCP_ECN_make_synack(req, th);
  2294. th->source = ireq->loc_port;
  2295. th->dest = ireq->rmt_port;
  2296. /* Setting of flags are superfluous here for callers (and ECE is
  2297. * not even correctly set)
  2298. */
  2299. tcp_init_nondata_skb(skb, tcp_rsk(req)->snt_isn,
  2300. TCPHDR_SYN | TCPHDR_ACK);
  2301. th->seq = htonl(TCP_SKB_CB(skb)->seq);
  2302. /* XXX data is queued and acked as is. No buffer/window check */
  2303. th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt);
  2304. /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
  2305. th->window = htons(min(req->rcv_wnd, 65535U));
  2306. tcp_options_write((__be32 *)(th + 1), tp, &opts);
  2307. th->doff = (tcp_header_size >> 2);
  2308. TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS, tcp_skb_pcount(skb));
  2309. #ifdef CONFIG_TCP_MD5SIG
  2310. /* Okay, we have all we need - do the md5 hash if needed */
  2311. if (md5) {
  2312. tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location,
  2313. md5, NULL, req, skb);
  2314. }
  2315. #endif
  2316. return skb;
  2317. }
  2318. EXPORT_SYMBOL(tcp_make_synack);
  2319. /* Do all connect socket setups that can be done AF independent. */
  2320. void tcp_connect_init(struct sock *sk)
  2321. {
  2322. const struct dst_entry *dst = __sk_dst_get(sk);
  2323. struct tcp_sock *tp = tcp_sk(sk);
  2324. __u8 rcv_wscale;
  2325. /* We'll fix this up when we get a response from the other end.
  2326. * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
  2327. */
  2328. tp->tcp_header_len = sizeof(struct tcphdr) +
  2329. (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
  2330. #ifdef CONFIG_TCP_MD5SIG
  2331. if (tp->af_specific->md5_lookup(sk, sk) != NULL)
  2332. tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
  2333. #endif
  2334. /* If user gave his TCP_MAXSEG, record it to clamp */
  2335. if (tp->rx_opt.user_mss)
  2336. tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
  2337. tp->max_window = 0;
  2338. tcp_mtup_init(sk);
  2339. tcp_sync_mss(sk, dst_mtu(dst));
  2340. if (!tp->window_clamp)
  2341. tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
  2342. tp->advmss = dst_metric_advmss(dst);
  2343. if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->advmss)
  2344. tp->advmss = tp->rx_opt.user_mss;
  2345. tcp_initialize_rcv_mss(sk);
  2346. /* limit the window selection if the user enforce a smaller rx buffer */
  2347. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
  2348. (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0))
  2349. tp->window_clamp = tcp_full_space(sk);
  2350. tcp_select_initial_window(tcp_full_space(sk),
  2351. tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
  2352. &tp->rcv_wnd,
  2353. &tp->window_clamp,
  2354. sysctl_tcp_window_scaling,
  2355. &rcv_wscale,
  2356. dst_metric(dst, RTAX_INITRWND));
  2357. tp->rx_opt.rcv_wscale = rcv_wscale;
  2358. tp->rcv_ssthresh = tp->rcv_wnd;
  2359. sk->sk_err = 0;
  2360. sock_reset_flag(sk, SOCK_DONE);
  2361. tp->snd_wnd = 0;
  2362. tcp_init_wl(tp, 0);
  2363. tp->snd_una = tp->write_seq;
  2364. tp->snd_sml = tp->write_seq;
  2365. tp->snd_up = tp->write_seq;
  2366. tp->snd_nxt = tp->write_seq;
  2367. if (likely(!tp->repair))
  2368. tp->rcv_nxt = 0;
  2369. tp->rcv_wup = tp->rcv_nxt;
  2370. tp->copied_seq = tp->rcv_nxt;
  2371. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
  2372. inet_csk(sk)->icsk_retransmits = 0;
  2373. tcp_clear_retrans(tp);
  2374. }
  2375. static void tcp_connect_queue_skb(struct sock *sk, struct sk_buff *skb)
  2376. {
  2377. struct tcp_sock *tp = tcp_sk(sk);
  2378. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  2379. tcb->end_seq += skb->len;
  2380. skb_header_release(skb);
  2381. __tcp_add_write_queue_tail(sk, skb);
  2382. sk->sk_wmem_queued += skb->truesize;
  2383. sk_mem_charge(sk, skb->truesize);
  2384. tp->write_seq = tcb->end_seq;
  2385. tp->packets_out += tcp_skb_pcount(skb);
  2386. }
  2387. /* Build and send a SYN with data and (cached) Fast Open cookie. However,
  2388. * queue a data-only packet after the regular SYN, such that regular SYNs
  2389. * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges
  2390. * only the SYN sequence, the data are retransmitted in the first ACK.
  2391. * If cookie is not cached or other error occurs, falls back to send a
  2392. * regular SYN with Fast Open cookie request option.
  2393. */
  2394. static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
  2395. {
  2396. struct tcp_sock *tp = tcp_sk(sk);
  2397. struct tcp_fastopen_request *fo = tp->fastopen_req;
  2398. int syn_loss = 0, space, i, err = 0, iovlen = fo->data->msg_iovlen;
  2399. struct sk_buff *syn_data = NULL, *data;
  2400. unsigned long last_syn_loss = 0;
  2401. tp->rx_opt.mss_clamp = tp->advmss; /* If MSS is not cached */
  2402. tcp_fastopen_cache_get(sk, &tp->rx_opt.mss_clamp, &fo->cookie,
  2403. &syn_loss, &last_syn_loss);
  2404. /* Recurring FO SYN losses: revert to regular handshake temporarily */
  2405. if (syn_loss > 1 &&
  2406. time_before(jiffies, last_syn_loss + (60*HZ << syn_loss))) {
  2407. fo->cookie.len = -1;
  2408. goto fallback;
  2409. }
  2410. if (sysctl_tcp_fastopen & TFO_CLIENT_NO_COOKIE)
  2411. fo->cookie.len = -1;
  2412. else if (fo->cookie.len <= 0)
  2413. goto fallback;
  2414. /* MSS for SYN-data is based on cached MSS and bounded by PMTU and
  2415. * user-MSS. Reserve maximum option space for middleboxes that add
  2416. * private TCP options. The cost is reduced data space in SYN :(
  2417. */
  2418. if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->rx_opt.mss_clamp)
  2419. tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
  2420. space = __tcp_mtu_to_mss(sk, inet_csk(sk)->icsk_pmtu_cookie) -
  2421. MAX_TCP_OPTION_SPACE;
  2422. syn_data = skb_copy_expand(syn, skb_headroom(syn), space,
  2423. sk->sk_allocation);
  2424. if (syn_data == NULL)
  2425. goto fallback;
  2426. for (i = 0; i < iovlen && syn_data->len < space; ++i) {
  2427. struct iovec *iov = &fo->data->msg_iov[i];
  2428. unsigned char __user *from = iov->iov_base;
  2429. int len = iov->iov_len;
  2430. if (syn_data->len + len > space)
  2431. len = space - syn_data->len;
  2432. else if (i + 1 == iovlen)
  2433. /* No more data pending in inet_wait_for_connect() */
  2434. fo->data = NULL;
  2435. if (skb_add_data(syn_data, from, len))
  2436. goto fallback;
  2437. }
  2438. /* Queue a data-only packet after the regular SYN for retransmission */
  2439. data = pskb_copy(syn_data, sk->sk_allocation);
  2440. if (data == NULL)
  2441. goto fallback;
  2442. TCP_SKB_CB(data)->seq++;
  2443. TCP_SKB_CB(data)->tcp_flags &= ~TCPHDR_SYN;
  2444. TCP_SKB_CB(data)->tcp_flags = (TCPHDR_ACK|TCPHDR_PSH);
  2445. tcp_connect_queue_skb(sk, data);
  2446. fo->copied = data->len;
  2447. if (tcp_transmit_skb(sk, syn_data, 0, sk->sk_allocation) == 0) {
  2448. tp->syn_data = (fo->copied > 0);
  2449. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
  2450. goto done;
  2451. }
  2452. syn_data = NULL;
  2453. fallback:
  2454. /* Send a regular SYN with Fast Open cookie request option */
  2455. if (fo->cookie.len > 0)
  2456. fo->cookie.len = 0;
  2457. err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation);
  2458. if (err)
  2459. tp->syn_fastopen = 0;
  2460. kfree_skb(syn_data);
  2461. done:
  2462. fo->cookie.len = -1; /* Exclude Fast Open option for SYN retries */
  2463. return err;
  2464. }
  2465. /* Build a SYN and send it off. */
  2466. int tcp_connect(struct sock *sk)
  2467. {
  2468. struct tcp_sock *tp = tcp_sk(sk);
  2469. struct sk_buff *buff;
  2470. int err;
  2471. tcp_connect_init(sk);
  2472. if (unlikely(tp->repair)) {
  2473. tcp_finish_connect(sk, NULL);
  2474. return 0;
  2475. }
  2476. buff = alloc_skb_fclone(MAX_TCP_HEADER + 15, sk->sk_allocation);
  2477. if (unlikely(buff == NULL))
  2478. return -ENOBUFS;
  2479. /* Reserve space for headers. */
  2480. skb_reserve(buff, MAX_TCP_HEADER);
  2481. tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN);
  2482. tp->retrans_stamp = TCP_SKB_CB(buff)->when = tcp_time_stamp;
  2483. tcp_connect_queue_skb(sk, buff);
  2484. TCP_ECN_send_syn(sk, buff);
  2485. /* Send off SYN; include data in Fast Open. */
  2486. err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) :
  2487. tcp_transmit_skb(sk, buff, 1, sk->sk_allocation);
  2488. if (err == -ECONNREFUSED)
  2489. return err;
  2490. /* We change tp->snd_nxt after the tcp_transmit_skb() call
  2491. * in order to make this packet get counted in tcpOutSegs.
  2492. */
  2493. tp->snd_nxt = tp->write_seq;
  2494. tp->pushed_seq = tp->write_seq;
  2495. TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS);
  2496. /* Timer for repeating the SYN until an answer. */
  2497. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  2498. inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
  2499. return 0;
  2500. }
  2501. EXPORT_SYMBOL(tcp_connect);
  2502. /* Send out a delayed ack, the caller does the policy checking
  2503. * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check()
  2504. * for details.
  2505. */
  2506. void tcp_send_delayed_ack(struct sock *sk)
  2507. {
  2508. struct inet_connection_sock *icsk = inet_csk(sk);
  2509. int ato = icsk->icsk_ack.ato;
  2510. unsigned long timeout;
  2511. if (ato > TCP_DELACK_MIN) {
  2512. const struct tcp_sock *tp = tcp_sk(sk);
  2513. int max_ato = HZ / 2;
  2514. if (icsk->icsk_ack.pingpong ||
  2515. (icsk->icsk_ack.pending & ICSK_ACK_PUSHED))
  2516. max_ato = TCP_DELACK_MAX;
  2517. /* Slow path, intersegment interval is "high". */
  2518. /* If some rtt estimate is known, use it to bound delayed ack.
  2519. * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements
  2520. * directly.
  2521. */
  2522. if (tp->srtt) {
  2523. int rtt = max(tp->srtt >> 3, TCP_DELACK_MIN);
  2524. if (rtt < max_ato)
  2525. max_ato = rtt;
  2526. }
  2527. ato = min(ato, max_ato);
  2528. }
  2529. /* Stay within the limit we were given */
  2530. timeout = jiffies + ato;
  2531. /* Use new timeout only if there wasn't a older one earlier. */
  2532. if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
  2533. /* If delack timer was blocked or is about to expire,
  2534. * send ACK now.
  2535. */
  2536. if (icsk->icsk_ack.blocked ||
  2537. time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) {
  2538. tcp_send_ack(sk);
  2539. return;
  2540. }
  2541. if (!time_before(timeout, icsk->icsk_ack.timeout))
  2542. timeout = icsk->icsk_ack.timeout;
  2543. }
  2544. icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
  2545. icsk->icsk_ack.timeout = timeout;
  2546. sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
  2547. }
  2548. /* This routine sends an ack and also updates the window. */
  2549. void tcp_send_ack(struct sock *sk)
  2550. {
  2551. struct sk_buff *buff;
  2552. /* If we have been reset, we may not send again. */
  2553. if (sk->sk_state == TCP_CLOSE)
  2554. return;
  2555. /* We are not putting this on the write queue, so
  2556. * tcp_transmit_skb() will set the ownership to this
  2557. * sock.
  2558. */
  2559. buff = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC));
  2560. if (buff == NULL) {
  2561. inet_csk_schedule_ack(sk);
  2562. inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
  2563. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
  2564. TCP_DELACK_MAX, TCP_RTO_MAX);
  2565. return;
  2566. }
  2567. /* Reserve space for headers and prepare control bits. */
  2568. skb_reserve(buff, MAX_TCP_HEADER);
  2569. tcp_init_nondata_skb(buff, tcp_acceptable_seq(sk), TCPHDR_ACK);
  2570. /* Send it off, this clears delayed acks for us. */
  2571. TCP_SKB_CB(buff)->when = tcp_time_stamp;
  2572. tcp_transmit_skb(sk, buff, 0, sk_gfp_atomic(sk, GFP_ATOMIC));
  2573. }
  2574. /* This routine sends a packet with an out of date sequence
  2575. * number. It assumes the other end will try to ack it.
  2576. *
  2577. * Question: what should we make while urgent mode?
  2578. * 4.4BSD forces sending single byte of data. We cannot send
  2579. * out of window data, because we have SND.NXT==SND.MAX...
  2580. *
  2581. * Current solution: to send TWO zero-length segments in urgent mode:
  2582. * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
  2583. * out-of-date with SND.UNA-1 to probe window.
  2584. */
  2585. static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
  2586. {
  2587. struct tcp_sock *tp = tcp_sk(sk);
  2588. struct sk_buff *skb;
  2589. /* We don't queue it, tcp_transmit_skb() sets ownership. */
  2590. skb = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC));
  2591. if (skb == NULL)
  2592. return -1;
  2593. /* Reserve space for headers and set control bits. */
  2594. skb_reserve(skb, MAX_TCP_HEADER);
  2595. /* Use a previous sequence. This should cause the other
  2596. * end to send an ack. Don't queue or clone SKB, just
  2597. * send it.
  2598. */
  2599. tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK);
  2600. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  2601. return tcp_transmit_skb(sk, skb, 0, GFP_ATOMIC);
  2602. }
  2603. void tcp_send_window_probe(struct sock *sk)
  2604. {
  2605. if (sk->sk_state == TCP_ESTABLISHED) {
  2606. tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1;
  2607. tcp_sk(sk)->snd_nxt = tcp_sk(sk)->write_seq;
  2608. tcp_xmit_probe_skb(sk, 0);
  2609. }
  2610. }
  2611. /* Initiate keepalive or window probe from timer. */
  2612. int tcp_write_wakeup(struct sock *sk)
  2613. {
  2614. struct tcp_sock *tp = tcp_sk(sk);
  2615. struct sk_buff *skb;
  2616. if (sk->sk_state == TCP_CLOSE)
  2617. return -1;
  2618. if ((skb = tcp_send_head(sk)) != NULL &&
  2619. before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) {
  2620. int err;
  2621. unsigned int mss = tcp_current_mss(sk);
  2622. unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
  2623. if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
  2624. tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
  2625. /* We are probing the opening of a window
  2626. * but the window size is != 0
  2627. * must have been a result SWS avoidance ( sender )
  2628. */
  2629. if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
  2630. skb->len > mss) {
  2631. seg_size = min(seg_size, mss);
  2632. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
  2633. if (tcp_fragment(sk, skb, seg_size, mss))
  2634. return -1;
  2635. } else if (!tcp_skb_pcount(skb))
  2636. tcp_set_skb_tso_segs(sk, skb, mss);
  2637. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
  2638. TCP_SKB_CB(skb)->when = tcp_time_stamp;
  2639. err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
  2640. if (!err)
  2641. tcp_event_new_data_sent(sk, skb);
  2642. return err;
  2643. } else {
  2644. if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF))
  2645. tcp_xmit_probe_skb(sk, 1);
  2646. return tcp_xmit_probe_skb(sk, 0);
  2647. }
  2648. }
  2649. /* A window probe timeout has occurred. If window is not closed send
  2650. * a partial packet else a zero probe.
  2651. */
  2652. void tcp_send_probe0(struct sock *sk)
  2653. {
  2654. struct inet_connection_sock *icsk = inet_csk(sk);
  2655. struct tcp_sock *tp = tcp_sk(sk);
  2656. int err;
  2657. err = tcp_write_wakeup(sk);
  2658. if (tp->packets_out || !tcp_send_head(sk)) {
  2659. /* Cancel probe timer, if it is not required. */
  2660. icsk->icsk_probes_out = 0;
  2661. icsk->icsk_backoff = 0;
  2662. return;
  2663. }
  2664. if (err <= 0) {
  2665. if (icsk->icsk_backoff < sysctl_tcp_retries2)
  2666. icsk->icsk_backoff++;
  2667. icsk->icsk_probes_out++;
  2668. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  2669. min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
  2670. TCP_RTO_MAX);
  2671. } else {
  2672. /* If packet was not sent due to local congestion,
  2673. * do not backoff and do not remember icsk_probes_out.
  2674. * Let local senders to fight for local resources.
  2675. *
  2676. * Use accumulated backoff yet.
  2677. */
  2678. if (!icsk->icsk_probes_out)
  2679. icsk->icsk_probes_out = 1;
  2680. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  2681. min(icsk->icsk_rto << icsk->icsk_backoff,
  2682. TCP_RESOURCE_PROBE_INTERVAL),
  2683. TCP_RTO_MAX);
  2684. }
  2685. }