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); 12895766fffSHideo Aoki 129f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536 130f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 13198322f22SEric Dumazet 132f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 133645ca708SEric Dumazet const struct udp_hslot *hslot, 13498322f22SEric Dumazet unsigned long *bitmap, 135fe38d2a1SJosef Bacik struct sock *sk, unsigned int log) 13625030a7fSGerrit Renker { 137f24d43c0SEric Dumazet struct sock *sk2; 138ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 13925030a7fSGerrit Renker 140ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 141f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 142f24d43c0SEric Dumazet sk2 != sk && 143d4cada4aSEric Dumazet (bitmap || udp_sk(sk2)->udp_port_hash == num) && 144f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1459d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1469d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 147fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 148df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 149df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 150df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 151df560056SEric Garver if (!bitmap) 152df560056SEric Garver return 0; 153df560056SEric Garver } else { 1544243cdc2SJoe Perches if (!bitmap) 155fc038410SDavid S. Miller return 1; 156df560056SEric Garver __set_bit(udp_sk(sk2)->udp_port_hash >> log, 157df560056SEric Garver bitmap); 158df560056SEric Garver } 1594243cdc2SJoe Perches } 16098322f22SEric Dumazet } 16125030a7fSGerrit Renker return 0; 16225030a7fSGerrit Renker } 16325030a7fSGerrit Renker 16430fff923SEric Dumazet /* 16530fff923SEric Dumazet * Note: we still hold spinlock of primary hash chain, so no other writer 16630fff923SEric Dumazet * can insert/delete a socket with local_port == num 16730fff923SEric Dumazet */ 16830fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num, 16930fff923SEric Dumazet struct udp_hslot *hslot2, 170fe38d2a1SJosef Bacik struct sock *sk) 17130fff923SEric Dumazet { 17230fff923SEric Dumazet struct sock *sk2; 173ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 17430fff923SEric Dumazet int res = 0; 17530fff923SEric Dumazet 17630fff923SEric Dumazet spin_lock(&hslot2->lock); 177ca065d0cSEric Dumazet udp_portaddr_for_each_entry(sk2, &hslot2->head) { 17830fff923SEric Dumazet if (net_eq(sock_net(sk2), net) && 17930fff923SEric Dumazet sk2 != sk && 18030fff923SEric Dumazet (udp_sk(sk2)->udp_port_hash == num) && 18130fff923SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1829d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1839d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 184fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 185df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 186df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 187df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 188df560056SEric Garver res = 0; 189df560056SEric Garver } else { 19030fff923SEric Dumazet res = 1; 191df560056SEric Garver } 19230fff923SEric Dumazet break; 19330fff923SEric Dumazet } 1944243cdc2SJoe Perches } 19530fff923SEric Dumazet spin_unlock(&hslot2->lock); 19630fff923SEric Dumazet return res; 19730fff923SEric Dumazet } 19830fff923SEric Dumazet 199fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 200e32ea7e7SCraig Gallek { 201e32ea7e7SCraig Gallek struct net *net = sock_net(sk); 202e32ea7e7SCraig Gallek kuid_t uid = sock_i_uid(sk); 203e32ea7e7SCraig Gallek struct sock *sk2; 204e32ea7e7SCraig Gallek 205ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 206e32ea7e7SCraig Gallek if (net_eq(sock_net(sk2), net) && 207e32ea7e7SCraig Gallek sk2 != sk && 208e32ea7e7SCraig Gallek sk2->sk_family == sk->sk_family && 209e32ea7e7SCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 210e32ea7e7SCraig Gallek (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 211e32ea7e7SCraig Gallek (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 212e32ea7e7SCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 213fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) { 2142dbb9b9eSMartin KaFai Lau return reuseport_add_sock(sk, sk2, 2152dbb9b9eSMartin KaFai Lau inet_rcv_saddr_any(sk)); 216e32ea7e7SCraig Gallek } 217e32ea7e7SCraig Gallek } 218e32ea7e7SCraig Gallek 2192dbb9b9eSMartin KaFai Lau return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 220e32ea7e7SCraig Gallek } 221e32ea7e7SCraig Gallek 22225030a7fSGerrit Renker /** 2236ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 22425030a7fSGerrit Renker * 22525030a7fSGerrit Renker * @sk: socket struct in question 22625030a7fSGerrit Renker * @snum: port number to look up 22725985edcSLucas De Marchi * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 22830fff923SEric Dumazet * with NULL address 22925030a7fSGerrit Renker */ 2306ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 23130fff923SEric Dumazet unsigned int hash2_nulladdr) 2321da177e4SLinus Torvalds { 233512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 234645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 23525030a7fSGerrit Renker int error = 1; 2363b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 2371da177e4SLinus Torvalds 23832c1da70SStephen Hemminger if (!snum) { 2399088c560SEric Dumazet int low, high, remaining; 24095c96174SEric Dumazet unsigned int rand; 24198322f22SEric Dumazet unsigned short first, last; 24298322f22SEric Dumazet DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 2431da177e4SLinus Torvalds 2440bbf87d8SEric W. Biederman inet_get_local_port_range(net, &low, &high); 245a25de534SAnton Arapov remaining = (high - low) + 1; 246227b60f5SStephen Hemminger 24763862b5bSAruna-Hewapathirane rand = prandom_u32(); 2488fc54f68SDaniel Borkmann first = reciprocal_scale(rand, remaining) + low; 24998322f22SEric Dumazet /* 25098322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 25198322f22SEric Dumazet */ 252f86dcc5aSEric Dumazet rand = (rand | 1) * (udptable->mask + 1); 2535781b235SEric Dumazet last = first + udptable->mask + 1; 2545781b235SEric Dumazet do { 255f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, first); 25698322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 257645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 25898322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 259fe38d2a1SJosef Bacik udptable->log); 26098322f22SEric Dumazet 26198322f22SEric Dumazet snum = first; 26298322f22SEric Dumazet /* 26398322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 26498322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 26598322f22SEric Dumazet * give us randomization and full range coverage. 26698322f22SEric Dumazet */ 2679088c560SEric Dumazet do { 26898322f22SEric Dumazet if (low <= snum && snum <= high && 269e3826f1eSAmerigo Wang !test_bit(snum >> udptable->log, bitmap) && 270122ff243SWANG Cong !inet_is_local_reserved_port(net, snum)) 27198322f22SEric Dumazet goto found; 27298322f22SEric Dumazet snum += rand; 27398322f22SEric Dumazet } while (snum != first); 27498322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 275df560056SEric Garver cond_resched(); 2765781b235SEric Dumazet } while (++first != last); 27798322f22SEric Dumazet goto fail; 278645ca708SEric Dumazet } else { 279f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, snum); 280645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 28130fff923SEric Dumazet if (hslot->count > 10) { 28230fff923SEric Dumazet int exist; 28330fff923SEric Dumazet unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 28430fff923SEric Dumazet 28530fff923SEric Dumazet slot2 &= udptable->mask; 28630fff923SEric Dumazet hash2_nulladdr &= udptable->mask; 28730fff923SEric Dumazet 28830fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, slot2); 28930fff923SEric Dumazet if (hslot->count < hslot2->count) 29030fff923SEric Dumazet goto scan_primary_hash; 29130fff923SEric Dumazet 292fe38d2a1SJosef Bacik exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 29330fff923SEric Dumazet if (!exist && (hash2_nulladdr != slot2)) { 29430fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 29530fff923SEric Dumazet exist = udp_lib_lport_inuse2(net, snum, hslot2, 296fe38d2a1SJosef Bacik sk); 29730fff923SEric Dumazet } 29830fff923SEric Dumazet if (exist) 29930fff923SEric Dumazet goto fail_unlock; 30030fff923SEric Dumazet else 30130fff923SEric Dumazet goto found; 30230fff923SEric Dumazet } 30330fff923SEric Dumazet scan_primary_hash: 304fe38d2a1SJosef Bacik if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 305645ca708SEric Dumazet goto fail_unlock; 306645ca708SEric Dumazet } 30798322f22SEric Dumazet found: 308c720c7e8SEric Dumazet inet_sk(sk)->inet_num = snum; 309d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash = snum; 310d4cada4aSEric Dumazet udp_sk(sk)->udp_portaddr_hash ^= snum; 3111da177e4SLinus Torvalds if (sk_unhashed(sk)) { 312e32ea7e7SCraig Gallek if (sk->sk_reuseport && 313fe38d2a1SJosef Bacik udp_reuseport_add_sock(sk, hslot)) { 314e32ea7e7SCraig Gallek inet_sk(sk)->inet_num = 0; 315e32ea7e7SCraig Gallek udp_sk(sk)->udp_port_hash = 0; 316e32ea7e7SCraig Gallek udp_sk(sk)->udp_portaddr_hash ^= snum; 317e32ea7e7SCraig Gallek goto fail_unlock; 318e32ea7e7SCraig Gallek } 319e32ea7e7SCraig Gallek 320ca065d0cSEric Dumazet sk_add_node_rcu(sk, &hslot->head); 321fdcc8aa9SEric Dumazet hslot->count++; 322c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 323512615b6SEric Dumazet 324512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 325512615b6SEric Dumazet spin_lock(&hslot2->lock); 326d894ba18SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 327d894ba18SCraig Gallek sk->sk_family == AF_INET6) 3281602f49bSDavid S. Miller hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 329d894ba18SCraig Gallek &hslot2->head); 330d894ba18SCraig Gallek else 331ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 332512615b6SEric Dumazet &hslot2->head); 333512615b6SEric Dumazet hslot2->count++; 334512615b6SEric Dumazet spin_unlock(&hslot2->lock); 3351da177e4SLinus Torvalds } 336ca065d0cSEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 33725030a7fSGerrit Renker error = 0; 338645ca708SEric Dumazet fail_unlock: 339645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 3401da177e4SLinus Torvalds fail: 34125030a7fSGerrit Renker return error; 3421da177e4SLinus Torvalds } 343c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port); 3441da177e4SLinus Torvalds 3456ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 346db8dac20SDavid S. Miller { 34730fff923SEric Dumazet unsigned int hash2_nulladdr = 348f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 34930fff923SEric Dumazet unsigned int hash2_partial = 350f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 35130fff923SEric Dumazet 352d4cada4aSEric Dumazet /* precompute partial secondary hash */ 35330fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 354fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 355db8dac20SDavid S. Miller } 356db8dac20SDavid S. Miller 357d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 358d1e37288SSu, Xuemin __be32 saddr, __be16 sport, 359fb74c277SDavid Ahern __be32 daddr, unsigned short hnum, 36073545373STim Beale int dif, int sdif) 361645ca708SEric Dumazet { 36260c04aecSJoe Perches int score; 36360c04aecSJoe Perches struct inet_sock *inet; 3646da5b0f0SMike Manning bool dev_match; 365645ca708SEric Dumazet 36660c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 36760c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 36860c04aecSJoe Perches ipv6_only_sock(sk)) 36960c04aecSJoe Perches return -1; 370645ca708SEric Dumazet 3714cdeeee9SPeter Oskolkov if (sk->sk_rcv_saddr != daddr) 372645ca708SEric Dumazet return -1; 37360c04aecSJoe Perches 3744cdeeee9SPeter Oskolkov score = (sk->sk_family == PF_INET) ? 2 : 1; 3754cdeeee9SPeter Oskolkov 3764cdeeee9SPeter Oskolkov inet = inet_sk(sk); 377c720c7e8SEric Dumazet if (inet->inet_daddr) { 378c720c7e8SEric Dumazet if (inet->inet_daddr != saddr) 379645ca708SEric Dumazet return -1; 380ba418fa3STom Herbert score += 4; 381645ca708SEric Dumazet } 38260c04aecSJoe Perches 383c720c7e8SEric Dumazet if (inet->inet_dport) { 384c720c7e8SEric Dumazet if (inet->inet_dport != sport) 385645ca708SEric Dumazet return -1; 386ba418fa3STom Herbert score += 4; 387645ca708SEric Dumazet } 38860c04aecSJoe Perches 3896da5b0f0SMike Manning dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 3906da5b0f0SMike Manning dif, sdif); 39169678bcdSPaolo Abeni if (!dev_match) 392645ca708SEric Dumazet return -1; 3938d6c414cSMike Manning if (sk->sk_bound_dev_if) 394ba418fa3STom Herbert score += 4; 395fb74c277SDavid Ahern 3967170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 39770da268bSEric Dumazet score++; 398645ca708SEric Dumazet return score; 399645ca708SEric Dumazet } 400645ca708SEric Dumazet 4016eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 40265cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 40365cd8033SHannes Frederic Sowa const __be16 fport) 40465cd8033SHannes Frederic Sowa { 4051bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4061bbdceefSHannes Frederic Sowa 4071bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4081bbdceefSHannes Frederic Sowa 40965cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4101bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 41165cd8033SHannes Frederic Sowa } 41265cd8033SHannes Frederic Sowa 413a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk, 4147629c73aSJakub Sitnicki struct sk_buff *skb, 4157629c73aSJakub Sitnicki __be32 saddr, __be16 sport, 4167629c73aSJakub Sitnicki __be32 daddr, unsigned short hnum) 4177629c73aSJakub Sitnicki { 4187629c73aSJakub Sitnicki struct sock *reuse_sk = NULL; 4197629c73aSJakub Sitnicki u32 hash; 4207629c73aSJakub Sitnicki 4217629c73aSJakub Sitnicki if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 4227629c73aSJakub Sitnicki hash = udp_ehashfn(net, daddr, hnum, saddr, sport); 4237629c73aSJakub Sitnicki reuse_sk = reuseport_select_sock(sk, hash, skb, 4247629c73aSJakub Sitnicki sizeof(struct udphdr)); 4257629c73aSJakub Sitnicki } 4267629c73aSJakub Sitnicki return reuse_sk; 4277629c73aSJakub Sitnicki } 4287629c73aSJakub Sitnicki 429d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4305051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4315051ebd2SEric Dumazet __be32 saddr, __be16 sport, 432fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 43373545373STim Beale int dif, int sdif, 434d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4351134158bSCraig Gallek struct sk_buff *skb) 4365051ebd2SEric Dumazet { 4375051ebd2SEric Dumazet struct sock *sk, *result; 438e94a62f5SPaolo Abeni int score, badness; 4395051ebd2SEric Dumazet 4405051ebd2SEric Dumazet result = NULL; 441ba418fa3STom Herbert badness = 0; 442ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 443d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 44473545373STim Beale daddr, hnum, dif, sdif); 4455051ebd2SEric Dumazet if (score > badness) { 4467629c73aSJakub Sitnicki result = lookup_reuseport(net, sk, skb, 4477629c73aSJakub Sitnicki saddr, sport, daddr, hnum); 448a57066b1SDavid S. Miller /* Fall back to scoring if group has connections */ 449a57066b1SDavid S. Miller if (result && !reuseport_has_conns(sk, false)) 450ca065d0cSEric Dumazet return result; 4517629c73aSJakub Sitnicki 452a57066b1SDavid S. Miller result = result ? : sk; 453ca065d0cSEric Dumazet badness = score; 4545051ebd2SEric Dumazet } 4555051ebd2SEric Dumazet } 4565051ebd2SEric Dumazet return result; 4575051ebd2SEric Dumazet } 4585051ebd2SEric Dumazet 459a57066b1SDavid S. Miller static struct sock *udp4_lookup_run_bpf(struct net *net, 46072f7e944SJakub Sitnicki struct udp_table *udptable, 46172f7e944SJakub Sitnicki struct sk_buff *skb, 46272f7e944SJakub Sitnicki __be32 saddr, __be16 sport, 463f8931565SMark Pashmfouroush __be32 daddr, u16 hnum, const int dif) 46472f7e944SJakub Sitnicki { 46572f7e944SJakub Sitnicki struct sock *sk, *reuse_sk; 46672f7e944SJakub Sitnicki bool no_reuseport; 46772f7e944SJakub Sitnicki 46872f7e944SJakub Sitnicki if (udptable != &udp_table) 46972f7e944SJakub Sitnicki return NULL; /* only UDP is supported */ 47072f7e944SJakub Sitnicki 471f8931565SMark Pashmfouroush no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP, saddr, sport, 472f8931565SMark Pashmfouroush daddr, hnum, dif, &sk); 47372f7e944SJakub Sitnicki if (no_reuseport || IS_ERR_OR_NULL(sk)) 47472f7e944SJakub Sitnicki return sk; 47572f7e944SJakub Sitnicki 47672f7e944SJakub Sitnicki reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); 477c64c9c28SJakub Sitnicki if (reuse_sk) 47872f7e944SJakub Sitnicki sk = reuse_sk; 47972f7e944SJakub Sitnicki return sk; 48072f7e944SJakub Sitnicki } 48172f7e944SJakub Sitnicki 482db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 483db8dac20SDavid S. Miller * harder than this. -DaveM 484db8dac20SDavid S. Miller */ 485fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 486fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 487fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 488db8dac20SDavid S. Miller { 489db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4904cdeeee9SPeter Oskolkov unsigned int hash2, slot2; 4914cdeeee9SPeter Oskolkov struct udp_hslot *hslot2; 49272f7e944SJakub Sitnicki struct sock *result, *sk; 493db8dac20SDavid S. Miller 494f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 4955051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4965051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4975051ebd2SEric Dumazet 49872f7e944SJakub Sitnicki /* Lookup connected or non-wildcard socket */ 4995051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 500fb74c277SDavid Ahern daddr, hnum, dif, sdif, 50173545373STim Beale hslot2, skb); 50272f7e944SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 50372f7e944SJakub Sitnicki goto done; 50472f7e944SJakub Sitnicki 50572f7e944SJakub Sitnicki /* Lookup redirect from BPF */ 50672f7e944SJakub Sitnicki if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 50772f7e944SJakub Sitnicki sk = udp4_lookup_run_bpf(net, udptable, skb, 508f8931565SMark Pashmfouroush saddr, sport, daddr, hnum, dif); 50972f7e944SJakub Sitnicki if (sk) { 51072f7e944SJakub Sitnicki result = sk; 51172f7e944SJakub Sitnicki goto done; 51272f7e944SJakub Sitnicki } 51372f7e944SJakub Sitnicki } 51472f7e944SJakub Sitnicki 51572f7e944SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 51672f7e944SJakub Sitnicki if (result) 51772f7e944SJakub Sitnicki goto done; 51872f7e944SJakub Sitnicki 51972f7e944SJakub Sitnicki /* Lookup wildcard sockets */ 520f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 5215051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 5225051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 5235051ebd2SEric Dumazet 5241223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 5254cdeeee9SPeter Oskolkov htonl(INADDR_ANY), hnum, dif, sdif, 52673545373STim Beale hslot2, skb); 52772f7e944SJakub Sitnicki done: 52888e235b8SEnrico Weigelt if (IS_ERR(result)) 5298217ca65SMartin KaFai Lau return NULL; 5305051ebd2SEric Dumazet return result; 5315051ebd2SEric Dumazet } 532fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 533db8dac20SDavid S. Miller 534607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 535607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 536645ca708SEric Dumazet struct udp_table *udptable) 537607c4aafSKOVACS Krisztian { 538607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 539607c4aafSKOVACS Krisztian 540ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 541607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 542fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 543607c4aafSKOVACS Krisztian } 544607c4aafSKOVACS Krisztian 5457b58e63eSEric Dumazet struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb, 54663058308STom Herbert __be16 sport, __be16 dport) 54763058308STom Herbert { 548257a525fSMartin KaFai Lau const struct iphdr *iph = ip_hdr(skb); 549257a525fSMartin KaFai Lau 550257a525fSMartin KaFai Lau return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 551257a525fSMartin KaFai Lau iph->daddr, dport, inet_iif(skb), 552257a525fSMartin KaFai Lau inet_sdif(skb), &udp_table, NULL); 55363058308STom Herbert } 55463058308STom Herbert 555ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 556ca065d0cSEric Dumazet * Does increment socket refcount. 557ca065d0cSEric Dumazet */ 5586e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 559bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 560bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 561bcd41303SKOVACS Krisztian { 562ca065d0cSEric Dumazet struct sock *sk; 563ca065d0cSEric Dumazet 564ca065d0cSEric Dumazet sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 565fb74c277SDavid Ahern dif, 0, &udp_table, NULL); 56641c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 567ca065d0cSEric Dumazet sk = NULL; 568ca065d0cSEric Dumazet return sk; 569bcd41303SKOVACS Krisztian } 570bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 571ca065d0cSEric Dumazet #endif 572bcd41303SKOVACS Krisztian 573421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, 574421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 575421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 576fb74c277SDavid Ahern int dif, int sdif, unsigned short hnum) 577421b3885SShawn Bohrer { 578421b3885SShawn Bohrer struct inet_sock *inet = inet_sk(sk); 579421b3885SShawn Bohrer 580421b3885SShawn Bohrer if (!net_eq(sock_net(sk), net) || 581421b3885SShawn Bohrer udp_sk(sk)->udp_port_hash != hnum || 582421b3885SShawn Bohrer (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 583421b3885SShawn Bohrer (inet->inet_dport != rmt_port && inet->inet_dport) || 584421b3885SShawn Bohrer (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 585421b3885SShawn Bohrer ipv6_only_sock(sk) || 58682ba25c6STim Beale !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 587421b3885SShawn Bohrer return false; 58860d9b031SDavid Ahern if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 589421b3885SShawn Bohrer return false; 590421b3885SShawn Bohrer return true; 591421b3885SShawn Bohrer } 592421b3885SShawn Bohrer 593a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 594a36e185eSStefano Brivio void udp_encap_enable(void) 595a36e185eSStefano Brivio { 5969c480601SPaolo Abeni static_branch_inc(&udp_encap_needed_key); 597a36e185eSStefano Brivio } 598a36e185eSStefano Brivio EXPORT_SYMBOL(udp_encap_enable); 599a36e185eSStefano Brivio 600a4a600ddSXin Long void udp_encap_disable(void) 601a4a600ddSXin Long { 602a4a600ddSXin Long static_branch_dec(&udp_encap_needed_key); 603a4a600ddSXin Long } 604a4a600ddSXin Long EXPORT_SYMBOL(udp_encap_disable); 605a4a600ddSXin Long 606e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 607e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 608e7cc0824SStefano Brivio */ 609e7cc0824SStefano Brivio static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 610e7cc0824SStefano Brivio { 611e7cc0824SStefano Brivio int i; 612e7cc0824SStefano Brivio 613e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 614e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, u32 info); 61592b95364SPaolo Abeni const struct ip_tunnel_encap_ops *encap; 616e7cc0824SStefano Brivio 61792b95364SPaolo Abeni encap = rcu_dereference(iptun_encaps[i]); 61892b95364SPaolo Abeni if (!encap) 619e7cc0824SStefano Brivio continue; 62092b95364SPaolo Abeni handler = encap->err_handler; 621e7cc0824SStefano Brivio if (handler && !handler(skb, info)) 622e7cc0824SStefano Brivio return 0; 623e7cc0824SStefano Brivio } 624e7cc0824SStefano Brivio 625e7cc0824SStefano Brivio return -ENOENT; 626e7cc0824SStefano Brivio } 627e7cc0824SStefano Brivio 628a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 629a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 630a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 631a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 632a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 633a36e185eSStefano Brivio * trace ICMP messages back to them. 634a36e185eSStefano Brivio * 635e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 636e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 637e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 638e7cc0824SStefano Brivio * 639a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 640a36e185eSStefano Brivio * association. 641a36e185eSStefano Brivio * 642e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 643e7cc0824SStefano Brivio * processing, zero otherwise. 644a36e185eSStefano Brivio */ 645a36e185eSStefano Brivio static struct sock *__udp4_lib_err_encap(struct net *net, 646a36e185eSStefano Brivio const struct iphdr *iph, 647a36e185eSStefano Brivio struct udphdr *uh, 648a36e185eSStefano Brivio struct udp_table *udptable, 6499bfce73cSVadim Fedorenko struct sock *sk, 650e7cc0824SStefano Brivio struct sk_buff *skb, u32 info) 651a36e185eSStefano Brivio { 6529bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb); 653a36e185eSStefano Brivio int network_offset, transport_offset; 6549bfce73cSVadim Fedorenko struct udp_sock *up; 655a36e185eSStefano Brivio 656a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 657a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 658a36e185eSStefano Brivio 659a36e185eSStefano Brivio /* Network header needs to point to the outer IPv4 header inside ICMP */ 660a36e185eSStefano Brivio skb_reset_network_header(skb); 661a36e185eSStefano Brivio 662a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 663a36e185eSStefano Brivio skb_set_transport_header(skb, iph->ihl << 2); 664a36e185eSStefano Brivio 6659bfce73cSVadim Fedorenko if (sk) { 6669bfce73cSVadim Fedorenko up = udp_sk(sk); 6679bfce73cSVadim Fedorenko 6689bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup); 6699bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb)) 6709bfce73cSVadim Fedorenko sk = NULL; 6719bfce73cSVadim Fedorenko 6729bfce73cSVadim Fedorenko goto out; 6739bfce73cSVadim Fedorenko } 6749bfce73cSVadim Fedorenko 675e7cc0824SStefano Brivio sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 676e7cc0824SStefano Brivio iph->saddr, uh->dest, skb->dev->ifindex, 0, 677e7cc0824SStefano Brivio udptable, NULL); 678e7cc0824SStefano Brivio if (sk) { 6799bfce73cSVadim Fedorenko up = udp_sk(sk); 680e7cc0824SStefano Brivio 681a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 682a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 683a36e185eSStefano Brivio sk = NULL; 684e7cc0824SStefano Brivio } 685e7cc0824SStefano Brivio 6869bfce73cSVadim Fedorenko out: 687e7cc0824SStefano Brivio if (!sk) 688e7cc0824SStefano Brivio sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 689a36e185eSStefano Brivio 690a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 691a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 692a36e185eSStefano Brivio 693a36e185eSStefano Brivio return sk; 694a36e185eSStefano Brivio } 695a36e185eSStefano Brivio 696db8dac20SDavid S. Miller /* 697db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 698db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 699db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 700db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 701db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 702db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 703db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 704db8dac20SDavid S. Miller * to find the appropriate port. 705db8dac20SDavid S. Miller */ 706db8dac20SDavid S. Miller 70732bbd879SStefano Brivio int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 708db8dac20SDavid S. Miller { 709db8dac20SDavid S. Miller struct inet_sock *inet; 710b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 711db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 712db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 713db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 714a36e185eSStefano Brivio bool tunnel = false; 715db8dac20SDavid S. Miller struct sock *sk; 716db8dac20SDavid S. Miller int harderr; 717db8dac20SDavid S. Miller int err; 718fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 719db8dac20SDavid S. Miller 720fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 721f64bf6b8SMike Manning iph->saddr, uh->source, skb->dev->ifindex, 722f64bf6b8SMike Manning inet_sdif(skb), udptable, NULL); 7239bfce73cSVadim Fedorenko 724d26796aeSXin Long if (!sk || udp_sk(sk)->encap_type) { 725a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 726e7cc0824SStefano Brivio if (static_branch_unlikely(&udp_encap_needed_key)) { 7279bfce73cSVadim Fedorenko sk = __udp4_lib_err_encap(net, iph, uh, udptable, sk, skb, 728e7cc0824SStefano Brivio info); 729e7cc0824SStefano Brivio if (!sk) 730e7cc0824SStefano Brivio return 0; 7319bfce73cSVadim Fedorenko } else 7329bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT); 733e7cc0824SStefano Brivio 734e7cc0824SStefano Brivio if (IS_ERR(sk)) { 735e7cc0824SStefano Brivio __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 736e7cc0824SStefano Brivio return PTR_ERR(sk); 737e7cc0824SStefano Brivio } 738e7cc0824SStefano Brivio 739a36e185eSStefano Brivio tunnel = true; 740db8dac20SDavid S. Miller } 741db8dac20SDavid S. Miller 742db8dac20SDavid S. Miller err = 0; 743db8dac20SDavid S. Miller harderr = 0; 744db8dac20SDavid S. Miller inet = inet_sk(sk); 745db8dac20SDavid S. Miller 746db8dac20SDavid S. Miller switch (type) { 747db8dac20SDavid S. Miller default: 748db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 749db8dac20SDavid S. Miller err = EHOSTUNREACH; 750db8dac20SDavid S. Miller break; 751db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 752db8dac20SDavid S. Miller goto out; 753db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 754db8dac20SDavid S. Miller err = EPROTO; 755db8dac20SDavid S. Miller harderr = 1; 756db8dac20SDavid S. Miller break; 757db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 758db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 75936393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 760db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 761db8dac20SDavid S. Miller err = EMSGSIZE; 762db8dac20SDavid S. Miller harderr = 1; 763db8dac20SDavid S. Miller break; 764db8dac20SDavid S. Miller } 765db8dac20SDavid S. Miller goto out; 766db8dac20SDavid S. Miller } 767db8dac20SDavid S. Miller err = EHOSTUNREACH; 768db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 769db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 770db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 771db8dac20SDavid S. Miller } 772db8dac20SDavid S. Miller break; 77355be7a9cSDavid S. Miller case ICMP_REDIRECT: 77455be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 7751a462d18SDuan Jiong goto out; 776db8dac20SDavid S. Miller } 777db8dac20SDavid S. Miller 778db8dac20SDavid S. Miller /* 779db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 780db8dac20SDavid S. Miller * 4.1.3.3. 781db8dac20SDavid S. Miller */ 782a36e185eSStefano Brivio if (tunnel) { 783a36e185eSStefano Brivio /* ...not for tunnels though: we don't have a sending socket */ 784a36e185eSStefano Brivio goto out; 785a36e185eSStefano Brivio } 786db8dac20SDavid S. Miller if (!inet->recverr) { 787db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 788db8dac20SDavid S. Miller goto out; 789b1faf566SEric Dumazet } else 790db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 791b1faf566SEric Dumazet 792db8dac20SDavid S. Miller sk->sk_err = err; 793e3ae2365SAlexander Aring sk_error_report(sk); 794db8dac20SDavid S. Miller out: 79532bbd879SStefano Brivio return 0; 796db8dac20SDavid S. Miller } 797db8dac20SDavid S. Miller 79832bbd879SStefano Brivio int udp_err(struct sk_buff *skb, u32 info) 799db8dac20SDavid S. Miller { 80032bbd879SStefano Brivio return __udp4_lib_err(skb, info, &udp_table); 801db8dac20SDavid S. Miller } 802db8dac20SDavid S. Miller 803db8dac20SDavid S. Miller /* 804db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 805db8dac20SDavid S. Miller */ 80636d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 807db8dac20SDavid S. Miller { 808db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 809db8dac20SDavid S. Miller 810db8dac20SDavid S. Miller if (up->pending) { 811db8dac20SDavid S. Miller up->len = 0; 812db8dac20SDavid S. Miller up->pending = 0; 813db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 814db8dac20SDavid S. Miller } 815db8dac20SDavid S. Miller } 81636d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 817db8dac20SDavid S. Miller 818db8dac20SDavid S. Miller /** 819f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 820db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 821db8dac20SDavid S. Miller * (checksum field must be zeroed out) 822f6b9664fSHerbert Xu * @src: source IP address 823f6b9664fSHerbert Xu * @dst: destination IP address 824db8dac20SDavid S. Miller */ 825c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 826db8dac20SDavid S. Miller { 827db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 828f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 829f6b9664fSHerbert Xu int len = skb->len - offset; 830f6b9664fSHerbert Xu int hlen = len; 831db8dac20SDavid S. Miller __wsum csum = 0; 832db8dac20SDavid S. Miller 833ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 834db8dac20SDavid S. Miller /* 835db8dac20SDavid S. Miller * Only one fragment on the socket. 836db8dac20SDavid S. Miller */ 837db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 838db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 839f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 840f6b9664fSHerbert Xu IPPROTO_UDP, 0); 841db8dac20SDavid S. Miller } else { 842ebbe495fSWANG Cong struct sk_buff *frags; 843ebbe495fSWANG Cong 844db8dac20SDavid S. Miller /* 845db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 846db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 847db8dac20SDavid S. Miller * should be together 848db8dac20SDavid S. Miller */ 849ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 850f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 851f6b9664fSHerbert Xu hlen -= frags->len; 852ebbe495fSWANG Cong } 853db8dac20SDavid S. Miller 854f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 855db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 856db8dac20SDavid S. Miller 857db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 858db8dac20SDavid S. Miller if (uh->check == 0) 859db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 860db8dac20SDavid S. Miller } 861db8dac20SDavid S. Miller } 862c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 863db8dac20SDavid S. Miller 864af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 865af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 866af5fcba7STom Herbert */ 867af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 868af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 869af5fcba7STom Herbert { 870af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 871af5fcba7STom Herbert 872179bc67fSEdward Cree if (nocheck) { 873af5fcba7STom Herbert uh->check = 0; 874179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 875af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 876179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 877179bc67fSEdward Cree uh->check = 0; 878179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 879179bc67fSEdward Cree if (uh->check == 0) 880179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 881d75f1306SEdward Cree } else { 882af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 883af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 884af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 885af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 886af5fcba7STom Herbert } 887af5fcba7STom Herbert } 888af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 889af5fcba7STom Herbert 890bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 891bec1f6f6SWillem de Bruijn struct inet_cork *cork) 892f6b9664fSHerbert Xu { 893f6b9664fSHerbert Xu struct sock *sk = skb->sk; 894f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 895f6b9664fSHerbert Xu struct udphdr *uh; 896cffb8f61SMenglong Dong int err; 897f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 898f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 899f6b9664fSHerbert Xu int len = skb->len - offset; 9004094871dSJosh Hunt int datalen = len - sizeof(*uh); 901f6b9664fSHerbert Xu __wsum csum = 0; 902f6b9664fSHerbert Xu 903f6b9664fSHerbert Xu /* 904f6b9664fSHerbert Xu * Create a UDP header 905f6b9664fSHerbert Xu */ 906f6b9664fSHerbert Xu uh = udp_hdr(skb); 907f6b9664fSHerbert Xu uh->source = inet->inet_sport; 90879ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 909f6b9664fSHerbert Xu uh->len = htons(len); 910f6b9664fSHerbert Xu uh->check = 0; 911f6b9664fSHerbert Xu 912bec1f6f6SWillem de Bruijn if (cork->gso_size) { 913bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 914bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 915bec1f6f6SWillem de Bruijn 9160f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 9170f149c9fSWillem de Bruijn kfree_skb(skb); 918bec1f6f6SWillem de Bruijn return -EINVAL; 9190f149c9fSWillem de Bruijn } 920158390e4SJianguo Wu if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 9210f149c9fSWillem de Bruijn kfree_skb(skb); 922bec1f6f6SWillem de Bruijn return -EINVAL; 9230f149c9fSWillem de Bruijn } 9240f149c9fSWillem de Bruijn if (sk->sk_no_check_tx) { 9250f149c9fSWillem de Bruijn kfree_skb(skb); 926a8c744a8SWillem de Bruijn return -EINVAL; 9270f149c9fSWillem de Bruijn } 928ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 9290f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 9300f149c9fSWillem de Bruijn kfree_skb(skb); 931bec1f6f6SWillem de Bruijn return -EIO; 9320f149c9fSWillem de Bruijn } 933bec1f6f6SWillem de Bruijn 9344094871dSJosh Hunt if (datalen > cork->gso_size) { 935bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 936bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 9374094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 938dfec0ee2SAlexander Duyck cork->gso_size); 9394094871dSJosh Hunt } 940a8c744a8SWillem de Bruijn goto csum_partial; 941bec1f6f6SWillem de Bruijn } 942bec1f6f6SWillem de Bruijn 943f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 944f6b9664fSHerbert Xu csum = udplite_csum(skb); 945f6b9664fSHerbert Xu 946ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 947f6b9664fSHerbert Xu 948f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 949f6b9664fSHerbert Xu goto send; 950f6b9664fSHerbert Xu 951f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 952a8c744a8SWillem de Bruijn csum_partial: 953f6b9664fSHerbert Xu 95479ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 955f6b9664fSHerbert Xu goto send; 956f6b9664fSHerbert Xu 957f6b9664fSHerbert Xu } else 958f6b9664fSHerbert Xu csum = udp_csum(skb); 959f6b9664fSHerbert Xu 960f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 96179ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 962f6b9664fSHerbert Xu sk->sk_protocol, csum); 963f6b9664fSHerbert Xu if (uh->check == 0) 964f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 965f6b9664fSHerbert Xu 966f6b9664fSHerbert Xu send: 967b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 968f6b9664fSHerbert Xu if (err) { 969f6b9664fSHerbert Xu if (err == -ENOBUFS && !inet->recverr) { 9706aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 971f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 972f6b9664fSHerbert Xu err = 0; 973f6b9664fSHerbert Xu } 974f6b9664fSHerbert Xu } else 9756aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 976f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 977f6b9664fSHerbert Xu return err; 978f6b9664fSHerbert Xu } 979f6b9664fSHerbert Xu 980db8dac20SDavid S. Miller /* 981db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 982db8dac20SDavid S. Miller */ 9838822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 984db8dac20SDavid S. Miller { 985db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 986db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 987b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 988db8dac20SDavid S. Miller struct sk_buff *skb; 989db8dac20SDavid S. Miller int err = 0; 990db8dac20SDavid S. Miller 99177968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 992f6b9664fSHerbert Xu if (!skb) 993db8dac20SDavid S. Miller goto out; 994db8dac20SDavid S. Miller 995bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &inet->cork.base); 996db8dac20SDavid S. Miller 997db8dac20SDavid S. Miller out: 998db8dac20SDavid S. Miller up->len = 0; 999db8dac20SDavid S. Miller up->pending = 0; 1000db8dac20SDavid S. Miller return err; 1001db8dac20SDavid S. Miller } 10028822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 1003db8dac20SDavid S. Miller 10042e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 10052e8de857SWillem de Bruijn { 10062e8de857SWillem de Bruijn switch (cmsg->cmsg_type) { 10072e8de857SWillem de Bruijn case UDP_SEGMENT: 10082e8de857SWillem de Bruijn if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 10092e8de857SWillem de Bruijn return -EINVAL; 10102e8de857SWillem de Bruijn *gso_size = *(__u16 *)CMSG_DATA(cmsg); 10112e8de857SWillem de Bruijn return 0; 10122e8de857SWillem de Bruijn default: 10132e8de857SWillem de Bruijn return -EINVAL; 10142e8de857SWillem de Bruijn } 10152e8de857SWillem de Bruijn } 10162e8de857SWillem de Bruijn 10172e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 10182e8de857SWillem de Bruijn { 10192e8de857SWillem de Bruijn struct cmsghdr *cmsg; 10202e8de857SWillem de Bruijn bool need_ip = false; 10212e8de857SWillem de Bruijn int err; 10222e8de857SWillem de Bruijn 10232e8de857SWillem de Bruijn for_each_cmsghdr(cmsg, msg) { 10242e8de857SWillem de Bruijn if (!CMSG_OK(msg, cmsg)) 10252e8de857SWillem de Bruijn return -EINVAL; 10262e8de857SWillem de Bruijn 10272e8de857SWillem de Bruijn if (cmsg->cmsg_level != SOL_UDP) { 10282e8de857SWillem de Bruijn need_ip = true; 10292e8de857SWillem de Bruijn continue; 10302e8de857SWillem de Bruijn } 10312e8de857SWillem de Bruijn 10322e8de857SWillem de Bruijn err = __udp_cmsg_send(cmsg, gso_size); 10332e8de857SWillem de Bruijn if (err) 10342e8de857SWillem de Bruijn return err; 10352e8de857SWillem de Bruijn } 10362e8de857SWillem de Bruijn 10372e8de857SWillem de Bruijn return need_ip; 10382e8de857SWillem de Bruijn } 10392e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send); 10402e8de857SWillem de Bruijn 10411b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1042db8dac20SDavid S. Miller { 1043db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1044db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 10451cedee13SAndrey Ignatov DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 1046e474995fSDavid S. Miller struct flowi4 fl4_stack; 1047b6f21b26SDavid S. Miller struct flowi4 *fl4; 1048db8dac20SDavid S. Miller int ulen = len; 1049db8dac20SDavid S. Miller struct ipcm_cookie ipc; 1050db8dac20SDavid S. Miller struct rtable *rt = NULL; 1051db8dac20SDavid S. Miller int free = 0; 1052db8dac20SDavid S. Miller int connected = 0; 1053db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 1054db8dac20SDavid S. Miller __be16 dport; 1055db8dac20SDavid S. Miller u8 tos; 1056db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 1057a9f59707SEric Dumazet int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE; 1058db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1059903ab86dSHerbert Xu struct sk_buff *skb; 1060f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 1061db8dac20SDavid S. Miller 1062db8dac20SDavid S. Miller if (len > 0xFFFF) 1063db8dac20SDavid S. Miller return -EMSGSIZE; 1064db8dac20SDavid S. Miller 1065db8dac20SDavid S. Miller /* 1066db8dac20SDavid S. Miller * Check the flags. 1067db8dac20SDavid S. Miller */ 1068db8dac20SDavid S. Miller 1069db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 1070db8dac20SDavid S. Miller return -EOPNOTSUPP; 1071db8dac20SDavid S. Miller 1072903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1073903ab86dSHerbert Xu 1074f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1075db8dac20SDavid S. Miller if (up->pending) { 1076db8dac20SDavid S. Miller /* 1077db8dac20SDavid S. Miller * There are pending frames. 1078db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1079db8dac20SDavid S. Miller */ 1080db8dac20SDavid S. Miller lock_sock(sk); 1081db8dac20SDavid S. Miller if (likely(up->pending)) { 1082db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 1083db8dac20SDavid S. Miller release_sock(sk); 1084db8dac20SDavid S. Miller return -EINVAL; 1085db8dac20SDavid S. Miller } 1086db8dac20SDavid S. Miller goto do_append_data; 1087db8dac20SDavid S. Miller } 1088db8dac20SDavid S. Miller release_sock(sk); 1089db8dac20SDavid S. Miller } 1090db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1091db8dac20SDavid S. Miller 1092db8dac20SDavid S. Miller /* 1093db8dac20SDavid S. Miller * Get and verify the address. 1094db8dac20SDavid S. Miller */ 10951cedee13SAndrey Ignatov if (usin) { 1096db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 1097db8dac20SDavid S. Miller return -EINVAL; 1098db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 1099db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 1100db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1101db8dac20SDavid S. Miller } 1102db8dac20SDavid S. Miller 1103db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 1104db8dac20SDavid S. Miller dport = usin->sin_port; 1105db8dac20SDavid S. Miller if (dport == 0) 1106db8dac20SDavid S. Miller return -EINVAL; 1107db8dac20SDavid S. Miller } else { 1108db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1109db8dac20SDavid S. Miller return -EDESTADDRREQ; 1110c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1111c720c7e8SEric Dumazet dport = inet->inet_dport; 1112db8dac20SDavid S. Miller /* Open fast path for connected socket. 1113db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 1114db8dac20SDavid S. Miller */ 1115db8dac20SDavid S. Miller connected = 1; 1116db8dac20SDavid S. Miller } 1117c14ac945SSoheil Hassas Yeganeh 111835178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 111918a419baSEric Dumazet ipc.gso_size = READ_ONCE(up->gso_size); 1120bf84a010SDaniel Borkmann 1121db8dac20SDavid S. Miller if (msg->msg_controllen) { 11222e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc.gso_size); 11232e8de857SWillem de Bruijn if (err > 0) 11242e8de857SWillem de Bruijn err = ip_cmsg_send(sk, msg, &ipc, 11252e8de857SWillem de Bruijn sk->sk_family == AF_INET6); 11262e8de857SWillem de Bruijn if (unlikely(err < 0)) { 112791948309SEric Dumazet kfree(ipc.opt); 1128db8dac20SDavid S. Miller return err; 112991948309SEric Dumazet } 1130db8dac20SDavid S. Miller if (ipc.opt) 1131db8dac20SDavid S. Miller free = 1; 1132db8dac20SDavid S. Miller connected = 0; 1133db8dac20SDavid S. Miller } 1134f6d8bd05SEric Dumazet if (!ipc.opt) { 1135f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 1136f6d8bd05SEric Dumazet 1137f6d8bd05SEric Dumazet rcu_read_lock(); 1138f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1139f6d8bd05SEric Dumazet if (inet_opt) { 1140f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 1141f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 1142f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 1143f6d8bd05SEric Dumazet } 1144f6d8bd05SEric Dumazet rcu_read_unlock(); 1145f6d8bd05SEric Dumazet } 1146db8dac20SDavid S. Miller 11476fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP4_SENDMSG) && !connected) { 11481cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 11491cedee13SAndrey Ignatov (struct sockaddr *)usin, &ipc.addr); 11501cedee13SAndrey Ignatov if (err) 11511cedee13SAndrey Ignatov goto out_free; 11521cedee13SAndrey Ignatov if (usin) { 11531cedee13SAndrey Ignatov if (usin->sin_port == 0) { 11541cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 11551cedee13SAndrey Ignatov err = -EINVAL; 11561cedee13SAndrey Ignatov goto out_free; 11571cedee13SAndrey Ignatov } 11581cedee13SAndrey Ignatov daddr = usin->sin_addr.s_addr; 11591cedee13SAndrey Ignatov dport = usin->sin_port; 11601cedee13SAndrey Ignatov } 11611cedee13SAndrey Ignatov } 11621cedee13SAndrey Ignatov 1163db8dac20SDavid S. Miller saddr = ipc.addr; 1164db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 1165db8dac20SDavid S. Miller 1166f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 11671b97013bSAndrey Ignatov if (!daddr) { 11681b97013bSAndrey Ignatov err = -EINVAL; 11691b97013bSAndrey Ignatov goto out_free; 11701b97013bSAndrey Ignatov } 1171f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 1172db8dac20SDavid S. Miller connected = 0; 1173db8dac20SDavid S. Miller } 1174aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 1175db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 1176db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 1177f6d8bd05SEric Dumazet (ipc.opt && ipc.opt->opt.is_strictroute)) { 1178db8dac20SDavid S. Miller tos |= RTO_ONLINK; 1179db8dac20SDavid S. Miller connected = 0; 1180db8dac20SDavid S. Miller } 1181db8dac20SDavid S. Miller 1182db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 1183854da991SRobert Shearman if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 1184db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 1185db8dac20SDavid S. Miller if (!saddr) 1186db8dac20SDavid S. Miller saddr = inet->mc_addr; 1187db8dac20SDavid S. Miller connected = 0; 11889515a2e0SDavid Ahern } else if (!ipc.oif) { 118976e21053SErich E. Hoover ipc.oif = inet->uc_index; 11909515a2e0SDavid Ahern } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 11919515a2e0SDavid Ahern /* oif is set, packet is to local broadcast and 11922bdcc73cSRandy Dunlap * uc_index is set. oif is most likely set 11939515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 11949515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 11959515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 11969515a2e0SDavid Ahern */ 11979515a2e0SDavid Ahern if (ipc.oif != inet->uc_index && 11989515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 11999515a2e0SDavid Ahern inet->uc_index)) { 12009515a2e0SDavid Ahern ipc.oif = inet->uc_index; 12019515a2e0SDavid Ahern } 12029515a2e0SDavid Ahern } 1203db8dac20SDavid S. Miller 1204db8dac20SDavid S. Miller if (connected) 1205db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 1206db8dac20SDavid S. Miller 120751456b29SIan Morris if (!rt) { 120884a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 12099a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 121084a3aa00SPavel Emelyanov 1211e474995fSDavid S. Miller fl4 = &fl4_stack; 12129a24abfaSDavid Ahern 1213c6af0c22SWillem de Bruijn flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, 12149a24abfaSDavid Ahern RT_SCOPE_UNIVERSE, sk->sk_protocol, 12159a24abfaSDavid Ahern flow_flags, 1216e2d118a1SLorenzo Colitti faddr, saddr, dport, inet->inet_sport, 1217e2d118a1SLorenzo Colitti sk->sk_uid); 1218c0951cbcSDavid S. Miller 12193df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 1220e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1221b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1222b23dd4feSDavid S. Miller err = PTR_ERR(rt); 122306dc94b1SDavid S. Miller rt = NULL; 1224db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1225f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1226db8dac20SDavid S. Miller goto out; 1227db8dac20SDavid S. Miller } 1228db8dac20SDavid S. Miller 1229db8dac20SDavid S. Miller err = -EACCES; 1230db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1231db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1232db8dac20SDavid S. Miller goto out; 1233db8dac20SDavid S. Miller if (connected) 1234d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1235db8dac20SDavid S. Miller } 1236db8dac20SDavid S. Miller 1237db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1238db8dac20SDavid S. Miller goto do_confirm; 1239db8dac20SDavid S. Miller back_from_confirm: 1240db8dac20SDavid S. Miller 1241e474995fSDavid S. Miller saddr = fl4->saddr; 1242db8dac20SDavid S. Miller if (!ipc.addr) 1243e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1244db8dac20SDavid S. Miller 1245903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1246903ab86dSHerbert Xu if (!corkreq) { 12471cd7884dSWillem de Bruijn struct inet_cork cork; 12481cd7884dSWillem de Bruijn 1249f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1250903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 12511cd7884dSWillem de Bruijn &cork, msg->msg_flags); 1252903ab86dSHerbert Xu err = PTR_ERR(skb); 125350c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 1254bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &cork); 1255903ab86dSHerbert Xu goto out; 1256903ab86dSHerbert Xu } 1257903ab86dSHerbert Xu 1258db8dac20SDavid S. Miller lock_sock(sk); 1259db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1260db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1261db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1262db8dac20SDavid S. Miller release_sock(sk); 1263db8dac20SDavid S. Miller 1264197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1265db8dac20SDavid S. Miller err = -EINVAL; 1266db8dac20SDavid S. Miller goto out; 1267db8dac20SDavid S. Miller } 1268db8dac20SDavid S. Miller /* 1269db8dac20SDavid S. Miller * Now cork the socket to pend data. 1270db8dac20SDavid S. Miller */ 1271b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1272b6f21b26SDavid S. Miller fl4->daddr = daddr; 1273b6f21b26SDavid S. Miller fl4->saddr = saddr; 12749cce96dfSDavid S. Miller fl4->fl4_dport = dport; 12759cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1276db8dac20SDavid S. Miller up->pending = AF_INET; 1277db8dac20SDavid S. Miller 1278db8dac20SDavid S. Miller do_append_data: 1279db8dac20SDavid S. Miller up->len += ulen; 1280f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 12812e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1282db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1283db8dac20SDavid S. Miller if (err) 1284db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1285db8dac20SDavid S. Miller else if (!corkreq) 1286db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1287db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1288db8dac20SDavid S. Miller up->pending = 0; 1289db8dac20SDavid S. Miller release_sock(sk); 1290db8dac20SDavid S. Miller 1291db8dac20SDavid S. Miller out: 1292db8dac20SDavid S. Miller ip_rt_put(rt); 12931b97013bSAndrey Ignatov out_free: 1294db8dac20SDavid S. Miller if (free) 1295db8dac20SDavid S. Miller kfree(ipc.opt); 1296db8dac20SDavid S. Miller if (!err) 1297db8dac20SDavid S. Miller return len; 1298db8dac20SDavid S. Miller /* 1299db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1300db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1301db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1302db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1303db8dac20SDavid S. Miller * seems like overkill. 1304db8dac20SDavid S. Miller */ 1305db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 13066aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1307629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1308db8dac20SDavid S. Miller } 1309db8dac20SDavid S. Miller return err; 1310db8dac20SDavid S. Miller 1311db8dac20SDavid S. Miller do_confirm: 13120dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 13130dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1314db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1315db8dac20SDavid S. Miller goto back_from_confirm; 1316db8dac20SDavid S. Miller err = 0; 1317db8dac20SDavid S. Miller goto out; 1318db8dac20SDavid S. Miller } 1319c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1320db8dac20SDavid S. Miller 1321db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 1322db8dac20SDavid S. Miller size_t size, int flags) 1323db8dac20SDavid S. Miller { 1324f5fca608SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1325db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1326db8dac20SDavid S. Miller int ret; 1327db8dac20SDavid S. Miller 1328d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 1329d3f7d56aSShawn Landden flags |= MSG_MORE; 1330d3f7d56aSShawn Landden 1331db8dac20SDavid S. Miller if (!up->pending) { 1332db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1333db8dac20SDavid S. Miller 1334db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 1335db8dac20SDavid S. Miller * sendpage interface can't pass. 1336db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 1337db8dac20SDavid S. Miller */ 13381b784140SYing Xue ret = udp_sendmsg(sk, &msg, 0); 1339db8dac20SDavid S. Miller if (ret < 0) 1340db8dac20SDavid S. Miller return ret; 1341db8dac20SDavid S. Miller } 1342db8dac20SDavid S. Miller 1343db8dac20SDavid S. Miller lock_sock(sk); 1344db8dac20SDavid S. Miller 1345db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 1346db8dac20SDavid S. Miller release_sock(sk); 1347db8dac20SDavid S. Miller 1348197df02cSMatteo Croce net_dbg_ratelimited("cork failed\n"); 1349db8dac20SDavid S. Miller return -EINVAL; 1350db8dac20SDavid S. Miller } 1351db8dac20SDavid S. Miller 1352f5fca608SDavid S. Miller ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1353f5fca608SDavid S. Miller page, offset, size, flags); 1354db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 1355db8dac20SDavid S. Miller release_sock(sk); 1356db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 1357db8dac20SDavid S. Miller size, flags); 1358db8dac20SDavid S. Miller } 1359db8dac20SDavid S. Miller if (ret < 0) { 1360db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1361db8dac20SDavid S. Miller goto out; 1362db8dac20SDavid S. Miller } 1363db8dac20SDavid S. Miller 1364db8dac20SDavid S. Miller up->len += size; 1365a9f59707SEric Dumazet if (!(READ_ONCE(up->corkflag) || (flags&MSG_MORE))) 1366db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 1367db8dac20SDavid S. Miller if (!ret) 1368db8dac20SDavid S. Miller ret = size; 1369db8dac20SDavid S. Miller out: 1370db8dac20SDavid S. Miller release_sock(sk); 1371db8dac20SDavid S. Miller return ret; 1372db8dac20SDavid S. Miller } 1373db8dac20SDavid S. Miller 1374dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1375dce4551cSPaolo Abeni 1376677bf08cSFlorian Westphal /* all head states (dst, sk, nf conntrack) except skb extensions are 1377677bf08cSFlorian Westphal * cleared by udp_rcv(). 1378677bf08cSFlorian Westphal * 1379677bf08cSFlorian Westphal * We need to preserve secpath, if present, to eventually process 1380677bf08cSFlorian Westphal * IP_CMSG_PASSSEC at recvmsg() time. 1381677bf08cSFlorian Westphal * 1382677bf08cSFlorian Westphal * Other extensions can be cleared. 1383677bf08cSFlorian Westphal */ 1384677bf08cSFlorian Westphal static bool udp_try_make_stateless(struct sk_buff *skb) 1385677bf08cSFlorian Westphal { 1386677bf08cSFlorian Westphal if (!skb_has_extensions(skb)) 1387677bf08cSFlorian Westphal return true; 1388677bf08cSFlorian Westphal 1389677bf08cSFlorian Westphal if (!secpath_exists(skb)) { 1390677bf08cSFlorian Westphal skb_ext_reset(skb); 1391677bf08cSFlorian Westphal return true; 1392677bf08cSFlorian Westphal } 1393677bf08cSFlorian Westphal 1394677bf08cSFlorian Westphal return false; 1395677bf08cSFlorian Westphal } 1396677bf08cSFlorian Westphal 1397b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1398b65ac446SPaolo Abeni { 1399dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1400b65ac446SPaolo Abeni 1401b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1402dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1403dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1404b65ac446SPaolo Abeni scratch->len = skb->len; 1405b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1406b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1407b65ac446SPaolo Abeni #endif 1408677bf08cSFlorian Westphal if (udp_try_make_stateless(skb)) 1409dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1410dce4551cSPaolo Abeni } 1411dce4551cSPaolo Abeni 1412a793183cSEric Dumazet static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) 1413a793183cSEric Dumazet { 1414a793183cSEric Dumazet /* We come here after udp_lib_checksum_complete() returned 0. 1415a793183cSEric Dumazet * This means that __skb_checksum_complete() might have 1416a793183cSEric Dumazet * set skb->csum_valid to 1. 1417a793183cSEric Dumazet * On 64bit platforms, we can set csum_unnecessary 1418a793183cSEric Dumazet * to true, but only if the skb is not shared. 1419a793183cSEric Dumazet */ 1420a793183cSEric Dumazet #if BITS_PER_LONG == 64 1421a793183cSEric Dumazet if (!skb_shared(skb)) 1422a793183cSEric Dumazet udp_skb_scratch(skb)->csum_unnecessary = true; 1423a793183cSEric Dumazet #endif 1424a793183cSEric Dumazet } 1425a793183cSEric Dumazet 1426dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1427dce4551cSPaolo Abeni { 1428dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1429dce4551cSPaolo Abeni } 1430dce4551cSPaolo Abeni 1431dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1432dce4551cSPaolo Abeni { 1433dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1434dce4551cSPaolo Abeni } 1435b65ac446SPaolo Abeni 14367c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 14376dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 14386dfb4367SPaolo Abeni bool rx_queue_lock_held) 1439f970bd9eSPaolo Abeni { 14406b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 14412276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1442f970bd9eSPaolo Abeni int amt; 1443f970bd9eSPaolo Abeni 14446b229cf7SEric Dumazet if (likely(partial)) { 14456b229cf7SEric Dumazet up->forward_deficit += size; 14466b229cf7SEric Dumazet size = up->forward_deficit; 1447d39ca259SPaolo Abeni if (size < (sk->sk_rcvbuf >> 2) && 1448d39ca259SPaolo Abeni !skb_queue_empty(&up->reader_queue)) 14496b229cf7SEric Dumazet return; 14506b229cf7SEric Dumazet } else { 14516b229cf7SEric Dumazet size += up->forward_deficit; 14526b229cf7SEric Dumazet } 14536b229cf7SEric Dumazet up->forward_deficit = 0; 14546b229cf7SEric Dumazet 14556dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 14566dfb4367SPaolo Abeni * if the called don't held it already 14576dfb4367SPaolo Abeni */ 14582276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 14596dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14602276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 14612276f58aSPaolo Abeni 14626dfb4367SPaolo Abeni 1463f970bd9eSPaolo Abeni sk->sk_forward_alloc += size; 1464f970bd9eSPaolo Abeni amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); 1465f970bd9eSPaolo Abeni sk->sk_forward_alloc -= amt; 1466f970bd9eSPaolo Abeni 1467f970bd9eSPaolo Abeni if (amt) 1468f970bd9eSPaolo Abeni __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); 146902ab0d13SEric Dumazet 147002ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 14712276f58aSPaolo Abeni 14722276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 14732276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 14742276f58aSPaolo Abeni 14756dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14762276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1477f970bd9eSPaolo Abeni } 1478f970bd9eSPaolo Abeni 14792276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1480c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1481c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1482c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1483c84d9490SEric Dumazet */ 14847c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1485f970bd9eSPaolo Abeni { 1486b65ac446SPaolo Abeni prefetch(&skb->data); 1487b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1488f970bd9eSPaolo Abeni } 14897c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1490f970bd9eSPaolo Abeni 14916dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 149264f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 14936dfb4367SPaolo Abeni { 1494b65ac446SPaolo Abeni prefetch(&skb->data); 1495b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 14966dfb4367SPaolo Abeni } 14976dfb4367SPaolo Abeni 14984b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 14994b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 15004b272750SEric Dumazet * Under flood, this means that only one producer can be in line 15014b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 15024b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 15034b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 15044b272750SEric Dumazet */ 15054b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 15064b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 15074b272750SEric Dumazet 15084b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 15094b272750SEric Dumazet { 15104b272750SEric Dumazet spinlock_t *busy; 15114b272750SEric Dumazet 15124b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 15134b272750SEric Dumazet spin_lock(busy); 15144b272750SEric Dumazet return busy; 15154b272750SEric Dumazet } 15164b272750SEric Dumazet 15174b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 15184b272750SEric Dumazet { 15194b272750SEric Dumazet if (busy) 15204b272750SEric Dumazet spin_unlock(busy); 15214b272750SEric Dumazet } 15224b272750SEric Dumazet 1523f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1524f970bd9eSPaolo Abeni { 1525f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1526f970bd9eSPaolo Abeni int rmem, delta, amt, err = -ENOMEM; 15274b272750SEric Dumazet spinlock_t *busy = NULL; 1528c8c8b127SEric Dumazet int size; 1529f970bd9eSPaolo Abeni 1530f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1531f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1532f970bd9eSPaolo Abeni */ 1533f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1534363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1535f970bd9eSPaolo Abeni goto drop; 1536f970bd9eSPaolo Abeni 1537c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1538c8c8b127SEric Dumazet * having linear skbs : 1539c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1540c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1541c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1542c8c8b127SEric Dumazet */ 15434b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1544c8c8b127SEric Dumazet skb_condense(skb); 15454b272750SEric Dumazet 15464b272750SEric Dumazet busy = busylock_acquire(sk); 15474b272750SEric Dumazet } 1548c8c8b127SEric Dumazet size = skb->truesize; 1549b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1550c8c8b127SEric Dumazet 1551f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1552f970bd9eSPaolo Abeni * queue contains some other skb 1553f970bd9eSPaolo Abeni */ 1554f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1555feed8a4fSAntonio Messina if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) 1556f970bd9eSPaolo Abeni goto uncharge_drop; 1557f970bd9eSPaolo Abeni 1558f970bd9eSPaolo Abeni spin_lock(&list->lock); 1559f970bd9eSPaolo Abeni if (size >= sk->sk_forward_alloc) { 1560f970bd9eSPaolo Abeni amt = sk_mem_pages(size); 1561f970bd9eSPaolo Abeni delta = amt << SK_MEM_QUANTUM_SHIFT; 1562f970bd9eSPaolo Abeni if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1563f970bd9eSPaolo Abeni err = -ENOBUFS; 1564f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1565f970bd9eSPaolo Abeni goto uncharge_drop; 1566f970bd9eSPaolo Abeni } 1567f970bd9eSPaolo Abeni 1568f970bd9eSPaolo Abeni sk->sk_forward_alloc += delta; 1569f970bd9eSPaolo Abeni } 1570f970bd9eSPaolo Abeni 1571f970bd9eSPaolo Abeni sk->sk_forward_alloc -= size; 1572f970bd9eSPaolo Abeni 15737c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 15747c13f97fSPaolo Abeni * forward allocated memory on dequeue 15757c13f97fSPaolo Abeni */ 1576f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1577f970bd9eSPaolo Abeni 1578f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1579f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1580f970bd9eSPaolo Abeni 1581f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 1582f970bd9eSPaolo Abeni sk->sk_data_ready(sk); 1583f970bd9eSPaolo Abeni 15844b272750SEric Dumazet busylock_release(busy); 1585f970bd9eSPaolo Abeni return 0; 1586f970bd9eSPaolo Abeni 1587f970bd9eSPaolo Abeni uncharge_drop: 1588f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1589f970bd9eSPaolo Abeni 1590f970bd9eSPaolo Abeni drop: 1591f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 15924b272750SEric Dumazet busylock_release(busy); 1593f970bd9eSPaolo Abeni return err; 1594f970bd9eSPaolo Abeni } 1595f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1596f970bd9eSPaolo Abeni 1597c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk) 1598f970bd9eSPaolo Abeni { 1599f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 16002276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 16017c13f97fSPaolo Abeni unsigned int total = 0; 16027c13f97fSPaolo Abeni struct sk_buff *skb; 16037c13f97fSPaolo Abeni 16042276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 16052276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 16067c13f97fSPaolo Abeni total += skb->truesize; 16077c13f97fSPaolo Abeni kfree_skb(skb); 16087c13f97fSPaolo Abeni } 16096dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 16107c13f97fSPaolo Abeni 1611f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1612f970bd9eSPaolo Abeni } 1613c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock); 1614f970bd9eSPaolo Abeni 1615f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1616f970bd9eSPaolo Abeni { 16172276f58aSPaolo Abeni skb_queue_head_init(&udp_sk(sk)->reader_queue); 1618f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1619f970bd9eSPaolo Abeni return 0; 1620f970bd9eSPaolo Abeni } 1621f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock); 1622f970bd9eSPaolo Abeni 1623f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1624f970bd9eSPaolo Abeni { 1625f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1626f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1627f970bd9eSPaolo Abeni 1628f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1629f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1630f970bd9eSPaolo Abeni } 16310a463c78SPaolo Abeni 1632ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1633ca2c1418SPaolo Abeni return; 1634ca2c1418SPaolo Abeni 1635dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1636dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 16370ddf3fb2SPaolo Abeni */ 1638dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 16390ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1640ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1641f970bd9eSPaolo Abeni } 1642f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1643f970bd9eSPaolo Abeni 16442276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 16452276f58aSPaolo Abeni struct sk_buff_head *rcvq, 16462276f58aSPaolo Abeni int *total) 16472276f58aSPaolo Abeni { 16482276f58aSPaolo Abeni struct sk_buff *skb; 16492276f58aSPaolo Abeni 16509bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 16519bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 16522276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 16532276f58aSPaolo Abeni IS_UDPLITE(sk)); 16542276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 16552276f58aSPaolo Abeni IS_UDPLITE(sk)); 16562276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 16572276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 16582276f58aSPaolo Abeni *total += skb->truesize; 16592276f58aSPaolo Abeni kfree_skb(skb); 16609bd780f5SPaolo Abeni } else { 1661a793183cSEric Dumazet udp_skb_csum_unnecessary_set(skb); 16629bd780f5SPaolo Abeni break; 16639bd780f5SPaolo Abeni } 16642276f58aSPaolo Abeni } 16652276f58aSPaolo Abeni return skb; 16662276f58aSPaolo Abeni } 16672276f58aSPaolo Abeni 166885584672SEric Dumazet /** 166985584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 167085584672SEric Dumazet * @sk: socket 167185584672SEric Dumazet * 167285584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1673e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 167485584672SEric Dumazet */ 1675e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 167685584672SEric Dumazet { 16772276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 16782276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 167985584672SEric Dumazet struct sk_buff *skb; 16807c13f97fSPaolo Abeni int total = 0; 1681e83c6744SEric Dumazet int res; 168285584672SEric Dumazet 168385584672SEric Dumazet spin_lock_bh(&rcvq->lock); 16842276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 1685137a0dbeSEric Dumazet if (!skb && !skb_queue_empty_lockless(sk_queue)) { 16862276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 16872276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 16882276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 16892276f58aSPaolo Abeni 16902276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 169185584672SEric Dumazet } 1692e83c6744SEric Dumazet res = skb ? skb->len : -1; 16937c13f97fSPaolo Abeni if (total) 16946dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 169585584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 169685584672SEric Dumazet return res; 169785584672SEric Dumazet } 169885584672SEric Dumazet 16991da177e4SLinus Torvalds /* 17001da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 17011da177e4SLinus Torvalds */ 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 17041da177e4SLinus Torvalds { 17056516c655SStephen Hemminger switch (cmd) { 17061da177e4SLinus Torvalds case SIOCOUTQ: 17071da177e4SLinus Torvalds { 170831e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 170931e6d363SEric Dumazet 17101da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds case SIOCINQ: 17141da177e4SLinus Torvalds { 1715e83c6744SEric Dumazet int amount = max_t(int, 0, first_packet_length(sk)); 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds default: 17211da177e4SLinus Torvalds return -ENOIOCTLCMD; 17221da177e4SLinus Torvalds } 17236516c655SStephen Hemminger 17246516c655SStephen Hemminger return 0; 17251da177e4SLinus Torvalds } 1726c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 17271da177e4SLinus Torvalds 17282276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1729ec095263SOliver Hartkopp int *off, int *err) 17302276f58aSPaolo Abeni { 17312276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 17322276f58aSPaolo Abeni struct sk_buff_head *queue; 17332276f58aSPaolo Abeni struct sk_buff *last; 17342276f58aSPaolo Abeni long timeo; 17352276f58aSPaolo Abeni int error; 17362276f58aSPaolo Abeni 17372276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 17382276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 17392276f58aSPaolo Abeni do { 17402276f58aSPaolo Abeni struct sk_buff *skb; 17412276f58aSPaolo Abeni 17422276f58aSPaolo Abeni error = sock_error(sk); 17432276f58aSPaolo Abeni if (error) 17442276f58aSPaolo Abeni break; 17452276f58aSPaolo Abeni 17462276f58aSPaolo Abeni error = -EAGAIN; 17472276f58aSPaolo Abeni do { 17482276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 1749e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1750e427cad6SPaolo Abeni err, &last); 17512276f58aSPaolo Abeni if (skb) { 1752e427cad6SPaolo Abeni if (!(flags & MSG_PEEK)) 1753e427cad6SPaolo Abeni udp_skb_destructor(sk, skb); 17542276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17552276f58aSPaolo Abeni return skb; 17562276f58aSPaolo Abeni } 17572276f58aSPaolo Abeni 1758137a0dbeSEric Dumazet if (skb_queue_empty_lockless(sk_queue)) { 17592276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17602276f58aSPaolo Abeni goto busy_check; 17612276f58aSPaolo Abeni } 17622276f58aSPaolo Abeni 17636dfb4367SPaolo Abeni /* refill the reader queue and walk it again 17646dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 17656dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 17666dfb4367SPaolo Abeni * is needed. 17676dfb4367SPaolo Abeni */ 17682276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 17692276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 17702276f58aSPaolo Abeni 1771e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1772e427cad6SPaolo Abeni err, &last); 1773e427cad6SPaolo Abeni if (skb && !(flags & MSG_PEEK)) 1774e427cad6SPaolo Abeni udp_skb_dtor_locked(sk, skb); 17756dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 17762276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1777de321ed3SAndrey Vagin if (skb) 17782276f58aSPaolo Abeni return skb; 17792276f58aSPaolo Abeni 17802276f58aSPaolo Abeni busy_check: 17812276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 17822276f58aSPaolo Abeni break; 17832276f58aSPaolo Abeni 17842276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 1785137a0dbeSEric Dumazet } while (!skb_queue_empty_lockless(sk_queue)); 17862276f58aSPaolo Abeni 17872276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 17882276f58aSPaolo Abeni } while (timeo && 1789b50b0580SSabrina Dubroca !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, 1790b50b0580SSabrina Dubroca &error, &timeo, 17912276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 17922276f58aSPaolo Abeni 17932276f58aSPaolo Abeni *err = error; 17942276f58aSPaolo Abeni return NULL; 17952276f58aSPaolo Abeni } 17967e823644SJiri Kosina EXPORT_SYMBOL(__skb_recv_udp); 17972276f58aSPaolo Abeni 1798d7f57118SCong Wang int udp_read_sock(struct sock *sk, read_descriptor_t *desc, 1799d7f57118SCong Wang sk_read_actor_t recv_actor) 1800d7f57118SCong Wang { 1801d7f57118SCong Wang int copied = 0; 1802d7f57118SCong Wang 1803d7f57118SCong Wang while (1) { 1804d7f57118SCong Wang struct sk_buff *skb; 1805d7f57118SCong Wang int err, used; 1806d7f57118SCong Wang 1807ec095263SOliver Hartkopp skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 1808d7f57118SCong Wang if (!skb) 1809d7f57118SCong Wang return err; 1810099f896fSCong Wang 1811099f896fSCong Wang if (udp_lib_checksum_complete(skb)) { 1812099f896fSCong Wang __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 1813099f896fSCong Wang IS_UDPLITE(sk)); 1814099f896fSCong Wang __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 1815099f896fSCong Wang IS_UDPLITE(sk)); 1816099f896fSCong Wang atomic_inc(&sk->sk_drops); 1817099f896fSCong Wang kfree_skb(skb); 1818099f896fSCong Wang continue; 1819099f896fSCong Wang } 1820099f896fSCong Wang 1821d7f57118SCong Wang used = recv_actor(desc, skb, 0, skb->len); 1822d7f57118SCong Wang if (used <= 0) { 1823d7f57118SCong Wang if (!copied) 1824d7f57118SCong Wang copied = used; 1825e00a5c33SCong Wang kfree_skb(skb); 1826d7f57118SCong Wang break; 1827d7f57118SCong Wang } else if (used <= skb->len) { 1828d7f57118SCong Wang copied += used; 1829d7f57118SCong Wang } 1830d7f57118SCong Wang 1831e00a5c33SCong Wang kfree_skb(skb); 1832d7f57118SCong Wang if (!desc->count) 1833d7f57118SCong Wang break; 1834d7f57118SCong Wang } 1835d7f57118SCong Wang 1836d7f57118SCong Wang return copied; 1837d7f57118SCong Wang } 1838d7f57118SCong Wang EXPORT_SYMBOL(udp_read_sock); 1839d7f57118SCong Wang 1840db8dac20SDavid S. Miller /* 1841db8dac20SDavid S. Miller * This should be easy, if there is something there we 1842db8dac20SDavid S. Miller * return it, otherwise we block. 1843db8dac20SDavid S. Miller */ 1844db8dac20SDavid S. Miller 1845ec095263SOliver Hartkopp int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags, 1846ec095263SOliver Hartkopp int *addr_len) 1847db8dac20SDavid S. Miller { 1848db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1849342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1850db8dac20SDavid S. Miller struct sk_buff *skb; 185159c2cdaeSDavid S. Miller unsigned int ulen, copied; 1852fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 1853db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1854197c949eSEric Dumazet bool checksum_valid = false; 1855db8dac20SDavid S. Miller 1856db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 185785fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1858db8dac20SDavid S. Miller 1859db8dac20SDavid S. Miller try_again: 1860a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 1861ec095263SOliver Hartkopp skb = __skb_recv_udp(sk, flags, &off, &err); 1862db8dac20SDavid S. Miller if (!skb) 1863627d2d6bSsamanthakumar return err; 1864db8dac20SDavid S. Miller 1865b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 186659c2cdaeSDavid S. Miller copied = len; 1867627d2d6bSsamanthakumar if (copied > ulen - off) 1868627d2d6bSsamanthakumar copied = ulen - off; 186959c2cdaeSDavid S. Miller else if (copied < ulen) 1870db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1871db8dac20SDavid S. Miller 1872db8dac20SDavid S. Miller /* 1873db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1874db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1875db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1876db8dac20SDavid S. Miller */ 1877db8dac20SDavid S. Miller 1878d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1879d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1880b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1881b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1882197c949eSEric Dumazet if (!checksum_valid) 1883db8dac20SDavid S. Miller goto csum_copy_err; 1884db8dac20SDavid S. Miller } 1885db8dac20SDavid S. Miller 1886b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1887b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1888b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1889b65ac446SPaolo Abeni else 1890627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1891b65ac446SPaolo Abeni } else { 1892627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1893db8dac20SDavid S. Miller 1894db8dac20SDavid S. Miller if (err == -EINVAL) 1895db8dac20SDavid S. Miller goto csum_copy_err; 1896db8dac20SDavid S. Miller } 1897db8dac20SDavid S. Miller 189822911fc5SEric Dumazet if (unlikely(err)) { 1899fd69c399SPaolo Abeni if (!peeking) { 1900979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 19016aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1902979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1903979402b1SEric Dumazet } 1904850cbaddSPaolo Abeni kfree_skb(skb); 1905627d2d6bSsamanthakumar return err; 190622911fc5SEric Dumazet } 1907db8dac20SDavid S. Miller 1908fd69c399SPaolo Abeni if (!peeking) 19096aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1910629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1911db8dac20SDavid S. Miller 1912*6fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 1913db8dac20SDavid S. Miller 1914db8dac20SDavid S. Miller /* Copy the address. */ 1915c482c568SEric Dumazet if (sin) { 1916db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1917db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1918db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1919db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1920bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1921983695faSDaniel Borkmann 1922983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, 1923983695faSDaniel Borkmann (struct sockaddr *)sin); 1924db8dac20SDavid S. Miller } 1925bcd1665eSPaolo Abeni 1926bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 1927bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 1928bcd1665eSPaolo Abeni 1929db8dac20SDavid S. Miller if (inet->cmsg_flags) 1930ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1931db8dac20SDavid S. Miller 193259c2cdaeSDavid S. Miller err = copied; 1933db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1934db8dac20SDavid S. Miller err = ulen; 1935db8dac20SDavid S. Miller 1936850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1937db8dac20SDavid S. Miller return err; 1938db8dac20SDavid S. Miller 1939db8dac20SDavid S. Miller csum_copy_err: 19402276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 19412276f58aSPaolo Abeni udp_skb_destructor)) { 19426aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 19436aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 19446a5dc9e5SEric Dumazet } 1945850cbaddSPaolo Abeni kfree_skb(skb); 1946db8dac20SDavid S. Miller 1947beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1948beb39db5SEric Dumazet cond_resched(); 19499cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1950db8dac20SDavid S. Miller goto try_again; 1951db8dac20SDavid S. Miller } 1952db8dac20SDavid S. Miller 1953d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1954d74bad4eSAndrey Ignatov { 1955d74bad4eSAndrey Ignatov /* This check is replicated from __ip4_datagram_connect() and 1956d74bad4eSAndrey Ignatov * intended to prevent BPF program called below from accessing bytes 1957d74bad4eSAndrey Ignatov * that are out of the bound specified by user in addr_len. 1958d74bad4eSAndrey Ignatov */ 1959d74bad4eSAndrey Ignatov if (addr_len < sizeof(struct sockaddr_in)) 1960d74bad4eSAndrey Ignatov return -EINVAL; 1961d74bad4eSAndrey Ignatov 1962d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); 1963d74bad4eSAndrey Ignatov } 1964d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect); 1965d74bad4eSAndrey Ignatov 1966286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 19671da177e4SLinus Torvalds { 19681da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19691da177e4SLinus Torvalds /* 19701da177e4SLinus Torvalds * 1003.1g - break association. 19711da177e4SLinus Torvalds */ 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1974c720c7e8SEric Dumazet inet->inet_daddr = 0; 1975c720c7e8SEric Dumazet inet->inet_dport = 0; 1976bdeab991STom Herbert sock_rps_reset_rxhash(sk); 19771da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 1978303d0403SWillem de Bruijn if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { 19791da177e4SLinus Torvalds inet_reset_saddr(sk); 1980303d0403SWillem de Bruijn if (sk->sk_prot->rehash && 1981303d0403SWillem de Bruijn (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 1982303d0403SWillem de Bruijn sk->sk_prot->rehash(sk); 1983303d0403SWillem de Bruijn } 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 19861da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1987c720c7e8SEric Dumazet inet->inet_sport = 0; 19881da177e4SLinus Torvalds } 19891da177e4SLinus Torvalds sk_dst_reset(sk); 19901da177e4SLinus Torvalds return 0; 19911da177e4SLinus Torvalds } 1992286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1993286c72deSEric Dumazet 1994286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1995286c72deSEric Dumazet { 1996286c72deSEric Dumazet lock_sock(sk); 1997286c72deSEric Dumazet __udp_disconnect(sk, flags); 1998286c72deSEric Dumazet release_sock(sk); 1999286c72deSEric Dumazet return 0; 2000286c72deSEric Dumazet } 2001c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 20021da177e4SLinus Torvalds 2003645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 2004645ca708SEric Dumazet { 2005723b4610SEric Dumazet if (sk_hashed(sk)) { 2006645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 2007512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 2008512615b6SEric Dumazet 2009512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 2010d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 2011512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 2012645ca708SEric Dumazet 2013c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 2014e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 2015e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 2016ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 2017fdcc8aa9SEric Dumazet hslot->count--; 2018c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 2019645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 2020512615b6SEric Dumazet 2021512615b6SEric Dumazet spin_lock(&hslot2->lock); 2022ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2023512615b6SEric Dumazet hslot2->count--; 2024512615b6SEric Dumazet spin_unlock(&hslot2->lock); 2025645ca708SEric Dumazet } 2026c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 2027645ca708SEric Dumazet } 2028723b4610SEric Dumazet } 2029645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 2030645ca708SEric Dumazet 2031719f8358SEric Dumazet /* 2032719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 2033719f8358SEric Dumazet */ 2034719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 2035719f8358SEric Dumazet { 2036719f8358SEric Dumazet if (sk_hashed(sk)) { 2037719f8358SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 2038719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 2039719f8358SEric Dumazet 2040719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 2041719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 2042719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 2043e32ea7e7SCraig Gallek 2044e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 2045e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 2046719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 2047719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 2048719f8358SEric Dumazet /* we must lock primary chain too */ 2049719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 2050e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 2051e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 2052719f8358SEric Dumazet 2053e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 2054719f8358SEric Dumazet spin_lock(&hslot2->lock); 2055ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 2056719f8358SEric Dumazet hslot2->count--; 2057719f8358SEric Dumazet spin_unlock(&hslot2->lock); 2058719f8358SEric Dumazet 2059719f8358SEric Dumazet spin_lock(&nhslot2->lock); 2060ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 2061719f8358SEric Dumazet &nhslot2->head); 2062719f8358SEric Dumazet nhslot2->count++; 2063719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 2064e32ea7e7SCraig Gallek } 2065719f8358SEric Dumazet 2066719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 2067719f8358SEric Dumazet } 2068719f8358SEric Dumazet } 2069719f8358SEric Dumazet } 2070719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 2071719f8358SEric Dumazet 20728f6b5392SAlexey Kodanev void udp_v4_rehash(struct sock *sk) 2073719f8358SEric Dumazet { 2074f0b1e64cSMartin KaFai Lau u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 2075719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 2076719f8358SEric Dumazet inet_sk(sk)->inet_num); 2077719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 2078719f8358SEric Dumazet } 2079719f8358SEric Dumazet 2080a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 208193821778SHerbert Xu { 2082fec5e652STom Herbert int rc; 208393821778SHerbert Xu 2084005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 2085bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 2086005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 20872c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 2088e68b6e50SEric Dumazet } else { 2089e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 2090005ec974SShawn Bohrer } 2091fec5e652STom Herbert 2092850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 2093766e9037SEric Dumazet if (rc < 0) { 2094766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 209508d4c037SMenglong Dong int drop_reason; 2096766e9037SEric Dumazet 209793821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 209808d4c037SMenglong Dong if (rc == -ENOMEM) { 2099e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 210093821778SHerbert Xu is_udplite); 210108d4c037SMenglong Dong drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF; 210208d4c037SMenglong Dong } else { 2103a3ce2b10SMenglong Dong UDP_INC_STATS(sock_net(sk), UDP_MIB_MEMERRORS, 2104a3ce2b10SMenglong Dong is_udplite); 210508d4c037SMenglong Dong drop_reason = SKB_DROP_REASON_PROTO_MEM; 210608d4c037SMenglong Dong } 2107e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 210808d4c037SMenglong Dong kfree_skb_reason(skb, drop_reason); 2109296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 2110766e9037SEric Dumazet return -1; 211193821778SHerbert Xu } 211293821778SHerbert Xu 211393821778SHerbert Xu return 0; 211493821778SHerbert Xu } 211593821778SHerbert Xu 2116db8dac20SDavid S. Miller /* returns: 2117db8dac20SDavid S. Miller * -1: error 2118db8dac20SDavid S. Miller * 0: success 2119db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 2120db8dac20SDavid S. Miller * 2121db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 2122db8dac20SDavid S. Miller * have either been requeued or freed. 2123db8dac20SDavid S. Miller */ 2124cf329aa4SPaolo Abeni static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 2125db8dac20SDavid S. Miller { 21261379a92dSMenglong Dong int drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2127db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 2128db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 2129db8dac20SDavid S. Miller 2130db8dac20SDavid S. Miller /* 2131db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 2132db8dac20SDavid S. Miller */ 21331379a92dSMenglong Dong if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 21341379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_XFRM_POLICY; 2135db8dac20SDavid S. Miller goto drop; 21361379a92dSMenglong Dong } 2137895b5c9fSFlorian Westphal nf_reset_ct(skb); 2138db8dac20SDavid S. Miller 213988ab3108SDavidlohr Bueso if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 21400ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 21410ad92ad0SEric Dumazet 2142db8dac20SDavid S. Miller /* 2143db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 2144db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 2145db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 2146db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 2147db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 2148db8dac20SDavid S. Miller * handler or was discarded by it. 2149db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 2150db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 2151db8dac20SDavid S. Miller */ 2152db8dac20SDavid S. Miller 2153db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 21546aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 2155e5aed006SHannes Frederic Sowa if (encap_rcv) { 2156db8dac20SDavid S. Miller int ret; 2157db8dac20SDavid S. Miller 21580a80966bSTom Herbert /* Verify checksum before giving to encap */ 21590a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 21600a80966bSTom Herbert goto csum_error; 21610a80966bSTom Herbert 21620ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 2163db8dac20SDavid S. Miller if (ret <= 0) { 216402c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 21650283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 2166db8dac20SDavid S. Miller is_udplite); 2167db8dac20SDavid S. Miller return -ret; 2168db8dac20SDavid S. Miller } 2169db8dac20SDavid S. Miller } 2170db8dac20SDavid S. Miller 2171db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 2172db8dac20SDavid S. Miller } 2173db8dac20SDavid S. Miller 2174db8dac20SDavid S. Miller /* 2175db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 2176db8dac20SDavid S. Miller */ 2177b0a42277SMiaohe Lin if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 2178db8dac20SDavid S. Miller 2179db8dac20SDavid S. Miller /* 2180db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 2181db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 2182db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 2183db8dac20SDavid S. Miller * protocol stack as such. 2184db8dac20SDavid S. Miller * 2185db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 2186db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 2187db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 2188db8dac20SDavid S. Miller * provided by the application." 2189db8dac20SDavid S. Miller */ 2190db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 2191ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 2192db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 2193db8dac20SDavid S. Miller goto drop; 2194db8dac20SDavid S. Miller } 2195db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 2196db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 2197db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 2198db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 2199db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 2200db8dac20SDavid S. Miller */ 2201db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 2202ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 2203db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 2204db8dac20SDavid S. Miller goto drop; 2205db8dac20SDavid S. Miller } 2206db8dac20SDavid S. Miller } 2207db8dac20SDavid S. Miller 2208dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 2209ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 2210ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 22116a5dc9e5SEric Dumazet goto csum_error; 2212ce25d66aSEric Dumazet 22131379a92dSMenglong Dong if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) { 22141379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 2215a6127697SMichal Kubeček goto drop; 22161379a92dSMenglong Dong } 2217db8dac20SDavid S. Miller 2218e6afc8acSsamanthakumar udp_csum_pull_header(skb); 2219db8dac20SDavid S. Miller 2220fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 2221850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 2222db8dac20SDavid S. Miller 22236a5dc9e5SEric Dumazet csum_error: 22241379a92dSMenglong Dong drop_reason = SKB_DROP_REASON_UDP_CSUM; 222502c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2226db8dac20SDavid S. Miller drop: 222702c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 22288edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 22291379a92dSMenglong Dong kfree_skb_reason(skb, drop_reason); 2230db8dac20SDavid S. Miller return -1; 2231db8dac20SDavid S. Miller } 2232db8dac20SDavid S. Miller 2233cf329aa4SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2234cf329aa4SPaolo Abeni { 2235cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 2236cf329aa4SPaolo Abeni int ret; 2237cf329aa4SPaolo Abeni 2238cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 2239cf329aa4SPaolo Abeni return udp_queue_rcv_one_skb(sk, skb); 2240cf329aa4SPaolo Abeni 2241a08e7fd9SCambda Zhu BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); 2242cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 2243cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, true); 22441a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 2245cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 2246000ac44dSPaolo Abeni 2247000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb); 2248cf329aa4SPaolo Abeni ret = udp_queue_rcv_one_skb(sk, skb); 2249cf329aa4SPaolo Abeni if (ret > 0) 225010c678bdSXin Long ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret); 2251cf329aa4SPaolo Abeni } 2252cf329aa4SPaolo Abeni return 0; 2253cf329aa4SPaolo Abeni } 2254cf329aa4SPaolo Abeni 225597502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 2256e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 225797502231SEric Dumazet */ 225864f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2259421b3885SShawn Bohrer { 226097502231SEric Dumazet struct dst_entry *old; 2261421b3885SShawn Bohrer 2262d24406c8SWei Wang if (dst_hold_safe(dst)) { 22638f905c0eSEric Dumazet old = xchg((__force struct dst_entry **)&sk->sk_rx_dst, dst); 226497502231SEric Dumazet dst_release(old); 226564f0f5d1SPaolo Abeni return old != dst; 226697502231SEric Dumazet } 226764f0f5d1SPaolo Abeni return false; 2268d24406c8SWei Wang } 2269c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 2270421b3885SShawn Bohrer 2271db8dac20SDavid S. Miller /* 2272db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 2273db8dac20SDavid S. Miller * 22741240d137SEric Dumazet * Note: called only from the BH handler context. 2275db8dac20SDavid S. Miller */ 2276e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2277db8dac20SDavid S. Miller struct udphdr *uh, 2278db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 227936cbb245SRick Jones struct udp_table *udptable, 228036cbb245SRick Jones int proto) 2281db8dac20SDavid S. Miller { 2282ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 22835cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 22845cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 22852dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2286ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 2287ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 2288fb74c277SDavid Ahern int sdif = inet_sdif(skb); 2289ca065d0cSEric Dumazet struct hlist_node *node; 2290ca065d0cSEric Dumazet struct sk_buff *nskb; 22912dc41cffSDavid Held 22922dc41cffSDavid Held if (use_hash2) { 2293f0b1e64cSMartin KaFai Lau hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 229473e2d5e3SPablo Neira udptable->mask; 2295f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 22962dc41cffSDavid Held start_lookup: 229773e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 22982dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 22992dc41cffSDavid Held } 2300db8dac20SDavid S. Miller 2301ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2302ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2303fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 2304ca065d0cSEric Dumazet continue; 23051240d137SEric Dumazet 2306ca065d0cSEric Dumazet if (!first) { 2307ca065d0cSEric Dumazet first = sk; 2308ca065d0cSEric Dumazet continue; 2309ca065d0cSEric Dumazet } 2310ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 2311ca065d0cSEric Dumazet 2312ca065d0cSEric Dumazet if (unlikely(!nskb)) { 2313ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 231402c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2315ca065d0cSEric Dumazet IS_UDPLITE(sk)); 231602c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2317ca065d0cSEric Dumazet IS_UDPLITE(sk)); 2318ca065d0cSEric Dumazet continue; 2319ca065d0cSEric Dumazet } 2320ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 2321ca065d0cSEric Dumazet consume_skb(nskb); 2322ca065d0cSEric Dumazet } 23231240d137SEric Dumazet 23242dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 23252dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 23262dc41cffSDavid Held hash2 = hash2_any; 23272dc41cffSDavid Held goto start_lookup; 23282dc41cffSDavid Held } 23292dc41cffSDavid Held 2330ca065d0cSEric Dumazet if (first) { 2331ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 2332ca065d0cSEric Dumazet consume_skb(skb); 23331240d137SEric Dumazet } else { 2334ca065d0cSEric Dumazet kfree_skb(skb); 233502c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 233636cbb245SRick Jones proto == IPPROTO_UDPLITE); 23371240d137SEric Dumazet } 2338db8dac20SDavid S. Miller return 0; 2339db8dac20SDavid S. Miller } 2340db8dac20SDavid S. Miller 2341db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2342db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2343666a3d6eSSu Yanjun * Otherwise, csum completion requires checksumming packet body, 2344db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2345db8dac20SDavid S. Miller */ 2346db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2347db8dac20SDavid S. Miller int proto) 2348db8dac20SDavid S. Miller { 2349db8dac20SDavid S. Miller int err; 2350db8dac20SDavid S. Miller 2351db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2352db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2353db8dac20SDavid S. Miller 2354db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2355db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2356db8dac20SDavid S. Miller if (err) 2357db8dac20SDavid S. Miller return err; 235815f35d49SAlexey Kodanev 235915f35d49SAlexey Kodanev if (UDP_SKB_CB(skb)->partial_cov) { 236015f35d49SAlexey Kodanev skb->csum = inet_compute_pseudo(skb, proto); 236115f35d49SAlexey Kodanev return 0; 236215f35d49SAlexey Kodanev } 2363db8dac20SDavid S. Miller } 2364db8dac20SDavid S. Miller 2365b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2366b46d9f62SHannes Frederic Sowa * force to int. 2367b46d9f62SHannes Frederic Sowa */ 2368db4f1be3SSean Tranchetti err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2369ed70fcfcSTom Herbert inet_compute_pseudo); 2370db4f1be3SSean Tranchetti if (err) 2371db4f1be3SSean Tranchetti return err; 2372db4f1be3SSean Tranchetti 2373db4f1be3SSean Tranchetti if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2374db4f1be3SSean Tranchetti /* If SW calculated the value, we know it's bad */ 2375db4f1be3SSean Tranchetti if (skb->csum_complete_sw) 2376db4f1be3SSean Tranchetti return 1; 2377db4f1be3SSean Tranchetti 2378db4f1be3SSean Tranchetti /* HW says the value is bad. Let's validate that. 2379db4f1be3SSean Tranchetti * skb->csum is no longer the full packet checksum, 2380db4f1be3SSean Tranchetti * so don't treat it as such. 2381db4f1be3SSean Tranchetti */ 2382db4f1be3SSean Tranchetti skb_checksum_complete_unset(skb); 2383db4f1be3SSean Tranchetti } 2384db4f1be3SSean Tranchetti 2385db4f1be3SSean Tranchetti return 0; 2386db8dac20SDavid S. Miller } 2387db8dac20SDavid S. Miller 23882b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 23892b5a9217SPaolo Abeni * return code conversion for ip layer consumption 23902b5a9217SPaolo Abeni */ 23912b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 23922b5a9217SPaolo Abeni struct udphdr *uh) 23932b5a9217SPaolo Abeni { 23942b5a9217SPaolo Abeni int ret; 23952b5a9217SPaolo Abeni 23962b5a9217SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2397e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); 23982b5a9217SPaolo Abeni 23992b5a9217SPaolo Abeni ret = udp_queue_rcv_skb(sk, skb); 24002b5a9217SPaolo Abeni 24012b5a9217SPaolo Abeni /* a return value > 0 means to resubmit the input, but 24022b5a9217SPaolo Abeni * it wants the return to be -protocol, or 0 24032b5a9217SPaolo Abeni */ 24042b5a9217SPaolo Abeni if (ret > 0) 24052b5a9217SPaolo Abeni return -ret; 24062b5a9217SPaolo Abeni return 0; 24072b5a9217SPaolo Abeni } 24082b5a9217SPaolo Abeni 2409db8dac20SDavid S. Miller /* 2410db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2411db8dac20SDavid S. Miller */ 2412db8dac20SDavid S. Miller 2413645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2414db8dac20SDavid S. Miller int proto) 2415db8dac20SDavid S. Miller { 2416db8dac20SDavid S. Miller struct sock *sk; 24177b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2418db8dac20SDavid S. Miller unsigned short ulen; 2419adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 24202783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 24210283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 242271489e21SJoe Stringer bool refcounted; 24231c7fab70SMenglong Dong int drop_reason; 24241c7fab70SMenglong Dong 24251c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 2426db8dac20SDavid S. Miller 2427db8dac20SDavid S. Miller /* 2428db8dac20SDavid S. Miller * Validate the packet. 2429db8dac20SDavid S. Miller */ 2430db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2431db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2432db8dac20SDavid S. Miller 24337b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2434db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2435ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2436ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2437ccc2d97cSBjørn Mork 2438db8dac20SDavid S. Miller if (ulen > skb->len) 2439db8dac20SDavid S. Miller goto short_packet; 2440db8dac20SDavid S. Miller 2441db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2442db8dac20SDavid S. Miller /* UDP validates ulen. */ 2443db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2444db8dac20SDavid S. Miller goto short_packet; 2445db8dac20SDavid S. Miller uh = udp_hdr(skb); 2446db8dac20SDavid S. Miller } 2447db8dac20SDavid S. Miller 2448db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2449db8dac20SDavid S. Miller goto csum_error; 2450db8dac20SDavid S. Miller 245171489e21SJoe Stringer sk = skb_steal_sock(skb, &refcounted); 24528afdd99aSEric Dumazet if (sk) { 245397502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2454421b3885SShawn Bohrer int ret; 2455421b3885SShawn Bohrer 24568f905c0eSEric Dumazet if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 245797502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2458421b3885SShawn Bohrer 24592b5a9217SPaolo Abeni ret = udp_unicast_rcv_skb(sk, skb, uh); 246071489e21SJoe Stringer if (refcounted) 24618afdd99aSEric Dumazet sock_put(sk); 24622b5a9217SPaolo Abeni return ret; 2463c18450a5SFabian Frederick } 2464c18450a5SFabian Frederick 2465db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2466e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 246736cbb245SRick Jones saddr, daddr, udptable, proto); 2468db8dac20SDavid S. Miller 2469607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 24702b5a9217SPaolo Abeni if (sk) 24712b5a9217SPaolo Abeni return udp_unicast_rcv_skb(sk, skb, uh); 2472db8dac20SDavid S. Miller 2473db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2474db8dac20SDavid S. Miller goto drop; 2475895b5c9fSFlorian Westphal nf_reset_ct(skb); 2476db8dac20SDavid S. Miller 2477db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2478db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2479db8dac20SDavid S. Miller goto csum_error; 2480db8dac20SDavid S. Miller 24811c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_NO_SOCKET; 248202c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2483db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2484db8dac20SDavid S. Miller 2485db8dac20SDavid S. Miller /* 2486db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2487db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2488db8dac20SDavid S. Miller */ 24891c7fab70SMenglong Dong kfree_skb_reason(skb, drop_reason); 2490db8dac20SDavid S. Miller return 0; 2491db8dac20SDavid S. Miller 2492db8dac20SDavid S. Miller short_packet: 24931c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL; 2494ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2495afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2496afd46503SJoe Perches &saddr, ntohs(uh->source), 2497afd46503SJoe Perches ulen, skb->len, 2498afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2499db8dac20SDavid S. Miller goto drop; 2500db8dac20SDavid S. Miller 2501db8dac20SDavid S. Miller csum_error: 2502db8dac20SDavid S. Miller /* 2503db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2504db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2505db8dac20SDavid S. Miller */ 25061c7fab70SMenglong Dong drop_reason = SKB_DROP_REASON_UDP_CSUM; 2507ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2508afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2509afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2510db8dac20SDavid S. Miller ulen); 251102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2512db8dac20SDavid S. Miller drop: 251302c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 25141c7fab70SMenglong Dong kfree_skb_reason(skb, drop_reason); 2515db8dac20SDavid S. Miller return 0; 2516db8dac20SDavid S. Miller } 2517db8dac20SDavid S. Miller 2518421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2519421b3885SShawn Bohrer * If more than one socket found returns NULL 2520421b3885SShawn Bohrer */ 2521421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2522421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2523421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2524fb74c277SDavid Ahern int dif, int sdif) 2525421b3885SShawn Bohrer { 2526421b3885SShawn Bohrer struct sock *sk, *result; 2527421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2528ca065d0cSEric Dumazet unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); 2529421b3885SShawn Bohrer struct udp_hslot *hslot = &udp_table.hash[slot]; 2530421b3885SShawn Bohrer 253163c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 253263c6f81cSEric Dumazet if (hslot->count > 10) 253363c6f81cSEric Dumazet return NULL; 253463c6f81cSEric Dumazet 2535421b3885SShawn Bohrer result = NULL; 2536ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2537ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2538fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2539ca065d0cSEric Dumazet if (result) 2540ca065d0cSEric Dumazet return NULL; 2541421b3885SShawn Bohrer result = sk; 2542421b3885SShawn Bohrer } 2543421b3885SShawn Bohrer } 2544421b3885SShawn Bohrer 2545421b3885SShawn Bohrer return result; 2546421b3885SShawn Bohrer } 2547421b3885SShawn Bohrer 2548421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2549421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2550421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2551421b3885SShawn Bohrer */ 2552421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2553421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2554421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 25553fa6f616SDavid Ahern int dif, int sdif) 2556421b3885SShawn Bohrer { 2557421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2558f0b1e64cSMartin KaFai Lau unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2559421b3885SShawn Bohrer unsigned int slot2 = hash2 & udp_table.mask; 2560421b3885SShawn Bohrer struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 2561c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2562421b3885SShawn Bohrer const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 2563ca065d0cSEric Dumazet struct sock *sk; 2564421b3885SShawn Bohrer 2565ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2566ca065d0cSEric Dumazet if (INET_MATCH(sk, net, acookie, rmt_addr, 25673fa6f616SDavid Ahern loc_addr, ports, dif, sdif)) 2568ca065d0cSEric Dumazet return sk; 2569421b3885SShawn Bohrer /* Only check first socket in chain */ 2570421b3885SShawn Bohrer break; 2571421b3885SShawn Bohrer } 2572ca065d0cSEric Dumazet return NULL; 2573421b3885SShawn Bohrer } 2574421b3885SShawn Bohrer 25757487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2576421b3885SShawn Bohrer { 2577610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2578bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2579610438b7SEric Dumazet const struct iphdr *iph; 2580610438b7SEric Dumazet const struct udphdr *uh; 2581ca065d0cSEric Dumazet struct sock *sk = NULL; 2582421b3885SShawn Bohrer struct dst_entry *dst; 2583421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2584fb74c277SDavid Ahern int sdif = inet_sdif(skb); 25856e540309SShawn Bohrer int ours; 2586421b3885SShawn Bohrer 2587421b3885SShawn Bohrer /* validate the packet */ 2588421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 25897487449cSPaolo Abeni return 0; 2590421b3885SShawn Bohrer 2591610438b7SEric Dumazet iph = ip_hdr(skb); 2592610438b7SEric Dumazet uh = udp_hdr(skb); 2593610438b7SEric Dumazet 2594996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2595bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 25966e540309SShawn Bohrer 25976e540309SShawn Bohrer if (!in_dev) 25987487449cSPaolo Abeni return 0; 25996e540309SShawn Bohrer 26006e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 26016e540309SShawn Bohrer iph->protocol); 26026e540309SShawn Bohrer if (!ours) 26037487449cSPaolo Abeni return 0; 2604ad0ea198SPaolo Abeni 2605421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2606fb74c277SDavid Ahern uh->source, iph->saddr, 2607fb74c277SDavid Ahern dif, sdif); 26086e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2609421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 26103fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 26116e540309SShawn Bohrer } 2612421b3885SShawn Bohrer 261341c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 26147487449cSPaolo Abeni return 0; 2615421b3885SShawn Bohrer 2616421b3885SShawn Bohrer skb->sk = sk; 261782eabd9eSAlexander Duyck skb->destructor = sock_efree; 26188f905c0eSEric Dumazet dst = rcu_dereference(sk->sk_rx_dst); 2619421b3885SShawn Bohrer 2620421b3885SShawn Bohrer if (dst) 2621421b3885SShawn Bohrer dst = dst_check(dst, 0); 262210e2eb87SEric Dumazet if (dst) { 2623bc044e8dSPaolo Abeni u32 itag = 0; 2624bc044e8dSPaolo Abeni 2625d24406c8SWei Wang /* set noref for now. 2626d24406c8SWei Wang * any place which wants to hold dst has to call 2627d24406c8SWei Wang * dst_hold_safe() 2628d24406c8SWei Wang */ 2629421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2630bc044e8dSPaolo Abeni 2631bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2632bc044e8dSPaolo Abeni * the source on each packet 2633bc044e8dSPaolo Abeni */ 2634bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2635bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 26368d2b51b0SGuillaume Nault iph->saddr, 26378d2b51b0SGuillaume Nault iph->tos & IPTOS_RT_MASK, 2638bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2639421b3885SShawn Bohrer } 26407487449cSPaolo Abeni return 0; 264110e2eb87SEric Dumazet } 2642421b3885SShawn Bohrer 2643db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2644db8dac20SDavid S. Miller { 2645645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2646db8dac20SDavid S. Miller } 2647db8dac20SDavid S. Miller 26487d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2649db8dac20SDavid S. Miller { 265044046a59STom Parkin struct udp_sock *up = udp_sk(sk); 26518a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2652a8b897c7SPaolo Abeni 2653a8b897c7SPaolo Abeni /* protects from races with udp_abort() */ 2654a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 2655db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 26568a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 265760fb9567SPaolo Abeni if (static_branch_unlikely(&udp_encap_needed_key)) { 265860fb9567SPaolo Abeni if (up->encap_type) { 265944046a59STom Parkin void (*encap_destroy)(struct sock *sk); 26606aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 266144046a59STom Parkin if (encap_destroy) 266244046a59STom Parkin encap_destroy(sk); 266344046a59STom Parkin } 266460fb9567SPaolo Abeni if (up->encap_enabled) 26659c480601SPaolo Abeni static_branch_dec(&udp_encap_needed_key); 266660fb9567SPaolo Abeni } 2667db8dac20SDavid S. Miller } 2668db8dac20SDavid S. Miller 26691da177e4SLinus Torvalds /* 26701da177e4SLinus Torvalds * Socket option code for UDP 26711da177e4SLinus Torvalds */ 26724c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 267391ac1ccaSChristoph Hellwig sockptr_t optval, unsigned int optlen, 26744c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 26751da177e4SLinus Torvalds { 26761da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 26771c19448cSTom Herbert int val, valbool; 26781da177e4SLinus Torvalds int err = 0; 2679b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds if (optlen < sizeof(int)) 26821da177e4SLinus Torvalds return -EINVAL; 26831da177e4SLinus Torvalds 268491ac1ccaSChristoph Hellwig if (copy_from_sockptr(&val, optval, sizeof(val))) 26851da177e4SLinus Torvalds return -EFAULT; 26861da177e4SLinus Torvalds 26871c19448cSTom Herbert valbool = val ? 1 : 0; 26881c19448cSTom Herbert 26891da177e4SLinus Torvalds switch (optname) { 26901da177e4SLinus Torvalds case UDP_CORK: 26911da177e4SLinus Torvalds if (val != 0) { 2692a9f59707SEric Dumazet WRITE_ONCE(up->corkflag, 1); 26931da177e4SLinus Torvalds } else { 2694a9f59707SEric Dumazet WRITE_ONCE(up->corkflag, 0); 26951da177e4SLinus Torvalds lock_sock(sk); 26964243cdc2SJoe Perches push_pending_frames(sk); 26971da177e4SLinus Torvalds release_sock(sk); 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds break; 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds case UDP_ENCAP: 27021da177e4SLinus Torvalds switch (val) { 27031da177e4SLinus Torvalds case 0: 2704fd1ac07fSAlexey Dobriyan #ifdef CONFIG_XFRM 27051da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 27061da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 27070146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 27080146dca7SSabrina Dubroca if (sk->sk_family == AF_INET6) 27090146dca7SSabrina Dubroca up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv; 27100146dca7SSabrina Dubroca else 27110146dca7SSabrina Dubroca #endif 2712067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 2713fd1ac07fSAlexey Dobriyan #endif 2714a8eceea8SJoe Perches fallthrough; 2715342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 27161da177e4SLinus Torvalds up->encap_type = val; 271760fb9567SPaolo Abeni lock_sock(sk); 271860fb9567SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 271960fb9567SPaolo Abeni release_sock(sk); 27201da177e4SLinus Torvalds break; 27211da177e4SLinus Torvalds default: 27221da177e4SLinus Torvalds err = -ENOPROTOOPT; 27231da177e4SLinus Torvalds break; 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds break; 27261da177e4SLinus Torvalds 27271c19448cSTom Herbert case UDP_NO_CHECK6_TX: 27281c19448cSTom Herbert up->no_check6_tx = valbool; 27291c19448cSTom Herbert break; 27301c19448cSTom Herbert 27311c19448cSTom Herbert case UDP_NO_CHECK6_RX: 27321c19448cSTom Herbert up->no_check6_rx = valbool; 27331c19448cSTom Herbert break; 27341c19448cSTom Herbert 2735bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2736bec1f6f6SWillem de Bruijn if (val < 0 || val > USHRT_MAX) 2737bec1f6f6SWillem de Bruijn return -EINVAL; 273818a419baSEric Dumazet WRITE_ONCE(up->gso_size, val); 2739bec1f6f6SWillem de Bruijn break; 2740bec1f6f6SWillem de Bruijn 2741e20cf8d3SPaolo Abeni case UDP_GRO: 2742e20cf8d3SPaolo Abeni lock_sock(sk); 274378352f73SPaolo Abeni 274478352f73SPaolo Abeni /* when enabling GRO, accept the related GSO packet type */ 2745e20cf8d3SPaolo Abeni if (valbool) 2746e20cf8d3SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 2747e20cf8d3SPaolo Abeni up->gro_enabled = valbool; 274878352f73SPaolo Abeni up->accept_udp_l4 = valbool; 2749e20cf8d3SPaolo Abeni release_sock(sk); 2750e20cf8d3SPaolo Abeni break; 2751e20cf8d3SPaolo Abeni 2752ba4e58ecSGerrit Renker /* 2753ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2754ba4e58ecSGerrit Renker */ 2755ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2756ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2757ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2758b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2759ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2760ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2761ba4e58ecSGerrit Renker val = 8; 27624be929beSAlexey Dobriyan else if (val > USHRT_MAX) 27634be929beSAlexey Dobriyan val = USHRT_MAX; 2764ba4e58ecSGerrit Renker up->pcslen = val; 2765ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 2766ba4e58ecSGerrit Renker break; 2767ba4e58ecSGerrit Renker 2768ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2769ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2770ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2771ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2772b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2773ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2774ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2775ba4e58ecSGerrit Renker val = 8; 27764be929beSAlexey Dobriyan else if (val > USHRT_MAX) 27774be929beSAlexey Dobriyan val = USHRT_MAX; 2778ba4e58ecSGerrit Renker up->pcrlen = val; 2779ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 2780ba4e58ecSGerrit Renker break; 2781ba4e58ecSGerrit Renker 27821da177e4SLinus Torvalds default: 27831da177e4SLinus Torvalds err = -ENOPROTOOPT; 27841da177e4SLinus Torvalds break; 27856516c655SStephen Hemminger } 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds return err; 27881da177e4SLinus Torvalds } 2789c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 27901da177e4SLinus Torvalds 2791a7b75c5aSChristoph Hellwig int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 2792a7b75c5aSChristoph Hellwig unsigned int optlen) 2793db8dac20SDavid S. Miller { 2794db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 279591ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 2796a7b75c5aSChristoph Hellwig optval, optlen, 2797db8dac20SDavid S. Miller udp_push_pending_frames); 2798db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2799db8dac20SDavid S. Miller } 2800db8dac20SDavid S. Miller 28014c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 28021da177e4SLinus Torvalds char __user *optval, int __user *optlen) 28031da177e4SLinus Torvalds { 28041da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 28051da177e4SLinus Torvalds int val, len; 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds if (get_user(len, optlen)) 28081da177e4SLinus Torvalds return -EFAULT; 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds if (len < 0) 28131da177e4SLinus Torvalds return -EINVAL; 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds switch (optname) { 28161da177e4SLinus Torvalds case UDP_CORK: 2817a9f59707SEric Dumazet val = READ_ONCE(up->corkflag); 28181da177e4SLinus Torvalds break; 28191da177e4SLinus Torvalds 28201da177e4SLinus Torvalds case UDP_ENCAP: 28211da177e4SLinus Torvalds val = up->encap_type; 28221da177e4SLinus Torvalds break; 28231da177e4SLinus Torvalds 28241c19448cSTom Herbert case UDP_NO_CHECK6_TX: 28251c19448cSTom Herbert val = up->no_check6_tx; 28261c19448cSTom Herbert break; 28271c19448cSTom Herbert 28281c19448cSTom Herbert case UDP_NO_CHECK6_RX: 28291c19448cSTom Herbert val = up->no_check6_rx; 28301c19448cSTom Herbert break; 28311c19448cSTom Herbert 2832bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 283318a419baSEric Dumazet val = READ_ONCE(up->gso_size); 2834bec1f6f6SWillem de Bruijn break; 2835bec1f6f6SWillem de Bruijn 283698184612SNorman Maurer case UDP_GRO: 283798184612SNorman Maurer val = up->gro_enabled; 283898184612SNorman Maurer break; 283998184612SNorman Maurer 2840ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2841ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2842ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2843ba4e58ecSGerrit Renker val = up->pcslen; 2844ba4e58ecSGerrit Renker break; 2845ba4e58ecSGerrit Renker 2846ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2847ba4e58ecSGerrit Renker val = up->pcrlen; 2848ba4e58ecSGerrit Renker break; 2849ba4e58ecSGerrit Renker 28501da177e4SLinus Torvalds default: 28511da177e4SLinus Torvalds return -ENOPROTOOPT; 28526516c655SStephen Hemminger } 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds if (put_user(len, optlen)) 28551da177e4SLinus Torvalds return -EFAULT; 28561da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 28571da177e4SLinus Torvalds return -EFAULT; 28581da177e4SLinus Torvalds return 0; 28591da177e4SLinus Torvalds } 2860c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 28611da177e4SLinus Torvalds 2862db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2863db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2864db8dac20SDavid S. Miller { 2865db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2866db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2867db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2868db8dac20SDavid S. Miller } 2869db8dac20SDavid S. Miller 28701da177e4SLinus Torvalds /** 28711da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 28723628e3cbSAndrew Lunn * @file: - file struct 28733628e3cbSAndrew Lunn * @sock: - socket 28743628e3cbSAndrew Lunn * @wait: - poll table 28751da177e4SLinus Torvalds * 28761da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 28771da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 28781da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 28791da177e4SLinus Torvalds * then it could get return from select indicating data available 28801da177e4SLinus Torvalds * but then block when reading it. Add special case code 28811da177e4SLinus Torvalds * to work around these arguably broken applications. 28821da177e4SLinus Torvalds */ 2883a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 28841da177e4SLinus Torvalds { 2885a11e1d43SLinus Torvalds __poll_t mask = datagram_poll(file, sock, wait); 28861da177e4SLinus Torvalds struct sock *sk = sock->sk; 28871da177e4SLinus Torvalds 28883ef7cf57SEric Dumazet if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) 2889a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 28902276f58aSPaolo Abeni 28911da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 2892a11e1d43SLinus Torvalds if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2893e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2894a9a08845SLinus Torvalds mask &= ~(EPOLLIN | EPOLLRDNORM); 28951da177e4SLinus Torvalds 2896af493388SCong Wang /* psock ingress_msg queue should not contain any bad checksum frames */ 2897af493388SCong Wang if (sk_is_readable(sk)) 2898af493388SCong Wang mask |= EPOLLIN | EPOLLRDNORM; 28991da177e4SLinus Torvalds return mask; 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds } 2902a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll); 29031da177e4SLinus Torvalds 29045d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 29055d77dca8SDavid Ahern { 29065d77dca8SDavid Ahern lock_sock(sk); 29075d77dca8SDavid Ahern 2908a8b897c7SPaolo Abeni /* udp{v6}_destroy_sock() sets it under the sk lock, avoid racing 2909a8b897c7SPaolo Abeni * with close() 2910a8b897c7SPaolo Abeni */ 2911a8b897c7SPaolo Abeni if (sock_flag(sk, SOCK_DEAD)) 2912a8b897c7SPaolo Abeni goto out; 2913a8b897c7SPaolo Abeni 29145d77dca8SDavid Ahern sk->sk_err = err; 2915e3ae2365SAlexander Aring sk_error_report(sk); 2916286c72deSEric Dumazet __udp_disconnect(sk, 0); 29175d77dca8SDavid Ahern 2918a8b897c7SPaolo Abeni out: 29195d77dca8SDavid Ahern release_sock(sk); 29205d77dca8SDavid Ahern 29215d77dca8SDavid Ahern return 0; 29225d77dca8SDavid Ahern } 29235d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 29245d77dca8SDavid Ahern 2925db8dac20SDavid S. Miller struct proto udp_prot = { 2926db8dac20SDavid S. Miller .name = "UDP", 2927db8dac20SDavid S. Miller .owner = THIS_MODULE, 2928db8dac20SDavid S. Miller .close = udp_lib_close, 2929d74bad4eSAndrey Ignatov .pre_connect = udp_pre_connect, 2930db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2931db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2932db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2933850cbaddSPaolo Abeni .init = udp_init_sock, 2934db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2935db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2936db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2937db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2938db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 2939db8dac20SDavid S. Miller .sendpage = udp_sendpage, 29408141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2941db8dac20SDavid S. Miller .hash = udp_lib_hash, 2942db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2943719f8358SEric Dumazet .rehash = udp_v4_rehash, 2944db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 294591a760b2SMenglong Dong .put_port = udp_lib_unhash, 29468a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL 29478a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto, 29488a59f9d1SCong Wang #endif 2949db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 2950db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 29511e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 29521e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2953db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 2954645ca708SEric Dumazet .h.udp_table = &udp_table, 29555d77dca8SDavid Ahern .diag_destroy = udp_abort, 2956db8dac20SDavid S. Miller }; 2957c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 29601da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 29611da177e4SLinus Torvalds 2962645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 29631da177e4SLinus Torvalds { 29641da177e4SLinus Torvalds struct sock *sk; 29659e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 29661da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 29676f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 29681da177e4SLinus Torvalds 29699e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 29709e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 29719e8ca27aSYonghong Song else 2972359745d7SMuchun Song afinfo = pde_data(file_inode(seq->file)); 29739e8ca27aSYonghong Song 2974a3d2599bSChristoph Hellwig for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; 2975f86dcc5aSEric Dumazet ++state->bucket) { 2976a3d2599bSChristoph Hellwig struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; 2977f86dcc5aSEric Dumazet 2978ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2979f86dcc5aSEric Dumazet continue; 2980f86dcc5aSEric Dumazet 2981645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 2982ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 2983878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 2984a91275efSDaniel Lezcano continue; 29859e8ca27aSYonghong Song if (afinfo->family == AF_UNSPEC || 29869e8ca27aSYonghong Song sk->sk_family == afinfo->family) 29871da177e4SLinus Torvalds goto found; 29881da177e4SLinus Torvalds } 2989645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 29901da177e4SLinus Torvalds } 29911da177e4SLinus Torvalds sk = NULL; 29921da177e4SLinus Torvalds found: 29931da177e4SLinus Torvalds return sk; 29941da177e4SLinus Torvalds } 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 29971da177e4SLinus Torvalds { 29989e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 29991da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 30006f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 30011da177e4SLinus Torvalds 30029e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 30039e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 30049e8ca27aSYonghong Song else 3005359745d7SMuchun Song afinfo = pde_data(file_inode(seq->file)); 30069e8ca27aSYonghong Song 30071da177e4SLinus Torvalds do { 3008ca065d0cSEric Dumazet sk = sk_next(sk); 30099e8ca27aSYonghong Song } while (sk && (!net_eq(sock_net(sk), net) || 30109e8ca27aSYonghong Song (afinfo->family != AF_UNSPEC && 30119e8ca27aSYonghong Song sk->sk_family != afinfo->family))); 30121da177e4SLinus Torvalds 3013645ca708SEric Dumazet if (!sk) { 3014a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 3015a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 3016645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds return sk; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 30211da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 30221da177e4SLinus Torvalds { 3023645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds if (sk) 30261da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 30271da177e4SLinus Torvalds --pos; 30281da177e4SLinus Torvalds return pos ? NULL : sk; 30291da177e4SLinus Torvalds } 30301da177e4SLinus Torvalds 3031a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos) 30321da177e4SLinus Torvalds { 303330842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 3034f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 303530842f29SVitaly Mayatskikh 3036b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 30371da177e4SLinus Torvalds } 3038a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start); 30391da177e4SLinus Torvalds 3040a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 30411da177e4SLinus Torvalds { 30421da177e4SLinus Torvalds struct sock *sk; 30431da177e4SLinus Torvalds 3044b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 30451da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 30461da177e4SLinus Torvalds else 30471da177e4SLinus Torvalds sk = udp_get_next(seq, v); 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds ++*pos; 30501da177e4SLinus Torvalds return sk; 30511da177e4SLinus Torvalds } 3052a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next); 30531da177e4SLinus Torvalds 3054a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v) 30551da177e4SLinus Torvalds { 30569e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 3057645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 3058645ca708SEric Dumazet 30599e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 30609e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 30619e8ca27aSYonghong Song else 3062359745d7SMuchun Song afinfo = pde_data(file_inode(seq->file)); 30639e8ca27aSYonghong Song 3064a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 3065a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 30661da177e4SLinus Torvalds } 3067a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop); 3068db8dac20SDavid S. Miller 3069db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 30705e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 3071652586dfSTetsuo Handa int bucket) 3072db8dac20SDavid S. Miller { 3073db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 3074c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 3075c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 3076c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 3077c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 3078db8dac20SDavid S. Miller 3079f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 3080ea9a0379SPatrick Talbert " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", 3081db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 308231e6d363SEric Dumazet sk_wmem_alloc_get(sp), 30836c206b20SPaolo Abeni udp_rqueue_get(sp), 3084a7cb5a49SEric W. Biederman 0, 0L, 0, 3085a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 3086a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 308741c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 3088652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 3089db8dac20SDavid S. Miller } 3090db8dac20SDavid S. Miller 3091db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 3092db8dac20SDavid S. Miller { 3093652586dfSTetsuo Handa seq_setwidth(seq, 127); 3094db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 3095652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 3096db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 3097cb61cb9bSEric Dumazet "inode ref pointer drops"); 3098db8dac20SDavid S. Miller else { 3099db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 3100db8dac20SDavid S. Miller 3101652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 3102db8dac20SDavid S. Miller } 3103652586dfSTetsuo Handa seq_pad(seq, '\n'); 3104db8dac20SDavid S. Miller return 0; 3105db8dac20SDavid S. Miller } 3106db8dac20SDavid S. Miller 31075788b3a0SYonghong Song #ifdef CONFIG_BPF_SYSCALL 31085788b3a0SYonghong Song struct bpf_iter__udp { 31095788b3a0SYonghong Song __bpf_md_ptr(struct bpf_iter_meta *, meta); 31105788b3a0SYonghong Song __bpf_md_ptr(struct udp_sock *, udp_sk); 31115788b3a0SYonghong Song uid_t uid __aligned(8); 31125788b3a0SYonghong Song int bucket __aligned(8); 31135788b3a0SYonghong Song }; 31145788b3a0SYonghong Song 31155788b3a0SYonghong Song static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 31165788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 31175788b3a0SYonghong Song { 31185788b3a0SYonghong Song struct bpf_iter__udp ctx; 31195788b3a0SYonghong Song 31205788b3a0SYonghong Song meta->seq_num--; /* skip SEQ_START_TOKEN */ 31215788b3a0SYonghong Song ctx.meta = meta; 31225788b3a0SYonghong Song ctx.udp_sk = udp_sk; 31235788b3a0SYonghong Song ctx.uid = uid; 31245788b3a0SYonghong Song ctx.bucket = bucket; 31255788b3a0SYonghong Song return bpf_iter_run_prog(prog, &ctx); 31265788b3a0SYonghong Song } 31275788b3a0SYonghong Song 31285788b3a0SYonghong Song static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) 31295788b3a0SYonghong Song { 31305788b3a0SYonghong Song struct udp_iter_state *state = seq->private; 31315788b3a0SYonghong Song struct bpf_iter_meta meta; 31325788b3a0SYonghong Song struct bpf_prog *prog; 31335788b3a0SYonghong Song struct sock *sk = v; 31345788b3a0SYonghong Song uid_t uid; 31355788b3a0SYonghong Song 31365788b3a0SYonghong Song if (v == SEQ_START_TOKEN) 31375788b3a0SYonghong Song return 0; 31385788b3a0SYonghong Song 31395788b3a0SYonghong Song uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); 31405788b3a0SYonghong Song meta.seq = seq; 31415788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, false); 31425788b3a0SYonghong Song return udp_prog_seq_show(prog, &meta, v, uid, state->bucket); 31435788b3a0SYonghong Song } 31445788b3a0SYonghong Song 31455788b3a0SYonghong Song static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) 31465788b3a0SYonghong Song { 31475788b3a0SYonghong Song struct bpf_iter_meta meta; 31485788b3a0SYonghong Song struct bpf_prog *prog; 31495788b3a0SYonghong Song 31505788b3a0SYonghong Song if (!v) { 31515788b3a0SYonghong Song meta.seq = seq; 31525788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, true); 31535788b3a0SYonghong Song if (prog) 31545788b3a0SYonghong Song (void)udp_prog_seq_show(prog, &meta, v, 0, 0); 31555788b3a0SYonghong Song } 31565788b3a0SYonghong Song 31575788b3a0SYonghong Song udp_seq_stop(seq, v); 31585788b3a0SYonghong Song } 31595788b3a0SYonghong Song 31605788b3a0SYonghong Song static const struct seq_operations bpf_iter_udp_seq_ops = { 31615788b3a0SYonghong Song .start = udp_seq_start, 31625788b3a0SYonghong Song .next = udp_seq_next, 31635788b3a0SYonghong Song .stop = bpf_iter_udp_seq_stop, 31645788b3a0SYonghong Song .show = bpf_iter_udp_seq_show, 31655788b3a0SYonghong Song }; 31665788b3a0SYonghong Song #endif 31675788b3a0SYonghong Song 3168c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = { 3169a3d2599bSChristoph Hellwig .start = udp_seq_start, 3170a3d2599bSChristoph Hellwig .next = udp_seq_next, 3171a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 3172a3d2599bSChristoph Hellwig .show = udp4_seq_show, 3173a3d2599bSChristoph Hellwig }; 3174c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops); 317573cb88ecSArjan van de Ven 3176db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 3177db8dac20SDavid S. Miller .family = AF_INET, 3178645ca708SEric Dumazet .udp_table = &udp_table, 3179db8dac20SDavid S. Miller }; 3180db8dac20SDavid S. Miller 31812c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 318215439febSPavel Emelyanov { 3183c3506372SChristoph Hellwig if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 3184c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 3185a3d2599bSChristoph Hellwig return -ENOMEM; 3186a3d2599bSChristoph Hellwig return 0; 318715439febSPavel Emelyanov } 318815439febSPavel Emelyanov 31892c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 319015439febSPavel Emelyanov { 3191a3d2599bSChristoph Hellwig remove_proc_entry("udp", net->proc_net); 319215439febSPavel Emelyanov } 319315439febSPavel Emelyanov 319415439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 319515439febSPavel Emelyanov .init = udp4_proc_init_net, 319615439febSPavel Emelyanov .exit = udp4_proc_exit_net, 319715439febSPavel Emelyanov }; 319815439febSPavel Emelyanov 3199db8dac20SDavid S. Miller int __init udp4_proc_init(void) 3200db8dac20SDavid S. Miller { 320115439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 3202db8dac20SDavid S. Miller } 3203db8dac20SDavid S. Miller 3204db8dac20SDavid S. Miller void udp4_proc_exit(void) 3205db8dac20SDavid S. Miller { 320615439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 3207db8dac20SDavid S. Miller } 32081da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 32091da177e4SLinus Torvalds 3210f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 3211f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 3212645ca708SEric Dumazet { 3213413c27d8SEldad Zack ssize_t ret; 3214413c27d8SEldad Zack 3215f86dcc5aSEric Dumazet if (!str) 3216f86dcc5aSEric Dumazet return 0; 3217413c27d8SEldad Zack 3218413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 3219413c27d8SEldad Zack if (ret) 3220413c27d8SEldad Zack return 0; 3221413c27d8SEldad Zack 3222f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 3223f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 3224f86dcc5aSEric Dumazet return 1; 3225f86dcc5aSEric Dumazet } 3226f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 3227645ca708SEric Dumazet 3228f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 3229f86dcc5aSEric Dumazet { 3230f86dcc5aSEric Dumazet unsigned int i; 3231f86dcc5aSEric Dumazet 3232f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 3233512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 3234f86dcc5aSEric Dumazet uhash_entries, 3235f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 3236f86dcc5aSEric Dumazet 0, 3237f86dcc5aSEric Dumazet &table->log, 3238f86dcc5aSEric Dumazet &table->mask, 323931fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 3240f86dcc5aSEric Dumazet 64 * 1024); 324131fe62b9STim Bird 3242512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 3243f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 3244ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 3245fdcc8aa9SEric Dumazet table->hash[i].count = 0; 3246645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 3247645ca708SEric Dumazet } 3248512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 3249ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 3250512615b6SEric Dumazet table->hash2[i].count = 0; 3251512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 3252512615b6SEric Dumazet } 3253645ca708SEric Dumazet } 3254645ca708SEric Dumazet 3255723b8e46STom Herbert u32 udp_flow_hashrnd(void) 3256723b8e46STom Herbert { 3257723b8e46STom Herbert static u32 hashrnd __read_mostly; 3258723b8e46STom Herbert 3259723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 3260723b8e46STom Herbert 3261723b8e46STom Herbert return hashrnd; 3262723b8e46STom Herbert } 3263723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 3264723b8e46STom Herbert 32651e802951STonghao Zhang static void __udp_sysctl_init(struct net *net) 32661e802951STonghao Zhang { 32671e802951STonghao Zhang net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM; 32681e802951STonghao Zhang net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM; 32691e802951STonghao Zhang 32701e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV 32711e802951STonghao Zhang net->ipv4.sysctl_udp_l3mdev_accept = 0; 32721e802951STonghao Zhang #endif 32731e802951STonghao Zhang } 32741e802951STonghao Zhang 32751e802951STonghao Zhang static int __net_init udp_sysctl_init(struct net *net) 32761e802951STonghao Zhang { 32771e802951STonghao Zhang __udp_sysctl_init(net); 32781e802951STonghao Zhang return 0; 32791e802951STonghao Zhang } 32801e802951STonghao Zhang 32811e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = { 32821e802951STonghao Zhang .init = udp_sysctl_init, 32831e802951STonghao Zhang }; 32841e802951STonghao Zhang 32855788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 32865788b3a0SYonghong Song DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, 32875788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 32885788b3a0SYonghong Song 3289f9c79272SYonghong Song static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux) 32905788b3a0SYonghong Song { 32915788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 32925788b3a0SYonghong Song struct udp_seq_afinfo *afinfo; 32935788b3a0SYonghong Song int ret; 32945788b3a0SYonghong Song 32955788b3a0SYonghong Song afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN); 32965788b3a0SYonghong Song if (!afinfo) 32975788b3a0SYonghong Song return -ENOMEM; 32985788b3a0SYonghong Song 32995788b3a0SYonghong Song afinfo->family = AF_UNSPEC; 33005788b3a0SYonghong Song afinfo->udp_table = &udp_table; 33015788b3a0SYonghong Song st->bpf_seq_afinfo = afinfo; 3302f9c79272SYonghong Song ret = bpf_iter_init_seq_net(priv_data, aux); 33035788b3a0SYonghong Song if (ret) 33045788b3a0SYonghong Song kfree(afinfo); 33055788b3a0SYonghong Song return ret; 33065788b3a0SYonghong Song } 33075788b3a0SYonghong Song 33085788b3a0SYonghong Song static void bpf_iter_fini_udp(void *priv_data) 33095788b3a0SYonghong Song { 33105788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 33115788b3a0SYonghong Song 33125788b3a0SYonghong Song kfree(st->bpf_seq_afinfo); 33135788b3a0SYonghong Song bpf_iter_fini_seq_net(priv_data); 33145788b3a0SYonghong Song } 33155788b3a0SYonghong Song 331614fc6bd6SYonghong Song static const struct bpf_iter_seq_info udp_seq_info = { 33175788b3a0SYonghong Song .seq_ops = &bpf_iter_udp_seq_ops, 33185788b3a0SYonghong Song .init_seq_private = bpf_iter_init_udp, 33195788b3a0SYonghong Song .fini_seq_private = bpf_iter_fini_udp, 33205788b3a0SYonghong Song .seq_priv_size = sizeof(struct udp_iter_state), 332114fc6bd6SYonghong Song }; 332214fc6bd6SYonghong Song 332314fc6bd6SYonghong Song static struct bpf_iter_reg udp_reg_info = { 332414fc6bd6SYonghong Song .target = "udp", 33255788b3a0SYonghong Song .ctx_arg_info_size = 1, 33265788b3a0SYonghong Song .ctx_arg_info = { 33275788b3a0SYonghong Song { offsetof(struct bpf_iter__udp, udp_sk), 33285788b3a0SYonghong Song PTR_TO_BTF_ID_OR_NULL }, 33295788b3a0SYonghong Song }, 333014fc6bd6SYonghong Song .seq_info = &udp_seq_info, 33315788b3a0SYonghong Song }; 33325788b3a0SYonghong Song 33335788b3a0SYonghong Song static void __init bpf_iter_register(void) 33345788b3a0SYonghong Song { 3335951cf368SYonghong Song udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP]; 33365788b3a0SYonghong Song if (bpf_iter_reg_target(&udp_reg_info)) 33375788b3a0SYonghong Song pr_warn("Warning: could not register bpf iterator udp\n"); 33385788b3a0SYonghong Song } 33395788b3a0SYonghong Song #endif 33405788b3a0SYonghong Song 334195766fffSHideo Aoki void __init udp_init(void) 334295766fffSHideo Aoki { 3343f03d78dbSEric Dumazet unsigned long limit; 33444b272750SEric Dumazet unsigned int i; 334595766fffSHideo Aoki 3346f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 3347f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 334895766fffSHideo Aoki limit = max(limit, 128UL); 334995766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 335095766fffSHideo Aoki sysctl_udp_mem[1] = limit; 335195766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 335295766fffSHideo Aoki 33531e802951STonghao Zhang __udp_sysctl_init(&init_net); 33544b272750SEric Dumazet 33554b272750SEric Dumazet /* 16 spinlocks per cpu */ 33564b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 33574b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 33584b272750SEric Dumazet GFP_KERNEL); 33594b272750SEric Dumazet if (!udp_busylocks) 33604b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 33614b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 33624b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 33631e802951STonghao Zhang 33641e802951STonghao Zhang if (register_pernet_subsys(&udp_sysctl_ops)) 33651e802951STonghao Zhang panic("UDP: failed to init sysctl parameters.\n"); 33665788b3a0SYonghong Song 33675788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 33685788b3a0SYonghong Song bpf_iter_register(); 33695788b3a0SYonghong Song #endif 337095766fffSHideo Aoki } 3371