tcp_minisocks.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 <net/tcp.h>
  22. #include <net/tcp_ecn.h>
  23. #include <net/xfrm.h>
  24. #include <net/busy_poll.h>
  25. #include <net/rstreason.h>
  26. #include <net/psp.h>
  27. static bool tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
  28. {
  29. if (seq == s_win)
  30. return true;
  31. if (after(end_seq, s_win) && before(seq, e_win))
  32. return true;
  33. return seq == e_win && seq == end_seq;
  34. }
  35. static enum tcp_tw_status
  36. tcp_timewait_check_oow_rate_limit(struct inet_timewait_sock *tw,
  37. const struct sk_buff *skb, int mib_idx)
  38. {
  39. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  40. if (!tcp_oow_rate_limited(twsk_net(tw), skb, mib_idx,
  41. &tcptw->tw_last_oow_ack_time)) {
  42. /* Send ACK. Note, we do not put the bucket,
  43. * it will be released by caller.
  44. */
  45. return TCP_TW_ACK_OOW;
  46. }
  47. /* We are rate-limiting, so just release the tw sock and drop skb. */
  48. inet_twsk_put(tw);
  49. return TCP_TW_SUCCESS;
  50. }
  51. static void twsk_rcv_nxt_update(struct tcp_timewait_sock *tcptw, u32 seq,
  52. u32 rcv_nxt)
  53. {
  54. #ifdef CONFIG_TCP_AO
  55. struct tcp_ao_info *ao;
  56. ao = rcu_dereference(tcptw->ao_info);
  57. if (unlikely(ao && seq < rcv_nxt))
  58. WRITE_ONCE(ao->rcv_sne, ao->rcv_sne + 1);
  59. #endif
  60. WRITE_ONCE(tcptw->tw_rcv_nxt, seq);
  61. }
  62. /*
  63. * * Main purpose of TIME-WAIT state is to close connection gracefully,
  64. * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
  65. * (and, probably, tail of data) and one or more our ACKs are lost.
  66. * * What is TIME-WAIT timeout? It is associated with maximal packet
  67. * lifetime in the internet, which results in wrong conclusion, that
  68. * it is set to catch "old duplicate segments" wandering out of their path.
  69. * It is not quite correct. This timeout is calculated so that it exceeds
  70. * maximal retransmission timeout enough to allow to lose one (or more)
  71. * segments sent by peer and our ACKs. This time may be calculated from RTO.
  72. * * When TIME-WAIT socket receives RST, it means that another end
  73. * finally closed and we are allowed to kill TIME-WAIT too.
  74. * * Second purpose of TIME-WAIT is catching old duplicate segments.
  75. * Well, certainly it is pure paranoia, but if we load TIME-WAIT
  76. * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
  77. * * If we invented some more clever way to catch duplicates
  78. * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
  79. *
  80. * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
  81. * When you compare it to RFCs, please, read section SEGMENT ARRIVES
  82. * from the very beginning.
  83. *
  84. * NOTE. With recycling (and later with fin-wait-2) TW bucket
  85. * is _not_ stateless. It means, that strictly speaking we must
  86. * spinlock it. I do not want! Well, probability of misbehaviour
  87. * is ridiculously low and, seems, we could use some mb() tricks
  88. * to avoid misread sequence numbers, states etc. --ANK
  89. *
  90. * We don't need to initialize tmp_out.sack_ok as we don't use the results
  91. */
  92. enum tcp_tw_status
  93. tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
  94. const struct tcphdr *th, u32 *tw_isn,
  95. enum skb_drop_reason *drop_reason)
  96. {
  97. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  98. u32 rcv_nxt = READ_ONCE(tcptw->tw_rcv_nxt);
  99. struct tcp_options_received tmp_opt;
  100. enum skb_drop_reason psp_drop;
  101. bool paws_reject = false;
  102. int ts_recent_stamp;
  103. /* Instead of dropping immediately, wait to see what value is
  104. * returned. We will accept a non psp-encapsulated syn in the
  105. * case where TCP_TW_SYN is returned.
  106. */
  107. psp_drop = psp_twsk_rx_policy_check(tw, skb);
  108. tmp_opt.saw_tstamp = 0;
  109. ts_recent_stamp = READ_ONCE(tcptw->tw_ts_recent_stamp);
  110. if (th->doff > (sizeof(*th) >> 2) && ts_recent_stamp) {
  111. tcp_parse_options(twsk_net(tw), skb, &tmp_opt, 0, NULL);
  112. if (tmp_opt.saw_tstamp) {
  113. if (tmp_opt.rcv_tsecr)
  114. tmp_opt.rcv_tsecr -= tcptw->tw_ts_offset;
  115. tmp_opt.ts_recent = READ_ONCE(tcptw->tw_ts_recent);
  116. tmp_opt.ts_recent_stamp = ts_recent_stamp;
  117. paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
  118. }
  119. }
  120. if (READ_ONCE(tw->tw_substate) == TCP_FIN_WAIT2) {
  121. /* Just repeat all the checks of tcp_rcv_state_process() */
  122. if (psp_drop)
  123. goto out_put;
  124. /* Out of window, send ACK */
  125. if (paws_reject ||
  126. !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  127. rcv_nxt,
  128. rcv_nxt + tcptw->tw_rcv_wnd))
  129. return tcp_timewait_check_oow_rate_limit(
  130. tw, skb, LINUX_MIB_TCPACKSKIPPEDFINWAIT2);
  131. if (th->rst)
  132. goto kill;
  133. if (th->syn && !before(TCP_SKB_CB(skb)->seq, rcv_nxt))
  134. return TCP_TW_RST;
  135. /* Dup ACK? */
  136. if (!th->ack ||
  137. !after(TCP_SKB_CB(skb)->end_seq, rcv_nxt) ||
  138. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
  139. inet_twsk_put(tw);
  140. return TCP_TW_SUCCESS;
  141. }
  142. /* New data or FIN. If new data arrive after half-duplex close,
  143. * reset.
  144. */
  145. if (!th->fin ||
  146. TCP_SKB_CB(skb)->end_seq != rcv_nxt + 1)
  147. return TCP_TW_RST;
  148. /* FIN arrived, enter true time-wait state. */
  149. WRITE_ONCE(tw->tw_substate, TCP_TIME_WAIT);
  150. twsk_rcv_nxt_update(tcptw, TCP_SKB_CB(skb)->end_seq,
  151. rcv_nxt);
  152. if (tmp_opt.saw_tstamp) {
  153. u64 ts = tcp_clock_ms();
  154. WRITE_ONCE(tw->tw_entry_stamp, ts);
  155. WRITE_ONCE(tcptw->tw_ts_recent_stamp,
  156. div_u64(ts, MSEC_PER_SEC));
  157. WRITE_ONCE(tcptw->tw_ts_recent,
  158. tmp_opt.rcv_tsval);
  159. }
  160. inet_twsk_reschedule(tw, TCP_TIMEWAIT_LEN);
  161. return TCP_TW_ACK;
  162. }
  163. /*
  164. * Now real TIME-WAIT state.
  165. *
  166. * RFC 1122:
  167. * "When a connection is [...] on TIME-WAIT state [...]
  168. * [a TCP] MAY accept a new SYN from the remote TCP to
  169. * reopen the connection directly, if it:
  170. *
  171. * (1) assigns its initial sequence number for the new
  172. * connection to be larger than the largest sequence
  173. * number it used on the previous connection incarnation,
  174. * and
  175. *
  176. * (2) returns to TIME-WAIT state if the SYN turns out
  177. * to be an old duplicate".
  178. */
  179. if (!paws_reject &&
  180. (TCP_SKB_CB(skb)->seq == rcv_nxt &&
  181. (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
  182. /* In window segment, it may be only reset or bare ack. */
  183. if (psp_drop)
  184. goto out_put;
  185. if (th->rst) {
  186. /* This is TIME_WAIT assassination, in two flavors.
  187. * Oh well... nobody has a sufficient solution to this
  188. * protocol bug yet.
  189. */
  190. if (!READ_ONCE(twsk_net(tw)->ipv4.sysctl_tcp_rfc1337)) {
  191. kill:
  192. inet_twsk_deschedule_put(tw);
  193. return TCP_TW_SUCCESS;
  194. }
  195. } else {
  196. inet_twsk_reschedule(tw, TCP_TIMEWAIT_LEN);
  197. }
  198. if (tmp_opt.saw_tstamp) {
  199. WRITE_ONCE(tcptw->tw_ts_recent,
  200. tmp_opt.rcv_tsval);
  201. WRITE_ONCE(tcptw->tw_ts_recent_stamp,
  202. ktime_get_seconds());
  203. }
  204. inet_twsk_put(tw);
  205. return TCP_TW_SUCCESS;
  206. }
  207. /* Out of window segment.
  208. All the segments are ACKed immediately.
  209. The only exception is new SYN. We accept it, if it is
  210. not old duplicate and we are not in danger to be killed
  211. by delayed old duplicates. RFC check is that it has
  212. newer sequence number works at rates <40Mbit/sec.
  213. However, if paws works, it is reliable AND even more,
  214. we even may relax silly seq space cutoff.
  215. RED-PEN: we violate main RFC requirement, if this SYN will appear
  216. old duplicate (i.e. we receive RST in reply to SYN-ACK),
  217. we must return socket to time-wait state. It is not good,
  218. but not fatal yet.
  219. */
  220. if (th->syn && !th->rst && !th->ack && !paws_reject &&
  221. (after(TCP_SKB_CB(skb)->seq, rcv_nxt) ||
  222. (tmp_opt.saw_tstamp &&
  223. (s32)(READ_ONCE(tcptw->tw_ts_recent) - tmp_opt.rcv_tsval) < 0))) {
  224. u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
  225. if (isn == 0)
  226. isn++;
  227. *tw_isn = isn;
  228. return TCP_TW_SYN;
  229. }
  230. if (psp_drop)
  231. goto out_put;
  232. if (paws_reject) {
  233. *drop_reason = SKB_DROP_REASON_TCP_RFC7323_TW_PAWS;
  234. __NET_INC_STATS(twsk_net(tw), LINUX_MIB_PAWS_TW_REJECTED);
  235. }
  236. if (!th->rst) {
  237. /* In this case we must reset the TIMEWAIT timer.
  238. *
  239. * If it is ACKless SYN it may be both old duplicate
  240. * and new good SYN with random sequence number <rcv_nxt.
  241. * Do not reschedule in the last case.
  242. */
  243. if (paws_reject || th->ack)
  244. inet_twsk_reschedule(tw, TCP_TIMEWAIT_LEN);
  245. return tcp_timewait_check_oow_rate_limit(
  246. tw, skb, LINUX_MIB_TCPACKSKIPPEDTIMEWAIT);
  247. }
  248. out_put:
  249. inet_twsk_put(tw);
  250. return TCP_TW_SUCCESS;
  251. }
  252. EXPORT_IPV6_MOD(tcp_timewait_state_process);
  253. static void tcp_time_wait_init(struct sock *sk, struct tcp_timewait_sock *tcptw)
  254. {
  255. #ifdef CONFIG_TCP_MD5SIG
  256. const struct tcp_sock *tp = tcp_sk(sk);
  257. struct tcp_md5sig_key *key;
  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. tcptw->tw_md5_key = NULL;
  265. if (!static_branch_unlikely(&tcp_md5_needed.key))
  266. return;
  267. key = tp->af_specific->md5_lookup(sk, sk);
  268. if (key) {
  269. tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC);
  270. if (!tcptw->tw_md5_key)
  271. return;
  272. if (!static_key_fast_inc_not_disabled(&tcp_md5_needed.key.key))
  273. goto out_free;
  274. }
  275. return;
  276. out_free:
  277. WARN_ON_ONCE(1);
  278. kfree(tcptw->tw_md5_key);
  279. tcptw->tw_md5_key = NULL;
  280. #endif
  281. }
  282. /*
  283. * Move a socket to time-wait or dead fin-wait-2 state.
  284. */
  285. void tcp_time_wait(struct sock *sk, int state, int timeo)
  286. {
  287. const struct inet_connection_sock *icsk = inet_csk(sk);
  288. struct tcp_sock *tp = tcp_sk(sk);
  289. struct net *net = sock_net(sk);
  290. struct inet_timewait_sock *tw;
  291. tw = inet_twsk_alloc(sk, &net->ipv4.tcp_death_row, state);
  292. if (tw) {
  293. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  294. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  295. tw->tw_mark = sk->sk_mark;
  296. tw->tw_priority = READ_ONCE(sk->sk_priority);
  297. tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
  298. /* refreshed when we enter true TIME-WAIT state */
  299. tw->tw_entry_stamp = tcp_time_stamp_ms(tp);
  300. tcptw->tw_rcv_nxt = tp->rcv_nxt;
  301. tcptw->tw_snd_nxt = tp->snd_nxt;
  302. tcptw->tw_rcv_wnd = tcp_receive_window(tp);
  303. tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
  304. tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
  305. tcptw->tw_ts_offset = tp->tsoffset;
  306. tw->tw_usec_ts = tp->tcp_usec_ts;
  307. tcptw->tw_last_oow_ack_time = 0;
  308. tcptw->tw_tx_delay = tp->tcp_tx_delay;
  309. tw->tw_txhash = sk->sk_txhash;
  310. tw->tw_tx_queue_mapping = sk->sk_tx_queue_mapping;
  311. #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
  312. tw->tw_rx_queue_mapping = sk->sk_rx_queue_mapping;
  313. #endif
  314. #if IS_ENABLED(CONFIG_IPV6)
  315. if (tw->tw_family == PF_INET6) {
  316. struct ipv6_pinfo *np = inet6_sk(sk);
  317. tw->tw_v6_daddr = sk->sk_v6_daddr;
  318. tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  319. tw->tw_tclass = np->tclass;
  320. tw->tw_flowlabel = be32_to_cpu(np->flow_label & IPV6_FLOWLABEL_MASK);
  321. tw->tw_ipv6only = sk->sk_ipv6only;
  322. }
  323. #endif
  324. tcp_time_wait_init(sk, tcptw);
  325. tcp_ao_time_wait(tcptw, tp);
  326. /* Get the TIME_WAIT timeout firing. */
  327. if (timeo < rto)
  328. timeo = rto;
  329. if (state == TCP_TIME_WAIT)
  330. timeo = TCP_TIMEWAIT_LEN;
  331. /* Linkage updates.
  332. * Note that access to tw after this point is illegal.
  333. */
  334. inet_twsk_hashdance_schedule(tw, sk, net->ipv4.tcp_death_row.hashinfo, timeo);
  335. } else {
  336. /* Sorry, if we're out of memory, just CLOSE this
  337. * socket up. We've got bigger problems than
  338. * non-graceful socket closings.
  339. */
  340. NET_INC_STATS(net, LINUX_MIB_TCPTIMEWAITOVERFLOW);
  341. }
  342. tcp_update_metrics(sk);
  343. tcp_done(sk);
  344. }
  345. EXPORT_SYMBOL(tcp_time_wait);
  346. void tcp_twsk_destructor(struct sock *sk)
  347. {
  348. #ifdef CONFIG_TCP_MD5SIG
  349. if (static_branch_unlikely(&tcp_md5_needed.key)) {
  350. struct tcp_timewait_sock *twsk = tcp_twsk(sk);
  351. if (twsk->tw_md5_key) {
  352. kfree(twsk->tw_md5_key);
  353. static_branch_slow_dec_deferred(&tcp_md5_needed);
  354. }
  355. }
  356. #endif
  357. tcp_ao_destroy_sock(sk, true);
  358. psp_twsk_assoc_free(inet_twsk(sk));
  359. }
  360. void tcp_twsk_purge(struct list_head *net_exit_list)
  361. {
  362. bool purged_once = false;
  363. struct net *net;
  364. list_for_each_entry(net, net_exit_list, exit_list) {
  365. if (net->ipv4.tcp_death_row.hashinfo->pernet) {
  366. /* Even if tw_refcount == 1, we must clean up kernel reqsk */
  367. inet_twsk_purge(net->ipv4.tcp_death_row.hashinfo);
  368. } else if (!purged_once) {
  369. inet_twsk_purge(&tcp_hashinfo);
  370. purged_once = true;
  371. }
  372. }
  373. }
  374. /* Warning : This function is called without sk_listener being locked.
  375. * Be sure to read socket fields once, as their value could change under us.
  376. */
  377. void tcp_openreq_init_rwin(struct request_sock *req,
  378. const struct sock *sk_listener,
  379. const struct dst_entry *dst)
  380. {
  381. struct inet_request_sock *ireq = inet_rsk(req);
  382. const struct tcp_sock *tp = tcp_sk(sk_listener);
  383. int full_space = tcp_full_space(sk_listener);
  384. u32 window_clamp;
  385. __u8 rcv_wscale;
  386. u32 rcv_wnd;
  387. int mss;
  388. mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
  389. window_clamp = READ_ONCE(tp->window_clamp);
  390. /* Set this up on the first call only */
  391. req->rsk_window_clamp = window_clamp ? : dst_metric(dst, RTAX_WINDOW);
  392. /* limit the window selection if the user enforce a smaller rx buffer */
  393. if (sk_listener->sk_userlocks & SOCK_RCVBUF_LOCK &&
  394. (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
  395. req->rsk_window_clamp = full_space;
  396. rcv_wnd = tcp_rwnd_init_bpf((struct sock *)req);
  397. if (rcv_wnd == 0)
  398. rcv_wnd = dst_metric(dst, RTAX_INITRWND);
  399. else if (full_space < rcv_wnd * mss)
  400. full_space = rcv_wnd * mss;
  401. /* tcp_full_space because it is guaranteed to be the first packet */
  402. tcp_select_initial_window(sk_listener, full_space,
  403. mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
  404. &req->rsk_rcv_wnd,
  405. &req->rsk_window_clamp,
  406. ireq->wscale_ok,
  407. &rcv_wscale,
  408. rcv_wnd);
  409. ireq->rcv_wscale = rcv_wscale;
  410. }
  411. static void tcp_ecn_openreq_child(struct sock *sk,
  412. const struct request_sock *req,
  413. const struct sk_buff *skb)
  414. {
  415. const struct tcp_request_sock *treq = tcp_rsk(req);
  416. struct tcp_sock *tp = tcp_sk(sk);
  417. if (treq->accecn_ok) {
  418. tcp_ecn_mode_set(tp, TCP_ECN_MODE_ACCECN);
  419. tp->syn_ect_snt = treq->syn_ect_snt;
  420. tcp_accecn_third_ack(sk, skb, treq->syn_ect_snt);
  421. tp->saw_accecn_opt = treq->saw_accecn_opt;
  422. if (treq->accecn_fail_mode & TCP_ACCECN_ACE_FAIL_SEND)
  423. tcp_accecn_fail_mode_set(tp, TCP_ACCECN_ACE_FAIL_SEND);
  424. if (treq->accecn_fail_mode & TCP_ACCECN_ACE_FAIL_RECV)
  425. tcp_accecn_fail_mode_set(tp, TCP_ACCECN_ACE_FAIL_RECV);
  426. tp->prev_ecnfield = treq->syn_ect_rcv;
  427. tp->accecn_opt_demand = 1;
  428. tcp_ecn_received_counters_payload(sk, skb);
  429. } else {
  430. if (inet_rsk(req)->ecn_ok && !tcp_ca_no_fallback_rfc3168(sk))
  431. tcp_ecn_mode_set(tp, TCP_ECN_MODE_RFC3168);
  432. else
  433. tcp_ecn_mode_set(tp, TCP_ECN_DISABLED);
  434. }
  435. }
  436. void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
  437. {
  438. struct inet_connection_sock *icsk = inet_csk(sk);
  439. u32 ca_key = dst_metric(dst, RTAX_CC_ALGO);
  440. bool ca_got_dst = false;
  441. if (ca_key != TCP_CA_UNSPEC) {
  442. const struct tcp_congestion_ops *ca;
  443. rcu_read_lock();
  444. ca = tcp_ca_find_key(ca_key);
  445. if (likely(ca && bpf_try_module_get(ca, ca->owner))) {
  446. icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
  447. icsk->icsk_ca_ops = ca;
  448. ca_got_dst = true;
  449. }
  450. rcu_read_unlock();
  451. }
  452. /* If no valid choice made yet, assign current system default ca. */
  453. if (!ca_got_dst &&
  454. (!icsk->icsk_ca_setsockopt ||
  455. !bpf_try_module_get(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner)))
  456. tcp_assign_congestion_control(sk);
  457. tcp_set_ca_state(sk, TCP_CA_Open);
  458. }
  459. EXPORT_IPV6_MOD_GPL(tcp_ca_openreq_child);
  460. static void smc_check_reset_syn_req(const struct tcp_sock *oldtp,
  461. struct request_sock *req,
  462. struct tcp_sock *newtp)
  463. {
  464. #if IS_ENABLED(CONFIG_SMC)
  465. struct inet_request_sock *ireq;
  466. if (static_branch_unlikely(&tcp_have_smc)) {
  467. ireq = inet_rsk(req);
  468. if (oldtp->syn_smc && !ireq->smc_ok)
  469. newtp->syn_smc = 0;
  470. }
  471. #endif
  472. }
  473. /* This is not only more efficient than what we used to do, it eliminates
  474. * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
  475. *
  476. * Actually, we could lots of memory writes here. tp of listening
  477. * socket contains all necessary default parameters.
  478. */
  479. struct sock *tcp_create_openreq_child(const struct sock *sk,
  480. struct request_sock *req,
  481. struct sk_buff *skb)
  482. {
  483. struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
  484. const struct inet_request_sock *ireq = inet_rsk(req);
  485. struct tcp_request_sock *treq = tcp_rsk(req);
  486. struct inet_connection_sock *newicsk;
  487. const struct tcp_sock *oldtp;
  488. struct tcp_sock *newtp;
  489. u32 seq;
  490. if (!newsk)
  491. return NULL;
  492. newicsk = inet_csk(newsk);
  493. newtp = tcp_sk(newsk);
  494. oldtp = tcp_sk(sk);
  495. smc_check_reset_syn_req(oldtp, req, newtp);
  496. /* Now setup tcp_sock */
  497. newtp->pred_flags = 0;
  498. seq = treq->rcv_isn + 1;
  499. newtp->rcv_wup = seq;
  500. WRITE_ONCE(newtp->copied_seq, seq);
  501. WRITE_ONCE(newtp->rcv_nxt, seq);
  502. newtp->segs_in = 1;
  503. seq = treq->snt_isn + 1;
  504. newtp->snd_sml = newtp->snd_una = seq;
  505. WRITE_ONCE(newtp->snd_nxt, seq);
  506. newtp->snd_up = seq;
  507. INIT_LIST_HEAD(&newtp->tsq_node);
  508. INIT_LIST_HEAD(&newtp->tsorted_sent_queue);
  509. tcp_init_wl(newtp, treq->rcv_isn);
  510. minmax_reset(&newtp->rtt_min, tcp_jiffies32, ~0U);
  511. newicsk->icsk_ack.lrcvtime = tcp_jiffies32;
  512. newtp->lsndtime = tcp_jiffies32;
  513. newsk->sk_txhash = READ_ONCE(treq->txhash);
  514. newtp->total_retrans = req->num_retrans;
  515. tcp_init_xmit_timers(newsk);
  516. WRITE_ONCE(newtp->write_seq, newtp->pushed_seq = treq->snt_isn + 1);
  517. if (sock_flag(newsk, SOCK_KEEPOPEN))
  518. tcp_reset_keepalive_timer(newsk, keepalive_time_when(newtp));
  519. newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
  520. newtp->rx_opt.sack_ok = ireq->sack_ok;
  521. newtp->window_clamp = req->rsk_window_clamp;
  522. newtp->rcv_ssthresh = req->rsk_rcv_wnd;
  523. newtp->rcv_wnd = req->rsk_rcv_wnd;
  524. newtp->rx_opt.wscale_ok = ireq->wscale_ok;
  525. if (newtp->rx_opt.wscale_ok) {
  526. newtp->rx_opt.snd_wscale = ireq->snd_wscale;
  527. newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
  528. } else {
  529. newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
  530. newtp->window_clamp = min(newtp->window_clamp, 65535U);
  531. }
  532. newtp->snd_wnd = ntohs(tcp_hdr(skb)->window) << newtp->rx_opt.snd_wscale;
  533. newtp->max_window = newtp->snd_wnd;
  534. if (newtp->rx_opt.tstamp_ok) {
  535. newtp->tcp_usec_ts = treq->req_usec_ts;
  536. newtp->rx_opt.ts_recent = req->ts_recent;
  537. newtp->rx_opt.ts_recent_stamp = ktime_get_seconds();
  538. newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  539. } else {
  540. newtp->tcp_usec_ts = 0;
  541. newtp->rx_opt.ts_recent_stamp = 0;
  542. newtp->tcp_header_len = sizeof(struct tcphdr);
  543. }
  544. if (req->num_timeout) {
  545. newtp->total_rto = req->num_timeout;
  546. newtp->undo_marker = treq->snt_isn;
  547. if (newtp->tcp_usec_ts) {
  548. newtp->retrans_stamp = treq->snt_synack;
  549. newtp->total_rto_time = (u32)(tcp_clock_us() -
  550. newtp->retrans_stamp) / USEC_PER_MSEC;
  551. } else {
  552. newtp->retrans_stamp = div_u64(treq->snt_synack,
  553. USEC_PER_SEC / TCP_TS_HZ);
  554. newtp->total_rto_time = tcp_clock_ms() -
  555. newtp->retrans_stamp;
  556. }
  557. newtp->total_rto_recoveries = 1;
  558. }
  559. newtp->tsoffset = treq->ts_off;
  560. #ifdef CONFIG_TCP_MD5SIG
  561. newtp->md5sig_info = NULL; /*XXX*/
  562. #endif
  563. #ifdef CONFIG_TCP_AO
  564. newtp->ao_info = NULL;
  565. if (tcp_rsk_used_ao(req)) {
  566. struct tcp_ao_key *ao_key;
  567. ao_key = treq->af_specific->ao_lookup(sk, req, tcp_rsk(req)->ao_keyid, -1);
  568. if (ao_key)
  569. newtp->tcp_header_len += tcp_ao_len_aligned(ao_key);
  570. }
  571. #endif
  572. if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
  573. newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
  574. newtp->rx_opt.mss_clamp = req->mss;
  575. tcp_ecn_openreq_child(newsk, req, skb);
  576. newtp->fastopen_req = NULL;
  577. RCU_INIT_POINTER(newtp->fastopen_rsk, NULL);
  578. newtp->bpf_chg_cc_inprogress = 0;
  579. tcp_bpf_clone(sk, newsk);
  580. __TCP_INC_STATS(sock_net(sk), TCP_MIB_PASSIVEOPENS);
  581. xa_init_flags(&newsk->sk_user_frags, XA_FLAGS_ALLOC1);
  582. return newsk;
  583. }
  584. EXPORT_SYMBOL(tcp_create_openreq_child);
  585. /*
  586. * Process an incoming packet for SYN_RECV sockets represented as a
  587. * request_sock. Normally sk is the listener socket but for TFO it
  588. * points to the child socket.
  589. *
  590. * XXX (TFO) - The current impl contains a special check for ack
  591. * validation and inside tcp_v4_reqsk_send_ack(). Can we do better?
  592. *
  593. * We don't need to initialize tmp_opt.sack_ok as we don't use the results
  594. *
  595. * Note: If @fastopen is true, this can be called from process context.
  596. * Otherwise, this is from BH context.
  597. */
  598. struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
  599. struct request_sock *req,
  600. bool fastopen, bool *req_stolen,
  601. enum skb_drop_reason *drop_reason)
  602. {
  603. struct tcp_options_received tmp_opt;
  604. struct sock *child;
  605. const struct tcphdr *th = tcp_hdr(skb);
  606. __be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
  607. bool tsecr_reject = false;
  608. bool paws_reject = false;
  609. bool own_req;
  610. tmp_opt.saw_tstamp = 0;
  611. tmp_opt.accecn = 0;
  612. if (th->doff > (sizeof(struct tcphdr)>>2)) {
  613. tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0, NULL);
  614. if (tmp_opt.saw_tstamp) {
  615. tmp_opt.ts_recent = req->ts_recent;
  616. if (tmp_opt.rcv_tsecr) {
  617. if (inet_rsk(req)->tstamp_ok && !fastopen)
  618. tsecr_reject = !between(tmp_opt.rcv_tsecr,
  619. tcp_rsk(req)->snt_tsval_first,
  620. READ_ONCE(tcp_rsk(req)->snt_tsval_last));
  621. tmp_opt.rcv_tsecr -= tcp_rsk(req)->ts_off;
  622. }
  623. /* We do not store true stamp, but it is not required,
  624. * it can be estimated (approximately)
  625. * from another data.
  626. */
  627. tmp_opt.ts_recent_stamp = ktime_get_seconds() -
  628. tcp_reqsk_timeout(req) / HZ;
  629. paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
  630. }
  631. }
  632. /* Check for pure retransmitted SYN. */
  633. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
  634. flg == TCP_FLAG_SYN &&
  635. !paws_reject) {
  636. /*
  637. * RFC793 draws (Incorrectly! It was fixed in RFC1122)
  638. * this case on figure 6 and figure 8, but formal
  639. * protocol description says NOTHING.
  640. * To be more exact, it says that we should send ACK,
  641. * because this segment (at least, if it has no data)
  642. * is out of window.
  643. *
  644. * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
  645. * describe SYN-RECV state. All the description
  646. * is wrong, we cannot believe to it and should
  647. * rely only on common sense and implementation
  648. * experience.
  649. *
  650. * Enforce "SYN-ACK" according to figure 8, figure 6
  651. * of RFC793, fixed by RFC1122.
  652. *
  653. * Note that even if there is new data in the SYN packet
  654. * they will be thrown away too.
  655. *
  656. * Reset timer after retransmitting SYNACK, similar to
  657. * the idea of fast retransmit in recovery.
  658. */
  659. if (!tcp_oow_rate_limited(sock_net(sk), skb,
  660. LINUX_MIB_TCPACKSKIPPEDSYNRECV,
  661. &tcp_rsk(req)->last_oow_ack_time)) {
  662. if (tcp_rsk(req)->accecn_ok) {
  663. u8 ect_rcv = TCP_SKB_CB(skb)->ip_dsfield &
  664. INET_ECN_MASK;
  665. tcp_rsk(req)->syn_ect_rcv = ect_rcv;
  666. if (tcp_accecn_ace(tcp_hdr(skb)) == 0x0)
  667. tcp_rsk(req)->accecn_fail_mode |= TCP_ACCECN_ACE_FAIL_RECV;
  668. }
  669. if (!tcp_rtx_synack(sk, req)) {
  670. unsigned long expires = jiffies;
  671. if (req->num_retrans > 1 && tcp_rsk(req)->accecn_ok)
  672. tcp_rsk(req)->accecn_fail_mode |= TCP_ACCECN_ACE_FAIL_SEND;
  673. expires += tcp_reqsk_timeout(req);
  674. if (!fastopen)
  675. mod_timer_pending(&req->rsk_timer,
  676. expires);
  677. else
  678. req->rsk_timer.expires = expires;
  679. }
  680. }
  681. return NULL;
  682. }
  683. /* Further reproduces section "SEGMENT ARRIVES"
  684. for state SYN-RECEIVED of RFC793.
  685. It is broken, however, it does not work only
  686. when SYNs are crossed.
  687. You would think that SYN crossing is impossible here, since
  688. we should have a SYN_SENT socket (from connect()) on our end,
  689. but this is not true if the crossed SYNs were sent to both
  690. ends by a malicious third party. We must defend against this,
  691. and to do that we first verify the ACK (as per RFC793, page
  692. 36) and reset if it is invalid. Is this a true full defense?
  693. To convince ourselves, let us consider a way in which the ACK
  694. test can still pass in this 'malicious crossed SYNs' case.
  695. Malicious sender sends identical SYNs (and thus identical sequence
  696. numbers) to both A and B:
  697. A: gets SYN, seq=7
  698. B: gets SYN, seq=7
  699. By our good fortune, both A and B select the same initial
  700. send sequence number of seven :-)
  701. A: sends SYN|ACK, seq=7, ack_seq=8
  702. B: sends SYN|ACK, seq=7, ack_seq=8
  703. So we are now A eating this SYN|ACK, ACK test passes. So
  704. does sequence test, SYN is truncated, and thus we consider
  705. it a bare ACK.
  706. If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
  707. bare ACK. Otherwise, we create an established connection. Both
  708. ends (listening sockets) accept the new incoming connection and try
  709. to talk to each other. 8-)
  710. Note: This case is both harmless, and rare. Possibility is about the
  711. same as us discovering intelligent life on another plant tomorrow.
  712. But generally, we should (RFC lies!) to accept ACK
  713. from SYNACK both here and in tcp_rcv_state_process().
  714. tcp_rcv_state_process() does not, hence, we do not too.
  715. Note that the case is absolutely generic:
  716. we cannot optimize anything here without
  717. violating protocol. All the checks must be made
  718. before attempt to create socket.
  719. */
  720. /* RFC793 page 36: "If the connection is in any non-synchronized state ...
  721. * and the incoming segment acknowledges something not yet
  722. * sent (the segment carries an unacceptable ACK) ...
  723. * a reset is sent."
  724. *
  725. * Invalid ACK: reset will be sent by listening socket.
  726. * Note that the ACK validity check for a Fast Open socket is done
  727. * elsewhere and is checked directly against the child socket rather
  728. * than req because user data may have been sent out.
  729. */
  730. if ((flg & TCP_FLAG_ACK) && !fastopen &&
  731. (TCP_SKB_CB(skb)->ack_seq !=
  732. tcp_rsk(req)->snt_isn + 1))
  733. return sk;
  734. /* RFC793: "first check sequence number". */
  735. if (paws_reject || tsecr_reject ||
  736. !tcp_in_window(TCP_SKB_CB(skb)->seq,
  737. TCP_SKB_CB(skb)->end_seq,
  738. tcp_rsk(req)->rcv_nxt,
  739. tcp_rsk(req)->rcv_nxt +
  740. tcp_synack_window(req))) {
  741. /* Out of window: send ACK and drop. */
  742. if (!(flg & TCP_FLAG_RST) &&
  743. !tcp_oow_rate_limited(sock_net(sk), skb,
  744. LINUX_MIB_TCPACKSKIPPEDSYNRECV,
  745. &tcp_rsk(req)->last_oow_ack_time))
  746. req->rsk_ops->send_ack(sk, skb, req);
  747. if (paws_reject) {
  748. SKB_DR_SET(*drop_reason, TCP_RFC7323_PAWS);
  749. NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
  750. } else if (tsecr_reject) {
  751. SKB_DR_SET(*drop_reason, TCP_RFC7323_TSECR);
  752. NET_INC_STATS(sock_net(sk), LINUX_MIB_TSECRREJECTED);
  753. } else {
  754. SKB_DR_SET(*drop_reason, TCP_OVERWINDOW);
  755. }
  756. return NULL;
  757. }
  758. /* In sequence, PAWS is OK. */
  759. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
  760. /* Truncate SYN, it is out of window starting
  761. at tcp_rsk(req)->rcv_isn + 1. */
  762. flg &= ~TCP_FLAG_SYN;
  763. }
  764. /* RFC793: "second check the RST bit" and
  765. * "fourth, check the SYN bit"
  766. */
  767. if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
  768. TCP_INC_STATS(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
  769. goto embryonic_reset;
  770. }
  771. /* ACK sequence verified above, just make sure ACK is
  772. * set. If ACK not set, just silently drop the packet.
  773. *
  774. * XXX (TFO) - if we ever allow "data after SYN", the
  775. * following check needs to be removed.
  776. */
  777. if (!(flg & TCP_FLAG_ACK))
  778. return NULL;
  779. if (tcp_rsk(req)->accecn_ok && tmp_opt.accecn &&
  780. tcp_rsk(req)->saw_accecn_opt < TCP_ACCECN_OPT_COUNTER_SEEN) {
  781. u8 saw_opt = tcp_accecn_option_init(skb, tmp_opt.accecn);
  782. tcp_rsk(req)->saw_accecn_opt = saw_opt;
  783. if (tcp_rsk(req)->saw_accecn_opt == TCP_ACCECN_OPT_FAIL_SEEN) {
  784. u8 fail_mode = TCP_ACCECN_OPT_FAIL_RECV;
  785. tcp_rsk(req)->accecn_fail_mode |= fail_mode;
  786. }
  787. }
  788. /* For Fast Open no more processing is needed (sk is the
  789. * child socket).
  790. */
  791. if (fastopen)
  792. return sk;
  793. /* While TCP_DEFER_ACCEPT is active, drop bare ACK. */
  794. if (req->num_timeout < READ_ONCE(inet_csk(sk)->icsk_accept_queue.rskq_defer_accept) &&
  795. TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
  796. inet_rsk(req)->acked = 1;
  797. __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDEFERACCEPTDROP);
  798. return NULL;
  799. }
  800. /* OK, ACK is valid, create big socket and
  801. * feed this segment to it. It will repeat all
  802. * the tests. THIS SEGMENT MUST MOVE SOCKET TO
  803. * ESTABLISHED STATE. If it will be dropped after
  804. * socket is created, wait for troubles.
  805. */
  806. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
  807. req, &own_req, NULL);
  808. if (!child)
  809. goto listen_overflow;
  810. if (own_req && tmp_opt.saw_tstamp &&
  811. !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_nxt))
  812. tcp_sk(child)->rx_opt.ts_recent = tmp_opt.rcv_tsval;
  813. if (own_req && rsk_drop_req(req)) {
  814. reqsk_queue_removed(&inet_csk(req->rsk_listener)->icsk_accept_queue, req);
  815. inet_csk_reqsk_queue_drop_and_put(req->rsk_listener, req);
  816. return child;
  817. }
  818. sock_rps_save_rxhash(child, skb);
  819. tcp_synack_rtt_meas(child, req);
  820. *req_stolen = !own_req;
  821. return inet_csk_complete_hashdance(sk, child, req, own_req);
  822. listen_overflow:
  823. SKB_DR_SET(*drop_reason, TCP_LISTEN_OVERFLOW);
  824. if (sk != req->rsk_listener)
  825. __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMIGRATEREQFAILURE);
  826. if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_abort_on_overflow)) {
  827. inet_rsk(req)->acked = 1;
  828. return NULL;
  829. }
  830. embryonic_reset:
  831. if (!(flg & TCP_FLAG_RST)) {
  832. /* Received a bad SYN pkt - for TFO We try not to reset
  833. * the local connection unless it's really necessary to
  834. * avoid becoming vulnerable to outside attack aiming at
  835. * resetting legit local connections.
  836. */
  837. req->rsk_ops->send_reset(sk, skb, SK_RST_REASON_INVALID_SYN);
  838. } else if (fastopen) { /* received a valid RST pkt */
  839. reqsk_fastopen_remove(sk, req, true);
  840. tcp_reset(sk, skb);
  841. }
  842. if (!fastopen) {
  843. bool unlinked = inet_csk_reqsk_queue_drop(sk, req);
  844. if (unlinked)
  845. __NET_INC_STATS(sock_net(sk), LINUX_MIB_EMBRYONICRSTS);
  846. *req_stolen = !unlinked;
  847. }
  848. return NULL;
  849. }
  850. EXPORT_IPV6_MOD(tcp_check_req);
  851. /*
  852. * Queue segment on the new socket if the new socket is active,
  853. * otherwise we just shortcircuit this and continue with
  854. * the new socket.
  855. *
  856. * For the vast majority of cases child->sk_state will be TCP_SYN_RECV
  857. * when entering. But other states are possible due to a race condition
  858. * where after __inet_lookup_established() fails but before the listener
  859. * locked is obtained, other packets cause the same connection to
  860. * be created.
  861. */
  862. enum skb_drop_reason tcp_child_process(struct sock *parent, struct sock *child,
  863. struct sk_buff *skb)
  864. __releases(&((child)->sk_lock.slock))
  865. {
  866. enum skb_drop_reason reason = SKB_NOT_DROPPED_YET;
  867. int state = child->sk_state;
  868. /* record sk_napi_id and sk_rx_queue_mapping of child. */
  869. sk_mark_napi_id_set(child, skb);
  870. tcp_segs_in(tcp_sk(child), skb);
  871. if (!sock_owned_by_user(child)) {
  872. reason = tcp_rcv_state_process(child, skb);
  873. /* Wakeup parent, send SIGIO */
  874. if (state == TCP_SYN_RECV && child->sk_state != state)
  875. READ_ONCE(parent->sk_data_ready)(parent);
  876. } else {
  877. /* Alas, it is possible again, because we do lookup
  878. * in main socket hash table and lock on listening
  879. * socket does not protect us more.
  880. */
  881. __sk_add_backlog(child, skb);
  882. }
  883. bh_unlock_sock(child);
  884. sock_put(child);
  885. return reason;
  886. }
  887. EXPORT_IPV6_MOD(tcp_child_process);