tcp_output.c 92 KB

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