tcp_fastopen.c 17 KB

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