tcp_fastopen.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/tcp.h>
  4. #include <linux/rcupdate.h>
  5. #include <net/tcp.h>
  6. #include <net/busy_poll.h>
  7. /*
  8. * This function is called to set a Fast Open socket's "fastopen_rsk" field
  9. * to NULL when a TFO socket no longer needs to access the request_sock.
  10. * This happens only after 3WHS has been either completed or aborted (e.g.,
  11. * RST is received).
  12. *
  13. * Before TFO, a child socket is created only after 3WHS is completed,
  14. * hence it never needs to access the request_sock. things get a lot more
  15. * complex with TFO. A child socket, accepted or not, has to access its
  16. * request_sock for 3WHS processing, e.g., to retransmit SYN-ACK pkts,
  17. * until 3WHS is either completed or aborted. Afterwards the req will stay
  18. * until either the child socket is accepted, or in the rare case when the
  19. * listener is closed before the child is accepted.
  20. *
  21. * In short, a request socket is only freed after BOTH 3WHS has completed
  22. * (or aborted) and the child socket has been accepted (or listener closed).
  23. * When a child socket is accepted, its corresponding req->sk is set to
  24. * NULL since it's no longer needed. More importantly, "req->sk == NULL"
  25. * will be used by the code below to determine if a child socket has been
  26. * accepted or not, and the check is protected by the fastopenq->lock
  27. * described below.
  28. *
  29. * Note that fastopen_rsk is only accessed from the child socket's context
  30. * with its socket lock held. But a request_sock (req) can be accessed by
  31. * both its child socket through fastopen_rsk, and a listener socket through
  32. * icsk_accept_queue.rskq_accept_head. To protect the access a simple spin
  33. * lock per listener "icsk->icsk_accept_queue.fastopenq->lock" is created.
  34. * only in the rare case when both the listener and the child locks are held,
  35. * e.g., in inet_csk_listen_stop() do we not need to acquire the lock.
  36. * The lock also protects other fields such as fastopenq->qlen, which is
  37. * decremented by this function when fastopen_rsk is no longer needed.
  38. *
  39. * Note that another solution was to simply use the existing socket lock
  40. * from the listener. But first socket lock is difficult to use. It is not
  41. * a simple spin lock - one must consider sock_owned_by_user() and arrange
  42. * to use sk_add_backlog() stuff. But what really makes it infeasible is the
  43. * locking hierarchy violation. E.g., inet_csk_listen_stop() may try to
  44. * acquire a child's lock while holding listener's socket lock.
  45. *
  46. * This function also sets "treq->tfo_listener" to false.
  47. * treq->tfo_listener is used by the listener so it is protected by the
  48. * fastopenq->lock in this function.
  49. */
  50. void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  51. bool reset)
  52. {
  53. struct sock *lsk = req->rsk_listener;
  54. struct fastopen_queue *fastopenq;
  55. fastopenq = &inet_csk(lsk)->icsk_accept_queue.fastopenq;
  56. RCU_INIT_POINTER(tcp_sk(sk)->fastopen_rsk, NULL);
  57. spin_lock_bh(&fastopenq->lock);
  58. fastopenq->qlen--;
  59. tcp_rsk(req)->tfo_listener = false;
  60. if (req->sk) /* the child socket hasn't been accepted yet */
  61. goto out;
  62. if (!reset || lsk->sk_state != TCP_LISTEN) {
  63. /* If the listener has been closed don't bother with the
  64. * special RST handling below.
  65. */
  66. spin_unlock_bh(&fastopenq->lock);
  67. reqsk_put(req);
  68. return;
  69. }
  70. /* Wait for 60secs before removing a req that has triggered RST.
  71. * This is a simple defense against TFO spoofing attack - by
  72. * counting the req against fastopen.max_qlen, and disabling
  73. * TFO when the qlen exceeds max_qlen.
  74. *
  75. * For more details see CoNext'11 "TCP Fast Open" paper.
  76. */
  77. req->rsk_timer.expires = jiffies + 60*HZ;
  78. if (fastopenq->rskq_rst_head == NULL)
  79. fastopenq->rskq_rst_head = req;
  80. else
  81. fastopenq->rskq_rst_tail->dl_next = req;
  82. req->dl_next = NULL;
  83. fastopenq->rskq_rst_tail = req;
  84. fastopenq->qlen++;
  85. out:
  86. spin_unlock_bh(&fastopenq->lock);
  87. }
  88. void tcp_fastopen_init_key_once(struct net *net)
  89. {
  90. u8 key[TCP_FASTOPEN_KEY_LENGTH];
  91. struct tcp_fastopen_context *ctxt;
  92. rcu_read_lock();
  93. ctxt = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
  94. if (ctxt) {
  95. rcu_read_unlock();
  96. return;
  97. }
  98. rcu_read_unlock();
  99. /* tcp_fastopen_reset_cipher publishes the new context
  100. * atomically, so we allow this race happening here.
  101. *
  102. * All call sites of tcp_fastopen_cookie_gen also check
  103. * for a valid cookie, so this is an acceptable risk.
  104. */
  105. get_random_bytes(key, sizeof(key));
  106. tcp_fastopen_reset_cipher(net, NULL, key, NULL);
  107. }
  108. static void tcp_fastopen_ctx_free(struct rcu_head *head)
  109. {
  110. struct tcp_fastopen_context *ctx =
  111. container_of(head, struct tcp_fastopen_context, rcu);
  112. kfree_sensitive(ctx);
  113. }
  114. void tcp_fastopen_destroy_cipher(struct sock *sk)
  115. {
  116. struct tcp_fastopen_context *ctx;
  117. ctx = rcu_dereference_protected(
  118. inet_csk(sk)->icsk_accept_queue.fastopenq.ctx, 1);
  119. if (ctx)
  120. call_rcu(&ctx->rcu, tcp_fastopen_ctx_free);
  121. }
  122. void tcp_fastopen_ctx_destroy(struct net *net)
  123. {
  124. struct tcp_fastopen_context *ctxt;
  125. ctxt = unrcu_pointer(xchg(&net->ipv4.tcp_fastopen_ctx, NULL));
  126. if (ctxt)
  127. call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free);
  128. }
  129. int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
  130. void *primary_key, void *backup_key)
  131. {
  132. struct tcp_fastopen_context *ctx, *octx;
  133. struct fastopen_queue *q;
  134. int err = 0;
  135. ctx = kmalloc_obj(*ctx);
  136. if (!ctx) {
  137. err = -ENOMEM;
  138. goto out;
  139. }
  140. ctx->key[0].key[0] = get_unaligned_le64(primary_key);
  141. ctx->key[0].key[1] = get_unaligned_le64(primary_key + 8);
  142. if (backup_key) {
  143. ctx->key[1].key[0] = get_unaligned_le64(backup_key);
  144. ctx->key[1].key[1] = get_unaligned_le64(backup_key + 8);
  145. ctx->num = 2;
  146. } else {
  147. ctx->num = 1;
  148. }
  149. if (sk) {
  150. q = &inet_csk(sk)->icsk_accept_queue.fastopenq;
  151. octx = unrcu_pointer(xchg(&q->ctx, RCU_INITIALIZER(ctx)));
  152. } else {
  153. octx = unrcu_pointer(xchg(&net->ipv4.tcp_fastopen_ctx,
  154. RCU_INITIALIZER(ctx)));
  155. }
  156. if (octx)
  157. call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
  158. out:
  159. return err;
  160. }
  161. int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
  162. u64 *key)
  163. {
  164. struct tcp_fastopen_context *ctx;
  165. int n_keys = 0, i;
  166. rcu_read_lock();
  167. if (icsk)
  168. ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx);
  169. else
  170. ctx = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
  171. if (ctx) {
  172. n_keys = tcp_fastopen_context_len(ctx);
  173. for (i = 0; i < n_keys; i++) {
  174. put_unaligned_le64(ctx->key[i].key[0], key + (i * 2));
  175. put_unaligned_le64(ctx->key[i].key[1], key + (i * 2) + 1);
  176. }
  177. }
  178. rcu_read_unlock();
  179. return n_keys;
  180. }
  181. static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req,
  182. struct sk_buff *syn,
  183. const siphash_key_t *key,
  184. struct tcp_fastopen_cookie *foc)
  185. {
  186. BUILD_BUG_ON(TCP_FASTOPEN_COOKIE_SIZE != sizeof(u64));
  187. if (req->rsk_ops->family == AF_INET) {
  188. const struct iphdr *iph = ip_hdr(syn);
  189. foc->val[0] = cpu_to_le64(siphash(&iph->saddr,
  190. sizeof(iph->saddr) +
  191. sizeof(iph->daddr),
  192. key));
  193. foc->len = TCP_FASTOPEN_COOKIE_SIZE;
  194. return true;
  195. }
  196. #if IS_ENABLED(CONFIG_IPV6)
  197. if (req->rsk_ops->family == AF_INET6) {
  198. const struct ipv6hdr *ip6h = ipv6_hdr(syn);
  199. foc->val[0] = cpu_to_le64(siphash(&ip6h->saddr,
  200. sizeof(ip6h->saddr) +
  201. sizeof(ip6h->daddr),
  202. key));
  203. foc->len = TCP_FASTOPEN_COOKIE_SIZE;
  204. return true;
  205. }
  206. #endif
  207. return false;
  208. }
  209. /* Generate the fastopen cookie by applying SipHash to both the source and
  210. * destination addresses.
  211. */
  212. static void tcp_fastopen_cookie_gen(struct sock *sk,
  213. struct request_sock *req,
  214. struct sk_buff *syn,
  215. struct tcp_fastopen_cookie *foc)
  216. {
  217. struct tcp_fastopen_context *ctx;
  218. rcu_read_lock();
  219. ctx = tcp_fastopen_get_ctx(sk);
  220. if (ctx)
  221. __tcp_fastopen_cookie_gen_cipher(req, syn, &ctx->key[0], foc);
  222. rcu_read_unlock();
  223. }
  224. /* If an incoming SYN or SYNACK frame contains a payload and/or FIN,
  225. * queue this additional data / FIN.
  226. */
  227. void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb)
  228. {
  229. struct tcp_sock *tp = tcp_sk(sk);
  230. if (TCP_SKB_CB(skb)->end_seq == tp->rcv_nxt)
  231. return;
  232. skb = skb_clone(skb, GFP_ATOMIC);
  233. if (!skb)
  234. return;
  235. tcp_cleanup_skb(skb);
  236. /* segs_in has been initialized to 1 in tcp_create_openreq_child().
  237. * Hence, reset segs_in to 0 before calling tcp_segs_in()
  238. * to avoid double counting. Also, tcp_segs_in() expects
  239. * skb->len to include the tcp_hdrlen. Hence, it should
  240. * be called before __skb_pull().
  241. */
  242. tp->segs_in = 0;
  243. tcp_segs_in(tp, skb);
  244. __skb_pull(skb, tcp_hdrlen(skb));
  245. sk_forced_mem_schedule(sk, skb->truesize);
  246. skb_set_owner_r(skb, sk);
  247. TCP_SKB_CB(skb)->seq++;
  248. TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN;
  249. tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  250. tcp_add_receive_queue(sk, skb);
  251. tp->syn_data_acked = 1;
  252. /* u64_stats_update_begin(&tp->syncp) not needed here,
  253. * as we certainly are not changing upper 32bit value (0)
  254. */
  255. tp->bytes_received = skb->len;
  256. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
  257. tcp_fin(sk);
  258. }
  259. /* returns 0 - no key match, 1 for primary, 2 for backup */
  260. static int tcp_fastopen_cookie_gen_check(struct sock *sk,
  261. struct request_sock *req,
  262. struct sk_buff *syn,
  263. struct tcp_fastopen_cookie *orig,
  264. struct tcp_fastopen_cookie *valid_foc)
  265. {
  266. struct tcp_fastopen_cookie search_foc = { .len = -1 };
  267. struct tcp_fastopen_cookie *foc = valid_foc;
  268. struct tcp_fastopen_context *ctx;
  269. int i, ret = 0;
  270. rcu_read_lock();
  271. ctx = tcp_fastopen_get_ctx(sk);
  272. if (!ctx)
  273. goto out;
  274. for (i = 0; i < tcp_fastopen_context_len(ctx); i++) {
  275. __tcp_fastopen_cookie_gen_cipher(req, syn, &ctx->key[i], foc);
  276. if (tcp_fastopen_cookie_match(foc, orig)) {
  277. ret = i + 1;
  278. goto out;
  279. }
  280. foc = &search_foc;
  281. }
  282. out:
  283. rcu_read_unlock();
  284. return ret;
  285. }
  286. static struct sock *tcp_fastopen_create_child(struct sock *sk,
  287. struct sk_buff *skb,
  288. struct request_sock *req)
  289. {
  290. struct tcp_sock *tp;
  291. struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
  292. struct sock *child;
  293. bool own_req;
  294. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
  295. NULL, &own_req, NULL);
  296. if (!child)
  297. return NULL;
  298. spin_lock(&queue->fastopenq.lock);
  299. queue->fastopenq.qlen++;
  300. spin_unlock(&queue->fastopenq.lock);
  301. /* Initialize the child socket. Have to fix some values to take
  302. * into account the child is a Fast Open socket and is created
  303. * only out of the bits carried in the SYN packet.
  304. */
  305. tp = tcp_sk(child);
  306. rcu_assign_pointer(tp->fastopen_rsk, req);
  307. tcp_rsk(req)->tfo_listener = true;
  308. /* RFC1323: The window in SYN & SYN/ACK segments is never
  309. * scaled. So correct it appropriately.
  310. */
  311. tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
  312. tp->max_window = tp->snd_wnd;
  313. /* Activate the retrans timer so that SYNACK can be retransmitted.
  314. * The request socket is not added to the ehash
  315. * because it's been added to the accept queue directly.
  316. */
  317. req->timeout = tcp_timeout_init(child);
  318. tcp_reset_xmit_timer(child, ICSK_TIME_RETRANS,
  319. req->timeout, false);
  320. refcount_set(&req->rsk_refcnt, 2);
  321. sk_mark_napi_id_set(child, skb);
  322. /* Now finish processing the fastopen child socket. */
  323. tcp_init_transfer(child, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, skb);
  324. tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
  325. tcp_fastopen_add_skb(child, skb);
  326. tcp_rsk(req)->rcv_nxt = tp->rcv_nxt;
  327. tp->rcv_wup = tp->rcv_nxt;
  328. /* tcp_conn_request() is sending the SYNACK,
  329. * and queues the child into listener accept queue.
  330. */
  331. return child;
  332. }
  333. static bool tcp_fastopen_queue_check(struct sock *sk)
  334. {
  335. struct fastopen_queue *fastopenq;
  336. int max_qlen;
  337. /* Make sure the listener has enabled fastopen, and we don't
  338. * exceed the max # of pending TFO requests allowed before trying
  339. * to validating the cookie in order to avoid burning CPU cycles
  340. * unnecessarily.
  341. *
  342. * XXX (TFO) - The implication of checking the max_qlen before
  343. * processing a cookie request is that clients can't differentiate
  344. * between qlen overflow causing Fast Open to be disabled
  345. * temporarily vs a server not supporting Fast Open at all.
  346. */
  347. fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
  348. max_qlen = READ_ONCE(fastopenq->max_qlen);
  349. if (max_qlen == 0)
  350. return false;
  351. if (fastopenq->qlen >= max_qlen) {
  352. struct request_sock *req1;
  353. spin_lock(&fastopenq->lock);
  354. req1 = fastopenq->rskq_rst_head;
  355. if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
  356. __NET_INC_STATS(sock_net(sk),
  357. LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
  358. spin_unlock(&fastopenq->lock);
  359. return false;
  360. }
  361. fastopenq->rskq_rst_head = req1->dl_next;
  362. fastopenq->qlen--;
  363. spin_unlock(&fastopenq->lock);
  364. reqsk_put(req1);
  365. }
  366. return true;
  367. }
  368. static bool tcp_fastopen_no_cookie(const struct sock *sk,
  369. const struct dst_entry *dst,
  370. int flag)
  371. {
  372. return (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen) & flag) ||
  373. tcp_sk(sk)->fastopen_no_cookie ||
  374. (dst && dst_metric(dst, RTAX_FASTOPEN_NO_COOKIE));
  375. }
  376. /* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
  377. * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
  378. * cookie request (foc->len == 0).
  379. */
  380. struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
  381. struct request_sock *req,
  382. struct tcp_fastopen_cookie *foc,
  383. const struct dst_entry *dst)
  384. {
  385. bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
  386. int tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
  387. struct tcp_fastopen_cookie valid_foc = { .len = -1 };
  388. struct sock *child;
  389. int ret = 0;
  390. if (foc->len == 0) /* Client requests a cookie */
  391. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
  392. if (!((tcp_fastopen & TFO_SERVER_ENABLE) &&
  393. (syn_data || foc->len >= 0) &&
  394. tcp_fastopen_queue_check(sk))) {
  395. foc->len = -1;
  396. return NULL;
  397. }
  398. if (tcp_fastopen_no_cookie(sk, dst, TFO_SERVER_COOKIE_NOT_REQD))
  399. goto fastopen;
  400. if (foc->len == 0) {
  401. /* Client requests a cookie. */
  402. tcp_fastopen_cookie_gen(sk, req, skb, &valid_foc);
  403. } else if (foc->len > 0) {
  404. ret = tcp_fastopen_cookie_gen_check(sk, req, skb, foc,
  405. &valid_foc);
  406. if (!ret) {
  407. NET_INC_STATS(sock_net(sk),
  408. LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
  409. } else {
  410. /* Cookie is valid. Create a (full) child socket to
  411. * accept the data in SYN before returning a SYN-ACK to
  412. * ack the data. If we fail to create the socket, fall
  413. * back and ack the ISN only but includes the same
  414. * cookie.
  415. *
  416. * Note: Data-less SYN with valid cookie is allowed to
  417. * send data in SYN_RECV state.
  418. */
  419. fastopen:
  420. child = tcp_fastopen_create_child(sk, skb, req);
  421. if (child) {
  422. if (ret == 2) {
  423. valid_foc.exp = foc->exp;
  424. *foc = valid_foc;
  425. NET_INC_STATS(sock_net(sk),
  426. LINUX_MIB_TCPFASTOPENPASSIVEALTKEY);
  427. } else {
  428. foc->len = -1;
  429. }
  430. NET_INC_STATS(sock_net(sk),
  431. LINUX_MIB_TCPFASTOPENPASSIVE);
  432. tcp_sk(child)->syn_fastopen_child = 1;
  433. return child;
  434. }
  435. NET_INC_STATS(sock_net(sk),
  436. LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
  437. }
  438. }
  439. valid_foc.exp = foc->exp;
  440. *foc = valid_foc;
  441. return NULL;
  442. }
  443. bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
  444. struct tcp_fastopen_cookie *cookie)
  445. {
  446. const struct dst_entry *dst;
  447. tcp_fastopen_cache_get(sk, mss, cookie);
  448. /* Firewall blackhole issue check */
  449. if (tcp_fastopen_active_should_disable(sk)) {
  450. cookie->len = -1;
  451. return false;
  452. }
  453. dst = __sk_dst_get(sk);
  454. if (tcp_fastopen_no_cookie(sk, dst, TFO_CLIENT_NO_COOKIE)) {
  455. cookie->len = -1;
  456. return true;
  457. }
  458. if (cookie->len > 0)
  459. return true;
  460. tcp_sk(sk)->fastopen_client_fail = TFO_COOKIE_UNAVAILABLE;
  461. return false;
  462. }
  463. /* This function checks if we want to defer sending SYN until the first
  464. * write(). We defer under the following conditions:
  465. * 1. fastopen_connect sockopt is set
  466. * 2. we have a valid cookie
  467. * Return value: return true if we want to defer until application writes data
  468. * return false if we want to send out SYN immediately
  469. */
  470. bool tcp_fastopen_defer_connect(struct sock *sk, int *err)
  471. {
  472. struct tcp_fastopen_cookie cookie = { .len = 0 };
  473. struct tcp_sock *tp = tcp_sk(sk);
  474. u16 mss;
  475. if (tp->fastopen_connect && !tp->fastopen_req) {
  476. if (tcp_fastopen_cookie_check(sk, &mss, &cookie)) {
  477. inet_set_bit(DEFER_CONNECT, sk);
  478. return true;
  479. }
  480. /* Alloc fastopen_req in order for FO option to be included
  481. * in SYN
  482. */
  483. tp->fastopen_req = kzalloc_obj(*tp->fastopen_req,
  484. sk->sk_allocation);
  485. if (tp->fastopen_req)
  486. tp->fastopen_req->cookie = cookie;
  487. else
  488. *err = -ENOBUFS;
  489. }
  490. return false;
  491. }
  492. EXPORT_IPV6_MOD(tcp_fastopen_defer_connect);
  493. /*
  494. * The following code block is to deal with middle box issues with TFO:
  495. * Middlebox firewall issues can potentially cause server's data being
  496. * blackholed after a successful 3WHS using TFO.
  497. * The proposed solution is to disable active TFO globally under the
  498. * following circumstances:
  499. * 1. client side TFO socket receives out of order FIN
  500. * 2. client side TFO socket receives out of order RST
  501. * 3. client side TFO socket has timed out three times consecutively during
  502. * or after handshake
  503. * We disable active side TFO globally for 1hr at first. Then if it
  504. * happens again, we disable it for 2h, then 4h, 8h, ...
  505. * And we reset the timeout back to 1hr when we see a successful active
  506. * TFO connection with data exchanges.
  507. */
  508. /* Disable active TFO and record current jiffies and
  509. * tfo_active_disable_times
  510. */
  511. void tcp_fastopen_active_disable(struct sock *sk)
  512. {
  513. struct net *net = sock_net(sk);
  514. if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout))
  515. return;
  516. /* Paired with READ_ONCE() in tcp_fastopen_active_should_disable() */
  517. WRITE_ONCE(net->ipv4.tfo_active_disable_stamp, jiffies);
  518. /* Paired with smp_rmb() in tcp_fastopen_active_should_disable().
  519. * We want net->ipv4.tfo_active_disable_stamp to be updated first.
  520. */
  521. smp_mb__before_atomic();
  522. atomic_inc(&net->ipv4.tfo_active_disable_times);
  523. NET_INC_STATS(net, LINUX_MIB_TCPFASTOPENBLACKHOLE);
  524. }
  525. /* Calculate timeout for tfo active disable
  526. * Return true if we are still in the active TFO disable period
  527. * Return false if timeout already expired and we should use active TFO
  528. */
  529. bool tcp_fastopen_active_should_disable(struct sock *sk)
  530. {
  531. unsigned int tfo_bh_timeout =
  532. READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout);
  533. unsigned long timeout;
  534. int tfo_da_times;
  535. int multiplier;
  536. if (!tfo_bh_timeout)
  537. return false;
  538. tfo_da_times = atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times);
  539. if (!tfo_da_times)
  540. return false;
  541. /* Paired with smp_mb__before_atomic() in tcp_fastopen_active_disable() */
  542. smp_rmb();
  543. /* Limit timeout to max: 2^6 * initial timeout */
  544. multiplier = 1 << min(tfo_da_times - 1, 6);
  545. /* Paired with the WRITE_ONCE() in tcp_fastopen_active_disable(). */
  546. timeout = READ_ONCE(sock_net(sk)->ipv4.tfo_active_disable_stamp) +
  547. multiplier * tfo_bh_timeout * HZ;
  548. if (time_before(jiffies, timeout))
  549. return true;
  550. /* Mark check bit so we can check for successful active TFO
  551. * condition and reset tfo_active_disable_times
  552. */
  553. tcp_sk(sk)->syn_fastopen_ch = 1;
  554. return false;
  555. }
  556. /* Disable active TFO if FIN is the only packet in the ofo queue
  557. * and no data is received.
  558. * Also check if we can reset tfo_active_disable_times if data is
  559. * received successfully on a marked active TFO sockets opened on
  560. * a non-loopback interface
  561. */
  562. void tcp_fastopen_active_disable_ofo_check(struct sock *sk)
  563. {
  564. struct tcp_sock *tp = tcp_sk(sk);
  565. struct net_device *dev;
  566. struct dst_entry *dst;
  567. struct sk_buff *skb;
  568. if (!tp->syn_fastopen)
  569. return;
  570. if (!tp->data_segs_in) {
  571. skb = skb_rb_first(&tp->out_of_order_queue);
  572. if (skb && !skb_rb_next(skb)) {
  573. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
  574. tcp_fastopen_active_disable(sk);
  575. return;
  576. }
  577. }
  578. } else if (tp->syn_fastopen_ch &&
  579. atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times)) {
  580. rcu_read_lock();
  581. dst = __sk_dst_get(sk);
  582. dev = dst ? dst_dev_rcu(dst) : NULL;
  583. if (!(dev && (dev->flags & IFF_LOOPBACK)))
  584. atomic_set(&sock_net(sk)->ipv4.tfo_active_disable_times, 0);
  585. rcu_read_unlock();
  586. }
  587. }
  588. void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired)
  589. {
  590. u32 timeouts = inet_csk(sk)->icsk_retransmits;
  591. struct tcp_sock *tp = tcp_sk(sk);
  592. /* Broken middle-boxes may black-hole Fast Open connection during or
  593. * even after the handshake. Be extremely conservative and pause
  594. * Fast Open globally after hitting the third consecutive timeout or
  595. * exceeding the configured timeout limit.
  596. */
  597. if ((tp->syn_fastopen || tp->syn_data || tp->syn_data_acked) &&
  598. (timeouts == 2 || (timeouts < 2 && expired))) {
  599. tcp_fastopen_active_disable(sk);
  600. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVEFAIL);
  601. }
  602. }