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> 10922911fc5SEric Dumazet #include <trace/events/skb.h> 110076bb0c8SEliezer Tamir #include <net/busy_poll.h> 111ba4e58ecSGerrit Renker #include "udp_impl.h" 112e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 113217375a0SEric Dumazet #include <net/addrconf.h> 11460fb9567SPaolo Abeni #include <net/udp_tunnel.h> 1150146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 1160146dca7SSabrina Dubroca #include <net/ipv6_stubs.h> 1170146dca7SSabrina Dubroca #endif 1181da177e4SLinus Torvalds 119f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly; 120645ca708SEric Dumazet EXPORT_SYMBOL(udp_table); 1211da177e4SLinus Torvalds 1228d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly; 12395766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 124c482c568SEric Dumazet 1258d987e5cSEric Dumazet atomic_long_t udp_memory_allocated; 12695766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 12795766fffSHideo Aoki 128f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536 129f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 13098322f22SEric Dumazet 131f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 132645ca708SEric Dumazet const struct udp_hslot *hslot, 13398322f22SEric Dumazet unsigned long *bitmap, 134fe38d2a1SJosef Bacik struct sock *sk, unsigned int log) 13525030a7fSGerrit Renker { 136f24d43c0SEric Dumazet struct sock *sk2; 137ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 13825030a7fSGerrit Renker 139ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 140f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 141f24d43c0SEric Dumazet sk2 != sk && 142d4cada4aSEric Dumazet (bitmap || udp_sk(sk2)->udp_port_hash == num) && 143f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1449d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1459d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 146fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 147df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 148df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 149df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 150df560056SEric Garver if (!bitmap) 151df560056SEric Garver return 0; 152df560056SEric Garver } else { 1534243cdc2SJoe Perches if (!bitmap) 154fc038410SDavid S. Miller return 1; 155df560056SEric Garver __set_bit(udp_sk(sk2)->udp_port_hash >> log, 156df560056SEric Garver bitmap); 157df560056SEric Garver } 1584243cdc2SJoe Perches } 15998322f22SEric Dumazet } 16025030a7fSGerrit Renker return 0; 16125030a7fSGerrit Renker } 16225030a7fSGerrit Renker 16330fff923SEric Dumazet /* 16430fff923SEric Dumazet * Note: we still hold spinlock of primary hash chain, so no other writer 16530fff923SEric Dumazet * can insert/delete a socket with local_port == num 16630fff923SEric Dumazet */ 16730fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num, 16830fff923SEric Dumazet struct udp_hslot *hslot2, 169fe38d2a1SJosef Bacik struct sock *sk) 17030fff923SEric Dumazet { 17130fff923SEric Dumazet struct sock *sk2; 172ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 17330fff923SEric Dumazet int res = 0; 17430fff923SEric Dumazet 17530fff923SEric Dumazet spin_lock(&hslot2->lock); 176ca065d0cSEric Dumazet udp_portaddr_for_each_entry(sk2, &hslot2->head) { 17730fff923SEric Dumazet if (net_eq(sock_net(sk2), net) && 17830fff923SEric Dumazet sk2 != sk && 17930fff923SEric Dumazet (udp_sk(sk2)->udp_port_hash == num) && 18030fff923SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1819d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1829d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 183fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 184df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 185df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 186df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 187df560056SEric Garver res = 0; 188df560056SEric Garver } else { 18930fff923SEric Dumazet res = 1; 190df560056SEric Garver } 19130fff923SEric Dumazet break; 19230fff923SEric Dumazet } 1934243cdc2SJoe Perches } 19430fff923SEric Dumazet spin_unlock(&hslot2->lock); 19530fff923SEric Dumazet return res; 19630fff923SEric Dumazet } 19730fff923SEric Dumazet 198fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 199e32ea7e7SCraig Gallek { 200e32ea7e7SCraig Gallek struct net *net = sock_net(sk); 201e32ea7e7SCraig Gallek kuid_t uid = sock_i_uid(sk); 202e32ea7e7SCraig Gallek struct sock *sk2; 203e32ea7e7SCraig Gallek 204ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 205e32ea7e7SCraig Gallek if (net_eq(sock_net(sk2), net) && 206e32ea7e7SCraig Gallek sk2 != sk && 207e32ea7e7SCraig Gallek sk2->sk_family == sk->sk_family && 208e32ea7e7SCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 209e32ea7e7SCraig Gallek (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 210e32ea7e7SCraig Gallek (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 211e32ea7e7SCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 212fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) { 2132dbb9b9eSMartin KaFai Lau return reuseport_add_sock(sk, sk2, 2142dbb9b9eSMartin KaFai Lau inet_rcv_saddr_any(sk)); 215e32ea7e7SCraig Gallek } 216e32ea7e7SCraig Gallek } 217e32ea7e7SCraig Gallek 2182dbb9b9eSMartin KaFai Lau return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 219e32ea7e7SCraig Gallek } 220e32ea7e7SCraig Gallek 22125030a7fSGerrit Renker /** 2226ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 22325030a7fSGerrit Renker * 22425030a7fSGerrit Renker * @sk: socket struct in question 22525030a7fSGerrit Renker * @snum: port number to look up 22625985edcSLucas De Marchi * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 22730fff923SEric Dumazet * with NULL address 22825030a7fSGerrit Renker */ 2296ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 23030fff923SEric Dumazet unsigned int hash2_nulladdr) 2311da177e4SLinus Torvalds { 232512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 233645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 23425030a7fSGerrit Renker int error = 1; 2353b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 2361da177e4SLinus Torvalds 23732c1da70SStephen Hemminger if (!snum) { 2389088c560SEric Dumazet int low, high, remaining; 23995c96174SEric Dumazet unsigned int rand; 24098322f22SEric Dumazet unsigned short first, last; 24198322f22SEric Dumazet DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 2421da177e4SLinus Torvalds 2430bbf87d8SEric W. Biederman inet_get_local_port_range(net, &low, &high); 244a25de534SAnton Arapov remaining = (high - low) + 1; 245227b60f5SStephen Hemminger 24663862b5bSAruna-Hewapathirane rand = prandom_u32(); 2478fc54f68SDaniel Borkmann first = reciprocal_scale(rand, remaining) + low; 24898322f22SEric Dumazet /* 24998322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 25098322f22SEric Dumazet */ 251f86dcc5aSEric Dumazet rand = (rand | 1) * (udptable->mask + 1); 2525781b235SEric Dumazet last = first + udptable->mask + 1; 2535781b235SEric Dumazet do { 254f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, first); 25598322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 256645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 25798322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 258fe38d2a1SJosef Bacik udptable->log); 25998322f22SEric Dumazet 26098322f22SEric Dumazet snum = first; 26198322f22SEric Dumazet /* 26298322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 26398322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 26498322f22SEric Dumazet * give us randomization and full range coverage. 26598322f22SEric Dumazet */ 2669088c560SEric Dumazet do { 26798322f22SEric Dumazet if (low <= snum && snum <= high && 268e3826f1eSAmerigo Wang !test_bit(snum >> udptable->log, bitmap) && 269122ff243SWANG Cong !inet_is_local_reserved_port(net, snum)) 27098322f22SEric Dumazet goto found; 27198322f22SEric Dumazet snum += rand; 27298322f22SEric Dumazet } while (snum != first); 27398322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 274df560056SEric Garver cond_resched(); 2755781b235SEric Dumazet } while (++first != last); 27698322f22SEric Dumazet goto fail; 277645ca708SEric Dumazet } else { 278f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, snum); 279645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 28030fff923SEric Dumazet if (hslot->count > 10) { 28130fff923SEric Dumazet int exist; 28230fff923SEric Dumazet unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 28330fff923SEric Dumazet 28430fff923SEric Dumazet slot2 &= udptable->mask; 28530fff923SEric Dumazet hash2_nulladdr &= udptable->mask; 28630fff923SEric Dumazet 28730fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, slot2); 28830fff923SEric Dumazet if (hslot->count < hslot2->count) 28930fff923SEric Dumazet goto scan_primary_hash; 29030fff923SEric Dumazet 291fe38d2a1SJosef Bacik exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 29230fff923SEric Dumazet if (!exist && (hash2_nulladdr != slot2)) { 29330fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 29430fff923SEric Dumazet exist = udp_lib_lport_inuse2(net, snum, hslot2, 295fe38d2a1SJosef Bacik sk); 29630fff923SEric Dumazet } 29730fff923SEric Dumazet if (exist) 29830fff923SEric Dumazet goto fail_unlock; 29930fff923SEric Dumazet else 30030fff923SEric Dumazet goto found; 30130fff923SEric Dumazet } 30230fff923SEric Dumazet scan_primary_hash: 303fe38d2a1SJosef Bacik if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 304645ca708SEric Dumazet goto fail_unlock; 305645ca708SEric Dumazet } 30698322f22SEric Dumazet found: 307c720c7e8SEric Dumazet inet_sk(sk)->inet_num = snum; 308d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash = snum; 309d4cada4aSEric Dumazet udp_sk(sk)->udp_portaddr_hash ^= snum; 3101da177e4SLinus Torvalds if (sk_unhashed(sk)) { 311e32ea7e7SCraig Gallek if (sk->sk_reuseport && 312fe38d2a1SJosef Bacik udp_reuseport_add_sock(sk, hslot)) { 313e32ea7e7SCraig Gallek inet_sk(sk)->inet_num = 0; 314e32ea7e7SCraig Gallek udp_sk(sk)->udp_port_hash = 0; 315e32ea7e7SCraig Gallek udp_sk(sk)->udp_portaddr_hash ^= snum; 316e32ea7e7SCraig Gallek goto fail_unlock; 317e32ea7e7SCraig Gallek } 318e32ea7e7SCraig Gallek 319ca065d0cSEric Dumazet sk_add_node_rcu(sk, &hslot->head); 320fdcc8aa9SEric Dumazet hslot->count++; 321c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 322512615b6SEric Dumazet 323512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 324512615b6SEric Dumazet spin_lock(&hslot2->lock); 325d894ba18SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 326d894ba18SCraig Gallek sk->sk_family == AF_INET6) 3271602f49bSDavid S. Miller hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 328d894ba18SCraig Gallek &hslot2->head); 329d894ba18SCraig Gallek else 330ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 331512615b6SEric Dumazet &hslot2->head); 332512615b6SEric Dumazet hslot2->count++; 333512615b6SEric Dumazet spin_unlock(&hslot2->lock); 3341da177e4SLinus Torvalds } 335ca065d0cSEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 33625030a7fSGerrit Renker error = 0; 337645ca708SEric Dumazet fail_unlock: 338645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 3391da177e4SLinus Torvalds fail: 34025030a7fSGerrit Renker return error; 3411da177e4SLinus Torvalds } 342c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port); 3431da177e4SLinus Torvalds 3446ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 345db8dac20SDavid S. Miller { 34630fff923SEric Dumazet unsigned int hash2_nulladdr = 347f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 34830fff923SEric Dumazet unsigned int hash2_partial = 349f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 35030fff923SEric Dumazet 351d4cada4aSEric Dumazet /* precompute partial secondary hash */ 35230fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 353fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 354db8dac20SDavid S. Miller } 355db8dac20SDavid S. Miller 356d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 357d1e37288SSu, Xuemin __be32 saddr, __be16 sport, 358fb74c277SDavid Ahern __be32 daddr, unsigned short hnum, 35973545373STim Beale int dif, int sdif) 360645ca708SEric Dumazet { 36160c04aecSJoe Perches int score; 36260c04aecSJoe Perches struct inet_sock *inet; 3636da5b0f0SMike Manning bool dev_match; 364645ca708SEric Dumazet 36560c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 36660c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 36760c04aecSJoe Perches ipv6_only_sock(sk)) 36860c04aecSJoe Perches return -1; 369645ca708SEric Dumazet 3704cdeeee9SPeter Oskolkov if (sk->sk_rcv_saddr != daddr) 371645ca708SEric Dumazet return -1; 37260c04aecSJoe Perches 3734cdeeee9SPeter Oskolkov score = (sk->sk_family == PF_INET) ? 2 : 1; 3744cdeeee9SPeter Oskolkov 3754cdeeee9SPeter Oskolkov inet = inet_sk(sk); 376c720c7e8SEric Dumazet if (inet->inet_daddr) { 377c720c7e8SEric Dumazet if (inet->inet_daddr != saddr) 378645ca708SEric Dumazet return -1; 379ba418fa3STom Herbert score += 4; 380645ca708SEric Dumazet } 38160c04aecSJoe Perches 382c720c7e8SEric Dumazet if (inet->inet_dport) { 383c720c7e8SEric Dumazet if (inet->inet_dport != sport) 384645ca708SEric Dumazet return -1; 385ba418fa3STom Herbert score += 4; 386645ca708SEric Dumazet } 38760c04aecSJoe Perches 3886da5b0f0SMike Manning dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 3896da5b0f0SMike Manning dif, sdif); 39069678bcdSPaolo Abeni if (!dev_match) 391645ca708SEric Dumazet return -1; 392ba418fa3STom Herbert score += 4; 393fb74c277SDavid Ahern 3947170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 39570da268bSEric Dumazet score++; 396645ca708SEric Dumazet return score; 397645ca708SEric Dumazet } 398645ca708SEric Dumazet 3996eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 40065cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 40165cd8033SHannes Frederic Sowa const __be16 fport) 40265cd8033SHannes Frederic Sowa { 4031bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4041bbdceefSHannes Frederic Sowa 4051bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4061bbdceefSHannes Frederic Sowa 40765cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4081bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 40965cd8033SHannes Frederic Sowa } 41065cd8033SHannes Frederic Sowa 411*7629c73aSJakub Sitnicki static inline struct sock *lookup_reuseport(struct net *net, struct sock *sk, 412*7629c73aSJakub Sitnicki struct sk_buff *skb, 413*7629c73aSJakub Sitnicki __be32 saddr, __be16 sport, 414*7629c73aSJakub Sitnicki __be32 daddr, unsigned short hnum) 415*7629c73aSJakub Sitnicki { 416*7629c73aSJakub Sitnicki struct sock *reuse_sk = NULL; 417*7629c73aSJakub Sitnicki u32 hash; 418*7629c73aSJakub Sitnicki 419*7629c73aSJakub Sitnicki if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 420*7629c73aSJakub Sitnicki hash = udp_ehashfn(net, daddr, hnum, saddr, sport); 421*7629c73aSJakub Sitnicki reuse_sk = reuseport_select_sock(sk, hash, skb, 422*7629c73aSJakub Sitnicki sizeof(struct udphdr)); 423*7629c73aSJakub Sitnicki /* Fall back to scoring if group has connections */ 424*7629c73aSJakub Sitnicki if (reuseport_has_conns(sk, false)) 425*7629c73aSJakub Sitnicki return NULL; 426*7629c73aSJakub Sitnicki } 427*7629c73aSJakub Sitnicki return reuse_sk; 428*7629c73aSJakub Sitnicki } 429*7629c73aSJakub Sitnicki 430d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4315051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4325051ebd2SEric Dumazet __be32 saddr, __be16 sport, 433fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 43473545373STim Beale int dif, int sdif, 435d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4361134158bSCraig Gallek struct sk_buff *skb) 4375051ebd2SEric Dumazet { 4385051ebd2SEric Dumazet struct sock *sk, *result; 439e94a62f5SPaolo Abeni int score, badness; 4405051ebd2SEric Dumazet 4415051ebd2SEric Dumazet result = NULL; 442ba418fa3STom Herbert badness = 0; 443ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 444d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 44573545373STim Beale daddr, hnum, dif, sdif); 4465051ebd2SEric Dumazet if (score > badness) { 447*7629c73aSJakub Sitnicki result = lookup_reuseport(net, sk, skb, 448*7629c73aSJakub Sitnicki saddr, sport, daddr, hnum); 449*7629c73aSJakub Sitnicki if (result) 450ca065d0cSEric Dumazet return result; 451*7629c73aSJakub Sitnicki 452ca065d0cSEric Dumazet badness = score; 453ca065d0cSEric Dumazet result = sk; 4545051ebd2SEric Dumazet } 4555051ebd2SEric Dumazet } 4565051ebd2SEric Dumazet return result; 4575051ebd2SEric Dumazet } 4585051ebd2SEric Dumazet 459db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 460db8dac20SDavid S. Miller * harder than this. -DaveM 461db8dac20SDavid S. Miller */ 462fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 463fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 464fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 465db8dac20SDavid S. Miller { 4664cdeeee9SPeter Oskolkov struct sock *result; 467db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4684cdeeee9SPeter Oskolkov unsigned int hash2, slot2; 4694cdeeee9SPeter Oskolkov struct udp_hslot *hslot2; 470db8dac20SDavid S. Miller 471f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 4725051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4735051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4745051ebd2SEric Dumazet 4755051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 476fb74c277SDavid Ahern daddr, hnum, dif, sdif, 47773545373STim Beale hslot2, skb); 4785051ebd2SEric Dumazet if (!result) { 479f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 4805051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4815051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4825051ebd2SEric Dumazet 4831223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 4844cdeeee9SPeter Oskolkov htonl(INADDR_ANY), hnum, dif, sdif, 48573545373STim Beale hslot2, skb); 4865051ebd2SEric Dumazet } 48788e235b8SEnrico Weigelt if (IS_ERR(result)) 4888217ca65SMartin KaFai Lau return NULL; 4895051ebd2SEric Dumazet return result; 4905051ebd2SEric Dumazet } 491fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 492db8dac20SDavid S. Miller 493607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 494607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 495645ca708SEric Dumazet struct udp_table *udptable) 496607c4aafSKOVACS Krisztian { 497607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 498607c4aafSKOVACS Krisztian 499ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 500607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 501fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 502607c4aafSKOVACS Krisztian } 503607c4aafSKOVACS Krisztian 50463058308STom Herbert struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, 50563058308STom Herbert __be16 sport, __be16 dport) 50663058308STom Herbert { 507257a525fSMartin KaFai Lau const struct iphdr *iph = ip_hdr(skb); 508257a525fSMartin KaFai Lau 509257a525fSMartin KaFai Lau return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 510257a525fSMartin KaFai Lau iph->daddr, dport, inet_iif(skb), 511257a525fSMartin KaFai Lau inet_sdif(skb), &udp_table, NULL); 51263058308STom Herbert } 51363058308STom Herbert EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); 51463058308STom Herbert 515ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 516ca065d0cSEric Dumazet * Does increment socket refcount. 517ca065d0cSEric Dumazet */ 5186e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 519bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 520bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 521bcd41303SKOVACS Krisztian { 522ca065d0cSEric Dumazet struct sock *sk; 523ca065d0cSEric Dumazet 524ca065d0cSEric Dumazet sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 525fb74c277SDavid Ahern dif, 0, &udp_table, NULL); 52641c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 527ca065d0cSEric Dumazet sk = NULL; 528ca065d0cSEric Dumazet return sk; 529bcd41303SKOVACS Krisztian } 530bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 531ca065d0cSEric Dumazet #endif 532bcd41303SKOVACS Krisztian 533421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, 534421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 535421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 536fb74c277SDavid Ahern int dif, int sdif, unsigned short hnum) 537421b3885SShawn Bohrer { 538421b3885SShawn Bohrer struct inet_sock *inet = inet_sk(sk); 539421b3885SShawn Bohrer 540421b3885SShawn Bohrer if (!net_eq(sock_net(sk), net) || 541421b3885SShawn Bohrer udp_sk(sk)->udp_port_hash != hnum || 542421b3885SShawn Bohrer (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 543421b3885SShawn Bohrer (inet->inet_dport != rmt_port && inet->inet_dport) || 544421b3885SShawn Bohrer (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 545421b3885SShawn Bohrer ipv6_only_sock(sk) || 54682ba25c6STim Beale !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 547421b3885SShawn Bohrer return false; 54860d9b031SDavid Ahern if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 549421b3885SShawn Bohrer return false; 550421b3885SShawn Bohrer return true; 551421b3885SShawn Bohrer } 552421b3885SShawn Bohrer 553a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 554a36e185eSStefano Brivio void udp_encap_enable(void) 555a36e185eSStefano Brivio { 5569c480601SPaolo Abeni static_branch_inc(&udp_encap_needed_key); 557a36e185eSStefano Brivio } 558a36e185eSStefano Brivio EXPORT_SYMBOL(udp_encap_enable); 559a36e185eSStefano Brivio 560e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 561e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 562e7cc0824SStefano Brivio */ 563e7cc0824SStefano Brivio static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) 564e7cc0824SStefano Brivio { 565e7cc0824SStefano Brivio int i; 566e7cc0824SStefano Brivio 567e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 568e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, u32 info); 56992b95364SPaolo Abeni const struct ip_tunnel_encap_ops *encap; 570e7cc0824SStefano Brivio 57192b95364SPaolo Abeni encap = rcu_dereference(iptun_encaps[i]); 57292b95364SPaolo Abeni if (!encap) 573e7cc0824SStefano Brivio continue; 57492b95364SPaolo Abeni handler = encap->err_handler; 575e7cc0824SStefano Brivio if (handler && !handler(skb, info)) 576e7cc0824SStefano Brivio return 0; 577e7cc0824SStefano Brivio } 578e7cc0824SStefano Brivio 579e7cc0824SStefano Brivio return -ENOENT; 580e7cc0824SStefano Brivio } 581e7cc0824SStefano Brivio 582a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 583a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 584a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 585a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 586a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 587a36e185eSStefano Brivio * trace ICMP messages back to them. 588a36e185eSStefano Brivio * 589e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 590e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 591e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 592e7cc0824SStefano Brivio * 593a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 594a36e185eSStefano Brivio * association. 595a36e185eSStefano Brivio * 596e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 597e7cc0824SStefano Brivio * processing, zero otherwise. 598a36e185eSStefano Brivio */ 599a36e185eSStefano Brivio static struct sock *__udp4_lib_err_encap(struct net *net, 600a36e185eSStefano Brivio const struct iphdr *iph, 601a36e185eSStefano Brivio struct udphdr *uh, 602a36e185eSStefano Brivio struct udp_table *udptable, 603e7cc0824SStefano Brivio struct sk_buff *skb, u32 info) 604a36e185eSStefano Brivio { 605a36e185eSStefano Brivio int network_offset, transport_offset; 606a36e185eSStefano Brivio struct sock *sk; 607a36e185eSStefano Brivio 608a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 609a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 610a36e185eSStefano Brivio 611a36e185eSStefano Brivio /* Network header needs to point to the outer IPv4 header inside ICMP */ 612a36e185eSStefano Brivio skb_reset_network_header(skb); 613a36e185eSStefano Brivio 614a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 615a36e185eSStefano Brivio skb_set_transport_header(skb, iph->ihl << 2); 616a36e185eSStefano Brivio 617e7cc0824SStefano Brivio sk = __udp4_lib_lookup(net, iph->daddr, uh->source, 618e7cc0824SStefano Brivio iph->saddr, uh->dest, skb->dev->ifindex, 0, 619e7cc0824SStefano Brivio udptable, NULL); 620e7cc0824SStefano Brivio if (sk) { 621e7cc0824SStefano Brivio int (*lookup)(struct sock *sk, struct sk_buff *skb); 622e7cc0824SStefano Brivio struct udp_sock *up = udp_sk(sk); 623e7cc0824SStefano Brivio 624a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 625a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 626a36e185eSStefano Brivio sk = NULL; 627e7cc0824SStefano Brivio } 628e7cc0824SStefano Brivio 629e7cc0824SStefano Brivio if (!sk) 630e7cc0824SStefano Brivio sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); 631a36e185eSStefano Brivio 632a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 633a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 634a36e185eSStefano Brivio 635a36e185eSStefano Brivio return sk; 636a36e185eSStefano Brivio } 637a36e185eSStefano Brivio 638db8dac20SDavid S. Miller /* 639db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 640db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 641db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 642db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 643db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 644db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 645db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 646db8dac20SDavid S. Miller * to find the appropriate port. 647db8dac20SDavid S. Miller */ 648db8dac20SDavid S. Miller 64932bbd879SStefano Brivio int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 650db8dac20SDavid S. Miller { 651db8dac20SDavid S. Miller struct inet_sock *inet; 652b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 653db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 654db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 655db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 656a36e185eSStefano Brivio bool tunnel = false; 657db8dac20SDavid S. Miller struct sock *sk; 658db8dac20SDavid S. Miller int harderr; 659db8dac20SDavid S. Miller int err; 660fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 661db8dac20SDavid S. Miller 662fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 663f64bf6b8SMike Manning iph->saddr, uh->source, skb->dev->ifindex, 664f64bf6b8SMike Manning inet_sdif(skb), udptable, NULL); 66551456b29SIan Morris if (!sk) { 666a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 667e7cc0824SStefano Brivio sk = ERR_PTR(-ENOENT); 668e7cc0824SStefano Brivio if (static_branch_unlikely(&udp_encap_needed_key)) { 669e7cc0824SStefano Brivio sk = __udp4_lib_err_encap(net, iph, uh, udptable, skb, 670e7cc0824SStefano Brivio info); 671e7cc0824SStefano Brivio if (!sk) 672e7cc0824SStefano Brivio return 0; 673a36e185eSStefano Brivio } 674e7cc0824SStefano Brivio 675e7cc0824SStefano Brivio if (IS_ERR(sk)) { 676e7cc0824SStefano Brivio __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 677e7cc0824SStefano Brivio return PTR_ERR(sk); 678e7cc0824SStefano Brivio } 679e7cc0824SStefano Brivio 680a36e185eSStefano Brivio tunnel = true; 681db8dac20SDavid S. Miller } 682db8dac20SDavid S. Miller 683db8dac20SDavid S. Miller err = 0; 684db8dac20SDavid S. Miller harderr = 0; 685db8dac20SDavid S. Miller inet = inet_sk(sk); 686db8dac20SDavid S. Miller 687db8dac20SDavid S. Miller switch (type) { 688db8dac20SDavid S. Miller default: 689db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 690db8dac20SDavid S. Miller err = EHOSTUNREACH; 691db8dac20SDavid S. Miller break; 692db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 693db8dac20SDavid S. Miller goto out; 694db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 695db8dac20SDavid S. Miller err = EPROTO; 696db8dac20SDavid S. Miller harderr = 1; 697db8dac20SDavid S. Miller break; 698db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 699db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 70036393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 701db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 702db8dac20SDavid S. Miller err = EMSGSIZE; 703db8dac20SDavid S. Miller harderr = 1; 704db8dac20SDavid S. Miller break; 705db8dac20SDavid S. Miller } 706db8dac20SDavid S. Miller goto out; 707db8dac20SDavid S. Miller } 708db8dac20SDavid S. Miller err = EHOSTUNREACH; 709db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 710db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 711db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 712db8dac20SDavid S. Miller } 713db8dac20SDavid S. Miller break; 71455be7a9cSDavid S. Miller case ICMP_REDIRECT: 71555be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 7161a462d18SDuan Jiong goto out; 717db8dac20SDavid S. Miller } 718db8dac20SDavid S. Miller 719db8dac20SDavid S. Miller /* 720db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 721db8dac20SDavid S. Miller * 4.1.3.3. 722db8dac20SDavid S. Miller */ 723a36e185eSStefano Brivio if (tunnel) { 724a36e185eSStefano Brivio /* ...not for tunnels though: we don't have a sending socket */ 725a36e185eSStefano Brivio goto out; 726a36e185eSStefano Brivio } 727db8dac20SDavid S. Miller if (!inet->recverr) { 728db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 729db8dac20SDavid S. Miller goto out; 730b1faf566SEric Dumazet } else 731db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 732b1faf566SEric Dumazet 733db8dac20SDavid S. Miller sk->sk_err = err; 734db8dac20SDavid S. Miller sk->sk_error_report(sk); 735db8dac20SDavid S. Miller out: 73632bbd879SStefano Brivio return 0; 737db8dac20SDavid S. Miller } 738db8dac20SDavid S. Miller 73932bbd879SStefano Brivio int udp_err(struct sk_buff *skb, u32 info) 740db8dac20SDavid S. Miller { 74132bbd879SStefano Brivio return __udp4_lib_err(skb, info, &udp_table); 742db8dac20SDavid S. Miller } 743db8dac20SDavid S. Miller 744db8dac20SDavid S. Miller /* 745db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 746db8dac20SDavid S. Miller */ 74736d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 748db8dac20SDavid S. Miller { 749db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 750db8dac20SDavid S. Miller 751db8dac20SDavid S. Miller if (up->pending) { 752db8dac20SDavid S. Miller up->len = 0; 753db8dac20SDavid S. Miller up->pending = 0; 754db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 755db8dac20SDavid S. Miller } 756db8dac20SDavid S. Miller } 75736d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 758db8dac20SDavid S. Miller 759db8dac20SDavid S. Miller /** 760f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 761db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 762db8dac20SDavid S. Miller * (checksum field must be zeroed out) 763f6b9664fSHerbert Xu * @src: source IP address 764f6b9664fSHerbert Xu * @dst: destination IP address 765db8dac20SDavid S. Miller */ 766c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 767db8dac20SDavid S. Miller { 768db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 769f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 770f6b9664fSHerbert Xu int len = skb->len - offset; 771f6b9664fSHerbert Xu int hlen = len; 772db8dac20SDavid S. Miller __wsum csum = 0; 773db8dac20SDavid S. Miller 774ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 775db8dac20SDavid S. Miller /* 776db8dac20SDavid S. Miller * Only one fragment on the socket. 777db8dac20SDavid S. Miller */ 778db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 779db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 780f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 781f6b9664fSHerbert Xu IPPROTO_UDP, 0); 782db8dac20SDavid S. Miller } else { 783ebbe495fSWANG Cong struct sk_buff *frags; 784ebbe495fSWANG Cong 785db8dac20SDavid S. Miller /* 786db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 787db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 788db8dac20SDavid S. Miller * should be together 789db8dac20SDavid S. Miller */ 790ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 791f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 792f6b9664fSHerbert Xu hlen -= frags->len; 793ebbe495fSWANG Cong } 794db8dac20SDavid S. Miller 795f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 796db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 797db8dac20SDavid S. Miller 798db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 799db8dac20SDavid S. Miller if (uh->check == 0) 800db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 801db8dac20SDavid S. Miller } 802db8dac20SDavid S. Miller } 803c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 804db8dac20SDavid S. Miller 805af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 806af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 807af5fcba7STom Herbert */ 808af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 809af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 810af5fcba7STom Herbert { 811af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 812af5fcba7STom Herbert 813179bc67fSEdward Cree if (nocheck) { 814af5fcba7STom Herbert uh->check = 0; 815179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 816af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 817179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 818179bc67fSEdward Cree uh->check = 0; 819179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 820179bc67fSEdward Cree if (uh->check == 0) 821179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 822d75f1306SEdward Cree } else { 823af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 824af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 825af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 826af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 827af5fcba7STom Herbert } 828af5fcba7STom Herbert } 829af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 830af5fcba7STom Herbert 831bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 832bec1f6f6SWillem de Bruijn struct inet_cork *cork) 833f6b9664fSHerbert Xu { 834f6b9664fSHerbert Xu struct sock *sk = skb->sk; 835f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 836f6b9664fSHerbert Xu struct udphdr *uh; 837f6b9664fSHerbert Xu int err = 0; 838f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 839f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 840f6b9664fSHerbert Xu int len = skb->len - offset; 8414094871dSJosh Hunt int datalen = len - sizeof(*uh); 842f6b9664fSHerbert Xu __wsum csum = 0; 843f6b9664fSHerbert Xu 844f6b9664fSHerbert Xu /* 845f6b9664fSHerbert Xu * Create a UDP header 846f6b9664fSHerbert Xu */ 847f6b9664fSHerbert Xu uh = udp_hdr(skb); 848f6b9664fSHerbert Xu uh->source = inet->inet_sport; 84979ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 850f6b9664fSHerbert Xu uh->len = htons(len); 851f6b9664fSHerbert Xu uh->check = 0; 852f6b9664fSHerbert Xu 853bec1f6f6SWillem de Bruijn if (cork->gso_size) { 854bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 855bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 856bec1f6f6SWillem de Bruijn 8570f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 8580f149c9fSWillem de Bruijn kfree_skb(skb); 859bec1f6f6SWillem de Bruijn return -EINVAL; 8600f149c9fSWillem de Bruijn } 8610f149c9fSWillem de Bruijn if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) { 8620f149c9fSWillem de Bruijn kfree_skb(skb); 863bec1f6f6SWillem de Bruijn return -EINVAL; 8640f149c9fSWillem de Bruijn } 8650f149c9fSWillem de Bruijn if (sk->sk_no_check_tx) { 8660f149c9fSWillem de Bruijn kfree_skb(skb); 867a8c744a8SWillem de Bruijn return -EINVAL; 8680f149c9fSWillem de Bruijn } 869ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 8700f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 8710f149c9fSWillem de Bruijn kfree_skb(skb); 872bec1f6f6SWillem de Bruijn return -EIO; 8730f149c9fSWillem de Bruijn } 874bec1f6f6SWillem de Bruijn 8754094871dSJosh Hunt if (datalen > cork->gso_size) { 876bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 877bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 8784094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 879dfec0ee2SAlexander Duyck cork->gso_size); 8804094871dSJosh Hunt } 881a8c744a8SWillem de Bruijn goto csum_partial; 882bec1f6f6SWillem de Bruijn } 883bec1f6f6SWillem de Bruijn 884f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 885f6b9664fSHerbert Xu csum = udplite_csum(skb); 886f6b9664fSHerbert Xu 887ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 888f6b9664fSHerbert Xu 889f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 890f6b9664fSHerbert Xu goto send; 891f6b9664fSHerbert Xu 892f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 893a8c744a8SWillem de Bruijn csum_partial: 894f6b9664fSHerbert Xu 89579ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 896f6b9664fSHerbert Xu goto send; 897f6b9664fSHerbert Xu 898f6b9664fSHerbert Xu } else 899f6b9664fSHerbert Xu csum = udp_csum(skb); 900f6b9664fSHerbert Xu 901f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 90279ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 903f6b9664fSHerbert Xu sk->sk_protocol, csum); 904f6b9664fSHerbert Xu if (uh->check == 0) 905f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 906f6b9664fSHerbert Xu 907f6b9664fSHerbert Xu send: 908b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 909f6b9664fSHerbert Xu if (err) { 910f6b9664fSHerbert Xu if (err == -ENOBUFS && !inet->recverr) { 9116aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 912f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 913f6b9664fSHerbert Xu err = 0; 914f6b9664fSHerbert Xu } 915f6b9664fSHerbert Xu } else 9166aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 917f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 918f6b9664fSHerbert Xu return err; 919f6b9664fSHerbert Xu } 920f6b9664fSHerbert Xu 921db8dac20SDavid S. Miller /* 922db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 923db8dac20SDavid S. Miller */ 9248822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 925db8dac20SDavid S. Miller { 926db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 927db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 928b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 929db8dac20SDavid S. Miller struct sk_buff *skb; 930db8dac20SDavid S. Miller int err = 0; 931db8dac20SDavid S. Miller 93277968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 933f6b9664fSHerbert Xu if (!skb) 934db8dac20SDavid S. Miller goto out; 935db8dac20SDavid S. Miller 936bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &inet->cork.base); 937db8dac20SDavid S. Miller 938db8dac20SDavid S. Miller out: 939db8dac20SDavid S. Miller up->len = 0; 940db8dac20SDavid S. Miller up->pending = 0; 941db8dac20SDavid S. Miller return err; 942db8dac20SDavid S. Miller } 9438822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 944db8dac20SDavid S. Miller 9452e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 9462e8de857SWillem de Bruijn { 9472e8de857SWillem de Bruijn switch (cmsg->cmsg_type) { 9482e8de857SWillem de Bruijn case UDP_SEGMENT: 9492e8de857SWillem de Bruijn if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 9502e8de857SWillem de Bruijn return -EINVAL; 9512e8de857SWillem de Bruijn *gso_size = *(__u16 *)CMSG_DATA(cmsg); 9522e8de857SWillem de Bruijn return 0; 9532e8de857SWillem de Bruijn default: 9542e8de857SWillem de Bruijn return -EINVAL; 9552e8de857SWillem de Bruijn } 9562e8de857SWillem de Bruijn } 9572e8de857SWillem de Bruijn 9582e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 9592e8de857SWillem de Bruijn { 9602e8de857SWillem de Bruijn struct cmsghdr *cmsg; 9612e8de857SWillem de Bruijn bool need_ip = false; 9622e8de857SWillem de Bruijn int err; 9632e8de857SWillem de Bruijn 9642e8de857SWillem de Bruijn for_each_cmsghdr(cmsg, msg) { 9652e8de857SWillem de Bruijn if (!CMSG_OK(msg, cmsg)) 9662e8de857SWillem de Bruijn return -EINVAL; 9672e8de857SWillem de Bruijn 9682e8de857SWillem de Bruijn if (cmsg->cmsg_level != SOL_UDP) { 9692e8de857SWillem de Bruijn need_ip = true; 9702e8de857SWillem de Bruijn continue; 9712e8de857SWillem de Bruijn } 9722e8de857SWillem de Bruijn 9732e8de857SWillem de Bruijn err = __udp_cmsg_send(cmsg, gso_size); 9742e8de857SWillem de Bruijn if (err) 9752e8de857SWillem de Bruijn return err; 9762e8de857SWillem de Bruijn } 9772e8de857SWillem de Bruijn 9782e8de857SWillem de Bruijn return need_ip; 9792e8de857SWillem de Bruijn } 9802e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send); 9812e8de857SWillem de Bruijn 9821b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 983db8dac20SDavid S. Miller { 984db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 985db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 9861cedee13SAndrey Ignatov DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 987e474995fSDavid S. Miller struct flowi4 fl4_stack; 988b6f21b26SDavid S. Miller struct flowi4 *fl4; 989db8dac20SDavid S. Miller int ulen = len; 990db8dac20SDavid S. Miller struct ipcm_cookie ipc; 991db8dac20SDavid S. Miller struct rtable *rt = NULL; 992db8dac20SDavid S. Miller int free = 0; 993db8dac20SDavid S. Miller int connected = 0; 994db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 995db8dac20SDavid S. Miller __be16 dport; 996db8dac20SDavid S. Miller u8 tos; 997db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 998db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 999db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1000903ab86dSHerbert Xu struct sk_buff *skb; 1001f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 1002db8dac20SDavid S. Miller 1003db8dac20SDavid S. Miller if (len > 0xFFFF) 1004db8dac20SDavid S. Miller return -EMSGSIZE; 1005db8dac20SDavid S. Miller 1006db8dac20SDavid S. Miller /* 1007db8dac20SDavid S. Miller * Check the flags. 1008db8dac20SDavid S. Miller */ 1009db8dac20SDavid S. Miller 1010db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 1011db8dac20SDavid S. Miller return -EOPNOTSUPP; 1012db8dac20SDavid S. Miller 1013903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1014903ab86dSHerbert Xu 1015f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1016db8dac20SDavid S. Miller if (up->pending) { 1017db8dac20SDavid S. Miller /* 1018db8dac20SDavid S. Miller * There are pending frames. 1019db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1020db8dac20SDavid S. Miller */ 1021db8dac20SDavid S. Miller lock_sock(sk); 1022db8dac20SDavid S. Miller if (likely(up->pending)) { 1023db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 1024db8dac20SDavid S. Miller release_sock(sk); 1025db8dac20SDavid S. Miller return -EINVAL; 1026db8dac20SDavid S. Miller } 1027db8dac20SDavid S. Miller goto do_append_data; 1028db8dac20SDavid S. Miller } 1029db8dac20SDavid S. Miller release_sock(sk); 1030db8dac20SDavid S. Miller } 1031db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1032db8dac20SDavid S. Miller 1033db8dac20SDavid S. Miller /* 1034db8dac20SDavid S. Miller * Get and verify the address. 1035db8dac20SDavid S. Miller */ 10361cedee13SAndrey Ignatov if (usin) { 1037db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 1038db8dac20SDavid S. Miller return -EINVAL; 1039db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 1040db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 1041db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1042db8dac20SDavid S. Miller } 1043db8dac20SDavid S. Miller 1044db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 1045db8dac20SDavid S. Miller dport = usin->sin_port; 1046db8dac20SDavid S. Miller if (dport == 0) 1047db8dac20SDavid S. Miller return -EINVAL; 1048db8dac20SDavid S. Miller } else { 1049db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1050db8dac20SDavid S. Miller return -EDESTADDRREQ; 1051c720c7e8SEric Dumazet daddr = inet->inet_daddr; 1052c720c7e8SEric Dumazet dport = inet->inet_dport; 1053db8dac20SDavid S. Miller /* Open fast path for connected socket. 1054db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 1055db8dac20SDavid S. Miller */ 1056db8dac20SDavid S. Miller connected = 1; 1057db8dac20SDavid S. Miller } 1058c14ac945SSoheil Hassas Yeganeh 105935178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 1060bec1f6f6SWillem de Bruijn ipc.gso_size = up->gso_size; 1061bf84a010SDaniel Borkmann 1062db8dac20SDavid S. Miller if (msg->msg_controllen) { 10632e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc.gso_size); 10642e8de857SWillem de Bruijn if (err > 0) 10652e8de857SWillem de Bruijn err = ip_cmsg_send(sk, msg, &ipc, 10662e8de857SWillem de Bruijn sk->sk_family == AF_INET6); 10672e8de857SWillem de Bruijn if (unlikely(err < 0)) { 106891948309SEric Dumazet kfree(ipc.opt); 1069db8dac20SDavid S. Miller return err; 107091948309SEric Dumazet } 1071db8dac20SDavid S. Miller if (ipc.opt) 1072db8dac20SDavid S. Miller free = 1; 1073db8dac20SDavid S. Miller connected = 0; 1074db8dac20SDavid S. Miller } 1075f6d8bd05SEric Dumazet if (!ipc.opt) { 1076f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 1077f6d8bd05SEric Dumazet 1078f6d8bd05SEric Dumazet rcu_read_lock(); 1079f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1080f6d8bd05SEric Dumazet if (inet_opt) { 1081f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 1082f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 1083f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 1084f6d8bd05SEric Dumazet } 1085f6d8bd05SEric Dumazet rcu_read_unlock(); 1086f6d8bd05SEric Dumazet } 1087db8dac20SDavid S. Miller 10881cedee13SAndrey Ignatov if (cgroup_bpf_enabled && !connected) { 10891cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 10901cedee13SAndrey Ignatov (struct sockaddr *)usin, &ipc.addr); 10911cedee13SAndrey Ignatov if (err) 10921cedee13SAndrey Ignatov goto out_free; 10931cedee13SAndrey Ignatov if (usin) { 10941cedee13SAndrey Ignatov if (usin->sin_port == 0) { 10951cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 10961cedee13SAndrey Ignatov err = -EINVAL; 10971cedee13SAndrey Ignatov goto out_free; 10981cedee13SAndrey Ignatov } 10991cedee13SAndrey Ignatov daddr = usin->sin_addr.s_addr; 11001cedee13SAndrey Ignatov dport = usin->sin_port; 11011cedee13SAndrey Ignatov } 11021cedee13SAndrey Ignatov } 11031cedee13SAndrey Ignatov 1104db8dac20SDavid S. Miller saddr = ipc.addr; 1105db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 1106db8dac20SDavid S. Miller 1107f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 11081b97013bSAndrey Ignatov if (!daddr) { 11091b97013bSAndrey Ignatov err = -EINVAL; 11101b97013bSAndrey Ignatov goto out_free; 11111b97013bSAndrey Ignatov } 1112f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 1113db8dac20SDavid S. Miller connected = 0; 1114db8dac20SDavid S. Miller } 1115aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 1116db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 1117db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 1118f6d8bd05SEric Dumazet (ipc.opt && ipc.opt->opt.is_strictroute)) { 1119db8dac20SDavid S. Miller tos |= RTO_ONLINK; 1120db8dac20SDavid S. Miller connected = 0; 1121db8dac20SDavid S. Miller } 1122db8dac20SDavid S. Miller 1123db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 1124854da991SRobert Shearman if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 1125db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 1126db8dac20SDavid S. Miller if (!saddr) 1127db8dac20SDavid S. Miller saddr = inet->mc_addr; 1128db8dac20SDavid S. Miller connected = 0; 11299515a2e0SDavid Ahern } else if (!ipc.oif) { 113076e21053SErich E. Hoover ipc.oif = inet->uc_index; 11319515a2e0SDavid Ahern } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 11329515a2e0SDavid Ahern /* oif is set, packet is to local broadcast and 11339515a2e0SDavid Ahern * and uc_index is set. oif is most likely set 11349515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 11359515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 11369515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 11379515a2e0SDavid Ahern */ 11389515a2e0SDavid Ahern if (ipc.oif != inet->uc_index && 11399515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 11409515a2e0SDavid Ahern inet->uc_index)) { 11419515a2e0SDavid Ahern ipc.oif = inet->uc_index; 11429515a2e0SDavid Ahern } 11439515a2e0SDavid Ahern } 1144db8dac20SDavid S. Miller 1145db8dac20SDavid S. Miller if (connected) 1146db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 1147db8dac20SDavid S. Miller 114851456b29SIan Morris if (!rt) { 114984a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 11509a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 115184a3aa00SPavel Emelyanov 1152e474995fSDavid S. Miller fl4 = &fl4_stack; 11539a24abfaSDavid Ahern 1154c6af0c22SWillem de Bruijn flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, 11559a24abfaSDavid Ahern RT_SCOPE_UNIVERSE, sk->sk_protocol, 11569a24abfaSDavid Ahern flow_flags, 1157e2d118a1SLorenzo Colitti faddr, saddr, dport, inet->inet_sport, 1158e2d118a1SLorenzo Colitti sk->sk_uid); 1159c0951cbcSDavid S. Miller 1160e474995fSDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 1161e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1162b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1163b23dd4feSDavid S. Miller err = PTR_ERR(rt); 116406dc94b1SDavid S. Miller rt = NULL; 1165db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1166f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1167db8dac20SDavid S. Miller goto out; 1168db8dac20SDavid S. Miller } 1169db8dac20SDavid S. Miller 1170db8dac20SDavid S. Miller err = -EACCES; 1171db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1172db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1173db8dac20SDavid S. Miller goto out; 1174db8dac20SDavid S. Miller if (connected) 1175d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1176db8dac20SDavid S. Miller } 1177db8dac20SDavid S. Miller 1178db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1179db8dac20SDavid S. Miller goto do_confirm; 1180db8dac20SDavid S. Miller back_from_confirm: 1181db8dac20SDavid S. Miller 1182e474995fSDavid S. Miller saddr = fl4->saddr; 1183db8dac20SDavid S. Miller if (!ipc.addr) 1184e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1185db8dac20SDavid S. Miller 1186903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1187903ab86dSHerbert Xu if (!corkreq) { 11881cd7884dSWillem de Bruijn struct inet_cork cork; 11891cd7884dSWillem de Bruijn 1190f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1191903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 11921cd7884dSWillem de Bruijn &cork, msg->msg_flags); 1193903ab86dSHerbert Xu err = PTR_ERR(skb); 119450c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 1195bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &cork); 1196903ab86dSHerbert Xu goto out; 1197903ab86dSHerbert Xu } 1198903ab86dSHerbert Xu 1199db8dac20SDavid S. Miller lock_sock(sk); 1200db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1201db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1202db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1203db8dac20SDavid S. Miller release_sock(sk); 1204db8dac20SDavid S. Miller 1205197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1206db8dac20SDavid S. Miller err = -EINVAL; 1207db8dac20SDavid S. Miller goto out; 1208db8dac20SDavid S. Miller } 1209db8dac20SDavid S. Miller /* 1210db8dac20SDavid S. Miller * Now cork the socket to pend data. 1211db8dac20SDavid S. Miller */ 1212b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1213b6f21b26SDavid S. Miller fl4->daddr = daddr; 1214b6f21b26SDavid S. Miller fl4->saddr = saddr; 12159cce96dfSDavid S. Miller fl4->fl4_dport = dport; 12169cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1217db8dac20SDavid S. Miller up->pending = AF_INET; 1218db8dac20SDavid S. Miller 1219db8dac20SDavid S. Miller do_append_data: 1220db8dac20SDavid S. Miller up->len += ulen; 1221f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 12222e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1223db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1224db8dac20SDavid S. Miller if (err) 1225db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1226db8dac20SDavid S. Miller else if (!corkreq) 1227db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1228db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1229db8dac20SDavid S. Miller up->pending = 0; 1230db8dac20SDavid S. Miller release_sock(sk); 1231db8dac20SDavid S. Miller 1232db8dac20SDavid S. Miller out: 1233db8dac20SDavid S. Miller ip_rt_put(rt); 12341b97013bSAndrey Ignatov out_free: 1235db8dac20SDavid S. Miller if (free) 1236db8dac20SDavid S. Miller kfree(ipc.opt); 1237db8dac20SDavid S. Miller if (!err) 1238db8dac20SDavid S. Miller return len; 1239db8dac20SDavid S. Miller /* 1240db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1241db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1242db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1243db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1244db8dac20SDavid S. Miller * seems like overkill. 1245db8dac20SDavid S. Miller */ 1246db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 12476aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1248629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1249db8dac20SDavid S. Miller } 1250db8dac20SDavid S. Miller return err; 1251db8dac20SDavid S. Miller 1252db8dac20SDavid S. Miller do_confirm: 12530dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 12540dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1255db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1256db8dac20SDavid S. Miller goto back_from_confirm; 1257db8dac20SDavid S. Miller err = 0; 1258db8dac20SDavid S. Miller goto out; 1259db8dac20SDavid S. Miller } 1260c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1261db8dac20SDavid S. Miller 1262db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 1263db8dac20SDavid S. Miller size_t size, int flags) 1264db8dac20SDavid S. Miller { 1265f5fca608SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1266db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1267db8dac20SDavid S. Miller int ret; 1268db8dac20SDavid S. Miller 1269d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 1270d3f7d56aSShawn Landden flags |= MSG_MORE; 1271d3f7d56aSShawn Landden 1272db8dac20SDavid S. Miller if (!up->pending) { 1273db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1274db8dac20SDavid S. Miller 1275db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 1276db8dac20SDavid S. Miller * sendpage interface can't pass. 1277db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 1278db8dac20SDavid S. Miller */ 12791b784140SYing Xue ret = udp_sendmsg(sk, &msg, 0); 1280db8dac20SDavid S. Miller if (ret < 0) 1281db8dac20SDavid S. Miller return ret; 1282db8dac20SDavid S. Miller } 1283db8dac20SDavid S. Miller 1284db8dac20SDavid S. Miller lock_sock(sk); 1285db8dac20SDavid S. Miller 1286db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 1287db8dac20SDavid S. Miller release_sock(sk); 1288db8dac20SDavid S. Miller 1289197df02cSMatteo Croce net_dbg_ratelimited("cork failed\n"); 1290db8dac20SDavid S. Miller return -EINVAL; 1291db8dac20SDavid S. Miller } 1292db8dac20SDavid S. Miller 1293f5fca608SDavid S. Miller ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1294f5fca608SDavid S. Miller page, offset, size, flags); 1295db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 1296db8dac20SDavid S. Miller release_sock(sk); 1297db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 1298db8dac20SDavid S. Miller size, flags); 1299db8dac20SDavid S. Miller } 1300db8dac20SDavid S. Miller if (ret < 0) { 1301db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1302db8dac20SDavid S. Miller goto out; 1303db8dac20SDavid S. Miller } 1304db8dac20SDavid S. Miller 1305db8dac20SDavid S. Miller up->len += size; 1306db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 1307db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 1308db8dac20SDavid S. Miller if (!ret) 1309db8dac20SDavid S. Miller ret = size; 1310db8dac20SDavid S. Miller out: 1311db8dac20SDavid S. Miller release_sock(sk); 1312db8dac20SDavid S. Miller return ret; 1313db8dac20SDavid S. Miller } 1314db8dac20SDavid S. Miller 1315dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1316dce4551cSPaolo Abeni 1317677bf08cSFlorian Westphal /* all head states (dst, sk, nf conntrack) except skb extensions are 1318677bf08cSFlorian Westphal * cleared by udp_rcv(). 1319677bf08cSFlorian Westphal * 1320677bf08cSFlorian Westphal * We need to preserve secpath, if present, to eventually process 1321677bf08cSFlorian Westphal * IP_CMSG_PASSSEC at recvmsg() time. 1322677bf08cSFlorian Westphal * 1323677bf08cSFlorian Westphal * Other extensions can be cleared. 1324677bf08cSFlorian Westphal */ 1325677bf08cSFlorian Westphal static bool udp_try_make_stateless(struct sk_buff *skb) 1326677bf08cSFlorian Westphal { 1327677bf08cSFlorian Westphal if (!skb_has_extensions(skb)) 1328677bf08cSFlorian Westphal return true; 1329677bf08cSFlorian Westphal 1330677bf08cSFlorian Westphal if (!secpath_exists(skb)) { 1331677bf08cSFlorian Westphal skb_ext_reset(skb); 1332677bf08cSFlorian Westphal return true; 1333677bf08cSFlorian Westphal } 1334677bf08cSFlorian Westphal 1335677bf08cSFlorian Westphal return false; 1336677bf08cSFlorian Westphal } 1337677bf08cSFlorian Westphal 1338b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1339b65ac446SPaolo Abeni { 1340dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1341b65ac446SPaolo Abeni 1342b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1343dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1344dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1345b65ac446SPaolo Abeni scratch->len = skb->len; 1346b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1347b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1348b65ac446SPaolo Abeni #endif 1349677bf08cSFlorian Westphal if (udp_try_make_stateless(skb)) 1350dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1351dce4551cSPaolo Abeni } 1352dce4551cSPaolo Abeni 1353a793183cSEric Dumazet static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) 1354a793183cSEric Dumazet { 1355a793183cSEric Dumazet /* We come here after udp_lib_checksum_complete() returned 0. 1356a793183cSEric Dumazet * This means that __skb_checksum_complete() might have 1357a793183cSEric Dumazet * set skb->csum_valid to 1. 1358a793183cSEric Dumazet * On 64bit platforms, we can set csum_unnecessary 1359a793183cSEric Dumazet * to true, but only if the skb is not shared. 1360a793183cSEric Dumazet */ 1361a793183cSEric Dumazet #if BITS_PER_LONG == 64 1362a793183cSEric Dumazet if (!skb_shared(skb)) 1363a793183cSEric Dumazet udp_skb_scratch(skb)->csum_unnecessary = true; 1364a793183cSEric Dumazet #endif 1365a793183cSEric Dumazet } 1366a793183cSEric Dumazet 1367dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1368dce4551cSPaolo Abeni { 1369dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1370dce4551cSPaolo Abeni } 1371dce4551cSPaolo Abeni 1372dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1373dce4551cSPaolo Abeni { 1374dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1375dce4551cSPaolo Abeni } 1376b65ac446SPaolo Abeni 13777c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 13786dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 13796dfb4367SPaolo Abeni bool rx_queue_lock_held) 1380f970bd9eSPaolo Abeni { 13816b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 13822276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1383f970bd9eSPaolo Abeni int amt; 1384f970bd9eSPaolo Abeni 13856b229cf7SEric Dumazet if (likely(partial)) { 13866b229cf7SEric Dumazet up->forward_deficit += size; 13876b229cf7SEric Dumazet size = up->forward_deficit; 1388d39ca259SPaolo Abeni if (size < (sk->sk_rcvbuf >> 2) && 1389d39ca259SPaolo Abeni !skb_queue_empty(&up->reader_queue)) 13906b229cf7SEric Dumazet return; 13916b229cf7SEric Dumazet } else { 13926b229cf7SEric Dumazet size += up->forward_deficit; 13936b229cf7SEric Dumazet } 13946b229cf7SEric Dumazet up->forward_deficit = 0; 13956b229cf7SEric Dumazet 13966dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 13976dfb4367SPaolo Abeni * if the called don't held it already 13986dfb4367SPaolo Abeni */ 13992276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 14006dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14012276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 14022276f58aSPaolo Abeni 14036dfb4367SPaolo Abeni 1404f970bd9eSPaolo Abeni sk->sk_forward_alloc += size; 1405f970bd9eSPaolo Abeni amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); 1406f970bd9eSPaolo Abeni sk->sk_forward_alloc -= amt; 1407f970bd9eSPaolo Abeni 1408f970bd9eSPaolo Abeni if (amt) 1409f970bd9eSPaolo Abeni __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); 141002ab0d13SEric Dumazet 141102ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 14122276f58aSPaolo Abeni 14132276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 14142276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 14152276f58aSPaolo Abeni 14166dfb4367SPaolo Abeni if (!rx_queue_lock_held) 14172276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1418f970bd9eSPaolo Abeni } 1419f970bd9eSPaolo Abeni 14202276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1421c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1422c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1423c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1424c84d9490SEric Dumazet */ 14257c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1426f970bd9eSPaolo Abeni { 1427b65ac446SPaolo Abeni prefetch(&skb->data); 1428b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1429f970bd9eSPaolo Abeni } 14307c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1431f970bd9eSPaolo Abeni 14326dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 143364f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 14346dfb4367SPaolo Abeni { 1435b65ac446SPaolo Abeni prefetch(&skb->data); 1436b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 14376dfb4367SPaolo Abeni } 14386dfb4367SPaolo Abeni 14394b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 14404b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 14414b272750SEric Dumazet * Under flood, this means that only one producer can be in line 14424b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 14434b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 14444b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 14454b272750SEric Dumazet */ 14464b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 14474b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 14484b272750SEric Dumazet 14494b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 14504b272750SEric Dumazet { 14514b272750SEric Dumazet spinlock_t *busy; 14524b272750SEric Dumazet 14534b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 14544b272750SEric Dumazet spin_lock(busy); 14554b272750SEric Dumazet return busy; 14564b272750SEric Dumazet } 14574b272750SEric Dumazet 14584b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 14594b272750SEric Dumazet { 14604b272750SEric Dumazet if (busy) 14614b272750SEric Dumazet spin_unlock(busy); 14624b272750SEric Dumazet } 14634b272750SEric Dumazet 1464f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1465f970bd9eSPaolo Abeni { 1466f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1467f970bd9eSPaolo Abeni int rmem, delta, amt, err = -ENOMEM; 14684b272750SEric Dumazet spinlock_t *busy = NULL; 1469c8c8b127SEric Dumazet int size; 1470f970bd9eSPaolo Abeni 1471f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1472f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1473f970bd9eSPaolo Abeni */ 1474f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1475363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1476f970bd9eSPaolo Abeni goto drop; 1477f970bd9eSPaolo Abeni 1478c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1479c8c8b127SEric Dumazet * having linear skbs : 1480c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1481c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1482c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1483c8c8b127SEric Dumazet */ 14844b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1485c8c8b127SEric Dumazet skb_condense(skb); 14864b272750SEric Dumazet 14874b272750SEric Dumazet busy = busylock_acquire(sk); 14884b272750SEric Dumazet } 1489c8c8b127SEric Dumazet size = skb->truesize; 1490b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1491c8c8b127SEric Dumazet 1492f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1493f970bd9eSPaolo Abeni * queue contains some other skb 1494f970bd9eSPaolo Abeni */ 1495f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1496feed8a4fSAntonio Messina if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) 1497f970bd9eSPaolo Abeni goto uncharge_drop; 1498f970bd9eSPaolo Abeni 1499f970bd9eSPaolo Abeni spin_lock(&list->lock); 1500f970bd9eSPaolo Abeni if (size >= sk->sk_forward_alloc) { 1501f970bd9eSPaolo Abeni amt = sk_mem_pages(size); 1502f970bd9eSPaolo Abeni delta = amt << SK_MEM_QUANTUM_SHIFT; 1503f970bd9eSPaolo Abeni if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1504f970bd9eSPaolo Abeni err = -ENOBUFS; 1505f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1506f970bd9eSPaolo Abeni goto uncharge_drop; 1507f970bd9eSPaolo Abeni } 1508f970bd9eSPaolo Abeni 1509f970bd9eSPaolo Abeni sk->sk_forward_alloc += delta; 1510f970bd9eSPaolo Abeni } 1511f970bd9eSPaolo Abeni 1512f970bd9eSPaolo Abeni sk->sk_forward_alloc -= size; 1513f970bd9eSPaolo Abeni 15147c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 15157c13f97fSPaolo Abeni * forward allocated memory on dequeue 15167c13f97fSPaolo Abeni */ 1517f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1518f970bd9eSPaolo Abeni 1519f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1520f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1521f970bd9eSPaolo Abeni 1522f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 1523f970bd9eSPaolo Abeni sk->sk_data_ready(sk); 1524f970bd9eSPaolo Abeni 15254b272750SEric Dumazet busylock_release(busy); 1526f970bd9eSPaolo Abeni return 0; 1527f970bd9eSPaolo Abeni 1528f970bd9eSPaolo Abeni uncharge_drop: 1529f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1530f970bd9eSPaolo Abeni 1531f970bd9eSPaolo Abeni drop: 1532f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 15334b272750SEric Dumazet busylock_release(busy); 1534f970bd9eSPaolo Abeni return err; 1535f970bd9eSPaolo Abeni } 1536f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1537f970bd9eSPaolo Abeni 1538c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk) 1539f970bd9eSPaolo Abeni { 1540f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 15412276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 15427c13f97fSPaolo Abeni unsigned int total = 0; 15437c13f97fSPaolo Abeni struct sk_buff *skb; 15447c13f97fSPaolo Abeni 15452276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 15462276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 15477c13f97fSPaolo Abeni total += skb->truesize; 15487c13f97fSPaolo Abeni kfree_skb(skb); 15497c13f97fSPaolo Abeni } 15506dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 15517c13f97fSPaolo Abeni 1552f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1553f970bd9eSPaolo Abeni } 1554c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock); 1555f970bd9eSPaolo Abeni 1556f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1557f970bd9eSPaolo Abeni { 15582276f58aSPaolo Abeni skb_queue_head_init(&udp_sk(sk)->reader_queue); 1559f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1560f970bd9eSPaolo Abeni return 0; 1561f970bd9eSPaolo Abeni } 1562f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock); 1563f970bd9eSPaolo Abeni 1564f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1565f970bd9eSPaolo Abeni { 1566f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1567f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1568f970bd9eSPaolo Abeni 1569f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1570f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1571f970bd9eSPaolo Abeni } 15720a463c78SPaolo Abeni 1573ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1574ca2c1418SPaolo Abeni return; 1575ca2c1418SPaolo Abeni 1576dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1577dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 15780ddf3fb2SPaolo Abeni */ 1579dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 15800ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1581ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1582f970bd9eSPaolo Abeni } 1583f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1584f970bd9eSPaolo Abeni 15852276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 15862276f58aSPaolo Abeni struct sk_buff_head *rcvq, 15872276f58aSPaolo Abeni int *total) 15882276f58aSPaolo Abeni { 15892276f58aSPaolo Abeni struct sk_buff *skb; 15902276f58aSPaolo Abeni 15919bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 15929bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 15932276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 15942276f58aSPaolo Abeni IS_UDPLITE(sk)); 15952276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 15962276f58aSPaolo Abeni IS_UDPLITE(sk)); 15972276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 15982276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 15992276f58aSPaolo Abeni *total += skb->truesize; 16002276f58aSPaolo Abeni kfree_skb(skb); 16019bd780f5SPaolo Abeni } else { 1602a793183cSEric Dumazet udp_skb_csum_unnecessary_set(skb); 16039bd780f5SPaolo Abeni break; 16049bd780f5SPaolo Abeni } 16052276f58aSPaolo Abeni } 16062276f58aSPaolo Abeni return skb; 16072276f58aSPaolo Abeni } 16082276f58aSPaolo Abeni 160985584672SEric Dumazet /** 161085584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 161185584672SEric Dumazet * @sk: socket 161285584672SEric Dumazet * 161385584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1614e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 161585584672SEric Dumazet */ 1616e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 161785584672SEric Dumazet { 16182276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 16192276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 162085584672SEric Dumazet struct sk_buff *skb; 16217c13f97fSPaolo Abeni int total = 0; 1622e83c6744SEric Dumazet int res; 162385584672SEric Dumazet 162485584672SEric Dumazet spin_lock_bh(&rcvq->lock); 16252276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 1626137a0dbeSEric Dumazet if (!skb && !skb_queue_empty_lockless(sk_queue)) { 16272276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 16282276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 16292276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 16302276f58aSPaolo Abeni 16312276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 163285584672SEric Dumazet } 1633e83c6744SEric Dumazet res = skb ? skb->len : -1; 16347c13f97fSPaolo Abeni if (total) 16356dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 163685584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 163785584672SEric Dumazet return res; 163885584672SEric Dumazet } 163985584672SEric Dumazet 16401da177e4SLinus Torvalds /* 16411da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 16421da177e4SLinus Torvalds */ 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 16451da177e4SLinus Torvalds { 16466516c655SStephen Hemminger switch (cmd) { 16471da177e4SLinus Torvalds case SIOCOUTQ: 16481da177e4SLinus Torvalds { 164931e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 165031e6d363SEric Dumazet 16511da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds case SIOCINQ: 16551da177e4SLinus Torvalds { 1656e83c6744SEric Dumazet int amount = max_t(int, 0, first_packet_length(sk)); 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds default: 16621da177e4SLinus Torvalds return -ENOIOCTLCMD; 16631da177e4SLinus Torvalds } 16646516c655SStephen Hemminger 16656516c655SStephen Hemminger return 0; 16661da177e4SLinus Torvalds } 1667c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 16681da177e4SLinus Torvalds 16692276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 1670fd69c399SPaolo Abeni int noblock, int *off, int *err) 16712276f58aSPaolo Abeni { 16722276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 16732276f58aSPaolo Abeni struct sk_buff_head *queue; 16742276f58aSPaolo Abeni struct sk_buff *last; 16752276f58aSPaolo Abeni long timeo; 16762276f58aSPaolo Abeni int error; 16772276f58aSPaolo Abeni 16782276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 16792276f58aSPaolo Abeni flags |= noblock ? MSG_DONTWAIT : 0; 16802276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 16812276f58aSPaolo Abeni do { 16822276f58aSPaolo Abeni struct sk_buff *skb; 16832276f58aSPaolo Abeni 16842276f58aSPaolo Abeni error = sock_error(sk); 16852276f58aSPaolo Abeni if (error) 16862276f58aSPaolo Abeni break; 16872276f58aSPaolo Abeni 16882276f58aSPaolo Abeni error = -EAGAIN; 16892276f58aSPaolo Abeni do { 16902276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 1691e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1692e427cad6SPaolo Abeni err, &last); 16932276f58aSPaolo Abeni if (skb) { 1694e427cad6SPaolo Abeni if (!(flags & MSG_PEEK)) 1695e427cad6SPaolo Abeni udp_skb_destructor(sk, skb); 16962276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 16972276f58aSPaolo Abeni return skb; 16982276f58aSPaolo Abeni } 16992276f58aSPaolo Abeni 1700137a0dbeSEric Dumazet if (skb_queue_empty_lockless(sk_queue)) { 17012276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 17022276f58aSPaolo Abeni goto busy_check; 17032276f58aSPaolo Abeni } 17042276f58aSPaolo Abeni 17056dfb4367SPaolo Abeni /* refill the reader queue and walk it again 17066dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 17076dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 17086dfb4367SPaolo Abeni * is needed. 17096dfb4367SPaolo Abeni */ 17102276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 17112276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 17122276f58aSPaolo Abeni 1713e427cad6SPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, off, 1714e427cad6SPaolo Abeni err, &last); 1715e427cad6SPaolo Abeni if (skb && !(flags & MSG_PEEK)) 1716e427cad6SPaolo Abeni udp_skb_dtor_locked(sk, skb); 17176dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 17182276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1719de321ed3SAndrey Vagin if (skb) 17202276f58aSPaolo Abeni return skb; 17212276f58aSPaolo Abeni 17222276f58aSPaolo Abeni busy_check: 17232276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 17242276f58aSPaolo Abeni break; 17252276f58aSPaolo Abeni 17262276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 1727137a0dbeSEric Dumazet } while (!skb_queue_empty_lockless(sk_queue)); 17282276f58aSPaolo Abeni 17292276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 17302276f58aSPaolo Abeni } while (timeo && 1731b50b0580SSabrina Dubroca !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, 1732b50b0580SSabrina Dubroca &error, &timeo, 17332276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 17342276f58aSPaolo Abeni 17352276f58aSPaolo Abeni *err = error; 17362276f58aSPaolo Abeni return NULL; 17372276f58aSPaolo Abeni } 17387e823644SJiri Kosina EXPORT_SYMBOL(__skb_recv_udp); 17392276f58aSPaolo Abeni 1740db8dac20SDavid S. Miller /* 1741db8dac20SDavid S. Miller * This should be easy, if there is something there we 1742db8dac20SDavid S. Miller * return it, otherwise we block. 1743db8dac20SDavid S. Miller */ 1744db8dac20SDavid S. Miller 17451b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, 17461b784140SYing Xue int flags, int *addr_len) 1747db8dac20SDavid S. Miller { 1748db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1749342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1750db8dac20SDavid S. Miller struct sk_buff *skb; 175159c2cdaeSDavid S. Miller unsigned int ulen, copied; 1752fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 1753db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1754197c949eSEric Dumazet bool checksum_valid = false; 1755db8dac20SDavid S. Miller 1756db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 175785fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1758db8dac20SDavid S. Miller 1759db8dac20SDavid S. Miller try_again: 1760a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 1761fd69c399SPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &off, &err); 1762db8dac20SDavid S. Miller if (!skb) 1763627d2d6bSsamanthakumar return err; 1764db8dac20SDavid S. Miller 1765b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 176659c2cdaeSDavid S. Miller copied = len; 1767627d2d6bSsamanthakumar if (copied > ulen - off) 1768627d2d6bSsamanthakumar copied = ulen - off; 176959c2cdaeSDavid S. Miller else if (copied < ulen) 1770db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1771db8dac20SDavid S. Miller 1772db8dac20SDavid S. Miller /* 1773db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1774db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1775db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1776db8dac20SDavid S. Miller */ 1777db8dac20SDavid S. Miller 1778d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1779d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1780b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1781b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1782197c949eSEric Dumazet if (!checksum_valid) 1783db8dac20SDavid S. Miller goto csum_copy_err; 1784db8dac20SDavid S. Miller } 1785db8dac20SDavid S. Miller 1786b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1787b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1788b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1789b65ac446SPaolo Abeni else 1790627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1791b65ac446SPaolo Abeni } else { 1792627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1793db8dac20SDavid S. Miller 1794db8dac20SDavid S. Miller if (err == -EINVAL) 1795db8dac20SDavid S. Miller goto csum_copy_err; 1796db8dac20SDavid S. Miller } 1797db8dac20SDavid S. Miller 179822911fc5SEric Dumazet if (unlikely(err)) { 1799fd69c399SPaolo Abeni if (!peeking) { 1800979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 18016aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1802979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1803979402b1SEric Dumazet } 1804850cbaddSPaolo Abeni kfree_skb(skb); 1805627d2d6bSsamanthakumar return err; 180622911fc5SEric Dumazet } 1807db8dac20SDavid S. Miller 1808fd69c399SPaolo Abeni if (!peeking) 18096aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1810629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1811db8dac20SDavid S. Miller 18123b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 1813db8dac20SDavid S. Miller 1814db8dac20SDavid S. Miller /* Copy the address. */ 1815c482c568SEric Dumazet if (sin) { 1816db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1817db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1818db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1819db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1820bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1821983695faSDaniel Borkmann 1822983695faSDaniel Borkmann if (cgroup_bpf_enabled) 1823983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, 1824983695faSDaniel Borkmann (struct sockaddr *)sin); 1825db8dac20SDavid S. Miller } 1826bcd1665eSPaolo Abeni 1827bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 1828bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 1829bcd1665eSPaolo Abeni 1830db8dac20SDavid S. Miller if (inet->cmsg_flags) 1831ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1832db8dac20SDavid S. Miller 183359c2cdaeSDavid S. Miller err = copied; 1834db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1835db8dac20SDavid S. Miller err = ulen; 1836db8dac20SDavid S. Miller 1837850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1838db8dac20SDavid S. Miller return err; 1839db8dac20SDavid S. Miller 1840db8dac20SDavid S. Miller csum_copy_err: 18412276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 18422276f58aSPaolo Abeni udp_skb_destructor)) { 18436aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 18446aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 18456a5dc9e5SEric Dumazet } 1846850cbaddSPaolo Abeni kfree_skb(skb); 1847db8dac20SDavid S. Miller 1848beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1849beb39db5SEric Dumazet cond_resched(); 18509cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1851db8dac20SDavid S. Miller goto try_again; 1852db8dac20SDavid S. Miller } 1853db8dac20SDavid S. Miller 1854d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1855d74bad4eSAndrey Ignatov { 1856d74bad4eSAndrey Ignatov /* This check is replicated from __ip4_datagram_connect() and 1857d74bad4eSAndrey Ignatov * intended to prevent BPF program called below from accessing bytes 1858d74bad4eSAndrey Ignatov * that are out of the bound specified by user in addr_len. 1859d74bad4eSAndrey Ignatov */ 1860d74bad4eSAndrey Ignatov if (addr_len < sizeof(struct sockaddr_in)) 1861d74bad4eSAndrey Ignatov return -EINVAL; 1862d74bad4eSAndrey Ignatov 1863d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); 1864d74bad4eSAndrey Ignatov } 1865d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect); 1866d74bad4eSAndrey Ignatov 1867286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 18681da177e4SLinus Torvalds { 18691da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 18701da177e4SLinus Torvalds /* 18711da177e4SLinus Torvalds * 1003.1g - break association. 18721da177e4SLinus Torvalds */ 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1875c720c7e8SEric Dumazet inet->inet_daddr = 0; 1876c720c7e8SEric Dumazet inet->inet_dport = 0; 1877bdeab991STom Herbert sock_rps_reset_rxhash(sk); 18781da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 1879303d0403SWillem de Bruijn if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { 18801da177e4SLinus Torvalds inet_reset_saddr(sk); 1881303d0403SWillem de Bruijn if (sk->sk_prot->rehash && 1882303d0403SWillem de Bruijn (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 1883303d0403SWillem de Bruijn sk->sk_prot->rehash(sk); 1884303d0403SWillem de Bruijn } 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 18871da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1888c720c7e8SEric Dumazet inet->inet_sport = 0; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds sk_dst_reset(sk); 18911da177e4SLinus Torvalds return 0; 18921da177e4SLinus Torvalds } 1893286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1894286c72deSEric Dumazet 1895286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1896286c72deSEric Dumazet { 1897286c72deSEric Dumazet lock_sock(sk); 1898286c72deSEric Dumazet __udp_disconnect(sk, flags); 1899286c72deSEric Dumazet release_sock(sk); 1900286c72deSEric Dumazet return 0; 1901286c72deSEric Dumazet } 1902c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 19031da177e4SLinus Torvalds 1904645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 1905645ca708SEric Dumazet { 1906723b4610SEric Dumazet if (sk_hashed(sk)) { 1907645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1908512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 1909512615b6SEric Dumazet 1910512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1911d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 1912512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1913645ca708SEric Dumazet 1914c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 1915e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1916e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1917ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 1918fdcc8aa9SEric Dumazet hslot->count--; 1919c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 1920645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1921512615b6SEric Dumazet 1922512615b6SEric Dumazet spin_lock(&hslot2->lock); 1923ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1924512615b6SEric Dumazet hslot2->count--; 1925512615b6SEric Dumazet spin_unlock(&hslot2->lock); 1926645ca708SEric Dumazet } 1927c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 1928645ca708SEric Dumazet } 1929723b4610SEric Dumazet } 1930645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 1931645ca708SEric Dumazet 1932719f8358SEric Dumazet /* 1933719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 1934719f8358SEric Dumazet */ 1935719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 1936719f8358SEric Dumazet { 1937719f8358SEric Dumazet if (sk_hashed(sk)) { 1938719f8358SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1939719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 1940719f8358SEric Dumazet 1941719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1942719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 1943719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 1944e32ea7e7SCraig Gallek 1945e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 1946e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 1947719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1948719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 1949719f8358SEric Dumazet /* we must lock primary chain too */ 1950719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 1951e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1952e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1953719f8358SEric Dumazet 1954e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 1955719f8358SEric Dumazet spin_lock(&hslot2->lock); 1956ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1957719f8358SEric Dumazet hslot2->count--; 1958719f8358SEric Dumazet spin_unlock(&hslot2->lock); 1959719f8358SEric Dumazet 1960719f8358SEric Dumazet spin_lock(&nhslot2->lock); 1961ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 1962719f8358SEric Dumazet &nhslot2->head); 1963719f8358SEric Dumazet nhslot2->count++; 1964719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 1965e32ea7e7SCraig Gallek } 1966719f8358SEric Dumazet 1967719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 1968719f8358SEric Dumazet } 1969719f8358SEric Dumazet } 1970719f8358SEric Dumazet } 1971719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 1972719f8358SEric Dumazet 19738f6b5392SAlexey Kodanev void udp_v4_rehash(struct sock *sk) 1974719f8358SEric Dumazet { 1975f0b1e64cSMartin KaFai Lau u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 1976719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 1977719f8358SEric Dumazet inet_sk(sk)->inet_num); 1978719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 1979719f8358SEric Dumazet } 1980719f8358SEric Dumazet 1981a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 198293821778SHerbert Xu { 1983fec5e652STom Herbert int rc; 198493821778SHerbert Xu 1985005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 1986bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 1987005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 19882c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 1989e68b6e50SEric Dumazet } else { 1990e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 1991005ec974SShawn Bohrer } 1992fec5e652STom Herbert 1993850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 1994766e9037SEric Dumazet if (rc < 0) { 1995766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 1996766e9037SEric Dumazet 199793821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 1998766e9037SEric Dumazet if (rc == -ENOMEM) 1999e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 200093821778SHerbert Xu is_udplite); 2001e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 2002766e9037SEric Dumazet kfree_skb(skb); 2003296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 2004766e9037SEric Dumazet return -1; 200593821778SHerbert Xu } 200693821778SHerbert Xu 200793821778SHerbert Xu return 0; 200893821778SHerbert Xu } 200993821778SHerbert Xu 2010db8dac20SDavid S. Miller /* returns: 2011db8dac20SDavid S. Miller * -1: error 2012db8dac20SDavid S. Miller * 0: success 2013db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 2014db8dac20SDavid S. Miller * 2015db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 2016db8dac20SDavid S. Miller * have either been requeued or freed. 2017db8dac20SDavid S. Miller */ 2018cf329aa4SPaolo Abeni static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 2019db8dac20SDavid S. Miller { 2020db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 2021db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 2022db8dac20SDavid S. Miller 2023db8dac20SDavid S. Miller /* 2024db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 2025db8dac20SDavid S. Miller */ 2026db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 2027db8dac20SDavid S. Miller goto drop; 2028895b5c9fSFlorian Westphal nf_reset_ct(skb); 2029db8dac20SDavid S. Miller 203088ab3108SDavidlohr Bueso if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 20310ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 20320ad92ad0SEric Dumazet 2033db8dac20SDavid S. Miller /* 2034db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 2035db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 2036db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 2037db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 2038db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 2039db8dac20SDavid S. Miller * handler or was discarded by it. 2040db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 2041db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 2042db8dac20SDavid S. Miller */ 2043db8dac20SDavid S. Miller 2044db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 20456aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 2046e5aed006SHannes Frederic Sowa if (encap_rcv) { 2047db8dac20SDavid S. Miller int ret; 2048db8dac20SDavid S. Miller 20490a80966bSTom Herbert /* Verify checksum before giving to encap */ 20500a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 20510a80966bSTom Herbert goto csum_error; 20520a80966bSTom Herbert 20530ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 2054db8dac20SDavid S. Miller if (ret <= 0) { 205502c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 20560283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 2057db8dac20SDavid S. Miller is_udplite); 2058db8dac20SDavid S. Miller return -ret; 2059db8dac20SDavid S. Miller } 2060db8dac20SDavid S. Miller } 2061db8dac20SDavid S. Miller 2062db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 2063db8dac20SDavid S. Miller } 2064db8dac20SDavid S. Miller 2065db8dac20SDavid S. Miller /* 2066db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 2067db8dac20SDavid S. Miller */ 2068db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 2069db8dac20SDavid S. Miller 2070db8dac20SDavid S. Miller /* 2071db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 2072db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 2073db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 2074db8dac20SDavid S. Miller * protocol stack as such. 2075db8dac20SDavid S. Miller * 2076db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 2077db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 2078db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 2079db8dac20SDavid S. Miller * provided by the application." 2080db8dac20SDavid S. Miller */ 2081db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 2082ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 2083db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 2084db8dac20SDavid S. Miller goto drop; 2085db8dac20SDavid S. Miller } 2086db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 2087db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 2088db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 2089db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 2090db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 2091db8dac20SDavid S. Miller */ 2092db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 2093ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 2094db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 2095db8dac20SDavid S. Miller goto drop; 2096db8dac20SDavid S. Miller } 2097db8dac20SDavid S. Miller } 2098db8dac20SDavid S. Miller 2099dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 2100ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 2101ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 21026a5dc9e5SEric Dumazet goto csum_error; 2103ce25d66aSEric Dumazet 2104ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 2105a6127697SMichal Kubeček goto drop; 2106db8dac20SDavid S. Miller 2107e6afc8acSsamanthakumar udp_csum_pull_header(skb); 2108db8dac20SDavid S. Miller 2109fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 2110850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 2111db8dac20SDavid S. Miller 21126a5dc9e5SEric Dumazet csum_error: 211302c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2114db8dac20SDavid S. Miller drop: 211502c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 21168edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 2117db8dac20SDavid S. Miller kfree_skb(skb); 2118db8dac20SDavid S. Miller return -1; 2119db8dac20SDavid S. Miller } 2120db8dac20SDavid S. Miller 2121cf329aa4SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 2122cf329aa4SPaolo Abeni { 2123cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 2124cf329aa4SPaolo Abeni int ret; 2125cf329aa4SPaolo Abeni 2126cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 2127cf329aa4SPaolo Abeni return udp_queue_rcv_one_skb(sk, skb); 2128cf329aa4SPaolo Abeni 2129a08e7fd9SCambda Zhu BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); 2130cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 2131cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, true); 21321a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 2133cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 2134cf329aa4SPaolo Abeni ret = udp_queue_rcv_one_skb(sk, skb); 2135cf329aa4SPaolo Abeni if (ret > 0) 2136cf329aa4SPaolo Abeni ip_protocol_deliver_rcu(dev_net(skb->dev), skb, -ret); 2137cf329aa4SPaolo Abeni } 2138cf329aa4SPaolo Abeni return 0; 2139cf329aa4SPaolo Abeni } 2140cf329aa4SPaolo Abeni 214197502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 2142e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 214397502231SEric Dumazet */ 214464f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2145421b3885SShawn Bohrer { 214697502231SEric Dumazet struct dst_entry *old; 2147421b3885SShawn Bohrer 2148d24406c8SWei Wang if (dst_hold_safe(dst)) { 2149e47eb5dfSEric Dumazet old = xchg(&sk->sk_rx_dst, dst); 215097502231SEric Dumazet dst_release(old); 215164f0f5d1SPaolo Abeni return old != dst; 215297502231SEric Dumazet } 215364f0f5d1SPaolo Abeni return false; 2154d24406c8SWei Wang } 2155c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 2156421b3885SShawn Bohrer 2157db8dac20SDavid S. Miller /* 2158db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 2159db8dac20SDavid S. Miller * 21601240d137SEric Dumazet * Note: called only from the BH handler context. 2161db8dac20SDavid S. Miller */ 2162e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2163db8dac20SDavid S. Miller struct udphdr *uh, 2164db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 216536cbb245SRick Jones struct udp_table *udptable, 216636cbb245SRick Jones int proto) 2167db8dac20SDavid S. Miller { 2168ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 21695cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 21705cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 21712dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2172ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 2173ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 2174fb74c277SDavid Ahern int sdif = inet_sdif(skb); 2175ca065d0cSEric Dumazet struct hlist_node *node; 2176ca065d0cSEric Dumazet struct sk_buff *nskb; 21772dc41cffSDavid Held 21782dc41cffSDavid Held if (use_hash2) { 2179f0b1e64cSMartin KaFai Lau hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 218073e2d5e3SPablo Neira udptable->mask; 2181f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 21822dc41cffSDavid Held start_lookup: 218373e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 21842dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 21852dc41cffSDavid Held } 2186db8dac20SDavid S. Miller 2187ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2188ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2189fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 2190ca065d0cSEric Dumazet continue; 21911240d137SEric Dumazet 2192ca065d0cSEric Dumazet if (!first) { 2193ca065d0cSEric Dumazet first = sk; 2194ca065d0cSEric Dumazet continue; 2195ca065d0cSEric Dumazet } 2196ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 2197ca065d0cSEric Dumazet 2198ca065d0cSEric Dumazet if (unlikely(!nskb)) { 2199ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 220002c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2201ca065d0cSEric Dumazet IS_UDPLITE(sk)); 220202c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2203ca065d0cSEric Dumazet IS_UDPLITE(sk)); 2204ca065d0cSEric Dumazet continue; 2205ca065d0cSEric Dumazet } 2206ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 2207ca065d0cSEric Dumazet consume_skb(nskb); 2208ca065d0cSEric Dumazet } 22091240d137SEric Dumazet 22102dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 22112dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 22122dc41cffSDavid Held hash2 = hash2_any; 22132dc41cffSDavid Held goto start_lookup; 22142dc41cffSDavid Held } 22152dc41cffSDavid Held 2216ca065d0cSEric Dumazet if (first) { 2217ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 2218ca065d0cSEric Dumazet consume_skb(skb); 22191240d137SEric Dumazet } else { 2220ca065d0cSEric Dumazet kfree_skb(skb); 222102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 222236cbb245SRick Jones proto == IPPROTO_UDPLITE); 22231240d137SEric Dumazet } 2224db8dac20SDavid S. Miller return 0; 2225db8dac20SDavid S. Miller } 2226db8dac20SDavid S. Miller 2227db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2228db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2229666a3d6eSSu Yanjun * Otherwise, csum completion requires checksumming packet body, 2230db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2231db8dac20SDavid S. Miller */ 2232db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2233db8dac20SDavid S. Miller int proto) 2234db8dac20SDavid S. Miller { 2235db8dac20SDavid S. Miller int err; 2236db8dac20SDavid S. Miller 2237db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2238db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2239db8dac20SDavid S. Miller 2240db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2241db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2242db8dac20SDavid S. Miller if (err) 2243db8dac20SDavid S. Miller return err; 224415f35d49SAlexey Kodanev 224515f35d49SAlexey Kodanev if (UDP_SKB_CB(skb)->partial_cov) { 224615f35d49SAlexey Kodanev skb->csum = inet_compute_pseudo(skb, proto); 224715f35d49SAlexey Kodanev return 0; 224815f35d49SAlexey Kodanev } 2249db8dac20SDavid S. Miller } 2250db8dac20SDavid S. Miller 2251b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2252b46d9f62SHannes Frederic Sowa * force to int. 2253b46d9f62SHannes Frederic Sowa */ 2254db4f1be3SSean Tranchetti err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2255ed70fcfcSTom Herbert inet_compute_pseudo); 2256db4f1be3SSean Tranchetti if (err) 2257db4f1be3SSean Tranchetti return err; 2258db4f1be3SSean Tranchetti 2259db4f1be3SSean Tranchetti if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { 2260db4f1be3SSean Tranchetti /* If SW calculated the value, we know it's bad */ 2261db4f1be3SSean Tranchetti if (skb->csum_complete_sw) 2262db4f1be3SSean Tranchetti return 1; 2263db4f1be3SSean Tranchetti 2264db4f1be3SSean Tranchetti /* HW says the value is bad. Let's validate that. 2265db4f1be3SSean Tranchetti * skb->csum is no longer the full packet checksum, 2266db4f1be3SSean Tranchetti * so don't treat it as such. 2267db4f1be3SSean Tranchetti */ 2268db4f1be3SSean Tranchetti skb_checksum_complete_unset(skb); 2269db4f1be3SSean Tranchetti } 2270db4f1be3SSean Tranchetti 2271db4f1be3SSean Tranchetti return 0; 2272db8dac20SDavid S. Miller } 2273db8dac20SDavid S. Miller 22742b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 22752b5a9217SPaolo Abeni * return code conversion for ip layer consumption 22762b5a9217SPaolo Abeni */ 22772b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 22782b5a9217SPaolo Abeni struct udphdr *uh) 22792b5a9217SPaolo Abeni { 22802b5a9217SPaolo Abeni int ret; 22812b5a9217SPaolo Abeni 22822b5a9217SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2283e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); 22842b5a9217SPaolo Abeni 22852b5a9217SPaolo Abeni ret = udp_queue_rcv_skb(sk, skb); 22862b5a9217SPaolo Abeni 22872b5a9217SPaolo Abeni /* a return value > 0 means to resubmit the input, but 22882b5a9217SPaolo Abeni * it wants the return to be -protocol, or 0 22892b5a9217SPaolo Abeni */ 22902b5a9217SPaolo Abeni if (ret > 0) 22912b5a9217SPaolo Abeni return -ret; 22922b5a9217SPaolo Abeni return 0; 22932b5a9217SPaolo Abeni } 22942b5a9217SPaolo Abeni 2295db8dac20SDavid S. Miller /* 2296db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2297db8dac20SDavid S. Miller */ 2298db8dac20SDavid S. Miller 2299645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2300db8dac20SDavid S. Miller int proto) 2301db8dac20SDavid S. Miller { 2302db8dac20SDavid S. Miller struct sock *sk; 23037b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2304db8dac20SDavid S. Miller unsigned short ulen; 2305adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 23062783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 23070283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 230871489e21SJoe Stringer bool refcounted; 2309db8dac20SDavid S. Miller 2310db8dac20SDavid S. Miller /* 2311db8dac20SDavid S. Miller * Validate the packet. 2312db8dac20SDavid S. Miller */ 2313db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2314db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2315db8dac20SDavid S. Miller 23167b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2317db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2318ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2319ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2320ccc2d97cSBjørn Mork 2321db8dac20SDavid S. Miller if (ulen > skb->len) 2322db8dac20SDavid S. Miller goto short_packet; 2323db8dac20SDavid S. Miller 2324db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2325db8dac20SDavid S. Miller /* UDP validates ulen. */ 2326db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2327db8dac20SDavid S. Miller goto short_packet; 2328db8dac20SDavid S. Miller uh = udp_hdr(skb); 2329db8dac20SDavid S. Miller } 2330db8dac20SDavid S. Miller 2331db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2332db8dac20SDavid S. Miller goto csum_error; 2333db8dac20SDavid S. Miller 233471489e21SJoe Stringer sk = skb_steal_sock(skb, &refcounted); 23358afdd99aSEric Dumazet if (sk) { 233697502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2337421b3885SShawn Bohrer int ret; 2338421b3885SShawn Bohrer 233997502231SEric Dumazet if (unlikely(sk->sk_rx_dst != dst)) 234097502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2341421b3885SShawn Bohrer 23422b5a9217SPaolo Abeni ret = udp_unicast_rcv_skb(sk, skb, uh); 234371489e21SJoe Stringer if (refcounted) 23448afdd99aSEric Dumazet sock_put(sk); 23452b5a9217SPaolo Abeni return ret; 2346c18450a5SFabian Frederick } 2347c18450a5SFabian Frederick 2348db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2349e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 235036cbb245SRick Jones saddr, daddr, udptable, proto); 2351db8dac20SDavid S. Miller 2352607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 23532b5a9217SPaolo Abeni if (sk) 23542b5a9217SPaolo Abeni return udp_unicast_rcv_skb(sk, skb, uh); 2355db8dac20SDavid S. Miller 2356db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2357db8dac20SDavid S. Miller goto drop; 2358895b5c9fSFlorian Westphal nf_reset_ct(skb); 2359db8dac20SDavid S. Miller 2360db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2361db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2362db8dac20SDavid S. Miller goto csum_error; 2363db8dac20SDavid S. Miller 236402c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2365db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2366db8dac20SDavid S. Miller 2367db8dac20SDavid S. Miller /* 2368db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2369db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2370db8dac20SDavid S. Miller */ 2371db8dac20SDavid S. Miller kfree_skb(skb); 2372db8dac20SDavid S. Miller return 0; 2373db8dac20SDavid S. Miller 2374db8dac20SDavid S. Miller short_packet: 2375ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2376afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2377afd46503SJoe Perches &saddr, ntohs(uh->source), 2378afd46503SJoe Perches ulen, skb->len, 2379afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2380db8dac20SDavid S. Miller goto drop; 2381db8dac20SDavid S. Miller 2382db8dac20SDavid S. Miller csum_error: 2383db8dac20SDavid S. Miller /* 2384db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2385db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2386db8dac20SDavid S. Miller */ 2387ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2388afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2389afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2390db8dac20SDavid S. Miller ulen); 239102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2392db8dac20SDavid S. Miller drop: 239302c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 2394db8dac20SDavid S. Miller kfree_skb(skb); 2395db8dac20SDavid S. Miller return 0; 2396db8dac20SDavid S. Miller } 2397db8dac20SDavid S. Miller 2398421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2399421b3885SShawn Bohrer * If more than one socket found returns NULL 2400421b3885SShawn Bohrer */ 2401421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2402421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2403421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2404fb74c277SDavid Ahern int dif, int sdif) 2405421b3885SShawn Bohrer { 2406421b3885SShawn Bohrer struct sock *sk, *result; 2407421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2408ca065d0cSEric Dumazet unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); 2409421b3885SShawn Bohrer struct udp_hslot *hslot = &udp_table.hash[slot]; 2410421b3885SShawn Bohrer 241163c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 241263c6f81cSEric Dumazet if (hslot->count > 10) 241363c6f81cSEric Dumazet return NULL; 241463c6f81cSEric Dumazet 2415421b3885SShawn Bohrer result = NULL; 2416ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2417ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2418fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2419ca065d0cSEric Dumazet if (result) 2420ca065d0cSEric Dumazet return NULL; 2421421b3885SShawn Bohrer result = sk; 2422421b3885SShawn Bohrer } 2423421b3885SShawn Bohrer } 2424421b3885SShawn Bohrer 2425421b3885SShawn Bohrer return result; 2426421b3885SShawn Bohrer } 2427421b3885SShawn Bohrer 2428421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2429421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2430421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2431421b3885SShawn Bohrer */ 2432421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2433421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2434421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 24353fa6f616SDavid Ahern int dif, int sdif) 2436421b3885SShawn Bohrer { 2437421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2438f0b1e64cSMartin KaFai Lau unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2439421b3885SShawn Bohrer unsigned int slot2 = hash2 & udp_table.mask; 2440421b3885SShawn Bohrer struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 2441c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2442421b3885SShawn Bohrer const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 2443ca065d0cSEric Dumazet struct sock *sk; 2444421b3885SShawn Bohrer 2445ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2446ca065d0cSEric Dumazet if (INET_MATCH(sk, net, acookie, rmt_addr, 24473fa6f616SDavid Ahern loc_addr, ports, dif, sdif)) 2448ca065d0cSEric Dumazet return sk; 2449421b3885SShawn Bohrer /* Only check first socket in chain */ 2450421b3885SShawn Bohrer break; 2451421b3885SShawn Bohrer } 2452ca065d0cSEric Dumazet return NULL; 2453421b3885SShawn Bohrer } 2454421b3885SShawn Bohrer 24557487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2456421b3885SShawn Bohrer { 2457610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2458bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2459610438b7SEric Dumazet const struct iphdr *iph; 2460610438b7SEric Dumazet const struct udphdr *uh; 2461ca065d0cSEric Dumazet struct sock *sk = NULL; 2462421b3885SShawn Bohrer struct dst_entry *dst; 2463421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2464fb74c277SDavid Ahern int sdif = inet_sdif(skb); 24656e540309SShawn Bohrer int ours; 2466421b3885SShawn Bohrer 2467421b3885SShawn Bohrer /* validate the packet */ 2468421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 24697487449cSPaolo Abeni return 0; 2470421b3885SShawn Bohrer 2471610438b7SEric Dumazet iph = ip_hdr(skb); 2472610438b7SEric Dumazet uh = udp_hdr(skb); 2473610438b7SEric Dumazet 2474996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2475bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 24766e540309SShawn Bohrer 24776e540309SShawn Bohrer if (!in_dev) 24787487449cSPaolo Abeni return 0; 24796e540309SShawn Bohrer 24806e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 24816e540309SShawn Bohrer iph->protocol); 24826e540309SShawn Bohrer if (!ours) 24837487449cSPaolo Abeni return 0; 2484ad0ea198SPaolo Abeni 2485421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2486fb74c277SDavid Ahern uh->source, iph->saddr, 2487fb74c277SDavid Ahern dif, sdif); 24886e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2489421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 24903fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 24916e540309SShawn Bohrer } 2492421b3885SShawn Bohrer 249341c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 24947487449cSPaolo Abeni return 0; 2495421b3885SShawn Bohrer 2496421b3885SShawn Bohrer skb->sk = sk; 249782eabd9eSAlexander Duyck skb->destructor = sock_efree; 249810e2eb87SEric Dumazet dst = READ_ONCE(sk->sk_rx_dst); 2499421b3885SShawn Bohrer 2500421b3885SShawn Bohrer if (dst) 2501421b3885SShawn Bohrer dst = dst_check(dst, 0); 250210e2eb87SEric Dumazet if (dst) { 2503bc044e8dSPaolo Abeni u32 itag = 0; 2504bc044e8dSPaolo Abeni 2505d24406c8SWei Wang /* set noref for now. 2506d24406c8SWei Wang * any place which wants to hold dst has to call 2507d24406c8SWei Wang * dst_hold_safe() 2508d24406c8SWei Wang */ 2509421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2510bc044e8dSPaolo Abeni 2511bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2512bc044e8dSPaolo Abeni * the source on each packet 2513bc044e8dSPaolo Abeni */ 2514bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2515bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 2516bc044e8dSPaolo Abeni iph->saddr, iph->tos, 2517bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2518421b3885SShawn Bohrer } 25197487449cSPaolo Abeni return 0; 252010e2eb87SEric Dumazet } 2521421b3885SShawn Bohrer 2522db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2523db8dac20SDavid S. Miller { 2524645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2525db8dac20SDavid S. Miller } 2526db8dac20SDavid S. Miller 25277d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2528db8dac20SDavid S. Miller { 252944046a59STom Parkin struct udp_sock *up = udp_sk(sk); 25308a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2531db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 25328a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 253360fb9567SPaolo Abeni if (static_branch_unlikely(&udp_encap_needed_key)) { 253460fb9567SPaolo Abeni if (up->encap_type) { 253544046a59STom Parkin void (*encap_destroy)(struct sock *sk); 25366aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 253744046a59STom Parkin if (encap_destroy) 253844046a59STom Parkin encap_destroy(sk); 253944046a59STom Parkin } 254060fb9567SPaolo Abeni if (up->encap_enabled) 25419c480601SPaolo Abeni static_branch_dec(&udp_encap_needed_key); 254260fb9567SPaolo Abeni } 2543db8dac20SDavid S. Miller } 2544db8dac20SDavid S. Miller 25451da177e4SLinus Torvalds /* 25461da177e4SLinus Torvalds * Socket option code for UDP 25471da177e4SLinus Torvalds */ 25484c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 2549b7058842SDavid S. Miller char __user *optval, unsigned int optlen, 25504c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 25511da177e4SLinus Torvalds { 25521da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 25531c19448cSTom Herbert int val, valbool; 25541da177e4SLinus Torvalds int err = 0; 2555b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds if (optlen < sizeof(int)) 25581da177e4SLinus Torvalds return -EINVAL; 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 25611da177e4SLinus Torvalds return -EFAULT; 25621da177e4SLinus Torvalds 25631c19448cSTom Herbert valbool = val ? 1 : 0; 25641c19448cSTom Herbert 25651da177e4SLinus Torvalds switch (optname) { 25661da177e4SLinus Torvalds case UDP_CORK: 25671da177e4SLinus Torvalds if (val != 0) { 25681da177e4SLinus Torvalds up->corkflag = 1; 25691da177e4SLinus Torvalds } else { 25701da177e4SLinus Torvalds up->corkflag = 0; 25711da177e4SLinus Torvalds lock_sock(sk); 25724243cdc2SJoe Perches push_pending_frames(sk); 25731da177e4SLinus Torvalds release_sock(sk); 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds break; 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds case UDP_ENCAP: 25781da177e4SLinus Torvalds switch (val) { 25791da177e4SLinus Torvalds case 0: 2580fd1ac07fSAlexey Dobriyan #ifdef CONFIG_XFRM 25811da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 25821da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 25830146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_IPV6) 25840146dca7SSabrina Dubroca if (sk->sk_family == AF_INET6) 25850146dca7SSabrina Dubroca up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv; 25860146dca7SSabrina Dubroca else 25870146dca7SSabrina Dubroca #endif 2588067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 2589fd1ac07fSAlexey Dobriyan #endif 2590a8eceea8SJoe Perches fallthrough; 2591342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 25921da177e4SLinus Torvalds up->encap_type = val; 259360fb9567SPaolo Abeni lock_sock(sk); 259460fb9567SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 259560fb9567SPaolo Abeni release_sock(sk); 25961da177e4SLinus Torvalds break; 25971da177e4SLinus Torvalds default: 25981da177e4SLinus Torvalds err = -ENOPROTOOPT; 25991da177e4SLinus Torvalds break; 26001da177e4SLinus Torvalds } 26011da177e4SLinus Torvalds break; 26021da177e4SLinus Torvalds 26031c19448cSTom Herbert case UDP_NO_CHECK6_TX: 26041c19448cSTom Herbert up->no_check6_tx = valbool; 26051c19448cSTom Herbert break; 26061c19448cSTom Herbert 26071c19448cSTom Herbert case UDP_NO_CHECK6_RX: 26081c19448cSTom Herbert up->no_check6_rx = valbool; 26091c19448cSTom Herbert break; 26101c19448cSTom Herbert 2611bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2612bec1f6f6SWillem de Bruijn if (val < 0 || val > USHRT_MAX) 2613bec1f6f6SWillem de Bruijn return -EINVAL; 2614bec1f6f6SWillem de Bruijn up->gso_size = val; 2615bec1f6f6SWillem de Bruijn break; 2616bec1f6f6SWillem de Bruijn 2617e20cf8d3SPaolo Abeni case UDP_GRO: 2618e20cf8d3SPaolo Abeni lock_sock(sk); 2619e20cf8d3SPaolo Abeni if (valbool) 2620e20cf8d3SPaolo Abeni udp_tunnel_encap_enable(sk->sk_socket); 2621e20cf8d3SPaolo Abeni up->gro_enabled = valbool; 2622e20cf8d3SPaolo Abeni release_sock(sk); 2623e20cf8d3SPaolo Abeni break; 2624e20cf8d3SPaolo Abeni 2625ba4e58ecSGerrit Renker /* 2626ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2627ba4e58ecSGerrit Renker */ 2628ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2629ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2630ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2631b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2632ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2633ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2634ba4e58ecSGerrit Renker val = 8; 26354be929beSAlexey Dobriyan else if (val > USHRT_MAX) 26364be929beSAlexey Dobriyan val = USHRT_MAX; 2637ba4e58ecSGerrit Renker up->pcslen = val; 2638ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 2639ba4e58ecSGerrit Renker break; 2640ba4e58ecSGerrit Renker 2641ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2642ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2643ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2644ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2645b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2646ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2647ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2648ba4e58ecSGerrit Renker val = 8; 26494be929beSAlexey Dobriyan else if (val > USHRT_MAX) 26504be929beSAlexey Dobriyan val = USHRT_MAX; 2651ba4e58ecSGerrit Renker up->pcrlen = val; 2652ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 2653ba4e58ecSGerrit Renker break; 2654ba4e58ecSGerrit Renker 26551da177e4SLinus Torvalds default: 26561da177e4SLinus Torvalds err = -ENOPROTOOPT; 26571da177e4SLinus Torvalds break; 26586516c655SStephen Hemminger } 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds return err; 26611da177e4SLinus Torvalds } 2662c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 26631da177e4SLinus Torvalds 2664db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 2665b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 2666db8dac20SDavid S. Miller { 2667db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2668db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2669db8dac20SDavid S. Miller udp_push_pending_frames); 2670db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2671db8dac20SDavid S. Miller } 2672db8dac20SDavid S. Miller 2673db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2674db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 2675b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 2676db8dac20SDavid S. Miller { 2677db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2678db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2679db8dac20SDavid S. Miller udp_push_pending_frames); 2680db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 2681db8dac20SDavid S. Miller } 2682db8dac20SDavid S. Miller #endif 2683db8dac20SDavid S. Miller 26844c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 26851da177e4SLinus Torvalds char __user *optval, int __user *optlen) 26861da177e4SLinus Torvalds { 26871da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 26881da177e4SLinus Torvalds int val, len; 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds if (get_user(len, optlen)) 26911da177e4SLinus Torvalds return -EFAULT; 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds if (len < 0) 26961da177e4SLinus Torvalds return -EINVAL; 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds switch (optname) { 26991da177e4SLinus Torvalds case UDP_CORK: 27001da177e4SLinus Torvalds val = up->corkflag; 27011da177e4SLinus Torvalds break; 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds case UDP_ENCAP: 27041da177e4SLinus Torvalds val = up->encap_type; 27051da177e4SLinus Torvalds break; 27061da177e4SLinus Torvalds 27071c19448cSTom Herbert case UDP_NO_CHECK6_TX: 27081c19448cSTom Herbert val = up->no_check6_tx; 27091c19448cSTom Herbert break; 27101c19448cSTom Herbert 27111c19448cSTom Herbert case UDP_NO_CHECK6_RX: 27121c19448cSTom Herbert val = up->no_check6_rx; 27131c19448cSTom Herbert break; 27141c19448cSTom Herbert 2715bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2716bec1f6f6SWillem de Bruijn val = up->gso_size; 2717bec1f6f6SWillem de Bruijn break; 2718bec1f6f6SWillem de Bruijn 2719ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2720ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2721ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2722ba4e58ecSGerrit Renker val = up->pcslen; 2723ba4e58ecSGerrit Renker break; 2724ba4e58ecSGerrit Renker 2725ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2726ba4e58ecSGerrit Renker val = up->pcrlen; 2727ba4e58ecSGerrit Renker break; 2728ba4e58ecSGerrit Renker 27291da177e4SLinus Torvalds default: 27301da177e4SLinus Torvalds return -ENOPROTOOPT; 27316516c655SStephen Hemminger } 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds if (put_user(len, optlen)) 27341da177e4SLinus Torvalds return -EFAULT; 27351da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 27361da177e4SLinus Torvalds return -EFAULT; 27371da177e4SLinus Torvalds return 0; 27381da177e4SLinus Torvalds } 2739c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 27401da177e4SLinus Torvalds 2741db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2742db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2743db8dac20SDavid S. Miller { 2744db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2745db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2746db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2747db8dac20SDavid S. Miller } 2748db8dac20SDavid S. Miller 2749db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2750db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 2751db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2752db8dac20SDavid S. Miller { 2753db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2754db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2755db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 2756db8dac20SDavid S. Miller } 2757db8dac20SDavid S. Miller #endif 27581da177e4SLinus Torvalds /** 27591da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 27603628e3cbSAndrew Lunn * @file: - file struct 27613628e3cbSAndrew Lunn * @sock: - socket 27623628e3cbSAndrew Lunn * @wait: - poll table 27631da177e4SLinus Torvalds * 27641da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 27651da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 27661da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 27671da177e4SLinus Torvalds * then it could get return from select indicating data available 27681da177e4SLinus Torvalds * but then block when reading it. Add special case code 27691da177e4SLinus Torvalds * to work around these arguably broken applications. 27701da177e4SLinus Torvalds */ 2771a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 27721da177e4SLinus Torvalds { 2773a11e1d43SLinus Torvalds __poll_t mask = datagram_poll(file, sock, wait); 27741da177e4SLinus Torvalds struct sock *sk = sock->sk; 27751da177e4SLinus Torvalds 27763ef7cf57SEric Dumazet if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) 2777a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 27782276f58aSPaolo Abeni 27791da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 2780a11e1d43SLinus Torvalds if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2781e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2782a9a08845SLinus Torvalds mask &= ~(EPOLLIN | EPOLLRDNORM); 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds return mask; 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds } 2787a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll); 27881da177e4SLinus Torvalds 27895d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 27905d77dca8SDavid Ahern { 27915d77dca8SDavid Ahern lock_sock(sk); 27925d77dca8SDavid Ahern 27935d77dca8SDavid Ahern sk->sk_err = err; 27945d77dca8SDavid Ahern sk->sk_error_report(sk); 2795286c72deSEric Dumazet __udp_disconnect(sk, 0); 27965d77dca8SDavid Ahern 27975d77dca8SDavid Ahern release_sock(sk); 27985d77dca8SDavid Ahern 27995d77dca8SDavid Ahern return 0; 28005d77dca8SDavid Ahern } 28015d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 28025d77dca8SDavid Ahern 2803db8dac20SDavid S. Miller struct proto udp_prot = { 2804db8dac20SDavid S. Miller .name = "UDP", 2805db8dac20SDavid S. Miller .owner = THIS_MODULE, 2806db8dac20SDavid S. Miller .close = udp_lib_close, 2807d74bad4eSAndrey Ignatov .pre_connect = udp_pre_connect, 2808db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2809db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2810db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2811850cbaddSPaolo Abeni .init = udp_init_sock, 2812db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2813db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2814db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2815db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2816db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 2817db8dac20SDavid S. Miller .sendpage = udp_sendpage, 28188141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2819db8dac20SDavid S. Miller .hash = udp_lib_hash, 2820db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2821719f8358SEric Dumazet .rehash = udp_v4_rehash, 2822db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 2823db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 2824db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 28251e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 28261e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2827db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 2828645ca708SEric Dumazet .h.udp_table = &udp_table, 2829db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2830db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 2831db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 2832db8dac20SDavid S. Miller #endif 28335d77dca8SDavid Ahern .diag_destroy = udp_abort, 2834db8dac20SDavid S. Miller }; 2835c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 28381da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 28391da177e4SLinus Torvalds 2840645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 28411da177e4SLinus Torvalds { 28421da177e4SLinus Torvalds struct sock *sk; 28439e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 28441da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 28456f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 28461da177e4SLinus Torvalds 28479e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 28489e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 28499e8ca27aSYonghong Song else 28509e8ca27aSYonghong Song afinfo = PDE_DATA(file_inode(seq->file)); 28519e8ca27aSYonghong Song 2852a3d2599bSChristoph Hellwig for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; 2853f86dcc5aSEric Dumazet ++state->bucket) { 2854a3d2599bSChristoph Hellwig struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; 2855f86dcc5aSEric Dumazet 2856ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2857f86dcc5aSEric Dumazet continue; 2858f86dcc5aSEric Dumazet 2859645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 2860ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 2861878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 2862a91275efSDaniel Lezcano continue; 28639e8ca27aSYonghong Song if (afinfo->family == AF_UNSPEC || 28649e8ca27aSYonghong Song sk->sk_family == afinfo->family) 28651da177e4SLinus Torvalds goto found; 28661da177e4SLinus Torvalds } 2867645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds sk = NULL; 28701da177e4SLinus Torvalds found: 28711da177e4SLinus Torvalds return sk; 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 28751da177e4SLinus Torvalds { 28769e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 28771da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 28786f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 28791da177e4SLinus Torvalds 28809e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 28819e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 28829e8ca27aSYonghong Song else 28839e8ca27aSYonghong Song afinfo = PDE_DATA(file_inode(seq->file)); 28849e8ca27aSYonghong Song 28851da177e4SLinus Torvalds do { 2886ca065d0cSEric Dumazet sk = sk_next(sk); 28879e8ca27aSYonghong Song } while (sk && (!net_eq(sock_net(sk), net) || 28889e8ca27aSYonghong Song (afinfo->family != AF_UNSPEC && 28899e8ca27aSYonghong Song sk->sk_family != afinfo->family))); 28901da177e4SLinus Torvalds 2891645ca708SEric Dumazet if (!sk) { 2892a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 2893a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 2894645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 28951da177e4SLinus Torvalds } 28961da177e4SLinus Torvalds return sk; 28971da177e4SLinus Torvalds } 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 29001da177e4SLinus Torvalds { 2901645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds if (sk) 29041da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 29051da177e4SLinus Torvalds --pos; 29061da177e4SLinus Torvalds return pos ? NULL : sk; 29071da177e4SLinus Torvalds } 29081da177e4SLinus Torvalds 2909a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos) 29101da177e4SLinus Torvalds { 291130842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 2912f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 291330842f29SVitaly Mayatskikh 2914b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 29151da177e4SLinus Torvalds } 2916a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start); 29171da177e4SLinus Torvalds 2918a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 29191da177e4SLinus Torvalds { 29201da177e4SLinus Torvalds struct sock *sk; 29211da177e4SLinus Torvalds 2922b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 29231da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 29241da177e4SLinus Torvalds else 29251da177e4SLinus Torvalds sk = udp_get_next(seq, v); 29261da177e4SLinus Torvalds 29271da177e4SLinus Torvalds ++*pos; 29281da177e4SLinus Torvalds return sk; 29291da177e4SLinus Torvalds } 2930a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next); 29311da177e4SLinus Torvalds 2932a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v) 29331da177e4SLinus Torvalds { 29349e8ca27aSYonghong Song struct udp_seq_afinfo *afinfo; 2935645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 2936645ca708SEric Dumazet 29379e8ca27aSYonghong Song if (state->bpf_seq_afinfo) 29389e8ca27aSYonghong Song afinfo = state->bpf_seq_afinfo; 29399e8ca27aSYonghong Song else 29409e8ca27aSYonghong Song afinfo = PDE_DATA(file_inode(seq->file)); 29419e8ca27aSYonghong Song 2942a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 2943a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 29441da177e4SLinus Torvalds } 2945a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop); 2946db8dac20SDavid S. Miller 2947db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 29485e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2949652586dfSTetsuo Handa int bucket) 2950db8dac20SDavid S. Miller { 2951db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 2952c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 2953c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 2954c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 2955c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 2956db8dac20SDavid S. Miller 2957f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2958ea9a0379SPatrick Talbert " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", 2959db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 296031e6d363SEric Dumazet sk_wmem_alloc_get(sp), 29616c206b20SPaolo Abeni udp_rqueue_get(sp), 2962a7cb5a49SEric W. Biederman 0, 0L, 0, 2963a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 2964a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 296541c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 2966652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 2967db8dac20SDavid S. Miller } 2968db8dac20SDavid S. Miller 2969db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 2970db8dac20SDavid S. Miller { 2971652586dfSTetsuo Handa seq_setwidth(seq, 127); 2972db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 2973652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 2974db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 2975cb61cb9bSEric Dumazet "inode ref pointer drops"); 2976db8dac20SDavid S. Miller else { 2977db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 2978db8dac20SDavid S. Miller 2979652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 2980db8dac20SDavid S. Miller } 2981652586dfSTetsuo Handa seq_pad(seq, '\n'); 2982db8dac20SDavid S. Miller return 0; 2983db8dac20SDavid S. Miller } 2984db8dac20SDavid S. Miller 29855788b3a0SYonghong Song #ifdef CONFIG_BPF_SYSCALL 29865788b3a0SYonghong Song struct bpf_iter__udp { 29875788b3a0SYonghong Song __bpf_md_ptr(struct bpf_iter_meta *, meta); 29885788b3a0SYonghong Song __bpf_md_ptr(struct udp_sock *, udp_sk); 29895788b3a0SYonghong Song uid_t uid __aligned(8); 29905788b3a0SYonghong Song int bucket __aligned(8); 29915788b3a0SYonghong Song }; 29925788b3a0SYonghong Song 29935788b3a0SYonghong Song static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, 29945788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 29955788b3a0SYonghong Song { 29965788b3a0SYonghong Song struct bpf_iter__udp ctx; 29975788b3a0SYonghong Song 29985788b3a0SYonghong Song meta->seq_num--; /* skip SEQ_START_TOKEN */ 29995788b3a0SYonghong Song ctx.meta = meta; 30005788b3a0SYonghong Song ctx.udp_sk = udp_sk; 30015788b3a0SYonghong Song ctx.uid = uid; 30025788b3a0SYonghong Song ctx.bucket = bucket; 30035788b3a0SYonghong Song return bpf_iter_run_prog(prog, &ctx); 30045788b3a0SYonghong Song } 30055788b3a0SYonghong Song 30065788b3a0SYonghong Song static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) 30075788b3a0SYonghong Song { 30085788b3a0SYonghong Song struct udp_iter_state *state = seq->private; 30095788b3a0SYonghong Song struct bpf_iter_meta meta; 30105788b3a0SYonghong Song struct bpf_prog *prog; 30115788b3a0SYonghong Song struct sock *sk = v; 30125788b3a0SYonghong Song uid_t uid; 30135788b3a0SYonghong Song 30145788b3a0SYonghong Song if (v == SEQ_START_TOKEN) 30155788b3a0SYonghong Song return 0; 30165788b3a0SYonghong Song 30175788b3a0SYonghong Song uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); 30185788b3a0SYonghong Song meta.seq = seq; 30195788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, false); 30205788b3a0SYonghong Song return udp_prog_seq_show(prog, &meta, v, uid, state->bucket); 30215788b3a0SYonghong Song } 30225788b3a0SYonghong Song 30235788b3a0SYonghong Song static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) 30245788b3a0SYonghong Song { 30255788b3a0SYonghong Song struct bpf_iter_meta meta; 30265788b3a0SYonghong Song struct bpf_prog *prog; 30275788b3a0SYonghong Song 30285788b3a0SYonghong Song if (!v) { 30295788b3a0SYonghong Song meta.seq = seq; 30305788b3a0SYonghong Song prog = bpf_iter_get_info(&meta, true); 30315788b3a0SYonghong Song if (prog) 30325788b3a0SYonghong Song (void)udp_prog_seq_show(prog, &meta, v, 0, 0); 30335788b3a0SYonghong Song } 30345788b3a0SYonghong Song 30355788b3a0SYonghong Song udp_seq_stop(seq, v); 30365788b3a0SYonghong Song } 30375788b3a0SYonghong Song 30385788b3a0SYonghong Song static const struct seq_operations bpf_iter_udp_seq_ops = { 30395788b3a0SYonghong Song .start = udp_seq_start, 30405788b3a0SYonghong Song .next = udp_seq_next, 30415788b3a0SYonghong Song .stop = bpf_iter_udp_seq_stop, 30425788b3a0SYonghong Song .show = bpf_iter_udp_seq_show, 30435788b3a0SYonghong Song }; 30445788b3a0SYonghong Song #endif 30455788b3a0SYonghong Song 3046c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = { 3047a3d2599bSChristoph Hellwig .start = udp_seq_start, 3048a3d2599bSChristoph Hellwig .next = udp_seq_next, 3049a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 3050a3d2599bSChristoph Hellwig .show = udp4_seq_show, 3051a3d2599bSChristoph Hellwig }; 3052c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops); 305373cb88ecSArjan van de Ven 3054db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 3055db8dac20SDavid S. Miller .family = AF_INET, 3056645ca708SEric Dumazet .udp_table = &udp_table, 3057db8dac20SDavid S. Miller }; 3058db8dac20SDavid S. Miller 30592c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 306015439febSPavel Emelyanov { 3061c3506372SChristoph Hellwig if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 3062c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 3063a3d2599bSChristoph Hellwig return -ENOMEM; 3064a3d2599bSChristoph Hellwig return 0; 306515439febSPavel Emelyanov } 306615439febSPavel Emelyanov 30672c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 306815439febSPavel Emelyanov { 3069a3d2599bSChristoph Hellwig remove_proc_entry("udp", net->proc_net); 307015439febSPavel Emelyanov } 307115439febSPavel Emelyanov 307215439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 307315439febSPavel Emelyanov .init = udp4_proc_init_net, 307415439febSPavel Emelyanov .exit = udp4_proc_exit_net, 307515439febSPavel Emelyanov }; 307615439febSPavel Emelyanov 3077db8dac20SDavid S. Miller int __init udp4_proc_init(void) 3078db8dac20SDavid S. Miller { 307915439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 3080db8dac20SDavid S. Miller } 3081db8dac20SDavid S. Miller 3082db8dac20SDavid S. Miller void udp4_proc_exit(void) 3083db8dac20SDavid S. Miller { 308415439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 3085db8dac20SDavid S. Miller } 30861da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 30871da177e4SLinus Torvalds 3088f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 3089f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 3090645ca708SEric Dumazet { 3091413c27d8SEldad Zack ssize_t ret; 3092413c27d8SEldad Zack 3093f86dcc5aSEric Dumazet if (!str) 3094f86dcc5aSEric Dumazet return 0; 3095413c27d8SEldad Zack 3096413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 3097413c27d8SEldad Zack if (ret) 3098413c27d8SEldad Zack return 0; 3099413c27d8SEldad Zack 3100f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 3101f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 3102f86dcc5aSEric Dumazet return 1; 3103f86dcc5aSEric Dumazet } 3104f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 3105645ca708SEric Dumazet 3106f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 3107f86dcc5aSEric Dumazet { 3108f86dcc5aSEric Dumazet unsigned int i; 3109f86dcc5aSEric Dumazet 3110f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 3111512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 3112f86dcc5aSEric Dumazet uhash_entries, 3113f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 3114f86dcc5aSEric Dumazet 0, 3115f86dcc5aSEric Dumazet &table->log, 3116f86dcc5aSEric Dumazet &table->mask, 311731fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 3118f86dcc5aSEric Dumazet 64 * 1024); 311931fe62b9STim Bird 3120512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 3121f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 3122ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 3123fdcc8aa9SEric Dumazet table->hash[i].count = 0; 3124645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 3125645ca708SEric Dumazet } 3126512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 3127ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 3128512615b6SEric Dumazet table->hash2[i].count = 0; 3129512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 3130512615b6SEric Dumazet } 3131645ca708SEric Dumazet } 3132645ca708SEric Dumazet 3133723b8e46STom Herbert u32 udp_flow_hashrnd(void) 3134723b8e46STom Herbert { 3135723b8e46STom Herbert static u32 hashrnd __read_mostly; 3136723b8e46STom Herbert 3137723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 3138723b8e46STom Herbert 3139723b8e46STom Herbert return hashrnd; 3140723b8e46STom Herbert } 3141723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 3142723b8e46STom Herbert 31431e802951STonghao Zhang static void __udp_sysctl_init(struct net *net) 31441e802951STonghao Zhang { 31451e802951STonghao Zhang net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM; 31461e802951STonghao Zhang net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM; 31471e802951STonghao Zhang 31481e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV 31491e802951STonghao Zhang net->ipv4.sysctl_udp_l3mdev_accept = 0; 31501e802951STonghao Zhang #endif 31511e802951STonghao Zhang } 31521e802951STonghao Zhang 31531e802951STonghao Zhang static int __net_init udp_sysctl_init(struct net *net) 31541e802951STonghao Zhang { 31551e802951STonghao Zhang __udp_sysctl_init(net); 31561e802951STonghao Zhang return 0; 31571e802951STonghao Zhang } 31581e802951STonghao Zhang 31591e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = { 31601e802951STonghao Zhang .init = udp_sysctl_init, 31611e802951STonghao Zhang }; 31621e802951STonghao Zhang 31635788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 31645788b3a0SYonghong Song DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, 31655788b3a0SYonghong Song struct udp_sock *udp_sk, uid_t uid, int bucket) 31665788b3a0SYonghong Song 31675788b3a0SYonghong Song static int bpf_iter_init_udp(void *priv_data) 31685788b3a0SYonghong Song { 31695788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 31705788b3a0SYonghong Song struct udp_seq_afinfo *afinfo; 31715788b3a0SYonghong Song int ret; 31725788b3a0SYonghong Song 31735788b3a0SYonghong Song afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN); 31745788b3a0SYonghong Song if (!afinfo) 31755788b3a0SYonghong Song return -ENOMEM; 31765788b3a0SYonghong Song 31775788b3a0SYonghong Song afinfo->family = AF_UNSPEC; 31785788b3a0SYonghong Song afinfo->udp_table = &udp_table; 31795788b3a0SYonghong Song st->bpf_seq_afinfo = afinfo; 31805788b3a0SYonghong Song ret = bpf_iter_init_seq_net(priv_data); 31815788b3a0SYonghong Song if (ret) 31825788b3a0SYonghong Song kfree(afinfo); 31835788b3a0SYonghong Song return ret; 31845788b3a0SYonghong Song } 31855788b3a0SYonghong Song 31865788b3a0SYonghong Song static void bpf_iter_fini_udp(void *priv_data) 31875788b3a0SYonghong Song { 31885788b3a0SYonghong Song struct udp_iter_state *st = priv_data; 31895788b3a0SYonghong Song 31905788b3a0SYonghong Song kfree(st->bpf_seq_afinfo); 31915788b3a0SYonghong Song bpf_iter_fini_seq_net(priv_data); 31925788b3a0SYonghong Song } 31935788b3a0SYonghong Song 31945788b3a0SYonghong Song static const struct bpf_iter_reg udp_reg_info = { 31955788b3a0SYonghong Song .target = "udp", 31965788b3a0SYonghong Song .seq_ops = &bpf_iter_udp_seq_ops, 31975788b3a0SYonghong Song .init_seq_private = bpf_iter_init_udp, 31985788b3a0SYonghong Song .fini_seq_private = bpf_iter_fini_udp, 31995788b3a0SYonghong Song .seq_priv_size = sizeof(struct udp_iter_state), 32005788b3a0SYonghong Song .ctx_arg_info_size = 1, 32015788b3a0SYonghong Song .ctx_arg_info = { 32025788b3a0SYonghong Song { offsetof(struct bpf_iter__udp, udp_sk), 32035788b3a0SYonghong Song PTR_TO_BTF_ID_OR_NULL }, 32045788b3a0SYonghong Song }, 32055788b3a0SYonghong Song }; 32065788b3a0SYonghong Song 32075788b3a0SYonghong Song static void __init bpf_iter_register(void) 32085788b3a0SYonghong Song { 32095788b3a0SYonghong Song if (bpf_iter_reg_target(&udp_reg_info)) 32105788b3a0SYonghong Song pr_warn("Warning: could not register bpf iterator udp\n"); 32115788b3a0SYonghong Song } 32125788b3a0SYonghong Song #endif 32135788b3a0SYonghong Song 321495766fffSHideo Aoki void __init udp_init(void) 321595766fffSHideo Aoki { 3216f03d78dbSEric Dumazet unsigned long limit; 32174b272750SEric Dumazet unsigned int i; 321895766fffSHideo Aoki 3219f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 3220f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 322195766fffSHideo Aoki limit = max(limit, 128UL); 322295766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 322395766fffSHideo Aoki sysctl_udp_mem[1] = limit; 322495766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 322595766fffSHideo Aoki 32261e802951STonghao Zhang __udp_sysctl_init(&init_net); 32274b272750SEric Dumazet 32284b272750SEric Dumazet /* 16 spinlocks per cpu */ 32294b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 32304b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 32314b272750SEric Dumazet GFP_KERNEL); 32324b272750SEric Dumazet if (!udp_busylocks) 32334b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 32344b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 32354b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 32361e802951STonghao Zhang 32371e802951STonghao Zhang if (register_pernet_subsys(&udp_sysctl_ops)) 32381e802951STonghao Zhang panic("UDP: failed to init sysctl parameters.\n"); 32395788b3a0SYonghong Song 32405788b3a0SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) 32415788b3a0SYonghong Song bpf_iter_register(); 32425788b3a0SYonghong Song #endif 324395766fffSHideo Aoki } 3244