12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The User Datagram Protocol (UDP). 81da177e4SLinus Torvalds * 902c30a84SJesper Juhl * Authors: Ross Biro 101da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 111da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 12113aa838SAlan Cox * Alan Cox, <alan@lxorguk.ukuu.org.uk> 131da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixes: 161da177e4SLinus Torvalds * Alan Cox : verify_area() calls 171da177e4SLinus Torvalds * Alan Cox : stopped close while in use off icmp 181da177e4SLinus Torvalds * messages. Not a fix but a botch that 191da177e4SLinus Torvalds * for udp at least is 'valid'. 201da177e4SLinus Torvalds * Alan Cox : Fixed icmp handling properly 211da177e4SLinus Torvalds * Alan Cox : Correct error for oversized datagrams 221da177e4SLinus Torvalds * Alan Cox : Tidied select() semantics. 231da177e4SLinus Torvalds * Alan Cox : udp_err() fixed properly, also now 241da177e4SLinus Torvalds * select and read wake correctly on errors 251da177e4SLinus Torvalds * Alan Cox : udp_send verify_area moved to avoid mem leak 261da177e4SLinus Torvalds * Alan Cox : UDP can count its memory 271da177e4SLinus Torvalds * Alan Cox : send to an unknown connection causes 281da177e4SLinus Torvalds * an ECONNREFUSED off the icmp, but 291da177e4SLinus Torvalds * does NOT close. 301da177e4SLinus Torvalds * Alan Cox : Switched to new sk_buff handlers. No more backlog! 311da177e4SLinus Torvalds * Alan Cox : Using generic datagram code. Even smaller and the PEEK 321da177e4SLinus Torvalds * bug no longer crashes it. 331da177e4SLinus Torvalds * Fred Van Kempen : Net2e support for sk->broadcast. 341da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram 351da177e4SLinus Torvalds * Alan Cox : Added get/set sockopt support. 361da177e4SLinus Torvalds * Alan Cox : Broadcasting without option set returns EACCES. 371da177e4SLinus Torvalds * Alan Cox : No wakeup calls. Instead we now use the callbacks. 381da177e4SLinus Torvalds * Alan Cox : Use ip_tos and ip_ttl 391da177e4SLinus Torvalds * Alan Cox : SNMP Mibs 401da177e4SLinus Torvalds * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 411da177e4SLinus Torvalds * Matt Dillon : UDP length checks. 421da177e4SLinus Torvalds * Alan Cox : Smarter af_inet used properly. 431da177e4SLinus Torvalds * Alan Cox : Use new kernel side addressing. 441da177e4SLinus Torvalds * Alan Cox : Incorrect return on truncated datagram receive. 451da177e4SLinus Torvalds * Arnt Gulbrandsen : New udp_send and stuff 461da177e4SLinus Torvalds * Alan Cox : Cache last socket 471da177e4SLinus Torvalds * Alan Cox : Route cache 481da177e4SLinus Torvalds * Jon Peatfield : Minor efficiency fix to sendto(). 491da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 501da177e4SLinus Torvalds * Alan Cox : Nonblocking error fix. 511da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 521da177e4SLinus Torvalds * Mike McLagan : Routing by source 531da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 541da177e4SLinus Torvalds * Last socket cache retained as it 551da177e4SLinus Torvalds * does have a high hit rate. 561da177e4SLinus Torvalds * Olaf Kirch : Don't linearise iovec on sendmsg. 571da177e4SLinus Torvalds * Andi Kleen : Some cleanups, cache destination entry 581da177e4SLinus Torvalds * for connect. 591da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 601da177e4SLinus Torvalds * Melvin Smith : Check msg_name not msg_namelen in sendto(), 611da177e4SLinus Torvalds * return ENOTCONN for unconnected sockets (POSIX) 621da177e4SLinus Torvalds * Janos Farkas : don't deliver multi/broadcasts to a different 631da177e4SLinus Torvalds * bound-to-device socket 641da177e4SLinus Torvalds * Hirokazu Takahashi : HW checksumming for outgoing UDP 651da177e4SLinus Torvalds * datagrams. 661da177e4SLinus Torvalds * Hirokazu Takahashi : sendfile() on UDP works now. 671da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/udp to seq_file 681da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 691da177e4SLinus Torvalds * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 701da177e4SLinus Torvalds * a single port at the same time. 711da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support 72342f0234SJames Chapman * James Chapman : Add L2TP encapsulation type. 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds 75afd46503SJoe Perches #define pr_fmt(fmt) "UDP: " fmt 76afd46503SJoe Perches 77aef2fedaSJakub Kicinski #include <linux/bpf-cgroup.h> 787c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 791da177e4SLinus Torvalds #include <asm/ioctls.h> 8057c8a661SMike Rapoport #include <linux/memblock.h> 818203efb3SEric Dumazet #include <linux/highmem.h> 821da177e4SLinus Torvalds #include <linux/types.h> 831da177e4SLinus Torvalds #include <linux/fcntl.h> 841da177e4SLinus Torvalds #include <linux/module.h> 851da177e4SLinus Torvalds #include <linux/socket.h> 861da177e4SLinus Torvalds #include <linux/sockios.h> 8714c85021SArnaldo Carvalho de Melo #include <linux/igmp.h> 886e540309SShawn Bohrer #include <linux/inetdevice.h> 891da177e4SLinus Torvalds #include <linux/in.h> 901da177e4SLinus Torvalds #include <linux/errno.h> 911da177e4SLinus Torvalds #include <linux/timer.h> 921da177e4SLinus Torvalds #include <linux/mm.h> 931da177e4SLinus Torvalds #include <linux/inet.h> 941da177e4SLinus Torvalds #include <linux/netdevice.h> 955a0e3ad6STejun Heo #include <linux/slab.h> 96c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 971da177e4SLinus Torvalds #include <linux/skbuff.h> 981da177e4SLinus Torvalds #include <linux/proc_fs.h> 991da177e4SLinus Torvalds #include <linux/seq_file.h> 100457c4cbcSEric W. Biederman #include <net/net_namespace.h> 1011da177e4SLinus Torvalds #include <net/icmp.h> 102421b3885SShawn Bohrer #include <net/inet_hashtables.h> 103e7cc0824SStefano Brivio #include <net/ip_tunnels.h> 1041da177e4SLinus Torvalds #include <net/route.h> 1051da177e4SLinus Torvalds #include <net/checksum.h> 106d457a0e3SEric Dumazet #include <net/gso.h> 1071da177e4SLinus Torvalds #include <net/xfrm.h> 108296f7ea7SSatoru Moriya #include <trace/events/udp.h> 109447167bfSEric Dumazet #include <linux/static_key.h> 110951cf368SYonghong Song #include <linux/btf_ids.h> 11122911fc5SEric Dumazet #include <trace/events/skb.h> 112076bb0c8SEliezer Tamir #include <net/busy_poll.h> 113ba4e58ecSGerrit Renker #include "udp_impl.h" 114e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 115217375a0SEric Dumazet #include <net/addrconf.h> 11660fb9567SPaolo Abeni #include <net/udp_tunnel.h> 1177938cd15SRichard Gobert #include <net/gro.h> 1180146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 1190146dca7SSabrina Dubroca #include <net/ipv6_stubs.h> 1200146dca7SSabrina Dubroca #endif 1211da177e4SLinus Torvalds 122f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly; 123645ca708SEric Dumazet EXPORT_SYMBOL(udp_table); 1241da177e4SLinus Torvalds 1258d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly; 12695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 127c482c568SEric Dumazet 12891b6d325SEric Dumazet atomic_long_t udp_memory_allocated ____cacheline_aligned_in_smp; 12995766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 1300defbb0aSEric Dumazet DEFINE_PER_CPU(int, udp_memory_per_cpu_fw_alloc); 1310defbb0aSEric Dumazet EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc); 13295766fffSHideo Aoki 133f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536 1349804985bSKuniyuki Iwashima #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN_PERNET) 13598322f22SEric Dumazet 13667fb4330SKuniyuki Iwashima static struct udp_table *udp_get_table_prot(struct sock *sk) 13767fb4330SKuniyuki Iwashima { 13867fb4330SKuniyuki Iwashima return sk->sk_prot->h.udp_table ? : sock_net(sk)->ipv4.udp_table; 13967fb4330SKuniyuki Iwashima } 14067fb4330SKuniyuki Iwashima 141f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 142645ca708SEric Dumazet const struct udp_hslot *hslot, 14398322f22SEric Dumazet unsigned long *bitmap, 144fe38d2a1SJosef Bacik struct sock *sk, unsigned int log) 14525030a7fSGerrit Renker { 146f24d43c0SEric Dumazet struct sock *sk2; 147ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 14825030a7fSGerrit Renker 149ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 150f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 151f24d43c0SEric Dumazet sk2 != sk && 152d4cada4aSEric Dumazet (bitmap || udp_sk(sk2)->udp_port_hash == num) && 153f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1549d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1559d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 156fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 157df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 158df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 159df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 160df560056SEric Garver if (!bitmap) 161df560056SEric Garver return 0; 162df560056SEric Garver } else { 1634243cdc2SJoe Perches if (!bitmap) 164fc038410SDavid S. Miller return 1; 165df560056SEric Garver __set_bit(udp_sk(sk2)->udp_port_hash >> log, 166df560056SEric Garver bitmap); 167df560056SEric Garver } 1684243cdc2SJoe Perches } 16998322f22SEric Dumazet } 17025030a7fSGerrit Renker return 0; 17125030a7fSGerrit Renker } 17225030a7fSGerrit Renker 17330fff923SEric Dumazet /* 17430fff923SEric Dumazet * Note: we still hold spinlock of primary hash chain, so no other writer 17530fff923SEric Dumazet * can insert/delete a socket with local_port == num 17630fff923SEric Dumazet */ 17730fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num, 17830fff923SEric Dumazet struct udp_hslot *hslot2, 179fe38d2a1SJosef Bacik struct sock *sk) 18030fff923SEric Dumazet { 18130fff923SEric Dumazet struct sock *sk2; 182ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 18330fff923SEric Dumazet int res = 0; 18430fff923SEric Dumazet 18530fff923SEric Dumazet spin_lock(&hslot2->lock); 186ca065d0cSEric Dumazet udp_portaddr_for_each_entry(sk2, &hslot2->head) { 18730fff923SEric Dumazet if (net_eq(sock_net(sk2), net) && 18830fff923SEric Dumazet sk2 != sk && 18930fff923SEric Dumazet (udp_sk(sk2)->udp_port_hash == num) && 19030fff923SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1919d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1929d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 193fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 194df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 195df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 196df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 197df560056SEric Garver res = 0; 198df560056SEric Garver } else { 19930fff923SEric Dumazet res = 1; 200df560056SEric Garver } 20130fff923SEric Dumazet break; 20230fff923SEric Dumazet } 2034243cdc2SJoe Perches } 20430fff923SEric Dumazet spin_unlock(&hslot2->lock); 20530fff923SEric Dumazet return res; 20630fff923SEric Dumazet } 20730fff923SEric Dumazet 208fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 209e32ea7e7SCraig Gallek { 210e32ea7e7SCraig Gallek struct net *net = sock_net(sk); 211e32ea7e7SCraig Gallek kuid_t uid = sock_i_uid(sk); 212e32ea7e7SCraig Gallek struct sock *sk2; 213e32ea7e7SCraig Gallek 214ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 215e32ea7e7SCraig Gallek if (net_eq(sock_net(sk2), net) && 216e32ea7e7SCraig Gallek sk2 != sk && 217e32ea7e7SCraig Gallek sk2->sk_family == sk->sk_family && 218e32ea7e7SCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 219e32ea7e7SCraig Gallek (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 220e32ea7e7SCraig Gallek (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 221e32ea7e7SCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 222fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) { 2232dbb9b9eSMartin KaFai Lau return reuseport_add_sock(sk, sk2, 2242dbb9b9eSMartin KaFai Lau inet_rcv_saddr_any(sk)); 225e32ea7e7SCraig Gallek } 226e32ea7e7SCraig Gallek } 227e32ea7e7SCraig Gallek 2282dbb9b9eSMartin KaFai Lau return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 229e32ea7e7SCraig Gallek } 230e32ea7e7SCraig Gallek 23125030a7fSGerrit Renker /** 2326ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 23325030a7fSGerrit Renker * 23425030a7fSGerrit Renker * @sk: socket struct in question 23525030a7fSGerrit Renker * @snum: port number to look up 23625985edcSLucas De Marchi * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 23730fff923SEric Dumazet * with NULL address 23825030a7fSGerrit Renker */ 2396ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 24030fff923SEric Dumazet unsigned int hash2_nulladdr) 2411da177e4SLinus Torvalds { 24267fb4330SKuniyuki Iwashima struct udp_table *udptable = udp_get_table_prot(sk); 243919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot, *hslot2; 2443b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 2457a7160edSKuniyuki Iwashima int error = -EADDRINUSE; 2461da177e4SLinus Torvalds 24732c1da70SStephen Hemminger if (!snum) { 248919dfa0bSKuniyuki Iwashima DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 249919dfa0bSKuniyuki Iwashima unsigned short first, last; 2509088c560SEric Dumazet int low, high, remaining; 25195c96174SEric Dumazet unsigned int rand; 2521da177e4SLinus Torvalds 25391d0b78cSJakub Sitnicki inet_sk_get_local_port_range(sk, &low, &high); 254a25de534SAnton Arapov remaining = (high - low) + 1; 255227b60f5SStephen Hemminger 256a251c17aSJason A. Donenfeld rand = get_random_u32(); 2578fc54f68SDaniel Borkmann first = reciprocal_scale(rand, remaining) + low; 25898322f22SEric Dumazet /* 25998322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 26098322f22SEric Dumazet */ 261f86dcc5aSEric Dumazet rand = (rand | 1) * (udptable->mask + 1); 2625781b235SEric Dumazet last = first + udptable->mask + 1; 2635781b235SEric Dumazet do { 264f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, first); 26598322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 266645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 26798322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 268fe38d2a1SJosef Bacik udptable->log); 26998322f22SEric Dumazet 27098322f22SEric Dumazet snum = first; 27198322f22SEric Dumazet /* 27298322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 27398322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 27498322f22SEric Dumazet * give us randomization and full range coverage. 27598322f22SEric Dumazet */ 2769088c560SEric Dumazet do { 27798322f22SEric Dumazet if (low <= snum && snum <= high && 278e3826f1eSAmerigo Wang !test_bit(snum >> udptable->log, bitmap) && 279122ff243SWANG Cong !inet_is_local_reserved_port(net, snum)) 28098322f22SEric Dumazet goto found; 28198322f22SEric Dumazet snum += rand; 28298322f22SEric Dumazet } while (snum != first); 28398322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 284df560056SEric Garver cond_resched(); 2855781b235SEric Dumazet } while (++first != last); 28698322f22SEric Dumazet goto fail; 287645ca708SEric Dumazet } else { 288f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, snum); 289645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 29030fff923SEric Dumazet if (hslot->count > 10) { 29130fff923SEric Dumazet int exist; 29230fff923SEric Dumazet unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 29330fff923SEric Dumazet 29430fff923SEric Dumazet slot2 &= udptable->mask; 29530fff923SEric Dumazet hash2_nulladdr &= udptable->mask; 29630fff923SEric Dumazet 29730fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, slot2); 29830fff923SEric Dumazet if (hslot->count < hslot2->count) 29930fff923SEric Dumazet goto scan_primary_hash; 30030fff923SEric Dumazet 301fe38d2a1SJosef Bacik exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 30230fff923SEric Dumazet if (!exist && (hash2_nulladdr != slot2)) { 30330fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 30430fff923SEric Dumazet exist = udp_lib_lport_inuse2(net, snum, hslot2, 305fe38d2a1SJosef Bacik sk); 30630fff923SEric Dumazet } 30730fff923SEric Dumazet if (exist) 30830fff923SEric Dumazet goto fail_unlock; 30930fff923SEric Dumazet else 31030fff923SEric Dumazet goto found; 31130fff923SEric Dumazet } 31230fff923SEric Dumazet scan_primary_hash: 313fe38d2a1SJosef Bacik if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 314645ca708SEric Dumazet goto fail_unlock; 315645ca708SEric Dumazet } 31698322f22SEric Dumazet found: 317c720c7e8SEric Dumazet inet_sk(sk)->inet_num = snum; 318d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash = snum; 319d4cada4aSEric Dumazet udp_sk(sk)->udp_portaddr_hash ^= snum; 3201da177e4SLinus Torvalds if (sk_unhashed(sk)) { 321e32ea7e7SCraig Gallek if (sk->sk_reuseport && 322fe38d2a1SJosef Bacik udp_reuseport_add_sock(sk, hslot)) { 323e32ea7e7SCraig Gallek inet_sk(sk)->inet_num = 0; 324e32ea7e7SCraig Gallek udp_sk(sk)->udp_port_hash = 0; 325e32ea7e7SCraig Gallek udp_sk(sk)->udp_portaddr_hash ^= snum; 326e32ea7e7SCraig Gallek goto fail_unlock; 327e32ea7e7SCraig Gallek } 328e32ea7e7SCraig Gallek 329ca065d0cSEric Dumazet sk_add_node_rcu(sk, &hslot->head); 330fdcc8aa9SEric Dumazet hslot->count++; 331c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 332512615b6SEric Dumazet 333512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 334512615b6SEric Dumazet spin_lock(&hslot2->lock); 335d894ba18SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 336d894ba18SCraig Gallek sk->sk_family == AF_INET6) 3371602f49bSDavid S. Miller hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 338d894ba18SCraig Gallek &hslot2->head); 339d894ba18SCraig Gallek else 340ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 341512615b6SEric Dumazet &hslot2->head); 342512615b6SEric Dumazet hslot2->count++; 343512615b6SEric Dumazet spin_unlock(&hslot2->lock); 3441da177e4SLinus Torvalds } 345ca065d0cSEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 34625030a7fSGerrit Renker error = 0; 347645ca708SEric Dumazet fail_unlock: 348645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 3491da177e4SLinus Torvalds fail: 35025030a7fSGerrit Renker return error; 3511da177e4SLinus Torvalds } 352c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port); 3531da177e4SLinus Torvalds 3546ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 355db8dac20SDavid S. Miller { 35630fff923SEric Dumazet unsigned int hash2_nulladdr = 357f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 35830fff923SEric Dumazet unsigned int hash2_partial = 359f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 36030fff923SEric Dumazet 361d4cada4aSEric Dumazet /* precompute partial secondary hash */ 36230fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 363fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 364db8dac20SDavid S. Miller } 365db8dac20SDavid S. Miller 366d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 367d1e37288SSu, Xuemin __be32 saddr, __be16 sport, 368fb74c277SDavid Ahern __be32 daddr, unsigned short hnum, 36973545373STim Beale int dif, int sdif) 370645ca708SEric Dumazet { 37160c04aecSJoe Perches int score; 37260c04aecSJoe Perches struct inet_sock *inet; 3736da5b0f0SMike Manning bool dev_match; 374645ca708SEric Dumazet 37560c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 37660c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 37760c04aecSJoe Perches ipv6_only_sock(sk)) 37860c04aecSJoe Perches return -1; 379645ca708SEric Dumazet 3804cdeeee9SPeter Oskolkov if (sk->sk_rcv_saddr != daddr) 381645ca708SEric Dumazet return -1; 38260c04aecSJoe Perches 3834cdeeee9SPeter Oskolkov score = (sk->sk_family == PF_INET) ? 2 : 1; 3844cdeeee9SPeter Oskolkov 3854cdeeee9SPeter Oskolkov inet = inet_sk(sk); 386c720c7e8SEric Dumazet if (inet->inet_daddr) { 387c720c7e8SEric Dumazet if (inet->inet_daddr != saddr) 388645ca708SEric Dumazet return -1; 389ba418fa3STom Herbert score += 4; 390645ca708SEric Dumazet } 39160c04aecSJoe Perches 392c720c7e8SEric Dumazet if (inet->inet_dport) { 393c720c7e8SEric Dumazet if (inet->inet_dport != sport) 394645ca708SEric Dumazet return -1; 395ba418fa3STom Herbert score += 4; 396645ca708SEric Dumazet } 39760c04aecSJoe Perches 3986da5b0f0SMike Manning dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 3996da5b0f0SMike Manning dif, sdif); 40069678bcdSPaolo Abeni if (!dev_match) 401645ca708SEric Dumazet return -1; 4028d6c414cSMike Manning if (sk->sk_bound_dev_if) 403ba418fa3STom Herbert score += 4; 404fb74c277SDavid Ahern 4057170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 40670da268bSEric Dumazet score++; 407645ca708SEric Dumazet return score; 408645ca708SEric Dumazet } 409645ca708SEric Dumazet 4100f495f76SLorenz Bauer INDIRECT_CALLABLE_SCOPE 4110f495f76SLorenz Bauer u32 udp_ehashfn(const struct net *net, const __be32 laddr, const __u16 lport, 4120f495f76SLorenz Bauer const __be32 faddr, const __be16 fport) 41365cd8033SHannes Frederic Sowa { 4141bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4151bbdceefSHannes Frederic Sowa 4161bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4171bbdceefSHannes Frederic Sowa 41865cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4191bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 42065cd8033SHannes Frederic Sowa } 42165cd8033SHannes Frederic Sowa 422d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4235051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4245051ebd2SEric Dumazet __be32 saddr, __be16 sport, 425fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 42673545373STim Beale int dif, int sdif, 427d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4281134158bSCraig Gallek struct sk_buff *skb) 4295051ebd2SEric Dumazet { 4305051ebd2SEric Dumazet struct sock *sk, *result; 431e94a62f5SPaolo Abeni int score, badness; 4325051ebd2SEric Dumazet 4335051ebd2SEric Dumazet result = NULL; 434ba418fa3STom Herbert badness = 0; 435ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 436d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 43773545373STim Beale daddr, hnum, dif, sdif); 4385051ebd2SEric Dumazet if (score > badness) { 439ca065d0cSEric Dumazet badness = score; 4400f495f76SLorenz Bauer 4410f495f76SLorenz Bauer if (sk->sk_state == TCP_ESTABLISHED) { 4420f495f76SLorenz Bauer result = sk; 4430f495f76SLorenz Bauer continue; 4440f495f76SLorenz Bauer } 4450f495f76SLorenz Bauer 4460f495f76SLorenz Bauer result = inet_lookup_reuseport(net, sk, skb, sizeof(struct udphdr), 4470f495f76SLorenz Bauer saddr, sport, daddr, hnum, udp_ehashfn); 448f0ea27e7SLorenz Bauer if (!result) { 449f0ea27e7SLorenz Bauer result = sk; 450f0ea27e7SLorenz Bauer continue; 451f0ea27e7SLorenz Bauer } 452f0ea27e7SLorenz Bauer 453e32ea7e7SCraig Gallek /* Fall back to scoring if group has connections */ 454f0ea27e7SLorenz Bauer if (!reuseport_has_conns(sk)) 455ba418fa3STom Herbert return result; 456ba418fa3STom Herbert 457f0ea27e7SLorenz Bauer /* Reuseport logic returned an error, keep original score. */ 458f0ea27e7SLorenz Bauer if (IS_ERR(result)) 459f0ea27e7SLorenz Bauer continue; 460f0ea27e7SLorenz Bauer 461f0ea27e7SLorenz Bauer badness = compute_score(result, net, saddr, sport, 462f0ea27e7SLorenz Bauer daddr, hnum, dif, sdif); 463f0ea27e7SLorenz Bauer 4645051ebd2SEric Dumazet } 4655051ebd2SEric Dumazet } 4665051ebd2SEric Dumazet return result; 4675051ebd2SEric Dumazet } 4685051ebd2SEric Dumazet 469db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 470db8dac20SDavid S. Miller * harder than this. -DaveM 471db8dac20SDavid S. Miller */ 472fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 473fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 474fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 475db8dac20SDavid S. Miller { 476db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4774cdeeee9SPeter Oskolkov unsigned int hash2, slot2; 4784cdeeee9SPeter Oskolkov struct udp_hslot *hslot2; 47972f7e944SJakub Sitnicki struct sock *result, *sk; 480db8dac20SDavid S. Miller 481f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 4825051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4835051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4845051ebd2SEric Dumazet 48572f7e944SJakub Sitnicki /* Lookup connected or non-wildcard socket */ 4865051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 487fb74c277SDavid Ahern daddr, hnum, dif, sdif, 48873545373STim Beale hslot2, skb); 48972f7e944SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 49072f7e944SJakub Sitnicki goto done; 49172f7e944SJakub Sitnicki 49272f7e944SJakub Sitnicki /* Lookup redirect from BPF */ 4936c886db2SLorenz Bauer if (static_branch_unlikely(&bpf_sk_lookup_enabled) && 4946c886db2SLorenz Bauer udptable == net->ipv4.udp_table) { 4956c886db2SLorenz Bauer sk = inet_lookup_run_sk_lookup(net, IPPROTO_UDP, skb, sizeof(struct udphdr), 4966c886db2SLorenz Bauer saddr, sport, daddr, hnum, dif, 4976c886db2SLorenz Bauer udp_ehashfn); 49872f7e944SJakub Sitnicki if (sk) { 49972f7e944SJakub Sitnicki result = sk; 50072f7e944SJakub Sitnicki goto done; 50172f7e944SJakub Sitnicki } 50272f7e944SJakub Sitnicki } 50372f7e944SJakub Sitnicki 50472f7e944SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 50572f7e944SJakub Sitnicki if (result) 50672f7e944SJakub Sitnicki goto done; 50772f7e944SJakub Sitnicki 50872f7e944SJakub Sitnicki /* Lookup wildcard sockets */ 509f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 5105051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 5115051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 5125051ebd2SEric Dumazet 5131223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 5144cdeeee9SPeter Oskolkov htonl(INADDR_ANY), hnum, dif, sdif, 51573545373STim Beale hslot2, skb); 51672f7e944SJakub Sitnicki done: 51788e235b8SEnrico Weigelt if (IS_ERR(result)) 5188217ca65SMartin KaFai Lau return NULL; 5195051ebd2SEric Dumazet return result; 5205051ebd2SEric Dumazet } 521fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 522db8dac20SDavid S. Miller 523607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 524607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 525645ca708SEric Dumazet struct udp_table *udptable) 526607c4aafSKOVACS Krisztian { 527607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 528607c4aafSKOVACS Krisztian 529ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 530607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 531fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 532607c4aafSKOVACS Krisztian } 533607c4aafSKOVACS Krisztian 5347b58e63eSEric Dumazet struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb, 53563058308STom Herbert __be16 sport, __be16 dport) 53663058308STom Herbert { 537257a525fSMartin KaFai Lau const struct iphdr *iph = ip_hdr(skb); 538ba6aac15SKuniyuki Iwashima struct net *net = dev_net(skb->dev); 5397938cd15SRichard Gobert int iif, sdif; 5407938cd15SRichard Gobert 5417938cd15SRichard Gobert inet_get_iif_sdif(skb, &iif, &sdif); 542257a525fSMartin KaFai Lau 543ba6aac15SKuniyuki Iwashima return __udp4_lib_lookup(net, iph->saddr, sport, 5447938cd15SRichard Gobert iph->daddr, dport, iif, 5457938cd15SRichard Gobert sdif, net->ipv4.udp_table, NULL); 54663058308STom Herbert } 54763058308STom Herbert 548ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 549ca065d0cSEric Dumazet * Does increment socket refcount. 550ca065d0cSEric Dumazet */ 5516e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 552bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 553bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 554bcd41303SKOVACS Krisztian { 555ca065d0cSEric Dumazet struct sock *sk; 556ca065d0cSEric Dumazet 557ca065d0cSEric Dumazet sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 558ba6aac15SKuniyuki Iwashima dif, 0, net->ipv4.udp_table, NULL); 55941c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 560ca065d0cSEric Dumazet sk = NULL; 561ca065d0cSEric Dumazet return sk; 562bcd41303SKOVACS Krisztian } 563bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 564ca065d0cSEric Dumazet #endif 565bcd41303SKOVACS Krisztian 566a0a989d3SEric Dumazet static inline bool __udp_is_mcast_sock(struct net *net, const struct sock *sk, 567421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 568421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 569fb74c277SDavid Ahern int dif, int sdif, unsigned short hnum) 570421b3885SShawn Bohrer { 571a0a989d3SEric Dumazet const struct inet_sock *inet = inet_sk(sk); 572421b3885SShawn Bohrer 573421b3885SShawn Bohrer if (!net_eq(sock_net(sk), net) || 574421b3885SShawn Bohrer udp_sk(sk)->udp_port_hash != hnum || 575421b3885SShawn Bohrer (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 576421b3885SShawn Bohrer (inet->inet_dport != rmt_port && inet->inet_dport) || 577421b3885SShawn Bohrer (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 578421b3885SShawn Bohrer ipv6_only_sock(sk) || 57982ba25c6STim Beale !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 580421b3885SShawn Bohrer return false; 58160d9b031SDavid Ahern if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 582421b3885SShawn Bohrer return false; 583421b3885SShawn Bohrer return true; 584421b3885SShawn Bohrer } 585421b3885SShawn Bohrer 586a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 587a36e185eSStefano Brivio void udp_encap_enable(void) 588a36e185eSStefano Brivio { 5899c480601SPaolo Abeni static_branch_inc(&udp_encap_needed_key); 590a36e185eSStefano Brivio } 591a36e185eSStefano Brivio EXPORT_SYMBOL(udp_encap_enable); 592a36e185eSStefano Brivio 593a4a600ddSXin Long void udp_encap_disable(void) 594a4a600ddSXin Long { 595a4a600ddSXin Long static_branch_dec(&udp_encap_needed_key); 596a4a600ddSXin Long } 597a4a600ddSXin Long EXPORT_SYMBOL(udp_encap_disable); 598a4a600ddSXin Long 599e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 600e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 601e7cc0824SStefano Brivio */ 602e7cc0824SStefano Brivio static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 603e7cc0824SStefano Brivio { 604e7cc0824SStefano Brivio int i; 605e7cc0824SStefano Brivio 606e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 607e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, u32 info); 60892b95364SPaolo Abeni const struct ip_tunnel_encap_ops *encap; 609e7cc0824SStefano Brivio 61092b95364SPaolo Abeni encap = rcu_dereference(iptun_encaps[i]); 61192b95364SPaolo Abeni if (!encap) 612e7cc0824SStefano Brivio continue; 61392b95364SPaolo Abeni handler = encap->err_handler; 614e7cc0824SStefano Brivio if (handler && !handler(skb, info)) 615e7cc0824SStefano Brivio return 0; 616e7cc0824SStefano Brivio } 617e7cc0824SStefano Brivio 618e7cc0824SStefano Brivio return -ENOENT; 619e7cc0824SStefano Brivio } 620e7cc0824SStefano Brivio 621a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 622a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 623a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 624a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 625a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 626a36e185eSStefano Brivio * trace ICMP messages back to them. 627a36e185eSStefano Brivio * 628e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 629e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 630e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 631e7cc0824SStefano Brivio * 632a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 633a36e185eSStefano Brivio * association. 634a36e185eSStefano Brivio * 635e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 636e7cc0824SStefano Brivio * processing, zero otherwise. 637a36e185eSStefano Brivio */ 638a36e185eSStefano Brivio static struct sock *__udp4_lib_err_encap(struct net *net, 639a36e185eSStefano Brivio const struct iphdr *iph, 640a36e185eSStefano Brivio struct udphdr *uh, 641a36e185eSStefano Brivio struct udp_table *udptable, 6429bfce73cSVadim Fedorenko struct sock *sk, 643e7cc0824SStefano Brivio struct sk_buff *skb, u32 info) 644a36e185eSStefano Brivio { 6459bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb); 646a36e185eSStefano Brivio int network_offset, transport_offset; 6479bfce73cSVadim Fedorenko struct udp_sock *up; 648a36e185eSStefano Brivio 649a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 650a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 651a36e185eSStefano Brivio 652a36e185eSStefano Brivio /* Network header needs to point to the outer IPv4 header inside ICMP */ 653a36e185eSStefano Brivio skb_reset_network_header(skb); 654a36e185eSStefano Brivio 655a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 656a36e185eSStefano Brivio skb_set_transport_header(skb, iph->ihl << 2); 657a36e185eSStefano Brivio 6589bfce73cSVadim Fedorenko if (sk) { 6599bfce73cSVadim Fedorenko up = udp_sk(sk); 6609bfce73cSVadim Fedorenko 6619bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup); 6629bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb)) 6639bfce73cSVadim Fedorenko sk = NULL; 6649bfce73cSVadim Fedorenko 6659bfce73cSVadim Fedorenko goto out; 6669bfce73cSVadim Fedorenko } 6679bfce73cSVadim Fedorenko 668e7cc0824SStefano Brivio sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 669e7cc0824SStefano Brivio iph->saddr, uh->dest, skb->dev->ifindex, 0, 670e7cc0824SStefano Brivio udptable, NULL); 671e7cc0824SStefano Brivio if (sk) { 6729bfce73cSVadim Fedorenko up = udp_sk(sk); 673e7cc0824SStefano Brivio 674a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 675a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 676a36e185eSStefano Brivio sk = NULL; 677e7cc0824SStefano Brivio } 678e7cc0824SStefano Brivio 6799bfce73cSVadim Fedorenko out: 680e7cc0824SStefano Brivio if (!sk) 681e7cc0824SStefano Brivio sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 682a36e185eSStefano Brivio 683a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 684a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 685a36e185eSStefano Brivio 686a36e185eSStefano Brivio return sk; 687a36e185eSStefano Brivio } 688a36e185eSStefano Brivio 689db8dac20SDavid S. Miller /* 690db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 691db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 692db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 693db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 694db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 695db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 696db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 697db8dac20SDavid S. Miller * to find the appropriate port. 698db8dac20SDavid S. Miller */ 699db8dac20SDavid S. Miller 70032bbd879SStefano Brivio int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 701db8dac20SDavid S. Miller { 702db8dac20SDavid S. Miller struct inet_sock *inet; 703b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 704db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 705db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 706db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 707a36e185eSStefano Brivio bool tunnel = false; 708db8dac20SDavid S. Miller struct sock *sk; 709db8dac20SDavid S. Miller int harderr; 710db8dac20SDavid S. Miller int err; 711fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 712db8dac20SDavid S. Miller 713fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 714f64bf6b8SMike Manning iph->saddr, uh->source, skb->dev->ifindex, 715f64bf6b8SMike Manning inet_sdif(skb), udptable, NULL); 7169bfce73cSVadim Fedorenko 71770a36f57SEric Dumazet if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) { 718a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 719e7cc0824SStefano Brivio if (static_branch_unlikely(&udp_encap_needed_key)) { 7209bfce73cSVadim Fedorenko sk = __udp4_lib_err_encap(net, iph, uh, udptable, sk, skb, 721e7cc0824SStefano Brivio info); 722e7cc0824SStefano Brivio if (!sk) 723e7cc0824SStefano Brivio return 0; 7249bfce73cSVadim Fedorenko } else 7259bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT); 726e7cc0824SStefano Brivio 727e7cc0824SStefano Brivio if (IS_ERR(sk)) { 728e7cc0824SStefano Brivio __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 729e7cc0824SStefano Brivio return PTR_ERR(sk); 730e7cc0824SStefano Brivio } 731e7cc0824SStefano Brivio 732a36e185eSStefano Brivio tunnel = true; 733db8dac20SDavid S. Miller } 734db8dac20SDavid S. Miller 735db8dac20SDavid S. Miller err = 0; 736db8dac20SDavid S. Miller harderr = 0; 737db8dac20SDavid S. Miller inet = inet_sk(sk); 738db8dac20SDavid S. Miller 739db8dac20SDavid S. Miller switch (type) { 740db8dac20SDavid S. Miller default: 741db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 742db8dac20SDavid S. Miller err = EHOSTUNREACH; 743db8dac20SDavid S. Miller break; 744db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 745db8dac20SDavid S. Miller goto out; 746db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 747db8dac20SDavid S. Miller err = EPROTO; 748db8dac20SDavid S. Miller harderr = 1; 749db8dac20SDavid S. Miller break; 750db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 751db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 75236393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 753ceaa7141SEric Dumazet if (READ_ONCE(inet->pmtudisc) != IP_PMTUDISC_DONT) { 754db8dac20SDavid S. Miller err = EMSGSIZE; 755db8dac20SDavid S. Miller harderr = 1; 756db8dac20SDavid S. Miller break; 757db8dac20SDavid S. Miller } 758db8dac20SDavid S. Miller goto out; 759db8dac20SDavid S. Miller } 760db8dac20SDavid S. Miller err = EHOSTUNREACH; 761db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 762db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 763db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 764db8dac20SDavid S. Miller } 765db8dac20SDavid S. Miller break; 76655be7a9cSDavid S. Miller case ICMP_REDIRECT: 76755be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 7681a462d18SDuan Jiong goto out; 769db8dac20SDavid S. Miller } 770db8dac20SDavid S. Miller 771db8dac20SDavid S. Miller /* 772db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 773db8dac20SDavid S. Miller * 4.1.3.3. 774db8dac20SDavid S. Miller */ 775a36e185eSStefano Brivio if (tunnel) { 776a36e185eSStefano Brivio /* ...not for tunnels though: we don't have a sending socket */ 777ac56a0b4SDavid Howells if (udp_sk(sk)->encap_err_rcv) 77842fb06b3SDavid Howells udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest, info, 77942fb06b3SDavid Howells (u8 *)(uh+1)); 780a36e185eSStefano Brivio goto out; 781a36e185eSStefano Brivio } 7826b5f43eaSEric Dumazet if (!inet_test_bit(RECVERR, sk)) { 783db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 784db8dac20SDavid S. Miller goto out; 785b1faf566SEric Dumazet } else 786db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 787b1faf566SEric Dumazet 788db8dac20SDavid S. Miller sk->sk_err = err; 789e3ae2365SAlexander Aring sk_error_report(sk); 790db8dac20SDavid S. Miller out: 79132bbd879SStefano Brivio return 0; 792db8dac20SDavid S. Miller } 793db8dac20SDavid S. Miller 79432bbd879SStefano Brivio int udp_err(struct sk_buff *skb, u32 info) 795db8dac20SDavid S. Miller { 796ba6aac15SKuniyuki Iwashima return __udp4_lib_err(skb, info, dev_net(skb->dev)->ipv4.udp_table); 797db8dac20SDavid S. Miller } 798db8dac20SDavid S. Miller 799db8dac20SDavid S. Miller /* 800db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 801db8dac20SDavid S. Miller */ 80236d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 803db8dac20SDavid S. Miller { 804db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 805db8dac20SDavid S. Miller 806db8dac20SDavid S. Miller if (up->pending) { 807db8dac20SDavid S. Miller up->len = 0; 808482521d8SEric Dumazet WRITE_ONCE(up->pending, 0); 809db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 810db8dac20SDavid S. Miller } 811db8dac20SDavid S. Miller } 81236d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 813db8dac20SDavid S. Miller 814db8dac20SDavid S. Miller /** 815f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 816db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 817db8dac20SDavid S. Miller * (checksum field must be zeroed out) 818f6b9664fSHerbert Xu * @src: source IP address 819f6b9664fSHerbert Xu * @dst: destination IP address 820db8dac20SDavid S. Miller */ 821c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 822db8dac20SDavid S. Miller { 823db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 824f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 825f6b9664fSHerbert Xu int len = skb->len - offset; 826f6b9664fSHerbert Xu int hlen = len; 827db8dac20SDavid S. Miller __wsum csum = 0; 828db8dac20SDavid S. Miller 829ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 830db8dac20SDavid S. Miller /* 831db8dac20SDavid S. Miller * Only one fragment on the socket. 832db8dac20SDavid S. Miller */ 833db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 834db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 835f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 836f6b9664fSHerbert Xu IPPROTO_UDP, 0); 837db8dac20SDavid S. Miller } else { 838ebbe495fSWANG Cong struct sk_buff *frags; 839ebbe495fSWANG Cong 840db8dac20SDavid S. Miller /* 841db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 842db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 843db8dac20SDavid S. Miller * should be together 844db8dac20SDavid S. Miller */ 845ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 846f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 847f6b9664fSHerbert Xu hlen -= frags->len; 848ebbe495fSWANG Cong } 849db8dac20SDavid S. Miller 850f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 851db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 852db8dac20SDavid S. Miller 853db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 854db8dac20SDavid S. Miller if (uh->check == 0) 855db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 856db8dac20SDavid S. Miller } 857db8dac20SDavid S. Miller } 858c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 859db8dac20SDavid S. Miller 860af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 861af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 862af5fcba7STom Herbert */ 863af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 864af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 865af5fcba7STom Herbert { 866af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 867af5fcba7STom Herbert 868179bc67fSEdward Cree if (nocheck) { 869af5fcba7STom Herbert uh->check = 0; 870179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 871af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 872179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 873179bc67fSEdward Cree uh->check = 0; 874179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 875179bc67fSEdward Cree if (uh->check == 0) 876179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 877d75f1306SEdward Cree } else { 878af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 879af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 880af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 881af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 882af5fcba7STom Herbert } 883af5fcba7STom Herbert } 884af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 885af5fcba7STom Herbert 886bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 887bec1f6f6SWillem de Bruijn struct inet_cork *cork) 888f6b9664fSHerbert Xu { 889f6b9664fSHerbert Xu struct sock *sk = skb->sk; 890f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 891f6b9664fSHerbert Xu struct udphdr *uh; 892cffb8f61SMenglong Dong int err; 893f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 894f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 895f6b9664fSHerbert Xu int len = skb->len - offset; 8964094871dSJosh Hunt int datalen = len - sizeof(*uh); 897f6b9664fSHerbert Xu __wsum csum = 0; 898f6b9664fSHerbert Xu 899f6b9664fSHerbert Xu /* 900f6b9664fSHerbert Xu * Create a UDP header 901f6b9664fSHerbert Xu */ 902f6b9664fSHerbert Xu uh = udp_hdr(skb); 903f6b9664fSHerbert Xu uh->source = inet->inet_sport; 90479ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 905f6b9664fSHerbert Xu uh->len = htons(len); 906f6b9664fSHerbert Xu uh->check = 0; 907f6b9664fSHerbert Xu 908bec1f6f6SWillem de Bruijn if (cork->gso_size) { 909bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 910bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 911bec1f6f6SWillem de Bruijn 9120f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 9130f149c9fSWillem de Bruijn kfree_skb(skb); 914bec1f6f6SWillem de Bruijn return -EINVAL; 9150f149c9fSWillem de Bruijn } 916158390e4SJianguo Wu if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 9170f149c9fSWillem de Bruijn kfree_skb(skb); 918bec1f6f6SWillem de Bruijn return -EINVAL; 9190f149c9fSWillem de Bruijn } 9200f149c9fSWillem de Bruijn if (sk->sk_no_check_tx) { 9210f149c9fSWillem de Bruijn kfree_skb(skb); 922a8c744a8SWillem de Bruijn return -EINVAL; 9230f149c9fSWillem de Bruijn } 924ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 9250f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 9260f149c9fSWillem de Bruijn kfree_skb(skb); 927bec1f6f6SWillem de Bruijn return -EIO; 9280f149c9fSWillem de Bruijn } 929bec1f6f6SWillem de Bruijn 9304094871dSJosh Hunt if (datalen > cork->gso_size) { 931bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 932bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 9334094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 934dfec0ee2SAlexander Duyck cork->gso_size); 9354094871dSJosh Hunt } 936a8c744a8SWillem de Bruijn goto csum_partial; 937bec1f6f6SWillem de Bruijn } 938bec1f6f6SWillem de Bruijn 939f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 940f6b9664fSHerbert Xu csum = udplite_csum(skb); 941f6b9664fSHerbert Xu 942ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 943f6b9664fSHerbert Xu 944f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 945f6b9664fSHerbert Xu goto send; 946f6b9664fSHerbert Xu 947f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 948a8c744a8SWillem de Bruijn csum_partial: 949f6b9664fSHerbert Xu 95079ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 951f6b9664fSHerbert Xu goto send; 952f6b9664fSHerbert Xu 953f6b9664fSHerbert Xu } else 954f6b9664fSHerbert Xu csum = udp_csum(skb); 955f6b9664fSHerbert Xu 956f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 95779ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 958f6b9664fSHerbert Xu sk->sk_protocol, csum); 959f6b9664fSHerbert Xu if (uh->check == 0) 960f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 961f6b9664fSHerbert Xu 962f6b9664fSHerbert Xu send: 963b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 964f6b9664fSHerbert Xu if (err) { 9656b5f43eaSEric Dumazet if (err == -ENOBUFS && 9666b5f43eaSEric Dumazet !inet_test_bit(RECVERR, sk)) { 9676aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 968f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 969f6b9664fSHerbert Xu err = 0; 970f6b9664fSHerbert Xu } 971f6b9664fSHerbert Xu } else 9726aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 973f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 974f6b9664fSHerbert Xu return err; 975f6b9664fSHerbert Xu } 976f6b9664fSHerbert Xu 977db8dac20SDavid S. Miller /* 978db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 979db8dac20SDavid S. Miller */ 9808822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 981db8dac20SDavid S. Miller { 982db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 983db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 984b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 985db8dac20SDavid S. Miller struct sk_buff *skb; 986db8dac20SDavid S. Miller int err = 0; 987db8dac20SDavid S. Miller 98877968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 989f6b9664fSHerbert Xu if (!skb) 990db8dac20SDavid S. Miller goto out; 991db8dac20SDavid S. Miller 992bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &inet->cork.base); 993db8dac20SDavid S. Miller 994db8dac20SDavid S. Miller out: 995db8dac20SDavid S. Miller up->len = 0; 996482521d8SEric Dumazet WRITE_ONCE(up->pending, 0); 997db8dac20SDavid S. Miller return err; 998db8dac20SDavid S. Miller } 9998822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 1000db8dac20SDavid S. Miller 10012e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 10022e8de857SWillem de Bruijn { 10032e8de857SWillem de Bruijn switch (cmsg->cmsg_type) { 10042e8de857SWillem de Bruijn case UDP_SEGMENT: 10052e8de857SWillem de Bruijn if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 10062e8de857SWillem de Bruijn return -EINVAL; 10072e8de857SWillem de Bruijn *gso_size = *(__u16 *)CMSG_DATA(cmsg); 10082e8de857SWillem de Bruijn return 0; 10092e8de857SWillem de Bruijn default: 10102e8de857SWillem de Bruijn return -EINVAL; 10112e8de857SWillem de Bruijn } 10122e8de857SWillem de Bruijn } 10132e8de857SWillem de Bruijn 10142e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 10152e8de857SWillem de Bruijn { 10162e8de857SWillem de Bruijn struct cmsghdr *cmsg; 10172e8de857SWillem de Bruijn bool need_ip = false; 10182e8de857SWillem de Bruijn int err; 10192e8de857SWillem de Bruijn 10202e8de857SWillem de Bruijn for_each_cmsghdr(cmsg, msg) { 10212e8de857SWillem de Bruijn if (!CMSG_OK(msg, cmsg)) 10222e8de857SWillem de Bruijn return -EINVAL; 10232e8de857SWillem de Bruijn 10242e8de857SWillem de Bruijn if (cmsg->cmsg_level != SOL_UDP) { 10252e8de857SWillem de Bruijn need_ip = true; 10262e8de857SWillem de Bruijn continue; 10272e8de857SWillem de Bruijn } 10282e8de857SWillem de Bruijn 10292e8de857SWillem de Bruijn err = __udp_cmsg_send(cmsg, gso_size); 10302e8de857SWillem de Bruijn if (err) 10312e8de857SWillem de Bruijn return err; 10322e8de857SWillem de Bruijn } 10332e8de857SWillem de Bruijn 10342e8de857SWillem de Bruijn return need_ip; 10352e8de857SWillem de Bruijn } 10362e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send); 10372e8de857SWillem de Bruijn 10381b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1039db8dac20SDavid S. Miller { 1040db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1041db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 10421cedee13SAndrey Ignatov DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 1043e474995fSDavid S. Miller struct flowi4 fl4_stack; 1044b6f21b26SDavid S. Miller struct flowi4 *fl4; 1045db8dac20SDavid S. Miller int ulen = len; 1046db8dac20SDavid S. Miller struct ipcm_cookie ipc; 1047db8dac20SDavid S. Miller struct rtable *rt = NULL; 1048db8dac20SDavid S. Miller int free = 0; 1049db8dac20SDavid S. Miller int connected = 0; 1050db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 10510e26371dSGuillaume Nault u8 tos, scope; 1052db8dac20SDavid S. Miller __be16 dport; 1053db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 105481b36803SEric Dumazet int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE; 1055db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1056903ab86dSHerbert Xu struct sk_buff *skb; 1057f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 1058959d5c11SEric Dumazet int uc_index; 1059db8dac20SDavid S. Miller 1060db8dac20SDavid S. Miller if (len > 0xFFFF) 1061db8dac20SDavid S. Miller return -EMSGSIZE; 1062db8dac20SDavid S. Miller 1063db8dac20SDavid S. Miller /* 1064db8dac20SDavid S. Miller * Check the flags. 1065db8dac20SDavid S. Miller */ 1066db8dac20SDavid S. Miller 1067db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 1068db8dac20SDavid S. Miller return -EOPNOTSUPP; 1069db8dac20SDavid S. Miller 1070903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1071903ab86dSHerbert Xu 1072f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1073482521d8SEric Dumazet if (READ_ONCE(up->pending)) { 1074db8dac20SDavid S. Miller /* 1075db8dac20SDavid S. Miller * There are pending frames. 1076db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1077db8dac20SDavid S. Miller */ 1078db8dac20SDavid S. Miller lock_sock(sk); 1079db8dac20SDavid S. Miller if (likely(up->pending)) { 1080db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 1081db8dac20SDavid S. Miller release_sock(sk); 1082db8dac20SDavid S. Miller return -EINVAL; 1083db8dac20SDavid S. Miller } 1084db8dac20SDavid S. Miller goto do_append_data; 1085db8dac20SDavid S. Miller } 1086db8dac20SDavid S. Miller release_sock(sk); 1087db8dac20SDavid S. Miller } 1088db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1089db8dac20SDavid S. Miller 1090db8dac20SDavid S. Miller /* 1091db8dac20SDavid S. Miller * Get and verify the address. 1092db8dac20SDavid S. Miller */ 10931cedee13SAndrey Ignatov if (usin) { 1094db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 1095db8dac20SDavid S. Miller return -EINVAL; 1096db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 1097db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 1098db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1099db8dac20SDavid S. Miller } 1100db8dac20SDavid S. Miller 1101db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 1102db8dac20SDavid S. Miller dport = usin->sin_port; 1103db8dac20SDavid S. Miller if (dport == 0) 1104db8dac20SDavid S. Miller return -EINVAL; 1105db8dac20SDavid S. Miller } else { 1106db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1107db8dac20SDavid S. Miller return -EDESTADDRREQ; 1108c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1109c720c7e8SEric Dumazet dport = inet->inet_dport; 1110db8dac20SDavid S. Miller /* Open fast path for connected socket. 1111db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 1112db8dac20SDavid S. Miller */ 1113db8dac20SDavid S. Miller connected = 1; 1114db8dac20SDavid S. Miller } 1115c14ac945SSoheil Hassas Yeganeh 111635178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 111718a419baSEric Dumazet ipc.gso_size = READ_ONCE(up->gso_size); 1118bf84a010SDaniel Borkmann 1119db8dac20SDavid S. Miller if (msg->msg_controllen) { 11202e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc.gso_size); 11212e8de857SWillem de Bruijn if (err > 0) 11222e8de857SWillem de Bruijn err = ip_cmsg_send(sk, msg, &ipc, 11232e8de857SWillem de Bruijn sk->sk_family == AF_INET6); 11242e8de857SWillem de Bruijn if (unlikely(err < 0)) { 112591948309SEric Dumazet kfree(ipc.opt); 1126db8dac20SDavid S. Miller return err; 112791948309SEric Dumazet } 1128db8dac20SDavid S. Miller if (ipc.opt) 1129db8dac20SDavid S. Miller free = 1; 1130db8dac20SDavid S. Miller connected = 0; 1131db8dac20SDavid S. Miller } 1132f6d8bd05SEric Dumazet if (!ipc.opt) { 1133f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 1134f6d8bd05SEric Dumazet 1135f6d8bd05SEric Dumazet rcu_read_lock(); 1136f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1137f6d8bd05SEric Dumazet if (inet_opt) { 1138f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 1139f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 1140f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 1141f6d8bd05SEric Dumazet } 1142f6d8bd05SEric Dumazet rcu_read_unlock(); 1143f6d8bd05SEric Dumazet } 1144db8dac20SDavid S. Miller 11456fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP4_SENDMSG) && !connected) { 11461cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 1147fefba7d1SDaan De Meyer (struct sockaddr *)usin, 1148fefba7d1SDaan De Meyer &msg->msg_namelen, 1149fefba7d1SDaan De Meyer &ipc.addr); 11501cedee13SAndrey Ignatov if (err) 11511cedee13SAndrey Ignatov goto out_free; 11521cedee13SAndrey Ignatov if (usin) { 11531cedee13SAndrey Ignatov if (usin->sin_port == 0) { 11541cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 11551cedee13SAndrey Ignatov err = -EINVAL; 11561cedee13SAndrey Ignatov goto out_free; 11571cedee13SAndrey Ignatov } 11581cedee13SAndrey Ignatov daddr = usin->sin_addr.s_addr; 11591cedee13SAndrey Ignatov dport = usin->sin_port; 11601cedee13SAndrey Ignatov } 11611cedee13SAndrey Ignatov } 11621cedee13SAndrey Ignatov 1163db8dac20SDavid S. Miller saddr = ipc.addr; 1164db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 1165db8dac20SDavid S. Miller 1166f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 11671b97013bSAndrey Ignatov if (!daddr) { 11681b97013bSAndrey Ignatov err = -EINVAL; 11691b97013bSAndrey Ignatov goto out_free; 11701b97013bSAndrey Ignatov } 1171f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 1172db8dac20SDavid S. Miller connected = 0; 1173db8dac20SDavid S. Miller } 1174aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 11750e26371dSGuillaume Nault scope = ip_sendmsg_scope(inet, &ipc, msg); 11760e26371dSGuillaume Nault if (scope == RT_SCOPE_LINK) 1177db8dac20SDavid S. Miller connected = 0; 1178db8dac20SDavid S. Miller 1179959d5c11SEric Dumazet uc_index = READ_ONCE(inet->uc_index); 1180db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 1181854da991SRobert Shearman if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 118202715925SEric Dumazet ipc.oif = READ_ONCE(inet->mc_index); 1183db8dac20SDavid S. Miller if (!saddr) 118402715925SEric Dumazet saddr = READ_ONCE(inet->mc_addr); 1185db8dac20SDavid S. Miller connected = 0; 11869515a2e0SDavid Ahern } else if (!ipc.oif) { 1187959d5c11SEric Dumazet ipc.oif = uc_index; 1188959d5c11SEric Dumazet } else if (ipv4_is_lbcast(daddr) && uc_index) { 11899515a2e0SDavid Ahern /* oif is set, packet is to local broadcast and 11902bdcc73cSRandy Dunlap * uc_index is set. oif is most likely set 11919515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 11929515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 11939515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 11949515a2e0SDavid Ahern */ 1195959d5c11SEric Dumazet if (ipc.oif != uc_index && 11969515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 1197959d5c11SEric Dumazet uc_index)) { 1198959d5c11SEric Dumazet ipc.oif = uc_index; 11999515a2e0SDavid Ahern } 12009515a2e0SDavid Ahern } 1201db8dac20SDavid S. Miller 1202db8dac20SDavid S. Miller if (connected) 1203db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 1204db8dac20SDavid S. Miller 120551456b29SIan Morris if (!rt) { 120684a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 12079a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 120884a3aa00SPavel Emelyanov 1209e474995fSDavid S. Miller fl4 = &fl4_stack; 12109a24abfaSDavid Ahern 12110e26371dSGuillaume Nault flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, scope, 12120e26371dSGuillaume Nault sk->sk_protocol, flow_flags, faddr, saddr, 12130e26371dSGuillaume Nault dport, inet->inet_sport, sk->sk_uid); 1214c0951cbcSDavid S. Miller 12153df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 1216e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1217b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1218b23dd4feSDavid S. Miller err = PTR_ERR(rt); 121906dc94b1SDavid S. Miller rt = NULL; 1220db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1221f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1222db8dac20SDavid S. Miller goto out; 1223db8dac20SDavid S. Miller } 1224db8dac20SDavid S. Miller 1225db8dac20SDavid S. Miller err = -EACCES; 1226db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1227db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1228db8dac20SDavid S. Miller goto out; 1229db8dac20SDavid S. Miller if (connected) 1230d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1231db8dac20SDavid S. Miller } 1232db8dac20SDavid S. Miller 1233db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1234db8dac20SDavid S. Miller goto do_confirm; 1235db8dac20SDavid S. Miller back_from_confirm: 1236db8dac20SDavid S. Miller 1237e474995fSDavid S. Miller saddr = fl4->saddr; 1238db8dac20SDavid S. Miller if (!ipc.addr) 1239e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1240db8dac20SDavid S. Miller 1241903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1242903ab86dSHerbert Xu if (!corkreq) { 12431cd7884dSWillem de Bruijn struct inet_cork cork; 12441cd7884dSWillem de Bruijn 1245f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1246903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 12471cd7884dSWillem de Bruijn &cork, msg->msg_flags); 1248903ab86dSHerbert Xu err = PTR_ERR(skb); 124950c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 1250bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &cork); 1251903ab86dSHerbert Xu goto out; 1252903ab86dSHerbert Xu } 1253903ab86dSHerbert Xu 1254db8dac20SDavid S. Miller lock_sock(sk); 1255db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1256db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1257db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1258db8dac20SDavid S. Miller release_sock(sk); 1259db8dac20SDavid S. Miller 1260197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1261db8dac20SDavid S. Miller err = -EINVAL; 1262db8dac20SDavid S. Miller goto out; 1263db8dac20SDavid S. Miller } 1264db8dac20SDavid S. Miller /* 1265db8dac20SDavid S. Miller * Now cork the socket to pend data. 1266db8dac20SDavid S. Miller */ 1267b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1268b6f21b26SDavid S. Miller fl4->daddr = daddr; 1269b6f21b26SDavid S. Miller fl4->saddr = saddr; 12709cce96dfSDavid S. Miller fl4->fl4_dport = dport; 12719cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1272482521d8SEric Dumazet WRITE_ONCE(up->pending, AF_INET); 1273db8dac20SDavid S. Miller 1274db8dac20SDavid S. Miller do_append_data: 1275db8dac20SDavid S. Miller up->len += ulen; 1276f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 12772e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1278db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1279db8dac20SDavid S. Miller if (err) 1280db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1281db8dac20SDavid S. Miller else if (!corkreq) 1282db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1283db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1284482521d8SEric Dumazet WRITE_ONCE(up->pending, 0); 1285db8dac20SDavid S. Miller release_sock(sk); 1286db8dac20SDavid S. Miller 1287db8dac20SDavid S. Miller out: 1288db8dac20SDavid S. Miller ip_rt_put(rt); 12891b97013bSAndrey Ignatov out_free: 1290db8dac20SDavid S. Miller if (free) 1291db8dac20SDavid S. Miller kfree(ipc.opt); 1292db8dac20SDavid S. Miller if (!err) 1293db8dac20SDavid S. Miller return len; 1294db8dac20SDavid S. Miller /* 1295db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1296db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1297db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1298db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1299db8dac20SDavid S. Miller * seems like overkill. 1300db8dac20SDavid S. Miller */ 1301db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 13026aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1303629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1304db8dac20SDavid S. Miller } 1305db8dac20SDavid S. Miller return err; 1306db8dac20SDavid S. Miller 1307db8dac20SDavid S. Miller do_confirm: 13080dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 13090dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1310db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1311db8dac20SDavid S. Miller goto back_from_confirm; 1312db8dac20SDavid S. Miller err = 0; 1313db8dac20SDavid S. Miller goto out; 1314db8dac20SDavid S. Miller } 1315c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1316db8dac20SDavid S. Miller 13171d7e4538SDavid Howells void udp_splice_eof(struct socket *sock) 13181d7e4538SDavid Howells { 13191d7e4538SDavid Howells struct sock *sk = sock->sk; 13201d7e4538SDavid Howells struct udp_sock *up = udp_sk(sk); 13211d7e4538SDavid Howells 1322482521d8SEric Dumazet if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk)) 13231d7e4538SDavid Howells return; 13241d7e4538SDavid Howells 13251d7e4538SDavid Howells lock_sock(sk); 132681b36803SEric Dumazet if (up->pending && !udp_test_bit(CORK, sk)) 13271d7e4538SDavid Howells udp_push_pending_frames(sk); 13281d7e4538SDavid Howells release_sock(sk); 13291d7e4538SDavid Howells } 13301d7e4538SDavid Howells EXPORT_SYMBOL_GPL(udp_splice_eof); 13311d7e4538SDavid Howells 1332dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1333dce4551cSPaolo Abeni 1334677bf08cSFlorian Westphal /* all head states (dst, sk, nf conntrack) except skb extensions are 1335677bf08cSFlorian Westphal * cleared by udp_rcv(). 1336677bf08cSFlorian Westphal * 1337677bf08cSFlorian Westphal * We need to preserve secpath, if present, to eventually process 1338677bf08cSFlorian Westphal * IP_CMSG_PASSSEC at recvmsg() time. 1339677bf08cSFlorian Westphal * 1340677bf08cSFlorian Westphal * Other extensions can be cleared. 1341677bf08cSFlorian Westphal */ 1342677bf08cSFlorian Westphal static bool udp_try_make_stateless(struct sk_buff *skb) 1343677bf08cSFlorian Westphal { 1344677bf08cSFlorian Westphal if (!skb_has_extensions(skb)) 1345677bf08cSFlorian Westphal return true; 1346677bf08cSFlorian Westphal 1347677bf08cSFlorian Westphal if (!secpath_exists(skb)) { 1348677bf08cSFlorian Westphal skb_ext_reset(skb); 1349677bf08cSFlorian Westphal return true; 1350677bf08cSFlorian Westphal } 1351677bf08cSFlorian Westphal 1352677bf08cSFlorian Westphal return false; 1353677bf08cSFlorian Westphal } 1354677bf08cSFlorian Westphal 1355b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1356b65ac446SPaolo Abeni { 1357dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1358b65ac446SPaolo Abeni 1359b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1360dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1361dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1362b65ac446SPaolo Abeni scratch->len = skb->len; 1363b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1364b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1365b65ac446SPaolo Abeni #endif 1366677bf08cSFlorian Westphal if (udp_try_make_stateless(skb)) 1367dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1368dce4551cSPaolo Abeni } 1369dce4551cSPaolo Abeni 1370a793183cSEric Dumazet static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) 1371a793183cSEric Dumazet { 1372a793183cSEric Dumazet /* We come here after udp_lib_checksum_complete() returned 0. 1373a793183cSEric Dumazet * This means that __skb_checksum_complete() might have 1374a793183cSEric Dumazet * set skb->csum_valid to 1. 1375a793183cSEric Dumazet * On 64bit platforms, we can set csum_unnecessary 1376a793183cSEric Dumazet * to true, but only if the skb is not shared. 1377a793183cSEric Dumazet */ 1378a793183cSEric Dumazet #if BITS_PER_LONG == 64 1379a793183cSEric Dumazet if (!skb_shared(skb)) 1380a793183cSEric Dumazet udp_skb_scratch(skb)->csum_unnecessary = true; 1381a793183cSEric Dumazet #endif 1382a793183cSEric Dumazet } 1383a793183cSEric Dumazet 1384dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1385dce4551cSPaolo Abeni { 1386dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1387dce4551cSPaolo Abeni } 1388dce4551cSPaolo Abeni 1389dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1390dce4551cSPaolo Abeni { 1391dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1392dce4551cSPaolo Abeni } 1393b65ac446SPaolo Abeni 13947c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 13956dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 13966dfb4367SPaolo Abeni bool rx_queue_lock_held) 1397f970bd9eSPaolo Abeni { 13986b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 13992276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1400f970bd9eSPaolo Abeni int amt; 1401f970bd9eSPaolo Abeni 14026b229cf7SEric Dumazet if (likely(partial)) { 14036b229cf7SEric Dumazet up->forward_deficit += size; 14046b229cf7SEric Dumazet size = up->forward_deficit; 14058a3854c7SPaolo Abeni if (size < READ_ONCE(up->forward_threshold) && 1406d39ca259SPaolo Abeni !skb_queue_empty(&up->reader_queue)) 14076b229cf7SEric Dumazet return; 14086b229cf7SEric Dumazet } else { 14096b229cf7SEric Dumazet size += up->forward_deficit; 14106b229cf7SEric Dumazet } 14116b229cf7SEric Dumazet up->forward_deficit = 0; 14126b229cf7SEric Dumazet 14136dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 14146dfb4367SPaolo Abeni * if the called don't held it already 14156dfb4367SPaolo Abeni */ 14162276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 14176dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14182276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 14192276f58aSPaolo Abeni 14206dfb4367SPaolo Abeni 14215e6300e7SEric Dumazet sk_forward_alloc_add(sk, size); 1422100fdd1fSEric Dumazet amt = (sk->sk_forward_alloc - partial) & ~(PAGE_SIZE - 1); 14235e6300e7SEric Dumazet sk_forward_alloc_add(sk, -amt); 1424f970bd9eSPaolo Abeni 1425f970bd9eSPaolo Abeni if (amt) 1426100fdd1fSEric Dumazet __sk_mem_reduce_allocated(sk, amt >> PAGE_SHIFT); 142702ab0d13SEric Dumazet 142802ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 14292276f58aSPaolo Abeni 14302276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 14312276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 14322276f58aSPaolo Abeni 14336dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14342276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1435f970bd9eSPaolo Abeni } 1436f970bd9eSPaolo Abeni 14372276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1438c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1439c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1440c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1441c84d9490SEric Dumazet */ 14427c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1443f970bd9eSPaolo Abeni { 1444b65ac446SPaolo Abeni prefetch(&skb->data); 1445b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1446f970bd9eSPaolo Abeni } 14477c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1448f970bd9eSPaolo Abeni 14496dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 145064f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 14516dfb4367SPaolo Abeni { 1452b65ac446SPaolo Abeni prefetch(&skb->data); 1453b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 14546dfb4367SPaolo Abeni } 14556dfb4367SPaolo Abeni 14564b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 14574b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 14584b272750SEric Dumazet * Under flood, this means that only one producer can be in line 14594b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 14604b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 14614b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 14624b272750SEric Dumazet */ 14634b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 14644b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 14654b272750SEric Dumazet 14664b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 14674b272750SEric Dumazet { 14684b272750SEric Dumazet spinlock_t *busy; 14694b272750SEric Dumazet 14704b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 14714b272750SEric Dumazet spin_lock(busy); 14724b272750SEric Dumazet return busy; 14734b272750SEric Dumazet } 14744b272750SEric Dumazet 14754b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 14764b272750SEric Dumazet { 14774b272750SEric Dumazet if (busy) 14784b272750SEric Dumazet spin_unlock(busy); 14794b272750SEric Dumazet } 14804b272750SEric Dumazet 1481fd9c31f8SJason Xing static int udp_rmem_schedule(struct sock *sk, int size) 1482fd9c31f8SJason Xing { 1483fd9c31f8SJason Xing int delta; 1484fd9c31f8SJason Xing 1485fd9c31f8SJason Xing delta = size - sk->sk_forward_alloc; 1486fd9c31f8SJason Xing if (delta > 0 && !__sk_mem_schedule(sk, delta, SK_MEM_RECV)) 1487fd9c31f8SJason Xing return -ENOBUFS; 1488fd9c31f8SJason Xing 1489fd9c31f8SJason Xing return 0; 1490fd9c31f8SJason Xing } 1491fd9c31f8SJason Xing 1492f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1493f970bd9eSPaolo Abeni { 1494f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1495fd9c31f8SJason Xing int rmem, err = -ENOMEM; 14964b272750SEric Dumazet spinlock_t *busy = NULL; 1497c8c8b127SEric Dumazet int size; 1498f970bd9eSPaolo Abeni 1499f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1500f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1501f970bd9eSPaolo Abeni */ 1502f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1503363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1504f970bd9eSPaolo Abeni goto drop; 1505f970bd9eSPaolo Abeni 1506c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1507c8c8b127SEric Dumazet * having linear skbs : 1508c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1509c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1510c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1511c8c8b127SEric Dumazet */ 15124b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1513c8c8b127SEric Dumazet skb_condense(skb); 15144b272750SEric Dumazet 15154b272750SEric Dumazet busy = busylock_acquire(sk); 15164b272750SEric Dumazet } 1517c8c8b127SEric Dumazet size = skb->truesize; 1518b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1519c8c8b127SEric Dumazet 1520f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1521f970bd9eSPaolo Abeni * queue contains some other skb 1522f970bd9eSPaolo Abeni */ 1523f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1524feed8a4fSAntonio Messina if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) 1525f970bd9eSPaolo Abeni goto uncharge_drop; 1526f970bd9eSPaolo Abeni 1527f970bd9eSPaolo Abeni spin_lock(&list->lock); 1528fd9c31f8SJason Xing err = udp_rmem_schedule(sk, size); 1529fd9c31f8SJason Xing if (err) { 1530f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1531f970bd9eSPaolo Abeni goto uncharge_drop; 1532f970bd9eSPaolo Abeni } 1533f970bd9eSPaolo Abeni 15345e6300e7SEric Dumazet sk_forward_alloc_add(sk, -size); 1535f970bd9eSPaolo Abeni 15367c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 15377c13f97fSPaolo Abeni * forward allocated memory on dequeue 15387c13f97fSPaolo Abeni */ 1539f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1540f970bd9eSPaolo Abeni 1541f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1542f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1543f970bd9eSPaolo Abeni 1544f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 15450558e167SPaolo Abeni INDIRECT_CALL_1(sk->sk_data_ready, sock_def_readable, sk); 1546f970bd9eSPaolo Abeni 15474b272750SEric Dumazet busylock_release(busy); 1548f970bd9eSPaolo Abeni return 0; 1549f970bd9eSPaolo Abeni 1550f970bd9eSPaolo Abeni uncharge_drop: 1551f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1552f970bd9eSPaolo Abeni 1553f970bd9eSPaolo Abeni drop: 1554f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 15554b272750SEric Dumazet busylock_release(busy); 1556f970bd9eSPaolo Abeni return err; 1557f970bd9eSPaolo Abeni } 1558f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1559f970bd9eSPaolo Abeni 1560d38afeecSKuniyuki Iwashima void udp_destruct_common(struct sock *sk) 1561f970bd9eSPaolo Abeni { 1562f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 15632276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 15647c13f97fSPaolo Abeni unsigned int total = 0; 15657c13f97fSPaolo Abeni struct sk_buff *skb; 15667c13f97fSPaolo Abeni 15672276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 15682276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 15697c13f97fSPaolo Abeni total += skb->truesize; 15707c13f97fSPaolo Abeni kfree_skb(skb); 15717c13f97fSPaolo Abeni } 15726dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 1573d38afeecSKuniyuki Iwashima } 1574d38afeecSKuniyuki Iwashima EXPORT_SYMBOL_GPL(udp_destruct_common); 15757c13f97fSPaolo Abeni 1576d38afeecSKuniyuki Iwashima static void udp_destruct_sock(struct sock *sk) 1577d38afeecSKuniyuki Iwashima { 1578d38afeecSKuniyuki Iwashima udp_destruct_common(sk); 1579f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1580f970bd9eSPaolo Abeni } 1581f970bd9eSPaolo Abeni 1582f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1583f970bd9eSPaolo Abeni { 15848a3854c7SPaolo Abeni udp_lib_init_sock(sk); 1585f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1586e993ffe3SPavel Begunkov set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 1587f970bd9eSPaolo Abeni return 0; 1588f970bd9eSPaolo Abeni } 1589f970bd9eSPaolo Abeni 1590f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1591f970bd9eSPaolo Abeni { 1592f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1593f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1594f970bd9eSPaolo Abeni 1595f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1596f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1597f970bd9eSPaolo Abeni } 15980a463c78SPaolo Abeni 1599ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1600ca2c1418SPaolo Abeni return; 1601ca2c1418SPaolo Abeni 1602dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1603dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 16040ddf3fb2SPaolo Abeni */ 1605dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 16060ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1607ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1608f970bd9eSPaolo Abeni } 1609f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1610f970bd9eSPaolo Abeni 16112276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 16122276f58aSPaolo Abeni struct sk_buff_head *rcvq, 16132276f58aSPaolo Abeni int *total) 16142276f58aSPaolo Abeni { 16152276f58aSPaolo Abeni struct sk_buff *skb; 16162276f58aSPaolo Abeni 16179bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 16189bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 16192276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 16202276f58aSPaolo Abeni IS_UDPLITE(sk)); 16212276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 16222276f58aSPaolo Abeni IS_UDPLITE(sk)); 16232276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 16242276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 16252276f58aSPaolo Abeni *total += skb->truesize; 16262276f58aSPaolo Abeni kfree_skb(skb); 16279bd780f5SPaolo Abeni } else { 1628a793183cSEric Dumazet udp_skb_csum_unnecessary_set(skb); 16299bd780f5SPaolo Abeni break; 16309bd780f5SPaolo Abeni } 16312276f58aSPaolo Abeni } 16322276f58aSPaolo Abeni return skb; 16332276f58aSPaolo Abeni } 16342276f58aSPaolo Abeni 163585584672SEric Dumazet /** 163685584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 163785584672SEric Dumazet * @sk: socket 163885584672SEric Dumazet * 163985584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1640e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 164185584672SEric Dumazet */ 1642e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 164385584672SEric Dumazet { 16442276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 16452276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 164685584672SEric Dumazet struct sk_buff *skb; 16477c13f97fSPaolo Abeni int total = 0; 1648e83c6744SEric Dumazet int res; 164985584672SEric Dumazet 165085584672SEric Dumazet spin_lock_bh(&rcvq->lock); 16512276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 1652137a0dbeSEric Dumazet if (!skb && !skb_queue_empty_lockless(sk_queue)) { 16532276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 16542276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 16552276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 16562276f58aSPaolo Abeni 16572276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 165885584672SEric Dumazet } 1659e83c6744SEric Dumazet res = skb ? skb->len : -1; 16607c13f97fSPaolo Abeni if (total) 16616dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 166285584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 166385584672SEric Dumazet return res; 166485584672SEric Dumazet } 166585584672SEric Dumazet 16661da177e4SLinus Torvalds /* 16671da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 16681da177e4SLinus Torvalds */ 16691da177e4SLinus Torvalds 1670e1d001faSBreno Leitao int udp_ioctl(struct sock *sk, int cmd, int *karg) 16711da177e4SLinus Torvalds { 16726516c655SStephen Hemminger switch (cmd) { 16731da177e4SLinus Torvalds case SIOCOUTQ: 16741da177e4SLinus Torvalds { 1675e1d001faSBreno Leitao *karg = sk_wmem_alloc_get(sk); 1676e1d001faSBreno Leitao return 0; 16771da177e4SLinus Torvalds } 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds case SIOCINQ: 16801da177e4SLinus Torvalds { 1681e1d001faSBreno Leitao *karg = max_t(int, 0, first_packet_length(sk)); 1682e1d001faSBreno Leitao return 0; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds default: 16861da177e4SLinus Torvalds return -ENOIOCTLCMD; 16871da177e4SLinus Torvalds } 16886516c655SStephen Hemminger 16896516c655SStephen Hemminger return 0; 16901da177e4SLinus Torvalds } 1691c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 16921da177e4SLinus Torvalds 16932276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1694ec095263SOliver Hartkopp int *off, int *err) 16952276f58aSPaolo Abeni { 16962276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 16972276f58aSPaolo Abeni struct sk_buff_head *queue; 16982276f58aSPaolo Abeni struct sk_buff *last; 16992276f58aSPaolo Abeni long timeo; 17002276f58aSPaolo Abeni int error; 17012276f58aSPaolo Abeni 17022276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 17032276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 17042276f58aSPaolo Abeni do { 17052276f58aSPaolo Abeni struct sk_buff *skb; 17062276f58aSPaolo Abeni 17072276f58aSPaolo Abeni error = sock_error(sk); 17082276f58aSPaolo Abeni if (error) 17092276f58aSPaolo Abeni break; 17102276f58aSPaolo Abeni 17112276f58aSPaolo Abeni error = -EAGAIN; 17122276f58aSPaolo Abeni do { 17132276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 1714e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1715e427cad6SPaolo Abeni err, &last); 17162276f58aSPaolo Abeni if (skb) { 1717e427cad6SPaolo Abeni if (!(flags & MSG_PEEK)) 1718e427cad6SPaolo Abeni udp_skb_destructor(sk, skb); 17192276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17202276f58aSPaolo Abeni return skb; 17212276f58aSPaolo Abeni } 17222276f58aSPaolo Abeni 1723137a0dbeSEric Dumazet if (skb_queue_empty_lockless(sk_queue)) { 17242276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17252276f58aSPaolo Abeni goto busy_check; 17262276f58aSPaolo Abeni } 17272276f58aSPaolo Abeni 17286dfb4367SPaolo Abeni /* refill the reader queue and walk it again 17296dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 17306dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 17316dfb4367SPaolo Abeni * is needed. 17326dfb4367SPaolo Abeni */ 17332276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 17342276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 17352276f58aSPaolo Abeni 1736e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1737e427cad6SPaolo Abeni err, &last); 1738e427cad6SPaolo Abeni if (skb && !(flags & MSG_PEEK)) 1739e427cad6SPaolo Abeni udp_skb_dtor_locked(sk, skb); 17406dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 17412276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1742de321ed3SAndrey Vagin if (skb) 17432276f58aSPaolo Abeni return skb; 17442276f58aSPaolo Abeni 17452276f58aSPaolo Abeni busy_check: 17462276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 17472276f58aSPaolo Abeni break; 17482276f58aSPaolo Abeni 17492276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 1750137a0dbeSEric Dumazet } while (!skb_queue_empty_lockless(sk_queue)); 17512276f58aSPaolo Abeni 17522276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 17532276f58aSPaolo Abeni } while (timeo && 1754b50b0580SSabrina Dubroca !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, 1755b50b0580SSabrina Dubroca &error, &timeo, 17562276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 17572276f58aSPaolo Abeni 17582276f58aSPaolo Abeni *err = error; 17592276f58aSPaolo Abeni return NULL; 17602276f58aSPaolo Abeni } 17617e823644SJiri Kosina EXPORT_SYMBOL(__skb_recv_udp); 17622276f58aSPaolo Abeni 1763965b57b4SCong Wang int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) 1764d7f57118SCong Wang { 1765d7f57118SCong Wang struct sk_buff *skb; 176678fa0d61SJohn Fastabend int err; 1767d7f57118SCong Wang 176831f1fbcbSPeilin Ye try_again: 1769ec095263SOliver Hartkopp skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 1770d7f57118SCong Wang if (!skb) 1771d7f57118SCong Wang return err; 1772099f896fSCong Wang 1773099f896fSCong Wang if (udp_lib_checksum_complete(skb)) { 177431f1fbcbSPeilin Ye int is_udplite = IS_UDPLITE(sk); 177531f1fbcbSPeilin Ye struct net *net = sock_net(sk); 177631f1fbcbSPeilin Ye 177731f1fbcbSPeilin Ye __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, is_udplite); 177831f1fbcbSPeilin Ye __UDP_INC_STATS(net, UDP_MIB_INERRORS, is_udplite); 1779099f896fSCong Wang atomic_inc(&sk->sk_drops); 1780099f896fSCong Wang kfree_skb(skb); 178131f1fbcbSPeilin Ye goto try_again; 1782099f896fSCong Wang } 1783099f896fSCong Wang 1784db39dfdcSPeilin Ye WARN_ON_ONCE(!skb_set_owner_sk_safe(skb, sk)); 178578fa0d61SJohn Fastabend return recv_actor(sk, skb); 1786d7f57118SCong Wang } 1787965b57b4SCong Wang EXPORT_SYMBOL(udp_read_skb); 1788d7f57118SCong Wang 1789db8dac20SDavid S. Miller /* 1790db8dac20SDavid S. Miller * This should be easy, if there is something there we 1791db8dac20SDavid S. Miller * return it, otherwise we block. 1792db8dac20SDavid S. Miller */ 1793db8dac20SDavid S. Miller 1794ec095263SOliver Hartkopp int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags, 1795ec095263SOliver Hartkopp int *addr_len) 1796db8dac20SDavid S. Miller { 1797db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1798342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1799db8dac20SDavid S. Miller struct sk_buff *skb; 180059c2cdaeSDavid S. Miller unsigned int ulen, copied; 1801fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 1802db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1803197c949eSEric Dumazet bool checksum_valid = false; 1804db8dac20SDavid S. Miller 1805db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 180685fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1807db8dac20SDavid S. Miller 1808db8dac20SDavid S. Miller try_again: 1809a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 1810ec095263SOliver Hartkopp skb = __skb_recv_udp(sk, flags, &off, &err); 1811db8dac20SDavid S. Miller if (!skb) 1812627d2d6bSsamanthakumar return err; 1813db8dac20SDavid S. Miller 1814b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 181559c2cdaeSDavid S. Miller copied = len; 1816627d2d6bSsamanthakumar if (copied > ulen - off) 1817627d2d6bSsamanthakumar copied = ulen - off; 181859c2cdaeSDavid S. Miller else if (copied < ulen) 1819db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1820db8dac20SDavid S. Miller 1821db8dac20SDavid S. Miller /* 1822db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1823db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1824db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1825db8dac20SDavid S. Miller */ 1826db8dac20SDavid S. Miller 1827d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1828d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1829b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1830b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1831197c949eSEric Dumazet if (!checksum_valid) 1832db8dac20SDavid S. Miller goto csum_copy_err; 1833db8dac20SDavid S. Miller } 1834db8dac20SDavid S. Miller 1835b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1836b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1837b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1838b65ac446SPaolo Abeni else 1839627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1840b65ac446SPaolo Abeni } else { 1841627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1842db8dac20SDavid S. Miller 1843db8dac20SDavid S. Miller if (err == -EINVAL) 1844db8dac20SDavid S. Miller goto csum_copy_err; 1845db8dac20SDavid S. Miller } 1846db8dac20SDavid S. Miller 184722911fc5SEric Dumazet if (unlikely(err)) { 1848fd69c399SPaolo Abeni if (!peeking) { 1849979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 18506aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1851979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1852979402b1SEric Dumazet } 1853850cbaddSPaolo Abeni kfree_skb(skb); 1854627d2d6bSsamanthakumar return err; 185522911fc5SEric Dumazet } 1856db8dac20SDavid S. Miller 1857fd69c399SPaolo Abeni if (!peeking) 18586aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1859629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1860db8dac20SDavid S. Miller 18616fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 1862db8dac20SDavid S. Miller 1863db8dac20SDavid S. Miller /* Copy the address. */ 1864c482c568SEric Dumazet if (sin) { 1865db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1866db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1867db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1868db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1869bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1870983695faSDaniel Borkmann 1871983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, 1872fefba7d1SDaan De Meyer (struct sockaddr *)sin, 1873fefba7d1SDaan De Meyer addr_len); 1874db8dac20SDavid S. Miller } 1875bcd1665eSPaolo Abeni 1876e1dc0615SEric Dumazet if (udp_test_bit(GRO_ENABLED, sk)) 1877bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 1878bcd1665eSPaolo Abeni 1879c274af22SEric Dumazet if (inet_cmsg_flags(inet)) 1880ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1881db8dac20SDavid S. Miller 188259c2cdaeSDavid S. Miller err = copied; 1883db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1884db8dac20SDavid S. Miller err = ulen; 1885db8dac20SDavid S. Miller 1886850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1887db8dac20SDavid S. Miller return err; 1888db8dac20SDavid S. Miller 1889db8dac20SDavid S. Miller csum_copy_err: 18902276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 18912276f58aSPaolo Abeni udp_skb_destructor)) { 18926aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 18936aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 18946a5dc9e5SEric Dumazet } 1895850cbaddSPaolo Abeni kfree_skb(skb); 1896db8dac20SDavid S. Miller 1897beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1898beb39db5SEric Dumazet cond_resched(); 18999cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1900db8dac20SDavid S. Miller goto try_again; 1901db8dac20SDavid S. Miller } 1902db8dac20SDavid S. Miller 1903d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1904d74bad4eSAndrey Ignatov { 1905d74bad4eSAndrey Ignatov /* This check is replicated from __ip4_datagram_connect() and 1906d74bad4eSAndrey Ignatov * intended to prevent BPF program called below from accessing bytes 1907d74bad4eSAndrey Ignatov * that are out of the bound specified by user in addr_len. 1908d74bad4eSAndrey Ignatov */ 1909d74bad4eSAndrey Ignatov if (addr_len < sizeof(struct sockaddr_in)) 1910d74bad4eSAndrey Ignatov return -EINVAL; 1911d74bad4eSAndrey Ignatov 1912fefba7d1SDaan De Meyer return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr, &addr_len); 1913d74bad4eSAndrey Ignatov } 1914d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect); 1915d74bad4eSAndrey Ignatov 1916286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 19171da177e4SLinus Torvalds { 19181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19191da177e4SLinus Torvalds /* 19201da177e4SLinus Torvalds * 1003.1g - break association. 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1924c720c7e8SEric Dumazet inet->inet_daddr = 0; 1925c720c7e8SEric Dumazet inet->inet_dport = 0; 1926bdeab991STom Herbert sock_rps_reset_rxhash(sk); 19271da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 1928303d0403SWillem de Bruijn if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { 19291da177e4SLinus Torvalds inet_reset_saddr(sk); 1930303d0403SWillem de Bruijn if (sk->sk_prot->rehash && 1931303d0403SWillem de Bruijn (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 1932303d0403SWillem de Bruijn sk->sk_prot->rehash(sk); 1933303d0403SWillem de Bruijn } 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 19361da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1937c720c7e8SEric Dumazet inet->inet_sport = 0; 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds sk_dst_reset(sk); 19401da177e4SLinus Torvalds return 0; 19411da177e4SLinus Torvalds } 1942286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1943286c72deSEric Dumazet 1944286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1945286c72deSEric Dumazet { 1946286c72deSEric Dumazet lock_sock(sk); 1947286c72deSEric Dumazet __udp_disconnect(sk, flags); 1948286c72deSEric Dumazet release_sock(sk); 1949286c72deSEric Dumazet return 0; 1950286c72deSEric Dumazet } 1951c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 19521da177e4SLinus Torvalds 1953645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 1954645ca708SEric Dumazet { 1955723b4610SEric Dumazet if (sk_hashed(sk)) { 195667fb4330SKuniyuki Iwashima struct udp_table *udptable = udp_get_table_prot(sk); 1957512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 1958512615b6SEric Dumazet 1959512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1960d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 1961512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1962645ca708SEric Dumazet 1963c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 1964e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1965e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1966ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 1967fdcc8aa9SEric Dumazet hslot->count--; 1968c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 1969645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1970512615b6SEric Dumazet 1971512615b6SEric Dumazet spin_lock(&hslot2->lock); 1972ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1973512615b6SEric Dumazet hslot2->count--; 1974512615b6SEric Dumazet spin_unlock(&hslot2->lock); 1975645ca708SEric Dumazet } 1976c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 1977645ca708SEric Dumazet } 1978723b4610SEric Dumazet } 1979645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 1980645ca708SEric Dumazet 1981719f8358SEric Dumazet /* 1982719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 1983719f8358SEric Dumazet */ 1984719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 1985719f8358SEric Dumazet { 1986719f8358SEric Dumazet if (sk_hashed(sk)) { 198767fb4330SKuniyuki Iwashima struct udp_table *udptable = udp_get_table_prot(sk); 1988719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 1989719f8358SEric Dumazet 1990719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1991719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 1992719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 1993e32ea7e7SCraig Gallek 1994e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 1995e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 1996719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1997719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 1998719f8358SEric Dumazet /* we must lock primary chain too */ 1999719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 2000e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 2001e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 2002719f8358SEric Dumazet 2003e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 2004719f8358SEric Dumazet spin_lock(&hslot2->lock); 2005ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2006719f8358SEric Dumazet hslot2->count--; 2007719f8358SEric Dumazet spin_unlock(&hslot2->lock); 2008719f8358SEric Dumazet 2009719f8358SEric Dumazet spin_lock(&nhslot2->lock); 2010ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 2011719f8358SEric Dumazet &nhslot2->head); 2012719f8358SEric Dumazet nhslot2->count++; 2013719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 2014e32ea7e7SCraig Gallek } 2015719f8358SEric Dumazet 2016719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 2017719f8358SEric Dumazet } 2018719f8358SEric Dumazet } 2019719f8358SEric Dumazet } 2020719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 2021719f8358SEric Dumazet 20228f6b5392SAlexey Kodanev void udp_v4_rehash(struct sock *sk) 2023719f8358SEric Dumazet { 2024f0b1e64cSMartin KaFai Lau u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 2025719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 2026719f8358SEric Dumazet inet_sk(sk)->inet_num); 2027719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 2028719f8358SEric Dumazet } 2029719f8358SEric Dumazet 2030a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 203193821778SHerbert Xu { 2032fec5e652STom Herbert int rc; 203393821778SHerbert Xu 2034005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 2035bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 2036005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 20372c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 2038e68b6e50SEric Dumazet } else { 2039e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 2040005ec974SShawn Bohrer } 2041fec5e652STom Herbert 2042850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 2043766e9037SEric Dumazet if (rc < 0) { 2044766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 204508d4c037SMenglong Dong int drop_reason; 2046766e9037SEric Dumazet 204793821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 204808d4c037SMenglong Dong if (rc == -ENOMEM) { 2049e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 205093821778SHerbert Xu is_udplite); 205108d4c037SMenglong Dong drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF; 205208d4c037SMenglong Dong } else { 2053a3ce2b10SMenglong Dong UDP_INC_STATS(sock_net(sk), UDP_MIB_MEMERRORS, 2054a3ce2b10SMenglong Dong is_udplite); 205508d4c037SMenglong Dong drop_reason = SKB_DROP_REASON_PROTO_MEM; 205608d4c037SMenglong Dong } 2057e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 205808d4c037SMenglong Dong kfree_skb_reason(skb, drop_reason); 2059296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 2060766e9037SEric Dumazet return -1; 206193821778SHerbert Xu } 206293821778SHerbert Xu 206393821778SHerbert Xu return 0; 206493821778SHerbert Xu } 206593821778SHerbert Xu 2066db8dac20SDavid S. Miller /* returns: 2067db8dac20SDavid S. Miller * -1: error 2068db8dac20SDavid S. Miller * 0: success 2069db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 2070db8dac20SDavid S. Miller * 2071db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 2072db8dac20SDavid S. Miller * have either been requeued or freed. 2073db8dac20SDavid S. Miller */ 2074cf329aa4SPaolo Abeni static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 2075db8dac20SDavid S. Miller { 20761379a92dSMenglong Dong int drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2077db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 2078db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 2079db8dac20SDavid S. Miller 2080db8dac20SDavid S. Miller /* 2081db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 2082db8dac20SDavid S. Miller */ 20831379a92dSMenglong Dong if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 20841379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2085db8dac20SDavid S. Miller goto drop; 20861379a92dSMenglong Dong } 2087895b5c9fSFlorian Westphal nf_reset_ct(skb); 2088db8dac20SDavid S. Miller 208970a36f57SEric Dumazet if (static_branch_unlikely(&udp_encap_needed_key) && 209070a36f57SEric Dumazet READ_ONCE(up->encap_type)) { 20910ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 20920ad92ad0SEric Dumazet 2093db8dac20SDavid S. Miller /* 2094db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 2095db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 2096db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 2097db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 2098db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 2099db8dac20SDavid S. Miller * handler or was discarded by it. 2100db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 2101db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 2102db8dac20SDavid S. Miller */ 2103db8dac20SDavid S. Miller 2104db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 21056aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 2106e5aed006SHannes Frederic Sowa if (encap_rcv) { 2107db8dac20SDavid S. Miller int ret; 2108db8dac20SDavid S. Miller 21090a80966bSTom Herbert /* Verify checksum before giving to encap */ 21100a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 21110a80966bSTom Herbert goto csum_error; 21120a80966bSTom Herbert 21130ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 2114db8dac20SDavid S. Miller if (ret <= 0) { 211502c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 21160283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 2117db8dac20SDavid S. Miller is_udplite); 2118db8dac20SDavid S. Miller return -ret; 2119db8dac20SDavid S. Miller } 2120db8dac20SDavid S. Miller } 2121db8dac20SDavid S. Miller 2122db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 2123db8dac20SDavid S. Miller } 2124db8dac20SDavid S. Miller 2125db8dac20SDavid S. Miller /* 2126db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 2127db8dac20SDavid S. Miller */ 2128882af43aSEric Dumazet if (udp_test_bit(UDPLITE_RECV_CC, sk) && UDP_SKB_CB(skb)->partial_cov) { 2129882af43aSEric Dumazet u16 pcrlen = READ_ONCE(up->pcrlen); 2130db8dac20SDavid S. Miller 2131db8dac20SDavid S. Miller /* 2132db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 2133db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 2134db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 2135db8dac20SDavid S. Miller * protocol stack as such. 2136db8dac20SDavid S. Miller * 2137db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 2138db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 2139db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 2140db8dac20SDavid S. Miller * provided by the application." 2141db8dac20SDavid S. Miller */ 2142882af43aSEric Dumazet if (pcrlen == 0) { /* full coverage was set */ 2143ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 2144db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 2145db8dac20SDavid S. Miller goto drop; 2146db8dac20SDavid S. Miller } 2147db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 2148db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 2149db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 2150db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 2151db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 2152db8dac20SDavid S. Miller */ 2153882af43aSEric Dumazet if (UDP_SKB_CB(skb)->cscov < pcrlen) { 2154ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 2155882af43aSEric Dumazet UDP_SKB_CB(skb)->cscov, pcrlen); 2156db8dac20SDavid S. Miller goto drop; 2157db8dac20SDavid S. Miller } 2158db8dac20SDavid S. Miller } 2159db8dac20SDavid S. Miller 2160dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 2161ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 2162ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 21636a5dc9e5SEric Dumazet goto csum_error; 2164ce25d66aSEric Dumazet 21651379a92dSMenglong Dong if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) { 21661379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 2167a6127697SMichal Kubeček goto drop; 21681379a92dSMenglong Dong } 2169db8dac20SDavid S. Miller 2170e6afc8acSsamanthakumar udp_csum_pull_header(skb); 2171db8dac20SDavid S. Miller 2172*e622502cSNicolas Dichtel ipv4_pktinfo_prepare(sk, skb, true); 2173850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 2174db8dac20SDavid S. Miller 21756a5dc9e5SEric Dumazet csum_error: 21761379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_UDP_CSUM; 217702c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2178db8dac20SDavid S. Miller drop: 217902c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 21808edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 21811379a92dSMenglong Dong kfree_skb_reason(skb, drop_reason); 2182db8dac20SDavid S. Miller return -1; 2183db8dac20SDavid S. Miller } 2184db8dac20SDavid S. Miller 2185cf329aa4SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2186cf329aa4SPaolo Abeni { 2187cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 2188cf329aa4SPaolo Abeni int ret; 2189cf329aa4SPaolo Abeni 2190cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 2191cf329aa4SPaolo Abeni return udp_queue_rcv_one_skb(sk, skb); 2192cf329aa4SPaolo Abeni 2193a08e7fd9SCambda Zhu BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); 2194cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 2195cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, true); 21961a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 2197cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 2198000ac44dSPaolo Abeni 2199000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb); 2200cf329aa4SPaolo Abeni ret = udp_queue_rcv_one_skb(sk, skb); 2201cf329aa4SPaolo Abeni if (ret > 0) 220210c678bdSXin Long ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret); 2203cf329aa4SPaolo Abeni } 2204cf329aa4SPaolo Abeni return 0; 2205cf329aa4SPaolo Abeni } 2206cf329aa4SPaolo Abeni 220797502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 2208e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 220997502231SEric Dumazet */ 221064f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2211421b3885SShawn Bohrer { 221297502231SEric Dumazet struct dst_entry *old; 2213421b3885SShawn Bohrer 2214d24406c8SWei Wang if (dst_hold_safe(dst)) { 22158f905c0eSEric Dumazet old = xchg((__force struct dst_entry **)&sk->sk_rx_dst, dst); 221697502231SEric Dumazet dst_release(old); 221764f0f5d1SPaolo Abeni return old != dst; 221897502231SEric Dumazet } 221964f0f5d1SPaolo Abeni return false; 2220d24406c8SWei Wang } 2221c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 2222421b3885SShawn Bohrer 2223db8dac20SDavid S. Miller /* 2224db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 2225db8dac20SDavid S. Miller * 22261240d137SEric Dumazet * Note: called only from the BH handler context. 2227db8dac20SDavid S. Miller */ 2228e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2229db8dac20SDavid S. Miller struct udphdr *uh, 2230db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 223136cbb245SRick Jones struct udp_table *udptable, 223236cbb245SRick Jones int proto) 2233db8dac20SDavid S. Miller { 2234ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 22355cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 22365cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 22372dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2238ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 2239ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 2240fb74c277SDavid Ahern int sdif = inet_sdif(skb); 2241ca065d0cSEric Dumazet struct hlist_node *node; 2242ca065d0cSEric Dumazet struct sk_buff *nskb; 22432dc41cffSDavid Held 22442dc41cffSDavid Held if (use_hash2) { 2245f0b1e64cSMartin KaFai Lau hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 224673e2d5e3SPablo Neira udptable->mask; 2247f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 22482dc41cffSDavid Held start_lookup: 224973e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 22502dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 22512dc41cffSDavid Held } 2252db8dac20SDavid S. Miller 2253ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2254ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2255fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 2256ca065d0cSEric Dumazet continue; 22571240d137SEric Dumazet 2258ca065d0cSEric Dumazet if (!first) { 2259ca065d0cSEric Dumazet first = sk; 2260ca065d0cSEric Dumazet continue; 2261ca065d0cSEric Dumazet } 2262ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 2263ca065d0cSEric Dumazet 2264ca065d0cSEric Dumazet if (unlikely(!nskb)) { 2265ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 226602c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2267ca065d0cSEric Dumazet IS_UDPLITE(sk)); 226802c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2269ca065d0cSEric Dumazet IS_UDPLITE(sk)); 2270ca065d0cSEric Dumazet continue; 2271ca065d0cSEric Dumazet } 2272ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 2273ca065d0cSEric Dumazet consume_skb(nskb); 2274ca065d0cSEric Dumazet } 22751240d137SEric Dumazet 22762dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 22772dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 22782dc41cffSDavid Held hash2 = hash2_any; 22792dc41cffSDavid Held goto start_lookup; 22802dc41cffSDavid Held } 22812dc41cffSDavid Held 2282ca065d0cSEric Dumazet if (first) { 2283ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 2284ca065d0cSEric Dumazet consume_skb(skb); 22851240d137SEric Dumazet } else { 2286ca065d0cSEric Dumazet kfree_skb(skb); 228702c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 228836cbb245SRick Jones proto == IPPROTO_UDPLITE); 22891240d137SEric Dumazet } 2290db8dac20SDavid S. Miller return 0; 2291db8dac20SDavid S. Miller } 2292db8dac20SDavid S. Miller 2293db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2294db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2295666a3d6eSSu Yanjun * Otherwise, csum completion requires checksumming packet body, 2296db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2297db8dac20SDavid S. Miller */ 2298db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2299db8dac20SDavid S. Miller int proto) 2300db8dac20SDavid S. Miller { 2301db8dac20SDavid S. Miller int err; 2302db8dac20SDavid S. Miller 2303db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2304db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2305db8dac20SDavid S. Miller 2306db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2307db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2308db8dac20SDavid S. Miller if (err) 2309db8dac20SDavid S. Miller return err; 231015f35d49SAlexey Kodanev 231115f35d49SAlexey Kodanev if (UDP_SKB_CB(skb)->partial_cov) { 231215f35d49SAlexey Kodanev skb->csum = inet_compute_pseudo(skb, proto); 231315f35d49SAlexey Kodanev return 0; 231415f35d49SAlexey Kodanev } 2315db8dac20SDavid S. Miller } 2316db8dac20SDavid S. Miller 2317b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2318b46d9f62SHannes Frederic Sowa * force to int. 2319b46d9f62SHannes Frederic Sowa */ 2320db4f1be3SSean Tranchetti err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2321ed70fcfcSTom Herbert inet_compute_pseudo); 2322db4f1be3SSean Tranchetti if (err) 2323db4f1be3SSean Tranchetti return err; 2324db4f1be3SSean Tranchetti 2325db4f1be3SSean Tranchetti if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2326db4f1be3SSean Tranchetti /* If SW calculated the value, we know it's bad */ 2327db4f1be3SSean Tranchetti if (skb->csum_complete_sw) 2328db4f1be3SSean Tranchetti return 1; 2329db4f1be3SSean Tranchetti 2330db4f1be3SSean Tranchetti /* HW says the value is bad. Let's validate that. 2331db4f1be3SSean Tranchetti * skb->csum is no longer the full packet checksum, 2332db4f1be3SSean Tranchetti * so don't treat it as such. 2333db4f1be3SSean Tranchetti */ 2334db4f1be3SSean Tranchetti skb_checksum_complete_unset(skb); 2335db4f1be3SSean Tranchetti } 2336db4f1be3SSean Tranchetti 2337db4f1be3SSean Tranchetti return 0; 2338db8dac20SDavid S. Miller } 2339db8dac20SDavid S. Miller 23402b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 23412b5a9217SPaolo Abeni * return code conversion for ip layer consumption 23422b5a9217SPaolo Abeni */ 23432b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 23442b5a9217SPaolo Abeni struct udphdr *uh) 23452b5a9217SPaolo Abeni { 23462b5a9217SPaolo Abeni int ret; 23472b5a9217SPaolo Abeni 23482b5a9217SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2349e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); 23502b5a9217SPaolo Abeni 23512b5a9217SPaolo Abeni ret = udp_queue_rcv_skb(sk, skb); 23522b5a9217SPaolo Abeni 23532b5a9217SPaolo Abeni /* a return value > 0 means to resubmit the input, but 23542b5a9217SPaolo Abeni * it wants the return to be -protocol, or 0 23552b5a9217SPaolo Abeni */ 23562b5a9217SPaolo Abeni if (ret > 0) 23572b5a9217SPaolo Abeni return -ret; 23582b5a9217SPaolo Abeni return 0; 23592b5a9217SPaolo Abeni } 23602b5a9217SPaolo Abeni 2361db8dac20SDavid S. Miller /* 2362db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2363db8dac20SDavid S. Miller */ 2364db8dac20SDavid S. Miller 2365645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2366db8dac20SDavid S. Miller int proto) 2367db8dac20SDavid S. Miller { 2368db8dac20SDavid S. Miller struct sock *sk; 23697b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2370db8dac20SDavid S. Miller unsigned short ulen; 2371adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 23722783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 23730283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 237471489e21SJoe Stringer bool refcounted; 23751c7fab70SMenglong Dong int drop_reason; 23761c7fab70SMenglong Dong 23771c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2378db8dac20SDavid S. Miller 2379db8dac20SDavid S. Miller /* 2380db8dac20SDavid S. Miller * Validate the packet. 2381db8dac20SDavid S. Miller */ 2382db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2383db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2384db8dac20SDavid S. Miller 23857b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2386db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2387ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2388ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2389ccc2d97cSBjørn Mork 2390db8dac20SDavid S. Miller if (ulen > skb->len) 2391db8dac20SDavid S. Miller goto short_packet; 2392db8dac20SDavid S. Miller 2393db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2394db8dac20SDavid S. Miller /* UDP validates ulen. */ 2395db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2396db8dac20SDavid S. Miller goto short_packet; 2397db8dac20SDavid S. Miller uh = udp_hdr(skb); 2398db8dac20SDavid S. Miller } 2399db8dac20SDavid S. Miller 2400db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2401db8dac20SDavid S. Miller goto csum_error; 2402db8dac20SDavid S. Miller 24039c02bec9SLorenz Bauer sk = inet_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest, 24049c02bec9SLorenz Bauer &refcounted, udp_ehashfn); 24059c02bec9SLorenz Bauer if (IS_ERR(sk)) 24069c02bec9SLorenz Bauer goto no_sk; 24079c02bec9SLorenz Bauer 24088afdd99aSEric Dumazet if (sk) { 240997502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2410421b3885SShawn Bohrer int ret; 2411421b3885SShawn Bohrer 24128f905c0eSEric Dumazet if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 241397502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2414421b3885SShawn Bohrer 24152b5a9217SPaolo Abeni ret = udp_unicast_rcv_skb(sk, skb, uh); 241671489e21SJoe Stringer if (refcounted) 24178afdd99aSEric Dumazet sock_put(sk); 24182b5a9217SPaolo Abeni return ret; 2419c18450a5SFabian Frederick } 2420c18450a5SFabian Frederick 2421db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2422e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 242336cbb245SRick Jones saddr, daddr, udptable, proto); 2424db8dac20SDavid S. Miller 2425607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 24262b5a9217SPaolo Abeni if (sk) 24272b5a9217SPaolo Abeni return udp_unicast_rcv_skb(sk, skb, uh); 24289c02bec9SLorenz Bauer no_sk: 2429db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2430db8dac20SDavid S. Miller goto drop; 2431895b5c9fSFlorian Westphal nf_reset_ct(skb); 2432db8dac20SDavid S. Miller 2433db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2434db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2435db8dac20SDavid S. Miller goto csum_error; 2436db8dac20SDavid S. Miller 24371c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_NO_SOCKET; 243802c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2439db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2440db8dac20SDavid S. Miller 2441db8dac20SDavid S. Miller /* 2442db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2443db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2444db8dac20SDavid S. Miller */ 24451c7fab70SMenglong Dong kfree_skb_reason(skb, drop_reason); 2446db8dac20SDavid S. Miller return 0; 2447db8dac20SDavid S. Miller 2448db8dac20SDavid S. Miller short_packet: 24491c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL; 2450ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2451afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2452afd46503SJoe Perches &saddr, ntohs(uh->source), 2453afd46503SJoe Perches ulen, skb->len, 2454afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2455db8dac20SDavid S. Miller goto drop; 2456db8dac20SDavid S. Miller 2457db8dac20SDavid S. Miller csum_error: 2458db8dac20SDavid S. Miller /* 2459db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2460db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2461db8dac20SDavid S. Miller */ 24621c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_UDP_CSUM; 2463ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2464afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2465afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2466db8dac20SDavid S. Miller ulen); 246702c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2468db8dac20SDavid S. Miller drop: 246902c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 24701c7fab70SMenglong Dong kfree_skb_reason(skb, drop_reason); 2471db8dac20SDavid S. Miller return 0; 2472db8dac20SDavid S. Miller } 2473db8dac20SDavid S. Miller 2474421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2475421b3885SShawn Bohrer * If more than one socket found returns NULL 2476421b3885SShawn Bohrer */ 2477421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2478421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2479421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2480fb74c277SDavid Ahern int dif, int sdif) 2481421b3885SShawn Bohrer { 2482ba6aac15SKuniyuki Iwashima struct udp_table *udptable = net->ipv4.udp_table; 2483421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2484919dfa0bSKuniyuki Iwashima struct sock *sk, *result; 2485919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot; 2486919dfa0bSKuniyuki Iwashima unsigned int slot; 2487919dfa0bSKuniyuki Iwashima 2488ba6aac15SKuniyuki Iwashima slot = udp_hashfn(net, hnum, udptable->mask); 2489ba6aac15SKuniyuki Iwashima hslot = &udptable->hash[slot]; 2490421b3885SShawn Bohrer 249163c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 249263c6f81cSEric Dumazet if (hslot->count > 10) 249363c6f81cSEric Dumazet return NULL; 249463c6f81cSEric Dumazet 2495421b3885SShawn Bohrer result = NULL; 2496ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2497ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2498fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2499ca065d0cSEric Dumazet if (result) 2500ca065d0cSEric Dumazet return NULL; 2501421b3885SShawn Bohrer result = sk; 2502421b3885SShawn Bohrer } 2503421b3885SShawn Bohrer } 2504421b3885SShawn Bohrer 2505421b3885SShawn Bohrer return result; 2506421b3885SShawn Bohrer } 2507421b3885SShawn Bohrer 2508421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2509421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2510421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2511421b3885SShawn Bohrer */ 2512421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2513421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2514421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 25153fa6f616SDavid Ahern int dif, int sdif) 2516421b3885SShawn Bohrer { 2517ba6aac15SKuniyuki Iwashima struct udp_table *udptable = net->ipv4.udp_table; 2518c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2519919dfa0bSKuniyuki Iwashima unsigned short hnum = ntohs(loc_port); 2520919dfa0bSKuniyuki Iwashima unsigned int hash2, slot2; 2521919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot2; 2522919dfa0bSKuniyuki Iwashima __portpair ports; 2523ca065d0cSEric Dumazet struct sock *sk; 2524421b3885SShawn Bohrer 2525919dfa0bSKuniyuki Iwashima hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2526ba6aac15SKuniyuki Iwashima slot2 = hash2 & udptable->mask; 2527ba6aac15SKuniyuki Iwashima hslot2 = &udptable->hash2[slot2]; 2528919dfa0bSKuniyuki Iwashima ports = INET_COMBINED_PORTS(rmt_port, hnum); 2529919dfa0bSKuniyuki Iwashima 2530ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2531eda090c3SEric Dumazet if (inet_match(net, sk, acookie, ports, dif, sdif)) 2532ca065d0cSEric Dumazet return sk; 2533421b3885SShawn Bohrer /* Only check first socket in chain */ 2534421b3885SShawn Bohrer break; 2535421b3885SShawn Bohrer } 2536ca065d0cSEric Dumazet return NULL; 2537421b3885SShawn Bohrer } 2538421b3885SShawn Bohrer 25397487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2540421b3885SShawn Bohrer { 2541610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2542bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2543610438b7SEric Dumazet const struct iphdr *iph; 2544610438b7SEric Dumazet const struct udphdr *uh; 2545ca065d0cSEric Dumazet struct sock *sk = NULL; 2546421b3885SShawn Bohrer struct dst_entry *dst; 2547421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2548fb74c277SDavid Ahern int sdif = inet_sdif(skb); 25496e540309SShawn Bohrer int ours; 2550421b3885SShawn Bohrer 2551421b3885SShawn Bohrer /* validate the packet */ 2552421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 25537487449cSPaolo Abeni return 0; 2554421b3885SShawn Bohrer 2555610438b7SEric Dumazet iph = ip_hdr(skb); 2556610438b7SEric Dumazet uh = udp_hdr(skb); 2557610438b7SEric Dumazet 2558996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2559bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 25606e540309SShawn Bohrer 25616e540309SShawn Bohrer if (!in_dev) 25627487449cSPaolo Abeni return 0; 25636e540309SShawn Bohrer 25646e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 25656e540309SShawn Bohrer iph->protocol); 25666e540309SShawn Bohrer if (!ours) 25677487449cSPaolo Abeni return 0; 2568ad0ea198SPaolo Abeni 2569421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2570fb74c277SDavid Ahern uh->source, iph->saddr, 2571fb74c277SDavid Ahern dif, sdif); 25726e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2573421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 25743fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 25756e540309SShawn Bohrer } 2576421b3885SShawn Bohrer 257741c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 25787487449cSPaolo Abeni return 0; 2579421b3885SShawn Bohrer 2580421b3885SShawn Bohrer skb->sk = sk; 258182eabd9eSAlexander Duyck skb->destructor = sock_efree; 25828f905c0eSEric Dumazet dst = rcu_dereference(sk->sk_rx_dst); 2583421b3885SShawn Bohrer 2584421b3885SShawn Bohrer if (dst) 2585421b3885SShawn Bohrer dst = dst_check(dst, 0); 258610e2eb87SEric Dumazet if (dst) { 2587bc044e8dSPaolo Abeni u32 itag = 0; 2588bc044e8dSPaolo Abeni 2589d24406c8SWei Wang /* set noref for now. 2590d24406c8SWei Wang * any place which wants to hold dst has to call 2591d24406c8SWei Wang * dst_hold_safe() 2592d24406c8SWei Wang */ 2593421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2594bc044e8dSPaolo Abeni 2595bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2596bc044e8dSPaolo Abeni * the source on each packet 2597bc044e8dSPaolo Abeni */ 2598bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2599bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 26008d2b51b0SGuillaume Nault iph->saddr, 26018d2b51b0SGuillaume Nault iph->tos & IPTOS_RT_MASK, 2602bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2603421b3885SShawn Bohrer } 26047487449cSPaolo Abeni return 0; 260510e2eb87SEric Dumazet } 2606421b3885SShawn Bohrer 2607db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2608db8dac20SDavid S. Miller { 2609ba6aac15SKuniyuki Iwashima return __udp4_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP); 2610db8dac20SDavid S. Miller } 2611db8dac20SDavid S. Miller 26127d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2613db8dac20SDavid S. Miller { 261444046a59STom Parkin struct udp_sock *up = udp_sk(sk); 26158a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2616a8b897c7SPaolo Abeni 2617a8b897c7SPaolo Abeni /* protects from races with udp_abort() */ 2618a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 2619db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 26208a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 262160fb9567SPaolo Abeni if (static_branch_unlikely(&udp_encap_needed_key)) { 262260fb9567SPaolo Abeni if (up->encap_type) { 262344046a59STom Parkin void (*encap_destroy)(struct sock *sk); 26246aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 262544046a59STom Parkin if (encap_destroy) 262644046a59STom Parkin encap_destroy(sk); 262744046a59STom Parkin } 2628ac9a7f4cSEric Dumazet if (udp_test_bit(ENCAP_ENABLED, sk)) 26299c480601SPaolo Abeni static_branch_dec(&udp_encap_needed_key); 263060fb9567SPaolo Abeni } 2631db8dac20SDavid S. Miller } 2632db8dac20SDavid S. Miller 2633172bf009SSteffen Klassert static void set_xfrm_gro_udp_encap_rcv(__u16 encap_type, unsigned short family, 2634172bf009SSteffen Klassert struct sock *sk) 2635172bf009SSteffen Klassert { 2636172bf009SSteffen Klassert #ifdef CONFIG_XFRM 2637172bf009SSteffen Klassert if (udp_test_bit(GRO_ENABLED, sk) && encap_type == UDP_ENCAP_ESPINUDP) { 2638172bf009SSteffen Klassert if (family == AF_INET) 2639172bf009SSteffen Klassert WRITE_ONCE(udp_sk(sk)->gro_receive, xfrm4_gro_udp_encap_rcv); 2640221ddb72SSteffen Klassert else if (IS_ENABLED(CONFIG_IPV6) && family == AF_INET6) 2641221ddb72SSteffen Klassert WRITE_ONCE(udp_sk(sk)->gro_receive, ipv6_stub->xfrm6_gro_udp_encap_rcv); 2642172bf009SSteffen Klassert } 2643172bf009SSteffen Klassert #endif 2644172bf009SSteffen Klassert } 2645172bf009SSteffen Klassert 26461da177e4SLinus Torvalds /* 26471da177e4SLinus Torvalds * Socket option code for UDP 26481da177e4SLinus Torvalds */ 26494c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 265091ac1ccaSChristoph Hellwig sockptr_t optval, unsigned int optlen, 26514c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 26521da177e4SLinus Torvalds { 26531da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 26541c19448cSTom Herbert int val, valbool; 26551da177e4SLinus Torvalds int err = 0; 2656b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 26571da177e4SLinus Torvalds 26588a3854c7SPaolo Abeni if (level == SOL_SOCKET) { 26598a3854c7SPaolo Abeni err = sk_setsockopt(sk, level, optname, optval, optlen); 26608a3854c7SPaolo Abeni 26618a3854c7SPaolo Abeni if (optname == SO_RCVBUF || optname == SO_RCVBUFFORCE) { 26628a3854c7SPaolo Abeni sockopt_lock_sock(sk); 26638a3854c7SPaolo Abeni /* paired with READ_ONCE in udp_rmem_release() */ 26648a3854c7SPaolo Abeni WRITE_ONCE(up->forward_threshold, sk->sk_rcvbuf >> 2); 26658a3854c7SPaolo Abeni sockopt_release_sock(sk); 26668a3854c7SPaolo Abeni } 26678a3854c7SPaolo Abeni return err; 26688a3854c7SPaolo Abeni } 26698a3854c7SPaolo Abeni 26701da177e4SLinus Torvalds if (optlen < sizeof(int)) 26711da177e4SLinus Torvalds return -EINVAL; 26721da177e4SLinus Torvalds 267391ac1ccaSChristoph Hellwig if (copy_from_sockptr(&val, optval, sizeof(val))) 26741da177e4SLinus Torvalds return -EFAULT; 26751da177e4SLinus Torvalds 26761c19448cSTom Herbert valbool = val ? 1 : 0; 26771c19448cSTom Herbert 26781da177e4SLinus Torvalds switch (optname) { 26791da177e4SLinus Torvalds case UDP_CORK: 26801da177e4SLinus Torvalds if (val != 0) { 268181b36803SEric Dumazet udp_set_bit(CORK, sk); 26821da177e4SLinus Torvalds } else { 268381b36803SEric Dumazet udp_clear_bit(CORK, sk); 26841da177e4SLinus Torvalds lock_sock(sk); 26854243cdc2SJoe Perches push_pending_frames(sk); 26861da177e4SLinus Torvalds release_sock(sk); 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds break; 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds case UDP_ENCAP: 26911da177e4SLinus Torvalds switch (val) { 26921da177e4SLinus Torvalds case 0: 2693fd1ac07fSAlexey Dobriyan #ifdef CONFIG_XFRM 26941da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 2695172bf009SSteffen Klassert set_xfrm_gro_udp_encap_rcv(val, sk->sk_family, sk); 2696172bf009SSteffen Klassert fallthrough; 26971da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 26980146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 26990146dca7SSabrina Dubroca if (sk->sk_family == AF_INET6) 27006d5a12ebSEric Dumazet WRITE_ONCE(up->encap_rcv, 27016d5a12ebSEric Dumazet ipv6_stub->xfrm6_udp_encap_rcv); 27020146dca7SSabrina Dubroca else 27030146dca7SSabrina Dubroca #endif 27046d5a12ebSEric Dumazet WRITE_ONCE(up->encap_rcv, 27056d5a12ebSEric Dumazet xfrm4_udp_encap_rcv); 2706fd1ac07fSAlexey Dobriyan #endif 2707a8eceea8SJoe Perches fallthrough; 2708342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 270970a36f57SEric Dumazet WRITE_ONCE(up->encap_type, val); 2710ac9a7f4cSEric Dumazet udp_tunnel_encap_enable(sk); 27111da177e4SLinus Torvalds break; 27121da177e4SLinus Torvalds default: 27131da177e4SLinus Torvalds err = -ENOPROTOOPT; 27141da177e4SLinus Torvalds break; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds break; 27171da177e4SLinus Torvalds 27181c19448cSTom Herbert case UDP_NO_CHECK6_TX: 2719a0002127SEric Dumazet udp_set_no_check6_tx(sk, valbool); 27201c19448cSTom Herbert break; 27211c19448cSTom Herbert 27221c19448cSTom Herbert case UDP_NO_CHECK6_RX: 2723bcbc1b1dSEric Dumazet udp_set_no_check6_rx(sk, valbool); 27241c19448cSTom Herbert break; 27251c19448cSTom Herbert 2726bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2727bec1f6f6SWillem de Bruijn if (val < 0 || val > USHRT_MAX) 2728bec1f6f6SWillem de Bruijn return -EINVAL; 272918a419baSEric Dumazet WRITE_ONCE(up->gso_size, val); 2730bec1f6f6SWillem de Bruijn break; 2731bec1f6f6SWillem de Bruijn 2732e20cf8d3SPaolo Abeni case UDP_GRO: 273378352f73SPaolo Abeni 273478352f73SPaolo Abeni /* when enabling GRO, accept the related GSO packet type */ 2735e20cf8d3SPaolo Abeni if (valbool) 2736ac9a7f4cSEric Dumazet udp_tunnel_encap_enable(sk); 2737e1dc0615SEric Dumazet udp_assign_bit(GRO_ENABLED, sk, valbool); 2738f5f52f08SEric Dumazet udp_assign_bit(ACCEPT_L4, sk, valbool); 2739172bf009SSteffen Klassert set_xfrm_gro_udp_encap_rcv(up->encap_type, sk->sk_family, sk); 2740e20cf8d3SPaolo Abeni break; 2741e20cf8d3SPaolo Abeni 2742ba4e58ecSGerrit Renker /* 2743ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2744ba4e58ecSGerrit Renker */ 2745ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2746ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2747ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2748b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2749ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2750ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2751ba4e58ecSGerrit Renker val = 8; 27524be929beSAlexey Dobriyan else if (val > USHRT_MAX) 27534be929beSAlexey Dobriyan val = USHRT_MAX; 2754882af43aSEric Dumazet WRITE_ONCE(up->pcslen, val); 2755882af43aSEric Dumazet udp_set_bit(UDPLITE_SEND_CC, sk); 2756ba4e58ecSGerrit Renker break; 2757ba4e58ecSGerrit Renker 2758ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2759ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2760ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2761ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2762b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2763ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2764ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2765ba4e58ecSGerrit Renker val = 8; 27664be929beSAlexey Dobriyan else if (val > USHRT_MAX) 27674be929beSAlexey Dobriyan val = USHRT_MAX; 2768882af43aSEric Dumazet WRITE_ONCE(up->pcrlen, val); 2769882af43aSEric Dumazet udp_set_bit(UDPLITE_RECV_CC, sk); 2770ba4e58ecSGerrit Renker break; 2771ba4e58ecSGerrit Renker 27721da177e4SLinus Torvalds default: 27731da177e4SLinus Torvalds err = -ENOPROTOOPT; 27741da177e4SLinus Torvalds break; 27756516c655SStephen Hemminger } 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds return err; 27781da177e4SLinus Torvalds } 2779c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 27801da177e4SLinus Torvalds 2781a7b75c5aSChristoph Hellwig int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 2782a7b75c5aSChristoph Hellwig unsigned int optlen) 2783db8dac20SDavid S. Miller { 27848a3854c7SPaolo Abeni if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET) 278591ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 2786a7b75c5aSChristoph Hellwig optval, optlen, 2787db8dac20SDavid S. Miller udp_push_pending_frames); 2788db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2789db8dac20SDavid S. Miller } 2790db8dac20SDavid S. Miller 27914c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 27921da177e4SLinus Torvalds char __user *optval, int __user *optlen) 27931da177e4SLinus Torvalds { 27941da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 27951da177e4SLinus Torvalds int val, len; 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds if (get_user(len, optlen)) 27981da177e4SLinus Torvalds return -EFAULT; 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds if (len < 0) 28031da177e4SLinus Torvalds return -EINVAL; 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds switch (optname) { 28061da177e4SLinus Torvalds case UDP_CORK: 280781b36803SEric Dumazet val = udp_test_bit(CORK, sk); 28081da177e4SLinus Torvalds break; 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds case UDP_ENCAP: 281170a36f57SEric Dumazet val = READ_ONCE(up->encap_type); 28121da177e4SLinus Torvalds break; 28131da177e4SLinus Torvalds 28141c19448cSTom Herbert case UDP_NO_CHECK6_TX: 2815a0002127SEric Dumazet val = udp_get_no_check6_tx(sk); 28161c19448cSTom Herbert break; 28171c19448cSTom Herbert 28181c19448cSTom Herbert case UDP_NO_CHECK6_RX: 2819bcbc1b1dSEric Dumazet val = udp_get_no_check6_rx(sk); 28201c19448cSTom Herbert break; 28211c19448cSTom Herbert 2822bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 282318a419baSEric Dumazet val = READ_ONCE(up->gso_size); 2824bec1f6f6SWillem de Bruijn break; 2825bec1f6f6SWillem de Bruijn 282698184612SNorman Maurer case UDP_GRO: 2827e1dc0615SEric Dumazet val = udp_test_bit(GRO_ENABLED, sk); 282898184612SNorman Maurer break; 282998184612SNorman Maurer 2830ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2831ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2832ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2833882af43aSEric Dumazet val = READ_ONCE(up->pcslen); 2834ba4e58ecSGerrit Renker break; 2835ba4e58ecSGerrit Renker 2836ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2837882af43aSEric Dumazet val = READ_ONCE(up->pcrlen); 2838ba4e58ecSGerrit Renker break; 2839ba4e58ecSGerrit Renker 28401da177e4SLinus Torvalds default: 28411da177e4SLinus Torvalds return -ENOPROTOOPT; 28426516c655SStephen Hemminger } 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds if (put_user(len, optlen)) 28451da177e4SLinus Torvalds return -EFAULT; 28461da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 28471da177e4SLinus Torvalds return -EFAULT; 28481da177e4SLinus Torvalds return 0; 28491da177e4SLinus Torvalds } 2850c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 28511da177e4SLinus Torvalds 2852db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2853db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2854db8dac20SDavid S. Miller { 2855db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2856db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2857db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2858db8dac20SDavid S. Miller } 2859db8dac20SDavid S. Miller 28601da177e4SLinus Torvalds /** 28611da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 28623628e3cbSAndrew Lunn * @file: - file struct 28633628e3cbSAndrew Lunn * @sock: - socket 28643628e3cbSAndrew Lunn * @wait: - poll table 28651da177e4SLinus Torvalds * 28661da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 28671da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 28681da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 28691da177e4SLinus Torvalds * then it could get return from select indicating data available 28701da177e4SLinus Torvalds * but then block when reading it. Add special case code 28711da177e4SLinus Torvalds * to work around these arguably broken applications. 28721da177e4SLinus Torvalds */ 2873a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 28741da177e4SLinus Torvalds { 2875a11e1d43SLinus Torvalds __poll_t mask = datagram_poll(file, sock, wait); 28761da177e4SLinus Torvalds struct sock *sk = sock->sk; 28771da177e4SLinus Torvalds 28783ef7cf57SEric Dumazet if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) 2879a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 28802276f58aSPaolo Abeni 28811da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 2882a11e1d43SLinus Torvalds if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2883e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2884a9a08845SLinus Torvalds mask &= ~(EPOLLIN | EPOLLRDNORM); 28851da177e4SLinus Torvalds 2886af493388SCong Wang /* psock ingress_msg queue should not contain any bad checksum frames */ 2887af493388SCong Wang if (sk_is_readable(sk)) 2888af493388SCong Wang mask |= EPOLLIN | EPOLLRDNORM; 28891da177e4SLinus Torvalds return mask; 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds } 2892a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll); 28931da177e4SLinus Torvalds 28945d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 28955d77dca8SDavid Ahern { 28964ddbcb88SAditi Ghag if (!has_current_bpf_ctx()) 28975d77dca8SDavid Ahern lock_sock(sk); 28985d77dca8SDavid Ahern 2899a8b897c7SPaolo Abeni /* udp{v6}_destroy_sock() sets it under the sk lock, avoid racing 2900a8b897c7SPaolo Abeni * with close() 2901a8b897c7SPaolo Abeni */ 2902a8b897c7SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 2903a8b897c7SPaolo Abeni goto out; 2904a8b897c7SPaolo Abeni 29055d77dca8SDavid Ahern sk->sk_err = err; 2906e3ae2365SAlexander Aring sk_error_report(sk); 2907286c72deSEric Dumazet __udp_disconnect(sk, 0); 29085d77dca8SDavid Ahern 2909a8b897c7SPaolo Abeni out: 29104ddbcb88SAditi Ghag if (!has_current_bpf_ctx()) 29115d77dca8SDavid Ahern release_sock(sk); 29125d77dca8SDavid Ahern 29135d77dca8SDavid Ahern return 0; 29145d77dca8SDavid Ahern } 29155d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 29165d77dca8SDavid Ahern 2917db8dac20SDavid S. Miller struct proto udp_prot = { 2918db8dac20SDavid S. Miller .name = "UDP", 2919db8dac20SDavid S. Miller .owner = THIS_MODULE, 2920db8dac20SDavid S. Miller .close = udp_lib_close, 2921d74bad4eSAndrey Ignatov .pre_connect = udp_pre_connect, 2922db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2923db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2924db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2925850cbaddSPaolo Abeni .init = udp_init_sock, 2926db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2927db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2928db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2929db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2930db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 29311d7e4538SDavid Howells .splice_eof = udp_splice_eof, 29328141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2933db8dac20SDavid S. Miller .hash = udp_lib_hash, 2934db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2935719f8358SEric Dumazet .rehash = udp_v4_rehash, 2936db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 293791a760b2SMenglong Dong .put_port = udp_lib_unhash, 29388a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL 29398a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto, 29408a59f9d1SCong Wang #endif 2941db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 29420defbb0aSEric Dumazet .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc, 29430defbb0aSEric Dumazet 2944db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 29451e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 29461e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2947db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 294867fb4330SKuniyuki Iwashima .h.udp_table = NULL, 29495d77dca8SDavid Ahern .diag_destroy = udp_abort, 2950db8dac20SDavid S. Miller }; 2951c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 29541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 29551da177e4SLinus Torvalds 2956f44b1c51SAditi Ghag static unsigned short seq_file_family(const struct seq_file *seq); 2957f44b1c51SAditi Ghag static bool seq_sk_match(struct seq_file *seq, const struct sock *sk) 2958f44b1c51SAditi Ghag { 2959f44b1c51SAditi Ghag unsigned short family = seq_file_family(seq); 2960f44b1c51SAditi Ghag 2961f44b1c51SAditi Ghag /* AF_UNSPEC is used as a match all */ 2962f44b1c51SAditi Ghag return ((family == AF_UNSPEC || family == sk->sk_family) && 2963f44b1c51SAditi Ghag net_eq(sock_net(sk), seq_file_net(seq))); 2964f44b1c51SAditi Ghag } 2965f44b1c51SAditi Ghag 2966e4fe1bf1SAditi Ghag #ifdef CONFIG_BPF_SYSCALL 2967e4fe1bf1SAditi Ghag static const struct seq_operations bpf_iter_udp_seq_ops; 2968e4fe1bf1SAditi Ghag #endif 29697625d2e9SAditi Ghag static struct udp_table *udp_get_table_seq(struct seq_file *seq, 2970478aee5dSKuniyuki Iwashima struct net *net) 2971478aee5dSKuniyuki Iwashima { 29727625d2e9SAditi Ghag const struct udp_seq_afinfo *afinfo; 29737625d2e9SAditi Ghag 2974e4fe1bf1SAditi Ghag #ifdef CONFIG_BPF_SYSCALL 2975e4fe1bf1SAditi Ghag if (seq->op == &bpf_iter_udp_seq_ops) 29767625d2e9SAditi Ghag return net->ipv4.udp_table; 2977e4fe1bf1SAditi Ghag #endif 29787625d2e9SAditi Ghag 29797625d2e9SAditi Ghag afinfo = pde_data(file_inode(seq->file)); 2980478aee5dSKuniyuki Iwashima return afinfo->udp_table ? : net->ipv4.udp_table; 2981478aee5dSKuniyuki Iwashima } 2982478aee5dSKuniyuki Iwashima 2983645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 29841da177e4SLinus Torvalds { 29851da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 29866f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 2987478aee5dSKuniyuki Iwashima struct udp_table *udptable; 2988919dfa0bSKuniyuki Iwashima struct sock *sk; 29891da177e4SLinus Torvalds 29907625d2e9SAditi Ghag udptable = udp_get_table_seq(seq, net); 2991478aee5dSKuniyuki Iwashima 2992478aee5dSKuniyuki Iwashima for (state->bucket = start; state->bucket <= udptable->mask; 2993f86dcc5aSEric Dumazet ++state->bucket) { 2994478aee5dSKuniyuki Iwashima struct udp_hslot *hslot = &udptable->hash[state->bucket]; 2995f86dcc5aSEric Dumazet 2996ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2997f86dcc5aSEric Dumazet continue; 2998f86dcc5aSEric Dumazet 2999645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 3000ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 3001f44b1c51SAditi Ghag if (seq_sk_match(seq, sk)) 30021da177e4SLinus Torvalds goto found; 30031da177e4SLinus Torvalds } 3004645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds sk = NULL; 30071da177e4SLinus Torvalds found: 30081da177e4SLinus Torvalds return sk; 30091da177e4SLinus Torvalds } 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 30121da177e4SLinus Torvalds { 30131da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 30146f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 3015478aee5dSKuniyuki Iwashima struct udp_table *udptable; 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds do { 3018ca065d0cSEric Dumazet sk = sk_next(sk); 3019f44b1c51SAditi Ghag } while (sk && !seq_sk_match(seq, sk)); 30201da177e4SLinus Torvalds 3021645ca708SEric Dumazet if (!sk) { 30227625d2e9SAditi Ghag udptable = udp_get_table_seq(seq, net); 3023478aee5dSKuniyuki Iwashima 3024478aee5dSKuniyuki Iwashima if (state->bucket <= udptable->mask) 3025478aee5dSKuniyuki Iwashima spin_unlock_bh(&udptable->hash[state->bucket].lock); 3026478aee5dSKuniyuki Iwashima 3027645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 30281da177e4SLinus Torvalds } 30291da177e4SLinus Torvalds return sk; 30301da177e4SLinus Torvalds } 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 30331da177e4SLinus Torvalds { 3034645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds if (sk) 30371da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 30381da177e4SLinus Torvalds --pos; 30391da177e4SLinus Torvalds return pos ? NULL : sk; 30401da177e4SLinus Torvalds } 30411da177e4SLinus Torvalds 3042a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos) 30431da177e4SLinus Torvalds { 304430842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 3045f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 304630842f29SVitaly Mayatskikh 3047b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 30481da177e4SLinus Torvalds } 3049a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start); 30501da177e4SLinus Torvalds 3051a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 30521da177e4SLinus Torvalds { 30531da177e4SLinus Torvalds struct sock *sk; 30541da177e4SLinus Torvalds 3055b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 30561da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 30571da177e4SLinus Torvalds else 30581da177e4SLinus Torvalds sk = udp_get_next(seq, v); 30591da177e4SLinus Torvalds 30601da177e4SLinus Torvalds ++*pos; 30611da177e4SLinus Torvalds return sk; 30621da177e4SLinus Torvalds } 3063a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next); 30641da177e4SLinus Torvalds 3065a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v) 30661da177e4SLinus Torvalds { 3067645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 3068478aee5dSKuniyuki Iwashima struct udp_table *udptable; 3069645ca708SEric Dumazet 30707625d2e9SAditi Ghag udptable = udp_get_table_seq(seq, seq_file_net(seq)); 3071478aee5dSKuniyuki Iwashima 3072478aee5dSKuniyuki Iwashima if (state->bucket <= udptable->mask) 3073478aee5dSKuniyuki Iwashima spin_unlock_bh(&udptable->hash[state->bucket].lock); 30741da177e4SLinus Torvalds } 3075a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop); 3076db8dac20SDavid S. Miller 3077db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 30785e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 3079652586dfSTetsuo Handa int bucket) 3080db8dac20SDavid S. Miller { 3081db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 3082c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 3083c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 3084c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 3085c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 3086db8dac20SDavid S. Miller 3087f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 3088ea9a0379SPatrick Talbert " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", 3089db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 309031e6d363SEric Dumazet sk_wmem_alloc_get(sp), 30916c206b20SPaolo Abeni udp_rqueue_get(sp), 3092a7cb5a49SEric W. Biederman 0, 0L, 0, 3093a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 3094a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 309541c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 3096652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 3097db8dac20SDavid S. Miller } 3098db8dac20SDavid S. Miller 3099db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 3100db8dac20SDavid S. Miller { 3101652586dfSTetsuo Handa seq_setwidth(seq, 127); 3102db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 3103652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 3104db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 3105cb61cb9bSEric Dumazet "inode ref pointer drops"); 3106db8dac20SDavid S. Miller else { 3107db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 3108db8dac20SDavid S. Miller 3109652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 3110db8dac20SDavid S. Miller } 3111652586dfSTetsuo Handa seq_pad(seq, '\n'); 3112db8dac20SDavid S. Miller return 0; 3113db8dac20SDavid S. Miller } 3114db8dac20SDavid S. Miller 31155788b3a0SYonghong Song #ifdef CONFIG_BPF_SYSCALL 31165788b3a0SYonghong Song struct bpf_iter__udp { 31175788b3a0SYonghong Song __bpf_md_ptr(struct bpf_iter_meta *, meta); 31185788b3a0SYonghong Song __bpf_md_ptr(struct udp_sock *, udp_sk); 31195788b3a0SYonghong Song uid_t uid __aligned(8); 31205788b3a0SYonghong Song int bucket __aligned(8); 31215788b3a0SYonghong Song }; 31225788b3a0SYonghong Song 3123c96dac8dSAditi Ghag struct bpf_udp_iter_state { 3124c96dac8dSAditi Ghag struct udp_iter_state state; 3125c96dac8dSAditi Ghag unsigned int cur_sk; 3126c96dac8dSAditi Ghag unsigned int end_sk; 3127c96dac8dSAditi Ghag unsigned int max_sk; 3128c96dac8dSAditi Ghag int offset; 3129c96dac8dSAditi Ghag struct sock **batch; 3130c96dac8dSAditi Ghag bool st_bucket_done; 3131c96dac8dSAditi Ghag }; 3132c96dac8dSAditi Ghag 3133c96dac8dSAditi Ghag static int bpf_iter_udp_realloc_batch(struct bpf_udp_iter_state *iter, 3134c96dac8dSAditi Ghag unsigned int new_batch_sz); 3135c96dac8dSAditi Ghag static struct sock *bpf_iter_udp_batch(struct seq_file *seq) 3136c96dac8dSAditi Ghag { 3137c96dac8dSAditi Ghag struct bpf_udp_iter_state *iter = seq->private; 3138c96dac8dSAditi Ghag struct udp_iter_state *state = &iter->state; 3139c96dac8dSAditi Ghag struct net *net = seq_file_net(seq); 31402242fd53SMartin KaFai Lau int resume_bucket, resume_offset; 3141c96dac8dSAditi Ghag struct udp_table *udptable; 3142c96dac8dSAditi Ghag unsigned int batch_sks = 0; 3143c96dac8dSAditi Ghag bool resized = false; 3144c96dac8dSAditi Ghag struct sock *sk; 3145c96dac8dSAditi Ghag 31462242fd53SMartin KaFai Lau resume_bucket = state->bucket; 31472242fd53SMartin KaFai Lau resume_offset = iter->offset; 31482242fd53SMartin KaFai Lau 3149c96dac8dSAditi Ghag /* The current batch is done, so advance the bucket. */ 31502242fd53SMartin KaFai Lau if (iter->st_bucket_done) 3151c96dac8dSAditi Ghag state->bucket++; 3152c96dac8dSAditi Ghag 3153c96dac8dSAditi Ghag udptable = udp_get_table_seq(seq, net); 3154c96dac8dSAditi Ghag 3155c96dac8dSAditi Ghag again: 3156c96dac8dSAditi Ghag /* New batch for the next bucket. 3157c96dac8dSAditi Ghag * Iterate over the hash table to find a bucket with sockets matching 3158c96dac8dSAditi Ghag * the iterator attributes, and return the first matching socket from 3159c96dac8dSAditi Ghag * the bucket. The remaining matched sockets from the bucket are batched 3160c96dac8dSAditi Ghag * before releasing the bucket lock. This allows BPF programs that are 3161c96dac8dSAditi Ghag * called in seq_show to acquire the bucket lock if needed. 3162c96dac8dSAditi Ghag */ 3163c96dac8dSAditi Ghag iter->cur_sk = 0; 3164c96dac8dSAditi Ghag iter->end_sk = 0; 3165c96dac8dSAditi Ghag iter->st_bucket_done = false; 3166c96dac8dSAditi Ghag batch_sks = 0; 3167c96dac8dSAditi Ghag 3168c96dac8dSAditi Ghag for (; state->bucket <= udptable->mask; state->bucket++) { 3169c96dac8dSAditi Ghag struct udp_hslot *hslot2 = &udptable->hash2[state->bucket]; 3170c96dac8dSAditi Ghag 31712242fd53SMartin KaFai Lau if (hlist_empty(&hslot2->head)) 3172c96dac8dSAditi Ghag continue; 3173c96dac8dSAditi Ghag 31742242fd53SMartin KaFai Lau iter->offset = 0; 3175c96dac8dSAditi Ghag spin_lock_bh(&hslot2->lock); 3176c96dac8dSAditi Ghag udp_portaddr_for_each_entry(sk, &hslot2->head) { 3177c96dac8dSAditi Ghag if (seq_sk_match(seq, sk)) { 3178c96dac8dSAditi Ghag /* Resume from the last iterated socket at the 3179c96dac8dSAditi Ghag * offset in the bucket before iterator was stopped. 3180c96dac8dSAditi Ghag */ 31812242fd53SMartin KaFai Lau if (state->bucket == resume_bucket && 31822242fd53SMartin KaFai Lau iter->offset < resume_offset) { 31832242fd53SMartin KaFai Lau ++iter->offset; 3184c96dac8dSAditi Ghag continue; 3185c96dac8dSAditi Ghag } 3186c96dac8dSAditi Ghag if (iter->end_sk < iter->max_sk) { 3187c96dac8dSAditi Ghag sock_hold(sk); 3188c96dac8dSAditi Ghag iter->batch[iter->end_sk++] = sk; 3189c96dac8dSAditi Ghag } 3190c96dac8dSAditi Ghag batch_sks++; 3191c96dac8dSAditi Ghag } 3192c96dac8dSAditi Ghag } 3193c96dac8dSAditi Ghag spin_unlock_bh(&hslot2->lock); 3194c96dac8dSAditi Ghag 3195c96dac8dSAditi Ghag if (iter->end_sk) 3196c96dac8dSAditi Ghag break; 3197c96dac8dSAditi Ghag } 3198c96dac8dSAditi Ghag 3199c96dac8dSAditi Ghag /* All done: no batch made. */ 3200c96dac8dSAditi Ghag if (!iter->end_sk) 3201c96dac8dSAditi Ghag return NULL; 3202c96dac8dSAditi Ghag 3203c96dac8dSAditi Ghag if (iter->end_sk == batch_sks) { 3204c96dac8dSAditi Ghag /* Batching is done for the current bucket; return the first 3205c96dac8dSAditi Ghag * socket to be iterated from the batch. 3206c96dac8dSAditi Ghag */ 3207c96dac8dSAditi Ghag iter->st_bucket_done = true; 3208c96dac8dSAditi Ghag goto done; 3209c96dac8dSAditi Ghag } 3210c96dac8dSAditi Ghag if (!resized && !bpf_iter_udp_realloc_batch(iter, batch_sks * 3 / 2)) { 3211c96dac8dSAditi Ghag resized = true; 3212c96dac8dSAditi Ghag /* After allocating a larger batch, retry one more time to grab 3213c96dac8dSAditi Ghag * the whole bucket. 3214c96dac8dSAditi Ghag */ 3215c96dac8dSAditi Ghag goto again; 3216c96dac8dSAditi Ghag } 3217c96dac8dSAditi Ghag done: 3218c96dac8dSAditi Ghag return iter->batch[0]; 3219c96dac8dSAditi Ghag } 3220c96dac8dSAditi Ghag 3221c96dac8dSAditi Ghag static void *bpf_iter_udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3222c96dac8dSAditi Ghag { 3223c96dac8dSAditi Ghag struct bpf_udp_iter_state *iter = seq->private; 3224c96dac8dSAditi Ghag struct sock *sk; 3225c96dac8dSAditi Ghag 3226c96dac8dSAditi Ghag /* Whenever seq_next() is called, the iter->cur_sk is 3227c96dac8dSAditi Ghag * done with seq_show(), so unref the iter->cur_sk. 3228c96dac8dSAditi Ghag */ 3229c96dac8dSAditi Ghag if (iter->cur_sk < iter->end_sk) { 3230c96dac8dSAditi Ghag sock_put(iter->batch[iter->cur_sk++]); 3231c96dac8dSAditi Ghag ++iter->offset; 3232c96dac8dSAditi Ghag } 3233c96dac8dSAditi Ghag 3234c96dac8dSAditi Ghag /* After updating iter->cur_sk, check if there are more sockets 3235c96dac8dSAditi Ghag * available in the current bucket batch. 3236c96dac8dSAditi Ghag */ 3237c96dac8dSAditi Ghag if (iter->cur_sk < iter->end_sk) 3238c96dac8dSAditi Ghag sk = iter->batch[iter->cur_sk]; 3239c96dac8dSAditi Ghag else 3240c96dac8dSAditi Ghag /* Prepare a new batch. */ 3241c96dac8dSAditi Ghag sk = bpf_iter_udp_batch(seq); 3242c96dac8dSAditi Ghag 3243c96dac8dSAditi Ghag ++*pos; 3244c96dac8dSAditi Ghag return sk; 3245c96dac8dSAditi Ghag } 3246c96dac8dSAditi Ghag 3247c96dac8dSAditi Ghag static void *bpf_iter_udp_seq_start(struct seq_file *seq, loff_t *pos) 3248c96dac8dSAditi Ghag { 3249c96dac8dSAditi Ghag /* bpf iter does not support lseek, so it always 3250c96dac8dSAditi Ghag * continue from where it was stop()-ped. 3251c96dac8dSAditi Ghag */ 3252c96dac8dSAditi Ghag if (*pos) 3253c96dac8dSAditi Ghag return bpf_iter_udp_batch(seq); 3254c96dac8dSAditi Ghag 3255c96dac8dSAditi Ghag return SEQ_START_TOKEN; 3256c96dac8dSAditi Ghag } 3257c96dac8dSAditi Ghag 32585788b3a0SYonghong Song static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 32595788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 32605788b3a0SYonghong Song { 32615788b3a0SYonghong Song struct bpf_iter__udp ctx; 32625788b3a0SYonghong Song 32635788b3a0SYonghong Song meta->seq_num--; /* skip SEQ_START_TOKEN */ 32645788b3a0SYonghong Song ctx.meta = meta; 32655788b3a0SYonghong Song ctx.udp_sk = udp_sk; 32665788b3a0SYonghong Song ctx.uid = uid; 32675788b3a0SYonghong Song ctx.bucket = bucket; 32685788b3a0SYonghong Song return bpf_iter_run_prog(prog, &ctx); 32695788b3a0SYonghong Song } 32705788b3a0SYonghong Song 32715788b3a0SYonghong Song static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) 32725788b3a0SYonghong Song { 32735788b3a0SYonghong Song struct udp_iter_state *state = seq->private; 32745788b3a0SYonghong Song struct bpf_iter_meta meta; 32755788b3a0SYonghong Song struct bpf_prog *prog; 32765788b3a0SYonghong Song struct sock *sk = v; 32775788b3a0SYonghong Song uid_t uid; 3278c96dac8dSAditi Ghag int ret; 32795788b3a0SYonghong Song 32805788b3a0SYonghong Song if (v == SEQ_START_TOKEN) 32815788b3a0SYonghong Song return 0; 32825788b3a0SYonghong Song 3283c96dac8dSAditi Ghag lock_sock(sk); 3284c96dac8dSAditi Ghag 3285c96dac8dSAditi Ghag if (unlikely(sk_unhashed(sk))) { 3286c96dac8dSAditi Ghag ret = SEQ_SKIP; 3287c96dac8dSAditi Ghag goto unlock; 3288c96dac8dSAditi Ghag } 3289c96dac8dSAditi Ghag 32905788b3a0SYonghong Song uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); 32915788b3a0SYonghong Song meta.seq = seq; 32925788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, false); 3293c96dac8dSAditi Ghag ret = udp_prog_seq_show(prog, &meta, v, uid, state->bucket); 3294c96dac8dSAditi Ghag 3295c96dac8dSAditi Ghag unlock: 3296c96dac8dSAditi Ghag release_sock(sk); 3297c96dac8dSAditi Ghag return ret; 3298c96dac8dSAditi Ghag } 3299c96dac8dSAditi Ghag 3300c96dac8dSAditi Ghag static void bpf_iter_udp_put_batch(struct bpf_udp_iter_state *iter) 3301c96dac8dSAditi Ghag { 3302c96dac8dSAditi Ghag while (iter->cur_sk < iter->end_sk) 3303c96dac8dSAditi Ghag sock_put(iter->batch[iter->cur_sk++]); 33045788b3a0SYonghong Song } 33055788b3a0SYonghong Song 33065788b3a0SYonghong Song static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) 33075788b3a0SYonghong Song { 3308c96dac8dSAditi Ghag struct bpf_udp_iter_state *iter = seq->private; 33095788b3a0SYonghong Song struct bpf_iter_meta meta; 33105788b3a0SYonghong Song struct bpf_prog *prog; 33115788b3a0SYonghong Song 33125788b3a0SYonghong Song if (!v) { 33135788b3a0SYonghong Song meta.seq = seq; 33145788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, true); 33155788b3a0SYonghong Song if (prog) 33165788b3a0SYonghong Song (void)udp_prog_seq_show(prog, &meta, v, 0, 0); 33175788b3a0SYonghong Song } 33185788b3a0SYonghong Song 3319c96dac8dSAditi Ghag if (iter->cur_sk < iter->end_sk) { 3320c96dac8dSAditi Ghag bpf_iter_udp_put_batch(iter); 3321c96dac8dSAditi Ghag iter->st_bucket_done = false; 3322c96dac8dSAditi Ghag } 33235788b3a0SYonghong Song } 33245788b3a0SYonghong Song 33255788b3a0SYonghong Song static const struct seq_operations bpf_iter_udp_seq_ops = { 3326c96dac8dSAditi Ghag .start = bpf_iter_udp_seq_start, 3327c96dac8dSAditi Ghag .next = bpf_iter_udp_seq_next, 33285788b3a0SYonghong Song .stop = bpf_iter_udp_seq_stop, 33295788b3a0SYonghong Song .show = bpf_iter_udp_seq_show, 33305788b3a0SYonghong Song }; 33315788b3a0SYonghong Song #endif 33325788b3a0SYonghong Song 3333f44b1c51SAditi Ghag static unsigned short seq_file_family(const struct seq_file *seq) 3334f44b1c51SAditi Ghag { 3335f44b1c51SAditi Ghag const struct udp_seq_afinfo *afinfo; 3336f44b1c51SAditi Ghag 3337f44b1c51SAditi Ghag #ifdef CONFIG_BPF_SYSCALL 3338f44b1c51SAditi Ghag /* BPF iterator: bpf programs to filter sockets. */ 3339f44b1c51SAditi Ghag if (seq->op == &bpf_iter_udp_seq_ops) 3340f44b1c51SAditi Ghag return AF_UNSPEC; 3341f44b1c51SAditi Ghag #endif 3342f44b1c51SAditi Ghag 3343f44b1c51SAditi Ghag /* Proc fs iterator */ 3344f44b1c51SAditi Ghag afinfo = pde_data(file_inode(seq->file)); 3345f44b1c51SAditi Ghag return afinfo->family; 3346f44b1c51SAditi Ghag } 3347f44b1c51SAditi Ghag 3348c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = { 3349a3d2599bSChristoph Hellwig .start = udp_seq_start, 3350a3d2599bSChristoph Hellwig .next = udp_seq_next, 3351a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 3352a3d2599bSChristoph Hellwig .show = udp4_seq_show, 3353a3d2599bSChristoph Hellwig }; 3354c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops); 335573cb88ecSArjan van de Ven 3356db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 3357db8dac20SDavid S. Miller .family = AF_INET, 3358478aee5dSKuniyuki Iwashima .udp_table = NULL, 3359db8dac20SDavid S. Miller }; 3360db8dac20SDavid S. Miller 33612c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 336215439febSPavel Emelyanov { 3363c3506372SChristoph Hellwig if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 3364c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 3365a3d2599bSChristoph Hellwig return -ENOMEM; 3366a3d2599bSChristoph Hellwig return 0; 336715439febSPavel Emelyanov } 336815439febSPavel Emelyanov 33692c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 337015439febSPavel Emelyanov { 3371a3d2599bSChristoph Hellwig remove_proc_entry("udp", net->proc_net); 337215439febSPavel Emelyanov } 337315439febSPavel Emelyanov 337415439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 337515439febSPavel Emelyanov .init = udp4_proc_init_net, 337615439febSPavel Emelyanov .exit = udp4_proc_exit_net, 337715439febSPavel Emelyanov }; 337815439febSPavel Emelyanov 3379db8dac20SDavid S. Miller int __init udp4_proc_init(void) 3380db8dac20SDavid S. Miller { 338115439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 3382db8dac20SDavid S. Miller } 3383db8dac20SDavid S. Miller 3384db8dac20SDavid S. Miller void udp4_proc_exit(void) 3385db8dac20SDavid S. Miller { 338615439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 3387db8dac20SDavid S. Miller } 33881da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 33891da177e4SLinus Torvalds 3390f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 3391f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 3392645ca708SEric Dumazet { 3393413c27d8SEldad Zack ssize_t ret; 3394413c27d8SEldad Zack 3395f86dcc5aSEric Dumazet if (!str) 3396f86dcc5aSEric Dumazet return 0; 3397413c27d8SEldad Zack 3398413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 3399413c27d8SEldad Zack if (ret) 3400413c27d8SEldad Zack return 0; 3401413c27d8SEldad Zack 3402f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 3403f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 3404f86dcc5aSEric Dumazet return 1; 3405f86dcc5aSEric Dumazet } 3406f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 3407645ca708SEric Dumazet 3408f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 3409f86dcc5aSEric Dumazet { 3410f86dcc5aSEric Dumazet unsigned int i; 3411f86dcc5aSEric Dumazet 3412f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 3413512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 3414f86dcc5aSEric Dumazet uhash_entries, 3415f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 3416f86dcc5aSEric Dumazet 0, 3417f86dcc5aSEric Dumazet &table->log, 3418f86dcc5aSEric Dumazet &table->mask, 341931fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 34209804985bSKuniyuki Iwashima UDP_HTABLE_SIZE_MAX); 342131fe62b9STim Bird 3422512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 3423f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 3424ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 3425fdcc8aa9SEric Dumazet table->hash[i].count = 0; 3426645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 3427645ca708SEric Dumazet } 3428512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 3429ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 3430512615b6SEric Dumazet table->hash2[i].count = 0; 3431512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 3432512615b6SEric Dumazet } 3433645ca708SEric Dumazet } 3434645ca708SEric Dumazet 3435723b8e46STom Herbert u32 udp_flow_hashrnd(void) 3436723b8e46STom Herbert { 3437723b8e46STom Herbert static u32 hashrnd __read_mostly; 3438723b8e46STom Herbert 3439723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 3440723b8e46STom Herbert 3441723b8e46STom Herbert return hashrnd; 3442723b8e46STom Herbert } 3443723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 3444723b8e46STom Herbert 34459804985bSKuniyuki Iwashima static void __net_init udp_sysctl_init(struct net *net) 34461e802951STonghao Zhang { 3447100fdd1fSEric Dumazet net->ipv4.sysctl_udp_rmem_min = PAGE_SIZE; 3448100fdd1fSEric Dumazet net->ipv4.sysctl_udp_wmem_min = PAGE_SIZE; 34491e802951STonghao Zhang 34501e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV 34511e802951STonghao Zhang net->ipv4.sysctl_udp_l3mdev_accept = 0; 34521e802951STonghao Zhang #endif 34539804985bSKuniyuki Iwashima } 34549804985bSKuniyuki Iwashima 34559804985bSKuniyuki Iwashima static struct udp_table __net_init *udp_pernet_table_alloc(unsigned int hash_entries) 34569804985bSKuniyuki Iwashima { 34579804985bSKuniyuki Iwashima struct udp_table *udptable; 34589804985bSKuniyuki Iwashima int i; 34599804985bSKuniyuki Iwashima 34609804985bSKuniyuki Iwashima udptable = kmalloc(sizeof(*udptable), GFP_KERNEL); 34619804985bSKuniyuki Iwashima if (!udptable) 34629804985bSKuniyuki Iwashima goto out; 34639804985bSKuniyuki Iwashima 34649804985bSKuniyuki Iwashima udptable->hash = vmalloc_huge(hash_entries * 2 * sizeof(struct udp_hslot), 34659804985bSKuniyuki Iwashima GFP_KERNEL_ACCOUNT); 34669804985bSKuniyuki Iwashima if (!udptable->hash) 34679804985bSKuniyuki Iwashima goto free_table; 34689804985bSKuniyuki Iwashima 34699804985bSKuniyuki Iwashima udptable->hash2 = udptable->hash + hash_entries; 34709804985bSKuniyuki Iwashima udptable->mask = hash_entries - 1; 34719804985bSKuniyuki Iwashima udptable->log = ilog2(hash_entries); 34729804985bSKuniyuki Iwashima 34739804985bSKuniyuki Iwashima for (i = 0; i < hash_entries; i++) { 34749804985bSKuniyuki Iwashima INIT_HLIST_HEAD(&udptable->hash[i].head); 34759804985bSKuniyuki Iwashima udptable->hash[i].count = 0; 34769804985bSKuniyuki Iwashima spin_lock_init(&udptable->hash[i].lock); 34779804985bSKuniyuki Iwashima 34789804985bSKuniyuki Iwashima INIT_HLIST_HEAD(&udptable->hash2[i].head); 34799804985bSKuniyuki Iwashima udptable->hash2[i].count = 0; 34809804985bSKuniyuki Iwashima spin_lock_init(&udptable->hash2[i].lock); 34819804985bSKuniyuki Iwashima } 34829804985bSKuniyuki Iwashima 34839804985bSKuniyuki Iwashima return udptable; 34849804985bSKuniyuki Iwashima 34859804985bSKuniyuki Iwashima free_table: 34869804985bSKuniyuki Iwashima kfree(udptable); 34879804985bSKuniyuki Iwashima out: 34889804985bSKuniyuki Iwashima return NULL; 34899804985bSKuniyuki Iwashima } 34909804985bSKuniyuki Iwashima 34919804985bSKuniyuki Iwashima static void __net_exit udp_pernet_table_free(struct net *net) 34929804985bSKuniyuki Iwashima { 34939804985bSKuniyuki Iwashima struct udp_table *udptable = net->ipv4.udp_table; 34949804985bSKuniyuki Iwashima 34959804985bSKuniyuki Iwashima if (udptable == &udp_table) 34969804985bSKuniyuki Iwashima return; 34979804985bSKuniyuki Iwashima 34989804985bSKuniyuki Iwashima kvfree(udptable->hash); 34999804985bSKuniyuki Iwashima kfree(udptable); 35009804985bSKuniyuki Iwashima } 35019804985bSKuniyuki Iwashima 35029804985bSKuniyuki Iwashima static void __net_init udp_set_table(struct net *net) 35039804985bSKuniyuki Iwashima { 35049804985bSKuniyuki Iwashima struct udp_table *udptable; 35059804985bSKuniyuki Iwashima unsigned int hash_entries; 35069804985bSKuniyuki Iwashima struct net *old_net; 35079804985bSKuniyuki Iwashima 35089804985bSKuniyuki Iwashima if (net_eq(net, &init_net)) 35099804985bSKuniyuki Iwashima goto fallback; 35109804985bSKuniyuki Iwashima 35119804985bSKuniyuki Iwashima old_net = current->nsproxy->net_ns; 35129804985bSKuniyuki Iwashima hash_entries = READ_ONCE(old_net->ipv4.sysctl_udp_child_hash_entries); 35139804985bSKuniyuki Iwashima if (!hash_entries) 35149804985bSKuniyuki Iwashima goto fallback; 35159804985bSKuniyuki Iwashima 35169804985bSKuniyuki Iwashima /* Set min to keep the bitmap on stack in udp_lib_get_port() */ 35179804985bSKuniyuki Iwashima if (hash_entries < UDP_HTABLE_SIZE_MIN_PERNET) 35189804985bSKuniyuki Iwashima hash_entries = UDP_HTABLE_SIZE_MIN_PERNET; 35199804985bSKuniyuki Iwashima else 35209804985bSKuniyuki Iwashima hash_entries = roundup_pow_of_two(hash_entries); 35219804985bSKuniyuki Iwashima 35229804985bSKuniyuki Iwashima udptable = udp_pernet_table_alloc(hash_entries); 35239804985bSKuniyuki Iwashima if (udptable) { 35249804985bSKuniyuki Iwashima net->ipv4.udp_table = udptable; 35259804985bSKuniyuki Iwashima } else { 35269804985bSKuniyuki Iwashima pr_warn("Failed to allocate UDP hash table (entries: %u) " 35279804985bSKuniyuki Iwashima "for a netns, fallback to the global one\n", 35289804985bSKuniyuki Iwashima hash_entries); 35299804985bSKuniyuki Iwashima fallback: 35309804985bSKuniyuki Iwashima net->ipv4.udp_table = &udp_table; 35319804985bSKuniyuki Iwashima } 35329804985bSKuniyuki Iwashima } 35339804985bSKuniyuki Iwashima 35349804985bSKuniyuki Iwashima static int __net_init udp_pernet_init(struct net *net) 35359804985bSKuniyuki Iwashima { 35369804985bSKuniyuki Iwashima udp_sysctl_init(net); 35379804985bSKuniyuki Iwashima udp_set_table(net); 35381e802951STonghao Zhang 35391e802951STonghao Zhang return 0; 35401e802951STonghao Zhang } 35411e802951STonghao Zhang 35429804985bSKuniyuki Iwashima static void __net_exit udp_pernet_exit(struct net *net) 35439804985bSKuniyuki Iwashima { 35449804985bSKuniyuki Iwashima udp_pernet_table_free(net); 35459804985bSKuniyuki Iwashima } 35469804985bSKuniyuki Iwashima 35471e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = { 35489804985bSKuniyuki Iwashima .init = udp_pernet_init, 35499804985bSKuniyuki Iwashima .exit = udp_pernet_exit, 35501e802951STonghao Zhang }; 35511e802951STonghao Zhang 35525788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 35535788b3a0SYonghong Song DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, 35545788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 35555788b3a0SYonghong Song 3556c96dac8dSAditi Ghag static int bpf_iter_udp_realloc_batch(struct bpf_udp_iter_state *iter, 3557c96dac8dSAditi Ghag unsigned int new_batch_sz) 35585788b3a0SYonghong Song { 3559c96dac8dSAditi Ghag struct sock **new_batch; 35605788b3a0SYonghong Song 3561c96dac8dSAditi Ghag new_batch = kvmalloc_array(new_batch_sz, sizeof(*new_batch), 3562c96dac8dSAditi Ghag GFP_USER | __GFP_NOWARN); 3563c96dac8dSAditi Ghag if (!new_batch) 35645788b3a0SYonghong Song return -ENOMEM; 35655788b3a0SYonghong Song 3566c96dac8dSAditi Ghag bpf_iter_udp_put_batch(iter); 3567c96dac8dSAditi Ghag kvfree(iter->batch); 3568c96dac8dSAditi Ghag iter->batch = new_batch; 3569c96dac8dSAditi Ghag iter->max_sk = new_batch_sz; 3570c96dac8dSAditi Ghag 3571c96dac8dSAditi Ghag return 0; 3572c96dac8dSAditi Ghag } 3573c96dac8dSAditi Ghag 3574c96dac8dSAditi Ghag #define INIT_BATCH_SZ 16 3575c96dac8dSAditi Ghag 35765788b3a0SYonghong Song static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux) 35775788b3a0SYonghong Song { 3578c96dac8dSAditi Ghag struct bpf_udp_iter_state *iter = priv_data; 3579c96dac8dSAditi Ghag int ret; 3580c96dac8dSAditi Ghag 3581f9c79272SYonghong Song ret = bpf_iter_init_seq_net(priv_data, aux); 35825788b3a0SYonghong Song if (ret) 3583c96dac8dSAditi Ghag return ret; 3584c96dac8dSAditi Ghag 3585c96dac8dSAditi Ghag ret = bpf_iter_udp_realloc_batch(iter, INIT_BATCH_SZ); 3586c96dac8dSAditi Ghag if (ret) 3587c96dac8dSAditi Ghag bpf_iter_fini_seq_net(priv_data); 3588c96dac8dSAditi Ghag 35895788b3a0SYonghong Song return ret; 35905788b3a0SYonghong Song } 35915788b3a0SYonghong Song 35925788b3a0SYonghong Song static void bpf_iter_fini_udp(void *priv_data) 35935788b3a0SYonghong Song { 3594c96dac8dSAditi Ghag struct bpf_udp_iter_state *iter = priv_data; 35955788b3a0SYonghong Song 35965788b3a0SYonghong Song bpf_iter_fini_seq_net(priv_data); 3597c96dac8dSAditi Ghag kvfree(iter->batch); 35985788b3a0SYonghong Song } 35995788b3a0SYonghong Song 360014fc6bd6SYonghong Song static const struct bpf_iter_seq_info udp_seq_info = { 36015788b3a0SYonghong Song .seq_ops = &bpf_iter_udp_seq_ops, 36025788b3a0SYonghong Song .init_seq_private = bpf_iter_init_udp, 36035788b3a0SYonghong Song .fini_seq_private = bpf_iter_fini_udp, 3604c96dac8dSAditi Ghag .seq_priv_size = sizeof(struct bpf_udp_iter_state), 360514fc6bd6SYonghong Song }; 360614fc6bd6SYonghong Song 360714fc6bd6SYonghong Song static struct bpf_iter_reg udp_reg_info = { 360814fc6bd6SYonghong Song .target = "udp", 36095788b3a0SYonghong Song .ctx_arg_info_size = 1, 36105788b3a0SYonghong Song .ctx_arg_info = { 36115788b3a0SYonghong Song { offsetof(struct bpf_iter__udp, udp_sk), 36124ddbcb88SAditi Ghag PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED }, 36135788b3a0SYonghong Song }, 361414fc6bd6SYonghong Song .seq_info = &udp_seq_info, 36155788b3a0SYonghong Song }; 36165788b3a0SYonghong Song 36175788b3a0SYonghong Song static void __init bpf_iter_register(void) 36185788b3a0SYonghong Song { 3619951cf368SYonghong Song udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP]; 36205788b3a0SYonghong Song if (bpf_iter_reg_target(&udp_reg_info)) 36215788b3a0SYonghong Song pr_warn("Warning: could not register bpf iterator udp\n"); 36225788b3a0SYonghong Song } 36235788b3a0SYonghong Song #endif 36245788b3a0SYonghong Song 362595766fffSHideo Aoki void __init udp_init(void) 362695766fffSHideo Aoki { 3627f03d78dbSEric Dumazet unsigned long limit; 36284b272750SEric Dumazet unsigned int i; 362995766fffSHideo Aoki 3630f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 3631f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 363295766fffSHideo Aoki limit = max(limit, 128UL); 363395766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 363495766fffSHideo Aoki sysctl_udp_mem[1] = limit; 363595766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 363695766fffSHideo Aoki 36374b272750SEric Dumazet /* 16 spinlocks per cpu */ 36384b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 36394b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 36404b272750SEric Dumazet GFP_KERNEL); 36414b272750SEric Dumazet if (!udp_busylocks) 36424b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 36434b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 36444b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 36451e802951STonghao Zhang 36461e802951STonghao Zhang if (register_pernet_subsys(&udp_sysctl_ops)) 36471e802951STonghao Zhang panic("UDP: failed to init sysctl parameters.\n"); 36485788b3a0SYonghong Song 36495788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 36505788b3a0SYonghong Song bpf_iter_register(); 36515788b3a0SYonghong Song #endif 365295766fffSHideo Aoki } 3653