11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * The User Datagram Protocol (UDP). 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 11113aa838SAlan Cox * Alan Cox, <alan@lxorguk.ukuu.org.uk> 121da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Fixes: 151da177e4SLinus Torvalds * Alan Cox : verify_area() calls 161da177e4SLinus Torvalds * Alan Cox : stopped close while in use off icmp 171da177e4SLinus Torvalds * messages. Not a fix but a botch that 181da177e4SLinus Torvalds * for udp at least is 'valid'. 191da177e4SLinus Torvalds * Alan Cox : Fixed icmp handling properly 201da177e4SLinus Torvalds * Alan Cox : Correct error for oversized datagrams 211da177e4SLinus Torvalds * Alan Cox : Tidied select() semantics. 221da177e4SLinus Torvalds * Alan Cox : udp_err() fixed properly, also now 231da177e4SLinus Torvalds * select and read wake correctly on errors 241da177e4SLinus Torvalds * Alan Cox : udp_send verify_area moved to avoid mem leak 251da177e4SLinus Torvalds * Alan Cox : UDP can count its memory 261da177e4SLinus Torvalds * Alan Cox : send to an unknown connection causes 271da177e4SLinus Torvalds * an ECONNREFUSED off the icmp, but 281da177e4SLinus Torvalds * does NOT close. 291da177e4SLinus Torvalds * Alan Cox : Switched to new sk_buff handlers. No more backlog! 301da177e4SLinus Torvalds * Alan Cox : Using generic datagram code. Even smaller and the PEEK 311da177e4SLinus Torvalds * bug no longer crashes it. 321da177e4SLinus Torvalds * Fred Van Kempen : Net2e support for sk->broadcast. 331da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram 341da177e4SLinus Torvalds * Alan Cox : Added get/set sockopt support. 351da177e4SLinus Torvalds * Alan Cox : Broadcasting without option set returns EACCES. 361da177e4SLinus Torvalds * Alan Cox : No wakeup calls. Instead we now use the callbacks. 371da177e4SLinus Torvalds * Alan Cox : Use ip_tos and ip_ttl 381da177e4SLinus Torvalds * Alan Cox : SNMP Mibs 391da177e4SLinus Torvalds * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 401da177e4SLinus Torvalds * Matt Dillon : UDP length checks. 411da177e4SLinus Torvalds * Alan Cox : Smarter af_inet used properly. 421da177e4SLinus Torvalds * Alan Cox : Use new kernel side addressing. 431da177e4SLinus Torvalds * Alan Cox : Incorrect return on truncated datagram receive. 441da177e4SLinus Torvalds * Arnt Gulbrandsen : New udp_send and stuff 451da177e4SLinus Torvalds * Alan Cox : Cache last socket 461da177e4SLinus Torvalds * Alan Cox : Route cache 471da177e4SLinus Torvalds * Jon Peatfield : Minor efficiency fix to sendto(). 481da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 491da177e4SLinus Torvalds * Alan Cox : Nonblocking error fix. 501da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 511da177e4SLinus Torvalds * Mike McLagan : Routing by source 521da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 531da177e4SLinus Torvalds * Last socket cache retained as it 541da177e4SLinus Torvalds * does have a high hit rate. 551da177e4SLinus Torvalds * Olaf Kirch : Don't linearise iovec on sendmsg. 561da177e4SLinus Torvalds * Andi Kleen : Some cleanups, cache destination entry 571da177e4SLinus Torvalds * for connect. 581da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 591da177e4SLinus Torvalds * Melvin Smith : Check msg_name not msg_namelen in sendto(), 601da177e4SLinus Torvalds * return ENOTCONN for unconnected sockets (POSIX) 611da177e4SLinus Torvalds * Janos Farkas : don't deliver multi/broadcasts to a different 621da177e4SLinus Torvalds * bound-to-device socket 631da177e4SLinus Torvalds * Hirokazu Takahashi : HW checksumming for outgoing UDP 641da177e4SLinus Torvalds * datagrams. 651da177e4SLinus Torvalds * Hirokazu Takahashi : sendfile() on UDP works now. 661da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/udp to seq_file 671da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 681da177e4SLinus Torvalds * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 691da177e4SLinus Torvalds * a single port at the same time. 701da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support 71342f0234SJames Chapman * James Chapman : Add L2TP encapsulation type. 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * 741da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 751da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 761da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 771da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds 80afd46503SJoe Perches #define pr_fmt(fmt) "UDP: " fmt 81afd46503SJoe Perches 827c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 831da177e4SLinus Torvalds #include <asm/ioctls.h> 8495766fffSHideo Aoki #include <linux/bootmem.h> 858203efb3SEric Dumazet #include <linux/highmem.h> 868203efb3SEric Dumazet #include <linux/swap.h> 871da177e4SLinus Torvalds #include <linux/types.h> 881da177e4SLinus Torvalds #include <linux/fcntl.h> 891da177e4SLinus Torvalds #include <linux/module.h> 901da177e4SLinus Torvalds #include <linux/socket.h> 911da177e4SLinus Torvalds #include <linux/sockios.h> 9214c85021SArnaldo Carvalho de Melo #include <linux/igmp.h> 936e540309SShawn Bohrer #include <linux/inetdevice.h> 941da177e4SLinus Torvalds #include <linux/in.h> 951da177e4SLinus Torvalds #include <linux/errno.h> 961da177e4SLinus Torvalds #include <linux/timer.h> 971da177e4SLinus Torvalds #include <linux/mm.h> 981da177e4SLinus Torvalds #include <linux/inet.h> 991da177e4SLinus Torvalds #include <linux/netdevice.h> 1005a0e3ad6STejun Heo #include <linux/slab.h> 101c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 1021da177e4SLinus Torvalds #include <linux/skbuff.h> 1031da177e4SLinus Torvalds #include <linux/proc_fs.h> 1041da177e4SLinus Torvalds #include <linux/seq_file.h> 105457c4cbcSEric W. Biederman #include <net/net_namespace.h> 1061da177e4SLinus Torvalds #include <net/icmp.h> 107421b3885SShawn Bohrer #include <net/inet_hashtables.h> 1081da177e4SLinus Torvalds #include <net/route.h> 1091da177e4SLinus Torvalds #include <net/checksum.h> 1101da177e4SLinus Torvalds #include <net/xfrm.h> 111296f7ea7SSatoru Moriya #include <trace/events/udp.h> 112447167bfSEric Dumazet #include <linux/static_key.h> 11322911fc5SEric Dumazet #include <trace/events/skb.h> 114076bb0c8SEliezer Tamir #include <net/busy_poll.h> 115ba4e58ecSGerrit Renker #include "udp_impl.h" 116e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 117217375a0SEric Dumazet #include <net/addrconf.h> 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 125c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly; 12695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min); 127c482c568SEric Dumazet 128c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly; 12995766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min); 13095766fffSHideo Aoki 1318d987e5cSEric Dumazet atomic_long_t udp_memory_allocated; 13295766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 13395766fffSHideo Aoki 134f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536 135f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 13698322f22SEric Dumazet 13763a6fff3SRobert Shearman /* IPCB reference means this can not be used from early demux */ 13863a6fff3SRobert Shearman static bool udp_lib_exact_dif_match(struct net *net, struct sk_buff *skb) 13963a6fff3SRobert Shearman { 14063a6fff3SRobert Shearman #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 14163a6fff3SRobert Shearman if (!net->ipv4.sysctl_udp_l3mdev_accept && 14263a6fff3SRobert Shearman skb && ipv4_l3mdev_skb(IPCB(skb)->flags)) 14363a6fff3SRobert Shearman return true; 14463a6fff3SRobert Shearman #endif 14563a6fff3SRobert Shearman return false; 14663a6fff3SRobert Shearman } 14763a6fff3SRobert Shearman 148f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 149645ca708SEric Dumazet const struct udp_hslot *hslot, 15098322f22SEric Dumazet unsigned long *bitmap, 151fe38d2a1SJosef Bacik struct sock *sk, unsigned int log) 15225030a7fSGerrit Renker { 153f24d43c0SEric Dumazet struct sock *sk2; 154ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 15525030a7fSGerrit Renker 156ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 157f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 158f24d43c0SEric Dumazet sk2 != sk && 159d4cada4aSEric Dumazet (bitmap || udp_sk(sk2)->udp_port_hash == num) && 160f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1619d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1629d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 163fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 164df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 165df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 166df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 167df560056SEric Garver if (!bitmap) 168df560056SEric Garver return 0; 169df560056SEric Garver } else { 1704243cdc2SJoe Perches if (!bitmap) 171fc038410SDavid S. Miller return 1; 172df560056SEric Garver __set_bit(udp_sk(sk2)->udp_port_hash >> log, 173df560056SEric Garver bitmap); 174df560056SEric Garver } 1754243cdc2SJoe Perches } 17698322f22SEric Dumazet } 17725030a7fSGerrit Renker return 0; 17825030a7fSGerrit Renker } 17925030a7fSGerrit Renker 18030fff923SEric Dumazet /* 18130fff923SEric Dumazet * Note: we still hold spinlock of primary hash chain, so no other writer 18230fff923SEric Dumazet * can insert/delete a socket with local_port == num 18330fff923SEric Dumazet */ 18430fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num, 18530fff923SEric Dumazet struct udp_hslot *hslot2, 186fe38d2a1SJosef Bacik struct sock *sk) 18730fff923SEric Dumazet { 18830fff923SEric Dumazet struct sock *sk2; 189ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 19030fff923SEric Dumazet int res = 0; 19130fff923SEric Dumazet 19230fff923SEric Dumazet spin_lock(&hslot2->lock); 193ca065d0cSEric Dumazet udp_portaddr_for_each_entry(sk2, &hslot2->head) { 19430fff923SEric Dumazet if (net_eq(sock_net(sk2), net) && 19530fff923SEric Dumazet sk2 != sk && 19630fff923SEric Dumazet (udp_sk(sk2)->udp_port_hash == num) && 19730fff923SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1989d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1999d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 200fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 201df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 202df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 203df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 204df560056SEric Garver res = 0; 205df560056SEric Garver } else { 20630fff923SEric Dumazet res = 1; 207df560056SEric Garver } 20830fff923SEric Dumazet break; 20930fff923SEric Dumazet } 2104243cdc2SJoe Perches } 21130fff923SEric Dumazet spin_unlock(&hslot2->lock); 21230fff923SEric Dumazet return res; 21330fff923SEric Dumazet } 21430fff923SEric Dumazet 215fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 216e32ea7e7SCraig Gallek { 217e32ea7e7SCraig Gallek struct net *net = sock_net(sk); 218e32ea7e7SCraig Gallek kuid_t uid = sock_i_uid(sk); 219e32ea7e7SCraig Gallek struct sock *sk2; 220e32ea7e7SCraig Gallek 221ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 222e32ea7e7SCraig Gallek if (net_eq(sock_net(sk2), net) && 223e32ea7e7SCraig Gallek sk2 != sk && 224e32ea7e7SCraig Gallek sk2->sk_family == sk->sk_family && 225e32ea7e7SCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 226e32ea7e7SCraig Gallek (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 227e32ea7e7SCraig Gallek (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 228e32ea7e7SCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 229fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) { 230e32ea7e7SCraig Gallek return reuseport_add_sock(sk, sk2); 231e32ea7e7SCraig Gallek } 232e32ea7e7SCraig Gallek } 233e32ea7e7SCraig Gallek 234e32ea7e7SCraig Gallek return reuseport_alloc(sk); 235e32ea7e7SCraig Gallek } 236e32ea7e7SCraig Gallek 23725030a7fSGerrit Renker /** 2386ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 23925030a7fSGerrit Renker * 24025030a7fSGerrit Renker * @sk: socket struct in question 24125030a7fSGerrit Renker * @snum: port number to look up 24225985edcSLucas De Marchi * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 24330fff923SEric Dumazet * with NULL address 24425030a7fSGerrit Renker */ 2456ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 24630fff923SEric Dumazet unsigned int hash2_nulladdr) 2471da177e4SLinus Torvalds { 248512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 249645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 25025030a7fSGerrit Renker int error = 1; 2513b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 2521da177e4SLinus Torvalds 25332c1da70SStephen Hemminger if (!snum) { 2549088c560SEric Dumazet int low, high, remaining; 25595c96174SEric Dumazet unsigned int rand; 25698322f22SEric Dumazet unsigned short first, last; 25798322f22SEric Dumazet DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 2581da177e4SLinus Torvalds 2590bbf87d8SEric W. Biederman inet_get_local_port_range(net, &low, &high); 260a25de534SAnton Arapov remaining = (high - low) + 1; 261227b60f5SStephen Hemminger 26263862b5bSAruna-Hewapathirane rand = prandom_u32(); 2638fc54f68SDaniel Borkmann first = reciprocal_scale(rand, remaining) + low; 26498322f22SEric Dumazet /* 26598322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 26698322f22SEric Dumazet */ 267f86dcc5aSEric Dumazet rand = (rand | 1) * (udptable->mask + 1); 2685781b235SEric Dumazet last = first + udptable->mask + 1; 2695781b235SEric Dumazet do { 270f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, first); 27198322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 272645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 27398322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 274fe38d2a1SJosef Bacik udptable->log); 27598322f22SEric Dumazet 27698322f22SEric Dumazet snum = first; 27798322f22SEric Dumazet /* 27898322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 27998322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 28098322f22SEric Dumazet * give us randomization and full range coverage. 28198322f22SEric Dumazet */ 2829088c560SEric Dumazet do { 28398322f22SEric Dumazet if (low <= snum && snum <= high && 284e3826f1eSAmerigo Wang !test_bit(snum >> udptable->log, bitmap) && 285122ff243SWANG Cong !inet_is_local_reserved_port(net, snum)) 28698322f22SEric Dumazet goto found; 28798322f22SEric Dumazet snum += rand; 28898322f22SEric Dumazet } while (snum != first); 28998322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 290df560056SEric Garver cond_resched(); 2915781b235SEric Dumazet } while (++first != last); 29298322f22SEric Dumazet goto fail; 293645ca708SEric Dumazet } else { 294f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, snum); 295645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 29630fff923SEric Dumazet if (hslot->count > 10) { 29730fff923SEric Dumazet int exist; 29830fff923SEric Dumazet unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 29930fff923SEric Dumazet 30030fff923SEric Dumazet slot2 &= udptable->mask; 30130fff923SEric Dumazet hash2_nulladdr &= udptable->mask; 30230fff923SEric Dumazet 30330fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, slot2); 30430fff923SEric Dumazet if (hslot->count < hslot2->count) 30530fff923SEric Dumazet goto scan_primary_hash; 30630fff923SEric Dumazet 307fe38d2a1SJosef Bacik exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 30830fff923SEric Dumazet if (!exist && (hash2_nulladdr != slot2)) { 30930fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 31030fff923SEric Dumazet exist = udp_lib_lport_inuse2(net, snum, hslot2, 311fe38d2a1SJosef Bacik sk); 31230fff923SEric Dumazet } 31330fff923SEric Dumazet if (exist) 31430fff923SEric Dumazet goto fail_unlock; 31530fff923SEric Dumazet else 31630fff923SEric Dumazet goto found; 31730fff923SEric Dumazet } 31830fff923SEric Dumazet scan_primary_hash: 319fe38d2a1SJosef Bacik if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 320645ca708SEric Dumazet goto fail_unlock; 321645ca708SEric Dumazet } 32298322f22SEric Dumazet found: 323c720c7e8SEric Dumazet inet_sk(sk)->inet_num = snum; 324d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash = snum; 325d4cada4aSEric Dumazet udp_sk(sk)->udp_portaddr_hash ^= snum; 3261da177e4SLinus Torvalds if (sk_unhashed(sk)) { 327e32ea7e7SCraig Gallek if (sk->sk_reuseport && 328fe38d2a1SJosef Bacik udp_reuseport_add_sock(sk, hslot)) { 329e32ea7e7SCraig Gallek inet_sk(sk)->inet_num = 0; 330e32ea7e7SCraig Gallek udp_sk(sk)->udp_port_hash = 0; 331e32ea7e7SCraig Gallek udp_sk(sk)->udp_portaddr_hash ^= snum; 332e32ea7e7SCraig Gallek goto fail_unlock; 333e32ea7e7SCraig Gallek } 334e32ea7e7SCraig Gallek 335ca065d0cSEric Dumazet sk_add_node_rcu(sk, &hslot->head); 336fdcc8aa9SEric Dumazet hslot->count++; 337c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 338512615b6SEric Dumazet 339512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 340512615b6SEric Dumazet spin_lock(&hslot2->lock); 341d894ba18SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 342d894ba18SCraig Gallek sk->sk_family == AF_INET6) 3431602f49bSDavid S. Miller hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 344d894ba18SCraig Gallek &hslot2->head); 345d894ba18SCraig Gallek else 346ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 347512615b6SEric Dumazet &hslot2->head); 348512615b6SEric Dumazet hslot2->count++; 349512615b6SEric Dumazet spin_unlock(&hslot2->lock); 3501da177e4SLinus Torvalds } 351ca065d0cSEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 35225030a7fSGerrit Renker error = 0; 353645ca708SEric Dumazet fail_unlock: 354645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 3551da177e4SLinus Torvalds fail: 35625030a7fSGerrit Renker return error; 3571da177e4SLinus Torvalds } 358c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port); 3591da177e4SLinus Torvalds 3606eada011SEric Dumazet static u32 udp4_portaddr_hash(const struct net *net, __be32 saddr, 361d4cada4aSEric Dumazet unsigned int port) 362d4cada4aSEric Dumazet { 3630eae88f3SEric Dumazet return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port; 364d4cada4aSEric Dumazet } 365d4cada4aSEric Dumazet 3666ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 367db8dac20SDavid S. Miller { 36830fff923SEric Dumazet unsigned int hash2_nulladdr = 3690eae88f3SEric Dumazet udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 37030fff923SEric Dumazet unsigned int hash2_partial = 37130fff923SEric Dumazet udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 37230fff923SEric Dumazet 373d4cada4aSEric Dumazet /* precompute partial secondary hash */ 37430fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 375fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 376db8dac20SDavid S. Miller } 377db8dac20SDavid S. Miller 378d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 379d1e37288SSu, Xuemin __be32 saddr, __be16 sport, 380fb74c277SDavid Ahern __be32 daddr, unsigned short hnum, 381fb74c277SDavid Ahern int dif, int sdif, bool exact_dif) 382645ca708SEric Dumazet { 38360c04aecSJoe Perches int score; 38460c04aecSJoe Perches struct inet_sock *inet; 385645ca708SEric Dumazet 38660c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 38760c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 38860c04aecSJoe Perches ipv6_only_sock(sk)) 38960c04aecSJoe Perches return -1; 390645ca708SEric Dumazet 39160c04aecSJoe Perches score = (sk->sk_family == PF_INET) ? 2 : 1; 39260c04aecSJoe Perches inet = inet_sk(sk); 39360c04aecSJoe Perches 394c720c7e8SEric Dumazet if (inet->inet_rcv_saddr) { 395c720c7e8SEric Dumazet if (inet->inet_rcv_saddr != daddr) 396645ca708SEric Dumazet return -1; 397ba418fa3STom Herbert score += 4; 398645ca708SEric Dumazet } 39960c04aecSJoe Perches 400c720c7e8SEric Dumazet if (inet->inet_daddr) { 401c720c7e8SEric Dumazet if (inet->inet_daddr != saddr) 402645ca708SEric Dumazet return -1; 403ba418fa3STom Herbert score += 4; 404645ca708SEric Dumazet } 40560c04aecSJoe Perches 406c720c7e8SEric Dumazet if (inet->inet_dport) { 407c720c7e8SEric Dumazet if (inet->inet_dport != sport) 408645ca708SEric Dumazet return -1; 409ba418fa3STom Herbert score += 4; 410645ca708SEric Dumazet } 41160c04aecSJoe Perches 41263a6fff3SRobert Shearman if (sk->sk_bound_dev_if || exact_dif) { 413fb74c277SDavid Ahern bool dev_match = (sk->sk_bound_dev_if == dif || 414fb74c277SDavid Ahern sk->sk_bound_dev_if == sdif); 415fb74c277SDavid Ahern 416fb74c277SDavid Ahern if (exact_dif && !dev_match) 417645ca708SEric Dumazet return -1; 418fb74c277SDavid Ahern if (sk->sk_bound_dev_if && dev_match) 419ba418fa3STom Herbert score += 4; 420645ca708SEric Dumazet } 421fb74c277SDavid Ahern 42270da268bSEric Dumazet if (sk->sk_incoming_cpu == raw_smp_processor_id()) 42370da268bSEric Dumazet score++; 424645ca708SEric Dumazet return score; 425645ca708SEric Dumazet } 426645ca708SEric Dumazet 4276eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 42865cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 42965cd8033SHannes Frederic Sowa const __be16 fport) 43065cd8033SHannes Frederic Sowa { 4311bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4321bbdceefSHannes Frederic Sowa 4331bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4341bbdceefSHannes Frederic Sowa 43565cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4361bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 43765cd8033SHannes Frederic Sowa } 43865cd8033SHannes Frederic Sowa 439d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4405051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4415051ebd2SEric Dumazet __be32 saddr, __be16 sport, 442fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 443fb74c277SDavid Ahern int dif, int sdif, bool exact_dif, 444d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4451134158bSCraig Gallek struct sk_buff *skb) 4465051ebd2SEric Dumazet { 4475051ebd2SEric Dumazet struct sock *sk, *result; 448*e94a62f5SPaolo Abeni int score, badness; 449ba418fa3STom Herbert u32 hash = 0; 4505051ebd2SEric Dumazet 4515051ebd2SEric Dumazet result = NULL; 452ba418fa3STom Herbert badness = 0; 453ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 454d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 455fb74c277SDavid Ahern daddr, hnum, dif, sdif, exact_dif); 4565051ebd2SEric Dumazet if (score > badness) { 457*e94a62f5SPaolo Abeni if (sk->sk_reuseport) { 45865cd8033SHannes Frederic Sowa hash = udp_ehashfn(net, daddr, hnum, 4597c0cadc6SEric Dumazet saddr, sport); 460ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 4611134158bSCraig Gallek sizeof(struct udphdr)); 462ca065d0cSEric Dumazet if (result) 463ca065d0cSEric Dumazet return result; 464ba418fa3STom Herbert } 465ca065d0cSEric Dumazet badness = score; 466ca065d0cSEric Dumazet result = sk; 4675051ebd2SEric Dumazet } 4685051ebd2SEric Dumazet } 4695051ebd2SEric Dumazet return result; 4705051ebd2SEric Dumazet } 4715051ebd2SEric Dumazet 472db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 473db8dac20SDavid S. Miller * harder than this. -DaveM 474db8dac20SDavid S. Miller */ 475fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 476fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 477fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 478db8dac20SDavid S. Miller { 479271b72c7SEric Dumazet struct sock *sk, *result; 480db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4815051ebd2SEric Dumazet unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 4825051ebd2SEric Dumazet struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 48363a6fff3SRobert Shearman bool exact_dif = udp_lib_exact_dif_match(net, skb); 484*e94a62f5SPaolo Abeni int score, badness; 485ba418fa3STom Herbert u32 hash = 0; 486db8dac20SDavid S. Miller 4875051ebd2SEric Dumazet if (hslot->count > 10) { 4885051ebd2SEric Dumazet hash2 = udp4_portaddr_hash(net, daddr, hnum); 4895051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4905051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4915051ebd2SEric Dumazet if (hslot->count < hslot2->count) 4925051ebd2SEric Dumazet goto begin; 4935051ebd2SEric Dumazet 4945051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 495fb74c277SDavid Ahern daddr, hnum, dif, sdif, 49663a6fff3SRobert Shearman exact_dif, hslot2, skb); 4975051ebd2SEric Dumazet if (!result) { 498d1e37288SSu, Xuemin unsigned int old_slot2 = slot2; 4990eae88f3SEric Dumazet hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 5005051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 501d1e37288SSu, Xuemin /* avoid searching the same slot again. */ 502d1e37288SSu, Xuemin if (unlikely(slot2 == old_slot2)) 503d1e37288SSu, Xuemin return result; 504d1e37288SSu, Xuemin 5055051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 5065051ebd2SEric Dumazet if (hslot->count < hslot2->count) 5075051ebd2SEric Dumazet goto begin; 5085051ebd2SEric Dumazet 5091223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 510fb74c277SDavid Ahern daddr, hnum, dif, sdif, 51163a6fff3SRobert Shearman exact_dif, hslot2, skb); 5125051ebd2SEric Dumazet } 5135051ebd2SEric Dumazet return result; 5145051ebd2SEric Dumazet } 515271b72c7SEric Dumazet begin: 516271b72c7SEric Dumazet result = NULL; 517ba418fa3STom Herbert badness = 0; 518ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 519d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 520fb74c277SDavid Ahern daddr, hnum, dif, sdif, exact_dif); 521645ca708SEric Dumazet if (score > badness) { 522*e94a62f5SPaolo Abeni if (sk->sk_reuseport) { 52365cd8033SHannes Frederic Sowa hash = udp_ehashfn(net, daddr, hnum, 5247c0cadc6SEric Dumazet saddr, sport); 525ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 526538950a1SCraig Gallek sizeof(struct udphdr)); 527ca065d0cSEric Dumazet if (result) 528ca065d0cSEric Dumazet return result; 529ba418fa3STom Herbert } 530ca065d0cSEric Dumazet result = sk; 531ca065d0cSEric Dumazet badness = score; 532db8dac20SDavid S. Miller } 533db8dac20SDavid S. Miller } 534db8dac20SDavid S. Miller return result; 535db8dac20SDavid S. Miller } 536fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 537db8dac20SDavid S. Miller 538607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 539607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 540645ca708SEric Dumazet struct udp_table *udptable) 541607c4aafSKOVACS Krisztian { 542607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 543607c4aafSKOVACS Krisztian 544ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 545607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 546fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 547607c4aafSKOVACS Krisztian } 548607c4aafSKOVACS Krisztian 54963058308STom Herbert struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, 55063058308STom Herbert __be16 sport, __be16 dport) 55163058308STom Herbert { 552ed7cbbceSAlexander Duyck return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table); 55363058308STom Herbert } 55463058308STom Herbert EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); 55563058308STom Herbert 556ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 557ca065d0cSEric Dumazet * Does increment socket refcount. 558ca065d0cSEric Dumazet */ 559ca065d0cSEric Dumazet #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \ 56030f58158SArnd Bergmann IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY) || \ 56130f58158SArnd Bergmann IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 562bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 563bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 564bcd41303SKOVACS Krisztian { 565ca065d0cSEric Dumazet struct sock *sk; 566ca065d0cSEric Dumazet 567ca065d0cSEric Dumazet sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 568fb74c277SDavid Ahern dif, 0, &udp_table, NULL); 56941c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 570ca065d0cSEric Dumazet sk = NULL; 571ca065d0cSEric Dumazet return sk; 572bcd41303SKOVACS Krisztian } 573bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 574ca065d0cSEric Dumazet #endif 575bcd41303SKOVACS Krisztian 576421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, 577421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 578421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 579fb74c277SDavid Ahern int dif, int sdif, unsigned short hnum) 580421b3885SShawn Bohrer { 581421b3885SShawn Bohrer struct inet_sock *inet = inet_sk(sk); 582421b3885SShawn Bohrer 583421b3885SShawn Bohrer if (!net_eq(sock_net(sk), net) || 584421b3885SShawn Bohrer udp_sk(sk)->udp_port_hash != hnum || 585421b3885SShawn Bohrer (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 586421b3885SShawn Bohrer (inet->inet_dport != rmt_port && inet->inet_dport) || 587421b3885SShawn Bohrer (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 588421b3885SShawn Bohrer ipv6_only_sock(sk) || 589fb74c277SDavid Ahern (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif && 590fb74c277SDavid Ahern sk->sk_bound_dev_if != sdif)) 591421b3885SShawn Bohrer return false; 59260d9b031SDavid Ahern if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 593421b3885SShawn Bohrer return false; 594421b3885SShawn Bohrer return true; 595421b3885SShawn Bohrer } 596421b3885SShawn Bohrer 597db8dac20SDavid S. Miller /* 598db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 599db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 600db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 601db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 602db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 603db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 604db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 605db8dac20SDavid S. Miller * to find the appropriate port. 606db8dac20SDavid S. Miller */ 607db8dac20SDavid S. Miller 608645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 609db8dac20SDavid S. Miller { 610db8dac20SDavid S. Miller struct inet_sock *inet; 611b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 612db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 613db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 614db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 615db8dac20SDavid S. Miller struct sock *sk; 616db8dac20SDavid S. Miller int harderr; 617db8dac20SDavid S. Miller int err; 618fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 619db8dac20SDavid S. Miller 620fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 621fb74c277SDavid Ahern iph->saddr, uh->source, skb->dev->ifindex, 0, 622fb74c277SDavid Ahern udptable, NULL); 62351456b29SIan Morris if (!sk) { 6245d3848bcSEric Dumazet __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 625db8dac20SDavid S. Miller return; /* No socket for error */ 626db8dac20SDavid S. Miller } 627db8dac20SDavid S. Miller 628db8dac20SDavid S. Miller err = 0; 629db8dac20SDavid S. Miller harderr = 0; 630db8dac20SDavid S. Miller inet = inet_sk(sk); 631db8dac20SDavid S. Miller 632db8dac20SDavid S. Miller switch (type) { 633db8dac20SDavid S. Miller default: 634db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 635db8dac20SDavid S. Miller err = EHOSTUNREACH; 636db8dac20SDavid S. Miller break; 637db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 638db8dac20SDavid S. Miller goto out; 639db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 640db8dac20SDavid S. Miller err = EPROTO; 641db8dac20SDavid S. Miller harderr = 1; 642db8dac20SDavid S. Miller break; 643db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 644db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 64536393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 646db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 647db8dac20SDavid S. Miller err = EMSGSIZE; 648db8dac20SDavid S. Miller harderr = 1; 649db8dac20SDavid S. Miller break; 650db8dac20SDavid S. Miller } 651db8dac20SDavid S. Miller goto out; 652db8dac20SDavid S. Miller } 653db8dac20SDavid S. Miller err = EHOSTUNREACH; 654db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 655db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 656db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 657db8dac20SDavid S. Miller } 658db8dac20SDavid S. Miller break; 65955be7a9cSDavid S. Miller case ICMP_REDIRECT: 66055be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 6611a462d18SDuan Jiong goto out; 662db8dac20SDavid S. Miller } 663db8dac20SDavid S. Miller 664db8dac20SDavid S. Miller /* 665db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 666db8dac20SDavid S. Miller * 4.1.3.3. 667db8dac20SDavid S. Miller */ 668db8dac20SDavid S. Miller if (!inet->recverr) { 669db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 670db8dac20SDavid S. Miller goto out; 671b1faf566SEric Dumazet } else 672db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 673b1faf566SEric Dumazet 674db8dac20SDavid S. Miller sk->sk_err = err; 675db8dac20SDavid S. Miller sk->sk_error_report(sk); 676db8dac20SDavid S. Miller out: 677ca065d0cSEric Dumazet return; 678db8dac20SDavid S. Miller } 679db8dac20SDavid S. Miller 680db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 681db8dac20SDavid S. Miller { 682645ca708SEric Dumazet __udp4_lib_err(skb, info, &udp_table); 683db8dac20SDavid S. Miller } 684db8dac20SDavid S. Miller 685db8dac20SDavid S. Miller /* 686db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 687db8dac20SDavid S. Miller */ 68836d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 689db8dac20SDavid S. Miller { 690db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 691db8dac20SDavid S. Miller 692db8dac20SDavid S. Miller if (up->pending) { 693db8dac20SDavid S. Miller up->len = 0; 694db8dac20SDavid S. Miller up->pending = 0; 695db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 696db8dac20SDavid S. Miller } 697db8dac20SDavid S. Miller } 69836d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 699db8dac20SDavid S. Miller 700db8dac20SDavid S. Miller /** 701f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 702db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 703db8dac20SDavid S. Miller * (checksum field must be zeroed out) 704f6b9664fSHerbert Xu * @src: source IP address 705f6b9664fSHerbert Xu * @dst: destination IP address 706db8dac20SDavid S. Miller */ 707c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 708db8dac20SDavid S. Miller { 709db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 710f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 711f6b9664fSHerbert Xu int len = skb->len - offset; 712f6b9664fSHerbert Xu int hlen = len; 713db8dac20SDavid S. Miller __wsum csum = 0; 714db8dac20SDavid S. Miller 715ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 716db8dac20SDavid S. Miller /* 717db8dac20SDavid S. Miller * Only one fragment on the socket. 718db8dac20SDavid S. Miller */ 719db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 720db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 721f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 722f6b9664fSHerbert Xu IPPROTO_UDP, 0); 723db8dac20SDavid S. Miller } else { 724ebbe495fSWANG Cong struct sk_buff *frags; 725ebbe495fSWANG Cong 726db8dac20SDavid S. Miller /* 727db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 728db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 729db8dac20SDavid S. Miller * should be together 730db8dac20SDavid S. Miller */ 731ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 732f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 733f6b9664fSHerbert Xu hlen -= frags->len; 734ebbe495fSWANG Cong } 735db8dac20SDavid S. Miller 736f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 737db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 738db8dac20SDavid S. Miller 739db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 740db8dac20SDavid S. Miller if (uh->check == 0) 741db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 742db8dac20SDavid S. Miller } 743db8dac20SDavid S. Miller } 744c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 745db8dac20SDavid S. Miller 746af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 747af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 748af5fcba7STom Herbert */ 749af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 750af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 751af5fcba7STom Herbert { 752af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 753af5fcba7STom Herbert 754179bc67fSEdward Cree if (nocheck) { 755af5fcba7STom Herbert uh->check = 0; 756179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 757af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 758179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 759179bc67fSEdward Cree uh->check = 0; 760179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 761179bc67fSEdward Cree if (uh->check == 0) 762179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 763d75f1306SEdward Cree } else { 764af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 765af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 766af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 767af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 768af5fcba7STom Herbert } 769af5fcba7STom Herbert } 770af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 771af5fcba7STom Herbert 77279ab0531SDavid S. Miller static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4) 773f6b9664fSHerbert Xu { 774f6b9664fSHerbert Xu struct sock *sk = skb->sk; 775f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 776f6b9664fSHerbert Xu struct udphdr *uh; 777f6b9664fSHerbert Xu int err = 0; 778f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 779f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 780f6b9664fSHerbert Xu int len = skb->len - offset; 781f6b9664fSHerbert Xu __wsum csum = 0; 782f6b9664fSHerbert Xu 783f6b9664fSHerbert Xu /* 784f6b9664fSHerbert Xu * Create a UDP header 785f6b9664fSHerbert Xu */ 786f6b9664fSHerbert Xu uh = udp_hdr(skb); 787f6b9664fSHerbert Xu uh->source = inet->inet_sport; 78879ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 789f6b9664fSHerbert Xu uh->len = htons(len); 790f6b9664fSHerbert Xu uh->check = 0; 791f6b9664fSHerbert Xu 792f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 793f6b9664fSHerbert Xu csum = udplite_csum(skb); 794f6b9664fSHerbert Xu 795ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 796f6b9664fSHerbert Xu 797f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 798f6b9664fSHerbert Xu goto send; 799f6b9664fSHerbert Xu 800f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 801f6b9664fSHerbert Xu 80279ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 803f6b9664fSHerbert Xu goto send; 804f6b9664fSHerbert Xu 805f6b9664fSHerbert Xu } else 806f6b9664fSHerbert Xu csum = udp_csum(skb); 807f6b9664fSHerbert Xu 808f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 80979ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 810f6b9664fSHerbert Xu sk->sk_protocol, csum); 811f6b9664fSHerbert Xu if (uh->check == 0) 812f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 813f6b9664fSHerbert Xu 814f6b9664fSHerbert Xu send: 815b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 816f6b9664fSHerbert Xu if (err) { 817f6b9664fSHerbert Xu if (err == -ENOBUFS && !inet->recverr) { 8186aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 819f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 820f6b9664fSHerbert Xu err = 0; 821f6b9664fSHerbert Xu } 822f6b9664fSHerbert Xu } else 8236aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 824f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 825f6b9664fSHerbert Xu return err; 826f6b9664fSHerbert Xu } 827f6b9664fSHerbert Xu 828db8dac20SDavid S. Miller /* 829db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 830db8dac20SDavid S. Miller */ 8318822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 832db8dac20SDavid S. Miller { 833db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 834db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 835b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 836db8dac20SDavid S. Miller struct sk_buff *skb; 837db8dac20SDavid S. Miller int err = 0; 838db8dac20SDavid S. Miller 83977968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 840f6b9664fSHerbert Xu if (!skb) 841db8dac20SDavid S. Miller goto out; 842db8dac20SDavid S. Miller 84379ab0531SDavid S. Miller err = udp_send_skb(skb, fl4); 844db8dac20SDavid S. Miller 845db8dac20SDavid S. Miller out: 846db8dac20SDavid S. Miller up->len = 0; 847db8dac20SDavid S. Miller up->pending = 0; 848db8dac20SDavid S. Miller return err; 849db8dac20SDavid S. Miller } 8508822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 851db8dac20SDavid S. Miller 8521b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 853db8dac20SDavid S. Miller { 854db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 855db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 856e474995fSDavid S. Miller struct flowi4 fl4_stack; 857b6f21b26SDavid S. Miller struct flowi4 *fl4; 858db8dac20SDavid S. Miller int ulen = len; 859db8dac20SDavid S. Miller struct ipcm_cookie ipc; 860db8dac20SDavid S. Miller struct rtable *rt = NULL; 861db8dac20SDavid S. Miller int free = 0; 862db8dac20SDavid S. Miller int connected = 0; 863db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 864db8dac20SDavid S. Miller __be16 dport; 865db8dac20SDavid S. Miller u8 tos; 866db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 867db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 868db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 869903ab86dSHerbert Xu struct sk_buff *skb; 870f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 871db8dac20SDavid S. Miller 872db8dac20SDavid S. Miller if (len > 0xFFFF) 873db8dac20SDavid S. Miller return -EMSGSIZE; 874db8dac20SDavid S. Miller 875db8dac20SDavid S. Miller /* 876db8dac20SDavid S. Miller * Check the flags. 877db8dac20SDavid S. Miller */ 878db8dac20SDavid S. Miller 879db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 880db8dac20SDavid S. Miller return -EOPNOTSUPP; 881db8dac20SDavid S. Miller 882db8dac20SDavid S. Miller ipc.opt = NULL; 8832244d07bSOliver Hartkopp ipc.tx_flags = 0; 884aa661581SFrancesco Fusco ipc.ttl = 0; 885aa661581SFrancesco Fusco ipc.tos = -1; 886db8dac20SDavid S. Miller 887903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 888903ab86dSHerbert Xu 889f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 890db8dac20SDavid S. Miller if (up->pending) { 891db8dac20SDavid S. Miller /* 892db8dac20SDavid S. Miller * There are pending frames. 893db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 894db8dac20SDavid S. Miller */ 895db8dac20SDavid S. Miller lock_sock(sk); 896db8dac20SDavid S. Miller if (likely(up->pending)) { 897db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 898db8dac20SDavid S. Miller release_sock(sk); 899db8dac20SDavid S. Miller return -EINVAL; 900db8dac20SDavid S. Miller } 901db8dac20SDavid S. Miller goto do_append_data; 902db8dac20SDavid S. Miller } 903db8dac20SDavid S. Miller release_sock(sk); 904db8dac20SDavid S. Miller } 905db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 906db8dac20SDavid S. Miller 907db8dac20SDavid S. Miller /* 908db8dac20SDavid S. Miller * Get and verify the address. 909db8dac20SDavid S. Miller */ 910db8dac20SDavid S. Miller if (msg->msg_name) { 911342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 912db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 913db8dac20SDavid S. Miller return -EINVAL; 914db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 915db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 916db8dac20SDavid S. Miller return -EAFNOSUPPORT; 917db8dac20SDavid S. Miller } 918db8dac20SDavid S. Miller 919db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 920db8dac20SDavid S. Miller dport = usin->sin_port; 921db8dac20SDavid S. Miller if (dport == 0) 922db8dac20SDavid S. Miller return -EINVAL; 923db8dac20SDavid S. Miller } else { 924db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 925db8dac20SDavid S. Miller return -EDESTADDRREQ; 926c720c7e8SEric Dumazet daddr = inet->inet_daddr; 927c720c7e8SEric Dumazet dport = inet->inet_dport; 928db8dac20SDavid S. Miller /* Open fast path for connected socket. 929db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 930db8dac20SDavid S. Miller */ 931db8dac20SDavid S. Miller connected = 1; 932db8dac20SDavid S. Miller } 933c14ac945SSoheil Hassas Yeganeh 934c14ac945SSoheil Hassas Yeganeh ipc.sockc.tsflags = sk->sk_tsflags; 935c720c7e8SEric Dumazet ipc.addr = inet->inet_saddr; 936db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 937bf84a010SDaniel Borkmann 938db8dac20SDavid S. Miller if (msg->msg_controllen) { 93924025c46SSoheil Hassas Yeganeh err = ip_cmsg_send(sk, msg, &ipc, sk->sk_family == AF_INET6); 94091948309SEric Dumazet if (unlikely(err)) { 94191948309SEric Dumazet kfree(ipc.opt); 942db8dac20SDavid S. Miller return err; 94391948309SEric Dumazet } 944db8dac20SDavid S. Miller if (ipc.opt) 945db8dac20SDavid S. Miller free = 1; 946db8dac20SDavid S. Miller connected = 0; 947db8dac20SDavid S. Miller } 948f6d8bd05SEric Dumazet if (!ipc.opt) { 949f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 950f6d8bd05SEric Dumazet 951f6d8bd05SEric Dumazet rcu_read_lock(); 952f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 953f6d8bd05SEric Dumazet if (inet_opt) { 954f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 955f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 956f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 957f6d8bd05SEric Dumazet } 958f6d8bd05SEric Dumazet rcu_read_unlock(); 959f6d8bd05SEric Dumazet } 960db8dac20SDavid S. Miller 961db8dac20SDavid S. Miller saddr = ipc.addr; 962db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 963db8dac20SDavid S. Miller 964c14ac945SSoheil Hassas Yeganeh sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags); 965c14ac945SSoheil Hassas Yeganeh 966f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 967db8dac20SDavid S. Miller if (!daddr) 968db8dac20SDavid S. Miller return -EINVAL; 969f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 970db8dac20SDavid S. Miller connected = 0; 971db8dac20SDavid S. Miller } 972aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 973db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 974db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 975f6d8bd05SEric Dumazet (ipc.opt && ipc.opt->opt.is_strictroute)) { 976db8dac20SDavid S. Miller tos |= RTO_ONLINK; 977db8dac20SDavid S. Miller connected = 0; 978db8dac20SDavid S. Miller } 979db8dac20SDavid S. Miller 980db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 981db8dac20SDavid S. Miller if (!ipc.oif) 982db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 983db8dac20SDavid S. Miller if (!saddr) 984db8dac20SDavid S. Miller saddr = inet->mc_addr; 985db8dac20SDavid S. Miller connected = 0; 98676e21053SErich E. Hoover } else if (!ipc.oif) 98776e21053SErich E. Hoover ipc.oif = inet->uc_index; 988db8dac20SDavid S. Miller 989db8dac20SDavid S. Miller if (connected) 990db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 991db8dac20SDavid S. Miller 99251456b29SIan Morris if (!rt) { 99384a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 9949a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 99584a3aa00SPavel Emelyanov 996e474995fSDavid S. Miller fl4 = &fl4_stack; 9979a24abfaSDavid Ahern 9989a24abfaSDavid Ahern flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos, 9999a24abfaSDavid Ahern RT_SCOPE_UNIVERSE, sk->sk_protocol, 10009a24abfaSDavid Ahern flow_flags, 1001e2d118a1SLorenzo Colitti faddr, saddr, dport, inet->inet_sport, 1002e2d118a1SLorenzo Colitti sk->sk_uid); 1003c0951cbcSDavid S. Miller 1004e474995fSDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 1005e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1006b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1007b23dd4feSDavid S. Miller err = PTR_ERR(rt); 100806dc94b1SDavid S. Miller rt = NULL; 1009db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1010f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1011db8dac20SDavid S. Miller goto out; 1012db8dac20SDavid S. Miller } 1013db8dac20SDavid S. Miller 1014db8dac20SDavid S. Miller err = -EACCES; 1015db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1016db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1017db8dac20SDavid S. Miller goto out; 1018db8dac20SDavid S. Miller if (connected) 1019d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1020db8dac20SDavid S. Miller } 1021db8dac20SDavid S. Miller 1022db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1023db8dac20SDavid S. Miller goto do_confirm; 1024db8dac20SDavid S. Miller back_from_confirm: 1025db8dac20SDavid S. Miller 1026e474995fSDavid S. Miller saddr = fl4->saddr; 1027db8dac20SDavid S. Miller if (!ipc.addr) 1028e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1029db8dac20SDavid S. Miller 1030903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1031903ab86dSHerbert Xu if (!corkreq) { 1032f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1033903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 1034903ab86dSHerbert Xu msg->msg_flags); 1035903ab86dSHerbert Xu err = PTR_ERR(skb); 103650c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 103779ab0531SDavid S. Miller err = udp_send_skb(skb, fl4); 1038903ab86dSHerbert Xu goto out; 1039903ab86dSHerbert Xu } 1040903ab86dSHerbert Xu 1041db8dac20SDavid S. Miller lock_sock(sk); 1042db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1043db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1044db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1045db8dac20SDavid S. Miller release_sock(sk); 1046db8dac20SDavid S. Miller 1047197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1048db8dac20SDavid S. Miller err = -EINVAL; 1049db8dac20SDavid S. Miller goto out; 1050db8dac20SDavid S. Miller } 1051db8dac20SDavid S. Miller /* 1052db8dac20SDavid S. Miller * Now cork the socket to pend data. 1053db8dac20SDavid S. Miller */ 1054b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1055b6f21b26SDavid S. Miller fl4->daddr = daddr; 1056b6f21b26SDavid S. Miller fl4->saddr = saddr; 10579cce96dfSDavid S. Miller fl4->fl4_dport = dport; 10589cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1059db8dac20SDavid S. Miller up->pending = AF_INET; 1060db8dac20SDavid S. Miller 1061db8dac20SDavid S. Miller do_append_data: 1062db8dac20SDavid S. Miller up->len += ulen; 1063f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 10642e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1065db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1066db8dac20SDavid S. Miller if (err) 1067db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1068db8dac20SDavid S. Miller else if (!corkreq) 1069db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1070db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1071db8dac20SDavid S. Miller up->pending = 0; 1072db8dac20SDavid S. Miller release_sock(sk); 1073db8dac20SDavid S. Miller 1074db8dac20SDavid S. Miller out: 1075db8dac20SDavid S. Miller ip_rt_put(rt); 1076db8dac20SDavid S. Miller if (free) 1077db8dac20SDavid S. Miller kfree(ipc.opt); 1078db8dac20SDavid S. Miller if (!err) 1079db8dac20SDavid S. Miller return len; 1080db8dac20SDavid S. Miller /* 1081db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1082db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1083db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1084db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1085db8dac20SDavid S. Miller * seems like overkill. 1086db8dac20SDavid S. Miller */ 1087db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 10886aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1089629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1090db8dac20SDavid S. Miller } 1091db8dac20SDavid S. Miller return err; 1092db8dac20SDavid S. Miller 1093db8dac20SDavid S. Miller do_confirm: 10940dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 10950dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1096db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1097db8dac20SDavid S. Miller goto back_from_confirm; 1098db8dac20SDavid S. Miller err = 0; 1099db8dac20SDavid S. Miller goto out; 1100db8dac20SDavid S. Miller } 1101c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1102db8dac20SDavid S. Miller 1103db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 1104db8dac20SDavid S. Miller size_t size, int flags) 1105db8dac20SDavid S. Miller { 1106f5fca608SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1107db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1108db8dac20SDavid S. Miller int ret; 1109db8dac20SDavid S. Miller 1110d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 1111d3f7d56aSShawn Landden flags |= MSG_MORE; 1112d3f7d56aSShawn Landden 1113db8dac20SDavid S. Miller if (!up->pending) { 1114db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1115db8dac20SDavid S. Miller 1116db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 1117db8dac20SDavid S. Miller * sendpage interface can't pass. 1118db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 1119db8dac20SDavid S. Miller */ 11201b784140SYing Xue ret = udp_sendmsg(sk, &msg, 0); 1121db8dac20SDavid S. Miller if (ret < 0) 1122db8dac20SDavid S. Miller return ret; 1123db8dac20SDavid S. Miller } 1124db8dac20SDavid S. Miller 1125db8dac20SDavid S. Miller lock_sock(sk); 1126db8dac20SDavid S. Miller 1127db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 1128db8dac20SDavid S. Miller release_sock(sk); 1129db8dac20SDavid S. Miller 1130197df02cSMatteo Croce net_dbg_ratelimited("cork failed\n"); 1131db8dac20SDavid S. Miller return -EINVAL; 1132db8dac20SDavid S. Miller } 1133db8dac20SDavid S. Miller 1134f5fca608SDavid S. Miller ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1135f5fca608SDavid S. Miller page, offset, size, flags); 1136db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 1137db8dac20SDavid S. Miller release_sock(sk); 1138db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 1139db8dac20SDavid S. Miller size, flags); 1140db8dac20SDavid S. Miller } 1141db8dac20SDavid S. Miller if (ret < 0) { 1142db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1143db8dac20SDavid S. Miller goto out; 1144db8dac20SDavid S. Miller } 1145db8dac20SDavid S. Miller 1146db8dac20SDavid S. Miller up->len += size; 1147db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 1148db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 1149db8dac20SDavid S. Miller if (!ret) 1150db8dac20SDavid S. Miller ret = size; 1151db8dac20SDavid S. Miller out: 1152db8dac20SDavid S. Miller release_sock(sk); 1153db8dac20SDavid S. Miller return ret; 1154db8dac20SDavid S. Miller } 1155db8dac20SDavid S. Miller 1156dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1157dce4551cSPaolo Abeni 1158b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1159b65ac446SPaolo Abeni { 1160dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1161b65ac446SPaolo Abeni 1162b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1163dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1164dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1165b65ac446SPaolo Abeni scratch->len = skb->len; 1166b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1167b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1168b65ac446SPaolo Abeni #endif 11693bdefdf9SPaolo Abeni /* all head states execept sp (dst, sk, nf) are always cleared by 11703bdefdf9SPaolo Abeni * udp_rcv() and we need to preserve secpath, if present, to eventually 11713bdefdf9SPaolo Abeni * process IP_CMSG_PASSSEC at recvmsg() time 11723bdefdf9SPaolo Abeni */ 11733bdefdf9SPaolo Abeni if (likely(!skb_sec_path(skb))) 1174dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1175dce4551cSPaolo Abeni } 1176dce4551cSPaolo Abeni 1177dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1178dce4551cSPaolo Abeni { 1179dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1180dce4551cSPaolo Abeni } 1181dce4551cSPaolo Abeni 1182dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1183dce4551cSPaolo Abeni { 1184dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1185dce4551cSPaolo Abeni } 1186b65ac446SPaolo Abeni 11877c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 11886dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 11896dfb4367SPaolo Abeni bool rx_queue_lock_held) 1190f970bd9eSPaolo Abeni { 11916b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 11922276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1193f970bd9eSPaolo Abeni int amt; 1194f970bd9eSPaolo Abeni 11956b229cf7SEric Dumazet if (likely(partial)) { 11966b229cf7SEric Dumazet up->forward_deficit += size; 11976b229cf7SEric Dumazet size = up->forward_deficit; 11980d4a6608SPaolo Abeni if (size < (sk->sk_rcvbuf >> 2)) 11996b229cf7SEric Dumazet return; 12006b229cf7SEric Dumazet } else { 12016b229cf7SEric Dumazet size += up->forward_deficit; 12026b229cf7SEric Dumazet } 12036b229cf7SEric Dumazet up->forward_deficit = 0; 12046b229cf7SEric Dumazet 12056dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 12066dfb4367SPaolo Abeni * if the called don't held it already 12076dfb4367SPaolo Abeni */ 12082276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 12096dfb4367SPaolo Abeni if (!rx_queue_lock_held) 12102276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 12112276f58aSPaolo Abeni 12126dfb4367SPaolo Abeni 1213f970bd9eSPaolo Abeni sk->sk_forward_alloc += size; 1214f970bd9eSPaolo Abeni amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); 1215f970bd9eSPaolo Abeni sk->sk_forward_alloc -= amt; 1216f970bd9eSPaolo Abeni 1217f970bd9eSPaolo Abeni if (amt) 1218f970bd9eSPaolo Abeni __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); 121902ab0d13SEric Dumazet 122002ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 12212276f58aSPaolo Abeni 12222276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 12232276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 12242276f58aSPaolo Abeni 12256dfb4367SPaolo Abeni if (!rx_queue_lock_held) 12262276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1227f970bd9eSPaolo Abeni } 1228f970bd9eSPaolo Abeni 12292276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1230c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1231c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1232c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1233c84d9490SEric Dumazet */ 12347c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1235f970bd9eSPaolo Abeni { 1236b65ac446SPaolo Abeni prefetch(&skb->data); 1237b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1238f970bd9eSPaolo Abeni } 12397c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1240f970bd9eSPaolo Abeni 12416dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 124264f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 12436dfb4367SPaolo Abeni { 1244b65ac446SPaolo Abeni prefetch(&skb->data); 1245b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 12466dfb4367SPaolo Abeni } 12476dfb4367SPaolo Abeni 12484b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 12494b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 12504b272750SEric Dumazet * Under flood, this means that only one producer can be in line 12514b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 12524b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 12534b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 12544b272750SEric Dumazet */ 12554b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 12564b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 12574b272750SEric Dumazet 12584b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 12594b272750SEric Dumazet { 12604b272750SEric Dumazet spinlock_t *busy; 12614b272750SEric Dumazet 12624b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 12634b272750SEric Dumazet spin_lock(busy); 12644b272750SEric Dumazet return busy; 12654b272750SEric Dumazet } 12664b272750SEric Dumazet 12674b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 12684b272750SEric Dumazet { 12694b272750SEric Dumazet if (busy) 12704b272750SEric Dumazet spin_unlock(busy); 12714b272750SEric Dumazet } 12724b272750SEric Dumazet 1273f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1274f970bd9eSPaolo Abeni { 1275f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1276f970bd9eSPaolo Abeni int rmem, delta, amt, err = -ENOMEM; 12774b272750SEric Dumazet spinlock_t *busy = NULL; 1278c8c8b127SEric Dumazet int size; 1279f970bd9eSPaolo Abeni 1280f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1281f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1282f970bd9eSPaolo Abeni */ 1283f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1284363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1285f970bd9eSPaolo Abeni goto drop; 1286f970bd9eSPaolo Abeni 1287c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1288c8c8b127SEric Dumazet * having linear skbs : 1289c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1290c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1291c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1292c8c8b127SEric Dumazet */ 12934b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1294c8c8b127SEric Dumazet skb_condense(skb); 12954b272750SEric Dumazet 12964b272750SEric Dumazet busy = busylock_acquire(sk); 12974b272750SEric Dumazet } 1298c8c8b127SEric Dumazet size = skb->truesize; 1299b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1300c8c8b127SEric Dumazet 1301f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1302f970bd9eSPaolo Abeni * queue contains some other skb 1303f970bd9eSPaolo Abeni */ 1304f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1305363dc73aSPaolo Abeni if (rmem > (size + sk->sk_rcvbuf)) 1306f970bd9eSPaolo Abeni goto uncharge_drop; 1307f970bd9eSPaolo Abeni 1308f970bd9eSPaolo Abeni spin_lock(&list->lock); 1309f970bd9eSPaolo Abeni if (size >= sk->sk_forward_alloc) { 1310f970bd9eSPaolo Abeni amt = sk_mem_pages(size); 1311f970bd9eSPaolo Abeni delta = amt << SK_MEM_QUANTUM_SHIFT; 1312f970bd9eSPaolo Abeni if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1313f970bd9eSPaolo Abeni err = -ENOBUFS; 1314f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1315f970bd9eSPaolo Abeni goto uncharge_drop; 1316f970bd9eSPaolo Abeni } 1317f970bd9eSPaolo Abeni 1318f970bd9eSPaolo Abeni sk->sk_forward_alloc += delta; 1319f970bd9eSPaolo Abeni } 1320f970bd9eSPaolo Abeni 1321f970bd9eSPaolo Abeni sk->sk_forward_alloc -= size; 1322f970bd9eSPaolo Abeni 13237c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 13247c13f97fSPaolo Abeni * forward allocated memory on dequeue 13257c13f97fSPaolo Abeni */ 1326f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1327f970bd9eSPaolo Abeni 1328f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1329f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1330f970bd9eSPaolo Abeni 1331f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 1332f970bd9eSPaolo Abeni sk->sk_data_ready(sk); 1333f970bd9eSPaolo Abeni 13344b272750SEric Dumazet busylock_release(busy); 1335f970bd9eSPaolo Abeni return 0; 1336f970bd9eSPaolo Abeni 1337f970bd9eSPaolo Abeni uncharge_drop: 1338f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1339f970bd9eSPaolo Abeni 1340f970bd9eSPaolo Abeni drop: 1341f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 13424b272750SEric Dumazet busylock_release(busy); 1343f970bd9eSPaolo Abeni return err; 1344f970bd9eSPaolo Abeni } 1345f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1346f970bd9eSPaolo Abeni 1347c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk) 1348f970bd9eSPaolo Abeni { 1349f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 13502276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 13517c13f97fSPaolo Abeni unsigned int total = 0; 13527c13f97fSPaolo Abeni struct sk_buff *skb; 13537c13f97fSPaolo Abeni 13542276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 13552276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 13567c13f97fSPaolo Abeni total += skb->truesize; 13577c13f97fSPaolo Abeni kfree_skb(skb); 13587c13f97fSPaolo Abeni } 13596dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 13607c13f97fSPaolo Abeni 1361f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1362f970bd9eSPaolo Abeni } 1363c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock); 1364f970bd9eSPaolo Abeni 1365f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1366f970bd9eSPaolo Abeni { 13672276f58aSPaolo Abeni skb_queue_head_init(&udp_sk(sk)->reader_queue); 1368f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1369f970bd9eSPaolo Abeni return 0; 1370f970bd9eSPaolo Abeni } 1371f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock); 1372f970bd9eSPaolo Abeni 1373f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1374f970bd9eSPaolo Abeni { 1375f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1376f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1377f970bd9eSPaolo Abeni 1378f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1379f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1380f970bd9eSPaolo Abeni } 13810a463c78SPaolo Abeni 1382ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1383ca2c1418SPaolo Abeni return; 1384ca2c1418SPaolo Abeni 1385dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1386dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 13870ddf3fb2SPaolo Abeni */ 1388dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 13890ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1390ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1391f970bd9eSPaolo Abeni } 1392f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1393f970bd9eSPaolo Abeni 13942276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 13952276f58aSPaolo Abeni struct sk_buff_head *rcvq, 13962276f58aSPaolo Abeni int *total) 13972276f58aSPaolo Abeni { 13982276f58aSPaolo Abeni struct sk_buff *skb; 13992276f58aSPaolo Abeni 14009bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 14019bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 14022276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 14032276f58aSPaolo Abeni IS_UDPLITE(sk)); 14042276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 14052276f58aSPaolo Abeni IS_UDPLITE(sk)); 14062276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 14072276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 14082276f58aSPaolo Abeni *total += skb->truesize; 14092276f58aSPaolo Abeni kfree_skb(skb); 14109bd780f5SPaolo Abeni } else { 14119bd780f5SPaolo Abeni /* the csum related bits could be changed, refresh 14129bd780f5SPaolo Abeni * the scratch area 14139bd780f5SPaolo Abeni */ 14149bd780f5SPaolo Abeni udp_set_dev_scratch(skb); 14159bd780f5SPaolo Abeni break; 14169bd780f5SPaolo Abeni } 14172276f58aSPaolo Abeni } 14182276f58aSPaolo Abeni return skb; 14192276f58aSPaolo Abeni } 14202276f58aSPaolo Abeni 142185584672SEric Dumazet /** 142285584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 142385584672SEric Dumazet * @sk: socket 142485584672SEric Dumazet * 142585584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1426e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 142785584672SEric Dumazet */ 1428e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 142985584672SEric Dumazet { 14302276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 14312276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 143285584672SEric Dumazet struct sk_buff *skb; 14337c13f97fSPaolo Abeni int total = 0; 1434e83c6744SEric Dumazet int res; 143585584672SEric Dumazet 143685584672SEric Dumazet spin_lock_bh(&rcvq->lock); 14372276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 14382276f58aSPaolo Abeni if (!skb && !skb_queue_empty(sk_queue)) { 14392276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 14402276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 14412276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 14422276f58aSPaolo Abeni 14432276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 144485584672SEric Dumazet } 1445e83c6744SEric Dumazet res = skb ? skb->len : -1; 14467c13f97fSPaolo Abeni if (total) 14476dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 144885584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 144985584672SEric Dumazet return res; 145085584672SEric Dumazet } 145185584672SEric Dumazet 14521da177e4SLinus Torvalds /* 14531da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 14541da177e4SLinus Torvalds */ 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 14571da177e4SLinus Torvalds { 14586516c655SStephen Hemminger switch (cmd) { 14591da177e4SLinus Torvalds case SIOCOUTQ: 14601da177e4SLinus Torvalds { 146131e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 146231e6d363SEric Dumazet 14631da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds case SIOCINQ: 14671da177e4SLinus Torvalds { 1468e83c6744SEric Dumazet int amount = max_t(int, 0, first_packet_length(sk)); 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds default: 14741da177e4SLinus Torvalds return -ENOIOCTLCMD; 14751da177e4SLinus Torvalds } 14766516c655SStephen Hemminger 14776516c655SStephen Hemminger return 0; 14781da177e4SLinus Torvalds } 1479c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 14801da177e4SLinus Torvalds 14812276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 14822276f58aSPaolo Abeni int noblock, int *peeked, int *off, int *err) 14832276f58aSPaolo Abeni { 14842276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 14852276f58aSPaolo Abeni struct sk_buff_head *queue; 14862276f58aSPaolo Abeni struct sk_buff *last; 14872276f58aSPaolo Abeni long timeo; 14882276f58aSPaolo Abeni int error; 14892276f58aSPaolo Abeni 14902276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 14912276f58aSPaolo Abeni flags |= noblock ? MSG_DONTWAIT : 0; 14922276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 14932276f58aSPaolo Abeni do { 14942276f58aSPaolo Abeni struct sk_buff *skb; 14952276f58aSPaolo Abeni 14962276f58aSPaolo Abeni error = sock_error(sk); 14972276f58aSPaolo Abeni if (error) 14982276f58aSPaolo Abeni break; 14992276f58aSPaolo Abeni 15002276f58aSPaolo Abeni error = -EAGAIN; 15012276f58aSPaolo Abeni *peeked = 0; 15022276f58aSPaolo Abeni do { 15032276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 15042276f58aSPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, 15052276f58aSPaolo Abeni udp_skb_destructor, 1506de321ed3SAndrey Vagin peeked, off, err, 15072276f58aSPaolo Abeni &last); 15082276f58aSPaolo Abeni if (skb) { 15092276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 15102276f58aSPaolo Abeni return skb; 15112276f58aSPaolo Abeni } 15122276f58aSPaolo Abeni 15132276f58aSPaolo Abeni if (skb_queue_empty(sk_queue)) { 15142276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 15152276f58aSPaolo Abeni goto busy_check; 15162276f58aSPaolo Abeni } 15172276f58aSPaolo Abeni 15186dfb4367SPaolo Abeni /* refill the reader queue and walk it again 15196dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 15206dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 15216dfb4367SPaolo Abeni * is needed. 15226dfb4367SPaolo Abeni */ 15232276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 15242276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 15252276f58aSPaolo Abeni 15262276f58aSPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, 15276dfb4367SPaolo Abeni udp_skb_dtor_locked, 1528de321ed3SAndrey Vagin peeked, off, err, 15292276f58aSPaolo Abeni &last); 15306dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 15312276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1532de321ed3SAndrey Vagin if (skb) 15332276f58aSPaolo Abeni return skb; 15342276f58aSPaolo Abeni 15352276f58aSPaolo Abeni busy_check: 15362276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 15372276f58aSPaolo Abeni break; 15382276f58aSPaolo Abeni 15392276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 15402276f58aSPaolo Abeni } while (!skb_queue_empty(sk_queue)); 15412276f58aSPaolo Abeni 15422276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 15432276f58aSPaolo Abeni } while (timeo && 15442276f58aSPaolo Abeni !__skb_wait_for_more_packets(sk, &error, &timeo, 15452276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 15462276f58aSPaolo Abeni 15472276f58aSPaolo Abeni *err = error; 15482276f58aSPaolo Abeni return NULL; 15492276f58aSPaolo Abeni } 15502276f58aSPaolo Abeni EXPORT_SYMBOL_GPL(__skb_recv_udp); 15512276f58aSPaolo Abeni 1552db8dac20SDavid S. Miller /* 1553db8dac20SDavid S. Miller * This should be easy, if there is something there we 1554db8dac20SDavid S. Miller * return it, otherwise we block. 1555db8dac20SDavid S. Miller */ 1556db8dac20SDavid S. Miller 15571b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, 15581b784140SYing Xue int flags, int *addr_len) 1559db8dac20SDavid S. Miller { 1560db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1561342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1562db8dac20SDavid S. Miller struct sk_buff *skb; 156359c2cdaeSDavid S. Miller unsigned int ulen, copied; 1564627d2d6bSsamanthakumar int peeked, peeking, off; 1565db8dac20SDavid S. Miller int err; 1566db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1567197c949eSEric Dumazet bool checksum_valid = false; 1568db8dac20SDavid S. Miller 1569db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 157085fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1571db8dac20SDavid S. Miller 1572db8dac20SDavid S. Miller try_again: 1573a0917e0bSMatthew Dawson peeking = flags & MSG_PEEK; 1574a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 15757c13f97fSPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err); 1576db8dac20SDavid S. Miller if (!skb) 1577627d2d6bSsamanthakumar return err; 1578db8dac20SDavid S. Miller 1579b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 158059c2cdaeSDavid S. Miller copied = len; 1581627d2d6bSsamanthakumar if (copied > ulen - off) 1582627d2d6bSsamanthakumar copied = ulen - off; 158359c2cdaeSDavid S. Miller else if (copied < ulen) 1584db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1585db8dac20SDavid S. Miller 1586db8dac20SDavid S. Miller /* 1587db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1588db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1589db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1590db8dac20SDavid S. Miller */ 1591db8dac20SDavid S. Miller 1592d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1593d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1594b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1595b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1596197c949eSEric Dumazet if (!checksum_valid) 1597db8dac20SDavid S. Miller goto csum_copy_err; 1598db8dac20SDavid S. Miller } 1599db8dac20SDavid S. Miller 1600b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1601b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1602b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1603b65ac446SPaolo Abeni else 1604627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1605b65ac446SPaolo Abeni } else { 1606627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1607db8dac20SDavid S. Miller 1608db8dac20SDavid S. Miller if (err == -EINVAL) 1609db8dac20SDavid S. Miller goto csum_copy_err; 1610db8dac20SDavid S. Miller } 1611db8dac20SDavid S. Miller 161222911fc5SEric Dumazet if (unlikely(err)) { 1613979402b1SEric Dumazet if (!peeked) { 1614979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 16156aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1616979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1617979402b1SEric Dumazet } 1618850cbaddSPaolo Abeni kfree_skb(skb); 1619627d2d6bSsamanthakumar return err; 162022911fc5SEric Dumazet } 1621db8dac20SDavid S. Miller 1622db8dac20SDavid S. Miller if (!peeked) 16236aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1624629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1625db8dac20SDavid S. Miller 16263b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 1627db8dac20SDavid S. Miller 1628db8dac20SDavid S. Miller /* Copy the address. */ 1629c482c568SEric Dumazet if (sin) { 1630db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1631db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1632db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1633db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1634bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1635db8dac20SDavid S. Miller } 1636db8dac20SDavid S. Miller if (inet->cmsg_flags) 1637ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1638db8dac20SDavid S. Miller 163959c2cdaeSDavid S. Miller err = copied; 1640db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1641db8dac20SDavid S. Miller err = ulen; 1642db8dac20SDavid S. Miller 1643850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1644db8dac20SDavid S. Miller return err; 1645db8dac20SDavid S. Miller 1646db8dac20SDavid S. Miller csum_copy_err: 16472276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 16482276f58aSPaolo Abeni udp_skb_destructor)) { 16496aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 16506aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 16516a5dc9e5SEric Dumazet } 1652850cbaddSPaolo Abeni kfree_skb(skb); 1653db8dac20SDavid S. Miller 1654beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1655beb39db5SEric Dumazet cond_resched(); 16569cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1657db8dac20SDavid S. Miller goto try_again; 1658db8dac20SDavid S. Miller } 1659db8dac20SDavid S. Miller 1660286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 16611da177e4SLinus Torvalds { 16621da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 16631da177e4SLinus Torvalds /* 16641da177e4SLinus Torvalds * 1003.1g - break association. 16651da177e4SLinus Torvalds */ 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1668c720c7e8SEric Dumazet inet->inet_daddr = 0; 1669c720c7e8SEric Dumazet inet->inet_dport = 0; 1670bdeab991STom Herbert sock_rps_reset_rxhash(sk); 16711da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 16721da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 16731da177e4SLinus Torvalds inet_reset_saddr(sk); 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 16761da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1677c720c7e8SEric Dumazet inet->inet_sport = 0; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds sk_dst_reset(sk); 16801da177e4SLinus Torvalds return 0; 16811da177e4SLinus Torvalds } 1682286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1683286c72deSEric Dumazet 1684286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1685286c72deSEric Dumazet { 1686286c72deSEric Dumazet lock_sock(sk); 1687286c72deSEric Dumazet __udp_disconnect(sk, flags); 1688286c72deSEric Dumazet release_sock(sk); 1689286c72deSEric Dumazet return 0; 1690286c72deSEric Dumazet } 1691c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 16921da177e4SLinus Torvalds 1693645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 1694645ca708SEric Dumazet { 1695723b4610SEric Dumazet if (sk_hashed(sk)) { 1696645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1697512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 1698512615b6SEric Dumazet 1699512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1700d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 1701512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1702645ca708SEric Dumazet 1703c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 1704e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1705e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1706ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 1707fdcc8aa9SEric Dumazet hslot->count--; 1708c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 1709645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1710512615b6SEric Dumazet 1711512615b6SEric Dumazet spin_lock(&hslot2->lock); 1712ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1713512615b6SEric Dumazet hslot2->count--; 1714512615b6SEric Dumazet spin_unlock(&hslot2->lock); 1715645ca708SEric Dumazet } 1716c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 1717645ca708SEric Dumazet } 1718723b4610SEric Dumazet } 1719645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 1720645ca708SEric Dumazet 1721719f8358SEric Dumazet /* 1722719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 1723719f8358SEric Dumazet */ 1724719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 1725719f8358SEric Dumazet { 1726719f8358SEric Dumazet if (sk_hashed(sk)) { 1727719f8358SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1728719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 1729719f8358SEric Dumazet 1730719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1731719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 1732719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 1733e32ea7e7SCraig Gallek 1734e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 1735e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 1736719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1737719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 1738719f8358SEric Dumazet /* we must lock primary chain too */ 1739719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 1740e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1741e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1742719f8358SEric Dumazet 1743e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 1744719f8358SEric Dumazet spin_lock(&hslot2->lock); 1745ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1746719f8358SEric Dumazet hslot2->count--; 1747719f8358SEric Dumazet spin_unlock(&hslot2->lock); 1748719f8358SEric Dumazet 1749719f8358SEric Dumazet spin_lock(&nhslot2->lock); 1750ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 1751719f8358SEric Dumazet &nhslot2->head); 1752719f8358SEric Dumazet nhslot2->count++; 1753719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 1754e32ea7e7SCraig Gallek } 1755719f8358SEric Dumazet 1756719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 1757719f8358SEric Dumazet } 1758719f8358SEric Dumazet } 1759719f8358SEric Dumazet } 1760719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 1761719f8358SEric Dumazet 1762719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk) 1763719f8358SEric Dumazet { 1764719f8358SEric Dumazet u16 new_hash = udp4_portaddr_hash(sock_net(sk), 1765719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 1766719f8358SEric Dumazet inet_sk(sk)->inet_num); 1767719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 1768719f8358SEric Dumazet } 1769719f8358SEric Dumazet 1770a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 177193821778SHerbert Xu { 1772fec5e652STom Herbert int rc; 177393821778SHerbert Xu 1774005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 1775bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 1776005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 17772c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 1778e68b6e50SEric Dumazet } else { 1779e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 1780005ec974SShawn Bohrer } 1781fec5e652STom Herbert 1782850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 1783766e9037SEric Dumazet if (rc < 0) { 1784766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 1785766e9037SEric Dumazet 178693821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 1787766e9037SEric Dumazet if (rc == -ENOMEM) 1788e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 178993821778SHerbert Xu is_udplite); 1790e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1791766e9037SEric Dumazet kfree_skb(skb); 1792296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 1793766e9037SEric Dumazet return -1; 179493821778SHerbert Xu } 179593821778SHerbert Xu 179693821778SHerbert Xu return 0; 179793821778SHerbert Xu } 179893821778SHerbert Xu 1799447167bfSEric Dumazet static struct static_key udp_encap_needed __read_mostly; 1800447167bfSEric Dumazet void udp_encap_enable(void) 1801447167bfSEric Dumazet { 18027a34bcb8SPaolo Bonzini static_key_enable(&udp_encap_needed); 1803447167bfSEric Dumazet } 1804447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable); 1805447167bfSEric Dumazet 1806db8dac20SDavid S. Miller /* returns: 1807db8dac20SDavid S. Miller * -1: error 1808db8dac20SDavid S. Miller * 0: success 1809db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 1810db8dac20SDavid S. Miller * 1811db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 1812db8dac20SDavid S. Miller * have either been requeued or freed. 1813db8dac20SDavid S. Miller */ 1814a3f96c47SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1815db8dac20SDavid S. Miller { 1816db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1817db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1818db8dac20SDavid S. Miller 1819db8dac20SDavid S. Miller /* 1820db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 1821db8dac20SDavid S. Miller */ 1822db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 1823db8dac20SDavid S. Miller goto drop; 1824db8dac20SDavid S. Miller nf_reset(skb); 1825db8dac20SDavid S. Miller 1826447167bfSEric Dumazet if (static_key_false(&udp_encap_needed) && up->encap_type) { 18270ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 18280ad92ad0SEric Dumazet 1829db8dac20SDavid S. Miller /* 1830db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 1831db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 1832db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 1833db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 1834db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 1835db8dac20SDavid S. Miller * handler or was discarded by it. 1836db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 1837db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 1838db8dac20SDavid S. Miller */ 1839db8dac20SDavid S. Miller 1840db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 18416aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 1842e5aed006SHannes Frederic Sowa if (encap_rcv) { 1843db8dac20SDavid S. Miller int ret; 1844db8dac20SDavid S. Miller 18450a80966bSTom Herbert /* Verify checksum before giving to encap */ 18460a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 18470a80966bSTom Herbert goto csum_error; 18480a80966bSTom Herbert 18490ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 1850db8dac20SDavid S. Miller if (ret <= 0) { 185102c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 18520283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 1853db8dac20SDavid S. Miller is_udplite); 1854db8dac20SDavid S. Miller return -ret; 1855db8dac20SDavid S. Miller } 1856db8dac20SDavid S. Miller } 1857db8dac20SDavid S. Miller 1858db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1859db8dac20SDavid S. Miller } 1860db8dac20SDavid S. Miller 1861db8dac20SDavid S. Miller /* 1862db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1863db8dac20SDavid S. Miller */ 1864db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1865db8dac20SDavid S. Miller 1866db8dac20SDavid S. Miller /* 1867db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1868db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1869db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1870db8dac20SDavid S. Miller * protocol stack as such. 1871db8dac20SDavid S. Miller * 1872db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1873db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1874db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1875db8dac20SDavid S. Miller * provided by the application." 1876db8dac20SDavid S. Miller */ 1877db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1878ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 1879db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1880db8dac20SDavid S. Miller goto drop; 1881db8dac20SDavid S. Miller } 1882db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1883db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1884db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1885db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1886db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1887db8dac20SDavid S. Miller */ 1888db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1889ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 1890db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1891db8dac20SDavid S. Miller goto drop; 1892db8dac20SDavid S. Miller } 1893db8dac20SDavid S. Miller } 1894db8dac20SDavid S. Miller 1895dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 1896ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 1897ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 18986a5dc9e5SEric Dumazet goto csum_error; 1899ce25d66aSEric Dumazet 1900ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 1901a6127697SMichal Kubeček goto drop; 1902db8dac20SDavid S. Miller 1903e6afc8acSsamanthakumar udp_csum_pull_header(skb); 1904db8dac20SDavid S. Miller 1905fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 1906850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 1907db8dac20SDavid S. Miller 19086a5dc9e5SEric Dumazet csum_error: 190902c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 1910db8dac20SDavid S. Miller drop: 191102c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 19128edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 1913db8dac20SDavid S. Miller kfree_skb(skb); 1914db8dac20SDavid S. Miller return -1; 1915db8dac20SDavid S. Miller } 1916db8dac20SDavid S. Miller 191797502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 1918e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 191997502231SEric Dumazet */ 192064f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 1921421b3885SShawn Bohrer { 192297502231SEric Dumazet struct dst_entry *old; 1923421b3885SShawn Bohrer 1924d24406c8SWei Wang if (dst_hold_safe(dst)) { 1925e47eb5dfSEric Dumazet old = xchg(&sk->sk_rx_dst, dst); 192697502231SEric Dumazet dst_release(old); 192764f0f5d1SPaolo Abeni return old != dst; 192897502231SEric Dumazet } 192964f0f5d1SPaolo Abeni return false; 1930d24406c8SWei Wang } 1931c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 1932421b3885SShawn Bohrer 1933db8dac20SDavid S. Miller /* 1934db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1935db8dac20SDavid S. Miller * 19361240d137SEric Dumazet * Note: called only from the BH handler context. 1937db8dac20SDavid S. Miller */ 1938e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 1939db8dac20SDavid S. Miller struct udphdr *uh, 1940db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 194136cbb245SRick Jones struct udp_table *udptable, 194236cbb245SRick Jones int proto) 1943db8dac20SDavid S. Miller { 1944ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 19455cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 19465cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 19472dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 1948ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 1949ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 1950fb74c277SDavid Ahern int sdif = inet_sdif(skb); 1951ca065d0cSEric Dumazet struct hlist_node *node; 1952ca065d0cSEric Dumazet struct sk_buff *nskb; 19532dc41cffSDavid Held 19542dc41cffSDavid Held if (use_hash2) { 19552dc41cffSDavid Held hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 195673e2d5e3SPablo Neira udptable->mask; 195773e2d5e3SPablo Neira hash2 = udp4_portaddr_hash(net, daddr, hnum) & udptable->mask; 19582dc41cffSDavid Held start_lookup: 195973e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 19602dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 19612dc41cffSDavid Held } 1962db8dac20SDavid S. Miller 1963ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 1964ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 1965fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 1966ca065d0cSEric Dumazet continue; 19671240d137SEric Dumazet 1968ca065d0cSEric Dumazet if (!first) { 1969ca065d0cSEric Dumazet first = sk; 1970ca065d0cSEric Dumazet continue; 1971ca065d0cSEric Dumazet } 1972ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 1973ca065d0cSEric Dumazet 1974ca065d0cSEric Dumazet if (unlikely(!nskb)) { 1975ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 197602c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 1977ca065d0cSEric Dumazet IS_UDPLITE(sk)); 197802c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 1979ca065d0cSEric Dumazet IS_UDPLITE(sk)); 1980ca065d0cSEric Dumazet continue; 1981ca065d0cSEric Dumazet } 1982ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 1983ca065d0cSEric Dumazet consume_skb(nskb); 1984ca065d0cSEric Dumazet } 19851240d137SEric Dumazet 19862dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 19872dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 19882dc41cffSDavid Held hash2 = hash2_any; 19892dc41cffSDavid Held goto start_lookup; 19902dc41cffSDavid Held } 19912dc41cffSDavid Held 1992ca065d0cSEric Dumazet if (first) { 1993ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 1994ca065d0cSEric Dumazet consume_skb(skb); 19951240d137SEric Dumazet } else { 1996ca065d0cSEric Dumazet kfree_skb(skb); 199702c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 199836cbb245SRick Jones proto == IPPROTO_UDPLITE); 19991240d137SEric Dumazet } 2000db8dac20SDavid S. Miller return 0; 2001db8dac20SDavid S. Miller } 2002db8dac20SDavid S. Miller 2003db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2004db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2005db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 2006db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2007db8dac20SDavid S. Miller */ 2008db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2009db8dac20SDavid S. Miller int proto) 2010db8dac20SDavid S. Miller { 2011db8dac20SDavid S. Miller int err; 2012db8dac20SDavid S. Miller 2013db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2014db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2015db8dac20SDavid S. Miller 2016db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2017db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2018db8dac20SDavid S. Miller if (err) 2019db8dac20SDavid S. Miller return err; 2020db8dac20SDavid S. Miller } 2021db8dac20SDavid S. Miller 2022b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2023b46d9f62SHannes Frederic Sowa * force to int. 2024b46d9f62SHannes Frederic Sowa */ 2025b46d9f62SHannes Frederic Sowa return (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2026ed70fcfcSTom Herbert inet_compute_pseudo); 2027db8dac20SDavid S. Miller } 2028db8dac20SDavid S. Miller 2029db8dac20SDavid S. Miller /* 2030db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2031db8dac20SDavid S. Miller */ 2032db8dac20SDavid S. Miller 2033645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2034db8dac20SDavid S. Miller int proto) 2035db8dac20SDavid S. Miller { 2036db8dac20SDavid S. Miller struct sock *sk; 20377b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2038db8dac20SDavid S. Miller unsigned short ulen; 2039adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 20402783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 20410283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 2042db8dac20SDavid S. Miller 2043db8dac20SDavid S. Miller /* 2044db8dac20SDavid S. Miller * Validate the packet. 2045db8dac20SDavid S. Miller */ 2046db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2047db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2048db8dac20SDavid S. Miller 20497b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2050db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2051ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2052ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2053ccc2d97cSBjørn Mork 2054db8dac20SDavid S. Miller if (ulen > skb->len) 2055db8dac20SDavid S. Miller goto short_packet; 2056db8dac20SDavid S. Miller 2057db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2058db8dac20SDavid S. Miller /* UDP validates ulen. */ 2059db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2060db8dac20SDavid S. Miller goto short_packet; 2061db8dac20SDavid S. Miller uh = udp_hdr(skb); 2062db8dac20SDavid S. Miller } 2063db8dac20SDavid S. Miller 2064db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2065db8dac20SDavid S. Miller goto csum_error; 2066db8dac20SDavid S. Miller 20678afdd99aSEric Dumazet sk = skb_steal_sock(skb); 20688afdd99aSEric Dumazet if (sk) { 206997502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2070421b3885SShawn Bohrer int ret; 2071421b3885SShawn Bohrer 207297502231SEric Dumazet if (unlikely(sk->sk_rx_dst != dst)) 207397502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2074421b3885SShawn Bohrer 2075421b3885SShawn Bohrer ret = udp_queue_rcv_skb(sk, skb); 20768afdd99aSEric Dumazet sock_put(sk); 2077421b3885SShawn Bohrer /* a return value > 0 means to resubmit the input, but 2078421b3885SShawn Bohrer * it wants the return to be -protocol, or 0 2079421b3885SShawn Bohrer */ 2080421b3885SShawn Bohrer if (ret > 0) 2081421b3885SShawn Bohrer return -ret; 2082421b3885SShawn Bohrer return 0; 2083c18450a5SFabian Frederick } 2084c18450a5SFabian Frederick 2085db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2086e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 208736cbb245SRick Jones saddr, daddr, udptable, proto); 2088db8dac20SDavid S. Miller 2089607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 209000db4124SIan Morris if (sk) { 2091a5b50476SEliezer Tamir int ret; 2092a5b50476SEliezer Tamir 2093224d019cSTom Herbert if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 20942abb7cdcSTom Herbert skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, 20952abb7cdcSTom Herbert inet_compute_pseudo); 20962abb7cdcSTom Herbert 2097a5b50476SEliezer Tamir ret = udp_queue_rcv_skb(sk, skb); 2098db8dac20SDavid S. Miller 2099db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 2100db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 2101db8dac20SDavid S. Miller */ 2102db8dac20SDavid S. Miller if (ret > 0) 2103db8dac20SDavid S. Miller return -ret; 2104db8dac20SDavid S. Miller return 0; 2105db8dac20SDavid S. Miller } 2106db8dac20SDavid S. Miller 2107db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2108db8dac20SDavid S. Miller goto drop; 2109db8dac20SDavid S. Miller nf_reset(skb); 2110db8dac20SDavid S. Miller 2111db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2112db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2113db8dac20SDavid S. Miller goto csum_error; 2114db8dac20SDavid S. Miller 211502c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2116db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2117db8dac20SDavid S. Miller 2118db8dac20SDavid S. Miller /* 2119db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2120db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2121db8dac20SDavid S. Miller */ 2122db8dac20SDavid S. Miller kfree_skb(skb); 2123db8dac20SDavid S. Miller return 0; 2124db8dac20SDavid S. Miller 2125db8dac20SDavid S. Miller short_packet: 2126ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2127afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2128afd46503SJoe Perches &saddr, ntohs(uh->source), 2129afd46503SJoe Perches ulen, skb->len, 2130afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2131db8dac20SDavid S. Miller goto drop; 2132db8dac20SDavid S. Miller 2133db8dac20SDavid S. Miller csum_error: 2134db8dac20SDavid S. Miller /* 2135db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2136db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2137db8dac20SDavid S. Miller */ 2138ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2139afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2140afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2141db8dac20SDavid S. Miller ulen); 214202c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2143db8dac20SDavid S. Miller drop: 214402c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 2145db8dac20SDavid S. Miller kfree_skb(skb); 2146db8dac20SDavid S. Miller return 0; 2147db8dac20SDavid S. Miller } 2148db8dac20SDavid S. Miller 2149421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2150421b3885SShawn Bohrer * If more than one socket found returns NULL 2151421b3885SShawn Bohrer */ 2152421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2153421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2154421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2155fb74c277SDavid Ahern int dif, int sdif) 2156421b3885SShawn Bohrer { 2157421b3885SShawn Bohrer struct sock *sk, *result; 2158421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2159ca065d0cSEric Dumazet unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); 2160421b3885SShawn Bohrer struct udp_hslot *hslot = &udp_table.hash[slot]; 2161421b3885SShawn Bohrer 216263c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 216363c6f81cSEric Dumazet if (hslot->count > 10) 216463c6f81cSEric Dumazet return NULL; 216563c6f81cSEric Dumazet 2166421b3885SShawn Bohrer result = NULL; 2167ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2168ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2169fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2170ca065d0cSEric Dumazet if (result) 2171ca065d0cSEric Dumazet return NULL; 2172421b3885SShawn Bohrer result = sk; 2173421b3885SShawn Bohrer } 2174421b3885SShawn Bohrer } 2175421b3885SShawn Bohrer 2176421b3885SShawn Bohrer return result; 2177421b3885SShawn Bohrer } 2178421b3885SShawn Bohrer 2179421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2180421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2181421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2182421b3885SShawn Bohrer */ 2183421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2184421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2185421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 21863fa6f616SDavid Ahern int dif, int sdif) 2187421b3885SShawn Bohrer { 2188421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2189421b3885SShawn Bohrer unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum); 2190421b3885SShawn Bohrer unsigned int slot2 = hash2 & udp_table.mask; 2191421b3885SShawn Bohrer struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 2192c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2193421b3885SShawn Bohrer const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 2194ca065d0cSEric Dumazet struct sock *sk; 2195421b3885SShawn Bohrer 2196ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2197ca065d0cSEric Dumazet if (INET_MATCH(sk, net, acookie, rmt_addr, 21983fa6f616SDavid Ahern loc_addr, ports, dif, sdif)) 2199ca065d0cSEric Dumazet return sk; 2200421b3885SShawn Bohrer /* Only check first socket in chain */ 2201421b3885SShawn Bohrer break; 2202421b3885SShawn Bohrer } 2203ca065d0cSEric Dumazet return NULL; 2204421b3885SShawn Bohrer } 2205421b3885SShawn Bohrer 22067487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2207421b3885SShawn Bohrer { 2208610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2209bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2210610438b7SEric Dumazet const struct iphdr *iph; 2211610438b7SEric Dumazet const struct udphdr *uh; 2212ca065d0cSEric Dumazet struct sock *sk = NULL; 2213421b3885SShawn Bohrer struct dst_entry *dst; 2214421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2215fb74c277SDavid Ahern int sdif = inet_sdif(skb); 22166e540309SShawn Bohrer int ours; 2217421b3885SShawn Bohrer 2218421b3885SShawn Bohrer /* validate the packet */ 2219421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 22207487449cSPaolo Abeni return 0; 2221421b3885SShawn Bohrer 2222610438b7SEric Dumazet iph = ip_hdr(skb); 2223610438b7SEric Dumazet uh = udp_hdr(skb); 2224610438b7SEric Dumazet 2225996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2226bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 22276e540309SShawn Bohrer 22286e540309SShawn Bohrer if (!in_dev) 22297487449cSPaolo Abeni return 0; 22306e540309SShawn Bohrer 22316e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 22326e540309SShawn Bohrer iph->protocol); 22336e540309SShawn Bohrer if (!ours) 22347487449cSPaolo Abeni return 0; 2235ad0ea198SPaolo Abeni 2236421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2237fb74c277SDavid Ahern uh->source, iph->saddr, 2238fb74c277SDavid Ahern dif, sdif); 22396e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2240421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 22413fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 22426e540309SShawn Bohrer } 2243421b3885SShawn Bohrer 224441c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 22457487449cSPaolo Abeni return 0; 2246421b3885SShawn Bohrer 2247421b3885SShawn Bohrer skb->sk = sk; 224882eabd9eSAlexander Duyck skb->destructor = sock_efree; 224910e2eb87SEric Dumazet dst = READ_ONCE(sk->sk_rx_dst); 2250421b3885SShawn Bohrer 2251421b3885SShawn Bohrer if (dst) 2252421b3885SShawn Bohrer dst = dst_check(dst, 0); 225310e2eb87SEric Dumazet if (dst) { 2254bc044e8dSPaolo Abeni u32 itag = 0; 2255bc044e8dSPaolo Abeni 2256d24406c8SWei Wang /* set noref for now. 2257d24406c8SWei Wang * any place which wants to hold dst has to call 2258d24406c8SWei Wang * dst_hold_safe() 2259d24406c8SWei Wang */ 2260421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2261bc044e8dSPaolo Abeni 2262bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2263bc044e8dSPaolo Abeni * the source on each packet 2264bc044e8dSPaolo Abeni */ 2265bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2266bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 2267bc044e8dSPaolo Abeni iph->saddr, iph->tos, 2268bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2269421b3885SShawn Bohrer } 22707487449cSPaolo Abeni return 0; 227110e2eb87SEric Dumazet } 2272421b3885SShawn Bohrer 2273db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2274db8dac20SDavid S. Miller { 2275645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2276db8dac20SDavid S. Miller } 2277db8dac20SDavid S. Miller 22787d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2279db8dac20SDavid S. Miller { 228044046a59STom Parkin struct udp_sock *up = udp_sk(sk); 22818a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2282db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 22838a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 228444046a59STom Parkin if (static_key_false(&udp_encap_needed) && up->encap_type) { 228544046a59STom Parkin void (*encap_destroy)(struct sock *sk); 22866aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 228744046a59STom Parkin if (encap_destroy) 228844046a59STom Parkin encap_destroy(sk); 228944046a59STom Parkin } 2290db8dac20SDavid S. Miller } 2291db8dac20SDavid S. Miller 22921da177e4SLinus Torvalds /* 22931da177e4SLinus Torvalds * Socket option code for UDP 22941da177e4SLinus Torvalds */ 22954c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 2296b7058842SDavid S. Miller char __user *optval, unsigned int optlen, 22974c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 23001c19448cSTom Herbert int val, valbool; 23011da177e4SLinus Torvalds int err = 0; 2302b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds if (optlen < sizeof(int)) 23051da177e4SLinus Torvalds return -EINVAL; 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 23081da177e4SLinus Torvalds return -EFAULT; 23091da177e4SLinus Torvalds 23101c19448cSTom Herbert valbool = val ? 1 : 0; 23111c19448cSTom Herbert 23121da177e4SLinus Torvalds switch (optname) { 23131da177e4SLinus Torvalds case UDP_CORK: 23141da177e4SLinus Torvalds if (val != 0) { 23151da177e4SLinus Torvalds up->corkflag = 1; 23161da177e4SLinus Torvalds } else { 23171da177e4SLinus Torvalds up->corkflag = 0; 23181da177e4SLinus Torvalds lock_sock(sk); 23194243cdc2SJoe Perches push_pending_frames(sk); 23201da177e4SLinus Torvalds release_sock(sk); 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds break; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds case UDP_ENCAP: 23251da177e4SLinus Torvalds switch (val) { 23261da177e4SLinus Torvalds case 0: 23271da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 23281da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 2329067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 2330067b207bSJames Chapman /* FALLTHROUGH */ 2331342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 23321da177e4SLinus Torvalds up->encap_type = val; 2333447167bfSEric Dumazet udp_encap_enable(); 23341da177e4SLinus Torvalds break; 23351da177e4SLinus Torvalds default: 23361da177e4SLinus Torvalds err = -ENOPROTOOPT; 23371da177e4SLinus Torvalds break; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds break; 23401da177e4SLinus Torvalds 23411c19448cSTom Herbert case UDP_NO_CHECK6_TX: 23421c19448cSTom Herbert up->no_check6_tx = valbool; 23431c19448cSTom Herbert break; 23441c19448cSTom Herbert 23451c19448cSTom Herbert case UDP_NO_CHECK6_RX: 23461c19448cSTom Herbert up->no_check6_rx = valbool; 23471c19448cSTom Herbert break; 23481c19448cSTom Herbert 2349ba4e58ecSGerrit Renker /* 2350ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2351ba4e58ecSGerrit Renker */ 2352ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2353ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2354ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2355b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2356ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2357ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2358ba4e58ecSGerrit Renker val = 8; 23594be929beSAlexey Dobriyan else if (val > USHRT_MAX) 23604be929beSAlexey Dobriyan val = USHRT_MAX; 2361ba4e58ecSGerrit Renker up->pcslen = val; 2362ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 2363ba4e58ecSGerrit Renker break; 2364ba4e58ecSGerrit Renker 2365ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2366ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2367ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2368ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2369b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2370ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2371ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2372ba4e58ecSGerrit Renker val = 8; 23734be929beSAlexey Dobriyan else if (val > USHRT_MAX) 23744be929beSAlexey Dobriyan val = USHRT_MAX; 2375ba4e58ecSGerrit Renker up->pcrlen = val; 2376ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 2377ba4e58ecSGerrit Renker break; 2378ba4e58ecSGerrit Renker 23791da177e4SLinus Torvalds default: 23801da177e4SLinus Torvalds err = -ENOPROTOOPT; 23811da177e4SLinus Torvalds break; 23826516c655SStephen Hemminger } 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds return err; 23851da177e4SLinus Torvalds } 2386c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 23871da177e4SLinus Torvalds 2388db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 2389b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 2390db8dac20SDavid S. Miller { 2391db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2392db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2393db8dac20SDavid S. Miller udp_push_pending_frames); 2394db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2395db8dac20SDavid S. Miller } 2396db8dac20SDavid S. Miller 2397db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2398db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 2399b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 2400db8dac20SDavid S. Miller { 2401db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2402db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2403db8dac20SDavid S. Miller udp_push_pending_frames); 2404db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 2405db8dac20SDavid S. Miller } 2406db8dac20SDavid S. Miller #endif 2407db8dac20SDavid S. Miller 24084c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 24091da177e4SLinus Torvalds char __user *optval, int __user *optlen) 24101da177e4SLinus Torvalds { 24111da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 24121da177e4SLinus Torvalds int val, len; 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds if (get_user(len, optlen)) 24151da177e4SLinus Torvalds return -EFAULT; 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds if (len < 0) 24201da177e4SLinus Torvalds return -EINVAL; 24211da177e4SLinus Torvalds 24221da177e4SLinus Torvalds switch (optname) { 24231da177e4SLinus Torvalds case UDP_CORK: 24241da177e4SLinus Torvalds val = up->corkflag; 24251da177e4SLinus Torvalds break; 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds case UDP_ENCAP: 24281da177e4SLinus Torvalds val = up->encap_type; 24291da177e4SLinus Torvalds break; 24301da177e4SLinus Torvalds 24311c19448cSTom Herbert case UDP_NO_CHECK6_TX: 24321c19448cSTom Herbert val = up->no_check6_tx; 24331c19448cSTom Herbert break; 24341c19448cSTom Herbert 24351c19448cSTom Herbert case UDP_NO_CHECK6_RX: 24361c19448cSTom Herbert val = up->no_check6_rx; 24371c19448cSTom Herbert break; 24381c19448cSTom Herbert 2439ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2440ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2441ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2442ba4e58ecSGerrit Renker val = up->pcslen; 2443ba4e58ecSGerrit Renker break; 2444ba4e58ecSGerrit Renker 2445ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2446ba4e58ecSGerrit Renker val = up->pcrlen; 2447ba4e58ecSGerrit Renker break; 2448ba4e58ecSGerrit Renker 24491da177e4SLinus Torvalds default: 24501da177e4SLinus Torvalds return -ENOPROTOOPT; 24516516c655SStephen Hemminger } 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds if (put_user(len, optlen)) 24541da177e4SLinus Torvalds return -EFAULT; 24551da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 24561da177e4SLinus Torvalds return -EFAULT; 24571da177e4SLinus Torvalds return 0; 24581da177e4SLinus Torvalds } 2459c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 24601da177e4SLinus Torvalds 2461db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2462db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2463db8dac20SDavid S. Miller { 2464db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2465db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2466db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2467db8dac20SDavid S. Miller } 2468db8dac20SDavid S. Miller 2469db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2470db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 2471db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2472db8dac20SDavid S. Miller { 2473db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2474db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2475db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 2476db8dac20SDavid S. Miller } 2477db8dac20SDavid S. Miller #endif 24781da177e4SLinus Torvalds /** 24791da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 24801da177e4SLinus Torvalds * @file - file struct 24811da177e4SLinus Torvalds * @sock - socket 24821da177e4SLinus Torvalds * @wait - poll table 24831da177e4SLinus Torvalds * 24841da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 24851da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 24861da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 24871da177e4SLinus Torvalds * then it could get return from select indicating data available 24881da177e4SLinus Torvalds * but then block when reading it. Add special case code 24891da177e4SLinus Torvalds * to work around these arguably broken applications. 24901da177e4SLinus Torvalds */ 24911da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 24921da177e4SLinus Torvalds { 24931da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 24941da177e4SLinus Torvalds struct sock *sk = sock->sk; 24951da177e4SLinus Torvalds 24962276f58aSPaolo Abeni if (!skb_queue_empty(&udp_sk(sk)->reader_queue)) 24972276f58aSPaolo Abeni mask |= POLLIN | POLLRDNORM; 24982276f58aSPaolo Abeni 2499c3f1dbafSDavid Majnemer sock_rps_record_flow(sk); 2500c3f1dbafSDavid Majnemer 25011da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 250285584672SEric Dumazet if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2503e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 25041da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds return mask; 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds } 2509c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll); 25101da177e4SLinus Torvalds 25115d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 25125d77dca8SDavid Ahern { 25135d77dca8SDavid Ahern lock_sock(sk); 25145d77dca8SDavid Ahern 25155d77dca8SDavid Ahern sk->sk_err = err; 25165d77dca8SDavid Ahern sk->sk_error_report(sk); 2517286c72deSEric Dumazet __udp_disconnect(sk, 0); 25185d77dca8SDavid Ahern 25195d77dca8SDavid Ahern release_sock(sk); 25205d77dca8SDavid Ahern 25215d77dca8SDavid Ahern return 0; 25225d77dca8SDavid Ahern } 25235d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 25245d77dca8SDavid Ahern 2525db8dac20SDavid S. Miller struct proto udp_prot = { 2526db8dac20SDavid S. Miller .name = "UDP", 2527db8dac20SDavid S. Miller .owner = THIS_MODULE, 2528db8dac20SDavid S. Miller .close = udp_lib_close, 2529db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2530db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2531db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2532850cbaddSPaolo Abeni .init = udp_init_sock, 2533db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2534db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2535db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2536db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2537db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 2538db8dac20SDavid S. Miller .sendpage = udp_sendpage, 25398141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2540db8dac20SDavid S. Miller .hash = udp_lib_hash, 2541db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2542719f8358SEric Dumazet .rehash = udp_v4_rehash, 2543db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 2544db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 2545db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 2546db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 2547db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 2548db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 2549645ca708SEric Dumazet .h.udp_table = &udp_table, 2550db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2551db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 2552db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 2553db8dac20SDavid S. Miller #endif 25545d77dca8SDavid Ahern .diag_destroy = udp_abort, 2555db8dac20SDavid S. Miller }; 2556c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 25591da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 25601da177e4SLinus Torvalds 2561645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds struct sock *sk; 25641da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 25656f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 25661da177e4SLinus Torvalds 2567f86dcc5aSEric Dumazet for (state->bucket = start; state->bucket <= state->udp_table->mask; 2568f86dcc5aSEric Dumazet ++state->bucket) { 2569645ca708SEric Dumazet struct udp_hslot *hslot = &state->udp_table->hash[state->bucket]; 2570f86dcc5aSEric Dumazet 2571ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2572f86dcc5aSEric Dumazet continue; 2573f86dcc5aSEric Dumazet 2574645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 2575ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 2576878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 2577a91275efSDaniel Lezcano continue; 25781da177e4SLinus Torvalds if (sk->sk_family == state->family) 25791da177e4SLinus Torvalds goto found; 25801da177e4SLinus Torvalds } 2581645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds sk = NULL; 25841da177e4SLinus Torvalds found: 25851da177e4SLinus Torvalds return sk; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 25891da177e4SLinus Torvalds { 25901da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 25916f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds do { 2594ca065d0cSEric Dumazet sk = sk_next(sk); 2595878628fbSYOSHIFUJI Hideaki } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 25961da177e4SLinus Torvalds 2597645ca708SEric Dumazet if (!sk) { 2598f86dcc5aSEric Dumazet if (state->bucket <= state->udp_table->mask) 2599f52b5054SEric Dumazet spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 2600645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 26011da177e4SLinus Torvalds } 26021da177e4SLinus Torvalds return sk; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 26061da177e4SLinus Torvalds { 2607645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds if (sk) 26101da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 26111da177e4SLinus Torvalds --pos; 26121da177e4SLinus Torvalds return pos ? NULL : sk; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 26161da177e4SLinus Torvalds { 261730842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 2618f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 261930842f29SVitaly Mayatskikh 2620b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26241da177e4SLinus Torvalds { 26251da177e4SLinus Torvalds struct sock *sk; 26261da177e4SLinus Torvalds 2627b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 26281da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 26291da177e4SLinus Torvalds else 26301da177e4SLinus Torvalds sk = udp_get_next(seq, v); 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds ++*pos; 26331da177e4SLinus Torvalds return sk; 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 26371da177e4SLinus Torvalds { 2638645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 2639645ca708SEric Dumazet 2640f86dcc5aSEric Dumazet if (state->bucket <= state->udp_table->mask) 2641645ca708SEric Dumazet spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds 264473cb88ecSArjan van de Ven int udp_seq_open(struct inode *inode, struct file *file) 26451da177e4SLinus Torvalds { 2646d9dda78bSAl Viro struct udp_seq_afinfo *afinfo = PDE_DATA(inode); 2647a2be75c1SDenis V. Lunev struct udp_iter_state *s; 2648a2be75c1SDenis V. Lunev int err; 26491da177e4SLinus Torvalds 2650a2be75c1SDenis V. Lunev err = seq_open_net(inode, file, &afinfo->seq_ops, 2651a2be75c1SDenis V. Lunev sizeof(struct udp_iter_state)); 2652a2be75c1SDenis V. Lunev if (err < 0) 2653a2be75c1SDenis V. Lunev return err; 2654a91275efSDaniel Lezcano 2655a2be75c1SDenis V. Lunev s = ((struct seq_file *)file->private_data)->private; 26561da177e4SLinus Torvalds s->family = afinfo->family; 2657645ca708SEric Dumazet s->udp_table = afinfo->udp_table; 2658a2be75c1SDenis V. Lunev return err; 2659a91275efSDaniel Lezcano } 266073cb88ecSArjan van de Ven EXPORT_SYMBOL(udp_seq_open); 2661a91275efSDaniel Lezcano 26621da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 26630c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds struct proc_dir_entry *p; 26661da177e4SLinus Torvalds int rc = 0; 26671da177e4SLinus Torvalds 2668dda61925SDenis V. Lunev afinfo->seq_ops.start = udp_seq_start; 2669dda61925SDenis V. Lunev afinfo->seq_ops.next = udp_seq_next; 2670dda61925SDenis V. Lunev afinfo->seq_ops.stop = udp_seq_stop; 2671dda61925SDenis V. Lunev 267284841c3cSDenis V. Lunev p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, 267373cb88ecSArjan van de Ven afinfo->seq_fops, afinfo); 267484841c3cSDenis V. Lunev if (!p) 26751da177e4SLinus Torvalds rc = -ENOMEM; 26761da177e4SLinus Torvalds return rc; 26771da177e4SLinus Torvalds } 2678c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register); 26791da177e4SLinus Torvalds 26800c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 26811da177e4SLinus Torvalds { 2682ece31ffdSGao feng remove_proc_entry(afinfo->name, net->proc_net); 26831da177e4SLinus Torvalds } 2684c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister); 2685db8dac20SDavid S. Miller 2686db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 26875e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2688652586dfSTetsuo Handa int bucket) 2689db8dac20SDavid S. Miller { 2690db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 2691c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 2692c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 2693c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 2694c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 2695db8dac20SDavid S. Miller 2696f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2697652586dfSTetsuo Handa " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d", 2698db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 269931e6d363SEric Dumazet sk_wmem_alloc_get(sp), 270031e6d363SEric Dumazet sk_rmem_alloc_get(sp), 2701a7cb5a49SEric W. Biederman 0, 0L, 0, 2702a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 2703a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 270441c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 2705652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 2706db8dac20SDavid S. Miller } 2707db8dac20SDavid S. Miller 2708db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 2709db8dac20SDavid S. Miller { 2710652586dfSTetsuo Handa seq_setwidth(seq, 127); 2711db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 2712652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 2713db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 2714cb61cb9bSEric Dumazet "inode ref pointer drops"); 2715db8dac20SDavid S. Miller else { 2716db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 2717db8dac20SDavid S. Miller 2718652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 2719db8dac20SDavid S. Miller } 2720652586dfSTetsuo Handa seq_pad(seq, '\n'); 2721db8dac20SDavid S. Miller return 0; 2722db8dac20SDavid S. Miller } 2723db8dac20SDavid S. Miller 272473cb88ecSArjan van de Ven static const struct file_operations udp_afinfo_seq_fops = { 272573cb88ecSArjan van de Ven .owner = THIS_MODULE, 272673cb88ecSArjan van de Ven .open = udp_seq_open, 272773cb88ecSArjan van de Ven .read = seq_read, 272873cb88ecSArjan van de Ven .llseek = seq_lseek, 272973cb88ecSArjan van de Ven .release = seq_release_net 273073cb88ecSArjan van de Ven }; 273173cb88ecSArjan van de Ven 2732db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 2733db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 2734db8dac20SDavid S. Miller .name = "udp", 2735db8dac20SDavid S. Miller .family = AF_INET, 2736645ca708SEric Dumazet .udp_table = &udp_table, 273773cb88ecSArjan van de Ven .seq_fops = &udp_afinfo_seq_fops, 2738dda61925SDenis V. Lunev .seq_ops = { 2739dda61925SDenis V. Lunev .show = udp4_seq_show, 2740dda61925SDenis V. Lunev }, 2741db8dac20SDavid S. Miller }; 2742db8dac20SDavid S. Miller 27432c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 274415439febSPavel Emelyanov { 274515439febSPavel Emelyanov return udp_proc_register(net, &udp4_seq_afinfo); 274615439febSPavel Emelyanov } 274715439febSPavel Emelyanov 27482c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 274915439febSPavel Emelyanov { 275015439febSPavel Emelyanov udp_proc_unregister(net, &udp4_seq_afinfo); 275115439febSPavel Emelyanov } 275215439febSPavel Emelyanov 275315439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 275415439febSPavel Emelyanov .init = udp4_proc_init_net, 275515439febSPavel Emelyanov .exit = udp4_proc_exit_net, 275615439febSPavel Emelyanov }; 275715439febSPavel Emelyanov 2758db8dac20SDavid S. Miller int __init udp4_proc_init(void) 2759db8dac20SDavid S. Miller { 276015439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 2761db8dac20SDavid S. Miller } 2762db8dac20SDavid S. Miller 2763db8dac20SDavid S. Miller void udp4_proc_exit(void) 2764db8dac20SDavid S. Miller { 276515439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 2766db8dac20SDavid S. Miller } 27671da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27681da177e4SLinus Torvalds 2769f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 2770f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 2771645ca708SEric Dumazet { 2772413c27d8SEldad Zack ssize_t ret; 2773413c27d8SEldad Zack 2774f86dcc5aSEric Dumazet if (!str) 2775f86dcc5aSEric Dumazet return 0; 2776413c27d8SEldad Zack 2777413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 2778413c27d8SEldad Zack if (ret) 2779413c27d8SEldad Zack return 0; 2780413c27d8SEldad Zack 2781f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 2782f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 2783f86dcc5aSEric Dumazet return 1; 2784f86dcc5aSEric Dumazet } 2785f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 2786645ca708SEric Dumazet 2787f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 2788f86dcc5aSEric Dumazet { 2789f86dcc5aSEric Dumazet unsigned int i; 2790f86dcc5aSEric Dumazet 2791f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 2792512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 2793f86dcc5aSEric Dumazet uhash_entries, 2794f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 2795f86dcc5aSEric Dumazet 0, 2796f86dcc5aSEric Dumazet &table->log, 2797f86dcc5aSEric Dumazet &table->mask, 279831fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 2799f86dcc5aSEric Dumazet 64 * 1024); 280031fe62b9STim Bird 2801512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 2802f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 2803ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 2804fdcc8aa9SEric Dumazet table->hash[i].count = 0; 2805645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 2806645ca708SEric Dumazet } 2807512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 2808ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 2809512615b6SEric Dumazet table->hash2[i].count = 0; 2810512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 2811512615b6SEric Dumazet } 2812645ca708SEric Dumazet } 2813645ca708SEric Dumazet 2814723b8e46STom Herbert u32 udp_flow_hashrnd(void) 2815723b8e46STom Herbert { 2816723b8e46STom Herbert static u32 hashrnd __read_mostly; 2817723b8e46STom Herbert 2818723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 2819723b8e46STom Herbert 2820723b8e46STom Herbert return hashrnd; 2821723b8e46STom Herbert } 2822723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 2823723b8e46STom Herbert 282495766fffSHideo Aoki void __init udp_init(void) 282595766fffSHideo Aoki { 2826f03d78dbSEric Dumazet unsigned long limit; 28274b272750SEric Dumazet unsigned int i; 282895766fffSHideo Aoki 2829f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 2830f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 283195766fffSHideo Aoki limit = max(limit, 128UL); 283295766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 283395766fffSHideo Aoki sysctl_udp_mem[1] = limit; 283495766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 283595766fffSHideo Aoki 283695766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 283795766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 28384b272750SEric Dumazet 28394b272750SEric Dumazet /* 16 spinlocks per cpu */ 28404b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 28414b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 28424b272750SEric Dumazet GFP_KERNEL); 28434b272750SEric Dumazet if (!udp_busylocks) 28444b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 28454b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 28464b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 284795766fffSHideo Aoki } 2848