tcp_minisocks.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_minisocks.c,v 1.1.1.1 2007/06/12 07:27:14 eyryu 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. #include <linux/mm.h>
  23. #include <linux/module.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/workqueue.h>
  26. #include <net/tcp.h>
  27. #include <net/inet_common.h>
  28. #include <net/xfrm.h>
  29. #ifdef CONFIG_SYSCTL
  30. #define SYNC_INIT 0 /* let the user enable it */
  31. #else
  32. #define SYNC_INIT 1
  33. #endif
  34. int sysctl_tcp_syncookies __read_mostly = SYNC_INIT;
  35. int sysctl_tcp_abort_on_overflow __read_mostly;
  36. struct inet_timewait_death_row tcp_death_row = {
  37. .sysctl_max_tw_buckets = NR_FILE * 2,
  38. .period = TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
  39. .death_lock = __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock),
  40. .hashinfo = &tcp_hashinfo,
  41. .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
  42. (unsigned long)&tcp_death_row),
  43. .twkill_work = __WORK_INITIALIZER(tcp_death_row.twkill_work,
  44. inet_twdr_twkill_work),
  45. /* Short-time timewait calendar */
  46. .twcal_hand = -1,
  47. .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
  48. (unsigned long)&tcp_death_row),
  49. };
  50. EXPORT_SYMBOL_GPL(tcp_death_row);
  51. static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
  52. {
  53. if (seq == s_win)
  54. return 1;
  55. if (after(end_seq, s_win) && before(seq, e_win))
  56. return 1;
  57. return (seq == e_win && seq == end_seq);
  58. }
  59. /*
  60. * * Main purpose of TIME-WAIT state is to close connection gracefully,
  61. * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
  62. * (and, probably, tail of data) and one or more our ACKs are lost.
  63. * * What is TIME-WAIT timeout? It is associated with maximal packet
  64. * lifetime in the internet, which results in wrong conclusion, that
  65. * it is set to catch "old duplicate segments" wandering out of their path.
  66. * It is not quite correct. This timeout is calculated so that it exceeds
  67. * maximal retransmission timeout enough to allow to lose one (or more)
  68. * segments sent by peer and our ACKs. This time may be calculated from RTO.
  69. * * When TIME-WAIT socket receives RST, it means that another end
  70. * finally closed and we are allowed to kill TIME-WAIT too.
  71. * * Second purpose of TIME-WAIT is catching old duplicate segments.
  72. * Well, certainly it is pure paranoia, but if we load TIME-WAIT
  73. * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
  74. * * If we invented some more clever way to catch duplicates
  75. * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
  76. *
  77. * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
  78. * When you compare it to RFCs, please, read section SEGMENT ARRIVES
  79. * from the very beginning.
  80. *
  81. * NOTE. With recycling (and later with fin-wait-2) TW bucket
  82. * is _not_ stateless. It means, that strictly speaking we must
  83. * spinlock it. I do not want! Well, probability of misbehaviour
  84. * is ridiculously low and, seems, we could use some mb() tricks
  85. * to avoid misread sequence numbers, states etc. --ANK
  86. */
  87. enum tcp_tw_status
  88. tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
  89. const struct tcphdr *th)
  90. {
  91. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  92. struct tcp_options_received tmp_opt;
  93. int paws_reject = 0;
  94. tmp_opt.saw_tstamp = 0;
  95. if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
  96. tcp_parse_options(skb, &tmp_opt, 0);
  97. if (tmp_opt.saw_tstamp) {
  98. tmp_opt.ts_recent = tcptw->tw_ts_recent;
  99. tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  100. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  101. }
  102. }
  103. if (tw->tw_substate == TCP_FIN_WAIT2) {
  104. /* Just repeat all the checks of tcp_rcv_state_process() */
  105. /* Out of window, send ACK */
  106. if (paws_reject ||
  107. !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  108. tcptw->tw_rcv_nxt,
  109. tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
  110. return TCP_TW_ACK;
  111. if (th->rst)
  112. goto kill;
  113. if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
  114. goto kill_with_rst;
  115. /* Dup ACK? */
  116. if (!after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
  117. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
  118. inet_twsk_put(tw);
  119. return TCP_TW_SUCCESS;
  120. }
  121. /* New data or FIN. If new data arrive after half-duplex close,
  122. * reset.
  123. */
  124. if (!th->fin ||
  125. TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
  126. kill_with_rst:
  127. inet_twsk_deschedule(tw, &tcp_death_row);
  128. inet_twsk_put(tw);
  129. return TCP_TW_RST;
  130. }
  131. /* FIN arrived, enter true time-wait state. */
  132. tw->tw_substate = TCP_TIME_WAIT;
  133. tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  134. if (tmp_opt.saw_tstamp) {
  135. tcptw->tw_ts_recent_stamp = xtime.tv_sec;
  136. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  137. }
  138. /* I am shamed, but failed to make it more elegant.
  139. * Yes, it is direct reference to IP, which is impossible
  140. * to generalize to IPv6. Taking into account that IPv6
  141. * do not understand recycling in any case, it not
  142. * a big problem in practice. --ANK */
  143. if (tw->tw_family == AF_INET &&
  144. tcp_death_row.sysctl_tw_recycle && tcptw->tw_ts_recent_stamp &&
  145. tcp_v4_tw_remember_stamp(tw))
  146. inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout,
  147. TCP_TIMEWAIT_LEN);
  148. else
  149. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  150. TCP_TIMEWAIT_LEN);
  151. return TCP_TW_ACK;
  152. }
  153. /*
  154. * Now real TIME-WAIT state.
  155. *
  156. * RFC 1122:
  157. * "When a connection is [...] on TIME-WAIT state [...]
  158. * [a TCP] MAY accept a new SYN from the remote TCP to
  159. * reopen the connection directly, if it:
  160. *
  161. * (1) assigns its initial sequence number for the new
  162. * connection to be larger than the largest sequence
  163. * number it used on the previous connection incarnation,
  164. * and
  165. *
  166. * (2) returns to TIME-WAIT state if the SYN turns out
  167. * to be an old duplicate".
  168. */
  169. if (!paws_reject &&
  170. (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
  171. (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
  172. /* In window segment, it may be only reset or bare ack. */
  173. if (th->rst) {
  174. /* This is TIME_WAIT assassination, in two flavors.
  175. * Oh well... nobody has a sufficient solution to this
  176. * protocol bug yet.
  177. */
  178. if (sysctl_tcp_rfc1337 == 0) {
  179. kill:
  180. inet_twsk_deschedule(tw, &tcp_death_row);
  181. inet_twsk_put(tw);
  182. return TCP_TW_SUCCESS;
  183. }
  184. }
  185. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  186. TCP_TIMEWAIT_LEN);
  187. if (tmp_opt.saw_tstamp) {
  188. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  189. tcptw->tw_ts_recent_stamp = xtime.tv_sec;
  190. }
  191. inet_twsk_put(tw);
  192. return TCP_TW_SUCCESS;
  193. }
  194. /* Out of window segment.
  195. All the segments are ACKed immediately.
  196. The only exception is new SYN. We accept it, if it is
  197. not old duplicate and we are not in danger to be killed
  198. by delayed old duplicates. RFC check is that it has
  199. newer sequence number works at rates <40Mbit/sec.
  200. However, if paws works, it is reliable AND even more,
  201. we even may relax silly seq space cutoff.
  202. RED-PEN: we violate main RFC requirement, if this SYN will appear
  203. old duplicate (i.e. we receive RST in reply to SYN-ACK),
  204. we must return socket to time-wait state. It is not good,
  205. but not fatal yet.
  206. */
  207. if (th->syn && !th->rst && !th->ack && !paws_reject &&
  208. (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
  209. (tmp_opt.saw_tstamp &&
  210. (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
  211. u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
  212. if (isn == 0)
  213. isn++;
  214. TCP_SKB_CB(skb)->when = isn;
  215. return TCP_TW_SYN;
  216. }
  217. if (paws_reject)
  218. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  219. if(!th->rst) {
  220. /* In this case we must reset the TIMEWAIT timer.
  221. *
  222. * If it is ACKless SYN it may be both old duplicate
  223. * and new good SYN with random sequence number <rcv_nxt.
  224. * Do not reschedule in the last case.
  225. */
  226. if (paws_reject || th->ack)
  227. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  228. TCP_TIMEWAIT_LEN);
  229. /* Send ACK. Note, we do not put the bucket,
  230. * it will be released by caller.
  231. */
  232. return TCP_TW_ACK;
  233. }
  234. inet_twsk_put(tw);
  235. return TCP_TW_SUCCESS;
  236. }
  237. /*
  238. * Move a socket to time-wait or dead fin-wait-2 state.
  239. */
  240. void tcp_time_wait(struct sock *sk, int state, int timeo)
  241. {
  242. struct inet_timewait_sock *tw = NULL;
  243. const struct inet_connection_sock *icsk = inet_csk(sk);
  244. const struct tcp_sock *tp = tcp_sk(sk);
  245. int recycle_ok = 0;
  246. if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
  247. recycle_ok = icsk->icsk_af_ops->remember_stamp(sk);
  248. if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)
  249. tw = inet_twsk_alloc(sk, state);
  250. if (tw != NULL) {
  251. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  252. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  253. tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
  254. tcptw->tw_rcv_nxt = tp->rcv_nxt;
  255. tcptw->tw_snd_nxt = tp->snd_nxt;
  256. tcptw->tw_rcv_wnd = tcp_receive_window(tp);
  257. tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
  258. tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
  259. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  260. if (tw->tw_family == PF_INET6) {
  261. struct ipv6_pinfo *np = inet6_sk(sk);
  262. struct inet6_timewait_sock *tw6;
  263. tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
  264. tw6 = inet6_twsk((struct sock *)tw);
  265. ipv6_addr_copy(&tw6->tw_v6_daddr, &np->daddr);
  266. ipv6_addr_copy(&tw6->tw_v6_rcv_saddr, &np->rcv_saddr);
  267. tw->tw_ipv6only = np->ipv6only;
  268. }
  269. #endif
  270. #ifdef CONFIG_TCP_MD5SIG
  271. /*
  272. * The timewait bucket does not have the key DB from the
  273. * sock structure. We just make a quick copy of the
  274. * md5 key being used (if indeed we are using one)
  275. * so the timewait ack generating code has the key.
  276. */
  277. do {
  278. struct tcp_md5sig_key *key;
  279. memset(tcptw->tw_md5_key, 0, sizeof(tcptw->tw_md5_key));
  280. tcptw->tw_md5_keylen = 0;
  281. key = tp->af_specific->md5_lookup(sk, sk);
  282. if (key != NULL) {
  283. memcpy(&tcptw->tw_md5_key, key->key, key->keylen);
  284. tcptw->tw_md5_keylen = key->keylen;
  285. if (tcp_alloc_md5sig_pool() == NULL)
  286. BUG();
  287. }
  288. } while(0);
  289. #endif
  290. /* Linkage updates. */
  291. __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
  292. /* Get the TIME_WAIT timeout firing. */
  293. if (timeo < rto)
  294. timeo = rto;
  295. if (recycle_ok) {
  296. tw->tw_timeout = rto;
  297. } else {
  298. tw->tw_timeout = TCP_TIMEWAIT_LEN;
  299. if (state == TCP_TIME_WAIT)
  300. timeo = TCP_TIMEWAIT_LEN;
  301. }
  302. inet_twsk_schedule(tw, &tcp_death_row, timeo,
  303. TCP_TIMEWAIT_LEN);
  304. inet_twsk_put(tw);
  305. } else {
  306. /* Sorry, if we're out of memory, just CLOSE this
  307. * socket up. We've got bigger problems than
  308. * non-graceful socket closings.
  309. */
  310. LIMIT_NETDEBUG(KERN_INFO "TCP: time wait bucket table overflow\n");
  311. }
  312. tcp_update_metrics(sk);
  313. tcp_done(sk);
  314. }
  315. void tcp_twsk_destructor(struct sock *sk)
  316. {
  317. #ifdef CONFIG_TCP_MD5SIG
  318. struct tcp_timewait_sock *twsk = tcp_twsk(sk);
  319. if (twsk->tw_md5_keylen)
  320. tcp_put_md5sig_pool();
  321. #endif
  322. }
  323. EXPORT_SYMBOL_GPL(tcp_twsk_destructor);
  324. /* This is not only more efficient than what we used to do, it eliminates
  325. * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
  326. *
  327. * Actually, we could lots of memory writes here. tp of listening
  328. * socket contains all necessary default parameters.
  329. */
  330. struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
  331. {
  332. struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
  333. if (newsk != NULL) {
  334. const struct inet_request_sock *ireq = inet_rsk(req);
  335. struct tcp_request_sock *treq = tcp_rsk(req);
  336. struct inet_connection_sock *newicsk = inet_csk(newsk);
  337. struct tcp_sock *newtp;
  338. /* Now setup tcp_sock */
  339. newtp = tcp_sk(newsk);
  340. newtp->pred_flags = 0;
  341. newtp->rcv_nxt = treq->rcv_isn + 1;
  342. newtp->snd_nxt = newtp->snd_una = newtp->snd_sml = treq->snt_isn + 1;
  343. tcp_prequeue_init(newtp);
  344. tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);
  345. newtp->srtt = 0;
  346. newtp->mdev = TCP_TIMEOUT_INIT;
  347. newicsk->icsk_rto = TCP_TIMEOUT_INIT;
  348. newtp->packets_out = 0;
  349. newtp->left_out = 0;
  350. newtp->retrans_out = 0;
  351. newtp->sacked_out = 0;
  352. newtp->fackets_out = 0;
  353. newtp->snd_ssthresh = 0x7fffffff;
  354. /* So many TCP implementations out there (incorrectly) count the
  355. * initial SYN frame in their delayed-ACK and congestion control
  356. * algorithms that we must have the following bandaid to talk
  357. * efficiently to them. -DaveM
  358. */
  359. newtp->snd_cwnd = 2;
  360. newtp->snd_cwnd_cnt = 0;
  361. newtp->bytes_acked = 0;
  362. newtp->frto_counter = 0;
  363. newtp->frto_highmark = 0;
  364. newicsk->icsk_ca_ops = &tcp_init_congestion_ops;
  365. tcp_set_ca_state(newsk, TCP_CA_Open);
  366. tcp_init_xmit_timers(newsk);
  367. skb_queue_head_init(&newtp->out_of_order_queue);
  368. newtp->rcv_wup = treq->rcv_isn + 1;
  369. newtp->write_seq = treq->snt_isn + 1;
  370. newtp->pushed_seq = newtp->write_seq;
  371. newtp->copied_seq = treq->rcv_isn + 1;
  372. newtp->rx_opt.saw_tstamp = 0;
  373. newtp->rx_opt.dsack = 0;
  374. newtp->rx_opt.eff_sacks = 0;
  375. newtp->rx_opt.num_sacks = 0;
  376. newtp->urg_data = 0;
  377. if (sock_flag(newsk, SOCK_KEEPOPEN))
  378. inet_csk_reset_keepalive_timer(newsk,
  379. keepalive_time_when(newtp));
  380. newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
  381. if((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
  382. if (sysctl_tcp_fack)
  383. newtp->rx_opt.sack_ok |= 2;
  384. }
  385. newtp->window_clamp = req->window_clamp;
  386. newtp->rcv_ssthresh = req->rcv_wnd;
  387. newtp->rcv_wnd = req->rcv_wnd;
  388. newtp->rx_opt.wscale_ok = ireq->wscale_ok;
  389. if (newtp->rx_opt.wscale_ok) {
  390. newtp->rx_opt.snd_wscale = ireq->snd_wscale;
  391. newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
  392. } else {
  393. newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
  394. newtp->window_clamp = min(newtp->window_clamp, 65535U);
  395. }
  396. newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->rx_opt.snd_wscale;
  397. newtp->max_window = newtp->snd_wnd;
  398. if (newtp->rx_opt.tstamp_ok) {
  399. newtp->rx_opt.ts_recent = req->ts_recent;
  400. newtp->rx_opt.ts_recent_stamp = xtime.tv_sec;
  401. newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  402. } else {
  403. newtp->rx_opt.ts_recent_stamp = 0;
  404. newtp->tcp_header_len = sizeof(struct tcphdr);
  405. }
  406. #ifdef CONFIG_TCP_MD5SIG
  407. newtp->md5sig_info = NULL; /*XXX*/
  408. if (newtp->af_specific->md5_lookup(sk, newsk))
  409. newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
  410. #endif
  411. if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
  412. newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
  413. newtp->rx_opt.mss_clamp = req->mss;
  414. TCP_ECN_openreq_child(newtp, req);
  415. TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);
  416. }
  417. return newsk;
  418. }
  419. /*
  420. * Process an incoming packet for SYN_RECV sockets represented
  421. * as a request_sock.
  422. */
  423. struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
  424. struct request_sock *req,
  425. struct request_sock **prev)
  426. {
  427. struct tcphdr *th = skb->h.th;
  428. __be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
  429. int paws_reject = 0;
  430. struct tcp_options_received tmp_opt;
  431. struct sock *child;
  432. tmp_opt.saw_tstamp = 0;
  433. if (th->doff > (sizeof(struct tcphdr)>>2)) {
  434. tcp_parse_options(skb, &tmp_opt, 0);
  435. if (tmp_opt.saw_tstamp) {
  436. tmp_opt.ts_recent = req->ts_recent;
  437. /* We do not store true stamp, but it is not required,
  438. * it can be estimated (approximately)
  439. * from another data.
  440. */
  441. tmp_opt.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
  442. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  443. }
  444. }
  445. /* Check for pure retransmitted SYN. */
  446. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
  447. flg == TCP_FLAG_SYN &&
  448. !paws_reject) {
  449. /*
  450. * RFC793 draws (Incorrectly! It was fixed in RFC1122)
  451. * this case on figure 6 and figure 8, but formal
  452. * protocol description says NOTHING.
  453. * To be more exact, it says that we should send ACK,
  454. * because this segment (at least, if it has no data)
  455. * is out of window.
  456. *
  457. * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
  458. * describe SYN-RECV state. All the description
  459. * is wrong, we cannot believe to it and should
  460. * rely only on common sense and implementation
  461. * experience.
  462. *
  463. * Enforce "SYN-ACK" according to figure 8, figure 6
  464. * of RFC793, fixed by RFC1122.
  465. */
  466. req->rsk_ops->rtx_syn_ack(sk, req, NULL);
  467. return NULL;
  468. }
  469. /* Further reproduces section "SEGMENT ARRIVES"
  470. for state SYN-RECEIVED of RFC793.
  471. It is broken, however, it does not work only
  472. when SYNs are crossed.
  473. You would think that SYN crossing is impossible here, since
  474. we should have a SYN_SENT socket (from connect()) on our end,
  475. but this is not true if the crossed SYNs were sent to both
  476. ends by a malicious third party. We must defend against this,
  477. and to do that we first verify the ACK (as per RFC793, page
  478. 36) and reset if it is invalid. Is this a true full defense?
  479. To convince ourselves, let us consider a way in which the ACK
  480. test can still pass in this 'malicious crossed SYNs' case.
  481. Malicious sender sends identical SYNs (and thus identical sequence
  482. numbers) to both A and B:
  483. A: gets SYN, seq=7
  484. B: gets SYN, seq=7
  485. By our good fortune, both A and B select the same initial
  486. send sequence number of seven :-)
  487. A: sends SYN|ACK, seq=7, ack_seq=8
  488. B: sends SYN|ACK, seq=7, ack_seq=8
  489. So we are now A eating this SYN|ACK, ACK test passes. So
  490. does sequence test, SYN is truncated, and thus we consider
  491. it a bare ACK.
  492. If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
  493. bare ACK. Otherwise, we create an established connection. Both
  494. ends (listening sockets) accept the new incoming connection and try
  495. to talk to each other. 8-)
  496. Note: This case is both harmless, and rare. Possibility is about the
  497. same as us discovering intelligent life on another plant tomorrow.
  498. But generally, we should (RFC lies!) to accept ACK
  499. from SYNACK both here and in tcp_rcv_state_process().
  500. tcp_rcv_state_process() does not, hence, we do not too.
  501. Note that the case is absolutely generic:
  502. we cannot optimize anything here without
  503. violating protocol. All the checks must be made
  504. before attempt to create socket.
  505. */
  506. /* RFC793 page 36: "If the connection is in any non-synchronized state ...
  507. * and the incoming segment acknowledges something not yet
  508. * sent (the segment carries an unacceptable ACK) ...
  509. * a reset is sent."
  510. *
  511. * Invalid ACK: reset will be sent by listening socket
  512. */
  513. if ((flg & TCP_FLAG_ACK) &&
  514. (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))
  515. return sk;
  516. /* Also, it would be not so bad idea to check rcv_tsecr, which
  517. * is essentially ACK extension and too early or too late values
  518. * should cause reset in unsynchronized states.
  519. */
  520. /* RFC793: "first check sequence number". */
  521. if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  522. tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
  523. /* Out of window: send ACK and drop. */
  524. if (!(flg & TCP_FLAG_RST))
  525. req->rsk_ops->send_ack(skb, req);
  526. if (paws_reject)
  527. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  528. return NULL;
  529. }
  530. /* In sequence, PAWS is OK. */
  531. if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
  532. req->ts_recent = tmp_opt.rcv_tsval;
  533. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
  534. /* Truncate SYN, it is out of window starting
  535. at tcp_rsk(req)->rcv_isn + 1. */
  536. flg &= ~TCP_FLAG_SYN;
  537. }
  538. /* RFC793: "second check the RST bit" and
  539. * "fourth, check the SYN bit"
  540. */
  541. if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
  542. TCP_INC_STATS_BH(TCP_MIB_ATTEMPTFAILS);
  543. goto embryonic_reset;
  544. }
  545. /* ACK sequence verified above, just make sure ACK is
  546. * set. If ACK not set, just silently drop the packet.
  547. */
  548. if (!(flg & TCP_FLAG_ACK))
  549. return NULL;
  550. /* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
  551. if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
  552. TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
  553. inet_rsk(req)->acked = 1;
  554. return NULL;
  555. }
  556. /* OK, ACK is valid, create big socket and
  557. * feed this segment to it. It will repeat all
  558. * the tests. THIS SEGMENT MUST MOVE SOCKET TO
  559. * ESTABLISHED STATE. If it will be dropped after
  560. * socket is created, wait for troubles.
  561. */
  562. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb,
  563. req, NULL);
  564. if (child == NULL)
  565. goto listen_overflow;
  566. #ifdef CONFIG_TCP_MD5SIG
  567. else {
  568. /* Copy over the MD5 key from the original socket */
  569. struct tcp_md5sig_key *key;
  570. struct tcp_sock *tp = tcp_sk(sk);
  571. key = tp->af_specific->md5_lookup(sk, child);
  572. if (key != NULL) {
  573. /*
  574. * We're using one, so create a matching key on the
  575. * newsk structure. If we fail to get memory then we
  576. * end up not copying the key across. Shucks.
  577. */
  578. char *newkey = kmemdup(key->key, key->keylen,
  579. GFP_ATOMIC);
  580. if (newkey) {
  581. if (!tcp_alloc_md5sig_pool())
  582. BUG();
  583. tp->af_specific->md5_add(child, child,
  584. newkey,
  585. key->keylen);
  586. }
  587. }
  588. }
  589. #endif
  590. inet_csk_reqsk_queue_unlink(sk, req, prev);
  591. inet_csk_reqsk_queue_removed(sk, req);
  592. inet_csk_reqsk_queue_add(sk, req, child);
  593. return child;
  594. listen_overflow:
  595. if (!sysctl_tcp_abort_on_overflow) {
  596. inet_rsk(req)->acked = 1;
  597. return NULL;
  598. }
  599. embryonic_reset:
  600. NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);
  601. if (!(flg & TCP_FLAG_RST))
  602. req->rsk_ops->send_reset(sk, skb);
  603. inet_csk_reqsk_queue_drop(sk, req, prev);
  604. return NULL;
  605. }
  606. /*
  607. * Queue segment on the new socket if the new socket is active,
  608. * otherwise we just shortcircuit this and continue with
  609. * the new socket.
  610. */
  611. int tcp_child_process(struct sock *parent, struct sock *child,
  612. struct sk_buff *skb)
  613. {
  614. int ret = 0;
  615. int state = child->sk_state;
  616. if (!sock_owned_by_user(child)) {
  617. ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
  618. /* Wakeup parent, send SIGIO */
  619. if (state == TCP_SYN_RECV && child->sk_state != state)
  620. parent->sk_data_ready(parent, 0);
  621. } else {
  622. /* Alas, it is possible again, because we do lookup
  623. * in main socket hash table and lock on listening
  624. * socket does not protect us more.
  625. */
  626. sk_add_backlog(child, skb);
  627. }
  628. bh_unlock_sock(child);
  629. sock_put(child);
  630. return ret;
  631. }
  632. EXPORT_SYMBOL(tcp_check_req);
  633. EXPORT_SYMBOL(tcp_child_process);
  634. EXPORT_SYMBOL(tcp_create_openreq_child);
  635. EXPORT_SYMBOL(tcp_timewait_state_process);