tcp.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Implementation of the Transmission Control Protocol(TCP).
  7. *
  8. * Version: $Id: tcp.c,v 1.216 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  13. * Corey Minyard <wf-rch!minyard@relay.EU.net>
  14. * Florian La Roche, <flla@stud.uni-sb.de>
  15. * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  16. * Linus Torvalds, <torvalds@cs.helsinki.fi>
  17. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  18. * Matthew Dillon, <dillon@apollo.west.oic.com>
  19. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  20. * Jorge Cwik, <jorge@laser.satlink.net>
  21. *
  22. * Fixes:
  23. * Alan Cox : Numerous verify_area() calls
  24. * Alan Cox : Set the ACK bit on a reset
  25. * Alan Cox : Stopped it crashing if it closed while
  26. * sk->inuse=1 and was trying to connect
  27. * (tcp_err()).
  28. * Alan Cox : All icmp error handling was broken
  29. * pointers passed where wrong and the
  30. * socket was looked up backwards. Nobody
  31. * tested any icmp error code obviously.
  32. * Alan Cox : tcp_err() now handled properly. It
  33. * wakes people on errors. poll
  34. * behaves and the icmp error race
  35. * has gone by moving it into sock.c
  36. * Alan Cox : tcp_send_reset() fixed to work for
  37. * everything not just packets for
  38. * unknown sockets.
  39. * Alan Cox : tcp option processing.
  40. * Alan Cox : Reset tweaked (still not 100%) [Had
  41. * syn rule wrong]
  42. * Herp Rosmanith : More reset fixes
  43. * Alan Cox : No longer acks invalid rst frames.
  44. * Acking any kind of RST is right out.
  45. * Alan Cox : Sets an ignore me flag on an rst
  46. * receive otherwise odd bits of prattle
  47. * escape still
  48. * Alan Cox : Fixed another acking RST frame bug.
  49. * Should stop LAN workplace lockups.
  50. * Alan Cox : Some tidyups using the new skb list
  51. * facilities
  52. * Alan Cox : sk->keepopen now seems to work
  53. * Alan Cox : Pulls options out correctly on accepts
  54. * Alan Cox : Fixed assorted sk->rqueue->next errors
  55. * Alan Cox : PSH doesn't end a TCP read. Switched a
  56. * bit to skb ops.
  57. * Alan Cox : Tidied tcp_data to avoid a potential
  58. * nasty.
  59. * Alan Cox : Added some better commenting, as the
  60. * tcp is hard to follow
  61. * Alan Cox : Removed incorrect check for 20 * psh
  62. * Michael O'Reilly : ack < copied bug fix.
  63. * Johannes Stille : Misc tcp fixes (not all in yet).
  64. * Alan Cox : FIN with no memory -> CRASH
  65. * Alan Cox : Added socket option proto entries.
  66. * Also added awareness of them to accept.
  67. * Alan Cox : Added TCP options (SOL_TCP)
  68. * Alan Cox : Switched wakeup calls to callbacks,
  69. * so the kernel can layer network
  70. * sockets.
  71. * Alan Cox : Use ip_tos/ip_ttl settings.
  72. * Alan Cox : Handle FIN (more) properly (we hope).
  73. * Alan Cox : RST frames sent on unsynchronised
  74. * state ack error.
  75. * Alan Cox : Put in missing check for SYN bit.
  76. * Alan Cox : Added tcp_select_window() aka NET2E
  77. * window non shrink trick.
  78. * Alan Cox : Added a couple of small NET2E timer
  79. * fixes
  80. * Charles Hedrick : TCP fixes
  81. * Toomas Tamm : TCP window fixes
  82. * Alan Cox : Small URG fix to rlogin ^C ack fight
  83. * Charles Hedrick : Rewrote most of it to actually work
  84. * Linus : Rewrote tcp_read() and URG handling
  85. * completely
  86. * Gerhard Koerting: Fixed some missing timer handling
  87. * Matthew Dillon : Reworked TCP machine states as per RFC
  88. * Gerhard Koerting: PC/TCP workarounds
  89. * Adam Caldwell : Assorted timer/timing errors
  90. * Matthew Dillon : Fixed another RST bug
  91. * Alan Cox : Move to kernel side addressing changes.
  92. * Alan Cox : Beginning work on TCP fastpathing
  93. * (not yet usable)
  94. * Arnt Gulbrandsen: Turbocharged tcp_check() routine.
  95. * Alan Cox : TCP fast path debugging
  96. * Alan Cox : Window clamping
  97. * Michael Riepe : Bug in tcp_check()
  98. * Matt Dillon : More TCP improvements and RST bug fixes
  99. * Matt Dillon : Yet more small nasties remove from the
  100. * TCP code (Be very nice to this man if
  101. * tcp finally works 100%) 8)
  102. * Alan Cox : BSD accept semantics.
  103. * Alan Cox : Reset on closedown bug.
  104. * Peter De Schrijver : ENOTCONN check missing in tcp_sendto().
  105. * Michael Pall : Handle poll() after URG properly in
  106. * all cases.
  107. * Michael Pall : Undo the last fix in tcp_read_urg()
  108. * (multi URG PUSH broke rlogin).
  109. * Michael Pall : Fix the multi URG PUSH problem in
  110. * tcp_readable(), poll() after URG
  111. * works now.
  112. * Michael Pall : recv(...,MSG_OOB) never blocks in the
  113. * BSD api.
  114. * Alan Cox : Changed the semantics of sk->socket to
  115. * fix a race and a signal problem with
  116. * accept() and async I/O.
  117. * Alan Cox : Relaxed the rules on tcp_sendto().
  118. * Yury Shevchuk : Really fixed accept() blocking problem.
  119. * Craig I. Hagan : Allow for BSD compatible TIME_WAIT for
  120. * clients/servers which listen in on
  121. * fixed ports.
  122. * Alan Cox : Cleaned the above up and shrank it to
  123. * a sensible code size.
  124. * Alan Cox : Self connect lockup fix.
  125. * Alan Cox : No connect to multicast.
  126. * Ross Biro : Close unaccepted children on master
  127. * socket close.
  128. * Alan Cox : Reset tracing code.
  129. * Alan Cox : Spurious resets on shutdown.
  130. * Alan Cox : Giant 15 minute/60 second timer error
  131. * Alan Cox : Small whoops in polling before an
  132. * accept.
  133. * Alan Cox : Kept the state trace facility since
  134. * it's handy for debugging.
  135. * Alan Cox : More reset handler fixes.
  136. * Alan Cox : Started rewriting the code based on
  137. * the RFC's for other useful protocol
  138. * references see: Comer, KA9Q NOS, and
  139. * for a reference on the difference
  140. * between specifications and how BSD
  141. * works see the 4.4lite source.
  142. * A.N.Kuznetsov : Don't time wait on completion of tidy
  143. * close.
  144. * Linus Torvalds : Fin/Shutdown & copied_seq changes.
  145. * Linus Torvalds : Fixed BSD port reuse to work first syn
  146. * Alan Cox : Reimplemented timers as per the RFC
  147. * and using multiple timers for sanity.
  148. * Alan Cox : Small bug fixes, and a lot of new
  149. * comments.
  150. * Alan Cox : Fixed dual reader crash by locking
  151. * the buffers (much like datagram.c)
  152. * Alan Cox : Fixed stuck sockets in probe. A probe
  153. * now gets fed up of retrying without
  154. * (even a no space) answer.
  155. * Alan Cox : Extracted closing code better
  156. * Alan Cox : Fixed the closing state machine to
  157. * resemble the RFC.
  158. * Alan Cox : More 'per spec' fixes.
  159. * Jorge Cwik : Even faster checksumming.
  160. * Alan Cox : tcp_data() doesn't ack illegal PSH
  161. * only frames. At least one pc tcp stack
  162. * generates them.
  163. * Alan Cox : Cache last socket.
  164. * Alan Cox : Per route irtt.
  165. * Matt Day : poll()->select() match BSD precisely on error
  166. * Alan Cox : New buffers
  167. * Marc Tamsky : Various sk->prot->retransmits and
  168. * sk->retransmits misupdating fixed.
  169. * Fixed tcp_write_timeout: stuck close,
  170. * and TCP syn retries gets used now.
  171. * Mark Yarvis : In tcp_read_wakeup(), don't send an
  172. * ack if state is TCP_CLOSED.
  173. * Alan Cox : Look up device on a retransmit - routes may
  174. * change. Doesn't yet cope with MSS shrink right
  175. * but it's a start!
  176. * Marc Tamsky : Closing in closing fixes.
  177. * Mike Shaver : RFC1122 verifications.
  178. * Alan Cox : rcv_saddr errors.
  179. * Alan Cox : Block double connect().
  180. * Alan Cox : Small hooks for enSKIP.
  181. * Alexey Kuznetsov: Path MTU discovery.
  182. * Alan Cox : Support soft errors.
  183. * Alan Cox : Fix MTU discovery pathological case
  184. * when the remote claims no mtu!
  185. * Marc Tamsky : TCP_CLOSE fix.
  186. * Colin (G3TNE) : Send a reset on syn ack replies in
  187. * window but wrong (fixes NT lpd problems)
  188. * Pedro Roque : Better TCP window handling, delayed ack.
  189. * Joerg Reuter : No modification of locked buffers in
  190. * tcp_do_retransmit()
  191. * Eric Schenk : Changed receiver side silly window
  192. * avoidance algorithm to BSD style
  193. * algorithm. This doubles throughput
  194. * against machines running Solaris,
  195. * and seems to result in general
  196. * improvement.
  197. * Stefan Magdalinski : adjusted tcp_readable() to fix FIONREAD
  198. * Willy Konynenberg : Transparent proxying support.
  199. * Mike McLagan : Routing by source
  200. * Keith Owens : Do proper merging with partial SKB's in
  201. * tcp_do_sendmsg to avoid burstiness.
  202. * Eric Schenk : Fix fast close down bug with
  203. * shutdown() followed by close().
  204. * Andi Kleen : Make poll agree with SIGIO
  205. * Salvatore Sanfilippo : Support SO_LINGER with linger == 1 and
  206. * lingertime == 0 (RFC 793 ABORT Call)
  207. * Hirokazu Takahashi : Use copy_from_user() instead of
  208. * csum_and_copy_from_user() if possible.
  209. *
  210. * This program is free software; you can redistribute it and/or
  211. * modify it under the terms of the GNU General Public License
  212. * as published by the Free Software Foundation; either version
  213. * 2 of the License, or(at your option) any later version.
  214. *
  215. * Description of States:
  216. *
  217. * TCP_SYN_SENT sent a connection request, waiting for ack
  218. *
  219. * TCP_SYN_RECV received a connection request, sent ack,
  220. * waiting for final ack in three-way handshake.
  221. *
  222. * TCP_ESTABLISHED connection established
  223. *
  224. * TCP_FIN_WAIT1 our side has shutdown, waiting to complete
  225. * transmission of remaining buffered data
  226. *
  227. * TCP_FIN_WAIT2 all buffered data sent, waiting for remote
  228. * to shutdown
  229. *
  230. * TCP_CLOSING both sides have shutdown but we still have
  231. * data we have to finish sending
  232. *
  233. * TCP_TIME_WAIT timeout to catch resent junk before entering
  234. * closed, can only be entered from FIN_WAIT2
  235. * or CLOSING. Required because the other end
  236. * may not have gotten our last ACK causing it
  237. * to retransmit the data packet (which we ignore)
  238. *
  239. * TCP_CLOSE_WAIT remote side has shutdown and is waiting for
  240. * us to finish writing our data and to shutdown
  241. * (we have to close() to move on to LAST_ACK)
  242. *
  243. * TCP_LAST_ACK out side has shutdown after remote has
  244. * shutdown. There may still be data in our
  245. * buffer that we have to finish sending
  246. *
  247. * TCP_CLOSE socket is finished
  248. */
  249. #include <linux/config.h>
  250. #include <linux/module.h>
  251. #include <linux/types.h>
  252. #include <linux/fcntl.h>
  253. #include <linux/poll.h>
  254. #include <linux/init.h>
  255. #include <linux/smp_lock.h>
  256. #include <linux/fs.h>
  257. #include <linux/random.h>
  258. #include <linux/bootmem.h>
  259. #include <net/icmp.h>
  260. #include <net/tcp.h>
  261. #include <net/xfrm.h>
  262. #include <net/ip.h>
  263. #include <asm/uaccess.h>
  264. #include <asm/ioctls.h>
  265. int sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT;
  266. DEFINE_SNMP_STAT(struct tcp_mib, tcp_statistics);
  267. kmem_cache_t *tcp_bucket_cachep;
  268. kmem_cache_t *tcp_timewait_cachep;
  269. atomic_t tcp_orphan_count = ATOMIC_INIT(0);
  270. int sysctl_tcp_mem[3];
  271. int sysctl_tcp_wmem[3] = { 4 * 1024, 16 * 1024, 128 * 1024 };
  272. int sysctl_tcp_rmem[3] = { 4 * 1024, 87380, 87380 * 2 };
  273. EXPORT_SYMBOL(sysctl_tcp_mem);
  274. EXPORT_SYMBOL(sysctl_tcp_rmem);
  275. EXPORT_SYMBOL(sysctl_tcp_wmem);
  276. atomic_t tcp_memory_allocated; /* Current allocated memory. */
  277. atomic_t tcp_sockets_allocated; /* Current number of TCP sockets. */
  278. EXPORT_SYMBOL(tcp_memory_allocated);
  279. EXPORT_SYMBOL(tcp_sockets_allocated);
  280. /*
  281. * Pressure flag: try to collapse.
  282. * Technical note: it is used by multiple contexts non atomically.
  283. * All the sk_stream_mem_schedule() is of this nature: accounting
  284. * is strict, actions are advisory and have some latency.
  285. */
  286. int tcp_memory_pressure;
  287. EXPORT_SYMBOL(tcp_memory_pressure);
  288. void tcp_enter_memory_pressure(void)
  289. {
  290. if (!tcp_memory_pressure) {
  291. NET_INC_STATS(LINUX_MIB_TCPMEMORYPRESSURES);
  292. tcp_memory_pressure = 1;
  293. }
  294. }
  295. EXPORT_SYMBOL(tcp_enter_memory_pressure);
  296. /*
  297. * LISTEN is a special case for poll..
  298. */
  299. static __inline__ unsigned int tcp_listen_poll(struct sock *sk,
  300. poll_table *wait)
  301. {
  302. return !reqsk_queue_empty(&tcp_sk(sk)->accept_queue) ? (POLLIN | POLLRDNORM) : 0;
  303. }
  304. /*
  305. * Wait for a TCP event.
  306. *
  307. * Note that we don't need to lock the socket, as the upper poll layers
  308. * take care of normal races (between the test and the event) and we don't
  309. * go look at any of the socket buffers directly.
  310. */
  311. unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
  312. {
  313. unsigned int mask;
  314. struct sock *sk = sock->sk;
  315. struct tcp_sock *tp = tcp_sk(sk);
  316. poll_wait(file, sk->sk_sleep, wait);
  317. if (sk->sk_state == TCP_LISTEN)
  318. return tcp_listen_poll(sk, wait);
  319. /* Socket is not locked. We are protected from async events
  320. by poll logic and correct handling of state changes
  321. made by another threads is impossible in any case.
  322. */
  323. mask = 0;
  324. if (sk->sk_err)
  325. mask = POLLERR;
  326. /*
  327. * POLLHUP is certainly not done right. But poll() doesn't
  328. * have a notion of HUP in just one direction, and for a
  329. * socket the read side is more interesting.
  330. *
  331. * Some poll() documentation says that POLLHUP is incompatible
  332. * with the POLLOUT/POLLWR flags, so somebody should check this
  333. * all. But careful, it tends to be safer to return too many
  334. * bits than too few, and you can easily break real applications
  335. * if you don't tell them that something has hung up!
  336. *
  337. * Check-me.
  338. *
  339. * Check number 1. POLLHUP is _UNMASKABLE_ event (see UNIX98 and
  340. * our fs/select.c). It means that after we received EOF,
  341. * poll always returns immediately, making impossible poll() on write()
  342. * in state CLOSE_WAIT. One solution is evident --- to set POLLHUP
  343. * if and only if shutdown has been made in both directions.
  344. * Actually, it is interesting to look how Solaris and DUX
  345. * solve this dilemma. I would prefer, if PULLHUP were maskable,
  346. * then we could set it on SND_SHUTDOWN. BTW examples given
  347. * in Stevens' books assume exactly this behaviour, it explains
  348. * why PULLHUP is incompatible with POLLOUT. --ANK
  349. *
  350. * NOTE. Check for TCP_CLOSE is added. The goal is to prevent
  351. * blocking on fresh not-connected or disconnected socket. --ANK
  352. */
  353. if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == TCP_CLOSE)
  354. mask |= POLLHUP;
  355. if (sk->sk_shutdown & RCV_SHUTDOWN)
  356. mask |= POLLIN | POLLRDNORM;
  357. /* Connected? */
  358. if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  359. /* Potential race condition. If read of tp below will
  360. * escape above sk->sk_state, we can be illegally awaken
  361. * in SYN_* states. */
  362. if ((tp->rcv_nxt != tp->copied_seq) &&
  363. (tp->urg_seq != tp->copied_seq ||
  364. tp->rcv_nxt != tp->copied_seq + 1 ||
  365. sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data))
  366. mask |= POLLIN | POLLRDNORM;
  367. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  368. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
  369. mask |= POLLOUT | POLLWRNORM;
  370. } else { /* send SIGIO later */
  371. set_bit(SOCK_ASYNC_NOSPACE,
  372. &sk->sk_socket->flags);
  373. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  374. /* Race breaker. If space is freed after
  375. * wspace test but before the flags are set,
  376. * IO signal will be lost.
  377. */
  378. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
  379. mask |= POLLOUT | POLLWRNORM;
  380. }
  381. }
  382. if (tp->urg_data & TCP_URG_VALID)
  383. mask |= POLLPRI;
  384. }
  385. return mask;
  386. }
  387. int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  388. {
  389. struct tcp_sock *tp = tcp_sk(sk);
  390. int answ;
  391. switch (cmd) {
  392. case SIOCINQ:
  393. if (sk->sk_state == TCP_LISTEN)
  394. return -EINVAL;
  395. lock_sock(sk);
  396. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
  397. answ = 0;
  398. else if (sock_flag(sk, SOCK_URGINLINE) ||
  399. !tp->urg_data ||
  400. before(tp->urg_seq, tp->copied_seq) ||
  401. !before(tp->urg_seq, tp->rcv_nxt)) {
  402. answ = tp->rcv_nxt - tp->copied_seq;
  403. /* Subtract 1, if FIN is in queue. */
  404. if (answ && !skb_queue_empty(&sk->sk_receive_queue))
  405. answ -=
  406. ((struct sk_buff *)sk->sk_receive_queue.prev)->h.th->fin;
  407. } else
  408. answ = tp->urg_seq - tp->copied_seq;
  409. release_sock(sk);
  410. break;
  411. case SIOCATMARK:
  412. answ = tp->urg_data && tp->urg_seq == tp->copied_seq;
  413. break;
  414. case SIOCOUTQ:
  415. if (sk->sk_state == TCP_LISTEN)
  416. return -EINVAL;
  417. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
  418. answ = 0;
  419. else
  420. answ = tp->write_seq - tp->snd_una;
  421. break;
  422. default:
  423. return -ENOIOCTLCMD;
  424. };
  425. return put_user(answ, (int __user *)arg);
  426. }
  427. int tcp_listen_start(struct sock *sk)
  428. {
  429. struct inet_sock *inet = inet_sk(sk);
  430. struct tcp_sock *tp = tcp_sk(sk);
  431. int rc = reqsk_queue_alloc(&tp->accept_queue, TCP_SYNQ_HSIZE);
  432. if (rc != 0)
  433. return rc;
  434. sk->sk_max_ack_backlog = 0;
  435. sk->sk_ack_backlog = 0;
  436. tcp_delack_init(tp);
  437. /* There is race window here: we announce ourselves listening,
  438. * but this transition is still not validated by get_port().
  439. * It is OK, because this socket enters to hash table only
  440. * after validation is complete.
  441. */
  442. sk->sk_state = TCP_LISTEN;
  443. if (!sk->sk_prot->get_port(sk, inet->num)) {
  444. inet->sport = htons(inet->num);
  445. sk_dst_reset(sk);
  446. sk->sk_prot->hash(sk);
  447. return 0;
  448. }
  449. sk->sk_state = TCP_CLOSE;
  450. reqsk_queue_destroy(&tp->accept_queue);
  451. return -EADDRINUSE;
  452. }
  453. /*
  454. * This routine closes sockets which have been at least partially
  455. * opened, but not yet accepted.
  456. */
  457. static void tcp_listen_stop (struct sock *sk)
  458. {
  459. struct tcp_sock *tp = tcp_sk(sk);
  460. struct listen_sock *lopt;
  461. struct request_sock *acc_req;
  462. struct request_sock *req;
  463. int i;
  464. tcp_delete_keepalive_timer(sk);
  465. /* make all the listen_opt local to us */
  466. lopt = reqsk_queue_yank_listen_sk(&tp->accept_queue);
  467. acc_req = reqsk_queue_yank_acceptq(&tp->accept_queue);
  468. if (lopt->qlen) {
  469. for (i = 0; i < TCP_SYNQ_HSIZE; i++) {
  470. while ((req = lopt->syn_table[i]) != NULL) {
  471. lopt->syn_table[i] = req->dl_next;
  472. lopt->qlen--;
  473. reqsk_free(req);
  474. /* Following specs, it would be better either to send FIN
  475. * (and enter FIN-WAIT-1, it is normal close)
  476. * or to send active reset (abort).
  477. * Certainly, it is pretty dangerous while synflood, but it is
  478. * bad justification for our negligence 8)
  479. * To be honest, we are not able to make either
  480. * of the variants now. --ANK
  481. */
  482. }
  483. }
  484. }
  485. BUG_TRAP(!lopt->qlen);
  486. kfree(lopt);
  487. while ((req = acc_req) != NULL) {
  488. struct sock *child = req->sk;
  489. acc_req = req->dl_next;
  490. local_bh_disable();
  491. bh_lock_sock(child);
  492. BUG_TRAP(!sock_owned_by_user(child));
  493. sock_hold(child);
  494. tcp_disconnect(child, O_NONBLOCK);
  495. sock_orphan(child);
  496. atomic_inc(&tcp_orphan_count);
  497. tcp_destroy_sock(child);
  498. bh_unlock_sock(child);
  499. local_bh_enable();
  500. sock_put(child);
  501. sk_acceptq_removed(sk);
  502. __reqsk_free(req);
  503. }
  504. BUG_TRAP(!sk->sk_ack_backlog);
  505. }
  506. static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb)
  507. {
  508. TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
  509. tp->pushed_seq = tp->write_seq;
  510. }
  511. static inline int forced_push(struct tcp_sock *tp)
  512. {
  513. return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1));
  514. }
  515. static inline void skb_entail(struct sock *sk, struct tcp_sock *tp,
  516. struct sk_buff *skb)
  517. {
  518. skb->csum = 0;
  519. TCP_SKB_CB(skb)->seq = tp->write_seq;
  520. TCP_SKB_CB(skb)->end_seq = tp->write_seq;
  521. TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
  522. TCP_SKB_CB(skb)->sacked = 0;
  523. skb_header_release(skb);
  524. __skb_queue_tail(&sk->sk_write_queue, skb);
  525. sk_charge_skb(sk, skb);
  526. if (!sk->sk_send_head)
  527. sk->sk_send_head = skb;
  528. else if (tp->nonagle&TCP_NAGLE_PUSH)
  529. tp->nonagle &= ~TCP_NAGLE_PUSH;
  530. }
  531. static inline void tcp_mark_urg(struct tcp_sock *tp, int flags,
  532. struct sk_buff *skb)
  533. {
  534. if (flags & MSG_OOB) {
  535. tp->urg_mode = 1;
  536. tp->snd_up = tp->write_seq;
  537. TCP_SKB_CB(skb)->sacked |= TCPCB_URG;
  538. }
  539. }
  540. static inline void tcp_push(struct sock *sk, struct tcp_sock *tp, int flags,
  541. int mss_now, int nonagle)
  542. {
  543. if (sk->sk_send_head) {
  544. struct sk_buff *skb = sk->sk_write_queue.prev;
  545. if (!(flags & MSG_MORE) || forced_push(tp))
  546. tcp_mark_push(tp, skb);
  547. tcp_mark_urg(tp, flags, skb);
  548. __tcp_push_pending_frames(sk, tp, mss_now,
  549. (flags & MSG_MORE) ? TCP_NAGLE_CORK : nonagle);
  550. }
  551. }
  552. static ssize_t do_tcp_sendpages(struct sock *sk, struct page **pages, int poffset,
  553. size_t psize, int flags)
  554. {
  555. struct tcp_sock *tp = tcp_sk(sk);
  556. int mss_now;
  557. int err;
  558. ssize_t copied;
  559. long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  560. /* Wait for a connection to finish. */
  561. if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))
  562. if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
  563. goto out_err;
  564. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  565. mss_now = tcp_current_mss(sk, !(flags&MSG_OOB));
  566. copied = 0;
  567. err = -EPIPE;
  568. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  569. goto do_error;
  570. while (psize > 0) {
  571. struct sk_buff *skb = sk->sk_write_queue.prev;
  572. struct page *page = pages[poffset / PAGE_SIZE];
  573. int copy, i, can_coalesce;
  574. int offset = poffset % PAGE_SIZE;
  575. int size = min_t(size_t, psize, PAGE_SIZE - offset);
  576. if (!sk->sk_send_head || (copy = mss_now - skb->len) <= 0) {
  577. new_segment:
  578. if (!sk_stream_memory_free(sk))
  579. goto wait_for_sndbuf;
  580. skb = sk_stream_alloc_pskb(sk, 0, 0,
  581. sk->sk_allocation);
  582. if (!skb)
  583. goto wait_for_memory;
  584. skb_entail(sk, tp, skb);
  585. copy = mss_now;
  586. }
  587. if (copy > size)
  588. copy = size;
  589. i = skb_shinfo(skb)->nr_frags;
  590. can_coalesce = skb_can_coalesce(skb, i, page, offset);
  591. if (!can_coalesce && i >= MAX_SKB_FRAGS) {
  592. tcp_mark_push(tp, skb);
  593. goto new_segment;
  594. }
  595. if (sk->sk_forward_alloc < copy &&
  596. !sk_stream_mem_schedule(sk, copy, 0))
  597. goto wait_for_memory;
  598. if (can_coalesce) {
  599. skb_shinfo(skb)->frags[i - 1].size += copy;
  600. } else {
  601. get_page(page);
  602. skb_fill_page_desc(skb, i, page, offset, copy);
  603. }
  604. skb->len += copy;
  605. skb->data_len += copy;
  606. skb->truesize += copy;
  607. sk->sk_wmem_queued += copy;
  608. sk->sk_forward_alloc -= copy;
  609. skb->ip_summed = CHECKSUM_HW;
  610. tp->write_seq += copy;
  611. TCP_SKB_CB(skb)->end_seq += copy;
  612. skb_shinfo(skb)->tso_segs = 0;
  613. if (!copied)
  614. TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_PSH;
  615. copied += copy;
  616. poffset += copy;
  617. if (!(psize -= copy))
  618. goto out;
  619. if (skb->len != mss_now || (flags & MSG_OOB))
  620. continue;
  621. if (forced_push(tp)) {
  622. tcp_mark_push(tp, skb);
  623. __tcp_push_pending_frames(sk, tp, mss_now, TCP_NAGLE_PUSH);
  624. } else if (skb == sk->sk_send_head)
  625. tcp_push_one(sk, mss_now);
  626. continue;
  627. wait_for_sndbuf:
  628. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  629. wait_for_memory:
  630. if (copied)
  631. tcp_push(sk, tp, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);
  632. if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
  633. goto do_error;
  634. mss_now = tcp_current_mss(sk, !(flags&MSG_OOB));
  635. }
  636. out:
  637. if (copied)
  638. tcp_push(sk, tp, flags, mss_now, tp->nonagle);
  639. return copied;
  640. do_error:
  641. if (copied)
  642. goto out;
  643. out_err:
  644. return sk_stream_error(sk, flags, err);
  645. }
  646. ssize_t tcp_sendpage(struct socket *sock, struct page *page, int offset,
  647. size_t size, int flags)
  648. {
  649. ssize_t res;
  650. struct sock *sk = sock->sk;
  651. #define TCP_ZC_CSUM_FLAGS (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)
  652. if (!(sk->sk_route_caps & NETIF_F_SG) ||
  653. !(sk->sk_route_caps & TCP_ZC_CSUM_FLAGS))
  654. return sock_no_sendpage(sock, page, offset, size, flags);
  655. #undef TCP_ZC_CSUM_FLAGS
  656. lock_sock(sk);
  657. TCP_CHECK_TIMER(sk);
  658. res = do_tcp_sendpages(sk, &page, offset, size, flags);
  659. TCP_CHECK_TIMER(sk);
  660. release_sock(sk);
  661. return res;
  662. }
  663. #define TCP_PAGE(sk) (sk->sk_sndmsg_page)
  664. #define TCP_OFF(sk) (sk->sk_sndmsg_off)
  665. static inline int select_size(struct sock *sk, struct tcp_sock *tp)
  666. {
  667. int tmp = tp->mss_cache_std;
  668. if (sk->sk_route_caps & NETIF_F_SG)
  669. tmp = 0;
  670. return tmp;
  671. }
  672. int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  673. size_t size)
  674. {
  675. struct iovec *iov;
  676. struct tcp_sock *tp = tcp_sk(sk);
  677. struct sk_buff *skb;
  678. int iovlen, flags;
  679. int mss_now;
  680. int err, copied;
  681. long timeo;
  682. lock_sock(sk);
  683. TCP_CHECK_TIMER(sk);
  684. flags = msg->msg_flags;
  685. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  686. /* Wait for a connection to finish. */
  687. if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))
  688. if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
  689. goto out_err;
  690. /* This should be in poll */
  691. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  692. mss_now = tcp_current_mss(sk, !(flags&MSG_OOB));
  693. /* Ok commence sending. */
  694. iovlen = msg->msg_iovlen;
  695. iov = msg->msg_iov;
  696. copied = 0;
  697. err = -EPIPE;
  698. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  699. goto do_error;
  700. while (--iovlen >= 0) {
  701. int seglen = iov->iov_len;
  702. unsigned char __user *from = iov->iov_base;
  703. iov++;
  704. while (seglen > 0) {
  705. int copy;
  706. skb = sk->sk_write_queue.prev;
  707. if (!sk->sk_send_head ||
  708. (copy = mss_now - skb->len) <= 0) {
  709. new_segment:
  710. /* Allocate new segment. If the interface is SG,
  711. * allocate skb fitting to single page.
  712. */
  713. if (!sk_stream_memory_free(sk))
  714. goto wait_for_sndbuf;
  715. skb = sk_stream_alloc_pskb(sk, select_size(sk, tp),
  716. 0, sk->sk_allocation);
  717. if (!skb)
  718. goto wait_for_memory;
  719. /*
  720. * Check whether we can use HW checksum.
  721. */
  722. if (sk->sk_route_caps &
  723. (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM |
  724. NETIF_F_HW_CSUM))
  725. skb->ip_summed = CHECKSUM_HW;
  726. skb_entail(sk, tp, skb);
  727. copy = mss_now;
  728. }
  729. /* Try to append data to the end of skb. */
  730. if (copy > seglen)
  731. copy = seglen;
  732. /* Where to copy to? */
  733. if (skb_tailroom(skb) > 0) {
  734. /* We have some space in skb head. Superb! */
  735. if (copy > skb_tailroom(skb))
  736. copy = skb_tailroom(skb);
  737. if ((err = skb_add_data(skb, from, copy)) != 0)
  738. goto do_fault;
  739. } else {
  740. int merge = 0;
  741. int i = skb_shinfo(skb)->nr_frags;
  742. struct page *page = TCP_PAGE(sk);
  743. int off = TCP_OFF(sk);
  744. if (skb_can_coalesce(skb, i, page, off) &&
  745. off != PAGE_SIZE) {
  746. /* We can extend the last page
  747. * fragment. */
  748. merge = 1;
  749. } else if (i == MAX_SKB_FRAGS ||
  750. (!i &&
  751. !(sk->sk_route_caps & NETIF_F_SG))) {
  752. /* Need to add new fragment and cannot
  753. * do this because interface is non-SG,
  754. * or because all the page slots are
  755. * busy. */
  756. tcp_mark_push(tp, skb);
  757. goto new_segment;
  758. } else if (page) {
  759. if (off == PAGE_SIZE) {
  760. put_page(page);
  761. TCP_PAGE(sk) = page = NULL;
  762. }
  763. }
  764. if (!page) {
  765. /* Allocate new cache page. */
  766. if (!(page = sk_stream_alloc_page(sk)))
  767. goto wait_for_memory;
  768. off = 0;
  769. }
  770. if (copy > PAGE_SIZE - off)
  771. copy = PAGE_SIZE - off;
  772. /* Time to copy data. We are close to
  773. * the end! */
  774. err = skb_copy_to_page(sk, from, skb, page,
  775. off, copy);
  776. if (err) {
  777. /* If this page was new, give it to the
  778. * socket so it does not get leaked.
  779. */
  780. if (!TCP_PAGE(sk)) {
  781. TCP_PAGE(sk) = page;
  782. TCP_OFF(sk) = 0;
  783. }
  784. goto do_error;
  785. }
  786. /* Update the skb. */
  787. if (merge) {
  788. skb_shinfo(skb)->frags[i - 1].size +=
  789. copy;
  790. } else {
  791. skb_fill_page_desc(skb, i, page, off, copy);
  792. if (TCP_PAGE(sk)) {
  793. get_page(page);
  794. } else if (off + copy < PAGE_SIZE) {
  795. get_page(page);
  796. TCP_PAGE(sk) = page;
  797. }
  798. }
  799. TCP_OFF(sk) = off + copy;
  800. }
  801. if (!copied)
  802. TCP_SKB_CB(skb)->flags &= ~TCPCB_FLAG_PSH;
  803. tp->write_seq += copy;
  804. TCP_SKB_CB(skb)->end_seq += copy;
  805. skb_shinfo(skb)->tso_segs = 0;
  806. from += copy;
  807. copied += copy;
  808. if ((seglen -= copy) == 0 && iovlen == 0)
  809. goto out;
  810. if (skb->len != mss_now || (flags & MSG_OOB))
  811. continue;
  812. if (forced_push(tp)) {
  813. tcp_mark_push(tp, skb);
  814. __tcp_push_pending_frames(sk, tp, mss_now, TCP_NAGLE_PUSH);
  815. } else if (skb == sk->sk_send_head)
  816. tcp_push_one(sk, mss_now);
  817. continue;
  818. wait_for_sndbuf:
  819. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  820. wait_for_memory:
  821. if (copied)
  822. tcp_push(sk, tp, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH);
  823. if ((err = sk_stream_wait_memory(sk, &timeo)) != 0)
  824. goto do_error;
  825. mss_now = tcp_current_mss(sk, !(flags&MSG_OOB));
  826. }
  827. }
  828. out:
  829. if (copied)
  830. tcp_push(sk, tp, flags, mss_now, tp->nonagle);
  831. TCP_CHECK_TIMER(sk);
  832. release_sock(sk);
  833. return copied;
  834. do_fault:
  835. if (!skb->len) {
  836. if (sk->sk_send_head == skb)
  837. sk->sk_send_head = NULL;
  838. __skb_unlink(skb, skb->list);
  839. sk_stream_free_skb(sk, skb);
  840. }
  841. do_error:
  842. if (copied)
  843. goto out;
  844. out_err:
  845. err = sk_stream_error(sk, flags, err);
  846. TCP_CHECK_TIMER(sk);
  847. release_sock(sk);
  848. return err;
  849. }
  850. /*
  851. * Handle reading urgent data. BSD has very simple semantics for
  852. * this, no blocking and very strange errors 8)
  853. */
  854. static int tcp_recv_urg(struct sock *sk, long timeo,
  855. struct msghdr *msg, int len, int flags,
  856. int *addr_len)
  857. {
  858. struct tcp_sock *tp = tcp_sk(sk);
  859. /* No URG data to read. */
  860. if (sock_flag(sk, SOCK_URGINLINE) || !tp->urg_data ||
  861. tp->urg_data == TCP_URG_READ)
  862. return -EINVAL; /* Yes this is right ! */
  863. if (sk->sk_state == TCP_CLOSE && !sock_flag(sk, SOCK_DONE))
  864. return -ENOTCONN;
  865. if (tp->urg_data & TCP_URG_VALID) {
  866. int err = 0;
  867. char c = tp->urg_data;
  868. if (!(flags & MSG_PEEK))
  869. tp->urg_data = TCP_URG_READ;
  870. /* Read urgent data. */
  871. msg->msg_flags |= MSG_OOB;
  872. if (len > 0) {
  873. if (!(flags & MSG_TRUNC))
  874. err = memcpy_toiovec(msg->msg_iov, &c, 1);
  875. len = 1;
  876. } else
  877. msg->msg_flags |= MSG_TRUNC;
  878. return err ? -EFAULT : len;
  879. }
  880. if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
  881. return 0;
  882. /* Fixed the recv(..., MSG_OOB) behaviour. BSD docs and
  883. * the available implementations agree in this case:
  884. * this call should never block, independent of the
  885. * blocking state of the socket.
  886. * Mike <pall@rz.uni-karlsruhe.de>
  887. */
  888. return -EAGAIN;
  889. }
  890. /* Clean up the receive buffer for full frames taken by the user,
  891. * then send an ACK if necessary. COPIED is the number of bytes
  892. * tcp_recvmsg has given to the user so far, it speeds up the
  893. * calculation of whether or not we must ACK for the sake of
  894. * a window update.
  895. */
  896. static void cleanup_rbuf(struct sock *sk, int copied)
  897. {
  898. struct tcp_sock *tp = tcp_sk(sk);
  899. int time_to_ack = 0;
  900. #if TCP_DEBUG
  901. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  902. BUG_TRAP(!skb || before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq));
  903. #endif
  904. if (tcp_ack_scheduled(tp)) {
  905. /* Delayed ACKs frequently hit locked sockets during bulk
  906. * receive. */
  907. if (tp->ack.blocked ||
  908. /* Once-per-two-segments ACK was not sent by tcp_input.c */
  909. tp->rcv_nxt - tp->rcv_wup > tp->ack.rcv_mss ||
  910. /*
  911. * If this read emptied read buffer, we send ACK, if
  912. * connection is not bidirectional, user drained
  913. * receive buffer and there was a small segment
  914. * in queue.
  915. */
  916. (copied > 0 && (tp->ack.pending & TCP_ACK_PUSHED) &&
  917. !tp->ack.pingpong && !atomic_read(&sk->sk_rmem_alloc)))
  918. time_to_ack = 1;
  919. }
  920. /* We send an ACK if we can now advertise a non-zero window
  921. * which has been raised "significantly".
  922. *
  923. * Even if window raised up to infinity, do not send window open ACK
  924. * in states, where we will not receive more. It is useless.
  925. */
  926. if (copied > 0 && !time_to_ack && !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  927. __u32 rcv_window_now = tcp_receive_window(tp);
  928. /* Optimize, __tcp_select_window() is not cheap. */
  929. if (2*rcv_window_now <= tp->window_clamp) {
  930. __u32 new_window = __tcp_select_window(sk);
  931. /* Send ACK now, if this read freed lots of space
  932. * in our buffer. Certainly, new_window is new window.
  933. * We can advertise it now, if it is not less than current one.
  934. * "Lots" means "at least twice" here.
  935. */
  936. if (new_window && new_window >= 2 * rcv_window_now)
  937. time_to_ack = 1;
  938. }
  939. }
  940. if (time_to_ack)
  941. tcp_send_ack(sk);
  942. }
  943. static void tcp_prequeue_process(struct sock *sk)
  944. {
  945. struct sk_buff *skb;
  946. struct tcp_sock *tp = tcp_sk(sk);
  947. NET_ADD_STATS_USER(LINUX_MIB_TCPPREQUEUED, skb_queue_len(&tp->ucopy.prequeue));
  948. /* RX process wants to run with disabled BHs, though it is not
  949. * necessary */
  950. local_bh_disable();
  951. while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
  952. sk->sk_backlog_rcv(sk, skb);
  953. local_bh_enable();
  954. /* Clear memory counter. */
  955. tp->ucopy.memory = 0;
  956. }
  957. static inline struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
  958. {
  959. struct sk_buff *skb;
  960. u32 offset;
  961. skb_queue_walk(&sk->sk_receive_queue, skb) {
  962. offset = seq - TCP_SKB_CB(skb)->seq;
  963. if (skb->h.th->syn)
  964. offset--;
  965. if (offset < skb->len || skb->h.th->fin) {
  966. *off = offset;
  967. return skb;
  968. }
  969. }
  970. return NULL;
  971. }
  972. /*
  973. * This routine provides an alternative to tcp_recvmsg() for routines
  974. * that would like to handle copying from skbuffs directly in 'sendfile'
  975. * fashion.
  976. * Note:
  977. * - It is assumed that the socket was locked by the caller.
  978. * - The routine does not block.
  979. * - At present, there is no support for reading OOB data
  980. * or for 'peeking' the socket using this routine
  981. * (although both would be easy to implement).
  982. */
  983. int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  984. sk_read_actor_t recv_actor)
  985. {
  986. struct sk_buff *skb;
  987. struct tcp_sock *tp = tcp_sk(sk);
  988. u32 seq = tp->copied_seq;
  989. u32 offset;
  990. int copied = 0;
  991. if (sk->sk_state == TCP_LISTEN)
  992. return -ENOTCONN;
  993. while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
  994. if (offset < skb->len) {
  995. size_t used, len;
  996. len = skb->len - offset;
  997. /* Stop reading if we hit a patch of urgent data */
  998. if (tp->urg_data) {
  999. u32 urg_offset = tp->urg_seq - seq;
  1000. if (urg_offset < len)
  1001. len = urg_offset;
  1002. if (!len)
  1003. break;
  1004. }
  1005. used = recv_actor(desc, skb, offset, len);
  1006. if (used <= len) {
  1007. seq += used;
  1008. copied += used;
  1009. offset += used;
  1010. }
  1011. if (offset != skb->len)
  1012. break;
  1013. }
  1014. if (skb->h.th->fin) {
  1015. sk_eat_skb(sk, skb);
  1016. ++seq;
  1017. break;
  1018. }
  1019. sk_eat_skb(sk, skb);
  1020. if (!desc->count)
  1021. break;
  1022. }
  1023. tp->copied_seq = seq;
  1024. tcp_rcv_space_adjust(sk);
  1025. /* Clean up data we have read: This will do ACK frames. */
  1026. if (copied)
  1027. cleanup_rbuf(sk, copied);
  1028. return copied;
  1029. }
  1030. /*
  1031. * This routine copies from a sock struct into the user buffer.
  1032. *
  1033. * Technical note: in 2.3 we work on _locked_ socket, so that
  1034. * tricks with *seq access order and skb->users are not required.
  1035. * Probably, code can be easily improved even more.
  1036. */
  1037. int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  1038. size_t len, int nonblock, int flags, int *addr_len)
  1039. {
  1040. struct tcp_sock *tp = tcp_sk(sk);
  1041. int copied = 0;
  1042. u32 peek_seq;
  1043. u32 *seq;
  1044. unsigned long used;
  1045. int err;
  1046. int target; /* Read at least this many bytes */
  1047. long timeo;
  1048. struct task_struct *user_recv = NULL;
  1049. lock_sock(sk);
  1050. TCP_CHECK_TIMER(sk);
  1051. err = -ENOTCONN;
  1052. if (sk->sk_state == TCP_LISTEN)
  1053. goto out;
  1054. timeo = sock_rcvtimeo(sk, nonblock);
  1055. /* Urgent data needs to be handled specially. */
  1056. if (flags & MSG_OOB)
  1057. goto recv_urg;
  1058. seq = &tp->copied_seq;
  1059. if (flags & MSG_PEEK) {
  1060. peek_seq = tp->copied_seq;
  1061. seq = &peek_seq;
  1062. }
  1063. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1064. do {
  1065. struct sk_buff *skb;
  1066. u32 offset;
  1067. /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */
  1068. if (tp->urg_data && tp->urg_seq == *seq) {
  1069. if (copied)
  1070. break;
  1071. if (signal_pending(current)) {
  1072. copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
  1073. break;
  1074. }
  1075. }
  1076. /* Next get a buffer. */
  1077. skb = skb_peek(&sk->sk_receive_queue);
  1078. do {
  1079. if (!skb)
  1080. break;
  1081. /* Now that we have two receive queues this
  1082. * shouldn't happen.
  1083. */
  1084. if (before(*seq, TCP_SKB_CB(skb)->seq)) {
  1085. printk(KERN_INFO "recvmsg bug: copied %X "
  1086. "seq %X\n", *seq, TCP_SKB_CB(skb)->seq);
  1087. break;
  1088. }
  1089. offset = *seq - TCP_SKB_CB(skb)->seq;
  1090. if (skb->h.th->syn)
  1091. offset--;
  1092. if (offset < skb->len)
  1093. goto found_ok_skb;
  1094. if (skb->h.th->fin)
  1095. goto found_fin_ok;
  1096. BUG_TRAP(flags & MSG_PEEK);
  1097. skb = skb->next;
  1098. } while (skb != (struct sk_buff *)&sk->sk_receive_queue);
  1099. /* Well, if we have backlog, try to process it now yet. */
  1100. if (copied >= target && !sk->sk_backlog.tail)
  1101. break;
  1102. if (copied) {
  1103. if (sk->sk_err ||
  1104. sk->sk_state == TCP_CLOSE ||
  1105. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  1106. !timeo ||
  1107. signal_pending(current) ||
  1108. (flags & MSG_PEEK))
  1109. break;
  1110. } else {
  1111. if (sock_flag(sk, SOCK_DONE))
  1112. break;
  1113. if (sk->sk_err) {
  1114. copied = sock_error(sk);
  1115. break;
  1116. }
  1117. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1118. break;
  1119. if (sk->sk_state == TCP_CLOSE) {
  1120. if (!sock_flag(sk, SOCK_DONE)) {
  1121. /* This occurs when user tries to read
  1122. * from never connected socket.
  1123. */
  1124. copied = -ENOTCONN;
  1125. break;
  1126. }
  1127. break;
  1128. }
  1129. if (!timeo) {
  1130. copied = -EAGAIN;
  1131. break;
  1132. }
  1133. if (signal_pending(current)) {
  1134. copied = sock_intr_errno(timeo);
  1135. break;
  1136. }
  1137. }
  1138. cleanup_rbuf(sk, copied);
  1139. if (!sysctl_tcp_low_latency && tp->ucopy.task == user_recv) {
  1140. /* Install new reader */
  1141. if (!user_recv && !(flags & (MSG_TRUNC | MSG_PEEK))) {
  1142. user_recv = current;
  1143. tp->ucopy.task = user_recv;
  1144. tp->ucopy.iov = msg->msg_iov;
  1145. }
  1146. tp->ucopy.len = len;
  1147. BUG_TRAP(tp->copied_seq == tp->rcv_nxt ||
  1148. (flags & (MSG_PEEK | MSG_TRUNC)));
  1149. /* Ugly... If prequeue is not empty, we have to
  1150. * process it before releasing socket, otherwise
  1151. * order will be broken at second iteration.
  1152. * More elegant solution is required!!!
  1153. *
  1154. * Look: we have the following (pseudo)queues:
  1155. *
  1156. * 1. packets in flight
  1157. * 2. backlog
  1158. * 3. prequeue
  1159. * 4. receive_queue
  1160. *
  1161. * Each queue can be processed only if the next ones
  1162. * are empty. At this point we have empty receive_queue.
  1163. * But prequeue _can_ be not empty after 2nd iteration,
  1164. * when we jumped to start of loop because backlog
  1165. * processing added something to receive_queue.
  1166. * We cannot release_sock(), because backlog contains
  1167. * packets arrived _after_ prequeued ones.
  1168. *
  1169. * Shortly, algorithm is clear --- to process all
  1170. * the queues in order. We could make it more directly,
  1171. * requeueing packets from backlog to prequeue, if
  1172. * is not empty. It is more elegant, but eats cycles,
  1173. * unfortunately.
  1174. */
  1175. if (skb_queue_len(&tp->ucopy.prequeue))
  1176. goto do_prequeue;
  1177. /* __ Set realtime policy in scheduler __ */
  1178. }
  1179. if (copied >= target) {
  1180. /* Do not sleep, just process backlog. */
  1181. release_sock(sk);
  1182. lock_sock(sk);
  1183. } else
  1184. sk_wait_data(sk, &timeo);
  1185. if (user_recv) {
  1186. int chunk;
  1187. /* __ Restore normal policy in scheduler __ */
  1188. if ((chunk = len - tp->ucopy.len) != 0) {
  1189. NET_ADD_STATS_USER(LINUX_MIB_TCPDIRECTCOPYFROMBACKLOG, chunk);
  1190. len -= chunk;
  1191. copied += chunk;
  1192. }
  1193. if (tp->rcv_nxt == tp->copied_seq &&
  1194. skb_queue_len(&tp->ucopy.prequeue)) {
  1195. do_prequeue:
  1196. tcp_prequeue_process(sk);
  1197. if ((chunk = len - tp->ucopy.len) != 0) {
  1198. NET_ADD_STATS_USER(LINUX_MIB_TCPDIRECTCOPYFROMPREQUEUE, chunk);
  1199. len -= chunk;
  1200. copied += chunk;
  1201. }
  1202. }
  1203. }
  1204. if ((flags & MSG_PEEK) && peek_seq != tp->copied_seq) {
  1205. if (net_ratelimit())
  1206. printk(KERN_DEBUG "TCP(%s:%d): Application bug, race in MSG_PEEK.\n",
  1207. current->comm, current->pid);
  1208. peek_seq = tp->copied_seq;
  1209. }
  1210. continue;
  1211. found_ok_skb:
  1212. /* Ok so how much can we use? */
  1213. used = skb->len - offset;
  1214. if (len < used)
  1215. used = len;
  1216. /* Do we have urgent data here? */
  1217. if (tp->urg_data) {
  1218. u32 urg_offset = tp->urg_seq - *seq;
  1219. if (urg_offset < used) {
  1220. if (!urg_offset) {
  1221. if (!sock_flag(sk, SOCK_URGINLINE)) {
  1222. ++*seq;
  1223. offset++;
  1224. used--;
  1225. if (!used)
  1226. goto skip_copy;
  1227. }
  1228. } else
  1229. used = urg_offset;
  1230. }
  1231. }
  1232. if (!(flags & MSG_TRUNC)) {
  1233. err = skb_copy_datagram_iovec(skb, offset,
  1234. msg->msg_iov, used);
  1235. if (err) {
  1236. /* Exception. Bailout! */
  1237. if (!copied)
  1238. copied = -EFAULT;
  1239. break;
  1240. }
  1241. }
  1242. *seq += used;
  1243. copied += used;
  1244. len -= used;
  1245. tcp_rcv_space_adjust(sk);
  1246. skip_copy:
  1247. if (tp->urg_data && after(tp->copied_seq, tp->urg_seq)) {
  1248. tp->urg_data = 0;
  1249. tcp_fast_path_check(sk, tp);
  1250. }
  1251. if (used + offset < skb->len)
  1252. continue;
  1253. if (skb->h.th->fin)
  1254. goto found_fin_ok;
  1255. if (!(flags & MSG_PEEK))
  1256. sk_eat_skb(sk, skb);
  1257. continue;
  1258. found_fin_ok:
  1259. /* Process the FIN. */
  1260. ++*seq;
  1261. if (!(flags & MSG_PEEK))
  1262. sk_eat_skb(sk, skb);
  1263. break;
  1264. } while (len > 0);
  1265. if (user_recv) {
  1266. if (skb_queue_len(&tp->ucopy.prequeue)) {
  1267. int chunk;
  1268. tp->ucopy.len = copied > 0 ? len : 0;
  1269. tcp_prequeue_process(sk);
  1270. if (copied > 0 && (chunk = len - tp->ucopy.len) != 0) {
  1271. NET_ADD_STATS_USER(LINUX_MIB_TCPDIRECTCOPYFROMPREQUEUE, chunk);
  1272. len -= chunk;
  1273. copied += chunk;
  1274. }
  1275. }
  1276. tp->ucopy.task = NULL;
  1277. tp->ucopy.len = 0;
  1278. }
  1279. /* According to UNIX98, msg_name/msg_namelen are ignored
  1280. * on connected socket. I was just happy when found this 8) --ANK
  1281. */
  1282. /* Clean up data we have read: This will do ACK frames. */
  1283. cleanup_rbuf(sk, copied);
  1284. TCP_CHECK_TIMER(sk);
  1285. release_sock(sk);
  1286. return copied;
  1287. out:
  1288. TCP_CHECK_TIMER(sk);
  1289. release_sock(sk);
  1290. return err;
  1291. recv_urg:
  1292. err = tcp_recv_urg(sk, timeo, msg, len, flags, addr_len);
  1293. goto out;
  1294. }
  1295. /*
  1296. * State processing on a close. This implements the state shift for
  1297. * sending our FIN frame. Note that we only send a FIN for some
  1298. * states. A shutdown() may have already sent the FIN, or we may be
  1299. * closed.
  1300. */
  1301. static unsigned char new_state[16] = {
  1302. /* current state: new state: action: */
  1303. /* (Invalid) */ TCP_CLOSE,
  1304. /* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  1305. /* TCP_SYN_SENT */ TCP_CLOSE,
  1306. /* TCP_SYN_RECV */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
  1307. /* TCP_FIN_WAIT1 */ TCP_FIN_WAIT1,
  1308. /* TCP_FIN_WAIT2 */ TCP_FIN_WAIT2,
  1309. /* TCP_TIME_WAIT */ TCP_CLOSE,
  1310. /* TCP_CLOSE */ TCP_CLOSE,
  1311. /* TCP_CLOSE_WAIT */ TCP_LAST_ACK | TCP_ACTION_FIN,
  1312. /* TCP_LAST_ACK */ TCP_LAST_ACK,
  1313. /* TCP_LISTEN */ TCP_CLOSE,
  1314. /* TCP_CLOSING */ TCP_CLOSING,
  1315. };
  1316. static int tcp_close_state(struct sock *sk)
  1317. {
  1318. int next = (int)new_state[sk->sk_state];
  1319. int ns = next & TCP_STATE_MASK;
  1320. tcp_set_state(sk, ns);
  1321. return next & TCP_ACTION_FIN;
  1322. }
  1323. /*
  1324. * Shutdown the sending side of a connection. Much like close except
  1325. * that we don't receive shut down or set_sock_flag(sk, SOCK_DEAD).
  1326. */
  1327. void tcp_shutdown(struct sock *sk, int how)
  1328. {
  1329. /* We need to grab some memory, and put together a FIN,
  1330. * and then put it into the queue to be sent.
  1331. * Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
  1332. */
  1333. if (!(how & SEND_SHUTDOWN))
  1334. return;
  1335. /* If we've already sent a FIN, or it's a closed state, skip this. */
  1336. if ((1 << sk->sk_state) &
  1337. (TCPF_ESTABLISHED | TCPF_SYN_SENT |
  1338. TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
  1339. /* Clear out any half completed packets. FIN if needed. */
  1340. if (tcp_close_state(sk))
  1341. tcp_send_fin(sk);
  1342. }
  1343. }
  1344. /*
  1345. * At this point, there should be no process reference to this
  1346. * socket, and thus no user references at all. Therefore we
  1347. * can assume the socket waitqueue is inactive and nobody will
  1348. * try to jump onto it.
  1349. */
  1350. void tcp_destroy_sock(struct sock *sk)
  1351. {
  1352. BUG_TRAP(sk->sk_state == TCP_CLOSE);
  1353. BUG_TRAP(sock_flag(sk, SOCK_DEAD));
  1354. /* It cannot be in hash table! */
  1355. BUG_TRAP(sk_unhashed(sk));
  1356. /* If it has not 0 inet_sk(sk)->num, it must be bound */
  1357. BUG_TRAP(!inet_sk(sk)->num || tcp_sk(sk)->bind_hash);
  1358. sk->sk_prot->destroy(sk);
  1359. sk_stream_kill_queues(sk);
  1360. xfrm_sk_free_policy(sk);
  1361. #ifdef INET_REFCNT_DEBUG
  1362. if (atomic_read(&sk->sk_refcnt) != 1) {
  1363. printk(KERN_DEBUG "Destruction TCP %p delayed, c=%d\n",
  1364. sk, atomic_read(&sk->sk_refcnt));
  1365. }
  1366. #endif
  1367. atomic_dec(&tcp_orphan_count);
  1368. sock_put(sk);
  1369. }
  1370. void tcp_close(struct sock *sk, long timeout)
  1371. {
  1372. struct sk_buff *skb;
  1373. int data_was_unread = 0;
  1374. lock_sock(sk);
  1375. sk->sk_shutdown = SHUTDOWN_MASK;
  1376. if (sk->sk_state == TCP_LISTEN) {
  1377. tcp_set_state(sk, TCP_CLOSE);
  1378. /* Special case. */
  1379. tcp_listen_stop(sk);
  1380. goto adjudge_to_death;
  1381. }
  1382. /* We need to flush the recv. buffs. We do this only on the
  1383. * descriptor close, not protocol-sourced closes, because the
  1384. * reader process may not have drained the data yet!
  1385. */
  1386. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  1387. u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq -
  1388. skb->h.th->fin;
  1389. data_was_unread += len;
  1390. __kfree_skb(skb);
  1391. }
  1392. sk_stream_mem_reclaim(sk);
  1393. /* As outlined in draft-ietf-tcpimpl-prob-03.txt, section
  1394. * 3.10, we send a RST here because data was lost. To
  1395. * witness the awful effects of the old behavior of always
  1396. * doing a FIN, run an older 2.1.x kernel or 2.0.x, start
  1397. * a bulk GET in an FTP client, suspend the process, wait
  1398. * for the client to advertise a zero window, then kill -9
  1399. * the FTP client, wheee... Note: timeout is always zero
  1400. * in such a case.
  1401. */
  1402. if (data_was_unread) {
  1403. /* Unread data was tossed, zap the connection. */
  1404. NET_INC_STATS_USER(LINUX_MIB_TCPABORTONCLOSE);
  1405. tcp_set_state(sk, TCP_CLOSE);
  1406. tcp_send_active_reset(sk, GFP_KERNEL);
  1407. } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  1408. /* Check zero linger _after_ checking for unread data. */
  1409. sk->sk_prot->disconnect(sk, 0);
  1410. NET_INC_STATS_USER(LINUX_MIB_TCPABORTONDATA);
  1411. } else if (tcp_close_state(sk)) {
  1412. /* We FIN if the application ate all the data before
  1413. * zapping the connection.
  1414. */
  1415. /* RED-PEN. Formally speaking, we have broken TCP state
  1416. * machine. State transitions:
  1417. *
  1418. * TCP_ESTABLISHED -> TCP_FIN_WAIT1
  1419. * TCP_SYN_RECV -> TCP_FIN_WAIT1 (forget it, it's impossible)
  1420. * TCP_CLOSE_WAIT -> TCP_LAST_ACK
  1421. *
  1422. * are legal only when FIN has been sent (i.e. in window),
  1423. * rather than queued out of window. Purists blame.
  1424. *
  1425. * F.e. "RFC state" is ESTABLISHED,
  1426. * if Linux state is FIN-WAIT-1, but FIN is still not sent.
  1427. *
  1428. * The visible declinations are that sometimes
  1429. * we enter time-wait state, when it is not required really
  1430. * (harmless), do not send active resets, when they are
  1431. * required by specs (TCP_ESTABLISHED, TCP_CLOSE_WAIT, when
  1432. * they look as CLOSING or LAST_ACK for Linux)
  1433. * Probably, I missed some more holelets.
  1434. * --ANK
  1435. */
  1436. tcp_send_fin(sk);
  1437. }
  1438. sk_stream_wait_close(sk, timeout);
  1439. adjudge_to_death:
  1440. /* It is the last release_sock in its life. It will remove backlog. */
  1441. release_sock(sk);
  1442. /* Now socket is owned by kernel and we acquire BH lock
  1443. to finish close. No need to check for user refs.
  1444. */
  1445. local_bh_disable();
  1446. bh_lock_sock(sk);
  1447. BUG_TRAP(!sock_owned_by_user(sk));
  1448. sock_hold(sk);
  1449. sock_orphan(sk);
  1450. /* This is a (useful) BSD violating of the RFC. There is a
  1451. * problem with TCP as specified in that the other end could
  1452. * keep a socket open forever with no application left this end.
  1453. * We use a 3 minute timeout (about the same as BSD) then kill
  1454. * our end. If they send after that then tough - BUT: long enough
  1455. * that we won't make the old 4*rto = almost no time - whoops
  1456. * reset mistake.
  1457. *
  1458. * Nope, it was not mistake. It is really desired behaviour
  1459. * f.e. on http servers, when such sockets are useless, but
  1460. * consume significant resources. Let's do it with special
  1461. * linger2 option. --ANK
  1462. */
  1463. if (sk->sk_state == TCP_FIN_WAIT2) {
  1464. struct tcp_sock *tp = tcp_sk(sk);
  1465. if (tp->linger2 < 0) {
  1466. tcp_set_state(sk, TCP_CLOSE);
  1467. tcp_send_active_reset(sk, GFP_ATOMIC);
  1468. NET_INC_STATS_BH(LINUX_MIB_TCPABORTONLINGER);
  1469. } else {
  1470. int tmo = tcp_fin_time(tp);
  1471. if (tmo > TCP_TIMEWAIT_LEN) {
  1472. tcp_reset_keepalive_timer(sk, tcp_fin_time(tp));
  1473. } else {
  1474. atomic_inc(&tcp_orphan_count);
  1475. tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
  1476. goto out;
  1477. }
  1478. }
  1479. }
  1480. if (sk->sk_state != TCP_CLOSE) {
  1481. sk_stream_mem_reclaim(sk);
  1482. if (atomic_read(&tcp_orphan_count) > sysctl_tcp_max_orphans ||
  1483. (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
  1484. atomic_read(&tcp_memory_allocated) > sysctl_tcp_mem[2])) {
  1485. if (net_ratelimit())
  1486. printk(KERN_INFO "TCP: too many of orphaned "
  1487. "sockets\n");
  1488. tcp_set_state(sk, TCP_CLOSE);
  1489. tcp_send_active_reset(sk, GFP_ATOMIC);
  1490. NET_INC_STATS_BH(LINUX_MIB_TCPABORTONMEMORY);
  1491. }
  1492. }
  1493. atomic_inc(&tcp_orphan_count);
  1494. if (sk->sk_state == TCP_CLOSE)
  1495. tcp_destroy_sock(sk);
  1496. /* Otherwise, socket is reprieved until protocol close. */
  1497. out:
  1498. bh_unlock_sock(sk);
  1499. local_bh_enable();
  1500. sock_put(sk);
  1501. }
  1502. /* These states need RST on ABORT according to RFC793 */
  1503. static inline int tcp_need_reset(int state)
  1504. {
  1505. return (1 << state) &
  1506. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_FIN_WAIT1 |
  1507. TCPF_FIN_WAIT2 | TCPF_SYN_RECV);
  1508. }
  1509. int tcp_disconnect(struct sock *sk, int flags)
  1510. {
  1511. struct inet_sock *inet = inet_sk(sk);
  1512. struct tcp_sock *tp = tcp_sk(sk);
  1513. int err = 0;
  1514. int old_state = sk->sk_state;
  1515. if (old_state != TCP_CLOSE)
  1516. tcp_set_state(sk, TCP_CLOSE);
  1517. /* ABORT function of RFC793 */
  1518. if (old_state == TCP_LISTEN) {
  1519. tcp_listen_stop(sk);
  1520. } else if (tcp_need_reset(old_state) ||
  1521. (tp->snd_nxt != tp->write_seq &&
  1522. (1 << old_state) & (TCPF_CLOSING | TCPF_LAST_ACK))) {
  1523. /* The last check adjusts for discrepance of Linux wrt. RFC
  1524. * states
  1525. */
  1526. tcp_send_active_reset(sk, gfp_any());
  1527. sk->sk_err = ECONNRESET;
  1528. } else if (old_state == TCP_SYN_SENT)
  1529. sk->sk_err = ECONNRESET;
  1530. tcp_clear_xmit_timers(sk);
  1531. __skb_queue_purge(&sk->sk_receive_queue);
  1532. sk_stream_writequeue_purge(sk);
  1533. __skb_queue_purge(&tp->out_of_order_queue);
  1534. inet->dport = 0;
  1535. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  1536. inet_reset_saddr(sk);
  1537. sk->sk_shutdown = 0;
  1538. sock_reset_flag(sk, SOCK_DONE);
  1539. tp->srtt = 0;
  1540. if ((tp->write_seq += tp->max_window + 2) == 0)
  1541. tp->write_seq = 1;
  1542. tp->backoff = 0;
  1543. tp->snd_cwnd = 2;
  1544. tp->probes_out = 0;
  1545. tp->packets_out = 0;
  1546. tp->snd_ssthresh = 0x7fffffff;
  1547. tp->snd_cwnd_cnt = 0;
  1548. tcp_set_ca_state(tp, TCP_CA_Open);
  1549. tcp_clear_retrans(tp);
  1550. tcp_delack_init(tp);
  1551. sk->sk_send_head = NULL;
  1552. tp->rx_opt.saw_tstamp = 0;
  1553. tcp_sack_reset(&tp->rx_opt);
  1554. __sk_dst_reset(sk);
  1555. BUG_TRAP(!inet->num || tp->bind_hash);
  1556. sk->sk_error_report(sk);
  1557. return err;
  1558. }
  1559. /*
  1560. * Wait for an incoming connection, avoid race
  1561. * conditions. This must be called with the socket locked.
  1562. */
  1563. static int wait_for_connect(struct sock *sk, long timeo)
  1564. {
  1565. struct tcp_sock *tp = tcp_sk(sk);
  1566. DEFINE_WAIT(wait);
  1567. int err;
  1568. /*
  1569. * True wake-one mechanism for incoming connections: only
  1570. * one process gets woken up, not the 'whole herd'.
  1571. * Since we do not 'race & poll' for established sockets
  1572. * anymore, the common case will execute the loop only once.
  1573. *
  1574. * Subtle issue: "add_wait_queue_exclusive()" will be added
  1575. * after any current non-exclusive waiters, and we know that
  1576. * it will always _stay_ after any new non-exclusive waiters
  1577. * because all non-exclusive waiters are added at the
  1578. * beginning of the wait-queue. As such, it's ok to "drop"
  1579. * our exclusiveness temporarily when we get woken up without
  1580. * having to remove and re-insert us on the wait queue.
  1581. */
  1582. for (;;) {
  1583. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  1584. TASK_INTERRUPTIBLE);
  1585. release_sock(sk);
  1586. if (reqsk_queue_empty(&tp->accept_queue))
  1587. timeo = schedule_timeout(timeo);
  1588. lock_sock(sk);
  1589. err = 0;
  1590. if (!reqsk_queue_empty(&tp->accept_queue))
  1591. break;
  1592. err = -EINVAL;
  1593. if (sk->sk_state != TCP_LISTEN)
  1594. break;
  1595. err = sock_intr_errno(timeo);
  1596. if (signal_pending(current))
  1597. break;
  1598. err = -EAGAIN;
  1599. if (!timeo)
  1600. break;
  1601. }
  1602. finish_wait(sk->sk_sleep, &wait);
  1603. return err;
  1604. }
  1605. /*
  1606. * This will accept the next outstanding connection.
  1607. */
  1608. struct sock *tcp_accept(struct sock *sk, int flags, int *err)
  1609. {
  1610. struct tcp_sock *tp = tcp_sk(sk);
  1611. struct sock *newsk;
  1612. int error;
  1613. lock_sock(sk);
  1614. /* We need to make sure that this socket is listening,
  1615. * and that it has something pending.
  1616. */
  1617. error = -EINVAL;
  1618. if (sk->sk_state != TCP_LISTEN)
  1619. goto out_err;
  1620. /* Find already established connection */
  1621. if (reqsk_queue_empty(&tp->accept_queue)) {
  1622. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1623. /* If this is a non blocking socket don't sleep */
  1624. error = -EAGAIN;
  1625. if (!timeo)
  1626. goto out_err;
  1627. error = wait_for_connect(sk, timeo);
  1628. if (error)
  1629. goto out_err;
  1630. }
  1631. newsk = reqsk_queue_get_child(&tp->accept_queue, sk);
  1632. BUG_TRAP(newsk->sk_state != TCP_SYN_RECV);
  1633. out:
  1634. release_sock(sk);
  1635. return newsk;
  1636. out_err:
  1637. newsk = NULL;
  1638. *err = error;
  1639. goto out;
  1640. }
  1641. /*
  1642. * Socket option code for TCP.
  1643. */
  1644. int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
  1645. int optlen)
  1646. {
  1647. struct tcp_sock *tp = tcp_sk(sk);
  1648. int val;
  1649. int err = 0;
  1650. if (level != SOL_TCP)
  1651. return tp->af_specific->setsockopt(sk, level, optname,
  1652. optval, optlen);
  1653. /* This is a string value all the others are int's */
  1654. if (optname == TCP_CONGESTION) {
  1655. char name[TCP_CA_NAME_MAX];
  1656. if (optlen < 1)
  1657. return -EINVAL;
  1658. val = strncpy_from_user(name, optval,
  1659. min(TCP_CA_NAME_MAX-1, optlen));
  1660. if (val < 0)
  1661. return -EFAULT;
  1662. name[val] = 0;
  1663. lock_sock(sk);
  1664. err = tcp_set_congestion_control(tp, name);
  1665. release_sock(sk);
  1666. return err;
  1667. }
  1668. if (optlen < sizeof(int))
  1669. return -EINVAL;
  1670. if (get_user(val, (int __user *)optval))
  1671. return -EFAULT;
  1672. lock_sock(sk);
  1673. switch (optname) {
  1674. case TCP_MAXSEG:
  1675. /* Values greater than interface MTU won't take effect. However
  1676. * at the point when this call is done we typically don't yet
  1677. * know which interface is going to be used */
  1678. if (val < 8 || val > MAX_TCP_WINDOW) {
  1679. err = -EINVAL;
  1680. break;
  1681. }
  1682. tp->rx_opt.user_mss = val;
  1683. break;
  1684. case TCP_NODELAY:
  1685. if (val) {
  1686. /* TCP_NODELAY is weaker than TCP_CORK, so that
  1687. * this option on corked socket is remembered, but
  1688. * it is not activated until cork is cleared.
  1689. *
  1690. * However, when TCP_NODELAY is set we make
  1691. * an explicit push, which overrides even TCP_CORK
  1692. * for currently queued segments.
  1693. */
  1694. tp->nonagle |= TCP_NAGLE_OFF|TCP_NAGLE_PUSH;
  1695. tcp_push_pending_frames(sk, tp);
  1696. } else {
  1697. tp->nonagle &= ~TCP_NAGLE_OFF;
  1698. }
  1699. break;
  1700. case TCP_CORK:
  1701. /* When set indicates to always queue non-full frames.
  1702. * Later the user clears this option and we transmit
  1703. * any pending partial frames in the queue. This is
  1704. * meant to be used alongside sendfile() to get properly
  1705. * filled frames when the user (for example) must write
  1706. * out headers with a write() call first and then use
  1707. * sendfile to send out the data parts.
  1708. *
  1709. * TCP_CORK can be set together with TCP_NODELAY and it is
  1710. * stronger than TCP_NODELAY.
  1711. */
  1712. if (val) {
  1713. tp->nonagle |= TCP_NAGLE_CORK;
  1714. } else {
  1715. tp->nonagle &= ~TCP_NAGLE_CORK;
  1716. if (tp->nonagle&TCP_NAGLE_OFF)
  1717. tp->nonagle |= TCP_NAGLE_PUSH;
  1718. tcp_push_pending_frames(sk, tp);
  1719. }
  1720. break;
  1721. case TCP_KEEPIDLE:
  1722. if (val < 1 || val > MAX_TCP_KEEPIDLE)
  1723. err = -EINVAL;
  1724. else {
  1725. tp->keepalive_time = val * HZ;
  1726. if (sock_flag(sk, SOCK_KEEPOPEN) &&
  1727. !((1 << sk->sk_state) &
  1728. (TCPF_CLOSE | TCPF_LISTEN))) {
  1729. __u32 elapsed = tcp_time_stamp - tp->rcv_tstamp;
  1730. if (tp->keepalive_time > elapsed)
  1731. elapsed = tp->keepalive_time - elapsed;
  1732. else
  1733. elapsed = 0;
  1734. tcp_reset_keepalive_timer(sk, elapsed);
  1735. }
  1736. }
  1737. break;
  1738. case TCP_KEEPINTVL:
  1739. if (val < 1 || val > MAX_TCP_KEEPINTVL)
  1740. err = -EINVAL;
  1741. else
  1742. tp->keepalive_intvl = val * HZ;
  1743. break;
  1744. case TCP_KEEPCNT:
  1745. if (val < 1 || val > MAX_TCP_KEEPCNT)
  1746. err = -EINVAL;
  1747. else
  1748. tp->keepalive_probes = val;
  1749. break;
  1750. case TCP_SYNCNT:
  1751. if (val < 1 || val > MAX_TCP_SYNCNT)
  1752. err = -EINVAL;
  1753. else
  1754. tp->syn_retries = val;
  1755. break;
  1756. case TCP_LINGER2:
  1757. if (val < 0)
  1758. tp->linger2 = -1;
  1759. else if (val > sysctl_tcp_fin_timeout / HZ)
  1760. tp->linger2 = 0;
  1761. else
  1762. tp->linger2 = val * HZ;
  1763. break;
  1764. case TCP_DEFER_ACCEPT:
  1765. tp->defer_accept = 0;
  1766. if (val > 0) {
  1767. /* Translate value in seconds to number of
  1768. * retransmits */
  1769. while (tp->defer_accept < 32 &&
  1770. val > ((TCP_TIMEOUT_INIT / HZ) <<
  1771. tp->defer_accept))
  1772. tp->defer_accept++;
  1773. tp->defer_accept++;
  1774. }
  1775. break;
  1776. case TCP_WINDOW_CLAMP:
  1777. if (!val) {
  1778. if (sk->sk_state != TCP_CLOSE) {
  1779. err = -EINVAL;
  1780. break;
  1781. }
  1782. tp->window_clamp = 0;
  1783. } else
  1784. tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
  1785. SOCK_MIN_RCVBUF / 2 : val;
  1786. break;
  1787. case TCP_QUICKACK:
  1788. if (!val) {
  1789. tp->ack.pingpong = 1;
  1790. } else {
  1791. tp->ack.pingpong = 0;
  1792. if ((1 << sk->sk_state) &
  1793. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
  1794. tcp_ack_scheduled(tp)) {
  1795. tp->ack.pending |= TCP_ACK_PUSHED;
  1796. cleanup_rbuf(sk, 1);
  1797. if (!(val & 1))
  1798. tp->ack.pingpong = 1;
  1799. }
  1800. }
  1801. break;
  1802. default:
  1803. err = -ENOPROTOOPT;
  1804. break;
  1805. };
  1806. release_sock(sk);
  1807. return err;
  1808. }
  1809. /* Return information about state of tcp endpoint in API format. */
  1810. void tcp_get_info(struct sock *sk, struct tcp_info *info)
  1811. {
  1812. struct tcp_sock *tp = tcp_sk(sk);
  1813. u32 now = tcp_time_stamp;
  1814. memset(info, 0, sizeof(*info));
  1815. info->tcpi_state = sk->sk_state;
  1816. info->tcpi_ca_state = tp->ca_state;
  1817. info->tcpi_retransmits = tp->retransmits;
  1818. info->tcpi_probes = tp->probes_out;
  1819. info->tcpi_backoff = tp->backoff;
  1820. if (tp->rx_opt.tstamp_ok)
  1821. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  1822. if (tp->rx_opt.sack_ok)
  1823. info->tcpi_options |= TCPI_OPT_SACK;
  1824. if (tp->rx_opt.wscale_ok) {
  1825. info->tcpi_options |= TCPI_OPT_WSCALE;
  1826. info->tcpi_snd_wscale = tp->rx_opt.snd_wscale;
  1827. info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale;
  1828. }
  1829. if (tp->ecn_flags&TCP_ECN_OK)
  1830. info->tcpi_options |= TCPI_OPT_ECN;
  1831. info->tcpi_rto = jiffies_to_usecs(tp->rto);
  1832. info->tcpi_ato = jiffies_to_usecs(tp->ack.ato);
  1833. info->tcpi_snd_mss = tp->mss_cache_std;
  1834. info->tcpi_rcv_mss = tp->ack.rcv_mss;
  1835. info->tcpi_unacked = tp->packets_out;
  1836. info->tcpi_sacked = tp->sacked_out;
  1837. info->tcpi_lost = tp->lost_out;
  1838. info->tcpi_retrans = tp->retrans_out;
  1839. info->tcpi_fackets = tp->fackets_out;
  1840. info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime);
  1841. info->tcpi_last_data_recv = jiffies_to_msecs(now - tp->ack.lrcvtime);
  1842. info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp);
  1843. info->tcpi_pmtu = tp->pmtu_cookie;
  1844. info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
  1845. info->tcpi_rtt = jiffies_to_usecs(tp->srtt)>>3;
  1846. info->tcpi_rttvar = jiffies_to_usecs(tp->mdev)>>2;
  1847. info->tcpi_snd_ssthresh = tp->snd_ssthresh;
  1848. info->tcpi_snd_cwnd = tp->snd_cwnd;
  1849. info->tcpi_advmss = tp->advmss;
  1850. info->tcpi_reordering = tp->reordering;
  1851. info->tcpi_rcv_rtt = jiffies_to_usecs(tp->rcv_rtt_est.rtt)>>3;
  1852. info->tcpi_rcv_space = tp->rcvq_space.space;
  1853. info->tcpi_total_retrans = tp->total_retrans;
  1854. }
  1855. EXPORT_SYMBOL_GPL(tcp_get_info);
  1856. int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
  1857. int __user *optlen)
  1858. {
  1859. struct tcp_sock *tp = tcp_sk(sk);
  1860. int val, len;
  1861. if (level != SOL_TCP)
  1862. return tp->af_specific->getsockopt(sk, level, optname,
  1863. optval, optlen);
  1864. if (get_user(len, optlen))
  1865. return -EFAULT;
  1866. len = min_t(unsigned int, len, sizeof(int));
  1867. if (len < 0)
  1868. return -EINVAL;
  1869. switch (optname) {
  1870. case TCP_MAXSEG:
  1871. val = tp->mss_cache_std;
  1872. if (!val && ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  1873. val = tp->rx_opt.user_mss;
  1874. break;
  1875. case TCP_NODELAY:
  1876. val = !!(tp->nonagle&TCP_NAGLE_OFF);
  1877. break;
  1878. case TCP_CORK:
  1879. val = !!(tp->nonagle&TCP_NAGLE_CORK);
  1880. break;
  1881. case TCP_KEEPIDLE:
  1882. val = (tp->keepalive_time ? : sysctl_tcp_keepalive_time) / HZ;
  1883. break;
  1884. case TCP_KEEPINTVL:
  1885. val = (tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl) / HZ;
  1886. break;
  1887. case TCP_KEEPCNT:
  1888. val = tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
  1889. break;
  1890. case TCP_SYNCNT:
  1891. val = tp->syn_retries ? : sysctl_tcp_syn_retries;
  1892. break;
  1893. case TCP_LINGER2:
  1894. val = tp->linger2;
  1895. if (val >= 0)
  1896. val = (val ? : sysctl_tcp_fin_timeout) / HZ;
  1897. break;
  1898. case TCP_DEFER_ACCEPT:
  1899. val = !tp->defer_accept ? 0 : ((TCP_TIMEOUT_INIT / HZ) <<
  1900. (tp->defer_accept - 1));
  1901. break;
  1902. case TCP_WINDOW_CLAMP:
  1903. val = tp->window_clamp;
  1904. break;
  1905. case TCP_INFO: {
  1906. struct tcp_info info;
  1907. if (get_user(len, optlen))
  1908. return -EFAULT;
  1909. tcp_get_info(sk, &info);
  1910. len = min_t(unsigned int, len, sizeof(info));
  1911. if (put_user(len, optlen))
  1912. return -EFAULT;
  1913. if (copy_to_user(optval, &info, len))
  1914. return -EFAULT;
  1915. return 0;
  1916. }
  1917. case TCP_QUICKACK:
  1918. val = !tp->ack.pingpong;
  1919. break;
  1920. case TCP_CONGESTION:
  1921. if (get_user(len, optlen))
  1922. return -EFAULT;
  1923. len = min_t(unsigned int, len, TCP_CA_NAME_MAX);
  1924. if (put_user(len, optlen))
  1925. return -EFAULT;
  1926. if (copy_to_user(optval, tp->ca_ops->name, len))
  1927. return -EFAULT;
  1928. return 0;
  1929. default:
  1930. return -ENOPROTOOPT;
  1931. };
  1932. if (put_user(len, optlen))
  1933. return -EFAULT;
  1934. if (copy_to_user(optval, &val, len))
  1935. return -EFAULT;
  1936. return 0;
  1937. }
  1938. extern void __skb_cb_too_small_for_tcp(int, int);
  1939. extern struct tcp_congestion_ops tcp_reno;
  1940. static __initdata unsigned long thash_entries;
  1941. static int __init set_thash_entries(char *str)
  1942. {
  1943. if (!str)
  1944. return 0;
  1945. thash_entries = simple_strtoul(str, &str, 0);
  1946. return 1;
  1947. }
  1948. __setup("thash_entries=", set_thash_entries);
  1949. void __init tcp_init(void)
  1950. {
  1951. struct sk_buff *skb = NULL;
  1952. int order, i;
  1953. if (sizeof(struct tcp_skb_cb) > sizeof(skb->cb))
  1954. __skb_cb_too_small_for_tcp(sizeof(struct tcp_skb_cb),
  1955. sizeof(skb->cb));
  1956. tcp_bucket_cachep = kmem_cache_create("tcp_bind_bucket",
  1957. sizeof(struct tcp_bind_bucket),
  1958. 0, SLAB_HWCACHE_ALIGN,
  1959. NULL, NULL);
  1960. if (!tcp_bucket_cachep)
  1961. panic("tcp_init: Cannot alloc tcp_bind_bucket cache.");
  1962. tcp_timewait_cachep = kmem_cache_create("tcp_tw_bucket",
  1963. sizeof(struct tcp_tw_bucket),
  1964. 0, SLAB_HWCACHE_ALIGN,
  1965. NULL, NULL);
  1966. if (!tcp_timewait_cachep)
  1967. panic("tcp_init: Cannot alloc tcp_tw_bucket cache.");
  1968. /* Size and allocate the main established and bind bucket
  1969. * hash tables.
  1970. *
  1971. * The methodology is similar to that of the buffer cache.
  1972. */
  1973. tcp_ehash = (struct tcp_ehash_bucket *)
  1974. alloc_large_system_hash("TCP established",
  1975. sizeof(struct tcp_ehash_bucket),
  1976. thash_entries,
  1977. (num_physpages >= 128 * 1024) ?
  1978. (25 - PAGE_SHIFT) :
  1979. (27 - PAGE_SHIFT),
  1980. HASH_HIGHMEM,
  1981. &tcp_ehash_size,
  1982. NULL,
  1983. 0);
  1984. tcp_ehash_size = (1 << tcp_ehash_size) >> 1;
  1985. for (i = 0; i < (tcp_ehash_size << 1); i++) {
  1986. rwlock_init(&tcp_ehash[i].lock);
  1987. INIT_HLIST_HEAD(&tcp_ehash[i].chain);
  1988. }
  1989. tcp_bhash = (struct tcp_bind_hashbucket *)
  1990. alloc_large_system_hash("TCP bind",
  1991. sizeof(struct tcp_bind_hashbucket),
  1992. tcp_ehash_size,
  1993. (num_physpages >= 128 * 1024) ?
  1994. (25 - PAGE_SHIFT) :
  1995. (27 - PAGE_SHIFT),
  1996. HASH_HIGHMEM,
  1997. &tcp_bhash_size,
  1998. NULL,
  1999. 64 * 1024);
  2000. tcp_bhash_size = 1 << tcp_bhash_size;
  2001. for (i = 0; i < tcp_bhash_size; i++) {
  2002. spin_lock_init(&tcp_bhash[i].lock);
  2003. INIT_HLIST_HEAD(&tcp_bhash[i].chain);
  2004. }
  2005. /* Try to be a bit smarter and adjust defaults depending
  2006. * on available memory.
  2007. */
  2008. for (order = 0; ((1 << order) << PAGE_SHIFT) <
  2009. (tcp_bhash_size * sizeof(struct tcp_bind_hashbucket));
  2010. order++)
  2011. ;
  2012. if (order >= 4) {
  2013. sysctl_local_port_range[0] = 32768;
  2014. sysctl_local_port_range[1] = 61000;
  2015. sysctl_tcp_max_tw_buckets = 180000;
  2016. sysctl_tcp_max_orphans = 4096 << (order - 4);
  2017. sysctl_max_syn_backlog = 1024;
  2018. } else if (order < 3) {
  2019. sysctl_local_port_range[0] = 1024 * (3 - order);
  2020. sysctl_tcp_max_tw_buckets >>= (3 - order);
  2021. sysctl_tcp_max_orphans >>= (3 - order);
  2022. sysctl_max_syn_backlog = 128;
  2023. }
  2024. tcp_port_rover = sysctl_local_port_range[0] - 1;
  2025. sysctl_tcp_mem[0] = 768 << order;
  2026. sysctl_tcp_mem[1] = 1024 << order;
  2027. sysctl_tcp_mem[2] = 1536 << order;
  2028. if (order < 3) {
  2029. sysctl_tcp_wmem[2] = 64 * 1024;
  2030. sysctl_tcp_rmem[0] = PAGE_SIZE;
  2031. sysctl_tcp_rmem[1] = 43689;
  2032. sysctl_tcp_rmem[2] = 2 * 43689;
  2033. }
  2034. printk(KERN_INFO "TCP: Hash tables configured "
  2035. "(established %d bind %d)\n",
  2036. tcp_ehash_size << 1, tcp_bhash_size);
  2037. tcp_register_congestion_control(&tcp_reno);
  2038. }
  2039. EXPORT_SYMBOL(tcp_accept);
  2040. EXPORT_SYMBOL(tcp_close);
  2041. EXPORT_SYMBOL(tcp_destroy_sock);
  2042. EXPORT_SYMBOL(tcp_disconnect);
  2043. EXPORT_SYMBOL(tcp_getsockopt);
  2044. EXPORT_SYMBOL(tcp_ioctl);
  2045. EXPORT_SYMBOL(tcp_poll);
  2046. EXPORT_SYMBOL(tcp_read_sock);
  2047. EXPORT_SYMBOL(tcp_recvmsg);
  2048. EXPORT_SYMBOL(tcp_sendmsg);
  2049. EXPORT_SYMBOL(tcp_sendpage);
  2050. EXPORT_SYMBOL(tcp_setsockopt);
  2051. EXPORT_SYMBOL(tcp_shutdown);
  2052. EXPORT_SYMBOL(tcp_statistics);
  2053. EXPORT_SYMBOL(tcp_timewait_cachep);