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 1258d987e5cSEric Dumazet atomic_long_t udp_memory_allocated; 12695766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 12795766fffSHideo Aoki 128f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536 129f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 13098322f22SEric Dumazet 13163a6fff3SRobert Shearman /* IPCB reference means this can not be used from early demux */ 13263a6fff3SRobert Shearman static bool udp_lib_exact_dif_match(struct net *net, struct sk_buff *skb) 13363a6fff3SRobert Shearman { 13463a6fff3SRobert Shearman #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 13563a6fff3SRobert Shearman if (!net->ipv4.sysctl_udp_l3mdev_accept && 13663a6fff3SRobert Shearman skb && ipv4_l3mdev_skb(IPCB(skb)->flags)) 13763a6fff3SRobert Shearman return true; 13863a6fff3SRobert Shearman #endif 13963a6fff3SRobert Shearman return false; 14063a6fff3SRobert Shearman } 14163a6fff3SRobert Shearman 142f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 143645ca708SEric Dumazet const struct udp_hslot *hslot, 14498322f22SEric Dumazet unsigned long *bitmap, 145fe38d2a1SJosef Bacik struct sock *sk, unsigned int log) 14625030a7fSGerrit Renker { 147f24d43c0SEric Dumazet struct sock *sk2; 148ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 14925030a7fSGerrit Renker 150ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 151f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 152f24d43c0SEric Dumazet sk2 != sk && 153d4cada4aSEric Dumazet (bitmap || udp_sk(sk2)->udp_port_hash == num) && 154f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1559d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1569d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 157fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 158df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 159df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 160df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 161df560056SEric Garver if (!bitmap) 162df560056SEric Garver return 0; 163df560056SEric Garver } else { 1644243cdc2SJoe Perches if (!bitmap) 165fc038410SDavid S. Miller return 1; 166df560056SEric Garver __set_bit(udp_sk(sk2)->udp_port_hash >> log, 167df560056SEric Garver bitmap); 168df560056SEric Garver } 1694243cdc2SJoe Perches } 17098322f22SEric Dumazet } 17125030a7fSGerrit Renker return 0; 17225030a7fSGerrit Renker } 17325030a7fSGerrit Renker 17430fff923SEric Dumazet /* 17530fff923SEric Dumazet * Note: we still hold spinlock of primary hash chain, so no other writer 17630fff923SEric Dumazet * can insert/delete a socket with local_port == num 17730fff923SEric Dumazet */ 17830fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num, 17930fff923SEric Dumazet struct udp_hslot *hslot2, 180fe38d2a1SJosef Bacik struct sock *sk) 18130fff923SEric Dumazet { 18230fff923SEric Dumazet struct sock *sk2; 183ba418fa3STom Herbert kuid_t uid = sock_i_uid(sk); 18430fff923SEric Dumazet int res = 0; 18530fff923SEric Dumazet 18630fff923SEric Dumazet spin_lock(&hslot2->lock); 187ca065d0cSEric Dumazet udp_portaddr_for_each_entry(sk2, &hslot2->head) { 18830fff923SEric Dumazet if (net_eq(sock_net(sk2), net) && 18930fff923SEric Dumazet sk2 != sk && 19030fff923SEric Dumazet (udp_sk(sk2)->udp_port_hash == num) && 19130fff923SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 1929d4fb27dSJoe Perches (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 1939d4fb27dSJoe Perches sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 194fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, true)) { 195df560056SEric Garver if (sk2->sk_reuseport && sk->sk_reuseport && 196df560056SEric Garver !rcu_access_pointer(sk->sk_reuseport_cb) && 197df560056SEric Garver uid_eq(uid, sock_i_uid(sk2))) { 198df560056SEric Garver res = 0; 199df560056SEric Garver } else { 20030fff923SEric Dumazet res = 1; 201df560056SEric Garver } 20230fff923SEric Dumazet break; 20330fff923SEric Dumazet } 2044243cdc2SJoe Perches } 20530fff923SEric Dumazet spin_unlock(&hslot2->lock); 20630fff923SEric Dumazet return res; 20730fff923SEric Dumazet } 20830fff923SEric Dumazet 209fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) 210e32ea7e7SCraig Gallek { 211e32ea7e7SCraig Gallek struct net *net = sock_net(sk); 212e32ea7e7SCraig Gallek kuid_t uid = sock_i_uid(sk); 213e32ea7e7SCraig Gallek struct sock *sk2; 214e32ea7e7SCraig Gallek 215ca065d0cSEric Dumazet sk_for_each(sk2, &hslot->head) { 216e32ea7e7SCraig Gallek if (net_eq(sock_net(sk2), net) && 217e32ea7e7SCraig Gallek sk2 != sk && 218e32ea7e7SCraig Gallek sk2->sk_family == sk->sk_family && 219e32ea7e7SCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 220e32ea7e7SCraig Gallek (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && 221e32ea7e7SCraig Gallek (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 222e32ea7e7SCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 223fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) { 2242dbb9b9eSMartin KaFai Lau return reuseport_add_sock(sk, sk2, 2252dbb9b9eSMartin KaFai Lau inet_rcv_saddr_any(sk)); 226e32ea7e7SCraig Gallek } 227e32ea7e7SCraig Gallek } 228e32ea7e7SCraig Gallek 2292dbb9b9eSMartin KaFai Lau return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 230e32ea7e7SCraig Gallek } 231e32ea7e7SCraig Gallek 23225030a7fSGerrit Renker /** 2336ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 23425030a7fSGerrit Renker * 23525030a7fSGerrit Renker * @sk: socket struct in question 23625030a7fSGerrit Renker * @snum: port number to look up 23725985edcSLucas De Marchi * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 23830fff923SEric Dumazet * with NULL address 23925030a7fSGerrit Renker */ 2406ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 24130fff923SEric Dumazet unsigned int hash2_nulladdr) 2421da177e4SLinus Torvalds { 243512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 244645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 24525030a7fSGerrit Renker int error = 1; 2463b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 2471da177e4SLinus Torvalds 24832c1da70SStephen Hemminger if (!snum) { 2499088c560SEric Dumazet int low, high, remaining; 25095c96174SEric Dumazet unsigned int rand; 25198322f22SEric Dumazet unsigned short first, last; 25298322f22SEric Dumazet DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 2531da177e4SLinus Torvalds 2540bbf87d8SEric W. Biederman inet_get_local_port_range(net, &low, &high); 255a25de534SAnton Arapov remaining = (high - low) + 1; 256227b60f5SStephen Hemminger 25763862b5bSAruna-Hewapathirane rand = prandom_u32(); 2588fc54f68SDaniel Borkmann first = reciprocal_scale(rand, remaining) + low; 25998322f22SEric Dumazet /* 26098322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 26198322f22SEric Dumazet */ 262f86dcc5aSEric Dumazet rand = (rand | 1) * (udptable->mask + 1); 2635781b235SEric Dumazet last = first + udptable->mask + 1; 2645781b235SEric Dumazet do { 265f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, first); 26698322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 267645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 26898322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 269fe38d2a1SJosef Bacik udptable->log); 27098322f22SEric Dumazet 27198322f22SEric Dumazet snum = first; 27298322f22SEric Dumazet /* 27398322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 27498322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 27598322f22SEric Dumazet * give us randomization and full range coverage. 27698322f22SEric Dumazet */ 2779088c560SEric Dumazet do { 27898322f22SEric Dumazet if (low <= snum && snum <= high && 279e3826f1eSAmerigo Wang !test_bit(snum >> udptable->log, bitmap) && 280122ff243SWANG Cong !inet_is_local_reserved_port(net, snum)) 28198322f22SEric Dumazet goto found; 28298322f22SEric Dumazet snum += rand; 28398322f22SEric Dumazet } while (snum != first); 28498322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 285df560056SEric Garver cond_resched(); 2865781b235SEric Dumazet } while (++first != last); 28798322f22SEric Dumazet goto fail; 288645ca708SEric Dumazet } else { 289f86dcc5aSEric Dumazet hslot = udp_hashslot(udptable, net, snum); 290645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 29130fff923SEric Dumazet if (hslot->count > 10) { 29230fff923SEric Dumazet int exist; 29330fff923SEric Dumazet unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 29430fff923SEric Dumazet 29530fff923SEric Dumazet slot2 &= udptable->mask; 29630fff923SEric Dumazet hash2_nulladdr &= udptable->mask; 29730fff923SEric Dumazet 29830fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, slot2); 29930fff923SEric Dumazet if (hslot->count < hslot2->count) 30030fff923SEric Dumazet goto scan_primary_hash; 30130fff923SEric Dumazet 302fe38d2a1SJosef Bacik exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); 30330fff923SEric Dumazet if (!exist && (hash2_nulladdr != slot2)) { 30430fff923SEric Dumazet hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 30530fff923SEric Dumazet exist = udp_lib_lport_inuse2(net, snum, hslot2, 306fe38d2a1SJosef Bacik sk); 30730fff923SEric Dumazet } 30830fff923SEric Dumazet if (exist) 30930fff923SEric Dumazet goto fail_unlock; 31030fff923SEric Dumazet else 31130fff923SEric Dumazet goto found; 31230fff923SEric Dumazet } 31330fff923SEric Dumazet scan_primary_hash: 314fe38d2a1SJosef Bacik if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) 315645ca708SEric Dumazet goto fail_unlock; 316645ca708SEric Dumazet } 31798322f22SEric Dumazet found: 318c720c7e8SEric Dumazet inet_sk(sk)->inet_num = snum; 319d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash = snum; 320d4cada4aSEric Dumazet udp_sk(sk)->udp_portaddr_hash ^= snum; 3211da177e4SLinus Torvalds if (sk_unhashed(sk)) { 322e32ea7e7SCraig Gallek if (sk->sk_reuseport && 323fe38d2a1SJosef Bacik udp_reuseport_add_sock(sk, hslot)) { 324e32ea7e7SCraig Gallek inet_sk(sk)->inet_num = 0; 325e32ea7e7SCraig Gallek udp_sk(sk)->udp_port_hash = 0; 326e32ea7e7SCraig Gallek udp_sk(sk)->udp_portaddr_hash ^= snum; 327e32ea7e7SCraig Gallek goto fail_unlock; 328e32ea7e7SCraig Gallek } 329e32ea7e7SCraig Gallek 330ca065d0cSEric Dumazet sk_add_node_rcu(sk, &hslot->head); 331fdcc8aa9SEric Dumazet hslot->count++; 332c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 333512615b6SEric Dumazet 334512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 335512615b6SEric Dumazet spin_lock(&hslot2->lock); 336d894ba18SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 337d894ba18SCraig Gallek sk->sk_family == AF_INET6) 3381602f49bSDavid S. Miller hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, 339d894ba18SCraig Gallek &hslot2->head); 340d894ba18SCraig Gallek else 341ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 342512615b6SEric Dumazet &hslot2->head); 343512615b6SEric Dumazet hslot2->count++; 344512615b6SEric Dumazet spin_unlock(&hslot2->lock); 3451da177e4SLinus Torvalds } 346ca065d0cSEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 34725030a7fSGerrit Renker error = 0; 348645ca708SEric Dumazet fail_unlock: 349645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 3501da177e4SLinus Torvalds fail: 35125030a7fSGerrit Renker return error; 3521da177e4SLinus Torvalds } 353c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port); 3541da177e4SLinus Torvalds 3556ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 356db8dac20SDavid S. Miller { 35730fff923SEric Dumazet unsigned int hash2_nulladdr = 358f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 35930fff923SEric Dumazet unsigned int hash2_partial = 360f0b1e64cSMartin KaFai Lau ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 36130fff923SEric Dumazet 362d4cada4aSEric Dumazet /* precompute partial secondary hash */ 36330fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 364fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 365db8dac20SDavid S. Miller } 366db8dac20SDavid S. Miller 367d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 368d1e37288SSu, Xuemin __be32 saddr, __be16 sport, 369fb74c277SDavid Ahern __be32 daddr, unsigned short hnum, 370fb74c277SDavid Ahern int dif, int sdif, bool exact_dif) 371645ca708SEric Dumazet { 37260c04aecSJoe Perches int score; 37360c04aecSJoe Perches struct inet_sock *inet; 374645ca708SEric Dumazet 37560c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 37660c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 37760c04aecSJoe Perches ipv6_only_sock(sk)) 37860c04aecSJoe Perches return -1; 379645ca708SEric Dumazet 38060c04aecSJoe Perches score = (sk->sk_family == PF_INET) ? 2 : 1; 38160c04aecSJoe Perches inet = inet_sk(sk); 38260c04aecSJoe Perches 383c720c7e8SEric Dumazet if (inet->inet_rcv_saddr) { 384c720c7e8SEric Dumazet if (inet->inet_rcv_saddr != daddr) 385645ca708SEric Dumazet return -1; 386ba418fa3STom Herbert score += 4; 387645ca708SEric Dumazet } 38860c04aecSJoe Perches 389c720c7e8SEric Dumazet if (inet->inet_daddr) { 390c720c7e8SEric Dumazet if (inet->inet_daddr != saddr) 391645ca708SEric Dumazet return -1; 392ba418fa3STom Herbert score += 4; 393645ca708SEric Dumazet } 39460c04aecSJoe Perches 395c720c7e8SEric Dumazet if (inet->inet_dport) { 396c720c7e8SEric Dumazet if (inet->inet_dport != sport) 397645ca708SEric Dumazet return -1; 398ba418fa3STom Herbert score += 4; 399645ca708SEric Dumazet } 40060c04aecSJoe Perches 40163a6fff3SRobert Shearman if (sk->sk_bound_dev_if || exact_dif) { 402fb74c277SDavid Ahern bool dev_match = (sk->sk_bound_dev_if == dif || 403fb74c277SDavid Ahern sk->sk_bound_dev_if == sdif); 404fb74c277SDavid Ahern 40569678bcdSPaolo Abeni if (!dev_match) 406645ca708SEric Dumazet return -1; 40769678bcdSPaolo Abeni if (sk->sk_bound_dev_if) 408ba418fa3STom Herbert score += 4; 409645ca708SEric Dumazet } 410fb74c277SDavid Ahern 41170da268bSEric Dumazet if (sk->sk_incoming_cpu == raw_smp_processor_id()) 41270da268bSEric Dumazet score++; 413645ca708SEric Dumazet return score; 414645ca708SEric Dumazet } 415645ca708SEric Dumazet 4166eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr, 41765cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 41865cd8033SHannes Frederic Sowa const __be16 fport) 41965cd8033SHannes Frederic Sowa { 4201bbdceefSHannes Frederic Sowa static u32 udp_ehash_secret __read_mostly; 4211bbdceefSHannes Frederic Sowa 4221bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); 4231bbdceefSHannes Frederic Sowa 42465cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4251bbdceefSHannes Frederic Sowa udp_ehash_secret + net_hash_mix(net)); 42665cd8033SHannes Frederic Sowa } 42765cd8033SHannes Frederic Sowa 428d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 4295051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net, 4305051ebd2SEric Dumazet __be32 saddr, __be16 sport, 431fb74c277SDavid Ahern __be32 daddr, unsigned int hnum, 432fb74c277SDavid Ahern int dif, int sdif, bool exact_dif, 433d1e37288SSu, Xuemin struct udp_hslot *hslot2, 4341134158bSCraig Gallek struct sk_buff *skb) 4355051ebd2SEric Dumazet { 4365051ebd2SEric Dumazet struct sock *sk, *result; 437e94a62f5SPaolo Abeni int score, badness; 438ba418fa3STom Herbert u32 hash = 0; 4395051ebd2SEric Dumazet 4405051ebd2SEric Dumazet result = NULL; 441ba418fa3STom Herbert badness = 0; 442ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 443d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 444fb74c277SDavid Ahern daddr, hnum, dif, sdif, exact_dif); 4455051ebd2SEric Dumazet if (score > badness) { 446e94a62f5SPaolo Abeni if (sk->sk_reuseport) { 44765cd8033SHannes Frederic Sowa hash = udp_ehashfn(net, daddr, hnum, 4487c0cadc6SEric Dumazet saddr, sport); 449ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 4501134158bSCraig Gallek sizeof(struct udphdr)); 451ca065d0cSEric Dumazet if (result) 452ca065d0cSEric Dumazet return result; 453ba418fa3STom Herbert } 454ca065d0cSEric Dumazet badness = score; 455ca065d0cSEric Dumazet result = sk; 4565051ebd2SEric Dumazet } 4575051ebd2SEric Dumazet } 4585051ebd2SEric Dumazet return result; 4595051ebd2SEric Dumazet } 4605051ebd2SEric Dumazet 461db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 462db8dac20SDavid S. Miller * harder than this. -DaveM 463db8dac20SDavid S. Miller */ 464fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 465fb74c277SDavid Ahern __be16 sport, __be32 daddr, __be16 dport, int dif, 466fb74c277SDavid Ahern int sdif, struct udp_table *udptable, struct sk_buff *skb) 467db8dac20SDavid S. Miller { 468271b72c7SEric Dumazet struct sock *sk, *result; 469db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 4705051ebd2SEric Dumazet unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 4715051ebd2SEric Dumazet struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 47263a6fff3SRobert Shearman bool exact_dif = udp_lib_exact_dif_match(net, skb); 473e94a62f5SPaolo Abeni int score, badness; 474ba418fa3STom Herbert u32 hash = 0; 475db8dac20SDavid S. Miller 4765051ebd2SEric Dumazet if (hslot->count > 10) { 477f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 4785051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 4795051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4805051ebd2SEric Dumazet if (hslot->count < hslot2->count) 4815051ebd2SEric Dumazet goto begin; 4825051ebd2SEric Dumazet 4835051ebd2SEric Dumazet result = udp4_lib_lookup2(net, saddr, sport, 484fb74c277SDavid Ahern daddr, hnum, dif, sdif, 48563a6fff3SRobert Shearman exact_dif, hslot2, skb); 4865051ebd2SEric Dumazet if (!result) { 487d1e37288SSu, Xuemin unsigned int old_slot2 = slot2; 488f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 4895051ebd2SEric Dumazet slot2 = hash2 & udptable->mask; 490d1e37288SSu, Xuemin /* avoid searching the same slot again. */ 491d1e37288SSu, Xuemin if (unlikely(slot2 == old_slot2)) 492d1e37288SSu, Xuemin return result; 493d1e37288SSu, Xuemin 4945051ebd2SEric Dumazet hslot2 = &udptable->hash2[slot2]; 4955051ebd2SEric Dumazet if (hslot->count < hslot2->count) 4965051ebd2SEric Dumazet goto begin; 4975051ebd2SEric Dumazet 4981223c67cSJorge Boncompte [DTI2] result = udp4_lib_lookup2(net, saddr, sport, 499fb74c277SDavid Ahern daddr, hnum, dif, sdif, 50063a6fff3SRobert Shearman exact_dif, hslot2, skb); 5015051ebd2SEric Dumazet } 5028217ca65SMartin KaFai Lau if (unlikely(IS_ERR(result))) 5038217ca65SMartin KaFai Lau return NULL; 5045051ebd2SEric Dumazet return result; 5055051ebd2SEric Dumazet } 506271b72c7SEric Dumazet begin: 507271b72c7SEric Dumazet result = NULL; 508ba418fa3STom Herbert badness = 0; 509ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 510d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 511fb74c277SDavid Ahern daddr, hnum, dif, sdif, exact_dif); 512645ca708SEric Dumazet if (score > badness) { 513e94a62f5SPaolo Abeni if (sk->sk_reuseport) { 51465cd8033SHannes Frederic Sowa hash = udp_ehashfn(net, daddr, hnum, 5157c0cadc6SEric Dumazet saddr, sport); 516ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 517538950a1SCraig Gallek sizeof(struct udphdr)); 5188217ca65SMartin KaFai Lau if (unlikely(IS_ERR(result))) 5198217ca65SMartin KaFai Lau return NULL; 520ca065d0cSEric Dumazet if (result) 521ca065d0cSEric Dumazet return result; 522ba418fa3STom Herbert } 523ca065d0cSEric Dumazet result = sk; 524ca065d0cSEric Dumazet badness = score; 525db8dac20SDavid S. Miller } 526db8dac20SDavid S. Miller } 527db8dac20SDavid S. Miller return result; 528db8dac20SDavid S. Miller } 529fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 530db8dac20SDavid S. Miller 531607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 532607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 533645ca708SEric Dumazet struct udp_table *udptable) 534607c4aafSKOVACS Krisztian { 535607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 536607c4aafSKOVACS Krisztian 537ed7cbbceSAlexander Duyck return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, 538607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 539fb74c277SDavid Ahern inet_sdif(skb), udptable, skb); 540607c4aafSKOVACS Krisztian } 541607c4aafSKOVACS Krisztian 54263058308STom Herbert struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, 54363058308STom Herbert __be16 sport, __be16 dport) 54463058308STom Herbert { 545ed7cbbceSAlexander Duyck return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table); 54663058308STom Herbert } 54763058308STom Herbert EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); 54863058308STom Herbert 549ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 550ca065d0cSEric Dumazet * Does increment socket refcount. 551ca065d0cSEric Dumazet */ 5526e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) 553bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 554bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 555bcd41303SKOVACS Krisztian { 556ca065d0cSEric Dumazet struct sock *sk; 557ca065d0cSEric Dumazet 558ca065d0cSEric Dumazet sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, 559fb74c277SDavid Ahern dif, 0, &udp_table, NULL); 56041c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 561ca065d0cSEric Dumazet sk = NULL; 562ca065d0cSEric Dumazet return sk; 563bcd41303SKOVACS Krisztian } 564bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 565ca065d0cSEric Dumazet #endif 566bcd41303SKOVACS Krisztian 567421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, 568421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 569421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 570fb74c277SDavid Ahern int dif, int sdif, unsigned short hnum) 571421b3885SShawn Bohrer { 572421b3885SShawn Bohrer struct inet_sock *inet = inet_sk(sk); 573421b3885SShawn Bohrer 574421b3885SShawn Bohrer if (!net_eq(sock_net(sk), net) || 575421b3885SShawn Bohrer udp_sk(sk)->udp_port_hash != hnum || 576421b3885SShawn Bohrer (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 577421b3885SShawn Bohrer (inet->inet_dport != rmt_port && inet->inet_dport) || 578421b3885SShawn Bohrer (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || 579421b3885SShawn Bohrer ipv6_only_sock(sk) || 580fb74c277SDavid Ahern (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif && 581fb74c277SDavid Ahern sk->sk_bound_dev_if != sdif)) 582421b3885SShawn Bohrer return false; 58360d9b031SDavid Ahern if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) 584421b3885SShawn Bohrer return false; 585421b3885SShawn Bohrer return true; 586421b3885SShawn Bohrer } 587421b3885SShawn Bohrer 588db8dac20SDavid S. Miller /* 589db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 590db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 591db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 592db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 593db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 594db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 595db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 596db8dac20SDavid S. Miller * to find the appropriate port. 597db8dac20SDavid S. Miller */ 598db8dac20SDavid S. Miller 599645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 600db8dac20SDavid S. Miller { 601db8dac20SDavid S. Miller struct inet_sock *inet; 602b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 603db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 604db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 605db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 606db8dac20SDavid S. Miller struct sock *sk; 607db8dac20SDavid S. Miller int harderr; 608db8dac20SDavid S. Miller int err; 609fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 610db8dac20SDavid S. Miller 611fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 612fb74c277SDavid Ahern iph->saddr, uh->source, skb->dev->ifindex, 0, 613fb74c277SDavid Ahern udptable, NULL); 61451456b29SIan Morris if (!sk) { 6155d3848bcSEric Dumazet __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 616db8dac20SDavid S. Miller return; /* No socket for error */ 617db8dac20SDavid S. Miller } 618db8dac20SDavid S. Miller 619db8dac20SDavid S. Miller err = 0; 620db8dac20SDavid S. Miller harderr = 0; 621db8dac20SDavid S. Miller inet = inet_sk(sk); 622db8dac20SDavid S. Miller 623db8dac20SDavid S. Miller switch (type) { 624db8dac20SDavid S. Miller default: 625db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 626db8dac20SDavid S. Miller err = EHOSTUNREACH; 627db8dac20SDavid S. Miller break; 628db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 629db8dac20SDavid S. Miller goto out; 630db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 631db8dac20SDavid S. Miller err = EPROTO; 632db8dac20SDavid S. Miller harderr = 1; 633db8dac20SDavid S. Miller break; 634db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 635db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 63636393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 637db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 638db8dac20SDavid S. Miller err = EMSGSIZE; 639db8dac20SDavid S. Miller harderr = 1; 640db8dac20SDavid S. Miller break; 641db8dac20SDavid S. Miller } 642db8dac20SDavid S. Miller goto out; 643db8dac20SDavid S. Miller } 644db8dac20SDavid S. Miller err = EHOSTUNREACH; 645db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 646db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 647db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 648db8dac20SDavid S. Miller } 649db8dac20SDavid S. Miller break; 65055be7a9cSDavid S. Miller case ICMP_REDIRECT: 65155be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 6521a462d18SDuan Jiong goto out; 653db8dac20SDavid S. Miller } 654db8dac20SDavid S. Miller 655db8dac20SDavid S. Miller /* 656db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 657db8dac20SDavid S. Miller * 4.1.3.3. 658db8dac20SDavid S. Miller */ 659db8dac20SDavid S. Miller if (!inet->recverr) { 660db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 661db8dac20SDavid S. Miller goto out; 662b1faf566SEric Dumazet } else 663db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 664b1faf566SEric Dumazet 665db8dac20SDavid S. Miller sk->sk_err = err; 666db8dac20SDavid S. Miller sk->sk_error_report(sk); 667db8dac20SDavid S. Miller out: 668ca065d0cSEric Dumazet return; 669db8dac20SDavid S. Miller } 670db8dac20SDavid S. Miller 671db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 672db8dac20SDavid S. Miller { 673645ca708SEric Dumazet __udp4_lib_err(skb, info, &udp_table); 674db8dac20SDavid S. Miller } 675db8dac20SDavid S. Miller 676db8dac20SDavid S. Miller /* 677db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 678db8dac20SDavid S. Miller */ 67936d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 680db8dac20SDavid S. Miller { 681db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 682db8dac20SDavid S. Miller 683db8dac20SDavid S. Miller if (up->pending) { 684db8dac20SDavid S. Miller up->len = 0; 685db8dac20SDavid S. Miller up->pending = 0; 686db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 687db8dac20SDavid S. Miller } 688db8dac20SDavid S. Miller } 68936d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 690db8dac20SDavid S. Miller 691db8dac20SDavid S. Miller /** 692f6b9664fSHerbert Xu * udp4_hwcsum - handle outgoing HW checksumming 693db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 694db8dac20SDavid S. Miller * (checksum field must be zeroed out) 695f6b9664fSHerbert Xu * @src: source IP address 696f6b9664fSHerbert Xu * @dst: destination IP address 697db8dac20SDavid S. Miller */ 698c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 699db8dac20SDavid S. Miller { 700db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 701f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 702f6b9664fSHerbert Xu int len = skb->len - offset; 703f6b9664fSHerbert Xu int hlen = len; 704db8dac20SDavid S. Miller __wsum csum = 0; 705db8dac20SDavid S. Miller 706ebbe495fSWANG Cong if (!skb_has_frag_list(skb)) { 707db8dac20SDavid S. Miller /* 708db8dac20SDavid S. Miller * Only one fragment on the socket. 709db8dac20SDavid S. Miller */ 710db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 711db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 712f6b9664fSHerbert Xu uh->check = ~csum_tcpudp_magic(src, dst, len, 713f6b9664fSHerbert Xu IPPROTO_UDP, 0); 714db8dac20SDavid S. Miller } else { 715ebbe495fSWANG Cong struct sk_buff *frags; 716ebbe495fSWANG Cong 717db8dac20SDavid S. Miller /* 718db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 719db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 720db8dac20SDavid S. Miller * should be together 721db8dac20SDavid S. Miller */ 722ebbe495fSWANG Cong skb_walk_frags(skb, frags) { 723f6b9664fSHerbert Xu csum = csum_add(csum, frags->csum); 724f6b9664fSHerbert Xu hlen -= frags->len; 725ebbe495fSWANG Cong } 726db8dac20SDavid S. Miller 727f6b9664fSHerbert Xu csum = skb_checksum(skb, offset, hlen, csum); 728db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 729db8dac20SDavid S. Miller 730db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 731db8dac20SDavid S. Miller if (uh->check == 0) 732db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 733db8dac20SDavid S. Miller } 734db8dac20SDavid S. Miller } 735c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum); 736db8dac20SDavid S. Miller 737af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended 738af5fcba7STom Herbert * for the simple case like when setting the checksum for a UDP tunnel. 739af5fcba7STom Herbert */ 740af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb, 741af5fcba7STom Herbert __be32 saddr, __be32 daddr, int len) 742af5fcba7STom Herbert { 743af5fcba7STom Herbert struct udphdr *uh = udp_hdr(skb); 744af5fcba7STom Herbert 745179bc67fSEdward Cree if (nocheck) { 746af5fcba7STom Herbert uh->check = 0; 747179bc67fSEdward Cree } else if (skb_is_gso(skb)) { 748af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 749179bc67fSEdward Cree } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 750179bc67fSEdward Cree uh->check = 0; 751179bc67fSEdward Cree uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); 752179bc67fSEdward Cree if (uh->check == 0) 753179bc67fSEdward Cree uh->check = CSUM_MANGLED_0; 754d75f1306SEdward Cree } else { 755af5fcba7STom Herbert skb->ip_summed = CHECKSUM_PARTIAL; 756af5fcba7STom Herbert skb->csum_start = skb_transport_header(skb) - skb->head; 757af5fcba7STom Herbert skb->csum_offset = offsetof(struct udphdr, check); 758af5fcba7STom Herbert uh->check = ~udp_v4_check(len, saddr, daddr, 0); 759af5fcba7STom Herbert } 760af5fcba7STom Herbert } 761af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum); 762af5fcba7STom Herbert 763bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, 764bec1f6f6SWillem de Bruijn struct inet_cork *cork) 765f6b9664fSHerbert Xu { 766f6b9664fSHerbert Xu struct sock *sk = skb->sk; 767f6b9664fSHerbert Xu struct inet_sock *inet = inet_sk(sk); 768f6b9664fSHerbert Xu struct udphdr *uh; 769f6b9664fSHerbert Xu int err = 0; 770f6b9664fSHerbert Xu int is_udplite = IS_UDPLITE(sk); 771f6b9664fSHerbert Xu int offset = skb_transport_offset(skb); 772f6b9664fSHerbert Xu int len = skb->len - offset; 773f6b9664fSHerbert Xu __wsum csum = 0; 774f6b9664fSHerbert Xu 775f6b9664fSHerbert Xu /* 776f6b9664fSHerbert Xu * Create a UDP header 777f6b9664fSHerbert Xu */ 778f6b9664fSHerbert Xu uh = udp_hdr(skb); 779f6b9664fSHerbert Xu uh->source = inet->inet_sport; 78079ab0531SDavid S. Miller uh->dest = fl4->fl4_dport; 781f6b9664fSHerbert Xu uh->len = htons(len); 782f6b9664fSHerbert Xu uh->check = 0; 783f6b9664fSHerbert Xu 784bec1f6f6SWillem de Bruijn if (cork->gso_size) { 785bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 786bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 787bec1f6f6SWillem de Bruijn 788bec1f6f6SWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) 789bec1f6f6SWillem de Bruijn return -EINVAL; 790bec1f6f6SWillem de Bruijn if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) 791bec1f6f6SWillem de Bruijn return -EINVAL; 792a8c744a8SWillem de Bruijn if (sk->sk_no_check_tx) 793a8c744a8SWillem de Bruijn return -EINVAL; 794ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 795ff06342cSWillem de Bruijn dst_xfrm(skb_dst(skb))) 796bec1f6f6SWillem de Bruijn return -EIO; 797bec1f6f6SWillem de Bruijn 798bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 799bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 800dfec0ee2SAlexander Duyck skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(len - sizeof(uh), 801dfec0ee2SAlexander Duyck cork->gso_size); 802a8c744a8SWillem de Bruijn goto csum_partial; 803bec1f6f6SWillem de Bruijn } 804bec1f6f6SWillem de Bruijn 805f6b9664fSHerbert Xu if (is_udplite) /* UDP-Lite */ 806f6b9664fSHerbert Xu csum = udplite_csum(skb); 807f6b9664fSHerbert Xu 808ab2fb7e3SWillem de Bruijn else if (sk->sk_no_check_tx) { /* UDP csum off */ 809f6b9664fSHerbert Xu 810f6b9664fSHerbert Xu skb->ip_summed = CHECKSUM_NONE; 811f6b9664fSHerbert Xu goto send; 812f6b9664fSHerbert Xu 813f6b9664fSHerbert Xu } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 814a8c744a8SWillem de Bruijn csum_partial: 815f6b9664fSHerbert Xu 81679ab0531SDavid S. Miller udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 817f6b9664fSHerbert Xu goto send; 818f6b9664fSHerbert Xu 819f6b9664fSHerbert Xu } else 820f6b9664fSHerbert Xu csum = udp_csum(skb); 821f6b9664fSHerbert Xu 822f6b9664fSHerbert Xu /* add protocol-dependent pseudo-header */ 82379ab0531SDavid S. Miller uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 824f6b9664fSHerbert Xu sk->sk_protocol, csum); 825f6b9664fSHerbert Xu if (uh->check == 0) 826f6b9664fSHerbert Xu uh->check = CSUM_MANGLED_0; 827f6b9664fSHerbert Xu 828f6b9664fSHerbert Xu send: 829b5ec8eeaSEric Dumazet err = ip_send_skb(sock_net(sk), skb); 830f6b9664fSHerbert Xu if (err) { 831f6b9664fSHerbert Xu if (err == -ENOBUFS && !inet->recverr) { 8326aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 833f6b9664fSHerbert Xu UDP_MIB_SNDBUFERRORS, is_udplite); 834f6b9664fSHerbert Xu err = 0; 835f6b9664fSHerbert Xu } 836f6b9664fSHerbert Xu } else 8376aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 838f6b9664fSHerbert Xu UDP_MIB_OUTDATAGRAMS, is_udplite); 839f6b9664fSHerbert Xu return err; 840f6b9664fSHerbert Xu } 841f6b9664fSHerbert Xu 842db8dac20SDavid S. Miller /* 843db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 844db8dac20SDavid S. Miller */ 8458822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk) 846db8dac20SDavid S. Miller { 847db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 848db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 849b6f21b26SDavid S. Miller struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 850db8dac20SDavid S. Miller struct sk_buff *skb; 851db8dac20SDavid S. Miller int err = 0; 852db8dac20SDavid S. Miller 85377968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 854f6b9664fSHerbert Xu if (!skb) 855db8dac20SDavid S. Miller goto out; 856db8dac20SDavid S. Miller 857bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &inet->cork.base); 858db8dac20SDavid S. Miller 859db8dac20SDavid S. Miller out: 860db8dac20SDavid S. Miller up->len = 0; 861db8dac20SDavid S. Miller up->pending = 0; 862db8dac20SDavid S. Miller return err; 863db8dac20SDavid S. Miller } 8648822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames); 865db8dac20SDavid S. Miller 8662e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) 8672e8de857SWillem de Bruijn { 8682e8de857SWillem de Bruijn switch (cmsg->cmsg_type) { 8692e8de857SWillem de Bruijn case UDP_SEGMENT: 8702e8de857SWillem de Bruijn if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) 8712e8de857SWillem de Bruijn return -EINVAL; 8722e8de857SWillem de Bruijn *gso_size = *(__u16 *)CMSG_DATA(cmsg); 8732e8de857SWillem de Bruijn return 0; 8742e8de857SWillem de Bruijn default: 8752e8de857SWillem de Bruijn return -EINVAL; 8762e8de857SWillem de Bruijn } 8772e8de857SWillem de Bruijn } 8782e8de857SWillem de Bruijn 8792e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) 8802e8de857SWillem de Bruijn { 8812e8de857SWillem de Bruijn struct cmsghdr *cmsg; 8822e8de857SWillem de Bruijn bool need_ip = false; 8832e8de857SWillem de Bruijn int err; 8842e8de857SWillem de Bruijn 8852e8de857SWillem de Bruijn for_each_cmsghdr(cmsg, msg) { 8862e8de857SWillem de Bruijn if (!CMSG_OK(msg, cmsg)) 8872e8de857SWillem de Bruijn return -EINVAL; 8882e8de857SWillem de Bruijn 8892e8de857SWillem de Bruijn if (cmsg->cmsg_level != SOL_UDP) { 8902e8de857SWillem de Bruijn need_ip = true; 8912e8de857SWillem de Bruijn continue; 8922e8de857SWillem de Bruijn } 8932e8de857SWillem de Bruijn 8942e8de857SWillem de Bruijn err = __udp_cmsg_send(cmsg, gso_size); 8952e8de857SWillem de Bruijn if (err) 8962e8de857SWillem de Bruijn return err; 8972e8de857SWillem de Bruijn } 8982e8de857SWillem de Bruijn 8992e8de857SWillem de Bruijn return need_ip; 9002e8de857SWillem de Bruijn } 9012e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send); 9022e8de857SWillem de Bruijn 9031b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 904db8dac20SDavid S. Miller { 905db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 906db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 9071cedee13SAndrey Ignatov DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 908e474995fSDavid S. Miller struct flowi4 fl4_stack; 909b6f21b26SDavid S. Miller struct flowi4 *fl4; 910db8dac20SDavid S. Miller int ulen = len; 911db8dac20SDavid S. Miller struct ipcm_cookie ipc; 912db8dac20SDavid S. Miller struct rtable *rt = NULL; 913db8dac20SDavid S. Miller int free = 0; 914db8dac20SDavid S. Miller int connected = 0; 915db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 916db8dac20SDavid S. Miller __be16 dport; 917db8dac20SDavid S. Miller u8 tos; 918db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 919db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 920db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 921903ab86dSHerbert Xu struct sk_buff *skb; 922f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 923db8dac20SDavid S. Miller 924db8dac20SDavid S. Miller if (len > 0xFFFF) 925db8dac20SDavid S. Miller return -EMSGSIZE; 926db8dac20SDavid S. Miller 927db8dac20SDavid S. Miller /* 928db8dac20SDavid S. Miller * Check the flags. 929db8dac20SDavid S. Miller */ 930db8dac20SDavid S. Miller 931db8dac20SDavid S. Miller if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 932db8dac20SDavid S. Miller return -EOPNOTSUPP; 933db8dac20SDavid S. Miller 934903ab86dSHerbert Xu getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 935903ab86dSHerbert Xu 936f5fca608SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 937db8dac20SDavid S. Miller if (up->pending) { 938db8dac20SDavid S. Miller /* 939db8dac20SDavid S. Miller * There are pending frames. 940db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 941db8dac20SDavid S. Miller */ 942db8dac20SDavid S. Miller lock_sock(sk); 943db8dac20SDavid S. Miller if (likely(up->pending)) { 944db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 945db8dac20SDavid S. Miller release_sock(sk); 946db8dac20SDavid S. Miller return -EINVAL; 947db8dac20SDavid S. Miller } 948db8dac20SDavid S. Miller goto do_append_data; 949db8dac20SDavid S. Miller } 950db8dac20SDavid S. Miller release_sock(sk); 951db8dac20SDavid S. Miller } 952db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 953db8dac20SDavid S. Miller 954db8dac20SDavid S. Miller /* 955db8dac20SDavid S. Miller * Get and verify the address. 956db8dac20SDavid S. Miller */ 9571cedee13SAndrey Ignatov if (usin) { 958db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 959db8dac20SDavid S. Miller return -EINVAL; 960db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 961db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 962db8dac20SDavid S. Miller return -EAFNOSUPPORT; 963db8dac20SDavid S. Miller } 964db8dac20SDavid S. Miller 965db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 966db8dac20SDavid S. Miller dport = usin->sin_port; 967db8dac20SDavid S. Miller if (dport == 0) 968db8dac20SDavid S. Miller return -EINVAL; 969db8dac20SDavid S. Miller } else { 970db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 971db8dac20SDavid S. Miller return -EDESTADDRREQ; 972c720c7e8SEric Dumazet daddr = inet->inet_daddr; 973c720c7e8SEric Dumazet dport = inet->inet_dport; 974db8dac20SDavid S. Miller /* Open fast path for connected socket. 975db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 976db8dac20SDavid S. Miller */ 977db8dac20SDavid S. Miller connected = 1; 978db8dac20SDavid S. Miller } 979c14ac945SSoheil Hassas Yeganeh 98035178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 981bec1f6f6SWillem de Bruijn ipc.gso_size = up->gso_size; 982bf84a010SDaniel Borkmann 983db8dac20SDavid S. Miller if (msg->msg_controllen) { 9842e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc.gso_size); 9852e8de857SWillem de Bruijn if (err > 0) 9862e8de857SWillem de Bruijn err = ip_cmsg_send(sk, msg, &ipc, 9872e8de857SWillem de Bruijn sk->sk_family == AF_INET6); 9882e8de857SWillem de Bruijn if (unlikely(err < 0)) { 98991948309SEric Dumazet kfree(ipc.opt); 990db8dac20SDavid S. Miller return err; 99191948309SEric Dumazet } 992db8dac20SDavid S. Miller if (ipc.opt) 993db8dac20SDavid S. Miller free = 1; 994db8dac20SDavid S. Miller connected = 0; 995db8dac20SDavid S. Miller } 996f6d8bd05SEric Dumazet if (!ipc.opt) { 997f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 998f6d8bd05SEric Dumazet 999f6d8bd05SEric Dumazet rcu_read_lock(); 1000f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 1001f6d8bd05SEric Dumazet if (inet_opt) { 1002f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 1003f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 1004f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 1005f6d8bd05SEric Dumazet } 1006f6d8bd05SEric Dumazet rcu_read_unlock(); 1007f6d8bd05SEric Dumazet } 1008db8dac20SDavid S. Miller 10091cedee13SAndrey Ignatov if (cgroup_bpf_enabled && !connected) { 10101cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, 10111cedee13SAndrey Ignatov (struct sockaddr *)usin, &ipc.addr); 10121cedee13SAndrey Ignatov if (err) 10131cedee13SAndrey Ignatov goto out_free; 10141cedee13SAndrey Ignatov if (usin) { 10151cedee13SAndrey Ignatov if (usin->sin_port == 0) { 10161cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 10171cedee13SAndrey Ignatov err = -EINVAL; 10181cedee13SAndrey Ignatov goto out_free; 10191cedee13SAndrey Ignatov } 10201cedee13SAndrey Ignatov daddr = usin->sin_addr.s_addr; 10211cedee13SAndrey Ignatov dport = usin->sin_port; 10221cedee13SAndrey Ignatov } 10231cedee13SAndrey Ignatov } 10241cedee13SAndrey Ignatov 1025db8dac20SDavid S. Miller saddr = ipc.addr; 1026db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 1027db8dac20SDavid S. Miller 1028f6d8bd05SEric Dumazet if (ipc.opt && ipc.opt->opt.srr) { 10291b97013bSAndrey Ignatov if (!daddr) { 10301b97013bSAndrey Ignatov err = -EINVAL; 10311b97013bSAndrey Ignatov goto out_free; 10321b97013bSAndrey Ignatov } 1033f6d8bd05SEric Dumazet faddr = ipc.opt->opt.faddr; 1034db8dac20SDavid S. Miller connected = 0; 1035db8dac20SDavid S. Miller } 1036aa661581SFrancesco Fusco tos = get_rttos(&ipc, inet); 1037db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 1038db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 1039f6d8bd05SEric Dumazet (ipc.opt && ipc.opt->opt.is_strictroute)) { 1040db8dac20SDavid S. Miller tos |= RTO_ONLINK; 1041db8dac20SDavid S. Miller connected = 0; 1042db8dac20SDavid S. Miller } 1043db8dac20SDavid S. Miller 1044db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 1045db8dac20SDavid S. Miller if (!ipc.oif) 1046db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 1047db8dac20SDavid S. Miller if (!saddr) 1048db8dac20SDavid S. Miller saddr = inet->mc_addr; 1049db8dac20SDavid S. Miller connected = 0; 10509515a2e0SDavid Ahern } else if (!ipc.oif) { 105176e21053SErich E. Hoover ipc.oif = inet->uc_index; 10529515a2e0SDavid Ahern } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 10539515a2e0SDavid Ahern /* oif is set, packet is to local broadcast and 10549515a2e0SDavid Ahern * and uc_index is set. oif is most likely set 10559515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 10569515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 10579515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 10589515a2e0SDavid Ahern */ 10599515a2e0SDavid Ahern if (ipc.oif != inet->uc_index && 10609515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 10619515a2e0SDavid Ahern inet->uc_index)) { 10629515a2e0SDavid Ahern ipc.oif = inet->uc_index; 10639515a2e0SDavid Ahern } 10649515a2e0SDavid Ahern } 1065db8dac20SDavid S. Miller 1066db8dac20SDavid S. Miller if (connected) 1067db8dac20SDavid S. Miller rt = (struct rtable *)sk_dst_check(sk, 0); 1068db8dac20SDavid S. Miller 106951456b29SIan Morris if (!rt) { 107084a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 10719a24abfaSDavid Ahern __u8 flow_flags = inet_sk_flowi_flags(sk); 107284a3aa00SPavel Emelyanov 1073e474995fSDavid S. Miller fl4 = &fl4_stack; 10749a24abfaSDavid Ahern 10759a24abfaSDavid Ahern flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos, 10769a24abfaSDavid Ahern RT_SCOPE_UNIVERSE, sk->sk_protocol, 10779a24abfaSDavid Ahern flow_flags, 1078e2d118a1SLorenzo Colitti faddr, saddr, dport, inet->inet_sport, 1079e2d118a1SLorenzo Colitti sk->sk_uid); 1080c0951cbcSDavid S. Miller 1081e474995fSDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 1082e474995fSDavid S. Miller rt = ip_route_output_flow(net, fl4, sk); 1083b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 1084b23dd4feSDavid S. Miller err = PTR_ERR(rt); 108506dc94b1SDavid S. Miller rt = NULL; 1086db8dac20SDavid S. Miller if (err == -ENETUNREACH) 1087f1d8cba6SEric Dumazet IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 1088db8dac20SDavid S. Miller goto out; 1089db8dac20SDavid S. Miller } 1090db8dac20SDavid S. Miller 1091db8dac20SDavid S. Miller err = -EACCES; 1092db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 1093db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 1094db8dac20SDavid S. Miller goto out; 1095db8dac20SDavid S. Miller if (connected) 1096d8d1f30bSChangli Gao sk_dst_set(sk, dst_clone(&rt->dst)); 1097db8dac20SDavid S. Miller } 1098db8dac20SDavid S. Miller 1099db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1100db8dac20SDavid S. Miller goto do_confirm; 1101db8dac20SDavid S. Miller back_from_confirm: 1102db8dac20SDavid S. Miller 1103e474995fSDavid S. Miller saddr = fl4->saddr; 1104db8dac20SDavid S. Miller if (!ipc.addr) 1105e474995fSDavid S. Miller daddr = ipc.addr = fl4->daddr; 1106db8dac20SDavid S. Miller 1107903ab86dSHerbert Xu /* Lockless fast path for the non-corking case. */ 1108903ab86dSHerbert Xu if (!corkreq) { 11091cd7884dSWillem de Bruijn struct inet_cork cork; 11101cd7884dSWillem de Bruijn 1111f69e6d13SAl Viro skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, 1112903ab86dSHerbert Xu sizeof(struct udphdr), &ipc, &rt, 11131cd7884dSWillem de Bruijn &cork, msg->msg_flags); 1114903ab86dSHerbert Xu err = PTR_ERR(skb); 111550c3a487SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR_OR_NULL(skb)) 1116bec1f6f6SWillem de Bruijn err = udp_send_skb(skb, fl4, &cork); 1117903ab86dSHerbert Xu goto out; 1118903ab86dSHerbert Xu } 1119903ab86dSHerbert Xu 1120db8dac20SDavid S. Miller lock_sock(sk); 1121db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1122db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1123db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1124db8dac20SDavid S. Miller release_sock(sk); 1125db8dac20SDavid S. Miller 1126197df02cSMatteo Croce net_dbg_ratelimited("socket already corked\n"); 1127db8dac20SDavid S. Miller err = -EINVAL; 1128db8dac20SDavid S. Miller goto out; 1129db8dac20SDavid S. Miller } 1130db8dac20SDavid S. Miller /* 1131db8dac20SDavid S. Miller * Now cork the socket to pend data. 1132db8dac20SDavid S. Miller */ 1133b6f21b26SDavid S. Miller fl4 = &inet->cork.fl.u.ip4; 1134b6f21b26SDavid S. Miller fl4->daddr = daddr; 1135b6f21b26SDavid S. Miller fl4->saddr = saddr; 11369cce96dfSDavid S. Miller fl4->fl4_dport = dport; 11379cce96dfSDavid S. Miller fl4->fl4_sport = inet->inet_sport; 1138db8dac20SDavid S. Miller up->pending = AF_INET; 1139db8dac20SDavid S. Miller 1140db8dac20SDavid S. Miller do_append_data: 1141db8dac20SDavid S. Miller up->len += ulen; 1142f69e6d13SAl Viro err = ip_append_data(sk, fl4, getfrag, msg, ulen, 11432e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 1144db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1145db8dac20SDavid S. Miller if (err) 1146db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1147db8dac20SDavid S. Miller else if (!corkreq) 1148db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 1149db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1150db8dac20SDavid S. Miller up->pending = 0; 1151db8dac20SDavid S. Miller release_sock(sk); 1152db8dac20SDavid S. Miller 1153db8dac20SDavid S. Miller out: 1154db8dac20SDavid S. Miller ip_rt_put(rt); 11551b97013bSAndrey Ignatov out_free: 1156db8dac20SDavid S. Miller if (free) 1157db8dac20SDavid S. Miller kfree(ipc.opt); 1158db8dac20SDavid S. Miller if (!err) 1159db8dac20SDavid S. Miller return len; 1160db8dac20SDavid S. Miller /* 1161db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1162db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1163db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1164db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1165db8dac20SDavid S. Miller * seems like overkill. 1166db8dac20SDavid S. Miller */ 1167db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 11686aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1169629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1170db8dac20SDavid S. Miller } 1171db8dac20SDavid S. Miller return err; 1172db8dac20SDavid S. Miller 1173db8dac20SDavid S. Miller do_confirm: 11740dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 11750dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4->daddr); 1176db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1177db8dac20SDavid S. Miller goto back_from_confirm; 1178db8dac20SDavid S. Miller err = 0; 1179db8dac20SDavid S. Miller goto out; 1180db8dac20SDavid S. Miller } 1181c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg); 1182db8dac20SDavid S. Miller 1183db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 1184db8dac20SDavid S. Miller size_t size, int flags) 1185db8dac20SDavid S. Miller { 1186f5fca608SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1187db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1188db8dac20SDavid S. Miller int ret; 1189db8dac20SDavid S. Miller 1190d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 1191d3f7d56aSShawn Landden flags |= MSG_MORE; 1192d3f7d56aSShawn Landden 1193db8dac20SDavid S. Miller if (!up->pending) { 1194db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1195db8dac20SDavid S. Miller 1196db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 1197db8dac20SDavid S. Miller * sendpage interface can't pass. 1198db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 1199db8dac20SDavid S. Miller */ 12001b784140SYing Xue ret = udp_sendmsg(sk, &msg, 0); 1201db8dac20SDavid S. Miller if (ret < 0) 1202db8dac20SDavid S. Miller return ret; 1203db8dac20SDavid S. Miller } 1204db8dac20SDavid S. Miller 1205db8dac20SDavid S. Miller lock_sock(sk); 1206db8dac20SDavid S. Miller 1207db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 1208db8dac20SDavid S. Miller release_sock(sk); 1209db8dac20SDavid S. Miller 1210197df02cSMatteo Croce net_dbg_ratelimited("cork failed\n"); 1211db8dac20SDavid S. Miller return -EINVAL; 1212db8dac20SDavid S. Miller } 1213db8dac20SDavid S. Miller 1214f5fca608SDavid S. Miller ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1215f5fca608SDavid S. Miller page, offset, size, flags); 1216db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 1217db8dac20SDavid S. Miller release_sock(sk); 1218db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 1219db8dac20SDavid S. Miller size, flags); 1220db8dac20SDavid S. Miller } 1221db8dac20SDavid S. Miller if (ret < 0) { 1222db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1223db8dac20SDavid S. Miller goto out; 1224db8dac20SDavid S. Miller } 1225db8dac20SDavid S. Miller 1226db8dac20SDavid S. Miller up->len += size; 1227db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 1228db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 1229db8dac20SDavid S. Miller if (!ret) 1230db8dac20SDavid S. Miller ret = size; 1231db8dac20SDavid S. Miller out: 1232db8dac20SDavid S. Miller release_sock(sk); 1233db8dac20SDavid S. Miller return ret; 1234db8dac20SDavid S. Miller } 1235db8dac20SDavid S. Miller 1236dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000 1237dce4551cSPaolo Abeni 1238b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb) 1239b65ac446SPaolo Abeni { 1240dce4551cSPaolo Abeni struct udp_dev_scratch *scratch = udp_skb_scratch(skb); 1241b65ac446SPaolo Abeni 1242b65ac446SPaolo Abeni BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); 1243dce4551cSPaolo Abeni scratch->_tsize_state = skb->truesize; 1244dce4551cSPaolo Abeni #if BITS_PER_LONG == 64 1245b65ac446SPaolo Abeni scratch->len = skb->len; 1246b65ac446SPaolo Abeni scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); 1247b65ac446SPaolo Abeni scratch->is_linear = !skb_is_nonlinear(skb); 1248b65ac446SPaolo Abeni #endif 12493bdefdf9SPaolo Abeni /* all head states execept sp (dst, sk, nf) are always cleared by 12503bdefdf9SPaolo Abeni * udp_rcv() and we need to preserve secpath, if present, to eventually 12513bdefdf9SPaolo Abeni * process IP_CMSG_PASSSEC at recvmsg() time 12523bdefdf9SPaolo Abeni */ 12533bdefdf9SPaolo Abeni if (likely(!skb_sec_path(skb))) 1254dce4551cSPaolo Abeni scratch->_tsize_state |= UDP_SKB_IS_STATELESS; 1255dce4551cSPaolo Abeni } 1256dce4551cSPaolo Abeni 1257dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb) 1258dce4551cSPaolo Abeni { 1259dce4551cSPaolo Abeni return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; 1260dce4551cSPaolo Abeni } 1261dce4551cSPaolo Abeni 1262dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb) 1263dce4551cSPaolo Abeni { 1264dce4551cSPaolo Abeni return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); 1265dce4551cSPaolo Abeni } 1266b65ac446SPaolo Abeni 12677c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */ 12686dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial, 12696dfb4367SPaolo Abeni bool rx_queue_lock_held) 1270f970bd9eSPaolo Abeni { 12716b229cf7SEric Dumazet struct udp_sock *up = udp_sk(sk); 12722276f58aSPaolo Abeni struct sk_buff_head *sk_queue; 1273f970bd9eSPaolo Abeni int amt; 1274f970bd9eSPaolo Abeni 12756b229cf7SEric Dumazet if (likely(partial)) { 12766b229cf7SEric Dumazet up->forward_deficit += size; 12776b229cf7SEric Dumazet size = up->forward_deficit; 12780d4a6608SPaolo Abeni if (size < (sk->sk_rcvbuf >> 2)) 12796b229cf7SEric Dumazet return; 12806b229cf7SEric Dumazet } else { 12816b229cf7SEric Dumazet size += up->forward_deficit; 12826b229cf7SEric Dumazet } 12836b229cf7SEric Dumazet up->forward_deficit = 0; 12846b229cf7SEric Dumazet 12856dfb4367SPaolo Abeni /* acquire the sk_receive_queue for fwd allocated memory scheduling, 12866dfb4367SPaolo Abeni * if the called don't held it already 12876dfb4367SPaolo Abeni */ 12882276f58aSPaolo Abeni sk_queue = &sk->sk_receive_queue; 12896dfb4367SPaolo Abeni if (!rx_queue_lock_held) 12902276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 12912276f58aSPaolo Abeni 12926dfb4367SPaolo Abeni 1293f970bd9eSPaolo Abeni sk->sk_forward_alloc += size; 1294f970bd9eSPaolo Abeni amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); 1295f970bd9eSPaolo Abeni sk->sk_forward_alloc -= amt; 1296f970bd9eSPaolo Abeni 1297f970bd9eSPaolo Abeni if (amt) 1298f970bd9eSPaolo Abeni __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); 129902ab0d13SEric Dumazet 130002ab0d13SEric Dumazet atomic_sub(size, &sk->sk_rmem_alloc); 13012276f58aSPaolo Abeni 13022276f58aSPaolo Abeni /* this can save us from acquiring the rx queue lock on next receive */ 13032276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, &up->reader_queue); 13042276f58aSPaolo Abeni 13056dfb4367SPaolo Abeni if (!rx_queue_lock_held) 13062276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 1307f970bd9eSPaolo Abeni } 1308f970bd9eSPaolo Abeni 13092276f58aSPaolo Abeni /* Note: called with reader_queue.lock held. 1310c84d9490SEric Dumazet * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch 1311c84d9490SEric Dumazet * This avoids a cache line miss while receive_queue lock is held. 1312c84d9490SEric Dumazet * Look at __udp_enqueue_schedule_skb() to find where this copy is done. 1313c84d9490SEric Dumazet */ 13147c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) 1315f970bd9eSPaolo Abeni { 1316b65ac446SPaolo Abeni prefetch(&skb->data); 1317b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); 1318f970bd9eSPaolo Abeni } 13197c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor); 1320f970bd9eSPaolo Abeni 13216dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */ 132264f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) 13236dfb4367SPaolo Abeni { 1324b65ac446SPaolo Abeni prefetch(&skb->data); 1325b65ac446SPaolo Abeni udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); 13266dfb4367SPaolo Abeni } 13276dfb4367SPaolo Abeni 13284b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock 13294b272750SEric Dumazet * to relieve pressure on the receive_queue spinlock shared by consumer. 13304b272750SEric Dumazet * Under flood, this means that only one producer can be in line 13314b272750SEric Dumazet * trying to acquire the receive_queue spinlock. 13324b272750SEric Dumazet * These busylock can be allocated on a per cpu manner, instead of a 13334b272750SEric Dumazet * per socket one (that would consume a cache line per socket) 13344b272750SEric Dumazet */ 13354b272750SEric Dumazet static int udp_busylocks_log __read_mostly; 13364b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly; 13374b272750SEric Dumazet 13384b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr) 13394b272750SEric Dumazet { 13404b272750SEric Dumazet spinlock_t *busy; 13414b272750SEric Dumazet 13424b272750SEric Dumazet busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); 13434b272750SEric Dumazet spin_lock(busy); 13444b272750SEric Dumazet return busy; 13454b272750SEric Dumazet } 13464b272750SEric Dumazet 13474b272750SEric Dumazet static void busylock_release(spinlock_t *busy) 13484b272750SEric Dumazet { 13494b272750SEric Dumazet if (busy) 13504b272750SEric Dumazet spin_unlock(busy); 13514b272750SEric Dumazet } 13524b272750SEric Dumazet 1353f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) 1354f970bd9eSPaolo Abeni { 1355f970bd9eSPaolo Abeni struct sk_buff_head *list = &sk->sk_receive_queue; 1356f970bd9eSPaolo Abeni int rmem, delta, amt, err = -ENOMEM; 13574b272750SEric Dumazet spinlock_t *busy = NULL; 1358c8c8b127SEric Dumazet int size; 1359f970bd9eSPaolo Abeni 1360f970bd9eSPaolo Abeni /* try to avoid the costly atomic add/sub pair when the receive 1361f970bd9eSPaolo Abeni * queue is full; always allow at least a packet 1362f970bd9eSPaolo Abeni */ 1363f970bd9eSPaolo Abeni rmem = atomic_read(&sk->sk_rmem_alloc); 1364363dc73aSPaolo Abeni if (rmem > sk->sk_rcvbuf) 1365f970bd9eSPaolo Abeni goto drop; 1366f970bd9eSPaolo Abeni 1367c8c8b127SEric Dumazet /* Under mem pressure, it might be helpful to help udp_recvmsg() 1368c8c8b127SEric Dumazet * having linear skbs : 1369c8c8b127SEric Dumazet * - Reduce memory overhead and thus increase receive queue capacity 1370c8c8b127SEric Dumazet * - Less cache line misses at copyout() time 1371c8c8b127SEric Dumazet * - Less work at consume_skb() (less alien page frag freeing) 1372c8c8b127SEric Dumazet */ 13734b272750SEric Dumazet if (rmem > (sk->sk_rcvbuf >> 1)) { 1374c8c8b127SEric Dumazet skb_condense(skb); 13754b272750SEric Dumazet 13764b272750SEric Dumazet busy = busylock_acquire(sk); 13774b272750SEric Dumazet } 1378c8c8b127SEric Dumazet size = skb->truesize; 1379b65ac446SPaolo Abeni udp_set_dev_scratch(skb); 1380c8c8b127SEric Dumazet 1381f970bd9eSPaolo Abeni /* we drop only if the receive buf is full and the receive 1382f970bd9eSPaolo Abeni * queue contains some other skb 1383f970bd9eSPaolo Abeni */ 1384f970bd9eSPaolo Abeni rmem = atomic_add_return(size, &sk->sk_rmem_alloc); 1385363dc73aSPaolo Abeni if (rmem > (size + sk->sk_rcvbuf)) 1386f970bd9eSPaolo Abeni goto uncharge_drop; 1387f970bd9eSPaolo Abeni 1388f970bd9eSPaolo Abeni spin_lock(&list->lock); 1389f970bd9eSPaolo Abeni if (size >= sk->sk_forward_alloc) { 1390f970bd9eSPaolo Abeni amt = sk_mem_pages(size); 1391f970bd9eSPaolo Abeni delta = amt << SK_MEM_QUANTUM_SHIFT; 1392f970bd9eSPaolo Abeni if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { 1393f970bd9eSPaolo Abeni err = -ENOBUFS; 1394f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1395f970bd9eSPaolo Abeni goto uncharge_drop; 1396f970bd9eSPaolo Abeni } 1397f970bd9eSPaolo Abeni 1398f970bd9eSPaolo Abeni sk->sk_forward_alloc += delta; 1399f970bd9eSPaolo Abeni } 1400f970bd9eSPaolo Abeni 1401f970bd9eSPaolo Abeni sk->sk_forward_alloc -= size; 1402f970bd9eSPaolo Abeni 14037c13f97fSPaolo Abeni /* no need to setup a destructor, we will explicitly release the 14047c13f97fSPaolo Abeni * forward allocated memory on dequeue 14057c13f97fSPaolo Abeni */ 1406f970bd9eSPaolo Abeni sock_skb_set_dropcount(sk, skb); 1407f970bd9eSPaolo Abeni 1408f970bd9eSPaolo Abeni __skb_queue_tail(list, skb); 1409f970bd9eSPaolo Abeni spin_unlock(&list->lock); 1410f970bd9eSPaolo Abeni 1411f970bd9eSPaolo Abeni if (!sock_flag(sk, SOCK_DEAD)) 1412f970bd9eSPaolo Abeni sk->sk_data_ready(sk); 1413f970bd9eSPaolo Abeni 14144b272750SEric Dumazet busylock_release(busy); 1415f970bd9eSPaolo Abeni return 0; 1416f970bd9eSPaolo Abeni 1417f970bd9eSPaolo Abeni uncharge_drop: 1418f970bd9eSPaolo Abeni atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 1419f970bd9eSPaolo Abeni 1420f970bd9eSPaolo Abeni drop: 1421f970bd9eSPaolo Abeni atomic_inc(&sk->sk_drops); 14224b272750SEric Dumazet busylock_release(busy); 1423f970bd9eSPaolo Abeni return err; 1424f970bd9eSPaolo Abeni } 1425f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); 1426f970bd9eSPaolo Abeni 1427c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk) 1428f970bd9eSPaolo Abeni { 1429f970bd9eSPaolo Abeni /* reclaim completely the forward allocated memory */ 14302276f58aSPaolo Abeni struct udp_sock *up = udp_sk(sk); 14317c13f97fSPaolo Abeni unsigned int total = 0; 14327c13f97fSPaolo Abeni struct sk_buff *skb; 14337c13f97fSPaolo Abeni 14342276f58aSPaolo Abeni skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); 14352276f58aSPaolo Abeni while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { 14367c13f97fSPaolo Abeni total += skb->truesize; 14377c13f97fSPaolo Abeni kfree_skb(skb); 14387c13f97fSPaolo Abeni } 14396dfb4367SPaolo Abeni udp_rmem_release(sk, total, 0, true); 14407c13f97fSPaolo Abeni 1441f970bd9eSPaolo Abeni inet_sock_destruct(sk); 1442f970bd9eSPaolo Abeni } 1443c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock); 1444f970bd9eSPaolo Abeni 1445f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk) 1446f970bd9eSPaolo Abeni { 14472276f58aSPaolo Abeni skb_queue_head_init(&udp_sk(sk)->reader_queue); 1448f970bd9eSPaolo Abeni sk->sk_destruct = udp_destruct_sock; 1449f970bd9eSPaolo Abeni return 0; 1450f970bd9eSPaolo Abeni } 1451f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock); 1452f970bd9eSPaolo Abeni 1453f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) 1454f970bd9eSPaolo Abeni { 1455f970bd9eSPaolo Abeni if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { 1456f970bd9eSPaolo Abeni bool slow = lock_sock_fast(sk); 1457f970bd9eSPaolo Abeni 1458f970bd9eSPaolo Abeni sk_peek_offset_bwd(sk, len); 1459f970bd9eSPaolo Abeni unlock_sock_fast(sk, slow); 1460f970bd9eSPaolo Abeni } 14610a463c78SPaolo Abeni 1462ca2c1418SPaolo Abeni if (!skb_unref(skb)) 1463ca2c1418SPaolo Abeni return; 1464ca2c1418SPaolo Abeni 1465dce4551cSPaolo Abeni /* In the more common cases we cleared the head states previously, 1466dce4551cSPaolo Abeni * see __udp_queue_rcv_skb(). 14670ddf3fb2SPaolo Abeni */ 1468dce4551cSPaolo Abeni if (unlikely(udp_skb_has_head_state(skb))) 14690ddf3fb2SPaolo Abeni skb_release_head_state(skb); 1470ca2c1418SPaolo Abeni __consume_stateless_skb(skb); 1471f970bd9eSPaolo Abeni } 1472f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp); 1473f970bd9eSPaolo Abeni 14742276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk, 14752276f58aSPaolo Abeni struct sk_buff_head *rcvq, 14762276f58aSPaolo Abeni int *total) 14772276f58aSPaolo Abeni { 14782276f58aSPaolo Abeni struct sk_buff *skb; 14792276f58aSPaolo Abeni 14809bd780f5SPaolo Abeni while ((skb = skb_peek(rcvq)) != NULL) { 14819bd780f5SPaolo Abeni if (udp_lib_checksum_complete(skb)) { 14822276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, 14832276f58aSPaolo Abeni IS_UDPLITE(sk)); 14842276f58aSPaolo Abeni __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 14852276f58aSPaolo Abeni IS_UDPLITE(sk)); 14862276f58aSPaolo Abeni atomic_inc(&sk->sk_drops); 14872276f58aSPaolo Abeni __skb_unlink(skb, rcvq); 14882276f58aSPaolo Abeni *total += skb->truesize; 14892276f58aSPaolo Abeni kfree_skb(skb); 14909bd780f5SPaolo Abeni } else { 14919bd780f5SPaolo Abeni /* the csum related bits could be changed, refresh 14929bd780f5SPaolo Abeni * the scratch area 14939bd780f5SPaolo Abeni */ 14949bd780f5SPaolo Abeni udp_set_dev_scratch(skb); 14959bd780f5SPaolo Abeni break; 14969bd780f5SPaolo Abeni } 14972276f58aSPaolo Abeni } 14982276f58aSPaolo Abeni return skb; 14992276f58aSPaolo Abeni } 15002276f58aSPaolo Abeni 150185584672SEric Dumazet /** 150285584672SEric Dumazet * first_packet_length - return length of first packet in receive queue 150385584672SEric Dumazet * @sk: socket 150485584672SEric Dumazet * 150585584672SEric Dumazet * Drops all bad checksum frames, until a valid one is found. 1506e83c6744SEric Dumazet * Returns the length of found skb, or -1 if none is found. 150785584672SEric Dumazet */ 1508e83c6744SEric Dumazet static int first_packet_length(struct sock *sk) 150985584672SEric Dumazet { 15102276f58aSPaolo Abeni struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; 15112276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 151285584672SEric Dumazet struct sk_buff *skb; 15137c13f97fSPaolo Abeni int total = 0; 1514e83c6744SEric Dumazet int res; 151585584672SEric Dumazet 151685584672SEric Dumazet spin_lock_bh(&rcvq->lock); 15172276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 15182276f58aSPaolo Abeni if (!skb && !skb_queue_empty(sk_queue)) { 15192276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 15202276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, rcvq); 15212276f58aSPaolo Abeni spin_unlock(&sk_queue->lock); 15222276f58aSPaolo Abeni 15232276f58aSPaolo Abeni skb = __first_packet_length(sk, rcvq, &total); 152485584672SEric Dumazet } 1525e83c6744SEric Dumazet res = skb ? skb->len : -1; 15267c13f97fSPaolo Abeni if (total) 15276dfb4367SPaolo Abeni udp_rmem_release(sk, total, 1, false); 152885584672SEric Dumazet spin_unlock_bh(&rcvq->lock); 152985584672SEric Dumazet return res; 153085584672SEric Dumazet } 153185584672SEric Dumazet 15321da177e4SLinus Torvalds /* 15331da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 15341da177e4SLinus Torvalds */ 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 15371da177e4SLinus Torvalds { 15386516c655SStephen Hemminger switch (cmd) { 15391da177e4SLinus Torvalds case SIOCOUTQ: 15401da177e4SLinus Torvalds { 154131e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 154231e6d363SEric Dumazet 15431da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds case SIOCINQ: 15471da177e4SLinus Torvalds { 1548e83c6744SEric Dumazet int amount = max_t(int, 0, first_packet_length(sk)); 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds default: 15541da177e4SLinus Torvalds return -ENOIOCTLCMD; 15551da177e4SLinus Torvalds } 15566516c655SStephen Hemminger 15576516c655SStephen Hemminger return 0; 15581da177e4SLinus Torvalds } 1559c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl); 15601da177e4SLinus Torvalds 15612276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, 15622276f58aSPaolo Abeni int noblock, int *peeked, int *off, int *err) 15632276f58aSPaolo Abeni { 15642276f58aSPaolo Abeni struct sk_buff_head *sk_queue = &sk->sk_receive_queue; 15652276f58aSPaolo Abeni struct sk_buff_head *queue; 15662276f58aSPaolo Abeni struct sk_buff *last; 15672276f58aSPaolo Abeni long timeo; 15682276f58aSPaolo Abeni int error; 15692276f58aSPaolo Abeni 15702276f58aSPaolo Abeni queue = &udp_sk(sk)->reader_queue; 15712276f58aSPaolo Abeni flags |= noblock ? MSG_DONTWAIT : 0; 15722276f58aSPaolo Abeni timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 15732276f58aSPaolo Abeni do { 15742276f58aSPaolo Abeni struct sk_buff *skb; 15752276f58aSPaolo Abeni 15762276f58aSPaolo Abeni error = sock_error(sk); 15772276f58aSPaolo Abeni if (error) 15782276f58aSPaolo Abeni break; 15792276f58aSPaolo Abeni 15802276f58aSPaolo Abeni error = -EAGAIN; 15812276f58aSPaolo Abeni *peeked = 0; 15822276f58aSPaolo Abeni do { 15832276f58aSPaolo Abeni spin_lock_bh(&queue->lock); 15842276f58aSPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, 15852276f58aSPaolo Abeni udp_skb_destructor, 1586de321ed3SAndrey Vagin peeked, off, err, 15872276f58aSPaolo Abeni &last); 15882276f58aSPaolo Abeni if (skb) { 15892276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 15902276f58aSPaolo Abeni return skb; 15912276f58aSPaolo Abeni } 15922276f58aSPaolo Abeni 15932276f58aSPaolo Abeni if (skb_queue_empty(sk_queue)) { 15942276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 15952276f58aSPaolo Abeni goto busy_check; 15962276f58aSPaolo Abeni } 15972276f58aSPaolo Abeni 15986dfb4367SPaolo Abeni /* refill the reader queue and walk it again 15996dfb4367SPaolo Abeni * keep both queues locked to avoid re-acquiring 16006dfb4367SPaolo Abeni * the sk_receive_queue lock if fwd memory scheduling 16016dfb4367SPaolo Abeni * is needed. 16026dfb4367SPaolo Abeni */ 16032276f58aSPaolo Abeni spin_lock(&sk_queue->lock); 16042276f58aSPaolo Abeni skb_queue_splice_tail_init(sk_queue, queue); 16052276f58aSPaolo Abeni 16062276f58aSPaolo Abeni skb = __skb_try_recv_from_queue(sk, queue, flags, 16076dfb4367SPaolo Abeni udp_skb_dtor_locked, 1608de321ed3SAndrey Vagin peeked, off, err, 16092276f58aSPaolo Abeni &last); 16106dfb4367SPaolo Abeni spin_unlock(&sk_queue->lock); 16112276f58aSPaolo Abeni spin_unlock_bh(&queue->lock); 1612de321ed3SAndrey Vagin if (skb) 16132276f58aSPaolo Abeni return skb; 16142276f58aSPaolo Abeni 16152276f58aSPaolo Abeni busy_check: 16162276f58aSPaolo Abeni if (!sk_can_busy_loop(sk)) 16172276f58aSPaolo Abeni break; 16182276f58aSPaolo Abeni 16192276f58aSPaolo Abeni sk_busy_loop(sk, flags & MSG_DONTWAIT); 16202276f58aSPaolo Abeni } while (!skb_queue_empty(sk_queue)); 16212276f58aSPaolo Abeni 16222276f58aSPaolo Abeni /* sk_queue is empty, reader_queue may contain peeked packets */ 16232276f58aSPaolo Abeni } while (timeo && 16242276f58aSPaolo Abeni !__skb_wait_for_more_packets(sk, &error, &timeo, 16252276f58aSPaolo Abeni (struct sk_buff *)sk_queue)); 16262276f58aSPaolo Abeni 16272276f58aSPaolo Abeni *err = error; 16282276f58aSPaolo Abeni return NULL; 16292276f58aSPaolo Abeni } 16302276f58aSPaolo Abeni EXPORT_SYMBOL_GPL(__skb_recv_udp); 16312276f58aSPaolo Abeni 1632db8dac20SDavid S. Miller /* 1633db8dac20SDavid S. Miller * This should be easy, if there is something there we 1634db8dac20SDavid S. Miller * return it, otherwise we block. 1635db8dac20SDavid S. Miller */ 1636db8dac20SDavid S. Miller 16371b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, 16381b784140SYing Xue int flags, int *addr_len) 1639db8dac20SDavid S. Miller { 1640db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1641342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1642db8dac20SDavid S. Miller struct sk_buff *skb; 164359c2cdaeSDavid S. Miller unsigned int ulen, copied; 1644627d2d6bSsamanthakumar int peeked, peeking, off; 1645db8dac20SDavid S. Miller int err; 1646db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1647197c949eSEric Dumazet bool checksum_valid = false; 1648db8dac20SDavid S. Miller 1649db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 165085fbaa75SHannes Frederic Sowa return ip_recv_error(sk, msg, len, addr_len); 1651db8dac20SDavid S. Miller 1652db8dac20SDavid S. Miller try_again: 1653a0917e0bSMatthew Dawson peeking = flags & MSG_PEEK; 1654a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 16557c13f97fSPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err); 1656db8dac20SDavid S. Miller if (!skb) 1657627d2d6bSsamanthakumar return err; 1658db8dac20SDavid S. Miller 1659b65ac446SPaolo Abeni ulen = udp_skb_len(skb); 166059c2cdaeSDavid S. Miller copied = len; 1661627d2d6bSsamanthakumar if (copied > ulen - off) 1662627d2d6bSsamanthakumar copied = ulen - off; 166359c2cdaeSDavid S. Miller else if (copied < ulen) 1664db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 1665db8dac20SDavid S. Miller 1666db8dac20SDavid S. Miller /* 1667db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 1668db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 1669db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 1670db8dac20SDavid S. Miller */ 1671db8dac20SDavid S. Miller 1672d21dbdfeSEric Dumazet if (copied < ulen || peeking || 1673d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 1674b65ac446SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 1675b65ac446SPaolo Abeni !__udp_lib_checksum_complete(skb); 1676197c949eSEric Dumazet if (!checksum_valid) 1677db8dac20SDavid S. Miller goto csum_copy_err; 1678db8dac20SDavid S. Miller } 1679db8dac20SDavid S. Miller 1680b65ac446SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 1681b65ac446SPaolo Abeni if (udp_skb_is_linear(skb)) 1682b65ac446SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 1683b65ac446SPaolo Abeni else 1684627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 1685b65ac446SPaolo Abeni } else { 1686627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 1687db8dac20SDavid S. Miller 1688db8dac20SDavid S. Miller if (err == -EINVAL) 1689db8dac20SDavid S. Miller goto csum_copy_err; 1690db8dac20SDavid S. Miller } 1691db8dac20SDavid S. Miller 169222911fc5SEric Dumazet if (unlikely(err)) { 1693979402b1SEric Dumazet if (!peeked) { 1694979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 16956aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1696979402b1SEric Dumazet UDP_MIB_INERRORS, is_udplite); 1697979402b1SEric Dumazet } 1698850cbaddSPaolo Abeni kfree_skb(skb); 1699627d2d6bSsamanthakumar return err; 170022911fc5SEric Dumazet } 1701db8dac20SDavid S. Miller 1702db8dac20SDavid S. Miller if (!peeked) 17036aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 1704629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 1705db8dac20SDavid S. Miller 17063b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 1707db8dac20SDavid S. Miller 1708db8dac20SDavid S. Miller /* Copy the address. */ 1709c482c568SEric Dumazet if (sin) { 1710db8dac20SDavid S. Miller sin->sin_family = AF_INET; 1711db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 1712db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1713db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1714bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 1715db8dac20SDavid S. Miller } 1716db8dac20SDavid S. Miller if (inet->cmsg_flags) 1717ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); 1718db8dac20SDavid S. Miller 171959c2cdaeSDavid S. Miller err = copied; 1720db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 1721db8dac20SDavid S. Miller err = ulen; 1722db8dac20SDavid S. Miller 1723850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 1724db8dac20SDavid S. Miller return err; 1725db8dac20SDavid S. Miller 1726db8dac20SDavid S. Miller csum_copy_err: 17272276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 17282276f58aSPaolo Abeni udp_skb_destructor)) { 17296aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 17306aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 17316a5dc9e5SEric Dumazet } 1732850cbaddSPaolo Abeni kfree_skb(skb); 1733db8dac20SDavid S. Miller 1734beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 1735beb39db5SEric Dumazet cond_resched(); 17369cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 1737db8dac20SDavid S. Miller goto try_again; 1738db8dac20SDavid S. Miller } 1739db8dac20SDavid S. Miller 1740d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) 1741d74bad4eSAndrey Ignatov { 1742d74bad4eSAndrey Ignatov /* This check is replicated from __ip4_datagram_connect() and 1743d74bad4eSAndrey Ignatov * intended to prevent BPF program called below from accessing bytes 1744d74bad4eSAndrey Ignatov * that are out of the bound specified by user in addr_len. 1745d74bad4eSAndrey Ignatov */ 1746d74bad4eSAndrey Ignatov if (addr_len < sizeof(struct sockaddr_in)) 1747d74bad4eSAndrey Ignatov return -EINVAL; 1748d74bad4eSAndrey Ignatov 1749d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); 1750d74bad4eSAndrey Ignatov } 1751d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect); 1752d74bad4eSAndrey Ignatov 1753286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags) 17541da177e4SLinus Torvalds { 17551da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 17561da177e4SLinus Torvalds /* 17571da177e4SLinus Torvalds * 1003.1g - break association. 17581da177e4SLinus Torvalds */ 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 1761c720c7e8SEric Dumazet inet->inet_daddr = 0; 1762c720c7e8SEric Dumazet inet->inet_dport = 0; 1763bdeab991STom Herbert sock_rps_reset_rxhash(sk); 17641da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 17651da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 17661da177e4SLinus Torvalds inet_reset_saddr(sk); 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 17691da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 1770c720c7e8SEric Dumazet inet->inet_sport = 0; 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds sk_dst_reset(sk); 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds } 1775286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect); 1776286c72deSEric Dumazet 1777286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags) 1778286c72deSEric Dumazet { 1779286c72deSEric Dumazet lock_sock(sk); 1780286c72deSEric Dumazet __udp_disconnect(sk, flags); 1781286c72deSEric Dumazet release_sock(sk); 1782286c72deSEric Dumazet return 0; 1783286c72deSEric Dumazet } 1784c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect); 17851da177e4SLinus Torvalds 1786645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 1787645ca708SEric Dumazet { 1788723b4610SEric Dumazet if (sk_hashed(sk)) { 1789645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1790512615b6SEric Dumazet struct udp_hslot *hslot, *hslot2; 1791512615b6SEric Dumazet 1792512615b6SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1793d4cada4aSEric Dumazet udp_sk(sk)->udp_port_hash); 1794512615b6SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1795645ca708SEric Dumazet 1796c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 1797e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1798e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1799ca065d0cSEric Dumazet if (sk_del_node_init_rcu(sk)) { 1800fdcc8aa9SEric Dumazet hslot->count--; 1801c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 1802645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1803512615b6SEric Dumazet 1804512615b6SEric Dumazet spin_lock(&hslot2->lock); 1805ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1806512615b6SEric Dumazet hslot2->count--; 1807512615b6SEric Dumazet spin_unlock(&hslot2->lock); 1808645ca708SEric Dumazet } 1809c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 1810645ca708SEric Dumazet } 1811723b4610SEric Dumazet } 1812645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 1813645ca708SEric Dumazet 1814719f8358SEric Dumazet /* 1815719f8358SEric Dumazet * inet_rcv_saddr was changed, we must rehash secondary hash 1816719f8358SEric Dumazet */ 1817719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash) 1818719f8358SEric Dumazet { 1819719f8358SEric Dumazet if (sk_hashed(sk)) { 1820719f8358SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1821719f8358SEric Dumazet struct udp_hslot *hslot, *hslot2, *nhslot2; 1822719f8358SEric Dumazet 1823719f8358SEric Dumazet hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1824719f8358SEric Dumazet nhslot2 = udp_hashslot2(udptable, newhash); 1825719f8358SEric Dumazet udp_sk(sk)->udp_portaddr_hash = newhash; 1826e32ea7e7SCraig Gallek 1827e32ea7e7SCraig Gallek if (hslot2 != nhslot2 || 1828e32ea7e7SCraig Gallek rcu_access_pointer(sk->sk_reuseport_cb)) { 1829719f8358SEric Dumazet hslot = udp_hashslot(udptable, sock_net(sk), 1830719f8358SEric Dumazet udp_sk(sk)->udp_port_hash); 1831719f8358SEric Dumazet /* we must lock primary chain too */ 1832719f8358SEric Dumazet spin_lock_bh(&hslot->lock); 1833e32ea7e7SCraig Gallek if (rcu_access_pointer(sk->sk_reuseport_cb)) 1834e32ea7e7SCraig Gallek reuseport_detach_sock(sk); 1835719f8358SEric Dumazet 1836e32ea7e7SCraig Gallek if (hslot2 != nhslot2) { 1837719f8358SEric Dumazet spin_lock(&hslot2->lock); 1838ca065d0cSEric Dumazet hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1839719f8358SEric Dumazet hslot2->count--; 1840719f8358SEric Dumazet spin_unlock(&hslot2->lock); 1841719f8358SEric Dumazet 1842719f8358SEric Dumazet spin_lock(&nhslot2->lock); 1843ca065d0cSEric Dumazet hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 1844719f8358SEric Dumazet &nhslot2->head); 1845719f8358SEric Dumazet nhslot2->count++; 1846719f8358SEric Dumazet spin_unlock(&nhslot2->lock); 1847e32ea7e7SCraig Gallek } 1848719f8358SEric Dumazet 1849719f8358SEric Dumazet spin_unlock_bh(&hslot->lock); 1850719f8358SEric Dumazet } 1851719f8358SEric Dumazet } 1852719f8358SEric Dumazet } 1853719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash); 1854719f8358SEric Dumazet 1855719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk) 1856719f8358SEric Dumazet { 1857f0b1e64cSMartin KaFai Lau u16 new_hash = ipv4_portaddr_hash(sock_net(sk), 1858719f8358SEric Dumazet inet_sk(sk)->inet_rcv_saddr, 1859719f8358SEric Dumazet inet_sk(sk)->inet_num); 1860719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 1861719f8358SEric Dumazet } 1862719f8358SEric Dumazet 1863a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 186493821778SHerbert Xu { 1865fec5e652STom Herbert int rc; 186693821778SHerbert Xu 1867005ec974SShawn Bohrer if (inet_sk(sk)->inet_daddr) { 1868bdeab991STom Herbert sock_rps_save_rxhash(sk, skb); 1869005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 18702c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 1871e68b6e50SEric Dumazet } else { 1872e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 1873005ec974SShawn Bohrer } 1874fec5e652STom Herbert 1875850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 1876766e9037SEric Dumazet if (rc < 0) { 1877766e9037SEric Dumazet int is_udplite = IS_UDPLITE(sk); 1878766e9037SEric Dumazet 187993821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 1880766e9037SEric Dumazet if (rc == -ENOMEM) 1881e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, 188293821778SHerbert Xu is_udplite); 1883e61da9e2SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1884766e9037SEric Dumazet kfree_skb(skb); 1885296f7ea7SSatoru Moriya trace_udp_fail_queue_rcv_skb(rc, sk); 1886766e9037SEric Dumazet return -1; 188793821778SHerbert Xu } 188893821778SHerbert Xu 188993821778SHerbert Xu return 0; 189093821778SHerbert Xu } 189193821778SHerbert Xu 189288ab3108SDavidlohr Bueso static DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); 1893447167bfSEric Dumazet void udp_encap_enable(void) 1894447167bfSEric Dumazet { 189588ab3108SDavidlohr Bueso static_branch_enable(&udp_encap_needed_key); 1896447167bfSEric Dumazet } 1897447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable); 1898447167bfSEric Dumazet 1899db8dac20SDavid S. Miller /* returns: 1900db8dac20SDavid S. Miller * -1: error 1901db8dac20SDavid S. Miller * 0: success 1902db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 1903db8dac20SDavid S. Miller * 1904db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 1905db8dac20SDavid S. Miller * have either been requeued or freed. 1906db8dac20SDavid S. Miller */ 1907a3f96c47SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1908db8dac20SDavid S. Miller { 1909db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1910db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1911db8dac20SDavid S. Miller 1912db8dac20SDavid S. Miller /* 1913db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 1914db8dac20SDavid S. Miller */ 1915db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 1916db8dac20SDavid S. Miller goto drop; 1917db8dac20SDavid S. Miller nf_reset(skb); 1918db8dac20SDavid S. Miller 191988ab3108SDavidlohr Bueso if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 19200ad92ad0SEric Dumazet int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 19210ad92ad0SEric Dumazet 1922db8dac20SDavid S. Miller /* 1923db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 1924db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 1925db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 1926db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 1927db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 1928db8dac20SDavid S. Miller * handler or was discarded by it. 1929db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 1930db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 1931db8dac20SDavid S. Miller */ 1932db8dac20SDavid S. Miller 1933db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 19346aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 1935e5aed006SHannes Frederic Sowa if (encap_rcv) { 1936db8dac20SDavid S. Miller int ret; 1937db8dac20SDavid S. Miller 19380a80966bSTom Herbert /* Verify checksum before giving to encap */ 19390a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 19400a80966bSTom Herbert goto csum_error; 19410a80966bSTom Herbert 19420ad92ad0SEric Dumazet ret = encap_rcv(sk, skb); 1943db8dac20SDavid S. Miller if (ret <= 0) { 194402c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 19450283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 1946db8dac20SDavid S. Miller is_udplite); 1947db8dac20SDavid S. Miller return -ret; 1948db8dac20SDavid S. Miller } 1949db8dac20SDavid S. Miller } 1950db8dac20SDavid S. Miller 1951db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1952db8dac20SDavid S. Miller } 1953db8dac20SDavid S. Miller 1954db8dac20SDavid S. Miller /* 1955db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1956db8dac20SDavid S. Miller */ 1957db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1958db8dac20SDavid S. Miller 1959db8dac20SDavid S. Miller /* 1960db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1961db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1962db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1963db8dac20SDavid S. Miller * protocol stack as such. 1964db8dac20SDavid S. Miller * 1965db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1966db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1967db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1968db8dac20SDavid S. Miller * provided by the application." 1969db8dac20SDavid S. Miller */ 1970db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1971ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", 1972db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1973db8dac20SDavid S. Miller goto drop; 1974db8dac20SDavid S. Miller } 1975db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1976db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1977db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1978db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1979db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1980db8dac20SDavid S. Miller */ 1981db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1982ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", 1983db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1984db8dac20SDavid S. Miller goto drop; 1985db8dac20SDavid S. Miller } 1986db8dac20SDavid S. Miller } 1987db8dac20SDavid S. Miller 1988dd99e425SPaolo Abeni prefetch(&sk->sk_rmem_alloc); 1989ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 1990ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 19916a5dc9e5SEric Dumazet goto csum_error; 1992ce25d66aSEric Dumazet 1993ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 1994a6127697SMichal Kubeček goto drop; 1995db8dac20SDavid S. Miller 1996e6afc8acSsamanthakumar udp_csum_pull_header(skb); 1997db8dac20SDavid S. Miller 1998fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 1999850cbaddSPaolo Abeni return __udp_queue_rcv_skb(sk, skb); 2000db8dac20SDavid S. Miller 20016a5dc9e5SEric Dumazet csum_error: 200202c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 2003db8dac20SDavid S. Miller drop: 200402c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 20058edf19c2SEric Dumazet atomic_inc(&sk->sk_drops); 2006db8dac20SDavid S. Miller kfree_skb(skb); 2007db8dac20SDavid S. Miller return -1; 2008db8dac20SDavid S. Miller } 2009db8dac20SDavid S. Miller 201097502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock 2011e47eb5dfSEric Dumazet * For UDP, we use xchg() to guard against concurrent changes. 201297502231SEric Dumazet */ 201364f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 2014421b3885SShawn Bohrer { 201597502231SEric Dumazet struct dst_entry *old; 2016421b3885SShawn Bohrer 2017d24406c8SWei Wang if (dst_hold_safe(dst)) { 2018e47eb5dfSEric Dumazet old = xchg(&sk->sk_rx_dst, dst); 201997502231SEric Dumazet dst_release(old); 202064f0f5d1SPaolo Abeni return old != dst; 202197502231SEric Dumazet } 202264f0f5d1SPaolo Abeni return false; 2023d24406c8SWei Wang } 2024c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set); 2025421b3885SShawn Bohrer 2026db8dac20SDavid S. Miller /* 2027db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 2028db8dac20SDavid S. Miller * 20291240d137SEric Dumazet * Note: called only from the BH handler context. 2030db8dac20SDavid S. Miller */ 2031e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 2032db8dac20SDavid S. Miller struct udphdr *uh, 2033db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 203436cbb245SRick Jones struct udp_table *udptable, 203536cbb245SRick Jones int proto) 2036db8dac20SDavid S. Miller { 2037ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 20385cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 20395cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 20402dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 2041ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 2042ca065d0cSEric Dumazet int dif = skb->dev->ifindex; 2043fb74c277SDavid Ahern int sdif = inet_sdif(skb); 2044ca065d0cSEric Dumazet struct hlist_node *node; 2045ca065d0cSEric Dumazet struct sk_buff *nskb; 20462dc41cffSDavid Held 20472dc41cffSDavid Held if (use_hash2) { 2048f0b1e64cSMartin KaFai Lau hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & 204973e2d5e3SPablo Neira udptable->mask; 2050f0b1e64cSMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; 20512dc41cffSDavid Held start_lookup: 205273e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 20532dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 20542dc41cffSDavid Held } 2055db8dac20SDavid S. Miller 2056ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 2057ca065d0cSEric Dumazet if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, 2058fb74c277SDavid Ahern uh->source, saddr, dif, sdif, hnum)) 2059ca065d0cSEric Dumazet continue; 20601240d137SEric Dumazet 2061ca065d0cSEric Dumazet if (!first) { 2062ca065d0cSEric Dumazet first = sk; 2063ca065d0cSEric Dumazet continue; 2064ca065d0cSEric Dumazet } 2065ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 2066ca065d0cSEric Dumazet 2067ca065d0cSEric Dumazet if (unlikely(!nskb)) { 2068ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 206902c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 2070ca065d0cSEric Dumazet IS_UDPLITE(sk)); 207102c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, 2072ca065d0cSEric Dumazet IS_UDPLITE(sk)); 2073ca065d0cSEric Dumazet continue; 2074ca065d0cSEric Dumazet } 2075ca065d0cSEric Dumazet if (udp_queue_rcv_skb(sk, nskb) > 0) 2076ca065d0cSEric Dumazet consume_skb(nskb); 2077ca065d0cSEric Dumazet } 20781240d137SEric Dumazet 20792dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 20802dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 20812dc41cffSDavid Held hash2 = hash2_any; 20822dc41cffSDavid Held goto start_lookup; 20832dc41cffSDavid Held } 20842dc41cffSDavid Held 2085ca065d0cSEric Dumazet if (first) { 2086ca065d0cSEric Dumazet if (udp_queue_rcv_skb(first, skb) > 0) 2087ca065d0cSEric Dumazet consume_skb(skb); 20881240d137SEric Dumazet } else { 2089ca065d0cSEric Dumazet kfree_skb(skb); 209002c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 209136cbb245SRick Jones proto == IPPROTO_UDPLITE); 20921240d137SEric Dumazet } 2093db8dac20SDavid S. Miller return 0; 2094db8dac20SDavid S. Miller } 2095db8dac20SDavid S. Miller 2096db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 2097db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 2098db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 2099db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 2100db8dac20SDavid S. Miller */ 2101db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 2102db8dac20SDavid S. Miller int proto) 2103db8dac20SDavid S. Miller { 2104db8dac20SDavid S. Miller int err; 2105db8dac20SDavid S. Miller 2106db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 2107db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 2108db8dac20SDavid S. Miller 2109db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 2110db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 2111db8dac20SDavid S. Miller if (err) 2112db8dac20SDavid S. Miller return err; 211315f35d49SAlexey Kodanev 211415f35d49SAlexey Kodanev if (UDP_SKB_CB(skb)->partial_cov) { 211515f35d49SAlexey Kodanev skb->csum = inet_compute_pseudo(skb, proto); 211615f35d49SAlexey Kodanev return 0; 211715f35d49SAlexey Kodanev } 2118db8dac20SDavid S. Miller } 2119db8dac20SDavid S. Miller 2120b46d9f62SHannes Frederic Sowa /* Note, we are only interested in != 0 or == 0, thus the 2121b46d9f62SHannes Frederic Sowa * force to int. 2122b46d9f62SHannes Frederic Sowa */ 2123b46d9f62SHannes Frederic Sowa return (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, 2124ed70fcfcSTom Herbert inet_compute_pseudo); 2125db8dac20SDavid S. Miller } 2126db8dac20SDavid S. Miller 2127*2b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 2128*2b5a9217SPaolo Abeni * return code conversion for ip layer consumption 2129*2b5a9217SPaolo Abeni */ 2130*2b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 2131*2b5a9217SPaolo Abeni struct udphdr *uh) 2132*2b5a9217SPaolo Abeni { 2133*2b5a9217SPaolo Abeni int ret; 2134*2b5a9217SPaolo Abeni 2135*2b5a9217SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 2136*2b5a9217SPaolo Abeni skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, 2137*2b5a9217SPaolo Abeni inet_compute_pseudo); 2138*2b5a9217SPaolo Abeni 2139*2b5a9217SPaolo Abeni ret = udp_queue_rcv_skb(sk, skb); 2140*2b5a9217SPaolo Abeni 2141*2b5a9217SPaolo Abeni /* a return value > 0 means to resubmit the input, but 2142*2b5a9217SPaolo Abeni * it wants the return to be -protocol, or 0 2143*2b5a9217SPaolo Abeni */ 2144*2b5a9217SPaolo Abeni if (ret > 0) 2145*2b5a9217SPaolo Abeni return -ret; 2146*2b5a9217SPaolo Abeni return 0; 2147*2b5a9217SPaolo Abeni } 2148*2b5a9217SPaolo Abeni 2149db8dac20SDavid S. Miller /* 2150db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 2151db8dac20SDavid S. Miller */ 2152db8dac20SDavid S. Miller 2153645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 2154db8dac20SDavid S. Miller int proto) 2155db8dac20SDavid S. Miller { 2156db8dac20SDavid S. Miller struct sock *sk; 21577b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 2158db8dac20SDavid S. Miller unsigned short ulen; 2159adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 21602783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 21610283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 2162db8dac20SDavid S. Miller 2163db8dac20SDavid S. Miller /* 2164db8dac20SDavid S. Miller * Validate the packet. 2165db8dac20SDavid S. Miller */ 2166db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 2167db8dac20SDavid S. Miller goto drop; /* No space for header. */ 2168db8dac20SDavid S. Miller 21697b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 2170db8dac20SDavid S. Miller ulen = ntohs(uh->len); 2171ccc2d97cSBjørn Mork saddr = ip_hdr(skb)->saddr; 2172ccc2d97cSBjørn Mork daddr = ip_hdr(skb)->daddr; 2173ccc2d97cSBjørn Mork 2174db8dac20SDavid S. Miller if (ulen > skb->len) 2175db8dac20SDavid S. Miller goto short_packet; 2176db8dac20SDavid S. Miller 2177db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 2178db8dac20SDavid S. Miller /* UDP validates ulen. */ 2179db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 2180db8dac20SDavid S. Miller goto short_packet; 2181db8dac20SDavid S. Miller uh = udp_hdr(skb); 2182db8dac20SDavid S. Miller } 2183db8dac20SDavid S. Miller 2184db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 2185db8dac20SDavid S. Miller goto csum_error; 2186db8dac20SDavid S. Miller 21878afdd99aSEric Dumazet sk = skb_steal_sock(skb); 21888afdd99aSEric Dumazet if (sk) { 218997502231SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2190421b3885SShawn Bohrer int ret; 2191421b3885SShawn Bohrer 219297502231SEric Dumazet if (unlikely(sk->sk_rx_dst != dst)) 219397502231SEric Dumazet udp_sk_rx_dst_set(sk, dst); 2194421b3885SShawn Bohrer 2195*2b5a9217SPaolo Abeni ret = udp_unicast_rcv_skb(sk, skb, uh); 21968afdd99aSEric Dumazet sock_put(sk); 2197*2b5a9217SPaolo Abeni return ret; 2198c18450a5SFabian Frederick } 2199c18450a5SFabian Frederick 2200db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 2201e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 220236cbb245SRick Jones saddr, daddr, udptable, proto); 2203db8dac20SDavid S. Miller 2204607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 2205*2b5a9217SPaolo Abeni if (sk) 2206*2b5a9217SPaolo Abeni return udp_unicast_rcv_skb(sk, skb, uh); 2207db8dac20SDavid S. Miller 2208db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 2209db8dac20SDavid S. Miller goto drop; 2210db8dac20SDavid S. Miller nf_reset(skb); 2211db8dac20SDavid S. Miller 2212db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 2213db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 2214db8dac20SDavid S. Miller goto csum_error; 2215db8dac20SDavid S. Miller 221602c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 2217db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 2218db8dac20SDavid S. Miller 2219db8dac20SDavid S. Miller /* 2220db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 2221db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 2222db8dac20SDavid S. Miller */ 2223db8dac20SDavid S. Miller kfree_skb(skb); 2224db8dac20SDavid S. Miller return 0; 2225db8dac20SDavid S. Miller 2226db8dac20SDavid S. Miller short_packet: 2227ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 2228afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2229afd46503SJoe Perches &saddr, ntohs(uh->source), 2230afd46503SJoe Perches ulen, skb->len, 2231afd46503SJoe Perches &daddr, ntohs(uh->dest)); 2232db8dac20SDavid S. Miller goto drop; 2233db8dac20SDavid S. Miller 2234db8dac20SDavid S. Miller csum_error: 2235db8dac20SDavid S. Miller /* 2236db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 2237db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 2238db8dac20SDavid S. Miller */ 2239ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 2240afd46503SJoe Perches proto == IPPROTO_UDPLITE ? "Lite" : "", 2241afd46503SJoe Perches &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 2242db8dac20SDavid S. Miller ulen); 224302c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 2244db8dac20SDavid S. Miller drop: 224502c22347SEric Dumazet __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 2246db8dac20SDavid S. Miller kfree_skb(skb); 2247db8dac20SDavid S. Miller return 0; 2248db8dac20SDavid S. Miller } 2249db8dac20SDavid S. Miller 2250421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket. 2251421b3885SShawn Bohrer * If more than one socket found returns NULL 2252421b3885SShawn Bohrer */ 2253421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, 2254421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2255421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 2256fb74c277SDavid Ahern int dif, int sdif) 2257421b3885SShawn Bohrer { 2258421b3885SShawn Bohrer struct sock *sk, *result; 2259421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2260ca065d0cSEric Dumazet unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); 2261421b3885SShawn Bohrer struct udp_hslot *hslot = &udp_table.hash[slot]; 2262421b3885SShawn Bohrer 226363c6f81cSEric Dumazet /* Do not bother scanning a too big list */ 226463c6f81cSEric Dumazet if (hslot->count > 10) 226563c6f81cSEric Dumazet return NULL; 226663c6f81cSEric Dumazet 2267421b3885SShawn Bohrer result = NULL; 2268ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 2269ca065d0cSEric Dumazet if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, 2270fb74c277SDavid Ahern rmt_port, rmt_addr, dif, sdif, hnum)) { 2271ca065d0cSEric Dumazet if (result) 2272ca065d0cSEric Dumazet return NULL; 2273421b3885SShawn Bohrer result = sk; 2274421b3885SShawn Bohrer } 2275421b3885SShawn Bohrer } 2276421b3885SShawn Bohrer 2277421b3885SShawn Bohrer return result; 2278421b3885SShawn Bohrer } 2279421b3885SShawn Bohrer 2280421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can 2281421b3885SShawn Bohrer * break forwarding setups. The chains here can be long so only check 2282421b3885SShawn Bohrer * if the first socket is an exact match and if not move on. 2283421b3885SShawn Bohrer */ 2284421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net, 2285421b3885SShawn Bohrer __be16 loc_port, __be32 loc_addr, 2286421b3885SShawn Bohrer __be16 rmt_port, __be32 rmt_addr, 22873fa6f616SDavid Ahern int dif, int sdif) 2288421b3885SShawn Bohrer { 2289421b3885SShawn Bohrer unsigned short hnum = ntohs(loc_port); 2290f0b1e64cSMartin KaFai Lau unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); 2291421b3885SShawn Bohrer unsigned int slot2 = hash2 & udp_table.mask; 2292421b3885SShawn Bohrer struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 2293c7228317SJoe Perches INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); 2294421b3885SShawn Bohrer const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 2295ca065d0cSEric Dumazet struct sock *sk; 2296421b3885SShawn Bohrer 2297ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 2298ca065d0cSEric Dumazet if (INET_MATCH(sk, net, acookie, rmt_addr, 22993fa6f616SDavid Ahern loc_addr, ports, dif, sdif)) 2300ca065d0cSEric Dumazet return sk; 2301421b3885SShawn Bohrer /* Only check first socket in chain */ 2302421b3885SShawn Bohrer break; 2303421b3885SShawn Bohrer } 2304ca065d0cSEric Dumazet return NULL; 2305421b3885SShawn Bohrer } 2306421b3885SShawn Bohrer 23077487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb) 2308421b3885SShawn Bohrer { 2309610438b7SEric Dumazet struct net *net = dev_net(skb->dev); 2310bc044e8dSPaolo Abeni struct in_device *in_dev = NULL; 2311610438b7SEric Dumazet const struct iphdr *iph; 2312610438b7SEric Dumazet const struct udphdr *uh; 2313ca065d0cSEric Dumazet struct sock *sk = NULL; 2314421b3885SShawn Bohrer struct dst_entry *dst; 2315421b3885SShawn Bohrer int dif = skb->dev->ifindex; 2316fb74c277SDavid Ahern int sdif = inet_sdif(skb); 23176e540309SShawn Bohrer int ours; 2318421b3885SShawn Bohrer 2319421b3885SShawn Bohrer /* validate the packet */ 2320421b3885SShawn Bohrer if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) 23217487449cSPaolo Abeni return 0; 2322421b3885SShawn Bohrer 2323610438b7SEric Dumazet iph = ip_hdr(skb); 2324610438b7SEric Dumazet uh = udp_hdr(skb); 2325610438b7SEric Dumazet 2326996b44fcSPaolo Abeni if (skb->pkt_type == PACKET_MULTICAST) { 2327bc044e8dSPaolo Abeni in_dev = __in_dev_get_rcu(skb->dev); 23286e540309SShawn Bohrer 23296e540309SShawn Bohrer if (!in_dev) 23307487449cSPaolo Abeni return 0; 23316e540309SShawn Bohrer 23326e540309SShawn Bohrer ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, 23336e540309SShawn Bohrer iph->protocol); 23346e540309SShawn Bohrer if (!ours) 23357487449cSPaolo Abeni return 0; 2336ad0ea198SPaolo Abeni 2337421b3885SShawn Bohrer sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, 2338fb74c277SDavid Ahern uh->source, iph->saddr, 2339fb74c277SDavid Ahern dif, sdif); 23406e540309SShawn Bohrer } else if (skb->pkt_type == PACKET_HOST) { 2341421b3885SShawn Bohrer sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, 23423fa6f616SDavid Ahern uh->source, iph->saddr, dif, sdif); 23436e540309SShawn Bohrer } 2344421b3885SShawn Bohrer 234541c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 23467487449cSPaolo Abeni return 0; 2347421b3885SShawn Bohrer 2348421b3885SShawn Bohrer skb->sk = sk; 234982eabd9eSAlexander Duyck skb->destructor = sock_efree; 235010e2eb87SEric Dumazet dst = READ_ONCE(sk->sk_rx_dst); 2351421b3885SShawn Bohrer 2352421b3885SShawn Bohrer if (dst) 2353421b3885SShawn Bohrer dst = dst_check(dst, 0); 235410e2eb87SEric Dumazet if (dst) { 2355bc044e8dSPaolo Abeni u32 itag = 0; 2356bc044e8dSPaolo Abeni 2357d24406c8SWei Wang /* set noref for now. 2358d24406c8SWei Wang * any place which wants to hold dst has to call 2359d24406c8SWei Wang * dst_hold_safe() 2360d24406c8SWei Wang */ 2361421b3885SShawn Bohrer skb_dst_set_noref(skb, dst); 2362bc044e8dSPaolo Abeni 2363bc044e8dSPaolo Abeni /* for unconnected multicast sockets we need to validate 2364bc044e8dSPaolo Abeni * the source on each packet 2365bc044e8dSPaolo Abeni */ 2366bc044e8dSPaolo Abeni if (!inet_sk(sk)->inet_daddr && in_dev) 2367bc044e8dSPaolo Abeni return ip_mc_validate_source(skb, iph->daddr, 2368bc044e8dSPaolo Abeni iph->saddr, iph->tos, 2369bc044e8dSPaolo Abeni skb->dev, in_dev, &itag); 2370421b3885SShawn Bohrer } 23717487449cSPaolo Abeni return 0; 237210e2eb87SEric Dumazet } 2373421b3885SShawn Bohrer 2374db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 2375db8dac20SDavid S. Miller { 2376645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 2377db8dac20SDavid S. Miller } 2378db8dac20SDavid S. Miller 23797d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 2380db8dac20SDavid S. Miller { 238144046a59STom Parkin struct udp_sock *up = udp_sk(sk); 23828a74ad60SEric Dumazet bool slow = lock_sock_fast(sk); 2383db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 23848a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 238588ab3108SDavidlohr Bueso if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { 238644046a59STom Parkin void (*encap_destroy)(struct sock *sk); 23876aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 238844046a59STom Parkin if (encap_destroy) 238944046a59STom Parkin encap_destroy(sk); 239044046a59STom Parkin } 2391db8dac20SDavid S. Miller } 2392db8dac20SDavid S. Miller 23931da177e4SLinus Torvalds /* 23941da177e4SLinus Torvalds * Socket option code for UDP 23951da177e4SLinus Torvalds */ 23964c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 2397b7058842SDavid S. Miller char __user *optval, unsigned int optlen, 23984c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 23991da177e4SLinus Torvalds { 24001da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 24011c19448cSTom Herbert int val, valbool; 24021da177e4SLinus Torvalds int err = 0; 2403b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds if (optlen < sizeof(int)) 24061da177e4SLinus Torvalds return -EINVAL; 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 24091da177e4SLinus Torvalds return -EFAULT; 24101da177e4SLinus Torvalds 24111c19448cSTom Herbert valbool = val ? 1 : 0; 24121c19448cSTom Herbert 24131da177e4SLinus Torvalds switch (optname) { 24141da177e4SLinus Torvalds case UDP_CORK: 24151da177e4SLinus Torvalds if (val != 0) { 24161da177e4SLinus Torvalds up->corkflag = 1; 24171da177e4SLinus Torvalds } else { 24181da177e4SLinus Torvalds up->corkflag = 0; 24191da177e4SLinus Torvalds lock_sock(sk); 24204243cdc2SJoe Perches push_pending_frames(sk); 24211da177e4SLinus Torvalds release_sock(sk); 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds break; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds case UDP_ENCAP: 24261da177e4SLinus Torvalds switch (val) { 24271da177e4SLinus Torvalds case 0: 24281da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 24291da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 2430067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 2431067b207bSJames Chapman /* FALLTHROUGH */ 2432342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 24331da177e4SLinus Torvalds up->encap_type = val; 2434447167bfSEric Dumazet udp_encap_enable(); 24351da177e4SLinus Torvalds break; 24361da177e4SLinus Torvalds default: 24371da177e4SLinus Torvalds err = -ENOPROTOOPT; 24381da177e4SLinus Torvalds break; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds break; 24411da177e4SLinus Torvalds 24421c19448cSTom Herbert case UDP_NO_CHECK6_TX: 24431c19448cSTom Herbert up->no_check6_tx = valbool; 24441c19448cSTom Herbert break; 24451c19448cSTom Herbert 24461c19448cSTom Herbert case UDP_NO_CHECK6_RX: 24471c19448cSTom Herbert up->no_check6_rx = valbool; 24481c19448cSTom Herbert break; 24491c19448cSTom Herbert 2450bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2451bec1f6f6SWillem de Bruijn if (val < 0 || val > USHRT_MAX) 2452bec1f6f6SWillem de Bruijn return -EINVAL; 2453bec1f6f6SWillem de Bruijn up->gso_size = val; 2454bec1f6f6SWillem de Bruijn break; 2455bec1f6f6SWillem de Bruijn 2456ba4e58ecSGerrit Renker /* 2457ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 2458ba4e58ecSGerrit Renker */ 2459ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 2460ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 2461ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2462b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2463ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2464ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 2465ba4e58ecSGerrit Renker val = 8; 24664be929beSAlexey Dobriyan else if (val > USHRT_MAX) 24674be929beSAlexey Dobriyan val = USHRT_MAX; 2468ba4e58ecSGerrit Renker up->pcslen = val; 2469ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 2470ba4e58ecSGerrit Renker break; 2471ba4e58ecSGerrit Renker 2472ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 2473ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 2474ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 2475ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2476b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 2477ba4e58ecSGerrit Renker return -ENOPROTOOPT; 2478ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 2479ba4e58ecSGerrit Renker val = 8; 24804be929beSAlexey Dobriyan else if (val > USHRT_MAX) 24814be929beSAlexey Dobriyan val = USHRT_MAX; 2482ba4e58ecSGerrit Renker up->pcrlen = val; 2483ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 2484ba4e58ecSGerrit Renker break; 2485ba4e58ecSGerrit Renker 24861da177e4SLinus Torvalds default: 24871da177e4SLinus Torvalds err = -ENOPROTOOPT; 24881da177e4SLinus Torvalds break; 24896516c655SStephen Hemminger } 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds return err; 24921da177e4SLinus Torvalds } 2493c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt); 24941da177e4SLinus Torvalds 2495db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 2496b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 2497db8dac20SDavid S. Miller { 2498db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2499db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2500db8dac20SDavid S. Miller udp_push_pending_frames); 2501db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 2502db8dac20SDavid S. Miller } 2503db8dac20SDavid S. Miller 2504db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2505db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 2506b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 2507db8dac20SDavid S. Miller { 2508db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2509db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 2510db8dac20SDavid S. Miller udp_push_pending_frames); 2511db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 2512db8dac20SDavid S. Miller } 2513db8dac20SDavid S. Miller #endif 2514db8dac20SDavid S. Miller 25154c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 25161da177e4SLinus Torvalds char __user *optval, int __user *optlen) 25171da177e4SLinus Torvalds { 25181da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 25191da177e4SLinus Torvalds int val, len; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds if (get_user(len, optlen)) 25221da177e4SLinus Torvalds return -EFAULT; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds if (len < 0) 25271da177e4SLinus Torvalds return -EINVAL; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds switch (optname) { 25301da177e4SLinus Torvalds case UDP_CORK: 25311da177e4SLinus Torvalds val = up->corkflag; 25321da177e4SLinus Torvalds break; 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds case UDP_ENCAP: 25351da177e4SLinus Torvalds val = up->encap_type; 25361da177e4SLinus Torvalds break; 25371da177e4SLinus Torvalds 25381c19448cSTom Herbert case UDP_NO_CHECK6_TX: 25391c19448cSTom Herbert val = up->no_check6_tx; 25401c19448cSTom Herbert break; 25411c19448cSTom Herbert 25421c19448cSTom Herbert case UDP_NO_CHECK6_RX: 25431c19448cSTom Herbert val = up->no_check6_rx; 25441c19448cSTom Herbert break; 25451c19448cSTom Herbert 2546bec1f6f6SWillem de Bruijn case UDP_SEGMENT: 2547bec1f6f6SWillem de Bruijn val = up->gso_size; 2548bec1f6f6SWillem de Bruijn break; 2549bec1f6f6SWillem de Bruijn 2550ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 2551ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 2552ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 2553ba4e58ecSGerrit Renker val = up->pcslen; 2554ba4e58ecSGerrit Renker break; 2555ba4e58ecSGerrit Renker 2556ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 2557ba4e58ecSGerrit Renker val = up->pcrlen; 2558ba4e58ecSGerrit Renker break; 2559ba4e58ecSGerrit Renker 25601da177e4SLinus Torvalds default: 25611da177e4SLinus Torvalds return -ENOPROTOOPT; 25626516c655SStephen Hemminger } 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds if (put_user(len, optlen)) 25651da177e4SLinus Torvalds return -EFAULT; 25661da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 25671da177e4SLinus Torvalds return -EFAULT; 25681da177e4SLinus Torvalds return 0; 25691da177e4SLinus Torvalds } 2570c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt); 25711da177e4SLinus Torvalds 2572db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 2573db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2574db8dac20SDavid S. Miller { 2575db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2576db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2577db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 2578db8dac20SDavid S. Miller } 2579db8dac20SDavid S. Miller 2580db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2581db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 2582db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 2583db8dac20SDavid S. Miller { 2584db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 2585db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 2586db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 2587db8dac20SDavid S. Miller } 2588db8dac20SDavid S. Miller #endif 25891da177e4SLinus Torvalds /** 25901da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 25911da177e4SLinus Torvalds * @file - file struct 25921da177e4SLinus Torvalds * @sock - socket 2593a11e1d43SLinus Torvalds * @wait - poll table 25941da177e4SLinus Torvalds * 25951da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 25961da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 25971da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 25981da177e4SLinus Torvalds * then it could get return from select indicating data available 25991da177e4SLinus Torvalds * but then block when reading it. Add special case code 26001da177e4SLinus Torvalds * to work around these arguably broken applications. 26011da177e4SLinus Torvalds */ 2602a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) 26031da177e4SLinus Torvalds { 2604a11e1d43SLinus Torvalds __poll_t mask = datagram_poll(file, sock, wait); 26051da177e4SLinus Torvalds struct sock *sk = sock->sk; 26061da177e4SLinus Torvalds 26072276f58aSPaolo Abeni if (!skb_queue_empty(&udp_sk(sk)->reader_queue)) 2608a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 26092276f58aSPaolo Abeni 26101da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 2611a11e1d43SLinus Torvalds if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 2612e83c6744SEric Dumazet !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) 2613a9a08845SLinus Torvalds mask &= ~(EPOLLIN | EPOLLRDNORM); 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds return mask; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds } 2618a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll); 26191da177e4SLinus Torvalds 26205d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err) 26215d77dca8SDavid Ahern { 26225d77dca8SDavid Ahern lock_sock(sk); 26235d77dca8SDavid Ahern 26245d77dca8SDavid Ahern sk->sk_err = err; 26255d77dca8SDavid Ahern sk->sk_error_report(sk); 2626286c72deSEric Dumazet __udp_disconnect(sk, 0); 26275d77dca8SDavid Ahern 26285d77dca8SDavid Ahern release_sock(sk); 26295d77dca8SDavid Ahern 26305d77dca8SDavid Ahern return 0; 26315d77dca8SDavid Ahern } 26325d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort); 26335d77dca8SDavid Ahern 2634db8dac20SDavid S. Miller struct proto udp_prot = { 2635db8dac20SDavid S. Miller .name = "UDP", 2636db8dac20SDavid S. Miller .owner = THIS_MODULE, 2637db8dac20SDavid S. Miller .close = udp_lib_close, 2638d74bad4eSAndrey Ignatov .pre_connect = udp_pre_connect, 2639db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 2640db8dac20SDavid S. Miller .disconnect = udp_disconnect, 2641db8dac20SDavid S. Miller .ioctl = udp_ioctl, 2642850cbaddSPaolo Abeni .init = udp_init_sock, 2643db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 2644db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 2645db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 2646db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 2647db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 2648db8dac20SDavid S. Miller .sendpage = udp_sendpage, 26498141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 2650db8dac20SDavid S. Miller .hash = udp_lib_hash, 2651db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 2652719f8358SEric Dumazet .rehash = udp_v4_rehash, 2653db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 2654db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 2655db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 26561e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 26571e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 2658db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 2659645ca708SEric Dumazet .h.udp_table = &udp_table, 2660db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 2661db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 2662db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 2663db8dac20SDavid S. Miller #endif 26645d77dca8SDavid Ahern .diag_destroy = udp_abort, 2665db8dac20SDavid S. Miller }; 2666c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot); 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 26691da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26701da177e4SLinus Torvalds 2671645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 26721da177e4SLinus Torvalds { 26731da177e4SLinus Torvalds struct sock *sk; 2674a3d2599bSChristoph Hellwig struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); 26751da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 26766f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 26771da177e4SLinus Torvalds 2678a3d2599bSChristoph Hellwig for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; 2679f86dcc5aSEric Dumazet ++state->bucket) { 2680a3d2599bSChristoph Hellwig struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; 2681f86dcc5aSEric Dumazet 2682ca065d0cSEric Dumazet if (hlist_empty(&hslot->head)) 2683f86dcc5aSEric Dumazet continue; 2684f86dcc5aSEric Dumazet 2685645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 2686ca065d0cSEric Dumazet sk_for_each(sk, &hslot->head) { 2687878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 2688a91275efSDaniel Lezcano continue; 2689a3d2599bSChristoph Hellwig if (sk->sk_family == afinfo->family) 26901da177e4SLinus Torvalds goto found; 26911da177e4SLinus Torvalds } 2692645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds sk = NULL; 26951da177e4SLinus Torvalds found: 26961da177e4SLinus Torvalds return sk; 26971da177e4SLinus Torvalds } 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 27001da177e4SLinus Torvalds { 2701a3d2599bSChristoph Hellwig struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); 27021da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 27036f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds do { 2706ca065d0cSEric Dumazet sk = sk_next(sk); 2707a3d2599bSChristoph Hellwig } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != afinfo->family)); 27081da177e4SLinus Torvalds 2709645ca708SEric Dumazet if (!sk) { 2710a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 2711a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 2712645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds return sk; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 27181da177e4SLinus Torvalds { 2719645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds if (sk) 27221da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 27231da177e4SLinus Torvalds --pos; 27241da177e4SLinus Torvalds return pos ? NULL : sk; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 2727a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos) 27281da177e4SLinus Torvalds { 272930842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 2730f86dcc5aSEric Dumazet state->bucket = MAX_UDP_PORTS; 273130842f29SVitaly Mayatskikh 2732b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 27331da177e4SLinus Torvalds } 2734a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start); 27351da177e4SLinus Torvalds 2736a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 27371da177e4SLinus Torvalds { 27381da177e4SLinus Torvalds struct sock *sk; 27391da177e4SLinus Torvalds 2740b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 27411da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 27421da177e4SLinus Torvalds else 27431da177e4SLinus Torvalds sk = udp_get_next(seq, v); 27441da177e4SLinus Torvalds 27451da177e4SLinus Torvalds ++*pos; 27461da177e4SLinus Torvalds return sk; 27471da177e4SLinus Torvalds } 2748a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next); 27491da177e4SLinus Torvalds 2750a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v) 27511da177e4SLinus Torvalds { 2752a3d2599bSChristoph Hellwig struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); 2753645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 2754645ca708SEric Dumazet 2755a3d2599bSChristoph Hellwig if (state->bucket <= afinfo->udp_table->mask) 2756a3d2599bSChristoph Hellwig spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); 27571da177e4SLinus Torvalds } 2758a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop); 2759db8dac20SDavid S. Miller 2760db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 27615e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2762652586dfSTetsuo Handa int bucket) 2763db8dac20SDavid S. Miller { 2764db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 2765c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr; 2766c720c7e8SEric Dumazet __be32 src = inet->inet_rcv_saddr; 2767c720c7e8SEric Dumazet __u16 destp = ntohs(inet->inet_dport); 2768c720c7e8SEric Dumazet __u16 srcp = ntohs(inet->inet_sport); 2769db8dac20SDavid S. Miller 2770f86dcc5aSEric Dumazet seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2771652586dfSTetsuo Handa " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d", 2772db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 277331e6d363SEric Dumazet sk_wmem_alloc_get(sp), 27746c206b20SPaolo Abeni udp_rqueue_get(sp), 2775a7cb5a49SEric W. Biederman 0, 0L, 0, 2776a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 2777a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 277841c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, 2779652586dfSTetsuo Handa atomic_read(&sp->sk_drops)); 2780db8dac20SDavid S. Miller } 2781db8dac20SDavid S. Miller 2782db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 2783db8dac20SDavid S. Miller { 2784652586dfSTetsuo Handa seq_setwidth(seq, 127); 2785db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 2786652586dfSTetsuo Handa seq_puts(seq, " sl local_address rem_address st tx_queue " 2787db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 2788cb61cb9bSEric Dumazet "inode ref pointer drops"); 2789db8dac20SDavid S. Miller else { 2790db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 2791db8dac20SDavid S. Miller 2792652586dfSTetsuo Handa udp4_format_sock(v, seq, state->bucket); 2793db8dac20SDavid S. Miller } 2794652586dfSTetsuo Handa seq_pad(seq, '\n'); 2795db8dac20SDavid S. Miller return 0; 2796db8dac20SDavid S. Miller } 2797db8dac20SDavid S. Miller 2798c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = { 2799a3d2599bSChristoph Hellwig .start = udp_seq_start, 2800a3d2599bSChristoph Hellwig .next = udp_seq_next, 2801a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 2802a3d2599bSChristoph Hellwig .show = udp4_seq_show, 2803a3d2599bSChristoph Hellwig }; 2804c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops); 280573cb88ecSArjan van de Ven 2806db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 2807db8dac20SDavid S. Miller .family = AF_INET, 2808645ca708SEric Dumazet .udp_table = &udp_table, 2809db8dac20SDavid S. Miller }; 2810db8dac20SDavid S. Miller 28112c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net) 281215439febSPavel Emelyanov { 2813c3506372SChristoph Hellwig if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, 2814c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp4_seq_afinfo)) 2815a3d2599bSChristoph Hellwig return -ENOMEM; 2816a3d2599bSChristoph Hellwig return 0; 281715439febSPavel Emelyanov } 281815439febSPavel Emelyanov 28192c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net) 282015439febSPavel Emelyanov { 2821a3d2599bSChristoph Hellwig remove_proc_entry("udp", net->proc_net); 282215439febSPavel Emelyanov } 282315439febSPavel Emelyanov 282415439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 282515439febSPavel Emelyanov .init = udp4_proc_init_net, 282615439febSPavel Emelyanov .exit = udp4_proc_exit_net, 282715439febSPavel Emelyanov }; 282815439febSPavel Emelyanov 2829db8dac20SDavid S. Miller int __init udp4_proc_init(void) 2830db8dac20SDavid S. Miller { 283115439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 2832db8dac20SDavid S. Miller } 2833db8dac20SDavid S. Miller 2834db8dac20SDavid S. Miller void udp4_proc_exit(void) 2835db8dac20SDavid S. Miller { 283615439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 2837db8dac20SDavid S. Miller } 28381da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 28391da177e4SLinus Torvalds 2840f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries; 2841f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str) 2842645ca708SEric Dumazet { 2843413c27d8SEldad Zack ssize_t ret; 2844413c27d8SEldad Zack 2845f86dcc5aSEric Dumazet if (!str) 2846f86dcc5aSEric Dumazet return 0; 2847413c27d8SEldad Zack 2848413c27d8SEldad Zack ret = kstrtoul(str, 0, &uhash_entries); 2849413c27d8SEldad Zack if (ret) 2850413c27d8SEldad Zack return 0; 2851413c27d8SEldad Zack 2852f86dcc5aSEric Dumazet if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 2853f86dcc5aSEric Dumazet uhash_entries = UDP_HTABLE_SIZE_MIN; 2854f86dcc5aSEric Dumazet return 1; 2855f86dcc5aSEric Dumazet } 2856f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries); 2857645ca708SEric Dumazet 2858f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name) 2859f86dcc5aSEric Dumazet { 2860f86dcc5aSEric Dumazet unsigned int i; 2861f86dcc5aSEric Dumazet 2862f86dcc5aSEric Dumazet table->hash = alloc_large_system_hash(name, 2863512615b6SEric Dumazet 2 * sizeof(struct udp_hslot), 2864f86dcc5aSEric Dumazet uhash_entries, 2865f86dcc5aSEric Dumazet 21, /* one slot per 2 MB */ 2866f86dcc5aSEric Dumazet 0, 2867f86dcc5aSEric Dumazet &table->log, 2868f86dcc5aSEric Dumazet &table->mask, 286931fe62b9STim Bird UDP_HTABLE_SIZE_MIN, 2870f86dcc5aSEric Dumazet 64 * 1024); 287131fe62b9STim Bird 2872512615b6SEric Dumazet table->hash2 = table->hash + (table->mask + 1); 2873f86dcc5aSEric Dumazet for (i = 0; i <= table->mask; i++) { 2874ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash[i].head); 2875fdcc8aa9SEric Dumazet table->hash[i].count = 0; 2876645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 2877645ca708SEric Dumazet } 2878512615b6SEric Dumazet for (i = 0; i <= table->mask; i++) { 2879ca065d0cSEric Dumazet INIT_HLIST_HEAD(&table->hash2[i].head); 2880512615b6SEric Dumazet table->hash2[i].count = 0; 2881512615b6SEric Dumazet spin_lock_init(&table->hash2[i].lock); 2882512615b6SEric Dumazet } 2883645ca708SEric Dumazet } 2884645ca708SEric Dumazet 2885723b8e46STom Herbert u32 udp_flow_hashrnd(void) 2886723b8e46STom Herbert { 2887723b8e46STom Herbert static u32 hashrnd __read_mostly; 2888723b8e46STom Herbert 2889723b8e46STom Herbert net_get_random_once(&hashrnd, sizeof(hashrnd)); 2890723b8e46STom Herbert 2891723b8e46STom Herbert return hashrnd; 2892723b8e46STom Herbert } 2893723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd); 2894723b8e46STom Herbert 28951e802951STonghao Zhang static void __udp_sysctl_init(struct net *net) 28961e802951STonghao Zhang { 28971e802951STonghao Zhang net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM; 28981e802951STonghao Zhang net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM; 28991e802951STonghao Zhang 29001e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV 29011e802951STonghao Zhang net->ipv4.sysctl_udp_l3mdev_accept = 0; 29021e802951STonghao Zhang #endif 29031e802951STonghao Zhang } 29041e802951STonghao Zhang 29051e802951STonghao Zhang static int __net_init udp_sysctl_init(struct net *net) 29061e802951STonghao Zhang { 29071e802951STonghao Zhang __udp_sysctl_init(net); 29081e802951STonghao Zhang return 0; 29091e802951STonghao Zhang } 29101e802951STonghao Zhang 29111e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = { 29121e802951STonghao Zhang .init = udp_sysctl_init, 29131e802951STonghao Zhang }; 29141e802951STonghao Zhang 291595766fffSHideo Aoki void __init udp_init(void) 291695766fffSHideo Aoki { 2917f03d78dbSEric Dumazet unsigned long limit; 29184b272750SEric Dumazet unsigned int i; 291995766fffSHideo Aoki 2920f86dcc5aSEric Dumazet udp_table_init(&udp_table, "UDP"); 2921f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 292295766fffSHideo Aoki limit = max(limit, 128UL); 292395766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 292495766fffSHideo Aoki sysctl_udp_mem[1] = limit; 292595766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 292695766fffSHideo Aoki 29271e802951STonghao Zhang __udp_sysctl_init(&init_net); 29284b272750SEric Dumazet 29294b272750SEric Dumazet /* 16 spinlocks per cpu */ 29304b272750SEric Dumazet udp_busylocks_log = ilog2(nr_cpu_ids) + 4; 29314b272750SEric Dumazet udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, 29324b272750SEric Dumazet GFP_KERNEL); 29334b272750SEric Dumazet if (!udp_busylocks) 29344b272750SEric Dumazet panic("UDP: failed to alloc udp_busylocks\n"); 29354b272750SEric Dumazet for (i = 0; i < (1U << udp_busylocks_log); i++) 29364b272750SEric Dumazet spin_lock_init(udp_busylocks + i); 29371e802951STonghao Zhang 29381e802951STonghao Zhang if (register_pernet_subsys(&udp_sysctl_ops)) 29391e802951STonghao Zhang panic("UDP: failed to init sysctl parameters.\n"); 294095766fffSHideo Aoki } 2941