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/uaccess.h> 78 #include <asm/ioctls.h> 79 #include <linux/memblock.h> 80 #include <linux/highmem.h> 81 #include <linux/swap.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_tunnels.h> 104 #include <net/route.h> 105 #include <net/checksum.h> 106 #include <net/xfrm.h> 107 #include <trace/events/udp.h> 108 #include <linux/static_key.h> 109 #include <trace/events/skb.h> 110 #include <net/busy_poll.h> 111 #include "udp_impl.h" 112 #include <net/sock_reuseport.h> 113 #include <net/addrconf.h> 114 #include <net/udp_tunnel.h> 115 116 struct udp_table udp_table __read_mostly; 117 EXPORT_SYMBOL(udp_table); 118 119 long sysctl_udp_mem[3] __read_mostly; 120 EXPORT_SYMBOL(sysctl_udp_mem); 121 122 atomic_long_t udp_memory_allocated; 123 EXPORT_SYMBOL(udp_memory_allocated); 124 125 #define MAX_UDP_PORTS 65536 126 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 127 128 static int udp_lib_lport_inuse(struct net *net, __u16 num, 129 const struct udp_hslot *hslot, 130 unsigned long *bitmap, 131 struct sock *sk, unsigned int log) 132 { 133 struct sock *sk2; 134 kuid_t uid = sock_i_uid(sk); 135 136 sk_for_each(sk2, &hslot->head) { 137 if (net_eq(sock_net(sk2), net) && 138 sk2 != sk && 139 (bitmap || udp_sk(sk2)->udp_port_hash == num) && 140 (!sk2->sk_reuse || !sk->sk_reuse) && 141 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 142 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 143 inet_rcv_saddr_equal(sk, sk2, true)) { 144 if (sk2->sk_reuseport && sk->sk_reuseport && 145 !rcu_access_pointer(sk->sk_reuseport_cb) && 146 uid_eq(uid, sock_i_uid(sk2))) { 147 if (!bitmap) 148 return 0; 149 } else { 150 if (!bitmap) 151 return 1; 152 __set_bit(udp_sk(sk2)->udp_port_hash >> log, 153 bitmap); 154 } 155 } 156 } 157 return 0; 158 } 159 160 /* 161 * Note: we still hold spinlock of primary hash chain, so no other writer 162 * can insert/delete a socket with local_port == num 163 */ 164 static int udp_lib_lport_inuse2(struct net *net, __u16 num, 165 struct udp_hslot *hslot2, 166 struct sock *sk) 167 { 168 struct sock *sk2; 169 kuid_t uid = sock_i_uid(sk); 170 int res = 0; 171 172 spin_lock(&hslot2->lock); 173 udp_portaddr_for_each_entry(sk2, &hslot2->head) { 174 if (net_eq(sock_net(sk2), net) && 175 sk2 != sk && 176 (udp_sk(sk2)->udp_port_hash == num) && 177 (!sk2->sk_reuse || !sk->sk_reuse) && 178 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 179 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 180 inet_rcv_saddr_equal(sk, sk2, true)) { 181 if (sk2->sk_reuseport && sk->sk_reuseport && 182 !rcu_access_pointer(sk->sk_reuseport_cb) && 183 uid_eq(uid, sock_i_uid(sk2))) { 184 res = 0; 185 } else { 186 res = 1; 187 } 188 break; 189 } 190 } 191 spin_unlock(&hslot2->lock); 192 return res; 193 } 194 195 static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 196 { 197 struct net *net = sock_net(sk); 198 kuid_t uid = sock_i_uid(sk); 199 struct sock *sk2; 200 201 sk_for_each(sk2, &hslot->head) { 202 if (net_eq(sock_net(sk2), net) && 203 sk2 != sk && 204 sk2->sk_family == sk->sk_family && 205 ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 206 (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 207 (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 208 sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 209 inet_rcv_saddr_equal(sk, sk2, false)) { 210 return reuseport_add_sock(sk, sk2, 211 inet_rcv_saddr_any(sk)); 212 } 213 } 214 215 return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 216 } 217 218 /** 219 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 220 * 221 * @sk: socket struct in question 222 * @snum: port number to look up 223 * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 224 * with NULL address 225 */ 226 int udp_lib_get_port(struct sock *sk, unsigned short snum, 227 unsigned int hash2_nulladdr) 228 { 229 struct udp_hslot *hslot, *hslot2; 230 struct udp_table *udptable = sk->sk_prot->h.udp_table; 231 int error = 1; 232 struct net *net = sock_net(sk); 233 234 if (!snum) { 235 int low, high, remaining; 236 unsigned int rand; 237 unsigned short first, last; 238 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 239 240 inet_get_local_port_range(net, &low, &high); 241 remaining = (high - low) + 1; 242 243 rand = prandom_u32(); 244 first = reciprocal_scale(rand, remaining) + low; 245 /* 246 * force rand to be an odd multiple of UDP_HTABLE_SIZE 247 */ 248 rand = (rand | 1) * (udptable->mask + 1); 249 last = first + udptable->mask + 1; 250 do { 251 hslot = udp_hashslot(udptable, net, first); 252 bitmap_zero(bitmap, PORTS_PER_CHAIN); 253 spin_lock_bh(&hslot->lock); 254 udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 255 udptable->log); 256 257 snum = first; 258 /* 259 * Iterate on all possible values of snum for this hash. 260 * Using steps of an odd multiple of UDP_HTABLE_SIZE 261 * give us randomization and full range coverage. 262 */ 263 do { 264 if (low <= snum && snum <= high && 265 !test_bit(snum >> udptable->log, bitmap) && 266 !inet_is_local_reserved_port(net, snum)) 267 goto found; 268 snum += rand; 269 } while (snum != first); 270 spin_unlock_bh(&hslot->lock); 271 cond_resched(); 272 } while (++first != last); 273 goto fail; 274 } else { 275 hslot = udp_hashslot(udptable, net, snum); 276 spin_lock_bh(&hslot->lock); 277 if (hslot->count > 10) { 278 int exist; 279 unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 280 281 slot2 &= udptable->mask; 282 hash2_nulladdr &= udptable->mask; 283 284 hslot2 = udp_hashslot2(udptable, slot2); 285 if (hslot->count < hslot2->count) 286 goto scan_primary_hash; 287 288 exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 289 if (!exist && (hash2_nulladdr != slot2)) { 290 hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 291 exist = udp_lib_lport_inuse2(net, snum, hslot2, 292 sk); 293 } 294 if (exist) 295 goto fail_unlock; 296 else 297 goto found; 298 } 299 scan_primary_hash: 300 if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 301 goto fail_unlock; 302 } 303 found: 304 inet_sk(sk)->inet_num = snum; 305 udp_sk(sk)->udp_port_hash = snum; 306 udp_sk(sk)->udp_portaddr_hash ^= snum; 307 if (sk_unhashed(sk)) { 308 if (sk->sk_reuseport && 309 udp_reuseport_add_sock(sk, hslot)) { 310 inet_sk(sk)->inet_num = 0; 311 udp_sk(sk)->udp_port_hash = 0; 312 udp_sk(sk)->udp_portaddr_hash ^= snum; 313 goto fail_unlock; 314 } 315 316 sk_add_node_rcu(sk, &hslot->head); 317 hslot->count++; 318 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 319 320 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 321 spin_lock(&hslot2->lock); 322 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 323 sk->sk_family == AF_INET6) 324 hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 325 &hslot2->head); 326 else 327 hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 328 &hslot2->head); 329 hslot2->count++; 330 spin_unlock(&hslot2->lock); 331 } 332 sock_set_flag(sk, SOCK_RCU_FREE); 333 error = 0; 334 fail_unlock: 335 spin_unlock_bh(&hslot->lock); 336 fail: 337 return error; 338 } 339 EXPORT_SYMBOL(udp_lib_get_port); 340 341 int udp_v4_get_port(struct sock *sk, unsigned short snum) 342 { 343 unsigned int hash2_nulladdr = 344 ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 345 unsigned int hash2_partial = 346 ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 347 348 /* precompute partial secondary hash */ 349 udp_sk(sk)->udp_portaddr_hash = hash2_partial; 350 return udp_lib_get_port(sk, snum, hash2_nulladdr); 351 } 352 353 static int compute_score(struct sock *sk, struct net *net, 354 __be32 saddr, __be16 sport, 355 __be32 daddr, unsigned short hnum, 356 int dif, int sdif) 357 { 358 int score; 359 struct inet_sock *inet; 360 bool dev_match; 361 362 if (!net_eq(sock_net(sk), net) || 363 udp_sk(sk)->udp_port_hash != hnum || 364 ipv6_only_sock(sk)) 365 return -1; 366 367 if (sk->sk_rcv_saddr != daddr) 368 return -1; 369 370 score = (sk->sk_family == PF_INET) ? 2 : 1; 371 372 inet = inet_sk(sk); 373 if (inet->inet_daddr) { 374 if (inet->inet_daddr != saddr) 375 return -1; 376 score += 4; 377 } 378 379 if (inet->inet_dport) { 380 if (inet->inet_dport != sport) 381 return -1; 382 score += 4; 383 } 384 385 dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 386 dif, sdif); 387 if (!dev_match) 388 return -1; 389 score += 4; 390 391 if (sk->sk_incoming_cpu == raw_smp_processor_id()) 392 score++; 393 return score; 394 } 395 396 static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 397 const __u16 lport, const __be32 faddr, 398 const __be16 fport) 399 { 400 static u32 udp_ehash_secret __read_mostly; 401 402 net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 403 404 return __inet_ehashfn(laddr, lport, faddr, fport, 405 udp_ehash_secret + net_hash_mix(net)); 406 } 407 408 /* called with rcu_read_lock() */ 409 static struct sock *udp4_lib_lookup2(struct net *net, 410 __be32 saddr, __be16 sport, 411 __be32 daddr, unsigned int hnum, 412 int dif, int sdif, 413 struct udp_hslot *hslot2, 414 struct sk_buff *skb) 415 { 416 struct sock *sk, *result; 417 int score, badness; 418 u32 hash = 0; 419 420 result = NULL; 421 badness = 0; 422 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 423 score = compute_score(sk, net, saddr, sport, 424 daddr, hnum, dif, sdif); 425 if (score > badness) { 426 if (sk->sk_reuseport) { 427 hash = udp_ehashfn(net, daddr, hnum, 428 saddr, sport); 429 result = reuseport_select_sock(sk, hash, skb, 430 sizeof(struct udphdr)); 431 if (result) 432 return result; 433 } 434 badness = score; 435 result = sk; 436 } 437 } 438 return result; 439 } 440 441 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 442 * harder than this. -DaveM 443 */ 444 struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 445 __be16 sport, __be32 daddr, __be16 dport, int dif, 446 int sdif, struct udp_table *udptable, struct sk_buff *skb) 447 { 448 struct sock *result; 449 unsigned short hnum = ntohs(dport); 450 unsigned int hash2, slot2; 451 struct udp_hslot *hslot2; 452 453 hash2 = ipv4_portaddr_hash(net, daddr, hnum); 454 slot2 = hash2 & udptable->mask; 455 hslot2 = &udptable->hash2[slot2]; 456 457 result = udp4_lib_lookup2(net, saddr, sport, 458 daddr, hnum, dif, sdif, 459 hslot2, skb); 460 if (!result) { 461 hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 462 slot2 = hash2 & udptable->mask; 463 hslot2 = &udptable->hash2[slot2]; 464 465 result = udp4_lib_lookup2(net, saddr, sport, 466 htonl(INADDR_ANY), hnum, dif, sdif, 467 hslot2, skb); 468 } 469 if (IS_ERR(result)) 470 return NULL; 471 return result; 472 } 473 EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 474 475 static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 476 __be16 sport, __be16 dport, 477 struct udp_table *udptable) 478 { 479 const struct iphdr *iph = ip_hdr(skb); 480 481 return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 482 iph->daddr, dport, inet_iif(skb), 483 inet_sdif(skb), udptable, skb); 484 } 485 486 struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, 487 __be16 sport, __be16 dport) 488 { 489 return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table); 490 } 491 EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); 492 493 /* Must be called under rcu_read_lock(). 494 * Does increment socket refcount. 495 */ 496 #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 497 struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 498 __be32 daddr, __be16 dport, int dif) 499 { 500 struct sock *sk; 501 502 sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 503 dif, 0, &udp_table, NULL); 504 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 505 sk = NULL; 506 return sk; 507 } 508 EXPORT_SYMBOL_GPL(udp4_lib_lookup); 509 #endif 510 511 static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, 512 __be16 loc_port, __be32 loc_addr, 513 __be16 rmt_port, __be32 rmt_addr, 514 int dif, int sdif, unsigned short hnum) 515 { 516 struct inet_sock *inet = inet_sk(sk); 517 518 if (!net_eq(sock_net(sk), net) || 519 udp_sk(sk)->udp_port_hash != hnum || 520 (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 521 (inet->inet_dport != rmt_port && inet->inet_dport) || 522 (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 523 ipv6_only_sock(sk) || 524 !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 525 return false; 526 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 527 return false; 528 return true; 529 } 530 531 DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 532 void udp_encap_enable(void) 533 { 534 static_branch_inc(&udp_encap_needed_key); 535 } 536 EXPORT_SYMBOL(udp_encap_enable); 537 538 /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 539 * through error handlers in encapsulations looking for a match. 540 */ 541 static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 542 { 543 int i; 544 545 for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 546 int (*handler)(struct sk_buff *skb, u32 info); 547 const struct ip_tunnel_encap_ops *encap; 548 549 encap = rcu_dereference(iptun_encaps[i]); 550 if (!encap) 551 continue; 552 handler = encap->err_handler; 553 if (handler && !handler(skb, info)) 554 return 0; 555 } 556 557 return -ENOENT; 558 } 559 560 /* Try to match ICMP errors to UDP tunnels by looking up a socket without 561 * reversing source and destination port: this will match tunnels that force the 562 * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 563 * lwtunnels might actually break this assumption by being configured with 564 * different destination ports on endpoints, in this case we won't be able to 565 * trace ICMP messages back to them. 566 * 567 * If this doesn't match any socket, probe tunnels with arbitrary destination 568 * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 569 * we've sent packets to won't necessarily match the local destination port. 570 * 571 * Then ask the tunnel implementation to match the error against a valid 572 * association. 573 * 574 * Return an error if we can't find a match, the socket if we need further 575 * processing, zero otherwise. 576 */ 577 static struct sock *__udp4_lib_err_encap(struct net *net, 578 const struct iphdr *iph, 579 struct udphdr *uh, 580 struct udp_table *udptable, 581 struct sk_buff *skb, u32 info) 582 { 583 int network_offset, transport_offset; 584 struct sock *sk; 585 586 network_offset = skb_network_offset(skb); 587 transport_offset = skb_transport_offset(skb); 588 589 /* Network header needs to point to the outer IPv4 header inside ICMP */ 590 skb_reset_network_header(skb); 591 592 /* Transport header needs to point to the UDP header */ 593 skb_set_transport_header(skb, iph->ihl << 2); 594 595 sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 596 iph->saddr, uh->dest, skb->dev->ifindex, 0, 597 udptable, NULL); 598 if (sk) { 599 int (*lookup)(struct sock *sk, struct sk_buff *skb); 600 struct udp_sock *up = udp_sk(sk); 601 602 lookup = READ_ONCE(up->encap_err_lookup); 603 if (!lookup || lookup(sk, skb)) 604 sk = NULL; 605 } 606 607 if (!sk) 608 sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 609 610 skb_set_transport_header(skb, transport_offset); 611 skb_set_network_header(skb, network_offset); 612 613 return sk; 614 } 615 616 /* 617 * This routine is called by the ICMP module when it gets some 618 * sort of error condition. If err < 0 then the socket should 619 * be closed and the error returned to the user. If err > 0 620 * it's just the icmp type << 8 | icmp code. 621 * Header points to the ip header of the error packet. We move 622 * on past this. Then (as it used to claim before adjustment) 623 * header points to the first 8 bytes of the udp header. We need 624 * to find the appropriate port. 625 */ 626 627 int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 628 { 629 struct inet_sock *inet; 630 const struct iphdr *iph = (const struct iphdr *)skb->data; 631 struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 632 const int type = icmp_hdr(skb)->type; 633 const int code = icmp_hdr(skb)->code; 634 bool tunnel = false; 635 struct sock *sk; 636 int harderr; 637 int err; 638 struct net *net = dev_net(skb->dev); 639 640 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 641 iph->saddr, uh->source, skb->dev->ifindex, 642 inet_sdif(skb), udptable, NULL); 643 if (!sk) { 644 /* No socket for error: try tunnels before discarding */ 645 sk = ERR_PTR(-ENOENT); 646 if (static_branch_unlikely(&udp_encap_needed_key)) { 647 sk = __udp4_lib_err_encap(net, iph, uh, udptable, skb, 648 info); 649 if (!sk) 650 return 0; 651 } 652 653 if (IS_ERR(sk)) { 654 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 655 return PTR_ERR(sk); 656 } 657 658 tunnel = true; 659 } 660 661 err = 0; 662 harderr = 0; 663 inet = inet_sk(sk); 664 665 switch (type) { 666 default: 667 case ICMP_TIME_EXCEEDED: 668 err = EHOSTUNREACH; 669 break; 670 case ICMP_SOURCE_QUENCH: 671 goto out; 672 case ICMP_PARAMETERPROB: 673 err = EPROTO; 674 harderr = 1; 675 break; 676 case ICMP_DEST_UNREACH: 677 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 678 ipv4_sk_update_pmtu(skb, sk, info); 679 if (inet->pmtudisc != IP_PMTUDISC_DONT) { 680 err = EMSGSIZE; 681 harderr = 1; 682 break; 683 } 684 goto out; 685 } 686 err = EHOSTUNREACH; 687 if (code <= NR_ICMP_UNREACH) { 688 harderr = icmp_err_convert[code].fatal; 689 err = icmp_err_convert[code].errno; 690 } 691 break; 692 case ICMP_REDIRECT: 693 ipv4_sk_redirect(skb, sk); 694 goto out; 695 } 696 697 /* 698 * RFC1122: OK. Passes ICMP errors back to application, as per 699 * 4.1.3.3. 700 */ 701 if (tunnel) { 702 /* ...not for tunnels though: we don't have a sending socket */ 703 goto out; 704 } 705 if (!inet->recverr) { 706 if (!harderr || sk->sk_state != TCP_ESTABLISHED) 707 goto out; 708 } else 709 ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 710 711 sk->sk_err = err; 712 sk->sk_error_report(sk); 713 out: 714 return 0; 715 } 716 717 int udp_err(struct sk_buff *skb, u32 info) 718 { 719 return __udp4_lib_err(skb, info, &udp_table); 720 } 721 722 /* 723 * Throw away all pending data and cancel the corking. Socket is locked. 724 */ 725 void udp_flush_pending_frames(struct sock *sk) 726 { 727 struct udp_sock *up = udp_sk(sk); 728 729 if (up->pending) { 730 up->len = 0; 731 up->pending = 0; 732 ip_flush_pending_frames(sk); 733 } 734 } 735 EXPORT_SYMBOL(udp_flush_pending_frames); 736 737 /** 738 * udp4_hwcsum - handle outgoing HW checksumming 739 * @skb: sk_buff containing the filled-in UDP header 740 * (checksum field must be zeroed out) 741 * @src: source IP address 742 * @dst: destination IP address 743 */ 744 void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 745 { 746 struct udphdr *uh = udp_hdr(skb); 747 int offset = skb_transport_offset(skb); 748 int len = skb->len - offset; 749 int hlen = len; 750 __wsum csum = 0; 751 752 if (!skb_has_frag_list(skb)) { 753 /* 754 * Only one fragment on the socket. 755 */ 756 skb->csum_start = skb_transport_header(skb) - skb->head; 757 skb->csum_offset = offsetof(struct udphdr, check); 758 uh->check = ~csum_tcpudp_magic(src, dst, len, 759 IPPROTO_UDP, 0); 760 } else { 761 struct sk_buff *frags; 762 763 /* 764 * HW-checksum won't work as there are two or more 765 * fragments on the socket so that all csums of sk_buffs 766 * should be together 767 */ 768 skb_walk_frags(skb, frags) { 769 csum = csum_add(csum, frags->csum); 770 hlen -= frags->len; 771 } 772 773 csum = skb_checksum(skb, offset, hlen, csum); 774 skb->ip_summed = CHECKSUM_NONE; 775 776 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 777 if (uh->check == 0) 778 uh->check = CSUM_MANGLED_0; 779 } 780 } 781 EXPORT_SYMBOL_GPL(udp4_hwcsum); 782 783 /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 784 * for the simple case like when setting the checksum for a UDP tunnel. 785 */ 786 void udp_set_csum(bool nocheck, struct sk_buff *skb, 787 __be32 saddr, __be32 daddr, int len) 788 { 789 struct udphdr *uh = udp_hdr(skb); 790 791 if (nocheck) { 792 uh->check = 0; 793 } else if (skb_is_gso(skb)) { 794 uh->check = ~udp_v4_check(len, saddr, daddr, 0); 795 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 796 uh->check = 0; 797 uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 798 if (uh->check == 0) 799 uh->check = CSUM_MANGLED_0; 800 } else { 801 skb->ip_summed = CHECKSUM_PARTIAL; 802 skb->csum_start = skb_transport_header(skb) - skb->head; 803 skb->csum_offset = offsetof(struct udphdr, check); 804 uh->check = ~udp_v4_check(len, saddr, daddr, 0); 805 } 806 } 807 EXPORT_SYMBOL(udp_set_csum); 808 809 static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 810 struct inet_cork *cork) 811 { 812 struct sock *sk = skb->sk; 813 struct inet_sock *inet = inet_sk(sk); 814 struct udphdr *uh; 815 int err = 0; 816 int is_udplite = IS_UDPLITE(sk); 817 int offset = skb_transport_offset(skb); 818 int len = skb->len - offset; 819 __wsum csum = 0; 820 821 /* 822 * Create a UDP header 823 */ 824 uh = udp_hdr(skb); 825 uh->source = inet->inet_sport; 826 uh->dest = fl4->fl4_dport; 827 uh->len = htons(len); 828 uh->check = 0; 829 830 if (cork->gso_size) { 831 const int hlen = skb_network_header_len(skb) + 832 sizeof(struct udphdr); 833 834 if (hlen + cork->gso_size > cork->fragsize) { 835 kfree_skb(skb); 836 return -EINVAL; 837 } 838 if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) { 839 kfree_skb(skb); 840 return -EINVAL; 841 } 842 if (sk->sk_no_check_tx) { 843 kfree_skb(skb); 844 return -EINVAL; 845 } 846 if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 847 dst_xfrm(skb_dst(skb))) { 848 kfree_skb(skb); 849 return -EIO; 850 } 851 852 skb_shinfo(skb)->gso_size = cork->gso_size; 853 skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 854 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(len - sizeof(uh), 855 cork->gso_size); 856 goto csum_partial; 857 } 858 859 if (is_udplite) /* UDP-Lite */ 860 csum = udplite_csum(skb); 861 862 else if (sk->sk_no_check_tx) { /* UDP csum off */ 863 864 skb->ip_summed = CHECKSUM_NONE; 865 goto send; 866 867 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 868 csum_partial: 869 870 udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 871 goto send; 872 873 } else 874 csum = udp_csum(skb); 875 876 /* add protocol-dependent pseudo-header */ 877 uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 878 sk->sk_protocol, csum); 879 if (uh->check == 0) 880 uh->check = CSUM_MANGLED_0; 881 882 send: 883 err = ip_send_skb(sock_net(sk), skb); 884 if (err) { 885 if (err == -ENOBUFS && !inet->recverr) { 886 UDP_INC_STATS(sock_net(sk), 887 UDP_MIB_SNDBUFERRORS, is_udplite); 888 err = 0; 889 } 890 } else 891 UDP_INC_STATS(sock_net(sk), 892 UDP_MIB_OUTDATAGRAMS, is_udplite); 893 return err; 894 } 895 896 /* 897 * Push out all pending data as one UDP datagram. Socket is locked. 898 */ 899 int udp_push_pending_frames(struct sock *sk) 900 { 901 struct udp_sock *up = udp_sk(sk); 902 struct inet_sock *inet = inet_sk(sk); 903 struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 904 struct sk_buff *skb; 905 int err = 0; 906 907 skb = ip_finish_skb(sk, fl4); 908 if (!skb) 909 goto out; 910 911 err = udp_send_skb(skb, fl4, &inet->cork.base); 912 913 out: 914 up->len = 0; 915 up->pending = 0; 916 return err; 917 } 918 EXPORT_SYMBOL(udp_push_pending_frames); 919 920 static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 921 { 922 switch (cmsg->cmsg_type) { 923 case UDP_SEGMENT: 924 if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 925 return -EINVAL; 926 *gso_size = *(__u16 *)CMSG_DATA(cmsg); 927 return 0; 928 default: 929 return -EINVAL; 930 } 931 } 932 933 int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 934 { 935 struct cmsghdr *cmsg; 936 bool need_ip = false; 937 int err; 938 939 for_each_cmsghdr(cmsg, msg) { 940 if (!CMSG_OK(msg, cmsg)) 941 return -EINVAL; 942 943 if (cmsg->cmsg_level != SOL_UDP) { 944 need_ip = true; 945 continue; 946 } 947 948 err = __udp_cmsg_send(cmsg, gso_size); 949 if (err) 950 return err; 951 } 952 953 return need_ip; 954 } 955 EXPORT_SYMBOL_GPL(udp_cmsg_send); 956 957 int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 958 { 959 struct inet_sock *inet = inet_sk(sk); 960 struct udp_sock *up = udp_sk(sk); 961 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 962 struct flowi4 fl4_stack; 963 struct flowi4 *fl4; 964 int ulen = len; 965 struct ipcm_cookie ipc; 966 struct rtable *rt = NULL; 967 int free = 0; 968 int connected = 0; 969 __be32 daddr, faddr, saddr; 970 __be16 dport; 971 u8 tos; 972 int err, is_udplite = IS_UDPLITE(sk); 973 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 974 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 975 struct sk_buff *skb; 976 struct ip_options_data opt_copy; 977 978 if (len > 0xFFFF) 979 return -EMSGSIZE; 980 981 /* 982 * Check the flags. 983 */ 984 985 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 986 return -EOPNOTSUPP; 987 988 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 989 990 fl4 = &inet->cork.fl.u.ip4; 991 if (up->pending) { 992 /* 993 * There are pending frames. 994 * The socket lock must be held while it's corked. 995 */ 996 lock_sock(sk); 997 if (likely(up->pending)) { 998 if (unlikely(up->pending != AF_INET)) { 999 release_sock(sk); 1000 return -EINVAL; 1001 } 1002 goto do_append_data; 1003 } 1004 release_sock(sk); 1005 } 1006 ulen += sizeof(struct udphdr); 1007 1008 /* 1009 * Get and verify the address. 1010 */ 1011 if (usin) { 1012 if (msg->msg_namelen < sizeof(*usin)) 1013 return -EINVAL; 1014 if (usin->sin_family != AF_INET) { 1015 if (usin->sin_family != AF_UNSPEC) 1016 return -EAFNOSUPPORT; 1017 } 1018 1019 daddr = usin->sin_addr.s_addr; 1020 dport = usin->sin_port; 1021 if (dport == 0) 1022 return -EINVAL; 1023 } else { 1024 if (sk->sk_state != TCP_ESTABLISHED) 1025 return -EDESTADDRREQ; 1026 daddr = inet->inet_daddr; 1027 dport = inet->inet_dport; 1028 /* Open fast path for connected socket. 1029 Route will not be used, if at least one option is set. 1030 */ 1031 connected = 1; 1032 } 1033 1034 ipcm_init_sk(&ipc, inet); 1035 ipc.gso_size = up->gso_size; 1036 1037 if (msg->msg_controllen) { 1038 err = udp_cmsg_send(sk, msg, &ipc.gso_size); 1039 if (err > 0) 1040 err = ip_cmsg_send(sk, msg, &ipc, 1041 sk->sk_family == AF_INET6); 1042 if (unlikely(err < 0)) { 1043 kfree(ipc.opt); 1044 return err; 1045 } 1046 if (ipc.opt) 1047 free = 1; 1048 connected = 0; 1049 } 1050 if (!ipc.opt) { 1051 struct ip_options_rcu *inet_opt; 1052 1053 rcu_read_lock(); 1054 inet_opt = rcu_dereference(inet->inet_opt); 1055 if (inet_opt) { 1056 memcpy(&opt_copy, inet_opt, 1057 sizeof(*inet_opt) + inet_opt->opt.optlen); 1058 ipc.opt = &opt_copy.opt; 1059 } 1060 rcu_read_unlock(); 1061 } 1062 1063 if (cgroup_bpf_enabled && !connected) { 1064 err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 1065 (struct sockaddr *)usin, &ipc.addr); 1066 if (err) 1067 goto out_free; 1068 if (usin) { 1069 if (usin->sin_port == 0) { 1070 /* BPF program set invalid port. Reject it. */ 1071 err = -EINVAL; 1072 goto out_free; 1073 } 1074 daddr = usin->sin_addr.s_addr; 1075 dport = usin->sin_port; 1076 } 1077 } 1078 1079 saddr = ipc.addr; 1080 ipc.addr = faddr = daddr; 1081 1082 if (ipc.opt && ipc.opt->opt.srr) { 1083 if (!daddr) { 1084 err = -EINVAL; 1085 goto out_free; 1086 } 1087 faddr = ipc.opt->opt.faddr; 1088 connected = 0; 1089 } 1090 tos = get_rttos(&ipc, inet); 1091 if (sock_flag(sk, SOCK_LOCALROUTE) || 1092 (msg->msg_flags & MSG_DONTROUTE) || 1093 (ipc.opt && ipc.opt->opt.is_strictroute)) { 1094 tos |= RTO_ONLINK; 1095 connected = 0; 1096 } 1097 1098 if (ipv4_is_multicast(daddr)) { 1099 if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 1100 ipc.oif = inet->mc_index; 1101 if (!saddr) 1102 saddr = inet->mc_addr; 1103 connected = 0; 1104 } else if (!ipc.oif) { 1105 ipc.oif = inet->uc_index; 1106 } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 1107 /* oif is set, packet is to local broadcast and 1108 * and uc_index is set. oif is most likely set 1109 * by sk_bound_dev_if. If uc_index != oif check if the 1110 * oif is an L3 master and uc_index is an L3 slave. 1111 * If so, we want to allow the send using the uc_index. 1112 */ 1113 if (ipc.oif != inet->uc_index && 1114 ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 1115 inet->uc_index)) { 1116 ipc.oif = inet->uc_index; 1117 } 1118 } 1119 1120 if (connected) 1121 rt = (struct rtable *)sk_dst_check(sk, 0); 1122 1123 if (!rt) { 1124 struct net *net = sock_net(sk); 1125 __u8 flow_flags = inet_sk_flowi_flags(sk); 1126 1127 fl4 = &fl4_stack; 1128 1129 flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos, 1130 RT_SCOPE_UNIVERSE, sk->sk_protocol, 1131 flow_flags, 1132 faddr, saddr, dport, inet->inet_sport, 1133 sk->sk_uid); 1134 1135 security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 1136 rt = ip_route_output_flow(net, fl4, sk); 1137 if (IS_ERR(rt)) { 1138 err = PTR_ERR(rt); 1139 rt = NULL; 1140 if (err == -ENETUNREACH) 1141 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1142 goto out; 1143 } 1144 1145 err = -EACCES; 1146 if ((rt->rt_flags & RTCF_BROADCAST) && 1147 !sock_flag(sk, SOCK_BROADCAST)) 1148 goto out; 1149 if (connected) 1150 sk_dst_set(sk, dst_clone(&rt->dst)); 1151 } 1152 1153 if (msg->msg_flags&MSG_CONFIRM) 1154 goto do_confirm; 1155 back_from_confirm: 1156 1157 saddr = fl4->saddr; 1158 if (!ipc.addr) 1159 daddr = ipc.addr = fl4->daddr; 1160 1161 /* Lockless fast path for the non-corking case. */ 1162 if (!corkreq) { 1163 struct inet_cork cork; 1164 1165 skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1166 sizeof(struct udphdr), &ipc, &rt, 1167 &cork, msg->msg_flags); 1168 err = PTR_ERR(skb); 1169 if (!IS_ERR_OR_NULL(skb)) 1170 err = udp_send_skb(skb, fl4, &cork); 1171 goto out; 1172 } 1173 1174 lock_sock(sk); 1175 if (unlikely(up->pending)) { 1176 /* The socket is already corked while preparing it. */ 1177 /* ... which is an evident application bug. --ANK */ 1178 release_sock(sk); 1179 1180 net_dbg_ratelimited("socket already corked\n"); 1181 err = -EINVAL; 1182 goto out; 1183 } 1184 /* 1185 * Now cork the socket to pend data. 1186 */ 1187 fl4 = &inet->cork.fl.u.ip4; 1188 fl4->daddr = daddr; 1189 fl4->saddr = saddr; 1190 fl4->fl4_dport = dport; 1191 fl4->fl4_sport = inet->inet_sport; 1192 up->pending = AF_INET; 1193 1194 do_append_data: 1195 up->len += ulen; 1196 err = ip_append_data(sk, fl4, getfrag, msg, ulen, 1197 sizeof(struct udphdr), &ipc, &rt, 1198 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1199 if (err) 1200 udp_flush_pending_frames(sk); 1201 else if (!corkreq) 1202 err = udp_push_pending_frames(sk); 1203 else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1204 up->pending = 0; 1205 release_sock(sk); 1206 1207 out: 1208 ip_rt_put(rt); 1209 out_free: 1210 if (free) 1211 kfree(ipc.opt); 1212 if (!err) 1213 return len; 1214 /* 1215 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1216 * ENOBUFS might not be good (it's not tunable per se), but otherwise 1217 * we don't have a good statistic (IpOutDiscards but it can be too many 1218 * things). We could add another new stat but at least for now that 1219 * seems like overkill. 1220 */ 1221 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 1222 UDP_INC_STATS(sock_net(sk), 1223 UDP_MIB_SNDBUFERRORS, is_udplite); 1224 } 1225 return err; 1226 1227 do_confirm: 1228 if (msg->msg_flags & MSG_PROBE) 1229 dst_confirm_neigh(&rt->dst, &fl4->daddr); 1230 if (!(msg->msg_flags&MSG_PROBE) || len) 1231 goto back_from_confirm; 1232 err = 0; 1233 goto out; 1234 } 1235 EXPORT_SYMBOL(udp_sendmsg); 1236 1237 int udp_sendpage(struct sock *sk, struct page *page, int offset, 1238 size_t size, int flags) 1239 { 1240 struct inet_sock *inet = inet_sk(sk); 1241 struct udp_sock *up = udp_sk(sk); 1242 int ret; 1243 1244 if (flags & MSG_SENDPAGE_NOTLAST) 1245 flags |= MSG_MORE; 1246 1247 if (!up->pending) { 1248 struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1249 1250 /* Call udp_sendmsg to specify destination address which 1251 * sendpage interface can't pass. 1252 * This will succeed only when the socket is connected. 1253 */ 1254 ret = udp_sendmsg(sk, &msg, 0); 1255 if (ret < 0) 1256 return ret; 1257 } 1258 1259 lock_sock(sk); 1260 1261 if (unlikely(!up->pending)) { 1262 release_sock(sk); 1263 1264 net_dbg_ratelimited("cork failed\n"); 1265 return -EINVAL; 1266 } 1267 1268 ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1269 page, offset, size, flags); 1270 if (ret == -EOPNOTSUPP) { 1271 release_sock(sk); 1272 return sock_no_sendpage(sk->sk_socket, page, offset, 1273 size, flags); 1274 } 1275 if (ret < 0) { 1276 udp_flush_pending_frames(sk); 1277 goto out; 1278 } 1279 1280 up->len += size; 1281 if (!(up->corkflag || (flags&MSG_MORE))) 1282 ret = udp_push_pending_frames(sk); 1283 if (!ret) 1284 ret = size; 1285 out: 1286 release_sock(sk); 1287 return ret; 1288 } 1289 1290 #define UDP_SKB_IS_STATELESS 0x80000000 1291 1292 static void udp_set_dev_scratch(struct sk_buff *skb) 1293 { 1294 struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1295 1296 BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1297 scratch->_tsize_state = skb->truesize; 1298 #if BITS_PER_LONG == 64 1299 scratch->len = skb->len; 1300 scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1301 scratch->is_linear = !skb_is_nonlinear(skb); 1302 #endif 1303 /* all head states execept sp (dst, sk, nf) are always cleared by 1304 * udp_rcv() and we need to preserve secpath, if present, to eventually 1305 * process IP_CMSG_PASSSEC at recvmsg() time 1306 */ 1307 if (likely(!skb_sec_path(skb))) 1308 scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1309 } 1310 1311 static int udp_skb_truesize(struct sk_buff *skb) 1312 { 1313 return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1314 } 1315 1316 static bool udp_skb_has_head_state(struct sk_buff *skb) 1317 { 1318 return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1319 } 1320 1321 /* fully reclaim rmem/fwd memory allocated for skb */ 1322 static void udp_rmem_release(struct sock *sk, int size, int partial, 1323 bool rx_queue_lock_held) 1324 { 1325 struct udp_sock *up = udp_sk(sk); 1326 struct sk_buff_head *sk_queue; 1327 int amt; 1328 1329 if (likely(partial)) { 1330 up->forward_deficit += size; 1331 size = up->forward_deficit; 1332 if (size < (sk->sk_rcvbuf >> 2)) 1333 return; 1334 } else { 1335 size += up->forward_deficit; 1336 } 1337 up->forward_deficit = 0; 1338 1339 /* acquire the sk_receive_queue for fwd allocated memory scheduling, 1340 * if the called don't held it already 1341 */ 1342 sk_queue = &sk->sk_receive_queue; 1343 if (!rx_queue_lock_held) 1344 spin_lock(&sk_queue->lock); 1345 1346 1347 sk->sk_forward_alloc += size; 1348 amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); 1349 sk->sk_forward_alloc -= amt; 1350 1351 if (amt) 1352 __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); 1353 1354 atomic_sub(size, &sk->sk_rmem_alloc); 1355 1356 /* this can save us from acquiring the rx queue lock on next receive */ 1357 skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 1358 1359 if (!rx_queue_lock_held) 1360 spin_unlock(&sk_queue->lock); 1361 } 1362 1363 /* Note: called with reader_queue.lock held. 1364 * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1365 * This avoids a cache line miss while receive_queue lock is held. 1366 * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1367 */ 1368 void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1369 { 1370 prefetch(&skb->data); 1371 udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1372 } 1373 EXPORT_SYMBOL(udp_skb_destructor); 1374 1375 /* as above, but the caller held the rx queue lock, too */ 1376 static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 1377 { 1378 prefetch(&skb->data); 1379 udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 1380 } 1381 1382 /* Idea of busylocks is to let producers grab an extra spinlock 1383 * to relieve pressure on the receive_queue spinlock shared by consumer. 1384 * Under flood, this means that only one producer can be in line 1385 * trying to acquire the receive_queue spinlock. 1386 * These busylock can be allocated on a per cpu manner, instead of a 1387 * per socket one (that would consume a cache line per socket) 1388 */ 1389 static int udp_busylocks_log __read_mostly; 1390 static spinlock_t *udp_busylocks __read_mostly; 1391 1392 static spinlock_t *busylock_acquire(void *ptr) 1393 { 1394 spinlock_t *busy; 1395 1396 busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 1397 spin_lock(busy); 1398 return busy; 1399 } 1400 1401 static void busylock_release(spinlock_t *busy) 1402 { 1403 if (busy) 1404 spin_unlock(busy); 1405 } 1406 1407 int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1408 { 1409 struct sk_buff_head *list = &sk->sk_receive_queue; 1410 int rmem, delta, amt, err = -ENOMEM; 1411 spinlock_t *busy = NULL; 1412 int size; 1413 1414 /* try to avoid the costly atomic add/sub pair when the receive 1415 * queue is full; always allow at least a packet 1416 */ 1417 rmem = atomic_read(&sk->sk_rmem_alloc); 1418 if (rmem > sk->sk_rcvbuf) 1419 goto drop; 1420 1421 /* Under mem pressure, it might be helpful to help udp_recvmsg() 1422 * having linear skbs : 1423 * - Reduce memory overhead and thus increase receive queue capacity 1424 * - Less cache line misses at copyout() time 1425 * - Less work at consume_skb() (less alien page frag freeing) 1426 */ 1427 if (rmem > (sk->sk_rcvbuf >> 1)) { 1428 skb_condense(skb); 1429 1430 busy = busylock_acquire(sk); 1431 } 1432 size = skb->truesize; 1433 udp_set_dev_scratch(skb); 1434 1435 /* we drop only if the receive buf is full and the receive 1436 * queue contains some other skb 1437 */ 1438 rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1439 if (rmem > (size + sk->sk_rcvbuf)) 1440 goto uncharge_drop; 1441 1442 spin_lock(&list->lock); 1443 if (size >= sk->sk_forward_alloc) { 1444 amt = sk_mem_pages(size); 1445 delta = amt << SK_MEM_QUANTUM_SHIFT; 1446 if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1447 err = -ENOBUFS; 1448 spin_unlock(&list->lock); 1449 goto uncharge_drop; 1450 } 1451 1452 sk->sk_forward_alloc += delta; 1453 } 1454 1455 sk->sk_forward_alloc -= size; 1456 1457 /* no need to setup a destructor, we will explicitly release the 1458 * forward allocated memory on dequeue 1459 */ 1460 sock_skb_set_dropcount(sk, skb); 1461 1462 __skb_queue_tail(list, skb); 1463 spin_unlock(&list->lock); 1464 1465 if (!sock_flag(sk, SOCK_DEAD)) 1466 sk->sk_data_ready(sk); 1467 1468 busylock_release(busy); 1469 return 0; 1470 1471 uncharge_drop: 1472 atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1473 1474 drop: 1475 atomic_inc(&sk->sk_drops); 1476 busylock_release(busy); 1477 return err; 1478 } 1479 EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1480 1481 void udp_destruct_sock(struct sock *sk) 1482 { 1483 /* reclaim completely the forward allocated memory */ 1484 struct udp_sock *up = udp_sk(sk); 1485 unsigned int total = 0; 1486 struct sk_buff *skb; 1487 1488 skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 1489 while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 1490 total += skb->truesize; 1491 kfree_skb(skb); 1492 } 1493 udp_rmem_release(sk, total, 0, true); 1494 1495 inet_sock_destruct(sk); 1496 } 1497 EXPORT_SYMBOL_GPL(udp_destruct_sock); 1498 1499 int udp_init_sock(struct sock *sk) 1500 { 1501 skb_queue_head_init(&udp_sk(sk)->reader_queue); 1502 sk->sk_destruct = udp_destruct_sock; 1503 return 0; 1504 } 1505 EXPORT_SYMBOL_GPL(udp_init_sock); 1506 1507 void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1508 { 1509 if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1510 bool slow = lock_sock_fast(sk); 1511 1512 sk_peek_offset_bwd(sk, len); 1513 unlock_sock_fast(sk, slow); 1514 } 1515 1516 if (!skb_unref(skb)) 1517 return; 1518 1519 /* In the more common cases we cleared the head states previously, 1520 * see __udp_queue_rcv_skb(). 1521 */ 1522 if (unlikely(udp_skb_has_head_state(skb))) 1523 skb_release_head_state(skb); 1524 __consume_stateless_skb(skb); 1525 } 1526 EXPORT_SYMBOL_GPL(skb_consume_udp); 1527 1528 static struct sk_buff *__first_packet_length(struct sock *sk, 1529 struct sk_buff_head *rcvq, 1530 int *total) 1531 { 1532 struct sk_buff *skb; 1533 1534 while ((skb = skb_peek(rcvq)) != NULL) { 1535 if (udp_lib_checksum_complete(skb)) { 1536 __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 1537 IS_UDPLITE(sk)); 1538 __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 1539 IS_UDPLITE(sk)); 1540 atomic_inc(&sk->sk_drops); 1541 __skb_unlink(skb, rcvq); 1542 *total += skb->truesize; 1543 kfree_skb(skb); 1544 } else { 1545 /* the csum related bits could be changed, refresh 1546 * the scratch area 1547 */ 1548 udp_set_dev_scratch(skb); 1549 break; 1550 } 1551 } 1552 return skb; 1553 } 1554 1555 /** 1556 * first_packet_length - return length of first packet in receive queue 1557 * @sk: socket 1558 * 1559 * Drops all bad checksum frames, until a valid one is found. 1560 * Returns the length of found skb, or -1 if none is found. 1561 */ 1562 static int first_packet_length(struct sock *sk) 1563 { 1564 struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 1565 struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 1566 struct sk_buff *skb; 1567 int total = 0; 1568 int res; 1569 1570 spin_lock_bh(&rcvq->lock); 1571 skb = __first_packet_length(sk, rcvq, &total); 1572 if (!skb && !skb_queue_empty(sk_queue)) { 1573 spin_lock(&sk_queue->lock); 1574 skb_queue_splice_tail_init(sk_queue, rcvq); 1575 spin_unlock(&sk_queue->lock); 1576 1577 skb = __first_packet_length(sk, rcvq, &total); 1578 } 1579 res = skb ? skb->len : -1; 1580 if (total) 1581 udp_rmem_release(sk, total, 1, false); 1582 spin_unlock_bh(&rcvq->lock); 1583 return res; 1584 } 1585 1586 /* 1587 * IOCTL requests applicable to the UDP protocol 1588 */ 1589 1590 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 1591 { 1592 switch (cmd) { 1593 case SIOCOUTQ: 1594 { 1595 int amount = sk_wmem_alloc_get(sk); 1596 1597 return put_user(amount, (int __user *)arg); 1598 } 1599 1600 case SIOCINQ: 1601 { 1602 int amount = max_t(int, 0, first_packet_length(sk)); 1603 1604 return put_user(amount, (int __user *)arg); 1605 } 1606 1607 default: 1608 return -ENOIOCTLCMD; 1609 } 1610 1611 return 0; 1612 } 1613 EXPORT_SYMBOL(udp_ioctl); 1614 1615 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1616 int noblock, int *off, int *err) 1617 { 1618 struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 1619 struct sk_buff_head *queue; 1620 struct sk_buff *last; 1621 long timeo; 1622 int error; 1623 1624 queue = &udp_sk(sk)->reader_queue; 1625 flags |= noblock ? MSG_DONTWAIT : 0; 1626 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 1627 do { 1628 struct sk_buff *skb; 1629 1630 error = sock_error(sk); 1631 if (error) 1632 break; 1633 1634 error = -EAGAIN; 1635 do { 1636 spin_lock_bh(&queue->lock); 1637 skb = __skb_try_recv_from_queue(sk, queue, flags, 1638 udp_skb_destructor, 1639 off, err, &last); 1640 if (skb) { 1641 spin_unlock_bh(&queue->lock); 1642 return skb; 1643 } 1644 1645 if (skb_queue_empty(sk_queue)) { 1646 spin_unlock_bh(&queue->lock); 1647 goto busy_check; 1648 } 1649 1650 /* refill the reader queue and walk it again 1651 * keep both queues locked to avoid re-acquiring 1652 * the sk_receive_queue lock if fwd memory scheduling 1653 * is needed. 1654 */ 1655 spin_lock(&sk_queue->lock); 1656 skb_queue_splice_tail_init(sk_queue, queue); 1657 1658 skb = __skb_try_recv_from_queue(sk, queue, flags, 1659 udp_skb_dtor_locked, 1660 off, err, &last); 1661 spin_unlock(&sk_queue->lock); 1662 spin_unlock_bh(&queue->lock); 1663 if (skb) 1664 return skb; 1665 1666 busy_check: 1667 if (!sk_can_busy_loop(sk)) 1668 break; 1669 1670 sk_busy_loop(sk, flags & MSG_DONTWAIT); 1671 } while (!skb_queue_empty(sk_queue)); 1672 1673 /* sk_queue is empty, reader_queue may contain peeked packets */ 1674 } while (timeo && 1675 !__skb_wait_for_more_packets(sk, &error, &timeo, 1676 (struct sk_buff *)sk_queue)); 1677 1678 *err = error; 1679 return NULL; 1680 } 1681 EXPORT_SYMBOL(__skb_recv_udp); 1682 1683 /* 1684 * This should be easy, if there is something there we 1685 * return it, otherwise we block. 1686 */ 1687 1688 int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, 1689 int flags, int *addr_len) 1690 { 1691 struct inet_sock *inet = inet_sk(sk); 1692 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1693 struct sk_buff *skb; 1694 unsigned int ulen, copied; 1695 int off, err, peeking = flags & MSG_PEEK; 1696 int is_udplite = IS_UDPLITE(sk); 1697 bool checksum_valid = false; 1698 1699 if (flags & MSG_ERRQUEUE) 1700 return ip_recv_error(sk, msg, len, addr_len); 1701 1702 try_again: 1703 off = sk_peek_offset(sk, flags); 1704 skb = __skb_recv_udp(sk, flags, noblock, &off, &err); 1705 if (!skb) 1706 return err; 1707 1708 ulen = udp_skb_len(skb); 1709 copied = len; 1710 if (copied > ulen - off) 1711 copied = ulen - off; 1712 else if (copied < ulen) 1713 msg->msg_flags |= MSG_TRUNC; 1714 1715 /* 1716 * If checksum is needed at all, try to do it while copying the 1717 * data. If the data is truncated, or if we only want a partial 1718 * coverage checksum (UDP-Lite), do it before the copy. 1719 */ 1720 1721 if (copied < ulen || peeking || 1722 (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1723 checksum_valid = udp_skb_csum_unnecessary(skb) || 1724 !__udp_lib_checksum_complete(skb); 1725 if (!checksum_valid) 1726 goto csum_copy_err; 1727 } 1728 1729 if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1730 if (udp_skb_is_linear(skb)) 1731 err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1732 else 1733 err = skb_copy_datagram_msg(skb, off, msg, copied); 1734 } else { 1735 err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1736 1737 if (err == -EINVAL) 1738 goto csum_copy_err; 1739 } 1740 1741 if (unlikely(err)) { 1742 if (!peeking) { 1743 atomic_inc(&sk->sk_drops); 1744 UDP_INC_STATS(sock_net(sk), 1745 UDP_MIB_INERRORS, is_udplite); 1746 } 1747 kfree_skb(skb); 1748 return err; 1749 } 1750 1751 if (!peeking) 1752 UDP_INC_STATS(sock_net(sk), 1753 UDP_MIB_INDATAGRAMS, is_udplite); 1754 1755 sock_recv_ts_and_drops(msg, sk, skb); 1756 1757 /* Copy the address. */ 1758 if (sin) { 1759 sin->sin_family = AF_INET; 1760 sin->sin_port = udp_hdr(skb)->source; 1761 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1762 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1763 *addr_len = sizeof(*sin); 1764 } 1765 1766 if (udp_sk(sk)->gro_enabled) 1767 udp_cmsg_recv(msg, sk, skb); 1768 1769 if (inet->cmsg_flags) 1770 ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1771 1772 err = copied; 1773 if (flags & MSG_TRUNC) 1774 err = ulen; 1775 1776 skb_consume_udp(sk, skb, peeking ? -err : err); 1777 return err; 1778 1779 csum_copy_err: 1780 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 1781 udp_skb_destructor)) { 1782 UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 1783 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1784 } 1785 kfree_skb(skb); 1786 1787 /* starting over for a new packet, but check if we need to yield */ 1788 cond_resched(); 1789 msg->msg_flags &= ~MSG_TRUNC; 1790 goto try_again; 1791 } 1792 1793 int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1794 { 1795 /* This check is replicated from __ip4_datagram_connect() and 1796 * intended to prevent BPF program called below from accessing bytes 1797 * that are out of the bound specified by user in addr_len. 1798 */ 1799 if (addr_len < sizeof(struct sockaddr_in)) 1800 return -EINVAL; 1801 1802 return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); 1803 } 1804 EXPORT_SYMBOL(udp_pre_connect); 1805 1806 int __udp_disconnect(struct sock *sk, int flags) 1807 { 1808 struct inet_sock *inet = inet_sk(sk); 1809 /* 1810 * 1003.1g - break association. 1811 */ 1812 1813 sk->sk_state = TCP_CLOSE; 1814 inet->inet_daddr = 0; 1815 inet->inet_dport = 0; 1816 sock_rps_reset_rxhash(sk); 1817 sk->sk_bound_dev_if = 0; 1818 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 1819 inet_reset_saddr(sk); 1820 1821 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 1822 sk->sk_prot->unhash(sk); 1823 inet->inet_sport = 0; 1824 } 1825 sk_dst_reset(sk); 1826 return 0; 1827 } 1828 EXPORT_SYMBOL(__udp_disconnect); 1829 1830 int udp_disconnect(struct sock *sk, int flags) 1831 { 1832 lock_sock(sk); 1833 __udp_disconnect(sk, flags); 1834 release_sock(sk); 1835 return 0; 1836 } 1837 EXPORT_SYMBOL(udp_disconnect); 1838 1839 void udp_lib_unhash(struct sock *sk) 1840 { 1841 if (sk_hashed(sk)) { 1842 struct udp_table *udptable = sk->sk_prot->h.udp_table; 1843 struct udp_hslot *hslot, *hslot2; 1844 1845 hslot = udp_hashslot(udptable, sock_net(sk), 1846 udp_sk(sk)->udp_port_hash); 1847 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1848 1849 spin_lock_bh(&hslot->lock); 1850 if (rcu_access_pointer(sk->sk_reuseport_cb)) 1851 reuseport_detach_sock(sk); 1852 if (sk_del_node_init_rcu(sk)) { 1853 hslot->count--; 1854 inet_sk(sk)->inet_num = 0; 1855 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1856 1857 spin_lock(&hslot2->lock); 1858 hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1859 hslot2->count--; 1860 spin_unlock(&hslot2->lock); 1861 } 1862 spin_unlock_bh(&hslot->lock); 1863 } 1864 } 1865 EXPORT_SYMBOL(udp_lib_unhash); 1866 1867 /* 1868 * inet_rcv_saddr was changed, we must rehash secondary hash 1869 */ 1870 void udp_lib_rehash(struct sock *sk, u16 newhash) 1871 { 1872 if (sk_hashed(sk)) { 1873 struct udp_table *udptable = sk->sk_prot->h.udp_table; 1874 struct udp_hslot *hslot, *hslot2, *nhslot2; 1875 1876 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1877 nhslot2 = udp_hashslot2(udptable, newhash); 1878 udp_sk(sk)->udp_portaddr_hash = newhash; 1879 1880 if (hslot2 != nhslot2 || 1881 rcu_access_pointer(sk->sk_reuseport_cb)) { 1882 hslot = udp_hashslot(udptable, sock_net(sk), 1883 udp_sk(sk)->udp_port_hash); 1884 /* we must lock primary chain too */ 1885 spin_lock_bh(&hslot->lock); 1886 if (rcu_access_pointer(sk->sk_reuseport_cb)) 1887 reuseport_detach_sock(sk); 1888 1889 if (hslot2 != nhslot2) { 1890 spin_lock(&hslot2->lock); 1891 hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1892 hslot2->count--; 1893 spin_unlock(&hslot2->lock); 1894 1895 spin_lock(&nhslot2->lock); 1896 hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 1897 &nhslot2->head); 1898 nhslot2->count++; 1899 spin_unlock(&nhslot2->lock); 1900 } 1901 1902 spin_unlock_bh(&hslot->lock); 1903 } 1904 } 1905 } 1906 EXPORT_SYMBOL(udp_lib_rehash); 1907 1908 void udp_v4_rehash(struct sock *sk) 1909 { 1910 u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 1911 inet_sk(sk)->inet_rcv_saddr, 1912 inet_sk(sk)->inet_num); 1913 udp_lib_rehash(sk, new_hash); 1914 } 1915 1916 static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1917 { 1918 int rc; 1919 1920 if (inet_sk(sk)->inet_daddr) { 1921 sock_rps_save_rxhash(sk, skb); 1922 sk_mark_napi_id(sk, skb); 1923 sk_incoming_cpu_update(sk); 1924 } else { 1925 sk_mark_napi_id_once(sk, skb); 1926 } 1927 1928 rc = __udp_enqueue_schedule_skb(sk, skb); 1929 if (rc < 0) { 1930 int is_udplite = IS_UDPLITE(sk); 1931 1932 /* Note that an ENOMEM error is charged twice */ 1933 if (rc == -ENOMEM) 1934 UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 1935 is_udplite); 1936 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1937 kfree_skb(skb); 1938 trace_udp_fail_queue_rcv_skb(rc, sk); 1939 return -1; 1940 } 1941 1942 return 0; 1943 } 1944 1945 /* returns: 1946 * -1: error 1947 * 0: success 1948 * >0: "udp encap" protocol resubmission 1949 * 1950 * Note that in the success and error cases, the skb is assumed to 1951 * have either been requeued or freed. 1952 */ 1953 static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 1954 { 1955 struct udp_sock *up = udp_sk(sk); 1956 int is_udplite = IS_UDPLITE(sk); 1957 1958 /* 1959 * Charge it to the socket, dropping if the queue is full. 1960 */ 1961 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 1962 goto drop; 1963 nf_reset(skb); 1964 1965 if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 1966 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 1967 1968 /* 1969 * This is an encapsulation socket so pass the skb to 1970 * the socket's udp_encap_rcv() hook. Otherwise, just 1971 * fall through and pass this up the UDP socket. 1972 * up->encap_rcv() returns the following value: 1973 * =0 if skb was successfully passed to the encap 1974 * handler or was discarded by it. 1975 * >0 if skb should be passed on to UDP. 1976 * <0 if skb should be resubmitted as proto -N 1977 */ 1978 1979 /* if we're overly short, let UDP handle it */ 1980 encap_rcv = READ_ONCE(up->encap_rcv); 1981 if (encap_rcv) { 1982 int ret; 1983 1984 /* Verify checksum before giving to encap */ 1985 if (udp_lib_checksum_complete(skb)) 1986 goto csum_error; 1987 1988 ret = encap_rcv(sk, skb); 1989 if (ret <= 0) { 1990 __UDP_INC_STATS(sock_net(sk), 1991 UDP_MIB_INDATAGRAMS, 1992 is_udplite); 1993 return -ret; 1994 } 1995 } 1996 1997 /* FALLTHROUGH -- it's a UDP Packet */ 1998 } 1999 2000 /* 2001 * UDP-Lite specific tests, ignored on UDP sockets 2002 */ 2003 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 2004 2005 /* 2006 * MIB statistics other than incrementing the error count are 2007 * disabled for the following two types of errors: these depend 2008 * on the application settings, not on the functioning of the 2009 * protocol stack as such. 2010 * 2011 * RFC 3828 here recommends (sec 3.3): "There should also be a 2012 * way ... to ... at least let the receiving application block 2013 * delivery of packets with coverage values less than a value 2014 * provided by the application." 2015 */ 2016 if (up->pcrlen == 0) { /* full coverage was set */ 2017 net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 2018 UDP_SKB_CB(skb)->cscov, skb->len); 2019 goto drop; 2020 } 2021 /* The next case involves violating the min. coverage requested 2022 * by the receiver. This is subtle: if receiver wants x and x is 2023 * greater than the buffersize/MTU then receiver will complain 2024 * that it wants x while sender emits packets of smaller size y. 2025 * Therefore the above ...()->partial_cov statement is essential. 2026 */ 2027 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 2028 net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 2029 UDP_SKB_CB(skb)->cscov, up->pcrlen); 2030 goto drop; 2031 } 2032 } 2033 2034 prefetch(&sk->sk_rmem_alloc); 2035 if (rcu_access_pointer(sk->sk_filter) && 2036 udp_lib_checksum_complete(skb)) 2037 goto csum_error; 2038 2039 if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 2040 goto drop; 2041 2042 udp_csum_pull_header(skb); 2043 2044 ipv4_pktinfo_prepare(sk, skb); 2045 return __udp_queue_rcv_skb(sk, skb); 2046 2047 csum_error: 2048 __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2049 drop: 2050 __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 2051 atomic_inc(&sk->sk_drops); 2052 kfree_skb(skb); 2053 return -1; 2054 } 2055 2056 static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2057 { 2058 struct sk_buff *next, *segs; 2059 int ret; 2060 2061 if (likely(!udp_unexpected_gso(sk, skb))) 2062 return udp_queue_rcv_one_skb(sk, skb); 2063 2064 BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_SGO_CB_OFFSET); 2065 __skb_push(skb, -skb_mac_offset(skb)); 2066 segs = udp_rcv_segment(sk, skb, true); 2067 for (skb = segs; skb; skb = next) { 2068 next = skb->next; 2069 __skb_pull(skb, skb_transport_offset(skb)); 2070 ret = udp_queue_rcv_one_skb(sk, skb); 2071 if (ret > 0) 2072 ip_protocol_deliver_rcu(dev_net(skb->dev), skb, -ret); 2073 } 2074 return 0; 2075 } 2076 2077 /* For TCP sockets, sk_rx_dst is protected by socket lock 2078 * For UDP, we use xchg() to guard against concurrent changes. 2079 */ 2080 bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2081 { 2082 struct dst_entry *old; 2083 2084 if (dst_hold_safe(dst)) { 2085 old = xchg(&sk->sk_rx_dst, dst); 2086 dst_release(old); 2087 return old != dst; 2088 } 2089 return false; 2090 } 2091 EXPORT_SYMBOL(udp_sk_rx_dst_set); 2092 2093 /* 2094 * Multicasts and broadcasts go to each listener. 2095 * 2096 * Note: called only from the BH handler context. 2097 */ 2098 static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2099 struct udphdr *uh, 2100 __be32 saddr, __be32 daddr, 2101 struct udp_table *udptable, 2102 int proto) 2103 { 2104 struct sock *sk, *first = NULL; 2105 unsigned short hnum = ntohs(uh->dest); 2106 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 2107 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2108 unsigned int offset = offsetof(typeof(*sk), sk_node); 2109 int dif = skb->dev->ifindex; 2110 int sdif = inet_sdif(skb); 2111 struct hlist_node *node; 2112 struct sk_buff *nskb; 2113 2114 if (use_hash2) { 2115 hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 2116 udptable->mask; 2117 hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 2118 start_lookup: 2119 hslot = &udptable->hash2[hash2]; 2120 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 2121 } 2122 2123 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2124 if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2125 uh->source, saddr, dif, sdif, hnum)) 2126 continue; 2127 2128 if (!first) { 2129 first = sk; 2130 continue; 2131 } 2132 nskb = skb_clone(skb, GFP_ATOMIC); 2133 2134 if (unlikely(!nskb)) { 2135 atomic_inc(&sk->sk_drops); 2136 __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2137 IS_UDPLITE(sk)); 2138 __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2139 IS_UDPLITE(sk)); 2140 continue; 2141 } 2142 if (udp_queue_rcv_skb(sk, nskb) > 0) 2143 consume_skb(nskb); 2144 } 2145 2146 /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 2147 if (use_hash2 && hash2 != hash2_any) { 2148 hash2 = hash2_any; 2149 goto start_lookup; 2150 } 2151 2152 if (first) { 2153 if (udp_queue_rcv_skb(first, skb) > 0) 2154 consume_skb(skb); 2155 } else { 2156 kfree_skb(skb); 2157 __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 2158 proto == IPPROTO_UDPLITE); 2159 } 2160 return 0; 2161 } 2162 2163 /* Initialize UDP checksum. If exited with zero value (success), 2164 * CHECKSUM_UNNECESSARY means, that no more checks are required. 2165 * Otherwise, csum completion requires chacksumming packet body, 2166 * including udp header and folding it to skb->csum. 2167 */ 2168 static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2169 int proto) 2170 { 2171 int err; 2172 2173 UDP_SKB_CB(skb)->partial_cov = 0; 2174 UDP_SKB_CB(skb)->cscov = skb->len; 2175 2176 if (proto == IPPROTO_UDPLITE) { 2177 err = udplite_checksum_init(skb, uh); 2178 if (err) 2179 return err; 2180 2181 if (UDP_SKB_CB(skb)->partial_cov) { 2182 skb->csum = inet_compute_pseudo(skb, proto); 2183 return 0; 2184 } 2185 } 2186 2187 /* Note, we are only interested in != 0 or == 0, thus the 2188 * force to int. 2189 */ 2190 err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2191 inet_compute_pseudo); 2192 if (err) 2193 return err; 2194 2195 if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2196 /* If SW calculated the value, we know it's bad */ 2197 if (skb->csum_complete_sw) 2198 return 1; 2199 2200 /* HW says the value is bad. Let's validate that. 2201 * skb->csum is no longer the full packet checksum, 2202 * so don't treat it as such. 2203 */ 2204 skb_checksum_complete_unset(skb); 2205 } 2206 2207 return 0; 2208 } 2209 2210 /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 2211 * return code conversion for ip layer consumption 2212 */ 2213 static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 2214 struct udphdr *uh) 2215 { 2216 int ret; 2217 2218 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2219 skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, 2220 inet_compute_pseudo); 2221 2222 ret = udp_queue_rcv_skb(sk, skb); 2223 2224 /* a return value > 0 means to resubmit the input, but 2225 * it wants the return to be -protocol, or 0 2226 */ 2227 if (ret > 0) 2228 return -ret; 2229 return 0; 2230 } 2231 2232 /* 2233 * All we need to do is get the socket, and then do a checksum. 2234 */ 2235 2236 int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2237 int proto) 2238 { 2239 struct sock *sk; 2240 struct udphdr *uh; 2241 unsigned short ulen; 2242 struct rtable *rt = skb_rtable(skb); 2243 __be32 saddr, daddr; 2244 struct net *net = dev_net(skb->dev); 2245 2246 /* 2247 * Validate the packet. 2248 */ 2249 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2250 goto drop; /* No space for header. */ 2251 2252 uh = udp_hdr(skb); 2253 ulen = ntohs(uh->len); 2254 saddr = ip_hdr(skb)->saddr; 2255 daddr = ip_hdr(skb)->daddr; 2256 2257 if (ulen > skb->len) 2258 goto short_packet; 2259 2260 if (proto == IPPROTO_UDP) { 2261 /* UDP validates ulen. */ 2262 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2263 goto short_packet; 2264 uh = udp_hdr(skb); 2265 } 2266 2267 if (udp4_csum_init(skb, uh, proto)) 2268 goto csum_error; 2269 2270 sk = skb_steal_sock(skb); 2271 if (sk) { 2272 struct dst_entry *dst = skb_dst(skb); 2273 int ret; 2274 2275 if (unlikely(sk->sk_rx_dst != dst)) 2276 udp_sk_rx_dst_set(sk, dst); 2277 2278 ret = udp_unicast_rcv_skb(sk, skb, uh); 2279 sock_put(sk); 2280 return ret; 2281 } 2282 2283 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2284 return __udp4_lib_mcast_deliver(net, skb, uh, 2285 saddr, daddr, udptable, proto); 2286 2287 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 2288 if (sk) 2289 return udp_unicast_rcv_skb(sk, skb, uh); 2290 2291 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2292 goto drop; 2293 nf_reset(skb); 2294 2295 /* No socket. Drop packet silently, if checksum is wrong */ 2296 if (udp_lib_checksum_complete(skb)) 2297 goto csum_error; 2298 2299 __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2300 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2301 2302 /* 2303 * Hmm. We got an UDP packet to a port to which we 2304 * don't wanna listen. Ignore it. 2305 */ 2306 kfree_skb(skb); 2307 return 0; 2308 2309 short_packet: 2310 net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2311 proto == IPPROTO_UDPLITE ? "Lite" : "", 2312 &saddr, ntohs(uh->source), 2313 ulen, skb->len, 2314 &daddr, ntohs(uh->dest)); 2315 goto drop; 2316 2317 csum_error: 2318 /* 2319 * RFC1122: OK. Discards the bad packet silently (as far as 2320 * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2321 */ 2322 net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2323 proto == IPPROTO_UDPLITE ? "Lite" : "", 2324 &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2325 ulen); 2326 __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2327 drop: 2328 __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 2329 kfree_skb(skb); 2330 return 0; 2331 } 2332 2333 /* We can only early demux multicast if there is a single matching socket. 2334 * If more than one socket found returns NULL 2335 */ 2336 static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2337 __be16 loc_port, __be32 loc_addr, 2338 __be16 rmt_port, __be32 rmt_addr, 2339 int dif, int sdif) 2340 { 2341 struct sock *sk, *result; 2342 unsigned short hnum = ntohs(loc_port); 2343 unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); 2344 struct udp_hslot *hslot = &udp_table.hash[slot]; 2345 2346 /* Do not bother scanning a too big list */ 2347 if (hslot->count > 10) 2348 return NULL; 2349 2350 result = NULL; 2351 sk_for_each_rcu(sk, &hslot->head) { 2352 if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2353 rmt_port, rmt_addr, dif, sdif, hnum)) { 2354 if (result) 2355 return NULL; 2356 result = sk; 2357 } 2358 } 2359 2360 return result; 2361 } 2362 2363 /* For unicast we should only early demux connected sockets or we can 2364 * break forwarding setups. The chains here can be long so only check 2365 * if the first socket is an exact match and if not move on. 2366 */ 2367 static struct sock *__udp4_lib_demux_lookup(struct net *net, 2368 __be16 loc_port, __be32 loc_addr, 2369 __be16 rmt_port, __be32 rmt_addr, 2370 int dif, int sdif) 2371 { 2372 unsigned short hnum = ntohs(loc_port); 2373 unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2374 unsigned int slot2 = hash2 & udp_table.mask; 2375 struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 2376 INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2377 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 2378 struct sock *sk; 2379 2380 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2381 if (INET_MATCH(sk, net, acookie, rmt_addr, 2382 loc_addr, ports, dif, sdif)) 2383 return sk; 2384 /* Only check first socket in chain */ 2385 break; 2386 } 2387 return NULL; 2388 } 2389 2390 int udp_v4_early_demux(struct sk_buff *skb) 2391 { 2392 struct net *net = dev_net(skb->dev); 2393 struct in_device *in_dev = NULL; 2394 const struct iphdr *iph; 2395 const struct udphdr *uh; 2396 struct sock *sk = NULL; 2397 struct dst_entry *dst; 2398 int dif = skb->dev->ifindex; 2399 int sdif = inet_sdif(skb); 2400 int ours; 2401 2402 /* validate the packet */ 2403 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 2404 return 0; 2405 2406 iph = ip_hdr(skb); 2407 uh = udp_hdr(skb); 2408 2409 if (skb->pkt_type == PACKET_MULTICAST) { 2410 in_dev = __in_dev_get_rcu(skb->dev); 2411 2412 if (!in_dev) 2413 return 0; 2414 2415 ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 2416 iph->protocol); 2417 if (!ours) 2418 return 0; 2419 2420 sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2421 uh->source, iph->saddr, 2422 dif, sdif); 2423 } else if (skb->pkt_type == PACKET_HOST) { 2424 sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 2425 uh->source, iph->saddr, dif, sdif); 2426 } 2427 2428 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 2429 return 0; 2430 2431 skb->sk = sk; 2432 skb->destructor = sock_efree; 2433 dst = READ_ONCE(sk->sk_rx_dst); 2434 2435 if (dst) 2436 dst = dst_check(dst, 0); 2437 if (dst) { 2438 u32 itag = 0; 2439 2440 /* set noref for now. 2441 * any place which wants to hold dst has to call 2442 * dst_hold_safe() 2443 */ 2444 skb_dst_set_noref(skb, dst); 2445 2446 /* for unconnected multicast sockets we need to validate 2447 * the source on each packet 2448 */ 2449 if (!inet_sk(sk)->inet_daddr && in_dev) 2450 return ip_mc_validate_source(skb, iph->daddr, 2451 iph->saddr, iph->tos, 2452 skb->dev, in_dev, &itag); 2453 } 2454 return 0; 2455 } 2456 2457 int udp_rcv(struct sk_buff *skb) 2458 { 2459 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2460 } 2461 2462 void udp_destroy_sock(struct sock *sk) 2463 { 2464 struct udp_sock *up = udp_sk(sk); 2465 bool slow = lock_sock_fast(sk); 2466 udp_flush_pending_frames(sk); 2467 unlock_sock_fast(sk, slow); 2468 if (static_branch_unlikely(&udp_encap_needed_key)) { 2469 if (up->encap_type) { 2470 void (*encap_destroy)(struct sock *sk); 2471 encap_destroy = READ_ONCE(up->encap_destroy); 2472 if (encap_destroy) 2473 encap_destroy(sk); 2474 } 2475 if (up->encap_enabled) 2476 static_branch_dec(&udp_encap_needed_key); 2477 } 2478 } 2479 2480 /* 2481 * Socket option code for UDP 2482 */ 2483 int udp_lib_setsockopt(struct sock *sk, int level, int optname, 2484 char __user *optval, unsigned int optlen, 2485 int (*push_pending_frames)(struct sock *)) 2486 { 2487 struct udp_sock *up = udp_sk(sk); 2488 int val, valbool; 2489 int err = 0; 2490 int is_udplite = IS_UDPLITE(sk); 2491 2492 if (optlen < sizeof(int)) 2493 return -EINVAL; 2494 2495 if (get_user(val, (int __user *)optval)) 2496 return -EFAULT; 2497 2498 valbool = val ? 1 : 0; 2499 2500 switch (optname) { 2501 case UDP_CORK: 2502 if (val != 0) { 2503 up->corkflag = 1; 2504 } else { 2505 up->corkflag = 0; 2506 lock_sock(sk); 2507 push_pending_frames(sk); 2508 release_sock(sk); 2509 } 2510 break; 2511 2512 case UDP_ENCAP: 2513 switch (val) { 2514 case 0: 2515 case UDP_ENCAP_ESPINUDP: 2516 case UDP_ENCAP_ESPINUDP_NON_IKE: 2517 up->encap_rcv = xfrm4_udp_encap_rcv; 2518 /* FALLTHROUGH */ 2519 case UDP_ENCAP_L2TPINUDP: 2520 up->encap_type = val; 2521 lock_sock(sk); 2522 udp_tunnel_encap_enable(sk->sk_socket); 2523 release_sock(sk); 2524 break; 2525 default: 2526 err = -ENOPROTOOPT; 2527 break; 2528 } 2529 break; 2530 2531 case UDP_NO_CHECK6_TX: 2532 up->no_check6_tx = valbool; 2533 break; 2534 2535 case UDP_NO_CHECK6_RX: 2536 up->no_check6_rx = valbool; 2537 break; 2538 2539 case UDP_SEGMENT: 2540 if (val < 0 || val > USHRT_MAX) 2541 return -EINVAL; 2542 up->gso_size = val; 2543 break; 2544 2545 case UDP_GRO: 2546 lock_sock(sk); 2547 if (valbool) 2548 udp_tunnel_encap_enable(sk->sk_socket); 2549 up->gro_enabled = valbool; 2550 release_sock(sk); 2551 break; 2552 2553 /* 2554 * UDP-Lite's partial checksum coverage (RFC 3828). 2555 */ 2556 /* The sender sets actual checksum coverage length via this option. 2557 * The case coverage > packet length is handled by send module. */ 2558 case UDPLITE_SEND_CSCOV: 2559 if (!is_udplite) /* Disable the option on UDP sockets */ 2560 return -ENOPROTOOPT; 2561 if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2562 val = 8; 2563 else if (val > USHRT_MAX) 2564 val = USHRT_MAX; 2565 up->pcslen = val; 2566 up->pcflag |= UDPLITE_SEND_CC; 2567 break; 2568 2569 /* The receiver specifies a minimum checksum coverage value. To make 2570 * sense, this should be set to at least 8 (as done below). If zero is 2571 * used, this again means full checksum coverage. */ 2572 case UDPLITE_RECV_CSCOV: 2573 if (!is_udplite) /* Disable the option on UDP sockets */ 2574 return -ENOPROTOOPT; 2575 if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2576 val = 8; 2577 else if (val > USHRT_MAX) 2578 val = USHRT_MAX; 2579 up->pcrlen = val; 2580 up->pcflag |= UDPLITE_RECV_CC; 2581 break; 2582 2583 default: 2584 err = -ENOPROTOOPT; 2585 break; 2586 } 2587 2588 return err; 2589 } 2590 EXPORT_SYMBOL(udp_lib_setsockopt); 2591 2592 int udp_setsockopt(struct sock *sk, int level, int optname, 2593 char __user *optval, unsigned int optlen) 2594 { 2595 if (level == SOL_UDP || level == SOL_UDPLITE) 2596 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2597 udp_push_pending_frames); 2598 return ip_setsockopt(sk, level, optname, optval, optlen); 2599 } 2600 2601 #ifdef CONFIG_COMPAT 2602 int compat_udp_setsockopt(struct sock *sk, int level, int optname, 2603 char __user *optval, unsigned int optlen) 2604 { 2605 if (level == SOL_UDP || level == SOL_UDPLITE) 2606 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2607 udp_push_pending_frames); 2608 return compat_ip_setsockopt(sk, level, optname, optval, optlen); 2609 } 2610 #endif 2611 2612 int udp_lib_getsockopt(struct sock *sk, int level, int optname, 2613 char __user *optval, int __user *optlen) 2614 { 2615 struct udp_sock *up = udp_sk(sk); 2616 int val, len; 2617 2618 if (get_user(len, optlen)) 2619 return -EFAULT; 2620 2621 len = min_t(unsigned int, len, sizeof(int)); 2622 2623 if (len < 0) 2624 return -EINVAL; 2625 2626 switch (optname) { 2627 case UDP_CORK: 2628 val = up->corkflag; 2629 break; 2630 2631 case UDP_ENCAP: 2632 val = up->encap_type; 2633 break; 2634 2635 case UDP_NO_CHECK6_TX: 2636 val = up->no_check6_tx; 2637 break; 2638 2639 case UDP_NO_CHECK6_RX: 2640 val = up->no_check6_rx; 2641 break; 2642 2643 case UDP_SEGMENT: 2644 val = up->gso_size; 2645 break; 2646 2647 /* The following two cannot be changed on UDP sockets, the return is 2648 * always 0 (which corresponds to the full checksum coverage of UDP). */ 2649 case UDPLITE_SEND_CSCOV: 2650 val = up->pcslen; 2651 break; 2652 2653 case UDPLITE_RECV_CSCOV: 2654 val = up->pcrlen; 2655 break; 2656 2657 default: 2658 return -ENOPROTOOPT; 2659 } 2660 2661 if (put_user(len, optlen)) 2662 return -EFAULT; 2663 if (copy_to_user(optval, &val, len)) 2664 return -EFAULT; 2665 return 0; 2666 } 2667 EXPORT_SYMBOL(udp_lib_getsockopt); 2668 2669 int udp_getsockopt(struct sock *sk, int level, int optname, 2670 char __user *optval, int __user *optlen) 2671 { 2672 if (level == SOL_UDP || level == SOL_UDPLITE) 2673 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2674 return ip_getsockopt(sk, level, optname, optval, optlen); 2675 } 2676 2677 #ifdef CONFIG_COMPAT 2678 int compat_udp_getsockopt(struct sock *sk, int level, int optname, 2679 char __user *optval, int __user *optlen) 2680 { 2681 if (level == SOL_UDP || level == SOL_UDPLITE) 2682 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2683 return compat_ip_getsockopt(sk, level, optname, optval, optlen); 2684 } 2685 #endif 2686 /** 2687 * udp_poll - wait for a UDP event. 2688 * @file - file struct 2689 * @sock - socket 2690 * @wait - poll table 2691 * 2692 * This is same as datagram poll, except for the special case of 2693 * blocking sockets. If application is using a blocking fd 2694 * and a packet with checksum error is in the queue; 2695 * then it could get return from select indicating data available 2696 * but then block when reading it. Add special case code 2697 * to work around these arguably broken applications. 2698 */ 2699 __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 2700 { 2701 __poll_t mask = datagram_poll(file, sock, wait); 2702 struct sock *sk = sock->sk; 2703 2704 if (!skb_queue_empty(&udp_sk(sk)->reader_queue)) 2705 mask |= EPOLLIN | EPOLLRDNORM; 2706 2707 /* Check for false positives due to checksum errors */ 2708 if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2709 !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2710 mask &= ~(EPOLLIN | EPOLLRDNORM); 2711 2712 return mask; 2713 2714 } 2715 EXPORT_SYMBOL(udp_poll); 2716 2717 int udp_abort(struct sock *sk, int err) 2718 { 2719 lock_sock(sk); 2720 2721 sk->sk_err = err; 2722 sk->sk_error_report(sk); 2723 __udp_disconnect(sk, 0); 2724 2725 release_sock(sk); 2726 2727 return 0; 2728 } 2729 EXPORT_SYMBOL_GPL(udp_abort); 2730 2731 struct proto udp_prot = { 2732 .name = "UDP", 2733 .owner = THIS_MODULE, 2734 .close = udp_lib_close, 2735 .pre_connect = udp_pre_connect, 2736 .connect = ip4_datagram_connect, 2737 .disconnect = udp_disconnect, 2738 .ioctl = udp_ioctl, 2739 .init = udp_init_sock, 2740 .destroy = udp_destroy_sock, 2741 .setsockopt = udp_setsockopt, 2742 .getsockopt = udp_getsockopt, 2743 .sendmsg = udp_sendmsg, 2744 .recvmsg = udp_recvmsg, 2745 .sendpage = udp_sendpage, 2746 .release_cb = ip4_datagram_release_cb, 2747 .hash = udp_lib_hash, 2748 .unhash = udp_lib_unhash, 2749 .rehash = udp_v4_rehash, 2750 .get_port = udp_v4_get_port, 2751 .memory_allocated = &udp_memory_allocated, 2752 .sysctl_mem = sysctl_udp_mem, 2753 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 2754 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2755 .obj_size = sizeof(struct udp_sock), 2756 .h.udp_table = &udp_table, 2757 #ifdef CONFIG_COMPAT 2758 .compat_setsockopt = compat_udp_setsockopt, 2759 .compat_getsockopt = compat_udp_getsockopt, 2760 #endif 2761 .diag_destroy = udp_abort, 2762 }; 2763 EXPORT_SYMBOL(udp_prot); 2764 2765 /* ------------------------------------------------------------------------ */ 2766 #ifdef CONFIG_PROC_FS 2767 2768 static struct sock *udp_get_first(struct seq_file *seq, int start) 2769 { 2770 struct sock *sk; 2771 struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); 2772 struct udp_iter_state *state = seq->private; 2773 struct net *net = seq_file_net(seq); 2774 2775 for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; 2776 ++state->bucket) { 2777 struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; 2778 2779 if (hlist_empty(&hslot->head)) 2780 continue; 2781 2782 spin_lock_bh(&hslot->lock); 2783 sk_for_each(sk, &hslot->head) { 2784 if (!net_eq(sock_net(sk), net)) 2785 continue; 2786 if (sk->sk_family == afinfo->family) 2787 goto found; 2788 } 2789 spin_unlock_bh(&hslot->lock); 2790 } 2791 sk = NULL; 2792 found: 2793 return sk; 2794 } 2795 2796 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 2797 { 2798 struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); 2799 struct udp_iter_state *state = seq->private; 2800 struct net *net = seq_file_net(seq); 2801 2802 do { 2803 sk = sk_next(sk); 2804 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != afinfo->family)); 2805 2806 if (!sk) { 2807 if (state->bucket <= afinfo->udp_table->mask) 2808 spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 2809 return udp_get_first(seq, state->bucket + 1); 2810 } 2811 return sk; 2812 } 2813 2814 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 2815 { 2816 struct sock *sk = udp_get_first(seq, 0); 2817 2818 if (sk) 2819 while (pos && (sk = udp_get_next(seq, sk)) != NULL) 2820 --pos; 2821 return pos ? NULL : sk; 2822 } 2823 2824 void *udp_seq_start(struct seq_file *seq, loff_t *pos) 2825 { 2826 struct udp_iter_state *state = seq->private; 2827 state->bucket = MAX_UDP_PORTS; 2828 2829 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 2830 } 2831 EXPORT_SYMBOL(udp_seq_start); 2832 2833 void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2834 { 2835 struct sock *sk; 2836 2837 if (v == SEQ_START_TOKEN) 2838 sk = udp_get_idx(seq, 0); 2839 else 2840 sk = udp_get_next(seq, v); 2841 2842 ++*pos; 2843 return sk; 2844 } 2845 EXPORT_SYMBOL(udp_seq_next); 2846 2847 void udp_seq_stop(struct seq_file *seq, void *v) 2848 { 2849 struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); 2850 struct udp_iter_state *state = seq->private; 2851 2852 if (state->bucket <= afinfo->udp_table->mask) 2853 spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 2854 } 2855 EXPORT_SYMBOL(udp_seq_stop); 2856 2857 /* ------------------------------------------------------------------------ */ 2858 static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2859 int bucket) 2860 { 2861 struct inet_sock *inet = inet_sk(sp); 2862 __be32 dest = inet->inet_daddr; 2863 __be32 src = inet->inet_rcv_saddr; 2864 __u16 destp = ntohs(inet->inet_dport); 2865 __u16 srcp = ntohs(inet->inet_sport); 2866 2867 seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2868 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", 2869 bucket, src, srcp, dest, destp, sp->sk_state, 2870 sk_wmem_alloc_get(sp), 2871 udp_rqueue_get(sp), 2872 0, 0L, 0, 2873 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 2874 0, sock_i_ino(sp), 2875 refcount_read(&sp->sk_refcnt), sp, 2876 atomic_read(&sp->sk_drops)); 2877 } 2878 2879 int udp4_seq_show(struct seq_file *seq, void *v) 2880 { 2881 seq_setwidth(seq, 127); 2882 if (v == SEQ_START_TOKEN) 2883 seq_puts(seq, " sl local_address rem_address st tx_queue " 2884 "rx_queue tr tm->when retrnsmt uid timeout " 2885 "inode ref pointer drops"); 2886 else { 2887 struct udp_iter_state *state = seq->private; 2888 2889 udp4_format_sock(v, seq, state->bucket); 2890 } 2891 seq_pad(seq, '\n'); 2892 return 0; 2893 } 2894 2895 const struct seq_operations udp_seq_ops = { 2896 .start = udp_seq_start, 2897 .next = udp_seq_next, 2898 .stop = udp_seq_stop, 2899 .show = udp4_seq_show, 2900 }; 2901 EXPORT_SYMBOL(udp_seq_ops); 2902 2903 static struct udp_seq_afinfo udp4_seq_afinfo = { 2904 .family = AF_INET, 2905 .udp_table = &udp_table, 2906 }; 2907 2908 static int __net_init udp4_proc_init_net(struct net *net) 2909 { 2910 if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 2911 sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 2912 return -ENOMEM; 2913 return 0; 2914 } 2915 2916 static void __net_exit udp4_proc_exit_net(struct net *net) 2917 { 2918 remove_proc_entry("udp", net->proc_net); 2919 } 2920 2921 static struct pernet_operations udp4_net_ops = { 2922 .init = udp4_proc_init_net, 2923 .exit = udp4_proc_exit_net, 2924 }; 2925 2926 int __init udp4_proc_init(void) 2927 { 2928 return register_pernet_subsys(&udp4_net_ops); 2929 } 2930 2931 void udp4_proc_exit(void) 2932 { 2933 unregister_pernet_subsys(&udp4_net_ops); 2934 } 2935 #endif /* CONFIG_PROC_FS */ 2936 2937 static __initdata unsigned long uhash_entries; 2938 static int __init set_uhash_entries(char *str) 2939 { 2940 ssize_t ret; 2941 2942 if (!str) 2943 return 0; 2944 2945 ret = kstrtoul(str, 0, &uhash_entries); 2946 if (ret) 2947 return 0; 2948 2949 if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 2950 uhash_entries = UDP_HTABLE_SIZE_MIN; 2951 return 1; 2952 } 2953 __setup("uhash_entries=", set_uhash_entries); 2954 2955 void __init udp_table_init(struct udp_table *table, const char *name) 2956 { 2957 unsigned int i; 2958 2959 table->hash = alloc_large_system_hash(name, 2960 2 * sizeof(struct udp_hslot), 2961 uhash_entries, 2962 21, /* one slot per 2 MB */ 2963 0, 2964 &table->log, 2965 &table->mask, 2966 UDP_HTABLE_SIZE_MIN, 2967 64 * 1024); 2968 2969 table->hash2 = table->hash + (table->mask + 1); 2970 for (i = 0; i <= table->mask; i++) { 2971 INIT_HLIST_HEAD(&table->hash[i].head); 2972 table->hash[i].count = 0; 2973 spin_lock_init(&table->hash[i].lock); 2974 } 2975 for (i = 0; i <= table->mask; i++) { 2976 INIT_HLIST_HEAD(&table->hash2[i].head); 2977 table->hash2[i].count = 0; 2978 spin_lock_init(&table->hash2[i].lock); 2979 } 2980 } 2981 2982 u32 udp_flow_hashrnd(void) 2983 { 2984 static u32 hashrnd __read_mostly; 2985 2986 net_get_random_once(&hashrnd, sizeof(hashrnd)); 2987 2988 return hashrnd; 2989 } 2990 EXPORT_SYMBOL(udp_flow_hashrnd); 2991 2992 static void __udp_sysctl_init(struct net *net) 2993 { 2994 net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM; 2995 net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM; 2996 2997 #ifdef CONFIG_NET_L3_MASTER_DEV 2998 net->ipv4.sysctl_udp_l3mdev_accept = 0; 2999 #endif 3000 } 3001 3002 static int __net_init udp_sysctl_init(struct net *net) 3003 { 3004 __udp_sysctl_init(net); 3005 return 0; 3006 } 3007 3008 static struct pernet_operations __net_initdata udp_sysctl_ops = { 3009 .init = udp_sysctl_init, 3010 }; 3011 3012 void __init udp_init(void) 3013 { 3014 unsigned long limit; 3015 unsigned int i; 3016 3017 udp_table_init(&udp_table, "UDP"); 3018 limit = nr_free_buffer_pages() / 8; 3019 limit = max(limit, 128UL); 3020 sysctl_udp_mem[0] = limit / 4 * 3; 3021 sysctl_udp_mem[1] = limit; 3022 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 3023 3024 __udp_sysctl_init(&init_net); 3025 3026 /* 16 spinlocks per cpu */ 3027 udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 3028 udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 3029 GFP_KERNEL); 3030 if (!udp_busylocks) 3031 panic("UDP: failed to alloc udp_busylocks\n"); 3032 for (i = 0; i < (1U << udp_busylocks_log); i++) 3033 spin_lock_init(udp_busylocks + i); 3034 3035 if (register_pernet_subsys(&udp_sysctl_ops)) 3036 panic("UDP: failed to init sysctl parameters.\n"); 3037 } 3038