tcp_minisocks.c 27 KB

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