tcp.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483
  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 <net/icmp.h>
  271. #include <net/inet_common.h>
  272. #include <net/tcp.h>
  273. #include <net/xfrm.h>
  274. #include <net/ip.h>
  275. #include <net/sock.h>
  276. #include <linux/uaccess.h>
  277. #include <asm/ioctls.h>
  278. #include <net/busy_poll.h>
  279. int sysctl_tcp_min_tso_segs __read_mostly = 2;
  280. int sysctl_tcp_autocorking __read_mostly = 1;
  281. struct percpu_counter tcp_orphan_count;
  282. EXPORT_SYMBOL_GPL(tcp_orphan_count);
  283. long sysctl_tcp_mem[3] __read_mostly;
  284. int sysctl_tcp_wmem[3] __read_mostly;
  285. int sysctl_tcp_rmem[3] __read_mostly;
  286. EXPORT_SYMBOL(sysctl_tcp_mem);
  287. EXPORT_SYMBOL(sysctl_tcp_rmem);
  288. EXPORT_SYMBOL(sysctl_tcp_wmem);
  289. atomic_long_t tcp_memory_allocated; /* Current allocated memory. */
  290. EXPORT_SYMBOL(tcp_memory_allocated);
  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 (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 (!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. tcp_init_xmit_timers(sk);
  389. INIT_LIST_HEAD(&tp->tsq_node);
  390. icsk->icsk_rto = TCP_TIMEOUT_INIT;
  391. tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT);
  392. minmax_reset(&tp->rtt_min, tcp_jiffies32, ~0U);
  393. /* So many TCP implementations out there (incorrectly) count the
  394. * initial SYN frame in their delayed-ACK and congestion control
  395. * algorithms that we must have the following bandaid to talk
  396. * efficiently to them. -DaveM
  397. */
  398. tp->snd_cwnd = TCP_INIT_CWND;
  399. /* There's a bubble in the pipe until at least the first ACK. */
  400. tp->app_limited = ~0U;
  401. /* See draft-stevens-tcpca-spec-01 for discussion of the
  402. * initialization of these values.
  403. */
  404. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  405. tp->snd_cwnd_clamp = ~0;
  406. tp->mss_cache = TCP_MSS_DEFAULT;
  407. tp->reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering;
  408. tcp_assign_congestion_control(sk);
  409. tp->tsoffset = 0;
  410. sk->sk_state = TCP_CLOSE;
  411. sk->sk_write_space = sk_stream_write_space;
  412. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  413. icsk->icsk_sync_mss = tcp_sync_mss;
  414. sk->sk_sndbuf = sysctl_tcp_wmem[1];
  415. sk->sk_rcvbuf = sysctl_tcp_rmem[1];
  416. sk_sockets_allocated_inc(sk);
  417. }
  418. EXPORT_SYMBOL(tcp_init_sock);
  419. static void tcp_tx_timestamp(struct sock *sk, u16 tsflags, struct sk_buff *skb)
  420. {
  421. if (tsflags && skb) {
  422. struct skb_shared_info *shinfo = skb_shinfo(skb);
  423. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  424. sock_tx_timestamp(sk, tsflags, &shinfo->tx_flags);
  425. if (tsflags & SOF_TIMESTAMPING_TX_ACK)
  426. tcb->txstamp_ack = 1;
  427. if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK)
  428. shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
  429. }
  430. }
  431. /*
  432. * Wait for a TCP event.
  433. *
  434. * Note that we don't need to lock the socket, as the upper poll layers
  435. * take care of normal races (between the test and the event) and we don't
  436. * go look at any of the socket buffers directly.
  437. */
  438. unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
  439. {
  440. unsigned int mask;
  441. struct sock *sk = sock->sk;
  442. const struct tcp_sock *tp = tcp_sk(sk);
  443. int state;
  444. sock_rps_record_flow(sk);
  445. sock_poll_wait(file, sk_sleep(sk), wait);
  446. state = sk_state_load(sk);
  447. if (state == TCP_LISTEN)
  448. return inet_csk_listen_poll(sk);
  449. /* Socket is not locked. We are protected from async events
  450. * by poll logic and correct handling of state changes
  451. * made by other threads is impossible in any case.
  452. */
  453. mask = 0;
  454. /*
  455. * POLLHUP is certainly not done right. But poll() doesn't
  456. * have a notion of HUP in just one direction, and for a
  457. * socket the read side is more interesting.
  458. *
  459. * Some poll() documentation says that POLLHUP is incompatible
  460. * with the POLLOUT/POLLWR flags, so somebody should check this
  461. * all. But careful, it tends to be safer to return too many
  462. * bits than too few, and you can easily break real applications
  463. * if you don't tell them that something has hung up!
  464. *
  465. * Check-me.
  466. *
  467. * Check number 1. POLLHUP is _UNMASKABLE_ event (see UNIX98 and
  468. * our fs/select.c). It means that after we received EOF,
  469. * poll always returns immediately, making impossible poll() on write()
  470. * in state CLOSE_WAIT. One solution is evident --- to set POLLHUP
  471. * if and only if shutdown has been made in both directions.
  472. * Actually, it is interesting to look how Solaris and DUX
  473. * solve this dilemma. I would prefer, if POLLHUP were maskable,
  474. * then we could set it on SND_SHUTDOWN. BTW examples given
  475. * in Stevens' books assume exactly this behaviour, it explains
  476. * why POLLHUP is incompatible with POLLOUT. --ANK
  477. *
  478. * NOTE. Check for TCP_CLOSE is added. The goal is to prevent
  479. * blocking on fresh not-connected or disconnected socket. --ANK
  480. */
  481. if (sk->sk_shutdown == SHUTDOWN_MASK || state == TCP_CLOSE)
  482. mask |= POLLHUP;
  483. if (sk->sk_shutdown & RCV_SHUTDOWN)
  484. mask |= POLLIN | POLLRDNORM | POLLRDHUP;
  485. /* Connected or passive Fast Open socket? */
  486. if (state != TCP_SYN_SENT &&
  487. (state != TCP_SYN_RECV || tp->fastopen_rsk)) {
  488. int target = sock_rcvlowat(sk, 0, INT_MAX);
  489. if (tp->urg_seq == tp->copied_seq &&
  490. !sock_flag(sk, SOCK_URGINLINE) &&
  491. tp->urg_data)
  492. target++;
  493. if (tp->rcv_nxt - tp->copied_seq >= target)
  494. mask |= POLLIN | POLLRDNORM;
  495. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  496. if (sk_stream_is_writeable(sk)) {
  497. mask |= POLLOUT | POLLWRNORM;
  498. } else { /* send SIGIO later */
  499. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  500. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  501. /* Race breaker. If space is freed after
  502. * wspace test but before the flags are set,
  503. * IO signal will be lost. Memory barrier
  504. * pairs with the input side.
  505. */
  506. smp_mb__after_atomic();
  507. if (sk_stream_is_writeable(sk))
  508. mask |= POLLOUT | POLLWRNORM;
  509. }
  510. } else
  511. mask |= POLLOUT | POLLWRNORM;
  512. if (tp->urg_data & TCP_URG_VALID)
  513. mask |= POLLPRI;
  514. } else if (state == TCP_SYN_SENT && inet_sk(sk)->defer_connect) {
  515. /* Active TCP fastopen socket with defer_connect
  516. * Return POLLOUT so application can call write()
  517. * in order for kernel to generate SYN+data
  518. */
  519. mask |= POLLOUT | POLLWRNORM;
  520. }
  521. /* This barrier is coupled with smp_wmb() in tcp_reset() */
  522. smp_rmb();
  523. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  524. mask |= POLLERR;
  525. return mask;
  526. }
  527. EXPORT_SYMBOL(tcp_poll);
  528. int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  529. {
  530. struct tcp_sock *tp = tcp_sk(sk);
  531. int answ;
  532. bool slow;
  533. switch (cmd) {
  534. case SIOCINQ:
  535. if (sk->sk_state == TCP_LISTEN)
  536. return -EINVAL;
  537. slow = lock_sock_fast(sk);
  538. answ = tcp_inq(sk);
  539. unlock_sock_fast(sk, slow);
  540. break;
  541. case SIOCATMARK:
  542. answ = tp->urg_data && tp->urg_seq == tp->copied_seq;
  543. break;
  544. case SIOCOUTQ:
  545. if (sk->sk_state == TCP_LISTEN)
  546. return -EINVAL;
  547. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
  548. answ = 0;
  549. else
  550. answ = tp->write_seq - tp->snd_una;
  551. break;
  552. case SIOCOUTQNSD:
  553. if (sk->sk_state == TCP_LISTEN)
  554. return -EINVAL;
  555. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
  556. answ = 0;
  557. else
  558. answ = tp->write_seq - tp->snd_nxt;
  559. break;
  560. default:
  561. return -ENOIOCTLCMD;
  562. }
  563. return put_user(answ, (int __user *)arg);
  564. }
  565. EXPORT_SYMBOL(tcp_ioctl);
  566. static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb)
  567. {
  568. TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
  569. tp->pushed_seq = tp->write_seq;
  570. }
  571. static inline bool forced_push(const struct tcp_sock *tp)
  572. {
  573. return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1));
  574. }
  575. static void skb_entail(struct sock *sk, struct sk_buff *skb)
  576. {
  577. struct tcp_sock *tp = tcp_sk(sk);
  578. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  579. skb->csum = 0;
  580. tcb->seq = tcb->end_seq = tp->write_seq;
  581. tcb->tcp_flags = TCPHDR_ACK;
  582. tcb->sacked = 0;
  583. __skb_header_release(skb);
  584. tcp_add_write_queue_tail(sk, skb);
  585. sk->sk_wmem_queued += skb->truesize;
  586. sk_mem_charge(sk, skb->truesize);
  587. if (tp->nonagle & TCP_NAGLE_PUSH)
  588. tp->nonagle &= ~TCP_NAGLE_PUSH;
  589. tcp_slow_start_after_idle_check(sk);
  590. }
  591. static inline void tcp_mark_urg(struct tcp_sock *tp, int flags)
  592. {
  593. if (flags & MSG_OOB)
  594. tp->snd_up = tp->write_seq;
  595. }
  596. /* If a not yet filled skb is pushed, do not send it if
  597. * we have data packets in Qdisc or NIC queues :
  598. * Because TX completion will happen shortly, it gives a chance
  599. * to coalesce future sendmsg() payload into this skb, without
  600. * need for a timer, and with no latency trade off.
  601. * As packets containing data payload have a bigger truesize
  602. * than pure acks (dataless) packets, the last checks prevent
  603. * autocorking if we only have an ACK in Qdisc/NIC queues,
  604. * or if TX completion was delayed after we processed ACK packet.
  605. */
  606. static bool tcp_should_autocork(struct sock *sk, struct sk_buff *skb,
  607. int size_goal)
  608. {
  609. return skb->len < size_goal &&
  610. sysctl_tcp_autocorking &&
  611. skb != tcp_write_queue_head(sk) &&
  612. refcount_read(&sk->sk_wmem_alloc) > skb->truesize;
  613. }
  614. static void tcp_push(struct sock *sk, int flags, int mss_now,
  615. int nonagle, int size_goal)
  616. {
  617. struct tcp_sock *tp = tcp_sk(sk);
  618. struct sk_buff *skb;
  619. if (!tcp_send_head(sk))
  620. return;
  621. skb = tcp_write_queue_tail(sk);
  622. if (!(flags & MSG_MORE) || forced_push(tp))
  623. tcp_mark_push(tp, skb);
  624. tcp_mark_urg(tp, flags);
  625. if (tcp_should_autocork(sk, skb, size_goal)) {
  626. /* avoid atomic op if TSQ_THROTTLED bit is already set */
  627. if (!test_bit(TSQ_THROTTLED, &sk->sk_tsq_flags)) {
  628. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAUTOCORKING);
  629. set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags);
  630. }
  631. /* It is possible TX completion already happened
  632. * before we set TSQ_THROTTLED.
  633. */
  634. if (refcount_read(&sk->sk_wmem_alloc) > skb->truesize)
  635. return;
  636. }
  637. if (flags & MSG_MORE)
  638. nonagle = TCP_NAGLE_CORK;
  639. __tcp_push_pending_frames(sk, mss_now, nonagle);
  640. }
  641. static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  642. unsigned int offset, size_t len)
  643. {
  644. struct tcp_splice_state *tss = rd_desc->arg.data;
  645. int ret;
  646. ret = skb_splice_bits(skb, skb->sk, offset, tss->pipe,
  647. min(rd_desc->count, len), tss->flags);
  648. if (ret > 0)
  649. rd_desc->count -= ret;
  650. return ret;
  651. }
  652. static int __tcp_splice_read(struct sock *sk, struct tcp_splice_state *tss)
  653. {
  654. /* Store TCP splice context information in read_descriptor_t. */
  655. read_descriptor_t rd_desc = {
  656. .arg.data = tss,
  657. .count = tss->len,
  658. };
  659. return tcp_read_sock(sk, &rd_desc, tcp_splice_data_recv);
  660. }
  661. /**
  662. * tcp_splice_read - splice data from TCP socket to a pipe
  663. * @sock: socket to splice from
  664. * @ppos: position (not valid)
  665. * @pipe: pipe to splice to
  666. * @len: number of bytes to splice
  667. * @flags: splice modifier flags
  668. *
  669. * Description:
  670. * Will read pages from given socket and fill them into a pipe.
  671. *
  672. **/
  673. ssize_t tcp_splice_read(struct socket *sock, loff_t *ppos,
  674. struct pipe_inode_info *pipe, size_t len,
  675. unsigned int flags)
  676. {
  677. struct sock *sk = sock->sk;
  678. struct tcp_splice_state tss = {
  679. .pipe = pipe,
  680. .len = len,
  681. .flags = flags,
  682. };
  683. long timeo;
  684. ssize_t spliced;
  685. int ret;
  686. sock_rps_record_flow(sk);
  687. /*
  688. * We can't seek on a socket input
  689. */
  690. if (unlikely(*ppos))
  691. return -ESPIPE;
  692. ret = spliced = 0;
  693. lock_sock(sk);
  694. timeo = sock_rcvtimeo(sk, sock->file->f_flags & O_NONBLOCK);
  695. while (tss.len) {
  696. ret = __tcp_splice_read(sk, &tss);
  697. if (ret < 0)
  698. break;
  699. else if (!ret) {
  700. if (spliced)
  701. break;
  702. if (sock_flag(sk, SOCK_DONE))
  703. break;
  704. if (sk->sk_err) {
  705. ret = sock_error(sk);
  706. break;
  707. }
  708. if (sk->sk_shutdown & RCV_SHUTDOWN)
  709. break;
  710. if (sk->sk_state == TCP_CLOSE) {
  711. /*
  712. * This occurs when user tries to read
  713. * from never connected socket.
  714. */
  715. if (!sock_flag(sk, SOCK_DONE))
  716. ret = -ENOTCONN;
  717. break;
  718. }
  719. if (!timeo) {
  720. ret = -EAGAIN;
  721. break;
  722. }
  723. /* if __tcp_splice_read() got nothing while we have
  724. * an skb in receive queue, we do not want to loop.
  725. * This might happen with URG data.
  726. */
  727. if (!skb_queue_empty(&sk->sk_receive_queue))
  728. break;
  729. sk_wait_data(sk, &timeo, NULL);
  730. if (signal_pending(current)) {
  731. ret = sock_intr_errno(timeo);
  732. break;
  733. }
  734. continue;
  735. }
  736. tss.len -= ret;
  737. spliced += ret;
  738. if (!timeo)
  739. break;
  740. release_sock(sk);
  741. lock_sock(sk);
  742. if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
  743. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  744. signal_pending(current))
  745. break;
  746. }
  747. release_sock(sk);
  748. if (spliced)
  749. return spliced;
  750. return ret;
  751. }
  752. EXPORT_SYMBOL(tcp_splice_read);
  753. struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp,
  754. bool force_schedule)
  755. {
  756. struct sk_buff *skb;
  757. /* The TCP header must be at least 32-bit aligned. */
  758. size = ALIGN(size, 4);
  759. if (unlikely(tcp_under_memory_pressure(sk)))
  760. sk_mem_reclaim_partial(sk);
  761. skb = alloc_skb_fclone(size + sk->sk_prot->max_header, gfp);
  762. if (likely(skb)) {
  763. bool mem_scheduled;
  764. if (force_schedule) {
  765. mem_scheduled = true;
  766. sk_forced_mem_schedule(sk, skb->truesize);
  767. } else {
  768. mem_scheduled = sk_wmem_schedule(sk, skb->truesize);
  769. }
  770. if (likely(mem_scheduled)) {
  771. skb_reserve(skb, sk->sk_prot->max_header);
  772. /*
  773. * Make sure that we have exactly size bytes
  774. * available to the caller, no more, no less.
  775. */
  776. skb->reserved_tailroom = skb->end - skb->tail - size;
  777. return skb;
  778. }
  779. __kfree_skb(skb);
  780. } else {
  781. sk->sk_prot->enter_memory_pressure(sk);
  782. sk_stream_moderate_sndbuf(sk);
  783. }
  784. return NULL;
  785. }
  786. static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now,
  787. int large_allowed)
  788. {
  789. struct tcp_sock *tp = tcp_sk(sk);
  790. u32 new_size_goal, size_goal;
  791. if (!large_allowed || !sk_can_gso(sk))
  792. return mss_now;
  793. /* Note : tcp_tso_autosize() will eventually split this later */
  794. new_size_goal = sk->sk_gso_max_size - 1 - MAX_TCP_HEADER;
  795. new_size_goal = tcp_bound_to_half_wnd(tp, new_size_goal);
  796. /* We try hard to avoid divides here */
  797. size_goal = tp->gso_segs * mss_now;
  798. if (unlikely(new_size_goal < size_goal ||
  799. new_size_goal >= size_goal + mss_now)) {
  800. tp->gso_segs = min_t(u16, new_size_goal / mss_now,
  801. sk->sk_gso_max_segs);
  802. size_goal = tp->gso_segs * mss_now;
  803. }
  804. return max(size_goal, mss_now);
  805. }
  806. static int tcp_send_mss(struct sock *sk, int *size_goal, int flags)
  807. {
  808. int mss_now;
  809. mss_now = tcp_current_mss(sk);
  810. *size_goal = tcp_xmit_size_goal(sk, mss_now, !(flags & MSG_OOB));
  811. return mss_now;
  812. }
  813. ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  814. size_t size, int flags)
  815. {
  816. struct tcp_sock *tp = tcp_sk(sk);
  817. int mss_now, size_goal;
  818. int err;
  819. ssize_t copied;
  820. long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  821. /* Wait for a connection to finish. One exception is TCP Fast Open
  822. * (passive side) where data is allowed to be sent before a connection
  823. * is fully established.
  824. */
  825. if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) &&
  826. !tcp_passive_fastopen(sk)) {
  827. err = sk_stream_wait_connect(sk, &timeo);
  828. if (err != 0)
  829. goto out_err;
  830. }
  831. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  832. mss_now = tcp_send_mss(sk, &size_goal, flags);
  833. copied = 0;
  834. err = -EPIPE;
  835. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  836. goto out_err;
  837. while (size > 0) {
  838. struct sk_buff *skb = tcp_write_queue_tail(sk);
  839. int copy, i;
  840. bool can_coalesce;
  841. if (!tcp_send_head(sk) || (copy = size_goal - skb->len) <= 0 ||
  842. !tcp_skb_can_collapse_to(skb)) {
  843. new_segment:
  844. if (!sk_stream_memory_free(sk))
  845. goto wait_for_sndbuf;
  846. skb = sk_stream_alloc_skb(sk, 0, sk->sk_allocation,
  847. skb_queue_empty(&sk->sk_write_queue));
  848. if (!skb)
  849. goto wait_for_memory;
  850. skb_entail(sk, skb);
  851. copy = size_goal;
  852. }
  853. if (copy > size)
  854. copy = size;
  855. i = skb_shinfo(skb)->nr_frags;
  856. can_coalesce = skb_can_coalesce(skb, i, page, offset);
  857. if (!can_coalesce && i >= sysctl_max_skb_frags) {
  858. tcp_mark_push(tp, skb);
  859. goto new_segment;
  860. }
  861. if (!sk_wmem_schedule(sk, copy))
  862. goto wait_for_memory;
  863. if (can_coalesce) {
  864. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  865. } else {
  866. get_page(page);
  867. skb_fill_page_desc(skb, i, page, offset, copy);
  868. }
  869. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  870. skb->len += copy;
  871. skb->data_len += copy;
  872. skb->truesize += copy;
  873. sk->sk_wmem_queued += copy;
  874. sk_mem_charge(sk, copy);
  875. skb->ip_summed = CHECKSUM_PARTIAL;
  876. tp->write_seq += copy;
  877. TCP_SKB_CB(skb)->end_seq += copy;
  878. tcp_skb_pcount_set(skb, 0);
  879. if (!copied)
  880. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
  881. copied += copy;
  882. offset += copy;
  883. size -= copy;
  884. if (!size)
  885. goto out;
  886. if (skb->len < size_goal || (flags & MSG_OOB))
  887. continue;
  888. if (forced_push(tp)) {
  889. tcp_mark_push(tp, skb);
  890. __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
  891. } else if (skb == tcp_send_head(sk))
  892. tcp_push_one(sk, mss_now);
  893. continue;
  894. wait_for_sndbuf:
  895. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  896. wait_for_memory:
  897. tcp_push(sk, flags & ~MSG_MORE, mss_now,
  898. TCP_NAGLE_PUSH, size_goal);
  899. err = sk_stream_wait_memory(sk, &timeo);
  900. if (err != 0)
  901. goto do_error;
  902. mss_now = tcp_send_mss(sk, &size_goal, flags);
  903. }
  904. out:
  905. if (copied) {
  906. tcp_tx_timestamp(sk, sk->sk_tsflags, tcp_write_queue_tail(sk));
  907. if (!(flags & MSG_SENDPAGE_NOTLAST))
  908. tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
  909. }
  910. return copied;
  911. do_error:
  912. if (copied)
  913. goto out;
  914. out_err:
  915. /* make sure we wake any epoll edge trigger waiter */
  916. if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 &&
  917. err == -EAGAIN)) {
  918. sk->sk_write_space(sk);
  919. tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
  920. }
  921. return sk_stream_error(sk, flags, err);
  922. }
  923. EXPORT_SYMBOL_GPL(do_tcp_sendpages);
  924. int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
  925. size_t size, int flags)
  926. {
  927. if (!(sk->sk_route_caps & NETIF_F_SG) ||
  928. !sk_check_csum_caps(sk))
  929. return sock_no_sendpage(sk->sk_socket, page, offset, size,
  930. flags);
  931. tcp_rate_check_app_limited(sk); /* is sending application-limited? */
  932. return do_tcp_sendpages(sk, page, offset, size, flags);
  933. }
  934. int tcp_sendpage(struct sock *sk, struct page *page, int offset,
  935. size_t size, int flags)
  936. {
  937. int ret;
  938. lock_sock(sk);
  939. ret = tcp_sendpage_locked(sk, page, offset, size, flags);
  940. release_sock(sk);
  941. return ret;
  942. }
  943. EXPORT_SYMBOL(tcp_sendpage);
  944. /* Do not bother using a page frag for very small frames.
  945. * But use this heuristic only for the first skb in write queue.
  946. *
  947. * Having no payload in skb->head allows better SACK shifting
  948. * in tcp_shift_skb_data(), reducing sack/rack overhead, because
  949. * write queue has less skbs.
  950. * Each skb can hold up to MAX_SKB_FRAGS * 32Kbytes, or ~0.5 MB.
  951. * This also speeds up tso_fragment(), since it wont fallback
  952. * to tcp_fragment().
  953. */
  954. static int linear_payload_sz(bool first_skb)
  955. {
  956. if (first_skb)
  957. return SKB_WITH_OVERHEAD(2048 - MAX_TCP_HEADER);
  958. return 0;
  959. }
  960. static int select_size(const struct sock *sk, bool sg, bool first_skb)
  961. {
  962. const struct tcp_sock *tp = tcp_sk(sk);
  963. int tmp = tp->mss_cache;
  964. if (sg) {
  965. if (sk_can_gso(sk)) {
  966. tmp = linear_payload_sz(first_skb);
  967. } else {
  968. int pgbreak = SKB_MAX_HEAD(MAX_TCP_HEADER);
  969. if (tmp >= pgbreak &&
  970. tmp <= pgbreak + (MAX_SKB_FRAGS - 1) * PAGE_SIZE)
  971. tmp = pgbreak;
  972. }
  973. }
  974. return tmp;
  975. }
  976. void tcp_free_fastopen_req(struct tcp_sock *tp)
  977. {
  978. if (tp->fastopen_req) {
  979. kfree(tp->fastopen_req);
  980. tp->fastopen_req = NULL;
  981. }
  982. }
  983. static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
  984. int *copied, size_t size)
  985. {
  986. struct tcp_sock *tp = tcp_sk(sk);
  987. struct inet_sock *inet = inet_sk(sk);
  988. struct sockaddr *uaddr = msg->msg_name;
  989. int err, flags;
  990. if (!(sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) ||
  991. (uaddr && msg->msg_namelen >= sizeof(uaddr->sa_family) &&
  992. uaddr->sa_family == AF_UNSPEC))
  993. return -EOPNOTSUPP;
  994. if (tp->fastopen_req)
  995. return -EALREADY; /* Another Fast Open is in progress */
  996. tp->fastopen_req = kzalloc(sizeof(struct tcp_fastopen_request),
  997. sk->sk_allocation);
  998. if (unlikely(!tp->fastopen_req))
  999. return -ENOBUFS;
  1000. tp->fastopen_req->data = msg;
  1001. tp->fastopen_req->size = size;
  1002. if (inet->defer_connect) {
  1003. err = tcp_connect(sk);
  1004. /* Same failure procedure as in tcp_v4/6_connect */
  1005. if (err) {
  1006. tcp_set_state(sk, TCP_CLOSE);
  1007. inet->inet_dport = 0;
  1008. sk->sk_route_caps = 0;
  1009. }
  1010. }
  1011. flags = (msg->msg_flags & MSG_DONTWAIT) ? O_NONBLOCK : 0;
  1012. err = __inet_stream_connect(sk->sk_socket, uaddr,
  1013. msg->msg_namelen, flags, 1);
  1014. /* fastopen_req could already be freed in __inet_stream_connect
  1015. * if the connection times out or gets rst
  1016. */
  1017. if (tp->fastopen_req) {
  1018. *copied = tp->fastopen_req->copied;
  1019. tcp_free_fastopen_req(tp);
  1020. inet->defer_connect = 0;
  1021. }
  1022. return err;
  1023. }
  1024. int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
  1025. {
  1026. struct tcp_sock *tp = tcp_sk(sk);
  1027. struct ubuf_info *uarg = NULL;
  1028. struct sk_buff *skb;
  1029. struct sockcm_cookie sockc;
  1030. int flags, err, copied = 0;
  1031. int mss_now = 0, size_goal, copied_syn = 0;
  1032. bool process_backlog = false;
  1033. bool sg;
  1034. long timeo;
  1035. flags = msg->msg_flags;
  1036. if (flags & MSG_ZEROCOPY && size) {
  1037. if (sk->sk_state != TCP_ESTABLISHED) {
  1038. err = -EINVAL;
  1039. goto out_err;
  1040. }
  1041. skb = tcp_send_head(sk) ? tcp_write_queue_tail(sk) : NULL;
  1042. uarg = sock_zerocopy_realloc(sk, size, skb_zcopy(skb));
  1043. if (!uarg) {
  1044. err = -ENOBUFS;
  1045. goto out_err;
  1046. }
  1047. /* skb may be freed in main loop, keep extra ref on uarg */
  1048. sock_zerocopy_get(uarg);
  1049. if (!(sk_check_csum_caps(sk) && sk->sk_route_caps & NETIF_F_SG))
  1050. uarg->zerocopy = 0;
  1051. }
  1052. if (unlikely(flags & MSG_FASTOPEN || inet_sk(sk)->defer_connect)) {
  1053. err = tcp_sendmsg_fastopen(sk, msg, &copied_syn, size);
  1054. if (err == -EINPROGRESS && copied_syn > 0)
  1055. goto out;
  1056. else if (err)
  1057. goto out_err;
  1058. }
  1059. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  1060. tcp_rate_check_app_limited(sk); /* is sending application-limited? */
  1061. /* Wait for a connection to finish. One exception is TCP Fast Open
  1062. * (passive side) where data is allowed to be sent before a connection
  1063. * is fully established.
  1064. */
  1065. if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) &&
  1066. !tcp_passive_fastopen(sk)) {
  1067. err = sk_stream_wait_connect(sk, &timeo);
  1068. if (err != 0)
  1069. goto do_error;
  1070. }
  1071. if (unlikely(tp->repair)) {
  1072. if (tp->repair_queue == TCP_RECV_QUEUE) {
  1073. copied = tcp_send_rcvq(sk, msg, size);
  1074. goto out_nopush;
  1075. }
  1076. err = -EINVAL;
  1077. if (tp->repair_queue == TCP_NO_QUEUE)
  1078. goto out_err;
  1079. /* 'common' sending to sendq */
  1080. }
  1081. sockc.tsflags = sk->sk_tsflags;
  1082. if (msg->msg_controllen) {
  1083. err = sock_cmsg_send(sk, msg, &sockc);
  1084. if (unlikely(err)) {
  1085. err = -EINVAL;
  1086. goto out_err;
  1087. }
  1088. }
  1089. /* This should be in poll */
  1090. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  1091. /* Ok commence sending. */
  1092. copied = 0;
  1093. restart:
  1094. mss_now = tcp_send_mss(sk, &size_goal, flags);
  1095. err = -EPIPE;
  1096. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  1097. goto do_error;
  1098. sg = !!(sk->sk_route_caps & NETIF_F_SG);
  1099. while (msg_data_left(msg)) {
  1100. int copy = 0;
  1101. int max = size_goal;
  1102. skb = tcp_write_queue_tail(sk);
  1103. if (tcp_send_head(sk)) {
  1104. if (skb->ip_summed == CHECKSUM_NONE)
  1105. max = mss_now;
  1106. copy = max - skb->len;
  1107. }
  1108. if (copy <= 0 || !tcp_skb_can_collapse_to(skb)) {
  1109. bool first_skb;
  1110. new_segment:
  1111. /* Allocate new segment. If the interface is SG,
  1112. * allocate skb fitting to single page.
  1113. */
  1114. if (!sk_stream_memory_free(sk))
  1115. goto wait_for_sndbuf;
  1116. if (process_backlog && sk_flush_backlog(sk)) {
  1117. process_backlog = false;
  1118. goto restart;
  1119. }
  1120. first_skb = skb_queue_empty(&sk->sk_write_queue);
  1121. skb = sk_stream_alloc_skb(sk,
  1122. select_size(sk, sg, first_skb),
  1123. sk->sk_allocation,
  1124. first_skb);
  1125. if (!skb)
  1126. goto wait_for_memory;
  1127. process_backlog = true;
  1128. /*
  1129. * Check whether we can use HW checksum.
  1130. */
  1131. if (sk_check_csum_caps(sk))
  1132. skb->ip_summed = CHECKSUM_PARTIAL;
  1133. skb_entail(sk, skb);
  1134. copy = size_goal;
  1135. max = size_goal;
  1136. /* All packets are restored as if they have
  1137. * already been sent. skb_mstamp isn't set to
  1138. * avoid wrong rtt estimation.
  1139. */
  1140. if (tp->repair)
  1141. TCP_SKB_CB(skb)->sacked |= TCPCB_REPAIRED;
  1142. }
  1143. /* Try to append data to the end of skb. */
  1144. if (copy > msg_data_left(msg))
  1145. copy = msg_data_left(msg);
  1146. /* Where to copy to? */
  1147. if (skb_availroom(skb) > 0) {
  1148. /* We have some space in skb head. Superb! */
  1149. copy = min_t(int, copy, skb_availroom(skb));
  1150. err = skb_add_data_nocache(sk, skb, &msg->msg_iter, copy);
  1151. if (err)
  1152. goto do_fault;
  1153. } else if (!uarg || !uarg->zerocopy) {
  1154. bool merge = true;
  1155. int i = skb_shinfo(skb)->nr_frags;
  1156. struct page_frag *pfrag = sk_page_frag(sk);
  1157. if (!sk_page_frag_refill(sk, pfrag))
  1158. goto wait_for_memory;
  1159. if (!skb_can_coalesce(skb, i, pfrag->page,
  1160. pfrag->offset)) {
  1161. if (i >= sysctl_max_skb_frags || !sg) {
  1162. tcp_mark_push(tp, skb);
  1163. goto new_segment;
  1164. }
  1165. merge = false;
  1166. }
  1167. copy = min_t(int, copy, pfrag->size - pfrag->offset);
  1168. if (!sk_wmem_schedule(sk, copy))
  1169. goto wait_for_memory;
  1170. err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb,
  1171. pfrag->page,
  1172. pfrag->offset,
  1173. copy);
  1174. if (err)
  1175. goto do_error;
  1176. /* Update the skb. */
  1177. if (merge) {
  1178. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  1179. } else {
  1180. skb_fill_page_desc(skb, i, pfrag->page,
  1181. pfrag->offset, copy);
  1182. page_ref_inc(pfrag->page);
  1183. }
  1184. pfrag->offset += copy;
  1185. } else {
  1186. err = skb_zerocopy_iter_stream(sk, skb, msg, copy, uarg);
  1187. if (err == -EMSGSIZE || err == -EEXIST)
  1188. goto new_segment;
  1189. if (err < 0)
  1190. goto do_error;
  1191. copy = err;
  1192. }
  1193. if (!copied)
  1194. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_PSH;
  1195. tp->write_seq += copy;
  1196. TCP_SKB_CB(skb)->end_seq += copy;
  1197. tcp_skb_pcount_set(skb, 0);
  1198. copied += copy;
  1199. if (!msg_data_left(msg)) {
  1200. if (unlikely(flags & MSG_EOR))
  1201. TCP_SKB_CB(skb)->eor = 1;
  1202. goto out;
  1203. }
  1204. if (skb->len < max || (flags & MSG_OOB) || unlikely(tp->repair))
  1205. continue;
  1206. if (forced_push(tp)) {
  1207. tcp_mark_push(tp, skb);
  1208. __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH);
  1209. } else if (skb == tcp_send_head(sk))
  1210. tcp_push_one(sk, mss_now);
  1211. continue;
  1212. wait_for_sndbuf:
  1213. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1214. wait_for_memory:
  1215. if (copied)
  1216. tcp_push(sk, flags & ~MSG_MORE, mss_now,
  1217. TCP_NAGLE_PUSH, size_goal);
  1218. err = sk_stream_wait_memory(sk, &timeo);
  1219. if (err != 0)
  1220. goto do_error;
  1221. mss_now = tcp_send_mss(sk, &size_goal, flags);
  1222. }
  1223. out:
  1224. if (copied) {
  1225. tcp_tx_timestamp(sk, sockc.tsflags, tcp_write_queue_tail(sk));
  1226. tcp_push(sk, flags, mss_now, tp->nonagle, size_goal);
  1227. }
  1228. out_nopush:
  1229. sock_zerocopy_put(uarg);
  1230. return copied + copied_syn;
  1231. do_fault:
  1232. if (!skb->len) {
  1233. tcp_unlink_write_queue(skb, sk);
  1234. /* It is the one place in all of TCP, except connection
  1235. * reset, where we can be unlinking the send_head.
  1236. */
  1237. tcp_check_send_head(sk, skb);
  1238. sk_wmem_free_skb(sk, skb);
  1239. }
  1240. do_error:
  1241. if (copied + copied_syn)
  1242. goto out;
  1243. out_err:
  1244. sock_zerocopy_put_abort(uarg);
  1245. err = sk_stream_error(sk, flags, err);
  1246. /* make sure we wake any epoll edge trigger waiter */
  1247. if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 &&
  1248. err == -EAGAIN)) {
  1249. sk->sk_write_space(sk);
  1250. tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
  1251. }
  1252. return err;
  1253. }
  1254. int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  1255. {
  1256. int ret;
  1257. lock_sock(sk);
  1258. ret = tcp_sendmsg_locked(sk, msg, size);
  1259. release_sock(sk);
  1260. return ret;
  1261. }
  1262. EXPORT_SYMBOL(tcp_sendmsg);
  1263. /*
  1264. * Handle reading urgent data. BSD has very simple semantics for
  1265. * this, no blocking and very strange errors 8)
  1266. */
  1267. static int tcp_recv_urg(struct sock *sk, struct msghdr *msg, int len, int flags)
  1268. {
  1269. struct tcp_sock *tp = tcp_sk(sk);
  1270. /* No URG data to read. */
  1271. if (sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data ||
  1272. tp->urg_data == TCP_URG_READ)
  1273. return -EINVAL; /* Yes this is right ! */
  1274. if (sk->sk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DONE))
  1275. return -ENOTCONN;
  1276. if (tp->urg_data & TCP_URG_VALID) {
  1277. int err = 0;
  1278. char c = tp->urg_data;
  1279. if (!(flags & MSG_PEEK))
  1280. tp->urg_data = TCP_URG_READ;
  1281. /* Read urgent data. */
  1282. msg->msg_flags |= MSG_OOB;
  1283. if (len > 0) {
  1284. if (!(flags & MSG_TRUNC))
  1285. err = memcpy_to_msg(msg, &c, 1);
  1286. len = 1;
  1287. } else
  1288. msg->msg_flags |= MSG_TRUNC;
  1289. return err ? -EFAULT : len;
  1290. }
  1291. if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
  1292. return 0;
  1293. /* Fixed the recv(..., MSG_OOB) behaviour. BSD docs and
  1294. * the available implementations agree in this case:
  1295. * this call should never block, independent of the
  1296. * blocking state of the socket.
  1297. * Mike <pall@rz.uni-karlsruhe.de>
  1298. */
  1299. return -EAGAIN;
  1300. }
  1301. static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)
  1302. {
  1303. struct sk_buff *skb;
  1304. int copied = 0, err = 0;
  1305. /* XXX -- need to support SO_PEEK_OFF */
  1306. skb_queue_walk(&sk->sk_write_queue, skb) {
  1307. err = skb_copy_datagram_msg(skb, 0, msg, skb->len);
  1308. if (err)
  1309. break;
  1310. copied += skb->len;
  1311. }
  1312. return err ?: copied;
  1313. }
  1314. /* Clean up the receive buffer for full frames taken by the user,
  1315. * then send an ACK if necessary. COPIED is the number of bytes
  1316. * tcp_recvmsg has given to the user so far, it speeds up the
  1317. * calculation of whether or not we must ACK for the sake of
  1318. * a window update.
  1319. */
  1320. static void tcp_cleanup_rbuf(struct sock *sk, int copied)
  1321. {
  1322. struct tcp_sock *tp = tcp_sk(sk);
  1323. bool time_to_ack = false;
  1324. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  1325. WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq),
  1326. "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n",
  1327. tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt);
  1328. if (inet_csk_ack_scheduled(sk)) {
  1329. const struct inet_connection_sock *icsk = inet_csk(sk);
  1330. /* Delayed ACKs frequently hit locked sockets during bulk
  1331. * receive. */
  1332. if (icsk->icsk_ack.blocked ||
  1333. /* Once-per-two-segments ACK was not sent by tcp_input.c */
  1334. tp->rcv_nxt - tp->rcv_wup > icsk->icsk_ack.rcv_mss ||
  1335. /*
  1336. * If this read emptied read buffer, we send ACK, if
  1337. * connection is not bidirectional, user drained
  1338. * receive buffer and there was a small segment
  1339. * in queue.
  1340. */
  1341. (copied > 0 &&
  1342. ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED2) ||
  1343. ((icsk->icsk_ack.pending & ICSK_ACK_PUSHED) &&
  1344. !icsk->icsk_ack.pingpong)) &&
  1345. !atomic_read(&sk->sk_rmem_alloc)))
  1346. time_to_ack = true;
  1347. }
  1348. /* We send an ACK if we can now advertise a non-zero window
  1349. * which has been raised "significantly".
  1350. *
  1351. * Even if window raised up to infinity, do not send window open ACK
  1352. * in states, where we will not receive more. It is useless.
  1353. */
  1354. if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  1355. __u32 rcv_window_now = tcp_receive_window(tp);
  1356. /* Optimize, __tcp_select_window() is not cheap. */
  1357. if (2*rcv_window_now <= tp->window_clamp) {
  1358. __u32 new_window = __tcp_select_window(sk);
  1359. /* Send ACK now, if this read freed lots of space
  1360. * in our buffer. Certainly, new_window is new window.
  1361. * We can advertise it now, if it is not less than current one.
  1362. * "Lots" means "at least twice" here.
  1363. */
  1364. if (new_window && new_window >= 2 * rcv_window_now)
  1365. time_to_ack = true;
  1366. }
  1367. }
  1368. if (time_to_ack)
  1369. tcp_send_ack(sk);
  1370. }
  1371. static struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
  1372. {
  1373. struct sk_buff *skb;
  1374. u32 offset;
  1375. while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
  1376. offset = seq - TCP_SKB_CB(skb)->seq;
  1377. if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  1378. pr_err_once("%s: found a SYN, please report !\n", __func__);
  1379. offset--;
  1380. }
  1381. if (offset < skb->len || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) {
  1382. *off = offset;
  1383. return skb;
  1384. }
  1385. /* This looks weird, but this can happen if TCP collapsing
  1386. * splitted a fat GRO packet, while we released socket lock
  1387. * in skb_splice_bits()
  1388. */
  1389. sk_eat_skb(sk, skb);
  1390. }
  1391. return NULL;
  1392. }
  1393. /*
  1394. * This routine provides an alternative to tcp_recvmsg() for routines
  1395. * that would like to handle copying from skbuffs directly in 'sendfile'
  1396. * fashion.
  1397. * Note:
  1398. * - It is assumed that the socket was locked by the caller.
  1399. * - The routine does not block.
  1400. * - At present, there is no support for reading OOB data
  1401. * or for 'peeking' the socket using this routine
  1402. * (although both would be easy to implement).
  1403. */
  1404. int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  1405. sk_read_actor_t recv_actor)
  1406. {
  1407. struct sk_buff *skb;
  1408. struct tcp_sock *tp = tcp_sk(sk);
  1409. u32 seq = tp->copied_seq;
  1410. u32 offset;
  1411. int copied = 0;
  1412. if (sk->sk_state == TCP_LISTEN)
  1413. return -ENOTCONN;
  1414. while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
  1415. if (offset < skb->len) {
  1416. int used;
  1417. size_t len;
  1418. len = skb->len - offset;
  1419. /* Stop reading if we hit a patch of urgent data */
  1420. if (tp->urg_data) {
  1421. u32 urg_offset = tp->urg_seq - seq;
  1422. if (urg_offset < len)
  1423. len = urg_offset;
  1424. if (!len)
  1425. break;
  1426. }
  1427. used = recv_actor(desc, skb, offset, len);
  1428. if (used <= 0) {
  1429. if (!copied)
  1430. copied = used;
  1431. break;
  1432. } else if (used <= len) {
  1433. seq += used;
  1434. copied += used;
  1435. offset += used;
  1436. }
  1437. /* If recv_actor drops the lock (e.g. TCP splice
  1438. * receive) the skb pointer might be invalid when
  1439. * getting here: tcp_collapse might have deleted it
  1440. * while aggregating skbs from the socket queue.
  1441. */
  1442. skb = tcp_recv_skb(sk, seq - 1, &offset);
  1443. if (!skb)
  1444. break;
  1445. /* TCP coalescing might have appended data to the skb.
  1446. * Try to splice more frags
  1447. */
  1448. if (offset + 1 != skb->len)
  1449. continue;
  1450. }
  1451. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
  1452. sk_eat_skb(sk, skb);
  1453. ++seq;
  1454. break;
  1455. }
  1456. sk_eat_skb(sk, skb);
  1457. if (!desc->count)
  1458. break;
  1459. tp->copied_seq = seq;
  1460. }
  1461. tp->copied_seq = seq;
  1462. tcp_rcv_space_adjust(sk);
  1463. /* Clean up data we have read: This will do ACK frames. */
  1464. if (copied > 0) {
  1465. tcp_recv_skb(sk, seq, &offset);
  1466. tcp_cleanup_rbuf(sk, copied);
  1467. }
  1468. return copied;
  1469. }
  1470. EXPORT_SYMBOL(tcp_read_sock);
  1471. int tcp_peek_len(struct socket *sock)
  1472. {
  1473. return tcp_inq(sock->sk);
  1474. }
  1475. EXPORT_SYMBOL(tcp_peek_len);
  1476. /*
  1477. * This routine copies from a sock struct into the user buffer.
  1478. *
  1479. * Technical note: in 2.3 we work on _locked_ socket, so that
  1480. * tricks with *seq access order and skb->users are not required.
  1481. * Probably, code can be easily improved even more.
  1482. */
  1483. int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  1484. int flags, int *addr_len)
  1485. {
  1486. struct tcp_sock *tp = tcp_sk(sk);
  1487. int copied = 0;
  1488. u32 peek_seq;
  1489. u32 *seq;
  1490. unsigned long used;
  1491. int err;
  1492. int target; /* Read at least this many bytes */
  1493. long timeo;
  1494. struct sk_buff *skb, *last;
  1495. u32 urg_hole = 0;
  1496. if (unlikely(flags & MSG_ERRQUEUE))
  1497. return inet_recv_error(sk, msg, len, addr_len);
  1498. if (sk_can_busy_loop(sk) && skb_queue_empty(&sk->sk_receive_queue) &&
  1499. (sk->sk_state == TCP_ESTABLISHED))
  1500. sk_busy_loop(sk, nonblock);
  1501. lock_sock(sk);
  1502. err = -ENOTCONN;
  1503. if (sk->sk_state == TCP_LISTEN)
  1504. goto out;
  1505. timeo = sock_rcvtimeo(sk, nonblock);
  1506. /* Urgent data needs to be handled specially. */
  1507. if (flags & MSG_OOB)
  1508. goto recv_urg;
  1509. if (unlikely(tp->repair)) {
  1510. err = -EPERM;
  1511. if (!(flags & MSG_PEEK))
  1512. goto out;
  1513. if (tp->repair_queue == TCP_SEND_QUEUE)
  1514. goto recv_sndq;
  1515. err = -EINVAL;
  1516. if (tp->repair_queue == TCP_NO_QUEUE)
  1517. goto out;
  1518. /* 'common' recv queue MSG_PEEK-ing */
  1519. }
  1520. seq = &tp->copied_seq;
  1521. if (flags & MSG_PEEK) {
  1522. peek_seq = tp->copied_seq;
  1523. seq = &peek_seq;
  1524. }
  1525. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1526. do {
  1527. u32 offset;
  1528. /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */
  1529. if (tp->urg_data && tp->urg_seq == *seq) {
  1530. if (copied)
  1531. break;
  1532. if (signal_pending(current)) {
  1533. copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
  1534. break;
  1535. }
  1536. }
  1537. /* Next get a buffer. */
  1538. last = skb_peek_tail(&sk->sk_receive_queue);
  1539. skb_queue_walk(&sk->sk_receive_queue, skb) {
  1540. last = skb;
  1541. /* Now that we have two receive queues this
  1542. * shouldn't happen.
  1543. */
  1544. if (WARN(before(*seq, TCP_SKB_CB(skb)->seq),
  1545. "recvmsg bug: copied %X seq %X rcvnxt %X fl %X\n",
  1546. *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt,
  1547. flags))
  1548. break;
  1549. offset = *seq - TCP_SKB_CB(skb)->seq;
  1550. if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  1551. pr_err_once("%s: found a SYN, please report !\n", __func__);
  1552. offset--;
  1553. }
  1554. if (offset < skb->len)
  1555. goto found_ok_skb;
  1556. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  1557. goto found_fin_ok;
  1558. WARN(!(flags & MSG_PEEK),
  1559. "recvmsg bug 2: copied %X seq %X rcvnxt %X fl %X\n",
  1560. *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt, flags);
  1561. }
  1562. /* Well, if we have backlog, try to process it now yet. */
  1563. if (copied >= target && !sk->sk_backlog.tail)
  1564. break;
  1565. if (copied) {
  1566. if (sk->sk_err ||
  1567. sk->sk_state == TCP_CLOSE ||
  1568. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1569. !timeo ||
  1570. signal_pending(current))
  1571. break;
  1572. } else {
  1573. if (sock_flag(sk, SOCK_DONE))
  1574. break;
  1575. if (sk->sk_err) {
  1576. copied = sock_error(sk);
  1577. break;
  1578. }
  1579. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1580. break;
  1581. if (sk->sk_state == TCP_CLOSE) {
  1582. if (!sock_flag(sk, SOCK_DONE)) {
  1583. /* This occurs when user tries to read
  1584. * from never connected socket.
  1585. */
  1586. copied = -ENOTCONN;
  1587. break;
  1588. }
  1589. break;
  1590. }
  1591. if (!timeo) {
  1592. copied = -EAGAIN;
  1593. break;
  1594. }
  1595. if (signal_pending(current)) {
  1596. copied = sock_intr_errno(timeo);
  1597. break;
  1598. }
  1599. }
  1600. tcp_cleanup_rbuf(sk, copied);
  1601. if (copied >= target) {
  1602. /* Do not sleep, just process backlog. */
  1603. release_sock(sk);
  1604. lock_sock(sk);
  1605. } else {
  1606. sk_wait_data(sk, &timeo, last);
  1607. }
  1608. if ((flags & MSG_PEEK) &&
  1609. (peek_seq - copied - urg_hole != tp->copied_seq)) {
  1610. net_dbg_ratelimited("TCP(%s:%d): Application bug, race in MSG_PEEK\n",
  1611. current->comm,
  1612. task_pid_nr(current));
  1613. peek_seq = tp->copied_seq;
  1614. }
  1615. continue;
  1616. found_ok_skb:
  1617. /* Ok so how much can we use? */
  1618. used = skb->len - offset;
  1619. if (len < used)
  1620. used = len;
  1621. /* Do we have urgent data here? */
  1622. if (tp->urg_data) {
  1623. u32 urg_offset = tp->urg_seq - *seq;
  1624. if (urg_offset < used) {
  1625. if (!urg_offset) {
  1626. if (!sock_flag(sk, SOCK_URGINLINE)) {
  1627. ++*seq;
  1628. urg_hole++;
  1629. offset++;
  1630. used--;
  1631. if (!used)
  1632. goto skip_copy;
  1633. }
  1634. } else
  1635. used = urg_offset;
  1636. }
  1637. }
  1638. if (!(flags & MSG_TRUNC)) {
  1639. err = skb_copy_datagram_msg(skb, offset, msg, used);
  1640. if (err) {
  1641. /* Exception. Bailout! */
  1642. if (!copied)
  1643. copied = -EFAULT;
  1644. break;
  1645. }
  1646. }
  1647. *seq += used;
  1648. copied += used;
  1649. len -= used;
  1650. tcp_rcv_space_adjust(sk);
  1651. skip_copy:
  1652. if (tp->urg_data && after(tp->copied_seq, tp->urg_seq))
  1653. tp->urg_data = 0;
  1654. if (used + offset < skb->len)
  1655. continue;
  1656. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  1657. goto found_fin_ok;
  1658. if (!(flags & MSG_PEEK))
  1659. sk_eat_skb(sk, skb);
  1660. continue;
  1661. found_fin_ok:
  1662. /* Process the FIN. */
  1663. ++*seq;
  1664. if (!(flags & MSG_PEEK))
  1665. sk_eat_skb(sk, skb);
  1666. break;
  1667. } while (len > 0);
  1668. /* According to UNIX98, msg_name/msg_namelen are ignored
  1669. * on connected socket. I was just happy when found this 8) --ANK
  1670. */
  1671. /* Clean up data we have read: This will do ACK frames. */
  1672. tcp_cleanup_rbuf(sk, copied);
  1673. release_sock(sk);
  1674. return copied;
  1675. out:
  1676. release_sock(sk);
  1677. return err;
  1678. recv_urg:
  1679. err = tcp_recv_urg(sk, msg, len, flags);
  1680. goto out;
  1681. recv_sndq:
  1682. err = tcp_peek_sndq(sk, msg, len);
  1683. goto out;
  1684. }
  1685. EXPORT_SYMBOL(tcp_recvmsg);
  1686. void tcp_set_state(struct sock *sk, int state)
  1687. {
  1688. int oldstate = sk->sk_state;
  1689. switch (state) {
  1690. case TCP_ESTABLISHED:
  1691. if (oldstate != TCP_ESTABLISHED)
  1692. TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
  1693. break;
  1694. case TCP_CLOSE:
  1695. if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
  1696. TCP_INC_STATS(sock_net(sk), TCP_MIB_ESTABRESETS);
  1697. sk->sk_prot->unhash(sk);
  1698. if (inet_csk(sk)->icsk_bind_hash &&
  1699. !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
  1700. inet_put_port(sk);
  1701. /* fall through */
  1702. default:
  1703. if (oldstate == TCP_ESTABLISHED)
  1704. TCP_DEC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
  1705. }
  1706. /* Change state AFTER socket is unhashed to avoid closed
  1707. * socket sitting in hash tables.
  1708. */
  1709. sk_state_store(sk, state);
  1710. #ifdef STATE_TRACE
  1711. SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n", sk, statename[oldstate], statename[state]);
  1712. #endif
  1713. }
  1714. EXPORT_SYMBOL_GPL(tcp_set_state);
  1715. /*
  1716. * State processing on a close. This implements the state shift for
  1717. * sending our FIN frame. Note that we only send a FIN for some
  1718. * states. A shutdown() may have already sent the FIN, or we may be
  1719. * closed.
  1720. */
  1721. static const unsigned char new_state[16] = {
  1722. /* current state: new state: action: */
  1723. [0 /* (Invalid) */] = TCP_CLOSE,
  1724. [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  1725. [TCP_SYN_SENT] = TCP_CLOSE,
  1726. [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  1727. [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
  1728. [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
  1729. [TCP_TIME_WAIT] = TCP_CLOSE,
  1730. [TCP_CLOSE] = TCP_CLOSE,
  1731. [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
  1732. [TCP_LAST_ACK] = TCP_LAST_ACK,
  1733. [TCP_LISTEN] = TCP_CLOSE,
  1734. [TCP_CLOSING] = TCP_CLOSING,
  1735. [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
  1736. };
  1737. static int tcp_close_state(struct sock *sk)
  1738. {
  1739. int next = (int)new_state[sk->sk_state];
  1740. int ns = next & TCP_STATE_MASK;
  1741. tcp_set_state(sk, ns);
  1742. return next & TCP_ACTION_FIN;
  1743. }
  1744. /*
  1745. * Shutdown the sending side of a connection. Much like close except
  1746. * that we don't receive shut down or sock_set_flag(sk, SOCK_DEAD).
  1747. */
  1748. void tcp_shutdown(struct sock *sk, int how)
  1749. {
  1750. /* We need to grab some memory, and put together a FIN,
  1751. * and then put it into the queue to be sent.
  1752. * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
  1753. */
  1754. if (!(how & SEND_SHUTDOWN))
  1755. return;
  1756. /* If we've already sent a FIN, or it's a closed state, skip this. */
  1757. if ((1 << sk->sk_state) &
  1758. (TCPF_ESTABLISHED | TCPF_SYN_SENT |
  1759. TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
  1760. /* Clear out any half completed packets. FIN if needed. */
  1761. if (tcp_close_state(sk))
  1762. tcp_send_fin(sk);
  1763. }
  1764. }
  1765. EXPORT_SYMBOL(tcp_shutdown);
  1766. bool tcp_check_oom(struct sock *sk, int shift)
  1767. {
  1768. bool too_many_orphans, out_of_socket_memory;
  1769. too_many_orphans = tcp_too_many_orphans(sk, shift);
  1770. out_of_socket_memory = tcp_out_of_memory(sk);
  1771. if (too_many_orphans)
  1772. net_info_ratelimited("too many orphaned sockets\n");
  1773. if (out_of_socket_memory)
  1774. net_info_ratelimited("out of memory -- consider tuning tcp_mem\n");
  1775. return too_many_orphans || out_of_socket_memory;
  1776. }
  1777. void tcp_close(struct sock *sk, long timeout)
  1778. {
  1779. struct sk_buff *skb;
  1780. int data_was_unread = 0;
  1781. int state;
  1782. lock_sock(sk);
  1783. sk->sk_shutdown = SHUTDOWN_MASK;
  1784. if (sk->sk_state == TCP_LISTEN) {
  1785. tcp_set_state(sk, TCP_CLOSE);
  1786. /* Special case. */
  1787. inet_csk_listen_stop(sk);
  1788. goto adjudge_to_death;
  1789. }
  1790. /* We need to flush the recv. buffs. We do this only on the
  1791. * descriptor close, not protocol-sourced closes, because the
  1792. * reader process may not have drained the data yet!
  1793. */
  1794. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  1795. u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq;
  1796. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  1797. len--;
  1798. data_was_unread += len;
  1799. __kfree_skb(skb);
  1800. }
  1801. sk_mem_reclaim(sk);
  1802. /* If socket has been already reset (e.g. in tcp_reset()) - kill it. */
  1803. if (sk->sk_state == TCP_CLOSE)
  1804. goto adjudge_to_death;
  1805. /* As outlined in RFC 2525, section 2.17, we send a RST here because
  1806. * data was lost. To witness the awful effects of the old behavior of
  1807. * always doing a FIN, run an older 2.1.x kernel or 2.0.x, start a bulk
  1808. * GET in an FTP client, suspend the process, wait for the client to
  1809. * advertise a zero window, then kill -9 the FTP client, wheee...
  1810. * Note: timeout is always zero in such a case.
  1811. */
  1812. if (unlikely(tcp_sk(sk)->repair)) {
  1813. sk->sk_prot->disconnect(sk, 0);
  1814. } else if (data_was_unread) {
  1815. /* Unread data was tossed, zap the connection. */
  1816. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONCLOSE);
  1817. tcp_set_state(sk, TCP_CLOSE);
  1818. tcp_send_active_reset(sk, sk->sk_allocation);
  1819. } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  1820. /* Check zero linger _after_ checking for unread data. */
  1821. sk->sk_prot->disconnect(sk, 0);
  1822. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
  1823. } else if (tcp_close_state(sk)) {
  1824. /* We FIN if the application ate all the data before
  1825. * zapping the connection.
  1826. */
  1827. /* RED-PEN. Formally speaking, we have broken TCP state
  1828. * machine. State transitions:
  1829. *
  1830. * TCP_ESTABLISHED -> TCP_FIN_WAIT1
  1831. * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
  1832. * TCP_CLOSE_WAIT -> TCP_LAST_ACK
  1833. *
  1834. * are legal only when FIN has been sent (i.e. in window),
  1835. * rather than queued out of window. Purists blame.
  1836. *
  1837. * F.e. "RFC state" is ESTABLISHED,
  1838. * if Linux state is FIN-WAIT-1, but FIN is still not sent.
  1839. *
  1840. * The visible declinations are that sometimes
  1841. * we enter time-wait state, when it is not required really
  1842. * (harmless), do not send active resets, when they are
  1843. * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
  1844. * they look as CLOSING or LAST_ACK for Linux)
  1845. * Probably, I missed some more holelets.
  1846. * --ANK
  1847. * XXX (TFO) - To start off we don't support SYN+ACK+FIN
  1848. * in a single packet! (May consider it later but will
  1849. * probably need API support or TCP_CORK SYN-ACK until
  1850. * data is written and socket is closed.)
  1851. */
  1852. tcp_send_fin(sk);
  1853. }
  1854. sk_stream_wait_close(sk, timeout);
  1855. adjudge_to_death:
  1856. state = sk->sk_state;
  1857. sock_hold(sk);
  1858. sock_orphan(sk);
  1859. /* It is the last release_sock in its life. It will remove backlog. */
  1860. release_sock(sk);
  1861. /* Now socket is owned by kernel and we acquire BH lock
  1862. * to finish close. No need to check for user refs.
  1863. */
  1864. local_bh_disable();
  1865. bh_lock_sock(sk);
  1866. WARN_ON(sock_owned_by_user(sk));
  1867. percpu_counter_inc(sk->sk_prot->orphan_count);
  1868. /* Have we already been destroyed by a softirq or backlog? */
  1869. if (state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
  1870. goto out;
  1871. /* This is a (useful) BSD violating of the RFC. There is a
  1872. * problem with TCP as specified in that the other end could
  1873. * keep a socket open forever with no application left this end.
  1874. * We use a 1 minute timeout (about the same as BSD) then kill
  1875. * our end. If they send after that then tough - BUT: long enough
  1876. * that we won't make the old 4*rto = almost no time - whoops
  1877. * reset mistake.
  1878. *
  1879. * Nope, it was not mistake. It is really desired behaviour
  1880. * f.e. on http servers, when such sockets are useless, but
  1881. * consume significant resources. Let's do it with special
  1882. * linger2 option. --ANK
  1883. */
  1884. if (sk->sk_state == TCP_FIN_WAIT2) {
  1885. struct tcp_sock *tp = tcp_sk(sk);
  1886. if (tp->linger2 < 0) {
  1887. tcp_set_state(sk, TCP_CLOSE);
  1888. tcp_send_active_reset(sk, GFP_ATOMIC);
  1889. __NET_INC_STATS(sock_net(sk),
  1890. LINUX_MIB_TCPABORTONLINGER);
  1891. } else {
  1892. const int tmo = tcp_fin_time(sk);
  1893. if (tmo > TCP_TIMEWAIT_LEN) {
  1894. inet_csk_reset_keepalive_timer(sk,
  1895. tmo - TCP_TIMEWAIT_LEN);
  1896. } else {
  1897. tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
  1898. goto out;
  1899. }
  1900. }
  1901. }
  1902. if (sk->sk_state != TCP_CLOSE) {
  1903. sk_mem_reclaim(sk);
  1904. if (tcp_check_oom(sk, 0)) {
  1905. tcp_set_state(sk, TCP_CLOSE);
  1906. tcp_send_active_reset(sk, GFP_ATOMIC);
  1907. __NET_INC_STATS(sock_net(sk),
  1908. LINUX_MIB_TCPABORTONMEMORY);
  1909. }
  1910. }
  1911. if (sk->sk_state == TCP_CLOSE) {
  1912. struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
  1913. /* We could get here with a non-NULL req if the socket is
  1914. * aborted (e.g., closed with unread data) before 3WHS
  1915. * finishes.
  1916. */
  1917. if (req)
  1918. reqsk_fastopen_remove(sk, req, false);
  1919. inet_csk_destroy_sock(sk);
  1920. }
  1921. /* Otherwise, socket is reprieved until protocol close. */
  1922. out:
  1923. bh_unlock_sock(sk);
  1924. local_bh_enable();
  1925. sock_put(sk);
  1926. }
  1927. EXPORT_SYMBOL(tcp_close);
  1928. /* These states need RST on ABORT according to RFC793 */
  1929. static inline bool tcp_need_reset(int state)
  1930. {
  1931. return (1 << state) &
  1932. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 |
  1933. TCPF_FIN_WAIT2 | TCPF_SYN_RECV);
  1934. }
  1935. int tcp_disconnect(struct sock *sk, int flags)
  1936. {
  1937. struct inet_sock *inet = inet_sk(sk);
  1938. struct inet_connection_sock *icsk = inet_csk(sk);
  1939. struct tcp_sock *tp = tcp_sk(sk);
  1940. int err = 0;
  1941. int old_state = sk->sk_state;
  1942. if (old_state != TCP_CLOSE)
  1943. tcp_set_state(sk, TCP_CLOSE);
  1944. /* ABORT function of RFC793 */
  1945. if (old_state == TCP_LISTEN) {
  1946. inet_csk_listen_stop(sk);
  1947. } else if (unlikely(tp->repair)) {
  1948. sk->sk_err = ECONNABORTED;
  1949. } else if (tcp_need_reset(old_state) ||
  1950. (tp->snd_nxt != tp->write_seq &&
  1951. (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) {
  1952. /* The last check adjusts for discrepancy of Linux wrt. RFC
  1953. * states
  1954. */
  1955. tcp_send_active_reset(sk, gfp_any());
  1956. sk->sk_err = ECONNRESET;
  1957. } else if (old_state == TCP_SYN_SENT)
  1958. sk->sk_err = ECONNRESET;
  1959. tcp_clear_xmit_timers(sk);
  1960. __skb_queue_purge(&sk->sk_receive_queue);
  1961. tcp_write_queue_purge(sk);
  1962. tcp_fastopen_active_disable_ofo_check(sk);
  1963. skb_rbtree_purge(&tp->out_of_order_queue);
  1964. inet->inet_dport = 0;
  1965. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  1966. inet_reset_saddr(sk);
  1967. sk->sk_shutdown = 0;
  1968. sock_reset_flag(sk, SOCK_DONE);
  1969. tp->srtt_us = 0;
  1970. tp->write_seq += tp->max_window + 2;
  1971. if (tp->write_seq == 0)
  1972. tp->write_seq = 1;
  1973. icsk->icsk_backoff = 0;
  1974. tp->snd_cwnd = 2;
  1975. icsk->icsk_probes_out = 0;
  1976. tp->packets_out = 0;
  1977. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  1978. tp->snd_cwnd_cnt = 0;
  1979. tp->window_clamp = 0;
  1980. tcp_set_ca_state(sk, TCP_CA_Open);
  1981. tcp_clear_retrans(tp);
  1982. inet_csk_delack_init(sk);
  1983. /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
  1984. * issue in __tcp_select_window()
  1985. */
  1986. icsk->icsk_ack.rcv_mss = TCP_MIN_MSS;
  1987. tcp_init_send_head(sk);
  1988. memset(&tp->rx_opt, 0, sizeof(tp->rx_opt));
  1989. __sk_dst_reset(sk);
  1990. dst_release(sk->sk_rx_dst);
  1991. sk->sk_rx_dst = NULL;
  1992. tcp_saved_syn_free(tp);
  1993. /* Clean up fastopen related fields */
  1994. tcp_free_fastopen_req(tp);
  1995. inet->defer_connect = 0;
  1996. WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
  1997. sk->sk_error_report(sk);
  1998. return err;
  1999. }
  2000. EXPORT_SYMBOL(tcp_disconnect);
  2001. static inline bool tcp_can_repair_sock(const struct sock *sk)
  2002. {
  2003. return ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN) &&
  2004. (sk->sk_state != TCP_LISTEN);
  2005. }
  2006. static int tcp_repair_set_window(struct tcp_sock *tp, char __user *optbuf, int len)
  2007. {
  2008. struct tcp_repair_window opt;
  2009. if (!tp->repair)
  2010. return -EPERM;
  2011. if (len != sizeof(opt))
  2012. return -EINVAL;
  2013. if (copy_from_user(&opt, optbuf, sizeof(opt)))
  2014. return -EFAULT;
  2015. if (opt.max_window < opt.snd_wnd)
  2016. return -EINVAL;
  2017. if (after(opt.snd_wl1, tp->rcv_nxt + opt.rcv_wnd))
  2018. return -EINVAL;
  2019. if (after(opt.rcv_wup, tp->rcv_nxt))
  2020. return -EINVAL;
  2021. tp->snd_wl1 = opt.snd_wl1;
  2022. tp->snd_wnd = opt.snd_wnd;
  2023. tp->max_window = opt.max_window;
  2024. tp->rcv_wnd = opt.rcv_wnd;
  2025. tp->rcv_wup = opt.rcv_wup;
  2026. return 0;
  2027. }
  2028. static int tcp_repair_options_est(struct sock *sk,
  2029. struct tcp_repair_opt __user *optbuf, unsigned int len)
  2030. {
  2031. struct tcp_sock *tp = tcp_sk(sk);
  2032. struct tcp_repair_opt opt;
  2033. while (len >= sizeof(opt)) {
  2034. if (copy_from_user(&opt, optbuf, sizeof(opt)))
  2035. return -EFAULT;
  2036. optbuf++;
  2037. len -= sizeof(opt);
  2038. switch (opt.opt_code) {
  2039. case TCPOPT_MSS:
  2040. tp->rx_opt.mss_clamp = opt.opt_val;
  2041. tcp_mtup_init(sk);
  2042. break;
  2043. case TCPOPT_WINDOW:
  2044. {
  2045. u16 snd_wscale = opt.opt_val & 0xFFFF;
  2046. u16 rcv_wscale = opt.opt_val >> 16;
  2047. if (snd_wscale > TCP_MAX_WSCALE || rcv_wscale > TCP_MAX_WSCALE)
  2048. return -EFBIG;
  2049. tp->rx_opt.snd_wscale = snd_wscale;
  2050. tp->rx_opt.rcv_wscale = rcv_wscale;
  2051. tp->rx_opt.wscale_ok = 1;
  2052. }
  2053. break;
  2054. case TCPOPT_SACK_PERM:
  2055. if (opt.opt_val != 0)
  2056. return -EINVAL;
  2057. tp->rx_opt.sack_ok |= TCP_SACK_SEEN;
  2058. if (sysctl_tcp_fack)
  2059. tcp_enable_fack(tp);
  2060. break;
  2061. case TCPOPT_TIMESTAMP:
  2062. if (opt.opt_val != 0)
  2063. return -EINVAL;
  2064. tp->rx_opt.tstamp_ok = 1;
  2065. break;
  2066. }
  2067. }
  2068. return 0;
  2069. }
  2070. /*
  2071. * Socket option code for TCP.
  2072. */
  2073. static int do_tcp_setsockopt(struct sock *sk, int level,
  2074. int optname, char __user *optval, unsigned int optlen)
  2075. {
  2076. struct tcp_sock *tp = tcp_sk(sk);
  2077. struct inet_connection_sock *icsk = inet_csk(sk);
  2078. struct net *net = sock_net(sk);
  2079. int val;
  2080. int err = 0;
  2081. /* These are data/string values, all the others are ints */
  2082. switch (optname) {
  2083. case TCP_CONGESTION: {
  2084. char name[TCP_CA_NAME_MAX];
  2085. if (optlen < 1)
  2086. return -EINVAL;
  2087. val = strncpy_from_user(name, optval,
  2088. min_t(long, TCP_CA_NAME_MAX-1, optlen));
  2089. if (val < 0)
  2090. return -EFAULT;
  2091. name[val] = 0;
  2092. lock_sock(sk);
  2093. err = tcp_set_congestion_control(sk, name, true);
  2094. release_sock(sk);
  2095. return err;
  2096. }
  2097. case TCP_ULP: {
  2098. char name[TCP_ULP_NAME_MAX];
  2099. if (optlen < 1)
  2100. return -EINVAL;
  2101. val = strncpy_from_user(name, optval,
  2102. min_t(long, TCP_ULP_NAME_MAX - 1,
  2103. optlen));
  2104. if (val < 0)
  2105. return -EFAULT;
  2106. name[val] = 0;
  2107. lock_sock(sk);
  2108. err = tcp_set_ulp(sk, name);
  2109. release_sock(sk);
  2110. return err;
  2111. }
  2112. default:
  2113. /* fallthru */
  2114. break;
  2115. }
  2116. if (optlen < sizeof(int))
  2117. return -EINVAL;
  2118. if (get_user(val, (int __user *)optval))
  2119. return -EFAULT;
  2120. lock_sock(sk);
  2121. switch (optname) {
  2122. case TCP_MAXSEG:
  2123. /* Values greater than interface MTU won't take effect. However
  2124. * at the point when this call is done we typically don't yet
  2125. * know which interface is going to be used
  2126. */
  2127. if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW)) {
  2128. err = -EINVAL;
  2129. break;
  2130. }
  2131. tp->rx_opt.user_mss = val;
  2132. break;
  2133. case TCP_NODELAY:
  2134. if (val) {
  2135. /* TCP_NODELAY is weaker than TCP_CORK, so that
  2136. * this option on corked socket is remembered, but
  2137. * it is not activated until cork is cleared.
  2138. *
  2139. * However, when TCP_NODELAY is set we make
  2140. * an explicit push, which overrides even TCP_CORK
  2141. * for currently queued segments.
  2142. */
  2143. tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
  2144. tcp_push_pending_frames(sk);
  2145. } else {
  2146. tp->nonagle &= ~TCP_NAGLE_OFF;
  2147. }
  2148. break;
  2149. case TCP_THIN_LINEAR_TIMEOUTS:
  2150. if (val < 0 || val > 1)
  2151. err = -EINVAL;
  2152. else
  2153. tp->thin_lto = val;
  2154. break;
  2155. case TCP_THIN_DUPACK:
  2156. if (val < 0 || val > 1)
  2157. err = -EINVAL;
  2158. break;
  2159. case TCP_REPAIR:
  2160. if (!tcp_can_repair_sock(sk))
  2161. err = -EPERM;
  2162. else if (val == 1) {
  2163. tp->repair = 1;
  2164. sk->sk_reuse = SK_FORCE_REUSE;
  2165. tp->repair_queue = TCP_NO_QUEUE;
  2166. } else if (val == 0) {
  2167. tp->repair = 0;
  2168. sk->sk_reuse = SK_NO_REUSE;
  2169. tcp_send_window_probe(sk);
  2170. } else
  2171. err = -EINVAL;
  2172. break;
  2173. case TCP_REPAIR_QUEUE:
  2174. if (!tp->repair)
  2175. err = -EPERM;
  2176. else if (val < TCP_QUEUES_NR)
  2177. tp->repair_queue = val;
  2178. else
  2179. err = -EINVAL;
  2180. break;
  2181. case TCP_QUEUE_SEQ:
  2182. if (sk->sk_state != TCP_CLOSE)
  2183. err = -EPERM;
  2184. else if (tp->repair_queue == TCP_SEND_QUEUE)
  2185. tp->write_seq = val;
  2186. else if (tp->repair_queue == TCP_RECV_QUEUE)
  2187. tp->rcv_nxt = val;
  2188. else
  2189. err = -EINVAL;
  2190. break;
  2191. case TCP_REPAIR_OPTIONS:
  2192. if (!tp->repair)
  2193. err = -EINVAL;
  2194. else if (sk->sk_state == TCP_ESTABLISHED)
  2195. err = tcp_repair_options_est(sk,
  2196. (struct tcp_repair_opt __user *)optval,
  2197. optlen);
  2198. else
  2199. err = -EPERM;
  2200. break;
  2201. case TCP_CORK:
  2202. /* When set indicates to always queue non-full frames.
  2203. * Later the user clears this option and we transmit
  2204. * any pending partial frames in the queue. This is
  2205. * meant to be used alongside sendfile() to get properly
  2206. * filled frames when the user (for example) must write
  2207. * out headers with a write() call first and then use
  2208. * sendfile to send out the data parts.
  2209. *
  2210. * TCP_CORK can be set together with TCP_NODELAY and it is
  2211. * stronger than TCP_NODELAY.
  2212. */
  2213. if (val) {
  2214. tp->nonagle |= TCP_NAGLE_CORK;
  2215. } else {
  2216. tp->nonagle &= ~TCP_NAGLE_CORK;
  2217. if (tp->nonagle&TCP_NAGLE_OFF)
  2218. tp->nonagle |= TCP_NAGLE_PUSH;
  2219. tcp_push_pending_frames(sk);
  2220. }
  2221. break;
  2222. case TCP_KEEPIDLE:
  2223. if (val < 1 || val > MAX_TCP_KEEPIDLE)
  2224. err = -EINVAL;
  2225. else {
  2226. tp->keepalive_time = val * HZ;
  2227. if (sock_flag(sk, SOCK_KEEPOPEN) &&
  2228. !((1 << sk->sk_state) &
  2229. (TCPF_CLOSE | TCPF_LISTEN))) {
  2230. u32 elapsed = keepalive_time_elapsed(tp);
  2231. if (tp->keepalive_time > elapsed)
  2232. elapsed = tp->keepalive_time - elapsed;
  2233. else
  2234. elapsed = 0;
  2235. inet_csk_reset_keepalive_timer(sk, elapsed);
  2236. }
  2237. }
  2238. break;
  2239. case TCP_KEEPINTVL:
  2240. if (val < 1 || val > MAX_TCP_KEEPINTVL)
  2241. err = -EINVAL;
  2242. else
  2243. tp->keepalive_intvl = val * HZ;
  2244. break;
  2245. case TCP_KEEPCNT:
  2246. if (val < 1 || val > MAX_TCP_KEEPCNT)
  2247. err = -EINVAL;
  2248. else
  2249. tp->keepalive_probes = val;
  2250. break;
  2251. case TCP_SYNCNT:
  2252. if (val < 1 || val > MAX_TCP_SYNCNT)
  2253. err = -EINVAL;
  2254. else
  2255. icsk->icsk_syn_retries = val;
  2256. break;
  2257. case TCP_SAVE_SYN:
  2258. if (val < 0 || val > 1)
  2259. err = -EINVAL;
  2260. else
  2261. tp->save_syn = val;
  2262. break;
  2263. case TCP_LINGER2:
  2264. if (val < 0)
  2265. tp->linger2 = -1;
  2266. else if (val > net->ipv4.sysctl_tcp_fin_timeout / HZ)
  2267. tp->linger2 = 0;
  2268. else
  2269. tp->linger2 = val * HZ;
  2270. break;
  2271. case TCP_DEFER_ACCEPT:
  2272. /* Translate value in seconds to number of retransmits */
  2273. icsk->icsk_accept_queue.rskq_defer_accept =
  2274. secs_to_retrans(val, TCP_TIMEOUT_INIT / HZ,
  2275. TCP_RTO_MAX / HZ);
  2276. break;
  2277. case TCP_WINDOW_CLAMP:
  2278. if (!val) {
  2279. if (sk->sk_state != TCP_CLOSE) {
  2280. err = -EINVAL;
  2281. break;
  2282. }
  2283. tp->window_clamp = 0;
  2284. } else
  2285. tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
  2286. SOCK_MIN_RCVBUF / 2 : val;
  2287. break;
  2288. case TCP_QUICKACK:
  2289. if (!val) {
  2290. icsk->icsk_ack.pingpong = 1;
  2291. } else {
  2292. icsk->icsk_ack.pingpong = 0;
  2293. if ((1 << sk->sk_state) &
  2294. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
  2295. inet_csk_ack_scheduled(sk)) {
  2296. icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
  2297. tcp_cleanup_rbuf(sk, 1);
  2298. if (!(val & 1))
  2299. icsk->icsk_ack.pingpong = 1;
  2300. }
  2301. }
  2302. break;
  2303. #ifdef CONFIG_TCP_MD5SIG
  2304. case TCP_MD5SIG:
  2305. case TCP_MD5SIG_EXT:
  2306. /* Read the IP->Key mappings from userspace */
  2307. err = tp->af_specific->md5_parse(sk, optname, optval, optlen);
  2308. break;
  2309. #endif
  2310. case TCP_USER_TIMEOUT:
  2311. /* Cap the max time in ms TCP will retry or probe the window
  2312. * before giving up and aborting (ETIMEDOUT) a connection.
  2313. */
  2314. if (val < 0)
  2315. err = -EINVAL;
  2316. else
  2317. icsk->icsk_user_timeout = msecs_to_jiffies(val);
  2318. break;
  2319. case TCP_FASTOPEN:
  2320. if (val >= 0 && ((1 << sk->sk_state) & (TCPF_CLOSE |
  2321. TCPF_LISTEN))) {
  2322. tcp_fastopen_init_key_once(true);
  2323. fastopen_queue_tune(sk, val);
  2324. } else {
  2325. err = -EINVAL;
  2326. }
  2327. break;
  2328. case TCP_FASTOPEN_CONNECT:
  2329. if (val > 1 || val < 0) {
  2330. err = -EINVAL;
  2331. } else if (sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) {
  2332. if (sk->sk_state == TCP_CLOSE)
  2333. tp->fastopen_connect = val;
  2334. else
  2335. err = -EINVAL;
  2336. } else {
  2337. err = -EOPNOTSUPP;
  2338. }
  2339. break;
  2340. case TCP_TIMESTAMP:
  2341. if (!tp->repair)
  2342. err = -EPERM;
  2343. else
  2344. tp->tsoffset = val - tcp_time_stamp_raw();
  2345. break;
  2346. case TCP_REPAIR_WINDOW:
  2347. err = tcp_repair_set_window(tp, optval, optlen);
  2348. break;
  2349. case TCP_NOTSENT_LOWAT:
  2350. tp->notsent_lowat = val;
  2351. sk->sk_write_space(sk);
  2352. break;
  2353. default:
  2354. err = -ENOPROTOOPT;
  2355. break;
  2356. }
  2357. release_sock(sk);
  2358. return err;
  2359. }
  2360. int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
  2361. unsigned int optlen)
  2362. {
  2363. const struct inet_connection_sock *icsk = inet_csk(sk);
  2364. if (level != SOL_TCP)
  2365. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  2366. optval, optlen);
  2367. return do_tcp_setsockopt(sk, level, optname, optval, optlen);
  2368. }
  2369. EXPORT_SYMBOL(tcp_setsockopt);
  2370. #ifdef CONFIG_COMPAT
  2371. int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
  2372. char __user *optval, unsigned int optlen)
  2373. {
  2374. if (level != SOL_TCP)
  2375. return inet_csk_compat_setsockopt(sk, level, optname,
  2376. optval, optlen);
  2377. return do_tcp_setsockopt(sk, level, optname, optval, optlen);
  2378. }
  2379. EXPORT_SYMBOL(compat_tcp_setsockopt);
  2380. #endif
  2381. static void tcp_get_info_chrono_stats(const struct tcp_sock *tp,
  2382. struct tcp_info *info)
  2383. {
  2384. u64 stats[__TCP_CHRONO_MAX], total = 0;
  2385. enum tcp_chrono i;
  2386. for (i = TCP_CHRONO_BUSY; i < __TCP_CHRONO_MAX; ++i) {
  2387. stats[i] = tp->chrono_stat[i - 1];
  2388. if (i == tp->chrono_type)
  2389. stats[i] += tcp_jiffies32 - tp->chrono_start;
  2390. stats[i] *= USEC_PER_SEC / HZ;
  2391. total += stats[i];
  2392. }
  2393. info->tcpi_busy_time = total;
  2394. info->tcpi_rwnd_limited = stats[TCP_CHRONO_RWND_LIMITED];
  2395. info->tcpi_sndbuf_limited = stats[TCP_CHRONO_SNDBUF_LIMITED];
  2396. }
  2397. /* Return information about state of tcp endpoint in API format. */
  2398. void tcp_get_info(struct sock *sk, struct tcp_info *info)
  2399. {
  2400. const struct tcp_sock *tp = tcp_sk(sk); /* iff sk_type == SOCK_STREAM */
  2401. const struct inet_connection_sock *icsk = inet_csk(sk);
  2402. u32 now;
  2403. u64 rate64;
  2404. bool slow;
  2405. u32 rate;
  2406. memset(info, 0, sizeof(*info));
  2407. if (sk->sk_type != SOCK_STREAM)
  2408. return;
  2409. info->tcpi_state = sk_state_load(sk);
  2410. /* Report meaningful fields for all TCP states, including listeners */
  2411. rate = READ_ONCE(sk->sk_pacing_rate);
  2412. rate64 = rate != ~0U ? rate : ~0ULL;
  2413. info->tcpi_pacing_rate = rate64;
  2414. rate = READ_ONCE(sk->sk_max_pacing_rate);
  2415. rate64 = rate != ~0U ? rate : ~0ULL;
  2416. info->tcpi_max_pacing_rate = rate64;
  2417. info->tcpi_reordering = tp->reordering;
  2418. info->tcpi_snd_cwnd = tp->snd_cwnd;
  2419. if (info->tcpi_state == TCP_LISTEN) {
  2420. /* listeners aliased fields :
  2421. * tcpi_unacked -> Number of children ready for accept()
  2422. * tcpi_sacked -> max backlog
  2423. */
  2424. info->tcpi_unacked = sk->sk_ack_backlog;
  2425. info->tcpi_sacked = sk->sk_max_ack_backlog;
  2426. return;
  2427. }
  2428. slow = lock_sock_fast(sk);
  2429. info->tcpi_ca_state = icsk->icsk_ca_state;
  2430. info->tcpi_retransmits = icsk->icsk_retransmits;
  2431. info->tcpi_probes = icsk->icsk_probes_out;
  2432. info->tcpi_backoff = icsk->icsk_backoff;
  2433. if (tp->rx_opt.tstamp_ok)
  2434. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  2435. if (tcp_is_sack(tp))
  2436. info->tcpi_options |= TCPI_OPT_SACK;
  2437. if (tp->rx_opt.wscale_ok) {
  2438. info->tcpi_options |= TCPI_OPT_WSCALE;
  2439. info->tcpi_snd_wscale = tp->rx_opt.snd_wscale;
  2440. info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale;
  2441. }
  2442. if (tp->ecn_flags & TCP_ECN_OK)
  2443. info->tcpi_options |= TCPI_OPT_ECN;
  2444. if (tp->ecn_flags & TCP_ECN_SEEN)
  2445. info->tcpi_options |= TCPI_OPT_ECN_SEEN;
  2446. if (tp->syn_data_acked)
  2447. info->tcpi_options |= TCPI_OPT_SYN_DATA;
  2448. info->tcpi_rto = jiffies_to_usecs(icsk->icsk_rto);
  2449. info->tcpi_ato = jiffies_to_usecs(icsk->icsk_ack.ato);
  2450. info->tcpi_snd_mss = tp->mss_cache;
  2451. info->tcpi_rcv_mss = icsk->icsk_ack.rcv_mss;
  2452. info->tcpi_unacked = tp->packets_out;
  2453. info->tcpi_sacked = tp->sacked_out;
  2454. info->tcpi_lost = tp->lost_out;
  2455. info->tcpi_retrans = tp->retrans_out;
  2456. info->tcpi_fackets = tp->fackets_out;
  2457. now = tcp_jiffies32;
  2458. info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime);
  2459. info->tcpi_last_data_recv = jiffies_to_msecs(now - icsk->icsk_ack.lrcvtime);
  2460. info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp);
  2461. info->tcpi_pmtu = icsk->icsk_pmtu_cookie;
  2462. info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
  2463. info->tcpi_rtt = tp->srtt_us >> 3;
  2464. info->tcpi_rttvar = tp->mdev_us >> 2;
  2465. info->tcpi_snd_ssthresh = tp->snd_ssthresh;
  2466. info->tcpi_advmss = tp->advmss;
  2467. info->tcpi_rcv_rtt = tp->rcv_rtt_est.rtt_us >> 3;
  2468. info->tcpi_rcv_space = tp->rcvq_space.space;
  2469. info->tcpi_total_retrans = tp->total_retrans;
  2470. info->tcpi_bytes_acked = tp->bytes_acked;
  2471. info->tcpi_bytes_received = tp->bytes_received;
  2472. info->tcpi_notsent_bytes = max_t(int, 0, tp->write_seq - tp->snd_nxt);
  2473. tcp_get_info_chrono_stats(tp, info);
  2474. info->tcpi_segs_out = tp->segs_out;
  2475. info->tcpi_segs_in = tp->segs_in;
  2476. info->tcpi_min_rtt = tcp_min_rtt(tp);
  2477. info->tcpi_data_segs_in = tp->data_segs_in;
  2478. info->tcpi_data_segs_out = tp->data_segs_out;
  2479. info->tcpi_delivery_rate_app_limited = tp->rate_app_limited ? 1 : 0;
  2480. rate64 = tcp_compute_delivery_rate(tp);
  2481. if (rate64)
  2482. info->tcpi_delivery_rate = rate64;
  2483. unlock_sock_fast(sk, slow);
  2484. }
  2485. EXPORT_SYMBOL_GPL(tcp_get_info);
  2486. struct sk_buff *tcp_get_timestamping_opt_stats(const struct sock *sk)
  2487. {
  2488. const struct tcp_sock *tp = tcp_sk(sk);
  2489. struct sk_buff *stats;
  2490. struct tcp_info info;
  2491. u64 rate64;
  2492. u32 rate;
  2493. stats = alloc_skb(7 * nla_total_size_64bit(sizeof(u64)) +
  2494. 3 * nla_total_size(sizeof(u32)) +
  2495. 2 * nla_total_size(sizeof(u8)), GFP_ATOMIC);
  2496. if (!stats)
  2497. return NULL;
  2498. tcp_get_info_chrono_stats(tp, &info);
  2499. nla_put_u64_64bit(stats, TCP_NLA_BUSY,
  2500. info.tcpi_busy_time, TCP_NLA_PAD);
  2501. nla_put_u64_64bit(stats, TCP_NLA_RWND_LIMITED,
  2502. info.tcpi_rwnd_limited, TCP_NLA_PAD);
  2503. nla_put_u64_64bit(stats, TCP_NLA_SNDBUF_LIMITED,
  2504. info.tcpi_sndbuf_limited, TCP_NLA_PAD);
  2505. nla_put_u64_64bit(stats, TCP_NLA_DATA_SEGS_OUT,
  2506. tp->data_segs_out, TCP_NLA_PAD);
  2507. nla_put_u64_64bit(stats, TCP_NLA_TOTAL_RETRANS,
  2508. tp->total_retrans, TCP_NLA_PAD);
  2509. rate = READ_ONCE(sk->sk_pacing_rate);
  2510. rate64 = rate != ~0U ? rate : ~0ULL;
  2511. nla_put_u64_64bit(stats, TCP_NLA_PACING_RATE, rate64, TCP_NLA_PAD);
  2512. rate64 = tcp_compute_delivery_rate(tp);
  2513. nla_put_u64_64bit(stats, TCP_NLA_DELIVERY_RATE, rate64, TCP_NLA_PAD);
  2514. nla_put_u32(stats, TCP_NLA_SND_CWND, tp->snd_cwnd);
  2515. nla_put_u32(stats, TCP_NLA_REORDERING, tp->reordering);
  2516. nla_put_u32(stats, TCP_NLA_MIN_RTT, tcp_min_rtt(tp));
  2517. nla_put_u8(stats, TCP_NLA_RECUR_RETRANS, inet_csk(sk)->icsk_retransmits);
  2518. nla_put_u8(stats, TCP_NLA_DELIVERY_RATE_APP_LMT, !!tp->rate_app_limited);
  2519. return stats;
  2520. }
  2521. static int do_tcp_getsockopt(struct sock *sk, int level,
  2522. int optname, char __user *optval, int __user *optlen)
  2523. {
  2524. struct inet_connection_sock *icsk = inet_csk(sk);
  2525. struct tcp_sock *tp = tcp_sk(sk);
  2526. struct net *net = sock_net(sk);
  2527. int val, len;
  2528. if (get_user(len, optlen))
  2529. return -EFAULT;
  2530. len = min_t(unsigned int, len, sizeof(int));
  2531. if (len < 0)
  2532. return -EINVAL;
  2533. switch (optname) {
  2534. case TCP_MAXSEG:
  2535. val = tp->mss_cache;
  2536. if (!val && ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  2537. val = tp->rx_opt.user_mss;
  2538. if (tp->repair)
  2539. val = tp->rx_opt.mss_clamp;
  2540. break;
  2541. case TCP_NODELAY:
  2542. val = !!(tp->nonagle&TCP_NAGLE_OFF);
  2543. break;
  2544. case TCP_CORK:
  2545. val = !!(tp->nonagle&TCP_NAGLE_CORK);
  2546. break;
  2547. case TCP_KEEPIDLE:
  2548. val = keepalive_time_when(tp) / HZ;
  2549. break;
  2550. case TCP_KEEPINTVL:
  2551. val = keepalive_intvl_when(tp) / HZ;
  2552. break;
  2553. case TCP_KEEPCNT:
  2554. val = keepalive_probes(tp);
  2555. break;
  2556. case TCP_SYNCNT:
  2557. val = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
  2558. break;
  2559. case TCP_LINGER2:
  2560. val = tp->linger2;
  2561. if (val >= 0)
  2562. val = (val ? : net->ipv4.sysctl_tcp_fin_timeout) / HZ;
  2563. break;
  2564. case TCP_DEFER_ACCEPT:
  2565. val = retrans_to_secs(icsk->icsk_accept_queue.rskq_defer_accept,
  2566. TCP_TIMEOUT_INIT / HZ, TCP_RTO_MAX / HZ);
  2567. break;
  2568. case TCP_WINDOW_CLAMP:
  2569. val = tp->window_clamp;
  2570. break;
  2571. case TCP_INFO: {
  2572. struct tcp_info info;
  2573. if (get_user(len, optlen))
  2574. return -EFAULT;
  2575. tcp_get_info(sk, &info);
  2576. len = min_t(unsigned int, len, sizeof(info));
  2577. if (put_user(len, optlen))
  2578. return -EFAULT;
  2579. if (copy_to_user(optval, &info, len))
  2580. return -EFAULT;
  2581. return 0;
  2582. }
  2583. case TCP_CC_INFO: {
  2584. const struct tcp_congestion_ops *ca_ops;
  2585. union tcp_cc_info info;
  2586. size_t sz = 0;
  2587. int attr;
  2588. if (get_user(len, optlen))
  2589. return -EFAULT;
  2590. ca_ops = icsk->icsk_ca_ops;
  2591. if (ca_ops && ca_ops->get_info)
  2592. sz = ca_ops->get_info(sk, ~0U, &attr, &info);
  2593. len = min_t(unsigned int, len, sz);
  2594. if (put_user(len, optlen))
  2595. return -EFAULT;
  2596. if (copy_to_user(optval, &info, len))
  2597. return -EFAULT;
  2598. return 0;
  2599. }
  2600. case TCP_QUICKACK:
  2601. val = !icsk->icsk_ack.pingpong;
  2602. break;
  2603. case TCP_CONGESTION:
  2604. if (get_user(len, optlen))
  2605. return -EFAULT;
  2606. len = min_t(unsigned int, len, TCP_CA_NAME_MAX);
  2607. if (put_user(len, optlen))
  2608. return -EFAULT;
  2609. if (copy_to_user(optval, icsk->icsk_ca_ops->name, len))
  2610. return -EFAULT;
  2611. return 0;
  2612. case TCP_ULP:
  2613. if (get_user(len, optlen))
  2614. return -EFAULT;
  2615. len = min_t(unsigned int, len, TCP_ULP_NAME_MAX);
  2616. if (!icsk->icsk_ulp_ops) {
  2617. if (put_user(0, optlen))
  2618. return -EFAULT;
  2619. return 0;
  2620. }
  2621. if (put_user(len, optlen))
  2622. return -EFAULT;
  2623. if (copy_to_user(optval, icsk->icsk_ulp_ops->name, len))
  2624. return -EFAULT;
  2625. return 0;
  2626. case TCP_THIN_LINEAR_TIMEOUTS:
  2627. val = tp->thin_lto;
  2628. break;
  2629. case TCP_THIN_DUPACK:
  2630. val = 0;
  2631. break;
  2632. case TCP_REPAIR:
  2633. val = tp->repair;
  2634. break;
  2635. case TCP_REPAIR_QUEUE:
  2636. if (tp->repair)
  2637. val = tp->repair_queue;
  2638. else
  2639. return -EINVAL;
  2640. break;
  2641. case TCP_REPAIR_WINDOW: {
  2642. struct tcp_repair_window opt;
  2643. if (get_user(len, optlen))
  2644. return -EFAULT;
  2645. if (len != sizeof(opt))
  2646. return -EINVAL;
  2647. if (!tp->repair)
  2648. return -EPERM;
  2649. opt.snd_wl1 = tp->snd_wl1;
  2650. opt.snd_wnd = tp->snd_wnd;
  2651. opt.max_window = tp->max_window;
  2652. opt.rcv_wnd = tp->rcv_wnd;
  2653. opt.rcv_wup = tp->rcv_wup;
  2654. if (copy_to_user(optval, &opt, len))
  2655. return -EFAULT;
  2656. return 0;
  2657. }
  2658. case TCP_QUEUE_SEQ:
  2659. if (tp->repair_queue == TCP_SEND_QUEUE)
  2660. val = tp->write_seq;
  2661. else if (tp->repair_queue == TCP_RECV_QUEUE)
  2662. val = tp->rcv_nxt;
  2663. else
  2664. return -EINVAL;
  2665. break;
  2666. case TCP_USER_TIMEOUT:
  2667. val = jiffies_to_msecs(icsk->icsk_user_timeout);
  2668. break;
  2669. case TCP_FASTOPEN:
  2670. val = icsk->icsk_accept_queue.fastopenq.max_qlen;
  2671. break;
  2672. case TCP_FASTOPEN_CONNECT:
  2673. val = tp->fastopen_connect;
  2674. break;
  2675. case TCP_TIMESTAMP:
  2676. val = tcp_time_stamp_raw() + tp->tsoffset;
  2677. break;
  2678. case TCP_NOTSENT_LOWAT:
  2679. val = tp->notsent_lowat;
  2680. break;
  2681. case TCP_SAVE_SYN:
  2682. val = tp->save_syn;
  2683. break;
  2684. case TCP_SAVED_SYN: {
  2685. if (get_user(len, optlen))
  2686. return -EFAULT;
  2687. lock_sock(sk);
  2688. if (tp->saved_syn) {
  2689. if (len < tp->saved_syn[0]) {
  2690. if (put_user(tp->saved_syn[0], optlen)) {
  2691. release_sock(sk);
  2692. return -EFAULT;
  2693. }
  2694. release_sock(sk);
  2695. return -EINVAL;
  2696. }
  2697. len = tp->saved_syn[0];
  2698. if (put_user(len, optlen)) {
  2699. release_sock(sk);
  2700. return -EFAULT;
  2701. }
  2702. if (copy_to_user(optval, tp->saved_syn + 1, len)) {
  2703. release_sock(sk);
  2704. return -EFAULT;
  2705. }
  2706. tcp_saved_syn_free(tp);
  2707. release_sock(sk);
  2708. } else {
  2709. release_sock(sk);
  2710. len = 0;
  2711. if (put_user(len, optlen))
  2712. return -EFAULT;
  2713. }
  2714. return 0;
  2715. }
  2716. default:
  2717. return -ENOPROTOOPT;
  2718. }
  2719. if (put_user(len, optlen))
  2720. return -EFAULT;
  2721. if (copy_to_user(optval, &val, len))
  2722. return -EFAULT;
  2723. return 0;
  2724. }
  2725. int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
  2726. int __user *optlen)
  2727. {
  2728. struct inet_connection_sock *icsk = inet_csk(sk);
  2729. if (level != SOL_TCP)
  2730. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  2731. optval, optlen);
  2732. return do_tcp_getsockopt(sk, level, optname, optval, optlen);
  2733. }
  2734. EXPORT_SYMBOL(tcp_getsockopt);
  2735. #ifdef CONFIG_COMPAT
  2736. int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
  2737. char __user *optval, int __user *optlen)
  2738. {
  2739. if (level != SOL_TCP)
  2740. return inet_csk_compat_getsockopt(sk, level, optname,
  2741. optval, optlen);
  2742. return do_tcp_getsockopt(sk, level, optname, optval, optlen);
  2743. }
  2744. EXPORT_SYMBOL(compat_tcp_getsockopt);
  2745. #endif
  2746. #ifdef CONFIG_TCP_MD5SIG
  2747. static DEFINE_PER_CPU(struct tcp_md5sig_pool, tcp_md5sig_pool);
  2748. static DEFINE_MUTEX(tcp_md5sig_mutex);
  2749. static bool tcp_md5sig_pool_populated = false;
  2750. static void __tcp_alloc_md5sig_pool(void)
  2751. {
  2752. struct crypto_ahash *hash;
  2753. int cpu;
  2754. hash = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
  2755. if (IS_ERR(hash))
  2756. return;
  2757. for_each_possible_cpu(cpu) {
  2758. void *scratch = per_cpu(tcp_md5sig_pool, cpu).scratch;
  2759. struct ahash_request *req;
  2760. if (!scratch) {
  2761. scratch = kmalloc_node(sizeof(union tcp_md5sum_block) +
  2762. sizeof(struct tcphdr),
  2763. GFP_KERNEL,
  2764. cpu_to_node(cpu));
  2765. if (!scratch)
  2766. return;
  2767. per_cpu(tcp_md5sig_pool, cpu).scratch = scratch;
  2768. }
  2769. if (per_cpu(tcp_md5sig_pool, cpu).md5_req)
  2770. continue;
  2771. req = ahash_request_alloc(hash, GFP_KERNEL);
  2772. if (!req)
  2773. return;
  2774. ahash_request_set_callback(req, 0, NULL, NULL);
  2775. per_cpu(tcp_md5sig_pool, cpu).md5_req = req;
  2776. }
  2777. /* before setting tcp_md5sig_pool_populated, we must commit all writes
  2778. * to memory. See smp_rmb() in tcp_get_md5sig_pool()
  2779. */
  2780. smp_wmb();
  2781. tcp_md5sig_pool_populated = true;
  2782. }
  2783. bool tcp_alloc_md5sig_pool(void)
  2784. {
  2785. if (unlikely(!tcp_md5sig_pool_populated)) {
  2786. mutex_lock(&tcp_md5sig_mutex);
  2787. if (!tcp_md5sig_pool_populated)
  2788. __tcp_alloc_md5sig_pool();
  2789. mutex_unlock(&tcp_md5sig_mutex);
  2790. }
  2791. return tcp_md5sig_pool_populated;
  2792. }
  2793. EXPORT_SYMBOL(tcp_alloc_md5sig_pool);
  2794. /**
  2795. * tcp_get_md5sig_pool - get md5sig_pool for this user
  2796. *
  2797. * We use percpu structure, so if we succeed, we exit with preemption
  2798. * and BH disabled, to make sure another thread or softirq handling
  2799. * wont try to get same context.
  2800. */
  2801. struct tcp_md5sig_pool *tcp_get_md5sig_pool(void)
  2802. {
  2803. local_bh_disable();
  2804. if (tcp_md5sig_pool_populated) {
  2805. /* coupled with smp_wmb() in __tcp_alloc_md5sig_pool() */
  2806. smp_rmb();
  2807. return this_cpu_ptr(&tcp_md5sig_pool);
  2808. }
  2809. local_bh_enable();
  2810. return NULL;
  2811. }
  2812. EXPORT_SYMBOL(tcp_get_md5sig_pool);
  2813. int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
  2814. const struct sk_buff *skb, unsigned int header_len)
  2815. {
  2816. struct scatterlist sg;
  2817. const struct tcphdr *tp = tcp_hdr(skb);
  2818. struct ahash_request *req = hp->md5_req;
  2819. unsigned int i;
  2820. const unsigned int head_data_len = skb_headlen(skb) > header_len ?
  2821. skb_headlen(skb) - header_len : 0;
  2822. const struct skb_shared_info *shi = skb_shinfo(skb);
  2823. struct sk_buff *frag_iter;
  2824. sg_init_table(&sg, 1);
  2825. sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len);
  2826. ahash_request_set_crypt(req, &sg, NULL, head_data_len);
  2827. if (crypto_ahash_update(req))
  2828. return 1;
  2829. for (i = 0; i < shi->nr_frags; ++i) {
  2830. const struct skb_frag_struct *f = &shi->frags[i];
  2831. unsigned int offset = f->page_offset;
  2832. struct page *page = skb_frag_page(f) + (offset >> PAGE_SHIFT);
  2833. sg_set_page(&sg, page, skb_frag_size(f),
  2834. offset_in_page(offset));
  2835. ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f));
  2836. if (crypto_ahash_update(req))
  2837. return 1;
  2838. }
  2839. skb_walk_frags(skb, frag_iter)
  2840. if (tcp_md5_hash_skb_data(hp, frag_iter, 0))
  2841. return 1;
  2842. return 0;
  2843. }
  2844. EXPORT_SYMBOL(tcp_md5_hash_skb_data);
  2845. int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
  2846. {
  2847. struct scatterlist sg;
  2848. sg_init_one(&sg, key->key, key->keylen);
  2849. ahash_request_set_crypt(hp->md5_req, &sg, NULL, key->keylen);
  2850. return crypto_ahash_update(hp->md5_req);
  2851. }
  2852. EXPORT_SYMBOL(tcp_md5_hash_key);
  2853. #endif
  2854. void tcp_done(struct sock *sk)
  2855. {
  2856. struct request_sock *req = tcp_sk(sk)->fastopen_rsk;
  2857. if (sk->sk_state == TCP_SYN_SENT || sk->sk_state == TCP_SYN_RECV)
  2858. TCP_INC_STATS(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
  2859. tcp_set_state(sk, TCP_CLOSE);
  2860. tcp_clear_xmit_timers(sk);
  2861. if (req)
  2862. reqsk_fastopen_remove(sk, req, false);
  2863. sk->sk_shutdown = SHUTDOWN_MASK;
  2864. if (!sock_flag(sk, SOCK_DEAD))
  2865. sk->sk_state_change(sk);
  2866. else
  2867. inet_csk_destroy_sock(sk);
  2868. }
  2869. EXPORT_SYMBOL_GPL(tcp_done);
  2870. int tcp_abort(struct sock *sk, int err)
  2871. {
  2872. if (!sk_fullsock(sk)) {
  2873. if (sk->sk_state == TCP_NEW_SYN_RECV) {
  2874. struct request_sock *req = inet_reqsk(sk);
  2875. local_bh_disable();
  2876. inet_csk_reqsk_queue_drop_and_put(req->rsk_listener,
  2877. req);
  2878. local_bh_enable();
  2879. return 0;
  2880. }
  2881. return -EOPNOTSUPP;
  2882. }
  2883. /* Don't race with userspace socket closes such as tcp_close. */
  2884. lock_sock(sk);
  2885. if (sk->sk_state == TCP_LISTEN) {
  2886. tcp_set_state(sk, TCP_CLOSE);
  2887. inet_csk_listen_stop(sk);
  2888. }
  2889. /* Don't race with BH socket closes such as inet_csk_listen_stop. */
  2890. local_bh_disable();
  2891. bh_lock_sock(sk);
  2892. if (!sock_flag(sk, SOCK_DEAD)) {
  2893. sk->sk_err = err;
  2894. /* This barrier is coupled with smp_rmb() in tcp_poll() */
  2895. smp_wmb();
  2896. sk->sk_error_report(sk);
  2897. if (tcp_need_reset(sk->sk_state))
  2898. tcp_send_active_reset(sk, GFP_ATOMIC);
  2899. tcp_done(sk);
  2900. }
  2901. bh_unlock_sock(sk);
  2902. local_bh_enable();
  2903. release_sock(sk);
  2904. return 0;
  2905. }
  2906. EXPORT_SYMBOL_GPL(tcp_abort);
  2907. extern struct tcp_congestion_ops tcp_reno;
  2908. static __initdata unsigned long thash_entries;
  2909. static int __init set_thash_entries(char *str)
  2910. {
  2911. ssize_t ret;
  2912. if (!str)
  2913. return 0;
  2914. ret = kstrtoul(str, 0, &thash_entries);
  2915. if (ret)
  2916. return 0;
  2917. return 1;
  2918. }
  2919. __setup("thash_entries=", set_thash_entries);
  2920. static void __init tcp_init_mem(void)
  2921. {
  2922. unsigned long limit = nr_free_buffer_pages() / 16;
  2923. limit = max(limit, 128UL);
  2924. sysctl_tcp_mem[0] = limit / 4 * 3; /* 4.68 % */
  2925. sysctl_tcp_mem[1] = limit; /* 6.25 % */
  2926. sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2; /* 9.37 % */
  2927. }
  2928. void __init tcp_init(void)
  2929. {
  2930. int max_rshare, max_wshare, cnt;
  2931. unsigned long limit;
  2932. unsigned int i;
  2933. BUILD_BUG_ON(sizeof(struct tcp_skb_cb) >
  2934. FIELD_SIZEOF(struct sk_buff, cb));
  2935. percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL);
  2936. percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL);
  2937. inet_hashinfo_init(&tcp_hashinfo);
  2938. tcp_hashinfo.bind_bucket_cachep =
  2939. kmem_cache_create("tcp_bind_bucket",
  2940. sizeof(struct inet_bind_bucket), 0,
  2941. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2942. /* Size and allocate the main established and bind bucket
  2943. * hash tables.
  2944. *
  2945. * The methodology is similar to that of the buffer cache.
  2946. */
  2947. tcp_hashinfo.ehash =
  2948. alloc_large_system_hash("TCP established",
  2949. sizeof(struct inet_ehash_bucket),
  2950. thash_entries,
  2951. 17, /* one slot per 128 KB of memory */
  2952. 0,
  2953. NULL,
  2954. &tcp_hashinfo.ehash_mask,
  2955. 0,
  2956. thash_entries ? 0 : 512 * 1024);
  2957. for (i = 0; i <= tcp_hashinfo.ehash_mask; i++)
  2958. INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i);
  2959. if (inet_ehash_locks_alloc(&tcp_hashinfo))
  2960. panic("TCP: failed to alloc ehash_locks");
  2961. tcp_hashinfo.bhash =
  2962. alloc_large_system_hash("TCP bind",
  2963. sizeof(struct inet_bind_hashbucket),
  2964. tcp_hashinfo.ehash_mask + 1,
  2965. 17, /* one slot per 128 KB of memory */
  2966. 0,
  2967. &tcp_hashinfo.bhash_size,
  2968. NULL,
  2969. 0,
  2970. 64 * 1024);
  2971. tcp_hashinfo.bhash_size = 1U << tcp_hashinfo.bhash_size;
  2972. for (i = 0; i < tcp_hashinfo.bhash_size; i++) {
  2973. spin_lock_init(&tcp_hashinfo.bhash[i].lock);
  2974. INIT_HLIST_HEAD(&tcp_hashinfo.bhash[i].chain);
  2975. }
  2976. cnt = tcp_hashinfo.ehash_mask + 1;
  2977. sysctl_tcp_max_orphans = cnt / 2;
  2978. tcp_init_mem();
  2979. /* Set per-socket limits to no more than 1/128 the pressure threshold */
  2980. limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7);
  2981. max_wshare = min(4UL*1024*1024, limit);
  2982. max_rshare = min(6UL*1024*1024, limit);
  2983. sysctl_tcp_wmem[0] = SK_MEM_QUANTUM;
  2984. sysctl_tcp_wmem[1] = 16*1024;
  2985. sysctl_tcp_wmem[2] = max(64*1024, max_wshare);
  2986. sysctl_tcp_rmem[0] = SK_MEM_QUANTUM;
  2987. sysctl_tcp_rmem[1] = 87380;
  2988. sysctl_tcp_rmem[2] = max(87380, max_rshare);
  2989. pr_info("Hash tables configured (established %u bind %u)\n",
  2990. tcp_hashinfo.ehash_mask + 1, tcp_hashinfo.bhash_size);
  2991. tcp_v4_init();
  2992. tcp_metrics_init();
  2993. BUG_ON(tcp_register_congestion_control(&tcp_reno) != 0);
  2994. tcp_tasklet_init();
  2995. }