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 * The User Datagram Protocol (UDP). 8 * 9 * Authors: Ross Biro 10 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 11 * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 12 * Alan Cox, <alan@lxorguk.ukuu.org.uk> 13 * Hirokazu Takahashi, <taka@valinux.co.jp> 14 * 15 * Fixes: 16 * Alan Cox : verify_area() calls 17 * Alan Cox : stopped close while in use off icmp 18 * messages. Not a fix but a botch that 19 * for udp at least is 'valid'. 20 * Alan Cox : Fixed icmp handling properly 21 * Alan Cox : Correct error for oversized datagrams 22 * Alan Cox : Tidied select() semantics. 23 * Alan Cox : udp_err() fixed properly, also now 24 * select and read wake correctly on errors 25 * Alan Cox : udp_send verify_area moved to avoid mem leak 26 * Alan Cox : UDP can count its memory 27 * Alan Cox : send to an unknown connection causes 28 * an ECONNREFUSED off the icmp, but 29 * does NOT close. 30 * Alan Cox : Switched to new sk_buff handlers. No more backlog! 31 * Alan Cox : Using generic datagram code. Even smaller and the PEEK 32 * bug no longer crashes it. 33 * Fred Van Kempen : Net2e support for sk->broadcast. 34 * Alan Cox : Uses skb_free_datagram 35 * Alan Cox : Added get/set sockopt support. 36 * Alan Cox : Broadcasting without option set returns EACCES. 37 * Alan Cox : No wakeup calls. Instead we now use the callbacks. 38 * Alan Cox : Use ip_tos and ip_ttl 39 * Alan Cox : SNMP Mibs 40 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 41 * Matt Dillon : UDP length checks. 42 * Alan Cox : Smarter af_inet used properly. 43 * Alan Cox : Use new kernel side addressing. 44 * Alan Cox : Incorrect return on truncated datagram receive. 45 * Arnt Gulbrandsen : New udp_send and stuff 46 * Alan Cox : Cache last socket 47 * Alan Cox : Route cache 48 * Jon Peatfield : Minor efficiency fix to sendto(). 49 * Mike Shaver : RFC1122 checks. 50 * Alan Cox : Nonblocking error fix. 51 * Willy Konynenberg : Transparent proxying support. 52 * Mike McLagan : Routing by source 53 * David S. Miller : New socket lookup architecture. 54 * Last socket cache retained as it 55 * does have a high hit rate. 56 * Olaf Kirch : Don't linearise iovec on sendmsg. 57 * Andi Kleen : Some cleanups, cache destination entry 58 * for connect. 59 * Vitaly E. Lavrov : Transparent proxy revived after year coma. 60 * Melvin Smith : Check msg_name not msg_namelen in sendto(), 61 * return ENOTCONN for unconnected sockets (POSIX) 62 * Janos Farkas : don't deliver multi/broadcasts to a different 63 * bound-to-device socket 64 * Hirokazu Takahashi : HW checksumming for outgoing UDP 65 * datagrams. 66 * Hirokazu Takahashi : sendfile() on UDP works now. 67 * Arnaldo C. Melo : convert /proc/net/udp to seq_file 68 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 69 * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 70 * a single port at the same time. 71 * Derek Atkins <derek@ihtfp.com>: Add Encapsulation Support 72 * James Chapman : Add L2TP encapsulation type. 73 */ 74 75 #define pr_fmt(fmt) "UDP: " fmt 76 77 #include <linux/bpf-cgroup.h> 78 #include <linux/uaccess.h> 79 #include <linux/uio.h> 80 #include <asm/ioctls.h> 81 #include <linux/memblock.h> 82 #include <linux/highmem.h> 83 #include <linux/types.h> 84 #include <linux/fcntl.h> 85 #include <linux/module.h> 86 #include <linux/socket.h> 87 #include <linux/sockios.h> 88 #include <linux/igmp.h> 89 #include <linux/inetdevice.h> 90 #include <linux/in.h> 91 #include <linux/errno.h> 92 #include <linux/timer.h> 93 #include <linux/mm.h> 94 #include <linux/inet.h> 95 #include <linux/netdevice.h> 96 #include <linux/slab.h> 97 #include <linux/sock_diag.h> 98 #include <net/tcp_states.h> 99 #include <linux/skbuff.h> 100 #include <linux/proc_fs.h> 101 #include <linux/seq_file.h> 102 #include <net/aligned_data.h> 103 #include <net/net_namespace.h> 104 #include <net/icmp.h> 105 #include <net/inet_common.h> 106 #include <net/inet_hashtables.h> 107 #include <net/ip.h> 108 #include <net/ip_tunnels.h> 109 #include <net/route.h> 110 #include <net/checksum.h> 111 #include <net/gso.h> 112 #include <net/xfrm.h> 113 #include <trace/events/udp.h> 114 #include <linux/static_key.h> 115 #include <linux/btf_ids.h> 116 #include <trace/events/skb.h> 117 #include <net/busy_poll.h> 118 #include <net/sock_reuseport.h> 119 #include <net/addrconf.h> 120 #include <net/udp_tunnel.h> 121 #include <net/gro.h> 122 #include <net/rps.h> 123 124 struct udp_table udp_table __read_mostly; 125 126 long sysctl_udp_mem[3] __read_mostly; 127 128 DEFINE_PER_CPU(int, udp_memory_per_cpu_fw_alloc); 129 EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc); 130 131 #define MAX_UDP_PORTS 65536 132 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN_PERNET) 133 134 static int udp_lib_lport_inuse(struct net *net, __u16 num, 135 const struct udp_hslot *hslot, 136 unsigned long *bitmap, 137 struct sock *sk, unsigned int log) 138 { 139 kuid_t uid = sk_uid(sk); 140 struct sock *sk2; 141 142 sk_for_each(sk2, &hslot->head) { 143 if (net_eq(sock_net(sk2), net) && 144 sk2 != sk && 145 (bitmap || udp_sk(sk2)->udp_port_hash == num) && 146 (!sk2->sk_reuse || !sk->sk_reuse) && 147 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 148 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 149 inet_rcv_saddr_equal(sk, sk2, true)) { 150 if (sk2->sk_reuseport && sk->sk_reuseport && 151 !rcu_access_pointer(sk->sk_reuseport_cb) && 152 uid_eq(uid, sk_uid(sk2))) { 153 if (!bitmap) 154 return 0; 155 } else { 156 if (!bitmap) 157 return 1; 158 __set_bit(udp_sk(sk2)->udp_port_hash >> log, 159 bitmap); 160 } 161 } 162 } 163 return 0; 164 } 165 166 /* 167 * Note: we still hold spinlock of primary hash chain, so no other writer 168 * can insert/delete a socket with local_port == num 169 */ 170 static int udp_lib_lport_inuse2(struct net *net, __u16 num, 171 struct udp_hslot *hslot2, 172 struct sock *sk) 173 { 174 kuid_t uid = sk_uid(sk); 175 struct sock *sk2; 176 int res = 0; 177 178 spin_lock(&hslot2->lock); 179 udp_portaddr_for_each_entry(sk2, &hslot2->head) { 180 if (net_eq(sock_net(sk2), net) && 181 sk2 != sk && 182 (udp_sk(sk2)->udp_port_hash == num) && 183 (!sk2->sk_reuse || !sk->sk_reuse) && 184 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 185 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 186 inet_rcv_saddr_equal(sk, sk2, true)) { 187 if (sk2->sk_reuseport && sk->sk_reuseport && 188 !rcu_access_pointer(sk->sk_reuseport_cb) && 189 uid_eq(uid, sk_uid(sk2))) { 190 res = 0; 191 } else { 192 res = 1; 193 } 194 break; 195 } 196 } 197 spin_unlock(&hslot2->lock); 198 return res; 199 } 200 201 static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 202 { 203 struct net *net = sock_net(sk); 204 kuid_t uid = sk_uid(sk); 205 struct sock *sk2; 206 207 sk_for_each(sk2, &hslot->head) { 208 if (net_eq(sock_net(sk2), net) && 209 sk2 != sk && 210 sk2->sk_family == sk->sk_family && 211 ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 212 (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 213 (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 214 sk2->sk_reuseport && uid_eq(uid, sk_uid(sk2)) && 215 inet_rcv_saddr_equal(sk, sk2, false)) { 216 return reuseport_add_sock(sk, sk2, 217 inet_rcv_saddr_any(sk)); 218 } 219 } 220 221 return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 222 } 223 224 /** 225 * udp_lib_get_port - UDP port lookup for IPv4 and IPv6 226 * 227 * @sk: socket struct in question 228 * @snum: port number to look up 229 * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 230 * with NULL address 231 */ 232 int udp_lib_get_port(struct sock *sk, unsigned short snum, 233 unsigned int hash2_nulladdr) 234 { 235 struct udp_hslot *hslot, *hslot2; 236 struct net *net = sock_net(sk); 237 struct udp_table *udptable; 238 int error = -EADDRINUSE; 239 240 udptable = net->ipv4.udp_table; 241 242 if (!snum) { 243 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 244 unsigned short first, last; 245 int low, high, remaining; 246 unsigned int rand; 247 248 inet_sk_get_local_port_range(sk, &low, &high); 249 remaining = (high - low) + 1; 250 251 rand = get_random_u32(); 252 first = reciprocal_scale(rand, remaining) + low; 253 /* 254 * force rand to be an odd multiple of UDP_HTABLE_SIZE 255 */ 256 rand = (rand | 1) * (udptable->mask + 1); 257 last = first + udptable->mask + 1; 258 do { 259 hslot = udp_hashslot(udptable, net, first); 260 bitmap_zero(bitmap, PORTS_PER_CHAIN); 261 spin_lock_bh(&hslot->lock); 262 udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 263 udptable->log); 264 265 snum = first; 266 /* 267 * Iterate on all possible values of snum for this hash. 268 * Using steps of an odd multiple of UDP_HTABLE_SIZE 269 * give us randomization and full range coverage. 270 */ 271 do { 272 if (low <= snum && snum <= high && 273 !test_bit(snum >> udptable->log, bitmap) && 274 !inet_is_local_reserved_port(net, snum)) 275 goto found; 276 snum += rand; 277 } while (snum != first); 278 spin_unlock_bh(&hslot->lock); 279 cond_resched(); 280 } while (++first != last); 281 goto fail; 282 } else { 283 hslot = udp_hashslot(udptable, net, snum); 284 spin_lock_bh(&hslot->lock); 285 if (inet_use_hash2_on_bind(sk) && hslot->count > 10) { 286 int exist; 287 unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 288 289 slot2 &= udptable->mask; 290 hash2_nulladdr &= udptable->mask; 291 292 hslot2 = udp_hashslot2(udptable, slot2); 293 if (hslot->count < hslot2->count) 294 goto scan_primary_hash; 295 296 exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 297 if (!exist && (hash2_nulladdr != slot2)) { 298 hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 299 exist = udp_lib_lport_inuse2(net, snum, hslot2, 300 sk); 301 } 302 if (exist) 303 goto fail_unlock; 304 else 305 goto found; 306 } 307 scan_primary_hash: 308 if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 309 goto fail_unlock; 310 } 311 found: 312 inet_sk(sk)->inet_num = snum; 313 udp_sk(sk)->udp_port_hash = snum; 314 udp_sk(sk)->udp_portaddr_hash ^= snum; 315 if (sk_unhashed(sk)) { 316 if (sk->sk_reuseport && 317 udp_reuseport_add_sock(sk, hslot)) { 318 inet_sk(sk)->inet_num = 0; 319 udp_sk(sk)->udp_port_hash = 0; 320 udp_sk(sk)->udp_portaddr_hash ^= snum; 321 goto fail_unlock; 322 } 323 324 sock_set_flag(sk, SOCK_RCU_FREE); 325 326 sk_add_node_rcu(sk, &hslot->head); 327 hslot->count++; 328 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 329 330 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 331 spin_lock(&hslot2->lock); 332 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 333 sk->sk_family == AF_INET6) 334 hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 335 &hslot2->head); 336 else 337 hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 338 &hslot2->head); 339 hslot2->count++; 340 spin_unlock(&hslot2->lock); 341 } 342 343 error = 0; 344 fail_unlock: 345 spin_unlock_bh(&hslot->lock); 346 fail: 347 return error; 348 } 349 350 static int udp_v4_get_port(struct sock *sk, unsigned short snum) 351 { 352 unsigned int hash2_nulladdr = 353 ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 354 unsigned int hash2_partial = 355 ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 356 357 /* precompute partial secondary hash */ 358 udp_sk(sk)->udp_portaddr_hash = hash2_partial; 359 return udp_lib_get_port(sk, snum, hash2_nulladdr); 360 } 361 362 static __always_inline int 363 compute_score(struct sock *sk, const struct net *net, 364 __be32 saddr, __be16 sport, __be32 daddr, 365 unsigned short hnum, int dif, int sdif) 366 { 367 int score; 368 struct inet_sock *inet; 369 bool dev_match; 370 371 if (!net_eq(sock_net(sk), net) || 372 udp_sk(sk)->udp_port_hash != hnum || 373 ipv6_only_sock(sk)) 374 return -1; 375 376 if (sk->sk_rcv_saddr != daddr) 377 return -1; 378 379 score = (sk->sk_family == PF_INET) ? 2 : 1; 380 381 inet = inet_sk(sk); 382 if (inet->inet_daddr) { 383 if (inet->inet_daddr != saddr) 384 return -1; 385 score += 4; 386 } 387 388 if (inet->inet_dport) { 389 if (inet->inet_dport != sport) 390 return -1; 391 score += 4; 392 } 393 394 dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 395 dif, sdif); 396 if (!dev_match) 397 return -1; 398 if (sk->sk_bound_dev_if) 399 score += 4; 400 401 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 402 score++; 403 return score; 404 } 405 406 u32 udp_ehashfn(const struct net *net, const __be32 laddr, const __u16 lport, 407 const __be32 faddr, const __be16 fport) 408 { 409 net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 410 411 return __inet_ehashfn(laddr, lport, faddr, fport, 412 udp_ehash_secret + net_hash_mix(net)); 413 } 414 415 /** 416 * udp4_lib_lookup1() - Simplified lookup using primary hash (destination port) 417 * @net: Network namespace 418 * @saddr: Source address, network order 419 * @sport: Source port, network order 420 * @daddr: Destination address, network order 421 * @hnum: Destination port, host order 422 * @dif: Destination interface index 423 * @sdif: Destination bridge port index, if relevant 424 * @udptable: Set of UDP hash tables 425 * 426 * Simplified lookup to be used as fallback if no sockets are found due to a 427 * potential race between (receive) address change, and lookup happening before 428 * the rehash operation. This function ignores SO_REUSEPORT groups while scoring 429 * result sockets, because if we have one, we don't need the fallback at all. 430 * 431 * Called under rcu_read_lock(). 432 * 433 * Return: socket with highest matching score if any, NULL if none 434 */ 435 static struct sock *udp4_lib_lookup1(const struct net *net, 436 __be32 saddr, __be16 sport, 437 __be32 daddr, unsigned int hnum, 438 int dif, int sdif, 439 const struct udp_table *udptable) 440 { 441 unsigned int slot = udp_hashfn(net, hnum, udptable->mask); 442 struct udp_hslot *hslot = &udptable->hash[slot]; 443 struct sock *sk, *result = NULL; 444 int score, badness = 0; 445 446 sk_for_each_rcu(sk, &hslot->head) { 447 score = compute_score(sk, net, 448 saddr, sport, daddr, hnum, dif, sdif); 449 if (score > badness) { 450 result = sk; 451 badness = score; 452 } 453 } 454 455 return result; 456 } 457 458 /* called with rcu_read_lock() */ 459 static struct sock *udp4_lib_lookup2(const struct net *net, 460 __be32 saddr, __be16 sport, 461 __be32 daddr, unsigned int hnum, 462 int dif, int sdif, 463 struct udp_hslot *hslot2, 464 struct sk_buff *skb) 465 { 466 struct sock *sk, *result; 467 int score, badness; 468 bool need_rescore; 469 470 result = NULL; 471 badness = 0; 472 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 473 need_rescore = false; 474 rescore: 475 score = compute_score(need_rescore ? result : sk, net, saddr, 476 sport, daddr, hnum, dif, sdif); 477 if (score > badness) { 478 badness = score; 479 480 if (need_rescore) 481 continue; 482 483 if (sk->sk_state == TCP_ESTABLISHED) { 484 result = sk; 485 continue; 486 } 487 488 result = inet_lookup_reuseport(net, sk, skb, sizeof(struct udphdr), 489 saddr, sport, daddr, hnum, udp_ehashfn); 490 if (!result) { 491 result = sk; 492 continue; 493 } 494 495 /* Fall back to scoring if group has connections */ 496 if (!reuseport_has_conns(sk)) 497 return result; 498 499 /* Reuseport logic returned an error, keep original score. */ 500 if (IS_ERR(result)) 501 continue; 502 503 /* compute_score is too long of a function to be 504 * inlined twice here, and calling it uninlined 505 * here yields measurable overhead for some 506 * workloads. Work around it by jumping 507 * backwards to rescore 'result'. 508 */ 509 need_rescore = true; 510 goto rescore; 511 } 512 } 513 return result; 514 } 515 516 #if IS_ENABLED(CONFIG_BASE_SMALL) 517 static struct sock *udp4_lib_lookup4(const struct net *net, 518 __be32 saddr, __be16 sport, 519 __be32 daddr, unsigned int hnum, 520 int dif, int sdif, 521 struct udp_table *udptable) 522 { 523 return NULL; 524 } 525 526 static void udp_rehash4(struct udp_table *udptable, struct sock *sk, 527 u16 newhash4) 528 { 529 } 530 531 static void udp_unhash4(struct udp_table *udptable, struct sock *sk) 532 { 533 } 534 #else /* !CONFIG_BASE_SMALL */ 535 static struct sock *udp4_lib_lookup4(const struct net *net, 536 __be32 saddr, __be16 sport, 537 __be32 daddr, unsigned int hnum, 538 int dif, int sdif, 539 struct udp_table *udptable) 540 { 541 const __portpair ports = INET_COMBINED_PORTS(sport, hnum); 542 const struct hlist_nulls_node *node; 543 struct udp_hslot *hslot4; 544 unsigned int hash4, slot; 545 struct udp_sock *up; 546 struct sock *sk; 547 548 hash4 = udp_ehashfn(net, daddr, hnum, saddr, sport); 549 slot = hash4 & udptable->mask; 550 hslot4 = &udptable->hash4[slot]; 551 INET_ADDR_COOKIE(acookie, saddr, daddr); 552 553 begin: 554 /* SLAB_TYPESAFE_BY_RCU not used, so we don't need to touch sk_refcnt */ 555 udp_lrpa_for_each_entry_rcu(up, node, &hslot4->nulls_head) { 556 sk = (struct sock *)up; 557 if (inet_match(net, sk, acookie, ports, dif, sdif)) 558 return sk; 559 } 560 561 /* if the nulls value we got at the end of this lookup is not the 562 * expected one, we must restart lookup. We probably met an item that 563 * was moved to another chain due to rehash. 564 */ 565 if (get_nulls_value(node) != slot) 566 goto begin; 567 568 return NULL; 569 } 570 571 /* udp_rehash4() only checks hslot4, and hash4_cnt is not processed. */ 572 static void udp_rehash4(struct udp_table *udptable, struct sock *sk, 573 u16 newhash4) 574 { 575 struct udp_hslot *hslot4, *nhslot4; 576 577 hslot4 = udp_hashslot4(udptable, udp_sk(sk)->udp_lrpa_hash); 578 nhslot4 = udp_hashslot4(udptable, newhash4); 579 udp_sk(sk)->udp_lrpa_hash = newhash4; 580 581 if (hslot4 != nhslot4) { 582 spin_lock_bh(&hslot4->lock); 583 hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_lrpa_node); 584 hslot4->count--; 585 spin_unlock_bh(&hslot4->lock); 586 587 spin_lock_bh(&nhslot4->lock); 588 hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_lrpa_node, 589 &nhslot4->nulls_head); 590 nhslot4->count++; 591 spin_unlock_bh(&nhslot4->lock); 592 } 593 } 594 595 static void udp_unhash4(struct udp_table *udptable, struct sock *sk) 596 { 597 struct udp_hslot *hslot2, *hslot4; 598 599 if (udp_hashed4(sk)) { 600 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 601 hslot4 = udp_hashslot4(udptable, udp_sk(sk)->udp_lrpa_hash); 602 603 spin_lock(&hslot4->lock); 604 hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_lrpa_node); 605 hslot4->count--; 606 spin_unlock(&hslot4->lock); 607 608 spin_lock(&hslot2->lock); 609 udp_hash4_dec(hslot2); 610 spin_unlock(&hslot2->lock); 611 } 612 } 613 614 void udp_lib_hash4(struct sock *sk, u16 hash) 615 { 616 struct udp_hslot *hslot, *hslot2, *hslot4; 617 struct net *net = sock_net(sk); 618 struct udp_table *udptable; 619 620 /* Connected udp socket can re-connect to another remote address, which 621 * will be handled by rehash. Thus no need to redo hash4 here. 622 */ 623 if (udp_hashed4(sk)) 624 return; 625 626 udptable = net->ipv4.udp_table; 627 hslot = udp_hashslot(udptable, net, udp_sk(sk)->udp_port_hash); 628 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 629 hslot4 = udp_hashslot4(udptable, hash); 630 udp_sk(sk)->udp_lrpa_hash = hash; 631 632 spin_lock_bh(&hslot->lock); 633 if (rcu_access_pointer(sk->sk_reuseport_cb)) 634 reuseport_detach_sock(sk); 635 636 spin_lock(&hslot4->lock); 637 hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_lrpa_node, 638 &hslot4->nulls_head); 639 hslot4->count++; 640 spin_unlock(&hslot4->lock); 641 642 spin_lock(&hslot2->lock); 643 udp_hash4_inc(hslot2); 644 spin_unlock(&hslot2->lock); 645 646 spin_unlock_bh(&hslot->lock); 647 } 648 649 /* call with sock lock */ 650 void udp4_hash4(struct sock *sk) 651 { 652 struct net *net = sock_net(sk); 653 unsigned int hash; 654 655 if (sk_unhashed(sk) || sk->sk_rcv_saddr == htonl(INADDR_ANY)) 656 return; 657 658 hash = udp_ehashfn(net, sk->sk_rcv_saddr, sk->sk_num, 659 sk->sk_daddr, sk->sk_dport); 660 661 udp_lib_hash4(sk, hash); 662 } 663 #endif /* CONFIG_BASE_SMALL */ 664 665 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 666 * harder than this. -DaveM 667 */ 668 struct sock *__udp4_lib_lookup(const struct net *net, __be32 saddr, 669 __be16 sport, __be32 daddr, __be16 dport, 670 int dif, int sdif, struct sk_buff *skb) 671 { 672 struct udp_table *udptable = net->ipv4.udp_table; 673 unsigned short hnum = ntohs(dport); 674 struct udp_hslot *hslot2; 675 struct sock *result, *sk; 676 unsigned int hash2; 677 678 hash2 = ipv4_portaddr_hash(net, daddr, hnum); 679 hslot2 = udp_hashslot2(udptable, hash2); 680 681 if (udp_has_hash4(hslot2)) { 682 result = udp4_lib_lookup4(net, saddr, sport, daddr, hnum, 683 dif, sdif, udptable); 684 if (result) /* udp4_lib_lookup4 return sk or NULL */ 685 return result; 686 } 687 688 /* Lookup connected or non-wildcard socket */ 689 result = udp4_lib_lookup2(net, saddr, sport, 690 daddr, hnum, dif, sdif, 691 hslot2, skb); 692 if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 693 goto done; 694 695 /* Lookup redirect from BPF */ 696 if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 697 sk = inet_lookup_run_sk_lookup(net, IPPROTO_UDP, skb, sizeof(struct udphdr), 698 saddr, sport, daddr, hnum, dif, 699 udp_ehashfn); 700 if (sk) { 701 result = sk; 702 goto done; 703 } 704 } 705 706 /* Got non-wildcard socket or error on first lookup */ 707 if (result) 708 goto done; 709 710 /* Lookup wildcard sockets */ 711 hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 712 hslot2 = udp_hashslot2(udptable, hash2); 713 714 result = udp4_lib_lookup2(net, saddr, sport, 715 htonl(INADDR_ANY), hnum, dif, sdif, 716 hslot2, skb); 717 if (!IS_ERR_OR_NULL(result)) 718 goto done; 719 720 /* Primary hash (destination port) lookup as fallback for this race: 721 * 1. __ip4_datagram_connect() sets sk_rcv_saddr 722 * 2. lookup (this function): new sk_rcv_saddr, hashes not updated yet 723 * 3. rehash operation updating _secondary and four-tuple_ hashes 724 * The primary hash doesn't need an update after 1., so, thanks to this 725 * further step, 1. and 3. don't need to be atomic against the lookup. 726 */ 727 result = udp4_lib_lookup1(net, saddr, sport, daddr, hnum, dif, sdif, 728 udptable); 729 730 done: 731 if (IS_ERR(result)) 732 return NULL; 733 return result; 734 } 735 EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 736 737 static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 738 __be16 sport, __be16 dport) 739 { 740 const struct iphdr *iph = ip_hdr(skb); 741 742 return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 743 iph->daddr, dport, inet_iif(skb), 744 inet_sdif(skb), skb); 745 } 746 747 struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb, 748 __be16 sport, __be16 dport) 749 { 750 const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation]; 751 const struct iphdr *iph = (struct iphdr *)(skb->data + offset); 752 int iif, sdif; 753 754 inet_get_iif_sdif(skb, &iif, &sdif); 755 756 return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 757 iph->daddr, dport, iif, sdif, NULL); 758 } 759 760 /* Must be called under rcu_read_lock(). 761 * Does increment socket refcount. 762 */ 763 #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 764 struct sock *udp4_lib_lookup(const struct net *net, __be32 saddr, __be16 sport, 765 __be32 daddr, __be16 dport, int dif) 766 { 767 struct sock *sk; 768 769 sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, 0, NULL); 770 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 771 sk = NULL; 772 return sk; 773 } 774 EXPORT_SYMBOL_GPL(udp4_lib_lookup); 775 #endif 776 777 static inline bool __udp_is_mcast_sock(struct net *net, const struct sock *sk, 778 __be16 loc_port, __be32 loc_addr, 779 __be16 rmt_port, __be32 rmt_addr, 780 int dif, int sdif, unsigned short hnum) 781 { 782 const struct inet_sock *inet = inet_sk(sk); 783 784 if (!net_eq(sock_net(sk), net) || 785 udp_sk(sk)->udp_port_hash != hnum || 786 (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 787 (inet->inet_dport != rmt_port && inet->inet_dport) || 788 (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 789 ipv6_only_sock(sk) || 790 !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 791 return false; 792 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 793 return false; 794 return true; 795 } 796 797 DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 798 799 #if IS_ENABLED(CONFIG_IPV6) 800 DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 801 #endif 802 803 void udp_encap_enable(void) 804 { 805 static_branch_inc(&udp_encap_needed_key); 806 } 807 EXPORT_SYMBOL(udp_encap_enable); 808 809 void udp_encap_disable(void) 810 { 811 static_branch_dec(&udp_encap_needed_key); 812 } 813 EXPORT_SYMBOL(udp_encap_disable); 814 815 /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 816 * through error handlers in encapsulations looking for a match. 817 */ 818 static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 819 { 820 int i; 821 822 for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 823 int (*handler)(struct sk_buff *skb, u32 info); 824 const struct ip_tunnel_encap_ops *encap; 825 826 encap = rcu_dereference(iptun_encaps[i]); 827 if (!encap) 828 continue; 829 handler = encap->err_handler; 830 if (handler && !handler(skb, info)) 831 return 0; 832 } 833 834 return -ENOENT; 835 } 836 837 /* Try to match ICMP errors to UDP tunnels by looking up a socket without 838 * reversing source and destination port: this will match tunnels that force the 839 * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 840 * lwtunnels might actually break this assumption by being configured with 841 * different destination ports on endpoints, in this case we won't be able to 842 * trace ICMP messages back to them. 843 * 844 * If this doesn't match any socket, probe tunnels with arbitrary destination 845 * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 846 * we've sent packets to won't necessarily match the local destination port. 847 * 848 * Then ask the tunnel implementation to match the error against a valid 849 * association. 850 * 851 * Return an error if we can't find a match, the socket if we need further 852 * processing, zero otherwise. 853 */ 854 static struct sock *__udp4_lib_err_encap(struct net *net, 855 const struct iphdr *iph, 856 struct udphdr *uh, 857 struct sock *sk, 858 struct sk_buff *skb, u32 info) 859 { 860 int (*lookup)(struct sock *sk, struct sk_buff *skb); 861 int network_offset, transport_offset; 862 struct udp_sock *up; 863 864 network_offset = skb_network_offset(skb); 865 transport_offset = skb_transport_offset(skb); 866 867 /* Network header needs to point to the outer IPv4 header inside ICMP */ 868 skb_reset_network_header(skb); 869 870 /* Transport header needs to point to the UDP header */ 871 skb_set_transport_header(skb, iph->ihl << 2); 872 873 if (sk) { 874 up = udp_sk(sk); 875 876 lookup = READ_ONCE(up->encap_err_lookup); 877 if (lookup && lookup(sk, skb)) 878 sk = NULL; 879 880 goto out; 881 } 882 883 sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 884 iph->saddr, uh->dest, skb->dev->ifindex, 0, NULL); 885 if (sk) { 886 up = udp_sk(sk); 887 888 lookup = READ_ONCE(up->encap_err_lookup); 889 if (!lookup || lookup(sk, skb)) 890 sk = NULL; 891 } 892 893 out: 894 if (!sk) 895 sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 896 897 skb_set_transport_header(skb, transport_offset); 898 skb_set_network_header(skb, network_offset); 899 900 return sk; 901 } 902 903 static void udp_err_update_exception(struct net *net, struct sk_buff *skb, 904 int type, int code, u32 info) 905 { 906 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) 907 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_UDP); 908 else if (type == ICMP_REDIRECT) 909 ipv4_redirect(skb, net, 0, IPPROTO_UDP); 910 } 911 912 /* 913 * This routine is called by the ICMP module when it gets some 914 * sort of error condition. If err < 0 then the socket should 915 * be closed and the error returned to the user. If err > 0 916 * it's just the icmp type << 8 | icmp code. 917 * Header points to the ip header of the error packet. We move 918 * on past this. Then (as it used to claim before adjustment) 919 * header points to the first 8 bytes of the udp header. We need 920 * to find the appropriate port. 921 */ 922 int udp_err(struct sk_buff *skb, u32 info) 923 { 924 const struct iphdr *iph = (const struct iphdr *)skb->data; 925 const int type = icmp_hdr(skb)->type; 926 const int code = icmp_hdr(skb)->code; 927 struct net *net = dev_net(skb->dev); 928 struct inet_sock *inet; 929 bool tunnel = false; 930 struct udphdr *uh; 931 struct sock *sk; 932 int harderr; 933 int err; 934 935 udp_err_update_exception(net, skb, type, code, info); 936 937 uh = (struct udphdr *)(skb->data + (iph->ihl << 2)); 938 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 939 iph->saddr, uh->source, skb->dev->ifindex, 940 inet_sdif(skb), NULL); 941 942 if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) { 943 /* No socket for error: try tunnels before discarding */ 944 if (static_branch_unlikely(&udp_encap_needed_key)) { 945 sk = __udp4_lib_err_encap(net, iph, uh, sk, skb, info); 946 if (!sk) 947 return 0; 948 } else 949 sk = ERR_PTR(-ENOENT); 950 951 if (IS_ERR(sk)) { 952 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 953 return PTR_ERR(sk); 954 } 955 956 tunnel = true; 957 } 958 959 err = 0; 960 harderr = 0; 961 inet = inet_sk(sk); 962 963 switch (type) { 964 default: 965 case ICMP_TIME_EXCEEDED: 966 err = EHOSTUNREACH; 967 break; 968 case ICMP_SOURCE_QUENCH: 969 goto out; 970 case ICMP_PARAMETERPROB: 971 err = EPROTO; 972 harderr = 1; 973 break; 974 case ICMP_DEST_UNREACH: 975 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 976 ipv4_sk_update_pmtu(skb, sk, info); 977 if (READ_ONCE(inet->pmtudisc) != IP_PMTUDISC_DONT) { 978 err = EMSGSIZE; 979 harderr = 1; 980 break; 981 } 982 goto out; 983 } 984 err = EHOSTUNREACH; 985 if (code <= NR_ICMP_UNREACH) { 986 harderr = icmp_err_convert[code].fatal; 987 err = icmp_err_convert[code].errno; 988 } 989 break; 990 case ICMP_REDIRECT: 991 ipv4_sk_redirect(skb, sk); 992 goto out; 993 } 994 995 /* 996 * RFC1122: OK. Passes ICMP errors back to application, as per 997 * 4.1.3.3. 998 */ 999 if (tunnel) { 1000 /* ...not for tunnels though: we don't have a sending socket */ 1001 if (udp_sk(sk)->encap_err_rcv) 1002 udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest, info, 1003 (u8 *)(uh+1)); 1004 goto out; 1005 } 1006 if (!inet_test_bit(RECVERR, sk)) { 1007 if (!harderr || sk->sk_state != TCP_ESTABLISHED) 1008 goto out; 1009 } else 1010 ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 1011 1012 sk->sk_err = err; 1013 sk_error_report(sk); 1014 out: 1015 return 0; 1016 } 1017 1018 /* 1019 * Throw away all pending data and cancel the corking. Socket is locked. 1020 */ 1021 void udp_flush_pending_frames(struct sock *sk) 1022 { 1023 struct udp_sock *up = udp_sk(sk); 1024 1025 if (up->pending) { 1026 up->len = 0; 1027 WRITE_ONCE(up->pending, 0); 1028 ip_flush_pending_frames(sk); 1029 } 1030 } 1031 1032 /** 1033 * udp4_hwcsum - handle outgoing HW checksumming 1034 * @skb: sk_buff containing the filled-in UDP header 1035 * (checksum field must be zeroed out) 1036 * @src: source IP address 1037 * @dst: destination IP address 1038 */ 1039 void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 1040 { 1041 struct udphdr *uh = udp_hdr(skb); 1042 int offset = skb_transport_offset(skb); 1043 int len = skb->len - offset; 1044 int hlen = len; 1045 __wsum csum = 0; 1046 1047 if (!skb_has_frag_list(skb)) { 1048 /* 1049 * Only one fragment on the socket. 1050 */ 1051 skb->csum_start = skb_transport_header(skb) - skb->head; 1052 skb->csum_offset = offsetof(struct udphdr, check); 1053 uh->check = ~csum_tcpudp_magic(src, dst, len, 1054 IPPROTO_UDP, 0); 1055 } else { 1056 struct sk_buff *frags; 1057 1058 /* 1059 * HW-checksum won't work as there are two or more 1060 * fragments on the socket so that all csums of sk_buffs 1061 * should be together 1062 */ 1063 skb_walk_frags(skb, frags) { 1064 csum = csum_add(csum, frags->csum); 1065 hlen -= frags->len; 1066 } 1067 1068 csum = skb_checksum(skb, offset, hlen, csum); 1069 skb->ip_summed = CHECKSUM_NONE; 1070 1071 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 1072 if (uh->check == 0) 1073 uh->check = CSUM_MANGLED_0; 1074 } 1075 } 1076 EXPORT_SYMBOL_GPL(udp4_hwcsum); 1077 1078 /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 1079 * for the simple case like when setting the checksum for a UDP tunnel. 1080 */ 1081 void udp_set_csum(bool nocheck, struct sk_buff *skb, 1082 __be32 saddr, __be32 daddr, int len) 1083 { 1084 struct udphdr *uh = udp_hdr(skb); 1085 1086 if (nocheck) { 1087 uh->check = 0; 1088 } else if (skb_is_gso(skb)) { 1089 uh->check = ~udp_v4_check(len, saddr, daddr, 0); 1090 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 1091 uh->check = 0; 1092 uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 1093 if (uh->check == 0) 1094 uh->check = CSUM_MANGLED_0; 1095 } else { 1096 skb->ip_summed = CHECKSUM_PARTIAL; 1097 skb->csum_start = skb_transport_header(skb) - skb->head; 1098 skb->csum_offset = offsetof(struct udphdr, check); 1099 uh->check = ~udp_v4_check(len, saddr, daddr, 0); 1100 } 1101 } 1102 EXPORT_SYMBOL(udp_set_csum); 1103 1104 static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 1105 struct inet_cork *cork) 1106 { 1107 struct sock *sk = skb->sk; 1108 int offset, len, datalen; 1109 struct udphdr *uh; 1110 int err; 1111 1112 offset = skb_transport_offset(skb); 1113 len = skb->len - offset; 1114 datalen = len - sizeof(*uh); 1115 1116 /* 1117 * Create a UDP header 1118 */ 1119 uh = udp_hdr(skb); 1120 uh->source = inet_sk(sk)->inet_sport; 1121 uh->dest = fl4->fl4_dport; 1122 /* Datagram length checked in udp_sendmsg. */ 1123 udp_set_len_short(uh, len); 1124 uh->check = 0; 1125 1126 if (cork->gso_size) { 1127 const int hlen = skb_network_header_len(skb) + 1128 sizeof(struct udphdr); 1129 1130 if (hlen + min(datalen, cork->gso_size) > cork->fragsize) { 1131 kfree_skb(skb); 1132 return -EMSGSIZE; 1133 } 1134 if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 1135 kfree_skb(skb); 1136 return -EINVAL; 1137 } 1138 if (sk->sk_no_check_tx) { 1139 kfree_skb(skb); 1140 return -EINVAL; 1141 } 1142 if (dst_xfrm(skb_dst(skb))) { 1143 kfree_skb(skb); 1144 return -EIO; 1145 } 1146 1147 if (datalen > cork->gso_size) { 1148 skb_shinfo(skb)->gso_size = cork->gso_size; 1149 skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 1150 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 1151 cork->gso_size); 1152 1153 /* Don't checksum the payload, skb will get segmented */ 1154 goto csum_partial; 1155 } 1156 } 1157 1158 if (sk->sk_no_check_tx) { /* UDP csum off */ 1159 skb->ip_summed = CHECKSUM_NONE; 1160 goto send; 1161 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1162 csum_partial: 1163 udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 1164 goto send; 1165 } 1166 1167 /* add protocol-dependent pseudo-header */ 1168 uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 1169 IPPROTO_UDP, udp_csum(skb)); 1170 if (uh->check == 0) 1171 uh->check = CSUM_MANGLED_0; 1172 1173 send: 1174 err = ip_send_skb(sock_net(sk), skb); 1175 if (unlikely(err)) { 1176 if (err == -ENOBUFS && 1177 !inet_test_bit(RECVERR, sk)) { 1178 UDP_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS); 1179 err = 0; 1180 } 1181 } else { 1182 UDP_INC_STATS(sock_net(sk), UDP_MIB_OUTDATAGRAMS); 1183 } 1184 return err; 1185 } 1186 1187 /* 1188 * Push out all pending data as one UDP datagram. Socket is locked. 1189 */ 1190 int udp_push_pending_frames(struct sock *sk) 1191 { 1192 struct udp_sock *up = udp_sk(sk); 1193 struct inet_sock *inet = inet_sk(sk); 1194 struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 1195 struct sk_buff *skb; 1196 int err = 0; 1197 1198 skb = ip_finish_skb(sk, fl4); 1199 if (!skb) 1200 goto out; 1201 1202 err = udp_send_skb(skb, fl4, &inet->cork.base); 1203 1204 out: 1205 up->len = 0; 1206 WRITE_ONCE(up->pending, 0); 1207 return err; 1208 } 1209 1210 static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 1211 { 1212 switch (cmsg->cmsg_type) { 1213 case UDP_SEGMENT: 1214 if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 1215 return -EINVAL; 1216 *gso_size = *(__u16 *)CMSG_DATA(cmsg); 1217 return 0; 1218 default: 1219 return -EINVAL; 1220 } 1221 } 1222 1223 int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 1224 { 1225 struct cmsghdr *cmsg; 1226 bool need_ip = false; 1227 int err; 1228 1229 for_each_cmsghdr(cmsg, msg) { 1230 if (!CMSG_OK(msg, cmsg)) 1231 return -EINVAL; 1232 1233 if (cmsg->cmsg_level != SOL_UDP) { 1234 need_ip = true; 1235 continue; 1236 } 1237 1238 err = __udp_cmsg_send(cmsg, gso_size); 1239 if (err) 1240 return err; 1241 } 1242 1243 return need_ip; 1244 } 1245 1246 int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1247 { 1248 int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE; 1249 DEFINE_RAW_FLEX(struct ip_options_rcu, opt_copy, opt.__data, 1250 IP_OPTIONS_DATA_FIXED_SIZE); 1251 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 1252 int ulen = len, free = 0, connected = 0; 1253 struct inet_sock *inet = inet_sk(sk); 1254 struct udp_sock *up = udp_sk(sk); 1255 __be32 daddr, faddr, saddr; 1256 struct rtable *rt = NULL; 1257 struct flowi4 fl4_stack; 1258 struct ipcm_cookie ipc; 1259 struct sk_buff *skb; 1260 struct flowi4 *fl4; 1261 __be16 dport; 1262 int uc_index; 1263 u8 scope; 1264 int err; 1265 1266 if (len > 0xFFFF) 1267 return -EMSGSIZE; 1268 1269 /* 1270 * Check the flags. 1271 */ 1272 1273 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 1274 return -EOPNOTSUPP; 1275 1276 fl4 = &inet->cork.fl.u.ip4; 1277 if (READ_ONCE(up->pending)) { 1278 /* 1279 * There are pending frames. 1280 * The socket lock must be held while it's corked. 1281 */ 1282 lock_sock(sk); 1283 if (likely(up->pending)) { 1284 if (unlikely(up->pending != AF_INET)) { 1285 release_sock(sk); 1286 return -EINVAL; 1287 } 1288 goto do_append_data; 1289 } 1290 release_sock(sk); 1291 } 1292 ulen += sizeof(struct udphdr); 1293 1294 /* 1295 * Get and verify the address. 1296 */ 1297 if (usin) { 1298 if (msg->msg_namelen < sizeof(*usin)) 1299 return -EINVAL; 1300 if (usin->sin_family != AF_INET) { 1301 if (usin->sin_family != AF_UNSPEC) 1302 return -EAFNOSUPPORT; 1303 } 1304 1305 daddr = usin->sin_addr.s_addr; 1306 dport = usin->sin_port; 1307 if (dport == 0) 1308 return -EINVAL; 1309 } else { 1310 if (sk->sk_state != TCP_ESTABLISHED) 1311 return -EDESTADDRREQ; 1312 daddr = inet->inet_daddr; 1313 dport = inet->inet_dport; 1314 /* Open fast path for connected socket. 1315 Route will not be used, if at least one option is set. 1316 */ 1317 connected = 1; 1318 } 1319 1320 ipcm_init_sk(&ipc, inet); 1321 ipc.gso_size = READ_ONCE(up->gso_size); 1322 1323 if (msg->msg_controllen) { 1324 err = udp_cmsg_send(sk, msg, &ipc.gso_size); 1325 if (err > 0) { 1326 err = ip_cmsg_send(sk, msg, &ipc, 1327 sk->sk_family == AF_INET6); 1328 connected = 0; 1329 } 1330 if (unlikely(err < 0)) { 1331 kfree(ipc.opt); 1332 return err; 1333 } 1334 if (ipc.opt) 1335 free = 1; 1336 } 1337 if (!ipc.opt) { 1338 struct ip_options_rcu *inet_opt; 1339 1340 rcu_read_lock(); 1341 inet_opt = rcu_dereference(inet->inet_opt); 1342 if (inet_opt) { 1343 memcpy(opt_copy, inet_opt, 1344 sizeof(*inet_opt) + inet_opt->opt.optlen); 1345 ipc.opt = opt_copy; 1346 } 1347 rcu_read_unlock(); 1348 } 1349 1350 if (cgroup_bpf_enabled(CGROUP_UDP4_SENDMSG) && !connected) { 1351 err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 1352 (struct sockaddr *)usin, 1353 &msg->msg_namelen, 1354 &ipc.addr); 1355 if (err) 1356 goto out_free; 1357 if (usin) { 1358 if (usin->sin_port == 0) { 1359 /* BPF program set invalid port. Reject it. */ 1360 err = -EINVAL; 1361 goto out_free; 1362 } 1363 daddr = usin->sin_addr.s_addr; 1364 dport = usin->sin_port; 1365 } 1366 } 1367 1368 saddr = ipc.addr; 1369 ipc.addr = faddr = daddr; 1370 1371 if (ipc.opt && ipc.opt->opt.srr) { 1372 if (!daddr) { 1373 err = -EINVAL; 1374 goto out_free; 1375 } 1376 faddr = ipc.opt->opt.faddr; 1377 connected = 0; 1378 } 1379 scope = ip_sendmsg_scope(inet, &ipc, msg); 1380 if (scope == RT_SCOPE_LINK) 1381 connected = 0; 1382 1383 uc_index = READ_ONCE(inet->uc_index); 1384 if (ipv4_is_multicast(daddr)) { 1385 if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 1386 ipc.oif = READ_ONCE(inet->mc_index); 1387 if (!saddr) 1388 saddr = READ_ONCE(inet->mc_addr); 1389 connected = 0; 1390 } else if (!ipc.oif) { 1391 ipc.oif = uc_index; 1392 } else if (ipv4_is_lbcast(daddr) && uc_index) { 1393 /* oif is set, packet is to local broadcast and 1394 * uc_index is set. oif is most likely set 1395 * by sk_bound_dev_if. If uc_index != oif check if the 1396 * oif is an L3 master and uc_index is an L3 slave. 1397 * If so, we want to allow the send using the uc_index. 1398 */ 1399 if (ipc.oif != uc_index && 1400 ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 1401 uc_index)) { 1402 ipc.oif = uc_index; 1403 } 1404 } 1405 1406 if (connected) 1407 rt = dst_rtable(sk_dst_check(sk, 0)); 1408 1409 if (!rt) { 1410 struct net *net = sock_net(sk); 1411 __u8 flow_flags = inet_sk_flowi_flags(sk); 1412 1413 fl4 = &fl4_stack; 1414 1415 flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, 1416 ipc.tos & INET_DSCP_MASK, scope, 1417 IPPROTO_UDP, flow_flags, faddr, saddr, 1418 dport, inet->inet_sport, 1419 sk_uid(sk)); 1420 1421 security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 1422 rt = ip_route_output_flow(net, fl4, sk); 1423 if (IS_ERR(rt)) { 1424 err = PTR_ERR(rt); 1425 rt = NULL; 1426 if (err == -ENETUNREACH) 1427 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1428 goto out; 1429 } 1430 1431 err = -EACCES; 1432 if ((rt->rt_flags & RTCF_BROADCAST) && 1433 !sock_flag(sk, SOCK_BROADCAST)) 1434 goto out; 1435 if (connected) 1436 sk_dst_set(sk, dst_clone(&rt->dst)); 1437 } 1438 1439 if (msg->msg_flags&MSG_CONFIRM) 1440 goto do_confirm; 1441 back_from_confirm: 1442 1443 saddr = fl4->saddr; 1444 if (!ipc.addr) 1445 daddr = ipc.addr = fl4->daddr; 1446 1447 /* Lockless fast path for the non-corking case. */ 1448 if (!corkreq) { 1449 struct inet_cork cork; 1450 1451 skb = ip_make_skb(sk, fl4, ip_generic_getfrag, msg, ulen, 1452 sizeof(struct udphdr), &ipc, &rt, 1453 &cork, msg->msg_flags); 1454 err = PTR_ERR(skb); 1455 if (!IS_ERR_OR_NULL(skb)) 1456 err = udp_send_skb(skb, fl4, &cork); 1457 goto out; 1458 } 1459 1460 lock_sock(sk); 1461 if (unlikely(up->pending)) { 1462 /* The socket is already corked while preparing it. */ 1463 /* ... which is an evident application bug. --ANK */ 1464 release_sock(sk); 1465 1466 net_dbg_ratelimited("socket already corked\n"); 1467 err = -EINVAL; 1468 goto out; 1469 } 1470 /* 1471 * Now cork the socket to pend data. 1472 */ 1473 fl4 = &inet->cork.fl.u.ip4; 1474 fl4->daddr = daddr; 1475 fl4->saddr = saddr; 1476 fl4->fl4_dport = dport; 1477 fl4->fl4_sport = inet->inet_sport; 1478 WRITE_ONCE(up->pending, AF_INET); 1479 1480 do_append_data: 1481 up->len += ulen; 1482 err = ip_append_data(sk, fl4, ip_generic_getfrag, msg, ulen, 1483 sizeof(struct udphdr), &ipc, &rt, 1484 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1485 if (err) 1486 udp_flush_pending_frames(sk); 1487 else if (!corkreq) 1488 err = udp_push_pending_frames(sk); 1489 else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1490 WRITE_ONCE(up->pending, 0); 1491 release_sock(sk); 1492 1493 out: 1494 ip_rt_put(rt); 1495 out_free: 1496 if (free) 1497 kfree(ipc.opt); 1498 if (!err) 1499 return len; 1500 /* 1501 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1502 * ENOBUFS might not be good (it's not tunable per se), but otherwise 1503 * we don't have a good statistic (IpOutDiscards but it can be too many 1504 * things). We could add another new stat but at least for now that 1505 * seems like overkill. 1506 */ 1507 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) 1508 UDP_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS); 1509 1510 return err; 1511 1512 do_confirm: 1513 if (msg->msg_flags & MSG_PROBE) 1514 dst_confirm_neigh(&rt->dst, &fl4->daddr); 1515 if (!(msg->msg_flags&MSG_PROBE) || len) 1516 goto back_from_confirm; 1517 err = 0; 1518 goto out; 1519 } 1520 EXPORT_SYMBOL(udp_sendmsg); 1521 1522 void udp_splice_eof(struct socket *sock) 1523 { 1524 struct sock *sk = sock->sk; 1525 struct udp_sock *up = udp_sk(sk); 1526 1527 if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk)) 1528 return; 1529 1530 lock_sock(sk); 1531 if (up->pending && !udp_test_bit(CORK, sk)) 1532 udp_push_pending_frames(sk); 1533 release_sock(sk); 1534 } 1535 1536 #define UDP_SKB_IS_STATELESS 0x80000000 1537 1538 /* all head states (dst, sk, nf conntrack) except skb extensions are 1539 * cleared by udp_rcv(). 1540 * 1541 * We need to preserve secpath, if present, to eventually process 1542 * IP_CMSG_PASSSEC at recvmsg() time. 1543 * 1544 * Other extensions can be cleared. 1545 */ 1546 static bool udp_try_make_stateless(struct sk_buff *skb) 1547 { 1548 if (!skb_has_extensions(skb)) 1549 return true; 1550 1551 if (!secpath_exists(skb)) { 1552 skb_ext_reset(skb); 1553 return true; 1554 } 1555 1556 return false; 1557 } 1558 1559 static void udp_set_dev_scratch(struct sk_buff *skb) 1560 { 1561 struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1562 1563 BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1564 scratch->_tsize_state = skb->truesize; 1565 #if BITS_PER_LONG == 64 1566 scratch->len = skb->len; 1567 scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1568 scratch->is_linear = !skb_is_nonlinear(skb); 1569 #endif 1570 if (udp_try_make_stateless(skb)) 1571 scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1572 } 1573 1574 static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) 1575 { 1576 /* We come here after udp_lib_checksum_complete() returned 0. 1577 * This means that __skb_checksum_complete() might have 1578 * set skb->csum_valid to 1. 1579 * On 64bit platforms, we can set csum_unnecessary 1580 * to true, but only if the skb is not shared. 1581 */ 1582 #if BITS_PER_LONG == 64 1583 if (!skb_shared(skb)) 1584 udp_skb_scratch(skb)->csum_unnecessary = true; 1585 #endif 1586 } 1587 1588 static int udp_skb_truesize(struct sk_buff *skb) 1589 { 1590 return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1591 } 1592 1593 static bool udp_skb_has_head_state(struct sk_buff *skb) 1594 { 1595 return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1596 } 1597 1598 /* fully reclaim rmem/fwd memory allocated for skb */ 1599 static void udp_rmem_release(struct sock *sk, unsigned int size, 1600 int partial, bool rx_queue_lock_held) 1601 { 1602 struct udp_sock *up = udp_sk(sk); 1603 struct sk_buff_head *sk_queue; 1604 unsigned int amt; 1605 1606 if (likely(partial)) { 1607 up->forward_deficit += size; 1608 size = up->forward_deficit; 1609 if (size < READ_ONCE(up->forward_threshold) && 1610 !skb_queue_empty(&up->reader_queue)) 1611 return; 1612 } else { 1613 size += up->forward_deficit; 1614 } 1615 up->forward_deficit = 0; 1616 1617 /* acquire the sk_receive_queue for fwd allocated memory scheduling, 1618 * if the called don't held it already 1619 */ 1620 sk_queue = &sk->sk_receive_queue; 1621 if (!rx_queue_lock_held) 1622 spin_lock(&sk_queue->lock); 1623 1624 amt = (size + sk->sk_forward_alloc - partial) & ~(PAGE_SIZE - 1); 1625 sk_forward_alloc_add(sk, size - amt); 1626 1627 if (amt) 1628 __sk_mem_reduce_allocated(sk, amt >> PAGE_SHIFT); 1629 1630 atomic_sub(size, &sk->sk_rmem_alloc); 1631 1632 /* this can save us from acquiring the rx queue lock on next receive */ 1633 skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 1634 1635 if (!rx_queue_lock_held) 1636 spin_unlock(&sk_queue->lock); 1637 } 1638 1639 /* Note: called with reader_queue.lock held. 1640 * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1641 * This avoids a cache line miss while receive_queue lock is held. 1642 * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1643 */ 1644 void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1645 { 1646 prefetch(&skb->data); 1647 udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1648 } 1649 1650 /* as above, but the caller held the rx queue lock, too */ 1651 static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 1652 { 1653 prefetch(&skb->data); 1654 udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 1655 } 1656 1657 static int udp_rmem_schedule(struct sock *sk, int size) 1658 { 1659 int delta; 1660 1661 delta = size - sk->sk_forward_alloc; 1662 if (delta > 0 && !__sk_mem_schedule(sk, delta, SK_MEM_RECV)) 1663 return -ENOBUFS; 1664 1665 return 0; 1666 } 1667 1668 int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1669 { 1670 struct sk_buff_head *list = &sk->sk_receive_queue; 1671 struct udp_prod_queue *udp_prod_queue; 1672 struct sk_buff *next, *to_drop = NULL; 1673 struct llist_node *ll_list; 1674 unsigned int rmem, rcvbuf; 1675 int size, err = -ENOMEM; 1676 int total_size = 0; 1677 int q_size = 0; 1678 int dropcount; 1679 int nb = 0; 1680 1681 rmem = atomic_read(&sk->sk_rmem_alloc); 1682 rcvbuf = READ_ONCE(sk->sk_rcvbuf); 1683 size = skb->truesize; 1684 1685 udp_prod_queue = &udp_sk(sk)->udp_prod_queue[numa_node_id()]; 1686 1687 rmem += atomic_read(&udp_prod_queue->rmem_alloc); 1688 1689 /* Immediately drop when the receive queue is full. 1690 * Cast to unsigned int performs the boundary check for INT_MAX. 1691 */ 1692 if (rmem + size > rcvbuf) { 1693 if (rcvbuf > INT_MAX >> 1) 1694 goto drop; 1695 1696 /* Accept the packet if queue is empty. */ 1697 if (rmem) 1698 goto drop; 1699 } 1700 1701 /* Under mem pressure, it might be helpful to help udp_recvmsg() 1702 * having linear skbs : 1703 * - Reduce memory overhead and thus increase receive queue capacity 1704 * - Less cache line misses at copyout() time 1705 * - Less work at consume_skb() (less alien page frag freeing) 1706 */ 1707 if (rmem > (rcvbuf >> 1)) { 1708 skb_condense(skb); 1709 size = skb->truesize; 1710 } 1711 1712 udp_set_dev_scratch(skb); 1713 1714 atomic_add(size, &udp_prod_queue->rmem_alloc); 1715 1716 if (!llist_add(&skb->ll_node, &udp_prod_queue->ll_root)) 1717 return 0; 1718 1719 dropcount = sock_flag(sk, SOCK_RXQ_OVFL) ? sk_drops_read(sk) : 0; 1720 1721 spin_lock(&list->lock); 1722 1723 ll_list = llist_del_all(&udp_prod_queue->ll_root); 1724 1725 ll_list = llist_reverse_order(ll_list); 1726 1727 llist_for_each_entry_safe(skb, next, ll_list, ll_node) { 1728 size = udp_skb_truesize(skb); 1729 total_size += size; 1730 err = udp_rmem_schedule(sk, size); 1731 if (unlikely(err)) { 1732 /* Free the skbs outside of locked section. */ 1733 skb->next = to_drop; 1734 to_drop = skb; 1735 continue; 1736 } 1737 1738 q_size += size; 1739 sk_forward_alloc_add(sk, -size); 1740 1741 /* no need to setup a destructor, we will explicitly release the 1742 * forward allocated memory on dequeue 1743 */ 1744 SOCK_SKB_CB(skb)->dropcount = dropcount; 1745 nb++; 1746 __skb_queue_tail(list, skb); 1747 } 1748 1749 atomic_add(q_size, &sk->sk_rmem_alloc); 1750 1751 spin_unlock(&list->lock); 1752 1753 if (!sock_flag(sk, SOCK_DEAD)) { 1754 /* Multiple threads might be blocked in recvmsg(), 1755 * using prepare_to_wait_exclusive(). 1756 */ 1757 while (nb) { 1758 INDIRECT_CALL_1(READ_ONCE(sk->sk_data_ready), 1759 sock_def_readable, sk); 1760 nb--; 1761 } 1762 } 1763 1764 if (unlikely(to_drop)) { 1765 int err_ipv4 = 0; 1766 int err_ipv6 = 0; 1767 1768 for (nb = 0; to_drop != NULL; nb++) { 1769 skb = to_drop; 1770 if (skb->protocol == htons(ETH_P_IP)) 1771 err_ipv4++; 1772 else 1773 err_ipv6++; 1774 to_drop = skb->next; 1775 skb_mark_not_on_list(skb); 1776 sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_PROTO_MEM); 1777 } 1778 numa_drop_add(&udp_sk(sk)->drop_counters, nb); 1779 if (err_ipv4 > 0) { 1780 SNMP_ADD_STATS(__UDPX_MIB(sk, true), UDP_MIB_MEMERRORS, 1781 err_ipv4); 1782 SNMP_ADD_STATS(__UDPX_MIB(sk, true), UDP_MIB_INERRORS, 1783 err_ipv4); 1784 } 1785 if (err_ipv6 > 0) { 1786 SNMP_ADD_STATS(__UDPX_MIB(sk, false), UDP_MIB_MEMERRORS, 1787 err_ipv6); 1788 SNMP_ADD_STATS(__UDPX_MIB(sk, false), UDP_MIB_INERRORS, 1789 err_ipv6); 1790 } 1791 } 1792 1793 atomic_sub(total_size, &udp_prod_queue->rmem_alloc); 1794 1795 return 0; 1796 1797 drop: 1798 udp_drops_inc(sk); 1799 return err; 1800 } 1801 1802 void udp_destruct_common(struct sock *sk) 1803 { 1804 /* reclaim completely the forward allocated memory */ 1805 struct udp_sock *up = udp_sk(sk); 1806 unsigned int total = 0; 1807 struct sk_buff *skb; 1808 1809 skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 1810 while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 1811 total += skb->truesize; 1812 kfree_skb(skb); 1813 } 1814 udp_rmem_release(sk, total, 0, true); 1815 kfree(up->udp_prod_queue); 1816 } 1817 1818 static void udp_destruct_sock(struct sock *sk) 1819 { 1820 udp_destruct_common(sk); 1821 inet_sock_destruct(sk); 1822 } 1823 1824 static int udp_init_sock(struct sock *sk) 1825 { 1826 int res = udp_lib_init_sock(sk); 1827 1828 sk->sk_destruct = udp_destruct_sock; 1829 set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 1830 return res; 1831 } 1832 1833 void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1834 { 1835 if (unlikely(READ_ONCE(udp_sk(sk)->peeking_with_offset))) 1836 sk_peek_offset_bwd(sk, len); 1837 1838 if (!skb_shared(skb)) { 1839 skb_orphan(skb); 1840 skb_attempt_defer_free(skb); 1841 return; 1842 } 1843 1844 if (!skb_unref(skb)) 1845 return; 1846 1847 /* In the more common cases we cleared the head states previously, 1848 * see __udp_queue_rcv_skb(). 1849 */ 1850 if (unlikely(udp_skb_has_head_state(skb))) 1851 skb_release_head_state(skb); 1852 __consume_stateless_skb(skb); 1853 } 1854 1855 static struct sk_buff *__first_packet_length(struct sock *sk, 1856 struct sk_buff_head *rcvq, 1857 unsigned int *total) 1858 { 1859 struct sk_buff *skb; 1860 1861 while ((skb = skb_peek(rcvq)) != NULL) { 1862 if (udp_lib_checksum_complete(skb)) { 1863 struct net *net = sock_net(sk); 1864 1865 __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS); 1866 __UDP_INC_STATS(net, UDP_MIB_INERRORS); 1867 udp_drops_inc(sk); 1868 __skb_unlink(skb, rcvq); 1869 *total += skb->truesize; 1870 kfree_skb_reason(skb, SKB_DROP_REASON_UDP_CSUM); 1871 } else { 1872 udp_skb_csum_unnecessary_set(skb); 1873 break; 1874 } 1875 } 1876 return skb; 1877 } 1878 1879 /** 1880 * first_packet_length - return length of first packet in receive queue 1881 * @sk: socket 1882 * 1883 * Drops all bad checksum frames, until a valid one is found. 1884 * Returns the length of found skb, or -1 if none is found. 1885 */ 1886 static int first_packet_length(struct sock *sk) 1887 { 1888 struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 1889 struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 1890 unsigned int total = 0; 1891 struct sk_buff *skb; 1892 int res; 1893 1894 spin_lock_bh(&rcvq->lock); 1895 skb = __first_packet_length(sk, rcvq, &total); 1896 if (!skb && !skb_queue_empty_lockless(sk_queue)) { 1897 spin_lock(&sk_queue->lock); 1898 skb_queue_splice_tail_init(sk_queue, rcvq); 1899 spin_unlock(&sk_queue->lock); 1900 1901 skb = __first_packet_length(sk, rcvq, &total); 1902 } 1903 res = skb ? skb->len : -1; 1904 if (total) 1905 udp_rmem_release(sk, total, 1, false); 1906 spin_unlock_bh(&rcvq->lock); 1907 return res; 1908 } 1909 1910 /* 1911 * IOCTL requests applicable to the UDP protocol 1912 */ 1913 1914 int udp_ioctl(struct sock *sk, int cmd, int *karg) 1915 { 1916 switch (cmd) { 1917 case SIOCOUTQ: 1918 { 1919 *karg = sk_wmem_alloc_get(sk); 1920 return 0; 1921 } 1922 1923 case SIOCINQ: 1924 { 1925 *karg = max_t(int, 0, first_packet_length(sk)); 1926 return 0; 1927 } 1928 1929 default: 1930 return -ENOIOCTLCMD; 1931 } 1932 1933 return 0; 1934 } 1935 1936 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1937 int *off, int *err) 1938 { 1939 struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 1940 struct sk_buff_head *queue; 1941 struct sk_buff *last; 1942 long timeo; 1943 int error; 1944 1945 queue = &udp_sk(sk)->reader_queue; 1946 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 1947 do { 1948 struct sk_buff *skb; 1949 1950 error = sock_error(sk); 1951 if (error) 1952 break; 1953 1954 error = -EAGAIN; 1955 do { 1956 spin_lock_bh(&queue->lock); 1957 skb = __skb_try_recv_from_queue(queue, flags, off, err, 1958 &last); 1959 if (skb) { 1960 if (!(flags & MSG_PEEK)) 1961 udp_skb_destructor(sk, skb); 1962 spin_unlock_bh(&queue->lock); 1963 return skb; 1964 } 1965 1966 if (skb_queue_empty_lockless(sk_queue)) { 1967 spin_unlock_bh(&queue->lock); 1968 goto busy_check; 1969 } 1970 1971 /* refill the reader queue and walk it again 1972 * keep both queues locked to avoid re-acquiring 1973 * the sk_receive_queue lock if fwd memory scheduling 1974 * is needed. 1975 */ 1976 spin_lock(&sk_queue->lock); 1977 skb_queue_splice_tail_init(sk_queue, queue); 1978 1979 skb = __skb_try_recv_from_queue(queue, flags, off, err, 1980 &last); 1981 if (skb && !(flags & MSG_PEEK)) 1982 udp_skb_dtor_locked(sk, skb); 1983 spin_unlock(&sk_queue->lock); 1984 spin_unlock_bh(&queue->lock); 1985 if (skb) 1986 return skb; 1987 1988 busy_check: 1989 if (!sk_can_busy_loop(sk)) 1990 break; 1991 1992 sk_busy_loop(sk, flags & MSG_DONTWAIT); 1993 } while (!skb_queue_empty_lockless(sk_queue)); 1994 1995 /* sk_queue is empty, reader_queue may contain peeked packets */ 1996 } while (timeo && 1997 !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, 1998 &error, &timeo, 1999 (struct sk_buff *)sk_queue)); 2000 2001 *err = error; 2002 return NULL; 2003 } 2004 EXPORT_SYMBOL(__skb_recv_udp); 2005 2006 int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) 2007 { 2008 struct sk_buff *skb; 2009 int err; 2010 2011 try_again: 2012 skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 2013 if (!skb) 2014 return err; 2015 2016 if (udp_lib_checksum_complete(skb)) { 2017 struct net *net = sock_net(sk); 2018 2019 __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS); 2020 __UDP_INC_STATS(net, UDP_MIB_INERRORS); 2021 udp_drops_inc(sk); 2022 kfree_skb_reason(skb, SKB_DROP_REASON_UDP_CSUM); 2023 goto try_again; 2024 } 2025 2026 WARN_ON_ONCE(!skb_set_owner_sk_safe(skb, sk)); 2027 2028 /* 2029 * skb->dev still aliases the UDP rx dev_scratch (its charge was freed 2030 * on dequeue above); a sockmap verdict program may deref it via 2031 * bpf_sk_lookup_*(), so clear it -> bpf_skc_lookup() uses skb->sk 2032 */ 2033 skb->dev = NULL; 2034 2035 return recv_actor(sk, skb); 2036 } 2037 2038 /* 2039 * This should be easy, if there is something there we 2040 * return it, otherwise we block. 2041 */ 2042 2043 INDIRECT_CALLABLE_SCOPE 2044 int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags) 2045 { 2046 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 2047 int off, err, peeking = flags & MSG_PEEK; 2048 struct inet_sock *inet = inet_sk(sk); 2049 struct net *net = sock_net(sk); 2050 bool checksum_valid = false; 2051 unsigned int ulen, copied; 2052 struct sk_buff *skb; 2053 2054 if (flags & MSG_ERRQUEUE) 2055 return ip_recv_error(sk, msg, len); 2056 2057 try_again: 2058 off = sk_peek_offset(sk, flags); 2059 skb = __skb_recv_udp(sk, flags, &off, &err); 2060 if (!skb) 2061 return err; 2062 2063 ulen = udp_skb_len(skb); 2064 copied = len; 2065 if (copied > ulen - off) 2066 copied = ulen - off; 2067 else if (copied < ulen) 2068 msg->msg_flags |= MSG_TRUNC; 2069 2070 /* If checksum is needed at all, try to do it while copying the 2071 * data. If the data is truncated, do it before the copy. 2072 */ 2073 if (copied < ulen || peeking) { 2074 checksum_valid = udp_skb_csum_unnecessary(skb) || 2075 !__udp_lib_checksum_complete(skb); 2076 if (!checksum_valid) 2077 goto csum_copy_err; 2078 } 2079 2080 if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 2081 if (udp_skb_is_linear(skb)) 2082 err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 2083 else 2084 err = skb_copy_datagram_msg(skb, off, msg, copied); 2085 } else { 2086 err = skb_copy_and_csum_datagram_msg(skb, off, msg); 2087 2088 if (err == -EINVAL) 2089 goto csum_copy_err; 2090 } 2091 2092 if (unlikely(err)) { 2093 if (!peeking) { 2094 udp_drops_inc(sk); 2095 UDP_INC_STATS(net, UDP_MIB_INERRORS); 2096 } 2097 kfree_skb(skb); 2098 return err; 2099 } 2100 2101 if (!peeking) 2102 UDP_INC_STATS(net, UDP_MIB_INDATAGRAMS); 2103 2104 sock_recv_cmsgs(msg, sk, skb); 2105 2106 /* Copy the address. */ 2107 if (sin) { 2108 sin->sin_family = AF_INET; 2109 sin->sin_port = udp_hdr(skb)->source; 2110 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 2111 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 2112 msg->msg_namelen = sizeof(*sin); 2113 2114 BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, 2115 (struct sockaddr *)sin, 2116 &msg->msg_namelen); 2117 } 2118 2119 if (udp_test_bit(GRO_ENABLED, sk)) 2120 udp_cmsg_recv(msg, sk, skb); 2121 2122 if (inet_cmsg_flags(inet)) 2123 ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 2124 2125 err = copied; 2126 if (flags & MSG_TRUNC) 2127 err = ulen; 2128 2129 skb_consume_udp(sk, skb, peeking ? -err : err); 2130 return err; 2131 2132 csum_copy_err: 2133 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 2134 udp_skb_destructor)) { 2135 UDP_INC_STATS(net, UDP_MIB_CSUMERRORS); 2136 UDP_INC_STATS(net, UDP_MIB_INERRORS); 2137 } 2138 kfree_skb_reason(skb, SKB_DROP_REASON_UDP_CSUM); 2139 2140 /* starting over for a new packet, but check if we need to yield */ 2141 cond_resched(); 2142 msg->msg_flags &= ~MSG_TRUNC; 2143 goto try_again; 2144 } 2145 2146 int udp_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr, 2147 int addr_len) 2148 { 2149 /* This check is replicated from __ip4_datagram_connect() and 2150 * intended to prevent BPF program called below from accessing bytes 2151 * that are out of the bound specified by user in addr_len. 2152 */ 2153 if (addr_len < sizeof(struct sockaddr_in)) 2154 return -EINVAL; 2155 2156 return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr, &addr_len); 2157 } 2158 2159 static int udp_connect(struct sock *sk, struct sockaddr_unsized *uaddr, 2160 int addr_len) 2161 { 2162 int res; 2163 2164 lock_sock(sk); 2165 res = __ip4_datagram_connect(sk, uaddr, addr_len); 2166 if (!res) 2167 udp4_hash4(sk); 2168 release_sock(sk); 2169 return res; 2170 } 2171 2172 int __udp_disconnect(struct sock *sk, int flags) 2173 { 2174 struct inet_sock *inet = inet_sk(sk); 2175 /* 2176 * 1003.1g - break association. 2177 */ 2178 2179 sk->sk_state = TCP_CLOSE; 2180 WRITE_ONCE(inet->inet_daddr, 0); 2181 inet->inet_dport = 0; 2182 sock_rps_reset_rxhash(sk); 2183 WRITE_ONCE(sk->sk_bound_dev_if, 0); 2184 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { 2185 inet_reset_saddr(sk); 2186 if (sk->sk_prot->rehash && 2187 (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 2188 sk->sk_prot->rehash(sk); 2189 } 2190 2191 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 2192 sk->sk_prot->unhash(sk); 2193 inet->inet_sport = 0; 2194 } 2195 sk_dst_reset(sk); 2196 return 0; 2197 } 2198 EXPORT_SYMBOL(__udp_disconnect); 2199 2200 int udp_disconnect(struct sock *sk, int flags) 2201 { 2202 lock_sock(sk); 2203 __udp_disconnect(sk, flags); 2204 release_sock(sk); 2205 return 0; 2206 } 2207 2208 void udp_lib_unhash(struct sock *sk) 2209 { 2210 if (sk_hashed(sk)) { 2211 struct udp_hslot *hslot, *hslot2; 2212 struct net *net = sock_net(sk); 2213 struct udp_table *udptable; 2214 2215 sock_rps_delete_flow(sk); 2216 udptable = net->ipv4.udp_table; 2217 hslot = udp_hashslot(udptable, net, udp_sk(sk)->udp_port_hash); 2218 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 2219 2220 spin_lock_bh(&hslot->lock); 2221 if (rcu_access_pointer(sk->sk_reuseport_cb)) 2222 reuseport_detach_sock(sk); 2223 if (sk_del_node_init_rcu(sk)) { 2224 hslot->count--; 2225 inet_sk(sk)->inet_num = 0; 2226 sock_prot_inuse_add(net, sk->sk_prot, -1); 2227 2228 spin_lock(&hslot2->lock); 2229 hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2230 hslot2->count--; 2231 spin_unlock(&hslot2->lock); 2232 2233 udp_unhash4(udptable, sk); 2234 } 2235 spin_unlock_bh(&hslot->lock); 2236 } 2237 } 2238 2239 /* 2240 * inet_rcv_saddr was changed, we must rehash secondary hash 2241 */ 2242 void udp_lib_rehash(struct sock *sk, u16 newhash, u16 newhash4) 2243 { 2244 if (sk_hashed(sk)) { 2245 struct udp_hslot *hslot, *hslot2, *nhslot2; 2246 struct net *net = sock_net(sk); 2247 struct udp_table *udptable; 2248 2249 udptable = net->ipv4.udp_table; 2250 hslot = udp_hashslot(udptable, net, udp_sk(sk)->udp_port_hash); 2251 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 2252 nhslot2 = udp_hashslot2(udptable, newhash); 2253 2254 if (hslot2 != nhslot2 || 2255 rcu_access_pointer(sk->sk_reuseport_cb)) { 2256 /* we must lock primary chain too */ 2257 spin_lock_bh(&hslot->lock); 2258 if (rcu_access_pointer(sk->sk_reuseport_cb)) 2259 reuseport_detach_sock(sk); 2260 2261 if (hslot2 != nhslot2) { 2262 spin_lock(&hslot2->lock); 2263 hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2264 hslot2->count--; 2265 spin_unlock(&hslot2->lock); 2266 2267 spin_lock(&nhslot2->lock); 2268 hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 2269 &nhslot2->head); 2270 nhslot2->count++; 2271 spin_unlock(&nhslot2->lock); 2272 } 2273 2274 spin_unlock_bh(&hslot->lock); 2275 } 2276 2277 /* Now process hash4 if necessary: 2278 * (1) update hslot4; 2279 * (2) update hslot2->hash4_cnt. 2280 * Note that hslot2/hslot4 should be checked separately, as 2281 * either of them may change with the other unchanged. 2282 */ 2283 if (udp_hashed4(sk)) { 2284 spin_lock_bh(&hslot->lock); 2285 2286 if (inet_rcv_saddr_any(sk)) { 2287 udp_unhash4(udptable, sk); 2288 } else { 2289 udp_rehash4(udptable, sk, newhash4); 2290 if (hslot2 != nhslot2) { 2291 spin_lock(&hslot2->lock); 2292 udp_hash4_dec(hslot2); 2293 spin_unlock(&hslot2->lock); 2294 2295 spin_lock(&nhslot2->lock); 2296 udp_hash4_inc(nhslot2); 2297 spin_unlock(&nhslot2->lock); 2298 } 2299 } 2300 2301 spin_unlock_bh(&hslot->lock); 2302 } 2303 2304 udp_sk(sk)->udp_portaddr_hash = newhash; 2305 } 2306 } 2307 2308 static void udp_v4_rehash(struct sock *sk) 2309 { 2310 u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 2311 inet_sk(sk)->inet_rcv_saddr, 2312 inet_sk(sk)->inet_num); 2313 u16 new_hash4 = udp_ehashfn(sock_net(sk), 2314 sk->sk_rcv_saddr, sk->sk_num, 2315 sk->sk_daddr, sk->sk_dport); 2316 2317 udp_lib_rehash(sk, new_hash, new_hash4); 2318 } 2319 2320 static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2321 { 2322 int rc; 2323 2324 if (inet_sk(sk)->inet_daddr) { 2325 sock_rps_save_rxhash(sk, skb); 2326 sk_mark_napi_id(sk, skb); 2327 sk_incoming_cpu_update(sk); 2328 } else { 2329 sk_mark_napi_id_once(sk, skb); 2330 } 2331 2332 rc = __udp_enqueue_schedule_skb(sk, skb); 2333 if (rc < 0) { 2334 struct net *net = sock_net(sk); 2335 int drop_reason; 2336 2337 /* Note that an ENOMEM error is charged twice */ 2338 if (rc == -ENOMEM) { 2339 UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS); 2340 drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF; 2341 } else { 2342 UDP_INC_STATS(net, UDP_MIB_MEMERRORS); 2343 drop_reason = SKB_DROP_REASON_PROTO_MEM; 2344 } 2345 UDP_INC_STATS(net, UDP_MIB_INERRORS); 2346 trace_udp_fail_queue_rcv_skb(rc, sk, skb); 2347 sk_skb_reason_drop(sk, skb, drop_reason); 2348 return -1; 2349 } 2350 2351 return 0; 2352 } 2353 2354 /* returns: 2355 * -1: error 2356 * 0: success 2357 * >0: "udp encap" protocol resubmission 2358 * 2359 * Note that in the success and error cases, the skb is assumed to 2360 * have either been requeued or freed. 2361 */ 2362 static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 2363 { 2364 enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2365 struct udp_sock *up = udp_sk(sk); 2366 struct net *net = sock_net(sk); 2367 2368 /* 2369 * Charge it to the socket, dropping if the queue is full. 2370 */ 2371 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 2372 drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2373 goto drop; 2374 } 2375 nf_reset_ct(skb); 2376 2377 if (static_branch_unlikely(&udp_encap_needed_key) && 2378 READ_ONCE(up->encap_type)) { 2379 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 2380 2381 /* 2382 * This is an encapsulation socket so pass the skb to 2383 * the socket's udp_encap_rcv() hook. Otherwise, just 2384 * fall through and pass this up the UDP socket. 2385 * up->encap_rcv() returns the following value: 2386 * =0 if skb was successfully passed to the encap 2387 * handler or was discarded by it. 2388 * >0 if skb should be passed on to UDP. 2389 * <0 if skb should be resubmitted as proto -N 2390 */ 2391 2392 /* if we're overly short, let UDP handle it */ 2393 encap_rcv = READ_ONCE(up->encap_rcv); 2394 if (encap_rcv) { 2395 int ret; 2396 2397 /* Verify checksum before giving to encap */ 2398 if (udp_lib_checksum_complete(skb)) 2399 goto csum_error; 2400 2401 ret = encap_rcv(sk, skb); 2402 if (ret <= 0) { 2403 __UDP_INC_STATS(net, UDP_MIB_INDATAGRAMS); 2404 return -ret; 2405 } 2406 } 2407 2408 /* FALLTHROUGH -- it's a UDP Packet */ 2409 } 2410 2411 prefetch(&sk->sk_rmem_alloc); 2412 if (rcu_access_pointer(sk->sk_filter) && 2413 udp_lib_checksum_complete(skb)) 2414 goto csum_error; 2415 2416 drop_reason = sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)); 2417 if (drop_reason) 2418 goto drop; 2419 2420 udp_csum_pull_header(skb); 2421 2422 ipv4_pktinfo_prepare(sk, skb, true); 2423 return __udp_queue_rcv_skb(sk, skb); 2424 2425 csum_error: 2426 drop_reason = SKB_DROP_REASON_UDP_CSUM; 2427 __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS); 2428 drop: 2429 __UDP_INC_STATS(net, UDP_MIB_INERRORS); 2430 udp_drops_inc(sk); 2431 sk_skb_reason_drop(sk, skb, drop_reason); 2432 return -1; 2433 } 2434 2435 static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2436 { 2437 struct sk_buff *next, *segs; 2438 int ret; 2439 2440 if (likely(!udp_unexpected_gso(sk, skb))) 2441 return udp_queue_rcv_one_skb(sk, skb); 2442 2443 BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); 2444 __skb_push(skb, -skb_mac_offset(skb)); 2445 segs = udp_rcv_segment(sk, skb, true); 2446 skb_list_walk_safe(segs, skb, next) { 2447 __skb_pull(skb, skb_transport_offset(skb)); 2448 2449 udp_post_segment_fix_csum(skb); 2450 ret = udp_queue_rcv_one_skb(sk, skb); 2451 if (ret > 0) 2452 ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret); 2453 } 2454 return 0; 2455 } 2456 2457 /* For TCP sockets, sk_rx_dst is protected by socket lock 2458 * For UDP, we use xchg() to guard against concurrent changes. 2459 */ 2460 bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2461 { 2462 struct dst_entry *old; 2463 2464 if (dst_hold_safe(dst)) { 2465 old = unrcu_pointer(xchg(&sk->sk_rx_dst, RCU_INITIALIZER(dst))); 2466 dst_release(old); 2467 return old != dst; 2468 } 2469 return false; 2470 } 2471 2472 /* 2473 * Multicasts and broadcasts go to each listener. 2474 * 2475 * Note: called only from the BH handler context. 2476 */ 2477 static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2478 struct udphdr *uh, 2479 __be32 saddr, __be32 daddr) 2480 { 2481 struct udp_table *udptable = net->ipv4.udp_table; 2482 unsigned int hash2, hash2_any, offset; 2483 unsigned short hnum = ntohs(uh->dest); 2484 struct sock *sk, *first = NULL; 2485 int dif = skb->dev->ifindex; 2486 int sdif = inet_sdif(skb); 2487 struct hlist_node *node; 2488 struct udp_hslot *hslot; 2489 struct sk_buff *nskb; 2490 bool use_hash2; 2491 int ret; 2492 2493 hash2_any = 0; 2494 hash2 = 0; 2495 hslot = udp_hashslot(udptable, net, hnum); 2496 use_hash2 = hslot->count > 10; 2497 offset = offsetof(typeof(*sk), sk_node); 2498 2499 if (use_hash2) { 2500 hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 2501 udptable->mask; 2502 hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 2503 start_lookup: 2504 hslot = &udptable->hash2[hash2].hslot; 2505 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 2506 } 2507 2508 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2509 if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2510 uh->source, saddr, dif, sdif, hnum)) 2511 continue; 2512 2513 if (!first) { 2514 first = sk; 2515 continue; 2516 } 2517 nskb = skb_clone(skb, GFP_ATOMIC); 2518 2519 if (unlikely(!nskb)) { 2520 udp_drops_inc(sk); 2521 __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS); 2522 __UDP_INC_STATS(net, UDP_MIB_INERRORS); 2523 continue; 2524 } 2525 if (udp_queue_rcv_skb(sk, nskb) > 0) 2526 consume_skb(nskb); 2527 } 2528 2529 /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 2530 if (use_hash2 && hash2 != hash2_any) { 2531 hash2 = hash2_any; 2532 goto start_lookup; 2533 } 2534 2535 if (first) { 2536 ret = udp_queue_rcv_skb(first, skb); 2537 if (ret > 0) 2538 return -ret; 2539 } else { 2540 kfree_skb(skb); 2541 __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI); 2542 } 2543 return 0; 2544 } 2545 2546 /* Initialize UDP checksum. If exited with zero value (success), 2547 * CHECKSUM_UNNECESSARY means, that no more checks are required. 2548 * Otherwise, csum completion requires checksumming packet body, 2549 * including udp header and folding it to skb->csum. 2550 */ 2551 static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh) 2552 { 2553 int err; 2554 2555 /* Note, we are only interested in != 0 or == 0, thus the 2556 * force to int. 2557 */ 2558 err = (__force int)skb_checksum_init_zero_check(skb, IPPROTO_UDP, uh->check, 2559 inet_compute_pseudo); 2560 if (err) 2561 return err; 2562 2563 if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2564 /* If SW calculated the value, we know it's bad */ 2565 if (skb->csum_complete_sw) 2566 return 1; 2567 2568 /* HW says the value is bad. Let's validate that. 2569 * skb->csum is no longer the full packet checksum, 2570 * so don't treat it as such. 2571 */ 2572 skb_checksum_complete_unset(skb); 2573 } 2574 2575 return 0; 2576 } 2577 2578 /* wrapper for udp_queue_rcv_skb taking care of csum conversion and 2579 * return code conversion for ip layer consumption 2580 */ 2581 static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 2582 struct udphdr *uh) 2583 { 2584 int ret; 2585 2586 if (inet_get_convert_csum(sk) && uh->check) 2587 skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); 2588 2589 ret = udp_queue_rcv_skb(sk, skb); 2590 2591 /* a return value > 0 means to resubmit the input, but 2592 * it wants the return to be -protocol, or 0 2593 */ 2594 if (ret > 0) 2595 return -ret; 2596 return 0; 2597 } 2598 2599 /* 2600 * All we need to do is get the socket, and then do a checksum. 2601 */ 2602 2603 int udp_rcv(struct sk_buff *skb) 2604 { 2605 struct rtable *rt = skb_rtable(skb); 2606 struct net *net = dev_net(skb->dev); 2607 struct sock *sk = NULL; 2608 __be32 saddr, daddr; 2609 unsigned int ulen; 2610 struct udphdr *uh; 2611 bool refcounted; 2612 int drop_reason; 2613 2614 drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2615 2616 /* 2617 * Validate the packet. 2618 */ 2619 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2620 goto drop; /* No space for header. */ 2621 2622 uh = udp_hdr(skb); 2623 ulen = udp_get_len(skb, uh, 0); 2624 saddr = ip_hdr(skb)->saddr; 2625 daddr = ip_hdr(skb)->daddr; 2626 2627 if (ulen > skb->len) 2628 goto short_packet; 2629 2630 if (ulen < sizeof(*uh)) 2631 goto short_packet; 2632 2633 if (ulen < skb->len) { 2634 if (pskb_trim_rcsum(skb, ulen)) 2635 goto short_packet; 2636 2637 uh = udp_hdr(skb); 2638 } 2639 2640 if (udp4_csum_init(skb, uh)) 2641 goto csum_error; 2642 2643 sk = inet_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest, 2644 &refcounted, udp_ehashfn); 2645 if (IS_ERR(sk)) 2646 goto no_sk; 2647 2648 if (sk) { 2649 struct dst_entry *dst = skb_dst(skb); 2650 int ret; 2651 2652 if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 2653 udp_sk_rx_dst_set(sk, dst); 2654 2655 ret = udp_unicast_rcv_skb(sk, skb, uh); 2656 if (refcounted) 2657 sock_put(sk); 2658 return ret; 2659 } 2660 2661 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2662 return __udp4_lib_mcast_deliver(net, skb, uh, saddr, daddr); 2663 2664 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest); 2665 if (sk) 2666 return udp_unicast_rcv_skb(sk, skb, uh); 2667 no_sk: 2668 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2669 goto drop; 2670 nf_reset_ct(skb); 2671 2672 /* No socket. Drop packet silently, if checksum is wrong */ 2673 if (udp_lib_checksum_complete(skb)) 2674 goto csum_error; 2675 2676 drop_reason = SKB_DROP_REASON_NO_SOCKET; 2677 __UDP_INC_STATS(net, UDP_MIB_NOPORTS); 2678 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2679 2680 /* 2681 * Hmm. We got an UDP packet to a port to which we 2682 * don't wanna listen. Ignore it. 2683 */ 2684 sk_skb_reason_drop(sk, skb, drop_reason); 2685 return 0; 2686 2687 short_packet: 2688 drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL; 2689 net_dbg_ratelimited("UDP: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2690 &saddr, ntohs(uh->source), 2691 ulen, skb->len, 2692 &daddr, ntohs(uh->dest)); 2693 goto drop; 2694 2695 csum_error: 2696 /* 2697 * RFC1122: OK. Discards the bad packet silently (as far as 2698 * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2699 */ 2700 drop_reason = SKB_DROP_REASON_UDP_CSUM; 2701 net_dbg_ratelimited("UDP: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2702 &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2703 ulen); 2704 __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS); 2705 drop: 2706 __UDP_INC_STATS(net, UDP_MIB_INERRORS); 2707 sk_skb_reason_drop(sk, skb, drop_reason); 2708 return 0; 2709 } 2710 2711 /* We can only early demux multicast if there is a single matching socket. 2712 * If more than one socket found returns NULL 2713 */ 2714 static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2715 __be16 loc_port, __be32 loc_addr, 2716 __be16 rmt_port, __be32 rmt_addr, 2717 int dif, int sdif) 2718 { 2719 struct udp_table *udptable = net->ipv4.udp_table; 2720 unsigned short hnum = ntohs(loc_port); 2721 struct sock *sk, *result; 2722 struct udp_hslot *hslot; 2723 unsigned int slot; 2724 2725 slot = udp_hashfn(net, hnum, udptable->mask); 2726 hslot = &udptable->hash[slot]; 2727 2728 /* Do not bother scanning a too big list */ 2729 if (hslot->count > 10) 2730 return NULL; 2731 2732 result = NULL; 2733 sk_for_each_rcu(sk, &hslot->head) { 2734 if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2735 rmt_port, rmt_addr, dif, sdif, hnum)) { 2736 if (result) 2737 return NULL; 2738 result = sk; 2739 } 2740 } 2741 2742 return result; 2743 } 2744 2745 /* For unicast we should only early demux connected sockets or we can 2746 * break forwarding setups. The chains here can be long so only check 2747 * if the first socket is an exact match and if not move on. 2748 */ 2749 static struct sock *__udp4_lib_demux_lookup(struct net *net, 2750 __be16 loc_port, __be32 loc_addr, 2751 __be16 rmt_port, __be32 rmt_addr, 2752 int dif, int sdif) 2753 { 2754 struct udp_table *udptable = net->ipv4.udp_table; 2755 INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2756 unsigned short hnum = ntohs(loc_port); 2757 struct udp_hslot *hslot2; 2758 unsigned int hash2; 2759 __portpair ports; 2760 struct sock *sk; 2761 2762 hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2763 hslot2 = udp_hashslot2(udptable, hash2); 2764 ports = INET_COMBINED_PORTS(rmt_port, hnum); 2765 2766 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2767 if (inet_match(net, sk, acookie, ports, dif, sdif)) 2768 return sk; 2769 /* Only check first socket in chain */ 2770 break; 2771 } 2772 return NULL; 2773 } 2774 2775 enum skb_drop_reason udp_v4_early_demux(struct sk_buff *skb) 2776 { 2777 struct net *net = dev_net(skb->dev); 2778 struct in_device *in_dev = NULL; 2779 const struct iphdr *iph; 2780 const struct udphdr *uh; 2781 struct sock *sk = NULL; 2782 struct dst_entry *dst; 2783 int dif = skb->dev->ifindex; 2784 int sdif = inet_sdif(skb); 2785 int ours; 2786 2787 /* validate the packet */ 2788 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 2789 return SKB_NOT_DROPPED_YET; 2790 2791 iph = ip_hdr(skb); 2792 uh = udp_hdr(skb); 2793 2794 if (skb->pkt_type == PACKET_MULTICAST) { 2795 in_dev = __in_dev_get_rcu(skb->dev); 2796 2797 if (!in_dev) 2798 return SKB_NOT_DROPPED_YET; 2799 2800 ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 2801 iph->protocol); 2802 if (!ours) 2803 return SKB_NOT_DROPPED_YET; 2804 2805 sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2806 uh->source, iph->saddr, 2807 dif, sdif); 2808 } else if (skb->pkt_type == PACKET_HOST) { 2809 sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 2810 uh->source, iph->saddr, dif, sdif); 2811 } 2812 2813 if (!sk) 2814 return SKB_NOT_DROPPED_YET; 2815 2816 skb->sk = sk; 2817 DEBUG_NET_WARN_ON_ONCE(sk_is_refcounted(sk)); 2818 skb->destructor = sock_pfree; 2819 dst = rcu_dereference(sk->sk_rx_dst); 2820 2821 if (dst) 2822 dst = dst_check(dst, 0); 2823 if (dst) { 2824 u32 itag = 0; 2825 2826 /* set noref for now. 2827 * any place which wants to hold dst has to call 2828 * dst_hold_safe() 2829 */ 2830 skb_dst_set_noref(skb, dst); 2831 2832 /* for unconnected multicast sockets we need to validate 2833 * the source on each packet 2834 */ 2835 if (!inet_sk(sk)->inet_daddr && in_dev) 2836 return ip_mc_validate_source(skb, iph->daddr, 2837 iph->saddr, 2838 ip4h_dscp(iph), 2839 skb->dev, in_dev, &itag); 2840 } 2841 return SKB_NOT_DROPPED_YET; 2842 } 2843 2844 static void udp_destroy_sock(struct sock *sk) 2845 { 2846 struct udp_sock *up = udp_sk(sk); 2847 bool slow = lock_sock_fast(sk); 2848 2849 /* protects from races with udp_abort() */ 2850 sock_set_flag(sk, SOCK_DEAD); 2851 udp_flush_pending_frames(sk); 2852 unlock_sock_fast(sk, slow); 2853 if (static_branch_unlikely(&udp_encap_needed_key)) { 2854 if (up->encap_type) { 2855 void (*encap_destroy)(struct sock *sk); 2856 encap_destroy = READ_ONCE(up->encap_destroy); 2857 if (encap_destroy) 2858 encap_destroy(sk); 2859 } 2860 if (udp_test_bit(ENCAP_ENABLED, sk)) { 2861 static_branch_dec(&udp_encap_needed_key); 2862 udp_tunnel_cleanup_gro(sk); 2863 } 2864 } 2865 } 2866 2867 typedef struct sk_buff *(*udp_gro_receive_t)(struct sock *sk, 2868 struct list_head *head, 2869 struct sk_buff *skb); 2870 2871 static void set_xfrm_gro_udp_encap_rcv(__u16 encap_type, unsigned short family, 2872 struct sock *sk) 2873 { 2874 #ifdef CONFIG_XFRM 2875 udp_gro_receive_t new_gro_receive; 2876 2877 if (udp_test_bit(GRO_ENABLED, sk) && encap_type == UDP_ENCAP_ESPINUDP) { 2878 if (IS_ENABLED(CONFIG_IPV6) && family == AF_INET6) 2879 new_gro_receive = xfrm6_gro_udp_encap_rcv; 2880 else 2881 new_gro_receive = xfrm4_gro_udp_encap_rcv; 2882 2883 if (udp_sk(sk)->gro_receive != new_gro_receive) { 2884 /* 2885 * With IPV6_ADDRFORM the gro callback could change 2886 * after being set, unregister the old one, if valid. 2887 */ 2888 if (udp_sk(sk)->gro_receive) 2889 udp_tunnel_update_gro_rcv(sk, false); 2890 2891 WRITE_ONCE(udp_sk(sk)->gro_receive, new_gro_receive); 2892 udp_tunnel_update_gro_rcv(sk, true); 2893 } 2894 } 2895 #endif 2896 } 2897 2898 /* 2899 * Socket option code for UDP 2900 */ 2901 int udp_lib_setsockopt(struct sock *sk, int level, int optname, 2902 sockptr_t optval, unsigned int optlen, 2903 int (*push_pending_frames)(struct sock *)) 2904 { 2905 struct udp_sock *up = udp_sk(sk); 2906 int val, valbool; 2907 int err = 0; 2908 2909 if (level == SOL_SOCKET) { 2910 err = sk_setsockopt(sk, level, optname, optval, optlen); 2911 2912 if (optname == SO_RCVBUF || optname == SO_RCVBUFFORCE) { 2913 sockopt_lock_sock(sk); 2914 /* paired with READ_ONCE in udp_rmem_release() */ 2915 WRITE_ONCE(up->forward_threshold, sk->sk_rcvbuf >> 2); 2916 sockopt_release_sock(sk); 2917 } 2918 return err; 2919 } 2920 2921 if (optlen < sizeof(int)) 2922 return -EINVAL; 2923 2924 if (copy_from_sockptr(&val, optval, sizeof(val))) 2925 return -EFAULT; 2926 2927 valbool = val ? 1 : 0; 2928 2929 switch (optname) { 2930 case UDP_CORK: 2931 if (val != 0) { 2932 udp_set_bit(CORK, sk); 2933 } else { 2934 udp_clear_bit(CORK, sk); 2935 lock_sock(sk); 2936 push_pending_frames(sk); 2937 release_sock(sk); 2938 } 2939 break; 2940 2941 case UDP_ENCAP: 2942 sockopt_lock_sock(sk); 2943 switch (val) { 2944 case 0: 2945 #ifdef CONFIG_XFRM 2946 case UDP_ENCAP_ESPINUDP: 2947 set_xfrm_gro_udp_encap_rcv(val, sk->sk_family, sk); 2948 #if IS_ENABLED(CONFIG_IPV6) 2949 if (sk->sk_family == AF_INET6) 2950 WRITE_ONCE(up->encap_rcv, 2951 xfrm6_udp_encap_rcv); 2952 else 2953 #endif 2954 WRITE_ONCE(up->encap_rcv, 2955 xfrm4_udp_encap_rcv); 2956 #endif 2957 fallthrough; 2958 case UDP_ENCAP_L2TPINUDP: 2959 WRITE_ONCE(up->encap_type, val); 2960 udp_tunnel_encap_enable(sk); 2961 break; 2962 default: 2963 err = -ENOPROTOOPT; 2964 break; 2965 } 2966 sockopt_release_sock(sk); 2967 break; 2968 2969 case UDP_NO_CHECK6_TX: 2970 udp_set_no_check6_tx(sk, valbool); 2971 break; 2972 2973 case UDP_NO_CHECK6_RX: 2974 udp_set_no_check6_rx(sk, valbool); 2975 break; 2976 2977 case UDP_SEGMENT: 2978 if (val < 0 || val > USHRT_MAX) 2979 return -EINVAL; 2980 WRITE_ONCE(up->gso_size, val); 2981 break; 2982 2983 case UDP_GRO: 2984 sockopt_lock_sock(sk); 2985 /* when enabling GRO, accept the related GSO packet type */ 2986 if (valbool) 2987 udp_tunnel_encap_enable(sk); 2988 udp_assign_bit(GRO_ENABLED, sk, valbool); 2989 udp_assign_bit(ACCEPT_L4, sk, valbool); 2990 set_xfrm_gro_udp_encap_rcv(up->encap_type, sk->sk_family, sk); 2991 sockopt_release_sock(sk); 2992 break; 2993 2994 default: 2995 err = -ENOPROTOOPT; 2996 break; 2997 } 2998 2999 return err; 3000 } 3001 3002 static int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 3003 unsigned int optlen) 3004 { 3005 if (level == SOL_UDP || level == SOL_SOCKET) 3006 return udp_lib_setsockopt(sk, level, optname, 3007 optval, optlen, 3008 udp_push_pending_frames); 3009 return ip_setsockopt(sk, level, optname, optval, optlen); 3010 } 3011 3012 int udp_lib_getsockopt(struct sock *sk, int level, int optname, 3013 sockopt_t *opt) 3014 { 3015 struct udp_sock *up = udp_sk(sk); 3016 int val, len; 3017 3018 len = opt->optlen; 3019 /* keep the check so direct sockopt_t callers stay covered. */ 3020 if (len < 0) 3021 return -EINVAL; 3022 3023 len = min_t(unsigned int, len, sizeof(int)); 3024 3025 switch (optname) { 3026 case UDP_CORK: 3027 val = udp_test_bit(CORK, sk); 3028 break; 3029 3030 case UDP_ENCAP: 3031 val = READ_ONCE(up->encap_type); 3032 break; 3033 3034 case UDP_NO_CHECK6_TX: 3035 val = udp_get_no_check6_tx(sk); 3036 break; 3037 3038 case UDP_NO_CHECK6_RX: 3039 val = udp_get_no_check6_rx(sk); 3040 break; 3041 3042 case UDP_SEGMENT: 3043 val = READ_ONCE(up->gso_size); 3044 break; 3045 3046 case UDP_GRO: 3047 val = udp_test_bit(GRO_ENABLED, sk); 3048 break; 3049 3050 default: 3051 return -ENOPROTOOPT; 3052 } 3053 3054 opt->optlen = len; 3055 if (copy_to_iter(&val, len, &opt->iter_out) != len) 3056 return -EFAULT; 3057 return 0; 3058 } 3059 3060 static int udp_getsockopt(struct sock *sk, int level, int optname, 3061 char __user *optval, int __user *optlen) 3062 { 3063 sockopt_t opt; 3064 int err; 3065 3066 /* 3067 * keep the old __user pointers, until ip_getsockopt() moves 3068 * to sockopt_t 3069 */ 3070 if (level != SOL_UDP) 3071 return ip_getsockopt(sk, level, optname, optval, optlen); 3072 3073 err = sockopt_init_user(&opt, optval, optlen); 3074 if (err) 3075 return err; 3076 3077 err = udp_lib_getsockopt(sk, level, optname, &opt); 3078 if (err) 3079 return err; 3080 3081 /* optval was written by copy_to_iter() in udp_lib_getsockopt() */ 3082 if (put_user(opt.optlen, optlen)) 3083 return -EFAULT; 3084 3085 return 0; 3086 } 3087 3088 /** 3089 * udp_poll - wait for a UDP event. 3090 * @file: - file struct 3091 * @sock: - socket 3092 * @wait: - poll table 3093 * 3094 * This is same as datagram poll, except for the special case of 3095 * blocking sockets. If application is using a blocking fd 3096 * and a packet with checksum error is in the queue; 3097 * then it could get return from select indicating data available 3098 * but then block when reading it. Add special case code 3099 * to work around these arguably broken applications. 3100 */ 3101 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 3102 { 3103 __poll_t mask = datagram_poll(file, sock, wait); 3104 struct sock *sk = sock->sk; 3105 3106 if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) 3107 mask |= EPOLLIN | EPOLLRDNORM; 3108 3109 /* Check for false positives due to checksum errors */ 3110 if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 3111 !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 3112 mask &= ~(EPOLLIN | EPOLLRDNORM); 3113 3114 /* psock ingress_msg queue should not contain any bad checksum frames */ 3115 if (sk_is_readable(sk)) 3116 mask |= EPOLLIN | EPOLLRDNORM; 3117 return mask; 3118 3119 } 3120 3121 int udp_abort(struct sock *sk, int err) 3122 { 3123 if (!has_current_bpf_ctx()) 3124 lock_sock(sk); 3125 3126 /* udp{v6}_destroy_sock() sets it under the sk lock, avoid racing 3127 * with close() 3128 */ 3129 if (sock_flag(sk, SOCK_DEAD)) 3130 goto out; 3131 3132 sk->sk_err = err; 3133 sk_error_report(sk); 3134 __udp_disconnect(sk, 0); 3135 3136 out: 3137 if (!has_current_bpf_ctx()) 3138 release_sock(sk); 3139 3140 return 0; 3141 } 3142 3143 struct proto udp_prot = { 3144 .name = "UDP", 3145 .owner = THIS_MODULE, 3146 .close = udp_lib_close, 3147 .pre_connect = udp_pre_connect, 3148 .connect = udp_connect, 3149 .disconnect = udp_disconnect, 3150 .ioctl = udp_ioctl, 3151 .init = udp_init_sock, 3152 .destroy = udp_destroy_sock, 3153 .setsockopt = udp_setsockopt, 3154 .getsockopt = udp_getsockopt, 3155 .sendmsg = udp_sendmsg, 3156 .recvmsg = udp_recvmsg, 3157 .splice_eof = udp_splice_eof, 3158 .release_cb = ip4_datagram_release_cb, 3159 .hash = udp_lib_hash, 3160 .unhash = udp_lib_unhash, 3161 .rehash = udp_v4_rehash, 3162 .get_port = udp_v4_get_port, 3163 .put_port = udp_lib_unhash, 3164 #ifdef CONFIG_BPF_SYSCALL 3165 .psock_update_sk_prot = udp_bpf_update_proto, 3166 #endif 3167 .memory_allocated = &net_aligned_data.udp_memory_allocated, 3168 .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc, 3169 3170 .sysctl_mem = sysctl_udp_mem, 3171 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 3172 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 3173 .obj_size = sizeof(struct udp_sock), 3174 .diag_destroy = udp_abort, 3175 }; 3176 EXPORT_SYMBOL(udp_prot); 3177 3178 /* ------------------------------------------------------------------------ */ 3179 #ifdef CONFIG_PROC_FS 3180 3181 static unsigned short seq_file_family(const struct seq_file *seq); 3182 static bool seq_sk_match(struct seq_file *seq, const struct sock *sk) 3183 { 3184 unsigned short family = seq_file_family(seq); 3185 3186 /* AF_UNSPEC is used as a match all */ 3187 return ((family == AF_UNSPEC || family == sk->sk_family) && 3188 net_eq(sock_net(sk), seq_file_net(seq))); 3189 } 3190 3191 #ifdef CONFIG_BPF_SYSCALL 3192 static const struct seq_operations bpf_iter_udp_seq_ops; 3193 #endif 3194 3195 static struct sock *udp_get_first(struct seq_file *seq, int start) 3196 { 3197 struct udp_iter_state *state = seq->private; 3198 struct net *net = seq_file_net(seq); 3199 struct udp_table *udptable; 3200 struct sock *sk; 3201 3202 udptable = net->ipv4.udp_table; 3203 3204 for (state->bucket = start; state->bucket <= udptable->mask; 3205 ++state->bucket) { 3206 struct udp_hslot *hslot = &udptable->hash[state->bucket]; 3207 3208 if (hlist_empty(&hslot->head)) 3209 continue; 3210 3211 spin_lock_bh(&hslot->lock); 3212 sk_for_each(sk, &hslot->head) { 3213 if (seq_sk_match(seq, sk)) 3214 goto found; 3215 } 3216 spin_unlock_bh(&hslot->lock); 3217 } 3218 sk = NULL; 3219 found: 3220 return sk; 3221 } 3222 3223 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 3224 { 3225 struct udp_iter_state *state = seq->private; 3226 struct net *net = seq_file_net(seq); 3227 struct udp_table *udptable; 3228 3229 do { 3230 sk = sk_next(sk); 3231 } while (sk && !seq_sk_match(seq, sk)); 3232 3233 if (!sk) { 3234 udptable = net->ipv4.udp_table; 3235 3236 if (state->bucket <= udptable->mask) 3237 spin_unlock_bh(&udptable->hash[state->bucket].lock); 3238 3239 return udp_get_first(seq, state->bucket + 1); 3240 } 3241 return sk; 3242 } 3243 3244 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 3245 { 3246 struct sock *sk = udp_get_first(seq, 0); 3247 3248 if (sk) 3249 while (pos && (sk = udp_get_next(seq, sk)) != NULL) 3250 --pos; 3251 return pos ? NULL : sk; 3252 } 3253 3254 void *udp_seq_start(struct seq_file *seq, loff_t *pos) 3255 { 3256 struct udp_iter_state *state = seq->private; 3257 state->bucket = MAX_UDP_PORTS; 3258 3259 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 3260 } 3261 3262 void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3263 { 3264 struct sock *sk; 3265 3266 if (v == SEQ_START_TOKEN) 3267 sk = udp_get_idx(seq, 0); 3268 else 3269 sk = udp_get_next(seq, v); 3270 3271 ++*pos; 3272 return sk; 3273 } 3274 3275 void udp_seq_stop(struct seq_file *seq, void *v) 3276 { 3277 struct udp_iter_state *state = seq->private; 3278 struct udp_table *udptable; 3279 3280 udptable = seq_file_net(seq)->ipv4.udp_table; 3281 3282 if (state->bucket <= udptable->mask) 3283 spin_unlock_bh(&udptable->hash[state->bucket].lock); 3284 } 3285 3286 /* ------------------------------------------------------------------------ */ 3287 static void udp4_format_sock(struct sock *sp, struct seq_file *f, 3288 int bucket) 3289 { 3290 struct inet_sock *inet = inet_sk(sp); 3291 __be32 dest = inet->inet_daddr; 3292 __be32 src = inet->inet_rcv_saddr; 3293 __u16 destp = ntohs(inet->inet_dport); 3294 __u16 srcp = ntohs(inet->inet_sport); 3295 3296 seq_printf(f, "%5d: %08X:%04X %08X:%04X" 3297 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %llu %d %pK %u", 3298 bucket, src, srcp, dest, destp, sp->sk_state, 3299 sk_wmem_alloc_get(sp), 3300 udp_rqueue_get(sp), 3301 0, 0L, 0, 3302 from_kuid_munged(seq_user_ns(f), sk_uid(sp)), 3303 0, sock_i_ino(sp), 3304 refcount_read(&sp->sk_refcnt), sp, 3305 sk_drops_read(sp)); 3306 } 3307 3308 static int udp4_seq_show(struct seq_file *seq, void *v) 3309 { 3310 seq_setwidth(seq, 127); 3311 if (v == SEQ_START_TOKEN) 3312 seq_puts(seq, " sl local_address rem_address st tx_queue " 3313 "rx_queue tr tm->when retrnsmt uid timeout " 3314 "inode ref pointer drops"); 3315 else { 3316 struct udp_iter_state *state = seq->private; 3317 3318 udp4_format_sock(v, seq, state->bucket); 3319 } 3320 seq_pad(seq, '\n'); 3321 return 0; 3322 } 3323 3324 #ifdef CONFIG_BPF_SYSCALL 3325 struct bpf_iter__udp { 3326 __bpf_md_ptr(struct bpf_iter_meta *, meta); 3327 __bpf_md_ptr(struct udp_sock *, udp_sk); 3328 uid_t uid __aligned(8); 3329 int bucket __aligned(8); 3330 }; 3331 3332 union bpf_udp_iter_batch_item { 3333 struct sock *sk; 3334 __u64 cookie; 3335 }; 3336 3337 struct bpf_udp_iter_state { 3338 struct udp_iter_state state; 3339 unsigned int cur_sk; 3340 unsigned int end_sk; 3341 unsigned int max_sk; 3342 union bpf_udp_iter_batch_item *batch; 3343 }; 3344 3345 static int bpf_iter_udp_realloc_batch(struct bpf_udp_iter_state *iter, 3346 unsigned int new_batch_sz, gfp_t flags); 3347 static struct sock *bpf_iter_udp_resume(struct sock *first_sk, 3348 union bpf_udp_iter_batch_item *cookies, 3349 int n_cookies) 3350 { 3351 struct sock *sk = NULL; 3352 int i; 3353 3354 for (i = 0; i < n_cookies; i++) { 3355 sk = first_sk; 3356 udp_portaddr_for_each_entry_from(sk) 3357 if (cookies[i].cookie == atomic64_read(&sk->sk_cookie)) 3358 goto done; 3359 } 3360 done: 3361 return sk; 3362 } 3363 3364 static struct sock *bpf_iter_udp_batch(struct seq_file *seq) 3365 { 3366 struct bpf_udp_iter_state *iter = seq->private; 3367 struct udp_iter_state *state = &iter->state; 3368 unsigned int find_cookie, end_cookie; 3369 struct net *net = seq_file_net(seq); 3370 struct udp_table *udptable; 3371 unsigned int batch_sks = 0; 3372 int resume_bucket; 3373 int resizes = 0; 3374 struct sock *sk; 3375 int err = 0; 3376 3377 resume_bucket = state->bucket; 3378 3379 /* The current batch is done, so advance the bucket. */ 3380 if (iter->cur_sk == iter->end_sk) 3381 state->bucket++; 3382 3383 udptable = net->ipv4.udp_table; 3384 3385 again: 3386 /* New batch for the next bucket. 3387 * Iterate over the hash table to find a bucket with sockets matching 3388 * the iterator attributes, and return the first matching socket from 3389 * the bucket. The remaining matched sockets from the bucket are batched 3390 * before releasing the bucket lock. This allows BPF programs that are 3391 * called in seq_show to acquire the bucket lock if needed. 3392 */ 3393 find_cookie = iter->cur_sk; 3394 end_cookie = iter->end_sk; 3395 iter->cur_sk = 0; 3396 iter->end_sk = 0; 3397 batch_sks = 0; 3398 3399 for (; state->bucket <= udptable->mask; state->bucket++) { 3400 struct udp_hslot *hslot2 = &udptable->hash2[state->bucket].hslot; 3401 3402 if (hlist_empty(&hslot2->head)) 3403 goto next_bucket; 3404 3405 spin_lock_bh(&hslot2->lock); 3406 sk = hlist_entry_safe(hslot2->head.first, struct sock, 3407 __sk_common.skc_portaddr_node); 3408 /* Resume from the first (in iteration order) unseen socket from 3409 * the last batch that still exists in resume_bucket. Most of 3410 * the time this will just be where the last iteration left off 3411 * in resume_bucket unless that socket disappeared between 3412 * reads. 3413 */ 3414 if (state->bucket == resume_bucket) 3415 sk = bpf_iter_udp_resume(sk, &iter->batch[find_cookie], 3416 end_cookie - find_cookie); 3417 fill_batch: 3418 udp_portaddr_for_each_entry_from(sk) { 3419 if (seq_sk_match(seq, sk)) { 3420 if (iter->end_sk < iter->max_sk) { 3421 sock_hold(sk); 3422 iter->batch[iter->end_sk++].sk = sk; 3423 } 3424 batch_sks++; 3425 } 3426 } 3427 3428 /* Allocate a larger batch and try again. */ 3429 if (unlikely(resizes <= 1 && iter->end_sk && 3430 iter->end_sk != batch_sks)) { 3431 resizes++; 3432 3433 /* First, try with GFP_USER to maximize the chances of 3434 * grabbing more memory. 3435 */ 3436 if (resizes == 1) { 3437 spin_unlock_bh(&hslot2->lock); 3438 err = bpf_iter_udp_realloc_batch(iter, 3439 batch_sks * 3 / 2, 3440 GFP_USER); 3441 if (err) 3442 return ERR_PTR(err); 3443 /* Start over. */ 3444 goto again; 3445 } 3446 3447 /* Next, hold onto the lock, so the bucket doesn't 3448 * change while we get the rest of the sockets. 3449 */ 3450 err = bpf_iter_udp_realloc_batch(iter, batch_sks, 3451 GFP_NOWAIT); 3452 if (err) { 3453 spin_unlock_bh(&hslot2->lock); 3454 return ERR_PTR(err); 3455 } 3456 3457 /* Pick up where we left off. */ 3458 sk = iter->batch[iter->end_sk - 1].sk; 3459 sk = hlist_entry_safe(sk->__sk_common.skc_portaddr_node.next, 3460 struct sock, 3461 __sk_common.skc_portaddr_node); 3462 batch_sks = iter->end_sk; 3463 goto fill_batch; 3464 } 3465 3466 spin_unlock_bh(&hslot2->lock); 3467 3468 if (iter->end_sk) 3469 break; 3470 next_bucket: 3471 resizes = 0; 3472 } 3473 3474 WARN_ON_ONCE(iter->end_sk != batch_sks); 3475 return iter->end_sk ? iter->batch[0].sk : NULL; 3476 } 3477 3478 static void *bpf_iter_udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3479 { 3480 struct bpf_udp_iter_state *iter = seq->private; 3481 struct sock *sk; 3482 3483 /* Whenever seq_next() is called, the iter->cur_sk is 3484 * done with seq_show(), so unref the iter->cur_sk. 3485 */ 3486 if (iter->cur_sk < iter->end_sk) 3487 sock_put(iter->batch[iter->cur_sk++].sk); 3488 3489 /* After updating iter->cur_sk, check if there are more sockets 3490 * available in the current bucket batch. 3491 */ 3492 if (iter->cur_sk < iter->end_sk) 3493 sk = iter->batch[iter->cur_sk].sk; 3494 else 3495 /* Prepare a new batch. */ 3496 sk = bpf_iter_udp_batch(seq); 3497 3498 ++*pos; 3499 return sk; 3500 } 3501 3502 static void *bpf_iter_udp_seq_start(struct seq_file *seq, loff_t *pos) 3503 { 3504 /* bpf iter does not support lseek, so it always 3505 * continue from where it was stop()-ped. 3506 */ 3507 if (*pos) 3508 return bpf_iter_udp_batch(seq); 3509 3510 return SEQ_START_TOKEN; 3511 } 3512 3513 static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 3514 struct udp_sock *udp_sk, uid_t uid, int bucket) 3515 { 3516 struct bpf_iter__udp ctx; 3517 3518 meta->seq_num--; /* skip SEQ_START_TOKEN */ 3519 ctx.meta = meta; 3520 ctx.udp_sk = udp_sk; 3521 ctx.uid = uid; 3522 ctx.bucket = bucket; 3523 return bpf_iter_run_prog(prog, &ctx); 3524 } 3525 3526 static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) 3527 { 3528 struct udp_iter_state *state = seq->private; 3529 struct bpf_iter_meta meta; 3530 struct bpf_prog *prog; 3531 struct sock *sk = v; 3532 uid_t uid; 3533 int ret; 3534 3535 if (v == SEQ_START_TOKEN) 3536 return 0; 3537 3538 lock_sock(sk); 3539 3540 if (unlikely(sk_unhashed(sk))) { 3541 ret = SEQ_SKIP; 3542 goto unlock; 3543 } 3544 3545 uid = from_kuid_munged(seq_user_ns(seq), sk_uid(sk)); 3546 meta.seq = seq; 3547 prog = bpf_iter_get_info(&meta, false); 3548 ret = udp_prog_seq_show(prog, &meta, v, uid, state->bucket); 3549 3550 unlock: 3551 release_sock(sk); 3552 return ret; 3553 } 3554 3555 static void bpf_iter_udp_put_batch(struct bpf_udp_iter_state *iter) 3556 { 3557 union bpf_udp_iter_batch_item *item; 3558 unsigned int cur_sk = iter->cur_sk; 3559 __u64 cookie; 3560 3561 /* Remember the cookies of the sockets we haven't seen yet, so we can 3562 * pick up where we left off next time around. 3563 */ 3564 while (cur_sk < iter->end_sk) { 3565 item = &iter->batch[cur_sk++]; 3566 cookie = sock_gen_cookie(item->sk); 3567 sock_put(item->sk); 3568 item->cookie = cookie; 3569 } 3570 } 3571 3572 static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) 3573 { 3574 struct bpf_udp_iter_state *iter = seq->private; 3575 struct bpf_iter_meta meta; 3576 struct bpf_prog *prog; 3577 3578 if (!v) { 3579 meta.seq = seq; 3580 prog = bpf_iter_get_info(&meta, true); 3581 if (prog) 3582 (void)udp_prog_seq_show(prog, &meta, v, 0, 0); 3583 } 3584 3585 if (iter->cur_sk < iter->end_sk) 3586 bpf_iter_udp_put_batch(iter); 3587 } 3588 3589 static const struct seq_operations bpf_iter_udp_seq_ops = { 3590 .start = bpf_iter_udp_seq_start, 3591 .next = bpf_iter_udp_seq_next, 3592 .stop = bpf_iter_udp_seq_stop, 3593 .show = bpf_iter_udp_seq_show, 3594 }; 3595 #endif 3596 3597 static unsigned short seq_file_family(const struct seq_file *seq) 3598 { 3599 const struct udp_seq_afinfo *afinfo; 3600 3601 #ifdef CONFIG_BPF_SYSCALL 3602 /* BPF iterator: bpf programs to filter sockets. */ 3603 if (seq->op == &bpf_iter_udp_seq_ops) 3604 return AF_UNSPEC; 3605 #endif 3606 3607 /* Proc fs iterator */ 3608 afinfo = pde_data(file_inode(seq->file)); 3609 return afinfo->family; 3610 } 3611 3612 static const struct seq_operations udp_seq_ops = { 3613 .start = udp_seq_start, 3614 .next = udp_seq_next, 3615 .stop = udp_seq_stop, 3616 .show = udp4_seq_show, 3617 }; 3618 3619 static struct udp_seq_afinfo udp4_seq_afinfo = { 3620 .family = AF_INET, 3621 }; 3622 3623 static int __net_init udp4_proc_init_net(struct net *net) 3624 { 3625 if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 3626 sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 3627 return -ENOMEM; 3628 return 0; 3629 } 3630 3631 static void __net_exit udp4_proc_exit_net(struct net *net) 3632 { 3633 remove_proc_entry("udp", net->proc_net); 3634 } 3635 3636 static struct pernet_operations udp4_net_ops = { 3637 .init = udp4_proc_init_net, 3638 .exit = udp4_proc_exit_net, 3639 }; 3640 3641 int __init udp4_proc_init(void) 3642 { 3643 return register_pernet_subsys(&udp4_net_ops); 3644 } 3645 3646 void udp4_proc_exit(void) 3647 { 3648 unregister_pernet_subsys(&udp4_net_ops); 3649 } 3650 #endif /* CONFIG_PROC_FS */ 3651 3652 static __initdata unsigned long uhash_entries; 3653 static int __init set_uhash_entries(char *str) 3654 { 3655 ssize_t ret; 3656 3657 if (!str) 3658 return 0; 3659 3660 ret = kstrtoul(str, 0, &uhash_entries); 3661 if (ret) 3662 return 0; 3663 3664 if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 3665 uhash_entries = UDP_HTABLE_SIZE_MIN; 3666 return 1; 3667 } 3668 __setup("uhash_entries=", set_uhash_entries); 3669 3670 static void __init udp_table_init(struct udp_table *table, const char *name) 3671 { 3672 unsigned int i, slot_size; 3673 3674 slot_size = sizeof(struct udp_hslot) + sizeof(struct udp_hslot_main) + 3675 udp_hash4_slot_size(); 3676 table->hash = alloc_large_system_hash(name, 3677 slot_size, 3678 uhash_entries, 3679 21, /* one slot per 2 MB */ 3680 0, 3681 &table->log, 3682 &table->mask, 3683 UDP_HTABLE_SIZE_MIN, 3684 UDP_HTABLE_SIZE_MAX); 3685 3686 table->hash2 = (void *)(table->hash + (table->mask + 1)); 3687 for (i = 0; i <= table->mask; i++) { 3688 INIT_HLIST_HEAD(&table->hash[i].head); 3689 table->hash[i].count = 0; 3690 spin_lock_init(&table->hash[i].lock); 3691 } 3692 for (i = 0; i <= table->mask; i++) { 3693 INIT_HLIST_HEAD(&table->hash2[i].hslot.head); 3694 table->hash2[i].hslot.count = 0; 3695 spin_lock_init(&table->hash2[i].hslot.lock); 3696 } 3697 udp_table_hash4_init(table); 3698 } 3699 3700 u32 udp_flow_hashrnd(void) 3701 { 3702 static u32 hashrnd __read_mostly; 3703 3704 net_get_random_once(&hashrnd, sizeof(hashrnd)); 3705 3706 return hashrnd; 3707 } 3708 EXPORT_SYMBOL(udp_flow_hashrnd); 3709 3710 static void __net_init udp_sysctl_init(struct net *net) 3711 { 3712 net->ipv4.sysctl_udp_rmem_min = PAGE_SIZE; 3713 net->ipv4.sysctl_udp_wmem_min = PAGE_SIZE; 3714 3715 #ifdef CONFIG_NET_L3_MASTER_DEV 3716 net->ipv4.sysctl_udp_l3mdev_accept = 0; 3717 #endif 3718 } 3719 3720 static struct udp_table __net_init *udp_pernet_table_alloc(unsigned int hash_entries) 3721 { 3722 struct udp_table *udptable; 3723 unsigned int slot_size; 3724 int i; 3725 3726 udptable = kmalloc_obj(*udptable); 3727 if (!udptable) 3728 goto out; 3729 3730 slot_size = sizeof(struct udp_hslot) + sizeof(struct udp_hslot_main) + 3731 udp_hash4_slot_size(); 3732 udptable->hash = vmalloc_huge(hash_entries * slot_size, 3733 GFP_KERNEL_ACCOUNT); 3734 if (!udptable->hash) 3735 goto free_table; 3736 3737 udptable->hash2 = (void *)(udptable->hash + hash_entries); 3738 udptable->mask = hash_entries - 1; 3739 udptable->log = ilog2(hash_entries); 3740 3741 for (i = 0; i < hash_entries; i++) { 3742 INIT_HLIST_HEAD(&udptable->hash[i].head); 3743 udptable->hash[i].count = 0; 3744 spin_lock_init(&udptable->hash[i].lock); 3745 3746 INIT_HLIST_HEAD(&udptable->hash2[i].hslot.head); 3747 udptable->hash2[i].hslot.count = 0; 3748 spin_lock_init(&udptable->hash2[i].hslot.lock); 3749 } 3750 udp_table_hash4_init(udptable); 3751 3752 return udptable; 3753 3754 free_table: 3755 kfree(udptable); 3756 out: 3757 return NULL; 3758 } 3759 3760 static void __net_exit udp_pernet_table_free(struct net *net) 3761 { 3762 struct udp_table *udptable = net->ipv4.udp_table; 3763 3764 if (udptable == &udp_table) 3765 return; 3766 3767 kvfree(udptable->hash); 3768 kfree(udptable); 3769 } 3770 3771 static void __net_init udp_set_table(struct net *net) 3772 { 3773 struct udp_table *udptable; 3774 unsigned int hash_entries; 3775 struct net *old_net; 3776 3777 if (net_eq(net, &init_net)) 3778 goto fallback; 3779 3780 old_net = current->nsproxy->net_ns; 3781 hash_entries = READ_ONCE(old_net->ipv4.sysctl_udp_child_hash_entries); 3782 if (!hash_entries) 3783 goto fallback; 3784 3785 /* Set min to keep the bitmap on stack in udp_lib_get_port() */ 3786 if (hash_entries < UDP_HTABLE_SIZE_MIN_PERNET) 3787 hash_entries = UDP_HTABLE_SIZE_MIN_PERNET; 3788 else 3789 hash_entries = roundup_pow_of_two(hash_entries); 3790 3791 udptable = udp_pernet_table_alloc(hash_entries); 3792 if (udptable) { 3793 net->ipv4.udp_table = udptable; 3794 } else { 3795 pr_warn("Failed to allocate UDP hash table (entries: %u) " 3796 "for a netns, fallback to the global one\n", 3797 hash_entries); 3798 fallback: 3799 net->ipv4.udp_table = &udp_table; 3800 } 3801 } 3802 3803 static int __net_init udp_pernet_init(struct net *net) 3804 { 3805 #if IS_ENABLED(CONFIG_NET_UDP_TUNNEL) 3806 int i; 3807 3808 /* No tunnel is configured */ 3809 for (i = 0; i < ARRAY_SIZE(net->ipv4.udp_tunnel_gro); ++i) { 3810 INIT_HLIST_HEAD(&net->ipv4.udp_tunnel_gro[i].list); 3811 RCU_INIT_POINTER(net->ipv4.udp_tunnel_gro[i].sk, NULL); 3812 } 3813 #endif 3814 udp_sysctl_init(net); 3815 udp_set_table(net); 3816 3817 return 0; 3818 } 3819 3820 static void __net_exit udp_pernet_exit(struct net *net) 3821 { 3822 udp_pernet_table_free(net); 3823 } 3824 3825 static struct pernet_operations __net_initdata udp_sysctl_ops = { 3826 .init = udp_pernet_init, 3827 .exit = udp_pernet_exit, 3828 }; 3829 3830 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 3831 DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, 3832 struct udp_sock *udp_sk, uid_t uid, int bucket) 3833 3834 static int bpf_iter_udp_realloc_batch(struct bpf_udp_iter_state *iter, 3835 unsigned int new_batch_sz, gfp_t flags) 3836 { 3837 union bpf_udp_iter_batch_item *new_batch; 3838 3839 new_batch = kvmalloc_objs(*new_batch, new_batch_sz, 3840 flags | __GFP_NOWARN); 3841 if (!new_batch) 3842 return -ENOMEM; 3843 3844 if (flags != GFP_NOWAIT) 3845 bpf_iter_udp_put_batch(iter); 3846 3847 memcpy(new_batch, iter->batch, sizeof(*iter->batch) * iter->end_sk); 3848 kvfree(iter->batch); 3849 iter->batch = new_batch; 3850 iter->max_sk = new_batch_sz; 3851 3852 return 0; 3853 } 3854 3855 #define INIT_BATCH_SZ 16 3856 3857 static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux) 3858 { 3859 struct bpf_udp_iter_state *iter = priv_data; 3860 int ret; 3861 3862 ret = bpf_iter_init_seq_net(priv_data, aux); 3863 if (ret) 3864 return ret; 3865 3866 ret = bpf_iter_udp_realloc_batch(iter, INIT_BATCH_SZ, GFP_USER); 3867 if (ret) 3868 bpf_iter_fini_seq_net(priv_data); 3869 3870 iter->state.bucket = -1; 3871 3872 return ret; 3873 } 3874 3875 static void bpf_iter_fini_udp(void *priv_data) 3876 { 3877 struct bpf_udp_iter_state *iter = priv_data; 3878 3879 bpf_iter_fini_seq_net(priv_data); 3880 kvfree(iter->batch); 3881 } 3882 3883 static const struct bpf_iter_seq_info udp_seq_info = { 3884 .seq_ops = &bpf_iter_udp_seq_ops, 3885 .init_seq_private = bpf_iter_init_udp, 3886 .fini_seq_private = bpf_iter_fini_udp, 3887 .seq_priv_size = sizeof(struct bpf_udp_iter_state), 3888 }; 3889 3890 static struct bpf_iter_reg udp_reg_info = { 3891 .target = "udp", 3892 .ctx_arg_info_size = 1, 3893 .ctx_arg_info = { 3894 { offsetof(struct bpf_iter__udp, udp_sk), 3895 PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED }, 3896 }, 3897 .seq_info = &udp_seq_info, 3898 }; 3899 3900 static void __init bpf_iter_register(void) 3901 { 3902 udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP]; 3903 if (bpf_iter_reg_target(&udp_reg_info)) 3904 pr_warn("Warning: could not register bpf iterator udp\n"); 3905 } 3906 #endif 3907 3908 void __init udp_init(void) 3909 { 3910 unsigned long limit; 3911 3912 udp_table_init(&udp_table, "UDP"); 3913 limit = nr_free_buffer_pages() / 8; 3914 limit = max(limit, 128UL); 3915 sysctl_udp_mem[0] = limit / 4 * 3; 3916 sysctl_udp_mem[1] = limit; 3917 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 3918 3919 if (register_pernet_subsys(&udp_sysctl_ops)) 3920 panic("UDP: failed to init sysctl parameters.\n"); 3921 3922 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 3923 bpf_iter_register(); 3924 #endif 3925 } 3926