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> 1061da177e4SLinus Torvalds #include <net/xfrm.h> 107296f7ea7SSatoru Moriya #include <trace/events/udp.h> 108447167bfSEric Dumazet #include <linux/static_key.h> 109951cf368SYonghong Song #include <linux/btf_ids.h> 11022911fc5SEric Dumazet #include <trace/events/skb.h> 111076bb0c8SEliezer Tamir #include <net/busy_poll.h> 112ba4e58ecSGerrit Renker #include "udp_impl.h" 113e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 114217375a0SEric Dumazet #include <net/addrconf.h> 11560fb9567SPaolo Abeni #include <net/udp_tunnel.h> 1160146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 1170146dca7SSabrina Dubroca #include <net/ipv6_stubs.h> 1180146dca7SSabrina Dubroca #endif 1191da177e4SLinus Torvalds 120f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly; 121645ca708SEric Dumazet EXPORT_SYMBOL(udp_table); 1221da177e4SLinus Torvalds 1238d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly; 12495766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 125c482c568SEric Dumazet 12691b6d325SEric Dumazet atomic_long_t udp_memory_allocated ____cacheline_aligned_in_smp; 12795766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 1280defbb0aSEric Dumazet DEFINE_PER_CPU(int, udp_memory_per_cpu_fw_alloc); 1290defbb0aSEric Dumazet EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc); 13095766fffSHideo Aoki 131f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536 132f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 13398322f22SEric Dumazet 134f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 135645ca708SEric Dumazet const struct udp_hslot *hslot, 13698322f22SEric Dumazet unsigned long *bitmap, 137fe38d2a1SJosef Bacik struct sock *sk, unsigned int log) 13825030a7fSGerrit Renker { 139f24d43c0SEric Dumazet struct sock *sk2; 140ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 14125030a7fSGerrit Renker 142ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 143f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 144f24d43c0SEric Dumazet sk2 != sk && 145d4cada4aSEric Dumazet (bitmap || udp_sk(sk2)->udp_port_hash == num) && 146f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1479d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1489d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 149fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 150df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 151df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 152df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 153df560056SEric Garver if (!bitmap) 154df560056SEric Garver return 0; 155df560056SEric Garver } else { 1564243cdc2SJoe Perches if (!bitmap) 157fc038410SDavid S. Miller return 1; 158df560056SEric Garver __set_bit(udp_sk(sk2)->udp_port_hash >> log, 159df560056SEric Garver bitmap); 160df560056SEric Garver } 1614243cdc2SJoe Perches } 16298322f22SEric Dumazet } 16325030a7fSGerrit Renker return 0; 16425030a7fSGerrit Renker } 16525030a7fSGerrit Renker 16630fff923SEric Dumazet /* 16730fff923SEric Dumazet * Note: we still hold spinlock of primary hash chain, so no other writer 16830fff923SEric Dumazet * can insert/delete a socket with local_port == num 16930fff923SEric Dumazet */ 17030fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num, 17130fff923SEric Dumazet struct udp_hslot *hslot2, 172fe38d2a1SJosef Bacik struct sock *sk) 17330fff923SEric Dumazet { 17430fff923SEric Dumazet struct sock *sk2; 175ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 17630fff923SEric Dumazet int res = 0; 17730fff923SEric Dumazet 17830fff923SEric Dumazet spin_lock(&hslot2->lock); 179ca065d0cSEric Dumazet udp_portaddr_for_each_entry(sk2, &hslot2->head) { 18030fff923SEric Dumazet if (net_eq(sock_net(sk2), net) && 18130fff923SEric Dumazet sk2 != sk && 18230fff923SEric Dumazet (udp_sk(sk2)->udp_port_hash == num) && 18330fff923SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1849d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1859d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 186fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 187df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 188df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 189df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 190df560056SEric Garver res = 0; 191df560056SEric Garver } else { 19230fff923SEric Dumazet res = 1; 193df560056SEric Garver } 19430fff923SEric Dumazet break; 19530fff923SEric Dumazet } 1964243cdc2SJoe Perches } 19730fff923SEric Dumazet spin_unlock(&hslot2->lock); 19830fff923SEric Dumazet return res; 19930fff923SEric Dumazet } 20030fff923SEric Dumazet 201fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 202e32ea7e7SCraig Gallek { 203e32ea7e7SCraig Gallek struct net *net = sock_net(sk); 204e32ea7e7SCraig Gallek kuid_t uid = sock_i_uid(sk); 205e32ea7e7SCraig Gallek struct sock *sk2; 206e32ea7e7SCraig Gallek 207ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 208e32ea7e7SCraig Gallek if (net_eq(sock_net(sk2), net) && 209e32ea7e7SCraig Gallek sk2 != sk && 210e32ea7e7SCraig Gallek sk2->sk_family == sk->sk_family && 211e32ea7e7SCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 212e32ea7e7SCraig Gallek (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 213e32ea7e7SCraig Gallek (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 214e32ea7e7SCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 215fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) { 2162dbb9b9eSMartin KaFai Lau return reuseport_add_sock(sk, sk2, 2172dbb9b9eSMartin KaFai Lau inet_rcv_saddr_any(sk)); 218e32ea7e7SCraig Gallek } 219e32ea7e7SCraig Gallek } 220e32ea7e7SCraig Gallek 2212dbb9b9eSMartin KaFai Lau return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 222e32ea7e7SCraig Gallek } 223e32ea7e7SCraig Gallek 22425030a7fSGerrit Renker /** 2256ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 22625030a7fSGerrit Renker * 22725030a7fSGerrit Renker * @sk: socket struct in question 22825030a7fSGerrit Renker * @snum: port number to look up 22925985edcSLucas De Marchi * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 23030fff923SEric Dumazet * with NULL address 23125030a7fSGerrit Renker */ 2326ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 23330fff923SEric Dumazet unsigned int hash2_nulladdr) 2341da177e4SLinus Torvalds { 235645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 236*919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot, *hslot2; 2373b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 238*919dfa0bSKuniyuki Iwashima int error = 1; 2391da177e4SLinus Torvalds 24032c1da70SStephen Hemminger if (!snum) { 241*919dfa0bSKuniyuki Iwashima DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 242*919dfa0bSKuniyuki Iwashima unsigned short first, last; 2439088c560SEric Dumazet int low, high, remaining; 24495c96174SEric Dumazet unsigned int rand; 2451da177e4SLinus Torvalds 2460bbf87d8SEric W. Biederman inet_get_local_port_range(net, &low, &high); 247a25de534SAnton Arapov remaining = (high - low) + 1; 248227b60f5SStephen Hemminger 249a251c17aSJason A. Donenfeld rand = get_random_u32(); 2508fc54f68SDaniel Borkmann first = reciprocal_scale(rand, remaining) + low; 25198322f22SEric Dumazet /* 25298322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 25398322f22SEric Dumazet */ 254f86dcc5aSEric Dumazet rand = (rand | 1) * (udptable->mask + 1); 2555781b235SEric Dumazet last = first + udptable->mask + 1; 2565781b235SEric Dumazet do { 257f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, first); 25898322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 259645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 26098322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 261fe38d2a1SJosef Bacik udptable->log); 26298322f22SEric Dumazet 26398322f22SEric Dumazet snum = first; 26498322f22SEric Dumazet /* 26598322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 26698322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 26798322f22SEric Dumazet * give us randomization and full range coverage. 26898322f22SEric Dumazet */ 2699088c560SEric Dumazet do { 27098322f22SEric Dumazet if (low <= snum && snum <= high && 271e3826f1eSAmerigo Wang !test_bit(snum >> udptable->log, bitmap) && 272122ff243SWANG Cong !inet_is_local_reserved_port(net, snum)) 27398322f22SEric Dumazet goto found; 27498322f22SEric Dumazet snum += rand; 27598322f22SEric Dumazet } while (snum != first); 27698322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 277df560056SEric Garver cond_resched(); 2785781b235SEric Dumazet } while (++first != last); 27998322f22SEric Dumazet goto fail; 280645ca708SEric Dumazet } else { 281f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, snum); 282645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 28330fff923SEric Dumazet if (hslot->count > 10) { 28430fff923SEric Dumazet int exist; 28530fff923SEric Dumazet unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 28630fff923SEric Dumazet 28730fff923SEric Dumazet slot2 &= udptable->mask; 28830fff923SEric Dumazet hash2_nulladdr &= udptable->mask; 28930fff923SEric Dumazet 29030fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, slot2); 29130fff923SEric Dumazet if (hslot->count < hslot2->count) 29230fff923SEric Dumazet goto scan_primary_hash; 29330fff923SEric Dumazet 294fe38d2a1SJosef Bacik exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 29530fff923SEric Dumazet if (!exist && (hash2_nulladdr != slot2)) { 29630fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 29730fff923SEric Dumazet exist = udp_lib_lport_inuse2(net, snum, hslot2, 298fe38d2a1SJosef Bacik sk); 29930fff923SEric Dumazet } 30030fff923SEric Dumazet if (exist) 30130fff923SEric Dumazet goto fail_unlock; 30230fff923SEric Dumazet else 30330fff923SEric Dumazet goto found; 30430fff923SEric Dumazet } 30530fff923SEric Dumazet scan_primary_hash: 306fe38d2a1SJosef Bacik if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 307645ca708SEric Dumazet goto fail_unlock; 308645ca708SEric Dumazet } 30998322f22SEric Dumazet found: 310c720c7e8SEric Dumazet inet_sk(sk)->inet_num = snum; 311d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash = snum; 312d4cada4aSEric Dumazet udp_sk(sk)->udp_portaddr_hash ^= snum; 3131da177e4SLinus Torvalds if (sk_unhashed(sk)) { 314e32ea7e7SCraig Gallek if (sk->sk_reuseport && 315fe38d2a1SJosef Bacik udp_reuseport_add_sock(sk, hslot)) { 316e32ea7e7SCraig Gallek inet_sk(sk)->inet_num = 0; 317e32ea7e7SCraig Gallek udp_sk(sk)->udp_port_hash = 0; 318e32ea7e7SCraig Gallek udp_sk(sk)->udp_portaddr_hash ^= snum; 319e32ea7e7SCraig Gallek goto fail_unlock; 320e32ea7e7SCraig Gallek } 321e32ea7e7SCraig Gallek 322ca065d0cSEric Dumazet sk_add_node_rcu(sk, &hslot->head); 323fdcc8aa9SEric Dumazet hslot->count++; 324c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 325512615b6SEric Dumazet 326512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 327512615b6SEric Dumazet spin_lock(&hslot2->lock); 328d894ba18SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 329d894ba18SCraig Gallek sk->sk_family == AF_INET6) 3301602f49bSDavid S. Miller hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 331d894ba18SCraig Gallek &hslot2->head); 332d894ba18SCraig Gallek else 333ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 334512615b6SEric Dumazet &hslot2->head); 335512615b6SEric Dumazet hslot2->count++; 336512615b6SEric Dumazet spin_unlock(&hslot2->lock); 3371da177e4SLinus Torvalds } 338ca065d0cSEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 33925030a7fSGerrit Renker error = 0; 340645ca708SEric Dumazet fail_unlock: 341645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 3421da177e4SLinus Torvalds fail: 34325030a7fSGerrit Renker return error; 3441da177e4SLinus Torvalds } 345c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port); 3461da177e4SLinus Torvalds 3476ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 348db8dac20SDavid S. Miller { 34930fff923SEric Dumazet unsigned int hash2_nulladdr = 350f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 35130fff923SEric Dumazet unsigned int hash2_partial = 352f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 35330fff923SEric Dumazet 354d4cada4aSEric Dumazet /* precompute partial secondary hash */ 35530fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 356fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 357db8dac20SDavid S. Miller } 358db8dac20SDavid S. Miller 359d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 360d1e37288SSu, Xuemin __be32 saddr, __be16 sport, 361fb74c277SDavid Ahern __be32 daddr, unsigned short hnum, 36273545373STim Beale int dif, int sdif) 363645ca708SEric Dumazet { 36460c04aecSJoe Perches int score; 36560c04aecSJoe Perches struct inet_sock *inet; 3666da5b0f0SMike Manning bool dev_match; 367645ca708SEric Dumazet 36860c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 36960c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 37060c04aecSJoe Perches ipv6_only_sock(sk)) 37160c04aecSJoe Perches return -1; 372645ca708SEric Dumazet 3734cdeeee9SPeter Oskolkov if (sk->sk_rcv_saddr != daddr) 374645ca708SEric Dumazet return -1; 37560c04aecSJoe Perches 3764cdeeee9SPeter Oskolkov score = (sk->sk_family == PF_INET) ? 2 : 1; 3774cdeeee9SPeter Oskolkov 3784cdeeee9SPeter Oskolkov inet = inet_sk(sk); 379c720c7e8SEric Dumazet if (inet->inet_daddr) { 380c720c7e8SEric Dumazet if (inet->inet_daddr != saddr) 381645ca708SEric Dumazet return -1; 382ba418fa3STom Herbert score += 4; 383645ca708SEric Dumazet } 38460c04aecSJoe Perches 385c720c7e8SEric Dumazet if (inet->inet_dport) { 386c720c7e8SEric Dumazet if (inet->inet_dport != sport) 387645ca708SEric Dumazet return -1; 388ba418fa3STom Herbert score += 4; 389645ca708SEric Dumazet } 39060c04aecSJoe Perches 3916da5b0f0SMike Manning dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 3926da5b0f0SMike Manning dif, sdif); 39369678bcdSPaolo Abeni if (!dev_match) 394645ca708SEric Dumazet return -1; 3958d6c414cSMike Manning if (sk->sk_bound_dev_if) 396ba418fa3STom Herbert score += 4; 397fb74c277SDavid Ahern 3987170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 39970da268bSEric Dumazet score++; 400645ca708SEric Dumazet return score; 401645ca708SEric Dumazet } 402645ca708SEric Dumazet 4036eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 40465cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 40565cd8033SHannes Frederic Sowa const __be16 fport) 40665cd8033SHannes Frederic Sowa { 4071bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4081bbdceefSHannes Frederic Sowa 4091bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4101bbdceefSHannes Frederic Sowa 41165cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4121bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 41365cd8033SHannes Frederic Sowa } 41465cd8033SHannes Frederic Sowa 415a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk, 4167629c73aSJakub Sitnicki struct sk_buff *skb, 4177629c73aSJakub Sitnicki __be32 saddr, __be16 sport, 4187629c73aSJakub Sitnicki __be32 daddr, unsigned short hnum) 4197629c73aSJakub Sitnicki { 4207629c73aSJakub Sitnicki struct sock *reuse_sk = NULL; 4217629c73aSJakub Sitnicki u32 hash; 4227629c73aSJakub Sitnicki 4237629c73aSJakub Sitnicki if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 4247629c73aSJakub Sitnicki hash = udp_ehashfn(net, daddr, hnum, saddr, sport); 4257629c73aSJakub Sitnicki reuse_sk = reuseport_select_sock(sk, hash, skb, 4267629c73aSJakub Sitnicki sizeof(struct udphdr)); 4277629c73aSJakub Sitnicki } 4287629c73aSJakub Sitnicki return reuse_sk; 4297629c73aSJakub Sitnicki } 4307629c73aSJakub Sitnicki 431d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4325051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4335051ebd2SEric Dumazet __be32 saddr, __be16 sport, 434fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 43573545373STim Beale int dif, int sdif, 436d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4371134158bSCraig Gallek struct sk_buff *skb) 4385051ebd2SEric Dumazet { 4395051ebd2SEric Dumazet struct sock *sk, *result; 440e94a62f5SPaolo Abeni int score, badness; 4415051ebd2SEric Dumazet 4425051ebd2SEric Dumazet result = NULL; 443ba418fa3STom Herbert badness = 0; 444ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 445d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 44673545373STim Beale daddr, hnum, dif, sdif); 4475051ebd2SEric Dumazet if (score > badness) { 4487629c73aSJakub Sitnicki result = lookup_reuseport(net, sk, skb, 4497629c73aSJakub Sitnicki saddr, sport, daddr, hnum); 450a57066b1SDavid S. Miller /* Fall back to scoring if group has connections */ 45169421bf9SKuniyuki Iwashima if (result && !reuseport_has_conns(sk)) 452ca065d0cSEric Dumazet return result; 4537629c73aSJakub Sitnicki 454a57066b1SDavid S. Miller result = result ? : sk; 455ca065d0cSEric Dumazet badness = score; 4565051ebd2SEric Dumazet } 4575051ebd2SEric Dumazet } 4585051ebd2SEric Dumazet return result; 4595051ebd2SEric Dumazet } 4605051ebd2SEric Dumazet 461a57066b1SDavid S. Miller static struct sock *udp4_lookup_run_bpf(struct net *net, 46272f7e944SJakub Sitnicki struct udp_table *udptable, 46372f7e944SJakub Sitnicki struct sk_buff *skb, 46472f7e944SJakub Sitnicki __be32 saddr, __be16 sport, 465f8931565SMark Pashmfouroush __be32 daddr, u16 hnum, const int dif) 46672f7e944SJakub Sitnicki { 46772f7e944SJakub Sitnicki struct sock *sk, *reuse_sk; 46872f7e944SJakub Sitnicki bool no_reuseport; 46972f7e944SJakub Sitnicki 47072f7e944SJakub Sitnicki if (udptable != &udp_table) 47172f7e944SJakub Sitnicki return NULL; /* only UDP is supported */ 47272f7e944SJakub Sitnicki 473f8931565SMark Pashmfouroush no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP, saddr, sport, 474f8931565SMark Pashmfouroush daddr, hnum, dif, &sk); 47572f7e944SJakub Sitnicki if (no_reuseport || IS_ERR_OR_NULL(sk)) 47672f7e944SJakub Sitnicki return sk; 47772f7e944SJakub Sitnicki 47872f7e944SJakub Sitnicki reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); 479c64c9c28SJakub Sitnicki if (reuse_sk) 48072f7e944SJakub Sitnicki sk = reuse_sk; 48172f7e944SJakub Sitnicki return sk; 48272f7e944SJakub Sitnicki } 48372f7e944SJakub Sitnicki 484db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 485db8dac20SDavid S. Miller * harder than this. -DaveM 486db8dac20SDavid S. Miller */ 487fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 488fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 489fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 490db8dac20SDavid S. Miller { 491db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4924cdeeee9SPeter Oskolkov unsigned int hash2, slot2; 4934cdeeee9SPeter Oskolkov struct udp_hslot *hslot2; 49472f7e944SJakub Sitnicki struct sock *result, *sk; 495db8dac20SDavid S. Miller 496f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 4975051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4985051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4995051ebd2SEric Dumazet 50072f7e944SJakub Sitnicki /* Lookup connected or non-wildcard socket */ 5015051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 502fb74c277SDavid Ahern daddr, hnum, dif, sdif, 50373545373STim Beale hslot2, skb); 50472f7e944SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 50572f7e944SJakub Sitnicki goto done; 50672f7e944SJakub Sitnicki 50772f7e944SJakub Sitnicki /* Lookup redirect from BPF */ 50872f7e944SJakub Sitnicki if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 50972f7e944SJakub Sitnicki sk = udp4_lookup_run_bpf(net, udptable, skb, 510f8931565SMark Pashmfouroush saddr, sport, daddr, hnum, dif); 51172f7e944SJakub Sitnicki if (sk) { 51272f7e944SJakub Sitnicki result = sk; 51372f7e944SJakub Sitnicki goto done; 51472f7e944SJakub Sitnicki } 51572f7e944SJakub Sitnicki } 51672f7e944SJakub Sitnicki 51772f7e944SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 51872f7e944SJakub Sitnicki if (result) 51972f7e944SJakub Sitnicki goto done; 52072f7e944SJakub Sitnicki 52172f7e944SJakub Sitnicki /* Lookup wildcard sockets */ 522f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 5235051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 5245051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 5255051ebd2SEric Dumazet 5261223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 5274cdeeee9SPeter Oskolkov htonl(INADDR_ANY), hnum, dif, sdif, 52873545373STim Beale hslot2, skb); 52972f7e944SJakub Sitnicki done: 53088e235b8SEnrico Weigelt if (IS_ERR(result)) 5318217ca65SMartin KaFai Lau return NULL; 5325051ebd2SEric Dumazet return result; 5335051ebd2SEric Dumazet } 534fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 535db8dac20SDavid S. Miller 536607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 537607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 538645ca708SEric Dumazet struct udp_table *udptable) 539607c4aafSKOVACS Krisztian { 540607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 541607c4aafSKOVACS Krisztian 542ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 543607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 544fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 545607c4aafSKOVACS Krisztian } 546607c4aafSKOVACS Krisztian 5477b58e63eSEric Dumazet struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb, 54863058308STom Herbert __be16 sport, __be16 dport) 54963058308STom Herbert { 550257a525fSMartin KaFai Lau const struct iphdr *iph = ip_hdr(skb); 551257a525fSMartin KaFai Lau 552257a525fSMartin KaFai Lau return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 553257a525fSMartin KaFai Lau iph->daddr, dport, inet_iif(skb), 554257a525fSMartin KaFai Lau inet_sdif(skb), &udp_table, NULL); 55563058308STom Herbert } 55663058308STom Herbert 557ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 558ca065d0cSEric Dumazet * Does increment socket refcount. 559ca065d0cSEric Dumazet */ 5606e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 561bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 562bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 563bcd41303SKOVACS Krisztian { 564ca065d0cSEric Dumazet struct sock *sk; 565ca065d0cSEric Dumazet 566ca065d0cSEric Dumazet sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 567fb74c277SDavid Ahern dif, 0, &udp_table, NULL); 56841c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 569ca065d0cSEric Dumazet sk = NULL; 570ca065d0cSEric Dumazet return sk; 571bcd41303SKOVACS Krisztian } 572bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 573ca065d0cSEric Dumazet #endif 574bcd41303SKOVACS Krisztian 575421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, 576421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 577421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 578fb74c277SDavid Ahern int dif, int sdif, unsigned short hnum) 579421b3885SShawn Bohrer { 580421b3885SShawn Bohrer struct inet_sock *inet = inet_sk(sk); 581421b3885SShawn Bohrer 582421b3885SShawn Bohrer if (!net_eq(sock_net(sk), net) || 583421b3885SShawn Bohrer udp_sk(sk)->udp_port_hash != hnum || 584421b3885SShawn Bohrer (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 585421b3885SShawn Bohrer (inet->inet_dport != rmt_port && inet->inet_dport) || 586421b3885SShawn Bohrer (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 587421b3885SShawn Bohrer ipv6_only_sock(sk) || 58882ba25c6STim Beale !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 589421b3885SShawn Bohrer return false; 59060d9b031SDavid Ahern if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 591421b3885SShawn Bohrer return false; 592421b3885SShawn Bohrer return true; 593421b3885SShawn Bohrer } 594421b3885SShawn Bohrer 595a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 596a36e185eSStefano Brivio void udp_encap_enable(void) 597a36e185eSStefano Brivio { 5989c480601SPaolo Abeni static_branch_inc(&udp_encap_needed_key); 599a36e185eSStefano Brivio } 600a36e185eSStefano Brivio EXPORT_SYMBOL(udp_encap_enable); 601a36e185eSStefano Brivio 602a4a600ddSXin Long void udp_encap_disable(void) 603a4a600ddSXin Long { 604a4a600ddSXin Long static_branch_dec(&udp_encap_needed_key); 605a4a600ddSXin Long } 606a4a600ddSXin Long EXPORT_SYMBOL(udp_encap_disable); 607a4a600ddSXin Long 608e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 609e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 610e7cc0824SStefano Brivio */ 611e7cc0824SStefano Brivio static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 612e7cc0824SStefano Brivio { 613e7cc0824SStefano Brivio int i; 614e7cc0824SStefano Brivio 615e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 616e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, u32 info); 61792b95364SPaolo Abeni const struct ip_tunnel_encap_ops *encap; 618e7cc0824SStefano Brivio 61992b95364SPaolo Abeni encap = rcu_dereference(iptun_encaps[i]); 62092b95364SPaolo Abeni if (!encap) 621e7cc0824SStefano Brivio continue; 62292b95364SPaolo Abeni handler = encap->err_handler; 623e7cc0824SStefano Brivio if (handler && !handler(skb, info)) 624e7cc0824SStefano Brivio return 0; 625e7cc0824SStefano Brivio } 626e7cc0824SStefano Brivio 627e7cc0824SStefano Brivio return -ENOENT; 628e7cc0824SStefano Brivio } 629e7cc0824SStefano Brivio 630a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 631a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 632a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 633a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 634a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 635a36e185eSStefano Brivio * trace ICMP messages back to them. 636a36e185eSStefano Brivio * 637e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 638e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 639e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 640e7cc0824SStefano Brivio * 641a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 642a36e185eSStefano Brivio * association. 643a36e185eSStefano Brivio * 644e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 645e7cc0824SStefano Brivio * processing, zero otherwise. 646a36e185eSStefano Brivio */ 647a36e185eSStefano Brivio static struct sock *__udp4_lib_err_encap(struct net *net, 648a36e185eSStefano Brivio const struct iphdr *iph, 649a36e185eSStefano Brivio struct udphdr *uh, 650a36e185eSStefano Brivio struct udp_table *udptable, 6519bfce73cSVadim Fedorenko struct sock *sk, 652e7cc0824SStefano Brivio struct sk_buff *skb, u32 info) 653a36e185eSStefano Brivio { 6549bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb); 655a36e185eSStefano Brivio int network_offset, transport_offset; 6569bfce73cSVadim Fedorenko struct udp_sock *up; 657a36e185eSStefano Brivio 658a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 659a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 660a36e185eSStefano Brivio 661a36e185eSStefano Brivio /* Network header needs to point to the outer IPv4 header inside ICMP */ 662a36e185eSStefano Brivio skb_reset_network_header(skb); 663a36e185eSStefano Brivio 664a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 665a36e185eSStefano Brivio skb_set_transport_header(skb, iph->ihl << 2); 666a36e185eSStefano Brivio 6679bfce73cSVadim Fedorenko if (sk) { 6689bfce73cSVadim Fedorenko up = udp_sk(sk); 6699bfce73cSVadim Fedorenko 6709bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup); 6719bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb)) 6729bfce73cSVadim Fedorenko sk = NULL; 6739bfce73cSVadim Fedorenko 6749bfce73cSVadim Fedorenko goto out; 6759bfce73cSVadim Fedorenko } 6769bfce73cSVadim Fedorenko 677e7cc0824SStefano Brivio sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 678e7cc0824SStefano Brivio iph->saddr, uh->dest, skb->dev->ifindex, 0, 679e7cc0824SStefano Brivio udptable, NULL); 680e7cc0824SStefano Brivio if (sk) { 6819bfce73cSVadim Fedorenko up = udp_sk(sk); 682e7cc0824SStefano Brivio 683a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 684a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 685a36e185eSStefano Brivio sk = NULL; 686e7cc0824SStefano Brivio } 687e7cc0824SStefano Brivio 6889bfce73cSVadim Fedorenko out: 689e7cc0824SStefano Brivio if (!sk) 690e7cc0824SStefano Brivio sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 691a36e185eSStefano Brivio 692a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 693a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 694a36e185eSStefano Brivio 695a36e185eSStefano Brivio return sk; 696a36e185eSStefano Brivio } 697a36e185eSStefano Brivio 698db8dac20SDavid S. Miller /* 699db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 700db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 701db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 702db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 703db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 704db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 705db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 706db8dac20SDavid S. Miller * to find the appropriate port. 707db8dac20SDavid S. Miller */ 708db8dac20SDavid S. Miller 70932bbd879SStefano Brivio int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 710db8dac20SDavid S. Miller { 711db8dac20SDavid S. Miller struct inet_sock *inet; 712b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 713db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 714db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 715db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 716a36e185eSStefano Brivio bool tunnel = false; 717db8dac20SDavid S. Miller struct sock *sk; 718db8dac20SDavid S. Miller int harderr; 719db8dac20SDavid S. Miller int err; 720fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 721db8dac20SDavid S. Miller 722fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 723f64bf6b8SMike Manning iph->saddr, uh->source, skb->dev->ifindex, 724f64bf6b8SMike Manning inet_sdif(skb), udptable, NULL); 7259bfce73cSVadim Fedorenko 726d26796aeSXin Long if (!sk || udp_sk(sk)->encap_type) { 727a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 728e7cc0824SStefano Brivio if (static_branch_unlikely(&udp_encap_needed_key)) { 7299bfce73cSVadim Fedorenko sk = __udp4_lib_err_encap(net, iph, uh, udptable, sk, skb, 730e7cc0824SStefano Brivio info); 731e7cc0824SStefano Brivio if (!sk) 732e7cc0824SStefano Brivio return 0; 7339bfce73cSVadim Fedorenko } else 7349bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT); 735e7cc0824SStefano Brivio 736e7cc0824SStefano Brivio if (IS_ERR(sk)) { 737e7cc0824SStefano Brivio __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 738e7cc0824SStefano Brivio return PTR_ERR(sk); 739e7cc0824SStefano Brivio } 740e7cc0824SStefano Brivio 741a36e185eSStefano Brivio tunnel = true; 742db8dac20SDavid S. Miller } 743db8dac20SDavid S. Miller 744db8dac20SDavid S. Miller err = 0; 745db8dac20SDavid S. Miller harderr = 0; 746db8dac20SDavid S. Miller inet = inet_sk(sk); 747db8dac20SDavid S. Miller 748db8dac20SDavid S. Miller switch (type) { 749db8dac20SDavid S. Miller default: 750db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 751db8dac20SDavid S. Miller err = EHOSTUNREACH; 752db8dac20SDavid S. Miller break; 753db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 754db8dac20SDavid S. Miller goto out; 755db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 756db8dac20SDavid S. Miller err = EPROTO; 757db8dac20SDavid S. Miller harderr = 1; 758db8dac20SDavid S. Miller break; 759db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 760db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 76136393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 762db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 763db8dac20SDavid S. Miller err = EMSGSIZE; 764db8dac20SDavid S. Miller harderr = 1; 765db8dac20SDavid S. Miller break; 766db8dac20SDavid S. Miller } 767db8dac20SDavid S. Miller goto out; 768db8dac20SDavid S. Miller } 769db8dac20SDavid S. Miller err = EHOSTUNREACH; 770db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 771db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 772db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 773db8dac20SDavid S. Miller } 774db8dac20SDavid S. Miller break; 77555be7a9cSDavid S. Miller case ICMP_REDIRECT: 77655be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 7771a462d18SDuan Jiong goto out; 778db8dac20SDavid S. Miller } 779db8dac20SDavid S. Miller 780db8dac20SDavid S. Miller /* 781db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 782db8dac20SDavid S. Miller * 4.1.3.3. 783db8dac20SDavid S. Miller */ 784a36e185eSStefano Brivio if (tunnel) { 785a36e185eSStefano Brivio /* ...not for tunnels though: we don't have a sending socket */ 786ac56a0b4SDavid Howells if (udp_sk(sk)->encap_err_rcv) 78742fb06b3SDavid Howells udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest, info, 78842fb06b3SDavid Howells (u8 *)(uh+1)); 789a36e185eSStefano Brivio goto out; 790a36e185eSStefano Brivio } 791db8dac20SDavid S. Miller if (!inet->recverr) { 792db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 793db8dac20SDavid S. Miller goto out; 794b1faf566SEric Dumazet } else 795db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 796b1faf566SEric Dumazet 797db8dac20SDavid S. Miller sk->sk_err = err; 798e3ae2365SAlexander Aring sk_error_report(sk); 799db8dac20SDavid S. Miller out: 80032bbd879SStefano Brivio return 0; 801db8dac20SDavid S. Miller } 802db8dac20SDavid S. Miller 80332bbd879SStefano Brivio int udp_err(struct sk_buff *skb, u32 info) 804db8dac20SDavid S. Miller { 80532bbd879SStefano Brivio return __udp4_lib_err(skb, info, &udp_table); 806db8dac20SDavid S. Miller } 807db8dac20SDavid S. Miller 808db8dac20SDavid S. Miller /* 809db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 810db8dac20SDavid S. Miller */ 81136d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 812db8dac20SDavid S. Miller { 813db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 814db8dac20SDavid S. Miller 815db8dac20SDavid S. Miller if (up->pending) { 816db8dac20SDavid S. Miller up->len = 0; 817db8dac20SDavid S. Miller up->pending = 0; 818db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 819db8dac20SDavid S. Miller } 820db8dac20SDavid S. Miller } 82136d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 822db8dac20SDavid S. Miller 823db8dac20SDavid S. Miller /** 824f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 825db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 826db8dac20SDavid S. Miller * (checksum field must be zeroed out) 827f6b9664fSHerbert Xu * @src: source IP address 828f6b9664fSHerbert Xu * @dst: destination IP address 829db8dac20SDavid S. Miller */ 830c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 831db8dac20SDavid S. Miller { 832db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 833f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 834f6b9664fSHerbert Xu int len = skb->len - offset; 835f6b9664fSHerbert Xu int hlen = len; 836db8dac20SDavid S. Miller __wsum csum = 0; 837db8dac20SDavid S. Miller 838ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 839db8dac20SDavid S. Miller /* 840db8dac20SDavid S. Miller * Only one fragment on the socket. 841db8dac20SDavid S. Miller */ 842db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 843db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 844f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 845f6b9664fSHerbert Xu IPPROTO_UDP, 0); 846db8dac20SDavid S. Miller } else { 847ebbe495fSWANG Cong struct sk_buff *frags; 848ebbe495fSWANG Cong 849db8dac20SDavid S. Miller /* 850db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 851db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 852db8dac20SDavid S. Miller * should be together 853db8dac20SDavid S. Miller */ 854ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 855f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 856f6b9664fSHerbert Xu hlen -= frags->len; 857ebbe495fSWANG Cong } 858db8dac20SDavid S. Miller 859f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 860db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 861db8dac20SDavid S. Miller 862db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 863db8dac20SDavid S. Miller if (uh->check == 0) 864db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 865db8dac20SDavid S. Miller } 866db8dac20SDavid S. Miller } 867c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 868db8dac20SDavid S. Miller 869af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 870af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 871af5fcba7STom Herbert */ 872af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 873af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 874af5fcba7STom Herbert { 875af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 876af5fcba7STom Herbert 877179bc67fSEdward Cree if (nocheck) { 878af5fcba7STom Herbert uh->check = 0; 879179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 880af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 881179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 882179bc67fSEdward Cree uh->check = 0; 883179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 884179bc67fSEdward Cree if (uh->check == 0) 885179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 886d75f1306SEdward Cree } else { 887af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 888af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 889af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 890af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 891af5fcba7STom Herbert } 892af5fcba7STom Herbert } 893af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 894af5fcba7STom Herbert 895bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 896bec1f6f6SWillem de Bruijn struct inet_cork *cork) 897f6b9664fSHerbert Xu { 898f6b9664fSHerbert Xu struct sock *sk = skb->sk; 899f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 900f6b9664fSHerbert Xu struct udphdr *uh; 901cffb8f61SMenglong Dong int err; 902f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 903f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 904f6b9664fSHerbert Xu int len = skb->len - offset; 9054094871dSJosh Hunt int datalen = len - sizeof(*uh); 906f6b9664fSHerbert Xu __wsum csum = 0; 907f6b9664fSHerbert Xu 908f6b9664fSHerbert Xu /* 909f6b9664fSHerbert Xu * Create a UDP header 910f6b9664fSHerbert Xu */ 911f6b9664fSHerbert Xu uh = udp_hdr(skb); 912f6b9664fSHerbert Xu uh->source = inet->inet_sport; 91379ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 914f6b9664fSHerbert Xu uh->len = htons(len); 915f6b9664fSHerbert Xu uh->check = 0; 916f6b9664fSHerbert Xu 917bec1f6f6SWillem de Bruijn if (cork->gso_size) { 918bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 919bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 920bec1f6f6SWillem de Bruijn 9210f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 9220f149c9fSWillem de Bruijn kfree_skb(skb); 923bec1f6f6SWillem de Bruijn return -EINVAL; 9240f149c9fSWillem de Bruijn } 925158390e4SJianguo Wu if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 9260f149c9fSWillem de Bruijn kfree_skb(skb); 927bec1f6f6SWillem de Bruijn return -EINVAL; 9280f149c9fSWillem de Bruijn } 9290f149c9fSWillem de Bruijn if (sk->sk_no_check_tx) { 9300f149c9fSWillem de Bruijn kfree_skb(skb); 931a8c744a8SWillem de Bruijn return -EINVAL; 9320f149c9fSWillem de Bruijn } 933ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 9340f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 9350f149c9fSWillem de Bruijn kfree_skb(skb); 936bec1f6f6SWillem de Bruijn return -EIO; 9370f149c9fSWillem de Bruijn } 938bec1f6f6SWillem de Bruijn 9394094871dSJosh Hunt if (datalen > cork->gso_size) { 940bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 941bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 9424094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 943dfec0ee2SAlexander Duyck cork->gso_size); 9444094871dSJosh Hunt } 945a8c744a8SWillem de Bruijn goto csum_partial; 946bec1f6f6SWillem de Bruijn } 947bec1f6f6SWillem de Bruijn 948f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 949f6b9664fSHerbert Xu csum = udplite_csum(skb); 950f6b9664fSHerbert Xu 951ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 952f6b9664fSHerbert Xu 953f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 954f6b9664fSHerbert Xu goto send; 955f6b9664fSHerbert Xu 956f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 957a8c744a8SWillem de Bruijn csum_partial: 958f6b9664fSHerbert Xu 95979ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 960f6b9664fSHerbert Xu goto send; 961f6b9664fSHerbert Xu 962f6b9664fSHerbert Xu } else 963f6b9664fSHerbert Xu csum = udp_csum(skb); 964f6b9664fSHerbert Xu 965f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 96679ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 967f6b9664fSHerbert Xu sk->sk_protocol, csum); 968f6b9664fSHerbert Xu if (uh->check == 0) 969f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 970f6b9664fSHerbert Xu 971f6b9664fSHerbert Xu send: 972b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 973f6b9664fSHerbert Xu if (err) { 974f6b9664fSHerbert Xu if (err == -ENOBUFS && !inet->recverr) { 9756aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 976f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 977f6b9664fSHerbert Xu err = 0; 978f6b9664fSHerbert Xu } 979f6b9664fSHerbert Xu } else 9806aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 981f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 982f6b9664fSHerbert Xu return err; 983f6b9664fSHerbert Xu } 984f6b9664fSHerbert Xu 985db8dac20SDavid S. Miller /* 986db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 987db8dac20SDavid S. Miller */ 9888822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 989db8dac20SDavid S. Miller { 990db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 991db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 992b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 993db8dac20SDavid S. Miller struct sk_buff *skb; 994db8dac20SDavid S. Miller int err = 0; 995db8dac20SDavid S. Miller 99677968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 997f6b9664fSHerbert Xu if (!skb) 998db8dac20SDavid S. Miller goto out; 999db8dac20SDavid S. Miller 1000bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &inet->cork.base); 1001db8dac20SDavid S. Miller 1002db8dac20SDavid S. Miller out: 1003db8dac20SDavid S. Miller up->len = 0; 1004db8dac20SDavid S. Miller up->pending = 0; 1005db8dac20SDavid S. Miller return err; 1006db8dac20SDavid S. Miller } 10078822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 1008db8dac20SDavid S. Miller 10092e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 10102e8de857SWillem de Bruijn { 10112e8de857SWillem de Bruijn switch (cmsg->cmsg_type) { 10122e8de857SWillem de Bruijn case UDP_SEGMENT: 10132e8de857SWillem de Bruijn if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 10142e8de857SWillem de Bruijn return -EINVAL; 10152e8de857SWillem de Bruijn *gso_size = *(__u16 *)CMSG_DATA(cmsg); 10162e8de857SWillem de Bruijn return 0; 10172e8de857SWillem de Bruijn default: 10182e8de857SWillem de Bruijn return -EINVAL; 10192e8de857SWillem de Bruijn } 10202e8de857SWillem de Bruijn } 10212e8de857SWillem de Bruijn 10222e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 10232e8de857SWillem de Bruijn { 10242e8de857SWillem de Bruijn struct cmsghdr *cmsg; 10252e8de857SWillem de Bruijn bool need_ip = false; 10262e8de857SWillem de Bruijn int err; 10272e8de857SWillem de Bruijn 10282e8de857SWillem de Bruijn for_each_cmsghdr(cmsg, msg) { 10292e8de857SWillem de Bruijn if (!CMSG_OK(msg, cmsg)) 10302e8de857SWillem de Bruijn return -EINVAL; 10312e8de857SWillem de Bruijn 10322e8de857SWillem de Bruijn if (cmsg->cmsg_level != SOL_UDP) { 10332e8de857SWillem de Bruijn need_ip = true; 10342e8de857SWillem de Bruijn continue; 10352e8de857SWillem de Bruijn } 10362e8de857SWillem de Bruijn 10372e8de857SWillem de Bruijn err = __udp_cmsg_send(cmsg, gso_size); 10382e8de857SWillem de Bruijn if (err) 10392e8de857SWillem de Bruijn return err; 10402e8de857SWillem de Bruijn } 10412e8de857SWillem de Bruijn 10422e8de857SWillem de Bruijn return need_ip; 10432e8de857SWillem de Bruijn } 10442e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send); 10452e8de857SWillem de Bruijn 10461b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1047db8dac20SDavid S. Miller { 1048db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1049db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 10501cedee13SAndrey Ignatov DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 1051e474995fSDavid S. Miller struct flowi4 fl4_stack; 1052b6f21b26SDavid S. Miller struct flowi4 *fl4; 1053db8dac20SDavid S. Miller int ulen = len; 1054db8dac20SDavid S. Miller struct ipcm_cookie ipc; 1055db8dac20SDavid S. Miller struct rtable *rt = NULL; 1056db8dac20SDavid S. Miller int free = 0; 1057db8dac20SDavid S. Miller int connected = 0; 1058db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 1059db8dac20SDavid S. Miller __be16 dport; 1060db8dac20SDavid S. Miller u8 tos; 1061db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 1062a9f59707SEric Dumazet int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE; 1063db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1064903ab86dSHerbert Xu struct sk_buff *skb; 1065f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 1066db8dac20SDavid S. Miller 1067db8dac20SDavid S. Miller if (len > 0xFFFF) 1068db8dac20SDavid S. Miller return -EMSGSIZE; 1069db8dac20SDavid S. Miller 1070db8dac20SDavid S. Miller /* 1071db8dac20SDavid S. Miller * Check the flags. 1072db8dac20SDavid S. Miller */ 1073db8dac20SDavid S. Miller 1074db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 1075db8dac20SDavid S. Miller return -EOPNOTSUPP; 1076db8dac20SDavid S. Miller 1077903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1078903ab86dSHerbert Xu 1079f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1080db8dac20SDavid S. Miller if (up->pending) { 1081db8dac20SDavid S. Miller /* 1082db8dac20SDavid S. Miller * There are pending frames. 1083db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1084db8dac20SDavid S. Miller */ 1085db8dac20SDavid S. Miller lock_sock(sk); 1086db8dac20SDavid S. Miller if (likely(up->pending)) { 1087db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 1088db8dac20SDavid S. Miller release_sock(sk); 1089db8dac20SDavid S. Miller return -EINVAL; 1090db8dac20SDavid S. Miller } 1091db8dac20SDavid S. Miller goto do_append_data; 1092db8dac20SDavid S. Miller } 1093db8dac20SDavid S. Miller release_sock(sk); 1094db8dac20SDavid S. Miller } 1095db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1096db8dac20SDavid S. Miller 1097db8dac20SDavid S. Miller /* 1098db8dac20SDavid S. Miller * Get and verify the address. 1099db8dac20SDavid S. Miller */ 11001cedee13SAndrey Ignatov if (usin) { 1101db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 1102db8dac20SDavid S. Miller return -EINVAL; 1103db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 1104db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 1105db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1106db8dac20SDavid S. Miller } 1107db8dac20SDavid S. Miller 1108db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 1109db8dac20SDavid S. Miller dport = usin->sin_port; 1110db8dac20SDavid S. Miller if (dport == 0) 1111db8dac20SDavid S. Miller return -EINVAL; 1112db8dac20SDavid S. Miller } else { 1113db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1114db8dac20SDavid S. Miller return -EDESTADDRREQ; 1115c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1116c720c7e8SEric Dumazet dport = inet->inet_dport; 1117db8dac20SDavid S. Miller /* Open fast path for connected socket. 1118db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 1119db8dac20SDavid S. Miller */ 1120db8dac20SDavid S. Miller connected = 1; 1121db8dac20SDavid S. Miller } 1122c14ac945SSoheil Hassas Yeganeh 112335178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 112418a419baSEric Dumazet ipc.gso_size = READ_ONCE(up->gso_size); 1125bf84a010SDaniel Borkmann 1126db8dac20SDavid S. Miller if (msg->msg_controllen) { 11272e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc.gso_size); 11282e8de857SWillem de Bruijn if (err > 0) 11292e8de857SWillem de Bruijn err = ip_cmsg_send(sk, msg, &ipc, 11302e8de857SWillem de Bruijn sk->sk_family == AF_INET6); 11312e8de857SWillem de Bruijn if (unlikely(err < 0)) { 113291948309SEric Dumazet kfree(ipc.opt); 1133db8dac20SDavid S. Miller return err; 113491948309SEric Dumazet } 1135db8dac20SDavid S. Miller if (ipc.opt) 1136db8dac20SDavid S. Miller free = 1; 1137db8dac20SDavid S. Miller connected = 0; 1138db8dac20SDavid S. Miller } 1139f6d8bd05SEric Dumazet if (!ipc.opt) { 1140f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 1141f6d8bd05SEric Dumazet 1142f6d8bd05SEric Dumazet rcu_read_lock(); 1143f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1144f6d8bd05SEric Dumazet if (inet_opt) { 1145f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 1146f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 1147f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 1148f6d8bd05SEric Dumazet } 1149f6d8bd05SEric Dumazet rcu_read_unlock(); 1150f6d8bd05SEric Dumazet } 1151db8dac20SDavid S. Miller 11526fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP4_SENDMSG) && !connected) { 11531cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 11541cedee13SAndrey Ignatov (struct sockaddr *)usin, &ipc.addr); 11551cedee13SAndrey Ignatov if (err) 11561cedee13SAndrey Ignatov goto out_free; 11571cedee13SAndrey Ignatov if (usin) { 11581cedee13SAndrey Ignatov if (usin->sin_port == 0) { 11591cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 11601cedee13SAndrey Ignatov err = -EINVAL; 11611cedee13SAndrey Ignatov goto out_free; 11621cedee13SAndrey Ignatov } 11631cedee13SAndrey Ignatov daddr = usin->sin_addr.s_addr; 11641cedee13SAndrey Ignatov dport = usin->sin_port; 11651cedee13SAndrey Ignatov } 11661cedee13SAndrey Ignatov } 11671cedee13SAndrey Ignatov 1168db8dac20SDavid S. Miller saddr = ipc.addr; 1169db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 1170db8dac20SDavid S. Miller 1171f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 11721b97013bSAndrey Ignatov if (!daddr) { 11731b97013bSAndrey Ignatov err = -EINVAL; 11741b97013bSAndrey Ignatov goto out_free; 11751b97013bSAndrey Ignatov } 1176f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 1177db8dac20SDavid S. Miller connected = 0; 1178db8dac20SDavid S. Miller } 1179aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 1180db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 1181db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 1182f6d8bd05SEric Dumazet (ipc.opt && ipc.opt->opt.is_strictroute)) { 1183db8dac20SDavid S. Miller tos |= RTO_ONLINK; 1184db8dac20SDavid S. Miller connected = 0; 1185db8dac20SDavid S. Miller } 1186db8dac20SDavid S. Miller 1187db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 1188854da991SRobert Shearman if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 1189db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 1190db8dac20SDavid S. Miller if (!saddr) 1191db8dac20SDavid S. Miller saddr = inet->mc_addr; 1192db8dac20SDavid S. Miller connected = 0; 11939515a2e0SDavid Ahern } else if (!ipc.oif) { 119476e21053SErich E. Hoover ipc.oif = inet->uc_index; 11959515a2e0SDavid Ahern } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 11969515a2e0SDavid Ahern /* oif is set, packet is to local broadcast and 11972bdcc73cSRandy Dunlap * uc_index is set. oif is most likely set 11989515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 11999515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 12009515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 12019515a2e0SDavid Ahern */ 12029515a2e0SDavid Ahern if (ipc.oif != inet->uc_index && 12039515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 12049515a2e0SDavid Ahern inet->uc_index)) { 12059515a2e0SDavid Ahern ipc.oif = inet->uc_index; 12069515a2e0SDavid Ahern } 12079515a2e0SDavid Ahern } 1208db8dac20SDavid S. Miller 1209db8dac20SDavid S. Miller if (connected) 1210db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 1211db8dac20SDavid S. Miller 121251456b29SIan Morris if (!rt) { 121384a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 12149a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 121584a3aa00SPavel Emelyanov 1216e474995fSDavid S. Miller fl4 = &fl4_stack; 12179a24abfaSDavid Ahern 1218c6af0c22SWillem de Bruijn flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, 12199a24abfaSDavid Ahern RT_SCOPE_UNIVERSE, sk->sk_protocol, 12209a24abfaSDavid Ahern flow_flags, 1221e2d118a1SLorenzo Colitti faddr, saddr, dport, inet->inet_sport, 1222e2d118a1SLorenzo Colitti sk->sk_uid); 1223c0951cbcSDavid S. Miller 12243df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 1225e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1226b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1227b23dd4feSDavid S. Miller err = PTR_ERR(rt); 122806dc94b1SDavid S. Miller rt = NULL; 1229db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1230f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1231db8dac20SDavid S. Miller goto out; 1232db8dac20SDavid S. Miller } 1233db8dac20SDavid S. Miller 1234db8dac20SDavid S. Miller err = -EACCES; 1235db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1236db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1237db8dac20SDavid S. Miller goto out; 1238db8dac20SDavid S. Miller if (connected) 1239d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1240db8dac20SDavid S. Miller } 1241db8dac20SDavid S. Miller 1242db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1243db8dac20SDavid S. Miller goto do_confirm; 1244db8dac20SDavid S. Miller back_from_confirm: 1245db8dac20SDavid S. Miller 1246e474995fSDavid S. Miller saddr = fl4->saddr; 1247db8dac20SDavid S. Miller if (!ipc.addr) 1248e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1249db8dac20SDavid S. Miller 1250903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1251903ab86dSHerbert Xu if (!corkreq) { 12521cd7884dSWillem de Bruijn struct inet_cork cork; 12531cd7884dSWillem de Bruijn 1254f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1255903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 12561cd7884dSWillem de Bruijn &cork, msg->msg_flags); 1257903ab86dSHerbert Xu err = PTR_ERR(skb); 125850c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 1259bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &cork); 1260903ab86dSHerbert Xu goto out; 1261903ab86dSHerbert Xu } 1262903ab86dSHerbert Xu 1263db8dac20SDavid S. Miller lock_sock(sk); 1264db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1265db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1266db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1267db8dac20SDavid S. Miller release_sock(sk); 1268db8dac20SDavid S. Miller 1269197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1270db8dac20SDavid S. Miller err = -EINVAL; 1271db8dac20SDavid S. Miller goto out; 1272db8dac20SDavid S. Miller } 1273db8dac20SDavid S. Miller /* 1274db8dac20SDavid S. Miller * Now cork the socket to pend data. 1275db8dac20SDavid S. Miller */ 1276b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1277b6f21b26SDavid S. Miller fl4->daddr = daddr; 1278b6f21b26SDavid S. Miller fl4->saddr = saddr; 12799cce96dfSDavid S. Miller fl4->fl4_dport = dport; 12809cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1281db8dac20SDavid S. Miller up->pending = AF_INET; 1282db8dac20SDavid S. Miller 1283db8dac20SDavid S. Miller do_append_data: 1284db8dac20SDavid S. Miller up->len += ulen; 1285f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 12862e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1287db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1288db8dac20SDavid S. Miller if (err) 1289db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1290db8dac20SDavid S. Miller else if (!corkreq) 1291db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1292db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1293db8dac20SDavid S. Miller up->pending = 0; 1294db8dac20SDavid S. Miller release_sock(sk); 1295db8dac20SDavid S. Miller 1296db8dac20SDavid S. Miller out: 1297db8dac20SDavid S. Miller ip_rt_put(rt); 12981b97013bSAndrey Ignatov out_free: 1299db8dac20SDavid S. Miller if (free) 1300db8dac20SDavid S. Miller kfree(ipc.opt); 1301db8dac20SDavid S. Miller if (!err) 1302db8dac20SDavid S. Miller return len; 1303db8dac20SDavid S. Miller /* 1304db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1305db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1306db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1307db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1308db8dac20SDavid S. Miller * seems like overkill. 1309db8dac20SDavid S. Miller */ 1310db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 13116aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1312629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1313db8dac20SDavid S. Miller } 1314db8dac20SDavid S. Miller return err; 1315db8dac20SDavid S. Miller 1316db8dac20SDavid S. Miller do_confirm: 13170dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 13180dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1319db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1320db8dac20SDavid S. Miller goto back_from_confirm; 1321db8dac20SDavid S. Miller err = 0; 1322db8dac20SDavid S. Miller goto out; 1323db8dac20SDavid S. Miller } 1324c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1325db8dac20SDavid S. Miller 1326db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 1327db8dac20SDavid S. Miller size_t size, int flags) 1328db8dac20SDavid S. Miller { 1329f5fca608SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1330db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1331db8dac20SDavid S. Miller int ret; 1332db8dac20SDavid S. Miller 1333d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 1334d3f7d56aSShawn Landden flags |= MSG_MORE; 1335d3f7d56aSShawn Landden 1336db8dac20SDavid S. Miller if (!up->pending) { 1337db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1338db8dac20SDavid S. Miller 1339db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 1340db8dac20SDavid S. Miller * sendpage interface can't pass. 1341db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 1342db8dac20SDavid S. Miller */ 13431b784140SYing Xue ret = udp_sendmsg(sk, &msg, 0); 1344db8dac20SDavid S. Miller if (ret < 0) 1345db8dac20SDavid S. Miller return ret; 1346db8dac20SDavid S. Miller } 1347db8dac20SDavid S. Miller 1348db8dac20SDavid S. Miller lock_sock(sk); 1349db8dac20SDavid S. Miller 1350db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 1351db8dac20SDavid S. Miller release_sock(sk); 1352db8dac20SDavid S. Miller 1353197df02cSMatteo Croce net_dbg_ratelimited("cork failed\n"); 1354db8dac20SDavid S. Miller return -EINVAL; 1355db8dac20SDavid S. Miller } 1356db8dac20SDavid S. Miller 1357f5fca608SDavid S. Miller ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1358f5fca608SDavid S. Miller page, offset, size, flags); 1359db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 1360db8dac20SDavid S. Miller release_sock(sk); 1361db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 1362db8dac20SDavid S. Miller size, flags); 1363db8dac20SDavid S. Miller } 1364db8dac20SDavid S. Miller if (ret < 0) { 1365db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1366db8dac20SDavid S. Miller goto out; 1367db8dac20SDavid S. Miller } 1368db8dac20SDavid S. Miller 1369db8dac20SDavid S. Miller up->len += size; 1370a9f59707SEric Dumazet if (!(READ_ONCE(up->corkflag) || (flags&MSG_MORE))) 1371db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 1372db8dac20SDavid S. Miller if (!ret) 1373db8dac20SDavid S. Miller ret = size; 1374db8dac20SDavid S. Miller out: 1375db8dac20SDavid S. Miller release_sock(sk); 1376db8dac20SDavid S. Miller return ret; 1377db8dac20SDavid S. Miller } 1378db8dac20SDavid S. Miller 1379dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1380dce4551cSPaolo Abeni 1381677bf08cSFlorian Westphal /* all head states (dst, sk, nf conntrack) except skb extensions are 1382677bf08cSFlorian Westphal * cleared by udp_rcv(). 1383677bf08cSFlorian Westphal * 1384677bf08cSFlorian Westphal * We need to preserve secpath, if present, to eventually process 1385677bf08cSFlorian Westphal * IP_CMSG_PASSSEC at recvmsg() time. 1386677bf08cSFlorian Westphal * 1387677bf08cSFlorian Westphal * Other extensions can be cleared. 1388677bf08cSFlorian Westphal */ 1389677bf08cSFlorian Westphal static bool udp_try_make_stateless(struct sk_buff *skb) 1390677bf08cSFlorian Westphal { 1391677bf08cSFlorian Westphal if (!skb_has_extensions(skb)) 1392677bf08cSFlorian Westphal return true; 1393677bf08cSFlorian Westphal 1394677bf08cSFlorian Westphal if (!secpath_exists(skb)) { 1395677bf08cSFlorian Westphal skb_ext_reset(skb); 1396677bf08cSFlorian Westphal return true; 1397677bf08cSFlorian Westphal } 1398677bf08cSFlorian Westphal 1399677bf08cSFlorian Westphal return false; 1400677bf08cSFlorian Westphal } 1401677bf08cSFlorian Westphal 1402b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1403b65ac446SPaolo Abeni { 1404dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1405b65ac446SPaolo Abeni 1406b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1407dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1408dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1409b65ac446SPaolo Abeni scratch->len = skb->len; 1410b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1411b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1412b65ac446SPaolo Abeni #endif 1413677bf08cSFlorian Westphal if (udp_try_make_stateless(skb)) 1414dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1415dce4551cSPaolo Abeni } 1416dce4551cSPaolo Abeni 1417a793183cSEric Dumazet static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) 1418a793183cSEric Dumazet { 1419a793183cSEric Dumazet /* We come here after udp_lib_checksum_complete() returned 0. 1420a793183cSEric Dumazet * This means that __skb_checksum_complete() might have 1421a793183cSEric Dumazet * set skb->csum_valid to 1. 1422a793183cSEric Dumazet * On 64bit platforms, we can set csum_unnecessary 1423a793183cSEric Dumazet * to true, but only if the skb is not shared. 1424a793183cSEric Dumazet */ 1425a793183cSEric Dumazet #if BITS_PER_LONG == 64 1426a793183cSEric Dumazet if (!skb_shared(skb)) 1427a793183cSEric Dumazet udp_skb_scratch(skb)->csum_unnecessary = true; 1428a793183cSEric Dumazet #endif 1429a793183cSEric Dumazet } 1430a793183cSEric Dumazet 1431dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1432dce4551cSPaolo Abeni { 1433dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1434dce4551cSPaolo Abeni } 1435dce4551cSPaolo Abeni 1436dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1437dce4551cSPaolo Abeni { 1438dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1439dce4551cSPaolo Abeni } 1440b65ac446SPaolo Abeni 14417c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 14426dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 14436dfb4367SPaolo Abeni bool rx_queue_lock_held) 1444f970bd9eSPaolo Abeni { 14456b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 14462276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1447f970bd9eSPaolo Abeni int amt; 1448f970bd9eSPaolo Abeni 14496b229cf7SEric Dumazet if (likely(partial)) { 14506b229cf7SEric Dumazet up->forward_deficit += size; 14516b229cf7SEric Dumazet size = up->forward_deficit; 14528a3854c7SPaolo Abeni if (size < READ_ONCE(up->forward_threshold) && 1453d39ca259SPaolo Abeni !skb_queue_empty(&up->reader_queue)) 14546b229cf7SEric Dumazet return; 14556b229cf7SEric Dumazet } else { 14566b229cf7SEric Dumazet size += up->forward_deficit; 14576b229cf7SEric Dumazet } 14586b229cf7SEric Dumazet up->forward_deficit = 0; 14596b229cf7SEric Dumazet 14606dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 14616dfb4367SPaolo Abeni * if the called don't held it already 14626dfb4367SPaolo Abeni */ 14632276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 14646dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14652276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 14662276f58aSPaolo Abeni 14676dfb4367SPaolo Abeni 1468f970bd9eSPaolo Abeni sk->sk_forward_alloc += size; 1469100fdd1fSEric Dumazet amt = (sk->sk_forward_alloc - partial) & ~(PAGE_SIZE - 1); 1470f970bd9eSPaolo Abeni sk->sk_forward_alloc -= amt; 1471f970bd9eSPaolo Abeni 1472f970bd9eSPaolo Abeni if (amt) 1473100fdd1fSEric Dumazet __sk_mem_reduce_allocated(sk, amt >> PAGE_SHIFT); 147402ab0d13SEric Dumazet 147502ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 14762276f58aSPaolo Abeni 14772276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 14782276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 14792276f58aSPaolo Abeni 14806dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14812276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1482f970bd9eSPaolo Abeni } 1483f970bd9eSPaolo Abeni 14842276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1485c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1486c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1487c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1488c84d9490SEric Dumazet */ 14897c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1490f970bd9eSPaolo Abeni { 1491b65ac446SPaolo Abeni prefetch(&skb->data); 1492b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1493f970bd9eSPaolo Abeni } 14947c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1495f970bd9eSPaolo Abeni 14966dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 149764f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 14986dfb4367SPaolo Abeni { 1499b65ac446SPaolo Abeni prefetch(&skb->data); 1500b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 15016dfb4367SPaolo Abeni } 15026dfb4367SPaolo Abeni 15034b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 15044b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 15054b272750SEric Dumazet * Under flood, this means that only one producer can be in line 15064b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 15074b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 15084b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 15094b272750SEric Dumazet */ 15104b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 15114b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 15124b272750SEric Dumazet 15134b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 15144b272750SEric Dumazet { 15154b272750SEric Dumazet spinlock_t *busy; 15164b272750SEric Dumazet 15174b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 15184b272750SEric Dumazet spin_lock(busy); 15194b272750SEric Dumazet return busy; 15204b272750SEric Dumazet } 15214b272750SEric Dumazet 15224b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 15234b272750SEric Dumazet { 15244b272750SEric Dumazet if (busy) 15254b272750SEric Dumazet spin_unlock(busy); 15264b272750SEric Dumazet } 15274b272750SEric Dumazet 1528f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1529f970bd9eSPaolo Abeni { 1530f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1531f970bd9eSPaolo Abeni int rmem, delta, amt, err = -ENOMEM; 15324b272750SEric Dumazet spinlock_t *busy = NULL; 1533c8c8b127SEric Dumazet int size; 1534f970bd9eSPaolo Abeni 1535f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1536f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1537f970bd9eSPaolo Abeni */ 1538f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1539363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1540f970bd9eSPaolo Abeni goto drop; 1541f970bd9eSPaolo Abeni 1542c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1543c8c8b127SEric Dumazet * having linear skbs : 1544c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1545c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1546c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1547c8c8b127SEric Dumazet */ 15484b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1549c8c8b127SEric Dumazet skb_condense(skb); 15504b272750SEric Dumazet 15514b272750SEric Dumazet busy = busylock_acquire(sk); 15524b272750SEric Dumazet } 1553c8c8b127SEric Dumazet size = skb->truesize; 1554b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1555c8c8b127SEric Dumazet 1556f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1557f970bd9eSPaolo Abeni * queue contains some other skb 1558f970bd9eSPaolo Abeni */ 1559f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1560feed8a4fSAntonio Messina if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) 1561f970bd9eSPaolo Abeni goto uncharge_drop; 1562f970bd9eSPaolo Abeni 1563f970bd9eSPaolo Abeni spin_lock(&list->lock); 1564f970bd9eSPaolo Abeni if (size >= sk->sk_forward_alloc) { 1565f970bd9eSPaolo Abeni amt = sk_mem_pages(size); 1566100fdd1fSEric Dumazet delta = amt << PAGE_SHIFT; 1567f970bd9eSPaolo Abeni if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1568f970bd9eSPaolo Abeni err = -ENOBUFS; 1569f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1570f970bd9eSPaolo Abeni goto uncharge_drop; 1571f970bd9eSPaolo Abeni } 1572f970bd9eSPaolo Abeni 1573f970bd9eSPaolo Abeni sk->sk_forward_alloc += delta; 1574f970bd9eSPaolo Abeni } 1575f970bd9eSPaolo Abeni 1576f970bd9eSPaolo Abeni sk->sk_forward_alloc -= size; 1577f970bd9eSPaolo Abeni 15787c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 15797c13f97fSPaolo Abeni * forward allocated memory on dequeue 15807c13f97fSPaolo Abeni */ 1581f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1582f970bd9eSPaolo Abeni 1583f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1584f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1585f970bd9eSPaolo Abeni 1586f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 1587f970bd9eSPaolo Abeni sk->sk_data_ready(sk); 1588f970bd9eSPaolo Abeni 15894b272750SEric Dumazet busylock_release(busy); 1590f970bd9eSPaolo Abeni return 0; 1591f970bd9eSPaolo Abeni 1592f970bd9eSPaolo Abeni uncharge_drop: 1593f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1594f970bd9eSPaolo Abeni 1595f970bd9eSPaolo Abeni drop: 1596f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 15974b272750SEric Dumazet busylock_release(busy); 1598f970bd9eSPaolo Abeni return err; 1599f970bd9eSPaolo Abeni } 1600f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1601f970bd9eSPaolo Abeni 1602d38afeecSKuniyuki Iwashima void udp_destruct_common(struct sock *sk) 1603f970bd9eSPaolo Abeni { 1604f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 16052276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 16067c13f97fSPaolo Abeni unsigned int total = 0; 16077c13f97fSPaolo Abeni struct sk_buff *skb; 16087c13f97fSPaolo Abeni 16092276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 16102276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 16117c13f97fSPaolo Abeni total += skb->truesize; 16127c13f97fSPaolo Abeni kfree_skb(skb); 16137c13f97fSPaolo Abeni } 16146dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 1615d38afeecSKuniyuki Iwashima } 1616d38afeecSKuniyuki Iwashima EXPORT_SYMBOL_GPL(udp_destruct_common); 16177c13f97fSPaolo Abeni 1618d38afeecSKuniyuki Iwashima static void udp_destruct_sock(struct sock *sk) 1619d38afeecSKuniyuki Iwashima { 1620d38afeecSKuniyuki Iwashima udp_destruct_common(sk); 1621f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1622f970bd9eSPaolo Abeni } 1623f970bd9eSPaolo Abeni 1624f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1625f970bd9eSPaolo Abeni { 16268a3854c7SPaolo Abeni udp_lib_init_sock(sk); 1627f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1628e993ffe3SPavel Begunkov set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 1629f970bd9eSPaolo Abeni return 0; 1630f970bd9eSPaolo Abeni } 1631f970bd9eSPaolo Abeni 1632f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1633f970bd9eSPaolo Abeni { 1634f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1635f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1636f970bd9eSPaolo Abeni 1637f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1638f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1639f970bd9eSPaolo Abeni } 16400a463c78SPaolo Abeni 1641ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1642ca2c1418SPaolo Abeni return; 1643ca2c1418SPaolo Abeni 1644dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1645dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 16460ddf3fb2SPaolo Abeni */ 1647dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 16480ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1649ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1650f970bd9eSPaolo Abeni } 1651f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1652f970bd9eSPaolo Abeni 16532276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 16542276f58aSPaolo Abeni struct sk_buff_head *rcvq, 16552276f58aSPaolo Abeni int *total) 16562276f58aSPaolo Abeni { 16572276f58aSPaolo Abeni struct sk_buff *skb; 16582276f58aSPaolo Abeni 16599bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 16609bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 16612276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 16622276f58aSPaolo Abeni IS_UDPLITE(sk)); 16632276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 16642276f58aSPaolo Abeni IS_UDPLITE(sk)); 16652276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 16662276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 16672276f58aSPaolo Abeni *total += skb->truesize; 16682276f58aSPaolo Abeni kfree_skb(skb); 16699bd780f5SPaolo Abeni } else { 1670a793183cSEric Dumazet udp_skb_csum_unnecessary_set(skb); 16719bd780f5SPaolo Abeni break; 16729bd780f5SPaolo Abeni } 16732276f58aSPaolo Abeni } 16742276f58aSPaolo Abeni return skb; 16752276f58aSPaolo Abeni } 16762276f58aSPaolo Abeni 167785584672SEric Dumazet /** 167885584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 167985584672SEric Dumazet * @sk: socket 168085584672SEric Dumazet * 168185584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1682e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 168385584672SEric Dumazet */ 1684e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 168585584672SEric Dumazet { 16862276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 16872276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 168885584672SEric Dumazet struct sk_buff *skb; 16897c13f97fSPaolo Abeni int total = 0; 1690e83c6744SEric Dumazet int res; 169185584672SEric Dumazet 169285584672SEric Dumazet spin_lock_bh(&rcvq->lock); 16932276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 1694137a0dbeSEric Dumazet if (!skb && !skb_queue_empty_lockless(sk_queue)) { 16952276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 16962276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 16972276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 16982276f58aSPaolo Abeni 16992276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 170085584672SEric Dumazet } 1701e83c6744SEric Dumazet res = skb ? skb->len : -1; 17027c13f97fSPaolo Abeni if (total) 17036dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 170485584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 170585584672SEric Dumazet return res; 170685584672SEric Dumazet } 170785584672SEric Dumazet 17081da177e4SLinus Torvalds /* 17091da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 17101da177e4SLinus Torvalds */ 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 17131da177e4SLinus Torvalds { 17146516c655SStephen Hemminger switch (cmd) { 17151da177e4SLinus Torvalds case SIOCOUTQ: 17161da177e4SLinus Torvalds { 171731e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 171831e6d363SEric Dumazet 17191da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds case SIOCINQ: 17231da177e4SLinus Torvalds { 1724e83c6744SEric Dumazet int amount = max_t(int, 0, first_packet_length(sk)); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds default: 17301da177e4SLinus Torvalds return -ENOIOCTLCMD; 17311da177e4SLinus Torvalds } 17326516c655SStephen Hemminger 17336516c655SStephen Hemminger return 0; 17341da177e4SLinus Torvalds } 1735c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 17361da177e4SLinus Torvalds 17372276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1738ec095263SOliver Hartkopp int *off, int *err) 17392276f58aSPaolo Abeni { 17402276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 17412276f58aSPaolo Abeni struct sk_buff_head *queue; 17422276f58aSPaolo Abeni struct sk_buff *last; 17432276f58aSPaolo Abeni long timeo; 17442276f58aSPaolo Abeni int error; 17452276f58aSPaolo Abeni 17462276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 17472276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 17482276f58aSPaolo Abeni do { 17492276f58aSPaolo Abeni struct sk_buff *skb; 17502276f58aSPaolo Abeni 17512276f58aSPaolo Abeni error = sock_error(sk); 17522276f58aSPaolo Abeni if (error) 17532276f58aSPaolo Abeni break; 17542276f58aSPaolo Abeni 17552276f58aSPaolo Abeni error = -EAGAIN; 17562276f58aSPaolo Abeni do { 17572276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 1758e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1759e427cad6SPaolo Abeni err, &last); 17602276f58aSPaolo Abeni if (skb) { 1761e427cad6SPaolo Abeni if (!(flags & MSG_PEEK)) 1762e427cad6SPaolo Abeni udp_skb_destructor(sk, skb); 17632276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17642276f58aSPaolo Abeni return skb; 17652276f58aSPaolo Abeni } 17662276f58aSPaolo Abeni 1767137a0dbeSEric Dumazet if (skb_queue_empty_lockless(sk_queue)) { 17682276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17692276f58aSPaolo Abeni goto busy_check; 17702276f58aSPaolo Abeni } 17712276f58aSPaolo Abeni 17726dfb4367SPaolo Abeni /* refill the reader queue and walk it again 17736dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 17746dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 17756dfb4367SPaolo Abeni * is needed. 17766dfb4367SPaolo Abeni */ 17772276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 17782276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 17792276f58aSPaolo Abeni 1780e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1781e427cad6SPaolo Abeni err, &last); 1782e427cad6SPaolo Abeni if (skb && !(flags & MSG_PEEK)) 1783e427cad6SPaolo Abeni udp_skb_dtor_locked(sk, skb); 17846dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 17852276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1786de321ed3SAndrey Vagin if (skb) 17872276f58aSPaolo Abeni return skb; 17882276f58aSPaolo Abeni 17892276f58aSPaolo Abeni busy_check: 17902276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 17912276f58aSPaolo Abeni break; 17922276f58aSPaolo Abeni 17932276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 1794137a0dbeSEric Dumazet } while (!skb_queue_empty_lockless(sk_queue)); 17952276f58aSPaolo Abeni 17962276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 17972276f58aSPaolo Abeni } while (timeo && 1798b50b0580SSabrina Dubroca !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, 1799b50b0580SSabrina Dubroca &error, &timeo, 18002276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 18012276f58aSPaolo Abeni 18022276f58aSPaolo Abeni *err = error; 18032276f58aSPaolo Abeni return NULL; 18042276f58aSPaolo Abeni } 18057e823644SJiri Kosina EXPORT_SYMBOL(__skb_recv_udp); 18062276f58aSPaolo Abeni 1807965b57b4SCong Wang int udp_read_skb(struct sock *sk, skb_read_actor_t recv_actor) 1808d7f57118SCong Wang { 1809d7f57118SCong Wang struct sk_buff *skb; 181031f1fbcbSPeilin Ye int err, copied; 1811d7f57118SCong Wang 181231f1fbcbSPeilin Ye try_again: 1813ec095263SOliver Hartkopp skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 1814d7f57118SCong Wang if (!skb) 1815d7f57118SCong Wang return err; 1816099f896fSCong Wang 1817099f896fSCong Wang if (udp_lib_checksum_complete(skb)) { 181831f1fbcbSPeilin Ye int is_udplite = IS_UDPLITE(sk); 181931f1fbcbSPeilin Ye struct net *net = sock_net(sk); 182031f1fbcbSPeilin Ye 182131f1fbcbSPeilin Ye __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, is_udplite); 182231f1fbcbSPeilin Ye __UDP_INC_STATS(net, UDP_MIB_INERRORS, is_udplite); 1823099f896fSCong Wang atomic_inc(&sk->sk_drops); 1824099f896fSCong Wang kfree_skb(skb); 182531f1fbcbSPeilin Ye goto try_again; 1826099f896fSCong Wang } 1827099f896fSCong Wang 1828db39dfdcSPeilin Ye WARN_ON_ONCE(!skb_set_owner_sk_safe(skb, sk)); 182931f1fbcbSPeilin Ye copied = recv_actor(sk, skb); 1830e00a5c33SCong Wang kfree_skb(skb); 1831d7f57118SCong Wang 1832d7f57118SCong Wang return copied; 1833d7f57118SCong Wang } 1834965b57b4SCong Wang EXPORT_SYMBOL(udp_read_skb); 1835d7f57118SCong Wang 1836db8dac20SDavid S. Miller /* 1837db8dac20SDavid S. Miller * This should be easy, if there is something there we 1838db8dac20SDavid S. Miller * return it, otherwise we block. 1839db8dac20SDavid S. Miller */ 1840db8dac20SDavid S. Miller 1841ec095263SOliver Hartkopp int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags, 1842ec095263SOliver Hartkopp int *addr_len) 1843db8dac20SDavid S. Miller { 1844db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1845342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1846db8dac20SDavid S. Miller struct sk_buff *skb; 184759c2cdaeSDavid S. Miller unsigned int ulen, copied; 1848fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 1849db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1850197c949eSEric Dumazet bool checksum_valid = false; 1851db8dac20SDavid S. Miller 1852db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 185385fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1854db8dac20SDavid S. Miller 1855db8dac20SDavid S. Miller try_again: 1856a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 1857ec095263SOliver Hartkopp skb = __skb_recv_udp(sk, flags, &off, &err); 1858db8dac20SDavid S. Miller if (!skb) 1859627d2d6bSsamanthakumar return err; 1860db8dac20SDavid S. Miller 1861b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 186259c2cdaeSDavid S. Miller copied = len; 1863627d2d6bSsamanthakumar if (copied > ulen - off) 1864627d2d6bSsamanthakumar copied = ulen - off; 186559c2cdaeSDavid S. Miller else if (copied < ulen) 1866db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1867db8dac20SDavid S. Miller 1868db8dac20SDavid S. Miller /* 1869db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1870db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1871db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1872db8dac20SDavid S. Miller */ 1873db8dac20SDavid S. Miller 1874d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1875d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1876b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1877b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1878197c949eSEric Dumazet if (!checksum_valid) 1879db8dac20SDavid S. Miller goto csum_copy_err; 1880db8dac20SDavid S. Miller } 1881db8dac20SDavid S. Miller 1882b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1883b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1884b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1885b65ac446SPaolo Abeni else 1886627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1887b65ac446SPaolo Abeni } else { 1888627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1889db8dac20SDavid S. Miller 1890db8dac20SDavid S. Miller if (err == -EINVAL) 1891db8dac20SDavid S. Miller goto csum_copy_err; 1892db8dac20SDavid S. Miller } 1893db8dac20SDavid S. Miller 189422911fc5SEric Dumazet if (unlikely(err)) { 1895fd69c399SPaolo Abeni if (!peeking) { 1896979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 18976aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1898979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1899979402b1SEric Dumazet } 1900850cbaddSPaolo Abeni kfree_skb(skb); 1901627d2d6bSsamanthakumar return err; 190222911fc5SEric Dumazet } 1903db8dac20SDavid S. Miller 1904fd69c399SPaolo Abeni if (!peeking) 19056aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1906629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1907db8dac20SDavid S. Miller 19086fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 1909db8dac20SDavid S. Miller 1910db8dac20SDavid S. Miller /* Copy the address. */ 1911c482c568SEric Dumazet if (sin) { 1912db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1913db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1914db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1915db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1916bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1917983695faSDaniel Borkmann 1918983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, 1919983695faSDaniel Borkmann (struct sockaddr *)sin); 1920db8dac20SDavid S. Miller } 1921bcd1665eSPaolo Abeni 1922bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 1923bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 1924bcd1665eSPaolo Abeni 1925db8dac20SDavid S. Miller if (inet->cmsg_flags) 1926ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1927db8dac20SDavid S. Miller 192859c2cdaeSDavid S. Miller err = copied; 1929db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1930db8dac20SDavid S. Miller err = ulen; 1931db8dac20SDavid S. Miller 1932850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1933db8dac20SDavid S. Miller return err; 1934db8dac20SDavid S. Miller 1935db8dac20SDavid S. Miller csum_copy_err: 19362276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 19372276f58aSPaolo Abeni udp_skb_destructor)) { 19386aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 19396aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 19406a5dc9e5SEric Dumazet } 1941850cbaddSPaolo Abeni kfree_skb(skb); 1942db8dac20SDavid S. Miller 1943beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1944beb39db5SEric Dumazet cond_resched(); 19459cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1946db8dac20SDavid S. Miller goto try_again; 1947db8dac20SDavid S. Miller } 1948db8dac20SDavid S. Miller 1949d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1950d74bad4eSAndrey Ignatov { 1951d74bad4eSAndrey Ignatov /* This check is replicated from __ip4_datagram_connect() and 1952d74bad4eSAndrey Ignatov * intended to prevent BPF program called below from accessing bytes 1953d74bad4eSAndrey Ignatov * that are out of the bound specified by user in addr_len. 1954d74bad4eSAndrey Ignatov */ 1955d74bad4eSAndrey Ignatov if (addr_len < sizeof(struct sockaddr_in)) 1956d74bad4eSAndrey Ignatov return -EINVAL; 1957d74bad4eSAndrey Ignatov 1958d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); 1959d74bad4eSAndrey Ignatov } 1960d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect); 1961d74bad4eSAndrey Ignatov 1962286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 19631da177e4SLinus Torvalds { 19641da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19651da177e4SLinus Torvalds /* 19661da177e4SLinus Torvalds * 1003.1g - break association. 19671da177e4SLinus Torvalds */ 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1970c720c7e8SEric Dumazet inet->inet_daddr = 0; 1971c720c7e8SEric Dumazet inet->inet_dport = 0; 1972bdeab991STom Herbert sock_rps_reset_rxhash(sk); 19731da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 1974303d0403SWillem de Bruijn if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { 19751da177e4SLinus Torvalds inet_reset_saddr(sk); 1976303d0403SWillem de Bruijn if (sk->sk_prot->rehash && 1977303d0403SWillem de Bruijn (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 1978303d0403SWillem de Bruijn sk->sk_prot->rehash(sk); 1979303d0403SWillem de Bruijn } 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 19821da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1983c720c7e8SEric Dumazet inet->inet_sport = 0; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds sk_dst_reset(sk); 19861da177e4SLinus Torvalds return 0; 19871da177e4SLinus Torvalds } 1988286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1989286c72deSEric Dumazet 1990286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1991286c72deSEric Dumazet { 1992286c72deSEric Dumazet lock_sock(sk); 1993286c72deSEric Dumazet __udp_disconnect(sk, flags); 1994286c72deSEric Dumazet release_sock(sk); 1995286c72deSEric Dumazet return 0; 1996286c72deSEric Dumazet } 1997c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 19981da177e4SLinus Torvalds 1999645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 2000645ca708SEric Dumazet { 2001723b4610SEric Dumazet if (sk_hashed(sk)) { 2002645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 2003512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 2004512615b6SEric Dumazet 2005512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 2006d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 2007512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 2008645ca708SEric Dumazet 2009c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 2010e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 2011e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 2012ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 2013fdcc8aa9SEric Dumazet hslot->count--; 2014c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 2015645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2016512615b6SEric Dumazet 2017512615b6SEric Dumazet spin_lock(&hslot2->lock); 2018ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2019512615b6SEric Dumazet hslot2->count--; 2020512615b6SEric Dumazet spin_unlock(&hslot2->lock); 2021645ca708SEric Dumazet } 2022c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 2023645ca708SEric Dumazet } 2024723b4610SEric Dumazet } 2025645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 2026645ca708SEric Dumazet 2027719f8358SEric Dumazet /* 2028719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 2029719f8358SEric Dumazet */ 2030719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 2031719f8358SEric Dumazet { 2032719f8358SEric Dumazet if (sk_hashed(sk)) { 2033719f8358SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 2034719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 2035719f8358SEric Dumazet 2036719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 2037719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 2038719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 2039e32ea7e7SCraig Gallek 2040e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 2041e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 2042719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 2043719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 2044719f8358SEric Dumazet /* we must lock primary chain too */ 2045719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 2046e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 2047e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 2048719f8358SEric Dumazet 2049e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 2050719f8358SEric Dumazet spin_lock(&hslot2->lock); 2051ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2052719f8358SEric Dumazet hslot2->count--; 2053719f8358SEric Dumazet spin_unlock(&hslot2->lock); 2054719f8358SEric Dumazet 2055719f8358SEric Dumazet spin_lock(&nhslot2->lock); 2056ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 2057719f8358SEric Dumazet &nhslot2->head); 2058719f8358SEric Dumazet nhslot2->count++; 2059719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 2060e32ea7e7SCraig Gallek } 2061719f8358SEric Dumazet 2062719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 2063719f8358SEric Dumazet } 2064719f8358SEric Dumazet } 2065719f8358SEric Dumazet } 2066719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 2067719f8358SEric Dumazet 20688f6b5392SAlexey Kodanev void udp_v4_rehash(struct sock *sk) 2069719f8358SEric Dumazet { 2070f0b1e64cSMartin KaFai Lau u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 2071719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 2072719f8358SEric Dumazet inet_sk(sk)->inet_num); 2073719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 2074719f8358SEric Dumazet } 2075719f8358SEric Dumazet 2076a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 207793821778SHerbert Xu { 2078fec5e652STom Herbert int rc; 207993821778SHerbert Xu 2080005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 2081bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 2082005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 20832c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 2084e68b6e50SEric Dumazet } else { 2085e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 2086005ec974SShawn Bohrer } 2087fec5e652STom Herbert 2088850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 2089766e9037SEric Dumazet if (rc < 0) { 2090766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 209108d4c037SMenglong Dong int drop_reason; 2092766e9037SEric Dumazet 209393821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 209408d4c037SMenglong Dong if (rc == -ENOMEM) { 2095e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 209693821778SHerbert Xu is_udplite); 209708d4c037SMenglong Dong drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF; 209808d4c037SMenglong Dong } else { 2099a3ce2b10SMenglong Dong UDP_INC_STATS(sock_net(sk), UDP_MIB_MEMERRORS, 2100a3ce2b10SMenglong Dong is_udplite); 210108d4c037SMenglong Dong drop_reason = SKB_DROP_REASON_PROTO_MEM; 210208d4c037SMenglong Dong } 2103e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 210408d4c037SMenglong Dong kfree_skb_reason(skb, drop_reason); 2105296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 2106766e9037SEric Dumazet return -1; 210793821778SHerbert Xu } 210893821778SHerbert Xu 210993821778SHerbert Xu return 0; 211093821778SHerbert Xu } 211193821778SHerbert Xu 2112db8dac20SDavid S. Miller /* returns: 2113db8dac20SDavid S. Miller * -1: error 2114db8dac20SDavid S. Miller * 0: success 2115db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 2116db8dac20SDavid S. Miller * 2117db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 2118db8dac20SDavid S. Miller * have either been requeued or freed. 2119db8dac20SDavid S. Miller */ 2120cf329aa4SPaolo Abeni static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 2121db8dac20SDavid S. Miller { 21221379a92dSMenglong Dong int drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2123db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 2124db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 2125db8dac20SDavid S. Miller 2126db8dac20SDavid S. Miller /* 2127db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 2128db8dac20SDavid S. Miller */ 21291379a92dSMenglong Dong if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 21301379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2131db8dac20SDavid S. Miller goto drop; 21321379a92dSMenglong Dong } 2133895b5c9fSFlorian Westphal nf_reset_ct(skb); 2134db8dac20SDavid S. Miller 213588ab3108SDavidlohr Bueso if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 21360ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 21370ad92ad0SEric Dumazet 2138db8dac20SDavid S. Miller /* 2139db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 2140db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 2141db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 2142db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 2143db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 2144db8dac20SDavid S. Miller * handler or was discarded by it. 2145db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 2146db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 2147db8dac20SDavid S. Miller */ 2148db8dac20SDavid S. Miller 2149db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 21506aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 2151e5aed006SHannes Frederic Sowa if (encap_rcv) { 2152db8dac20SDavid S. Miller int ret; 2153db8dac20SDavid S. Miller 21540a80966bSTom Herbert /* Verify checksum before giving to encap */ 21550a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 21560a80966bSTom Herbert goto csum_error; 21570a80966bSTom Herbert 21580ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 2159db8dac20SDavid S. Miller if (ret <= 0) { 216002c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 21610283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 2162db8dac20SDavid S. Miller is_udplite); 2163db8dac20SDavid S. Miller return -ret; 2164db8dac20SDavid S. Miller } 2165db8dac20SDavid S. Miller } 2166db8dac20SDavid S. Miller 2167db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 2168db8dac20SDavid S. Miller } 2169db8dac20SDavid S. Miller 2170db8dac20SDavid S. Miller /* 2171db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 2172db8dac20SDavid S. Miller */ 2173b0a42277SMiaohe Lin if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 2174db8dac20SDavid S. Miller 2175db8dac20SDavid S. Miller /* 2176db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 2177db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 2178db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 2179db8dac20SDavid S. Miller * protocol stack as such. 2180db8dac20SDavid S. Miller * 2181db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 2182db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 2183db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 2184db8dac20SDavid S. Miller * provided by the application." 2185db8dac20SDavid S. Miller */ 2186db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 2187ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 2188db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 2189db8dac20SDavid S. Miller goto drop; 2190db8dac20SDavid S. Miller } 2191db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 2192db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 2193db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 2194db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 2195db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 2196db8dac20SDavid S. Miller */ 2197db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 2198ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 2199db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 2200db8dac20SDavid S. Miller goto drop; 2201db8dac20SDavid S. Miller } 2202db8dac20SDavid S. Miller } 2203db8dac20SDavid S. Miller 2204dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 2205ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 2206ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 22076a5dc9e5SEric Dumazet goto csum_error; 2208ce25d66aSEric Dumazet 22091379a92dSMenglong Dong if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) { 22101379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 2211a6127697SMichal Kubeček goto drop; 22121379a92dSMenglong Dong } 2213db8dac20SDavid S. Miller 2214e6afc8acSsamanthakumar udp_csum_pull_header(skb); 2215db8dac20SDavid S. Miller 2216fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 2217850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 2218db8dac20SDavid S. Miller 22196a5dc9e5SEric Dumazet csum_error: 22201379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_UDP_CSUM; 222102c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2222db8dac20SDavid S. Miller drop: 222302c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 22248edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 22251379a92dSMenglong Dong kfree_skb_reason(skb, drop_reason); 2226db8dac20SDavid S. Miller return -1; 2227db8dac20SDavid S. Miller } 2228db8dac20SDavid S. Miller 2229cf329aa4SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2230cf329aa4SPaolo Abeni { 2231cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 2232cf329aa4SPaolo Abeni int ret; 2233cf329aa4SPaolo Abeni 2234cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 2235cf329aa4SPaolo Abeni return udp_queue_rcv_one_skb(sk, skb); 2236cf329aa4SPaolo Abeni 2237a08e7fd9SCambda Zhu BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); 2238cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 2239cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, true); 22401a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 2241cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 2242000ac44dSPaolo Abeni 2243000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb); 2244cf329aa4SPaolo Abeni ret = udp_queue_rcv_one_skb(sk, skb); 2245cf329aa4SPaolo Abeni if (ret > 0) 224610c678bdSXin Long ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret); 2247cf329aa4SPaolo Abeni } 2248cf329aa4SPaolo Abeni return 0; 2249cf329aa4SPaolo Abeni } 2250cf329aa4SPaolo Abeni 225197502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 2252e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 225397502231SEric Dumazet */ 225464f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2255421b3885SShawn Bohrer { 225697502231SEric Dumazet struct dst_entry *old; 2257421b3885SShawn Bohrer 2258d24406c8SWei Wang if (dst_hold_safe(dst)) { 22598f905c0eSEric Dumazet old = xchg((__force struct dst_entry **)&sk->sk_rx_dst, dst); 226097502231SEric Dumazet dst_release(old); 226164f0f5d1SPaolo Abeni return old != dst; 226297502231SEric Dumazet } 226364f0f5d1SPaolo Abeni return false; 2264d24406c8SWei Wang } 2265c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 2266421b3885SShawn Bohrer 2267db8dac20SDavid S. Miller /* 2268db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 2269db8dac20SDavid S. Miller * 22701240d137SEric Dumazet * Note: called only from the BH handler context. 2271db8dac20SDavid S. Miller */ 2272e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2273db8dac20SDavid S. Miller struct udphdr *uh, 2274db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 227536cbb245SRick Jones struct udp_table *udptable, 227636cbb245SRick Jones int proto) 2277db8dac20SDavid S. Miller { 2278ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 22795cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 22805cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 22812dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2282ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 2283ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 2284fb74c277SDavid Ahern int sdif = inet_sdif(skb); 2285ca065d0cSEric Dumazet struct hlist_node *node; 2286ca065d0cSEric Dumazet struct sk_buff *nskb; 22872dc41cffSDavid Held 22882dc41cffSDavid Held if (use_hash2) { 2289f0b1e64cSMartin KaFai Lau hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 229073e2d5e3SPablo Neira udptable->mask; 2291f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 22922dc41cffSDavid Held start_lookup: 229373e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 22942dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 22952dc41cffSDavid Held } 2296db8dac20SDavid S. Miller 2297ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2298ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2299fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 2300ca065d0cSEric Dumazet continue; 23011240d137SEric Dumazet 2302ca065d0cSEric Dumazet if (!first) { 2303ca065d0cSEric Dumazet first = sk; 2304ca065d0cSEric Dumazet continue; 2305ca065d0cSEric Dumazet } 2306ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 2307ca065d0cSEric Dumazet 2308ca065d0cSEric Dumazet if (unlikely(!nskb)) { 2309ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 231002c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2311ca065d0cSEric Dumazet IS_UDPLITE(sk)); 231202c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2313ca065d0cSEric Dumazet IS_UDPLITE(sk)); 2314ca065d0cSEric Dumazet continue; 2315ca065d0cSEric Dumazet } 2316ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 2317ca065d0cSEric Dumazet consume_skb(nskb); 2318ca065d0cSEric Dumazet } 23191240d137SEric Dumazet 23202dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 23212dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 23222dc41cffSDavid Held hash2 = hash2_any; 23232dc41cffSDavid Held goto start_lookup; 23242dc41cffSDavid Held } 23252dc41cffSDavid Held 2326ca065d0cSEric Dumazet if (first) { 2327ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 2328ca065d0cSEric Dumazet consume_skb(skb); 23291240d137SEric Dumazet } else { 2330ca065d0cSEric Dumazet kfree_skb(skb); 233102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 233236cbb245SRick Jones proto == IPPROTO_UDPLITE); 23331240d137SEric Dumazet } 2334db8dac20SDavid S. Miller return 0; 2335db8dac20SDavid S. Miller } 2336db8dac20SDavid S. Miller 2337db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2338db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2339666a3d6eSSu Yanjun * Otherwise, csum completion requires checksumming packet body, 2340db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2341db8dac20SDavid S. Miller */ 2342db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2343db8dac20SDavid S. Miller int proto) 2344db8dac20SDavid S. Miller { 2345db8dac20SDavid S. Miller int err; 2346db8dac20SDavid S. Miller 2347db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2348db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2349db8dac20SDavid S. Miller 2350db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2351db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2352db8dac20SDavid S. Miller if (err) 2353db8dac20SDavid S. Miller return err; 235415f35d49SAlexey Kodanev 235515f35d49SAlexey Kodanev if (UDP_SKB_CB(skb)->partial_cov) { 235615f35d49SAlexey Kodanev skb->csum = inet_compute_pseudo(skb, proto); 235715f35d49SAlexey Kodanev return 0; 235815f35d49SAlexey Kodanev } 2359db8dac20SDavid S. Miller } 2360db8dac20SDavid S. Miller 2361b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2362b46d9f62SHannes Frederic Sowa * force to int. 2363b46d9f62SHannes Frederic Sowa */ 2364db4f1be3SSean Tranchetti err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2365ed70fcfcSTom Herbert inet_compute_pseudo); 2366db4f1be3SSean Tranchetti if (err) 2367db4f1be3SSean Tranchetti return err; 2368db4f1be3SSean Tranchetti 2369db4f1be3SSean Tranchetti if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2370db4f1be3SSean Tranchetti /* If SW calculated the value, we know it's bad */ 2371db4f1be3SSean Tranchetti if (skb->csum_complete_sw) 2372db4f1be3SSean Tranchetti return 1; 2373db4f1be3SSean Tranchetti 2374db4f1be3SSean Tranchetti /* HW says the value is bad. Let's validate that. 2375db4f1be3SSean Tranchetti * skb->csum is no longer the full packet checksum, 2376db4f1be3SSean Tranchetti * so don't treat it as such. 2377db4f1be3SSean Tranchetti */ 2378db4f1be3SSean Tranchetti skb_checksum_complete_unset(skb); 2379db4f1be3SSean Tranchetti } 2380db4f1be3SSean Tranchetti 2381db4f1be3SSean Tranchetti return 0; 2382db8dac20SDavid S. Miller } 2383db8dac20SDavid S. Miller 23842b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 23852b5a9217SPaolo Abeni * return code conversion for ip layer consumption 23862b5a9217SPaolo Abeni */ 23872b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 23882b5a9217SPaolo Abeni struct udphdr *uh) 23892b5a9217SPaolo Abeni { 23902b5a9217SPaolo Abeni int ret; 23912b5a9217SPaolo Abeni 23922b5a9217SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2393e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); 23942b5a9217SPaolo Abeni 23952b5a9217SPaolo Abeni ret = udp_queue_rcv_skb(sk, skb); 23962b5a9217SPaolo Abeni 23972b5a9217SPaolo Abeni /* a return value > 0 means to resubmit the input, but 23982b5a9217SPaolo Abeni * it wants the return to be -protocol, or 0 23992b5a9217SPaolo Abeni */ 24002b5a9217SPaolo Abeni if (ret > 0) 24012b5a9217SPaolo Abeni return -ret; 24022b5a9217SPaolo Abeni return 0; 24032b5a9217SPaolo Abeni } 24042b5a9217SPaolo Abeni 2405db8dac20SDavid S. Miller /* 2406db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2407db8dac20SDavid S. Miller */ 2408db8dac20SDavid S. Miller 2409645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2410db8dac20SDavid S. Miller int proto) 2411db8dac20SDavid S. Miller { 2412db8dac20SDavid S. Miller struct sock *sk; 24137b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2414db8dac20SDavid S. Miller unsigned short ulen; 2415adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 24162783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 24170283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 241871489e21SJoe Stringer bool refcounted; 24191c7fab70SMenglong Dong int drop_reason; 24201c7fab70SMenglong Dong 24211c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2422db8dac20SDavid S. Miller 2423db8dac20SDavid S. Miller /* 2424db8dac20SDavid S. Miller * Validate the packet. 2425db8dac20SDavid S. Miller */ 2426db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2427db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2428db8dac20SDavid S. Miller 24297b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2430db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2431ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2432ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2433ccc2d97cSBjørn Mork 2434db8dac20SDavid S. Miller if (ulen > skb->len) 2435db8dac20SDavid S. Miller goto short_packet; 2436db8dac20SDavid S. Miller 2437db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2438db8dac20SDavid S. Miller /* UDP validates ulen. */ 2439db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2440db8dac20SDavid S. Miller goto short_packet; 2441db8dac20SDavid S. Miller uh = udp_hdr(skb); 2442db8dac20SDavid S. Miller } 2443db8dac20SDavid S. Miller 2444db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2445db8dac20SDavid S. Miller goto csum_error; 2446db8dac20SDavid S. Miller 244771489e21SJoe Stringer sk = skb_steal_sock(skb, &refcounted); 24488afdd99aSEric Dumazet if (sk) { 244997502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2450421b3885SShawn Bohrer int ret; 2451421b3885SShawn Bohrer 24528f905c0eSEric Dumazet if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 245397502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2454421b3885SShawn Bohrer 24552b5a9217SPaolo Abeni ret = udp_unicast_rcv_skb(sk, skb, uh); 245671489e21SJoe Stringer if (refcounted) 24578afdd99aSEric Dumazet sock_put(sk); 24582b5a9217SPaolo Abeni return ret; 2459c18450a5SFabian Frederick } 2460c18450a5SFabian Frederick 2461db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2462e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 246336cbb245SRick Jones saddr, daddr, udptable, proto); 2464db8dac20SDavid S. Miller 2465607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 24662b5a9217SPaolo Abeni if (sk) 24672b5a9217SPaolo Abeni return udp_unicast_rcv_skb(sk, skb, uh); 2468db8dac20SDavid S. Miller 2469db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2470db8dac20SDavid S. Miller goto drop; 2471895b5c9fSFlorian Westphal nf_reset_ct(skb); 2472db8dac20SDavid S. Miller 2473db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2474db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2475db8dac20SDavid S. Miller goto csum_error; 2476db8dac20SDavid S. Miller 24771c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_NO_SOCKET; 247802c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2479db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2480db8dac20SDavid S. Miller 2481db8dac20SDavid S. Miller /* 2482db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2483db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2484db8dac20SDavid S. Miller */ 24851c7fab70SMenglong Dong kfree_skb_reason(skb, drop_reason); 2486db8dac20SDavid S. Miller return 0; 2487db8dac20SDavid S. Miller 2488db8dac20SDavid S. Miller short_packet: 24891c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL; 2490ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2491afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2492afd46503SJoe Perches &saddr, ntohs(uh->source), 2493afd46503SJoe Perches ulen, skb->len, 2494afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2495db8dac20SDavid S. Miller goto drop; 2496db8dac20SDavid S. Miller 2497db8dac20SDavid S. Miller csum_error: 2498db8dac20SDavid S. Miller /* 2499db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2500db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2501db8dac20SDavid S. Miller */ 25021c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_UDP_CSUM; 2503ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2504afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2505afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2506db8dac20SDavid S. Miller ulen); 250702c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2508db8dac20SDavid S. Miller drop: 250902c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 25101c7fab70SMenglong Dong kfree_skb_reason(skb, drop_reason); 2511db8dac20SDavid S. Miller return 0; 2512db8dac20SDavid S. Miller } 2513db8dac20SDavid S. Miller 2514421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2515421b3885SShawn Bohrer * If more than one socket found returns NULL 2516421b3885SShawn Bohrer */ 2517421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2518421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2519421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2520fb74c277SDavid Ahern int dif, int sdif) 2521421b3885SShawn Bohrer { 2522421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2523*919dfa0bSKuniyuki Iwashima struct sock *sk, *result; 2524*919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot; 2525*919dfa0bSKuniyuki Iwashima unsigned int slot; 2526*919dfa0bSKuniyuki Iwashima 2527*919dfa0bSKuniyuki Iwashima slot = udp_hashfn(net, hnum, udp_table.mask); 2528*919dfa0bSKuniyuki Iwashima hslot = &udp_table.hash[slot]; 2529421b3885SShawn Bohrer 253063c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 253163c6f81cSEric Dumazet if (hslot->count > 10) 253263c6f81cSEric Dumazet return NULL; 253363c6f81cSEric Dumazet 2534421b3885SShawn Bohrer result = NULL; 2535ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2536ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2537fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2538ca065d0cSEric Dumazet if (result) 2539ca065d0cSEric Dumazet return NULL; 2540421b3885SShawn Bohrer result = sk; 2541421b3885SShawn Bohrer } 2542421b3885SShawn Bohrer } 2543421b3885SShawn Bohrer 2544421b3885SShawn Bohrer return result; 2545421b3885SShawn Bohrer } 2546421b3885SShawn Bohrer 2547421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2548421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2549421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2550421b3885SShawn Bohrer */ 2551421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2552421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2553421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 25543fa6f616SDavid Ahern int dif, int sdif) 2555421b3885SShawn Bohrer { 2556c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2557*919dfa0bSKuniyuki Iwashima unsigned short hnum = ntohs(loc_port); 2558*919dfa0bSKuniyuki Iwashima unsigned int hash2, slot2; 2559*919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot2; 2560*919dfa0bSKuniyuki Iwashima __portpair ports; 2561ca065d0cSEric Dumazet struct sock *sk; 2562421b3885SShawn Bohrer 2563*919dfa0bSKuniyuki Iwashima hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2564*919dfa0bSKuniyuki Iwashima slot2 = hash2 & udp_table.mask; 2565*919dfa0bSKuniyuki Iwashima hslot2 = &udp_table.hash2[slot2]; 2566*919dfa0bSKuniyuki Iwashima ports = INET_COMBINED_PORTS(rmt_port, hnum); 2567*919dfa0bSKuniyuki Iwashima 2568ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2569eda090c3SEric Dumazet if (inet_match(net, sk, acookie, ports, dif, sdif)) 2570ca065d0cSEric Dumazet return sk; 2571421b3885SShawn Bohrer /* Only check first socket in chain */ 2572421b3885SShawn Bohrer break; 2573421b3885SShawn Bohrer } 2574ca065d0cSEric Dumazet return NULL; 2575421b3885SShawn Bohrer } 2576421b3885SShawn Bohrer 25777487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2578421b3885SShawn Bohrer { 2579610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2580bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2581610438b7SEric Dumazet const struct iphdr *iph; 2582610438b7SEric Dumazet const struct udphdr *uh; 2583ca065d0cSEric Dumazet struct sock *sk = NULL; 2584421b3885SShawn Bohrer struct dst_entry *dst; 2585421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2586fb74c277SDavid Ahern int sdif = inet_sdif(skb); 25876e540309SShawn Bohrer int ours; 2588421b3885SShawn Bohrer 2589421b3885SShawn Bohrer /* validate the packet */ 2590421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 25917487449cSPaolo Abeni return 0; 2592421b3885SShawn Bohrer 2593610438b7SEric Dumazet iph = ip_hdr(skb); 2594610438b7SEric Dumazet uh = udp_hdr(skb); 2595610438b7SEric Dumazet 2596996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2597bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 25986e540309SShawn Bohrer 25996e540309SShawn Bohrer if (!in_dev) 26007487449cSPaolo Abeni return 0; 26016e540309SShawn Bohrer 26026e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 26036e540309SShawn Bohrer iph->protocol); 26046e540309SShawn Bohrer if (!ours) 26057487449cSPaolo Abeni return 0; 2606ad0ea198SPaolo Abeni 2607421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2608fb74c277SDavid Ahern uh->source, iph->saddr, 2609fb74c277SDavid Ahern dif, sdif); 26106e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2611421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 26123fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 26136e540309SShawn Bohrer } 2614421b3885SShawn Bohrer 261541c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 26167487449cSPaolo Abeni return 0; 2617421b3885SShawn Bohrer 2618421b3885SShawn Bohrer skb->sk = sk; 261982eabd9eSAlexander Duyck skb->destructor = sock_efree; 26208f905c0eSEric Dumazet dst = rcu_dereference(sk->sk_rx_dst); 2621421b3885SShawn Bohrer 2622421b3885SShawn Bohrer if (dst) 2623421b3885SShawn Bohrer dst = dst_check(dst, 0); 262410e2eb87SEric Dumazet if (dst) { 2625bc044e8dSPaolo Abeni u32 itag = 0; 2626bc044e8dSPaolo Abeni 2627d24406c8SWei Wang /* set noref for now. 2628d24406c8SWei Wang * any place which wants to hold dst has to call 2629d24406c8SWei Wang * dst_hold_safe() 2630d24406c8SWei Wang */ 2631421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2632bc044e8dSPaolo Abeni 2633bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2634bc044e8dSPaolo Abeni * the source on each packet 2635bc044e8dSPaolo Abeni */ 2636bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2637bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 26388d2b51b0SGuillaume Nault iph->saddr, 26398d2b51b0SGuillaume Nault iph->tos & IPTOS_RT_MASK, 2640bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2641421b3885SShawn Bohrer } 26427487449cSPaolo Abeni return 0; 264310e2eb87SEric Dumazet } 2644421b3885SShawn Bohrer 2645db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2646db8dac20SDavid S. Miller { 2647645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2648db8dac20SDavid S. Miller } 2649db8dac20SDavid S. Miller 26507d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2651db8dac20SDavid S. Miller { 265244046a59STom Parkin struct udp_sock *up = udp_sk(sk); 26538a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2654a8b897c7SPaolo Abeni 2655a8b897c7SPaolo Abeni /* protects from races with udp_abort() */ 2656a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 2657db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 26588a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 265960fb9567SPaolo Abeni if (static_branch_unlikely(&udp_encap_needed_key)) { 266060fb9567SPaolo Abeni if (up->encap_type) { 266144046a59STom Parkin void (*encap_destroy)(struct sock *sk); 26626aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 266344046a59STom Parkin if (encap_destroy) 266444046a59STom Parkin encap_destroy(sk); 266544046a59STom Parkin } 266660fb9567SPaolo Abeni if (up->encap_enabled) 26679c480601SPaolo Abeni static_branch_dec(&udp_encap_needed_key); 266860fb9567SPaolo Abeni } 2669db8dac20SDavid S. Miller } 2670db8dac20SDavid S. Miller 26711da177e4SLinus Torvalds /* 26721da177e4SLinus Torvalds * Socket option code for UDP 26731da177e4SLinus Torvalds */ 26744c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 267591ac1ccaSChristoph Hellwig sockptr_t optval, unsigned int optlen, 26764c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 26771da177e4SLinus Torvalds { 26781da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 26791c19448cSTom Herbert int val, valbool; 26801da177e4SLinus Torvalds int err = 0; 2681b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 26821da177e4SLinus Torvalds 26838a3854c7SPaolo Abeni if (level == SOL_SOCKET) { 26848a3854c7SPaolo Abeni err = sk_setsockopt(sk, level, optname, optval, optlen); 26858a3854c7SPaolo Abeni 26868a3854c7SPaolo Abeni if (optname == SO_RCVBUF || optname == SO_RCVBUFFORCE) { 26878a3854c7SPaolo Abeni sockopt_lock_sock(sk); 26888a3854c7SPaolo Abeni /* paired with READ_ONCE in udp_rmem_release() */ 26898a3854c7SPaolo Abeni WRITE_ONCE(up->forward_threshold, sk->sk_rcvbuf >> 2); 26908a3854c7SPaolo Abeni sockopt_release_sock(sk); 26918a3854c7SPaolo Abeni } 26928a3854c7SPaolo Abeni return err; 26938a3854c7SPaolo Abeni } 26948a3854c7SPaolo Abeni 26951da177e4SLinus Torvalds if (optlen < sizeof(int)) 26961da177e4SLinus Torvalds return -EINVAL; 26971da177e4SLinus Torvalds 269891ac1ccaSChristoph Hellwig if (copy_from_sockptr(&val, optval, sizeof(val))) 26991da177e4SLinus Torvalds return -EFAULT; 27001da177e4SLinus Torvalds 27011c19448cSTom Herbert valbool = val ? 1 : 0; 27021c19448cSTom Herbert 27031da177e4SLinus Torvalds switch (optname) { 27041da177e4SLinus Torvalds case UDP_CORK: 27051da177e4SLinus Torvalds if (val != 0) { 2706a9f59707SEric Dumazet WRITE_ONCE(up->corkflag, 1); 27071da177e4SLinus Torvalds } else { 2708a9f59707SEric Dumazet WRITE_ONCE(up->corkflag, 0); 27091da177e4SLinus Torvalds lock_sock(sk); 27104243cdc2SJoe Perches push_pending_frames(sk); 27111da177e4SLinus Torvalds release_sock(sk); 27121da177e4SLinus Torvalds } 27131da177e4SLinus Torvalds break; 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds case UDP_ENCAP: 27161da177e4SLinus Torvalds switch (val) { 27171da177e4SLinus Torvalds case 0: 2718fd1ac07fSAlexey Dobriyan #ifdef CONFIG_XFRM 27191da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 27201da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 27210146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 27220146dca7SSabrina Dubroca if (sk->sk_family == AF_INET6) 27230146dca7SSabrina Dubroca up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv; 27240146dca7SSabrina Dubroca else 27250146dca7SSabrina Dubroca #endif 2726067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 2727fd1ac07fSAlexey Dobriyan #endif 2728a8eceea8SJoe Perches fallthrough; 2729342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 27301da177e4SLinus Torvalds up->encap_type = val; 273160fb9567SPaolo Abeni lock_sock(sk); 273260fb9567SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 273360fb9567SPaolo Abeni release_sock(sk); 27341da177e4SLinus Torvalds break; 27351da177e4SLinus Torvalds default: 27361da177e4SLinus Torvalds err = -ENOPROTOOPT; 27371da177e4SLinus Torvalds break; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds break; 27401da177e4SLinus Torvalds 27411c19448cSTom Herbert case UDP_NO_CHECK6_TX: 27421c19448cSTom Herbert up->no_check6_tx = valbool; 27431c19448cSTom Herbert break; 27441c19448cSTom Herbert 27451c19448cSTom Herbert case UDP_NO_CHECK6_RX: 27461c19448cSTom Herbert up->no_check6_rx = valbool; 27471c19448cSTom Herbert break; 27481c19448cSTom Herbert 2749bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2750bec1f6f6SWillem de Bruijn if (val < 0 || val > USHRT_MAX) 2751bec1f6f6SWillem de Bruijn return -EINVAL; 275218a419baSEric Dumazet WRITE_ONCE(up->gso_size, val); 2753bec1f6f6SWillem de Bruijn break; 2754bec1f6f6SWillem de Bruijn 2755e20cf8d3SPaolo Abeni case UDP_GRO: 2756e20cf8d3SPaolo Abeni lock_sock(sk); 275778352f73SPaolo Abeni 275878352f73SPaolo Abeni /* when enabling GRO, accept the related GSO packet type */ 2759e20cf8d3SPaolo Abeni if (valbool) 2760e20cf8d3SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 2761e20cf8d3SPaolo Abeni up->gro_enabled = valbool; 276278352f73SPaolo Abeni up->accept_udp_l4 = valbool; 2763e20cf8d3SPaolo Abeni release_sock(sk); 2764e20cf8d3SPaolo Abeni break; 2765e20cf8d3SPaolo Abeni 2766ba4e58ecSGerrit Renker /* 2767ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2768ba4e58ecSGerrit Renker */ 2769ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2770ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2771ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2772b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2773ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2774ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2775ba4e58ecSGerrit Renker val = 8; 27764be929beSAlexey Dobriyan else if (val > USHRT_MAX) 27774be929beSAlexey Dobriyan val = USHRT_MAX; 2778ba4e58ecSGerrit Renker up->pcslen = val; 2779ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 2780ba4e58ecSGerrit Renker break; 2781ba4e58ecSGerrit Renker 2782ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2783ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2784ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2785ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2786b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2787ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2788ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2789ba4e58ecSGerrit Renker val = 8; 27904be929beSAlexey Dobriyan else if (val > USHRT_MAX) 27914be929beSAlexey Dobriyan val = USHRT_MAX; 2792ba4e58ecSGerrit Renker up->pcrlen = val; 2793ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 2794ba4e58ecSGerrit Renker break; 2795ba4e58ecSGerrit Renker 27961da177e4SLinus Torvalds default: 27971da177e4SLinus Torvalds err = -ENOPROTOOPT; 27981da177e4SLinus Torvalds break; 27996516c655SStephen Hemminger } 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds return err; 28021da177e4SLinus Torvalds } 2803c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 28041da177e4SLinus Torvalds 2805a7b75c5aSChristoph Hellwig int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 2806a7b75c5aSChristoph Hellwig unsigned int optlen) 2807db8dac20SDavid S. Miller { 28088a3854c7SPaolo Abeni if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET) 280991ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 2810a7b75c5aSChristoph Hellwig optval, optlen, 2811db8dac20SDavid S. Miller udp_push_pending_frames); 2812db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2813db8dac20SDavid S. Miller } 2814db8dac20SDavid S. Miller 28154c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 28161da177e4SLinus Torvalds char __user *optval, int __user *optlen) 28171da177e4SLinus Torvalds { 28181da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 28191da177e4SLinus Torvalds int val, len; 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds if (get_user(len, optlen)) 28221da177e4SLinus Torvalds return -EFAULT; 28231da177e4SLinus Torvalds 28241da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds if (len < 0) 28271da177e4SLinus Torvalds return -EINVAL; 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds switch (optname) { 28301da177e4SLinus Torvalds case UDP_CORK: 2831a9f59707SEric Dumazet val = READ_ONCE(up->corkflag); 28321da177e4SLinus Torvalds break; 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds case UDP_ENCAP: 28351da177e4SLinus Torvalds val = up->encap_type; 28361da177e4SLinus Torvalds break; 28371da177e4SLinus Torvalds 28381c19448cSTom Herbert case UDP_NO_CHECK6_TX: 28391c19448cSTom Herbert val = up->no_check6_tx; 28401c19448cSTom Herbert break; 28411c19448cSTom Herbert 28421c19448cSTom Herbert case UDP_NO_CHECK6_RX: 28431c19448cSTom Herbert val = up->no_check6_rx; 28441c19448cSTom Herbert break; 28451c19448cSTom Herbert 2846bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 284718a419baSEric Dumazet val = READ_ONCE(up->gso_size); 2848bec1f6f6SWillem de Bruijn break; 2849bec1f6f6SWillem de Bruijn 285098184612SNorman Maurer case UDP_GRO: 285198184612SNorman Maurer val = up->gro_enabled; 285298184612SNorman Maurer break; 285398184612SNorman Maurer 2854ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2855ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2856ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2857ba4e58ecSGerrit Renker val = up->pcslen; 2858ba4e58ecSGerrit Renker break; 2859ba4e58ecSGerrit Renker 2860ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2861ba4e58ecSGerrit Renker val = up->pcrlen; 2862ba4e58ecSGerrit Renker break; 2863ba4e58ecSGerrit Renker 28641da177e4SLinus Torvalds default: 28651da177e4SLinus Torvalds return -ENOPROTOOPT; 28666516c655SStephen Hemminger } 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds if (put_user(len, optlen)) 28691da177e4SLinus Torvalds return -EFAULT; 28701da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 28711da177e4SLinus Torvalds return -EFAULT; 28721da177e4SLinus Torvalds return 0; 28731da177e4SLinus Torvalds } 2874c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 28751da177e4SLinus Torvalds 2876db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2877db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2878db8dac20SDavid S. Miller { 2879db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2880db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2881db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2882db8dac20SDavid S. Miller } 2883db8dac20SDavid S. Miller 28841da177e4SLinus Torvalds /** 28851da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 28863628e3cbSAndrew Lunn * @file: - file struct 28873628e3cbSAndrew Lunn * @sock: - socket 28883628e3cbSAndrew Lunn * @wait: - poll table 28891da177e4SLinus Torvalds * 28901da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 28911da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 28921da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 28931da177e4SLinus Torvalds * then it could get return from select indicating data available 28941da177e4SLinus Torvalds * but then block when reading it. Add special case code 28951da177e4SLinus Torvalds * to work around these arguably broken applications. 28961da177e4SLinus Torvalds */ 2897a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 28981da177e4SLinus Torvalds { 2899a11e1d43SLinus Torvalds __poll_t mask = datagram_poll(file, sock, wait); 29001da177e4SLinus Torvalds struct sock *sk = sock->sk; 29011da177e4SLinus Torvalds 29023ef7cf57SEric Dumazet if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) 2903a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 29042276f58aSPaolo Abeni 29051da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 2906a11e1d43SLinus Torvalds if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2907e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2908a9a08845SLinus Torvalds mask &= ~(EPOLLIN | EPOLLRDNORM); 29091da177e4SLinus Torvalds 2910af493388SCong Wang /* psock ingress_msg queue should not contain any bad checksum frames */ 2911af493388SCong Wang if (sk_is_readable(sk)) 2912af493388SCong Wang mask |= EPOLLIN | EPOLLRDNORM; 29131da177e4SLinus Torvalds return mask; 29141da177e4SLinus Torvalds 29151da177e4SLinus Torvalds } 2916a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll); 29171da177e4SLinus Torvalds 29185d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 29195d77dca8SDavid Ahern { 29205d77dca8SDavid Ahern lock_sock(sk); 29215d77dca8SDavid Ahern 2922a8b897c7SPaolo Abeni /* udp{v6}_destroy_sock() sets it under the sk lock, avoid racing 2923a8b897c7SPaolo Abeni * with close() 2924a8b897c7SPaolo Abeni */ 2925a8b897c7SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 2926a8b897c7SPaolo Abeni goto out; 2927a8b897c7SPaolo Abeni 29285d77dca8SDavid Ahern sk->sk_err = err; 2929e3ae2365SAlexander Aring sk_error_report(sk); 2930286c72deSEric Dumazet __udp_disconnect(sk, 0); 29315d77dca8SDavid Ahern 2932a8b897c7SPaolo Abeni out: 29335d77dca8SDavid Ahern release_sock(sk); 29345d77dca8SDavid Ahern 29355d77dca8SDavid Ahern return 0; 29365d77dca8SDavid Ahern } 29375d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 29385d77dca8SDavid Ahern 2939db8dac20SDavid S. Miller struct proto udp_prot = { 2940db8dac20SDavid S. Miller .name = "UDP", 2941db8dac20SDavid S. Miller .owner = THIS_MODULE, 2942db8dac20SDavid S. Miller .close = udp_lib_close, 2943d74bad4eSAndrey Ignatov .pre_connect = udp_pre_connect, 2944db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2945db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2946db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2947850cbaddSPaolo Abeni .init = udp_init_sock, 2948db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2949db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2950db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2951db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2952db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 2953db8dac20SDavid S. Miller .sendpage = udp_sendpage, 29548141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2955db8dac20SDavid S. Miller .hash = udp_lib_hash, 2956db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2957719f8358SEric Dumazet .rehash = udp_v4_rehash, 2958db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 295991a760b2SMenglong Dong .put_port = udp_lib_unhash, 29608a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL 29618a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto, 29628a59f9d1SCong Wang #endif 2963db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 29640defbb0aSEric Dumazet .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc, 29650defbb0aSEric Dumazet 2966db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 29671e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 29681e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2969db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 2970645ca708SEric Dumazet .h.udp_table = &udp_table, 29715d77dca8SDavid Ahern .diag_destroy = udp_abort, 2972db8dac20SDavid S. Miller }; 2973c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 29761da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 29771da177e4SLinus Torvalds 2978645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 29791da177e4SLinus Torvalds { 29801da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 29816f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 2982*919dfa0bSKuniyuki Iwashima struct udp_seq_afinfo *afinfo; 2983*919dfa0bSKuniyuki Iwashima struct sock *sk; 29841da177e4SLinus Torvalds 29859e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 29869e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 29879e8ca27aSYonghong Song else 2988359745d7SMuchun Song afinfo = pde_data(file_inode(seq->file)); 29899e8ca27aSYonghong Song 2990a3d2599bSChristoph Hellwig for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; 2991f86dcc5aSEric Dumazet ++state->bucket) { 2992a3d2599bSChristoph Hellwig struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; 2993f86dcc5aSEric Dumazet 2994ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2995f86dcc5aSEric Dumazet continue; 2996f86dcc5aSEric Dumazet 2997645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 2998ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 2999878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 3000a91275efSDaniel Lezcano continue; 30019e8ca27aSYonghong Song if (afinfo->family == AF_UNSPEC || 30029e8ca27aSYonghong Song sk->sk_family == afinfo->family) 30031da177e4SLinus Torvalds goto found; 30041da177e4SLinus Torvalds } 3005645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 30061da177e4SLinus Torvalds } 30071da177e4SLinus Torvalds sk = NULL; 30081da177e4SLinus Torvalds found: 30091da177e4SLinus Torvalds return sk; 30101da177e4SLinus Torvalds } 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 30131da177e4SLinus Torvalds { 30141da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 30156f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 3016*919dfa0bSKuniyuki Iwashima struct udp_seq_afinfo *afinfo; 30171da177e4SLinus Torvalds 30189e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 30199e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 30209e8ca27aSYonghong Song else 3021359745d7SMuchun Song afinfo = pde_data(file_inode(seq->file)); 30229e8ca27aSYonghong Song 30231da177e4SLinus Torvalds do { 3024ca065d0cSEric Dumazet sk = sk_next(sk); 30259e8ca27aSYonghong Song } while (sk && (!net_eq(sock_net(sk), net) || 30269e8ca27aSYonghong Song (afinfo->family != AF_UNSPEC && 30279e8ca27aSYonghong Song sk->sk_family != afinfo->family))); 30281da177e4SLinus Torvalds 3029645ca708SEric Dumazet if (!sk) { 3030a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 3031a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 3032645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 30331da177e4SLinus Torvalds } 30341da177e4SLinus Torvalds return sk; 30351da177e4SLinus Torvalds } 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 30381da177e4SLinus Torvalds { 3039645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds if (sk) 30421da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 30431da177e4SLinus Torvalds --pos; 30441da177e4SLinus Torvalds return pos ? NULL : sk; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 3047a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos) 30481da177e4SLinus Torvalds { 304930842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 3050f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 305130842f29SVitaly Mayatskikh 3052b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 30531da177e4SLinus Torvalds } 3054a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start); 30551da177e4SLinus Torvalds 3056a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 30571da177e4SLinus Torvalds { 30581da177e4SLinus Torvalds struct sock *sk; 30591da177e4SLinus Torvalds 3060b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 30611da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 30621da177e4SLinus Torvalds else 30631da177e4SLinus Torvalds sk = udp_get_next(seq, v); 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds ++*pos; 30661da177e4SLinus Torvalds return sk; 30671da177e4SLinus Torvalds } 3068a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next); 30691da177e4SLinus Torvalds 3070a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v) 30711da177e4SLinus Torvalds { 3072645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 3073*919dfa0bSKuniyuki Iwashima struct udp_seq_afinfo *afinfo; 3074645ca708SEric Dumazet 30759e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 30769e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 30779e8ca27aSYonghong Song else 3078359745d7SMuchun Song afinfo = pde_data(file_inode(seq->file)); 30799e8ca27aSYonghong Song 3080a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 3081a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 30821da177e4SLinus Torvalds } 3083a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop); 3084db8dac20SDavid S. Miller 3085db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 30865e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 3087652586dfSTetsuo Handa int bucket) 3088db8dac20SDavid S. Miller { 3089db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 3090c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 3091c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 3092c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 3093c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 3094db8dac20SDavid S. Miller 3095f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 3096ea9a0379SPatrick Talbert " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", 3097db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 309831e6d363SEric Dumazet sk_wmem_alloc_get(sp), 30996c206b20SPaolo Abeni udp_rqueue_get(sp), 3100a7cb5a49SEric W. Biederman 0, 0L, 0, 3101a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 3102a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 310341c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 3104652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 3105db8dac20SDavid S. Miller } 3106db8dac20SDavid S. Miller 3107db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 3108db8dac20SDavid S. Miller { 3109652586dfSTetsuo Handa seq_setwidth(seq, 127); 3110db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 3111652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 3112db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 3113cb61cb9bSEric Dumazet "inode ref pointer drops"); 3114db8dac20SDavid S. Miller else { 3115db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 3116db8dac20SDavid S. Miller 3117652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 3118db8dac20SDavid S. Miller } 3119652586dfSTetsuo Handa seq_pad(seq, '\n'); 3120db8dac20SDavid S. Miller return 0; 3121db8dac20SDavid S. Miller } 3122db8dac20SDavid S. Miller 31235788b3a0SYonghong Song #ifdef CONFIG_BPF_SYSCALL 31245788b3a0SYonghong Song struct bpf_iter__udp { 31255788b3a0SYonghong Song __bpf_md_ptr(struct bpf_iter_meta *, meta); 31265788b3a0SYonghong Song __bpf_md_ptr(struct udp_sock *, udp_sk); 31275788b3a0SYonghong Song uid_t uid __aligned(8); 31285788b3a0SYonghong Song int bucket __aligned(8); 31295788b3a0SYonghong Song }; 31305788b3a0SYonghong Song 31315788b3a0SYonghong Song static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 31325788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 31335788b3a0SYonghong Song { 31345788b3a0SYonghong Song struct bpf_iter__udp ctx; 31355788b3a0SYonghong Song 31365788b3a0SYonghong Song meta->seq_num--; /* skip SEQ_START_TOKEN */ 31375788b3a0SYonghong Song ctx.meta = meta; 31385788b3a0SYonghong Song ctx.udp_sk = udp_sk; 31395788b3a0SYonghong Song ctx.uid = uid; 31405788b3a0SYonghong Song ctx.bucket = bucket; 31415788b3a0SYonghong Song return bpf_iter_run_prog(prog, &ctx); 31425788b3a0SYonghong Song } 31435788b3a0SYonghong Song 31445788b3a0SYonghong Song static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) 31455788b3a0SYonghong Song { 31465788b3a0SYonghong Song struct udp_iter_state *state = seq->private; 31475788b3a0SYonghong Song struct bpf_iter_meta meta; 31485788b3a0SYonghong Song struct bpf_prog *prog; 31495788b3a0SYonghong Song struct sock *sk = v; 31505788b3a0SYonghong Song uid_t uid; 31515788b3a0SYonghong Song 31525788b3a0SYonghong Song if (v == SEQ_START_TOKEN) 31535788b3a0SYonghong Song return 0; 31545788b3a0SYonghong Song 31555788b3a0SYonghong Song uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); 31565788b3a0SYonghong Song meta.seq = seq; 31575788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, false); 31585788b3a0SYonghong Song return udp_prog_seq_show(prog, &meta, v, uid, state->bucket); 31595788b3a0SYonghong Song } 31605788b3a0SYonghong Song 31615788b3a0SYonghong Song static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) 31625788b3a0SYonghong Song { 31635788b3a0SYonghong Song struct bpf_iter_meta meta; 31645788b3a0SYonghong Song struct bpf_prog *prog; 31655788b3a0SYonghong Song 31665788b3a0SYonghong Song if (!v) { 31675788b3a0SYonghong Song meta.seq = seq; 31685788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, true); 31695788b3a0SYonghong Song if (prog) 31705788b3a0SYonghong Song (void)udp_prog_seq_show(prog, &meta, v, 0, 0); 31715788b3a0SYonghong Song } 31725788b3a0SYonghong Song 31735788b3a0SYonghong Song udp_seq_stop(seq, v); 31745788b3a0SYonghong Song } 31755788b3a0SYonghong Song 31765788b3a0SYonghong Song static const struct seq_operations bpf_iter_udp_seq_ops = { 31775788b3a0SYonghong Song .start = udp_seq_start, 31785788b3a0SYonghong Song .next = udp_seq_next, 31795788b3a0SYonghong Song .stop = bpf_iter_udp_seq_stop, 31805788b3a0SYonghong Song .show = bpf_iter_udp_seq_show, 31815788b3a0SYonghong Song }; 31825788b3a0SYonghong Song #endif 31835788b3a0SYonghong Song 3184c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = { 3185a3d2599bSChristoph Hellwig .start = udp_seq_start, 3186a3d2599bSChristoph Hellwig .next = udp_seq_next, 3187a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 3188a3d2599bSChristoph Hellwig .show = udp4_seq_show, 3189a3d2599bSChristoph Hellwig }; 3190c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops); 319173cb88ecSArjan van de Ven 3192db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 3193db8dac20SDavid S. Miller .family = AF_INET, 3194645ca708SEric Dumazet .udp_table = &udp_table, 3195db8dac20SDavid S. Miller }; 3196db8dac20SDavid S. Miller 31972c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 319815439febSPavel Emelyanov { 3199c3506372SChristoph Hellwig if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 3200c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 3201a3d2599bSChristoph Hellwig return -ENOMEM; 3202a3d2599bSChristoph Hellwig return 0; 320315439febSPavel Emelyanov } 320415439febSPavel Emelyanov 32052c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 320615439febSPavel Emelyanov { 3207a3d2599bSChristoph Hellwig remove_proc_entry("udp", net->proc_net); 320815439febSPavel Emelyanov } 320915439febSPavel Emelyanov 321015439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 321115439febSPavel Emelyanov .init = udp4_proc_init_net, 321215439febSPavel Emelyanov .exit = udp4_proc_exit_net, 321315439febSPavel Emelyanov }; 321415439febSPavel Emelyanov 3215db8dac20SDavid S. Miller int __init udp4_proc_init(void) 3216db8dac20SDavid S. Miller { 321715439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 3218db8dac20SDavid S. Miller } 3219db8dac20SDavid S. Miller 3220db8dac20SDavid S. Miller void udp4_proc_exit(void) 3221db8dac20SDavid S. Miller { 322215439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 3223db8dac20SDavid S. Miller } 32241da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 32251da177e4SLinus Torvalds 3226f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 3227f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 3228645ca708SEric Dumazet { 3229413c27d8SEldad Zack ssize_t ret; 3230413c27d8SEldad Zack 3231f86dcc5aSEric Dumazet if (!str) 3232f86dcc5aSEric Dumazet return 0; 3233413c27d8SEldad Zack 3234413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 3235413c27d8SEldad Zack if (ret) 3236413c27d8SEldad Zack return 0; 3237413c27d8SEldad Zack 3238f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 3239f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 3240f86dcc5aSEric Dumazet return 1; 3241f86dcc5aSEric Dumazet } 3242f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 3243645ca708SEric Dumazet 3244f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 3245f86dcc5aSEric Dumazet { 3246f86dcc5aSEric Dumazet unsigned int i; 3247f86dcc5aSEric Dumazet 3248f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 3249512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 3250f86dcc5aSEric Dumazet uhash_entries, 3251f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 3252f86dcc5aSEric Dumazet 0, 3253f86dcc5aSEric Dumazet &table->log, 3254f86dcc5aSEric Dumazet &table->mask, 325531fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 3256f86dcc5aSEric Dumazet 64 * 1024); 325731fe62b9STim Bird 3258512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 3259f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 3260ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 3261fdcc8aa9SEric Dumazet table->hash[i].count = 0; 3262645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 3263645ca708SEric Dumazet } 3264512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 3265ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 3266512615b6SEric Dumazet table->hash2[i].count = 0; 3267512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 3268512615b6SEric Dumazet } 3269645ca708SEric Dumazet } 3270645ca708SEric Dumazet 3271723b8e46STom Herbert u32 udp_flow_hashrnd(void) 3272723b8e46STom Herbert { 3273723b8e46STom Herbert static u32 hashrnd __read_mostly; 3274723b8e46STom Herbert 3275723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 3276723b8e46STom Herbert 3277723b8e46STom Herbert return hashrnd; 3278723b8e46STom Herbert } 3279723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 3280723b8e46STom Herbert 328102a7cb28SKuniyuki Iwashima static int __net_init udp_sysctl_init(struct net *net) 32821e802951STonghao Zhang { 3283100fdd1fSEric Dumazet net->ipv4.sysctl_udp_rmem_min = PAGE_SIZE; 3284100fdd1fSEric Dumazet net->ipv4.sysctl_udp_wmem_min = PAGE_SIZE; 32851e802951STonghao Zhang 32861e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV 32871e802951STonghao Zhang net->ipv4.sysctl_udp_l3mdev_accept = 0; 32881e802951STonghao Zhang #endif 32891e802951STonghao Zhang 32901e802951STonghao Zhang return 0; 32911e802951STonghao Zhang } 32921e802951STonghao Zhang 32931e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = { 32941e802951STonghao Zhang .init = udp_sysctl_init, 32951e802951STonghao Zhang }; 32961e802951STonghao Zhang 32975788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 32985788b3a0SYonghong Song DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, 32995788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 33005788b3a0SYonghong Song 3301f9c79272SYonghong Song static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux) 33025788b3a0SYonghong Song { 33035788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 33045788b3a0SYonghong Song struct udp_seq_afinfo *afinfo; 33055788b3a0SYonghong Song int ret; 33065788b3a0SYonghong Song 33075788b3a0SYonghong Song afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN); 33085788b3a0SYonghong Song if (!afinfo) 33095788b3a0SYonghong Song return -ENOMEM; 33105788b3a0SYonghong Song 33115788b3a0SYonghong Song afinfo->family = AF_UNSPEC; 33125788b3a0SYonghong Song afinfo->udp_table = &udp_table; 33135788b3a0SYonghong Song st->bpf_seq_afinfo = afinfo; 3314f9c79272SYonghong Song ret = bpf_iter_init_seq_net(priv_data, aux); 33155788b3a0SYonghong Song if (ret) 33165788b3a0SYonghong Song kfree(afinfo); 33175788b3a0SYonghong Song return ret; 33185788b3a0SYonghong Song } 33195788b3a0SYonghong Song 33205788b3a0SYonghong Song static void bpf_iter_fini_udp(void *priv_data) 33215788b3a0SYonghong Song { 33225788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 33235788b3a0SYonghong Song 33245788b3a0SYonghong Song kfree(st->bpf_seq_afinfo); 33255788b3a0SYonghong Song bpf_iter_fini_seq_net(priv_data); 33265788b3a0SYonghong Song } 33275788b3a0SYonghong Song 332814fc6bd6SYonghong Song static const struct bpf_iter_seq_info udp_seq_info = { 33295788b3a0SYonghong Song .seq_ops = &bpf_iter_udp_seq_ops, 33305788b3a0SYonghong Song .init_seq_private = bpf_iter_init_udp, 33315788b3a0SYonghong Song .fini_seq_private = bpf_iter_fini_udp, 33325788b3a0SYonghong Song .seq_priv_size = sizeof(struct udp_iter_state), 333314fc6bd6SYonghong Song }; 333414fc6bd6SYonghong Song 333514fc6bd6SYonghong Song static struct bpf_iter_reg udp_reg_info = { 333614fc6bd6SYonghong Song .target = "udp", 33375788b3a0SYonghong Song .ctx_arg_info_size = 1, 33385788b3a0SYonghong Song .ctx_arg_info = { 33395788b3a0SYonghong Song { offsetof(struct bpf_iter__udp, udp_sk), 33405788b3a0SYonghong Song PTR_TO_BTF_ID_OR_NULL }, 33415788b3a0SYonghong Song }, 334214fc6bd6SYonghong Song .seq_info = &udp_seq_info, 33435788b3a0SYonghong Song }; 33445788b3a0SYonghong Song 33455788b3a0SYonghong Song static void __init bpf_iter_register(void) 33465788b3a0SYonghong Song { 3347951cf368SYonghong Song udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP]; 33485788b3a0SYonghong Song if (bpf_iter_reg_target(&udp_reg_info)) 33495788b3a0SYonghong Song pr_warn("Warning: could not register bpf iterator udp\n"); 33505788b3a0SYonghong Song } 33515788b3a0SYonghong Song #endif 33525788b3a0SYonghong Song 335395766fffSHideo Aoki void __init udp_init(void) 335495766fffSHideo Aoki { 3355f03d78dbSEric Dumazet unsigned long limit; 33564b272750SEric Dumazet unsigned int i; 335795766fffSHideo Aoki 3358f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 3359f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 336095766fffSHideo Aoki limit = max(limit, 128UL); 336195766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 336295766fffSHideo Aoki sysctl_udp_mem[1] = limit; 336395766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 336495766fffSHideo Aoki 33654b272750SEric Dumazet /* 16 spinlocks per cpu */ 33664b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 33674b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 33684b272750SEric Dumazet GFP_KERNEL); 33694b272750SEric Dumazet if (!udp_busylocks) 33704b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 33714b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 33724b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 33731e802951STonghao Zhang 33741e802951STonghao Zhang if (register_pernet_subsys(&udp_sysctl_ops)) 33751e802951STonghao Zhang panic("UDP: failed to init sysctl parameters.\n"); 33765788b3a0SYonghong Song 33775788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 33785788b3a0SYonghong Song bpf_iter_register(); 33795788b3a0SYonghong Song #endif 338095766fffSHideo Aoki } 3381