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