tcp.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957
  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. * Fixes:
  21. * Alan Cox : Numerous verify_area() calls
  22. * Alan Cox : Set the ACK bit on a reset
  23. * Alan Cox : Stopped it crashing if it closed while
  24. * sk->inuse=1 and was trying to connect
  25. * (tcp_err()).
  26. * Alan Cox : All icmp error handling was broken
  27. * pointers passed where wrong and the
  28. * socket was looked up backwards. Nobody
  29. * tested any icmp error code obviously.
  30. * Alan Cox : tcp_err() now handled properly. It
  31. * wakes people on errors. poll
  32. * behaves and the icmp error race
  33. * has gone by moving it into sock.c
  34. * Alan Cox : tcp_send_reset() fixed to work for
  35. * everything not just packets for
  36. * unknown sockets.
  37. * Alan Cox : tcp option processing.
  38. * Alan Cox : Reset tweaked (still not 100%) [Had
  39. * syn rule wrong]
  40. * Herp Rosmanith : More reset fixes
  41. * Alan Cox : No longer acks invalid rst frames.
  42. * Acking any kind of RST is right out.
  43. * Alan Cox : Sets an ignore me flag on an rst
  44. * receive otherwise odd bits of prattle
  45. * escape still
  46. * Alan Cox : Fixed another acking RST frame bug.
  47. * Should stop LAN workplace lockups.
  48. * Alan Cox : Some tidyups using the new skb list
  49. * facilities
  50. * Alan Cox : sk->keepopen now seems to work
  51. * Alan Cox : Pulls options out correctly on accepts
  52. * Alan Cox : Fixed assorted sk->rqueue->next errors
  53. * Alan Cox : PSH doesn't end a TCP read. Switched a
  54. * bit to skb ops.
  55. * Alan Cox : Tidied tcp_data to avoid a potential
  56. * nasty.
  57. * Alan Cox : Added some better commenting, as the
  58. * tcp is hard to follow
  59. * Alan Cox : Removed incorrect check for 20 * psh
  60. * Michael O'Reilly : ack < copied bug fix.
  61. * Johannes Stille : Misc tcp fixes (not all in yet).
  62. * Alan Cox : FIN with no memory -> CRASH
  63. * Alan Cox : Added socket option proto entries.
  64. * Also added awareness of them to accept.
  65. * Alan Cox : Added TCP options (SOL_TCP)
  66. * Alan Cox : Switched wakeup calls to callbacks,
  67. * so the kernel can layer network
  68. * sockets.
  69. * Alan Cox : Use ip_tos/ip_ttl settings.
  70. * Alan Cox : Handle FIN (more) properly (we hope).
  71. * Alan Cox : RST frames sent on unsynchronised
  72. * state ack error.
  73. * Alan Cox : Put in missing check for SYN bit.
  74. * Alan Cox : Added tcp_select_window() aka NET2E
  75. * window non shrink trick.
  76. * Alan Cox : Added a couple of small NET2E timer
  77. * fixes
  78. * Charles Hedrick : TCP fixes
  79. * Toomas Tamm : TCP window fixes
  80. * Alan Cox : Small URG fix to rlogin ^C ack fight
  81. * Charles Hedrick : Rewrote most of it to actually work
  82. * Linus : Rewrote tcp_read() and URG handling
  83. * completely
  84. * Gerhard Koerting: Fixed some missing timer handling
  85. * Matthew Dillon : Reworked TCP machine states as per RFC
  86. * Gerhard Koerting: PC/TCP workarounds
  87. * Adam Caldwell : Assorted timer/timing errors
  88. * Matthew Dillon : Fixed another RST bug
  89. * Alan Cox : Move to kernel side addressing changes.
  90. * Alan Cox : Beginning work on TCP fastpathing
  91. * (not yet usable)
  92. * Arnt Gulbrandsen: Turbocharged tcp_check() routine.
  93. * Alan Cox : TCP fast path debugging
  94. * Alan Cox : Window clamping
  95. * Michael Riepe : Bug in tcp_check()
  96. * Matt Dillon : More TCP improvements and RST bug fixes
  97. * Matt Dillon : Yet more small nasties remove from the
  98. * TCP code (Be very nice to this man if
  99. * tcp finally works 100%) 8)
  100. * Alan Cox : BSD accept semantics.
  101. * Alan Cox : Reset on closedown bug.
  102. * Peter De Schrijver : ENOTCONN check missing in tcp_sendto().
  103. * Michael Pall : Handle poll() after URG properly in
  104. * all cases.
  105. * Michael Pall : Undo the last fix in tcp_read_urg()
  106. * (multi URG PUSH broke rlogin).
  107. * Michael Pall : Fix the multi URG PUSH problem in
  108. * tcp_readable(), poll() after URG
  109. * works now.
  110. * Michael Pall : recv(...,MSG_OOB) never blocks in the
  111. * BSD api.
  112. * Alan Cox : Changed the semantics of sk->socket to
  113. * fix a race and a signal problem with
  114. * accept() and async I/O.
  115. * Alan Cox : Relaxed the rules on tcp_sendto().
  116. * Yury Shevchuk : Really fixed accept() blocking problem.
  117. * Craig I. Hagan : Allow for BSD compatible TIME_WAIT for
  118. * clients/servers which listen in on
  119. * fixed ports.
  120. * Alan Cox : Cleaned the above up and shrank it to
  121. * a sensible code size.
  122. * Alan Cox : Self connect lockup fix.
  123. * Alan Cox : No connect to multicast.
  124. * Ross Biro : Close unaccepted children on master
  125. * socket close.
  126. * Alan Cox : Reset tracing code.
  127. * Alan Cox : Spurious resets on shutdown.
  128. * Alan Cox : Giant 15 minute/60 second timer error
  129. * Alan Cox : Small whoops in polling before an
  130. * accept.
  131. * Alan Cox : Kept the state trace facility since
  132. * it's handy for debugging.
  133. * Alan Cox : More reset handler fixes.
  134. * Alan Cox : Started rewriting the code based on
  135. * the RFC's for other useful protocol
  136. * references see: Comer, KA9Q NOS, and
  137. * for a reference on the difference
  138. * between specifications and how BSD
  139. * works see the 4.4lite source.
  140. * A.N.Kuznetsov : Don't time wait on completion of tidy
  141. * close.
  142. * Linus Torvalds : Fin/Shutdown & copied_seq changes.
  143. * Linus Torvalds : Fixed BSD port reuse to work first syn
  144. * Alan Cox : Reimplemented timers as per the RFC
  145. * and using multiple timers for sanity.
  146. * Alan Cox : Small bug fixes, and a lot of new
  147. * comments.
  148. * Alan Cox : Fixed dual reader crash by locking
  149. * the buffers (much like datagram.c)
  150. * Alan Cox : Fixed stuck sockets in probe. A probe
  151. * now gets fed up of retrying without
  152. * (even a no space) answer.
  153. * Alan Cox : Extracted closing code better
  154. * Alan Cox : Fixed the closing state machine to
  155. * resemble the RFC.
  156. * Alan Cox : More 'per spec' fixes.
  157. * Jorge Cwik : Even faster checksumming.
  158. * Alan Cox : tcp_data() doesn't ack illegal PSH
  159. * only frames. At least one pc tcp stack
  160. * generates them.
  161. * Alan Cox : Cache last socket.
  162. * Alan Cox : Per route irtt.
  163. * Matt Day : poll()->select() match BSD precisely on error
  164. * Alan Cox : New buffers
  165. * Marc Tamsky : Various sk->prot->retransmits and
  166. * sk->retransmits misupdating fixed.
  167. * Fixed tcp_write_timeout: stuck close,
  168. * and TCP syn retries gets used now.
  169. * Mark Yarvis : In tcp_read_wakeup(), don't send an
  170. * ack if state is TCP_CLOSED.
  171. * Alan Cox : Look up device on a retransmit - routes may
  172. * change. Doesn't yet cope with MSS shrink right
  173. * but it's a start!
  174. * Marc Tamsky : Closing in closing fixes.
  175. * Mike Shaver : RFC1122 verifications.
  176. * Alan Cox : rcv_saddr errors.
  177. * Alan Cox : Block double connect().
  178. * Alan Cox : Small hooks for enSKIP.
  179. * Alexey Kuznetsov: Path MTU discovery.
  180. * Alan Cox : Support soft errors.
  181. * Alan Cox : Fix MTU discovery pathological case
  182. * when the remote claims no mtu!
  183. * Marc Tamsky : TCP_CLOSE fix.
  184. * Colin (G3TNE) : Send a reset on syn ack replies in
  185. * window but wrong (fixes NT lpd problems)
  186. * Pedro Roque : Better TCP window handling, delayed ack.
  187. * Joerg Reuter : No modification of locked buffers in
  188. * tcp_do_retransmit()
  189. * Eric Schenk : Changed receiver side silly window
  190. * avoidance algorithm to BSD style
  191. * algorithm. This doubles throughput
  192. * against machines running Solaris,
  193. * and seems to result in general
  194. * improvement.
  195. * Stefan Magdalinski : adjusted tcp_readable() to fix FIONREAD
  196. * Willy Konynenberg : Transparent proxying support.
  197. * Mike McLagan : Routing by source
  198. * Keith Owens : Do proper merging with partial SKB's in
  199. * tcp_do_sendmsg to avoid burstiness.
  200. * Eric Schenk : Fix fast close down bug with
  201. * shutdown() followed by close().
  202. * Andi Kleen : Make poll agree with SIGIO
  203. * Salvatore Sanfilippo : Support SO_LINGER with linger == 1 and
  204. * lingertime == 0 (RFC 793 ABORT Call)
  205. * Hirokazu Takahashi : Use copy_from_user() instead of
  206. * csum_and_copy_from_user() if possible.
  207. *
  208. * This program is free software; you can redistribute it and/or
  209. * modify it under the terms of the GNU General Public License
  210. * as published by the Free Software Foundation; either version
  211. * 2 of the License, or(at your option) any later version.
  212. *
  213. * Description of States:
  214. *
  215. * TCP_SYN_SENT sent a connection request, waiting for ack
  216. *
  217. * TCP_SYN_RECV received a connection request, sent ack,
  218. * waiting for final ack in three-way handshake.
  219. *
  220. * TCP_ESTABLISHED connection established
  221. *
  222. * TCP_FIN_WAIT1 our side has shutdown, waiting to complete
  223. * transmission of remaining buffered data
  224. *
  225. * TCP_FIN_WAIT2 all buffered data sent, waiting for remote
  226. * to shutdown
  227. *
  228. * TCP_CLOSING both sides have shutdown but we still have
  229. * data we have to finish sending
  230. *
  231. * TCP_TIME_WAIT timeout to catch resent junk before entering
  232. * closed, can only be entered from FIN_WAIT2
  233. * or CLOSING. Required because the other end
  234. * may not have gotten our last ACK causing it
  235. * to retransmit the data packet (which we ignore)
  236. *
  237. * TCP_CLOSE_WAIT remote side has shutdown and is waiting for
  238. * us to finish writing our data and to shutdown
  239. * (we have to close() to move on to LAST_ACK)
  240. *
  241. * TCP_LAST_ACK out side has shutdown after remote has
  242. * shutdown. There may still be data in our
  243. * buffer that we have to finish sending
  244. *
  245. * TCP_CLOSE socket is finished
  246. */
  247. #define pr_fmt(fmt) "TCP: " fmt
  248. #include <crypto/hash.h>
  249. #include <linux/kernel.h>
  250. #include <linux/module.h>
  251. #include <linux/types.h>
  252. #include <linux/fcntl.h>
  253. #include <linux/poll.h>
  254. #include <linux/inet_diag.h>
  255. #include <linux/init.h>
  256. #include <linux/fs.h>
  257. #include <linux/skbuff.h>
  258. #include <linux/scatterlist.h>
  259. #include <linux/splice.h>
  260. #include <linux/net.h>
  261. #include <linux/socket.h>
  262. #include <linux/random.h>
  263. #include <linux/bootmem.h>
  264. #include <linux/highmem.h>
  265. #include <linux/swap.h>
  266. #include <linux/cache.h>
  267. #include <linux/err.h>
  268. #include <linux/time.h>
  269. #include <linux/slab.h>
  270. #include <linux/errqueue.h>
  271. #include <linux/static_key.h>
  272. #include <net/icmp.h>
  273. #include <net/inet_common.h>
  274. #include <net/tcp.h>
  275. #include <net/xfrm.h>
  276. #include <net/ip.h>
  277. #include <net/sock.h>
  278. #include <linux/uaccess.h>
  279. #include <asm/ioctls.h>
  280. #include <net/busy_poll.h>
  281. struct percpu_counter tcp_orphan_count;
  282. EXPORT_SYMBOL_GPL(tcp_orphan_count);
  283. long sysctl_tcp_mem[3] __read_mostly;
  284. EXPORT_SYMBOL(sysctl_tcp_mem);
  285. atomic_long_t tcp_memory_allocated; /* Current allocated memory. */
  286. EXPORT_SYMBOL(tcp_memory_allocated);
  287. #if IS_ENABLED(CONFIG_SMC)
  288. DEFINE_STATIC_KEY_FALSE(tcp_have_smc);
  289. EXPORT_SYMBOL(tcp_have_smc);
  290. #endif
  291. /*
  292. * Current number of TCP sockets.
  293. */
  294. struct percpu_counter tcp_sockets_allocated;
  295. EXPORT_SYMBOL(tcp_sockets_allocated);
  296. /*
  297. * TCP splice context
  298. */
  299. struct tcp_splice_state {
  300. struct pipe_inode_info *pipe;
  301. size_t len;
  302. unsigned int flags;
  303. };
  304. /*
  305. * Pressure flag: try to collapse.
  306. * Technical note: it is used by multiple contexts non atomically.
  307. * All the __sk_mem_schedule() is of this nature: accounting
  308. * is strict, actions are advisory and have some latency.
  309. */
  310. unsigned long tcp_memory_pressure __read_mostly;
  311. EXPORT_SYMBOL_GPL(tcp_memory_pressure);
  312. void tcp_enter_memory_pressure(struct sock *sk)
  313. {
  314. unsigned long val;
  315. if (READ_ONCE(tcp_memory_pressure))
  316. return;
  317. val = jiffies;
  318. if (!val)
  319. val--;
  320. if (!cmpxchg(&tcp_memory_pressure, 0, val))
  321. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMEMORYPRESSURES);
  322. }
  323. EXPORT_SYMBOL_GPL(tcp_enter_memory_pressure);
  324. void tcp_leave_memory_pressure(struct sock *sk)
  325. {
  326. unsigned long val;
  327. if (!READ_ONCE(tcp_memory_pressure))
  328. return;
  329. val = xchg(&tcp_memory_pressure, 0);
  330. if (val)
  331. NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPMEMORYPRESSURESCHRONO,
  332. jiffies_to_msecs(jiffies - val));
  333. }
  334. EXPORT_SYMBOL_GPL(tcp_leave_memory_pressure);
  335. /* Convert seconds to retransmits based on initial and max timeout */
  336. static u8 secs_to_retrans(int seconds, int timeout, int rto_max)
  337. {
  338. u8 res = 0;
  339. if (seconds > 0) {
  340. int period = timeout;
  341. res = 1;
  342. while (seconds > period && res < 255) {
  343. res++;
  344. timeout <<= 1;
  345. if (timeout > rto_max)
  346. timeout = rto_max;
  347. period += timeout;
  348. }
  349. }
  350. return res;
  351. }
  352. /* Convert retransmits to seconds based on initial and max timeout */
  353. static int retrans_to_secs(u8 retrans, int timeout, int rto_max)
  354. {
  355. int period = 0;
  356. if (retrans > 0) {
  357. period = timeout;
  358. while (--retrans) {
  359. timeout <<= 1;
  360. if (timeout > rto_max)
  361. timeout = rto_max;
  362. period += timeout;
  363. }
  364. }
  365. return period;
  366. }
  367. static u64 tcp_compute_delivery_rate(const struct tcp_sock *tp)
  368. {
  369. u32 rate = READ_ONCE(tp->rate_delivered);
  370. u32 intv = READ_ONCE(tp->rate_interval_us);
  371. u64 rate64 = 0;
  372. if (rate && intv) {
  373. rate64 = (u64)rate * tp->mss_cache * USEC_PER_SEC;
  374. do_div(rate64, intv);
  375. }
  376. return rate64;
  377. }
  378. /* Address-family independent initialization for a tcp_sock.
  379. *
  380. * NOTE: A lot of things set to zero explicitly by call to
  381. * sk_alloc() so need not be done here.
  382. */
  383. void tcp_init_sock(struct sock *sk)
  384. {
  385. struct inet_connection_sock *icsk = inet_csk(sk);
  386. struct tcp_sock *tp = tcp_sk(sk);
  387. tp->out_of_order_queue = RB_ROOT;
  388. sk->tcp_rtx_queue = RB_ROOT;
  389. tcp_init_xmit_timers(sk);
  390. INIT_LIST_HEAD(&tp->tsq_node);
  391. INIT_LIST_HEAD(&tp->tsorted_sent_queue);
  392. icsk->icsk_rto = TCP_TIMEOUT_INIT;
  393. tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT);
  394. minmax_reset(&tp->rtt_min, tcp_jiffies32, ~0U);
  395. /* So many TCP implementations out there (incorrectly) count the
  396. * initial SYN frame in their delayed-ACK and congestion control
  397. * algorithms that we must have the following bandaid to talk
  398. * efficiently to them. -DaveM
  399. */
  400. tp->snd_cwnd = TCP_INIT_CWND;
  401. /* There's a bubble in the pipe until at least the first ACK. */
  402. tp->app_limited = ~0U;
  403. /* See draft-stevens-tcpca-spec-01 for discussion of the
  404. * initialization of these values.
  405. */
  406. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  407. tp->snd_cwnd_clamp = ~0;
  408. tp->mss_cache = TCP_MSS_DEFAULT;
  409. tp->reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering;
  410. tcp_assign_congestion_control(sk);
  411. tp->tsoffset = 0;
  412. tp->rack.reo_wnd_steps = 1;
  413. sk->sk_state = TCP_CLOSE;
  414. sk->sk_write_space = sk_stream_write_space;
  415. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  416. icsk->icsk_sync_mss = tcp_sync_mss;
  417. sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
  418. sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
  419. sk_sockets_allocated_inc(sk);
  420. sk->sk_route_forced_caps = NETIF_F_GSO;
  421. }
  422. EXPORT_SYMBOL(tcp_init_sock);
  423. void tcp_init_transfer(struct sock *sk, int bpf_op)
  424. {
  425. struct inet_connection_sock *icsk = inet_csk(sk);
  426. tcp_mtup_init(sk);
  427. icsk->icsk_af_ops->rebuild_header(sk);
  428. tcp_init_metrics(sk);
  429. tcp_call_bpf(sk, bpf_op, 0, NULL);
  430. tcp_init_congestion_control(sk);
  431. tcp_init_buffer_space(sk);
  432. }
  433. static void tcp_tx_timestamp(struct sock *sk, u16 tsflags)
  434. {
  435. struct sk_buff *skb = tcp_write_queue_tail(sk);
  436. if (tsflags && skb) {
  437. struct skb_shared_info *shinfo = skb_shinfo(skb);
  438. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  439. sock_tx_timestamp(sk, tsflags, &shinfo->tx_flags);
  440. if (tsflags & SOF_TIMESTAMPING_TX_ACK)
  441. tcb->txstamp_ack = 1;
  442. if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK)
  443. shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
  444. }
  445. }
  446. static inline bool tcp_stream_is_readable(const struct tcp_sock *tp,
  447. int target, struct sock *sk)
  448. {
  449. int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
  450. if (avail > 0) {
  451. if (avail >= target)
  452. return true;
  453. if (tcp_rmem_pressure(sk))
  454. return true;
  455. if (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss)
  456. return true;
  457. }
  458. if (sk->sk_prot->stream_memory_read)
  459. return sk->sk_prot->stream_memory_read(sk);
  460. return false;
  461. }
  462. /*
  463. * Wait for a TCP event.
  464. *
  465. * Note that we don't need to lock the socket, as the upper poll layers
  466. * take care of normal races (between the test and the event) and we don't
  467. * go look at any of the socket buffers directly.
  468. */
  469. __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
  470. {
  471. __poll_t mask;
  472. struct sock *sk = sock->sk;
  473. const struct tcp_sock *tp = tcp_sk(sk);
  474. int state;
  475. sock_poll_wait(file, sock, wait);
  476. state = inet_sk_state_load(sk);
  477. if (state == TCP_LISTEN)
  478. return inet_csk_listen_poll(sk);
  479. /* Socket is not locked. We are protected from async events
  480. * by poll logic and correct handling of state changes
  481. * made by other threads is impossible in any case.
  482. */
  483. mask = 0;
  484. /*
  485. * EPOLLHUP is certainly not done right. But poll() doesn't
  486. * have a notion of HUP in just one direction, and for a
  487. * socket the read side is more interesting.
  488. *
  489. * Some poll() documentation says that EPOLLHUP is incompatible
  490. * with the EPOLLOUT/POLLWR flags, so somebody should check this
  491. * all. But careful, it tends to be safer to return too many
  492. * bits than too few, and you can easily break real applications
  493. * if you don't tell them that something has hung up!
  494. *
  495. * Check-me.
  496. *
  497. * Check number 1. EPOLLHUP is _UNMASKABLE_ event (see UNIX98 and
  498. * our fs/select.c). It means that after we received EOF,
  499. * poll always returns immediately, making impossible poll() on write()
  500. * in state CLOSE_WAIT. One solution is evident --- to set EPOLLHUP
  501. * if and only if shutdown has been made in both directions.
  502. * Actually, it is interesting to look how Solaris and DUX
  503. * solve this dilemma. I would prefer, if EPOLLHUP were maskable,
  504. * then we could set it on SND_SHUTDOWN. BTW examples given
  505. * in Stevens' books assume exactly this behaviour, it explains
  506. * why EPOLLHUP is incompatible with EPOLLOUT. --ANK
  507. *
  508. * NOTE. Check for TCP_CLOSE is added. The goal is to prevent
  509. * blocking on fresh not-connected or disconnected socket. --ANK
  510. */
  511. if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
  512. mask |= EPOLLHUP;
  513. if (sk->sk_shutdown & RCV_SHUTDOWN)
  514. mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
  515. /* Connected or passive Fast Open socket? */
  516. if (state != TCP_SYN_SENT &&
  517. (state != TCP_SYN_RECV || tp->fastopen_rsk)) {
  518. int target = sock_rcvlowat(sk, 0, INT_MAX);
  519. if (tp->urg_seq == READ_ONCE(tp->copied_seq) &&
  520. !sock_flag(sk, SOCK_URGINLINE) &&
  521. tp->urg_data)
  522. target++;
  523. if (tcp_stream_is_readable(tp, target, sk))
  524. mask |= EPOLLIN | EPOLLRDNORM;
  525. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  526. if (sk_stream_is_writeable(sk)) {
  527. mask |= EPOLLOUT | EPOLLWRNORM;
  528. } else { /* send SIGIO later */
  529. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  530. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  531. /* Race breaker. If space is freed after
  532. * wspace test but before the flags are set,
  533. * IO signal will be lost. Memory barrier
  534. * pairs with the input side.
  535. */
  536. smp_mb__after_atomic();
  537. if (sk_stream_is_writeable(sk))
  538. mask |= EPOLLOUT | EPOLLWRNORM;
  539. }
  540. } else
  541. mask |= EPOLLOUT | EPOLLWRNORM;
  542. if (tp->urg_data & TCP_URG_VALID)
  543. mask |= EPOLLPRI;
  544. } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) {
  545. /* Active TCP fastopen socket with defer_connect
  546. * Return EPOLLOUT so application can call write()
  547. * in order for kernel to generate SYN+data
  548. */
  549. mask |= EPOLLOUT | EPOLLWRNORM;
  550. }
  551. /* This barrier is coupled with smp_wmb() in tcp_reset() */
  552. smp_rmb();
  553. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  554. mask |= EPOLLERR;
  555. return mask;
  556. }
  557. EXPORT_SYMBOL(tcp_poll);
  558. int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  559. {
  560. struct tcp_sock *tp = tcp_sk(sk);
  561. int answ;
  562. bool slow;
  563. switch (cmd) {
  564. case SIOCINQ:
  565. if (sk->sk_state == TCP_LISTEN)
  566. return -EINVAL;
  567. slow = lock_sock_fast(sk);
  568. answ = tcp_inq(sk);
  569. unlock_sock_fast(sk, slow);
  570. break;
  571. case SIOCATMARK:
  572. answ = tp->urg_data && tp->urg_seq == READ_ONCE(tp->copied_seq);
  573. break;
  574. case SIOCOUTQ:
  575. if (sk->sk_state == TCP_LISTEN)
  576. return -EINVAL;
  577. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
  578. answ = 0;
  579. else
  580. answ = READ_ONCE(tp->write_seq) - tp->snd_una;
  581. break;
  582. case SIOCOUTQNSD:
  583. if (sk->sk_state == TCP_LISTEN)
  584. return -EINVAL;
  585. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
  586. answ = 0;
  587. else
  588. answ = READ_ONCE(tp->write_seq) - tp->snd_nxt;
  589. break;
  590. default:
  591. return -ENOIOCTLCMD;
  592. }
  593. return put_user(answ, (int __user *)arg);
  594. }
  595. EXPORT_SYMBOL(tcp_ioctl);
  596. static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb)
  597. {
  598. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
  599. tp->pushed_seq = tp->write_seq;
  600. }
  601. static inline bool forced_push(const struct tcp_sock *tp)
  602. {
  603. return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1));
  604. }
  605. static void skb_entail(struct sock *sk, struct sk_buff *skb)
  606. {
  607. struct tcp_sock *tp = tcp_sk(sk);
  608. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  609. skb->csum = 0;
  610. tcb->seq = tcb->end_seq = tp->write_seq;
  611. tcb->tcp_flags = TCPHDR_ACK;
  612. tcb->sacked = 0;
  613. __skb_header_release(skb);
  614. tcp_add_write_queue_tail(sk, skb);
  615. sk->sk_wmem_queued += skb->truesize;
  616. sk_mem_charge(sk, skb->truesize);
  617. if (tp->nonagle & TCP_NAGLE_PUSH)
  618. tp->nonagle &= ~TCP_NAGLE_PUSH;
  619. tcp_slow_start_after_idle_check(sk);
  620. }
  621. static inline void tcp_mark_urg(struct tcp_sock *tp, int flags)
  622. {
  623. if (flags & MSG_OOB)
  624. tp->snd_up = tp->write_seq;
  625. }
  626. /* If a not yet filled skb is pushed, do not send it if
  627. * we have data packets in Qdisc or NIC queues :
  628. * Because TX completion will happen shortly, it gives a chance
  629. * to coalesce future sendmsg() payload into this skb, without
  630. * need for a timer, and with no latency trade off.
  631. * As packets containing data payload have a bigger truesize
  632. * than pure acks (dataless) packets, the last checks prevent
  633. * autocorking if we only have an ACK in Qdisc/NIC queues,
  634. * or if TX completion was delayed after we processed ACK packet.
  635. */
  636. static bool tcp_should_autocork(struct sock *sk, struct sk_buff *skb,
  637. int size_goal)
  638. {
  639. return skb->len < size_goal &&
  640. sock_net(sk)->ipv4.sysctl_tcp_autocorking &&
  641. !tcp_rtx_queue_empty(sk) &&
  642. refcount_read(&sk->sk_wmem_alloc) > skb->truesize;
  643. }
  644. static void tcp_push(struct sock *sk, int flags, int mss_now,
  645. int nonagle, int size_goal)
  646. {
  647. struct tcp_sock *tp = tcp_sk(sk);
  648. struct sk_buff *skb;
  649. skb = tcp_write_queue_tail(sk);
  650. if (!skb)
  651. return;
  652. if (!(flags & MSG_MORE) || forced_push(tp))
  653. tcp_mark_push(tp, skb);
  654. tcp_mark_urg(tp, flags);
  655. if (tcp_should_autocork(sk, skb, size_goal)) {
  656. /* avoid atomic op if TSQ_THROTTLED bit is already set */
  657. if (!test_bit(TSQ_THROTTLED, &sk->sk_tsq_flags)) {
  658. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAUTOCORKING);
  659. set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags);
  660. }
  661. /* It is possible TX completion already happened
  662. * before we set TSQ_THROTTLED.
  663. */
  664. if (refcount_read(&sk->sk_wmem_alloc) > skb->truesize)
  665. return;
  666. }
  667. if (flags & MSG_MORE)
  668. nonagle = TCP_NAGLE_CORK;
  669. __tcp_push_pending_frames(sk, mss_now, nonagle);
  670. }
  671. static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  672. unsigned int offset, size_t len)
  673. {
  674. struct tcp_splice_state *tss = rd_desc->arg.data;
  675. int ret;
  676. ret = skb_splice_bits(skb, skb->sk, offset, tss->pipe,
  677. min(rd_desc->count, len), tss->flags);
  678. if (ret > 0)
  679. rd_desc->count -= ret;
  680. return ret;
  681. }
  682. static int __tcp_splice_read(struct sock *sk, struct tcp_splice_state *tss)
  683. {
  684. /* Store TCP splice context information in read_descriptor_t. */
  685. read_descriptor_t rd_desc = {
  686. .arg.data = tss,
  687. .count = tss->len,
  688. };
  689. return tcp_read_sock(sk, &rd_desc, tcp_splice_data_recv);
  690. }
  691. /**
  692. * tcp_splice_read - splice data from TCP socket to a pipe
  693. * @sock: socket to splice from
  694. * @ppos: position (not valid)
  695. * @pipe: pipe to splice to
  696. * @len: number of bytes to splice
  697. * @flags: splice modifier flags
  698. *
  699. * Description:
  700. * Will read pages from given socket and fill them into a pipe.
  701. *
  702. **/
  703. ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
  704. struct pipe_inode_info *pipe, size_t len,
  705. unsigned int flags)
  706. {
  707. struct sock *sk = sock->sk;
  708. struct tcp_splice_state tss = {
  709. .pipe = pipe,
  710. .len = len,
  711. .flags = flags,
  712. };
  713. long timeo;
  714. ssize_t spliced;
  715. int ret;
  716. sock_rps_record_flow(sk);
  717. /*
  718. * We can't seek on a socket input
  719. */
  720. if (unlikely(*ppos))
  721. return -ESPIPE;
  722. ret = spliced = 0;
  723. lock_sock(sk);
  724. timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK);
  725. while (tss.len) {
  726. ret = __tcp_splice_read(sk, &tss);
  727. if (ret < 0)
  728. break;
  729. else if (!ret) {
  730. if (spliced)
  731. break;
  732. if (sock_flag(sk, SOCK_DONE))
  733. break;
  734. if (sk->sk_err) {
  735. ret = sock_error(sk);
  736. break;
  737. }
  738. if (sk->sk_shutdown & RCV_SHUTDOWN)
  739. break;
  740. if (sk->sk_state == TCP_CLOSE) {
  741. /*
  742. * This occurs when user tries to read
  743. * from never connected socket.
  744. */
  745. ret = -ENOTCONN;
  746. break;
  747. }
  748. if (!timeo) {
  749. ret = -EAGAIN;
  750. break;
  751. }
  752. /* if __tcp_splice_read() got nothing while we have
  753. * an skb in receive queue, we do not want to loop.
  754. * This might happen with URG data.
  755. */
  756. if (!skb_queue_empty(&sk->sk_receive_queue))
  757. break;
  758. sk_wait_data(sk, &timeo, NULL);
  759. if (signal_pending(current)) {
  760. ret = sock_intr_errno(timeo);
  761. break;
  762. }
  763. continue;
  764. }
  765. tss.len -= ret;
  766. spliced += ret;
  767. if (!timeo)
  768. break;
  769. release_sock(sk);
  770. lock_sock(sk);
  771. if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
  772. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  773. signal_pending(current))
  774. break;
  775. }
  776. release_sock(sk);
  777. if (spliced)
  778. return spliced;
  779. return ret;
  780. }
  781. EXPORT_SYMBOL(tcp_splice_read);
  782. struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp,
  783. bool force_schedule)
  784. {
  785. struct sk_buff *skb;
  786. /* The TCP header must be at least 32-bit aligned. */
  787. size = ALIGN(size, 4);
  788. if (unlikely(tcp_under_memory_pressure(sk)))
  789. sk_mem_reclaim_partial(sk);
  790. skb = alloc_skb_fclone(size + sk->sk_prot->max_header, gfp);
  791. if (likely(skb)) {
  792. bool mem_scheduled;
  793. if (force_schedule) {
  794. mem_scheduled = true;
  795. sk_forced_mem_schedule(sk, skb->truesize);
  796. } else {
  797. mem_scheduled = sk_wmem_schedule(sk, skb->truesize);
  798. }
  799. if (likely(mem_scheduled)) {
  800. skb_reserve(skb, sk->sk_prot->max_header);
  801. /*
  802. * Make sure that we have exactly size bytes
  803. * available to the caller, no more, no less.
  804. */
  805. skb->reserved_tailroom = skb->end - skb->tail - size;
  806. INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
  807. return skb;
  808. }
  809. __kfree_skb(skb);
  810. } else {
  811. sk->sk_prot->enter_memory_pressure(sk);
  812. sk_stream_moderate_sndbuf(sk);
  813. }
  814. return NULL;
  815. }
  816. static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now,
  817. int large_allowed)
  818. {
  819. struct tcp_sock *tp = tcp_sk(sk);
  820. u32 new_size_goal, size_goal;
  821. if (!large_allowed)
  822. return mss_now;
  823. /* Note : tcp_tso_autosize() will eventually split this later */
  824. new_size_goal = sk->sk_gso_max_size - 1 - MAX_TCP_HEADER;
  825. new_size_goal = tcp_bound_to_half_wnd(tp, new_size_goal);
  826. /* We try hard to avoid divides here */
  827. size_goal = tp->gso_segs * mss_now;
  828. if (unlikely(new_size_goal < size_goal ||
  829. new_size_goal >= size_goal + mss_now)) {
  830. tp->gso_segs = min_t(u16, new_size_goal / mss_now,
  831. sk->sk_gso_max_segs);
  832. size_goal = tp->gso_segs * mss_now;
  833. }
  834. return max(size_goal, mss_now);
  835. }
  836. static int tcp_send_mss(struct sock *sk, int *size_goal, int flags)
  837. {
  838. int mss_now;
  839. mss_now = tcp_current_mss(sk);
  840. *size_goal = tcp_xmit_size_goal(sk, mss_now, !(flags & MSG_OOB));
  841. return mss_now;
  842. }
  843. /* In some cases, both sendpage() and sendmsg() could have added
  844. * an skb to the write queue, but failed adding payload on it.
  845. * We need to remove it to consume less memory, but more
  846. * importantly be able to generate EPOLLOUT for Edge Trigger epoll()
  847. * users.
  848. */
  849. static void tcp_remove_empty_skb(struct sock *sk, struct sk_buff *skb)
  850. {
  851. if (skb && !skb->len) {
  852. tcp_unlink_write_queue(skb, sk);
  853. if (tcp_write_queue_empty(sk))
  854. tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
  855. sk_wmem_free_skb(sk, skb);
  856. }
  857. }
  858. ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  859. size_t size, int flags)
  860. {
  861. struct tcp_sock *tp = tcp_sk(sk);
  862. int mss_now, size_goal;
  863. int err;
  864. ssize_t copied;
  865. long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  866. /* Wait for a connection to finish. One exception is TCP Fast Open
  867. * (passive side) where data is allowed to be sent before a connection
  868. * is fully established.
  869. */
  870. if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) &&
  871. !tcp_passive_fastopen(sk)) {
  872. err = sk_stream_wait_connect(sk, &timeo);
  873. if (err != 0)
  874. goto out_err;
  875. }
  876. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  877. mss_now = tcp_send_mss(sk, &size_goal, flags);
  878. copied = 0;
  879. err = -EPIPE;
  880. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  881. goto out_err;
  882. while (size > 0) {
  883. struct sk_buff *skb = tcp_write_queue_tail(sk);
  884. int copy, i;
  885. bool can_coalesce;
  886. if (!skb || (copy = size_goal - skb->len) <= 0 ||
  887. !tcp_skb_can_collapse_to(skb)) {
  888. new_segment:
  889. if (!sk_stream_memory_free(sk))
  890. goto wait_for_sndbuf;
  891. skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation,
  892. tcp_rtx_and_write_queues_empty(sk));
  893. if (!skb)
  894. goto wait_for_memory;
  895. skb_entail(sk, skb);
  896. copy = size_goal;
  897. }
  898. if (copy > size)
  899. copy = size;
  900. i = skb_shinfo(skb)->nr_frags;
  901. can_coalesce = skb_can_coalesce(skb, i, page, offset);
  902. if (!can_coalesce && i >= sysctl_max_skb_frags) {
  903. tcp_mark_push(tp, skb);
  904. goto new_segment;
  905. }
  906. if (!sk_wmem_schedule(sk, copy))
  907. goto wait_for_memory;
  908. if (can_coalesce) {
  909. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  910. } else {
  911. get_page(page);
  912. skb_fill_page_desc(skb, i, page, offset, copy);
  913. }
  914. if (!(flags & MSG_NO_SHARED_FRAGS))
  915. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  916. skb->len += copy;
  917. skb->data_len += copy;
  918. skb->truesize += copy;
  919. sk->sk_wmem_queued += copy;
  920. sk_mem_charge(sk, copy);
  921. skb->ip_summed = CHECKSUM_PARTIAL;
  922. WRITE_ONCE(tp->write_seq, tp->write_seq + copy);
  923. TCP_SKB_CB(skb)->end_seq += copy;
  924. tcp_skb_pcount_set(skb, 0);
  925. if (!copied)
  926. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
  927. copied += copy;
  928. offset += copy;
  929. size -= copy;
  930. if (!size)
  931. goto out;
  932. if (skb->len < size_goal || (flags & MSG_OOB))
  933. continue;
  934. if (forced_push(tp)) {
  935. tcp_mark_push(tp, skb);
  936. __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
  937. } else if (skb == tcp_send_head(sk))
  938. tcp_push_one(sk, mss_now);
  939. continue;
  940. wait_for_sndbuf:
  941. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  942. wait_for_memory:
  943. tcp_push(sk, flags & ~MSG_MORE, mss_now,
  944. TCP_NAGLE_PUSH, size_goal);
  945. err = sk_stream_wait_memory(sk, &timeo);
  946. if (err != 0)
  947. goto do_error;
  948. mss_now = tcp_send_mss(sk, &size_goal, flags);
  949. }
  950. out:
  951. if (copied) {
  952. tcp_tx_timestamp(sk, sk->sk_tsflags);
  953. if (!(flags & MSG_SENDPAGE_NOTLAST))
  954. tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
  955. }
  956. return copied;
  957. do_error:
  958. tcp_remove_empty_skb(sk, tcp_write_queue_tail(sk));
  959. if (copied)
  960. goto out;
  961. out_err:
  962. /* make sure we wake any epoll edge trigger waiter */
  963. if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) {
  964. sk->sk_write_space(sk);
  965. tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
  966. }
  967. return sk_stream_error(sk, flags, err);
  968. }
  969. EXPORT_SYMBOL_GPL(do_tcp_sendpages);
  970. int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
  971. size_t size, int flags)
  972. {
  973. if (!(sk->sk_route_caps & NETIF_F_SG))
  974. return sock_no_sendpage_locked(sk, page, offset, size, flags);
  975. tcp_rate_check_app_limited(sk); /* is sending application-limited? */
  976. return do_tcp_sendpages(sk, page, offset, size, flags);
  977. }
  978. EXPORT_SYMBOL_GPL(tcp_sendpage_locked);
  979. int tcp_sendpage(struct sock *sk, struct page *page, int offset,
  980. size_t size, int flags)
  981. {
  982. int ret;
  983. lock_sock(sk);
  984. ret = tcp_sendpage_locked(sk, page, offset, size, flags);
  985. release_sock(sk);
  986. return ret;
  987. }
  988. EXPORT_SYMBOL(tcp_sendpage);
  989. /* Do not bother using a page frag for very small frames.
  990. * But use this heuristic only for the first skb in write queue.
  991. *
  992. * Having no payload in skb->head allows better SACK shifting
  993. * in tcp_shift_skb_data(), reducing sack/rack overhead, because
  994. * write queue has less skbs.
  995. * Each skb can hold up to MAX_SKB_FRAGS * 32Kbytes, or ~0.5 MB.
  996. * This also speeds up tso_fragment(), since it wont fallback
  997. * to tcp_fragment().
  998. */
  999. static int linear_payload_sz(bool first_skb)
  1000. {
  1001. if (first_skb)
  1002. return SKB_WITH_OVERHEAD(2048 - MAX_TCP_HEADER);
  1003. return 0;
  1004. }
  1005. static int select_size(bool first_skb, bool zc)
  1006. {
  1007. if (zc)
  1008. return 0;
  1009. return linear_payload_sz(first_skb);
  1010. }
  1011. void tcp_free_fastopen_req(struct tcp_sock *tp)
  1012. {
  1013. if (tp->fastopen_req) {
  1014. kfree(tp->fastopen_req);
  1015. tp->fastopen_req = NULL;
  1016. }
  1017. }
  1018. static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
  1019. int *copied, size_t size)
  1020. {
  1021. struct tcp_sock *tp = tcp_sk(sk);
  1022. struct inet_sock *inet = inet_sk(sk);
  1023. struct sockaddr *uaddr = msg->msg_name;
  1024. int err, flags;
  1025. if (!(sock_net(sk)->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) ||
  1026. (uaddr && msg->msg_namelen >= sizeof(uaddr->sa_family) &&
  1027. uaddr->sa_family == AF_UNSPEC))
  1028. return -EOPNOTSUPP;
  1029. if (tp->fastopen_req)
  1030. return -EALREADY; /* Another Fast Open is in progress */
  1031. tp->fastopen_req = kzalloc(sizeof(struct tcp_fastopen_request),
  1032. sk->sk_allocation);
  1033. if (unlikely(!tp->fastopen_req))
  1034. return -ENOBUFS;
  1035. tp->fastopen_req->data = msg;
  1036. tp->fastopen_req->size = size;
  1037. if (inet->defer_connect) {
  1038. err = tcp_connect(sk);
  1039. /* Same failure procedure as in tcp_v4/6_connect */
  1040. if (err) {
  1041. tcp_set_state(sk, TCP_CLOSE);
  1042. inet->inet_dport = 0;
  1043. sk->sk_route_caps = 0;
  1044. }
  1045. }
  1046. flags = (msg->msg_flags & MSG_DONTWAIT) ? O_NONBLOCK : 0;
  1047. err = __inet_stream_connect(sk->sk_socket, uaddr,
  1048. msg->msg_namelen, flags, 1);
  1049. /* fastopen_req could already be freed in __inet_stream_connect
  1050. * if the connection times out or gets rst
  1051. */
  1052. if (tp->fastopen_req) {
  1053. *copied = tp->fastopen_req->copied;
  1054. tcp_free_fastopen_req(tp);
  1055. inet->defer_connect = 0;
  1056. }
  1057. return err;
  1058. }
  1059. int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
  1060. {
  1061. struct tcp_sock *tp = tcp_sk(sk);
  1062. struct ubuf_info *uarg = NULL;
  1063. struct sk_buff *skb;
  1064. struct sockcm_cookie sockc;
  1065. int flags, err, copied = 0;
  1066. int mss_now = 0, size_goal, copied_syn = 0;
  1067. bool process_backlog = false;
  1068. bool zc = false;
  1069. long timeo;
  1070. flags = msg->msg_flags;
  1071. if (flags & MSG_ZEROCOPY && size && sock_flag(sk, SOCK_ZEROCOPY)) {
  1072. if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
  1073. err = -EINVAL;
  1074. goto out_err;
  1075. }
  1076. skb = tcp_write_queue_tail(sk);
  1077. uarg = sock_zerocopy_realloc(sk, size, skb_zcopy(skb));
  1078. if (!uarg) {
  1079. err = -ENOBUFS;
  1080. goto out_err;
  1081. }
  1082. zc = sk->sk_route_caps & NETIF_F_SG;
  1083. if (!zc)
  1084. uarg->zerocopy = 0;
  1085. }
  1086. if (unlikely(flags & MSG_FASTOPEN || inet_sk(sk)->defer_connect) &&
  1087. !tp->repair) {
  1088. err = tcp_sendmsg_fastopen(sk, msg, &copied_syn, size);
  1089. if (err == -EINPROGRESS && copied_syn > 0)
  1090. goto out;
  1091. else if (err)
  1092. goto out_err;
  1093. }
  1094. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  1095. tcp_rate_check_app_limited(sk); /* is sending application-limited? */
  1096. /* Wait for a connection to finish. One exception is TCP Fast Open
  1097. * (passive side) where data is allowed to be sent before a connection
  1098. * is fully established.
  1099. */
  1100. if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) &&
  1101. !tcp_passive_fastopen(sk)) {
  1102. err = sk_stream_wait_connect(sk, &timeo);
  1103. if (err != 0)
  1104. goto do_error;
  1105. }
  1106. if (unlikely(tp->repair)) {
  1107. if (tp->repair_queue == TCP_RECV_QUEUE) {
  1108. copied = tcp_send_rcvq(sk, msg, size);
  1109. goto out_nopush;
  1110. }
  1111. err = -EINVAL;
  1112. if (tp->repair_queue == TCP_NO_QUEUE)
  1113. goto out_err;
  1114. /* 'common' sending to sendq */
  1115. }
  1116. sockcm_init(&sockc, sk);
  1117. if (msg->msg_controllen) {
  1118. err = sock_cmsg_send(sk, msg, &sockc);
  1119. if (unlikely(err)) {
  1120. err = -EINVAL;
  1121. goto out_err;
  1122. }
  1123. }
  1124. /* This should be in poll */
  1125. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  1126. /* Ok commence sending. */
  1127. copied = 0;
  1128. restart:
  1129. mss_now = tcp_send_mss(sk, &size_goal, flags);
  1130. err = -EPIPE;
  1131. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  1132. goto do_error;
  1133. while (msg_data_left(msg)) {
  1134. int copy = 0;
  1135. skb = tcp_write_queue_tail(sk);
  1136. if (skb)
  1137. copy = size_goal - skb->len;
  1138. if (copy <= 0 || !tcp_skb_can_collapse_to(skb)) {
  1139. bool first_skb;
  1140. int linear;
  1141. new_segment:
  1142. if (!sk_stream_memory_free(sk))
  1143. goto wait_for_sndbuf;
  1144. if (process_backlog && sk_flush_backlog(sk)) {
  1145. process_backlog = false;
  1146. goto restart;
  1147. }
  1148. first_skb = tcp_rtx_and_write_queues_empty(sk);
  1149. linear = select_size(first_skb, zc);
  1150. skb = sk_stream_alloc_skb(sk, linear, sk->sk_allocation,
  1151. first_skb);
  1152. if (!skb)
  1153. goto wait_for_memory;
  1154. process_backlog = true;
  1155. skb->ip_summed = CHECKSUM_PARTIAL;
  1156. skb_entail(sk, skb);
  1157. copy = size_goal;
  1158. /* All packets are restored as if they have
  1159. * already been sent. skb_mstamp isn't set to
  1160. * avoid wrong rtt estimation.
  1161. */
  1162. if (tp->repair)
  1163. TCP_SKB_CB(skb)->sacked |= TCPCB_REPAIRED;
  1164. }
  1165. /* Try to append data to the end of skb. */
  1166. if (copy > msg_data_left(msg))
  1167. copy = msg_data_left(msg);
  1168. /* Where to copy to? */
  1169. if (skb_availroom(skb) > 0 && !zc) {
  1170. /* We have some space in skb head. Superb! */
  1171. copy = min_t(int, copy, skb_availroom(skb));
  1172. err = skb_add_data_nocache(sk, skb, &msg->msg_iter, copy);
  1173. if (err)
  1174. goto do_fault;
  1175. } else if (!zc) {
  1176. bool merge = true;
  1177. int i = skb_shinfo(skb)->nr_frags;
  1178. struct page_frag *pfrag = sk_page_frag(sk);
  1179. if (!sk_page_frag_refill(sk, pfrag))
  1180. goto wait_for_memory;
  1181. if (!skb_can_coalesce(skb, i, pfrag->page,
  1182. pfrag->offset)) {
  1183. if (i >= sysctl_max_skb_frags) {
  1184. tcp_mark_push(tp, skb);
  1185. goto new_segment;
  1186. }
  1187. merge = false;
  1188. }
  1189. copy = min_t(int, copy, pfrag->size - pfrag->offset);
  1190. if (!sk_wmem_schedule(sk, copy))
  1191. goto wait_for_memory;
  1192. err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb,
  1193. pfrag->page,
  1194. pfrag->offset,
  1195. copy);
  1196. if (err)
  1197. goto do_error;
  1198. /* Update the skb. */
  1199. if (merge) {
  1200. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  1201. } else {
  1202. skb_fill_page_desc(skb, i, pfrag->page,
  1203. pfrag->offset, copy);
  1204. page_ref_inc(pfrag->page);
  1205. }
  1206. pfrag->offset += copy;
  1207. } else {
  1208. err = skb_zerocopy_iter_stream(sk, skb, msg, copy, uarg);
  1209. if (err == -EMSGSIZE || err == -EEXIST) {
  1210. tcp_mark_push(tp, skb);
  1211. goto new_segment;
  1212. }
  1213. if (err < 0)
  1214. goto do_error;
  1215. copy = err;
  1216. }
  1217. if (!copied)
  1218. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
  1219. WRITE_ONCE(tp->write_seq, tp->write_seq + copy);
  1220. TCP_SKB_CB(skb)->end_seq += copy;
  1221. tcp_skb_pcount_set(skb, 0);
  1222. copied += copy;
  1223. if (!msg_data_left(msg)) {
  1224. if (unlikely(flags & MSG_EOR))
  1225. TCP_SKB_CB(skb)->eor = 1;
  1226. goto out;
  1227. }
  1228. if (skb->len < size_goal || (flags & MSG_OOB) || unlikely(tp->repair))
  1229. continue;
  1230. if (forced_push(tp)) {
  1231. tcp_mark_push(tp, skb);
  1232. __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
  1233. } else if (skb == tcp_send_head(sk))
  1234. tcp_push_one(sk, mss_now);
  1235. continue;
  1236. wait_for_sndbuf:
  1237. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1238. wait_for_memory:
  1239. if (copied)
  1240. tcp_push(sk, flags & ~MSG_MORE, mss_now,
  1241. TCP_NAGLE_PUSH, size_goal);
  1242. err = sk_stream_wait_memory(sk, &timeo);
  1243. if (err != 0)
  1244. goto do_error;
  1245. mss_now = tcp_send_mss(sk, &size_goal, flags);
  1246. }
  1247. out:
  1248. if (copied) {
  1249. tcp_tx_timestamp(sk, sockc.tsflags);
  1250. tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
  1251. }
  1252. out_nopush:
  1253. sock_zerocopy_put(uarg);
  1254. return copied + copied_syn;
  1255. do_error:
  1256. skb = tcp_write_queue_tail(sk);
  1257. do_fault:
  1258. tcp_remove_empty_skb(sk, skb);
  1259. if (copied + copied_syn)
  1260. goto out;
  1261. out_err:
  1262. sock_zerocopy_put_abort(uarg);
  1263. err = sk_stream_error(sk, flags, err);
  1264. /* make sure we wake any epoll edge trigger waiter */
  1265. if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err == -EAGAIN)) {
  1266. sk->sk_write_space(sk);
  1267. tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
  1268. }
  1269. return err;
  1270. }
  1271. EXPORT_SYMBOL_GPL(tcp_sendmsg_locked);
  1272. int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  1273. {
  1274. int ret;
  1275. lock_sock(sk);
  1276. ret = tcp_sendmsg_locked(sk, msg, size);
  1277. release_sock(sk);
  1278. return ret;
  1279. }
  1280. EXPORT_SYMBOL(tcp_sendmsg);
  1281. /*
  1282. * Handle reading urgent data. BSD has very simple semantics for
  1283. * this, no blocking and very strange errors 8)
  1284. */
  1285. static int tcp_recv_urg(struct sock *sk, struct msghdr *msg, int len, int flags)
  1286. {
  1287. struct tcp_sock *tp = tcp_sk(sk);
  1288. /* No URG data to read. */
  1289. if (sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data ||
  1290. tp->urg_data == TCP_URG_READ)
  1291. return -EINVAL; /* Yes this is right ! */
  1292. if (sk->sk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DONE))
  1293. return -ENOTCONN;
  1294. if (tp->urg_data & TCP_URG_VALID) {
  1295. int err = 0;
  1296. char c = tp->urg_data;
  1297. if (!(flags & MSG_PEEK))
  1298. tp->urg_data = TCP_URG_READ;
  1299. /* Read urgent data. */
  1300. msg->msg_flags |= MSG_OOB;
  1301. if (len > 0) {
  1302. if (!(flags & MSG_TRUNC))
  1303. err = memcpy_to_msg(msg, &c, 1);
  1304. len = 1;
  1305. } else
  1306. msg->msg_flags |= MSG_TRUNC;
  1307. return err ? -EFAULT : len;
  1308. }
  1309. if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
  1310. return 0;
  1311. /* Fixed the recv(..., MSG_OOB) behaviour. BSD docs and
  1312. * the available implementations agree in this case:
  1313. * this call should never block, independent of the
  1314. * blocking state of the socket.
  1315. * Mike <pall@rz.uni-karlsruhe.de>
  1316. */
  1317. return -EAGAIN;
  1318. }
  1319. static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)
  1320. {
  1321. struct sk_buff *skb;
  1322. int copied = 0, err = 0;
  1323. /* XXX -- need to support SO_PEEK_OFF */
  1324. skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
  1325. err = skb_copy_datagram_msg(skb, 0, msg, skb->len);
  1326. if (err)
  1327. return err;
  1328. copied += skb->len;
  1329. }
  1330. skb_queue_walk(&sk->sk_write_queue, skb) {
  1331. err = skb_copy_datagram_msg(skb, 0, msg, skb->len);
  1332. if (err)
  1333. break;
  1334. copied += skb->len;
  1335. }
  1336. return err ?: copied;
  1337. }
  1338. /* Clean up the receive buffer for full frames taken by the user,
  1339. * then send an ACK if necessary. COPIED is the number of bytes
  1340. * tcp_recvmsg has given to the user so far, it speeds up the
  1341. * calculation of whether or not we must ACK for the sake of
  1342. * a window update.
  1343. */
  1344. static void tcp_cleanup_rbuf(struct sock *sk, int copied)
  1345. {
  1346. struct tcp_sock *tp = tcp_sk(sk);
  1347. bool time_to_ack = false;
  1348. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  1349. WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq),
  1350. "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n",
  1351. tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt);
  1352. if (inet_csk_ack_scheduled(sk)) {
  1353. const struct inet_connection_sock *icsk = inet_csk(sk);
  1354. /* Delayed ACKs frequently hit locked sockets during bulk
  1355. * receive. */
  1356. if (icsk->icsk_ack.blocked ||
  1357. /* Once-per-two-segments ACK was not sent by tcp_input.c */
  1358. tp->rcv_nxt - tp->rcv_wup > icsk->icsk_ack.rcv_mss ||
  1359. /*
  1360. * If this read emptied read buffer, we send ACK, if
  1361. * connection is not bidirectional, user drained
  1362. * receive buffer and there was a small segment
  1363. * in queue.
  1364. */
  1365. (copied > 0 &&
  1366. ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED2) ||
  1367. ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED) &&
  1368. !icsk->icsk_ack.pingpong)) &&
  1369. !atomic_read(&sk->sk_rmem_alloc)))
  1370. time_to_ack = true;
  1371. }
  1372. /* We send an ACK if we can now advertise a non-zero window
  1373. * which has been raised "significantly".
  1374. *
  1375. * Even if window raised up to infinity, do not send window open ACK
  1376. * in states, where we will not receive more. It is useless.
  1377. */
  1378. if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  1379. __u32 rcv_window_now = tcp_receive_window(tp);
  1380. /* Optimize, __tcp_select_window() is not cheap. */
  1381. if (2*rcv_window_now <= tp->window_clamp) {
  1382. __u32 new_window = __tcp_select_window(sk);
  1383. /* Send ACK now, if this read freed lots of space
  1384. * in our buffer. Certainly, new_window is new window.
  1385. * We can advertise it now, if it is not less than current one.
  1386. * "Lots" means "at least twice" here.
  1387. */
  1388. if (new_window && new_window >= 2 * rcv_window_now)
  1389. time_to_ack = true;
  1390. }
  1391. }
  1392. if (time_to_ack)
  1393. tcp_send_ack(sk);
  1394. }
  1395. static struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
  1396. {
  1397. struct sk_buff *skb;
  1398. u32 offset;
  1399. while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
  1400. offset = seq - TCP_SKB_CB(skb)->seq;
  1401. if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  1402. pr_err_once("%s: found a SYN, please report !\n", __func__);
  1403. offset--;
  1404. }
  1405. if (offset < skb->len || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) {
  1406. *off = offset;
  1407. return skb;
  1408. }
  1409. /* This looks weird, but this can happen if TCP collapsing
  1410. * splitted a fat GRO packet, while we released socket lock
  1411. * in skb_splice_bits()
  1412. */
  1413. sk_eat_skb(sk, skb);
  1414. }
  1415. return NULL;
  1416. }
  1417. /*
  1418. * This routine provides an alternative to tcp_recvmsg() for routines
  1419. * that would like to handle copying from skbuffs directly in 'sendfile'
  1420. * fashion.
  1421. * Note:
  1422. * - It is assumed that the socket was locked by the caller.
  1423. * - The routine does not block.
  1424. * - At present, there is no support for reading OOB data
  1425. * or for 'peeking' the socket using this routine
  1426. * (although both would be easy to implement).
  1427. */
  1428. int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  1429. sk_read_actor_t recv_actor)
  1430. {
  1431. struct sk_buff *skb;
  1432. struct tcp_sock *tp = tcp_sk(sk);
  1433. u32 seq = tp->copied_seq;
  1434. u32 offset;
  1435. int copied = 0;
  1436. if (sk->sk_state == TCP_LISTEN)
  1437. return -ENOTCONN;
  1438. while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
  1439. if (offset < skb->len) {
  1440. int used;
  1441. size_t len;
  1442. len = skb->len - offset;
  1443. /* Stop reading if we hit a patch of urgent data */
  1444. if (tp->urg_data) {
  1445. u32 urg_offset = tp->urg_seq - seq;
  1446. if (urg_offset < len)
  1447. len = urg_offset;
  1448. if (!len)
  1449. break;
  1450. }
  1451. used = recv_actor(desc, skb, offset, len);
  1452. if (used <= 0) {
  1453. if (!copied)
  1454. copied = used;
  1455. break;
  1456. } else if (used <= len) {
  1457. seq += used;
  1458. copied += used;
  1459. offset += used;
  1460. }
  1461. /* If recv_actor drops the lock (e.g. TCP splice
  1462. * receive) the skb pointer might be invalid when
  1463. * getting here: tcp_collapse might have deleted it
  1464. * while aggregating skbs from the socket queue.
  1465. */
  1466. skb = tcp_recv_skb(sk, seq - 1, &offset);
  1467. if (!skb)
  1468. break;
  1469. /* TCP coalescing might have appended data to the skb.
  1470. * Try to splice more frags
  1471. */
  1472. if (offset + 1 != skb->len)
  1473. continue;
  1474. }
  1475. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
  1476. sk_eat_skb(sk, skb);
  1477. ++seq;
  1478. break;
  1479. }
  1480. sk_eat_skb(sk, skb);
  1481. if (!desc->count)
  1482. break;
  1483. WRITE_ONCE(tp->copied_seq, seq);
  1484. }
  1485. WRITE_ONCE(tp->copied_seq, seq);
  1486. tcp_rcv_space_adjust(sk);
  1487. /* Clean up data we have read: This will do ACK frames. */
  1488. if (copied > 0) {
  1489. tcp_recv_skb(sk, seq, &offset);
  1490. tcp_cleanup_rbuf(sk, copied);
  1491. }
  1492. return copied;
  1493. }
  1494. EXPORT_SYMBOL(tcp_read_sock);
  1495. int tcp_peek_len(struct socket *sock)
  1496. {
  1497. return tcp_inq(sock->sk);
  1498. }
  1499. EXPORT_SYMBOL(tcp_peek_len);
  1500. /* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */
  1501. int tcp_set_rcvlowat(struct sock *sk, int val)
  1502. {
  1503. int cap;
  1504. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
  1505. cap = sk->sk_rcvbuf >> 1;
  1506. else
  1507. cap = sock_net(sk)->ipv4.sysctl_tcp_rmem[2] >> 1;
  1508. val = min(val, cap);
  1509. sk->sk_rcvlowat = val ? : 1;
  1510. /* Check if we need to signal EPOLLIN right now */
  1511. tcp_data_ready(sk);
  1512. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
  1513. return 0;
  1514. val <<= 1;
  1515. if (val > sk->sk_rcvbuf) {
  1516. sk->sk_rcvbuf = val;
  1517. tcp_sk(sk)->window_clamp = tcp_win_from_space(sk, val);
  1518. }
  1519. return 0;
  1520. }
  1521. EXPORT_SYMBOL(tcp_set_rcvlowat);
  1522. #ifdef CONFIG_MMU
  1523. static const struct vm_operations_struct tcp_vm_ops = {
  1524. };
  1525. int tcp_mmap(struct file *file, struct socket *sock,
  1526. struct vm_area_struct *vma)
  1527. {
  1528. if (vma->vm_flags & (VM_WRITE | VM_EXEC))
  1529. return -EPERM;
  1530. vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
  1531. /* Instruct vm_insert_page() to not down_read(mmap_sem) */
  1532. vma->vm_flags |= VM_MIXEDMAP;
  1533. vma->vm_ops = &tcp_vm_ops;
  1534. return 0;
  1535. }
  1536. EXPORT_SYMBOL(tcp_mmap);
  1537. static int tcp_zerocopy_receive(struct sock *sk,
  1538. struct tcp_zerocopy_receive *zc)
  1539. {
  1540. unsigned long address = (unsigned long)zc->address;
  1541. const skb_frag_t *frags = NULL;
  1542. u32 length = 0, seq, offset;
  1543. struct vm_area_struct *vma;
  1544. struct sk_buff *skb = NULL;
  1545. struct tcp_sock *tp;
  1546. int ret;
  1547. if (address & (PAGE_SIZE - 1) || address != zc->address)
  1548. return -EINVAL;
  1549. if (sk->sk_state == TCP_LISTEN)
  1550. return -ENOTCONN;
  1551. sock_rps_record_flow(sk);
  1552. down_read(&current->mm->mmap_sem);
  1553. vma = find_vma(current->mm, address);
  1554. if (!vma || vma->vm_start > address || vma->vm_ops != &tcp_vm_ops) {
  1555. up_read(&current->mm->mmap_sem);
  1556. return -EINVAL;
  1557. }
  1558. zc->length = min_t(unsigned long, zc->length, vma->vm_end - address);
  1559. tp = tcp_sk(sk);
  1560. seq = tp->copied_seq;
  1561. zc->length = min_t(u32, zc->length, tcp_inq(sk));
  1562. zc->length &= ~(PAGE_SIZE - 1);
  1563. zap_page_range(vma, address, zc->length);
  1564. zc->recv_skip_hint = 0;
  1565. ret = 0;
  1566. while (length + PAGE_SIZE <= zc->length) {
  1567. if (zc->recv_skip_hint < PAGE_SIZE) {
  1568. if (skb) {
  1569. skb = skb->next;
  1570. offset = seq - TCP_SKB_CB(skb)->seq;
  1571. } else {
  1572. skb = tcp_recv_skb(sk, seq, &offset);
  1573. }
  1574. zc->recv_skip_hint = skb->len - offset;
  1575. offset -= skb_headlen(skb);
  1576. if ((int)offset < 0 || skb_has_frag_list(skb))
  1577. break;
  1578. frags = skb_shinfo(skb)->frags;
  1579. while (offset) {
  1580. if (frags->size > offset)
  1581. goto out;
  1582. offset -= frags->size;
  1583. frags++;
  1584. }
  1585. }
  1586. if (frags->size != PAGE_SIZE || frags->page_offset)
  1587. break;
  1588. ret = vm_insert_page(vma, address + length,
  1589. skb_frag_page(frags));
  1590. if (ret)
  1591. break;
  1592. length += PAGE_SIZE;
  1593. seq += PAGE_SIZE;
  1594. zc->recv_skip_hint -= PAGE_SIZE;
  1595. frags++;
  1596. }
  1597. out:
  1598. up_read(&current->mm->mmap_sem);
  1599. if (length) {
  1600. WRITE_ONCE(tp->copied_seq, seq);
  1601. tcp_rcv_space_adjust(sk);
  1602. /* Clean up data we have read: This will do ACK frames. */
  1603. tcp_recv_skb(sk, seq, &offset);
  1604. tcp_cleanup_rbuf(sk, length);
  1605. ret = 0;
  1606. if (length == zc->length)
  1607. zc->recv_skip_hint = 0;
  1608. } else {
  1609. if (!zc->recv_skip_hint && sock_flag(sk, SOCK_DONE))
  1610. ret = -EIO;
  1611. }
  1612. zc->length = length;
  1613. return ret;
  1614. }
  1615. #endif
  1616. static void tcp_update_recv_tstamps(struct sk_buff *skb,
  1617. struct scm_timestamping *tss)
  1618. {
  1619. if (skb->tstamp)
  1620. tss->ts[0] = ktime_to_timespec(skb->tstamp);
  1621. else
  1622. tss->ts[0] = (struct timespec) {0};
  1623. if (skb_hwtstamps(skb)->hwtstamp)
  1624. tss->ts[2] = ktime_to_timespec(skb_hwtstamps(skb)->hwtstamp);
  1625. else
  1626. tss->ts[2] = (struct timespec) {0};
  1627. }
  1628. /* Similar to __sock_recv_timestamp, but does not require an skb */
  1629. static void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
  1630. struct scm_timestamping *tss)
  1631. {
  1632. struct timeval tv;
  1633. bool has_timestamping = false;
  1634. if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) {
  1635. if (sock_flag(sk, SOCK_RCVTSTAMP)) {
  1636. if (sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  1637. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  1638. sizeof(tss->ts[0]), &tss->ts[0]);
  1639. } else {
  1640. tv.tv_sec = tss->ts[0].tv_sec;
  1641. tv.tv_usec = tss->ts[0].tv_nsec / 1000;
  1642. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  1643. sizeof(tv), &tv);
  1644. }
  1645. }
  1646. if (sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE)
  1647. has_timestamping = true;
  1648. else
  1649. tss->ts[0] = (struct timespec) {0};
  1650. }
  1651. if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) {
  1652. if (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)
  1653. has_timestamping = true;
  1654. else
  1655. tss->ts[2] = (struct timespec) {0};
  1656. }
  1657. if (has_timestamping) {
  1658. tss->ts[1] = (struct timespec) {0};
  1659. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING,
  1660. sizeof(*tss), tss);
  1661. }
  1662. }
  1663. static int tcp_inq_hint(struct sock *sk)
  1664. {
  1665. const struct tcp_sock *tp = tcp_sk(sk);
  1666. u32 copied_seq = READ_ONCE(tp->copied_seq);
  1667. u32 rcv_nxt = READ_ONCE(tp->rcv_nxt);
  1668. int inq;
  1669. inq = rcv_nxt - copied_seq;
  1670. if (unlikely(inq < 0 || copied_seq != READ_ONCE(tp->copied_seq))) {
  1671. lock_sock(sk);
  1672. inq = tp->rcv_nxt - tp->copied_seq;
  1673. release_sock(sk);
  1674. }
  1675. /* After receiving a FIN, tell the user-space to continue reading
  1676. * by returning a non-zero inq.
  1677. */
  1678. if (inq == 0 && sock_flag(sk, SOCK_DONE))
  1679. inq = 1;
  1680. return inq;
  1681. }
  1682. /*
  1683. * This routine copies from a sock struct into the user buffer.
  1684. *
  1685. * Technical note: in 2.3 we work on _locked_ socket, so that
  1686. * tricks with *seq access order and skb->users are not required.
  1687. * Probably, code can be easily improved even more.
  1688. */
  1689. int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  1690. int flags, int *addr_len)
  1691. {
  1692. struct tcp_sock *tp = tcp_sk(sk);
  1693. int copied = 0;
  1694. u32 peek_seq;
  1695. u32 *seq;
  1696. unsigned long used;
  1697. int err, inq;
  1698. int target; /* Read at least this many bytes */
  1699. long timeo;
  1700. struct sk_buff *skb, *last;
  1701. u32 urg_hole = 0;
  1702. struct scm_timestamping tss;
  1703. bool has_tss = false;
  1704. bool has_cmsg;
  1705. if (unlikely(flags & MSG_ERRQUEUE))
  1706. return inet_recv_error(sk, msg, len, addr_len);
  1707. if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue) &&
  1708. (sk->sk_state == TCP_ESTABLISHED))
  1709. sk_busy_loop(sk, nonblock);
  1710. lock_sock(sk);
  1711. err = -ENOTCONN;
  1712. if (sk->sk_state == TCP_LISTEN)
  1713. goto out;
  1714. has_cmsg = tp->recvmsg_inq;
  1715. timeo = sock_rcvtimeo(sk, nonblock);
  1716. /* Urgent data needs to be handled specially. */
  1717. if (flags & MSG_OOB)
  1718. goto recv_urg;
  1719. if (unlikely(tp->repair)) {
  1720. err = -EPERM;
  1721. if (!(flags & MSG_PEEK))
  1722. goto out;
  1723. if (tp->repair_queue == TCP_SEND_QUEUE)
  1724. goto recv_sndq;
  1725. err = -EINVAL;
  1726. if (tp->repair_queue == TCP_NO_QUEUE)
  1727. goto out;
  1728. /* 'common' recv queue MSG_PEEK-ing */
  1729. }
  1730. seq = &tp->copied_seq;
  1731. if (flags & MSG_PEEK) {
  1732. peek_seq = tp->copied_seq;
  1733. seq = &peek_seq;
  1734. }
  1735. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1736. do {
  1737. u32 offset;
  1738. /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */
  1739. if (tp->urg_data && tp->urg_seq == *seq) {
  1740. if (copied)
  1741. break;
  1742. if (signal_pending(current)) {
  1743. copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
  1744. break;
  1745. }
  1746. }
  1747. /* Next get a buffer. */
  1748. last = skb_peek_tail(&sk->sk_receive_queue);
  1749. skb_queue_walk(&sk->sk_receive_queue, skb) {
  1750. last = skb;
  1751. /* Now that we have two receive queues this
  1752. * shouldn't happen.
  1753. */
  1754. if (WARN(before(*seq, TCP_SKB_CB(skb)->seq),
  1755. "TCP recvmsg seq # bug: copied %X, seq %X, rcvnxt %X, fl %X\n",
  1756. *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt,
  1757. flags))
  1758. break;
  1759. offset = *seq - TCP_SKB_CB(skb)->seq;
  1760. if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  1761. pr_err_once("%s: found a SYN, please report !\n", __func__);
  1762. offset--;
  1763. }
  1764. if (offset < skb->len)
  1765. goto found_ok_skb;
  1766. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  1767. goto found_fin_ok;
  1768. WARN(!(flags & MSG_PEEK),
  1769. "TCP recvmsg seq # bug 2: copied %X, seq %X, rcvnxt %X, fl %X\n",
  1770. *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, flags);
  1771. }
  1772. /* Well, if we have backlog, try to process it now yet. */
  1773. if (copied >= target && !READ_ONCE(sk->sk_backlog.tail))
  1774. break;
  1775. if (copied) {
  1776. if (sk->sk_err ||
  1777. sk->sk_state == TCP_CLOSE ||
  1778. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1779. !timeo ||
  1780. signal_pending(current))
  1781. break;
  1782. } else {
  1783. if (sock_flag(sk, SOCK_DONE))
  1784. break;
  1785. if (sk->sk_err) {
  1786. copied = sock_error(sk);
  1787. break;
  1788. }
  1789. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1790. break;
  1791. if (sk->sk_state == TCP_CLOSE) {
  1792. /* This occurs when user tries to read
  1793. * from never connected socket.
  1794. */
  1795. copied = -ENOTCONN;
  1796. break;
  1797. }
  1798. if (!timeo) {
  1799. copied = -EAGAIN;
  1800. break;
  1801. }
  1802. if (signal_pending(current)) {
  1803. copied = sock_intr_errno(timeo);
  1804. break;
  1805. }
  1806. }
  1807. tcp_cleanup_rbuf(sk, copied);
  1808. if (copied >= target) {
  1809. /* Do not sleep, just process backlog. */
  1810. release_sock(sk);
  1811. lock_sock(sk);
  1812. } else {
  1813. sk_wait_data(sk, &timeo, last);
  1814. }
  1815. if ((flags & MSG_PEEK) &&
  1816. (peek_seq - copied - urg_hole != tp->copied_seq)) {
  1817. net_dbg_ratelimited("TCP(%s:%d): Application bug, race in MSG_PEEK\n",
  1818. current->comm,
  1819. task_pid_nr(current));
  1820. peek_seq = tp->copied_seq;
  1821. }
  1822. continue;
  1823. found_ok_skb:
  1824. /* Ok so how much can we use? */
  1825. used = skb->len - offset;
  1826. if (len < used)
  1827. used = len;
  1828. /* Do we have urgent data here? */
  1829. if (tp->urg_data) {
  1830. u32 urg_offset = tp->urg_seq - *seq;
  1831. if (urg_offset < used) {
  1832. if (!urg_offset) {
  1833. if (!sock_flag(sk, SOCK_URGINLINE)) {
  1834. WRITE_ONCE(*seq, *seq + 1);
  1835. urg_hole++;
  1836. offset++;
  1837. used--;
  1838. if (!used)
  1839. goto skip_copy;
  1840. }
  1841. } else
  1842. used = urg_offset;
  1843. }
  1844. }
  1845. if (!(flags & MSG_TRUNC)) {
  1846. err = skb_copy_datagram_msg(skb, offset, msg, used);
  1847. if (err) {
  1848. /* Exception. Bailout! */
  1849. if (!copied)
  1850. copied = -EFAULT;
  1851. break;
  1852. }
  1853. }
  1854. WRITE_ONCE(*seq, *seq + used);
  1855. copied += used;
  1856. len -= used;
  1857. tcp_rcv_space_adjust(sk);
  1858. skip_copy:
  1859. if (tp->urg_data && after(tp->copied_seq, tp->urg_seq)) {
  1860. tp->urg_data = 0;
  1861. tcp_fast_path_check(sk);
  1862. }
  1863. if (TCP_SKB_CB(skb)->has_rxtstamp) {
  1864. tcp_update_recv_tstamps(skb, &tss);
  1865. has_tss = true;
  1866. has_cmsg = true;
  1867. }
  1868. if (used + offset < skb->len)
  1869. continue;
  1870. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  1871. goto found_fin_ok;
  1872. if (!(flags & MSG_PEEK))
  1873. sk_eat_skb(sk, skb);
  1874. continue;
  1875. found_fin_ok:
  1876. /* Process the FIN. */
  1877. WRITE_ONCE(*seq, *seq + 1);
  1878. if (!(flags & MSG_PEEK))
  1879. sk_eat_skb(sk, skb);
  1880. break;
  1881. } while (len > 0);
  1882. /* According to UNIX98, msg_name/msg_namelen are ignored
  1883. * on connected socket. I was just happy when found this 8) --ANK
  1884. */
  1885. /* Clean up data we have read: This will do ACK frames. */
  1886. tcp_cleanup_rbuf(sk, copied);
  1887. release_sock(sk);
  1888. if (has_cmsg) {
  1889. if (has_tss)
  1890. tcp_recv_timestamp(msg, sk, &tss);
  1891. if (tp->recvmsg_inq) {
  1892. inq = tcp_inq_hint(sk);
  1893. put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
  1894. }
  1895. }
  1896. return copied;
  1897. out:
  1898. release_sock(sk);
  1899. return err;
  1900. recv_urg:
  1901. err = tcp_recv_urg(sk, msg, len, flags);
  1902. goto out;
  1903. recv_sndq:
  1904. err = tcp_peek_sndq(sk, msg, len);
  1905. goto out;
  1906. }
  1907. EXPORT_SYMBOL(tcp_recvmsg);
  1908. void tcp_set_state(struct sock *sk, int state)
  1909. {
  1910. int oldstate = sk->sk_state;
  1911. /* We defined a new enum for TCP states that are exported in BPF
  1912. * so as not force the internal TCP states to be frozen. The
  1913. * following checks will detect if an internal state value ever
  1914. * differs from the BPF value. If this ever happens, then we will
  1915. * need to remap the internal value to the BPF value before calling
  1916. * tcp_call_bpf_2arg.
  1917. */
  1918. BUILD_BUG_ON((int)BPF_TCP_ESTABLISHED != (int)TCP_ESTABLISHED);
  1919. BUILD_BUG_ON((int)BPF_TCP_SYN_SENT != (int)TCP_SYN_SENT);
  1920. BUILD_BUG_ON((int)BPF_TCP_SYN_RECV != (int)TCP_SYN_RECV);
  1921. BUILD_BUG_ON((int)BPF_TCP_FIN_WAIT1 != (int)TCP_FIN_WAIT1);
  1922. BUILD_BUG_ON((int)BPF_TCP_FIN_WAIT2 != (int)TCP_FIN_WAIT2);
  1923. BUILD_BUG_ON((int)BPF_TCP_TIME_WAIT != (int)TCP_TIME_WAIT);
  1924. BUILD_BUG_ON((int)BPF_TCP_CLOSE != (int)TCP_CLOSE);
  1925. BUILD_BUG_ON((int)BPF_TCP_CLOSE_WAIT != (int)TCP_CLOSE_WAIT);
  1926. BUILD_BUG_ON((int)BPF_TCP_LAST_ACK != (int)TCP_LAST_ACK);
  1927. BUILD_BUG_ON((int)BPF_TCP_LISTEN != (int)TCP_LISTEN);
  1928. BUILD_BUG_ON((int)BPF_TCP_CLOSING != (int)TCP_CLOSING);
  1929. BUILD_BUG_ON((int)BPF_TCP_NEW_SYN_RECV != (int)TCP_NEW_SYN_RECV);
  1930. BUILD_BUG_ON((int)BPF_TCP_MAX_STATES != (int)TCP_MAX_STATES);
  1931. if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_STATE_CB_FLAG))
  1932. tcp_call_bpf_2arg(sk, BPF_SOCK_OPS_STATE_CB, oldstate, state);
  1933. switch (state) {
  1934. case TCP_ESTABLISHED:
  1935. if (oldstate != TCP_ESTABLISHED)
  1936. TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
  1937. break;
  1938. case TCP_CLOSE:
  1939. if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
  1940. TCP_INC_STATS(sock_net(sk), TCP_MIB_ESTABRESETS);
  1941. sk->sk_prot->unhash(sk);
  1942. if (inet_csk(sk)->icsk_bind_hash &&
  1943. !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
  1944. inet_put_port(sk);
  1945. /* fall through */
  1946. default:
  1947. if (oldstate == TCP_ESTABLISHED)
  1948. TCP_DEC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
  1949. }
  1950. /* Change state AFTER socket is unhashed to avoid closed
  1951. * socket sitting in hash tables.
  1952. */
  1953. inet_sk_state_store(sk, state);
  1954. #ifdef STATE_TRACE
  1955. SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n", sk, statename[oldstate], statename[state]);
  1956. #endif
  1957. }
  1958. EXPORT_SYMBOL_GPL(tcp_set_state);
  1959. /*
  1960. * State processing on a close. This implements the state shift for
  1961. * sending our FIN frame. Note that we only send a FIN for some
  1962. * states. A shutdown() may have already sent the FIN, or we may be
  1963. * closed.
  1964. */
  1965. static const unsigned char new_state[16] = {
  1966. /* current state: new state: action: */
  1967. [0 /* (Invalid) */] = TCP_CLOSE,
  1968. [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  1969. [TCP_SYN_SENT] = TCP_CLOSE,
  1970. [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  1971. [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
  1972. [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
  1973. [TCP_TIME_WAIT] = TCP_CLOSE,
  1974. [TCP_CLOSE] = TCP_CLOSE,
  1975. [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
  1976. [TCP_LAST_ACK] = TCP_LAST_ACK,
  1977. [TCP_LISTEN] = TCP_CLOSE,
  1978. [TCP_CLOSING] = TCP_CLOSING,
  1979. [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
  1980. };
  1981. static int tcp_close_state(struct sock *sk)
  1982. {
  1983. int next = (int)new_state[sk->sk_state];
  1984. int ns = next & TCP_STATE_MASK;
  1985. tcp_set_state(sk, ns);
  1986. return next & TCP_ACTION_FIN;
  1987. }
  1988. /*
  1989. * Shutdown the sending side of a connection. Much like close except
  1990. * that we don't receive shut down or sock_set_flag(sk, SOCK_DEAD).
  1991. */
  1992. void tcp_shutdown(struct sock *sk, int how)
  1993. {
  1994. /* We need to grab some memory, and put together a FIN,
  1995. * and then put it into the queue to be sent.
  1996. * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
  1997. */
  1998. if (!(how & SEND_SHUTDOWN))
  1999. return;
  2000. /* If we've already sent a FIN, or it's a closed state, skip this. */
  2001. if ((1 << sk->sk_state) &
  2002. (TCPF_ESTABLISHED | TCPF_SYN_SENT |
  2003. TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
  2004. /* Clear out any half completed packets. FIN if needed. */
  2005. if (tcp_close_state(sk))
  2006. tcp_send_fin(sk);
  2007. }
  2008. }
  2009. EXPORT_SYMBOL(tcp_shutdown);
  2010. bool tcp_check_oom(struct sock *sk, int shift)
  2011. {
  2012. bool too_many_orphans, out_of_socket_memory;
  2013. too_many_orphans = tcp_too_many_orphans(sk, shift);
  2014. out_of_socket_memory = tcp_out_of_memory(sk);
  2015. if (too_many_orphans)
  2016. net_info_ratelimited("too many orphaned sockets\n");
  2017. if (out_of_socket_memory)
  2018. net_info_ratelimited("out of memory -- consider tuning tcp_mem\n");
  2019. return too_many_orphans || out_of_socket_memory;
  2020. }
  2021. void tcp_close(struct sock *sk, long timeout)
  2022. {
  2023. struct sk_buff *skb;
  2024. int data_was_unread = 0;
  2025. int state;
  2026. lock_sock(sk);
  2027. sk->sk_shutdown = SHUTDOWN_MASK;
  2028. if (sk->sk_state == TCP_LISTEN) {
  2029. tcp_set_state(sk, TCP_CLOSE);
  2030. /* Special case. */
  2031. inet_csk_listen_stop(sk);
  2032. goto adjudge_to_death;
  2033. }
  2034. /* We need to flush the recv. buffs. We do this only on the
  2035. * descriptor close, not protocol-sourced closes, because the
  2036. * reader process may not have drained the data yet!
  2037. */
  2038. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  2039. u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq;
  2040. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  2041. len--;
  2042. data_was_unread += len;
  2043. __kfree_skb(skb);
  2044. }
  2045. sk_mem_reclaim(sk);
  2046. /* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
  2047. if (sk->sk_state == TCP_CLOSE)
  2048. goto adjudge_to_death;
  2049. /* As outlined in RFC 2525, section 2.17, we send a RST here because
  2050. * data was lost. To witness the awful effects of the old behavior of
  2051. * always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk
  2052. * GET in an FTP client, suspend the process, wait for the client to
  2053. * advertise a zero window, then kill -9 the FTP client, wheee...
  2054. * Note: timeout is always zero in such a case.
  2055. */
  2056. if (unlikely(tcp_sk(sk)->repair)) {
  2057. sk->sk_prot->disconnect(sk, 0);
  2058. } else if (data_was_unread) {
  2059. /* Unread data was tossed, zap the connection. */
  2060. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE);
  2061. tcp_set_state(sk, TCP_CLOSE);
  2062. tcp_send_active_reset(sk, sk->sk_allocation);
  2063. } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  2064. /* Check zero linger _after_ checking for unread data. */
  2065. sk->sk_prot->disconnect(sk, 0);
  2066. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
  2067. } else if (tcp_close_state(sk)) {
  2068. /* We FIN if the application ate all the data before
  2069. * zapping the connection.
  2070. */
  2071. /* RED-PEN. Formally speaking, we have broken TCP state
  2072. * machine. State transitions:
  2073. *
  2074. * TCP_ESTABLISHED -> TCP_FIN_WAIT1
  2075. * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
  2076. * TCP_CLOSE_WAIT -> TCP_LAST_ACK
  2077. *
  2078. * are legal only when FIN has been sent (i.e. in window),
  2079. * rather than queued out of window. Purists blame.
  2080. *
  2081. * F.e. "RFC state" is ESTABLISHED,
  2082. * if Linux state is FIN-WAIT-1, but FIN is still not sent.
  2083. *
  2084. * The visible declinations are that sometimes
  2085. * we enter time-wait state, when it is not required really
  2086. * (harmless), do not send active resets, when they are
  2087. * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
  2088. * they look as CLOSING or LAST_ACK for Linux)
  2089. * Probably, I missed some more holelets.
  2090. * --ANK
  2091. * XXX (TFO) - To start off we don't support SYN+ACK+FIN
  2092. * in a single packet! (May consider it later but will
  2093. * probably need API support or TCP_CORK SYN-ACK until
  2094. * data is written and socket is closed.)
  2095. */
  2096. tcp_send_fin(sk);
  2097. }
  2098. sk_stream_wait_close(sk, timeout);
  2099. adjudge_to_death:
  2100. state = sk->sk_state;
  2101. sock_hold(sk);
  2102. sock_orphan(sk);
  2103. local_bh_disable();
  2104. bh_lock_sock(sk);
  2105. /* remove backlog if any, without releasing ownership. */
  2106. __release_sock(sk);
  2107. percpu_counter_inc(sk->sk_prot->orphan_count);
  2108. /* Have we already been destroyed by a softirq or backlog? */
  2109. if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
  2110. goto out;
  2111. /* This is a (useful) BSD violating of the RFC. There is a
  2112. * problem with TCP as specified in that the other end could
  2113. * keep a socket open forever with no application left this end.
  2114. * We use a 1 minute timeout (about the same as BSD) then kill
  2115. * our end. If they send after that then tough - BUT: long enough
  2116. * that we won't make the old 4*rto = almost no time - whoops
  2117. * reset mistake.
  2118. *
  2119. * Nope, it was not mistake. It is really desired behaviour
  2120. * f.e. on http servers, when such sockets are useless, but
  2121. * consume significant resources. Let's do it with special
  2122. * linger2 option. --ANK
  2123. */
  2124. if (sk->sk_state == TCP_FIN_WAIT2) {
  2125. struct tcp_sock *tp = tcp_sk(sk);
  2126. if (tp->linger2 < 0) {
  2127. tcp_set_state(sk, TCP_CLOSE);
  2128. tcp_send_active_reset(sk, GFP_ATOMIC);
  2129. __NET_INC_STATS(sock_net(sk),
  2130. LINUX_MIB_TCPABORTONLINGER);
  2131. } else {
  2132. const int tmo = tcp_fin_time(sk);
  2133. if (tmo > TCP_TIMEWAIT_LEN) {
  2134. inet_csk_reset_keepalive_timer(sk,
  2135. tmo - TCP_TIMEWAIT_LEN);
  2136. } else {
  2137. tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
  2138. goto out;
  2139. }
  2140. }
  2141. }
  2142. if (sk->sk_state != TCP_CLOSE) {
  2143. sk_mem_reclaim(sk);
  2144. if (tcp_check_oom(sk, 0)) {
  2145. tcp_set_state(sk, TCP_CLOSE);
  2146. tcp_send_active_reset(sk, GFP_ATOMIC);
  2147. __NET_INC_STATS(sock_net(sk),
  2148. LINUX_MIB_TCPABORTONMEMORY);
  2149. } else if (!check_net(sock_net(sk))) {
  2150. /* Not possible to send reset; just close */
  2151. tcp_set_state(sk, TCP_CLOSE);
  2152. }
  2153. }
  2154. if (sk->sk_state == TCP_CLOSE) {
  2155. struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
  2156. /* We could get here with a non-NULL req if the socket is
  2157. * aborted (e.g., closed with unread data) before 3WHS
  2158. * finishes.
  2159. */
  2160. if (req)
  2161. reqsk_fastopen_remove(sk, req, false);
  2162. inet_csk_destroy_sock(sk);
  2163. }
  2164. /* Otherwise, socket is reprieved until protocol close. */
  2165. out:
  2166. bh_unlock_sock(sk);
  2167. local_bh_enable();
  2168. release_sock(sk);
  2169. sock_put(sk);
  2170. }
  2171. EXPORT_SYMBOL(tcp_close);
  2172. /* These states need RST on ABORT according to RFC793 */
  2173. static inline bool tcp_need_reset(int state)
  2174. {
  2175. return (1 << state) &
  2176. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 |
  2177. TCPF_FIN_WAIT2 | TCPF_SYN_RECV);
  2178. }
  2179. static void tcp_rtx_queue_purge(struct sock *sk)
  2180. {
  2181. struct rb_node *p = rb_first(&sk->tcp_rtx_queue);
  2182. tcp_sk(sk)->highest_sack = NULL;
  2183. while (p) {
  2184. struct sk_buff *skb = rb_to_skb(p);
  2185. p = rb_next(p);
  2186. /* Since we are deleting whole queue, no need to
  2187. * list_del(&skb->tcp_tsorted_anchor)
  2188. */
  2189. tcp_rtx_queue_unlink(skb, sk);
  2190. sk_wmem_free_skb(sk, skb);
  2191. }
  2192. }
  2193. void tcp_write_queue_purge(struct sock *sk)
  2194. {
  2195. struct sk_buff *skb;
  2196. tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
  2197. while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  2198. tcp_skb_tsorted_anchor_cleanup(skb);
  2199. sk_wmem_free_skb(sk, skb);
  2200. }
  2201. tcp_rtx_queue_purge(sk);
  2202. INIT_LIST_HEAD(&tcp_sk(sk)->tsorted_sent_queue);
  2203. sk_mem_reclaim(sk);
  2204. tcp_clear_all_retrans_hints(tcp_sk(sk));
  2205. tcp_sk(sk)->packets_out = 0;
  2206. inet_csk(sk)->icsk_backoff = 0;
  2207. }
  2208. int tcp_disconnect(struct sock *sk, int flags)
  2209. {
  2210. struct inet_sock *inet = inet_sk(sk);
  2211. struct inet_connection_sock *icsk = inet_csk(sk);
  2212. struct tcp_sock *tp = tcp_sk(sk);
  2213. int old_state = sk->sk_state;
  2214. u32 seq;
  2215. if (old_state != TCP_CLOSE)
  2216. tcp_set_state(sk, TCP_CLOSE);
  2217. /* ABORT function of RFC793 */
  2218. if (old_state == TCP_LISTEN) {
  2219. inet_csk_listen_stop(sk);
  2220. } else if (unlikely(tp->repair)) {
  2221. sk->sk_err = ECONNABORTED;
  2222. } else if (tcp_need_reset(old_state) ||
  2223. (tp->snd_nxt != tp->write_seq &&
  2224. (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) {
  2225. /* The last check adjusts for discrepancy of Linux wrt. RFC
  2226. * states
  2227. */
  2228. tcp_send_active_reset(sk, gfp_any());
  2229. sk->sk_err = ECONNRESET;
  2230. } else if (old_state == TCP_SYN_SENT)
  2231. sk->sk_err = ECONNRESET;
  2232. tcp_clear_xmit_timers(sk);
  2233. __skb_queue_purge(&sk->sk_receive_queue);
  2234. WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
  2235. tp->urg_data = 0;
  2236. tcp_write_queue_purge(sk);
  2237. tcp_fastopen_active_disable_ofo_check(sk);
  2238. skb_rbtree_purge(&tp->out_of_order_queue);
  2239. inet->inet_dport = 0;
  2240. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  2241. inet_reset_saddr(sk);
  2242. sk->sk_shutdown = 0;
  2243. sock_reset_flag(sk, SOCK_DONE);
  2244. tp->srtt_us = 0;
  2245. tp->rcv_rtt_last_tsecr = 0;
  2246. seq = tp->write_seq + tp->max_window + 2;
  2247. if (!seq)
  2248. seq = 1;
  2249. WRITE_ONCE(tp->write_seq, seq);
  2250. tp->snd_cwnd = 2;
  2251. icsk->icsk_probes_out = 0;
  2252. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  2253. tp->snd_cwnd_cnt = 0;
  2254. tp->window_clamp = 0;
  2255. tp->delivered = 0;
  2256. tp->delivered_ce = 0;
  2257. if (icsk->icsk_ca_ops->release)
  2258. icsk->icsk_ca_ops->release(sk);
  2259. memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
  2260. tcp_set_ca_state(sk, TCP_CA_Open);
  2261. tp->is_sack_reneg = 0;
  2262. tcp_clear_retrans(tp);
  2263. tp->total_retrans = 0;
  2264. inet_csk_delack_init(sk);
  2265. /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
  2266. * issue in __tcp_select_window()
  2267. */
  2268. icsk->icsk_ack.rcv_mss = TCP_MIN_MSS;
  2269. memset(&tp->rx_opt, 0, sizeof(tp->rx_opt));
  2270. __sk_dst_reset(sk);
  2271. dst_release(sk->sk_rx_dst);
  2272. sk->sk_rx_dst = NULL;
  2273. tcp_saved_syn_free(tp);
  2274. tp->compressed_ack = 0;
  2275. tp->segs_in = 0;
  2276. tp->segs_out = 0;
  2277. tp->bytes_sent = 0;
  2278. tp->bytes_acked = 0;
  2279. tp->bytes_received = 0;
  2280. tp->bytes_retrans = 0;
  2281. tp->data_segs_in = 0;
  2282. tp->data_segs_out = 0;
  2283. tp->dsack_dups = 0;
  2284. tp->reord_seen = 0;
  2285. /* Clean up fastopen related fields */
  2286. tcp_free_fastopen_req(tp);
  2287. inet->defer_connect = 0;
  2288. WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
  2289. if (sk->sk_frag.page) {
  2290. put_page(sk->sk_frag.page);
  2291. sk->sk_frag.page = NULL;
  2292. sk->sk_frag.offset = 0;
  2293. }
  2294. sk->sk_error_report(sk);
  2295. return 0;
  2296. }
  2297. EXPORT_SYMBOL(tcp_disconnect);
  2298. static inline bool tcp_can_repair_sock(const struct sock *sk)
  2299. {
  2300. return ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN) &&
  2301. (sk->sk_state != TCP_LISTEN);
  2302. }
  2303. static int tcp_repair_set_window(struct tcp_sock *tp, char __user *optbuf, int len)
  2304. {
  2305. struct tcp_repair_window opt;
  2306. if (!tp->repair)
  2307. return -EPERM;
  2308. if (len != sizeof(opt))
  2309. return -EINVAL;
  2310. if (copy_from_user(&opt, optbuf, sizeof(opt)))
  2311. return -EFAULT;
  2312. if (opt.max_window < opt.snd_wnd)
  2313. return -EINVAL;
  2314. if (after(opt.snd_wl1, tp->rcv_nxt + opt.rcv_wnd))
  2315. return -EINVAL;
  2316. if (after(opt.rcv_wup, tp->rcv_nxt))
  2317. return -EINVAL;
  2318. tp->snd_wl1 = opt.snd_wl1;
  2319. tp->snd_wnd = opt.snd_wnd;
  2320. tp->max_window = opt.max_window;
  2321. tp->rcv_wnd = opt.rcv_wnd;
  2322. tp->rcv_wup = opt.rcv_wup;
  2323. return 0;
  2324. }
  2325. static int tcp_repair_options_est(struct sock *sk,
  2326. struct tcp_repair_opt __user *optbuf, unsigned int len)
  2327. {
  2328. struct tcp_sock *tp = tcp_sk(sk);
  2329. struct tcp_repair_opt opt;
  2330. while (len >= sizeof(opt)) {
  2331. if (copy_from_user(&opt, optbuf, sizeof(opt)))
  2332. return -EFAULT;
  2333. optbuf++;
  2334. len -= sizeof(opt);
  2335. switch (opt.opt_code) {
  2336. case TCPOPT_MSS:
  2337. tp->rx_opt.mss_clamp = opt.opt_val;
  2338. tcp_mtup_init(sk);
  2339. break;
  2340. case TCPOPT_WINDOW:
  2341. {
  2342. u16 snd_wscale = opt.opt_val & 0xFFFF;
  2343. u16 rcv_wscale = opt.opt_val >> 16;
  2344. if (snd_wscale > TCP_MAX_WSCALE || rcv_wscale > TCP_MAX_WSCALE)
  2345. return -EFBIG;
  2346. tp->rx_opt.snd_wscale = snd_wscale;
  2347. tp->rx_opt.rcv_wscale = rcv_wscale;
  2348. tp->rx_opt.wscale_ok = 1;
  2349. }
  2350. break;
  2351. case TCPOPT_SACK_PERM:
  2352. if (opt.opt_val != 0)
  2353. return -EINVAL;
  2354. tp->rx_opt.sack_ok |= TCP_SACK_SEEN;
  2355. break;
  2356. case TCPOPT_TIMESTAMP:
  2357. if (opt.opt_val != 0)
  2358. return -EINVAL;
  2359. tp->rx_opt.tstamp_ok = 1;
  2360. break;
  2361. }
  2362. }
  2363. return 0;
  2364. }
  2365. /*
  2366. * Socket option code for TCP.
  2367. */
  2368. static int do_tcp_setsockopt(struct sock *sk, int level,
  2369. int optname, char __user *optval, unsigned int optlen)
  2370. {
  2371. struct tcp_sock *tp = tcp_sk(sk);
  2372. struct inet_connection_sock *icsk = inet_csk(sk);
  2373. struct net *net = sock_net(sk);
  2374. int val;
  2375. int err = 0;
  2376. /* These are data/string values, all the others are ints */
  2377. switch (optname) {
  2378. case TCP_CONGESTION: {
  2379. char name[TCP_CA_NAME_MAX];
  2380. if (optlen < 1)
  2381. return -EINVAL;
  2382. val = strncpy_from_user(name, optval,
  2383. min_t(long, TCP_CA_NAME_MAX-1, optlen));
  2384. if (val < 0)
  2385. return -EFAULT;
  2386. name[val] = 0;
  2387. lock_sock(sk);
  2388. err = tcp_set_congestion_control(sk, name, true, true,
  2389. ns_capable(sock_net(sk)->user_ns,
  2390. CAP_NET_ADMIN));
  2391. release_sock(sk);
  2392. return err;
  2393. }
  2394. case TCP_ULP: {
  2395. char name[TCP_ULP_NAME_MAX];
  2396. if (optlen < 1)
  2397. return -EINVAL;
  2398. val = strncpy_from_user(name, optval,
  2399. min_t(long, TCP_ULP_NAME_MAX - 1,
  2400. optlen));
  2401. if (val < 0)
  2402. return -EFAULT;
  2403. name[val] = 0;
  2404. lock_sock(sk);
  2405. err = tcp_set_ulp(sk, name);
  2406. release_sock(sk);
  2407. return err;
  2408. }
  2409. case TCP_FASTOPEN_KEY: {
  2410. __u8 key[TCP_FASTOPEN_KEY_LENGTH];
  2411. if (optlen != sizeof(key))
  2412. return -EINVAL;
  2413. if (copy_from_user(key, optval, optlen))
  2414. return -EFAULT;
  2415. return tcp_fastopen_reset_cipher(net, sk, key, sizeof(key));
  2416. }
  2417. default:
  2418. /* fallthru */
  2419. break;
  2420. }
  2421. if (optlen < sizeof(int))
  2422. return -EINVAL;
  2423. if (get_user(val, (int __user *)optval))
  2424. return -EFAULT;
  2425. lock_sock(sk);
  2426. switch (optname) {
  2427. case TCP_MAXSEG:
  2428. /* Values greater than interface MTU won't take effect. However
  2429. * at the point when this call is done we typically don't yet
  2430. * know which interface is going to be used
  2431. */
  2432. if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW)) {
  2433. err = -EINVAL;
  2434. break;
  2435. }
  2436. tp->rx_opt.user_mss = val;
  2437. break;
  2438. case TCP_NODELAY:
  2439. if (val) {
  2440. /* TCP_NODELAY is weaker than TCP_CORK, so that
  2441. * this option on corked socket is remembered, but
  2442. * it is not activated until cork is cleared.
  2443. *
  2444. * However, when TCP_NODELAY is set we make
  2445. * an explicit push, which overrides even TCP_CORK
  2446. * for currently queued segments.
  2447. */
  2448. tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
  2449. tcp_push_pending_frames(sk);
  2450. } else {
  2451. tp->nonagle &= ~TCP_NAGLE_OFF;
  2452. }
  2453. break;
  2454. case TCP_THIN_LINEAR_TIMEOUTS:
  2455. if (val < 0 || val > 1)
  2456. err = -EINVAL;
  2457. else
  2458. tp->thin_lto = val;
  2459. break;
  2460. case TCP_THIN_DUPACK:
  2461. if (val < 0 || val > 1)
  2462. err = -EINVAL;
  2463. break;
  2464. case TCP_REPAIR:
  2465. if (!tcp_can_repair_sock(sk))
  2466. err = -EPERM;
  2467. else if (val == TCP_REPAIR_ON) {
  2468. tp->repair = 1;
  2469. sk->sk_reuse = SK_FORCE_REUSE;
  2470. tp->repair_queue = TCP_NO_QUEUE;
  2471. } else if (val == TCP_REPAIR_OFF) {
  2472. tp->repair = 0;
  2473. sk->sk_reuse = SK_NO_REUSE;
  2474. tcp_send_window_probe(sk);
  2475. } else if (val == TCP_REPAIR_OFF_NO_WP) {
  2476. tp->repair = 0;
  2477. sk->sk_reuse = SK_NO_REUSE;
  2478. } else
  2479. err = -EINVAL;
  2480. break;
  2481. case TCP_REPAIR_QUEUE:
  2482. if (!tp->repair)
  2483. err = -EPERM;
  2484. else if ((unsigned int)val < TCP_QUEUES_NR)
  2485. tp->repair_queue = val;
  2486. else
  2487. err = -EINVAL;
  2488. break;
  2489. case TCP_QUEUE_SEQ:
  2490. if (sk->sk_state != TCP_CLOSE) {
  2491. err = -EPERM;
  2492. } else if (tp->repair_queue == TCP_SEND_QUEUE) {
  2493. if (!tcp_rtx_queue_empty(sk))
  2494. err = -EPERM;
  2495. else
  2496. WRITE_ONCE(tp->write_seq, val);
  2497. } else if (tp->repair_queue == TCP_RECV_QUEUE) {
  2498. if (tp->rcv_nxt != tp->copied_seq) {
  2499. err = -EPERM;
  2500. } else {
  2501. WRITE_ONCE(tp->rcv_nxt, val);
  2502. WRITE_ONCE(tp->copied_seq, val);
  2503. }
  2504. } else {
  2505. err = -EINVAL;
  2506. }
  2507. break;
  2508. case TCP_REPAIR_OPTIONS:
  2509. if (!tp->repair)
  2510. err = -EINVAL;
  2511. else if (sk->sk_state == TCP_ESTABLISHED)
  2512. err = tcp_repair_options_est(sk,
  2513. (struct tcp_repair_opt __user *)optval,
  2514. optlen);
  2515. else
  2516. err = -EPERM;
  2517. break;
  2518. case TCP_CORK:
  2519. /* When set indicates to always queue non-full frames.
  2520. * Later the user clears this option and we transmit
  2521. * any pending partial frames in the queue. This is
  2522. * meant to be used alongside sendfile() to get properly
  2523. * filled frames when the user (for example) must write
  2524. * out headers with a write() call first and then use
  2525. * sendfile to send out the data parts.
  2526. *
  2527. * TCP_CORK can be set together with TCP_NODELAY and it is
  2528. * stronger than TCP_NODELAY.
  2529. */
  2530. if (val) {
  2531. tp->nonagle |= TCP_NAGLE_CORK;
  2532. } else {
  2533. tp->nonagle &= ~TCP_NAGLE_CORK;
  2534. if (tp->nonagle&TCP_NAGLE_OFF)
  2535. tp->nonagle |= TCP_NAGLE_PUSH;
  2536. tcp_push_pending_frames(sk);
  2537. }
  2538. break;
  2539. case TCP_KEEPIDLE:
  2540. if (val < 1 || val > MAX_TCP_KEEPIDLE)
  2541. err = -EINVAL;
  2542. else {
  2543. tp->keepalive_time = val * HZ;
  2544. if (sock_flag(sk, SOCK_KEEPOPEN) &&
  2545. !((1 << sk->sk_state) &
  2546. (TCPF_CLOSE | TCPF_LISTEN))) {
  2547. u32 elapsed = keepalive_time_elapsed(tp);
  2548. if (tp->keepalive_time > elapsed)
  2549. elapsed = tp->keepalive_time - elapsed;
  2550. else
  2551. elapsed = 0;
  2552. inet_csk_reset_keepalive_timer(sk, elapsed);
  2553. }
  2554. }
  2555. break;
  2556. case TCP_KEEPINTVL:
  2557. if (val < 1 || val > MAX_TCP_KEEPINTVL)
  2558. err = -EINVAL;
  2559. else
  2560. tp->keepalive_intvl = val * HZ;
  2561. break;
  2562. case TCP_KEEPCNT:
  2563. if (val < 1 || val > MAX_TCP_KEEPCNT)
  2564. err = -EINVAL;
  2565. else
  2566. tp->keepalive_probes = val;
  2567. break;
  2568. case TCP_SYNCNT:
  2569. if (val < 1 || val > MAX_TCP_SYNCNT)
  2570. err = -EINVAL;
  2571. else
  2572. icsk->icsk_syn_retries = val;
  2573. break;
  2574. case TCP_SAVE_SYN:
  2575. if (val < 0 || val > 1)
  2576. err = -EINVAL;
  2577. else
  2578. tp->save_syn = val;
  2579. break;
  2580. case TCP_LINGER2:
  2581. if (val < 0)
  2582. tp->linger2 = -1;
  2583. else if (val > net->ipv4.sysctl_tcp_fin_timeout / HZ)
  2584. tp->linger2 = 0;
  2585. else
  2586. tp->linger2 = val * HZ;
  2587. break;
  2588. case TCP_DEFER_ACCEPT:
  2589. /* Translate value in seconds to number of retransmits */
  2590. icsk->icsk_accept_queue.rskq_defer_accept =
  2591. secs_to_retrans(val, TCP_TIMEOUT_INIT / HZ,
  2592. TCP_RTO_MAX / HZ);
  2593. break;
  2594. case TCP_WINDOW_CLAMP:
  2595. if (!val) {
  2596. if (sk->sk_state != TCP_CLOSE) {
  2597. err = -EINVAL;
  2598. break;
  2599. }
  2600. tp->window_clamp = 0;
  2601. } else
  2602. tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
  2603. SOCK_MIN_RCVBUF / 2 : val;
  2604. break;
  2605. case TCP_QUICKACK:
  2606. if (!val) {
  2607. icsk->icsk_ack.pingpong = 1;
  2608. } else {
  2609. icsk->icsk_ack.pingpong = 0;
  2610. if ((1 << sk->sk_state) &
  2611. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
  2612. inet_csk_ack_scheduled(sk)) {
  2613. icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
  2614. tcp_cleanup_rbuf(sk, 1);
  2615. if (!(val & 1))
  2616. icsk->icsk_ack.pingpong = 1;
  2617. }
  2618. }
  2619. break;
  2620. #ifdef CONFIG_TCP_MD5SIG
  2621. case TCP_MD5SIG:
  2622. case TCP_MD5SIG_EXT:
  2623. err = tp->af_specific->md5_parse(sk, optname, optval, optlen);
  2624. break;
  2625. #endif
  2626. case TCP_USER_TIMEOUT:
  2627. /* Cap the max time in ms TCP will retry or probe the window
  2628. * before giving up and aborting (ETIMEDOUT) a connection.
  2629. */
  2630. if (val < 0)
  2631. err = -EINVAL;
  2632. else
  2633. icsk->icsk_user_timeout = val;
  2634. break;
  2635. case TCP_FASTOPEN:
  2636. if (val >= 0 && ((1 << sk->sk_state) & (TCPF_CLOSE |
  2637. TCPF_LISTEN))) {
  2638. tcp_fastopen_init_key_once(net);
  2639. fastopen_queue_tune(sk, val);
  2640. } else {
  2641. err = -EINVAL;
  2642. }
  2643. break;
  2644. case TCP_FASTOPEN_CONNECT:
  2645. if (val > 1 || val < 0) {
  2646. err = -EINVAL;
  2647. } else if (net->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) {
  2648. if (sk->sk_state == TCP_CLOSE)
  2649. tp->fastopen_connect = val;
  2650. else
  2651. err = -EINVAL;
  2652. } else {
  2653. err = -EOPNOTSUPP;
  2654. }
  2655. break;
  2656. case TCP_FASTOPEN_NO_COOKIE:
  2657. if (val > 1 || val < 0)
  2658. err = -EINVAL;
  2659. else if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  2660. err = -EINVAL;
  2661. else
  2662. tp->fastopen_no_cookie = val;
  2663. break;
  2664. case TCP_TIMESTAMP:
  2665. if (!tp->repair)
  2666. err = -EPERM;
  2667. else
  2668. tp->tsoffset = val - tcp_time_stamp_raw();
  2669. break;
  2670. case TCP_REPAIR_WINDOW:
  2671. err = tcp_repair_set_window(tp, optval, optlen);
  2672. break;
  2673. case TCP_NOTSENT_LOWAT:
  2674. tp->notsent_lowat = val;
  2675. sk->sk_write_space(sk);
  2676. break;
  2677. case TCP_INQ:
  2678. if (val > 1 || val < 0)
  2679. err = -EINVAL;
  2680. else
  2681. tp->recvmsg_inq = val;
  2682. break;
  2683. default:
  2684. err = -ENOPROTOOPT;
  2685. break;
  2686. }
  2687. release_sock(sk);
  2688. return err;
  2689. }
  2690. int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
  2691. unsigned int optlen)
  2692. {
  2693. const struct inet_connection_sock *icsk = inet_csk(sk);
  2694. if (level != SOL_TCP)
  2695. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  2696. optval, optlen);
  2697. return do_tcp_setsockopt(sk, level, optname, optval, optlen);
  2698. }
  2699. EXPORT_SYMBOL(tcp_setsockopt);
  2700. #ifdef CONFIG_COMPAT
  2701. int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
  2702. char __user *optval, unsigned int optlen)
  2703. {
  2704. if (level != SOL_TCP)
  2705. return inet_csk_compat_setsockopt(sk, level, optname,
  2706. optval, optlen);
  2707. return do_tcp_setsockopt(sk, level, optname, optval, optlen);
  2708. }
  2709. EXPORT_SYMBOL(compat_tcp_setsockopt);
  2710. #endif
  2711. static void tcp_get_info_chrono_stats(const struct tcp_sock *tp,
  2712. struct tcp_info *info)
  2713. {
  2714. u64 stats[__TCP_CHRONO_MAX], total = 0;
  2715. enum tcp_chrono i;
  2716. for (i = TCP_CHRONO_BUSY; i < __TCP_CHRONO_MAX; ++i) {
  2717. stats[i] = tp->chrono_stat[i - 1];
  2718. if (i == tp->chrono_type)
  2719. stats[i] += tcp_jiffies32 - tp->chrono_start;
  2720. stats[i] *= USEC_PER_SEC / HZ;
  2721. total += stats[i];
  2722. }
  2723. info->tcpi_busy_time = total;
  2724. info->tcpi_rwnd_limited = stats[TCP_CHRONO_RWND_LIMITED];
  2725. info->tcpi_sndbuf_limited = stats[TCP_CHRONO_SNDBUF_LIMITED];
  2726. }
  2727. /* Return information about state of tcp endpoint in API format. */
  2728. void tcp_get_info(struct sock *sk, struct tcp_info *info)
  2729. {
  2730. const struct tcp_sock *tp = tcp_sk(sk); /* iff sk_type == SOCK_STREAM */
  2731. const struct inet_connection_sock *icsk = inet_csk(sk);
  2732. u32 now;
  2733. u64 rate64;
  2734. bool slow;
  2735. u32 rate;
  2736. memset(info, 0, sizeof(*info));
  2737. if (sk->sk_type != SOCK_STREAM)
  2738. return;
  2739. info->tcpi_state = inet_sk_state_load(sk);
  2740. /* Report meaningful fields for all TCP states, including listeners */
  2741. rate = READ_ONCE(sk->sk_pacing_rate);
  2742. rate64 = rate != ~0U ? rate : ~0ULL;
  2743. info->tcpi_pacing_rate = rate64;
  2744. rate = READ_ONCE(sk->sk_max_pacing_rate);
  2745. rate64 = rate != ~0U ? rate : ~0ULL;
  2746. info->tcpi_max_pacing_rate = rate64;
  2747. info->tcpi_reordering = tp->reordering;
  2748. info->tcpi_snd_cwnd = tp->snd_cwnd;
  2749. if (info->tcpi_state == TCP_LISTEN) {
  2750. /* listeners aliased fields :
  2751. * tcpi_unacked -> Number of children ready for accept()
  2752. * tcpi_sacked -> max backlog
  2753. */
  2754. info->tcpi_unacked = sk->sk_ack_backlog;
  2755. info->tcpi_sacked = sk->sk_max_ack_backlog;
  2756. return;
  2757. }
  2758. slow = lock_sock_fast(sk);
  2759. info->tcpi_ca_state = icsk->icsk_ca_state;
  2760. info->tcpi_retransmits = icsk->icsk_retransmits;
  2761. info->tcpi_probes = icsk->icsk_probes_out;
  2762. info->tcpi_backoff = icsk->icsk_backoff;
  2763. if (tp->rx_opt.tstamp_ok)
  2764. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  2765. if (tcp_is_sack(tp))
  2766. info->tcpi_options |= TCPI_OPT_SACK;
  2767. if (tp->rx_opt.wscale_ok) {
  2768. info->tcpi_options |= TCPI_OPT_WSCALE;
  2769. info->tcpi_snd_wscale = tp->rx_opt.snd_wscale;
  2770. info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale;
  2771. }
  2772. if (tp->ecn_flags & TCP_ECN_OK)
  2773. info->tcpi_options |= TCPI_OPT_ECN;
  2774. if (tp->ecn_flags & TCP_ECN_SEEN)
  2775. info->tcpi_options |= TCPI_OPT_ECN_SEEN;
  2776. if (tp->syn_data_acked)
  2777. info->tcpi_options |= TCPI_OPT_SYN_DATA;
  2778. info->tcpi_rto = jiffies_to_usecs(icsk->icsk_rto);
  2779. info->tcpi_ato = jiffies_to_usecs(icsk->icsk_ack.ato);
  2780. info->tcpi_snd_mss = tp->mss_cache;
  2781. info->tcpi_rcv_mss = icsk->icsk_ack.rcv_mss;
  2782. info->tcpi_unacked = tp->packets_out;
  2783. info->tcpi_sacked = tp->sacked_out;
  2784. info->tcpi_lost = tp->lost_out;
  2785. info->tcpi_retrans = tp->retrans_out;
  2786. now = tcp_jiffies32;
  2787. info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime);
  2788. info->tcpi_last_data_recv = jiffies_to_msecs(now - icsk->icsk_ack.lrcvtime);
  2789. info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp);
  2790. info->tcpi_pmtu = icsk->icsk_pmtu_cookie;
  2791. info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
  2792. info->tcpi_rtt = tp->srtt_us >> 3;
  2793. info->tcpi_rttvar = tp->mdev_us >> 2;
  2794. info->tcpi_snd_ssthresh = tp->snd_ssthresh;
  2795. info->tcpi_advmss = tp->advmss;
  2796. info->tcpi_rcv_rtt = tp->rcv_rtt_est.rtt_us >> 3;
  2797. info->tcpi_rcv_space = tp->rcvq_space.space;
  2798. info->tcpi_total_retrans = tp->total_retrans;
  2799. info->tcpi_bytes_acked = tp->bytes_acked;
  2800. info->tcpi_bytes_received = tp->bytes_received;
  2801. info->tcpi_notsent_bytes = max_t(int, 0, tp->write_seq - tp->snd_nxt);
  2802. tcp_get_info_chrono_stats(tp, info);
  2803. info->tcpi_segs_out = tp->segs_out;
  2804. info->tcpi_segs_in = tp->segs_in;
  2805. info->tcpi_min_rtt = tcp_min_rtt(tp);
  2806. info->tcpi_data_segs_in = tp->data_segs_in;
  2807. info->tcpi_data_segs_out = tp->data_segs_out;
  2808. info->tcpi_delivery_rate_app_limited = tp->rate_app_limited ? 1 : 0;
  2809. rate64 = tcp_compute_delivery_rate(tp);
  2810. if (rate64)
  2811. info->tcpi_delivery_rate = rate64;
  2812. info->tcpi_delivered = tp->delivered;
  2813. info->tcpi_delivered_ce = tp->delivered_ce;
  2814. info->tcpi_bytes_sent = tp->bytes_sent;
  2815. info->tcpi_bytes_retrans = tp->bytes_retrans;
  2816. info->tcpi_dsack_dups = tp->dsack_dups;
  2817. info->tcpi_reord_seen = tp->reord_seen;
  2818. unlock_sock_fast(sk, slow);
  2819. }
  2820. EXPORT_SYMBOL_GPL(tcp_get_info);
  2821. static size_t tcp_opt_stats_get_size(void)
  2822. {
  2823. return
  2824. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_BUSY */
  2825. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_RWND_LIMITED */
  2826. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_SNDBUF_LIMITED */
  2827. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_DATA_SEGS_OUT */
  2828. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_TOTAL_RETRANS */
  2829. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_PACING_RATE */
  2830. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_DELIVERY_RATE */
  2831. nla_total_size(sizeof(u32)) + /* TCP_NLA_SND_CWND */
  2832. nla_total_size(sizeof(u32)) + /* TCP_NLA_REORDERING */
  2833. nla_total_size(sizeof(u32)) + /* TCP_NLA_MIN_RTT */
  2834. nla_total_size(sizeof(u8)) + /* TCP_NLA_RECUR_RETRANS */
  2835. nla_total_size(sizeof(u8)) + /* TCP_NLA_DELIVERY_RATE_APP_LMT */
  2836. nla_total_size(sizeof(u32)) + /* TCP_NLA_SNDQ_SIZE */
  2837. nla_total_size(sizeof(u8)) + /* TCP_NLA_CA_STATE */
  2838. nla_total_size(sizeof(u32)) + /* TCP_NLA_SND_SSTHRESH */
  2839. nla_total_size(sizeof(u32)) + /* TCP_NLA_DELIVERED */
  2840. nla_total_size(sizeof(u32)) + /* TCP_NLA_DELIVERED_CE */
  2841. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_BYTES_SENT */
  2842. nla_total_size_64bit(sizeof(u64)) + /* TCP_NLA_BYTES_RETRANS */
  2843. nla_total_size(sizeof(u32)) + /* TCP_NLA_DSACK_DUPS */
  2844. nla_total_size(sizeof(u32)) + /* TCP_NLA_REORD_SEEN */
  2845. 0;
  2846. }
  2847. struct sk_buff *tcp_get_timestamping_opt_stats(const struct sock *sk)
  2848. {
  2849. const struct tcp_sock *tp = tcp_sk(sk);
  2850. struct sk_buff *stats;
  2851. struct tcp_info info;
  2852. u64 rate64;
  2853. u32 rate;
  2854. stats = alloc_skb(tcp_opt_stats_get_size(), GFP_ATOMIC);
  2855. if (!stats)
  2856. return NULL;
  2857. tcp_get_info_chrono_stats(tp, &info);
  2858. nla_put_u64_64bit(stats, TCP_NLA_BUSY,
  2859. info.tcpi_busy_time, TCP_NLA_PAD);
  2860. nla_put_u64_64bit(stats, TCP_NLA_RWND_LIMITED,
  2861. info.tcpi_rwnd_limited, TCP_NLA_PAD);
  2862. nla_put_u64_64bit(stats, TCP_NLA_SNDBUF_LIMITED,
  2863. info.tcpi_sndbuf_limited, TCP_NLA_PAD);
  2864. nla_put_u64_64bit(stats, TCP_NLA_DATA_SEGS_OUT,
  2865. tp->data_segs_out, TCP_NLA_PAD);
  2866. nla_put_u64_64bit(stats, TCP_NLA_TOTAL_RETRANS,
  2867. tp->total_retrans, TCP_NLA_PAD);
  2868. rate = READ_ONCE(sk->sk_pacing_rate);
  2869. rate64 = rate != ~0U ? rate : ~0ULL;
  2870. nla_put_u64_64bit(stats, TCP_NLA_PACING_RATE, rate64, TCP_NLA_PAD);
  2871. rate64 = tcp_compute_delivery_rate(tp);
  2872. nla_put_u64_64bit(stats, TCP_NLA_DELIVERY_RATE, rate64, TCP_NLA_PAD);
  2873. nla_put_u32(stats, TCP_NLA_SND_CWND, tp->snd_cwnd);
  2874. nla_put_u32(stats, TCP_NLA_REORDERING, tp->reordering);
  2875. nla_put_u32(stats, TCP_NLA_MIN_RTT, tcp_min_rtt(tp));
  2876. nla_put_u8(stats, TCP_NLA_RECUR_RETRANS, inet_csk(sk)->icsk_retransmits);
  2877. nla_put_u8(stats, TCP_NLA_DELIVERY_RATE_APP_LMT, !!tp->rate_app_limited);
  2878. nla_put_u32(stats, TCP_NLA_SND_SSTHRESH, tp->snd_ssthresh);
  2879. nla_put_u32(stats, TCP_NLA_DELIVERED, tp->delivered);
  2880. nla_put_u32(stats, TCP_NLA_DELIVERED_CE, tp->delivered_ce);
  2881. nla_put_u32(stats, TCP_NLA_SNDQ_SIZE, tp->write_seq - tp->snd_una);
  2882. nla_put_u8(stats, TCP_NLA_CA_STATE, inet_csk(sk)->icsk_ca_state);
  2883. nla_put_u64_64bit(stats, TCP_NLA_BYTES_SENT, tp->bytes_sent,
  2884. TCP_NLA_PAD);
  2885. nla_put_u64_64bit(stats, TCP_NLA_BYTES_RETRANS, tp->bytes_retrans,
  2886. TCP_NLA_PAD);
  2887. nla_put_u32(stats, TCP_NLA_DSACK_DUPS, tp->dsack_dups);
  2888. nla_put_u32(stats, TCP_NLA_REORD_SEEN, tp->reord_seen);
  2889. return stats;
  2890. }
  2891. static int do_tcp_getsockopt(struct sock *sk, int level,
  2892. int optname, char __user *optval, int __user *optlen)
  2893. {
  2894. struct inet_connection_sock *icsk = inet_csk(sk);
  2895. struct tcp_sock *tp = tcp_sk(sk);
  2896. struct net *net = sock_net(sk);
  2897. int val, len;
  2898. if (get_user(len, optlen))
  2899. return -EFAULT;
  2900. len = min_t(unsigned int, len, sizeof(int));
  2901. if (len < 0)
  2902. return -EINVAL;
  2903. switch (optname) {
  2904. case TCP_MAXSEG:
  2905. val = tp->mss_cache;
  2906. if (!val && ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  2907. val = tp->rx_opt.user_mss;
  2908. if (tp->repair)
  2909. val = tp->rx_opt.mss_clamp;
  2910. break;
  2911. case TCP_NODELAY:
  2912. val = !!(tp->nonagle&TCP_NAGLE_OFF);
  2913. break;
  2914. case TCP_CORK:
  2915. val = !!(tp->nonagle&TCP_NAGLE_CORK);
  2916. break;
  2917. case TCP_KEEPIDLE:
  2918. val = keepalive_time_when(tp) / HZ;
  2919. break;
  2920. case TCP_KEEPINTVL:
  2921. val = keepalive_intvl_when(tp) / HZ;
  2922. break;
  2923. case TCP_KEEPCNT:
  2924. val = keepalive_probes(tp);
  2925. break;
  2926. case TCP_SYNCNT:
  2927. val = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
  2928. break;
  2929. case TCP_LINGER2:
  2930. val = tp->linger2;
  2931. if (val >= 0)
  2932. val = (val ? : net->ipv4.sysctl_tcp_fin_timeout) / HZ;
  2933. break;
  2934. case TCP_DEFER_ACCEPT:
  2935. val = retrans_to_secs(icsk->icsk_accept_queue.rskq_defer_accept,
  2936. TCP_TIMEOUT_INIT / HZ, TCP_RTO_MAX / HZ);
  2937. break;
  2938. case TCP_WINDOW_CLAMP:
  2939. val = tp->window_clamp;
  2940. break;
  2941. case TCP_INFO: {
  2942. struct tcp_info info;
  2943. if (get_user(len, optlen))
  2944. return -EFAULT;
  2945. tcp_get_info(sk, &info);
  2946. len = min_t(unsigned int, len, sizeof(info));
  2947. if (put_user(len, optlen))
  2948. return -EFAULT;
  2949. if (copy_to_user(optval, &info, len))
  2950. return -EFAULT;
  2951. return 0;
  2952. }
  2953. case TCP_CC_INFO: {
  2954. const struct tcp_congestion_ops *ca_ops;
  2955. union tcp_cc_info info;
  2956. size_t sz = 0;
  2957. int attr;
  2958. if (get_user(len, optlen))
  2959. return -EFAULT;
  2960. ca_ops = icsk->icsk_ca_ops;
  2961. if (ca_ops && ca_ops->get_info)
  2962. sz = ca_ops->get_info(sk, ~0U, &attr, &info);
  2963. len = min_t(unsigned int, len, sz);
  2964. if (put_user(len, optlen))
  2965. return -EFAULT;
  2966. if (copy_to_user(optval, &info, len))
  2967. return -EFAULT;
  2968. return 0;
  2969. }
  2970. case TCP_QUICKACK:
  2971. val = !icsk->icsk_ack.pingpong;
  2972. break;
  2973. case TCP_CONGESTION:
  2974. if (get_user(len, optlen))
  2975. return -EFAULT;
  2976. len = min_t(unsigned int, len, TCP_CA_NAME_MAX);
  2977. if (put_user(len, optlen))
  2978. return -EFAULT;
  2979. if (copy_to_user(optval, icsk->icsk_ca_ops->name, len))
  2980. return -EFAULT;
  2981. return 0;
  2982. case TCP_ULP:
  2983. if (get_user(len, optlen))
  2984. return -EFAULT;
  2985. len = min_t(unsigned int, len, TCP_ULP_NAME_MAX);
  2986. if (!icsk->icsk_ulp_ops) {
  2987. if (put_user(0, optlen))
  2988. return -EFAULT;
  2989. return 0;
  2990. }
  2991. if (put_user(len, optlen))
  2992. return -EFAULT;
  2993. if (copy_to_user(optval, icsk->icsk_ulp_ops->name, len))
  2994. return -EFAULT;
  2995. return 0;
  2996. case TCP_FASTOPEN_KEY: {
  2997. __u8 key[TCP_FASTOPEN_KEY_LENGTH];
  2998. struct tcp_fastopen_context *ctx;
  2999. if (get_user(len, optlen))
  3000. return -EFAULT;
  3001. rcu_read_lock();
  3002. ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx);
  3003. if (ctx)
  3004. memcpy(key, ctx->key, sizeof(key));
  3005. else
  3006. len = 0;
  3007. rcu_read_unlock();
  3008. len = min_t(unsigned int, len, sizeof(key));
  3009. if (put_user(len, optlen))
  3010. return -EFAULT;
  3011. if (copy_to_user(optval, key, len))
  3012. return -EFAULT;
  3013. return 0;
  3014. }
  3015. case TCP_THIN_LINEAR_TIMEOUTS:
  3016. val = tp->thin_lto;
  3017. break;
  3018. case TCP_THIN_DUPACK:
  3019. val = 0;
  3020. break;
  3021. case TCP_REPAIR:
  3022. val = tp->repair;
  3023. break;
  3024. case TCP_REPAIR_QUEUE:
  3025. if (tp->repair)
  3026. val = tp->repair_queue;
  3027. else
  3028. return -EINVAL;
  3029. break;
  3030. case TCP_REPAIR_WINDOW: {
  3031. struct tcp_repair_window opt;
  3032. if (get_user(len, optlen))
  3033. return -EFAULT;
  3034. if (len != sizeof(opt))
  3035. return -EINVAL;
  3036. if (!tp->repair)
  3037. return -EPERM;
  3038. opt.snd_wl1 = tp->snd_wl1;
  3039. opt.snd_wnd = tp->snd_wnd;
  3040. opt.max_window = tp->max_window;
  3041. opt.rcv_wnd = tp->rcv_wnd;
  3042. opt.rcv_wup = tp->rcv_wup;
  3043. if (copy_to_user(optval, &opt, len))
  3044. return -EFAULT;
  3045. return 0;
  3046. }
  3047. case TCP_QUEUE_SEQ:
  3048. if (tp->repair_queue == TCP_SEND_QUEUE)
  3049. val = tp->write_seq;
  3050. else if (tp->repair_queue == TCP_RECV_QUEUE)
  3051. val = tp->rcv_nxt;
  3052. else
  3053. return -EINVAL;
  3054. break;
  3055. case TCP_USER_TIMEOUT:
  3056. val = icsk->icsk_user_timeout;
  3057. break;
  3058. case TCP_FASTOPEN:
  3059. val = icsk->icsk_accept_queue.fastopenq.max_qlen;
  3060. break;
  3061. case TCP_FASTOPEN_CONNECT:
  3062. val = tp->fastopen_connect;
  3063. break;
  3064. case TCP_FASTOPEN_NO_COOKIE:
  3065. val = tp->fastopen_no_cookie;
  3066. break;
  3067. case TCP_TIMESTAMP:
  3068. val = tcp_time_stamp_raw() + tp->tsoffset;
  3069. break;
  3070. case TCP_NOTSENT_LOWAT:
  3071. val = tp->notsent_lowat;
  3072. break;
  3073. case TCP_INQ:
  3074. val = tp->recvmsg_inq;
  3075. break;
  3076. case TCP_SAVE_SYN:
  3077. val = tp->save_syn;
  3078. break;
  3079. case TCP_SAVED_SYN: {
  3080. if (get_user(len, optlen))
  3081. return -EFAULT;
  3082. lock_sock(sk);
  3083. if (tp->saved_syn) {
  3084. if (len < tp->saved_syn[0]) {
  3085. if (put_user(tp->saved_syn[0], optlen)) {
  3086. release_sock(sk);
  3087. return -EFAULT;
  3088. }
  3089. release_sock(sk);
  3090. return -EINVAL;
  3091. }
  3092. len = tp->saved_syn[0];
  3093. if (put_user(len, optlen)) {
  3094. release_sock(sk);
  3095. return -EFAULT;
  3096. }
  3097. if (copy_to_user(optval, tp->saved_syn + 1, len)) {
  3098. release_sock(sk);
  3099. return -EFAULT;
  3100. }
  3101. tcp_saved_syn_free(tp);
  3102. release_sock(sk);
  3103. } else {
  3104. release_sock(sk);
  3105. len = 0;
  3106. if (put_user(len, optlen))
  3107. return -EFAULT;
  3108. }
  3109. return 0;
  3110. }
  3111. #ifdef CONFIG_MMU
  3112. case TCP_ZEROCOPY_RECEIVE: {
  3113. struct tcp_zerocopy_receive zc;
  3114. int err;
  3115. if (get_user(len, optlen))
  3116. return -EFAULT;
  3117. if (len != sizeof(zc))
  3118. return -EINVAL;
  3119. if (copy_from_user(&zc, optval, len))
  3120. return -EFAULT;
  3121. lock_sock(sk);
  3122. err = tcp_zerocopy_receive(sk, &zc);
  3123. release_sock(sk);
  3124. if (!err && copy_to_user(optval, &zc, len))
  3125. err = -EFAULT;
  3126. return err;
  3127. }
  3128. #endif
  3129. default:
  3130. return -ENOPROTOOPT;
  3131. }
  3132. if (put_user(len, optlen))
  3133. return -EFAULT;
  3134. if (copy_to_user(optval, &val, len))
  3135. return -EFAULT;
  3136. return 0;
  3137. }
  3138. int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
  3139. int __user *optlen)
  3140. {
  3141. struct inet_connection_sock *icsk = inet_csk(sk);
  3142. if (level != SOL_TCP)
  3143. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  3144. optval, optlen);
  3145. return do_tcp_getsockopt(sk, level, optname, optval, optlen);
  3146. }
  3147. EXPORT_SYMBOL(tcp_getsockopt);
  3148. #ifdef CONFIG_COMPAT
  3149. int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
  3150. char __user *optval, int __user *optlen)
  3151. {
  3152. if (level != SOL_TCP)
  3153. return inet_csk_compat_getsockopt(sk, level, optname,
  3154. optval, optlen);
  3155. return do_tcp_getsockopt(sk, level, optname, optval, optlen);
  3156. }
  3157. EXPORT_SYMBOL(compat_tcp_getsockopt);
  3158. #endif
  3159. #ifdef CONFIG_TCP_MD5SIG
  3160. static DEFINE_PER_CPU(struct tcp_md5sig_pool, tcp_md5sig_pool);
  3161. static DEFINE_MUTEX(tcp_md5sig_mutex);
  3162. static bool tcp_md5sig_pool_populated = false;
  3163. static void __tcp_alloc_md5sig_pool(void)
  3164. {
  3165. struct crypto_ahash *hash;
  3166. int cpu;
  3167. hash = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
  3168. if (IS_ERR(hash))
  3169. return;
  3170. for_each_possible_cpu(cpu) {
  3171. void *scratch = per_cpu(tcp_md5sig_pool, cpu).scratch;
  3172. struct ahash_request *req;
  3173. if (!scratch) {
  3174. scratch = kmalloc_node(sizeof(union tcp_md5sum_block) +
  3175. sizeof(struct tcphdr),
  3176. GFP_KERNEL,
  3177. cpu_to_node(cpu));
  3178. if (!scratch)
  3179. return;
  3180. per_cpu(tcp_md5sig_pool, cpu).scratch = scratch;
  3181. }
  3182. if (per_cpu(tcp_md5sig_pool, cpu).md5_req)
  3183. continue;
  3184. req = ahash_request_alloc(hash, GFP_KERNEL);
  3185. if (!req)
  3186. return;
  3187. ahash_request_set_callback(req, 0, NULL, NULL);
  3188. per_cpu(tcp_md5sig_pool, cpu).md5_req = req;
  3189. }
  3190. /* before setting tcp_md5sig_pool_populated, we must commit all writes
  3191. * to memory. See smp_rmb() in tcp_get_md5sig_pool()
  3192. */
  3193. smp_wmb();
  3194. tcp_md5sig_pool_populated = true;
  3195. }
  3196. bool tcp_alloc_md5sig_pool(void)
  3197. {
  3198. if (unlikely(!tcp_md5sig_pool_populated)) {
  3199. mutex_lock(&tcp_md5sig_mutex);
  3200. if (!tcp_md5sig_pool_populated)
  3201. __tcp_alloc_md5sig_pool();
  3202. mutex_unlock(&tcp_md5sig_mutex);
  3203. }
  3204. return tcp_md5sig_pool_populated;
  3205. }
  3206. EXPORT_SYMBOL(tcp_alloc_md5sig_pool);
  3207. /**
  3208. * tcp_get_md5sig_pool - get md5sig_pool for this user
  3209. *
  3210. * We use percpu structure, so if we succeed, we exit with preemption
  3211. * and BH disabled, to make sure another thread or softirq handling
  3212. * wont try to get same context.
  3213. */
  3214. struct tcp_md5sig_pool *tcp_get_md5sig_pool(void)
  3215. {
  3216. local_bh_disable();
  3217. if (tcp_md5sig_pool_populated) {
  3218. /* coupled with smp_wmb() in __tcp_alloc_md5sig_pool() */
  3219. smp_rmb();
  3220. return this_cpu_ptr(&tcp_md5sig_pool);
  3221. }
  3222. local_bh_enable();
  3223. return NULL;
  3224. }
  3225. EXPORT_SYMBOL(tcp_get_md5sig_pool);
  3226. int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
  3227. const struct sk_buff *skb, unsigned int header_len)
  3228. {
  3229. struct scatterlist sg;
  3230. const struct tcphdr *tp = tcp_hdr(skb);
  3231. struct ahash_request *req = hp->md5_req;
  3232. unsigned int i;
  3233. const unsigned int head_data_len = skb_headlen(skb) > header_len ?
  3234. skb_headlen(skb) - header_len : 0;
  3235. const struct skb_shared_info *shi = skb_shinfo(skb);
  3236. struct sk_buff *frag_iter;
  3237. sg_init_table(&sg, 1);
  3238. sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len);
  3239. ahash_request_set_crypt(req, &sg, NULL, head_data_len);
  3240. if (crypto_ahash_update(req))
  3241. return 1;
  3242. for (i = 0; i < shi->nr_frags; ++i) {
  3243. const struct skb_frag_struct *f = &shi->frags[i];
  3244. unsigned int offset = f->page_offset;
  3245. struct page *page = skb_frag_page(f) + (offset >> PAGE_SHIFT);
  3246. sg_set_page(&sg, page, skb_frag_size(f),
  3247. offset_in_page(offset));
  3248. ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f));
  3249. if (crypto_ahash_update(req))
  3250. return 1;
  3251. }
  3252. skb_walk_frags(skb, frag_iter)
  3253. if (tcp_md5_hash_skb_data(hp, frag_iter, 0))
  3254. return 1;
  3255. return 0;
  3256. }
  3257. EXPORT_SYMBOL(tcp_md5_hash_skb_data);
  3258. int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
  3259. {
  3260. u8 keylen = READ_ONCE(key->keylen); /* paired with WRITE_ONCE() in tcp_md5_do_add */
  3261. struct scatterlist sg;
  3262. sg_init_one(&sg, key->key, keylen);
  3263. ahash_request_set_crypt(hp->md5_req, &sg, NULL, keylen);
  3264. /* tcp_md5_do_add() might change key->key under us */
  3265. return crypto_ahash_update(hp->md5_req);
  3266. }
  3267. EXPORT_SYMBOL(tcp_md5_hash_key);
  3268. #endif
  3269. void tcp_done(struct sock *sk)
  3270. {
  3271. struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
  3272. if (sk->sk_state == TCP_SYN_SENT || sk->sk_state == TCP_SYN_RECV)
  3273. TCP_INC_STATS(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
  3274. tcp_set_state(sk, TCP_CLOSE);
  3275. tcp_clear_xmit_timers(sk);
  3276. if (req)
  3277. reqsk_fastopen_remove(sk, req, false);
  3278. sk->sk_shutdown = SHUTDOWN_MASK;
  3279. if (!sock_flag(sk, SOCK_DEAD))
  3280. sk->sk_state_change(sk);
  3281. else
  3282. inet_csk_destroy_sock(sk);
  3283. }
  3284. EXPORT_SYMBOL_GPL(tcp_done);
  3285. int tcp_abort(struct sock *sk, int err)
  3286. {
  3287. if (!sk_fullsock(sk)) {
  3288. if (sk->sk_state == TCP_NEW_SYN_RECV) {
  3289. struct request_sock *req = inet_reqsk(sk);
  3290. local_bh_disable();
  3291. inet_csk_reqsk_queue_drop(req->rsk_listener, req);
  3292. local_bh_enable();
  3293. return 0;
  3294. }
  3295. return -EOPNOTSUPP;
  3296. }
  3297. /* Don't race with userspace socket closes such as tcp_close. */
  3298. lock_sock(sk);
  3299. if (sk->sk_state == TCP_LISTEN) {
  3300. tcp_set_state(sk, TCP_CLOSE);
  3301. inet_csk_listen_stop(sk);
  3302. }
  3303. /* Don't race with BH socket closes such as inet_csk_listen_stop. */
  3304. local_bh_disable();
  3305. bh_lock_sock(sk);
  3306. if (!sock_flag(sk, SOCK_DEAD)) {
  3307. sk->sk_err = err;
  3308. /* This barrier is coupled with smp_rmb() in tcp_poll() */
  3309. smp_wmb();
  3310. sk->sk_error_report(sk);
  3311. if (tcp_need_reset(sk->sk_state))
  3312. tcp_send_active_reset(sk, GFP_ATOMIC);
  3313. tcp_done(sk);
  3314. }
  3315. bh_unlock_sock(sk);
  3316. local_bh_enable();
  3317. tcp_write_queue_purge(sk);
  3318. release_sock(sk);
  3319. return 0;
  3320. }
  3321. EXPORT_SYMBOL_GPL(tcp_abort);
  3322. extern struct tcp_congestion_ops tcp_reno;
  3323. static __initdata unsigned long thash_entries;
  3324. static int __init set_thash_entries(char *str)
  3325. {
  3326. ssize_t ret;
  3327. if (!str)
  3328. return 0;
  3329. ret = kstrtoul(str, 0, &thash_entries);
  3330. if (ret)
  3331. return 0;
  3332. return 1;
  3333. }
  3334. __setup("thash_entries=", set_thash_entries);
  3335. static void __init tcp_init_mem(void)
  3336. {
  3337. unsigned long limit = nr_free_buffer_pages() / 16;
  3338. limit = max(limit, 128UL);
  3339. sysctl_tcp_mem[0] = limit / 4 * 3; /* 4.68 % */
  3340. sysctl_tcp_mem[1] = limit; /* 6.25 % */
  3341. sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2; /* 9.37 % */
  3342. }
  3343. void __init tcp_init(void)
  3344. {
  3345. int max_rshare, max_wshare, cnt;
  3346. unsigned long limit;
  3347. unsigned int i;
  3348. BUILD_BUG_ON(TCP_MIN_SND_MSS <= MAX_TCP_OPTION_SPACE);
  3349. BUILD_BUG_ON(sizeof(struct tcp_skb_cb) >
  3350. FIELD_SIZEOF(struct sk_buff, cb));
  3351. percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL);
  3352. percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL);
  3353. inet_hashinfo_init(&tcp_hashinfo);
  3354. inet_hashinfo2_init(&tcp_hashinfo, "tcp_listen_portaddr_hash",
  3355. thash_entries, 21, /* one slot per 2 MB*/
  3356. 0, 64 * 1024);
  3357. tcp_hashinfo.bind_bucket_cachep =
  3358. kmem_cache_create("tcp_bind_bucket",
  3359. sizeof(struct inet_bind_bucket), 0,
  3360. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  3361. /* Size and allocate the main established and bind bucket
  3362. * hash tables.
  3363. *
  3364. * The methodology is similar to that of the buffer cache.
  3365. */
  3366. tcp_hashinfo.ehash =
  3367. alloc_large_system_hash("TCP established",
  3368. sizeof(struct inet_ehash_bucket),
  3369. thash_entries,
  3370. 17, /* one slot per 128 KB of memory */
  3371. 0,
  3372. NULL,
  3373. &tcp_hashinfo.ehash_mask,
  3374. 0,
  3375. thash_entries ? 0 : 512 * 1024);
  3376. for (i = 0; i <= tcp_hashinfo.ehash_mask; i++)
  3377. INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i);
  3378. if (inet_ehash_locks_alloc(&tcp_hashinfo))
  3379. panic("TCP: failed to alloc ehash_locks");
  3380. tcp_hashinfo.bhash =
  3381. alloc_large_system_hash("TCP bind",
  3382. sizeof(struct inet_bind_hashbucket),
  3383. tcp_hashinfo.ehash_mask + 1,
  3384. 17, /* one slot per 128 KB of memory */
  3385. 0,
  3386. &tcp_hashinfo.bhash_size,
  3387. NULL,
  3388. 0,
  3389. 64 * 1024);
  3390. tcp_hashinfo.bhash_size = 1U << tcp_hashinfo.bhash_size;
  3391. for (i = 0; i < tcp_hashinfo.bhash_size; i++) {
  3392. spin_lock_init(&tcp_hashinfo.bhash[i].lock);
  3393. INIT_HLIST_HEAD(&tcp_hashinfo.bhash[i].chain);
  3394. }
  3395. cnt = tcp_hashinfo.ehash_mask + 1;
  3396. sysctl_tcp_max_orphans = cnt / 2;
  3397. tcp_init_mem();
  3398. /* Set per-socket limits to no more than 1/128 the pressure threshold */
  3399. limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7);
  3400. max_wshare = min(4UL*1024*1024, limit);
  3401. max_rshare = min(6UL*1024*1024, limit);
  3402. init_net.ipv4.sysctl_tcp_wmem[0] = SK_MEM_QUANTUM;
  3403. init_net.ipv4.sysctl_tcp_wmem[1] = 16*1024;
  3404. init_net.ipv4.sysctl_tcp_wmem[2] = max(64*1024, max_wshare);
  3405. init_net.ipv4.sysctl_tcp_rmem[0] = SK_MEM_QUANTUM;
  3406. init_net.ipv4.sysctl_tcp_rmem[1] = 131072;
  3407. init_net.ipv4.sysctl_tcp_rmem[2] = max(131072, max_rshare);
  3408. pr_info("Hash tables configured (established %u bind %u)\n",
  3409. tcp_hashinfo.ehash_mask + 1, tcp_hashinfo.bhash_size);
  3410. tcp_v4_init();
  3411. tcp_metrics_init();
  3412. BUG_ON(tcp_register_congestion_control(&tcp_reno) != 0);
  3413. tcp_tasklet_init();
  3414. }