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