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 777c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 781da177e4SLinus Torvalds #include <asm/ioctls.h> 7957c8a661SMike Rapoport #include <linux/memblock.h> 808203efb3SEric Dumazet #include <linux/highmem.h> 818203efb3SEric Dumazet #include <linux/swap.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 1268d987e5cSEric Dumazet atomic_long_t udp_memory_allocated; 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; 393ba418fa3STom Herbert score += 4; 394fb74c277SDavid Ahern 3957170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 39670da268bSEric Dumazet score++; 397645ca708SEric Dumazet return score; 398645ca708SEric Dumazet } 399645ca708SEric Dumazet 4006eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 40165cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 40265cd8033SHannes Frederic Sowa const __be16 fport) 40365cd8033SHannes Frederic Sowa { 4041bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4051bbdceefSHannes Frederic Sowa 4061bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4071bbdceefSHannes Frederic Sowa 40865cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4091bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 41065cd8033SHannes Frederic Sowa } 41165cd8033SHannes Frederic Sowa 412a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk, 4137629c73aSJakub Sitnicki struct sk_buff *skb, 4147629c73aSJakub Sitnicki __be32 saddr, __be16 sport, 4157629c73aSJakub Sitnicki __be32 daddr, unsigned short hnum) 4167629c73aSJakub Sitnicki { 4177629c73aSJakub Sitnicki struct sock *reuse_sk = NULL; 4187629c73aSJakub Sitnicki u32 hash; 4197629c73aSJakub Sitnicki 4207629c73aSJakub Sitnicki if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 4217629c73aSJakub Sitnicki hash = udp_ehashfn(net, daddr, hnum, saddr, sport); 4227629c73aSJakub Sitnicki reuse_sk = reuseport_select_sock(sk, hash, skb, 4237629c73aSJakub Sitnicki sizeof(struct udphdr)); 4247629c73aSJakub Sitnicki } 4257629c73aSJakub Sitnicki return reuse_sk; 4267629c73aSJakub Sitnicki } 4277629c73aSJakub Sitnicki 428d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4295051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4305051ebd2SEric Dumazet __be32 saddr, __be16 sport, 431fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 43273545373STim Beale int dif, int sdif, 433d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4341134158bSCraig Gallek struct sk_buff *skb) 4355051ebd2SEric Dumazet { 4365051ebd2SEric Dumazet struct sock *sk, *result; 437e94a62f5SPaolo Abeni int score, badness; 4385051ebd2SEric Dumazet 4395051ebd2SEric Dumazet result = NULL; 440ba418fa3STom Herbert badness = 0; 441ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 442d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 44373545373STim Beale daddr, hnum, dif, sdif); 4445051ebd2SEric Dumazet if (score > badness) { 4457629c73aSJakub Sitnicki result = lookup_reuseport(net, sk, skb, 4467629c73aSJakub Sitnicki saddr, sport, daddr, hnum); 447a57066b1SDavid S. Miller /* Fall back to scoring if group has connections */ 448a57066b1SDavid S. Miller if (result && !reuseport_has_conns(sk, false)) 449ca065d0cSEric Dumazet return result; 4507629c73aSJakub Sitnicki 451a57066b1SDavid S. Miller result = result ? : sk; 452ca065d0cSEric Dumazet badness = score; 4535051ebd2SEric Dumazet } 4545051ebd2SEric Dumazet } 4555051ebd2SEric Dumazet return result; 4565051ebd2SEric Dumazet } 4575051ebd2SEric Dumazet 458a57066b1SDavid S. Miller static struct sock *udp4_lookup_run_bpf(struct net *net, 45972f7e944SJakub Sitnicki struct udp_table *udptable, 46072f7e944SJakub Sitnicki struct sk_buff *skb, 46172f7e944SJakub Sitnicki __be32 saddr, __be16 sport, 46272f7e944SJakub Sitnicki __be32 daddr, u16 hnum) 46372f7e944SJakub Sitnicki { 46472f7e944SJakub Sitnicki struct sock *sk, *reuse_sk; 46572f7e944SJakub Sitnicki bool no_reuseport; 46672f7e944SJakub Sitnicki 46772f7e944SJakub Sitnicki if (udptable != &udp_table) 46872f7e944SJakub Sitnicki return NULL; /* only UDP is supported */ 46972f7e944SJakub Sitnicki 47072f7e944SJakub Sitnicki no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP, 47172f7e944SJakub Sitnicki saddr, sport, daddr, hnum, &sk); 47272f7e944SJakub Sitnicki if (no_reuseport || IS_ERR_OR_NULL(sk)) 47372f7e944SJakub Sitnicki return sk; 47472f7e944SJakub Sitnicki 47572f7e944SJakub Sitnicki reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); 476c64c9c28SJakub Sitnicki if (reuse_sk) 47772f7e944SJakub Sitnicki sk = reuse_sk; 47872f7e944SJakub Sitnicki return sk; 47972f7e944SJakub Sitnicki } 48072f7e944SJakub Sitnicki 481db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 482db8dac20SDavid S. Miller * harder than this. -DaveM 483db8dac20SDavid S. Miller */ 484fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 485fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 486fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 487db8dac20SDavid S. Miller { 488db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4894cdeeee9SPeter Oskolkov unsigned int hash2, slot2; 4904cdeeee9SPeter Oskolkov struct udp_hslot *hslot2; 49172f7e944SJakub Sitnicki struct sock *result, *sk; 492db8dac20SDavid S. Miller 493f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 4945051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4955051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4965051ebd2SEric Dumazet 49772f7e944SJakub Sitnicki /* Lookup connected or non-wildcard socket */ 4985051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 499fb74c277SDavid Ahern daddr, hnum, dif, sdif, 50073545373STim Beale hslot2, skb); 50172f7e944SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 50272f7e944SJakub Sitnicki goto done; 50372f7e944SJakub Sitnicki 50472f7e944SJakub Sitnicki /* Lookup redirect from BPF */ 50572f7e944SJakub Sitnicki if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 50672f7e944SJakub Sitnicki sk = udp4_lookup_run_bpf(net, udptable, skb, 50772f7e944SJakub Sitnicki saddr, sport, daddr, hnum); 50872f7e944SJakub Sitnicki if (sk) { 50972f7e944SJakub Sitnicki result = sk; 51072f7e944SJakub Sitnicki goto done; 51172f7e944SJakub Sitnicki } 51272f7e944SJakub Sitnicki } 51372f7e944SJakub Sitnicki 51472f7e944SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 51572f7e944SJakub Sitnicki if (result) 51672f7e944SJakub Sitnicki goto done; 51772f7e944SJakub Sitnicki 51872f7e944SJakub Sitnicki /* Lookup wildcard sockets */ 519f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 5205051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 5215051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 5225051ebd2SEric Dumazet 5231223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 5244cdeeee9SPeter Oskolkov htonl(INADDR_ANY), hnum, dif, sdif, 52573545373STim Beale hslot2, skb); 52672f7e944SJakub Sitnicki done: 52788e235b8SEnrico Weigelt if (IS_ERR(result)) 5288217ca65SMartin KaFai Lau return NULL; 5295051ebd2SEric Dumazet return result; 5305051ebd2SEric Dumazet } 531fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 532db8dac20SDavid S. Miller 533607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 534607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 535645ca708SEric Dumazet struct udp_table *udptable) 536607c4aafSKOVACS Krisztian { 537607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 538607c4aafSKOVACS Krisztian 539ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 540607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 541fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 542607c4aafSKOVACS Krisztian } 543607c4aafSKOVACS Krisztian 54463058308STom Herbert struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, 54563058308STom Herbert __be16 sport, __be16 dport) 54663058308STom Herbert { 547257a525fSMartin KaFai Lau const struct iphdr *iph = ip_hdr(skb); 548257a525fSMartin KaFai Lau 549257a525fSMartin KaFai Lau return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 550257a525fSMartin KaFai Lau iph->daddr, dport, inet_iif(skb), 551257a525fSMartin KaFai Lau inet_sdif(skb), &udp_table, NULL); 55263058308STom Herbert } 55363058308STom Herbert EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); 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 600e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 601e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 602e7cc0824SStefano Brivio */ 603e7cc0824SStefano Brivio static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 604e7cc0824SStefano Brivio { 605e7cc0824SStefano Brivio int i; 606e7cc0824SStefano Brivio 607e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 608e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, u32 info); 60992b95364SPaolo Abeni const struct ip_tunnel_encap_ops *encap; 610e7cc0824SStefano Brivio 61192b95364SPaolo Abeni encap = rcu_dereference(iptun_encaps[i]); 61292b95364SPaolo Abeni if (!encap) 613e7cc0824SStefano Brivio continue; 61492b95364SPaolo Abeni handler = encap->err_handler; 615e7cc0824SStefano Brivio if (handler && !handler(skb, info)) 616e7cc0824SStefano Brivio return 0; 617e7cc0824SStefano Brivio } 618e7cc0824SStefano Brivio 619e7cc0824SStefano Brivio return -ENOENT; 620e7cc0824SStefano Brivio } 621e7cc0824SStefano Brivio 622a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 623a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 624a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 625a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 626a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 627a36e185eSStefano Brivio * trace ICMP messages back to them. 628a36e185eSStefano Brivio * 629e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 630e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 631e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 632e7cc0824SStefano Brivio * 633a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 634a36e185eSStefano Brivio * association. 635a36e185eSStefano Brivio * 636e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 637e7cc0824SStefano Brivio * processing, zero otherwise. 638a36e185eSStefano Brivio */ 639a36e185eSStefano Brivio static struct sock *__udp4_lib_err_encap(struct net *net, 640a36e185eSStefano Brivio const struct iphdr *iph, 641a36e185eSStefano Brivio struct udphdr *uh, 642a36e185eSStefano Brivio struct udp_table *udptable, 643e7cc0824SStefano Brivio struct sk_buff *skb, u32 info) 644a36e185eSStefano Brivio { 645a36e185eSStefano Brivio int network_offset, transport_offset; 646a36e185eSStefano Brivio struct sock *sk; 647a36e185eSStefano Brivio 648a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 649a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 650a36e185eSStefano Brivio 651a36e185eSStefano Brivio /* Network header needs to point to the outer IPv4 header inside ICMP */ 652a36e185eSStefano Brivio skb_reset_network_header(skb); 653a36e185eSStefano Brivio 654a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 655a36e185eSStefano Brivio skb_set_transport_header(skb, iph->ihl << 2); 656a36e185eSStefano Brivio 657e7cc0824SStefano Brivio sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 658e7cc0824SStefano Brivio iph->saddr, uh->dest, skb->dev->ifindex, 0, 659e7cc0824SStefano Brivio udptable, NULL); 660e7cc0824SStefano Brivio if (sk) { 661e7cc0824SStefano Brivio int (*lookup)(struct sock *sk, struct sk_buff *skb); 662e7cc0824SStefano Brivio struct udp_sock *up = udp_sk(sk); 663e7cc0824SStefano Brivio 664a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 665a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 666a36e185eSStefano Brivio sk = NULL; 667e7cc0824SStefano Brivio } 668e7cc0824SStefano Brivio 669e7cc0824SStefano Brivio if (!sk) 670e7cc0824SStefano Brivio sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 671a36e185eSStefano Brivio 672a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 673a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 674a36e185eSStefano Brivio 675a36e185eSStefano Brivio return sk; 676a36e185eSStefano Brivio } 677a36e185eSStefano Brivio 678db8dac20SDavid S. Miller /* 679db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 680db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 681db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 682db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 683db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 684db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 685db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 686db8dac20SDavid S. Miller * to find the appropriate port. 687db8dac20SDavid S. Miller */ 688db8dac20SDavid S. Miller 68932bbd879SStefano Brivio int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 690db8dac20SDavid S. Miller { 691db8dac20SDavid S. Miller struct inet_sock *inet; 692b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 693db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 694db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 695db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 696a36e185eSStefano Brivio bool tunnel = false; 697db8dac20SDavid S. Miller struct sock *sk; 698db8dac20SDavid S. Miller int harderr; 699db8dac20SDavid S. Miller int err; 700fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 701db8dac20SDavid S. Miller 702fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 703f64bf6b8SMike Manning iph->saddr, uh->source, skb->dev->ifindex, 704f64bf6b8SMike Manning inet_sdif(skb), udptable, NULL); 70551456b29SIan Morris if (!sk) { 706a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 707e7cc0824SStefano Brivio sk = ERR_PTR(-ENOENT); 708e7cc0824SStefano Brivio if (static_branch_unlikely(&udp_encap_needed_key)) { 709e7cc0824SStefano Brivio sk = __udp4_lib_err_encap(net, iph, uh, udptable, skb, 710e7cc0824SStefano Brivio info); 711e7cc0824SStefano Brivio if (!sk) 712e7cc0824SStefano Brivio return 0; 713a36e185eSStefano Brivio } 714e7cc0824SStefano Brivio 715e7cc0824SStefano Brivio if (IS_ERR(sk)) { 716e7cc0824SStefano Brivio __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 717e7cc0824SStefano Brivio return PTR_ERR(sk); 718e7cc0824SStefano Brivio } 719e7cc0824SStefano Brivio 720a36e185eSStefano Brivio tunnel = true; 721db8dac20SDavid S. Miller } 722db8dac20SDavid S. Miller 723db8dac20SDavid S. Miller err = 0; 724db8dac20SDavid S. Miller harderr = 0; 725db8dac20SDavid S. Miller inet = inet_sk(sk); 726db8dac20SDavid S. Miller 727db8dac20SDavid S. Miller switch (type) { 728db8dac20SDavid S. Miller default: 729db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 730db8dac20SDavid S. Miller err = EHOSTUNREACH; 731db8dac20SDavid S. Miller break; 732db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 733db8dac20SDavid S. Miller goto out; 734db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 735db8dac20SDavid S. Miller err = EPROTO; 736db8dac20SDavid S. Miller harderr = 1; 737db8dac20SDavid S. Miller break; 738db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 739db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 74036393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 741db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 742db8dac20SDavid S. Miller err = EMSGSIZE; 743db8dac20SDavid S. Miller harderr = 1; 744db8dac20SDavid S. Miller break; 745db8dac20SDavid S. Miller } 746db8dac20SDavid S. Miller goto out; 747db8dac20SDavid S. Miller } 748db8dac20SDavid S. Miller err = EHOSTUNREACH; 749db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 750db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 751db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 752db8dac20SDavid S. Miller } 753db8dac20SDavid S. Miller break; 75455be7a9cSDavid S. Miller case ICMP_REDIRECT: 75555be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 7561a462d18SDuan Jiong goto out; 757db8dac20SDavid S. Miller } 758db8dac20SDavid S. Miller 759db8dac20SDavid S. Miller /* 760db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 761db8dac20SDavid S. Miller * 4.1.3.3. 762db8dac20SDavid S. Miller */ 763a36e185eSStefano Brivio if (tunnel) { 764a36e185eSStefano Brivio /* ...not for tunnels though: we don't have a sending socket */ 765a36e185eSStefano Brivio goto out; 766a36e185eSStefano Brivio } 767db8dac20SDavid S. Miller if (!inet->recverr) { 768db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 769db8dac20SDavid S. Miller goto out; 770b1faf566SEric Dumazet } else 771db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 772b1faf566SEric Dumazet 773db8dac20SDavid S. Miller sk->sk_err = err; 774db8dac20SDavid S. Miller sk->sk_error_report(sk); 775db8dac20SDavid S. Miller out: 77632bbd879SStefano Brivio return 0; 777db8dac20SDavid S. Miller } 778db8dac20SDavid S. Miller 77932bbd879SStefano Brivio int udp_err(struct sk_buff *skb, u32 info) 780db8dac20SDavid S. Miller { 78132bbd879SStefano Brivio return __udp4_lib_err(skb, info, &udp_table); 782db8dac20SDavid S. Miller } 783db8dac20SDavid S. Miller 784db8dac20SDavid S. Miller /* 785db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 786db8dac20SDavid S. Miller */ 78736d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 788db8dac20SDavid S. Miller { 789db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 790db8dac20SDavid S. Miller 791db8dac20SDavid S. Miller if (up->pending) { 792db8dac20SDavid S. Miller up->len = 0; 793db8dac20SDavid S. Miller up->pending = 0; 794db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 795db8dac20SDavid S. Miller } 796db8dac20SDavid S. Miller } 79736d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 798db8dac20SDavid S. Miller 799db8dac20SDavid S. Miller /** 800f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 801db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 802db8dac20SDavid S. Miller * (checksum field must be zeroed out) 803f6b9664fSHerbert Xu * @src: source IP address 804f6b9664fSHerbert Xu * @dst: destination IP address 805db8dac20SDavid S. Miller */ 806c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 807db8dac20SDavid S. Miller { 808db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 809f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 810f6b9664fSHerbert Xu int len = skb->len - offset; 811f6b9664fSHerbert Xu int hlen = len; 812db8dac20SDavid S. Miller __wsum csum = 0; 813db8dac20SDavid S. Miller 814ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 815db8dac20SDavid S. Miller /* 816db8dac20SDavid S. Miller * Only one fragment on the socket. 817db8dac20SDavid S. Miller */ 818db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 819db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 820f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 821f6b9664fSHerbert Xu IPPROTO_UDP, 0); 822db8dac20SDavid S. Miller } else { 823ebbe495fSWANG Cong struct sk_buff *frags; 824ebbe495fSWANG Cong 825db8dac20SDavid S. Miller /* 826db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 827db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 828db8dac20SDavid S. Miller * should be together 829db8dac20SDavid S. Miller */ 830ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 831f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 832f6b9664fSHerbert Xu hlen -= frags->len; 833ebbe495fSWANG Cong } 834db8dac20SDavid S. Miller 835f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 836db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 837db8dac20SDavid S. Miller 838db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 839db8dac20SDavid S. Miller if (uh->check == 0) 840db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 841db8dac20SDavid S. Miller } 842db8dac20SDavid S. Miller } 843c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 844db8dac20SDavid S. Miller 845af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 846af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 847af5fcba7STom Herbert */ 848af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 849af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 850af5fcba7STom Herbert { 851af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 852af5fcba7STom Herbert 853179bc67fSEdward Cree if (nocheck) { 854af5fcba7STom Herbert uh->check = 0; 855179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 856af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 857179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 858179bc67fSEdward Cree uh->check = 0; 859179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 860179bc67fSEdward Cree if (uh->check == 0) 861179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 862d75f1306SEdward Cree } else { 863af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 864af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 865af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 866af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 867af5fcba7STom Herbert } 868af5fcba7STom Herbert } 869af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 870af5fcba7STom Herbert 871bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 872bec1f6f6SWillem de Bruijn struct inet_cork *cork) 873f6b9664fSHerbert Xu { 874f6b9664fSHerbert Xu struct sock *sk = skb->sk; 875f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 876f6b9664fSHerbert Xu struct udphdr *uh; 877f6b9664fSHerbert Xu int err = 0; 878f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 879f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 880f6b9664fSHerbert Xu int len = skb->len - offset; 8814094871dSJosh Hunt int datalen = len - sizeof(*uh); 882f6b9664fSHerbert Xu __wsum csum = 0; 883f6b9664fSHerbert Xu 884f6b9664fSHerbert Xu /* 885f6b9664fSHerbert Xu * Create a UDP header 886f6b9664fSHerbert Xu */ 887f6b9664fSHerbert Xu uh = udp_hdr(skb); 888f6b9664fSHerbert Xu uh->source = inet->inet_sport; 88979ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 890f6b9664fSHerbert Xu uh->len = htons(len); 891f6b9664fSHerbert Xu uh->check = 0; 892f6b9664fSHerbert Xu 893bec1f6f6SWillem de Bruijn if (cork->gso_size) { 894bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 895bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 896bec1f6f6SWillem de Bruijn 8970f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 8980f149c9fSWillem de Bruijn kfree_skb(skb); 899bec1f6f6SWillem de Bruijn return -EINVAL; 9000f149c9fSWillem de Bruijn } 9010f149c9fSWillem de Bruijn if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) { 9020f149c9fSWillem de Bruijn kfree_skb(skb); 903bec1f6f6SWillem de Bruijn return -EINVAL; 9040f149c9fSWillem de Bruijn } 9050f149c9fSWillem de Bruijn if (sk->sk_no_check_tx) { 9060f149c9fSWillem de Bruijn kfree_skb(skb); 907a8c744a8SWillem de Bruijn return -EINVAL; 9080f149c9fSWillem de Bruijn } 909ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 9100f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 9110f149c9fSWillem de Bruijn kfree_skb(skb); 912bec1f6f6SWillem de Bruijn return -EIO; 9130f149c9fSWillem de Bruijn } 914bec1f6f6SWillem de Bruijn 9154094871dSJosh Hunt if (datalen > cork->gso_size) { 916bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 917bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 9184094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 919dfec0ee2SAlexander Duyck cork->gso_size); 9204094871dSJosh Hunt } 921a8c744a8SWillem de Bruijn goto csum_partial; 922bec1f6f6SWillem de Bruijn } 923bec1f6f6SWillem de Bruijn 924f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 925f6b9664fSHerbert Xu csum = udplite_csum(skb); 926f6b9664fSHerbert Xu 927ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 928f6b9664fSHerbert Xu 929f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 930f6b9664fSHerbert Xu goto send; 931f6b9664fSHerbert Xu 932f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 933a8c744a8SWillem de Bruijn csum_partial: 934f6b9664fSHerbert Xu 93579ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 936f6b9664fSHerbert Xu goto send; 937f6b9664fSHerbert Xu 938f6b9664fSHerbert Xu } else 939f6b9664fSHerbert Xu csum = udp_csum(skb); 940f6b9664fSHerbert Xu 941f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 94279ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 943f6b9664fSHerbert Xu sk->sk_protocol, csum); 944f6b9664fSHerbert Xu if (uh->check == 0) 945f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 946f6b9664fSHerbert Xu 947f6b9664fSHerbert Xu send: 948b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 949f6b9664fSHerbert Xu if (err) { 950f6b9664fSHerbert Xu if (err == -ENOBUFS && !inet->recverr) { 9516aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 952f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 953f6b9664fSHerbert Xu err = 0; 954f6b9664fSHerbert Xu } 955f6b9664fSHerbert Xu } else 9566aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 957f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 958f6b9664fSHerbert Xu return err; 959f6b9664fSHerbert Xu } 960f6b9664fSHerbert Xu 961db8dac20SDavid S. Miller /* 962db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 963db8dac20SDavid S. Miller */ 9648822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 965db8dac20SDavid S. Miller { 966db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 967db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 968b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 969db8dac20SDavid S. Miller struct sk_buff *skb; 970db8dac20SDavid S. Miller int err = 0; 971db8dac20SDavid S. Miller 97277968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 973f6b9664fSHerbert Xu if (!skb) 974db8dac20SDavid S. Miller goto out; 975db8dac20SDavid S. Miller 976bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &inet->cork.base); 977db8dac20SDavid S. Miller 978db8dac20SDavid S. Miller out: 979db8dac20SDavid S. Miller up->len = 0; 980db8dac20SDavid S. Miller up->pending = 0; 981db8dac20SDavid S. Miller return err; 982db8dac20SDavid S. Miller } 9838822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 984db8dac20SDavid S. Miller 9852e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 9862e8de857SWillem de Bruijn { 9872e8de857SWillem de Bruijn switch (cmsg->cmsg_type) { 9882e8de857SWillem de Bruijn case UDP_SEGMENT: 9892e8de857SWillem de Bruijn if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 9902e8de857SWillem de Bruijn return -EINVAL; 9912e8de857SWillem de Bruijn *gso_size = *(__u16 *)CMSG_DATA(cmsg); 9922e8de857SWillem de Bruijn return 0; 9932e8de857SWillem de Bruijn default: 9942e8de857SWillem de Bruijn return -EINVAL; 9952e8de857SWillem de Bruijn } 9962e8de857SWillem de Bruijn } 9972e8de857SWillem de Bruijn 9982e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 9992e8de857SWillem de Bruijn { 10002e8de857SWillem de Bruijn struct cmsghdr *cmsg; 10012e8de857SWillem de Bruijn bool need_ip = false; 10022e8de857SWillem de Bruijn int err; 10032e8de857SWillem de Bruijn 10042e8de857SWillem de Bruijn for_each_cmsghdr(cmsg, msg) { 10052e8de857SWillem de Bruijn if (!CMSG_OK(msg, cmsg)) 10062e8de857SWillem de Bruijn return -EINVAL; 10072e8de857SWillem de Bruijn 10082e8de857SWillem de Bruijn if (cmsg->cmsg_level != SOL_UDP) { 10092e8de857SWillem de Bruijn need_ip = true; 10102e8de857SWillem de Bruijn continue; 10112e8de857SWillem de Bruijn } 10122e8de857SWillem de Bruijn 10132e8de857SWillem de Bruijn err = __udp_cmsg_send(cmsg, gso_size); 10142e8de857SWillem de Bruijn if (err) 10152e8de857SWillem de Bruijn return err; 10162e8de857SWillem de Bruijn } 10172e8de857SWillem de Bruijn 10182e8de857SWillem de Bruijn return need_ip; 10192e8de857SWillem de Bruijn } 10202e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send); 10212e8de857SWillem de Bruijn 10221b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1023db8dac20SDavid S. Miller { 1024db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1025db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 10261cedee13SAndrey Ignatov DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 1027e474995fSDavid S. Miller struct flowi4 fl4_stack; 1028b6f21b26SDavid S. Miller struct flowi4 *fl4; 1029db8dac20SDavid S. Miller int ulen = len; 1030db8dac20SDavid S. Miller struct ipcm_cookie ipc; 1031db8dac20SDavid S. Miller struct rtable *rt = NULL; 1032db8dac20SDavid S. Miller int free = 0; 1033db8dac20SDavid S. Miller int connected = 0; 1034db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 1035db8dac20SDavid S. Miller __be16 dport; 1036db8dac20SDavid S. Miller u8 tos; 1037db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 1038db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 1039db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1040903ab86dSHerbert Xu struct sk_buff *skb; 1041f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 1042db8dac20SDavid S. Miller 1043db8dac20SDavid S. Miller if (len > 0xFFFF) 1044db8dac20SDavid S. Miller return -EMSGSIZE; 1045db8dac20SDavid S. Miller 1046db8dac20SDavid S. Miller /* 1047db8dac20SDavid S. Miller * Check the flags. 1048db8dac20SDavid S. Miller */ 1049db8dac20SDavid S. Miller 1050db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 1051db8dac20SDavid S. Miller return -EOPNOTSUPP; 1052db8dac20SDavid S. Miller 1053903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1054903ab86dSHerbert Xu 1055f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1056db8dac20SDavid S. Miller if (up->pending) { 1057db8dac20SDavid S. Miller /* 1058db8dac20SDavid S. Miller * There are pending frames. 1059db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1060db8dac20SDavid S. Miller */ 1061db8dac20SDavid S. Miller lock_sock(sk); 1062db8dac20SDavid S. Miller if (likely(up->pending)) { 1063db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 1064db8dac20SDavid S. Miller release_sock(sk); 1065db8dac20SDavid S. Miller return -EINVAL; 1066db8dac20SDavid S. Miller } 1067db8dac20SDavid S. Miller goto do_append_data; 1068db8dac20SDavid S. Miller } 1069db8dac20SDavid S. Miller release_sock(sk); 1070db8dac20SDavid S. Miller } 1071db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1072db8dac20SDavid S. Miller 1073db8dac20SDavid S. Miller /* 1074db8dac20SDavid S. Miller * Get and verify the address. 1075db8dac20SDavid S. Miller */ 10761cedee13SAndrey Ignatov if (usin) { 1077db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 1078db8dac20SDavid S. Miller return -EINVAL; 1079db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 1080db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 1081db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1082db8dac20SDavid S. Miller } 1083db8dac20SDavid S. Miller 1084db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 1085db8dac20SDavid S. Miller dport = usin->sin_port; 1086db8dac20SDavid S. Miller if (dport == 0) 1087db8dac20SDavid S. Miller return -EINVAL; 1088db8dac20SDavid S. Miller } else { 1089db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1090db8dac20SDavid S. Miller return -EDESTADDRREQ; 1091c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1092c720c7e8SEric Dumazet dport = inet->inet_dport; 1093db8dac20SDavid S. Miller /* Open fast path for connected socket. 1094db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 1095db8dac20SDavid S. Miller */ 1096db8dac20SDavid S. Miller connected = 1; 1097db8dac20SDavid S. Miller } 1098c14ac945SSoheil Hassas Yeganeh 109935178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 1100bec1f6f6SWillem de Bruijn ipc.gso_size = up->gso_size; 1101bf84a010SDaniel Borkmann 1102db8dac20SDavid S. Miller if (msg->msg_controllen) { 11032e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc.gso_size); 11042e8de857SWillem de Bruijn if (err > 0) 11052e8de857SWillem de Bruijn err = ip_cmsg_send(sk, msg, &ipc, 11062e8de857SWillem de Bruijn sk->sk_family == AF_INET6); 11072e8de857SWillem de Bruijn if (unlikely(err < 0)) { 110891948309SEric Dumazet kfree(ipc.opt); 1109db8dac20SDavid S. Miller return err; 111091948309SEric Dumazet } 1111db8dac20SDavid S. Miller if (ipc.opt) 1112db8dac20SDavid S. Miller free = 1; 1113db8dac20SDavid S. Miller connected = 0; 1114db8dac20SDavid S. Miller } 1115f6d8bd05SEric Dumazet if (!ipc.opt) { 1116f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 1117f6d8bd05SEric Dumazet 1118f6d8bd05SEric Dumazet rcu_read_lock(); 1119f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1120f6d8bd05SEric Dumazet if (inet_opt) { 1121f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 1122f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 1123f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 1124f6d8bd05SEric Dumazet } 1125f6d8bd05SEric Dumazet rcu_read_unlock(); 1126f6d8bd05SEric Dumazet } 1127db8dac20SDavid S. Miller 11281cedee13SAndrey Ignatov if (cgroup_bpf_enabled && !connected) { 11291cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 11301cedee13SAndrey Ignatov (struct sockaddr *)usin, &ipc.addr); 11311cedee13SAndrey Ignatov if (err) 11321cedee13SAndrey Ignatov goto out_free; 11331cedee13SAndrey Ignatov if (usin) { 11341cedee13SAndrey Ignatov if (usin->sin_port == 0) { 11351cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 11361cedee13SAndrey Ignatov err = -EINVAL; 11371cedee13SAndrey Ignatov goto out_free; 11381cedee13SAndrey Ignatov } 11391cedee13SAndrey Ignatov daddr = usin->sin_addr.s_addr; 11401cedee13SAndrey Ignatov dport = usin->sin_port; 11411cedee13SAndrey Ignatov } 11421cedee13SAndrey Ignatov } 11431cedee13SAndrey Ignatov 1144db8dac20SDavid S. Miller saddr = ipc.addr; 1145db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 1146db8dac20SDavid S. Miller 1147f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 11481b97013bSAndrey Ignatov if (!daddr) { 11491b97013bSAndrey Ignatov err = -EINVAL; 11501b97013bSAndrey Ignatov goto out_free; 11511b97013bSAndrey Ignatov } 1152f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 1153db8dac20SDavid S. Miller connected = 0; 1154db8dac20SDavid S. Miller } 1155aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 1156db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 1157db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 1158f6d8bd05SEric Dumazet (ipc.opt && ipc.opt->opt.is_strictroute)) { 1159db8dac20SDavid S. Miller tos |= RTO_ONLINK; 1160db8dac20SDavid S. Miller connected = 0; 1161db8dac20SDavid S. Miller } 1162db8dac20SDavid S. Miller 1163db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 1164854da991SRobert Shearman if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 1165db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 1166db8dac20SDavid S. Miller if (!saddr) 1167db8dac20SDavid S. Miller saddr = inet->mc_addr; 1168db8dac20SDavid S. Miller connected = 0; 11699515a2e0SDavid Ahern } else if (!ipc.oif) { 117076e21053SErich E. Hoover ipc.oif = inet->uc_index; 11719515a2e0SDavid Ahern } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 11729515a2e0SDavid Ahern /* oif is set, packet is to local broadcast and 11732bdcc73cSRandy Dunlap * uc_index is set. oif is most likely set 11749515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 11759515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 11769515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 11779515a2e0SDavid Ahern */ 11789515a2e0SDavid Ahern if (ipc.oif != inet->uc_index && 11799515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 11809515a2e0SDavid Ahern inet->uc_index)) { 11819515a2e0SDavid Ahern ipc.oif = inet->uc_index; 11829515a2e0SDavid Ahern } 11839515a2e0SDavid Ahern } 1184db8dac20SDavid S. Miller 1185db8dac20SDavid S. Miller if (connected) 1186db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 1187db8dac20SDavid S. Miller 118851456b29SIan Morris if (!rt) { 118984a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 11909a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 119184a3aa00SPavel Emelyanov 1192e474995fSDavid S. Miller fl4 = &fl4_stack; 11939a24abfaSDavid Ahern 1194c6af0c22SWillem de Bruijn flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, 11959a24abfaSDavid Ahern RT_SCOPE_UNIVERSE, sk->sk_protocol, 11969a24abfaSDavid Ahern flow_flags, 1197e2d118a1SLorenzo Colitti faddr, saddr, dport, inet->inet_sport, 1198e2d118a1SLorenzo Colitti sk->sk_uid); 1199c0951cbcSDavid S. Miller 1200e474995fSDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 1201e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1202b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1203b23dd4feSDavid S. Miller err = PTR_ERR(rt); 120406dc94b1SDavid S. Miller rt = NULL; 1205db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1206f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1207db8dac20SDavid S. Miller goto out; 1208db8dac20SDavid S. Miller } 1209db8dac20SDavid S. Miller 1210db8dac20SDavid S. Miller err = -EACCES; 1211db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1212db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1213db8dac20SDavid S. Miller goto out; 1214db8dac20SDavid S. Miller if (connected) 1215d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1216db8dac20SDavid S. Miller } 1217db8dac20SDavid S. Miller 1218db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1219db8dac20SDavid S. Miller goto do_confirm; 1220db8dac20SDavid S. Miller back_from_confirm: 1221db8dac20SDavid S. Miller 1222e474995fSDavid S. Miller saddr = fl4->saddr; 1223db8dac20SDavid S. Miller if (!ipc.addr) 1224e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1225db8dac20SDavid S. Miller 1226903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1227903ab86dSHerbert Xu if (!corkreq) { 12281cd7884dSWillem de Bruijn struct inet_cork cork; 12291cd7884dSWillem de Bruijn 1230f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1231903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 12321cd7884dSWillem de Bruijn &cork, msg->msg_flags); 1233903ab86dSHerbert Xu err = PTR_ERR(skb); 123450c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 1235bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &cork); 1236903ab86dSHerbert Xu goto out; 1237903ab86dSHerbert Xu } 1238903ab86dSHerbert Xu 1239db8dac20SDavid S. Miller lock_sock(sk); 1240db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1241db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1242db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1243db8dac20SDavid S. Miller release_sock(sk); 1244db8dac20SDavid S. Miller 1245197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1246db8dac20SDavid S. Miller err = -EINVAL; 1247db8dac20SDavid S. Miller goto out; 1248db8dac20SDavid S. Miller } 1249db8dac20SDavid S. Miller /* 1250db8dac20SDavid S. Miller * Now cork the socket to pend data. 1251db8dac20SDavid S. Miller */ 1252b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1253b6f21b26SDavid S. Miller fl4->daddr = daddr; 1254b6f21b26SDavid S. Miller fl4->saddr = saddr; 12559cce96dfSDavid S. Miller fl4->fl4_dport = dport; 12569cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1257db8dac20SDavid S. Miller up->pending = AF_INET; 1258db8dac20SDavid S. Miller 1259db8dac20SDavid S. Miller do_append_data: 1260db8dac20SDavid S. Miller up->len += ulen; 1261f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 12622e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1263db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1264db8dac20SDavid S. Miller if (err) 1265db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1266db8dac20SDavid S. Miller else if (!corkreq) 1267db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1268db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1269db8dac20SDavid S. Miller up->pending = 0; 1270db8dac20SDavid S. Miller release_sock(sk); 1271db8dac20SDavid S. Miller 1272db8dac20SDavid S. Miller out: 1273db8dac20SDavid S. Miller ip_rt_put(rt); 12741b97013bSAndrey Ignatov out_free: 1275db8dac20SDavid S. Miller if (free) 1276db8dac20SDavid S. Miller kfree(ipc.opt); 1277db8dac20SDavid S. Miller if (!err) 1278db8dac20SDavid S. Miller return len; 1279db8dac20SDavid S. Miller /* 1280db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1281db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1282db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1283db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1284db8dac20SDavid S. Miller * seems like overkill. 1285db8dac20SDavid S. Miller */ 1286db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 12876aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1288629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1289db8dac20SDavid S. Miller } 1290db8dac20SDavid S. Miller return err; 1291db8dac20SDavid S. Miller 1292db8dac20SDavid S. Miller do_confirm: 12930dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 12940dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1295db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1296db8dac20SDavid S. Miller goto back_from_confirm; 1297db8dac20SDavid S. Miller err = 0; 1298db8dac20SDavid S. Miller goto out; 1299db8dac20SDavid S. Miller } 1300c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1301db8dac20SDavid S. Miller 1302db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 1303db8dac20SDavid S. Miller size_t size, int flags) 1304db8dac20SDavid S. Miller { 1305f5fca608SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1306db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1307db8dac20SDavid S. Miller int ret; 1308db8dac20SDavid S. Miller 1309d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 1310d3f7d56aSShawn Landden flags |= MSG_MORE; 1311d3f7d56aSShawn Landden 1312db8dac20SDavid S. Miller if (!up->pending) { 1313db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1314db8dac20SDavid S. Miller 1315db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 1316db8dac20SDavid S. Miller * sendpage interface can't pass. 1317db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 1318db8dac20SDavid S. Miller */ 13191b784140SYing Xue ret = udp_sendmsg(sk, &msg, 0); 1320db8dac20SDavid S. Miller if (ret < 0) 1321db8dac20SDavid S. Miller return ret; 1322db8dac20SDavid S. Miller } 1323db8dac20SDavid S. Miller 1324db8dac20SDavid S. Miller lock_sock(sk); 1325db8dac20SDavid S. Miller 1326db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 1327db8dac20SDavid S. Miller release_sock(sk); 1328db8dac20SDavid S. Miller 1329197df02cSMatteo Croce net_dbg_ratelimited("cork failed\n"); 1330db8dac20SDavid S. Miller return -EINVAL; 1331db8dac20SDavid S. Miller } 1332db8dac20SDavid S. Miller 1333f5fca608SDavid S. Miller ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1334f5fca608SDavid S. Miller page, offset, size, flags); 1335db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 1336db8dac20SDavid S. Miller release_sock(sk); 1337db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 1338db8dac20SDavid S. Miller size, flags); 1339db8dac20SDavid S. Miller } 1340db8dac20SDavid S. Miller if (ret < 0) { 1341db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1342db8dac20SDavid S. Miller goto out; 1343db8dac20SDavid S. Miller } 1344db8dac20SDavid S. Miller 1345db8dac20SDavid S. Miller up->len += size; 1346db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 1347db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 1348db8dac20SDavid S. Miller if (!ret) 1349db8dac20SDavid S. Miller ret = size; 1350db8dac20SDavid S. Miller out: 1351db8dac20SDavid S. Miller release_sock(sk); 1352db8dac20SDavid S. Miller return ret; 1353db8dac20SDavid S. Miller } 1354db8dac20SDavid S. Miller 1355dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1356dce4551cSPaolo Abeni 1357677bf08cSFlorian Westphal /* all head states (dst, sk, nf conntrack) except skb extensions are 1358677bf08cSFlorian Westphal * cleared by udp_rcv(). 1359677bf08cSFlorian Westphal * 1360677bf08cSFlorian Westphal * We need to preserve secpath, if present, to eventually process 1361677bf08cSFlorian Westphal * IP_CMSG_PASSSEC at recvmsg() time. 1362677bf08cSFlorian Westphal * 1363677bf08cSFlorian Westphal * Other extensions can be cleared. 1364677bf08cSFlorian Westphal */ 1365677bf08cSFlorian Westphal static bool udp_try_make_stateless(struct sk_buff *skb) 1366677bf08cSFlorian Westphal { 1367677bf08cSFlorian Westphal if (!skb_has_extensions(skb)) 1368677bf08cSFlorian Westphal return true; 1369677bf08cSFlorian Westphal 1370677bf08cSFlorian Westphal if (!secpath_exists(skb)) { 1371677bf08cSFlorian Westphal skb_ext_reset(skb); 1372677bf08cSFlorian Westphal return true; 1373677bf08cSFlorian Westphal } 1374677bf08cSFlorian Westphal 1375677bf08cSFlorian Westphal return false; 1376677bf08cSFlorian Westphal } 1377677bf08cSFlorian Westphal 1378b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1379b65ac446SPaolo Abeni { 1380dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1381b65ac446SPaolo Abeni 1382b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1383dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1384dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1385b65ac446SPaolo Abeni scratch->len = skb->len; 1386b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1387b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1388b65ac446SPaolo Abeni #endif 1389677bf08cSFlorian Westphal if (udp_try_make_stateless(skb)) 1390dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1391dce4551cSPaolo Abeni } 1392dce4551cSPaolo Abeni 1393a793183cSEric Dumazet static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) 1394a793183cSEric Dumazet { 1395a793183cSEric Dumazet /* We come here after udp_lib_checksum_complete() returned 0. 1396a793183cSEric Dumazet * This means that __skb_checksum_complete() might have 1397a793183cSEric Dumazet * set skb->csum_valid to 1. 1398a793183cSEric Dumazet * On 64bit platforms, we can set csum_unnecessary 1399a793183cSEric Dumazet * to true, but only if the skb is not shared. 1400a793183cSEric Dumazet */ 1401a793183cSEric Dumazet #if BITS_PER_LONG == 64 1402a793183cSEric Dumazet if (!skb_shared(skb)) 1403a793183cSEric Dumazet udp_skb_scratch(skb)->csum_unnecessary = true; 1404a793183cSEric Dumazet #endif 1405a793183cSEric Dumazet } 1406a793183cSEric Dumazet 1407dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1408dce4551cSPaolo Abeni { 1409dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1410dce4551cSPaolo Abeni } 1411dce4551cSPaolo Abeni 1412dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1413dce4551cSPaolo Abeni { 1414dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1415dce4551cSPaolo Abeni } 1416b65ac446SPaolo Abeni 14177c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 14186dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 14196dfb4367SPaolo Abeni bool rx_queue_lock_held) 1420f970bd9eSPaolo Abeni { 14216b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 14222276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1423f970bd9eSPaolo Abeni int amt; 1424f970bd9eSPaolo Abeni 14256b229cf7SEric Dumazet if (likely(partial)) { 14266b229cf7SEric Dumazet up->forward_deficit += size; 14276b229cf7SEric Dumazet size = up->forward_deficit; 1428d39ca259SPaolo Abeni if (size < (sk->sk_rcvbuf >> 2) && 1429d39ca259SPaolo Abeni !skb_queue_empty(&up->reader_queue)) 14306b229cf7SEric Dumazet return; 14316b229cf7SEric Dumazet } else { 14326b229cf7SEric Dumazet size += up->forward_deficit; 14336b229cf7SEric Dumazet } 14346b229cf7SEric Dumazet up->forward_deficit = 0; 14356b229cf7SEric Dumazet 14366dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 14376dfb4367SPaolo Abeni * if the called don't held it already 14386dfb4367SPaolo Abeni */ 14392276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 14406dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14412276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 14422276f58aSPaolo Abeni 14436dfb4367SPaolo Abeni 1444f970bd9eSPaolo Abeni sk->sk_forward_alloc += size; 1445f970bd9eSPaolo Abeni amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); 1446f970bd9eSPaolo Abeni sk->sk_forward_alloc -= amt; 1447f970bd9eSPaolo Abeni 1448f970bd9eSPaolo Abeni if (amt) 1449f970bd9eSPaolo Abeni __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); 145002ab0d13SEric Dumazet 145102ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 14522276f58aSPaolo Abeni 14532276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 14542276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 14552276f58aSPaolo Abeni 14566dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14572276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1458f970bd9eSPaolo Abeni } 1459f970bd9eSPaolo Abeni 14602276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1461c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1462c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1463c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1464c84d9490SEric Dumazet */ 14657c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1466f970bd9eSPaolo Abeni { 1467b65ac446SPaolo Abeni prefetch(&skb->data); 1468b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1469f970bd9eSPaolo Abeni } 14707c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1471f970bd9eSPaolo Abeni 14726dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 147364f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 14746dfb4367SPaolo Abeni { 1475b65ac446SPaolo Abeni prefetch(&skb->data); 1476b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 14776dfb4367SPaolo Abeni } 14786dfb4367SPaolo Abeni 14794b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 14804b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 14814b272750SEric Dumazet * Under flood, this means that only one producer can be in line 14824b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 14834b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 14844b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 14854b272750SEric Dumazet */ 14864b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 14874b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 14884b272750SEric Dumazet 14894b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 14904b272750SEric Dumazet { 14914b272750SEric Dumazet spinlock_t *busy; 14924b272750SEric Dumazet 14934b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 14944b272750SEric Dumazet spin_lock(busy); 14954b272750SEric Dumazet return busy; 14964b272750SEric Dumazet } 14974b272750SEric Dumazet 14984b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 14994b272750SEric Dumazet { 15004b272750SEric Dumazet if (busy) 15014b272750SEric Dumazet spin_unlock(busy); 15024b272750SEric Dumazet } 15034b272750SEric Dumazet 1504f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1505f970bd9eSPaolo Abeni { 1506f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1507f970bd9eSPaolo Abeni int rmem, delta, amt, err = -ENOMEM; 15084b272750SEric Dumazet spinlock_t *busy = NULL; 1509c8c8b127SEric Dumazet int size; 1510f970bd9eSPaolo Abeni 1511f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1512f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1513f970bd9eSPaolo Abeni */ 1514f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1515363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1516f970bd9eSPaolo Abeni goto drop; 1517f970bd9eSPaolo Abeni 1518c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1519c8c8b127SEric Dumazet * having linear skbs : 1520c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1521c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1522c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1523c8c8b127SEric Dumazet */ 15244b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1525c8c8b127SEric Dumazet skb_condense(skb); 15264b272750SEric Dumazet 15274b272750SEric Dumazet busy = busylock_acquire(sk); 15284b272750SEric Dumazet } 1529c8c8b127SEric Dumazet size = skb->truesize; 1530b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1531c8c8b127SEric Dumazet 1532f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1533f970bd9eSPaolo Abeni * queue contains some other skb 1534f970bd9eSPaolo Abeni */ 1535f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1536feed8a4fSAntonio Messina if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) 1537f970bd9eSPaolo Abeni goto uncharge_drop; 1538f970bd9eSPaolo Abeni 1539f970bd9eSPaolo Abeni spin_lock(&list->lock); 1540f970bd9eSPaolo Abeni if (size >= sk->sk_forward_alloc) { 1541f970bd9eSPaolo Abeni amt = sk_mem_pages(size); 1542f970bd9eSPaolo Abeni delta = amt << SK_MEM_QUANTUM_SHIFT; 1543f970bd9eSPaolo Abeni if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1544f970bd9eSPaolo Abeni err = -ENOBUFS; 1545f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1546f970bd9eSPaolo Abeni goto uncharge_drop; 1547f970bd9eSPaolo Abeni } 1548f970bd9eSPaolo Abeni 1549f970bd9eSPaolo Abeni sk->sk_forward_alloc += delta; 1550f970bd9eSPaolo Abeni } 1551f970bd9eSPaolo Abeni 1552f970bd9eSPaolo Abeni sk->sk_forward_alloc -= size; 1553f970bd9eSPaolo Abeni 15547c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 15557c13f97fSPaolo Abeni * forward allocated memory on dequeue 15567c13f97fSPaolo Abeni */ 1557f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1558f970bd9eSPaolo Abeni 1559f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1560f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1561f970bd9eSPaolo Abeni 1562f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 1563f970bd9eSPaolo Abeni sk->sk_data_ready(sk); 1564f970bd9eSPaolo Abeni 15654b272750SEric Dumazet busylock_release(busy); 1566f970bd9eSPaolo Abeni return 0; 1567f970bd9eSPaolo Abeni 1568f970bd9eSPaolo Abeni uncharge_drop: 1569f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1570f970bd9eSPaolo Abeni 1571f970bd9eSPaolo Abeni drop: 1572f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 15734b272750SEric Dumazet busylock_release(busy); 1574f970bd9eSPaolo Abeni return err; 1575f970bd9eSPaolo Abeni } 1576f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1577f970bd9eSPaolo Abeni 1578c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk) 1579f970bd9eSPaolo Abeni { 1580f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 15812276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 15827c13f97fSPaolo Abeni unsigned int total = 0; 15837c13f97fSPaolo Abeni struct sk_buff *skb; 15847c13f97fSPaolo Abeni 15852276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 15862276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 15877c13f97fSPaolo Abeni total += skb->truesize; 15887c13f97fSPaolo Abeni kfree_skb(skb); 15897c13f97fSPaolo Abeni } 15906dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 15917c13f97fSPaolo Abeni 1592f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1593f970bd9eSPaolo Abeni } 1594c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock); 1595f970bd9eSPaolo Abeni 1596f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1597f970bd9eSPaolo Abeni { 15982276f58aSPaolo Abeni skb_queue_head_init(&udp_sk(sk)->reader_queue); 1599f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1600f970bd9eSPaolo Abeni return 0; 1601f970bd9eSPaolo Abeni } 1602f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock); 1603f970bd9eSPaolo Abeni 1604f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1605f970bd9eSPaolo Abeni { 1606f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1607f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1608f970bd9eSPaolo Abeni 1609f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1610f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1611f970bd9eSPaolo Abeni } 16120a463c78SPaolo Abeni 1613ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1614ca2c1418SPaolo Abeni return; 1615ca2c1418SPaolo Abeni 1616dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1617dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 16180ddf3fb2SPaolo Abeni */ 1619dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 16200ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1621ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1622f970bd9eSPaolo Abeni } 1623f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1624f970bd9eSPaolo Abeni 16252276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 16262276f58aSPaolo Abeni struct sk_buff_head *rcvq, 16272276f58aSPaolo Abeni int *total) 16282276f58aSPaolo Abeni { 16292276f58aSPaolo Abeni struct sk_buff *skb; 16302276f58aSPaolo Abeni 16319bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 16329bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 16332276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 16342276f58aSPaolo Abeni IS_UDPLITE(sk)); 16352276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 16362276f58aSPaolo Abeni IS_UDPLITE(sk)); 16372276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 16382276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 16392276f58aSPaolo Abeni *total += skb->truesize; 16402276f58aSPaolo Abeni kfree_skb(skb); 16419bd780f5SPaolo Abeni } else { 1642a793183cSEric Dumazet udp_skb_csum_unnecessary_set(skb); 16439bd780f5SPaolo Abeni break; 16449bd780f5SPaolo Abeni } 16452276f58aSPaolo Abeni } 16462276f58aSPaolo Abeni return skb; 16472276f58aSPaolo Abeni } 16482276f58aSPaolo Abeni 164985584672SEric Dumazet /** 165085584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 165185584672SEric Dumazet * @sk: socket 165285584672SEric Dumazet * 165385584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1654e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 165585584672SEric Dumazet */ 1656e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 165785584672SEric Dumazet { 16582276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 16592276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 166085584672SEric Dumazet struct sk_buff *skb; 16617c13f97fSPaolo Abeni int total = 0; 1662e83c6744SEric Dumazet int res; 166385584672SEric Dumazet 166485584672SEric Dumazet spin_lock_bh(&rcvq->lock); 16652276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 1666137a0dbeSEric Dumazet if (!skb && !skb_queue_empty_lockless(sk_queue)) { 16672276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 16682276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 16692276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 16702276f58aSPaolo Abeni 16712276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 167285584672SEric Dumazet } 1673e83c6744SEric Dumazet res = skb ? skb->len : -1; 16747c13f97fSPaolo Abeni if (total) 16756dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 167685584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 167785584672SEric Dumazet return res; 167885584672SEric Dumazet } 167985584672SEric Dumazet 16801da177e4SLinus Torvalds /* 16811da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 16821da177e4SLinus Torvalds */ 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 16851da177e4SLinus Torvalds { 16866516c655SStephen Hemminger switch (cmd) { 16871da177e4SLinus Torvalds case SIOCOUTQ: 16881da177e4SLinus Torvalds { 168931e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 169031e6d363SEric Dumazet 16911da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds case SIOCINQ: 16951da177e4SLinus Torvalds { 1696e83c6744SEric Dumazet int amount = max_t(int, 0, first_packet_length(sk)); 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds default: 17021da177e4SLinus Torvalds return -ENOIOCTLCMD; 17031da177e4SLinus Torvalds } 17046516c655SStephen Hemminger 17056516c655SStephen Hemminger return 0; 17061da177e4SLinus Torvalds } 1707c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 17081da177e4SLinus Torvalds 17092276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1710fd69c399SPaolo Abeni int noblock, int *off, int *err) 17112276f58aSPaolo Abeni { 17122276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 17132276f58aSPaolo Abeni struct sk_buff_head *queue; 17142276f58aSPaolo Abeni struct sk_buff *last; 17152276f58aSPaolo Abeni long timeo; 17162276f58aSPaolo Abeni int error; 17172276f58aSPaolo Abeni 17182276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 17192276f58aSPaolo Abeni flags |= noblock ? MSG_DONTWAIT : 0; 17202276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 17212276f58aSPaolo Abeni do { 17222276f58aSPaolo Abeni struct sk_buff *skb; 17232276f58aSPaolo Abeni 17242276f58aSPaolo Abeni error = sock_error(sk); 17252276f58aSPaolo Abeni if (error) 17262276f58aSPaolo Abeni break; 17272276f58aSPaolo Abeni 17282276f58aSPaolo Abeni error = -EAGAIN; 17292276f58aSPaolo Abeni do { 17302276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 1731e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1732e427cad6SPaolo Abeni err, &last); 17332276f58aSPaolo Abeni if (skb) { 1734e427cad6SPaolo Abeni if (!(flags & MSG_PEEK)) 1735e427cad6SPaolo Abeni udp_skb_destructor(sk, skb); 17362276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17372276f58aSPaolo Abeni return skb; 17382276f58aSPaolo Abeni } 17392276f58aSPaolo Abeni 1740137a0dbeSEric Dumazet if (skb_queue_empty_lockless(sk_queue)) { 17412276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17422276f58aSPaolo Abeni goto busy_check; 17432276f58aSPaolo Abeni } 17442276f58aSPaolo Abeni 17456dfb4367SPaolo Abeni /* refill the reader queue and walk it again 17466dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 17476dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 17486dfb4367SPaolo Abeni * is needed. 17496dfb4367SPaolo Abeni */ 17502276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 17512276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 17522276f58aSPaolo Abeni 1753e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1754e427cad6SPaolo Abeni err, &last); 1755e427cad6SPaolo Abeni if (skb && !(flags & MSG_PEEK)) 1756e427cad6SPaolo Abeni udp_skb_dtor_locked(sk, skb); 17576dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 17582276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1759de321ed3SAndrey Vagin if (skb) 17602276f58aSPaolo Abeni return skb; 17612276f58aSPaolo Abeni 17622276f58aSPaolo Abeni busy_check: 17632276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 17642276f58aSPaolo Abeni break; 17652276f58aSPaolo Abeni 17662276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 1767137a0dbeSEric Dumazet } while (!skb_queue_empty_lockless(sk_queue)); 17682276f58aSPaolo Abeni 17692276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 17702276f58aSPaolo Abeni } while (timeo && 1771b50b0580SSabrina Dubroca !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, 1772b50b0580SSabrina Dubroca &error, &timeo, 17732276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 17742276f58aSPaolo Abeni 17752276f58aSPaolo Abeni *err = error; 17762276f58aSPaolo Abeni return NULL; 17772276f58aSPaolo Abeni } 17787e823644SJiri Kosina EXPORT_SYMBOL(__skb_recv_udp); 17792276f58aSPaolo Abeni 1780db8dac20SDavid S. Miller /* 1781db8dac20SDavid S. Miller * This should be easy, if there is something there we 1782db8dac20SDavid S. Miller * return it, otherwise we block. 1783db8dac20SDavid S. Miller */ 1784db8dac20SDavid S. Miller 17851b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, 17861b784140SYing Xue int flags, int *addr_len) 1787db8dac20SDavid S. Miller { 1788db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1789342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1790db8dac20SDavid S. Miller struct sk_buff *skb; 179159c2cdaeSDavid S. Miller unsigned int ulen, copied; 1792fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 1793db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1794197c949eSEric Dumazet bool checksum_valid = false; 1795db8dac20SDavid S. Miller 1796db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 179785fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1798db8dac20SDavid S. Miller 1799db8dac20SDavid S. Miller try_again: 1800a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 1801fd69c399SPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &off, &err); 1802db8dac20SDavid S. Miller if (!skb) 1803627d2d6bSsamanthakumar return err; 1804db8dac20SDavid S. Miller 1805b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 180659c2cdaeSDavid S. Miller copied = len; 1807627d2d6bSsamanthakumar if (copied > ulen - off) 1808627d2d6bSsamanthakumar copied = ulen - off; 180959c2cdaeSDavid S. Miller else if (copied < ulen) 1810db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1811db8dac20SDavid S. Miller 1812db8dac20SDavid S. Miller /* 1813db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1814db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1815db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1816db8dac20SDavid S. Miller */ 1817db8dac20SDavid S. Miller 1818d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1819d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1820b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1821b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1822197c949eSEric Dumazet if (!checksum_valid) 1823db8dac20SDavid S. Miller goto csum_copy_err; 1824db8dac20SDavid S. Miller } 1825db8dac20SDavid S. Miller 1826b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1827b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1828b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1829b65ac446SPaolo Abeni else 1830627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1831b65ac446SPaolo Abeni } else { 1832627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1833db8dac20SDavid S. Miller 1834db8dac20SDavid S. Miller if (err == -EINVAL) 1835db8dac20SDavid S. Miller goto csum_copy_err; 1836db8dac20SDavid S. Miller } 1837db8dac20SDavid S. Miller 183822911fc5SEric Dumazet if (unlikely(err)) { 1839fd69c399SPaolo Abeni if (!peeking) { 1840979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 18416aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1842979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1843979402b1SEric Dumazet } 1844850cbaddSPaolo Abeni kfree_skb(skb); 1845627d2d6bSsamanthakumar return err; 184622911fc5SEric Dumazet } 1847db8dac20SDavid S. Miller 1848fd69c399SPaolo Abeni if (!peeking) 18496aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1850629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1851db8dac20SDavid S. Miller 18523b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 1853db8dac20SDavid S. Miller 1854db8dac20SDavid S. Miller /* Copy the address. */ 1855c482c568SEric Dumazet if (sin) { 1856db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1857db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1858db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1859db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1860bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1861983695faSDaniel Borkmann 1862983695faSDaniel Borkmann if (cgroup_bpf_enabled) 1863983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, 1864983695faSDaniel Borkmann (struct sockaddr *)sin); 1865db8dac20SDavid S. Miller } 1866bcd1665eSPaolo Abeni 1867bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 1868bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 1869bcd1665eSPaolo Abeni 1870db8dac20SDavid S. Miller if (inet->cmsg_flags) 1871ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1872db8dac20SDavid S. Miller 187359c2cdaeSDavid S. Miller err = copied; 1874db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1875db8dac20SDavid S. Miller err = ulen; 1876db8dac20SDavid S. Miller 1877850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1878db8dac20SDavid S. Miller return err; 1879db8dac20SDavid S. Miller 1880db8dac20SDavid S. Miller csum_copy_err: 18812276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 18822276f58aSPaolo Abeni udp_skb_destructor)) { 18836aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 18846aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 18856a5dc9e5SEric Dumazet } 1886850cbaddSPaolo Abeni kfree_skb(skb); 1887db8dac20SDavid S. Miller 1888beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1889beb39db5SEric Dumazet cond_resched(); 18909cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1891db8dac20SDavid S. Miller goto try_again; 1892db8dac20SDavid S. Miller } 1893db8dac20SDavid S. Miller 1894d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1895d74bad4eSAndrey Ignatov { 1896d74bad4eSAndrey Ignatov /* This check is replicated from __ip4_datagram_connect() and 1897d74bad4eSAndrey Ignatov * intended to prevent BPF program called below from accessing bytes 1898d74bad4eSAndrey Ignatov * that are out of the bound specified by user in addr_len. 1899d74bad4eSAndrey Ignatov */ 1900d74bad4eSAndrey Ignatov if (addr_len < sizeof(struct sockaddr_in)) 1901d74bad4eSAndrey Ignatov return -EINVAL; 1902d74bad4eSAndrey Ignatov 1903d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); 1904d74bad4eSAndrey Ignatov } 1905d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect); 1906d74bad4eSAndrey Ignatov 1907286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 19081da177e4SLinus Torvalds { 19091da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 19101da177e4SLinus Torvalds /* 19111da177e4SLinus Torvalds * 1003.1g - break association. 19121da177e4SLinus Torvalds */ 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1915c720c7e8SEric Dumazet inet->inet_daddr = 0; 1916c720c7e8SEric Dumazet inet->inet_dport = 0; 1917bdeab991STom Herbert sock_rps_reset_rxhash(sk); 19181da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 1919303d0403SWillem de Bruijn if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { 19201da177e4SLinus Torvalds inet_reset_saddr(sk); 1921303d0403SWillem de Bruijn if (sk->sk_prot->rehash && 1922303d0403SWillem de Bruijn (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 1923303d0403SWillem de Bruijn sk->sk_prot->rehash(sk); 1924303d0403SWillem de Bruijn } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 19271da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1928c720c7e8SEric Dumazet inet->inet_sport = 0; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds sk_dst_reset(sk); 19311da177e4SLinus Torvalds return 0; 19321da177e4SLinus Torvalds } 1933286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1934286c72deSEric Dumazet 1935286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1936286c72deSEric Dumazet { 1937286c72deSEric Dumazet lock_sock(sk); 1938286c72deSEric Dumazet __udp_disconnect(sk, flags); 1939286c72deSEric Dumazet release_sock(sk); 1940286c72deSEric Dumazet return 0; 1941286c72deSEric Dumazet } 1942c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 19431da177e4SLinus Torvalds 1944645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 1945645ca708SEric Dumazet { 1946723b4610SEric Dumazet if (sk_hashed(sk)) { 1947645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1948512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 1949512615b6SEric Dumazet 1950512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1951d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 1952512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1953645ca708SEric Dumazet 1954c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 1955e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1956e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1957ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 1958fdcc8aa9SEric Dumazet hslot->count--; 1959c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 1960645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1961512615b6SEric Dumazet 1962512615b6SEric Dumazet spin_lock(&hslot2->lock); 1963ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1964512615b6SEric Dumazet hslot2->count--; 1965512615b6SEric Dumazet spin_unlock(&hslot2->lock); 1966645ca708SEric Dumazet } 1967c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 1968645ca708SEric Dumazet } 1969723b4610SEric Dumazet } 1970645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 1971645ca708SEric Dumazet 1972719f8358SEric Dumazet /* 1973719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 1974719f8358SEric Dumazet */ 1975719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 1976719f8358SEric Dumazet { 1977719f8358SEric Dumazet if (sk_hashed(sk)) { 1978719f8358SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1979719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 1980719f8358SEric Dumazet 1981719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1982719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 1983719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 1984e32ea7e7SCraig Gallek 1985e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 1986e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 1987719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1988719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 1989719f8358SEric Dumazet /* we must lock primary chain too */ 1990719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 1991e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1992e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1993719f8358SEric Dumazet 1994e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 1995719f8358SEric Dumazet spin_lock(&hslot2->lock); 1996ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1997719f8358SEric Dumazet hslot2->count--; 1998719f8358SEric Dumazet spin_unlock(&hslot2->lock); 1999719f8358SEric Dumazet 2000719f8358SEric Dumazet spin_lock(&nhslot2->lock); 2001ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 2002719f8358SEric Dumazet &nhslot2->head); 2003719f8358SEric Dumazet nhslot2->count++; 2004719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 2005e32ea7e7SCraig Gallek } 2006719f8358SEric Dumazet 2007719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 2008719f8358SEric Dumazet } 2009719f8358SEric Dumazet } 2010719f8358SEric Dumazet } 2011719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 2012719f8358SEric Dumazet 20138f6b5392SAlexey Kodanev void udp_v4_rehash(struct sock *sk) 2014719f8358SEric Dumazet { 2015f0b1e64cSMartin KaFai Lau u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 2016719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 2017719f8358SEric Dumazet inet_sk(sk)->inet_num); 2018719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 2019719f8358SEric Dumazet } 2020719f8358SEric Dumazet 2021a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 202293821778SHerbert Xu { 2023fec5e652STom Herbert int rc; 202493821778SHerbert Xu 2025005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 2026bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 2027005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 20282c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 2029e68b6e50SEric Dumazet } else { 2030e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 2031005ec974SShawn Bohrer } 2032fec5e652STom Herbert 2033850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 2034766e9037SEric Dumazet if (rc < 0) { 2035766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 2036766e9037SEric Dumazet 203793821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 2038766e9037SEric Dumazet if (rc == -ENOMEM) 2039e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 204093821778SHerbert Xu is_udplite); 2041e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 2042766e9037SEric Dumazet kfree_skb(skb); 2043296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 2044766e9037SEric Dumazet return -1; 204593821778SHerbert Xu } 204693821778SHerbert Xu 204793821778SHerbert Xu return 0; 204893821778SHerbert Xu } 204993821778SHerbert Xu 2050db8dac20SDavid S. Miller /* returns: 2051db8dac20SDavid S. Miller * -1: error 2052db8dac20SDavid S. Miller * 0: success 2053db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 2054db8dac20SDavid S. Miller * 2055db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 2056db8dac20SDavid S. Miller * have either been requeued or freed. 2057db8dac20SDavid S. Miller */ 2058cf329aa4SPaolo Abeni static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 2059db8dac20SDavid S. Miller { 2060db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 2061db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 2062db8dac20SDavid S. Miller 2063db8dac20SDavid S. Miller /* 2064db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 2065db8dac20SDavid S. Miller */ 2066db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 2067db8dac20SDavid S. Miller goto drop; 2068895b5c9fSFlorian Westphal nf_reset_ct(skb); 2069db8dac20SDavid S. Miller 207088ab3108SDavidlohr Bueso if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 20710ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 20720ad92ad0SEric Dumazet 2073db8dac20SDavid S. Miller /* 2074db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 2075db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 2076db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 2077db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 2078db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 2079db8dac20SDavid S. Miller * handler or was discarded by it. 2080db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 2081db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 2082db8dac20SDavid S. Miller */ 2083db8dac20SDavid S. Miller 2084db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 20856aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 2086e5aed006SHannes Frederic Sowa if (encap_rcv) { 2087db8dac20SDavid S. Miller int ret; 2088db8dac20SDavid S. Miller 20890a80966bSTom Herbert /* Verify checksum before giving to encap */ 20900a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 20910a80966bSTom Herbert goto csum_error; 20920a80966bSTom Herbert 20930ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 2094db8dac20SDavid S. Miller if (ret <= 0) { 209502c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 20960283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 2097db8dac20SDavid S. Miller is_udplite); 2098db8dac20SDavid S. Miller return -ret; 2099db8dac20SDavid S. Miller } 2100db8dac20SDavid S. Miller } 2101db8dac20SDavid S. Miller 2102db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 2103db8dac20SDavid S. Miller } 2104db8dac20SDavid S. Miller 2105db8dac20SDavid S. Miller /* 2106db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 2107db8dac20SDavid S. Miller */ 2108b0a42277SMiaohe Lin if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 2109db8dac20SDavid S. Miller 2110db8dac20SDavid S. Miller /* 2111db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 2112db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 2113db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 2114db8dac20SDavid S. Miller * protocol stack as such. 2115db8dac20SDavid S. Miller * 2116db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 2117db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 2118db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 2119db8dac20SDavid S. Miller * provided by the application." 2120db8dac20SDavid S. Miller */ 2121db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 2122ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 2123db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 2124db8dac20SDavid S. Miller goto drop; 2125db8dac20SDavid S. Miller } 2126db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 2127db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 2128db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 2129db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 2130db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 2131db8dac20SDavid S. Miller */ 2132db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 2133ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 2134db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 2135db8dac20SDavid S. Miller goto drop; 2136db8dac20SDavid S. Miller } 2137db8dac20SDavid S. Miller } 2138db8dac20SDavid S. Miller 2139dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 2140ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 2141ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 21426a5dc9e5SEric Dumazet goto csum_error; 2143ce25d66aSEric Dumazet 2144ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 2145a6127697SMichal Kubeček goto drop; 2146db8dac20SDavid S. Miller 2147e6afc8acSsamanthakumar udp_csum_pull_header(skb); 2148db8dac20SDavid S. Miller 2149fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 2150850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 2151db8dac20SDavid S. Miller 21526a5dc9e5SEric Dumazet csum_error: 215302c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2154db8dac20SDavid S. Miller drop: 215502c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 21568edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 2157db8dac20SDavid S. Miller kfree_skb(skb); 2158db8dac20SDavid S. Miller return -1; 2159db8dac20SDavid S. Miller } 2160db8dac20SDavid S. Miller 2161cf329aa4SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2162cf329aa4SPaolo Abeni { 2163cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 2164cf329aa4SPaolo Abeni int ret; 2165cf329aa4SPaolo Abeni 2166cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 2167cf329aa4SPaolo Abeni return udp_queue_rcv_one_skb(sk, skb); 2168cf329aa4SPaolo Abeni 2169a08e7fd9SCambda Zhu BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); 2170cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 2171cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, true); 21721a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 2173cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 2174cf329aa4SPaolo Abeni ret = udp_queue_rcv_one_skb(sk, skb); 2175cf329aa4SPaolo Abeni if (ret > 0) 2176*10c678bdSXin Long ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret); 2177cf329aa4SPaolo Abeni } 2178cf329aa4SPaolo Abeni return 0; 2179cf329aa4SPaolo Abeni } 2180cf329aa4SPaolo Abeni 218197502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 2182e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 218397502231SEric Dumazet */ 218464f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2185421b3885SShawn Bohrer { 218697502231SEric Dumazet struct dst_entry *old; 2187421b3885SShawn Bohrer 2188d24406c8SWei Wang if (dst_hold_safe(dst)) { 2189e47eb5dfSEric Dumazet old = xchg(&sk->sk_rx_dst, dst); 219097502231SEric Dumazet dst_release(old); 219164f0f5d1SPaolo Abeni return old != dst; 219297502231SEric Dumazet } 219364f0f5d1SPaolo Abeni return false; 2194d24406c8SWei Wang } 2195c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 2196421b3885SShawn Bohrer 2197db8dac20SDavid S. Miller /* 2198db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 2199db8dac20SDavid S. Miller * 22001240d137SEric Dumazet * Note: called only from the BH handler context. 2201db8dac20SDavid S. Miller */ 2202e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2203db8dac20SDavid S. Miller struct udphdr *uh, 2204db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 220536cbb245SRick Jones struct udp_table *udptable, 220636cbb245SRick Jones int proto) 2207db8dac20SDavid S. Miller { 2208ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 22095cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 22105cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 22112dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2212ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 2213ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 2214fb74c277SDavid Ahern int sdif = inet_sdif(skb); 2215ca065d0cSEric Dumazet struct hlist_node *node; 2216ca065d0cSEric Dumazet struct sk_buff *nskb; 22172dc41cffSDavid Held 22182dc41cffSDavid Held if (use_hash2) { 2219f0b1e64cSMartin KaFai Lau hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 222073e2d5e3SPablo Neira udptable->mask; 2221f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 22222dc41cffSDavid Held start_lookup: 222373e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 22242dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 22252dc41cffSDavid Held } 2226db8dac20SDavid S. Miller 2227ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2228ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2229fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 2230ca065d0cSEric Dumazet continue; 22311240d137SEric Dumazet 2232ca065d0cSEric Dumazet if (!first) { 2233ca065d0cSEric Dumazet first = sk; 2234ca065d0cSEric Dumazet continue; 2235ca065d0cSEric Dumazet } 2236ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 2237ca065d0cSEric Dumazet 2238ca065d0cSEric Dumazet if (unlikely(!nskb)) { 2239ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 224002c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2241ca065d0cSEric Dumazet IS_UDPLITE(sk)); 224202c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2243ca065d0cSEric Dumazet IS_UDPLITE(sk)); 2244ca065d0cSEric Dumazet continue; 2245ca065d0cSEric Dumazet } 2246ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 2247ca065d0cSEric Dumazet consume_skb(nskb); 2248ca065d0cSEric Dumazet } 22491240d137SEric Dumazet 22502dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 22512dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 22522dc41cffSDavid Held hash2 = hash2_any; 22532dc41cffSDavid Held goto start_lookup; 22542dc41cffSDavid Held } 22552dc41cffSDavid Held 2256ca065d0cSEric Dumazet if (first) { 2257ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 2258ca065d0cSEric Dumazet consume_skb(skb); 22591240d137SEric Dumazet } else { 2260ca065d0cSEric Dumazet kfree_skb(skb); 226102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 226236cbb245SRick Jones proto == IPPROTO_UDPLITE); 22631240d137SEric Dumazet } 2264db8dac20SDavid S. Miller return 0; 2265db8dac20SDavid S. Miller } 2266db8dac20SDavid S. Miller 2267db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2268db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2269666a3d6eSSu Yanjun * Otherwise, csum completion requires checksumming packet body, 2270db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2271db8dac20SDavid S. Miller */ 2272db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2273db8dac20SDavid S. Miller int proto) 2274db8dac20SDavid S. Miller { 2275db8dac20SDavid S. Miller int err; 2276db8dac20SDavid S. Miller 2277db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2278db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2279db8dac20SDavid S. Miller 2280db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2281db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2282db8dac20SDavid S. Miller if (err) 2283db8dac20SDavid S. Miller return err; 228415f35d49SAlexey Kodanev 228515f35d49SAlexey Kodanev if (UDP_SKB_CB(skb)->partial_cov) { 228615f35d49SAlexey Kodanev skb->csum = inet_compute_pseudo(skb, proto); 228715f35d49SAlexey Kodanev return 0; 228815f35d49SAlexey Kodanev } 2289db8dac20SDavid S. Miller } 2290db8dac20SDavid S. Miller 2291b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2292b46d9f62SHannes Frederic Sowa * force to int. 2293b46d9f62SHannes Frederic Sowa */ 2294db4f1be3SSean Tranchetti err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2295ed70fcfcSTom Herbert inet_compute_pseudo); 2296db4f1be3SSean Tranchetti if (err) 2297db4f1be3SSean Tranchetti return err; 2298db4f1be3SSean Tranchetti 2299db4f1be3SSean Tranchetti if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2300db4f1be3SSean Tranchetti /* If SW calculated the value, we know it's bad */ 2301db4f1be3SSean Tranchetti if (skb->csum_complete_sw) 2302db4f1be3SSean Tranchetti return 1; 2303db4f1be3SSean Tranchetti 2304db4f1be3SSean Tranchetti /* HW says the value is bad. Let's validate that. 2305db4f1be3SSean Tranchetti * skb->csum is no longer the full packet checksum, 2306db4f1be3SSean Tranchetti * so don't treat it as such. 2307db4f1be3SSean Tranchetti */ 2308db4f1be3SSean Tranchetti skb_checksum_complete_unset(skb); 2309db4f1be3SSean Tranchetti } 2310db4f1be3SSean Tranchetti 2311db4f1be3SSean Tranchetti return 0; 2312db8dac20SDavid S. Miller } 2313db8dac20SDavid S. Miller 23142b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 23152b5a9217SPaolo Abeni * return code conversion for ip layer consumption 23162b5a9217SPaolo Abeni */ 23172b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 23182b5a9217SPaolo Abeni struct udphdr *uh) 23192b5a9217SPaolo Abeni { 23202b5a9217SPaolo Abeni int ret; 23212b5a9217SPaolo Abeni 23222b5a9217SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2323e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); 23242b5a9217SPaolo Abeni 23252b5a9217SPaolo Abeni ret = udp_queue_rcv_skb(sk, skb); 23262b5a9217SPaolo Abeni 23272b5a9217SPaolo Abeni /* a return value > 0 means to resubmit the input, but 23282b5a9217SPaolo Abeni * it wants the return to be -protocol, or 0 23292b5a9217SPaolo Abeni */ 23302b5a9217SPaolo Abeni if (ret > 0) 23312b5a9217SPaolo Abeni return -ret; 23322b5a9217SPaolo Abeni return 0; 23332b5a9217SPaolo Abeni } 23342b5a9217SPaolo Abeni 2335db8dac20SDavid S. Miller /* 2336db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2337db8dac20SDavid S. Miller */ 2338db8dac20SDavid S. Miller 2339645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2340db8dac20SDavid S. Miller int proto) 2341db8dac20SDavid S. Miller { 2342db8dac20SDavid S. Miller struct sock *sk; 23437b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2344db8dac20SDavid S. Miller unsigned short ulen; 2345adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 23462783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 23470283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 234871489e21SJoe Stringer bool refcounted; 2349db8dac20SDavid S. Miller 2350db8dac20SDavid S. Miller /* 2351db8dac20SDavid S. Miller * Validate the packet. 2352db8dac20SDavid S. Miller */ 2353db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2354db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2355db8dac20SDavid S. Miller 23567b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2357db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2358ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2359ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2360ccc2d97cSBjørn Mork 2361db8dac20SDavid S. Miller if (ulen > skb->len) 2362db8dac20SDavid S. Miller goto short_packet; 2363db8dac20SDavid S. Miller 2364db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2365db8dac20SDavid S. Miller /* UDP validates ulen. */ 2366db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2367db8dac20SDavid S. Miller goto short_packet; 2368db8dac20SDavid S. Miller uh = udp_hdr(skb); 2369db8dac20SDavid S. Miller } 2370db8dac20SDavid S. Miller 2371db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2372db8dac20SDavid S. Miller goto csum_error; 2373db8dac20SDavid S. Miller 237471489e21SJoe Stringer sk = skb_steal_sock(skb, &refcounted); 23758afdd99aSEric Dumazet if (sk) { 237697502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2377421b3885SShawn Bohrer int ret; 2378421b3885SShawn Bohrer 237997502231SEric Dumazet if (unlikely(sk->sk_rx_dst != dst)) 238097502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2381421b3885SShawn Bohrer 23822b5a9217SPaolo Abeni ret = udp_unicast_rcv_skb(sk, skb, uh); 238371489e21SJoe Stringer if (refcounted) 23848afdd99aSEric Dumazet sock_put(sk); 23852b5a9217SPaolo Abeni return ret; 2386c18450a5SFabian Frederick } 2387c18450a5SFabian Frederick 2388db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2389e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 239036cbb245SRick Jones saddr, daddr, udptable, proto); 2391db8dac20SDavid S. Miller 2392607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 23932b5a9217SPaolo Abeni if (sk) 23942b5a9217SPaolo Abeni return udp_unicast_rcv_skb(sk, skb, uh); 2395db8dac20SDavid S. Miller 2396db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2397db8dac20SDavid S. Miller goto drop; 2398895b5c9fSFlorian Westphal nf_reset_ct(skb); 2399db8dac20SDavid S. Miller 2400db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2401db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2402db8dac20SDavid S. Miller goto csum_error; 2403db8dac20SDavid S. Miller 240402c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2405db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2406db8dac20SDavid S. Miller 2407db8dac20SDavid S. Miller /* 2408db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2409db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2410db8dac20SDavid S. Miller */ 2411db8dac20SDavid S. Miller kfree_skb(skb); 2412db8dac20SDavid S. Miller return 0; 2413db8dac20SDavid S. Miller 2414db8dac20SDavid S. Miller short_packet: 2415ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2416afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2417afd46503SJoe Perches &saddr, ntohs(uh->source), 2418afd46503SJoe Perches ulen, skb->len, 2419afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2420db8dac20SDavid S. Miller goto drop; 2421db8dac20SDavid S. Miller 2422db8dac20SDavid S. Miller csum_error: 2423db8dac20SDavid S. Miller /* 2424db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2425db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2426db8dac20SDavid S. Miller */ 2427ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2428afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2429afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2430db8dac20SDavid S. Miller ulen); 243102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2432db8dac20SDavid S. Miller drop: 243302c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 2434db8dac20SDavid S. Miller kfree_skb(skb); 2435db8dac20SDavid S. Miller return 0; 2436db8dac20SDavid S. Miller } 2437db8dac20SDavid S. Miller 2438421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2439421b3885SShawn Bohrer * If more than one socket found returns NULL 2440421b3885SShawn Bohrer */ 2441421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2442421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2443421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2444fb74c277SDavid Ahern int dif, int sdif) 2445421b3885SShawn Bohrer { 2446421b3885SShawn Bohrer struct sock *sk, *result; 2447421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2448ca065d0cSEric Dumazet unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); 2449421b3885SShawn Bohrer struct udp_hslot *hslot = &udp_table.hash[slot]; 2450421b3885SShawn Bohrer 245163c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 245263c6f81cSEric Dumazet if (hslot->count > 10) 245363c6f81cSEric Dumazet return NULL; 245463c6f81cSEric Dumazet 2455421b3885SShawn Bohrer result = NULL; 2456ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2457ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2458fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2459ca065d0cSEric Dumazet if (result) 2460ca065d0cSEric Dumazet return NULL; 2461421b3885SShawn Bohrer result = sk; 2462421b3885SShawn Bohrer } 2463421b3885SShawn Bohrer } 2464421b3885SShawn Bohrer 2465421b3885SShawn Bohrer return result; 2466421b3885SShawn Bohrer } 2467421b3885SShawn Bohrer 2468421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2469421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2470421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2471421b3885SShawn Bohrer */ 2472421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2473421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2474421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 24753fa6f616SDavid Ahern int dif, int sdif) 2476421b3885SShawn Bohrer { 2477421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2478f0b1e64cSMartin KaFai Lau unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2479421b3885SShawn Bohrer unsigned int slot2 = hash2 & udp_table.mask; 2480421b3885SShawn Bohrer struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 2481c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2482421b3885SShawn Bohrer const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 2483ca065d0cSEric Dumazet struct sock *sk; 2484421b3885SShawn Bohrer 2485ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2486ca065d0cSEric Dumazet if (INET_MATCH(sk, net, acookie, rmt_addr, 24873fa6f616SDavid Ahern loc_addr, ports, dif, sdif)) 2488ca065d0cSEric Dumazet return sk; 2489421b3885SShawn Bohrer /* Only check first socket in chain */ 2490421b3885SShawn Bohrer break; 2491421b3885SShawn Bohrer } 2492ca065d0cSEric Dumazet return NULL; 2493421b3885SShawn Bohrer } 2494421b3885SShawn Bohrer 24957487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2496421b3885SShawn Bohrer { 2497610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2498bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2499610438b7SEric Dumazet const struct iphdr *iph; 2500610438b7SEric Dumazet const struct udphdr *uh; 2501ca065d0cSEric Dumazet struct sock *sk = NULL; 2502421b3885SShawn Bohrer struct dst_entry *dst; 2503421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2504fb74c277SDavid Ahern int sdif = inet_sdif(skb); 25056e540309SShawn Bohrer int ours; 2506421b3885SShawn Bohrer 2507421b3885SShawn Bohrer /* validate the packet */ 2508421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 25097487449cSPaolo Abeni return 0; 2510421b3885SShawn Bohrer 2511610438b7SEric Dumazet iph = ip_hdr(skb); 2512610438b7SEric Dumazet uh = udp_hdr(skb); 2513610438b7SEric Dumazet 2514996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2515bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 25166e540309SShawn Bohrer 25176e540309SShawn Bohrer if (!in_dev) 25187487449cSPaolo Abeni return 0; 25196e540309SShawn Bohrer 25206e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 25216e540309SShawn Bohrer iph->protocol); 25226e540309SShawn Bohrer if (!ours) 25237487449cSPaolo Abeni return 0; 2524ad0ea198SPaolo Abeni 2525421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2526fb74c277SDavid Ahern uh->source, iph->saddr, 2527fb74c277SDavid Ahern dif, sdif); 25286e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2529421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 25303fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 25316e540309SShawn Bohrer } 2532421b3885SShawn Bohrer 253341c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 25347487449cSPaolo Abeni return 0; 2535421b3885SShawn Bohrer 2536421b3885SShawn Bohrer skb->sk = sk; 253782eabd9eSAlexander Duyck skb->destructor = sock_efree; 253810e2eb87SEric Dumazet dst = READ_ONCE(sk->sk_rx_dst); 2539421b3885SShawn Bohrer 2540421b3885SShawn Bohrer if (dst) 2541421b3885SShawn Bohrer dst = dst_check(dst, 0); 254210e2eb87SEric Dumazet if (dst) { 2543bc044e8dSPaolo Abeni u32 itag = 0; 2544bc044e8dSPaolo Abeni 2545d24406c8SWei Wang /* set noref for now. 2546d24406c8SWei Wang * any place which wants to hold dst has to call 2547d24406c8SWei Wang * dst_hold_safe() 2548d24406c8SWei Wang */ 2549421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2550bc044e8dSPaolo Abeni 2551bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2552bc044e8dSPaolo Abeni * the source on each packet 2553bc044e8dSPaolo Abeni */ 2554bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2555bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 2556bc044e8dSPaolo Abeni iph->saddr, iph->tos, 2557bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2558421b3885SShawn Bohrer } 25597487449cSPaolo Abeni return 0; 256010e2eb87SEric Dumazet } 2561421b3885SShawn Bohrer 2562db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2563db8dac20SDavid S. Miller { 2564645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2565db8dac20SDavid S. Miller } 2566db8dac20SDavid S. Miller 25677d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2568db8dac20SDavid S. Miller { 256944046a59STom Parkin struct udp_sock *up = udp_sk(sk); 25708a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2571db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 25728a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 257360fb9567SPaolo Abeni if (static_branch_unlikely(&udp_encap_needed_key)) { 257460fb9567SPaolo Abeni if (up->encap_type) { 257544046a59STom Parkin void (*encap_destroy)(struct sock *sk); 25766aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 257744046a59STom Parkin if (encap_destroy) 257844046a59STom Parkin encap_destroy(sk); 257944046a59STom Parkin } 258060fb9567SPaolo Abeni if (up->encap_enabled) 25819c480601SPaolo Abeni static_branch_dec(&udp_encap_needed_key); 258260fb9567SPaolo Abeni } 2583db8dac20SDavid S. Miller } 2584db8dac20SDavid S. Miller 25851da177e4SLinus Torvalds /* 25861da177e4SLinus Torvalds * Socket option code for UDP 25871da177e4SLinus Torvalds */ 25884c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 258991ac1ccaSChristoph Hellwig sockptr_t optval, unsigned int optlen, 25904c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 25911da177e4SLinus Torvalds { 25921da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 25931c19448cSTom Herbert int val, valbool; 25941da177e4SLinus Torvalds int err = 0; 2595b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds if (optlen < sizeof(int)) 25981da177e4SLinus Torvalds return -EINVAL; 25991da177e4SLinus Torvalds 260091ac1ccaSChristoph Hellwig if (copy_from_sockptr(&val, optval, sizeof(val))) 26011da177e4SLinus Torvalds return -EFAULT; 26021da177e4SLinus Torvalds 26031c19448cSTom Herbert valbool = val ? 1 : 0; 26041c19448cSTom Herbert 26051da177e4SLinus Torvalds switch (optname) { 26061da177e4SLinus Torvalds case UDP_CORK: 26071da177e4SLinus Torvalds if (val != 0) { 26081da177e4SLinus Torvalds up->corkflag = 1; 26091da177e4SLinus Torvalds } else { 26101da177e4SLinus Torvalds up->corkflag = 0; 26111da177e4SLinus Torvalds lock_sock(sk); 26124243cdc2SJoe Perches push_pending_frames(sk); 26131da177e4SLinus Torvalds release_sock(sk); 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds break; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds case UDP_ENCAP: 26181da177e4SLinus Torvalds switch (val) { 26191da177e4SLinus Torvalds case 0: 2620fd1ac07fSAlexey Dobriyan #ifdef CONFIG_XFRM 26211da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 26221da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 26230146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 26240146dca7SSabrina Dubroca if (sk->sk_family == AF_INET6) 26250146dca7SSabrina Dubroca up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv; 26260146dca7SSabrina Dubroca else 26270146dca7SSabrina Dubroca #endif 2628067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 2629fd1ac07fSAlexey Dobriyan #endif 2630a8eceea8SJoe Perches fallthrough; 2631342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 26321da177e4SLinus Torvalds up->encap_type = val; 263360fb9567SPaolo Abeni lock_sock(sk); 263460fb9567SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 263560fb9567SPaolo Abeni release_sock(sk); 26361da177e4SLinus Torvalds break; 26371da177e4SLinus Torvalds default: 26381da177e4SLinus Torvalds err = -ENOPROTOOPT; 26391da177e4SLinus Torvalds break; 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds break; 26421da177e4SLinus Torvalds 26431c19448cSTom Herbert case UDP_NO_CHECK6_TX: 26441c19448cSTom Herbert up->no_check6_tx = valbool; 26451c19448cSTom Herbert break; 26461c19448cSTom Herbert 26471c19448cSTom Herbert case UDP_NO_CHECK6_RX: 26481c19448cSTom Herbert up->no_check6_rx = valbool; 26491c19448cSTom Herbert break; 26501c19448cSTom Herbert 2651bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2652bec1f6f6SWillem de Bruijn if (val < 0 || val > USHRT_MAX) 2653bec1f6f6SWillem de Bruijn return -EINVAL; 2654bec1f6f6SWillem de Bruijn up->gso_size = val; 2655bec1f6f6SWillem de Bruijn break; 2656bec1f6f6SWillem de Bruijn 2657e20cf8d3SPaolo Abeni case UDP_GRO: 2658e20cf8d3SPaolo Abeni lock_sock(sk); 2659e20cf8d3SPaolo Abeni if (valbool) 2660e20cf8d3SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 2661e20cf8d3SPaolo Abeni up->gro_enabled = valbool; 2662e20cf8d3SPaolo Abeni release_sock(sk); 2663e20cf8d3SPaolo Abeni break; 2664e20cf8d3SPaolo Abeni 2665ba4e58ecSGerrit Renker /* 2666ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2667ba4e58ecSGerrit Renker */ 2668ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2669ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2670ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2671b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2672ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2673ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2674ba4e58ecSGerrit Renker val = 8; 26754be929beSAlexey Dobriyan else if (val > USHRT_MAX) 26764be929beSAlexey Dobriyan val = USHRT_MAX; 2677ba4e58ecSGerrit Renker up->pcslen = val; 2678ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 2679ba4e58ecSGerrit Renker break; 2680ba4e58ecSGerrit Renker 2681ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2682ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2683ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2684ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2685b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2686ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2687ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2688ba4e58ecSGerrit Renker val = 8; 26894be929beSAlexey Dobriyan else if (val > USHRT_MAX) 26904be929beSAlexey Dobriyan val = USHRT_MAX; 2691ba4e58ecSGerrit Renker up->pcrlen = val; 2692ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 2693ba4e58ecSGerrit Renker break; 2694ba4e58ecSGerrit Renker 26951da177e4SLinus Torvalds default: 26961da177e4SLinus Torvalds err = -ENOPROTOOPT; 26971da177e4SLinus Torvalds break; 26986516c655SStephen Hemminger } 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds return err; 27011da177e4SLinus Torvalds } 2702c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 27031da177e4SLinus Torvalds 2704a7b75c5aSChristoph Hellwig int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 2705a7b75c5aSChristoph Hellwig unsigned int optlen) 2706db8dac20SDavid S. Miller { 2707db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 270891ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 2709a7b75c5aSChristoph Hellwig optval, optlen, 2710db8dac20SDavid S. Miller udp_push_pending_frames); 2711db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2712db8dac20SDavid S. Miller } 2713db8dac20SDavid S. Miller 27144c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 27151da177e4SLinus Torvalds char __user *optval, int __user *optlen) 27161da177e4SLinus Torvalds { 27171da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 27181da177e4SLinus Torvalds int val, len; 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds if (get_user(len, optlen)) 27211da177e4SLinus Torvalds return -EFAULT; 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds if (len < 0) 27261da177e4SLinus Torvalds return -EINVAL; 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds switch (optname) { 27291da177e4SLinus Torvalds case UDP_CORK: 27301da177e4SLinus Torvalds val = up->corkflag; 27311da177e4SLinus Torvalds break; 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds case UDP_ENCAP: 27341da177e4SLinus Torvalds val = up->encap_type; 27351da177e4SLinus Torvalds break; 27361da177e4SLinus Torvalds 27371c19448cSTom Herbert case UDP_NO_CHECK6_TX: 27381c19448cSTom Herbert val = up->no_check6_tx; 27391c19448cSTom Herbert break; 27401c19448cSTom Herbert 27411c19448cSTom Herbert case UDP_NO_CHECK6_RX: 27421c19448cSTom Herbert val = up->no_check6_rx; 27431c19448cSTom Herbert break; 27441c19448cSTom Herbert 2745bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2746bec1f6f6SWillem de Bruijn val = up->gso_size; 2747bec1f6f6SWillem de Bruijn break; 2748bec1f6f6SWillem de Bruijn 2749ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2750ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2751ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2752ba4e58ecSGerrit Renker val = up->pcslen; 2753ba4e58ecSGerrit Renker break; 2754ba4e58ecSGerrit Renker 2755ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2756ba4e58ecSGerrit Renker val = up->pcrlen; 2757ba4e58ecSGerrit Renker break; 2758ba4e58ecSGerrit Renker 27591da177e4SLinus Torvalds default: 27601da177e4SLinus Torvalds return -ENOPROTOOPT; 27616516c655SStephen Hemminger } 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds if (put_user(len, optlen)) 27641da177e4SLinus Torvalds return -EFAULT; 27651da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 27661da177e4SLinus Torvalds return -EFAULT; 27671da177e4SLinus Torvalds return 0; 27681da177e4SLinus Torvalds } 2769c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 27701da177e4SLinus Torvalds 2771db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2772db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2773db8dac20SDavid S. Miller { 2774db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2775db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2776db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2777db8dac20SDavid S. Miller } 2778db8dac20SDavid S. Miller 27791da177e4SLinus Torvalds /** 27801da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 27813628e3cbSAndrew Lunn * @file: - file struct 27823628e3cbSAndrew Lunn * @sock: - socket 27833628e3cbSAndrew Lunn * @wait: - poll table 27841da177e4SLinus Torvalds * 27851da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 27861da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 27871da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 27881da177e4SLinus Torvalds * then it could get return from select indicating data available 27891da177e4SLinus Torvalds * but then block when reading it. Add special case code 27901da177e4SLinus Torvalds * to work around these arguably broken applications. 27911da177e4SLinus Torvalds */ 2792a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 27931da177e4SLinus Torvalds { 2794a11e1d43SLinus Torvalds __poll_t mask = datagram_poll(file, sock, wait); 27951da177e4SLinus Torvalds struct sock *sk = sock->sk; 27961da177e4SLinus Torvalds 27973ef7cf57SEric Dumazet if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) 2798a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 27992276f58aSPaolo Abeni 28001da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 2801a11e1d43SLinus Torvalds if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2802e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2803a9a08845SLinus Torvalds mask &= ~(EPOLLIN | EPOLLRDNORM); 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds return mask; 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds } 2808a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll); 28091da177e4SLinus Torvalds 28105d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 28115d77dca8SDavid Ahern { 28125d77dca8SDavid Ahern lock_sock(sk); 28135d77dca8SDavid Ahern 28145d77dca8SDavid Ahern sk->sk_err = err; 28155d77dca8SDavid Ahern sk->sk_error_report(sk); 2816286c72deSEric Dumazet __udp_disconnect(sk, 0); 28175d77dca8SDavid Ahern 28185d77dca8SDavid Ahern release_sock(sk); 28195d77dca8SDavid Ahern 28205d77dca8SDavid Ahern return 0; 28215d77dca8SDavid Ahern } 28225d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 28235d77dca8SDavid Ahern 2824db8dac20SDavid S. Miller struct proto udp_prot = { 2825db8dac20SDavid S. Miller .name = "UDP", 2826db8dac20SDavid S. Miller .owner = THIS_MODULE, 2827db8dac20SDavid S. Miller .close = udp_lib_close, 2828d74bad4eSAndrey Ignatov .pre_connect = udp_pre_connect, 2829db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2830db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2831db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2832850cbaddSPaolo Abeni .init = udp_init_sock, 2833db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2834db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2835db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2836db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2837db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 2838db8dac20SDavid S. Miller .sendpage = udp_sendpage, 28398141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2840db8dac20SDavid S. Miller .hash = udp_lib_hash, 2841db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2842719f8358SEric Dumazet .rehash = udp_v4_rehash, 2843db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 2844db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 2845db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 28461e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 28471e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2848db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 2849645ca708SEric Dumazet .h.udp_table = &udp_table, 28505d77dca8SDavid Ahern .diag_destroy = udp_abort, 2851db8dac20SDavid S. Miller }; 2852c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 28551da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 28561da177e4SLinus Torvalds 2857645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 28581da177e4SLinus Torvalds { 28591da177e4SLinus Torvalds struct sock *sk; 28609e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 28611da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 28626f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 28631da177e4SLinus Torvalds 28649e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 28659e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 28669e8ca27aSYonghong Song else 28679e8ca27aSYonghong Song afinfo = PDE_DATA(file_inode(seq->file)); 28689e8ca27aSYonghong Song 2869a3d2599bSChristoph Hellwig for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; 2870f86dcc5aSEric Dumazet ++state->bucket) { 2871a3d2599bSChristoph Hellwig struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; 2872f86dcc5aSEric Dumazet 2873ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2874f86dcc5aSEric Dumazet continue; 2875f86dcc5aSEric Dumazet 2876645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 2877ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 2878878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 2879a91275efSDaniel Lezcano continue; 28809e8ca27aSYonghong Song if (afinfo->family == AF_UNSPEC || 28819e8ca27aSYonghong Song sk->sk_family == afinfo->family) 28821da177e4SLinus Torvalds goto found; 28831da177e4SLinus Torvalds } 2884645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds sk = NULL; 28871da177e4SLinus Torvalds found: 28881da177e4SLinus Torvalds return sk; 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 28921da177e4SLinus Torvalds { 28939e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 28941da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 28956f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 28961da177e4SLinus Torvalds 28979e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 28989e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 28999e8ca27aSYonghong Song else 29009e8ca27aSYonghong Song afinfo = PDE_DATA(file_inode(seq->file)); 29019e8ca27aSYonghong Song 29021da177e4SLinus Torvalds do { 2903ca065d0cSEric Dumazet sk = sk_next(sk); 29049e8ca27aSYonghong Song } while (sk && (!net_eq(sock_net(sk), net) || 29059e8ca27aSYonghong Song (afinfo->family != AF_UNSPEC && 29069e8ca27aSYonghong Song sk->sk_family != afinfo->family))); 29071da177e4SLinus Torvalds 2908645ca708SEric Dumazet if (!sk) { 2909a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 2910a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 2911645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 29121da177e4SLinus Torvalds } 29131da177e4SLinus Torvalds return sk; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 29171da177e4SLinus Torvalds { 2918645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds if (sk) 29211da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 29221da177e4SLinus Torvalds --pos; 29231da177e4SLinus Torvalds return pos ? NULL : sk; 29241da177e4SLinus Torvalds } 29251da177e4SLinus Torvalds 2926a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos) 29271da177e4SLinus Torvalds { 292830842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 2929f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 293030842f29SVitaly Mayatskikh 2931b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 29321da177e4SLinus Torvalds } 2933a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start); 29341da177e4SLinus Torvalds 2935a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 29361da177e4SLinus Torvalds { 29371da177e4SLinus Torvalds struct sock *sk; 29381da177e4SLinus Torvalds 2939b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 29401da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 29411da177e4SLinus Torvalds else 29421da177e4SLinus Torvalds sk = udp_get_next(seq, v); 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds ++*pos; 29451da177e4SLinus Torvalds return sk; 29461da177e4SLinus Torvalds } 2947a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next); 29481da177e4SLinus Torvalds 2949a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v) 29501da177e4SLinus Torvalds { 29519e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 2952645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 2953645ca708SEric Dumazet 29549e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 29559e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 29569e8ca27aSYonghong Song else 29579e8ca27aSYonghong Song afinfo = PDE_DATA(file_inode(seq->file)); 29589e8ca27aSYonghong Song 2959a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 2960a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 29611da177e4SLinus Torvalds } 2962a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop); 2963db8dac20SDavid S. Miller 2964db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 29655e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2966652586dfSTetsuo Handa int bucket) 2967db8dac20SDavid S. Miller { 2968db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 2969c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 2970c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 2971c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 2972c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 2973db8dac20SDavid S. Miller 2974f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2975ea9a0379SPatrick Talbert " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", 2976db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 297731e6d363SEric Dumazet sk_wmem_alloc_get(sp), 29786c206b20SPaolo Abeni udp_rqueue_get(sp), 2979a7cb5a49SEric W. Biederman 0, 0L, 0, 2980a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 2981a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 298241c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 2983652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 2984db8dac20SDavid S. Miller } 2985db8dac20SDavid S. Miller 2986db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 2987db8dac20SDavid S. Miller { 2988652586dfSTetsuo Handa seq_setwidth(seq, 127); 2989db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 2990652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 2991db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 2992cb61cb9bSEric Dumazet "inode ref pointer drops"); 2993db8dac20SDavid S. Miller else { 2994db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 2995db8dac20SDavid S. Miller 2996652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 2997db8dac20SDavid S. Miller } 2998652586dfSTetsuo Handa seq_pad(seq, '\n'); 2999db8dac20SDavid S. Miller return 0; 3000db8dac20SDavid S. Miller } 3001db8dac20SDavid S. Miller 30025788b3a0SYonghong Song #ifdef CONFIG_BPF_SYSCALL 30035788b3a0SYonghong Song struct bpf_iter__udp { 30045788b3a0SYonghong Song __bpf_md_ptr(struct bpf_iter_meta *, meta); 30055788b3a0SYonghong Song __bpf_md_ptr(struct udp_sock *, udp_sk); 30065788b3a0SYonghong Song uid_t uid __aligned(8); 30075788b3a0SYonghong Song int bucket __aligned(8); 30085788b3a0SYonghong Song }; 30095788b3a0SYonghong Song 30105788b3a0SYonghong Song static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 30115788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 30125788b3a0SYonghong Song { 30135788b3a0SYonghong Song struct bpf_iter__udp ctx; 30145788b3a0SYonghong Song 30155788b3a0SYonghong Song meta->seq_num--; /* skip SEQ_START_TOKEN */ 30165788b3a0SYonghong Song ctx.meta = meta; 30175788b3a0SYonghong Song ctx.udp_sk = udp_sk; 30185788b3a0SYonghong Song ctx.uid = uid; 30195788b3a0SYonghong Song ctx.bucket = bucket; 30205788b3a0SYonghong Song return bpf_iter_run_prog(prog, &ctx); 30215788b3a0SYonghong Song } 30225788b3a0SYonghong Song 30235788b3a0SYonghong Song static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) 30245788b3a0SYonghong Song { 30255788b3a0SYonghong Song struct udp_iter_state *state = seq->private; 30265788b3a0SYonghong Song struct bpf_iter_meta meta; 30275788b3a0SYonghong Song struct bpf_prog *prog; 30285788b3a0SYonghong Song struct sock *sk = v; 30295788b3a0SYonghong Song uid_t uid; 30305788b3a0SYonghong Song 30315788b3a0SYonghong Song if (v == SEQ_START_TOKEN) 30325788b3a0SYonghong Song return 0; 30335788b3a0SYonghong Song 30345788b3a0SYonghong Song uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); 30355788b3a0SYonghong Song meta.seq = seq; 30365788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, false); 30375788b3a0SYonghong Song return udp_prog_seq_show(prog, &meta, v, uid, state->bucket); 30385788b3a0SYonghong Song } 30395788b3a0SYonghong Song 30405788b3a0SYonghong Song static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) 30415788b3a0SYonghong Song { 30425788b3a0SYonghong Song struct bpf_iter_meta meta; 30435788b3a0SYonghong Song struct bpf_prog *prog; 30445788b3a0SYonghong Song 30455788b3a0SYonghong Song if (!v) { 30465788b3a0SYonghong Song meta.seq = seq; 30475788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, true); 30485788b3a0SYonghong Song if (prog) 30495788b3a0SYonghong Song (void)udp_prog_seq_show(prog, &meta, v, 0, 0); 30505788b3a0SYonghong Song } 30515788b3a0SYonghong Song 30525788b3a0SYonghong Song udp_seq_stop(seq, v); 30535788b3a0SYonghong Song } 30545788b3a0SYonghong Song 30555788b3a0SYonghong Song static const struct seq_operations bpf_iter_udp_seq_ops = { 30565788b3a0SYonghong Song .start = udp_seq_start, 30575788b3a0SYonghong Song .next = udp_seq_next, 30585788b3a0SYonghong Song .stop = bpf_iter_udp_seq_stop, 30595788b3a0SYonghong Song .show = bpf_iter_udp_seq_show, 30605788b3a0SYonghong Song }; 30615788b3a0SYonghong Song #endif 30625788b3a0SYonghong Song 3063c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = { 3064a3d2599bSChristoph Hellwig .start = udp_seq_start, 3065a3d2599bSChristoph Hellwig .next = udp_seq_next, 3066a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 3067a3d2599bSChristoph Hellwig .show = udp4_seq_show, 3068a3d2599bSChristoph Hellwig }; 3069c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops); 307073cb88ecSArjan van de Ven 3071db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 3072db8dac20SDavid S. Miller .family = AF_INET, 3073645ca708SEric Dumazet .udp_table = &udp_table, 3074db8dac20SDavid S. Miller }; 3075db8dac20SDavid S. Miller 30762c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 307715439febSPavel Emelyanov { 3078c3506372SChristoph Hellwig if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 3079c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 3080a3d2599bSChristoph Hellwig return -ENOMEM; 3081a3d2599bSChristoph Hellwig return 0; 308215439febSPavel Emelyanov } 308315439febSPavel Emelyanov 30842c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 308515439febSPavel Emelyanov { 3086a3d2599bSChristoph Hellwig remove_proc_entry("udp", net->proc_net); 308715439febSPavel Emelyanov } 308815439febSPavel Emelyanov 308915439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 309015439febSPavel Emelyanov .init = udp4_proc_init_net, 309115439febSPavel Emelyanov .exit = udp4_proc_exit_net, 309215439febSPavel Emelyanov }; 309315439febSPavel Emelyanov 3094db8dac20SDavid S. Miller int __init udp4_proc_init(void) 3095db8dac20SDavid S. Miller { 309615439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 3097db8dac20SDavid S. Miller } 3098db8dac20SDavid S. Miller 3099db8dac20SDavid S. Miller void udp4_proc_exit(void) 3100db8dac20SDavid S. Miller { 310115439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 3102db8dac20SDavid S. Miller } 31031da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 31041da177e4SLinus Torvalds 3105f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 3106f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 3107645ca708SEric Dumazet { 3108413c27d8SEldad Zack ssize_t ret; 3109413c27d8SEldad Zack 3110f86dcc5aSEric Dumazet if (!str) 3111f86dcc5aSEric Dumazet return 0; 3112413c27d8SEldad Zack 3113413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 3114413c27d8SEldad Zack if (ret) 3115413c27d8SEldad Zack return 0; 3116413c27d8SEldad Zack 3117f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 3118f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 3119f86dcc5aSEric Dumazet return 1; 3120f86dcc5aSEric Dumazet } 3121f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 3122645ca708SEric Dumazet 3123f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 3124f86dcc5aSEric Dumazet { 3125f86dcc5aSEric Dumazet unsigned int i; 3126f86dcc5aSEric Dumazet 3127f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 3128512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 3129f86dcc5aSEric Dumazet uhash_entries, 3130f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 3131f86dcc5aSEric Dumazet 0, 3132f86dcc5aSEric Dumazet &table->log, 3133f86dcc5aSEric Dumazet &table->mask, 313431fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 3135f86dcc5aSEric Dumazet 64 * 1024); 313631fe62b9STim Bird 3137512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 3138f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 3139ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 3140fdcc8aa9SEric Dumazet table->hash[i].count = 0; 3141645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 3142645ca708SEric Dumazet } 3143512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 3144ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 3145512615b6SEric Dumazet table->hash2[i].count = 0; 3146512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 3147512615b6SEric Dumazet } 3148645ca708SEric Dumazet } 3149645ca708SEric Dumazet 3150723b8e46STom Herbert u32 udp_flow_hashrnd(void) 3151723b8e46STom Herbert { 3152723b8e46STom Herbert static u32 hashrnd __read_mostly; 3153723b8e46STom Herbert 3154723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 3155723b8e46STom Herbert 3156723b8e46STom Herbert return hashrnd; 3157723b8e46STom Herbert } 3158723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 3159723b8e46STom Herbert 31601e802951STonghao Zhang static void __udp_sysctl_init(struct net *net) 31611e802951STonghao Zhang { 31621e802951STonghao Zhang net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM; 31631e802951STonghao Zhang net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM; 31641e802951STonghao Zhang 31651e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV 31661e802951STonghao Zhang net->ipv4.sysctl_udp_l3mdev_accept = 0; 31671e802951STonghao Zhang #endif 31681e802951STonghao Zhang } 31691e802951STonghao Zhang 31701e802951STonghao Zhang static int __net_init udp_sysctl_init(struct net *net) 31711e802951STonghao Zhang { 31721e802951STonghao Zhang __udp_sysctl_init(net); 31731e802951STonghao Zhang return 0; 31741e802951STonghao Zhang } 31751e802951STonghao Zhang 31761e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = { 31771e802951STonghao Zhang .init = udp_sysctl_init, 31781e802951STonghao Zhang }; 31791e802951STonghao Zhang 31805788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 31815788b3a0SYonghong Song DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, 31825788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 31835788b3a0SYonghong Song 3184f9c79272SYonghong Song static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux) 31855788b3a0SYonghong Song { 31865788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 31875788b3a0SYonghong Song struct udp_seq_afinfo *afinfo; 31885788b3a0SYonghong Song int ret; 31895788b3a0SYonghong Song 31905788b3a0SYonghong Song afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN); 31915788b3a0SYonghong Song if (!afinfo) 31925788b3a0SYonghong Song return -ENOMEM; 31935788b3a0SYonghong Song 31945788b3a0SYonghong Song afinfo->family = AF_UNSPEC; 31955788b3a0SYonghong Song afinfo->udp_table = &udp_table; 31965788b3a0SYonghong Song st->bpf_seq_afinfo = afinfo; 3197f9c79272SYonghong Song ret = bpf_iter_init_seq_net(priv_data, aux); 31985788b3a0SYonghong Song if (ret) 31995788b3a0SYonghong Song kfree(afinfo); 32005788b3a0SYonghong Song return ret; 32015788b3a0SYonghong Song } 32025788b3a0SYonghong Song 32035788b3a0SYonghong Song static void bpf_iter_fini_udp(void *priv_data) 32045788b3a0SYonghong Song { 32055788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 32065788b3a0SYonghong Song 32075788b3a0SYonghong Song kfree(st->bpf_seq_afinfo); 32085788b3a0SYonghong Song bpf_iter_fini_seq_net(priv_data); 32095788b3a0SYonghong Song } 32105788b3a0SYonghong Song 321114fc6bd6SYonghong Song static const struct bpf_iter_seq_info udp_seq_info = { 32125788b3a0SYonghong Song .seq_ops = &bpf_iter_udp_seq_ops, 32135788b3a0SYonghong Song .init_seq_private = bpf_iter_init_udp, 32145788b3a0SYonghong Song .fini_seq_private = bpf_iter_fini_udp, 32155788b3a0SYonghong Song .seq_priv_size = sizeof(struct udp_iter_state), 321614fc6bd6SYonghong Song }; 321714fc6bd6SYonghong Song 321814fc6bd6SYonghong Song static struct bpf_iter_reg udp_reg_info = { 321914fc6bd6SYonghong Song .target = "udp", 32205788b3a0SYonghong Song .ctx_arg_info_size = 1, 32215788b3a0SYonghong Song .ctx_arg_info = { 32225788b3a0SYonghong Song { offsetof(struct bpf_iter__udp, udp_sk), 32235788b3a0SYonghong Song PTR_TO_BTF_ID_OR_NULL }, 32245788b3a0SYonghong Song }, 322514fc6bd6SYonghong Song .seq_info = &udp_seq_info, 32265788b3a0SYonghong Song }; 32275788b3a0SYonghong Song 32285788b3a0SYonghong Song static void __init bpf_iter_register(void) 32295788b3a0SYonghong Song { 3230951cf368SYonghong Song udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP]; 32315788b3a0SYonghong Song if (bpf_iter_reg_target(&udp_reg_info)) 32325788b3a0SYonghong Song pr_warn("Warning: could not register bpf iterator udp\n"); 32335788b3a0SYonghong Song } 32345788b3a0SYonghong Song #endif 32355788b3a0SYonghong Song 323695766fffSHideo Aoki void __init udp_init(void) 323795766fffSHideo Aoki { 3238f03d78dbSEric Dumazet unsigned long limit; 32394b272750SEric Dumazet unsigned int i; 324095766fffSHideo Aoki 3241f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 3242f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 324395766fffSHideo Aoki limit = max(limit, 128UL); 324495766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 324595766fffSHideo Aoki sysctl_udp_mem[1] = limit; 324695766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 324795766fffSHideo Aoki 32481e802951STonghao Zhang __udp_sysctl_init(&init_net); 32494b272750SEric Dumazet 32504b272750SEric Dumazet /* 16 spinlocks per cpu */ 32514b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 32524b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 32534b272750SEric Dumazet GFP_KERNEL); 32544b272750SEric Dumazet if (!udp_busylocks) 32554b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 32564b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 32574b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 32581e802951STonghao Zhang 32591e802951STonghao Zhang if (register_pernet_subsys(&udp_sysctl_ops)) 32601e802951STonghao Zhang panic("UDP: failed to init sysctl parameters.\n"); 32615788b3a0SYonghong Song 32625788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 32635788b3a0SYonghong Song bpf_iter_register(); 32645788b3a0SYonghong Song #endif 326595766fffSHideo Aoki } 3266