1 // SPDX-License-Identifier: GPL-2.0-or-later 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 * IPv4 specific functions 10 * 11 * code split from: 12 * linux/ipv4/tcp.c 13 * linux/ipv4/tcp_input.c 14 * linux/ipv4/tcp_output.c 15 * 16 * See tcp.c for author information 17 */ 18 19 /* 20 * Changes: 21 * David S. Miller : New socket lookup architecture. 22 * This code is dedicated to John Dyson. 23 * David S. Miller : Change semantics of established hash, 24 * half is devoted to TIME_WAIT sockets 25 * and the rest go in the other half. 26 * Andi Kleen : Add support for syncookies and fixed 27 * some bugs: ip options weren't passed to 28 * the TCP layer, missed a check for an 29 * ACK bit. 30 * Andi Kleen : Implemented fast path mtu discovery. 31 * Fixed many serious bugs in the 32 * request_sock handling and moved 33 * most of it into the af independent code. 34 * Added tail drop and some other bugfixes. 35 * Added new listen semantics. 36 * Mike McLagan : Routing by source 37 * Juan Jose Ciarlante: ip_dynaddr bits 38 * Andi Kleen: various fixes. 39 * Vitaly E. Lavrov : Transparent proxy revived after year 40 * coma. 41 * Andi Kleen : Fix new listen. 42 * Andi Kleen : Fix accept error reporting. 43 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 44 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 45 * a single port at the same time. 46 */ 47 48 #define pr_fmt(fmt) "TCP: " fmt 49 50 #include <linux/bottom_half.h> 51 #include <linux/types.h> 52 #include <linux/fcntl.h> 53 #include <linux/module.h> 54 #include <linux/random.h> 55 #include <linux/cache.h> 56 #include <linux/jhash.h> 57 #include <linux/init.h> 58 #include <linux/times.h> 59 #include <linux/slab.h> 60 #include <linux/sched.h> 61 62 #include <net/net_namespace.h> 63 #include <net/icmp.h> 64 #include <net/inet_hashtables.h> 65 #include <net/tcp.h> 66 #include <net/transp_v6.h> 67 #include <net/ipv6.h> 68 #include <net/inet_common.h> 69 #include <net/timewait_sock.h> 70 #include <net/xfrm.h> 71 #include <net/secure_seq.h> 72 #include <net/busy_poll.h> 73 #include <net/rstreason.h> 74 75 #include <linux/inet.h> 76 #include <linux/ipv6.h> 77 #include <linux/stddef.h> 78 #include <linux/proc_fs.h> 79 #include <linux/seq_file.h> 80 #include <linux/inetdevice.h> 81 #include <linux/btf_ids.h> 82 83 #include <crypto/hash.h> 84 #include <linux/scatterlist.h> 85 86 #include <trace/events/tcp.h> 87 88 #ifdef CONFIG_TCP_MD5SIG 89 static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, 90 __be32 daddr, __be32 saddr, const struct tcphdr *th); 91 #endif 92 93 struct inet_hashinfo tcp_hashinfo; 94 EXPORT_SYMBOL(tcp_hashinfo); 95 96 static DEFINE_PER_CPU(struct sock *, ipv4_tcp_sk); 97 98 static u32 tcp_v4_init_seq(const struct sk_buff *skb) 99 { 100 return secure_tcp_seq(ip_hdr(skb)->daddr, 101 ip_hdr(skb)->saddr, 102 tcp_hdr(skb)->dest, 103 tcp_hdr(skb)->source); 104 } 105 106 static u32 tcp_v4_init_ts_off(const struct net *net, const struct sk_buff *skb) 107 { 108 return secure_tcp_ts_off(net, ip_hdr(skb)->daddr, ip_hdr(skb)->saddr); 109 } 110 111 int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp) 112 { 113 int reuse = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tw_reuse); 114 const struct inet_timewait_sock *tw = inet_twsk(sktw); 115 const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw); 116 struct tcp_sock *tp = tcp_sk(sk); 117 int ts_recent_stamp; 118 119 if (reuse == 2) { 120 /* Still does not detect *everything* that goes through 121 * lo, since we require a loopback src or dst address 122 * or direct binding to 'lo' interface. 123 */ 124 bool loopback = false; 125 if (tw->tw_bound_dev_if == LOOPBACK_IFINDEX) 126 loopback = true; 127 #if IS_ENABLED(CONFIG_IPV6) 128 if (tw->tw_family == AF_INET6) { 129 if (ipv6_addr_loopback(&tw->tw_v6_daddr) || 130 ipv6_addr_v4mapped_loopback(&tw->tw_v6_daddr) || 131 ipv6_addr_loopback(&tw->tw_v6_rcv_saddr) || 132 ipv6_addr_v4mapped_loopback(&tw->tw_v6_rcv_saddr)) 133 loopback = true; 134 } else 135 #endif 136 { 137 if (ipv4_is_loopback(tw->tw_daddr) || 138 ipv4_is_loopback(tw->tw_rcv_saddr)) 139 loopback = true; 140 } 141 if (!loopback) 142 reuse = 0; 143 } 144 145 /* With PAWS, it is safe from the viewpoint 146 of data integrity. Even without PAWS it is safe provided sequence 147 spaces do not overlap i.e. at data rates <= 80Mbit/sec. 148 149 Actually, the idea is close to VJ's one, only timestamp cache is 150 held not per host, but per port pair and TW bucket is used as state 151 holder. 152 153 If TW bucket has been already destroyed we fall back to VJ's scheme 154 and use initial timestamp retrieved from peer table. 155 */ 156 ts_recent_stamp = READ_ONCE(tcptw->tw_ts_recent_stamp); 157 if (ts_recent_stamp && 158 (!twp || (reuse && time_after32(ktime_get_seconds(), 159 ts_recent_stamp)))) { 160 /* inet_twsk_hashdance_schedule() sets sk_refcnt after putting twsk 161 * and releasing the bucket lock. 162 */ 163 if (unlikely(!refcount_inc_not_zero(&sktw->sk_refcnt))) 164 return 0; 165 166 /* In case of repair and re-using TIME-WAIT sockets we still 167 * want to be sure that it is safe as above but honor the 168 * sequence numbers and time stamps set as part of the repair 169 * process. 170 * 171 * Without this check re-using a TIME-WAIT socket with TCP 172 * repair would accumulate a -1 on the repair assigned 173 * sequence number. The first time it is reused the sequence 174 * is -1, the second time -2, etc. This fixes that issue 175 * without appearing to create any others. 176 */ 177 if (likely(!tp->repair)) { 178 u32 seq = tcptw->tw_snd_nxt + 65535 + 2; 179 180 if (!seq) 181 seq = 1; 182 WRITE_ONCE(tp->write_seq, seq); 183 tp->rx_opt.ts_recent = READ_ONCE(tcptw->tw_ts_recent); 184 tp->rx_opt.ts_recent_stamp = ts_recent_stamp; 185 } 186 187 return 1; 188 } 189 190 return 0; 191 } 192 EXPORT_SYMBOL_GPL(tcp_twsk_unique); 193 194 static int tcp_v4_pre_connect(struct sock *sk, struct sockaddr *uaddr, 195 int addr_len) 196 { 197 /* This check is replicated from tcp_v4_connect() and intended to 198 * prevent BPF program called below from accessing bytes that are out 199 * of the bound specified by user in addr_len. 200 */ 201 if (addr_len < sizeof(struct sockaddr_in)) 202 return -EINVAL; 203 204 sock_owned_by_me(sk); 205 206 return BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr, &addr_len); 207 } 208 209 /* This will initiate an outgoing connection. */ 210 int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 211 { 212 struct sockaddr_in *usin = (struct sockaddr_in *)uaddr; 213 struct inet_timewait_death_row *tcp_death_row; 214 struct inet_sock *inet = inet_sk(sk); 215 struct tcp_sock *tp = tcp_sk(sk); 216 struct ip_options_rcu *inet_opt; 217 struct net *net = sock_net(sk); 218 __be16 orig_sport, orig_dport; 219 __be32 daddr, nexthop; 220 struct flowi4 *fl4; 221 struct rtable *rt; 222 int err; 223 224 if (addr_len < sizeof(struct sockaddr_in)) 225 return -EINVAL; 226 227 if (usin->sin_family != AF_INET) 228 return -EAFNOSUPPORT; 229 230 nexthop = daddr = usin->sin_addr.s_addr; 231 inet_opt = rcu_dereference_protected(inet->inet_opt, 232 lockdep_sock_is_held(sk)); 233 if (inet_opt && inet_opt->opt.srr) { 234 if (!daddr) 235 return -EINVAL; 236 nexthop = inet_opt->opt.faddr; 237 } 238 239 orig_sport = inet->inet_sport; 240 orig_dport = usin->sin_port; 241 fl4 = &inet->cork.fl.u.ip4; 242 rt = ip_route_connect(fl4, nexthop, inet->inet_saddr, 243 sk->sk_bound_dev_if, IPPROTO_TCP, orig_sport, 244 orig_dport, sk); 245 if (IS_ERR(rt)) { 246 err = PTR_ERR(rt); 247 if (err == -ENETUNREACH) 248 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 249 return err; 250 } 251 252 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) { 253 ip_rt_put(rt); 254 return -ENETUNREACH; 255 } 256 257 if (!inet_opt || !inet_opt->opt.srr) 258 daddr = fl4->daddr; 259 260 tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row; 261 262 if (!inet->inet_saddr) { 263 err = inet_bhash2_update_saddr(sk, &fl4->saddr, AF_INET); 264 if (err) { 265 ip_rt_put(rt); 266 return err; 267 } 268 } else { 269 sk_rcv_saddr_set(sk, inet->inet_saddr); 270 } 271 272 if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) { 273 /* Reset inherited state */ 274 tp->rx_opt.ts_recent = 0; 275 tp->rx_opt.ts_recent_stamp = 0; 276 if (likely(!tp->repair)) 277 WRITE_ONCE(tp->write_seq, 0); 278 } 279 280 inet->inet_dport = usin->sin_port; 281 sk_daddr_set(sk, daddr); 282 283 inet_csk(sk)->icsk_ext_hdr_len = 0; 284 if (inet_opt) 285 inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen; 286 287 tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT; 288 289 /* Socket identity is still unknown (sport may be zero). 290 * However we set state to SYN-SENT and not releasing socket 291 * lock select source port, enter ourselves into the hash tables and 292 * complete initialization after this. 293 */ 294 tcp_set_state(sk, TCP_SYN_SENT); 295 err = inet_hash_connect(tcp_death_row, sk); 296 if (err) 297 goto failure; 298 299 sk_set_txhash(sk); 300 301 rt = ip_route_newports(fl4, rt, orig_sport, orig_dport, 302 inet->inet_sport, inet->inet_dport, sk); 303 if (IS_ERR(rt)) { 304 err = PTR_ERR(rt); 305 rt = NULL; 306 goto failure; 307 } 308 tp->tcp_usec_ts = dst_tcp_usec_ts(&rt->dst); 309 /* OK, now commit destination to socket. */ 310 sk->sk_gso_type = SKB_GSO_TCPV4; 311 sk_setup_caps(sk, &rt->dst); 312 rt = NULL; 313 314 if (likely(!tp->repair)) { 315 if (!tp->write_seq) 316 WRITE_ONCE(tp->write_seq, 317 secure_tcp_seq(inet->inet_saddr, 318 inet->inet_daddr, 319 inet->inet_sport, 320 usin->sin_port)); 321 WRITE_ONCE(tp->tsoffset, 322 secure_tcp_ts_off(net, inet->inet_saddr, 323 inet->inet_daddr)); 324 } 325 326 atomic_set(&inet->inet_id, get_random_u16()); 327 328 if (tcp_fastopen_defer_connect(sk, &err)) 329 return err; 330 if (err) 331 goto failure; 332 333 err = tcp_connect(sk); 334 335 if (err) 336 goto failure; 337 338 return 0; 339 340 failure: 341 /* 342 * This unhashes the socket and releases the local port, 343 * if necessary. 344 */ 345 tcp_set_state(sk, TCP_CLOSE); 346 inet_bhash2_reset_saddr(sk); 347 ip_rt_put(rt); 348 sk->sk_route_caps = 0; 349 inet->inet_dport = 0; 350 return err; 351 } 352 EXPORT_SYMBOL(tcp_v4_connect); 353 354 /* 355 * This routine reacts to ICMP_FRAG_NEEDED mtu indications as defined in RFC1191. 356 * It can be called through tcp_release_cb() if socket was owned by user 357 * at the time tcp_v4_err() was called to handle ICMP message. 358 */ 359 void tcp_v4_mtu_reduced(struct sock *sk) 360 { 361 struct inet_sock *inet = inet_sk(sk); 362 struct dst_entry *dst; 363 u32 mtu; 364 365 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) 366 return; 367 mtu = READ_ONCE(tcp_sk(sk)->mtu_info); 368 dst = inet_csk_update_pmtu(sk, mtu); 369 if (!dst) 370 return; 371 372 /* Something is about to be wrong... Remember soft error 373 * for the case, if this connection will not able to recover. 374 */ 375 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst)) 376 WRITE_ONCE(sk->sk_err_soft, EMSGSIZE); 377 378 mtu = dst_mtu(dst); 379 380 if (inet->pmtudisc != IP_PMTUDISC_DONT && 381 ip_sk_accept_pmtu(sk) && 382 inet_csk(sk)->icsk_pmtu_cookie > mtu) { 383 tcp_sync_mss(sk, mtu); 384 385 /* Resend the TCP packet because it's 386 * clear that the old packet has been 387 * dropped. This is the new "fast" path mtu 388 * discovery. 389 */ 390 tcp_simple_retransmit(sk); 391 } /* else let the usual retransmit timer handle it */ 392 } 393 EXPORT_SYMBOL(tcp_v4_mtu_reduced); 394 395 static void do_redirect(struct sk_buff *skb, struct sock *sk) 396 { 397 struct dst_entry *dst = __sk_dst_check(sk, 0); 398 399 if (dst) 400 dst->ops->redirect(dst, sk, skb); 401 } 402 403 404 /* handle ICMP messages on TCP_NEW_SYN_RECV request sockets */ 405 void tcp_req_err(struct sock *sk, u32 seq, bool abort) 406 { 407 struct request_sock *req = inet_reqsk(sk); 408 struct net *net = sock_net(sk); 409 410 /* ICMPs are not backlogged, hence we cannot get 411 * an established socket here. 412 */ 413 if (seq != tcp_rsk(req)->snt_isn) { 414 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS); 415 } else if (abort) { 416 /* 417 * Still in SYN_RECV, just remove it silently. 418 * There is no good way to pass the error to the newly 419 * created socket, and POSIX does not want network 420 * errors returned from accept(). 421 */ 422 inet_csk_reqsk_queue_drop(req->rsk_listener, req); 423 tcp_listendrop(req->rsk_listener); 424 } 425 reqsk_put(req); 426 } 427 EXPORT_SYMBOL(tcp_req_err); 428 429 /* TCP-LD (RFC 6069) logic */ 430 void tcp_ld_RTO_revert(struct sock *sk, u32 seq) 431 { 432 struct inet_connection_sock *icsk = inet_csk(sk); 433 struct tcp_sock *tp = tcp_sk(sk); 434 struct sk_buff *skb; 435 s32 remaining; 436 u32 delta_us; 437 438 if (sock_owned_by_user(sk)) 439 return; 440 441 if (seq != tp->snd_una || !icsk->icsk_retransmits || 442 !icsk->icsk_backoff) 443 return; 444 445 skb = tcp_rtx_queue_head(sk); 446 if (WARN_ON_ONCE(!skb)) 447 return; 448 449 icsk->icsk_backoff--; 450 icsk->icsk_rto = tp->srtt_us ? __tcp_set_rto(tp) : TCP_TIMEOUT_INIT; 451 icsk->icsk_rto = inet_csk_rto_backoff(icsk, TCP_RTO_MAX); 452 453 tcp_mstamp_refresh(tp); 454 delta_us = (u32)(tp->tcp_mstamp - tcp_skb_timestamp_us(skb)); 455 remaining = icsk->icsk_rto - usecs_to_jiffies(delta_us); 456 457 if (remaining > 0) { 458 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 459 remaining, TCP_RTO_MAX); 460 } else { 461 /* RTO revert clocked out retransmission. 462 * Will retransmit now. 463 */ 464 tcp_retransmit_timer(sk); 465 } 466 } 467 EXPORT_SYMBOL(tcp_ld_RTO_revert); 468 469 /* 470 * This routine is called by the ICMP module when it gets some 471 * sort of error condition. If err < 0 then the socket should 472 * be closed and the error returned to the user. If err > 0 473 * it's just the icmp type << 8 | icmp code. After adjustment 474 * header points to the first 8 bytes of the tcp header. We need 475 * to find the appropriate port. 476 * 477 * The locking strategy used here is very "optimistic". When 478 * someone else accesses the socket the ICMP is just dropped 479 * and for some paths there is no check at all. 480 * A more general error queue to queue errors for later handling 481 * is probably better. 482 * 483 */ 484 485 int tcp_v4_err(struct sk_buff *skb, u32 info) 486 { 487 const struct iphdr *iph = (const struct iphdr *)skb->data; 488 struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2)); 489 struct tcp_sock *tp; 490 const int type = icmp_hdr(skb)->type; 491 const int code = icmp_hdr(skb)->code; 492 struct sock *sk; 493 struct request_sock *fastopen; 494 u32 seq, snd_una; 495 int err; 496 struct net *net = dev_net(skb->dev); 497 498 sk = __inet_lookup_established(net, net->ipv4.tcp_death_row.hashinfo, 499 iph->daddr, th->dest, iph->saddr, 500 ntohs(th->source), inet_iif(skb), 0); 501 if (!sk) { 502 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 503 return -ENOENT; 504 } 505 if (sk->sk_state == TCP_TIME_WAIT) { 506 /* To increase the counter of ignored icmps for TCP-AO */ 507 tcp_ao_ignore_icmp(sk, AF_INET, type, code); 508 inet_twsk_put(inet_twsk(sk)); 509 return 0; 510 } 511 seq = ntohl(th->seq); 512 if (sk->sk_state == TCP_NEW_SYN_RECV) { 513 tcp_req_err(sk, seq, type == ICMP_PARAMETERPROB || 514 type == ICMP_TIME_EXCEEDED || 515 (type == ICMP_DEST_UNREACH && 516 (code == ICMP_NET_UNREACH || 517 code == ICMP_HOST_UNREACH))); 518 return 0; 519 } 520 521 if (tcp_ao_ignore_icmp(sk, AF_INET, type, code)) { 522 sock_put(sk); 523 return 0; 524 } 525 526 bh_lock_sock(sk); 527 /* If too many ICMPs get dropped on busy 528 * servers this needs to be solved differently. 529 * We do take care of PMTU discovery (RFC1191) special case : 530 * we can receive locally generated ICMP messages while socket is held. 531 */ 532 if (sock_owned_by_user(sk)) { 533 if (!(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)) 534 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS); 535 } 536 if (sk->sk_state == TCP_CLOSE) 537 goto out; 538 539 if (static_branch_unlikely(&ip4_min_ttl)) { 540 /* min_ttl can be changed concurrently from do_ip_setsockopt() */ 541 if (unlikely(iph->ttl < READ_ONCE(inet_sk(sk)->min_ttl))) { 542 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP); 543 goto out; 544 } 545 } 546 547 tp = tcp_sk(sk); 548 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */ 549 fastopen = rcu_dereference(tp->fastopen_rsk); 550 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una; 551 if (sk->sk_state != TCP_LISTEN && 552 !between(seq, snd_una, tp->snd_nxt)) { 553 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS); 554 goto out; 555 } 556 557 switch (type) { 558 case ICMP_REDIRECT: 559 if (!sock_owned_by_user(sk)) 560 do_redirect(skb, sk); 561 goto out; 562 case ICMP_SOURCE_QUENCH: 563 /* Just silently ignore these. */ 564 goto out; 565 case ICMP_PARAMETERPROB: 566 err = EPROTO; 567 break; 568 case ICMP_DEST_UNREACH: 569 if (code > NR_ICMP_UNREACH) 570 goto out; 571 572 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */ 573 /* We are not interested in TCP_LISTEN and open_requests 574 * (SYN-ACKs send out by Linux are always <576bytes so 575 * they should go through unfragmented). 576 */ 577 if (sk->sk_state == TCP_LISTEN) 578 goto out; 579 580 WRITE_ONCE(tp->mtu_info, info); 581 if (!sock_owned_by_user(sk)) { 582 tcp_v4_mtu_reduced(sk); 583 } else { 584 if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED, &sk->sk_tsq_flags)) 585 sock_hold(sk); 586 } 587 goto out; 588 } 589 590 err = icmp_err_convert[code].errno; 591 /* check if this ICMP message allows revert of backoff. 592 * (see RFC 6069) 593 */ 594 if (!fastopen && 595 (code == ICMP_NET_UNREACH || code == ICMP_HOST_UNREACH)) 596 tcp_ld_RTO_revert(sk, seq); 597 break; 598 case ICMP_TIME_EXCEEDED: 599 err = EHOSTUNREACH; 600 break; 601 default: 602 goto out; 603 } 604 605 switch (sk->sk_state) { 606 case TCP_SYN_SENT: 607 case TCP_SYN_RECV: 608 /* Only in fast or simultaneous open. If a fast open socket is 609 * already accepted it is treated as a connected one below. 610 */ 611 if (fastopen && !fastopen->sk) 612 break; 613 614 ip_icmp_error(sk, skb, err, th->dest, info, (u8 *)th); 615 616 if (!sock_owned_by_user(sk)) 617 tcp_done_with_error(sk, err); 618 else 619 WRITE_ONCE(sk->sk_err_soft, err); 620 goto out; 621 } 622 623 /* If we've already connected we will keep trying 624 * until we time out, or the user gives up. 625 * 626 * rfc1122 4.2.3.9 allows to consider as hard errors 627 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too, 628 * but it is obsoleted by pmtu discovery). 629 * 630 * Note, that in modern internet, where routing is unreliable 631 * and in each dark corner broken firewalls sit, sending random 632 * errors ordered by their masters even this two messages finally lose 633 * their original sense (even Linux sends invalid PORT_UNREACHs) 634 * 635 * Now we are in compliance with RFCs. 636 * --ANK (980905) 637 */ 638 639 if (!sock_owned_by_user(sk) && 640 inet_test_bit(RECVERR, sk)) { 641 WRITE_ONCE(sk->sk_err, err); 642 sk_error_report(sk); 643 } else { /* Only an error on timeout */ 644 WRITE_ONCE(sk->sk_err_soft, err); 645 } 646 647 out: 648 bh_unlock_sock(sk); 649 sock_put(sk); 650 return 0; 651 } 652 653 void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr) 654 { 655 struct tcphdr *th = tcp_hdr(skb); 656 657 th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0); 658 skb->csum_start = skb_transport_header(skb) - skb->head; 659 skb->csum_offset = offsetof(struct tcphdr, check); 660 } 661 662 /* This routine computes an IPv4 TCP checksum. */ 663 void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb) 664 { 665 const struct inet_sock *inet = inet_sk(sk); 666 667 __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr); 668 } 669 EXPORT_SYMBOL(tcp_v4_send_check); 670 671 #define REPLY_OPTIONS_LEN (MAX_TCP_OPTION_SPACE / sizeof(__be32)) 672 673 static bool tcp_v4_ao_sign_reset(const struct sock *sk, struct sk_buff *skb, 674 const struct tcp_ao_hdr *aoh, 675 struct ip_reply_arg *arg, struct tcphdr *reply, 676 __be32 reply_options[REPLY_OPTIONS_LEN]) 677 { 678 #ifdef CONFIG_TCP_AO 679 int sdif = tcp_v4_sdif(skb); 680 int dif = inet_iif(skb); 681 int l3index = sdif ? dif : 0; 682 bool allocated_traffic_key; 683 struct tcp_ao_key *key; 684 char *traffic_key; 685 bool drop = true; 686 u32 ao_sne = 0; 687 u8 keyid; 688 689 rcu_read_lock(); 690 if (tcp_ao_prepare_reset(sk, skb, aoh, l3index, ntohl(reply->seq), 691 &key, &traffic_key, &allocated_traffic_key, 692 &keyid, &ao_sne)) 693 goto out; 694 695 reply_options[0] = htonl((TCPOPT_AO << 24) | (tcp_ao_len(key) << 16) | 696 (aoh->rnext_keyid << 8) | keyid); 697 arg->iov[0].iov_len += tcp_ao_len_aligned(key); 698 reply->doff = arg->iov[0].iov_len / 4; 699 700 if (tcp_ao_hash_hdr(AF_INET, (char *)&reply_options[1], 701 key, traffic_key, 702 (union tcp_ao_addr *)&ip_hdr(skb)->saddr, 703 (union tcp_ao_addr *)&ip_hdr(skb)->daddr, 704 reply, ao_sne)) 705 goto out; 706 drop = false; 707 out: 708 rcu_read_unlock(); 709 if (allocated_traffic_key) 710 kfree(traffic_key); 711 return drop; 712 #else 713 return true; 714 #endif 715 } 716 717 /* 718 * This routine will send an RST to the other tcp. 719 * 720 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.) 721 * for reset. 722 * Answer: if a packet caused RST, it is not for a socket 723 * existing in our system, if it is matched to a socket, 724 * it is just duplicate segment or bug in other side's TCP. 725 * So that we build reply only basing on parameters 726 * arrived with segment. 727 * Exception: precedence violation. We do not implement it in any case. 728 */ 729 730 static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb, 731 enum sk_rst_reason reason) 732 { 733 const struct tcphdr *th = tcp_hdr(skb); 734 struct { 735 struct tcphdr th; 736 __be32 opt[REPLY_OPTIONS_LEN]; 737 } rep; 738 const __u8 *md5_hash_location = NULL; 739 const struct tcp_ao_hdr *aoh; 740 struct ip_reply_arg arg; 741 #ifdef CONFIG_TCP_MD5SIG 742 struct tcp_md5sig_key *key = NULL; 743 unsigned char newhash[16]; 744 struct sock *sk1 = NULL; 745 int genhash; 746 #endif 747 u64 transmit_time = 0; 748 struct sock *ctl_sk; 749 struct net *net; 750 u32 txhash = 0; 751 752 /* Never send a reset in response to a reset. */ 753 if (th->rst) 754 return; 755 756 /* If sk not NULL, it means we did a successful lookup and incoming 757 * route had to be correct. prequeue might have dropped our dst. 758 */ 759 if (!sk && skb_rtable(skb)->rt_type != RTN_LOCAL) 760 return; 761 762 /* Swap the send and the receive. */ 763 memset(&rep, 0, sizeof(rep)); 764 rep.th.dest = th->source; 765 rep.th.source = th->dest; 766 rep.th.doff = sizeof(struct tcphdr) / 4; 767 rep.th.rst = 1; 768 769 if (th->ack) { 770 rep.th.seq = th->ack_seq; 771 } else { 772 rep.th.ack = 1; 773 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin + 774 skb->len - (th->doff << 2)); 775 } 776 777 memset(&arg, 0, sizeof(arg)); 778 arg.iov[0].iov_base = (unsigned char *)&rep; 779 arg.iov[0].iov_len = sizeof(rep.th); 780 781 net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev); 782 783 /* Invalid TCP option size or twice included auth */ 784 if (tcp_parse_auth_options(tcp_hdr(skb), &md5_hash_location, &aoh)) 785 return; 786 787 if (aoh && tcp_v4_ao_sign_reset(sk, skb, aoh, &arg, &rep.th, rep.opt)) 788 return; 789 790 #ifdef CONFIG_TCP_MD5SIG 791 rcu_read_lock(); 792 if (sk && sk_fullsock(sk)) { 793 const union tcp_md5_addr *addr; 794 int l3index; 795 796 /* sdif set, means packet ingressed via a device 797 * in an L3 domain and inet_iif is set to it. 798 */ 799 l3index = tcp_v4_sdif(skb) ? inet_iif(skb) : 0; 800 addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr; 801 key = tcp_md5_do_lookup(sk, l3index, addr, AF_INET); 802 } else if (md5_hash_location) { 803 const union tcp_md5_addr *addr; 804 int sdif = tcp_v4_sdif(skb); 805 int dif = inet_iif(skb); 806 int l3index; 807 808 /* 809 * active side is lost. Try to find listening socket through 810 * source port, and then find md5 key through listening socket. 811 * we are not loose security here: 812 * Incoming packet is checked with md5 hash with finding key, 813 * no RST generated if md5 hash doesn't match. 814 */ 815 sk1 = __inet_lookup_listener(net, net->ipv4.tcp_death_row.hashinfo, 816 NULL, 0, ip_hdr(skb)->saddr, 817 th->source, ip_hdr(skb)->daddr, 818 ntohs(th->source), dif, sdif); 819 /* don't send rst if it can't find key */ 820 if (!sk1) 821 goto out; 822 823 /* sdif set, means packet ingressed via a device 824 * in an L3 domain and dif is set to it. 825 */ 826 l3index = sdif ? dif : 0; 827 addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr; 828 key = tcp_md5_do_lookup(sk1, l3index, addr, AF_INET); 829 if (!key) 830 goto out; 831 832 833 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb); 834 if (genhash || memcmp(md5_hash_location, newhash, 16) != 0) 835 goto out; 836 837 } 838 839 if (key) { 840 rep.opt[0] = htonl((TCPOPT_NOP << 24) | 841 (TCPOPT_NOP << 16) | 842 (TCPOPT_MD5SIG << 8) | 843 TCPOLEN_MD5SIG); 844 /* Update length and the length the header thinks exists */ 845 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED; 846 rep.th.doff = arg.iov[0].iov_len / 4; 847 848 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1], 849 key, ip_hdr(skb)->saddr, 850 ip_hdr(skb)->daddr, &rep.th); 851 } 852 #endif 853 /* Can't co-exist with TCPMD5, hence check rep.opt[0] */ 854 if (rep.opt[0] == 0) { 855 __be32 mrst = mptcp_reset_option(skb); 856 857 if (mrst) { 858 rep.opt[0] = mrst; 859 arg.iov[0].iov_len += sizeof(mrst); 860 rep.th.doff = arg.iov[0].iov_len / 4; 861 } 862 } 863 864 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, 865 ip_hdr(skb)->saddr, /* XXX */ 866 arg.iov[0].iov_len, IPPROTO_TCP, 0); 867 arg.csumoffset = offsetof(struct tcphdr, check) / 2; 868 arg.flags = (sk && inet_sk_transparent(sk)) ? IP_REPLY_ARG_NOSRCCHECK : 0; 869 870 /* When socket is gone, all binding information is lost. 871 * routing might fail in this case. No choice here, if we choose to force 872 * input interface, we will misroute in case of asymmetric route. 873 */ 874 if (sk) 875 arg.bound_dev_if = sk->sk_bound_dev_if; 876 877 trace_tcp_send_reset(sk, skb, reason); 878 879 BUILD_BUG_ON(offsetof(struct sock, sk_bound_dev_if) != 880 offsetof(struct inet_timewait_sock, tw_bound_dev_if)); 881 882 arg.tos = ip_hdr(skb)->tos; 883 arg.uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL); 884 local_bh_disable(); 885 ctl_sk = this_cpu_read(ipv4_tcp_sk); 886 sock_net_set(ctl_sk, net); 887 if (sk) { 888 ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ? 889 inet_twsk(sk)->tw_mark : sk->sk_mark; 890 ctl_sk->sk_priority = (sk->sk_state == TCP_TIME_WAIT) ? 891 inet_twsk(sk)->tw_priority : READ_ONCE(sk->sk_priority); 892 transmit_time = tcp_transmit_time(sk); 893 xfrm_sk_clone_policy(ctl_sk, sk); 894 txhash = (sk->sk_state == TCP_TIME_WAIT) ? 895 inet_twsk(sk)->tw_txhash : sk->sk_txhash; 896 } else { 897 ctl_sk->sk_mark = 0; 898 ctl_sk->sk_priority = 0; 899 } 900 ip_send_unicast_reply(ctl_sk, 901 skb, &TCP_SKB_CB(skb)->header.h4.opt, 902 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, 903 &arg, arg.iov[0].iov_len, 904 transmit_time, txhash); 905 906 xfrm_sk_free_policy(ctl_sk); 907 sock_net_set(ctl_sk, &init_net); 908 __TCP_INC_STATS(net, TCP_MIB_OUTSEGS); 909 __TCP_INC_STATS(net, TCP_MIB_OUTRSTS); 910 local_bh_enable(); 911 912 #ifdef CONFIG_TCP_MD5SIG 913 out: 914 rcu_read_unlock(); 915 #endif 916 } 917 918 /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states 919 outside socket context is ugly, certainly. What can I do? 920 */ 921 922 static void tcp_v4_send_ack(const struct sock *sk, 923 struct sk_buff *skb, u32 seq, u32 ack, 924 u32 win, u32 tsval, u32 tsecr, int oif, 925 struct tcp_key *key, 926 int reply_flags, u8 tos, u32 txhash) 927 { 928 const struct tcphdr *th = tcp_hdr(skb); 929 struct { 930 struct tcphdr th; 931 __be32 opt[(MAX_TCP_OPTION_SPACE >> 2)]; 932 } rep; 933 struct net *net = sock_net(sk); 934 struct ip_reply_arg arg; 935 struct sock *ctl_sk; 936 u64 transmit_time; 937 938 memset(&rep.th, 0, sizeof(struct tcphdr)); 939 memset(&arg, 0, sizeof(arg)); 940 941 arg.iov[0].iov_base = (unsigned char *)&rep; 942 arg.iov[0].iov_len = sizeof(rep.th); 943 if (tsecr) { 944 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | 945 (TCPOPT_TIMESTAMP << 8) | 946 TCPOLEN_TIMESTAMP); 947 rep.opt[1] = htonl(tsval); 948 rep.opt[2] = htonl(tsecr); 949 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED; 950 } 951 952 /* Swap the send and the receive. */ 953 rep.th.dest = th->source; 954 rep.th.source = th->dest; 955 rep.th.doff = arg.iov[0].iov_len / 4; 956 rep.th.seq = htonl(seq); 957 rep.th.ack_seq = htonl(ack); 958 rep.th.ack = 1; 959 rep.th.window = htons(win); 960 961 #ifdef CONFIG_TCP_MD5SIG 962 if (tcp_key_is_md5(key)) { 963 int offset = (tsecr) ? 3 : 0; 964 965 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) | 966 (TCPOPT_NOP << 16) | 967 (TCPOPT_MD5SIG << 8) | 968 TCPOLEN_MD5SIG); 969 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED; 970 rep.th.doff = arg.iov[0].iov_len/4; 971 972 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset], 973 key->md5_key, ip_hdr(skb)->saddr, 974 ip_hdr(skb)->daddr, &rep.th); 975 } 976 #endif 977 #ifdef CONFIG_TCP_AO 978 if (tcp_key_is_ao(key)) { 979 int offset = (tsecr) ? 3 : 0; 980 981 rep.opt[offset++] = htonl((TCPOPT_AO << 24) | 982 (tcp_ao_len(key->ao_key) << 16) | 983 (key->ao_key->sndid << 8) | 984 key->rcv_next); 985 arg.iov[0].iov_len += tcp_ao_len_aligned(key->ao_key); 986 rep.th.doff = arg.iov[0].iov_len / 4; 987 988 tcp_ao_hash_hdr(AF_INET, (char *)&rep.opt[offset], 989 key->ao_key, key->traffic_key, 990 (union tcp_ao_addr *)&ip_hdr(skb)->saddr, 991 (union tcp_ao_addr *)&ip_hdr(skb)->daddr, 992 &rep.th, key->sne); 993 } 994 #endif 995 arg.flags = reply_flags; 996 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, 997 ip_hdr(skb)->saddr, /* XXX */ 998 arg.iov[0].iov_len, IPPROTO_TCP, 0); 999 arg.csumoffset = offsetof(struct tcphdr, check) / 2; 1000 if (oif) 1001 arg.bound_dev_if = oif; 1002 arg.tos = tos; 1003 arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL); 1004 local_bh_disable(); 1005 ctl_sk = this_cpu_read(ipv4_tcp_sk); 1006 sock_net_set(ctl_sk, net); 1007 ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ? 1008 inet_twsk(sk)->tw_mark : READ_ONCE(sk->sk_mark); 1009 ctl_sk->sk_priority = (sk->sk_state == TCP_TIME_WAIT) ? 1010 inet_twsk(sk)->tw_priority : READ_ONCE(sk->sk_priority); 1011 transmit_time = tcp_transmit_time(sk); 1012 ip_send_unicast_reply(ctl_sk, 1013 skb, &TCP_SKB_CB(skb)->header.h4.opt, 1014 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, 1015 &arg, arg.iov[0].iov_len, 1016 transmit_time, txhash); 1017 1018 sock_net_set(ctl_sk, &init_net); 1019 __TCP_INC_STATS(net, TCP_MIB_OUTSEGS); 1020 local_bh_enable(); 1021 } 1022 1023 static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb) 1024 { 1025 struct inet_timewait_sock *tw = inet_twsk(sk); 1026 struct tcp_timewait_sock *tcptw = tcp_twsk(sk); 1027 struct tcp_key key = {}; 1028 #ifdef CONFIG_TCP_AO 1029 struct tcp_ao_info *ao_info; 1030 1031 if (static_branch_unlikely(&tcp_ao_needed.key)) { 1032 /* FIXME: the segment to-be-acked is not verified yet */ 1033 ao_info = rcu_dereference(tcptw->ao_info); 1034 if (ao_info) { 1035 const struct tcp_ao_hdr *aoh; 1036 1037 if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) { 1038 inet_twsk_put(tw); 1039 return; 1040 } 1041 1042 if (aoh) 1043 key.ao_key = tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1); 1044 } 1045 } 1046 if (key.ao_key) { 1047 struct tcp_ao_key *rnext_key; 1048 1049 key.traffic_key = snd_other_key(key.ao_key); 1050 key.sne = READ_ONCE(ao_info->snd_sne); 1051 rnext_key = READ_ONCE(ao_info->rnext_key); 1052 key.rcv_next = rnext_key->rcvid; 1053 key.type = TCP_KEY_AO; 1054 #else 1055 if (0) { 1056 #endif 1057 #ifdef CONFIG_TCP_MD5SIG 1058 } else if (static_branch_unlikely(&tcp_md5_needed.key)) { 1059 key.md5_key = tcp_twsk_md5_key(tcptw); 1060 if (key.md5_key) 1061 key.type = TCP_KEY_MD5; 1062 #endif 1063 } 1064 1065 tcp_v4_send_ack(sk, skb, 1066 tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, 1067 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, 1068 tcp_tw_tsval(tcptw), 1069 READ_ONCE(tcptw->tw_ts_recent), 1070 tw->tw_bound_dev_if, &key, 1071 tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0, 1072 tw->tw_tos, 1073 tw->tw_txhash); 1074 1075 inet_twsk_put(tw); 1076 } 1077 1078 static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb, 1079 struct request_sock *req) 1080 { 1081 struct tcp_key key = {}; 1082 1083 /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV 1084 * sk->sk_state == TCP_SYN_RECV -> for Fast Open. 1085 */ 1086 u32 seq = (sk->sk_state == TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 : 1087 tcp_sk(sk)->snd_nxt; 1088 1089 #ifdef CONFIG_TCP_AO 1090 if (static_branch_unlikely(&tcp_ao_needed.key) && 1091 tcp_rsk_used_ao(req)) { 1092 const union tcp_md5_addr *addr; 1093 const struct tcp_ao_hdr *aoh; 1094 int l3index; 1095 1096 /* Invalid TCP option size or twice included auth */ 1097 if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) 1098 return; 1099 if (!aoh) 1100 return; 1101 1102 addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr; 1103 l3index = tcp_v4_sdif(skb) ? inet_iif(skb) : 0; 1104 key.ao_key = tcp_ao_do_lookup(sk, l3index, addr, AF_INET, 1105 aoh->rnext_keyid, -1); 1106 if (unlikely(!key.ao_key)) { 1107 /* Send ACK with any matching MKT for the peer */ 1108 key.ao_key = tcp_ao_do_lookup(sk, l3index, addr, AF_INET, -1, -1); 1109 /* Matching key disappeared (user removed the key?) 1110 * let the handshake timeout. 1111 */ 1112 if (!key.ao_key) { 1113 net_info_ratelimited("TCP-AO key for (%pI4, %d)->(%pI4, %d) suddenly disappeared, won't ACK new connection\n", 1114 addr, 1115 ntohs(tcp_hdr(skb)->source), 1116 &ip_hdr(skb)->daddr, 1117 ntohs(tcp_hdr(skb)->dest)); 1118 return; 1119 } 1120 } 1121 key.traffic_key = kmalloc(tcp_ao_digest_size(key.ao_key), GFP_ATOMIC); 1122 if (!key.traffic_key) 1123 return; 1124 1125 key.type = TCP_KEY_AO; 1126 key.rcv_next = aoh->keyid; 1127 tcp_v4_ao_calc_key_rsk(key.ao_key, key.traffic_key, req); 1128 #else 1129 if (0) { 1130 #endif 1131 #ifdef CONFIG_TCP_MD5SIG 1132 } else if (static_branch_unlikely(&tcp_md5_needed.key)) { 1133 const union tcp_md5_addr *addr; 1134 int l3index; 1135 1136 addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr; 1137 l3index = tcp_v4_sdif(skb) ? inet_iif(skb) : 0; 1138 key.md5_key = tcp_md5_do_lookup(sk, l3index, addr, AF_INET); 1139 if (key.md5_key) 1140 key.type = TCP_KEY_MD5; 1141 #endif 1142 } 1143 1144 tcp_v4_send_ack(sk, skb, seq, 1145 tcp_rsk(req)->rcv_nxt, 1146 tcp_synack_window(req) >> inet_rsk(req)->rcv_wscale, 1147 tcp_rsk_tsval(tcp_rsk(req)), 1148 READ_ONCE(req->ts_recent), 1149 0, &key, 1150 inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0, 1151 ip_hdr(skb)->tos, 1152 READ_ONCE(tcp_rsk(req)->txhash)); 1153 if (tcp_key_is_ao(&key)) 1154 kfree(key.traffic_key); 1155 } 1156 1157 /* 1158 * Send a SYN-ACK after having received a SYN. 1159 * This still operates on a request_sock only, not on a big 1160 * socket. 1161 */ 1162 static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst, 1163 struct flowi *fl, 1164 struct request_sock *req, 1165 struct tcp_fastopen_cookie *foc, 1166 enum tcp_synack_type synack_type, 1167 struct sk_buff *syn_skb) 1168 { 1169 const struct inet_request_sock *ireq = inet_rsk(req); 1170 struct flowi4 fl4; 1171 int err = -1; 1172 struct sk_buff *skb; 1173 u8 tos; 1174 1175 /* First, grab a route. */ 1176 if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL) 1177 return -1; 1178 1179 skb = tcp_make_synack(sk, dst, req, foc, synack_type, syn_skb); 1180 1181 if (skb) { 1182 __tcp_v4_send_check(skb, ireq->ir_loc_addr, ireq->ir_rmt_addr); 1183 1184 tos = READ_ONCE(inet_sk(sk)->tos); 1185 1186 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos)) 1187 tos = (tcp_rsk(req)->syn_tos & ~INET_ECN_MASK) | 1188 (tos & INET_ECN_MASK); 1189 1190 if (!INET_ECN_is_capable(tos) && 1191 tcp_bpf_ca_needs_ecn((struct sock *)req)) 1192 tos |= INET_ECN_ECT_0; 1193 1194 rcu_read_lock(); 1195 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr, 1196 ireq->ir_rmt_addr, 1197 rcu_dereference(ireq->ireq_opt), 1198 tos); 1199 rcu_read_unlock(); 1200 err = net_xmit_eval(err); 1201 } 1202 1203 return err; 1204 } 1205 1206 /* 1207 * IPv4 request_sock destructor. 1208 */ 1209 static void tcp_v4_reqsk_destructor(struct request_sock *req) 1210 { 1211 kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1)); 1212 } 1213 1214 #ifdef CONFIG_TCP_MD5SIG 1215 /* 1216 * RFC2385 MD5 checksumming requires a mapping of 1217 * IP address->MD5 Key. 1218 * We need to maintain these in the sk structure. 1219 */ 1220 1221 DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_md5_needed, HZ); 1222 EXPORT_SYMBOL(tcp_md5_needed); 1223 1224 static bool better_md5_match(struct tcp_md5sig_key *old, struct tcp_md5sig_key *new) 1225 { 1226 if (!old) 1227 return true; 1228 1229 /* l3index always overrides non-l3index */ 1230 if (old->l3index && new->l3index == 0) 1231 return false; 1232 if (old->l3index == 0 && new->l3index) 1233 return true; 1234 1235 return old->prefixlen < new->prefixlen; 1236 } 1237 1238 /* Find the Key structure for an address. */ 1239 struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index, 1240 const union tcp_md5_addr *addr, 1241 int family, bool any_l3index) 1242 { 1243 const struct tcp_sock *tp = tcp_sk(sk); 1244 struct tcp_md5sig_key *key; 1245 const struct tcp_md5sig_info *md5sig; 1246 __be32 mask; 1247 struct tcp_md5sig_key *best_match = NULL; 1248 bool match; 1249 1250 /* caller either holds rcu_read_lock() or socket lock */ 1251 md5sig = rcu_dereference_check(tp->md5sig_info, 1252 lockdep_sock_is_held(sk)); 1253 if (!md5sig) 1254 return NULL; 1255 1256 hlist_for_each_entry_rcu(key, &md5sig->head, node, 1257 lockdep_sock_is_held(sk)) { 1258 if (key->family != family) 1259 continue; 1260 if (!any_l3index && key->flags & TCP_MD5SIG_FLAG_IFINDEX && 1261 key->l3index != l3index) 1262 continue; 1263 if (family == AF_INET) { 1264 mask = inet_make_mask(key->prefixlen); 1265 match = (key->addr.a4.s_addr & mask) == 1266 (addr->a4.s_addr & mask); 1267 #if IS_ENABLED(CONFIG_IPV6) 1268 } else if (family == AF_INET6) { 1269 match = ipv6_prefix_equal(&key->addr.a6, &addr->a6, 1270 key->prefixlen); 1271 #endif 1272 } else { 1273 match = false; 1274 } 1275 1276 if (match && better_md5_match(best_match, key)) 1277 best_match = key; 1278 } 1279 return best_match; 1280 } 1281 EXPORT_SYMBOL(__tcp_md5_do_lookup); 1282 1283 static struct tcp_md5sig_key *tcp_md5_do_lookup_exact(const struct sock *sk, 1284 const union tcp_md5_addr *addr, 1285 int family, u8 prefixlen, 1286 int l3index, u8 flags) 1287 { 1288 const struct tcp_sock *tp = tcp_sk(sk); 1289 struct tcp_md5sig_key *key; 1290 unsigned int size = sizeof(struct in_addr); 1291 const struct tcp_md5sig_info *md5sig; 1292 1293 /* caller either holds rcu_read_lock() or socket lock */ 1294 md5sig = rcu_dereference_check(tp->md5sig_info, 1295 lockdep_sock_is_held(sk)); 1296 if (!md5sig) 1297 return NULL; 1298 #if IS_ENABLED(CONFIG_IPV6) 1299 if (family == AF_INET6) 1300 size = sizeof(struct in6_addr); 1301 #endif 1302 hlist_for_each_entry_rcu(key, &md5sig->head, node, 1303 lockdep_sock_is_held(sk)) { 1304 if (key->family != family) 1305 continue; 1306 if ((key->flags & TCP_MD5SIG_FLAG_IFINDEX) != (flags & TCP_MD5SIG_FLAG_IFINDEX)) 1307 continue; 1308 if (key->l3index != l3index) 1309 continue; 1310 if (!memcmp(&key->addr, addr, size) && 1311 key->prefixlen == prefixlen) 1312 return key; 1313 } 1314 return NULL; 1315 } 1316 1317 struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk, 1318 const struct sock *addr_sk) 1319 { 1320 const union tcp_md5_addr *addr; 1321 int l3index; 1322 1323 l3index = l3mdev_master_ifindex_by_index(sock_net(sk), 1324 addr_sk->sk_bound_dev_if); 1325 addr = (const union tcp_md5_addr *)&addr_sk->sk_daddr; 1326 return tcp_md5_do_lookup(sk, l3index, addr, AF_INET); 1327 } 1328 EXPORT_SYMBOL(tcp_v4_md5_lookup); 1329 1330 static int tcp_md5sig_info_add(struct sock *sk, gfp_t gfp) 1331 { 1332 struct tcp_sock *tp = tcp_sk(sk); 1333 struct tcp_md5sig_info *md5sig; 1334 1335 md5sig = kmalloc(sizeof(*md5sig), gfp); 1336 if (!md5sig) 1337 return -ENOMEM; 1338 1339 sk_gso_disable(sk); 1340 INIT_HLIST_HEAD(&md5sig->head); 1341 rcu_assign_pointer(tp->md5sig_info, md5sig); 1342 return 0; 1343 } 1344 1345 /* This can be called on a newly created socket, from other files */ 1346 static int __tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, 1347 int family, u8 prefixlen, int l3index, u8 flags, 1348 const u8 *newkey, u8 newkeylen, gfp_t gfp) 1349 { 1350 /* Add Key to the list */ 1351 struct tcp_md5sig_key *key; 1352 struct tcp_sock *tp = tcp_sk(sk); 1353 struct tcp_md5sig_info *md5sig; 1354 1355 key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen, l3index, flags); 1356 if (key) { 1357 /* Pre-existing entry - just update that one. 1358 * Note that the key might be used concurrently. 1359 * data_race() is telling kcsan that we do not care of 1360 * key mismatches, since changing MD5 key on live flows 1361 * can lead to packet drops. 1362 */ 1363 data_race(memcpy(key->key, newkey, newkeylen)); 1364 1365 /* Pairs with READ_ONCE() in tcp_md5_hash_key(). 1366 * Also note that a reader could catch new key->keylen value 1367 * but old key->key[], this is the reason we use __GFP_ZERO 1368 * at sock_kmalloc() time below these lines. 1369 */ 1370 WRITE_ONCE(key->keylen, newkeylen); 1371 1372 return 0; 1373 } 1374 1375 md5sig = rcu_dereference_protected(tp->md5sig_info, 1376 lockdep_sock_is_held(sk)); 1377 1378 key = sock_kmalloc(sk, sizeof(*key), gfp | __GFP_ZERO); 1379 if (!key) 1380 return -ENOMEM; 1381 1382 memcpy(key->key, newkey, newkeylen); 1383 key->keylen = newkeylen; 1384 key->family = family; 1385 key->prefixlen = prefixlen; 1386 key->l3index = l3index; 1387 key->flags = flags; 1388 memcpy(&key->addr, addr, 1389 (IS_ENABLED(CONFIG_IPV6) && family == AF_INET6) ? sizeof(struct in6_addr) : 1390 sizeof(struct in_addr)); 1391 hlist_add_head_rcu(&key->node, &md5sig->head); 1392 return 0; 1393 } 1394 1395 int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, 1396 int family, u8 prefixlen, int l3index, u8 flags, 1397 const u8 *newkey, u8 newkeylen) 1398 { 1399 struct tcp_sock *tp = tcp_sk(sk); 1400 1401 if (!rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk))) { 1402 if (tcp_md5_alloc_sigpool()) 1403 return -ENOMEM; 1404 1405 if (tcp_md5sig_info_add(sk, GFP_KERNEL)) { 1406 tcp_md5_release_sigpool(); 1407 return -ENOMEM; 1408 } 1409 1410 if (!static_branch_inc(&tcp_md5_needed.key)) { 1411 struct tcp_md5sig_info *md5sig; 1412 1413 md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk)); 1414 rcu_assign_pointer(tp->md5sig_info, NULL); 1415 kfree_rcu(md5sig, rcu); 1416 tcp_md5_release_sigpool(); 1417 return -EUSERS; 1418 } 1419 } 1420 1421 return __tcp_md5_do_add(sk, addr, family, prefixlen, l3index, flags, 1422 newkey, newkeylen, GFP_KERNEL); 1423 } 1424 EXPORT_SYMBOL(tcp_md5_do_add); 1425 1426 int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr, 1427 int family, u8 prefixlen, int l3index, 1428 struct tcp_md5sig_key *key) 1429 { 1430 struct tcp_sock *tp = tcp_sk(sk); 1431 1432 if (!rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk))) { 1433 tcp_md5_add_sigpool(); 1434 1435 if (tcp_md5sig_info_add(sk, sk_gfp_mask(sk, GFP_ATOMIC))) { 1436 tcp_md5_release_sigpool(); 1437 return -ENOMEM; 1438 } 1439 1440 if (!static_key_fast_inc_not_disabled(&tcp_md5_needed.key.key)) { 1441 struct tcp_md5sig_info *md5sig; 1442 1443 md5sig = rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk)); 1444 net_warn_ratelimited("Too many TCP-MD5 keys in the system\n"); 1445 rcu_assign_pointer(tp->md5sig_info, NULL); 1446 kfree_rcu(md5sig, rcu); 1447 tcp_md5_release_sigpool(); 1448 return -EUSERS; 1449 } 1450 } 1451 1452 return __tcp_md5_do_add(sk, addr, family, prefixlen, l3index, 1453 key->flags, key->key, key->keylen, 1454 sk_gfp_mask(sk, GFP_ATOMIC)); 1455 } 1456 EXPORT_SYMBOL(tcp_md5_key_copy); 1457 1458 int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family, 1459 u8 prefixlen, int l3index, u8 flags) 1460 { 1461 struct tcp_md5sig_key *key; 1462 1463 key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen, l3index, flags); 1464 if (!key) 1465 return -ENOENT; 1466 hlist_del_rcu(&key->node); 1467 atomic_sub(sizeof(*key), &sk->sk_omem_alloc); 1468 kfree_rcu(key, rcu); 1469 return 0; 1470 } 1471 EXPORT_SYMBOL(tcp_md5_do_del); 1472 1473 void tcp_clear_md5_list(struct sock *sk) 1474 { 1475 struct tcp_sock *tp = tcp_sk(sk); 1476 struct tcp_md5sig_key *key; 1477 struct hlist_node *n; 1478 struct tcp_md5sig_info *md5sig; 1479 1480 md5sig = rcu_dereference_protected(tp->md5sig_info, 1); 1481 1482 hlist_for_each_entry_safe(key, n, &md5sig->head, node) { 1483 hlist_del_rcu(&key->node); 1484 atomic_sub(sizeof(*key), &sk->sk_omem_alloc); 1485 kfree_rcu(key, rcu); 1486 } 1487 } 1488 1489 static int tcp_v4_parse_md5_keys(struct sock *sk, int optname, 1490 sockptr_t optval, int optlen) 1491 { 1492 struct tcp_md5sig cmd; 1493 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr; 1494 const union tcp_md5_addr *addr; 1495 u8 prefixlen = 32; 1496 int l3index = 0; 1497 bool l3flag; 1498 u8 flags; 1499 1500 if (optlen < sizeof(cmd)) 1501 return -EINVAL; 1502 1503 if (copy_from_sockptr(&cmd, optval, sizeof(cmd))) 1504 return -EFAULT; 1505 1506 if (sin->sin_family != AF_INET) 1507 return -EINVAL; 1508 1509 flags = cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX; 1510 l3flag = cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX; 1511 1512 if (optname == TCP_MD5SIG_EXT && 1513 cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) { 1514 prefixlen = cmd.tcpm_prefixlen; 1515 if (prefixlen > 32) 1516 return -EINVAL; 1517 } 1518 1519 if (optname == TCP_MD5SIG_EXT && cmd.tcpm_ifindex && 1520 cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX) { 1521 struct net_device *dev; 1522 1523 rcu_read_lock(); 1524 dev = dev_get_by_index_rcu(sock_net(sk), cmd.tcpm_ifindex); 1525 if (dev && netif_is_l3_master(dev)) 1526 l3index = dev->ifindex; 1527 1528 rcu_read_unlock(); 1529 1530 /* ok to reference set/not set outside of rcu; 1531 * right now device MUST be an L3 master 1532 */ 1533 if (!dev || !l3index) 1534 return -EINVAL; 1535 } 1536 1537 addr = (union tcp_md5_addr *)&sin->sin_addr.s_addr; 1538 1539 if (!cmd.tcpm_keylen) 1540 return tcp_md5_do_del(sk, addr, AF_INET, prefixlen, l3index, flags); 1541 1542 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) 1543 return -EINVAL; 1544 1545 /* Don't allow keys for peers that have a matching TCP-AO key. 1546 * See the comment in tcp_ao_add_cmd() 1547 */ 1548 if (tcp_ao_required(sk, addr, AF_INET, l3flag ? l3index : -1, false)) 1549 return -EKEYREJECTED; 1550 1551 return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, 1552 cmd.tcpm_key, cmd.tcpm_keylen); 1553 } 1554 1555 static int tcp_v4_md5_hash_headers(struct tcp_sigpool *hp, 1556 __be32 daddr, __be32 saddr, 1557 const struct tcphdr *th, int nbytes) 1558 { 1559 struct tcp4_pseudohdr *bp; 1560 struct scatterlist sg; 1561 struct tcphdr *_th; 1562 1563 bp = hp->scratch; 1564 bp->saddr = saddr; 1565 bp->daddr = daddr; 1566 bp->pad = 0; 1567 bp->protocol = IPPROTO_TCP; 1568 bp->len = cpu_to_be16(nbytes); 1569 1570 _th = (struct tcphdr *)(bp + 1); 1571 memcpy(_th, th, sizeof(*th)); 1572 _th->check = 0; 1573 1574 sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th)); 1575 ahash_request_set_crypt(hp->req, &sg, NULL, 1576 sizeof(*bp) + sizeof(*th)); 1577 return crypto_ahash_update(hp->req); 1578 } 1579 1580 static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, 1581 __be32 daddr, __be32 saddr, const struct tcphdr *th) 1582 { 1583 struct tcp_sigpool hp; 1584 1585 if (tcp_sigpool_start(tcp_md5_sigpool_id, &hp)) 1586 goto clear_hash_nostart; 1587 1588 if (crypto_ahash_init(hp.req)) 1589 goto clear_hash; 1590 if (tcp_v4_md5_hash_headers(&hp, daddr, saddr, th, th->doff << 2)) 1591 goto clear_hash; 1592 if (tcp_md5_hash_key(&hp, key)) 1593 goto clear_hash; 1594 ahash_request_set_crypt(hp.req, NULL, md5_hash, 0); 1595 if (crypto_ahash_final(hp.req)) 1596 goto clear_hash; 1597 1598 tcp_sigpool_end(&hp); 1599 return 0; 1600 1601 clear_hash: 1602 tcp_sigpool_end(&hp); 1603 clear_hash_nostart: 1604 memset(md5_hash, 0, 16); 1605 return 1; 1606 } 1607 1608 int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, 1609 const struct sock *sk, 1610 const struct sk_buff *skb) 1611 { 1612 const struct tcphdr *th = tcp_hdr(skb); 1613 struct tcp_sigpool hp; 1614 __be32 saddr, daddr; 1615 1616 if (sk) { /* valid for establish/request sockets */ 1617 saddr = sk->sk_rcv_saddr; 1618 daddr = sk->sk_daddr; 1619 } else { 1620 const struct iphdr *iph = ip_hdr(skb); 1621 saddr = iph->saddr; 1622 daddr = iph->daddr; 1623 } 1624 1625 if (tcp_sigpool_start(tcp_md5_sigpool_id, &hp)) 1626 goto clear_hash_nostart; 1627 1628 if (crypto_ahash_init(hp.req)) 1629 goto clear_hash; 1630 1631 if (tcp_v4_md5_hash_headers(&hp, daddr, saddr, th, skb->len)) 1632 goto clear_hash; 1633 if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2)) 1634 goto clear_hash; 1635 if (tcp_md5_hash_key(&hp, key)) 1636 goto clear_hash; 1637 ahash_request_set_crypt(hp.req, NULL, md5_hash, 0); 1638 if (crypto_ahash_final(hp.req)) 1639 goto clear_hash; 1640 1641 tcp_sigpool_end(&hp); 1642 return 0; 1643 1644 clear_hash: 1645 tcp_sigpool_end(&hp); 1646 clear_hash_nostart: 1647 memset(md5_hash, 0, 16); 1648 return 1; 1649 } 1650 EXPORT_SYMBOL(tcp_v4_md5_hash_skb); 1651 1652 #endif 1653 1654 static void tcp_v4_init_req(struct request_sock *req, 1655 const struct sock *sk_listener, 1656 struct sk_buff *skb) 1657 { 1658 struct inet_request_sock *ireq = inet_rsk(req); 1659 struct net *net = sock_net(sk_listener); 1660 1661 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr); 1662 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr); 1663 RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(net, skb)); 1664 } 1665 1666 static struct dst_entry *tcp_v4_route_req(const struct sock *sk, 1667 struct sk_buff *skb, 1668 struct flowi *fl, 1669 struct request_sock *req, 1670 u32 tw_isn) 1671 { 1672 tcp_v4_init_req(req, sk, skb); 1673 1674 if (security_inet_conn_request(sk, skb, req)) 1675 return NULL; 1676 1677 return inet_csk_route_req(sk, &fl->u.ip4, req); 1678 } 1679 1680 struct request_sock_ops tcp_request_sock_ops __read_mostly = { 1681 .family = PF_INET, 1682 .obj_size = sizeof(struct tcp_request_sock), 1683 .rtx_syn_ack = tcp_rtx_synack, 1684 .send_ack = tcp_v4_reqsk_send_ack, 1685 .destructor = tcp_v4_reqsk_destructor, 1686 .send_reset = tcp_v4_send_reset, 1687 .syn_ack_timeout = tcp_syn_ack_timeout, 1688 }; 1689 1690 const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = { 1691 .mss_clamp = TCP_MSS_DEFAULT, 1692 #ifdef CONFIG_TCP_MD5SIG 1693 .req_md5_lookup = tcp_v4_md5_lookup, 1694 .calc_md5_hash = tcp_v4_md5_hash_skb, 1695 #endif 1696 #ifdef CONFIG_TCP_AO 1697 .ao_lookup = tcp_v4_ao_lookup_rsk, 1698 .ao_calc_key = tcp_v4_ao_calc_key_rsk, 1699 .ao_synack_hash = tcp_v4_ao_synack_hash, 1700 #endif 1701 #ifdef CONFIG_SYN_COOKIES 1702 .cookie_init_seq = cookie_v4_init_sequence, 1703 #endif 1704 .route_req = tcp_v4_route_req, 1705 .init_seq = tcp_v4_init_seq, 1706 .init_ts_off = tcp_v4_init_ts_off, 1707 .send_synack = tcp_v4_send_synack, 1708 }; 1709 1710 int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb) 1711 { 1712 /* Never answer to SYNs send to broadcast or multicast */ 1713 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) 1714 goto drop; 1715 1716 return tcp_conn_request(&tcp_request_sock_ops, 1717 &tcp_request_sock_ipv4_ops, sk, skb); 1718 1719 drop: 1720 tcp_listendrop(sk); 1721 return 0; 1722 } 1723 EXPORT_SYMBOL(tcp_v4_conn_request); 1724 1725 1726 /* 1727 * The three way handshake has completed - we got a valid synack - 1728 * now create the new socket. 1729 */ 1730 struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, 1731 struct request_sock *req, 1732 struct dst_entry *dst, 1733 struct request_sock *req_unhash, 1734 bool *own_req) 1735 { 1736 struct inet_request_sock *ireq; 1737 bool found_dup_sk = false; 1738 struct inet_sock *newinet; 1739 struct tcp_sock *newtp; 1740 struct sock *newsk; 1741 #ifdef CONFIG_TCP_MD5SIG 1742 const union tcp_md5_addr *addr; 1743 struct tcp_md5sig_key *key; 1744 int l3index; 1745 #endif 1746 struct ip_options_rcu *inet_opt; 1747 1748 if (sk_acceptq_is_full(sk)) 1749 goto exit_overflow; 1750 1751 newsk = tcp_create_openreq_child(sk, req, skb); 1752 if (!newsk) 1753 goto exit_nonewsk; 1754 1755 newsk->sk_gso_type = SKB_GSO_TCPV4; 1756 inet_sk_rx_dst_set(newsk, skb); 1757 1758 newtp = tcp_sk(newsk); 1759 newinet = inet_sk(newsk); 1760 ireq = inet_rsk(req); 1761 sk_daddr_set(newsk, ireq->ir_rmt_addr); 1762 sk_rcv_saddr_set(newsk, ireq->ir_loc_addr); 1763 newsk->sk_bound_dev_if = ireq->ir_iif; 1764 newinet->inet_saddr = ireq->ir_loc_addr; 1765 inet_opt = rcu_dereference(ireq->ireq_opt); 1766 RCU_INIT_POINTER(newinet->inet_opt, inet_opt); 1767 newinet->mc_index = inet_iif(skb); 1768 newinet->mc_ttl = ip_hdr(skb)->ttl; 1769 newinet->rcv_tos = ip_hdr(skb)->tos; 1770 inet_csk(newsk)->icsk_ext_hdr_len = 0; 1771 if (inet_opt) 1772 inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen; 1773 atomic_set(&newinet->inet_id, get_random_u16()); 1774 1775 /* Set ToS of the new socket based upon the value of incoming SYN. 1776 * ECT bits are set later in tcp_init_transfer(). 1777 */ 1778 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos)) 1779 newinet->tos = tcp_rsk(req)->syn_tos & ~INET_ECN_MASK; 1780 1781 if (!dst) { 1782 dst = inet_csk_route_child_sock(sk, newsk, req); 1783 if (!dst) 1784 goto put_and_exit; 1785 } else { 1786 /* syncookie case : see end of cookie_v4_check() */ 1787 } 1788 sk_setup_caps(newsk, dst); 1789 1790 tcp_ca_openreq_child(newsk, dst); 1791 1792 tcp_sync_mss(newsk, dst_mtu(dst)); 1793 newtp->advmss = tcp_mss_clamp(tcp_sk(sk), dst_metric_advmss(dst)); 1794 1795 tcp_initialize_rcv_mss(newsk); 1796 1797 #ifdef CONFIG_TCP_MD5SIG 1798 l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif); 1799 /* Copy over the MD5 key from the original socket */ 1800 addr = (union tcp_md5_addr *)&newinet->inet_daddr; 1801 key = tcp_md5_do_lookup(sk, l3index, addr, AF_INET); 1802 if (key && !tcp_rsk_used_ao(req)) { 1803 if (tcp_md5_key_copy(newsk, addr, AF_INET, 32, l3index, key)) 1804 goto put_and_exit; 1805 sk_gso_disable(newsk); 1806 } 1807 #endif 1808 #ifdef CONFIG_TCP_AO 1809 if (tcp_ao_copy_all_matching(sk, newsk, req, skb, AF_INET)) 1810 goto put_and_exit; /* OOM, release back memory */ 1811 #endif 1812 1813 if (__inet_inherit_port(sk, newsk) < 0) 1814 goto put_and_exit; 1815 *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash), 1816 &found_dup_sk); 1817 if (likely(*own_req)) { 1818 tcp_move_syn(newtp, req); 1819 ireq->ireq_opt = NULL; 1820 } else { 1821 newinet->inet_opt = NULL; 1822 1823 if (!req_unhash && found_dup_sk) { 1824 /* This code path should only be executed in the 1825 * syncookie case only 1826 */ 1827 bh_unlock_sock(newsk); 1828 sock_put(newsk); 1829 newsk = NULL; 1830 } 1831 } 1832 return newsk; 1833 1834 exit_overflow: 1835 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); 1836 exit_nonewsk: 1837 dst_release(dst); 1838 exit: 1839 tcp_listendrop(sk); 1840 return NULL; 1841 put_and_exit: 1842 newinet->inet_opt = NULL; 1843 inet_csk_prepare_forced_close(newsk); 1844 tcp_done(newsk); 1845 goto exit; 1846 } 1847 EXPORT_SYMBOL(tcp_v4_syn_recv_sock); 1848 1849 static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb) 1850 { 1851 #ifdef CONFIG_SYN_COOKIES 1852 const struct tcphdr *th = tcp_hdr(skb); 1853 1854 if (!th->syn) 1855 sk = cookie_v4_check(sk, skb); 1856 #endif 1857 return sk; 1858 } 1859 1860 u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph, 1861 struct tcphdr *th, u32 *cookie) 1862 { 1863 u16 mss = 0; 1864 #ifdef CONFIG_SYN_COOKIES 1865 mss = tcp_get_syncookie_mss(&tcp_request_sock_ops, 1866 &tcp_request_sock_ipv4_ops, sk, th); 1867 if (mss) { 1868 *cookie = __cookie_v4_init_sequence(iph, th, &mss); 1869 tcp_synq_overflow(sk); 1870 } 1871 #endif 1872 return mss; 1873 } 1874 1875 INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *, 1876 u32)); 1877 /* The socket must have it's spinlock held when we get 1878 * here, unless it is a TCP_LISTEN socket. 1879 * 1880 * We have a potential double-lock case here, so even when 1881 * doing backlog processing we use the BH locking scheme. 1882 * This is because we cannot sleep with the original spinlock 1883 * held. 1884 */ 1885 int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) 1886 { 1887 enum skb_drop_reason reason; 1888 struct sock *rsk; 1889 1890 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */ 1891 struct dst_entry *dst; 1892 1893 dst = rcu_dereference_protected(sk->sk_rx_dst, 1894 lockdep_sock_is_held(sk)); 1895 1896 sock_rps_save_rxhash(sk, skb); 1897 sk_mark_napi_id(sk, skb); 1898 if (dst) { 1899 if (sk->sk_rx_dst_ifindex != skb->skb_iif || 1900 !INDIRECT_CALL_1(dst->ops->check, ipv4_dst_check, 1901 dst, 0)) { 1902 RCU_INIT_POINTER(sk->sk_rx_dst, NULL); 1903 dst_release(dst); 1904 } 1905 } 1906 tcp_rcv_established(sk, skb); 1907 return 0; 1908 } 1909 1910 if (tcp_checksum_complete(skb)) 1911 goto csum_err; 1912 1913 if (sk->sk_state == TCP_LISTEN) { 1914 struct sock *nsk = tcp_v4_cookie_check(sk, skb); 1915 1916 if (!nsk) 1917 return 0; 1918 if (nsk != sk) { 1919 reason = tcp_child_process(sk, nsk, skb); 1920 if (reason) { 1921 rsk = nsk; 1922 goto reset; 1923 } 1924 return 0; 1925 } 1926 } else 1927 sock_rps_save_rxhash(sk, skb); 1928 1929 reason = tcp_rcv_state_process(sk, skb); 1930 if (reason) { 1931 rsk = sk; 1932 goto reset; 1933 } 1934 return 0; 1935 1936 reset: 1937 tcp_v4_send_reset(rsk, skb, sk_rst_convert_drop_reason(reason)); 1938 discard: 1939 kfree_skb_reason(skb, reason); 1940 /* Be careful here. If this function gets more complicated and 1941 * gcc suffers from register pressure on the x86, sk (in %ebx) 1942 * might be destroyed here. This current version compiles correctly, 1943 * but you have been warned. 1944 */ 1945 return 0; 1946 1947 csum_err: 1948 reason = SKB_DROP_REASON_TCP_CSUM; 1949 trace_tcp_bad_csum(skb); 1950 TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS); 1951 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS); 1952 goto discard; 1953 } 1954 EXPORT_SYMBOL(tcp_v4_do_rcv); 1955 1956 int tcp_v4_early_demux(struct sk_buff *skb) 1957 { 1958 struct net *net = dev_net(skb->dev); 1959 const struct iphdr *iph; 1960 const struct tcphdr *th; 1961 struct sock *sk; 1962 1963 if (skb->pkt_type != PACKET_HOST) 1964 return 0; 1965 1966 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr))) 1967 return 0; 1968 1969 iph = ip_hdr(skb); 1970 th = tcp_hdr(skb); 1971 1972 if (th->doff < sizeof(struct tcphdr) / 4) 1973 return 0; 1974 1975 sk = __inet_lookup_established(net, net->ipv4.tcp_death_row.hashinfo, 1976 iph->saddr, th->source, 1977 iph->daddr, ntohs(th->dest), 1978 skb->skb_iif, inet_sdif(skb)); 1979 if (sk) { 1980 skb->sk = sk; 1981 skb->destructor = sock_edemux; 1982 if (sk_fullsock(sk)) { 1983 struct dst_entry *dst = rcu_dereference(sk->sk_rx_dst); 1984 1985 if (dst) 1986 dst = dst_check(dst, 0); 1987 if (dst && 1988 sk->sk_rx_dst_ifindex == skb->skb_iif) 1989 skb_dst_set_noref(skb, dst); 1990 } 1991 } 1992 return 0; 1993 } 1994 1995 bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb, 1996 enum skb_drop_reason *reason) 1997 { 1998 u32 tail_gso_size, tail_gso_segs; 1999 struct skb_shared_info *shinfo; 2000 const struct tcphdr *th; 2001 struct tcphdr *thtail; 2002 struct sk_buff *tail; 2003 unsigned int hdrlen; 2004 bool fragstolen; 2005 u32 gso_segs; 2006 u32 gso_size; 2007 u64 limit; 2008 int delta; 2009 2010 /* In case all data was pulled from skb frags (in __pskb_pull_tail()), 2011 * we can fix skb->truesize to its real value to avoid future drops. 2012 * This is valid because skb is not yet charged to the socket. 2013 * It has been noticed pure SACK packets were sometimes dropped 2014 * (if cooked by drivers without copybreak feature). 2015 */ 2016 skb_condense(skb); 2017 2018 skb_dst_drop(skb); 2019 2020 if (unlikely(tcp_checksum_complete(skb))) { 2021 bh_unlock_sock(sk); 2022 trace_tcp_bad_csum(skb); 2023 *reason = SKB_DROP_REASON_TCP_CSUM; 2024 __TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS); 2025 __TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS); 2026 return true; 2027 } 2028 2029 /* Attempt coalescing to last skb in backlog, even if we are 2030 * above the limits. 2031 * This is okay because skb capacity is limited to MAX_SKB_FRAGS. 2032 */ 2033 th = (const struct tcphdr *)skb->data; 2034 hdrlen = th->doff * 4; 2035 2036 tail = sk->sk_backlog.tail; 2037 if (!tail) 2038 goto no_coalesce; 2039 thtail = (struct tcphdr *)tail->data; 2040 2041 if (TCP_SKB_CB(tail)->end_seq != TCP_SKB_CB(skb)->seq || 2042 TCP_SKB_CB(tail)->ip_dsfield != TCP_SKB_CB(skb)->ip_dsfield || 2043 ((TCP_SKB_CB(tail)->tcp_flags | 2044 TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_SYN | TCPHDR_RST | TCPHDR_URG)) || 2045 !((TCP_SKB_CB(tail)->tcp_flags & 2046 TCP_SKB_CB(skb)->tcp_flags) & TCPHDR_ACK) || 2047 ((TCP_SKB_CB(tail)->tcp_flags ^ 2048 TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_ECE | TCPHDR_CWR)) || 2049 !tcp_skb_can_collapse_rx(tail, skb) || 2050 thtail->doff != th->doff || 2051 memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th))) 2052 goto no_coalesce; 2053 2054 __skb_pull(skb, hdrlen); 2055 2056 shinfo = skb_shinfo(skb); 2057 gso_size = shinfo->gso_size ?: skb->len; 2058 gso_segs = shinfo->gso_segs ?: 1; 2059 2060 shinfo = skb_shinfo(tail); 2061 tail_gso_size = shinfo->gso_size ?: (tail->len - hdrlen); 2062 tail_gso_segs = shinfo->gso_segs ?: 1; 2063 2064 if (skb_try_coalesce(tail, skb, &fragstolen, &delta)) { 2065 TCP_SKB_CB(tail)->end_seq = TCP_SKB_CB(skb)->end_seq; 2066 2067 if (likely(!before(TCP_SKB_CB(skb)->ack_seq, TCP_SKB_CB(tail)->ack_seq))) { 2068 TCP_SKB_CB(tail)->ack_seq = TCP_SKB_CB(skb)->ack_seq; 2069 thtail->window = th->window; 2070 } 2071 2072 /* We have to update both TCP_SKB_CB(tail)->tcp_flags and 2073 * thtail->fin, so that the fast path in tcp_rcv_established() 2074 * is not entered if we append a packet with a FIN. 2075 * SYN, RST, URG are not present. 2076 * ACK is set on both packets. 2077 * PSH : we do not really care in TCP stack, 2078 * at least for 'GRO' packets. 2079 */ 2080 thtail->fin |= th->fin; 2081 TCP_SKB_CB(tail)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; 2082 2083 if (TCP_SKB_CB(skb)->has_rxtstamp) { 2084 TCP_SKB_CB(tail)->has_rxtstamp = true; 2085 tail->tstamp = skb->tstamp; 2086 skb_hwtstamps(tail)->hwtstamp = skb_hwtstamps(skb)->hwtstamp; 2087 } 2088 2089 /* Not as strict as GRO. We only need to carry mss max value */ 2090 shinfo->gso_size = max(gso_size, tail_gso_size); 2091 shinfo->gso_segs = min_t(u32, gso_segs + tail_gso_segs, 0xFFFF); 2092 2093 sk->sk_backlog.len += delta; 2094 __NET_INC_STATS(sock_net(sk), 2095 LINUX_MIB_TCPBACKLOGCOALESCE); 2096 kfree_skb_partial(skb, fragstolen); 2097 return false; 2098 } 2099 __skb_push(skb, hdrlen); 2100 2101 no_coalesce: 2102 /* sk->sk_backlog.len is reset only at the end of __release_sock(). 2103 * Both sk->sk_backlog.len and sk->sk_rmem_alloc could reach 2104 * sk_rcvbuf in normal conditions. 2105 */ 2106 limit = ((u64)READ_ONCE(sk->sk_rcvbuf)) << 1; 2107 2108 limit += ((u32)READ_ONCE(sk->sk_sndbuf)) >> 1; 2109 2110 /* Only socket owner can try to collapse/prune rx queues 2111 * to reduce memory overhead, so add a little headroom here. 2112 * Few sockets backlog are possibly concurrently non empty. 2113 */ 2114 limit += 64 * 1024; 2115 2116 limit = min_t(u64, limit, UINT_MAX); 2117 2118 if (unlikely(sk_add_backlog(sk, skb, limit))) { 2119 bh_unlock_sock(sk); 2120 *reason = SKB_DROP_REASON_SOCKET_BACKLOG; 2121 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPBACKLOGDROP); 2122 return true; 2123 } 2124 return false; 2125 } 2126 EXPORT_SYMBOL(tcp_add_backlog); 2127 2128 int tcp_filter(struct sock *sk, struct sk_buff *skb) 2129 { 2130 struct tcphdr *th = (struct tcphdr *)skb->data; 2131 2132 return sk_filter_trim_cap(sk, skb, th->doff * 4); 2133 } 2134 EXPORT_SYMBOL(tcp_filter); 2135 2136 static void tcp_v4_restore_cb(struct sk_buff *skb) 2137 { 2138 memmove(IPCB(skb), &TCP_SKB_CB(skb)->header.h4, 2139 sizeof(struct inet_skb_parm)); 2140 } 2141 2142 static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph, 2143 const struct tcphdr *th) 2144 { 2145 /* This is tricky : We move IPCB at its correct location into TCP_SKB_CB() 2146 * barrier() makes sure compiler wont play fool^Waliasing games. 2147 */ 2148 memmove(&TCP_SKB_CB(skb)->header.h4, IPCB(skb), 2149 sizeof(struct inet_skb_parm)); 2150 barrier(); 2151 2152 TCP_SKB_CB(skb)->seq = ntohl(th->seq); 2153 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin + 2154 skb->len - th->doff * 4); 2155 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq); 2156 TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th); 2157 TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph); 2158 TCP_SKB_CB(skb)->sacked = 0; 2159 TCP_SKB_CB(skb)->has_rxtstamp = 2160 skb->tstamp || skb_hwtstamps(skb)->hwtstamp; 2161 } 2162 2163 /* 2164 * From tcp_input.c 2165 */ 2166 2167 int tcp_v4_rcv(struct sk_buff *skb) 2168 { 2169 struct net *net = dev_net(skb->dev); 2170 enum skb_drop_reason drop_reason; 2171 int sdif = inet_sdif(skb); 2172 int dif = inet_iif(skb); 2173 const struct iphdr *iph; 2174 const struct tcphdr *th; 2175 bool refcounted; 2176 struct sock *sk; 2177 int ret; 2178 u32 isn; 2179 2180 drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2181 if (skb->pkt_type != PACKET_HOST) 2182 goto discard_it; 2183 2184 /* Count it even if it's bad */ 2185 __TCP_INC_STATS(net, TCP_MIB_INSEGS); 2186 2187 if (!pskb_may_pull(skb, sizeof(struct tcphdr))) 2188 goto discard_it; 2189 2190 th = (const struct tcphdr *)skb->data; 2191 2192 if (unlikely(th->doff < sizeof(struct tcphdr) / 4)) { 2193 drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL; 2194 goto bad_packet; 2195 } 2196 if (!pskb_may_pull(skb, th->doff * 4)) 2197 goto discard_it; 2198 2199 /* An explanation is required here, I think. 2200 * Packet length and doff are validated by header prediction, 2201 * provided case of th->doff==0 is eliminated. 2202 * So, we defer the checks. */ 2203 2204 if (skb_checksum_init(skb, IPPROTO_TCP, inet_compute_pseudo)) 2205 goto csum_error; 2206 2207 th = (const struct tcphdr *)skb->data; 2208 iph = ip_hdr(skb); 2209 lookup: 2210 sk = __inet_lookup_skb(net->ipv4.tcp_death_row.hashinfo, 2211 skb, __tcp_hdrlen(th), th->source, 2212 th->dest, sdif, &refcounted); 2213 if (!sk) 2214 goto no_tcp_socket; 2215 2216 if (sk->sk_state == TCP_TIME_WAIT) 2217 goto do_time_wait; 2218 2219 if (sk->sk_state == TCP_NEW_SYN_RECV) { 2220 struct request_sock *req = inet_reqsk(sk); 2221 bool req_stolen = false; 2222 struct sock *nsk; 2223 2224 sk = req->rsk_listener; 2225 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 2226 drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2227 else 2228 drop_reason = tcp_inbound_hash(sk, req, skb, 2229 &iph->saddr, &iph->daddr, 2230 AF_INET, dif, sdif); 2231 if (unlikely(drop_reason)) { 2232 sk_drops_add(sk, skb); 2233 reqsk_put(req); 2234 goto discard_it; 2235 } 2236 if (tcp_checksum_complete(skb)) { 2237 reqsk_put(req); 2238 goto csum_error; 2239 } 2240 if (unlikely(sk->sk_state != TCP_LISTEN)) { 2241 nsk = reuseport_migrate_sock(sk, req_to_sk(req), skb); 2242 if (!nsk) { 2243 inet_csk_reqsk_queue_drop_and_put(sk, req); 2244 goto lookup; 2245 } 2246 sk = nsk; 2247 /* reuseport_migrate_sock() has already held one sk_refcnt 2248 * before returning. 2249 */ 2250 } else { 2251 /* We own a reference on the listener, increase it again 2252 * as we might lose it too soon. 2253 */ 2254 sock_hold(sk); 2255 } 2256 refcounted = true; 2257 nsk = NULL; 2258 if (!tcp_filter(sk, skb)) { 2259 th = (const struct tcphdr *)skb->data; 2260 iph = ip_hdr(skb); 2261 tcp_v4_fill_cb(skb, iph, th); 2262 nsk = tcp_check_req(sk, skb, req, false, &req_stolen); 2263 } else { 2264 drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 2265 } 2266 if (!nsk) { 2267 reqsk_put(req); 2268 if (req_stolen) { 2269 /* Another cpu got exclusive access to req 2270 * and created a full blown socket. 2271 * Try to feed this packet to this socket 2272 * instead of discarding it. 2273 */ 2274 tcp_v4_restore_cb(skb); 2275 sock_put(sk); 2276 goto lookup; 2277 } 2278 goto discard_and_relse; 2279 } 2280 nf_reset_ct(skb); 2281 if (nsk == sk) { 2282 reqsk_put(req); 2283 tcp_v4_restore_cb(skb); 2284 } else { 2285 drop_reason = tcp_child_process(sk, nsk, skb); 2286 if (drop_reason) { 2287 enum sk_rst_reason rst_reason; 2288 2289 rst_reason = sk_rst_convert_drop_reason(drop_reason); 2290 tcp_v4_send_reset(nsk, skb, rst_reason); 2291 goto discard_and_relse; 2292 } 2293 sock_put(sk); 2294 return 0; 2295 } 2296 } 2297 2298 process: 2299 if (static_branch_unlikely(&ip4_min_ttl)) { 2300 /* min_ttl can be changed concurrently from do_ip_setsockopt() */ 2301 if (unlikely(iph->ttl < READ_ONCE(inet_sk(sk)->min_ttl))) { 2302 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP); 2303 drop_reason = SKB_DROP_REASON_TCP_MINTTL; 2304 goto discard_and_relse; 2305 } 2306 } 2307 2308 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 2309 drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2310 goto discard_and_relse; 2311 } 2312 2313 drop_reason = tcp_inbound_hash(sk, NULL, skb, &iph->saddr, &iph->daddr, 2314 AF_INET, dif, sdif); 2315 if (drop_reason) 2316 goto discard_and_relse; 2317 2318 nf_reset_ct(skb); 2319 2320 if (tcp_filter(sk, skb)) { 2321 drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 2322 goto discard_and_relse; 2323 } 2324 th = (const struct tcphdr *)skb->data; 2325 iph = ip_hdr(skb); 2326 tcp_v4_fill_cb(skb, iph, th); 2327 2328 skb->dev = NULL; 2329 2330 if (sk->sk_state == TCP_LISTEN) { 2331 ret = tcp_v4_do_rcv(sk, skb); 2332 goto put_and_return; 2333 } 2334 2335 sk_incoming_cpu_update(sk); 2336 2337 bh_lock_sock_nested(sk); 2338 tcp_segs_in(tcp_sk(sk), skb); 2339 ret = 0; 2340 if (!sock_owned_by_user(sk)) { 2341 ret = tcp_v4_do_rcv(sk, skb); 2342 } else { 2343 if (tcp_add_backlog(sk, skb, &drop_reason)) 2344 goto discard_and_relse; 2345 } 2346 bh_unlock_sock(sk); 2347 2348 put_and_return: 2349 if (refcounted) 2350 sock_put(sk); 2351 2352 return ret; 2353 2354 no_tcp_socket: 2355 drop_reason = SKB_DROP_REASON_NO_SOCKET; 2356 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2357 goto discard_it; 2358 2359 tcp_v4_fill_cb(skb, iph, th); 2360 2361 if (tcp_checksum_complete(skb)) { 2362 csum_error: 2363 drop_reason = SKB_DROP_REASON_TCP_CSUM; 2364 trace_tcp_bad_csum(skb); 2365 __TCP_INC_STATS(net, TCP_MIB_CSUMERRORS); 2366 bad_packet: 2367 __TCP_INC_STATS(net, TCP_MIB_INERRS); 2368 } else { 2369 tcp_v4_send_reset(NULL, skb, sk_rst_convert_drop_reason(drop_reason)); 2370 } 2371 2372 discard_it: 2373 SKB_DR_OR(drop_reason, NOT_SPECIFIED); 2374 /* Discard frame. */ 2375 kfree_skb_reason(skb, drop_reason); 2376 return 0; 2377 2378 discard_and_relse: 2379 sk_drops_add(sk, skb); 2380 if (refcounted) 2381 sock_put(sk); 2382 goto discard_it; 2383 2384 do_time_wait: 2385 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { 2386 drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2387 inet_twsk_put(inet_twsk(sk)); 2388 goto discard_it; 2389 } 2390 2391 tcp_v4_fill_cb(skb, iph, th); 2392 2393 if (tcp_checksum_complete(skb)) { 2394 inet_twsk_put(inet_twsk(sk)); 2395 goto csum_error; 2396 } 2397 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th, &isn)) { 2398 case TCP_TW_SYN: { 2399 struct sock *sk2 = inet_lookup_listener(net, 2400 net->ipv4.tcp_death_row.hashinfo, 2401 skb, __tcp_hdrlen(th), 2402 iph->saddr, th->source, 2403 iph->daddr, th->dest, 2404 inet_iif(skb), 2405 sdif); 2406 if (sk2) { 2407 inet_twsk_deschedule_put(inet_twsk(sk)); 2408 sk = sk2; 2409 tcp_v4_restore_cb(skb); 2410 refcounted = false; 2411 __this_cpu_write(tcp_tw_isn, isn); 2412 goto process; 2413 } 2414 } 2415 /* to ACK */ 2416 fallthrough; 2417 case TCP_TW_ACK: 2418 tcp_v4_timewait_ack(sk, skb); 2419 break; 2420 case TCP_TW_RST: 2421 tcp_v4_send_reset(sk, skb, SK_RST_REASON_TCP_TIMEWAIT_SOCKET); 2422 inet_twsk_deschedule_put(inet_twsk(sk)); 2423 goto discard_it; 2424 case TCP_TW_SUCCESS:; 2425 } 2426 goto discard_it; 2427 } 2428 2429 static struct timewait_sock_ops tcp_timewait_sock_ops = { 2430 .twsk_obj_size = sizeof(struct tcp_timewait_sock), 2431 .twsk_destructor= tcp_twsk_destructor, 2432 }; 2433 2434 void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb) 2435 { 2436 struct dst_entry *dst = skb_dst(skb); 2437 2438 if (dst && dst_hold_safe(dst)) { 2439 rcu_assign_pointer(sk->sk_rx_dst, dst); 2440 sk->sk_rx_dst_ifindex = skb->skb_iif; 2441 } 2442 } 2443 EXPORT_SYMBOL(inet_sk_rx_dst_set); 2444 2445 const struct inet_connection_sock_af_ops ipv4_specific = { 2446 .queue_xmit = ip_queue_xmit, 2447 .send_check = tcp_v4_send_check, 2448 .rebuild_header = inet_sk_rebuild_header, 2449 .sk_rx_dst_set = inet_sk_rx_dst_set, 2450 .conn_request = tcp_v4_conn_request, 2451 .syn_recv_sock = tcp_v4_syn_recv_sock, 2452 .net_header_len = sizeof(struct iphdr), 2453 .setsockopt = ip_setsockopt, 2454 .getsockopt = ip_getsockopt, 2455 .addr2sockaddr = inet_csk_addr2sockaddr, 2456 .sockaddr_len = sizeof(struct sockaddr_in), 2457 .mtu_reduced = tcp_v4_mtu_reduced, 2458 }; 2459 EXPORT_SYMBOL(ipv4_specific); 2460 2461 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) 2462 static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = { 2463 #ifdef CONFIG_TCP_MD5SIG 2464 .md5_lookup = tcp_v4_md5_lookup, 2465 .calc_md5_hash = tcp_v4_md5_hash_skb, 2466 .md5_parse = tcp_v4_parse_md5_keys, 2467 #endif 2468 #ifdef CONFIG_TCP_AO 2469 .ao_lookup = tcp_v4_ao_lookup, 2470 .calc_ao_hash = tcp_v4_ao_hash_skb, 2471 .ao_parse = tcp_v4_parse_ao, 2472 .ao_calc_key_sk = tcp_v4_ao_calc_key_sk, 2473 #endif 2474 }; 2475 #endif 2476 2477 /* NOTE: A lot of things set to zero explicitly by call to 2478 * sk_alloc() so need not be done here. 2479 */ 2480 static int tcp_v4_init_sock(struct sock *sk) 2481 { 2482 struct inet_connection_sock *icsk = inet_csk(sk); 2483 2484 tcp_init_sock(sk); 2485 2486 icsk->icsk_af_ops = &ipv4_specific; 2487 2488 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) 2489 tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific; 2490 #endif 2491 2492 return 0; 2493 } 2494 2495 #ifdef CONFIG_TCP_MD5SIG 2496 static void tcp_md5sig_info_free_rcu(struct rcu_head *head) 2497 { 2498 struct tcp_md5sig_info *md5sig; 2499 2500 md5sig = container_of(head, struct tcp_md5sig_info, rcu); 2501 kfree(md5sig); 2502 static_branch_slow_dec_deferred(&tcp_md5_needed); 2503 tcp_md5_release_sigpool(); 2504 } 2505 #endif 2506 2507 void tcp_v4_destroy_sock(struct sock *sk) 2508 { 2509 struct tcp_sock *tp = tcp_sk(sk); 2510 2511 trace_tcp_destroy_sock(sk); 2512 2513 tcp_clear_xmit_timers(sk); 2514 2515 tcp_cleanup_congestion_control(sk); 2516 2517 tcp_cleanup_ulp(sk); 2518 2519 /* Cleanup up the write buffer. */ 2520 tcp_write_queue_purge(sk); 2521 2522 /* Check if we want to disable active TFO */ 2523 tcp_fastopen_active_disable_ofo_check(sk); 2524 2525 /* Cleans up our, hopefully empty, out_of_order_queue. */ 2526 skb_rbtree_purge(&tp->out_of_order_queue); 2527 2528 #ifdef CONFIG_TCP_MD5SIG 2529 /* Clean up the MD5 key list, if any */ 2530 if (tp->md5sig_info) { 2531 struct tcp_md5sig_info *md5sig; 2532 2533 md5sig = rcu_dereference_protected(tp->md5sig_info, 1); 2534 tcp_clear_md5_list(sk); 2535 call_rcu(&md5sig->rcu, tcp_md5sig_info_free_rcu); 2536 rcu_assign_pointer(tp->md5sig_info, NULL); 2537 } 2538 #endif 2539 tcp_ao_destroy_sock(sk, false); 2540 2541 /* Clean up a referenced TCP bind bucket. */ 2542 if (inet_csk(sk)->icsk_bind_hash) 2543 inet_put_port(sk); 2544 2545 BUG_ON(rcu_access_pointer(tp->fastopen_rsk)); 2546 2547 /* If socket is aborted during connect operation */ 2548 tcp_free_fastopen_req(tp); 2549 tcp_fastopen_destroy_cipher(sk); 2550 tcp_saved_syn_free(tp); 2551 2552 sk_sockets_allocated_dec(sk); 2553 } 2554 EXPORT_SYMBOL(tcp_v4_destroy_sock); 2555 2556 #ifdef CONFIG_PROC_FS 2557 /* Proc filesystem TCP sock list dumping. */ 2558 2559 static unsigned short seq_file_family(const struct seq_file *seq); 2560 2561 static bool seq_sk_match(struct seq_file *seq, const struct sock *sk) 2562 { 2563 unsigned short family = seq_file_family(seq); 2564 2565 /* AF_UNSPEC is used as a match all */ 2566 return ((family == AF_UNSPEC || family == sk->sk_family) && 2567 net_eq(sock_net(sk), seq_file_net(seq))); 2568 } 2569 2570 /* Find a non empty bucket (starting from st->bucket) 2571 * and return the first sk from it. 2572 */ 2573 static void *listening_get_first(struct seq_file *seq) 2574 { 2575 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 2576 struct tcp_iter_state *st = seq->private; 2577 2578 st->offset = 0; 2579 for (; st->bucket <= hinfo->lhash2_mask; st->bucket++) { 2580 struct inet_listen_hashbucket *ilb2; 2581 struct hlist_nulls_node *node; 2582 struct sock *sk; 2583 2584 ilb2 = &hinfo->lhash2[st->bucket]; 2585 if (hlist_nulls_empty(&ilb2->nulls_head)) 2586 continue; 2587 2588 spin_lock(&ilb2->lock); 2589 sk_nulls_for_each(sk, node, &ilb2->nulls_head) { 2590 if (seq_sk_match(seq, sk)) 2591 return sk; 2592 } 2593 spin_unlock(&ilb2->lock); 2594 } 2595 2596 return NULL; 2597 } 2598 2599 /* Find the next sk of "cur" within the same bucket (i.e. st->bucket). 2600 * If "cur" is the last one in the st->bucket, 2601 * call listening_get_first() to return the first sk of the next 2602 * non empty bucket. 2603 */ 2604 static void *listening_get_next(struct seq_file *seq, void *cur) 2605 { 2606 struct tcp_iter_state *st = seq->private; 2607 struct inet_listen_hashbucket *ilb2; 2608 struct hlist_nulls_node *node; 2609 struct inet_hashinfo *hinfo; 2610 struct sock *sk = cur; 2611 2612 ++st->num; 2613 ++st->offset; 2614 2615 sk = sk_nulls_next(sk); 2616 sk_nulls_for_each_from(sk, node) { 2617 if (seq_sk_match(seq, sk)) 2618 return sk; 2619 } 2620 2621 hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 2622 ilb2 = &hinfo->lhash2[st->bucket]; 2623 spin_unlock(&ilb2->lock); 2624 ++st->bucket; 2625 return listening_get_first(seq); 2626 } 2627 2628 static void *listening_get_idx(struct seq_file *seq, loff_t *pos) 2629 { 2630 struct tcp_iter_state *st = seq->private; 2631 void *rc; 2632 2633 st->bucket = 0; 2634 st->offset = 0; 2635 rc = listening_get_first(seq); 2636 2637 while (rc && *pos) { 2638 rc = listening_get_next(seq, rc); 2639 --*pos; 2640 } 2641 return rc; 2642 } 2643 2644 static inline bool empty_bucket(struct inet_hashinfo *hinfo, 2645 const struct tcp_iter_state *st) 2646 { 2647 return hlist_nulls_empty(&hinfo->ehash[st->bucket].chain); 2648 } 2649 2650 /* 2651 * Get first established socket starting from bucket given in st->bucket. 2652 * If st->bucket is zero, the very first socket in the hash is returned. 2653 */ 2654 static void *established_get_first(struct seq_file *seq) 2655 { 2656 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 2657 struct tcp_iter_state *st = seq->private; 2658 2659 st->offset = 0; 2660 for (; st->bucket <= hinfo->ehash_mask; ++st->bucket) { 2661 struct sock *sk; 2662 struct hlist_nulls_node *node; 2663 spinlock_t *lock = inet_ehash_lockp(hinfo, st->bucket); 2664 2665 cond_resched(); 2666 2667 /* Lockless fast path for the common case of empty buckets */ 2668 if (empty_bucket(hinfo, st)) 2669 continue; 2670 2671 spin_lock_bh(lock); 2672 sk_nulls_for_each(sk, node, &hinfo->ehash[st->bucket].chain) { 2673 if (seq_sk_match(seq, sk)) 2674 return sk; 2675 } 2676 spin_unlock_bh(lock); 2677 } 2678 2679 return NULL; 2680 } 2681 2682 static void *established_get_next(struct seq_file *seq, void *cur) 2683 { 2684 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 2685 struct tcp_iter_state *st = seq->private; 2686 struct hlist_nulls_node *node; 2687 struct sock *sk = cur; 2688 2689 ++st->num; 2690 ++st->offset; 2691 2692 sk = sk_nulls_next(sk); 2693 2694 sk_nulls_for_each_from(sk, node) { 2695 if (seq_sk_match(seq, sk)) 2696 return sk; 2697 } 2698 2699 spin_unlock_bh(inet_ehash_lockp(hinfo, st->bucket)); 2700 ++st->bucket; 2701 return established_get_first(seq); 2702 } 2703 2704 static void *established_get_idx(struct seq_file *seq, loff_t pos) 2705 { 2706 struct tcp_iter_state *st = seq->private; 2707 void *rc; 2708 2709 st->bucket = 0; 2710 rc = established_get_first(seq); 2711 2712 while (rc && pos) { 2713 rc = established_get_next(seq, rc); 2714 --pos; 2715 } 2716 return rc; 2717 } 2718 2719 static void *tcp_get_idx(struct seq_file *seq, loff_t pos) 2720 { 2721 void *rc; 2722 struct tcp_iter_state *st = seq->private; 2723 2724 st->state = TCP_SEQ_STATE_LISTENING; 2725 rc = listening_get_idx(seq, &pos); 2726 2727 if (!rc) { 2728 st->state = TCP_SEQ_STATE_ESTABLISHED; 2729 rc = established_get_idx(seq, pos); 2730 } 2731 2732 return rc; 2733 } 2734 2735 static void *tcp_seek_last_pos(struct seq_file *seq) 2736 { 2737 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 2738 struct tcp_iter_state *st = seq->private; 2739 int bucket = st->bucket; 2740 int offset = st->offset; 2741 int orig_num = st->num; 2742 void *rc = NULL; 2743 2744 switch (st->state) { 2745 case TCP_SEQ_STATE_LISTENING: 2746 if (st->bucket > hinfo->lhash2_mask) 2747 break; 2748 rc = listening_get_first(seq); 2749 while (offset-- && rc && bucket == st->bucket) 2750 rc = listening_get_next(seq, rc); 2751 if (rc) 2752 break; 2753 st->bucket = 0; 2754 st->state = TCP_SEQ_STATE_ESTABLISHED; 2755 fallthrough; 2756 case TCP_SEQ_STATE_ESTABLISHED: 2757 if (st->bucket > hinfo->ehash_mask) 2758 break; 2759 rc = established_get_first(seq); 2760 while (offset-- && rc && bucket == st->bucket) 2761 rc = established_get_next(seq, rc); 2762 } 2763 2764 st->num = orig_num; 2765 2766 return rc; 2767 } 2768 2769 void *tcp_seq_start(struct seq_file *seq, loff_t *pos) 2770 { 2771 struct tcp_iter_state *st = seq->private; 2772 void *rc; 2773 2774 if (*pos && *pos == st->last_pos) { 2775 rc = tcp_seek_last_pos(seq); 2776 if (rc) 2777 goto out; 2778 } 2779 2780 st->state = TCP_SEQ_STATE_LISTENING; 2781 st->num = 0; 2782 st->bucket = 0; 2783 st->offset = 0; 2784 rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 2785 2786 out: 2787 st->last_pos = *pos; 2788 return rc; 2789 } 2790 EXPORT_SYMBOL(tcp_seq_start); 2791 2792 void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2793 { 2794 struct tcp_iter_state *st = seq->private; 2795 void *rc = NULL; 2796 2797 if (v == SEQ_START_TOKEN) { 2798 rc = tcp_get_idx(seq, 0); 2799 goto out; 2800 } 2801 2802 switch (st->state) { 2803 case TCP_SEQ_STATE_LISTENING: 2804 rc = listening_get_next(seq, v); 2805 if (!rc) { 2806 st->state = TCP_SEQ_STATE_ESTABLISHED; 2807 st->bucket = 0; 2808 st->offset = 0; 2809 rc = established_get_first(seq); 2810 } 2811 break; 2812 case TCP_SEQ_STATE_ESTABLISHED: 2813 rc = established_get_next(seq, v); 2814 break; 2815 } 2816 out: 2817 ++*pos; 2818 st->last_pos = *pos; 2819 return rc; 2820 } 2821 EXPORT_SYMBOL(tcp_seq_next); 2822 2823 void tcp_seq_stop(struct seq_file *seq, void *v) 2824 { 2825 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 2826 struct tcp_iter_state *st = seq->private; 2827 2828 switch (st->state) { 2829 case TCP_SEQ_STATE_LISTENING: 2830 if (v != SEQ_START_TOKEN) 2831 spin_unlock(&hinfo->lhash2[st->bucket].lock); 2832 break; 2833 case TCP_SEQ_STATE_ESTABLISHED: 2834 if (v) 2835 spin_unlock_bh(inet_ehash_lockp(hinfo, st->bucket)); 2836 break; 2837 } 2838 } 2839 EXPORT_SYMBOL(tcp_seq_stop); 2840 2841 static void get_openreq4(const struct request_sock *req, 2842 struct seq_file *f, int i) 2843 { 2844 const struct inet_request_sock *ireq = inet_rsk(req); 2845 long delta = req->rsk_timer.expires - jiffies; 2846 2847 seq_printf(f, "%4d: %08X:%04X %08X:%04X" 2848 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK", 2849 i, 2850 ireq->ir_loc_addr, 2851 ireq->ir_num, 2852 ireq->ir_rmt_addr, 2853 ntohs(ireq->ir_rmt_port), 2854 TCP_SYN_RECV, 2855 0, 0, /* could print option size, but that is af dependent. */ 2856 1, /* timers active (only the expire timer) */ 2857 jiffies_delta_to_clock_t(delta), 2858 req->num_timeout, 2859 from_kuid_munged(seq_user_ns(f), 2860 sock_i_uid(req->rsk_listener)), 2861 0, /* non standard timer */ 2862 0, /* open_requests have no inode */ 2863 0, 2864 req); 2865 } 2866 2867 static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i) 2868 { 2869 int timer_active; 2870 unsigned long timer_expires; 2871 const struct tcp_sock *tp = tcp_sk(sk); 2872 const struct inet_connection_sock *icsk = inet_csk(sk); 2873 const struct inet_sock *inet = inet_sk(sk); 2874 const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq; 2875 __be32 dest = inet->inet_daddr; 2876 __be32 src = inet->inet_rcv_saddr; 2877 __u16 destp = ntohs(inet->inet_dport); 2878 __u16 srcp = ntohs(inet->inet_sport); 2879 int rx_queue; 2880 int state; 2881 2882 if (icsk->icsk_pending == ICSK_TIME_RETRANS || 2883 icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || 2884 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 2885 timer_active = 1; 2886 timer_expires = icsk->icsk_timeout; 2887 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 2888 timer_active = 4; 2889 timer_expires = icsk->icsk_timeout; 2890 } else if (timer_pending(&sk->sk_timer)) { 2891 timer_active = 2; 2892 timer_expires = sk->sk_timer.expires; 2893 } else { 2894 timer_active = 0; 2895 timer_expires = jiffies; 2896 } 2897 2898 state = inet_sk_state_load(sk); 2899 if (state == TCP_LISTEN) 2900 rx_queue = READ_ONCE(sk->sk_ack_backlog); 2901 else 2902 /* Because we don't lock the socket, 2903 * we might find a transient negative value. 2904 */ 2905 rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) - 2906 READ_ONCE(tp->copied_seq), 0); 2907 2908 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX " 2909 "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d", 2910 i, src, srcp, dest, destp, state, 2911 READ_ONCE(tp->write_seq) - tp->snd_una, 2912 rx_queue, 2913 timer_active, 2914 jiffies_delta_to_clock_t(timer_expires - jiffies), 2915 icsk->icsk_retransmits, 2916 from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)), 2917 icsk->icsk_probes_out, 2918 sock_i_ino(sk), 2919 refcount_read(&sk->sk_refcnt), sk, 2920 jiffies_to_clock_t(icsk->icsk_rto), 2921 jiffies_to_clock_t(icsk->icsk_ack.ato), 2922 (icsk->icsk_ack.quick << 1) | inet_csk_in_pingpong_mode(sk), 2923 tcp_snd_cwnd(tp), 2924 state == TCP_LISTEN ? 2925 fastopenq->max_qlen : 2926 (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh)); 2927 } 2928 2929 static void get_timewait4_sock(const struct inet_timewait_sock *tw, 2930 struct seq_file *f, int i) 2931 { 2932 long delta = tw->tw_timer.expires - jiffies; 2933 __be32 dest, src; 2934 __u16 destp, srcp; 2935 2936 dest = tw->tw_daddr; 2937 src = tw->tw_rcv_saddr; 2938 destp = ntohs(tw->tw_dport); 2939 srcp = ntohs(tw->tw_sport); 2940 2941 seq_printf(f, "%4d: %08X:%04X %08X:%04X" 2942 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK", 2943 i, src, srcp, dest, destp, tw->tw_substate, 0, 0, 2944 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0, 2945 refcount_read(&tw->tw_refcnt), tw); 2946 } 2947 2948 #define TMPSZ 150 2949 2950 static int tcp4_seq_show(struct seq_file *seq, void *v) 2951 { 2952 struct tcp_iter_state *st; 2953 struct sock *sk = v; 2954 2955 seq_setwidth(seq, TMPSZ - 1); 2956 if (v == SEQ_START_TOKEN) { 2957 seq_puts(seq, " sl local_address rem_address st tx_queue " 2958 "rx_queue tr tm->when retrnsmt uid timeout " 2959 "inode"); 2960 goto out; 2961 } 2962 st = seq->private; 2963 2964 if (sk->sk_state == TCP_TIME_WAIT) 2965 get_timewait4_sock(v, seq, st->num); 2966 else if (sk->sk_state == TCP_NEW_SYN_RECV) 2967 get_openreq4(v, seq, st->num); 2968 else 2969 get_tcp4_sock(v, seq, st->num); 2970 out: 2971 seq_pad(seq, '\n'); 2972 return 0; 2973 } 2974 2975 #ifdef CONFIG_BPF_SYSCALL 2976 struct bpf_tcp_iter_state { 2977 struct tcp_iter_state state; 2978 unsigned int cur_sk; 2979 unsigned int end_sk; 2980 unsigned int max_sk; 2981 struct sock **batch; 2982 bool st_bucket_done; 2983 }; 2984 2985 struct bpf_iter__tcp { 2986 __bpf_md_ptr(struct bpf_iter_meta *, meta); 2987 __bpf_md_ptr(struct sock_common *, sk_common); 2988 uid_t uid __aligned(8); 2989 }; 2990 2991 static int tcp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 2992 struct sock_common *sk_common, uid_t uid) 2993 { 2994 struct bpf_iter__tcp ctx; 2995 2996 meta->seq_num--; /* skip SEQ_START_TOKEN */ 2997 ctx.meta = meta; 2998 ctx.sk_common = sk_common; 2999 ctx.uid = uid; 3000 return bpf_iter_run_prog(prog, &ctx); 3001 } 3002 3003 static void bpf_iter_tcp_put_batch(struct bpf_tcp_iter_state *iter) 3004 { 3005 while (iter->cur_sk < iter->end_sk) 3006 sock_gen_put(iter->batch[iter->cur_sk++]); 3007 } 3008 3009 static int bpf_iter_tcp_realloc_batch(struct bpf_tcp_iter_state *iter, 3010 unsigned int new_batch_sz) 3011 { 3012 struct sock **new_batch; 3013 3014 new_batch = kvmalloc(sizeof(*new_batch) * new_batch_sz, 3015 GFP_USER | __GFP_NOWARN); 3016 if (!new_batch) 3017 return -ENOMEM; 3018 3019 bpf_iter_tcp_put_batch(iter); 3020 kvfree(iter->batch); 3021 iter->batch = new_batch; 3022 iter->max_sk = new_batch_sz; 3023 3024 return 0; 3025 } 3026 3027 static unsigned int bpf_iter_tcp_listening_batch(struct seq_file *seq, 3028 struct sock *start_sk) 3029 { 3030 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 3031 struct bpf_tcp_iter_state *iter = seq->private; 3032 struct tcp_iter_state *st = &iter->state; 3033 struct hlist_nulls_node *node; 3034 unsigned int expected = 1; 3035 struct sock *sk; 3036 3037 sock_hold(start_sk); 3038 iter->batch[iter->end_sk++] = start_sk; 3039 3040 sk = sk_nulls_next(start_sk); 3041 sk_nulls_for_each_from(sk, node) { 3042 if (seq_sk_match(seq, sk)) { 3043 if (iter->end_sk < iter->max_sk) { 3044 sock_hold(sk); 3045 iter->batch[iter->end_sk++] = sk; 3046 } 3047 expected++; 3048 } 3049 } 3050 spin_unlock(&hinfo->lhash2[st->bucket].lock); 3051 3052 return expected; 3053 } 3054 3055 static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq, 3056 struct sock *start_sk) 3057 { 3058 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 3059 struct bpf_tcp_iter_state *iter = seq->private; 3060 struct tcp_iter_state *st = &iter->state; 3061 struct hlist_nulls_node *node; 3062 unsigned int expected = 1; 3063 struct sock *sk; 3064 3065 sock_hold(start_sk); 3066 iter->batch[iter->end_sk++] = start_sk; 3067 3068 sk = sk_nulls_next(start_sk); 3069 sk_nulls_for_each_from(sk, node) { 3070 if (seq_sk_match(seq, sk)) { 3071 if (iter->end_sk < iter->max_sk) { 3072 sock_hold(sk); 3073 iter->batch[iter->end_sk++] = sk; 3074 } 3075 expected++; 3076 } 3077 } 3078 spin_unlock_bh(inet_ehash_lockp(hinfo, st->bucket)); 3079 3080 return expected; 3081 } 3082 3083 static struct sock *bpf_iter_tcp_batch(struct seq_file *seq) 3084 { 3085 struct inet_hashinfo *hinfo = seq_file_net(seq)->ipv4.tcp_death_row.hashinfo; 3086 struct bpf_tcp_iter_state *iter = seq->private; 3087 struct tcp_iter_state *st = &iter->state; 3088 unsigned int expected; 3089 bool resized = false; 3090 struct sock *sk; 3091 3092 /* The st->bucket is done. Directly advance to the next 3093 * bucket instead of having the tcp_seek_last_pos() to skip 3094 * one by one in the current bucket and eventually find out 3095 * it has to advance to the next bucket. 3096 */ 3097 if (iter->st_bucket_done) { 3098 st->offset = 0; 3099 st->bucket++; 3100 if (st->state == TCP_SEQ_STATE_LISTENING && 3101 st->bucket > hinfo->lhash2_mask) { 3102 st->state = TCP_SEQ_STATE_ESTABLISHED; 3103 st->bucket = 0; 3104 } 3105 } 3106 3107 again: 3108 /* Get a new batch */ 3109 iter->cur_sk = 0; 3110 iter->end_sk = 0; 3111 iter->st_bucket_done = false; 3112 3113 sk = tcp_seek_last_pos(seq); 3114 if (!sk) 3115 return NULL; /* Done */ 3116 3117 if (st->state == TCP_SEQ_STATE_LISTENING) 3118 expected = bpf_iter_tcp_listening_batch(seq, sk); 3119 else 3120 expected = bpf_iter_tcp_established_batch(seq, sk); 3121 3122 if (iter->end_sk == expected) { 3123 iter->st_bucket_done = true; 3124 return sk; 3125 } 3126 3127 if (!resized && !bpf_iter_tcp_realloc_batch(iter, expected * 3 / 2)) { 3128 resized = true; 3129 goto again; 3130 } 3131 3132 return sk; 3133 } 3134 3135 static void *bpf_iter_tcp_seq_start(struct seq_file *seq, loff_t *pos) 3136 { 3137 /* bpf iter does not support lseek, so it always 3138 * continue from where it was stop()-ped. 3139 */ 3140 if (*pos) 3141 return bpf_iter_tcp_batch(seq); 3142 3143 return SEQ_START_TOKEN; 3144 } 3145 3146 static void *bpf_iter_tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3147 { 3148 struct bpf_tcp_iter_state *iter = seq->private; 3149 struct tcp_iter_state *st = &iter->state; 3150 struct sock *sk; 3151 3152 /* Whenever seq_next() is called, the iter->cur_sk is 3153 * done with seq_show(), so advance to the next sk in 3154 * the batch. 3155 */ 3156 if (iter->cur_sk < iter->end_sk) { 3157 /* Keeping st->num consistent in tcp_iter_state. 3158 * bpf_iter_tcp does not use st->num. 3159 * meta.seq_num is used instead. 3160 */ 3161 st->num++; 3162 /* Move st->offset to the next sk in the bucket such that 3163 * the future start() will resume at st->offset in 3164 * st->bucket. See tcp_seek_last_pos(). 3165 */ 3166 st->offset++; 3167 sock_gen_put(iter->batch[iter->cur_sk++]); 3168 } 3169 3170 if (iter->cur_sk < iter->end_sk) 3171 sk = iter->batch[iter->cur_sk]; 3172 else 3173 sk = bpf_iter_tcp_batch(seq); 3174 3175 ++*pos; 3176 /* Keeping st->last_pos consistent in tcp_iter_state. 3177 * bpf iter does not do lseek, so st->last_pos always equals to *pos. 3178 */ 3179 st->last_pos = *pos; 3180 return sk; 3181 } 3182 3183 static int bpf_iter_tcp_seq_show(struct seq_file *seq, void *v) 3184 { 3185 struct bpf_iter_meta meta; 3186 struct bpf_prog *prog; 3187 struct sock *sk = v; 3188 uid_t uid; 3189 int ret; 3190 3191 if (v == SEQ_START_TOKEN) 3192 return 0; 3193 3194 if (sk_fullsock(sk)) 3195 lock_sock(sk); 3196 3197 if (unlikely(sk_unhashed(sk))) { 3198 ret = SEQ_SKIP; 3199 goto unlock; 3200 } 3201 3202 if (sk->sk_state == TCP_TIME_WAIT) { 3203 uid = 0; 3204 } else if (sk->sk_state == TCP_NEW_SYN_RECV) { 3205 const struct request_sock *req = v; 3206 3207 uid = from_kuid_munged(seq_user_ns(seq), 3208 sock_i_uid(req->rsk_listener)); 3209 } else { 3210 uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); 3211 } 3212 3213 meta.seq = seq; 3214 prog = bpf_iter_get_info(&meta, false); 3215 ret = tcp_prog_seq_show(prog, &meta, v, uid); 3216 3217 unlock: 3218 if (sk_fullsock(sk)) 3219 release_sock(sk); 3220 return ret; 3221 3222 } 3223 3224 static void bpf_iter_tcp_seq_stop(struct seq_file *seq, void *v) 3225 { 3226 struct bpf_tcp_iter_state *iter = seq->private; 3227 struct bpf_iter_meta meta; 3228 struct bpf_prog *prog; 3229 3230 if (!v) { 3231 meta.seq = seq; 3232 prog = bpf_iter_get_info(&meta, true); 3233 if (prog) 3234 (void)tcp_prog_seq_show(prog, &meta, v, 0); 3235 } 3236 3237 if (iter->cur_sk < iter->end_sk) { 3238 bpf_iter_tcp_put_batch(iter); 3239 iter->st_bucket_done = false; 3240 } 3241 } 3242 3243 static const struct seq_operations bpf_iter_tcp_seq_ops = { 3244 .show = bpf_iter_tcp_seq_show, 3245 .start = bpf_iter_tcp_seq_start, 3246 .next = bpf_iter_tcp_seq_next, 3247 .stop = bpf_iter_tcp_seq_stop, 3248 }; 3249 #endif 3250 static unsigned short seq_file_family(const struct seq_file *seq) 3251 { 3252 const struct tcp_seq_afinfo *afinfo; 3253 3254 #ifdef CONFIG_BPF_SYSCALL 3255 /* Iterated from bpf_iter. Let the bpf prog to filter instead. */ 3256 if (seq->op == &bpf_iter_tcp_seq_ops) 3257 return AF_UNSPEC; 3258 #endif 3259 3260 /* Iterated from proc fs */ 3261 afinfo = pde_data(file_inode(seq->file)); 3262 return afinfo->family; 3263 } 3264 3265 static const struct seq_operations tcp4_seq_ops = { 3266 .show = tcp4_seq_show, 3267 .start = tcp_seq_start, 3268 .next = tcp_seq_next, 3269 .stop = tcp_seq_stop, 3270 }; 3271 3272 static struct tcp_seq_afinfo tcp4_seq_afinfo = { 3273 .family = AF_INET, 3274 }; 3275 3276 static int __net_init tcp4_proc_init_net(struct net *net) 3277 { 3278 if (!proc_create_net_data("tcp", 0444, net->proc_net, &tcp4_seq_ops, 3279 sizeof(struct tcp_iter_state), &tcp4_seq_afinfo)) 3280 return -ENOMEM; 3281 return 0; 3282 } 3283 3284 static void __net_exit tcp4_proc_exit_net(struct net *net) 3285 { 3286 remove_proc_entry("tcp", net->proc_net); 3287 } 3288 3289 static struct pernet_operations tcp4_net_ops = { 3290 .init = tcp4_proc_init_net, 3291 .exit = tcp4_proc_exit_net, 3292 }; 3293 3294 int __init tcp4_proc_init(void) 3295 { 3296 return register_pernet_subsys(&tcp4_net_ops); 3297 } 3298 3299 void tcp4_proc_exit(void) 3300 { 3301 unregister_pernet_subsys(&tcp4_net_ops); 3302 } 3303 #endif /* CONFIG_PROC_FS */ 3304 3305 /* @wake is one when sk_stream_write_space() calls us. 3306 * This sends EPOLLOUT only if notsent_bytes is half the limit. 3307 * This mimics the strategy used in sock_def_write_space(). 3308 */ 3309 bool tcp_stream_memory_free(const struct sock *sk, int wake) 3310 { 3311 const struct tcp_sock *tp = tcp_sk(sk); 3312 u32 notsent_bytes = READ_ONCE(tp->write_seq) - 3313 READ_ONCE(tp->snd_nxt); 3314 3315 return (notsent_bytes << wake) < tcp_notsent_lowat(tp); 3316 } 3317 EXPORT_SYMBOL(tcp_stream_memory_free); 3318 3319 struct proto tcp_prot = { 3320 .name = "TCP", 3321 .owner = THIS_MODULE, 3322 .close = tcp_close, 3323 .pre_connect = tcp_v4_pre_connect, 3324 .connect = tcp_v4_connect, 3325 .disconnect = tcp_disconnect, 3326 .accept = inet_csk_accept, 3327 .ioctl = tcp_ioctl, 3328 .init = tcp_v4_init_sock, 3329 .destroy = tcp_v4_destroy_sock, 3330 .shutdown = tcp_shutdown, 3331 .setsockopt = tcp_setsockopt, 3332 .getsockopt = tcp_getsockopt, 3333 .bpf_bypass_getsockopt = tcp_bpf_bypass_getsockopt, 3334 .keepalive = tcp_set_keepalive, 3335 .recvmsg = tcp_recvmsg, 3336 .sendmsg = tcp_sendmsg, 3337 .splice_eof = tcp_splice_eof, 3338 .backlog_rcv = tcp_v4_do_rcv, 3339 .release_cb = tcp_release_cb, 3340 .hash = inet_hash, 3341 .unhash = inet_unhash, 3342 .get_port = inet_csk_get_port, 3343 .put_port = inet_put_port, 3344 #ifdef CONFIG_BPF_SYSCALL 3345 .psock_update_sk_prot = tcp_bpf_update_proto, 3346 #endif 3347 .enter_memory_pressure = tcp_enter_memory_pressure, 3348 .leave_memory_pressure = tcp_leave_memory_pressure, 3349 .stream_memory_free = tcp_stream_memory_free, 3350 .sockets_allocated = &tcp_sockets_allocated, 3351 .orphan_count = &tcp_orphan_count, 3352 3353 .memory_allocated = &tcp_memory_allocated, 3354 .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc, 3355 3356 .memory_pressure = &tcp_memory_pressure, 3357 .sysctl_mem = sysctl_tcp_mem, 3358 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem), 3359 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), 3360 .max_header = MAX_TCP_HEADER, 3361 .obj_size = sizeof(struct tcp_sock), 3362 .slab_flags = SLAB_TYPESAFE_BY_RCU, 3363 .twsk_prot = &tcp_timewait_sock_ops, 3364 .rsk_prot = &tcp_request_sock_ops, 3365 .h.hashinfo = NULL, 3366 .no_autobind = true, 3367 .diag_destroy = tcp_abort, 3368 }; 3369 EXPORT_SYMBOL(tcp_prot); 3370 3371 static void __net_exit tcp_sk_exit(struct net *net) 3372 { 3373 if (net->ipv4.tcp_congestion_control) 3374 bpf_module_put(net->ipv4.tcp_congestion_control, 3375 net->ipv4.tcp_congestion_control->owner); 3376 } 3377 3378 static void __net_init tcp_set_hashinfo(struct net *net) 3379 { 3380 struct inet_hashinfo *hinfo; 3381 unsigned int ehash_entries; 3382 struct net *old_net; 3383 3384 if (net_eq(net, &init_net)) 3385 goto fallback; 3386 3387 old_net = current->nsproxy->net_ns; 3388 ehash_entries = READ_ONCE(old_net->ipv4.sysctl_tcp_child_ehash_entries); 3389 if (!ehash_entries) 3390 goto fallback; 3391 3392 ehash_entries = roundup_pow_of_two(ehash_entries); 3393 hinfo = inet_pernet_hashinfo_alloc(&tcp_hashinfo, ehash_entries); 3394 if (!hinfo) { 3395 pr_warn("Failed to allocate TCP ehash (entries: %u) " 3396 "for a netns, fallback to the global one\n", 3397 ehash_entries); 3398 fallback: 3399 hinfo = &tcp_hashinfo; 3400 ehash_entries = tcp_hashinfo.ehash_mask + 1; 3401 } 3402 3403 net->ipv4.tcp_death_row.hashinfo = hinfo; 3404 net->ipv4.tcp_death_row.sysctl_max_tw_buckets = ehash_entries / 2; 3405 net->ipv4.sysctl_max_syn_backlog = max(128U, ehash_entries / 128); 3406 } 3407 3408 static int __net_init tcp_sk_init(struct net *net) 3409 { 3410 net->ipv4.sysctl_tcp_ecn = 2; 3411 net->ipv4.sysctl_tcp_ecn_fallback = 1; 3412 3413 net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS; 3414 net->ipv4.sysctl_tcp_min_snd_mss = TCP_MIN_SND_MSS; 3415 net->ipv4.sysctl_tcp_probe_threshold = TCP_PROBE_THRESHOLD; 3416 net->ipv4.sysctl_tcp_probe_interval = TCP_PROBE_INTERVAL; 3417 net->ipv4.sysctl_tcp_mtu_probe_floor = TCP_MIN_SND_MSS; 3418 3419 net->ipv4.sysctl_tcp_keepalive_time = TCP_KEEPALIVE_TIME; 3420 net->ipv4.sysctl_tcp_keepalive_probes = TCP_KEEPALIVE_PROBES; 3421 net->ipv4.sysctl_tcp_keepalive_intvl = TCP_KEEPALIVE_INTVL; 3422 3423 net->ipv4.sysctl_tcp_syn_retries = TCP_SYN_RETRIES; 3424 net->ipv4.sysctl_tcp_synack_retries = TCP_SYNACK_RETRIES; 3425 net->ipv4.sysctl_tcp_syncookies = 1; 3426 net->ipv4.sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH; 3427 net->ipv4.sysctl_tcp_retries1 = TCP_RETR1; 3428 net->ipv4.sysctl_tcp_retries2 = TCP_RETR2; 3429 net->ipv4.sysctl_tcp_orphan_retries = 0; 3430 net->ipv4.sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT; 3431 net->ipv4.sysctl_tcp_notsent_lowat = UINT_MAX; 3432 net->ipv4.sysctl_tcp_tw_reuse = 2; 3433 net->ipv4.sysctl_tcp_no_ssthresh_metrics_save = 1; 3434 3435 refcount_set(&net->ipv4.tcp_death_row.tw_refcount, 1); 3436 tcp_set_hashinfo(net); 3437 3438 net->ipv4.sysctl_tcp_sack = 1; 3439 net->ipv4.sysctl_tcp_window_scaling = 1; 3440 net->ipv4.sysctl_tcp_timestamps = 1; 3441 net->ipv4.sysctl_tcp_early_retrans = 3; 3442 net->ipv4.sysctl_tcp_recovery = TCP_RACK_LOSS_DETECTION; 3443 net->ipv4.sysctl_tcp_slow_start_after_idle = 1; /* By default, RFC2861 behavior. */ 3444 net->ipv4.sysctl_tcp_retrans_collapse = 1; 3445 net->ipv4.sysctl_tcp_max_reordering = 300; 3446 net->ipv4.sysctl_tcp_dsack = 1; 3447 net->ipv4.sysctl_tcp_app_win = 31; 3448 net->ipv4.sysctl_tcp_adv_win_scale = 1; 3449 net->ipv4.sysctl_tcp_frto = 2; 3450 net->ipv4.sysctl_tcp_moderate_rcvbuf = 1; 3451 /* This limits the percentage of the congestion window which we 3452 * will allow a single TSO frame to consume. Building TSO frames 3453 * which are too large can cause TCP streams to be bursty. 3454 */ 3455 net->ipv4.sysctl_tcp_tso_win_divisor = 3; 3456 /* Default TSQ limit of 16 TSO segments */ 3457 net->ipv4.sysctl_tcp_limit_output_bytes = 16 * 65536; 3458 3459 /* rfc5961 challenge ack rate limiting, per net-ns, disabled by default. */ 3460 net->ipv4.sysctl_tcp_challenge_ack_limit = INT_MAX; 3461 3462 net->ipv4.sysctl_tcp_min_tso_segs = 2; 3463 net->ipv4.sysctl_tcp_tso_rtt_log = 9; /* 2^9 = 512 usec */ 3464 net->ipv4.sysctl_tcp_min_rtt_wlen = 300; 3465 net->ipv4.sysctl_tcp_autocorking = 1; 3466 net->ipv4.sysctl_tcp_invalid_ratelimit = HZ/2; 3467 net->ipv4.sysctl_tcp_pacing_ss_ratio = 200; 3468 net->ipv4.sysctl_tcp_pacing_ca_ratio = 120; 3469 if (net != &init_net) { 3470 memcpy(net->ipv4.sysctl_tcp_rmem, 3471 init_net.ipv4.sysctl_tcp_rmem, 3472 sizeof(init_net.ipv4.sysctl_tcp_rmem)); 3473 memcpy(net->ipv4.sysctl_tcp_wmem, 3474 init_net.ipv4.sysctl_tcp_wmem, 3475 sizeof(init_net.ipv4.sysctl_tcp_wmem)); 3476 } 3477 net->ipv4.sysctl_tcp_comp_sack_delay_ns = NSEC_PER_MSEC; 3478 net->ipv4.sysctl_tcp_comp_sack_slack_ns = 100 * NSEC_PER_USEC; 3479 net->ipv4.sysctl_tcp_comp_sack_nr = 44; 3480 net->ipv4.sysctl_tcp_backlog_ack_defer = 1; 3481 net->ipv4.sysctl_tcp_fastopen = TFO_CLIENT_ENABLE; 3482 net->ipv4.sysctl_tcp_fastopen_blackhole_timeout = 0; 3483 atomic_set(&net->ipv4.tfo_active_disable_times, 0); 3484 3485 /* Set default values for PLB */ 3486 net->ipv4.sysctl_tcp_plb_enabled = 0; /* Disabled by default */ 3487 net->ipv4.sysctl_tcp_plb_idle_rehash_rounds = 3; 3488 net->ipv4.sysctl_tcp_plb_rehash_rounds = 12; 3489 net->ipv4.sysctl_tcp_plb_suspend_rto_sec = 60; 3490 /* Default congestion threshold for PLB to mark a round is 50% */ 3491 net->ipv4.sysctl_tcp_plb_cong_thresh = (1 << TCP_PLB_SCALE) / 2; 3492 3493 /* Reno is always built in */ 3494 if (!net_eq(net, &init_net) && 3495 bpf_try_module_get(init_net.ipv4.tcp_congestion_control, 3496 init_net.ipv4.tcp_congestion_control->owner)) 3497 net->ipv4.tcp_congestion_control = init_net.ipv4.tcp_congestion_control; 3498 else 3499 net->ipv4.tcp_congestion_control = &tcp_reno; 3500 3501 net->ipv4.sysctl_tcp_syn_linear_timeouts = 4; 3502 net->ipv4.sysctl_tcp_shrink_window = 0; 3503 3504 net->ipv4.sysctl_tcp_pingpong_thresh = 1; 3505 net->ipv4.sysctl_tcp_rto_min_us = jiffies_to_usecs(TCP_RTO_MIN); 3506 3507 return 0; 3508 } 3509 3510 static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list) 3511 { 3512 struct net *net; 3513 3514 tcp_twsk_purge(net_exit_list); 3515 3516 list_for_each_entry(net, net_exit_list, exit_list) { 3517 inet_pernet_hashinfo_free(net->ipv4.tcp_death_row.hashinfo); 3518 WARN_ON_ONCE(!refcount_dec_and_test(&net->ipv4.tcp_death_row.tw_refcount)); 3519 tcp_fastopen_ctx_destroy(net); 3520 } 3521 } 3522 3523 static struct pernet_operations __net_initdata tcp_sk_ops = { 3524 .init = tcp_sk_init, 3525 .exit = tcp_sk_exit, 3526 .exit_batch = tcp_sk_exit_batch, 3527 }; 3528 3529 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 3530 DEFINE_BPF_ITER_FUNC(tcp, struct bpf_iter_meta *meta, 3531 struct sock_common *sk_common, uid_t uid) 3532 3533 #define INIT_BATCH_SZ 16 3534 3535 static int bpf_iter_init_tcp(void *priv_data, struct bpf_iter_aux_info *aux) 3536 { 3537 struct bpf_tcp_iter_state *iter = priv_data; 3538 int err; 3539 3540 err = bpf_iter_init_seq_net(priv_data, aux); 3541 if (err) 3542 return err; 3543 3544 err = bpf_iter_tcp_realloc_batch(iter, INIT_BATCH_SZ); 3545 if (err) { 3546 bpf_iter_fini_seq_net(priv_data); 3547 return err; 3548 } 3549 3550 return 0; 3551 } 3552 3553 static void bpf_iter_fini_tcp(void *priv_data) 3554 { 3555 struct bpf_tcp_iter_state *iter = priv_data; 3556 3557 bpf_iter_fini_seq_net(priv_data); 3558 kvfree(iter->batch); 3559 } 3560 3561 static const struct bpf_iter_seq_info tcp_seq_info = { 3562 .seq_ops = &bpf_iter_tcp_seq_ops, 3563 .init_seq_private = bpf_iter_init_tcp, 3564 .fini_seq_private = bpf_iter_fini_tcp, 3565 .seq_priv_size = sizeof(struct bpf_tcp_iter_state), 3566 }; 3567 3568 static const struct bpf_func_proto * 3569 bpf_iter_tcp_get_func_proto(enum bpf_func_id func_id, 3570 const struct bpf_prog *prog) 3571 { 3572 switch (func_id) { 3573 case BPF_FUNC_setsockopt: 3574 return &bpf_sk_setsockopt_proto; 3575 case BPF_FUNC_getsockopt: 3576 return &bpf_sk_getsockopt_proto; 3577 default: 3578 return NULL; 3579 } 3580 } 3581 3582 static struct bpf_iter_reg tcp_reg_info = { 3583 .target = "tcp", 3584 .ctx_arg_info_size = 1, 3585 .ctx_arg_info = { 3586 { offsetof(struct bpf_iter__tcp, sk_common), 3587 PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED }, 3588 }, 3589 .get_func_proto = bpf_iter_tcp_get_func_proto, 3590 .seq_info = &tcp_seq_info, 3591 }; 3592 3593 static void __init bpf_iter_register(void) 3594 { 3595 tcp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON]; 3596 if (bpf_iter_reg_target(&tcp_reg_info)) 3597 pr_warn("Warning: could not register bpf iterator tcp\n"); 3598 } 3599 3600 #endif 3601 3602 void __init tcp_v4_init(void) 3603 { 3604 int cpu, res; 3605 3606 for_each_possible_cpu(cpu) { 3607 struct sock *sk; 3608 3609 res = inet_ctl_sock_create(&sk, PF_INET, SOCK_RAW, 3610 IPPROTO_TCP, &init_net); 3611 if (res) 3612 panic("Failed to create the TCP control socket.\n"); 3613 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); 3614 3615 /* Please enforce IP_DF and IPID==0 for RST and 3616 * ACK sent in SYN-RECV and TIME-WAIT state. 3617 */ 3618 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DO; 3619 3620 sk->sk_clockid = CLOCK_MONOTONIC; 3621 3622 per_cpu(ipv4_tcp_sk, cpu) = sk; 3623 } 3624 if (register_pernet_subsys(&tcp_sk_ops)) 3625 panic("Failed to create the TCP control socket.\n"); 3626 3627 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 3628 bpf_iter_register(); 3629 #endif 3630 } 3631