tcp_minisocks.c 24 KB

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