tcp_ipv4.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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_ipv4.c,v 1.240 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * IPv4 specific functions
  11. *
  12. *
  13. * code split from:
  14. * linux/ipv4/tcp.c
  15. * linux/ipv4/tcp_input.c
  16. * linux/ipv4/tcp_output.c
  17. *
  18. * See tcp.c for author information
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. */
  25. /*
  26. * Changes:
  27. * David S. Miller : New socket lookup architecture.
  28. * This code is dedicated to John Dyson.
  29. * David S. Miller : Change semantics of established hash,
  30. * half is devoted to TIME_WAIT sockets
  31. * and the rest go in the other half.
  32. * Andi Kleen : Add support for syncookies and fixed
  33. * some bugs: ip options weren't passed to
  34. * the TCP layer, missed a check for an
  35. * ACK bit.
  36. * Andi Kleen : Implemented fast path mtu discovery.
  37. * Fixed many serious bugs in the
  38. * request_sock handling and moved
  39. * most of it into the af independent code.
  40. * Added tail drop and some other bugfixes.
  41. * Added new listen sematics.
  42. * Mike McLagan : Routing by source
  43. * Juan Jose Ciarlante: ip_dynaddr bits
  44. * Andi Kleen: various fixes.
  45. * Vitaly E. Lavrov : Transparent proxy revived after year
  46. * coma.
  47. * Andi Kleen : Fix new listen.
  48. * Andi Kleen : Fix accept error reporting.
  49. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  50. * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
  51. * a single port at the same time.
  52. */
  53. #include <linux/config.h>
  54. #include <linux/types.h>
  55. #include <linux/fcntl.h>
  56. #include <linux/module.h>
  57. #include <linux/random.h>
  58. #include <linux/cache.h>
  59. #include <linux/jhash.h>
  60. #include <linux/init.h>
  61. #include <linux/times.h>
  62. #include <net/icmp.h>
  63. #include <net/inet_hashtables.h>
  64. #include <net/tcp.h>
  65. #include <net/ipv6.h>
  66. #include <net/inet_common.h>
  67. #include <net/xfrm.h>
  68. #include <linux/inet.h>
  69. #include <linux/ipv6.h>
  70. #include <linux/stddef.h>
  71. #include <linux/proc_fs.h>
  72. #include <linux/seq_file.h>
  73. extern int sysctl_ip_dynaddr;
  74. int sysctl_tcp_tw_reuse;
  75. int sysctl_tcp_low_latency;
  76. /* Check TCP sequence numbers in ICMP packets. */
  77. #define ICMP_MIN_LENGTH 8
  78. /* Socket used for sending RSTs */
  79. static struct socket *tcp_socket;
  80. void tcp_v4_send_check(struct sock *sk, struct tcphdr *th, int len,
  81. struct sk_buff *skb);
  82. struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
  83. .lhash_lock = RW_LOCK_UNLOCKED,
  84. .lhash_users = ATOMIC_INIT(0),
  85. .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
  86. .portalloc_lock = SPIN_LOCK_UNLOCKED,
  87. .port_rover = 1024 - 1,
  88. };
  89. /*
  90. * This array holds the first and last local port number.
  91. * For high-usage systems, use sysctl to change this to
  92. * 32768-61000
  93. */
  94. int sysctl_local_port_range[2] = { 1024, 4999 };
  95. static inline int tcp_bind_conflict(struct sock *sk, struct inet_bind_bucket *tb)
  96. {
  97. const u32 sk_rcv_saddr = tcp_v4_rcv_saddr(sk);
  98. struct sock *sk2;
  99. struct hlist_node *node;
  100. int reuse = sk->sk_reuse;
  101. sk_for_each_bound(sk2, node, &tb->owners) {
  102. if (sk != sk2 &&
  103. !tcp_v6_ipv6only(sk2) &&
  104. (!sk->sk_bound_dev_if ||
  105. !sk2->sk_bound_dev_if ||
  106. sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
  107. if (!reuse || !sk2->sk_reuse ||
  108. sk2->sk_state == TCP_LISTEN) {
  109. const u32 sk2_rcv_saddr = tcp_v4_rcv_saddr(sk2);
  110. if (!sk2_rcv_saddr || !sk_rcv_saddr ||
  111. sk2_rcv_saddr == sk_rcv_saddr)
  112. break;
  113. }
  114. }
  115. }
  116. return node != NULL;
  117. }
  118. /* Obtain a reference to a local port for the given sock,
  119. * if snum is zero it means select any available local port.
  120. */
  121. static int tcp_v4_get_port(struct sock *sk, unsigned short snum)
  122. {
  123. struct inet_bind_hashbucket *head;
  124. struct hlist_node *node;
  125. struct inet_bind_bucket *tb;
  126. int ret;
  127. local_bh_disable();
  128. if (!snum) {
  129. int low = sysctl_local_port_range[0];
  130. int high = sysctl_local_port_range[1];
  131. int remaining = (high - low) + 1;
  132. int rover;
  133. spin_lock(&tcp_hashinfo.portalloc_lock);
  134. if (tcp_hashinfo.port_rover < low)
  135. rover = low;
  136. else
  137. rover = tcp_hashinfo.port_rover;
  138. do {
  139. rover++;
  140. if (rover > high)
  141. rover = low;
  142. head = &tcp_hashinfo.bhash[inet_bhashfn(rover, tcp_hashinfo.bhash_size)];
  143. spin_lock(&head->lock);
  144. inet_bind_bucket_for_each(tb, node, &head->chain)
  145. if (tb->port == rover)
  146. goto next;
  147. break;
  148. next:
  149. spin_unlock(&head->lock);
  150. } while (--remaining > 0);
  151. tcp_hashinfo.port_rover = rover;
  152. spin_unlock(&tcp_hashinfo.portalloc_lock);
  153. /* Exhausted local port range during search? It is not
  154. * possible for us to be holding one of the bind hash
  155. * locks if this test triggers, because if 'remaining'
  156. * drops to zero, we broke out of the do/while loop at
  157. * the top level, not from the 'break;' statement.
  158. */
  159. ret = 1;
  160. if (unlikely(remaining <= 0))
  161. goto fail;
  162. /* OK, here is the one we will use. HEAD is
  163. * non-NULL and we hold it's mutex.
  164. */
  165. snum = rover;
  166. } else {
  167. head = &tcp_hashinfo.bhash[inet_bhashfn(snum, tcp_hashinfo.bhash_size)];
  168. spin_lock(&head->lock);
  169. inet_bind_bucket_for_each(tb, node, &head->chain)
  170. if (tb->port == snum)
  171. goto tb_found;
  172. }
  173. tb = NULL;
  174. goto tb_not_found;
  175. tb_found:
  176. if (!hlist_empty(&tb->owners)) {
  177. if (sk->sk_reuse > 1)
  178. goto success;
  179. if (tb->fastreuse > 0 &&
  180. sk->sk_reuse && sk->sk_state != TCP_LISTEN) {
  181. goto success;
  182. } else {
  183. ret = 1;
  184. if (tcp_bind_conflict(sk, tb))
  185. goto fail_unlock;
  186. }
  187. }
  188. tb_not_found:
  189. ret = 1;
  190. if (!tb && (tb = inet_bind_bucket_create(tcp_hashinfo.bind_bucket_cachep, head, snum)) == NULL)
  191. goto fail_unlock;
  192. if (hlist_empty(&tb->owners)) {
  193. if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
  194. tb->fastreuse = 1;
  195. else
  196. tb->fastreuse = 0;
  197. } else if (tb->fastreuse &&
  198. (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
  199. tb->fastreuse = 0;
  200. success:
  201. if (!inet_sk(sk)->bind_hash)
  202. inet_bind_hash(sk, tb, snum);
  203. BUG_TRAP(inet_sk(sk)->bind_hash == tb);
  204. ret = 0;
  205. fail_unlock:
  206. spin_unlock(&head->lock);
  207. fail:
  208. local_bh_enable();
  209. return ret;
  210. }
  211. static void tcp_v4_hash(struct sock *sk)
  212. {
  213. if (sk->sk_state != TCP_CLOSE) {
  214. local_bh_disable();
  215. __inet_hash(&tcp_hashinfo, sk, 1);
  216. local_bh_enable();
  217. }
  218. }
  219. void tcp_unhash(struct sock *sk)
  220. {
  221. rwlock_t *lock;
  222. if (sk_unhashed(sk))
  223. goto ende;
  224. if (sk->sk_state == TCP_LISTEN) {
  225. local_bh_disable();
  226. inet_listen_wlock(&tcp_hashinfo);
  227. lock = &tcp_hashinfo.lhash_lock;
  228. } else {
  229. struct inet_ehash_bucket *head = &tcp_hashinfo.ehash[sk->sk_hashent];
  230. lock = &head->lock;
  231. write_lock_bh(&head->lock);
  232. }
  233. if (__sk_del_node_init(sk))
  234. sock_prot_dec_use(sk->sk_prot);
  235. write_unlock_bh(lock);
  236. ende:
  237. if (sk->sk_state == TCP_LISTEN)
  238. wake_up(&tcp_hashinfo.lhash_wait);
  239. }
  240. /* Don't inline this cruft. Here are some nice properties to
  241. * exploit here. The BSD API does not allow a listening TCP
  242. * to specify the remote port nor the remote address for the
  243. * connection. So always assume those are both wildcarded
  244. * during the search since they can never be otherwise.
  245. */
  246. static struct sock *__tcp_v4_lookup_listener(struct hlist_head *head,
  247. const u32 daddr,
  248. const unsigned short hnum,
  249. const int dif)
  250. {
  251. struct sock *result = NULL, *sk;
  252. struct hlist_node *node;
  253. int score, hiscore;
  254. hiscore=-1;
  255. sk_for_each(sk, node, head) {
  256. struct inet_sock *inet = inet_sk(sk);
  257. if (inet->num == hnum && !ipv6_only_sock(sk)) {
  258. __u32 rcv_saddr = inet->rcv_saddr;
  259. score = (sk->sk_family == PF_INET ? 1 : 0);
  260. if (rcv_saddr) {
  261. if (rcv_saddr != daddr)
  262. continue;
  263. score+=2;
  264. }
  265. if (sk->sk_bound_dev_if) {
  266. if (sk->sk_bound_dev_if != dif)
  267. continue;
  268. score+=2;
  269. }
  270. if (score == 5)
  271. return sk;
  272. if (score > hiscore) {
  273. hiscore = score;
  274. result = sk;
  275. }
  276. }
  277. }
  278. return result;
  279. }
  280. /* Optimize the common listener case. */
  281. static inline struct sock *tcp_v4_lookup_listener(const u32 daddr,
  282. const unsigned short hnum,
  283. const int dif)
  284. {
  285. struct sock *sk = NULL;
  286. struct hlist_head *head;
  287. read_lock(&tcp_hashinfo.lhash_lock);
  288. head = &tcp_hashinfo.listening_hash[inet_lhashfn(hnum)];
  289. if (!hlist_empty(head)) {
  290. struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
  291. if (inet->num == hnum && !sk->sk_node.next &&
  292. (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
  293. (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
  294. !sk->sk_bound_dev_if)
  295. goto sherry_cache;
  296. sk = __tcp_v4_lookup_listener(head, daddr, hnum, dif);
  297. }
  298. if (sk) {
  299. sherry_cache:
  300. sock_hold(sk);
  301. }
  302. read_unlock(&tcp_hashinfo.lhash_lock);
  303. return sk;
  304. }
  305. /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  306. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  307. *
  308. * Local BH must be disabled here.
  309. */
  310. static inline struct sock *__tcp_v4_lookup_established(const u32 saddr,
  311. const u16 sport,
  312. const u32 daddr,
  313. const u16 hnum,
  314. const int dif)
  315. {
  316. struct inet_ehash_bucket *head;
  317. TCP_V4_ADDR_COOKIE(acookie, saddr, daddr)
  318. __u32 ports = TCP_COMBINED_PORTS(sport, hnum);
  319. struct sock *sk;
  320. struct hlist_node *node;
  321. /* Optimize here for direct hit, only listening connections can
  322. * have wildcards anyways.
  323. */
  324. const int hash = inet_ehashfn(daddr, hnum, saddr, sport, tcp_hashinfo.ehash_size);
  325. head = &tcp_hashinfo.ehash[hash];
  326. read_lock(&head->lock);
  327. sk_for_each(sk, node, &head->chain) {
  328. if (TCP_IPV4_MATCH(sk, acookie, saddr, daddr, ports, dif))
  329. goto hit; /* You sunk my battleship! */
  330. }
  331. /* Must check for a TIME_WAIT'er before going to listener hash. */
  332. sk_for_each(sk, node, &(head + tcp_hashinfo.ehash_size)->chain) {
  333. if (TCP_IPV4_TW_MATCH(sk, acookie, saddr, daddr, ports, dif))
  334. goto hit;
  335. }
  336. sk = NULL;
  337. out:
  338. read_unlock(&head->lock);
  339. return sk;
  340. hit:
  341. sock_hold(sk);
  342. goto out;
  343. }
  344. static inline struct sock *__tcp_v4_lookup(u32 saddr, u16 sport,
  345. u32 daddr, u16 hnum, int dif)
  346. {
  347. struct sock *sk = __tcp_v4_lookup_established(saddr, sport,
  348. daddr, hnum, dif);
  349. return sk ? : tcp_v4_lookup_listener(daddr, hnum, dif);
  350. }
  351. inline struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr,
  352. u16 dport, int dif)
  353. {
  354. struct sock *sk;
  355. local_bh_disable();
  356. sk = __tcp_v4_lookup(saddr, sport, daddr, ntohs(dport), dif);
  357. local_bh_enable();
  358. return sk;
  359. }
  360. EXPORT_SYMBOL_GPL(tcp_v4_lookup);
  361. static inline __u32 tcp_v4_init_sequence(struct sock *sk, struct sk_buff *skb)
  362. {
  363. return secure_tcp_sequence_number(skb->nh.iph->daddr,
  364. skb->nh.iph->saddr,
  365. skb->h.th->dest,
  366. skb->h.th->source);
  367. }
  368. /* called with local bh disabled */
  369. static int __tcp_v4_check_established(struct sock *sk, __u16 lport,
  370. struct tcp_tw_bucket **twp)
  371. {
  372. struct inet_sock *inet = inet_sk(sk);
  373. u32 daddr = inet->rcv_saddr;
  374. u32 saddr = inet->daddr;
  375. int dif = sk->sk_bound_dev_if;
  376. TCP_V4_ADDR_COOKIE(acookie, saddr, daddr)
  377. __u32 ports = TCP_COMBINED_PORTS(inet->dport, lport);
  378. const int hash = inet_ehashfn(daddr, lport, saddr, inet->dport, tcp_hashinfo.ehash_size);
  379. struct inet_ehash_bucket *head = &tcp_hashinfo.ehash[hash];
  380. struct sock *sk2;
  381. struct hlist_node *node;
  382. struct tcp_tw_bucket *tw;
  383. write_lock(&head->lock);
  384. /* Check TIME-WAIT sockets first. */
  385. sk_for_each(sk2, node, &(head + tcp_hashinfo.ehash_size)->chain) {
  386. tw = (struct tcp_tw_bucket *)sk2;
  387. if (TCP_IPV4_TW_MATCH(sk2, acookie, saddr, daddr, ports, dif)) {
  388. struct tcp_sock *tp = tcp_sk(sk);
  389. /* With PAWS, it is safe from the viewpoint
  390. of data integrity. Even without PAWS it
  391. is safe provided sequence spaces do not
  392. overlap i.e. at data rates <= 80Mbit/sec.
  393. Actually, the idea is close to VJ's one,
  394. only timestamp cache is held not per host,
  395. but per port pair and TW bucket is used
  396. as state holder.
  397. If TW bucket has been already destroyed we
  398. fall back to VJ's scheme and use initial
  399. timestamp retrieved from peer table.
  400. */
  401. if (tw->tw_ts_recent_stamp &&
  402. (!twp || (sysctl_tcp_tw_reuse &&
  403. xtime.tv_sec -
  404. tw->tw_ts_recent_stamp > 1))) {
  405. if ((tp->write_seq =
  406. tw->tw_snd_nxt + 65535 + 2) == 0)
  407. tp->write_seq = 1;
  408. tp->rx_opt.ts_recent = tw->tw_ts_recent;
  409. tp->rx_opt.ts_recent_stamp = tw->tw_ts_recent_stamp;
  410. sock_hold(sk2);
  411. goto unique;
  412. } else
  413. goto not_unique;
  414. }
  415. }
  416. tw = NULL;
  417. /* And established part... */
  418. sk_for_each(sk2, node, &head->chain) {
  419. if (TCP_IPV4_MATCH(sk2, acookie, saddr, daddr, ports, dif))
  420. goto not_unique;
  421. }
  422. unique:
  423. /* Must record num and sport now. Otherwise we will see
  424. * in hash table socket with a funny identity. */
  425. inet->num = lport;
  426. inet->sport = htons(lport);
  427. sk->sk_hashent = hash;
  428. BUG_TRAP(sk_unhashed(sk));
  429. __sk_add_node(sk, &head->chain);
  430. sock_prot_inc_use(sk->sk_prot);
  431. write_unlock(&head->lock);
  432. if (twp) {
  433. *twp = tw;
  434. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  435. } else if (tw) {
  436. /* Silly. Should hash-dance instead... */
  437. tcp_tw_deschedule(tw);
  438. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  439. tcp_tw_put(tw);
  440. }
  441. return 0;
  442. not_unique:
  443. write_unlock(&head->lock);
  444. return -EADDRNOTAVAIL;
  445. }
  446. static inline u32 connect_port_offset(const struct sock *sk)
  447. {
  448. const struct inet_sock *inet = inet_sk(sk);
  449. return secure_tcp_port_ephemeral(inet->rcv_saddr, inet->daddr,
  450. inet->dport);
  451. }
  452. /*
  453. * Bind a port for a connect operation and hash it.
  454. */
  455. static inline int tcp_v4_hash_connect(struct sock *sk)
  456. {
  457. const unsigned short snum = inet_sk(sk)->num;
  458. struct inet_bind_hashbucket *head;
  459. struct inet_bind_bucket *tb;
  460. int ret;
  461. if (!snum) {
  462. int low = sysctl_local_port_range[0];
  463. int high = sysctl_local_port_range[1];
  464. int range = high - low;
  465. int i;
  466. int port;
  467. static u32 hint;
  468. u32 offset = hint + connect_port_offset(sk);
  469. struct hlist_node *node;
  470. struct tcp_tw_bucket *tw = NULL;
  471. local_bh_disable();
  472. for (i = 1; i <= range; i++) {
  473. port = low + (i + offset) % range;
  474. head = &tcp_hashinfo.bhash[inet_bhashfn(port, tcp_hashinfo.bhash_size)];
  475. spin_lock(&head->lock);
  476. /* Does not bother with rcv_saddr checks,
  477. * because the established check is already
  478. * unique enough.
  479. */
  480. inet_bind_bucket_for_each(tb, node, &head->chain) {
  481. if (tb->port == port) {
  482. BUG_TRAP(!hlist_empty(&tb->owners));
  483. if (tb->fastreuse >= 0)
  484. goto next_port;
  485. if (!__tcp_v4_check_established(sk,
  486. port,
  487. &tw))
  488. goto ok;
  489. goto next_port;
  490. }
  491. }
  492. tb = inet_bind_bucket_create(tcp_hashinfo.bind_bucket_cachep, head, port);
  493. if (!tb) {
  494. spin_unlock(&head->lock);
  495. break;
  496. }
  497. tb->fastreuse = -1;
  498. goto ok;
  499. next_port:
  500. spin_unlock(&head->lock);
  501. }
  502. local_bh_enable();
  503. return -EADDRNOTAVAIL;
  504. ok:
  505. hint += i;
  506. /* Head lock still held and bh's disabled */
  507. inet_bind_hash(sk, tb, port);
  508. if (sk_unhashed(sk)) {
  509. inet_sk(sk)->sport = htons(port);
  510. __inet_hash(&tcp_hashinfo, sk, 0);
  511. }
  512. spin_unlock(&head->lock);
  513. if (tw) {
  514. tcp_tw_deschedule(tw);
  515. tcp_tw_put(tw);
  516. }
  517. ret = 0;
  518. goto out;
  519. }
  520. head = &tcp_hashinfo.bhash[inet_bhashfn(snum, tcp_hashinfo.bhash_size)];
  521. tb = inet_sk(sk)->bind_hash;
  522. spin_lock_bh(&head->lock);
  523. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  524. __inet_hash(&tcp_hashinfo, sk, 0);
  525. spin_unlock_bh(&head->lock);
  526. return 0;
  527. } else {
  528. spin_unlock(&head->lock);
  529. /* No definite answer... Walk to established hash table */
  530. ret = __tcp_v4_check_established(sk, snum, NULL);
  531. out:
  532. local_bh_enable();
  533. return ret;
  534. }
  535. }
  536. /* This will initiate an outgoing connection. */
  537. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  538. {
  539. struct inet_sock *inet = inet_sk(sk);
  540. struct tcp_sock *tp = tcp_sk(sk);
  541. struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  542. struct rtable *rt;
  543. u32 daddr, nexthop;
  544. int tmp;
  545. int err;
  546. if (addr_len < sizeof(struct sockaddr_in))
  547. return -EINVAL;
  548. if (usin->sin_family != AF_INET)
  549. return -EAFNOSUPPORT;
  550. nexthop = daddr = usin->sin_addr.s_addr;
  551. if (inet->opt && inet->opt->srr) {
  552. if (!daddr)
  553. return -EINVAL;
  554. nexthop = inet->opt->faddr;
  555. }
  556. tmp = ip_route_connect(&rt, nexthop, inet->saddr,
  557. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  558. IPPROTO_TCP,
  559. inet->sport, usin->sin_port, sk);
  560. if (tmp < 0)
  561. return tmp;
  562. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  563. ip_rt_put(rt);
  564. return -ENETUNREACH;
  565. }
  566. if (!inet->opt || !inet->opt->srr)
  567. daddr = rt->rt_dst;
  568. if (!inet->saddr)
  569. inet->saddr = rt->rt_src;
  570. inet->rcv_saddr = inet->saddr;
  571. if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
  572. /* Reset inherited state */
  573. tp->rx_opt.ts_recent = 0;
  574. tp->rx_opt.ts_recent_stamp = 0;
  575. tp->write_seq = 0;
  576. }
  577. if (sysctl_tcp_tw_recycle &&
  578. !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
  579. struct inet_peer *peer = rt_get_peer(rt);
  580. /* VJ's idea. We save last timestamp seen from
  581. * the destination in peer table, when entering state TIME-WAIT
  582. * and initialize rx_opt.ts_recent from it, when trying new connection.
  583. */
  584. if (peer && peer->tcp_ts_stamp + TCP_PAWS_MSL >= xtime.tv_sec) {
  585. tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
  586. tp->rx_opt.ts_recent = peer->tcp_ts;
  587. }
  588. }
  589. inet->dport = usin->sin_port;
  590. inet->daddr = daddr;
  591. tp->ext_header_len = 0;
  592. if (inet->opt)
  593. tp->ext_header_len = inet->opt->optlen;
  594. tp->rx_opt.mss_clamp = 536;
  595. /* Socket identity is still unknown (sport may be zero).
  596. * However we set state to SYN-SENT and not releasing socket
  597. * lock select source port, enter ourselves into the hash tables and
  598. * complete initialization after this.
  599. */
  600. tcp_set_state(sk, TCP_SYN_SENT);
  601. err = tcp_v4_hash_connect(sk);
  602. if (err)
  603. goto failure;
  604. err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
  605. if (err)
  606. goto failure;
  607. /* OK, now commit destination to socket. */
  608. sk_setup_caps(sk, &rt->u.dst);
  609. if (!tp->write_seq)
  610. tp->write_seq = secure_tcp_sequence_number(inet->saddr,
  611. inet->daddr,
  612. inet->sport,
  613. usin->sin_port);
  614. inet->id = tp->write_seq ^ jiffies;
  615. err = tcp_connect(sk);
  616. rt = NULL;
  617. if (err)
  618. goto failure;
  619. return 0;
  620. failure:
  621. /* This unhashes the socket and releases the local port, if necessary. */
  622. tcp_set_state(sk, TCP_CLOSE);
  623. ip_rt_put(rt);
  624. sk->sk_route_caps = 0;
  625. inet->dport = 0;
  626. return err;
  627. }
  628. static __inline__ int tcp_v4_iif(struct sk_buff *skb)
  629. {
  630. return ((struct rtable *)skb->dst)->rt_iif;
  631. }
  632. static __inline__ u32 tcp_v4_synq_hash(u32 raddr, u16 rport, u32 rnd)
  633. {
  634. return (jhash_2words(raddr, (u32) rport, rnd) & (TCP_SYNQ_HSIZE - 1));
  635. }
  636. static struct request_sock *tcp_v4_search_req(struct tcp_sock *tp,
  637. struct request_sock ***prevp,
  638. __u16 rport,
  639. __u32 raddr, __u32 laddr)
  640. {
  641. struct listen_sock *lopt = tp->accept_queue.listen_opt;
  642. struct request_sock *req, **prev;
  643. for (prev = &lopt->syn_table[tcp_v4_synq_hash(raddr, rport, lopt->hash_rnd)];
  644. (req = *prev) != NULL;
  645. prev = &req->dl_next) {
  646. const struct inet_request_sock *ireq = inet_rsk(req);
  647. if (ireq->rmt_port == rport &&
  648. ireq->rmt_addr == raddr &&
  649. ireq->loc_addr == laddr &&
  650. TCP_INET_FAMILY(req->rsk_ops->family)) {
  651. BUG_TRAP(!req->sk);
  652. *prevp = prev;
  653. break;
  654. }
  655. }
  656. return req;
  657. }
  658. static void tcp_v4_synq_add(struct sock *sk, struct request_sock *req)
  659. {
  660. struct tcp_sock *tp = tcp_sk(sk);
  661. struct listen_sock *lopt = tp->accept_queue.listen_opt;
  662. u32 h = tcp_v4_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port, lopt->hash_rnd);
  663. reqsk_queue_hash_req(&tp->accept_queue, h, req, TCP_TIMEOUT_INIT);
  664. tcp_synq_added(sk);
  665. }
  666. /*
  667. * This routine does path mtu discovery as defined in RFC1191.
  668. */
  669. static inline void do_pmtu_discovery(struct sock *sk, struct iphdr *iph,
  670. u32 mtu)
  671. {
  672. struct dst_entry *dst;
  673. struct inet_sock *inet = inet_sk(sk);
  674. struct tcp_sock *tp = tcp_sk(sk);
  675. /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
  676. * send out by Linux are always <576bytes so they should go through
  677. * unfragmented).
  678. */
  679. if (sk->sk_state == TCP_LISTEN)
  680. return;
  681. /* We don't check in the destentry if pmtu discovery is forbidden
  682. * on this route. We just assume that no packet_to_big packets
  683. * are send back when pmtu discovery is not active.
  684. * There is a small race when the user changes this flag in the
  685. * route, but I think that's acceptable.
  686. */
  687. if ((dst = __sk_dst_check(sk, 0)) == NULL)
  688. return;
  689. dst->ops->update_pmtu(dst, mtu);
  690. /* Something is about to be wrong... Remember soft error
  691. * for the case, if this connection will not able to recover.
  692. */
  693. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  694. sk->sk_err_soft = EMSGSIZE;
  695. mtu = dst_mtu(dst);
  696. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  697. tp->pmtu_cookie > mtu) {
  698. tcp_sync_mss(sk, mtu);
  699. /* Resend the TCP packet because it's
  700. * clear that the old packet has been
  701. * dropped. This is the new "fast" path mtu
  702. * discovery.
  703. */
  704. tcp_simple_retransmit(sk);
  705. } /* else let the usual retransmit timer handle it */
  706. }
  707. /*
  708. * This routine is called by the ICMP module when it gets some
  709. * sort of error condition. If err < 0 then the socket should
  710. * be closed and the error returned to the user. If err > 0
  711. * it's just the icmp type << 8 | icmp code. After adjustment
  712. * header points to the first 8 bytes of the tcp header. We need
  713. * to find the appropriate port.
  714. *
  715. * The locking strategy used here is very "optimistic". When
  716. * someone else accesses the socket the ICMP is just dropped
  717. * and for some paths there is no check at all.
  718. * A more general error queue to queue errors for later handling
  719. * is probably better.
  720. *
  721. */
  722. void tcp_v4_err(struct sk_buff *skb, u32 info)
  723. {
  724. struct iphdr *iph = (struct iphdr *)skb->data;
  725. struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
  726. struct tcp_sock *tp;
  727. struct inet_sock *inet;
  728. int type = skb->h.icmph->type;
  729. int code = skb->h.icmph->code;
  730. struct sock *sk;
  731. __u32 seq;
  732. int err;
  733. if (skb->len < (iph->ihl << 2) + 8) {
  734. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  735. return;
  736. }
  737. sk = tcp_v4_lookup(iph->daddr, th->dest, iph->saddr,
  738. th->source, tcp_v4_iif(skb));
  739. if (!sk) {
  740. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  741. return;
  742. }
  743. if (sk->sk_state == TCP_TIME_WAIT) {
  744. tcp_tw_put((struct tcp_tw_bucket *)sk);
  745. return;
  746. }
  747. bh_lock_sock(sk);
  748. /* If too many ICMPs get dropped on busy
  749. * servers this needs to be solved differently.
  750. */
  751. if (sock_owned_by_user(sk))
  752. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  753. if (sk->sk_state == TCP_CLOSE)
  754. goto out;
  755. tp = tcp_sk(sk);
  756. seq = ntohl(th->seq);
  757. if (sk->sk_state != TCP_LISTEN &&
  758. !between(seq, tp->snd_una, tp->snd_nxt)) {
  759. NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
  760. goto out;
  761. }
  762. switch (type) {
  763. case ICMP_SOURCE_QUENCH:
  764. /* Just silently ignore these. */
  765. goto out;
  766. case ICMP_PARAMETERPROB:
  767. err = EPROTO;
  768. break;
  769. case ICMP_DEST_UNREACH:
  770. if (code > NR_ICMP_UNREACH)
  771. goto out;
  772. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  773. if (!sock_owned_by_user(sk))
  774. do_pmtu_discovery(sk, iph, info);
  775. goto out;
  776. }
  777. err = icmp_err_convert[code].errno;
  778. break;
  779. case ICMP_TIME_EXCEEDED:
  780. err = EHOSTUNREACH;
  781. break;
  782. default:
  783. goto out;
  784. }
  785. switch (sk->sk_state) {
  786. struct request_sock *req, **prev;
  787. case TCP_LISTEN:
  788. if (sock_owned_by_user(sk))
  789. goto out;
  790. req = tcp_v4_search_req(tp, &prev, th->dest,
  791. iph->daddr, iph->saddr);
  792. if (!req)
  793. goto out;
  794. /* ICMPs are not backlogged, hence we cannot get
  795. an established socket here.
  796. */
  797. BUG_TRAP(!req->sk);
  798. if (seq != tcp_rsk(req)->snt_isn) {
  799. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  800. goto out;
  801. }
  802. /*
  803. * Still in SYN_RECV, just remove it silently.
  804. * There is no good way to pass the error to the newly
  805. * created socket, and POSIX does not want network
  806. * errors returned from accept().
  807. */
  808. tcp_synq_drop(sk, req, prev);
  809. goto out;
  810. case TCP_SYN_SENT:
  811. case TCP_SYN_RECV: /* Cannot happen.
  812. It can f.e. if SYNs crossed.
  813. */
  814. if (!sock_owned_by_user(sk)) {
  815. TCP_INC_STATS_BH(TCP_MIB_ATTEMPTFAILS);
  816. sk->sk_err = err;
  817. sk->sk_error_report(sk);
  818. tcp_done(sk);
  819. } else {
  820. sk->sk_err_soft = err;
  821. }
  822. goto out;
  823. }
  824. /* If we've already connected we will keep trying
  825. * until we time out, or the user gives up.
  826. *
  827. * rfc1122 4.2.3.9 allows to consider as hard errors
  828. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  829. * but it is obsoleted by pmtu discovery).
  830. *
  831. * Note, that in modern internet, where routing is unreliable
  832. * and in each dark corner broken firewalls sit, sending random
  833. * errors ordered by their masters even this two messages finally lose
  834. * their original sense (even Linux sends invalid PORT_UNREACHs)
  835. *
  836. * Now we are in compliance with RFCs.
  837. * --ANK (980905)
  838. */
  839. inet = inet_sk(sk);
  840. if (!sock_owned_by_user(sk) && inet->recverr) {
  841. sk->sk_err = err;
  842. sk->sk_error_report(sk);
  843. } else { /* Only an error on timeout */
  844. sk->sk_err_soft = err;
  845. }
  846. out:
  847. bh_unlock_sock(sk);
  848. sock_put(sk);
  849. }
  850. /* This routine computes an IPv4 TCP checksum. */
  851. void tcp_v4_send_check(struct sock *sk, struct tcphdr *th, int len,
  852. struct sk_buff *skb)
  853. {
  854. struct inet_sock *inet = inet_sk(sk);
  855. if (skb->ip_summed == CHECKSUM_HW) {
  856. th->check = ~tcp_v4_check(th, len, inet->saddr, inet->daddr, 0);
  857. skb->csum = offsetof(struct tcphdr, check);
  858. } else {
  859. th->check = tcp_v4_check(th, len, inet->saddr, inet->daddr,
  860. csum_partial((char *)th,
  861. th->doff << 2,
  862. skb->csum));
  863. }
  864. }
  865. /*
  866. * This routine will send an RST to the other tcp.
  867. *
  868. * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
  869. * for reset.
  870. * Answer: if a packet caused RST, it is not for a socket
  871. * existing in our system, if it is matched to a socket,
  872. * it is just duplicate segment or bug in other side's TCP.
  873. * So that we build reply only basing on parameters
  874. * arrived with segment.
  875. * Exception: precedence violation. We do not implement it in any case.
  876. */
  877. static void tcp_v4_send_reset(struct sk_buff *skb)
  878. {
  879. struct tcphdr *th = skb->h.th;
  880. struct tcphdr rth;
  881. struct ip_reply_arg arg;
  882. /* Never send a reset in response to a reset. */
  883. if (th->rst)
  884. return;
  885. if (((struct rtable *)skb->dst)->rt_type != RTN_LOCAL)
  886. return;
  887. /* Swap the send and the receive. */
  888. memset(&rth, 0, sizeof(struct tcphdr));
  889. rth.dest = th->source;
  890. rth.source = th->dest;
  891. rth.doff = sizeof(struct tcphdr) / 4;
  892. rth.rst = 1;
  893. if (th->ack) {
  894. rth.seq = th->ack_seq;
  895. } else {
  896. rth.ack = 1;
  897. rth.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
  898. skb->len - (th->doff << 2));
  899. }
  900. memset(&arg, 0, sizeof arg);
  901. arg.iov[0].iov_base = (unsigned char *)&rth;
  902. arg.iov[0].iov_len = sizeof rth;
  903. arg.csum = csum_tcpudp_nofold(skb->nh.iph->daddr,
  904. skb->nh.iph->saddr, /*XXX*/
  905. sizeof(struct tcphdr), IPPROTO_TCP, 0);
  906. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  907. ip_send_reply(tcp_socket->sk, skb, &arg, sizeof rth);
  908. TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
  909. TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
  910. }
  911. /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
  912. outside socket context is ugly, certainly. What can I do?
  913. */
  914. static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
  915. u32 win, u32 ts)
  916. {
  917. struct tcphdr *th = skb->h.th;
  918. struct {
  919. struct tcphdr th;
  920. u32 tsopt[3];
  921. } rep;
  922. struct ip_reply_arg arg;
  923. memset(&rep.th, 0, sizeof(struct tcphdr));
  924. memset(&arg, 0, sizeof arg);
  925. arg.iov[0].iov_base = (unsigned char *)&rep;
  926. arg.iov[0].iov_len = sizeof(rep.th);
  927. if (ts) {
  928. rep.tsopt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  929. (TCPOPT_TIMESTAMP << 8) |
  930. TCPOLEN_TIMESTAMP);
  931. rep.tsopt[1] = htonl(tcp_time_stamp);
  932. rep.tsopt[2] = htonl(ts);
  933. arg.iov[0].iov_len = sizeof(rep);
  934. }
  935. /* Swap the send and the receive. */
  936. rep.th.dest = th->source;
  937. rep.th.source = th->dest;
  938. rep.th.doff = arg.iov[0].iov_len / 4;
  939. rep.th.seq = htonl(seq);
  940. rep.th.ack_seq = htonl(ack);
  941. rep.th.ack = 1;
  942. rep.th.window = htons(win);
  943. arg.csum = csum_tcpudp_nofold(skb->nh.iph->daddr,
  944. skb->nh.iph->saddr, /*XXX*/
  945. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  946. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  947. ip_send_reply(tcp_socket->sk, skb, &arg, arg.iov[0].iov_len);
  948. TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
  949. }
  950. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  951. {
  952. struct tcp_tw_bucket *tw = (struct tcp_tw_bucket *)sk;
  953. tcp_v4_send_ack(skb, tw->tw_snd_nxt, tw->tw_rcv_nxt,
  954. tw->tw_rcv_wnd >> tw->tw_rcv_wscale, tw->tw_ts_recent);
  955. tcp_tw_put(tw);
  956. }
  957. static void tcp_v4_reqsk_send_ack(struct sk_buff *skb, struct request_sock *req)
  958. {
  959. tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
  960. req->ts_recent);
  961. }
  962. static struct dst_entry* tcp_v4_route_req(struct sock *sk,
  963. struct request_sock *req)
  964. {
  965. struct rtable *rt;
  966. const struct inet_request_sock *ireq = inet_rsk(req);
  967. struct ip_options *opt = inet_rsk(req)->opt;
  968. struct flowi fl = { .oif = sk->sk_bound_dev_if,
  969. .nl_u = { .ip4_u =
  970. { .daddr = ((opt && opt->srr) ?
  971. opt->faddr :
  972. ireq->rmt_addr),
  973. .saddr = ireq->loc_addr,
  974. .tos = RT_CONN_FLAGS(sk) } },
  975. .proto = IPPROTO_TCP,
  976. .uli_u = { .ports =
  977. { .sport = inet_sk(sk)->sport,
  978. .dport = ireq->rmt_port } } };
  979. if (ip_route_output_flow(&rt, &fl, sk, 0)) {
  980. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  981. return NULL;
  982. }
  983. if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
  984. ip_rt_put(rt);
  985. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  986. return NULL;
  987. }
  988. return &rt->u.dst;
  989. }
  990. /*
  991. * Send a SYN-ACK after having received an ACK.
  992. * This still operates on a request_sock only, not on a big
  993. * socket.
  994. */
  995. static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
  996. struct dst_entry *dst)
  997. {
  998. const struct inet_request_sock *ireq = inet_rsk(req);
  999. int err = -1;
  1000. struct sk_buff * skb;
  1001. /* First, grab a route. */
  1002. if (!dst && (dst = tcp_v4_route_req(sk, req)) == NULL)
  1003. goto out;
  1004. skb = tcp_make_synack(sk, dst, req);
  1005. if (skb) {
  1006. struct tcphdr *th = skb->h.th;
  1007. th->check = tcp_v4_check(th, skb->len,
  1008. ireq->loc_addr,
  1009. ireq->rmt_addr,
  1010. csum_partial((char *)th, skb->len,
  1011. skb->csum));
  1012. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  1013. ireq->rmt_addr,
  1014. ireq->opt);
  1015. if (err == NET_XMIT_CN)
  1016. err = 0;
  1017. }
  1018. out:
  1019. dst_release(dst);
  1020. return err;
  1021. }
  1022. /*
  1023. * IPv4 request_sock destructor.
  1024. */
  1025. static void tcp_v4_reqsk_destructor(struct request_sock *req)
  1026. {
  1027. if (inet_rsk(req)->opt)
  1028. kfree(inet_rsk(req)->opt);
  1029. }
  1030. static inline void syn_flood_warning(struct sk_buff *skb)
  1031. {
  1032. static unsigned long warntime;
  1033. if (time_after(jiffies, (warntime + HZ * 60))) {
  1034. warntime = jiffies;
  1035. printk(KERN_INFO
  1036. "possible SYN flooding on port %d. Sending cookies.\n",
  1037. ntohs(skb->h.th->dest));
  1038. }
  1039. }
  1040. /*
  1041. * Save and compile IPv4 options into the request_sock if needed.
  1042. */
  1043. static inline struct ip_options *tcp_v4_save_options(struct sock *sk,
  1044. struct sk_buff *skb)
  1045. {
  1046. struct ip_options *opt = &(IPCB(skb)->opt);
  1047. struct ip_options *dopt = NULL;
  1048. if (opt && opt->optlen) {
  1049. int opt_size = optlength(opt);
  1050. dopt = kmalloc(opt_size, GFP_ATOMIC);
  1051. if (dopt) {
  1052. if (ip_options_echo(dopt, skb)) {
  1053. kfree(dopt);
  1054. dopt = NULL;
  1055. }
  1056. }
  1057. }
  1058. return dopt;
  1059. }
  1060. struct request_sock_ops tcp_request_sock_ops = {
  1061. .family = PF_INET,
  1062. .obj_size = sizeof(struct tcp_request_sock),
  1063. .rtx_syn_ack = tcp_v4_send_synack,
  1064. .send_ack = tcp_v4_reqsk_send_ack,
  1065. .destructor = tcp_v4_reqsk_destructor,
  1066. .send_reset = tcp_v4_send_reset,
  1067. };
  1068. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  1069. {
  1070. struct inet_request_sock *ireq;
  1071. struct tcp_options_received tmp_opt;
  1072. struct request_sock *req;
  1073. __u32 saddr = skb->nh.iph->saddr;
  1074. __u32 daddr = skb->nh.iph->daddr;
  1075. __u32 isn = TCP_SKB_CB(skb)->when;
  1076. struct dst_entry *dst = NULL;
  1077. #ifdef CONFIG_SYN_COOKIES
  1078. int want_cookie = 0;
  1079. #else
  1080. #define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
  1081. #endif
  1082. /* Never answer to SYNs send to broadcast or multicast */
  1083. if (((struct rtable *)skb->dst)->rt_flags &
  1084. (RTCF_BROADCAST | RTCF_MULTICAST))
  1085. goto drop;
  1086. /* TW buckets are converted to open requests without
  1087. * limitations, they conserve resources and peer is
  1088. * evidently real one.
  1089. */
  1090. if (tcp_synq_is_full(sk) && !isn) {
  1091. #ifdef CONFIG_SYN_COOKIES
  1092. if (sysctl_tcp_syncookies) {
  1093. want_cookie = 1;
  1094. } else
  1095. #endif
  1096. goto drop;
  1097. }
  1098. /* Accept backlog is full. If we have already queued enough
  1099. * of warm entries in syn queue, drop request. It is better than
  1100. * clogging syn queue with openreqs with exponentially increasing
  1101. * timeout.
  1102. */
  1103. if (sk_acceptq_is_full(sk) && tcp_synq_young(sk) > 1)
  1104. goto drop;
  1105. req = reqsk_alloc(&tcp_request_sock_ops);
  1106. if (!req)
  1107. goto drop;
  1108. tcp_clear_options(&tmp_opt);
  1109. tmp_opt.mss_clamp = 536;
  1110. tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
  1111. tcp_parse_options(skb, &tmp_opt, 0);
  1112. if (want_cookie) {
  1113. tcp_clear_options(&tmp_opt);
  1114. tmp_opt.saw_tstamp = 0;
  1115. }
  1116. if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
  1117. /* Some OSes (unknown ones, but I see them on web server, which
  1118. * contains information interesting only for windows'
  1119. * users) do not send their stamp in SYN. It is easy case.
  1120. * We simply do not advertise TS support.
  1121. */
  1122. tmp_opt.saw_tstamp = 0;
  1123. tmp_opt.tstamp_ok = 0;
  1124. }
  1125. tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
  1126. tcp_openreq_init(req, &tmp_opt, skb);
  1127. ireq = inet_rsk(req);
  1128. ireq->loc_addr = daddr;
  1129. ireq->rmt_addr = saddr;
  1130. ireq->opt = tcp_v4_save_options(sk, skb);
  1131. if (!want_cookie)
  1132. TCP_ECN_create_request(req, skb->h.th);
  1133. if (want_cookie) {
  1134. #ifdef CONFIG_SYN_COOKIES
  1135. syn_flood_warning(skb);
  1136. #endif
  1137. isn = cookie_v4_init_sequence(sk, skb, &req->mss);
  1138. } else if (!isn) {
  1139. struct inet_peer *peer = NULL;
  1140. /* VJ's idea. We save last timestamp seen
  1141. * from the destination in peer table, when entering
  1142. * state TIME-WAIT, and check against it before
  1143. * accepting new connection request.
  1144. *
  1145. * If "isn" is not zero, this request hit alive
  1146. * timewait bucket, so that all the necessary checks
  1147. * are made in the function processing timewait state.
  1148. */
  1149. if (tmp_opt.saw_tstamp &&
  1150. sysctl_tcp_tw_recycle &&
  1151. (dst = tcp_v4_route_req(sk, req)) != NULL &&
  1152. (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
  1153. peer->v4daddr == saddr) {
  1154. if (xtime.tv_sec < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
  1155. (s32)(peer->tcp_ts - req->ts_recent) >
  1156. TCP_PAWS_WINDOW) {
  1157. NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
  1158. dst_release(dst);
  1159. goto drop_and_free;
  1160. }
  1161. }
  1162. /* Kill the following clause, if you dislike this way. */
  1163. else if (!sysctl_tcp_syncookies &&
  1164. (sysctl_max_syn_backlog - tcp_synq_len(sk) <
  1165. (sysctl_max_syn_backlog >> 2)) &&
  1166. (!peer || !peer->tcp_ts_stamp) &&
  1167. (!dst || !dst_metric(dst, RTAX_RTT))) {
  1168. /* Without syncookies last quarter of
  1169. * backlog is filled with destinations,
  1170. * proven to be alive.
  1171. * It means that we continue to communicate
  1172. * to destinations, already remembered
  1173. * to the moment of synflood.
  1174. */
  1175. LIMIT_NETDEBUG(printk(KERN_DEBUG "TCP: drop open "
  1176. "request from %u.%u."
  1177. "%u.%u/%u\n",
  1178. NIPQUAD(saddr),
  1179. ntohs(skb->h.th->source)));
  1180. dst_release(dst);
  1181. goto drop_and_free;
  1182. }
  1183. isn = tcp_v4_init_sequence(sk, skb);
  1184. }
  1185. tcp_rsk(req)->snt_isn = isn;
  1186. if (tcp_v4_send_synack(sk, req, dst))
  1187. goto drop_and_free;
  1188. if (want_cookie) {
  1189. reqsk_free(req);
  1190. } else {
  1191. tcp_v4_synq_add(sk, req);
  1192. }
  1193. return 0;
  1194. drop_and_free:
  1195. reqsk_free(req);
  1196. drop:
  1197. TCP_INC_STATS_BH(TCP_MIB_ATTEMPTFAILS);
  1198. return 0;
  1199. }
  1200. /*
  1201. * The three way handshake has completed - we got a valid synack -
  1202. * now create the new socket.
  1203. */
  1204. struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  1205. struct request_sock *req,
  1206. struct dst_entry *dst)
  1207. {
  1208. struct inet_request_sock *ireq;
  1209. struct inet_sock *newinet;
  1210. struct tcp_sock *newtp;
  1211. struct sock *newsk;
  1212. if (sk_acceptq_is_full(sk))
  1213. goto exit_overflow;
  1214. if (!dst && (dst = tcp_v4_route_req(sk, req)) == NULL)
  1215. goto exit;
  1216. newsk = tcp_create_openreq_child(sk, req, skb);
  1217. if (!newsk)
  1218. goto exit;
  1219. sk_setup_caps(newsk, dst);
  1220. newtp = tcp_sk(newsk);
  1221. newinet = inet_sk(newsk);
  1222. ireq = inet_rsk(req);
  1223. newinet->daddr = ireq->rmt_addr;
  1224. newinet->rcv_saddr = ireq->loc_addr;
  1225. newinet->saddr = ireq->loc_addr;
  1226. newinet->opt = ireq->opt;
  1227. ireq->opt = NULL;
  1228. newinet->mc_index = tcp_v4_iif(skb);
  1229. newinet->mc_ttl = skb->nh.iph->ttl;
  1230. newtp->ext_header_len = 0;
  1231. if (newinet->opt)
  1232. newtp->ext_header_len = newinet->opt->optlen;
  1233. newinet->id = newtp->write_seq ^ jiffies;
  1234. tcp_sync_mss(newsk, dst_mtu(dst));
  1235. newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
  1236. tcp_initialize_rcv_mss(newsk);
  1237. __inet_hash(&tcp_hashinfo, newsk, 0);
  1238. __inet_inherit_port(&tcp_hashinfo, sk, newsk);
  1239. return newsk;
  1240. exit_overflow:
  1241. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  1242. exit:
  1243. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  1244. dst_release(dst);
  1245. return NULL;
  1246. }
  1247. static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  1248. {
  1249. struct tcphdr *th = skb->h.th;
  1250. struct iphdr *iph = skb->nh.iph;
  1251. struct tcp_sock *tp = tcp_sk(sk);
  1252. struct sock *nsk;
  1253. struct request_sock **prev;
  1254. /* Find possible connection requests. */
  1255. struct request_sock *req = tcp_v4_search_req(tp, &prev, th->source,
  1256. iph->saddr, iph->daddr);
  1257. if (req)
  1258. return tcp_check_req(sk, skb, req, prev);
  1259. nsk = __tcp_v4_lookup_established(skb->nh.iph->saddr,
  1260. th->source,
  1261. skb->nh.iph->daddr,
  1262. ntohs(th->dest),
  1263. tcp_v4_iif(skb));
  1264. if (nsk) {
  1265. if (nsk->sk_state != TCP_TIME_WAIT) {
  1266. bh_lock_sock(nsk);
  1267. return nsk;
  1268. }
  1269. tcp_tw_put((struct tcp_tw_bucket *)nsk);
  1270. return NULL;
  1271. }
  1272. #ifdef CONFIG_SYN_COOKIES
  1273. if (!th->rst && !th->syn && th->ack)
  1274. sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
  1275. #endif
  1276. return sk;
  1277. }
  1278. static int tcp_v4_checksum_init(struct sk_buff *skb)
  1279. {
  1280. if (skb->ip_summed == CHECKSUM_HW) {
  1281. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1282. if (!tcp_v4_check(skb->h.th, skb->len, skb->nh.iph->saddr,
  1283. skb->nh.iph->daddr, skb->csum))
  1284. return 0;
  1285. LIMIT_NETDEBUG(printk(KERN_DEBUG "hw tcp v4 csum failed\n"));
  1286. skb->ip_summed = CHECKSUM_NONE;
  1287. }
  1288. if (skb->len <= 76) {
  1289. if (tcp_v4_check(skb->h.th, skb->len, skb->nh.iph->saddr,
  1290. skb->nh.iph->daddr,
  1291. skb_checksum(skb, 0, skb->len, 0)))
  1292. return -1;
  1293. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1294. } else {
  1295. skb->csum = ~tcp_v4_check(skb->h.th, skb->len,
  1296. skb->nh.iph->saddr,
  1297. skb->nh.iph->daddr, 0);
  1298. }
  1299. return 0;
  1300. }
  1301. /* The socket must have it's spinlock held when we get
  1302. * here.
  1303. *
  1304. * We have a potential double-lock case here, so even when
  1305. * doing backlog processing we use the BH locking scheme.
  1306. * This is because we cannot sleep with the original spinlock
  1307. * held.
  1308. */
  1309. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  1310. {
  1311. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1312. TCP_CHECK_TIMER(sk);
  1313. if (tcp_rcv_established(sk, skb, skb->h.th, skb->len))
  1314. goto reset;
  1315. TCP_CHECK_TIMER(sk);
  1316. return 0;
  1317. }
  1318. if (skb->len < (skb->h.th->doff << 2) || tcp_checksum_complete(skb))
  1319. goto csum_err;
  1320. if (sk->sk_state == TCP_LISTEN) {
  1321. struct sock *nsk = tcp_v4_hnd_req(sk, skb);
  1322. if (!nsk)
  1323. goto discard;
  1324. if (nsk != sk) {
  1325. if (tcp_child_process(sk, nsk, skb))
  1326. goto reset;
  1327. return 0;
  1328. }
  1329. }
  1330. TCP_CHECK_TIMER(sk);
  1331. if (tcp_rcv_state_process(sk, skb, skb->h.th, skb->len))
  1332. goto reset;
  1333. TCP_CHECK_TIMER(sk);
  1334. return 0;
  1335. reset:
  1336. tcp_v4_send_reset(skb);
  1337. discard:
  1338. kfree_skb(skb);
  1339. /* Be careful here. If this function gets more complicated and
  1340. * gcc suffers from register pressure on the x86, sk (in %ebx)
  1341. * might be destroyed here. This current version compiles correctly,
  1342. * but you have been warned.
  1343. */
  1344. return 0;
  1345. csum_err:
  1346. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  1347. goto discard;
  1348. }
  1349. /*
  1350. * From tcp_input.c
  1351. */
  1352. int tcp_v4_rcv(struct sk_buff *skb)
  1353. {
  1354. struct tcphdr *th;
  1355. struct sock *sk;
  1356. int ret;
  1357. if (skb->pkt_type != PACKET_HOST)
  1358. goto discard_it;
  1359. /* Count it even if it's bad */
  1360. TCP_INC_STATS_BH(TCP_MIB_INSEGS);
  1361. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1362. goto discard_it;
  1363. th = skb->h.th;
  1364. if (th->doff < sizeof(struct tcphdr) / 4)
  1365. goto bad_packet;
  1366. if (!pskb_may_pull(skb, th->doff * 4))
  1367. goto discard_it;
  1368. /* An explanation is required here, I think.
  1369. * Packet length and doff are validated by header prediction,
  1370. * provided case of th->doff==0 is elimineted.
  1371. * So, we defer the checks. */
  1372. if ((skb->ip_summed != CHECKSUM_UNNECESSARY &&
  1373. tcp_v4_checksum_init(skb) < 0))
  1374. goto bad_packet;
  1375. th = skb->h.th;
  1376. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1377. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1378. skb->len - th->doff * 4);
  1379. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1380. TCP_SKB_CB(skb)->when = 0;
  1381. TCP_SKB_CB(skb)->flags = skb->nh.iph->tos;
  1382. TCP_SKB_CB(skb)->sacked = 0;
  1383. sk = __tcp_v4_lookup(skb->nh.iph->saddr, th->source,
  1384. skb->nh.iph->daddr, ntohs(th->dest),
  1385. tcp_v4_iif(skb));
  1386. if (!sk)
  1387. goto no_tcp_socket;
  1388. process:
  1389. if (sk->sk_state == TCP_TIME_WAIT)
  1390. goto do_time_wait;
  1391. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1392. goto discard_and_relse;
  1393. if (sk_filter(sk, skb, 0))
  1394. goto discard_and_relse;
  1395. skb->dev = NULL;
  1396. bh_lock_sock(sk);
  1397. ret = 0;
  1398. if (!sock_owned_by_user(sk)) {
  1399. if (!tcp_prequeue(sk, skb))
  1400. ret = tcp_v4_do_rcv(sk, skb);
  1401. } else
  1402. sk_add_backlog(sk, skb);
  1403. bh_unlock_sock(sk);
  1404. sock_put(sk);
  1405. return ret;
  1406. no_tcp_socket:
  1407. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1408. goto discard_it;
  1409. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  1410. bad_packet:
  1411. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  1412. } else {
  1413. tcp_v4_send_reset(skb);
  1414. }
  1415. discard_it:
  1416. /* Discard frame. */
  1417. kfree_skb(skb);
  1418. return 0;
  1419. discard_and_relse:
  1420. sock_put(sk);
  1421. goto discard_it;
  1422. do_time_wait:
  1423. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1424. tcp_tw_put((struct tcp_tw_bucket *) sk);
  1425. goto discard_it;
  1426. }
  1427. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  1428. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  1429. tcp_tw_put((struct tcp_tw_bucket *) sk);
  1430. goto discard_it;
  1431. }
  1432. switch (tcp_timewait_state_process((struct tcp_tw_bucket *)sk,
  1433. skb, th, skb->len)) {
  1434. case TCP_TW_SYN: {
  1435. struct sock *sk2 = tcp_v4_lookup_listener(skb->nh.iph->daddr,
  1436. ntohs(th->dest),
  1437. tcp_v4_iif(skb));
  1438. if (sk2) {
  1439. tcp_tw_deschedule((struct tcp_tw_bucket *)sk);
  1440. tcp_tw_put((struct tcp_tw_bucket *)sk);
  1441. sk = sk2;
  1442. goto process;
  1443. }
  1444. /* Fall through to ACK */
  1445. }
  1446. case TCP_TW_ACK:
  1447. tcp_v4_timewait_ack(sk, skb);
  1448. break;
  1449. case TCP_TW_RST:
  1450. goto no_tcp_socket;
  1451. case TCP_TW_SUCCESS:;
  1452. }
  1453. goto discard_it;
  1454. }
  1455. static void v4_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)
  1456. {
  1457. struct sockaddr_in *sin = (struct sockaddr_in *) uaddr;
  1458. struct inet_sock *inet = inet_sk(sk);
  1459. sin->sin_family = AF_INET;
  1460. sin->sin_addr.s_addr = inet->daddr;
  1461. sin->sin_port = inet->dport;
  1462. }
  1463. /* VJ's idea. Save last timestamp seen from this destination
  1464. * and hold it at least for normal timewait interval to use for duplicate
  1465. * segment detection in subsequent connections, before they enter synchronized
  1466. * state.
  1467. */
  1468. int tcp_v4_remember_stamp(struct sock *sk)
  1469. {
  1470. struct inet_sock *inet = inet_sk(sk);
  1471. struct tcp_sock *tp = tcp_sk(sk);
  1472. struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
  1473. struct inet_peer *peer = NULL;
  1474. int release_it = 0;
  1475. if (!rt || rt->rt_dst != inet->daddr) {
  1476. peer = inet_getpeer(inet->daddr, 1);
  1477. release_it = 1;
  1478. } else {
  1479. if (!rt->peer)
  1480. rt_bind_peer(rt, 1);
  1481. peer = rt->peer;
  1482. }
  1483. if (peer) {
  1484. if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
  1485. (peer->tcp_ts_stamp + TCP_PAWS_MSL < xtime.tv_sec &&
  1486. peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
  1487. peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
  1488. peer->tcp_ts = tp->rx_opt.ts_recent;
  1489. }
  1490. if (release_it)
  1491. inet_putpeer(peer);
  1492. return 1;
  1493. }
  1494. return 0;
  1495. }
  1496. int tcp_v4_tw_remember_stamp(struct tcp_tw_bucket *tw)
  1497. {
  1498. struct inet_peer *peer = NULL;
  1499. peer = inet_getpeer(tw->tw_daddr, 1);
  1500. if (peer) {
  1501. if ((s32)(peer->tcp_ts - tw->tw_ts_recent) <= 0 ||
  1502. (peer->tcp_ts_stamp + TCP_PAWS_MSL < xtime.tv_sec &&
  1503. peer->tcp_ts_stamp <= tw->tw_ts_recent_stamp)) {
  1504. peer->tcp_ts_stamp = tw->tw_ts_recent_stamp;
  1505. peer->tcp_ts = tw->tw_ts_recent;
  1506. }
  1507. inet_putpeer(peer);
  1508. return 1;
  1509. }
  1510. return 0;
  1511. }
  1512. struct tcp_func ipv4_specific = {
  1513. .queue_xmit = ip_queue_xmit,
  1514. .send_check = tcp_v4_send_check,
  1515. .rebuild_header = inet_sk_rebuild_header,
  1516. .conn_request = tcp_v4_conn_request,
  1517. .syn_recv_sock = tcp_v4_syn_recv_sock,
  1518. .remember_stamp = tcp_v4_remember_stamp,
  1519. .net_header_len = sizeof(struct iphdr),
  1520. .setsockopt = ip_setsockopt,
  1521. .getsockopt = ip_getsockopt,
  1522. .addr2sockaddr = v4_addr2sockaddr,
  1523. .sockaddr_len = sizeof(struct sockaddr_in),
  1524. };
  1525. /* NOTE: A lot of things set to zero explicitly by call to
  1526. * sk_alloc() so need not be done here.
  1527. */
  1528. static int tcp_v4_init_sock(struct sock *sk)
  1529. {
  1530. struct tcp_sock *tp = tcp_sk(sk);
  1531. skb_queue_head_init(&tp->out_of_order_queue);
  1532. tcp_init_xmit_timers(sk);
  1533. tcp_prequeue_init(tp);
  1534. tp->rto = TCP_TIMEOUT_INIT;
  1535. tp->mdev = TCP_TIMEOUT_INIT;
  1536. /* So many TCP implementations out there (incorrectly) count the
  1537. * initial SYN frame in their delayed-ACK and congestion control
  1538. * algorithms that we must have the following bandaid to talk
  1539. * efficiently to them. -DaveM
  1540. */
  1541. tp->snd_cwnd = 2;
  1542. /* See draft-stevens-tcpca-spec-01 for discussion of the
  1543. * initialization of these values.
  1544. */
  1545. tp->snd_ssthresh = 0x7fffffff; /* Infinity */
  1546. tp->snd_cwnd_clamp = ~0;
  1547. tp->mss_cache = 536;
  1548. tp->reordering = sysctl_tcp_reordering;
  1549. tp->ca_ops = &tcp_init_congestion_ops;
  1550. sk->sk_state = TCP_CLOSE;
  1551. sk->sk_write_space = sk_stream_write_space;
  1552. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1553. tp->af_specific = &ipv4_specific;
  1554. sk->sk_sndbuf = sysctl_tcp_wmem[1];
  1555. sk->sk_rcvbuf = sysctl_tcp_rmem[1];
  1556. atomic_inc(&tcp_sockets_allocated);
  1557. return 0;
  1558. }
  1559. int tcp_v4_destroy_sock(struct sock *sk)
  1560. {
  1561. struct tcp_sock *tp = tcp_sk(sk);
  1562. tcp_clear_xmit_timers(sk);
  1563. tcp_cleanup_congestion_control(tp);
  1564. /* Cleanup up the write buffer. */
  1565. sk_stream_writequeue_purge(sk);
  1566. /* Cleans up our, hopefully empty, out_of_order_queue. */
  1567. __skb_queue_purge(&tp->out_of_order_queue);
  1568. /* Clean prequeue, it must be empty really */
  1569. __skb_queue_purge(&tp->ucopy.prequeue);
  1570. /* Clean up a referenced TCP bind bucket. */
  1571. if (inet_sk(sk)->bind_hash)
  1572. inet_put_port(&tcp_hashinfo, sk);
  1573. /*
  1574. * If sendmsg cached page exists, toss it.
  1575. */
  1576. if (sk->sk_sndmsg_page) {
  1577. __free_page(sk->sk_sndmsg_page);
  1578. sk->sk_sndmsg_page = NULL;
  1579. }
  1580. atomic_dec(&tcp_sockets_allocated);
  1581. return 0;
  1582. }
  1583. EXPORT_SYMBOL(tcp_v4_destroy_sock);
  1584. #ifdef CONFIG_PROC_FS
  1585. /* Proc filesystem TCP sock list dumping. */
  1586. static inline struct tcp_tw_bucket *tw_head(struct hlist_head *head)
  1587. {
  1588. return hlist_empty(head) ? NULL :
  1589. list_entry(head->first, struct tcp_tw_bucket, tw_node);
  1590. }
  1591. static inline struct tcp_tw_bucket *tw_next(struct tcp_tw_bucket *tw)
  1592. {
  1593. return tw->tw_node.next ?
  1594. hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
  1595. }
  1596. static void *listening_get_next(struct seq_file *seq, void *cur)
  1597. {
  1598. struct tcp_sock *tp;
  1599. struct hlist_node *node;
  1600. struct sock *sk = cur;
  1601. struct tcp_iter_state* st = seq->private;
  1602. if (!sk) {
  1603. st->bucket = 0;
  1604. sk = sk_head(&tcp_hashinfo.listening_hash[0]);
  1605. goto get_sk;
  1606. }
  1607. ++st->num;
  1608. if (st->state == TCP_SEQ_STATE_OPENREQ) {
  1609. struct request_sock *req = cur;
  1610. tp = tcp_sk(st->syn_wait_sk);
  1611. req = req->dl_next;
  1612. while (1) {
  1613. while (req) {
  1614. if (req->rsk_ops->family == st->family) {
  1615. cur = req;
  1616. goto out;
  1617. }
  1618. req = req->dl_next;
  1619. }
  1620. if (++st->sbucket >= TCP_SYNQ_HSIZE)
  1621. break;
  1622. get_req:
  1623. req = tp->accept_queue.listen_opt->syn_table[st->sbucket];
  1624. }
  1625. sk = sk_next(st->syn_wait_sk);
  1626. st->state = TCP_SEQ_STATE_LISTENING;
  1627. read_unlock_bh(&tp->accept_queue.syn_wait_lock);
  1628. } else {
  1629. tp = tcp_sk(sk);
  1630. read_lock_bh(&tp->accept_queue.syn_wait_lock);
  1631. if (reqsk_queue_len(&tp->accept_queue))
  1632. goto start_req;
  1633. read_unlock_bh(&tp->accept_queue.syn_wait_lock);
  1634. sk = sk_next(sk);
  1635. }
  1636. get_sk:
  1637. sk_for_each_from(sk, node) {
  1638. if (sk->sk_family == st->family) {
  1639. cur = sk;
  1640. goto out;
  1641. }
  1642. tp = tcp_sk(sk);
  1643. read_lock_bh(&tp->accept_queue.syn_wait_lock);
  1644. if (reqsk_queue_len(&tp->accept_queue)) {
  1645. start_req:
  1646. st->uid = sock_i_uid(sk);
  1647. st->syn_wait_sk = sk;
  1648. st->state = TCP_SEQ_STATE_OPENREQ;
  1649. st->sbucket = 0;
  1650. goto get_req;
  1651. }
  1652. read_unlock_bh(&tp->accept_queue.syn_wait_lock);
  1653. }
  1654. if (++st->bucket < INET_LHTABLE_SIZE) {
  1655. sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
  1656. goto get_sk;
  1657. }
  1658. cur = NULL;
  1659. out:
  1660. return cur;
  1661. }
  1662. static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
  1663. {
  1664. void *rc = listening_get_next(seq, NULL);
  1665. while (rc && *pos) {
  1666. rc = listening_get_next(seq, rc);
  1667. --*pos;
  1668. }
  1669. return rc;
  1670. }
  1671. static void *established_get_first(struct seq_file *seq)
  1672. {
  1673. struct tcp_iter_state* st = seq->private;
  1674. void *rc = NULL;
  1675. for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
  1676. struct sock *sk;
  1677. struct hlist_node *node;
  1678. struct tcp_tw_bucket *tw;
  1679. /* We can reschedule _before_ having picked the target: */
  1680. cond_resched_softirq();
  1681. read_lock(&tcp_hashinfo.ehash[st->bucket].lock);
  1682. sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
  1683. if (sk->sk_family != st->family) {
  1684. continue;
  1685. }
  1686. rc = sk;
  1687. goto out;
  1688. }
  1689. st->state = TCP_SEQ_STATE_TIME_WAIT;
  1690. tw_for_each(tw, node,
  1691. &tcp_hashinfo.ehash[st->bucket + tcp_hashinfo.ehash_size].chain) {
  1692. if (tw->tw_family != st->family) {
  1693. continue;
  1694. }
  1695. rc = tw;
  1696. goto out;
  1697. }
  1698. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1699. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1700. }
  1701. out:
  1702. return rc;
  1703. }
  1704. static void *established_get_next(struct seq_file *seq, void *cur)
  1705. {
  1706. struct sock *sk = cur;
  1707. struct tcp_tw_bucket *tw;
  1708. struct hlist_node *node;
  1709. struct tcp_iter_state* st = seq->private;
  1710. ++st->num;
  1711. if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
  1712. tw = cur;
  1713. tw = tw_next(tw);
  1714. get_tw:
  1715. while (tw && tw->tw_family != st->family) {
  1716. tw = tw_next(tw);
  1717. }
  1718. if (tw) {
  1719. cur = tw;
  1720. goto out;
  1721. }
  1722. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1723. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1724. /* We can reschedule between buckets: */
  1725. cond_resched_softirq();
  1726. if (++st->bucket < tcp_hashinfo.ehash_size) {
  1727. read_lock(&tcp_hashinfo.ehash[st->bucket].lock);
  1728. sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
  1729. } else {
  1730. cur = NULL;
  1731. goto out;
  1732. }
  1733. } else
  1734. sk = sk_next(sk);
  1735. sk_for_each_from(sk, node) {
  1736. if (sk->sk_family == st->family)
  1737. goto found;
  1738. }
  1739. st->state = TCP_SEQ_STATE_TIME_WAIT;
  1740. tw = tw_head(&tcp_hashinfo.ehash[st->bucket + tcp_hashinfo.ehash_size].chain);
  1741. goto get_tw;
  1742. found:
  1743. cur = sk;
  1744. out:
  1745. return cur;
  1746. }
  1747. static void *established_get_idx(struct seq_file *seq, loff_t pos)
  1748. {
  1749. void *rc = established_get_first(seq);
  1750. while (rc && pos) {
  1751. rc = established_get_next(seq, rc);
  1752. --pos;
  1753. }
  1754. return rc;
  1755. }
  1756. static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
  1757. {
  1758. void *rc;
  1759. struct tcp_iter_state* st = seq->private;
  1760. inet_listen_lock(&tcp_hashinfo);
  1761. st->state = TCP_SEQ_STATE_LISTENING;
  1762. rc = listening_get_idx(seq, &pos);
  1763. if (!rc) {
  1764. inet_listen_unlock(&tcp_hashinfo);
  1765. local_bh_disable();
  1766. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1767. rc = established_get_idx(seq, pos);
  1768. }
  1769. return rc;
  1770. }
  1771. static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
  1772. {
  1773. struct tcp_iter_state* st = seq->private;
  1774. st->state = TCP_SEQ_STATE_LISTENING;
  1775. st->num = 0;
  1776. return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1777. }
  1778. static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1779. {
  1780. void *rc = NULL;
  1781. struct tcp_iter_state* st;
  1782. if (v == SEQ_START_TOKEN) {
  1783. rc = tcp_get_idx(seq, 0);
  1784. goto out;
  1785. }
  1786. st = seq->private;
  1787. switch (st->state) {
  1788. case TCP_SEQ_STATE_OPENREQ:
  1789. case TCP_SEQ_STATE_LISTENING:
  1790. rc = listening_get_next(seq, v);
  1791. if (!rc) {
  1792. inet_listen_unlock(&tcp_hashinfo);
  1793. local_bh_disable();
  1794. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1795. rc = established_get_first(seq);
  1796. }
  1797. break;
  1798. case TCP_SEQ_STATE_ESTABLISHED:
  1799. case TCP_SEQ_STATE_TIME_WAIT:
  1800. rc = established_get_next(seq, v);
  1801. break;
  1802. }
  1803. out:
  1804. ++*pos;
  1805. return rc;
  1806. }
  1807. static void tcp_seq_stop(struct seq_file *seq, void *v)
  1808. {
  1809. struct tcp_iter_state* st = seq->private;
  1810. switch (st->state) {
  1811. case TCP_SEQ_STATE_OPENREQ:
  1812. if (v) {
  1813. struct tcp_sock *tp = tcp_sk(st->syn_wait_sk);
  1814. read_unlock_bh(&tp->accept_queue.syn_wait_lock);
  1815. }
  1816. case TCP_SEQ_STATE_LISTENING:
  1817. if (v != SEQ_START_TOKEN)
  1818. inet_listen_unlock(&tcp_hashinfo);
  1819. break;
  1820. case TCP_SEQ_STATE_TIME_WAIT:
  1821. case TCP_SEQ_STATE_ESTABLISHED:
  1822. if (v)
  1823. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1824. local_bh_enable();
  1825. break;
  1826. }
  1827. }
  1828. static int tcp_seq_open(struct inode *inode, struct file *file)
  1829. {
  1830. struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
  1831. struct seq_file *seq;
  1832. struct tcp_iter_state *s;
  1833. int rc;
  1834. if (unlikely(afinfo == NULL))
  1835. return -EINVAL;
  1836. s = kmalloc(sizeof(*s), GFP_KERNEL);
  1837. if (!s)
  1838. return -ENOMEM;
  1839. memset(s, 0, sizeof(*s));
  1840. s->family = afinfo->family;
  1841. s->seq_ops.start = tcp_seq_start;
  1842. s->seq_ops.next = tcp_seq_next;
  1843. s->seq_ops.show = afinfo->seq_show;
  1844. s->seq_ops.stop = tcp_seq_stop;
  1845. rc = seq_open(file, &s->seq_ops);
  1846. if (rc)
  1847. goto out_kfree;
  1848. seq = file->private_data;
  1849. seq->private = s;
  1850. out:
  1851. return rc;
  1852. out_kfree:
  1853. kfree(s);
  1854. goto out;
  1855. }
  1856. int tcp_proc_register(struct tcp_seq_afinfo *afinfo)
  1857. {
  1858. int rc = 0;
  1859. struct proc_dir_entry *p;
  1860. if (!afinfo)
  1861. return -EINVAL;
  1862. afinfo->seq_fops->owner = afinfo->owner;
  1863. afinfo->seq_fops->open = tcp_seq_open;
  1864. afinfo->seq_fops->read = seq_read;
  1865. afinfo->seq_fops->llseek = seq_lseek;
  1866. afinfo->seq_fops->release = seq_release_private;
  1867. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1868. if (p)
  1869. p->data = afinfo;
  1870. else
  1871. rc = -ENOMEM;
  1872. return rc;
  1873. }
  1874. void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo)
  1875. {
  1876. if (!afinfo)
  1877. return;
  1878. proc_net_remove(afinfo->name);
  1879. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1880. }
  1881. static void get_openreq4(struct sock *sk, struct request_sock *req,
  1882. char *tmpbuf, int i, int uid)
  1883. {
  1884. const struct inet_request_sock *ireq = inet_rsk(req);
  1885. int ttd = req->expires - jiffies;
  1886. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1887. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p",
  1888. i,
  1889. ireq->loc_addr,
  1890. ntohs(inet_sk(sk)->sport),
  1891. ireq->rmt_addr,
  1892. ntohs(ireq->rmt_port),
  1893. TCP_SYN_RECV,
  1894. 0, 0, /* could print option size, but that is af dependent. */
  1895. 1, /* timers active (only the expire timer) */
  1896. jiffies_to_clock_t(ttd),
  1897. req->retrans,
  1898. uid,
  1899. 0, /* non standard timer */
  1900. 0, /* open_requests have no inode */
  1901. atomic_read(&sk->sk_refcnt),
  1902. req);
  1903. }
  1904. static void get_tcp4_sock(struct sock *sp, char *tmpbuf, int i)
  1905. {
  1906. int timer_active;
  1907. unsigned long timer_expires;
  1908. struct tcp_sock *tp = tcp_sk(sp);
  1909. struct inet_sock *inet = inet_sk(sp);
  1910. unsigned int dest = inet->daddr;
  1911. unsigned int src = inet->rcv_saddr;
  1912. __u16 destp = ntohs(inet->dport);
  1913. __u16 srcp = ntohs(inet->sport);
  1914. if (tp->pending == TCP_TIME_RETRANS) {
  1915. timer_active = 1;
  1916. timer_expires = tp->timeout;
  1917. } else if (tp->pending == TCP_TIME_PROBE0) {
  1918. timer_active = 4;
  1919. timer_expires = tp->timeout;
  1920. } else if (timer_pending(&sp->sk_timer)) {
  1921. timer_active = 2;
  1922. timer_expires = sp->sk_timer.expires;
  1923. } else {
  1924. timer_active = 0;
  1925. timer_expires = jiffies;
  1926. }
  1927. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
  1928. "%08X %5d %8d %lu %d %p %u %u %u %u %d",
  1929. i, src, srcp, dest, destp, sp->sk_state,
  1930. tp->write_seq - tp->snd_una, tp->rcv_nxt - tp->copied_seq,
  1931. timer_active,
  1932. jiffies_to_clock_t(timer_expires - jiffies),
  1933. tp->retransmits,
  1934. sock_i_uid(sp),
  1935. tp->probes_out,
  1936. sock_i_ino(sp),
  1937. atomic_read(&sp->sk_refcnt), sp,
  1938. tp->rto, tp->ack.ato, (tp->ack.quick << 1) | tp->ack.pingpong,
  1939. tp->snd_cwnd,
  1940. tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh);
  1941. }
  1942. static void get_timewait4_sock(struct tcp_tw_bucket *tw, char *tmpbuf, int i)
  1943. {
  1944. unsigned int dest, src;
  1945. __u16 destp, srcp;
  1946. int ttd = tw->tw_ttd - jiffies;
  1947. if (ttd < 0)
  1948. ttd = 0;
  1949. dest = tw->tw_daddr;
  1950. src = tw->tw_rcv_saddr;
  1951. destp = ntohs(tw->tw_dport);
  1952. srcp = ntohs(tw->tw_sport);
  1953. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1954. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p",
  1955. i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
  1956. 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
  1957. atomic_read(&tw->tw_refcnt), tw);
  1958. }
  1959. #define TMPSZ 150
  1960. static int tcp4_seq_show(struct seq_file *seq, void *v)
  1961. {
  1962. struct tcp_iter_state* st;
  1963. char tmpbuf[TMPSZ + 1];
  1964. if (v == SEQ_START_TOKEN) {
  1965. seq_printf(seq, "%-*s\n", TMPSZ - 1,
  1966. " sl local_address rem_address st tx_queue "
  1967. "rx_queue tr tm->when retrnsmt uid timeout "
  1968. "inode");
  1969. goto out;
  1970. }
  1971. st = seq->private;
  1972. switch (st->state) {
  1973. case TCP_SEQ_STATE_LISTENING:
  1974. case TCP_SEQ_STATE_ESTABLISHED:
  1975. get_tcp4_sock(v, tmpbuf, st->num);
  1976. break;
  1977. case TCP_SEQ_STATE_OPENREQ:
  1978. get_openreq4(st->syn_wait_sk, v, tmpbuf, st->num, st->uid);
  1979. break;
  1980. case TCP_SEQ_STATE_TIME_WAIT:
  1981. get_timewait4_sock(v, tmpbuf, st->num);
  1982. break;
  1983. }
  1984. seq_printf(seq, "%-*s\n", TMPSZ - 1, tmpbuf);
  1985. out:
  1986. return 0;
  1987. }
  1988. static struct file_operations tcp4_seq_fops;
  1989. static struct tcp_seq_afinfo tcp4_seq_afinfo = {
  1990. .owner = THIS_MODULE,
  1991. .name = "tcp",
  1992. .family = AF_INET,
  1993. .seq_show = tcp4_seq_show,
  1994. .seq_fops = &tcp4_seq_fops,
  1995. };
  1996. int __init tcp4_proc_init(void)
  1997. {
  1998. return tcp_proc_register(&tcp4_seq_afinfo);
  1999. }
  2000. void tcp4_proc_exit(void)
  2001. {
  2002. tcp_proc_unregister(&tcp4_seq_afinfo);
  2003. }
  2004. #endif /* CONFIG_PROC_FS */
  2005. struct proto tcp_prot = {
  2006. .name = "TCP",
  2007. .owner = THIS_MODULE,
  2008. .close = tcp_close,
  2009. .connect = tcp_v4_connect,
  2010. .disconnect = tcp_disconnect,
  2011. .accept = tcp_accept,
  2012. .ioctl = tcp_ioctl,
  2013. .init = tcp_v4_init_sock,
  2014. .destroy = tcp_v4_destroy_sock,
  2015. .shutdown = tcp_shutdown,
  2016. .setsockopt = tcp_setsockopt,
  2017. .getsockopt = tcp_getsockopt,
  2018. .sendmsg = tcp_sendmsg,
  2019. .recvmsg = tcp_recvmsg,
  2020. .backlog_rcv = tcp_v4_do_rcv,
  2021. .hash = tcp_v4_hash,
  2022. .unhash = tcp_unhash,
  2023. .get_port = tcp_v4_get_port,
  2024. .enter_memory_pressure = tcp_enter_memory_pressure,
  2025. .sockets_allocated = &tcp_sockets_allocated,
  2026. .memory_allocated = &tcp_memory_allocated,
  2027. .memory_pressure = &tcp_memory_pressure,
  2028. .sysctl_mem = sysctl_tcp_mem,
  2029. .sysctl_wmem = sysctl_tcp_wmem,
  2030. .sysctl_rmem = sysctl_tcp_rmem,
  2031. .max_header = MAX_TCP_HEADER,
  2032. .obj_size = sizeof(struct tcp_sock),
  2033. .rsk_prot = &tcp_request_sock_ops,
  2034. };
  2035. void __init tcp_v4_init(struct net_proto_family *ops)
  2036. {
  2037. int err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_TCP, &tcp_socket);
  2038. if (err < 0)
  2039. panic("Failed to create the TCP control socket.\n");
  2040. tcp_socket->sk->sk_allocation = GFP_ATOMIC;
  2041. inet_sk(tcp_socket->sk)->uc_ttl = -1;
  2042. /* Unhash it so that IP input processing does not even
  2043. * see it, we do not wish this socket to see incoming
  2044. * packets.
  2045. */
  2046. tcp_socket->sk->sk_prot->unhash(tcp_socket->sk);
  2047. }
  2048. EXPORT_SYMBOL(ipv4_specific);
  2049. EXPORT_SYMBOL(inet_bind_bucket_create);
  2050. EXPORT_SYMBOL(tcp_hashinfo);
  2051. EXPORT_SYMBOL(tcp_prot);
  2052. EXPORT_SYMBOL(tcp_unhash);
  2053. EXPORT_SYMBOL(tcp_v4_conn_request);
  2054. EXPORT_SYMBOL(tcp_v4_connect);
  2055. EXPORT_SYMBOL(tcp_v4_do_rcv);
  2056. EXPORT_SYMBOL(tcp_v4_remember_stamp);
  2057. EXPORT_SYMBOL(tcp_v4_send_check);
  2058. EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
  2059. #ifdef CONFIG_PROC_FS
  2060. EXPORT_SYMBOL(tcp_proc_register);
  2061. EXPORT_SYMBOL(tcp_proc_unregister);
  2062. #endif
  2063. EXPORT_SYMBOL(sysctl_local_port_range);
  2064. EXPORT_SYMBOL(sysctl_tcp_low_latency);
  2065. EXPORT_SYMBOL(sysctl_tcp_tw_reuse);