Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * The User Datagram Protocol (UDP). |
| 7 | * |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 8 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 10 | * Arnt Gulbrandsen, <agulbra@nvg.unit.no> |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 11 | * Alan Cox, <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Hirokazu Takahashi, <taka@valinux.co.jp> |
| 13 | * |
| 14 | * Fixes: |
| 15 | * Alan Cox : verify_area() calls |
| 16 | * Alan Cox : stopped close while in use off icmp |
| 17 | * messages. Not a fix but a botch that |
| 18 | * for udp at least is 'valid'. |
| 19 | * Alan Cox : Fixed icmp handling properly |
| 20 | * Alan Cox : Correct error for oversized datagrams |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 21 | * Alan Cox : Tidied select() semantics. |
| 22 | * Alan Cox : udp_err() fixed properly, also now |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * select and read wake correctly on errors |
| 24 | * Alan Cox : udp_send verify_area moved to avoid mem leak |
| 25 | * Alan Cox : UDP can count its memory |
| 26 | * Alan Cox : send to an unknown connection causes |
| 27 | * an ECONNREFUSED off the icmp, but |
| 28 | * does NOT close. |
| 29 | * Alan Cox : Switched to new sk_buff handlers. No more backlog! |
| 30 | * Alan Cox : Using generic datagram code. Even smaller and the PEEK |
| 31 | * bug no longer crashes it. |
| 32 | * Fred Van Kempen : Net2e support for sk->broadcast. |
| 33 | * Alan Cox : Uses skb_free_datagram |
| 34 | * Alan Cox : Added get/set sockopt support. |
| 35 | * Alan Cox : Broadcasting without option set returns EACCES. |
| 36 | * Alan Cox : No wakeup calls. Instead we now use the callbacks. |
| 37 | * Alan Cox : Use ip_tos and ip_ttl |
| 38 | * Alan Cox : SNMP Mibs |
| 39 | * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. |
| 40 | * Matt Dillon : UDP length checks. |
| 41 | * Alan Cox : Smarter af_inet used properly. |
| 42 | * Alan Cox : Use new kernel side addressing. |
| 43 | * Alan Cox : Incorrect return on truncated datagram receive. |
| 44 | * Arnt Gulbrandsen : New udp_send and stuff |
| 45 | * Alan Cox : Cache last socket |
| 46 | * Alan Cox : Route cache |
| 47 | * Jon Peatfield : Minor efficiency fix to sendto(). |
| 48 | * Mike Shaver : RFC1122 checks. |
| 49 | * Alan Cox : Nonblocking error fix. |
| 50 | * Willy Konynenberg : Transparent proxying support. |
| 51 | * Mike McLagan : Routing by source |
| 52 | * David S. Miller : New socket lookup architecture. |
| 53 | * Last socket cache retained as it |
| 54 | * does have a high hit rate. |
| 55 | * Olaf Kirch : Don't linearise iovec on sendmsg. |
| 56 | * Andi Kleen : Some cleanups, cache destination entry |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 57 | * for connect. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * Vitaly E. Lavrov : Transparent proxy revived after year coma. |
| 59 | * Melvin Smith : Check msg_name not msg_namelen in sendto(), |
| 60 | * return ENOTCONN for unconnected sockets (POSIX) |
| 61 | * Janos Farkas : don't deliver multi/broadcasts to a different |
| 62 | * bound-to-device socket |
| 63 | * Hirokazu Takahashi : HW checksumming for outgoing UDP |
| 64 | * datagrams. |
| 65 | * Hirokazu Takahashi : sendfile() on UDP works now. |
| 66 | * Arnaldo C. Melo : convert /proc/net/udp to seq_file |
| 67 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which |
| 68 | * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind |
| 69 | * a single port at the same time. |
| 70 | * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support |
James Chapman | 342f023 | 2007-06-27 15:37:46 -0700 | [diff] [blame] | 71 | * James Chapman : Add L2TP encapsulation type. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * |
| 73 | * |
| 74 | * This program is free software; you can redistribute it and/or |
| 75 | * modify it under the terms of the GNU General Public License |
| 76 | * as published by the Free Software Foundation; either version |
| 77 | * 2 of the License, or (at your option) any later version. |
| 78 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 79 | |
Joe Perches | afd46503 | 2012-03-12 07:03:32 +0000 | [diff] [blame] | 80 | #define pr_fmt(fmt) "UDP: " fmt |
| 81 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 82 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <asm/ioctls.h> |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 84 | #include <linux/bootmem.h> |
Eric Dumazet | 8203efb | 2008-10-29 02:32:32 -0700 | [diff] [blame] | 85 | #include <linux/highmem.h> |
| 86 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #include <linux/types.h> |
| 88 | #include <linux/fcntl.h> |
| 89 | #include <linux/module.h> |
| 90 | #include <linux/socket.h> |
| 91 | #include <linux/sockios.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 92 | #include <linux/igmp.h> |
Shawn Bohrer | 6e54030 | 2015-06-03 16:27:38 -0500 | [diff] [blame] | 93 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include <linux/in.h> |
| 95 | #include <linux/errno.h> |
| 96 | #include <linux/timer.h> |
| 97 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #include <linux/inet.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 100 | #include <linux/slab.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 101 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #include <linux/skbuff.h> |
| 103 | #include <linux/proc_fs.h> |
| 104 | #include <linux/seq_file.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 105 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #include <net/icmp.h> |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 107 | #include <net/inet_hashtables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #include <net/route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #include <net/checksum.h> |
| 110 | #include <net/xfrm.h> |
Satoru Moriya | 296f7ea | 2011-06-17 11:58:39 +0000 | [diff] [blame] | 111 | #include <trace/events/udp.h> |
Eric Dumazet | 447167b | 2012-04-11 23:05:28 +0000 | [diff] [blame] | 112 | #include <linux/static_key.h> |
Eric Dumazet | 22911fc | 2012-06-27 00:23:44 +0000 | [diff] [blame] | 113 | #include <trace/events/skb.h> |
Eliezer Tamir | 076bb0c | 2013-07-10 17:13:17 +0300 | [diff] [blame] | 114 | #include <net/busy_poll.h> |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 115 | #include "udp_impl.h" |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 116 | #include <net/sock_reuseport.h> |
Eric Dumazet | 217375a | 2016-08-18 09:59:12 -0700 | [diff] [blame] | 117 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 119 | struct udp_table udp_table __read_mostly; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 120 | EXPORT_SYMBOL(udp_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 122 | long sysctl_udp_mem[3] __read_mostly; |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 123 | EXPORT_SYMBOL(sysctl_udp_mem); |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 124 | |
| 125 | int sysctl_udp_rmem_min __read_mostly; |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 126 | EXPORT_SYMBOL(sysctl_udp_rmem_min); |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 127 | |
| 128 | int sysctl_udp_wmem_min __read_mostly; |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 129 | EXPORT_SYMBOL(sysctl_udp_wmem_min); |
| 130 | |
Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 131 | atomic_long_t udp_memory_allocated; |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 132 | EXPORT_SYMBOL(udp_memory_allocated); |
| 133 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 134 | #define MAX_UDP_PORTS 65536 |
| 135 | #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 136 | |
Robert Shearman | 63a6fff | 2017-01-26 18:02:24 +0000 | [diff] [blame] | 137 | /* IPCB reference means this can not be used from early demux */ |
| 138 | static bool udp_lib_exact_dif_match(struct net *net, struct sk_buff *skb) |
| 139 | { |
| 140 | #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) |
| 141 | if (!net->ipv4.sysctl_udp_l3mdev_accept && |
| 142 | skb && ipv4_l3mdev_skb(IPCB(skb)->flags)) |
| 143 | return true; |
| 144 | #endif |
| 145 | return false; |
| 146 | } |
| 147 | |
Eric Dumazet | f24d43c | 2008-10-09 14:51:27 -0700 | [diff] [blame] | 148 | static int udp_lib_lport_inuse(struct net *net, __u16 num, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 149 | const struct udp_hslot *hslot, |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 150 | unsigned long *bitmap, |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 151 | struct sock *sk, unsigned int log) |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 152 | { |
Eric Dumazet | f24d43c | 2008-10-09 14:51:27 -0700 | [diff] [blame] | 153 | struct sock *sk2; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 154 | kuid_t uid = sock_i_uid(sk); |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 155 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 156 | sk_for_each(sk2, &hslot->head) { |
Joe Perches | 9d4fb27 | 2009-11-23 10:41:23 -0800 | [diff] [blame] | 157 | if (net_eq(sock_net(sk2), net) && |
| 158 | sk2 != sk && |
Eric Dumazet | d4cada4 | 2009-11-08 10:17:30 +0000 | [diff] [blame] | 159 | (bitmap || udp_sk(sk2)->udp_port_hash == num) && |
Joe Perches | 9d4fb27 | 2009-11-23 10:41:23 -0800 | [diff] [blame] | 160 | (!sk2->sk_reuse || !sk->sk_reuse) && |
| 161 | (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || |
| 162 | sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 163 | inet_rcv_saddr_equal(sk, sk2, true)) { |
Eric Garver | df56005 | 2017-01-05 20:22:36 -0500 | [diff] [blame] | 164 | if (sk2->sk_reuseport && sk->sk_reuseport && |
| 165 | !rcu_access_pointer(sk->sk_reuseport_cb) && |
| 166 | uid_eq(uid, sock_i_uid(sk2))) { |
| 167 | if (!bitmap) |
| 168 | return 0; |
| 169 | } else { |
| 170 | if (!bitmap) |
| 171 | return 1; |
| 172 | __set_bit(udp_sk(sk2)->udp_port_hash >> log, |
| 173 | bitmap); |
| 174 | } |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 175 | } |
Joe Perches | 4243cdc | 2014-11-11 21:59:20 -0800 | [diff] [blame] | 176 | } |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Note: we still hold spinlock of primary hash chain, so no other writer |
| 182 | * can insert/delete a socket with local_port == num |
| 183 | */ |
| 184 | static int udp_lib_lport_inuse2(struct net *net, __u16 num, |
Joe Perches | 4243cdc | 2014-11-11 21:59:20 -0800 | [diff] [blame] | 185 | struct udp_hslot *hslot2, |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 186 | struct sock *sk) |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 187 | { |
| 188 | struct sock *sk2; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 189 | kuid_t uid = sock_i_uid(sk); |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 190 | int res = 0; |
| 191 | |
| 192 | spin_lock(&hslot2->lock); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 193 | udp_portaddr_for_each_entry(sk2, &hslot2->head) { |
Joe Perches | 9d4fb27 | 2009-11-23 10:41:23 -0800 | [diff] [blame] | 194 | if (net_eq(sock_net(sk2), net) && |
| 195 | sk2 != sk && |
| 196 | (udp_sk(sk2)->udp_port_hash == num) && |
| 197 | (!sk2->sk_reuse || !sk->sk_reuse) && |
| 198 | (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || |
| 199 | sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 200 | inet_rcv_saddr_equal(sk, sk2, true)) { |
Eric Garver | df56005 | 2017-01-05 20:22:36 -0500 | [diff] [blame] | 201 | if (sk2->sk_reuseport && sk->sk_reuseport && |
| 202 | !rcu_access_pointer(sk->sk_reuseport_cb) && |
| 203 | uid_eq(uid, sock_i_uid(sk2))) { |
| 204 | res = 0; |
| 205 | } else { |
| 206 | res = 1; |
| 207 | } |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 208 | break; |
| 209 | } |
Joe Perches | 4243cdc | 2014-11-11 21:59:20 -0800 | [diff] [blame] | 210 | } |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 211 | spin_unlock(&hslot2->lock); |
| 212 | return res; |
| 213 | } |
| 214 | |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 215 | static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 216 | { |
| 217 | struct net *net = sock_net(sk); |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 218 | kuid_t uid = sock_i_uid(sk); |
| 219 | struct sock *sk2; |
| 220 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 221 | sk_for_each(sk2, &hslot->head) { |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 222 | if (net_eq(sock_net(sk2), net) && |
| 223 | sk2 != sk && |
| 224 | sk2->sk_family == sk->sk_family && |
| 225 | ipv6_only_sock(sk2) == ipv6_only_sock(sk) && |
| 226 | (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && |
| 227 | (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
| 228 | sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 229 | inet_rcv_saddr_equal(sk, sk2, false)) { |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 230 | return reuseport_add_sock(sk, sk2); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* Initial allocation may have already happened via setsockopt */ |
| 235 | if (!rcu_access_pointer(sk->sk_reuseport_cb)) |
| 236 | return reuseport_alloc(sk); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 240 | /** |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 241 | * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 242 | * |
| 243 | * @sk: socket struct in question |
| 244 | * @snum: port number to look up |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 245 | * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 246 | * with NULL address |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 247 | */ |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 248 | int udp_lib_get_port(struct sock *sk, unsigned short snum, |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 249 | unsigned int hash2_nulladdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 251 | struct udp_hslot *hslot, *hslot2; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 252 | struct udp_table *udptable = sk->sk_prot->h.udp_table; |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 253 | int error = 1; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 254 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 256 | if (!snum) { |
Eric Dumazet | 9088c56 | 2008-10-08 11:44:17 -0700 | [diff] [blame] | 257 | int low, high, remaining; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 258 | unsigned int rand; |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 259 | unsigned short first, last; |
| 260 | DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 262 | inet_get_local_port_range(net, &low, &high); |
Anton Arapov | a25de53 | 2007-10-18 22:00:17 -0700 | [diff] [blame] | 263 | remaining = (high - low) + 1; |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 264 | |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 265 | rand = prandom_u32(); |
Daniel Borkmann | 8fc54f6 | 2014-08-23 20:58:54 +0200 | [diff] [blame] | 266 | first = reciprocal_scale(rand, remaining) + low; |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 267 | /* |
| 268 | * force rand to be an odd multiple of UDP_HTABLE_SIZE |
| 269 | */ |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 270 | rand = (rand | 1) * (udptable->mask + 1); |
Eric Dumazet | 5781b23 | 2009-12-13 19:32:39 -0800 | [diff] [blame] | 271 | last = first + udptable->mask + 1; |
| 272 | do { |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 273 | hslot = udp_hashslot(udptable, net, first); |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 274 | bitmap_zero(bitmap, PORTS_PER_CHAIN); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 275 | spin_lock_bh(&hslot->lock); |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 276 | udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 277 | udptable->log); |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 278 | |
| 279 | snum = first; |
| 280 | /* |
| 281 | * Iterate on all possible values of snum for this hash. |
| 282 | * Using steps of an odd multiple of UDP_HTABLE_SIZE |
| 283 | * give us randomization and full range coverage. |
| 284 | */ |
Eric Dumazet | 9088c56 | 2008-10-08 11:44:17 -0700 | [diff] [blame] | 285 | do { |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 286 | if (low <= snum && snum <= high && |
Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 287 | !test_bit(snum >> udptable->log, bitmap) && |
WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 288 | !inet_is_local_reserved_port(net, snum)) |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 289 | goto found; |
| 290 | snum += rand; |
| 291 | } while (snum != first); |
| 292 | spin_unlock_bh(&hslot->lock); |
Eric Garver | df56005 | 2017-01-05 20:22:36 -0500 | [diff] [blame] | 293 | cond_resched(); |
Eric Dumazet | 5781b23 | 2009-12-13 19:32:39 -0800 | [diff] [blame] | 294 | } while (++first != last); |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 295 | goto fail; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 296 | } else { |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 297 | hslot = udp_hashslot(udptable, net, snum); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 298 | spin_lock_bh(&hslot->lock); |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 299 | if (hslot->count > 10) { |
| 300 | int exist; |
| 301 | unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; |
| 302 | |
| 303 | slot2 &= udptable->mask; |
| 304 | hash2_nulladdr &= udptable->mask; |
| 305 | |
| 306 | hslot2 = udp_hashslot2(udptable, slot2); |
| 307 | if (hslot->count < hslot2->count) |
| 308 | goto scan_primary_hash; |
| 309 | |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 310 | exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 311 | if (!exist && (hash2_nulladdr != slot2)) { |
| 312 | hslot2 = udp_hashslot2(udptable, hash2_nulladdr); |
| 313 | exist = udp_lib_lport_inuse2(net, snum, hslot2, |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 314 | sk); |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 315 | } |
| 316 | if (exist) |
| 317 | goto fail_unlock; |
| 318 | else |
| 319 | goto found; |
| 320 | } |
| 321 | scan_primary_hash: |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 322 | if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 323 | goto fail_unlock; |
| 324 | } |
Eric Dumazet | 98322f2 | 2009-01-26 21:35:35 -0800 | [diff] [blame] | 325 | found: |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 326 | inet_sk(sk)->inet_num = snum; |
Eric Dumazet | d4cada4 | 2009-11-08 10:17:30 +0000 | [diff] [blame] | 327 | udp_sk(sk)->udp_port_hash = snum; |
| 328 | udp_sk(sk)->udp_portaddr_hash ^= snum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (sk_unhashed(sk)) { |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 330 | if (sk->sk_reuseport && |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 331 | udp_reuseport_add_sock(sk, hslot)) { |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 332 | inet_sk(sk)->inet_num = 0; |
| 333 | udp_sk(sk)->udp_port_hash = 0; |
| 334 | udp_sk(sk)->udp_portaddr_hash ^= snum; |
| 335 | goto fail_unlock; |
| 336 | } |
| 337 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 338 | sk_add_node_rcu(sk, &hslot->head); |
Eric Dumazet | fdcc8aa9 | 2009-11-08 10:17:05 +0000 | [diff] [blame] | 339 | hslot->count++; |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 340 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 341 | |
| 342 | hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); |
| 343 | spin_lock(&hslot2->lock); |
Craig Gallek | d894ba1 | 2016-04-12 13:11:25 -0400 | [diff] [blame] | 344 | if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && |
David S. Miller | 1602f49 | 2016-04-23 18:26:24 -0400 | [diff] [blame] | 345 | sk->sk_family == AF_INET6) |
| 346 | hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, |
| 347 | &hslot2->head); |
Craig Gallek | d894ba1 | 2016-04-12 13:11:25 -0400 | [diff] [blame] | 348 | else |
David S. Miller | 1602f49 | 2016-04-23 18:26:24 -0400 | [diff] [blame] | 349 | hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, |
| 350 | &hslot2->head); |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 351 | hslot2->count++; |
| 352 | spin_unlock(&hslot2->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 354 | sock_set_flag(sk, SOCK_RCU_FREE); |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 355 | error = 0; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 356 | fail_unlock: |
| 357 | spin_unlock_bh(&hslot->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | fail: |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 359 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 361 | EXPORT_SYMBOL(udp_lib_get_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Eric Dumazet | 6eada01 | 2015-03-18 14:05:33 -0700 | [diff] [blame] | 363 | static u32 udp4_portaddr_hash(const struct net *net, __be32 saddr, |
| 364 | unsigned int port) |
Eric Dumazet | d4cada4 | 2009-11-08 10:17:30 +0000 | [diff] [blame] | 365 | { |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 366 | return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port; |
Eric Dumazet | d4cada4 | 2009-11-08 10:17:30 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 369 | int udp_v4_get_port(struct sock *sk, unsigned short snum) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 370 | { |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 371 | unsigned int hash2_nulladdr = |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 372 | udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 373 | unsigned int hash2_partial = |
| 374 | udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); |
| 375 | |
Eric Dumazet | d4cada4 | 2009-11-08 10:17:30 +0000 | [diff] [blame] | 376 | /* precompute partial secondary hash */ |
Eric Dumazet | 30fff92 | 2009-11-09 05:26:33 +0000 | [diff] [blame] | 377 | udp_sk(sk)->udp_portaddr_hash = hash2_partial; |
Josef Bacik | fe38d2a | 2017-01-17 07:51:01 -0800 | [diff] [blame] | 378 | return udp_lib_get_port(sk, snum, hash2_nulladdr); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Su, Xuemin | d1e3728 | 2016-06-13 11:02:50 +0800 | [diff] [blame] | 381 | static int compute_score(struct sock *sk, struct net *net, |
| 382 | __be32 saddr, __be16 sport, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 383 | __be32 daddr, unsigned short hnum, |
| 384 | int dif, int sdif, bool exact_dif) |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 385 | { |
Joe Perches | 60c04ae | 2014-12-01 20:29:06 -0800 | [diff] [blame] | 386 | int score; |
| 387 | struct inet_sock *inet; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 388 | |
Joe Perches | 60c04ae | 2014-12-01 20:29:06 -0800 | [diff] [blame] | 389 | if (!net_eq(sock_net(sk), net) || |
| 390 | udp_sk(sk)->udp_port_hash != hnum || |
| 391 | ipv6_only_sock(sk)) |
| 392 | return -1; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 393 | |
Joe Perches | 60c04ae | 2014-12-01 20:29:06 -0800 | [diff] [blame] | 394 | score = (sk->sk_family == PF_INET) ? 2 : 1; |
| 395 | inet = inet_sk(sk); |
| 396 | |
| 397 | if (inet->inet_rcv_saddr) { |
| 398 | if (inet->inet_rcv_saddr != daddr) |
| 399 | return -1; |
| 400 | score += 4; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 401 | } |
Joe Perches | 60c04ae | 2014-12-01 20:29:06 -0800 | [diff] [blame] | 402 | |
| 403 | if (inet->inet_daddr) { |
| 404 | if (inet->inet_daddr != saddr) |
| 405 | return -1; |
| 406 | score += 4; |
| 407 | } |
| 408 | |
| 409 | if (inet->inet_dport) { |
| 410 | if (inet->inet_dport != sport) |
| 411 | return -1; |
| 412 | score += 4; |
| 413 | } |
| 414 | |
Robert Shearman | 63a6fff | 2017-01-26 18:02:24 +0000 | [diff] [blame] | 415 | if (sk->sk_bound_dev_if || exact_dif) { |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 416 | bool dev_match = (sk->sk_bound_dev_if == dif || |
| 417 | sk->sk_bound_dev_if == sdif); |
| 418 | |
| 419 | if (exact_dif && !dev_match) |
Joe Perches | 60c04ae | 2014-12-01 20:29:06 -0800 | [diff] [blame] | 420 | return -1; |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 421 | if (sk->sk_bound_dev_if && dev_match) |
| 422 | score += 4; |
Joe Perches | 60c04ae | 2014-12-01 20:29:06 -0800 | [diff] [blame] | 423 | } |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 424 | |
Eric Dumazet | 70da268 | 2015-10-08 19:33:21 -0700 | [diff] [blame] | 425 | if (sk->sk_incoming_cpu == raw_smp_processor_id()) |
| 426 | score++; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 427 | return score; |
| 428 | } |
| 429 | |
Eric Dumazet | 6eada01 | 2015-03-18 14:05:33 -0700 | [diff] [blame] | 430 | static u32 udp_ehashfn(const struct net *net, const __be32 laddr, |
| 431 | const __u16 lport, const __be32 faddr, |
| 432 | const __be16 fport) |
Hannes Frederic Sowa | 65cd803 | 2013-10-19 21:48:51 +0200 | [diff] [blame] | 433 | { |
Hannes Frederic Sowa | 1bbdcee | 2013-10-19 21:48:57 +0200 | [diff] [blame] | 434 | static u32 udp_ehash_secret __read_mostly; |
| 435 | |
| 436 | net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); |
| 437 | |
Hannes Frederic Sowa | 65cd803 | 2013-10-19 21:48:51 +0200 | [diff] [blame] | 438 | return __inet_ehashfn(laddr, lport, faddr, fport, |
Hannes Frederic Sowa | 1bbdcee | 2013-10-19 21:48:57 +0200 | [diff] [blame] | 439 | udp_ehash_secret + net_hash_mix(net)); |
Hannes Frederic Sowa | 65cd803 | 2013-10-19 21:48:51 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Su, Xuemin | d1e3728 | 2016-06-13 11:02:50 +0800 | [diff] [blame] | 442 | /* called with rcu_read_lock() */ |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 443 | static struct sock *udp4_lib_lookup2(struct net *net, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 444 | __be32 saddr, __be16 sport, |
| 445 | __be32 daddr, unsigned int hnum, |
| 446 | int dif, int sdif, bool exact_dif, |
| 447 | struct udp_hslot *hslot2, |
| 448 | struct sk_buff *skb) |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 449 | { |
| 450 | struct sock *sk, *result; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 451 | int score, badness, matches = 0, reuseport = 0; |
| 452 | u32 hash = 0; |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 453 | |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 454 | result = NULL; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 455 | badness = 0; |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 456 | udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { |
Su, Xuemin | d1e3728 | 2016-06-13 11:02:50 +0800 | [diff] [blame] | 457 | score = compute_score(sk, net, saddr, sport, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 458 | daddr, hnum, dif, sdif, exact_dif); |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 459 | if (score > badness) { |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 460 | reuseport = sk->sk_reuseport; |
| 461 | if (reuseport) { |
Hannes Frederic Sowa | 65cd803 | 2013-10-19 21:48:51 +0200 | [diff] [blame] | 462 | hash = udp_ehashfn(net, daddr, hnum, |
| 463 | saddr, sport); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 464 | result = reuseport_select_sock(sk, hash, skb, |
Eric Dumazet | ed0dfff | 2016-01-19 08:36:43 -0800 | [diff] [blame] | 465 | sizeof(struct udphdr)); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 466 | if (result) |
| 467 | return result; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 468 | matches = 1; |
| 469 | } |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 470 | badness = score; |
| 471 | result = sk; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 472 | } else if (score == badness && reuseport) { |
| 473 | matches++; |
Daniel Borkmann | 8fc54f6 | 2014-08-23 20:58:54 +0200 | [diff] [blame] | 474 | if (reciprocal_scale(hash, matches) == 0) |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 475 | result = sk; |
| 476 | hash = next_pseudo_random32(hash); |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 479 | return result; |
| 480 | } |
| 481 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 482 | /* UDP is nearly always wildcards out the wazoo, it makes no sense to try |
| 483 | * harder than this. -DaveM |
| 484 | */ |
Pavel Emelyanov | fce8233 | 2011-12-09 06:23:34 +0000 | [diff] [blame] | 485 | struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 486 | __be16 sport, __be32 daddr, __be16 dport, int dif, |
| 487 | int sdif, struct udp_table *udptable, struct sk_buff *skb) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 488 | { |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 489 | struct sock *sk, *result; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 490 | unsigned short hnum = ntohs(dport); |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 491 | unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); |
| 492 | struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; |
Robert Shearman | 63a6fff | 2017-01-26 18:02:24 +0000 | [diff] [blame] | 493 | bool exact_dif = udp_lib_exact_dif_match(net, skb); |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 494 | int score, badness, matches = 0, reuseport = 0; |
| 495 | u32 hash = 0; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 496 | |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 497 | if (hslot->count > 10) { |
| 498 | hash2 = udp4_portaddr_hash(net, daddr, hnum); |
| 499 | slot2 = hash2 & udptable->mask; |
| 500 | hslot2 = &udptable->hash2[slot2]; |
| 501 | if (hslot->count < hslot2->count) |
| 502 | goto begin; |
| 503 | |
| 504 | result = udp4_lib_lookup2(net, saddr, sport, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 505 | daddr, hnum, dif, sdif, |
Robert Shearman | 63a6fff | 2017-01-26 18:02:24 +0000 | [diff] [blame] | 506 | exact_dif, hslot2, skb); |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 507 | if (!result) { |
Su, Xuemin | d1e3728 | 2016-06-13 11:02:50 +0800 | [diff] [blame] | 508 | unsigned int old_slot2 = slot2; |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 509 | hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum); |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 510 | slot2 = hash2 & udptable->mask; |
Su, Xuemin | d1e3728 | 2016-06-13 11:02:50 +0800 | [diff] [blame] | 511 | /* avoid searching the same slot again. */ |
| 512 | if (unlikely(slot2 == old_slot2)) |
| 513 | return result; |
| 514 | |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 515 | hslot2 = &udptable->hash2[slot2]; |
| 516 | if (hslot->count < hslot2->count) |
| 517 | goto begin; |
| 518 | |
Jorge Boncompte [DTI2] | 1223c67 | 2010-04-08 04:56:48 +0000 | [diff] [blame] | 519 | result = udp4_lib_lookup2(net, saddr, sport, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 520 | daddr, hnum, dif, sdif, |
Robert Shearman | 63a6fff | 2017-01-26 18:02:24 +0000 | [diff] [blame] | 521 | exact_dif, hslot2, skb); |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 522 | } |
Eric Dumazet | 5051ebd | 2009-11-08 10:18:11 +0000 | [diff] [blame] | 523 | return result; |
| 524 | } |
Eric Dumazet | 271b72c | 2008-10-29 02:11:14 -0700 | [diff] [blame] | 525 | begin: |
| 526 | result = NULL; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 527 | badness = 0; |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 528 | sk_for_each_rcu(sk, &hslot->head) { |
Su, Xuemin | d1e3728 | 2016-06-13 11:02:50 +0800 | [diff] [blame] | 529 | score = compute_score(sk, net, saddr, sport, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 530 | daddr, hnum, dif, sdif, exact_dif); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 531 | if (score > badness) { |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 532 | reuseport = sk->sk_reuseport; |
| 533 | if (reuseport) { |
Hannes Frederic Sowa | 65cd803 | 2013-10-19 21:48:51 +0200 | [diff] [blame] | 534 | hash = udp_ehashfn(net, daddr, hnum, |
| 535 | saddr, sport); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 536 | result = reuseport_select_sock(sk, hash, skb, |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 537 | sizeof(struct udphdr)); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 538 | if (result) |
| 539 | return result; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 540 | matches = 1; |
| 541 | } |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 542 | result = sk; |
| 543 | badness = score; |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 544 | } else if (score == badness && reuseport) { |
| 545 | matches++; |
Daniel Borkmann | 8fc54f6 | 2014-08-23 20:58:54 +0200 | [diff] [blame] | 546 | if (reciprocal_scale(hash, matches) == 0) |
Tom Herbert | ba418fa | 2013-01-22 09:50:32 +0000 | [diff] [blame] | 547 | result = sk; |
| 548 | hash = next_pseudo_random32(hash); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 549 | } |
| 550 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 551 | return result; |
| 552 | } |
Pavel Emelyanov | fce8233 | 2011-12-09 06:23:34 +0000 | [diff] [blame] | 553 | EXPORT_SYMBOL_GPL(__udp4_lib_lookup); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 554 | |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 555 | static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, |
| 556 | __be16 sport, __be16 dport, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 557 | struct udp_table *udptable) |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 558 | { |
| 559 | const struct iphdr *iph = ip_hdr(skb); |
| 560 | |
Alexander Duyck | ed7cbbc | 2016-05-12 16:23:44 -0700 | [diff] [blame] | 561 | return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, |
Eric Dumazet | 8afdd99 | 2013-12-10 18:07:23 -0800 | [diff] [blame] | 562 | iph->daddr, dport, inet_iif(skb), |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 563 | inet_sdif(skb), udptable, skb); |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Tom Herbert | 6305830 | 2016-04-05 08:22:50 -0700 | [diff] [blame] | 566 | struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, |
| 567 | __be16 sport, __be16 dport) |
| 568 | { |
Alexander Duyck | ed7cbbc | 2016-05-12 16:23:44 -0700 | [diff] [blame] | 569 | return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table); |
Tom Herbert | 6305830 | 2016-04-05 08:22:50 -0700 | [diff] [blame] | 570 | } |
| 571 | EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); |
| 572 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 573 | /* Must be called under rcu_read_lock(). |
| 574 | * Does increment socket refcount. |
| 575 | */ |
| 576 | #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \ |
Arnd Bergmann | 30f5815 | 2016-11-08 14:28:18 +0100 | [diff] [blame] | 577 | IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY) || \ |
| 578 | IS_ENABLED(CONFIG_NF_SOCKET_IPV4) |
KOVACS Krisztian | bcd4130 | 2008-10-01 07:48:10 -0700 | [diff] [blame] | 579 | struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, |
| 580 | __be32 daddr, __be16 dport, int dif) |
| 581 | { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 582 | struct sock *sk; |
| 583 | |
| 584 | sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 585 | dif, 0, &udp_table, NULL); |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 586 | if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 587 | sk = NULL; |
| 588 | return sk; |
KOVACS Krisztian | bcd4130 | 2008-10-01 07:48:10 -0700 | [diff] [blame] | 589 | } |
| 590 | EXPORT_SYMBOL_GPL(udp4_lib_lookup); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 591 | #endif |
KOVACS Krisztian | bcd4130 | 2008-10-01 07:48:10 -0700 | [diff] [blame] | 592 | |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 593 | static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, |
| 594 | __be16 loc_port, __be32 loc_addr, |
| 595 | __be16 rmt_port, __be32 rmt_addr, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 596 | int dif, int sdif, unsigned short hnum) |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 597 | { |
| 598 | struct inet_sock *inet = inet_sk(sk); |
| 599 | |
| 600 | if (!net_eq(sock_net(sk), net) || |
| 601 | udp_sk(sk)->udp_port_hash != hnum || |
| 602 | (inet->inet_daddr && inet->inet_daddr != rmt_addr) || |
| 603 | (inet->inet_dport != rmt_port && inet->inet_dport) || |
| 604 | (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || |
| 605 | ipv6_only_sock(sk) || |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 606 | (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif && |
| 607 | sk->sk_bound_dev_if != sdif)) |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 608 | return false; |
| 609 | if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif)) |
| 610 | return false; |
| 611 | return true; |
| 612 | } |
| 613 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 614 | /* |
| 615 | * This routine is called by the ICMP module when it gets some |
| 616 | * sort of error condition. If err < 0 then the socket should |
| 617 | * be closed and the error returned to the user. If err > 0 |
| 618 | * it's just the icmp type << 8 | icmp code. |
| 619 | * Header points to the ip header of the error packet. We move |
| 620 | * on past this. Then (as it used to claim before adjustment) |
| 621 | * header points to the first 8 bytes of the udp header. We need |
| 622 | * to find the appropriate port. |
| 623 | */ |
| 624 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 625 | void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 626 | { |
| 627 | struct inet_sock *inet; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 628 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 629 | struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 630 | const int type = icmp_hdr(skb)->type; |
| 631 | const int code = icmp_hdr(skb)->code; |
| 632 | struct sock *sk; |
| 633 | int harderr; |
| 634 | int err; |
Pavel Emelyanov | fd54d71 | 2008-07-14 23:01:40 -0700 | [diff] [blame] | 635 | struct net *net = dev_net(skb->dev); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 636 | |
Pavel Emelyanov | fd54d71 | 2008-07-14 23:01:40 -0700 | [diff] [blame] | 637 | sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 638 | iph->saddr, uh->source, skb->dev->ifindex, 0, |
| 639 | udptable, NULL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 640 | if (!sk) { |
Eric Dumazet | 5d3848b | 2016-04-27 16:44:29 -0700 | [diff] [blame] | 641 | __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 642 | return; /* No socket for error */ |
| 643 | } |
| 644 | |
| 645 | err = 0; |
| 646 | harderr = 0; |
| 647 | inet = inet_sk(sk); |
| 648 | |
| 649 | switch (type) { |
| 650 | default: |
| 651 | case ICMP_TIME_EXCEEDED: |
| 652 | err = EHOSTUNREACH; |
| 653 | break; |
| 654 | case ICMP_SOURCE_QUENCH: |
| 655 | goto out; |
| 656 | case ICMP_PARAMETERPROB: |
| 657 | err = EPROTO; |
| 658 | harderr = 1; |
| 659 | break; |
| 660 | case ICMP_DEST_UNREACH: |
| 661 | if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ |
David S. Miller | 3639339 | 2012-06-14 22:21:46 -0700 | [diff] [blame] | 662 | ipv4_sk_update_pmtu(skb, sk, info); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 663 | if (inet->pmtudisc != IP_PMTUDISC_DONT) { |
| 664 | err = EMSGSIZE; |
| 665 | harderr = 1; |
| 666 | break; |
| 667 | } |
| 668 | goto out; |
| 669 | } |
| 670 | err = EHOSTUNREACH; |
| 671 | if (code <= NR_ICMP_UNREACH) { |
| 672 | harderr = icmp_err_convert[code].fatal; |
| 673 | err = icmp_err_convert[code].errno; |
| 674 | } |
| 675 | break; |
David S. Miller | 55be7a9 | 2012-07-11 21:27:49 -0700 | [diff] [blame] | 676 | case ICMP_REDIRECT: |
| 677 | ipv4_sk_redirect(skb, sk); |
Duan Jiong | 1a462d1 | 2013-09-20 18:20:28 +0800 | [diff] [blame] | 678 | goto out; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* |
| 682 | * RFC1122: OK. Passes ICMP errors back to application, as per |
| 683 | * 4.1.3.3. |
| 684 | */ |
| 685 | if (!inet->recverr) { |
| 686 | if (!harderr || sk->sk_state != TCP_ESTABLISHED) |
| 687 | goto out; |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 688 | } else |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 689 | ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 690 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 691 | sk->sk_err = err; |
| 692 | sk->sk_error_report(sk); |
| 693 | out: |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 694 | return; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void udp_err(struct sk_buff *skb, u32 info) |
| 698 | { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 699 | __udp4_lib_err(skb, info, &udp_table); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Throw away all pending data and cancel the corking. Socket is locked. |
| 704 | */ |
Denis V. Lunev | 36d926b | 2008-06-04 15:49:07 +0400 | [diff] [blame] | 705 | void udp_flush_pending_frames(struct sock *sk) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 706 | { |
| 707 | struct udp_sock *up = udp_sk(sk); |
| 708 | |
| 709 | if (up->pending) { |
| 710 | up->len = 0; |
| 711 | up->pending = 0; |
| 712 | ip_flush_pending_frames(sk); |
| 713 | } |
| 714 | } |
Denis V. Lunev | 36d926b | 2008-06-04 15:49:07 +0400 | [diff] [blame] | 715 | EXPORT_SYMBOL(udp_flush_pending_frames); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 716 | |
| 717 | /** |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 718 | * udp4_hwcsum - handle outgoing HW checksumming |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 719 | * @skb: sk_buff containing the filled-in UDP header |
| 720 | * (checksum field must be zeroed out) |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 721 | * @src: source IP address |
| 722 | * @dst: destination IP address |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 723 | */ |
Thomas Graf | c26bf4a | 2013-07-25 18:12:18 +0200 | [diff] [blame] | 724 | void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 725 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 726 | struct udphdr *uh = udp_hdr(skb); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 727 | int offset = skb_transport_offset(skb); |
| 728 | int len = skb->len - offset; |
| 729 | int hlen = len; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 730 | __wsum csum = 0; |
| 731 | |
WANG Cong | ebbe495 | 2014-06-02 16:12:02 -0700 | [diff] [blame] | 732 | if (!skb_has_frag_list(skb)) { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 733 | /* |
| 734 | * Only one fragment on the socket. |
| 735 | */ |
| 736 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 737 | skb->csum_offset = offsetof(struct udphdr, check); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 738 | uh->check = ~csum_tcpudp_magic(src, dst, len, |
| 739 | IPPROTO_UDP, 0); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 740 | } else { |
WANG Cong | ebbe495 | 2014-06-02 16:12:02 -0700 | [diff] [blame] | 741 | struct sk_buff *frags; |
| 742 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 743 | /* |
| 744 | * HW-checksum won't work as there are two or more |
| 745 | * fragments on the socket so that all csums of sk_buffs |
| 746 | * should be together |
| 747 | */ |
WANG Cong | ebbe495 | 2014-06-02 16:12:02 -0700 | [diff] [blame] | 748 | skb_walk_frags(skb, frags) { |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 749 | csum = csum_add(csum, frags->csum); |
| 750 | hlen -= frags->len; |
WANG Cong | ebbe495 | 2014-06-02 16:12:02 -0700 | [diff] [blame] | 751 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 752 | |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 753 | csum = skb_checksum(skb, offset, hlen, csum); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 754 | skb->ip_summed = CHECKSUM_NONE; |
| 755 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 756 | uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); |
| 757 | if (uh->check == 0) |
| 758 | uh->check = CSUM_MANGLED_0; |
| 759 | } |
| 760 | } |
Thomas Graf | c26bf4a | 2013-07-25 18:12:18 +0200 | [diff] [blame] | 761 | EXPORT_SYMBOL_GPL(udp4_hwcsum); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 762 | |
Tom Herbert | af5fcba | 2014-06-04 17:19:48 -0700 | [diff] [blame] | 763 | /* Function to set UDP checksum for an IPv4 UDP packet. This is intended |
| 764 | * for the simple case like when setting the checksum for a UDP tunnel. |
| 765 | */ |
| 766 | void udp_set_csum(bool nocheck, struct sk_buff *skb, |
| 767 | __be32 saddr, __be32 daddr, int len) |
| 768 | { |
| 769 | struct udphdr *uh = udp_hdr(skb); |
| 770 | |
Edward Cree | 179bc67 | 2016-02-11 20:48:04 +0000 | [diff] [blame] | 771 | if (nocheck) { |
Tom Herbert | af5fcba | 2014-06-04 17:19:48 -0700 | [diff] [blame] | 772 | uh->check = 0; |
Edward Cree | 179bc67 | 2016-02-11 20:48:04 +0000 | [diff] [blame] | 773 | } else if (skb_is_gso(skb)) { |
Tom Herbert | af5fcba | 2014-06-04 17:19:48 -0700 | [diff] [blame] | 774 | uh->check = ~udp_v4_check(len, saddr, daddr, 0); |
Edward Cree | 179bc67 | 2016-02-11 20:48:04 +0000 | [diff] [blame] | 775 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 776 | uh->check = 0; |
| 777 | uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); |
| 778 | if (uh->check == 0) |
| 779 | uh->check = CSUM_MANGLED_0; |
Edward Cree | d75f130 | 2016-02-11 20:49:40 +0000 | [diff] [blame] | 780 | } else { |
Tom Herbert | af5fcba | 2014-06-04 17:19:48 -0700 | [diff] [blame] | 781 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 782 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 783 | skb->csum_offset = offsetof(struct udphdr, check); |
| 784 | uh->check = ~udp_v4_check(len, saddr, daddr, 0); |
Tom Herbert | af5fcba | 2014-06-04 17:19:48 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | EXPORT_SYMBOL(udp_set_csum); |
| 788 | |
David S. Miller | 79ab053 | 2011-05-09 13:31:04 -0700 | [diff] [blame] | 789 | static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4) |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 790 | { |
| 791 | struct sock *sk = skb->sk; |
| 792 | struct inet_sock *inet = inet_sk(sk); |
| 793 | struct udphdr *uh; |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 794 | int err = 0; |
| 795 | int is_udplite = IS_UDPLITE(sk); |
| 796 | int offset = skb_transport_offset(skb); |
| 797 | int len = skb->len - offset; |
| 798 | __wsum csum = 0; |
| 799 | |
| 800 | /* |
| 801 | * Create a UDP header |
| 802 | */ |
| 803 | uh = udp_hdr(skb); |
| 804 | uh->source = inet->inet_sport; |
David S. Miller | 79ab053 | 2011-05-09 13:31:04 -0700 | [diff] [blame] | 805 | uh->dest = fl4->fl4_dport; |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 806 | uh->len = htons(len); |
| 807 | uh->check = 0; |
| 808 | |
| 809 | if (is_udplite) /* UDP-Lite */ |
| 810 | csum = udplite_csum(skb); |
| 811 | |
Tom Herbert | 28448b8 | 2014-05-23 08:47:19 -0700 | [diff] [blame] | 812 | else if (sk->sk_no_check_tx) { /* UDP csum disabled */ |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 813 | |
| 814 | skb->ip_summed = CHECKSUM_NONE; |
| 815 | goto send; |
| 816 | |
| 817 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ |
| 818 | |
David S. Miller | 79ab053 | 2011-05-09 13:31:04 -0700 | [diff] [blame] | 819 | udp4_hwcsum(skb, fl4->saddr, fl4->daddr); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 820 | goto send; |
| 821 | |
| 822 | } else |
| 823 | csum = udp_csum(skb); |
| 824 | |
| 825 | /* add protocol-dependent pseudo-header */ |
David S. Miller | 79ab053 | 2011-05-09 13:31:04 -0700 | [diff] [blame] | 826 | uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 827 | sk->sk_protocol, csum); |
| 828 | if (uh->check == 0) |
| 829 | uh->check = CSUM_MANGLED_0; |
| 830 | |
| 831 | send: |
Eric Dumazet | b5ec8ee | 2012-08-10 02:22:47 +0000 | [diff] [blame] | 832 | err = ip_send_skb(sock_net(sk), skb); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 833 | if (err) { |
| 834 | if (err == -ENOBUFS && !inet->recverr) { |
Eric Dumazet | 6aef70a | 2016-04-27 16:44:27 -0700 | [diff] [blame] | 835 | UDP_INC_STATS(sock_net(sk), |
| 836 | UDP_MIB_SNDBUFERRORS, is_udplite); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 837 | err = 0; |
| 838 | } |
| 839 | } else |
Eric Dumazet | 6aef70a | 2016-04-27 16:44:27 -0700 | [diff] [blame] | 840 | UDP_INC_STATS(sock_net(sk), |
| 841 | UDP_MIB_OUTDATAGRAMS, is_udplite); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 842 | return err; |
| 843 | } |
| 844 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 845 | /* |
| 846 | * Push out all pending data as one UDP datagram. Socket is locked. |
| 847 | */ |
Hannes Frederic Sowa | 8822b64 | 2013-07-01 20:21:30 +0200 | [diff] [blame] | 848 | int udp_push_pending_frames(struct sock *sk) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 849 | { |
| 850 | struct udp_sock *up = udp_sk(sk); |
| 851 | struct inet_sock *inet = inet_sk(sk); |
David S. Miller | b6f21b2 | 2011-03-12 02:09:18 -0500 | [diff] [blame] | 852 | struct flowi4 *fl4 = &inet->cork.fl.u.ip4; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 853 | struct sk_buff *skb; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 854 | int err = 0; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 855 | |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 856 | skb = ip_finish_skb(sk, fl4); |
Herbert Xu | f6b9664f | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 857 | if (!skb) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 858 | goto out; |
| 859 | |
David S. Miller | 79ab053 | 2011-05-09 13:31:04 -0700 | [diff] [blame] | 860 | err = udp_send_skb(skb, fl4); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 861 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 862 | out: |
| 863 | up->len = 0; |
| 864 | up->pending = 0; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 865 | return err; |
| 866 | } |
Hannes Frederic Sowa | 8822b64 | 2013-07-01 20:21:30 +0200 | [diff] [blame] | 867 | EXPORT_SYMBOL(udp_push_pending_frames); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 868 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 869 | int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 870 | { |
| 871 | struct inet_sock *inet = inet_sk(sk); |
| 872 | struct udp_sock *up = udp_sk(sk); |
David S. Miller | e474995 | 2011-05-08 16:38:45 -0700 | [diff] [blame] | 873 | struct flowi4 fl4_stack; |
David S. Miller | b6f21b2 | 2011-03-12 02:09:18 -0500 | [diff] [blame] | 874 | struct flowi4 *fl4; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 875 | int ulen = len; |
| 876 | struct ipcm_cookie ipc; |
| 877 | struct rtable *rt = NULL; |
| 878 | int free = 0; |
| 879 | int connected = 0; |
| 880 | __be32 daddr, faddr, saddr; |
| 881 | __be16 dport; |
| 882 | u8 tos; |
| 883 | int err, is_udplite = IS_UDPLITE(sk); |
| 884 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; |
| 885 | int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); |
Herbert Xu | 903ab86 | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 886 | struct sk_buff *skb; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 887 | struct ip_options_data opt_copy; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 888 | |
| 889 | if (len > 0xFFFF) |
| 890 | return -EMSGSIZE; |
| 891 | |
| 892 | /* |
| 893 | * Check the flags. |
| 894 | */ |
| 895 | |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 896 | if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 897 | return -EOPNOTSUPP; |
| 898 | |
| 899 | ipc.opt = NULL; |
Oliver Hartkopp | 2244d07 | 2010-08-17 08:59:14 +0000 | [diff] [blame] | 900 | ipc.tx_flags = 0; |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 901 | ipc.ttl = 0; |
| 902 | ipc.tos = -1; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 903 | |
Herbert Xu | 903ab86 | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 904 | getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
| 905 | |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 906 | fl4 = &inet->cork.fl.u.ip4; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 907 | if (up->pending) { |
| 908 | /* |
| 909 | * There are pending frames. |
| 910 | * The socket lock must be held while it's corked. |
| 911 | */ |
| 912 | lock_sock(sk); |
| 913 | if (likely(up->pending)) { |
| 914 | if (unlikely(up->pending != AF_INET)) { |
| 915 | release_sock(sk); |
| 916 | return -EINVAL; |
| 917 | } |
| 918 | goto do_append_data; |
| 919 | } |
| 920 | release_sock(sk); |
| 921 | } |
| 922 | ulen += sizeof(struct udphdr); |
| 923 | |
| 924 | /* |
| 925 | * Get and verify the address. |
| 926 | */ |
| 927 | if (msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 928 | DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 929 | if (msg->msg_namelen < sizeof(*usin)) |
| 930 | return -EINVAL; |
| 931 | if (usin->sin_family != AF_INET) { |
| 932 | if (usin->sin_family != AF_UNSPEC) |
| 933 | return -EAFNOSUPPORT; |
| 934 | } |
| 935 | |
| 936 | daddr = usin->sin_addr.s_addr; |
| 937 | dport = usin->sin_port; |
| 938 | if (dport == 0) |
| 939 | return -EINVAL; |
| 940 | } else { |
| 941 | if (sk->sk_state != TCP_ESTABLISHED) |
| 942 | return -EDESTADDRREQ; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 943 | daddr = inet->inet_daddr; |
| 944 | dport = inet->inet_dport; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 945 | /* Open fast path for connected socket. |
| 946 | Route will not be used, if at least one option is set. |
| 947 | */ |
| 948 | connected = 1; |
| 949 | } |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 950 | |
| 951 | ipc.sockc.tsflags = sk->sk_tsflags; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 952 | ipc.addr = inet->inet_saddr; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 953 | ipc.oif = sk->sk_bound_dev_if; |
Daniel Borkmann | bf84a010 | 2013-04-14 08:08:13 +0000 | [diff] [blame] | 954 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 955 | if (msg->msg_controllen) { |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 956 | err = ip_cmsg_send(sk, msg, &ipc, sk->sk_family == AF_INET6); |
Eric Dumazet | 9194830 | 2016-02-04 06:23:28 -0800 | [diff] [blame] | 957 | if (unlikely(err)) { |
| 958 | kfree(ipc.opt); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 959 | return err; |
Eric Dumazet | 9194830 | 2016-02-04 06:23:28 -0800 | [diff] [blame] | 960 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 961 | if (ipc.opt) |
| 962 | free = 1; |
| 963 | connected = 0; |
| 964 | } |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 965 | if (!ipc.opt) { |
| 966 | struct ip_options_rcu *inet_opt; |
| 967 | |
| 968 | rcu_read_lock(); |
| 969 | inet_opt = rcu_dereference(inet->inet_opt); |
| 970 | if (inet_opt) { |
| 971 | memcpy(&opt_copy, inet_opt, |
| 972 | sizeof(*inet_opt) + inet_opt->opt.optlen); |
| 973 | ipc.opt = &opt_copy.opt; |
| 974 | } |
| 975 | rcu_read_unlock(); |
| 976 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 977 | |
| 978 | saddr = ipc.addr; |
| 979 | ipc.addr = faddr = daddr; |
| 980 | |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 981 | sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags); |
| 982 | |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 983 | if (ipc.opt && ipc.opt->opt.srr) { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 984 | if (!daddr) |
| 985 | return -EINVAL; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 986 | faddr = ipc.opt->opt.faddr; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 987 | connected = 0; |
| 988 | } |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 989 | tos = get_rttos(&ipc, inet); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 990 | if (sock_flag(sk, SOCK_LOCALROUTE) || |
| 991 | (msg->msg_flags & MSG_DONTROUTE) || |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 992 | (ipc.opt && ipc.opt->opt.is_strictroute)) { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 993 | tos |= RTO_ONLINK; |
| 994 | connected = 0; |
| 995 | } |
| 996 | |
| 997 | if (ipv4_is_multicast(daddr)) { |
| 998 | if (!ipc.oif) |
| 999 | ipc.oif = inet->mc_index; |
| 1000 | if (!saddr) |
| 1001 | saddr = inet->mc_addr; |
| 1002 | connected = 0; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1003 | } else if (!ipc.oif) |
| 1004 | ipc.oif = inet->uc_index; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1005 | |
| 1006 | if (connected) |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 1007 | rt = (struct rtable *)sk_dst_check(sk, 0); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1008 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1009 | if (!rt) { |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 1010 | struct net *net = sock_net(sk); |
David Ahern | 9a24abf | 2015-08-13 14:59:03 -0600 | [diff] [blame] | 1011 | __u8 flow_flags = inet_sk_flowi_flags(sk); |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 1012 | |
David S. Miller | e474995 | 2011-05-08 16:38:45 -0700 | [diff] [blame] | 1013 | fl4 = &fl4_stack; |
David Ahern | 9a24abf | 2015-08-13 14:59:03 -0600 | [diff] [blame] | 1014 | |
David S. Miller | e474995 | 2011-05-08 16:38:45 -0700 | [diff] [blame] | 1015 | flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos, |
David S. Miller | c0951cb | 2011-03-31 04:54:27 -0700 | [diff] [blame] | 1016 | RT_SCOPE_UNIVERSE, sk->sk_protocol, |
David Ahern | 9a24abf | 2015-08-13 14:59:03 -0600 | [diff] [blame] | 1017 | flow_flags, |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 1018 | faddr, saddr, dport, inet->inet_sport, |
| 1019 | sk->sk_uid); |
David S. Miller | c0951cb | 2011-03-31 04:54:27 -0700 | [diff] [blame] | 1020 | |
David S. Miller | e474995 | 2011-05-08 16:38:45 -0700 | [diff] [blame] | 1021 | security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); |
| 1022 | rt = ip_route_output_flow(net, fl4, sk); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 1023 | if (IS_ERR(rt)) { |
| 1024 | err = PTR_ERR(rt); |
David S. Miller | 06dc94b | 2011-03-03 10:38:01 -0800 | [diff] [blame] | 1025 | rt = NULL; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1026 | if (err == -ENETUNREACH) |
Eric Dumazet | f1d8cba | 2013-11-28 09:51:22 -0800 | [diff] [blame] | 1027 | IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1028 | goto out; |
| 1029 | } |
| 1030 | |
| 1031 | err = -EACCES; |
| 1032 | if ((rt->rt_flags & RTCF_BROADCAST) && |
| 1033 | !sock_flag(sk, SOCK_BROADCAST)) |
| 1034 | goto out; |
| 1035 | if (connected) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1036 | sk_dst_set(sk, dst_clone(&rt->dst)); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | if (msg->msg_flags&MSG_CONFIRM) |
| 1040 | goto do_confirm; |
| 1041 | back_from_confirm: |
| 1042 | |
David S. Miller | e474995 | 2011-05-08 16:38:45 -0700 | [diff] [blame] | 1043 | saddr = fl4->saddr; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1044 | if (!ipc.addr) |
David S. Miller | e474995 | 2011-05-08 16:38:45 -0700 | [diff] [blame] | 1045 | daddr = ipc.addr = fl4->daddr; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1046 | |
Herbert Xu | 903ab86 | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 1047 | /* Lockless fast path for the non-corking case. */ |
| 1048 | if (!corkreq) { |
Al Viro | f69e6d1 | 2014-11-24 13:23:40 -0500 | [diff] [blame] | 1049 | skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, |
Herbert Xu | 903ab86 | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 1050 | sizeof(struct udphdr), &ipc, &rt, |
| 1051 | msg->msg_flags); |
| 1052 | err = PTR_ERR(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 50c3a48 | 2013-01-22 06:32:49 +0000 | [diff] [blame] | 1053 | if (!IS_ERR_OR_NULL(skb)) |
David S. Miller | 79ab053 | 2011-05-09 13:31:04 -0700 | [diff] [blame] | 1054 | err = udp_send_skb(skb, fl4); |
Herbert Xu | 903ab86 | 2011-03-01 02:36:48 +0000 | [diff] [blame] | 1055 | goto out; |
| 1056 | } |
| 1057 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1058 | lock_sock(sk); |
| 1059 | if (unlikely(up->pending)) { |
| 1060 | /* The socket is already corked while preparing it. */ |
| 1061 | /* ... which is an evident application bug. --ANK */ |
| 1062 | release_sock(sk); |
| 1063 | |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 1064 | net_dbg_ratelimited("cork app bug 2\n"); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1065 | err = -EINVAL; |
| 1066 | goto out; |
| 1067 | } |
| 1068 | /* |
| 1069 | * Now cork the socket to pend data. |
| 1070 | */ |
David S. Miller | b6f21b2 | 2011-03-12 02:09:18 -0500 | [diff] [blame] | 1071 | fl4 = &inet->cork.fl.u.ip4; |
| 1072 | fl4->daddr = daddr; |
| 1073 | fl4->saddr = saddr; |
David S. Miller | 9cce96d | 2011-03-12 03:00:33 -0500 | [diff] [blame] | 1074 | fl4->fl4_dport = dport; |
| 1075 | fl4->fl4_sport = inet->inet_sport; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1076 | up->pending = AF_INET; |
| 1077 | |
| 1078 | do_append_data: |
| 1079 | up->len += ulen; |
Al Viro | f69e6d1 | 2014-11-24 13:23:40 -0500 | [diff] [blame] | 1080 | err = ip_append_data(sk, fl4, getfrag, msg, ulen, |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 1081 | sizeof(struct udphdr), &ipc, &rt, |
| 1082 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1083 | if (err) |
| 1084 | udp_flush_pending_frames(sk); |
| 1085 | else if (!corkreq) |
| 1086 | err = udp_push_pending_frames(sk); |
| 1087 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) |
| 1088 | up->pending = 0; |
| 1089 | release_sock(sk); |
| 1090 | |
| 1091 | out: |
| 1092 | ip_rt_put(rt); |
| 1093 | if (free) |
| 1094 | kfree(ipc.opt); |
| 1095 | if (!err) |
| 1096 | return len; |
| 1097 | /* |
| 1098 | * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting |
| 1099 | * ENOBUFS might not be good (it's not tunable per se), but otherwise |
| 1100 | * we don't have a good statistic (IpOutDiscards but it can be too many |
| 1101 | * things). We could add another new stat but at least for now that |
| 1102 | * seems like overkill. |
| 1103 | */ |
| 1104 | if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { |
Eric Dumazet | 6aef70a | 2016-04-27 16:44:27 -0700 | [diff] [blame] | 1105 | UDP_INC_STATS(sock_net(sk), |
| 1106 | UDP_MIB_SNDBUFERRORS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1107 | } |
| 1108 | return err; |
| 1109 | |
| 1110 | do_confirm: |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 1111 | if (msg->msg_flags & MSG_PROBE) |
| 1112 | dst_confirm_neigh(&rt->dst, &fl4->daddr); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1113 | if (!(msg->msg_flags&MSG_PROBE) || len) |
| 1114 | goto back_from_confirm; |
| 1115 | err = 0; |
| 1116 | goto out; |
| 1117 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 1118 | EXPORT_SYMBOL(udp_sendmsg); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1119 | |
| 1120 | int udp_sendpage(struct sock *sk, struct page *page, int offset, |
| 1121 | size_t size, int flags) |
| 1122 | { |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 1123 | struct inet_sock *inet = inet_sk(sk); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1124 | struct udp_sock *up = udp_sk(sk); |
| 1125 | int ret; |
| 1126 | |
Shawn Landden | d3f7d56 | 2013-11-24 22:36:28 -0800 | [diff] [blame] | 1127 | if (flags & MSG_SENDPAGE_NOTLAST) |
| 1128 | flags |= MSG_MORE; |
| 1129 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1130 | if (!up->pending) { |
| 1131 | struct msghdr msg = { .msg_flags = flags|MSG_MORE }; |
| 1132 | |
| 1133 | /* Call udp_sendmsg to specify destination address which |
| 1134 | * sendpage interface can't pass. |
| 1135 | * This will succeed only when the socket is connected. |
| 1136 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1137 | ret = udp_sendmsg(sk, &msg, 0); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1138 | if (ret < 0) |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | lock_sock(sk); |
| 1143 | |
| 1144 | if (unlikely(!up->pending)) { |
| 1145 | release_sock(sk); |
| 1146 | |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 1147 | net_dbg_ratelimited("udp cork app bug 3\n"); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1148 | return -EINVAL; |
| 1149 | } |
| 1150 | |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 1151 | ret = ip_append_page(sk, &inet->cork.fl.u.ip4, |
| 1152 | page, offset, size, flags); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1153 | if (ret == -EOPNOTSUPP) { |
| 1154 | release_sock(sk); |
| 1155 | return sock_no_sendpage(sk->sk_socket, page, offset, |
| 1156 | size, flags); |
| 1157 | } |
| 1158 | if (ret < 0) { |
| 1159 | udp_flush_pending_frames(sk); |
| 1160 | goto out; |
| 1161 | } |
| 1162 | |
| 1163 | up->len += size; |
| 1164 | if (!(up->corkflag || (flags&MSG_MORE))) |
| 1165 | ret = udp_push_pending_frames(sk); |
| 1166 | if (!ret) |
| 1167 | ret = size; |
| 1168 | out: |
| 1169 | release_sock(sk); |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
Paolo Abeni | dce4551 | 2017-07-25 17:57:47 +0200 | [diff] [blame] | 1173 | #define UDP_SKB_IS_STATELESS 0x80000000 |
| 1174 | |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1175 | static void udp_set_dev_scratch(struct sk_buff *skb) |
| 1176 | { |
Paolo Abeni | dce4551 | 2017-07-25 17:57:47 +0200 | [diff] [blame] | 1177 | struct udp_dev_scratch *scratch = udp_skb_scratch(skb); |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1178 | |
| 1179 | BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); |
Paolo Abeni | dce4551 | 2017-07-25 17:57:47 +0200 | [diff] [blame] | 1180 | scratch->_tsize_state = skb->truesize; |
| 1181 | #if BITS_PER_LONG == 64 |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1182 | scratch->len = skb->len; |
| 1183 | scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); |
| 1184 | scratch->is_linear = !skb_is_nonlinear(skb); |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1185 | #endif |
Paolo Abeni | 3bdefdf | 2017-08-03 18:07:08 +0200 | [diff] [blame] | 1186 | /* all head states execept sp (dst, sk, nf) are always cleared by |
| 1187 | * udp_rcv() and we need to preserve secpath, if present, to eventually |
| 1188 | * process IP_CMSG_PASSSEC at recvmsg() time |
| 1189 | */ |
| 1190 | if (likely(!skb_sec_path(skb))) |
Paolo Abeni | dce4551 | 2017-07-25 17:57:47 +0200 | [diff] [blame] | 1191 | scratch->_tsize_state |= UDP_SKB_IS_STATELESS; |
| 1192 | } |
| 1193 | |
| 1194 | static int udp_skb_truesize(struct sk_buff *skb) |
| 1195 | { |
| 1196 | return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; |
| 1197 | } |
| 1198 | |
| 1199 | static bool udp_skb_has_head_state(struct sk_buff *skb) |
| 1200 | { |
| 1201 | return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); |
| 1202 | } |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1203 | |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1204 | /* fully reclaim rmem/fwd memory allocated for skb */ |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1205 | static void udp_rmem_release(struct sock *sk, int size, int partial, |
| 1206 | bool rx_queue_lock_held) |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1207 | { |
Eric Dumazet | 6b229cf | 2016-12-08 11:41:56 -0800 | [diff] [blame] | 1208 | struct udp_sock *up = udp_sk(sk); |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1209 | struct sk_buff_head *sk_queue; |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1210 | int amt; |
| 1211 | |
Eric Dumazet | 6b229cf | 2016-12-08 11:41:56 -0800 | [diff] [blame] | 1212 | if (likely(partial)) { |
| 1213 | up->forward_deficit += size; |
| 1214 | size = up->forward_deficit; |
| 1215 | if (size < (sk->sk_rcvbuf >> 2) && |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1216 | !skb_queue_empty(&up->reader_queue)) |
Eric Dumazet | 6b229cf | 2016-12-08 11:41:56 -0800 | [diff] [blame] | 1217 | return; |
| 1218 | } else { |
| 1219 | size += up->forward_deficit; |
| 1220 | } |
| 1221 | up->forward_deficit = 0; |
| 1222 | |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1223 | /* acquire the sk_receive_queue for fwd allocated memory scheduling, |
| 1224 | * if the called don't held it already |
| 1225 | */ |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1226 | sk_queue = &sk->sk_receive_queue; |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1227 | if (!rx_queue_lock_held) |
| 1228 | spin_lock(&sk_queue->lock); |
| 1229 | |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1230 | |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1231 | sk->sk_forward_alloc += size; |
| 1232 | amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); |
| 1233 | sk->sk_forward_alloc -= amt; |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1234 | |
| 1235 | if (amt) |
| 1236 | __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); |
Eric Dumazet | 02ab0d1 | 2016-12-08 11:41:57 -0800 | [diff] [blame] | 1237 | |
| 1238 | atomic_sub(size, &sk->sk_rmem_alloc); |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1239 | |
| 1240 | /* this can save us from acquiring the rx queue lock on next receive */ |
| 1241 | skb_queue_splice_tail_init(sk_queue, &up->reader_queue); |
| 1242 | |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1243 | if (!rx_queue_lock_held) |
| 1244 | spin_unlock(&sk_queue->lock); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1247 | /* Note: called with reader_queue.lock held. |
Eric Dumazet | c84d949 | 2016-12-08 11:41:55 -0800 | [diff] [blame] | 1248 | * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch |
| 1249 | * This avoids a cache line miss while receive_queue lock is held. |
| 1250 | * Look at __udp_enqueue_schedule_skb() to find where this copy is done. |
| 1251 | */ |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1252 | void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1253 | { |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1254 | prefetch(&skb->data); |
| 1255 | udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1256 | } |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1257 | EXPORT_SYMBOL(udp_skb_destructor); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1258 | |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1259 | /* as above, but the caller held the rx queue lock, too */ |
Colin Ian King | 64f5102 | 2017-05-17 09:50:36 +0100 | [diff] [blame] | 1260 | static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1261 | { |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1262 | prefetch(&skb->data); |
| 1263 | udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 1266 | /* Idea of busylocks is to let producers grab an extra spinlock |
| 1267 | * to relieve pressure on the receive_queue spinlock shared by consumer. |
| 1268 | * Under flood, this means that only one producer can be in line |
| 1269 | * trying to acquire the receive_queue spinlock. |
| 1270 | * These busylock can be allocated on a per cpu manner, instead of a |
| 1271 | * per socket one (that would consume a cache line per socket) |
| 1272 | */ |
| 1273 | static int udp_busylocks_log __read_mostly; |
| 1274 | static spinlock_t *udp_busylocks __read_mostly; |
| 1275 | |
| 1276 | static spinlock_t *busylock_acquire(void *ptr) |
| 1277 | { |
| 1278 | spinlock_t *busy; |
| 1279 | |
| 1280 | busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); |
| 1281 | spin_lock(busy); |
| 1282 | return busy; |
| 1283 | } |
| 1284 | |
| 1285 | static void busylock_release(spinlock_t *busy) |
| 1286 | { |
| 1287 | if (busy) |
| 1288 | spin_unlock(busy); |
| 1289 | } |
| 1290 | |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1291 | int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) |
| 1292 | { |
| 1293 | struct sk_buff_head *list = &sk->sk_receive_queue; |
| 1294 | int rmem, delta, amt, err = -ENOMEM; |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 1295 | spinlock_t *busy = NULL; |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 1296 | int size; |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1297 | |
| 1298 | /* try to avoid the costly atomic add/sub pair when the receive |
| 1299 | * queue is full; always allow at least a packet |
| 1300 | */ |
| 1301 | rmem = atomic_read(&sk->sk_rmem_alloc); |
Paolo Abeni | 363dc73 | 2016-12-02 17:35:49 +0100 | [diff] [blame] | 1302 | if (rmem > sk->sk_rcvbuf) |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1303 | goto drop; |
| 1304 | |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 1305 | /* Under mem pressure, it might be helpful to help udp_recvmsg() |
| 1306 | * having linear skbs : |
| 1307 | * - Reduce memory overhead and thus increase receive queue capacity |
| 1308 | * - Less cache line misses at copyout() time |
| 1309 | * - Less work at consume_skb() (less alien page frag freeing) |
| 1310 | */ |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 1311 | if (rmem > (sk->sk_rcvbuf >> 1)) { |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 1312 | skb_condense(skb); |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 1313 | |
| 1314 | busy = busylock_acquire(sk); |
| 1315 | } |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 1316 | size = skb->truesize; |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1317 | udp_set_dev_scratch(skb); |
Eric Dumazet | c8c8b12 | 2016-12-07 09:19:33 -0800 | [diff] [blame] | 1318 | |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1319 | /* we drop only if the receive buf is full and the receive |
| 1320 | * queue contains some other skb |
| 1321 | */ |
| 1322 | rmem = atomic_add_return(size, &sk->sk_rmem_alloc); |
Paolo Abeni | 363dc73 | 2016-12-02 17:35:49 +0100 | [diff] [blame] | 1323 | if (rmem > (size + sk->sk_rcvbuf)) |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1324 | goto uncharge_drop; |
| 1325 | |
| 1326 | spin_lock(&list->lock); |
| 1327 | if (size >= sk->sk_forward_alloc) { |
| 1328 | amt = sk_mem_pages(size); |
| 1329 | delta = amt << SK_MEM_QUANTUM_SHIFT; |
| 1330 | if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { |
| 1331 | err = -ENOBUFS; |
| 1332 | spin_unlock(&list->lock); |
| 1333 | goto uncharge_drop; |
| 1334 | } |
| 1335 | |
| 1336 | sk->sk_forward_alloc += delta; |
| 1337 | } |
| 1338 | |
| 1339 | sk->sk_forward_alloc -= size; |
| 1340 | |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1341 | /* no need to setup a destructor, we will explicitly release the |
| 1342 | * forward allocated memory on dequeue |
| 1343 | */ |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1344 | sock_skb_set_dropcount(sk, skb); |
| 1345 | |
| 1346 | __skb_queue_tail(list, skb); |
| 1347 | spin_unlock(&list->lock); |
| 1348 | |
| 1349 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1350 | sk->sk_data_ready(sk); |
| 1351 | |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 1352 | busylock_release(busy); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1353 | return 0; |
| 1354 | |
| 1355 | uncharge_drop: |
| 1356 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 1357 | |
| 1358 | drop: |
| 1359 | atomic_inc(&sk->sk_drops); |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 1360 | busylock_release(busy); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1361 | return err; |
| 1362 | } |
| 1363 | EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); |
| 1364 | |
Paolo Abeni | c915fe1 | 2016-11-15 16:37:53 +0100 | [diff] [blame] | 1365 | void udp_destruct_sock(struct sock *sk) |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1366 | { |
| 1367 | /* reclaim completely the forward allocated memory */ |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1368 | struct udp_sock *up = udp_sk(sk); |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1369 | unsigned int total = 0; |
| 1370 | struct sk_buff *skb; |
| 1371 | |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1372 | skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); |
| 1373 | while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1374 | total += skb->truesize; |
| 1375 | kfree_skb(skb); |
| 1376 | } |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1377 | udp_rmem_release(sk, total, 0, true); |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1378 | |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1379 | inet_sock_destruct(sk); |
| 1380 | } |
Paolo Abeni | c915fe1 | 2016-11-15 16:37:53 +0100 | [diff] [blame] | 1381 | EXPORT_SYMBOL_GPL(udp_destruct_sock); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1382 | |
| 1383 | int udp_init_sock(struct sock *sk) |
| 1384 | { |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1385 | skb_queue_head_init(&udp_sk(sk)->reader_queue); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1386 | sk->sk_destruct = udp_destruct_sock; |
| 1387 | return 0; |
| 1388 | } |
| 1389 | EXPORT_SYMBOL_GPL(udp_init_sock); |
| 1390 | |
| 1391 | void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) |
| 1392 | { |
| 1393 | if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { |
| 1394 | bool slow = lock_sock_fast(sk); |
| 1395 | |
| 1396 | sk_peek_offset_bwd(sk, len); |
| 1397 | unlock_sock_fast(sk, slow); |
| 1398 | } |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 1399 | |
Paolo Abeni | dce4551 | 2017-07-25 17:57:47 +0200 | [diff] [blame] | 1400 | /* In the more common cases we cleared the head states previously, |
| 1401 | * see __udp_queue_rcv_skb(). |
Paolo Abeni | 0ddf3fb | 2017-07-18 11:57:55 +0200 | [diff] [blame] | 1402 | */ |
Paolo Abeni | dce4551 | 2017-07-25 17:57:47 +0200 | [diff] [blame] | 1403 | if (unlikely(udp_skb_has_head_state(skb))) |
Paolo Abeni | 0ddf3fb | 2017-07-18 11:57:55 +0200 | [diff] [blame] | 1404 | skb_release_head_state(skb); |
Paolo Abeni | 0a463c7 | 2017-06-12 11:23:42 +0200 | [diff] [blame] | 1405 | consume_stateless_skb(skb); |
Paolo Abeni | f970bd9 | 2016-10-21 13:55:46 +0200 | [diff] [blame] | 1406 | } |
| 1407 | EXPORT_SYMBOL_GPL(skb_consume_udp); |
| 1408 | |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1409 | static struct sk_buff *__first_packet_length(struct sock *sk, |
| 1410 | struct sk_buff_head *rcvq, |
| 1411 | int *total) |
| 1412 | { |
| 1413 | struct sk_buff *skb; |
| 1414 | |
Paolo Abeni | 9bd780f | 2017-06-23 14:19:51 +0200 | [diff] [blame] | 1415 | while ((skb = skb_peek(rcvq)) != NULL) { |
| 1416 | if (udp_lib_checksum_complete(skb)) { |
| 1417 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, |
| 1418 | IS_UDPLITE(sk)); |
| 1419 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, |
| 1420 | IS_UDPLITE(sk)); |
| 1421 | atomic_inc(&sk->sk_drops); |
| 1422 | __skb_unlink(skb, rcvq); |
| 1423 | *total += skb->truesize; |
| 1424 | kfree_skb(skb); |
| 1425 | } else { |
| 1426 | /* the csum related bits could be changed, refresh |
| 1427 | * the scratch area |
| 1428 | */ |
| 1429 | udp_set_dev_scratch(skb); |
| 1430 | break; |
| 1431 | } |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1432 | } |
| 1433 | return skb; |
| 1434 | } |
| 1435 | |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1436 | /** |
| 1437 | * first_packet_length - return length of first packet in receive queue |
| 1438 | * @sk: socket |
| 1439 | * |
| 1440 | * Drops all bad checksum frames, until a valid one is found. |
Eric Dumazet | e83c674 | 2016-08-23 13:59:33 -0700 | [diff] [blame] | 1441 | * Returns the length of found skb, or -1 if none is found. |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1442 | */ |
Eric Dumazet | e83c674 | 2016-08-23 13:59:33 -0700 | [diff] [blame] | 1443 | static int first_packet_length(struct sock *sk) |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1444 | { |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1445 | struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; |
| 1446 | struct sk_buff_head *sk_queue = &sk->sk_receive_queue; |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1447 | struct sk_buff *skb; |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1448 | int total = 0; |
Eric Dumazet | e83c674 | 2016-08-23 13:59:33 -0700 | [diff] [blame] | 1449 | int res; |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1450 | |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1451 | spin_lock_bh(&rcvq->lock); |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1452 | skb = __first_packet_length(sk, rcvq, &total); |
| 1453 | if (!skb && !skb_queue_empty(sk_queue)) { |
| 1454 | spin_lock(&sk_queue->lock); |
| 1455 | skb_queue_splice_tail_init(sk_queue, rcvq); |
| 1456 | spin_unlock(&sk_queue->lock); |
| 1457 | |
| 1458 | skb = __first_packet_length(sk, rcvq, &total); |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1459 | } |
Eric Dumazet | e83c674 | 2016-08-23 13:59:33 -0700 | [diff] [blame] | 1460 | res = skb ? skb->len : -1; |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1461 | if (total) |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1462 | udp_rmem_release(sk, total, 1, false); |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1463 | spin_unlock_bh(&rcvq->lock); |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 1464 | return res; |
| 1465 | } |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * IOCTL requests applicable to the UDP protocol |
| 1469 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 1472 | { |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1473 | switch (cmd) { |
| 1474 | case SIOCOUTQ: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 1476 | int amount = sk_wmem_alloc_get(sk); |
| 1477 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1478 | return put_user(amount, (int __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | } |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1480 | |
| 1481 | case SIOCINQ: |
| 1482 | { |
Eric Dumazet | e83c674 | 2016-08-23 13:59:33 -0700 | [diff] [blame] | 1483 | int amount = max_t(int, 0, first_packet_length(sk)); |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1484 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1485 | return put_user(amount, (int __user *)arg); |
| 1486 | } |
| 1487 | |
| 1488 | default: |
| 1489 | return -ENOIOCTLCMD; |
| 1490 | } |
| 1491 | |
| 1492 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 1494 | EXPORT_SYMBOL(udp_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1496 | struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, |
| 1497 | int noblock, int *peeked, int *off, int *err) |
| 1498 | { |
| 1499 | struct sk_buff_head *sk_queue = &sk->sk_receive_queue; |
| 1500 | struct sk_buff_head *queue; |
| 1501 | struct sk_buff *last; |
| 1502 | long timeo; |
| 1503 | int error; |
| 1504 | |
| 1505 | queue = &udp_sk(sk)->reader_queue; |
| 1506 | flags |= noblock ? MSG_DONTWAIT : 0; |
| 1507 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1508 | do { |
| 1509 | struct sk_buff *skb; |
| 1510 | |
| 1511 | error = sock_error(sk); |
| 1512 | if (error) |
| 1513 | break; |
| 1514 | |
| 1515 | error = -EAGAIN; |
| 1516 | *peeked = 0; |
| 1517 | do { |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1518 | spin_lock_bh(&queue->lock); |
| 1519 | skb = __skb_try_recv_from_queue(sk, queue, flags, |
| 1520 | udp_skb_destructor, |
Andrey Vagin | de321ed | 2017-05-17 11:39:05 -0700 | [diff] [blame] | 1521 | peeked, off, err, |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1522 | &last); |
| 1523 | if (skb) { |
| 1524 | spin_unlock_bh(&queue->lock); |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1525 | return skb; |
| 1526 | } |
| 1527 | |
| 1528 | if (skb_queue_empty(sk_queue)) { |
| 1529 | spin_unlock_bh(&queue->lock); |
| 1530 | goto busy_check; |
| 1531 | } |
| 1532 | |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1533 | /* refill the reader queue and walk it again |
| 1534 | * keep both queues locked to avoid re-acquiring |
| 1535 | * the sk_receive_queue lock if fwd memory scheduling |
| 1536 | * is needed. |
| 1537 | */ |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1538 | spin_lock(&sk_queue->lock); |
| 1539 | skb_queue_splice_tail_init(sk_queue, queue); |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1540 | |
| 1541 | skb = __skb_try_recv_from_queue(sk, queue, flags, |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1542 | udp_skb_dtor_locked, |
Andrey Vagin | de321ed | 2017-05-17 11:39:05 -0700 | [diff] [blame] | 1543 | peeked, off, err, |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1544 | &last); |
Paolo Abeni | 6dfb436 | 2017-05-16 11:20:15 +0200 | [diff] [blame] | 1545 | spin_unlock(&sk_queue->lock); |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1546 | spin_unlock_bh(&queue->lock); |
Andrey Vagin | de321ed | 2017-05-17 11:39:05 -0700 | [diff] [blame] | 1547 | if (skb) |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1548 | return skb; |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1549 | |
| 1550 | busy_check: |
| 1551 | if (!sk_can_busy_loop(sk)) |
| 1552 | break; |
| 1553 | |
| 1554 | sk_busy_loop(sk, flags & MSG_DONTWAIT); |
| 1555 | } while (!skb_queue_empty(sk_queue)); |
| 1556 | |
| 1557 | /* sk_queue is empty, reader_queue may contain peeked packets */ |
| 1558 | } while (timeo && |
| 1559 | !__skb_wait_for_more_packets(sk, &error, &timeo, |
| 1560 | (struct sk_buff *)sk_queue)); |
| 1561 | |
| 1562 | *err = error; |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | EXPORT_SYMBOL_GPL(__skb_recv_udp); |
| 1566 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1567 | /* |
| 1568 | * This should be easy, if there is something there we |
| 1569 | * return it, otherwise we block. |
| 1570 | */ |
| 1571 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1572 | int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, |
| 1573 | int flags, int *addr_len) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1574 | { |
| 1575 | struct inet_sock *inet = inet_sk(sk); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1576 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1577 | struct sk_buff *skb; |
David S. Miller | 59c2cda | 2011-12-01 14:12:55 -0500 | [diff] [blame] | 1578 | unsigned int ulen, copied; |
samanthakumar | 627d2d6 | 2016-04-05 12:41:16 -0400 | [diff] [blame] | 1579 | int peeked, peeking, off; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1580 | int err; |
| 1581 | int is_udplite = IS_UDPLITE(sk); |
Eric Dumazet | 197c949 | 2015-12-30 08:51:12 -0500 | [diff] [blame] | 1582 | bool checksum_valid = false; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1583 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1584 | if (flags & MSG_ERRQUEUE) |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 1585 | return ip_recv_error(sk, msg, len, addr_len); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1586 | |
| 1587 | try_again: |
samanthakumar | 627d2d6 | 2016-04-05 12:41:16 -0400 | [diff] [blame] | 1588 | peeking = off = sk_peek_offset(sk, flags); |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1589 | skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1590 | if (!skb) |
samanthakumar | 627d2d6 | 2016-04-05 12:41:16 -0400 | [diff] [blame] | 1591 | return err; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1592 | |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1593 | ulen = udp_skb_len(skb); |
David S. Miller | 59c2cda | 2011-12-01 14:12:55 -0500 | [diff] [blame] | 1594 | copied = len; |
samanthakumar | 627d2d6 | 2016-04-05 12:41:16 -0400 | [diff] [blame] | 1595 | if (copied > ulen - off) |
| 1596 | copied = ulen - off; |
David S. Miller | 59c2cda | 2011-12-01 14:12:55 -0500 | [diff] [blame] | 1597 | else if (copied < ulen) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1598 | msg->msg_flags |= MSG_TRUNC; |
| 1599 | |
| 1600 | /* |
| 1601 | * If checksum is needed at all, try to do it while copying the |
| 1602 | * data. If the data is truncated, or if we only want a partial |
| 1603 | * coverage checksum (UDP-Lite), do it before the copy. |
| 1604 | */ |
| 1605 | |
Eric Dumazet | d21dbdf | 2016-11-18 17:18:03 -0800 | [diff] [blame] | 1606 | if (copied < ulen || peeking || |
| 1607 | (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1608 | checksum_valid = udp_skb_csum_unnecessary(skb) || |
| 1609 | !__udp_lib_checksum_complete(skb); |
Eric Dumazet | 197c949 | 2015-12-30 08:51:12 -0500 | [diff] [blame] | 1610 | if (!checksum_valid) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1611 | goto csum_copy_err; |
| 1612 | } |
| 1613 | |
Paolo Abeni | b65ac44 | 2017-06-12 11:23:43 +0200 | [diff] [blame] | 1614 | if (checksum_valid || udp_skb_csum_unnecessary(skb)) { |
| 1615 | if (udp_skb_is_linear(skb)) |
| 1616 | err = copy_linear_skb(skb, copied, off, &msg->msg_iter); |
| 1617 | else |
| 1618 | err = skb_copy_datagram_msg(skb, off, msg, copied); |
| 1619 | } else { |
samanthakumar | 627d2d6 | 2016-04-05 12:41:16 -0400 | [diff] [blame] | 1620 | err = skb_copy_and_csum_datagram_msg(skb, off, msg); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1621 | |
| 1622 | if (err == -EINVAL) |
| 1623 | goto csum_copy_err; |
| 1624 | } |
| 1625 | |
Eric Dumazet | 22911fc | 2012-06-27 00:23:44 +0000 | [diff] [blame] | 1626 | if (unlikely(err)) { |
Eric Dumazet | 979402b | 2012-09-05 23:34:44 +0000 | [diff] [blame] | 1627 | if (!peeked) { |
| 1628 | atomic_inc(&sk->sk_drops); |
Eric Dumazet | 6aef70a | 2016-04-27 16:44:27 -0700 | [diff] [blame] | 1629 | UDP_INC_STATS(sock_net(sk), |
| 1630 | UDP_MIB_INERRORS, is_udplite); |
Eric Dumazet | 979402b | 2012-09-05 23:34:44 +0000 | [diff] [blame] | 1631 | } |
Paolo Abeni | 850cbad | 2016-10-21 13:55:47 +0200 | [diff] [blame] | 1632 | kfree_skb(skb); |
samanthakumar | 627d2d6 | 2016-04-05 12:41:16 -0400 | [diff] [blame] | 1633 | return err; |
Eric Dumazet | 22911fc | 2012-06-27 00:23:44 +0000 | [diff] [blame] | 1634 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1635 | |
| 1636 | if (!peeked) |
Eric Dumazet | 6aef70a | 2016-04-27 16:44:27 -0700 | [diff] [blame] | 1637 | UDP_INC_STATS(sock_net(sk), |
| 1638 | UDP_MIB_INDATAGRAMS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1639 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 1640 | sock_recv_ts_and_drops(msg, sk, skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1641 | |
| 1642 | /* Copy the address. */ |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 1643 | if (sin) { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1644 | sin->sin_family = AF_INET; |
| 1645 | sin->sin_port = udp_hdr(skb)->source; |
| 1646 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 1647 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 1648 | *addr_len = sizeof(*sin); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1649 | } |
| 1650 | if (inet->cmsg_flags) |
Paolo Abeni | ad95903 | 2016-11-04 11:28:58 +0100 | [diff] [blame] | 1651 | ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1652 | |
David S. Miller | 59c2cda | 2011-12-01 14:12:55 -0500 | [diff] [blame] | 1653 | err = copied; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1654 | if (flags & MSG_TRUNC) |
| 1655 | err = ulen; |
| 1656 | |
Paolo Abeni | 850cbad | 2016-10-21 13:55:47 +0200 | [diff] [blame] | 1657 | skb_consume_udp(sk, skb, peeking ? -err : err); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1658 | return err; |
| 1659 | |
| 1660 | csum_copy_err: |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 1661 | if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, |
| 1662 | udp_skb_destructor)) { |
Eric Dumazet | 6aef70a | 2016-04-27 16:44:27 -0700 | [diff] [blame] | 1663 | UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); |
| 1664 | UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
Eric Dumazet | 6a5dc9e | 2013-04-29 08:39:56 +0000 | [diff] [blame] | 1665 | } |
Paolo Abeni | 850cbad | 2016-10-21 13:55:47 +0200 | [diff] [blame] | 1666 | kfree_skb(skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1667 | |
Eric Dumazet | beb39db | 2015-05-30 09:16:53 -0700 | [diff] [blame] | 1668 | /* starting over for a new packet, but check if we need to yield */ |
| 1669 | cond_resched(); |
Xufeng Zhang | 9cfaa8d | 2011-06-21 10:43:40 +0000 | [diff] [blame] | 1670 | msg->msg_flags &= ~MSG_TRUNC; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1671 | goto try_again; |
| 1672 | } |
| 1673 | |
Eric Dumazet | 286c72d | 2016-10-20 09:39:40 -0700 | [diff] [blame] | 1674 | int __udp_disconnect(struct sock *sk, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | struct inet_sock *inet = inet_sk(sk); |
| 1677 | /* |
| 1678 | * 1003.1g - break association. |
| 1679 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | sk->sk_state = TCP_CLOSE; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1682 | inet->inet_daddr = 0; |
| 1683 | inet->inet_dport = 0; |
Tom Herbert | bdeab99 | 2011-08-14 19:45:55 +0000 | [diff] [blame] | 1684 | sock_rps_reset_rxhash(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | sk->sk_bound_dev_if = 0; |
| 1686 | if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) |
| 1687 | inet_reset_saddr(sk); |
| 1688 | |
| 1689 | if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { |
| 1690 | sk->sk_prot->unhash(sk); |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1691 | inet->inet_sport = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | } |
| 1693 | sk_dst_reset(sk); |
| 1694 | return 0; |
| 1695 | } |
Eric Dumazet | 286c72d | 2016-10-20 09:39:40 -0700 | [diff] [blame] | 1696 | EXPORT_SYMBOL(__udp_disconnect); |
| 1697 | |
| 1698 | int udp_disconnect(struct sock *sk, int flags) |
| 1699 | { |
| 1700 | lock_sock(sk); |
| 1701 | __udp_disconnect(sk, flags); |
| 1702 | release_sock(sk); |
| 1703 | return 0; |
| 1704 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 1705 | EXPORT_SYMBOL(udp_disconnect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 1707 | void udp_lib_unhash(struct sock *sk) |
| 1708 | { |
Eric Dumazet | 723b461 | 2008-11-25 13:55:15 -0800 | [diff] [blame] | 1709 | if (sk_hashed(sk)) { |
| 1710 | struct udp_table *udptable = sk->sk_prot->h.udp_table; |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 1711 | struct udp_hslot *hslot, *hslot2; |
| 1712 | |
| 1713 | hslot = udp_hashslot(udptable, sock_net(sk), |
| 1714 | udp_sk(sk)->udp_port_hash); |
| 1715 | hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 1716 | |
Eric Dumazet | 723b461 | 2008-11-25 13:55:15 -0800 | [diff] [blame] | 1717 | spin_lock_bh(&hslot->lock); |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1718 | if (rcu_access_pointer(sk->sk_reuseport_cb)) |
| 1719 | reuseport_detach_sock(sk); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1720 | if (sk_del_node_init_rcu(sk)) { |
Eric Dumazet | fdcc8aa9 | 2009-11-08 10:17:05 +0000 | [diff] [blame] | 1721 | hslot->count--; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1722 | inet_sk(sk)->inet_num = 0; |
Eric Dumazet | 723b461 | 2008-11-25 13:55:15 -0800 | [diff] [blame] | 1723 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 1724 | |
| 1725 | spin_lock(&hslot2->lock); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1726 | hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 1727 | hslot2->count--; |
| 1728 | spin_unlock(&hslot2->lock); |
Eric Dumazet | 723b461 | 2008-11-25 13:55:15 -0800 | [diff] [blame] | 1729 | } |
| 1730 | spin_unlock_bh(&hslot->lock); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 1731 | } |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 1732 | } |
| 1733 | EXPORT_SYMBOL(udp_lib_unhash); |
| 1734 | |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | * inet_rcv_saddr was changed, we must rehash secondary hash |
| 1737 | */ |
| 1738 | void udp_lib_rehash(struct sock *sk, u16 newhash) |
| 1739 | { |
| 1740 | if (sk_hashed(sk)) { |
| 1741 | struct udp_table *udptable = sk->sk_prot->h.udp_table; |
| 1742 | struct udp_hslot *hslot, *hslot2, *nhslot2; |
| 1743 | |
| 1744 | hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); |
| 1745 | nhslot2 = udp_hashslot2(udptable, newhash); |
| 1746 | udp_sk(sk)->udp_portaddr_hash = newhash; |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1747 | |
| 1748 | if (hslot2 != nhslot2 || |
| 1749 | rcu_access_pointer(sk->sk_reuseport_cb)) { |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 1750 | hslot = udp_hashslot(udptable, sock_net(sk), |
| 1751 | udp_sk(sk)->udp_port_hash); |
| 1752 | /* we must lock primary chain too */ |
| 1753 | spin_lock_bh(&hslot->lock); |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1754 | if (rcu_access_pointer(sk->sk_reuseport_cb)) |
| 1755 | reuseport_detach_sock(sk); |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 1756 | |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1757 | if (hslot2 != nhslot2) { |
| 1758 | spin_lock(&hslot2->lock); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1759 | hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1760 | hslot2->count--; |
| 1761 | spin_unlock(&hslot2->lock); |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 1762 | |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1763 | spin_lock(&nhslot2->lock); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1764 | hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, |
Craig Gallek | e32ea7e | 2016-01-04 17:41:46 -0500 | [diff] [blame] | 1765 | &nhslot2->head); |
| 1766 | nhslot2->count++; |
| 1767 | spin_unlock(&nhslot2->lock); |
| 1768 | } |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 1769 | |
| 1770 | spin_unlock_bh(&hslot->lock); |
| 1771 | } |
| 1772 | } |
| 1773 | } |
| 1774 | EXPORT_SYMBOL(udp_lib_rehash); |
| 1775 | |
| 1776 | static void udp_v4_rehash(struct sock *sk) |
| 1777 | { |
| 1778 | u16 new_hash = udp4_portaddr_hash(sock_net(sk), |
| 1779 | inet_sk(sk)->inet_rcv_saddr, |
| 1780 | inet_sk(sk)->inet_num); |
| 1781 | udp_lib_rehash(sk, new_hash); |
| 1782 | } |
| 1783 | |
Paolo Abeni | a3f96c4 | 2017-05-17 14:52:16 +0200 | [diff] [blame] | 1784 | static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1785 | { |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 1786 | int rc; |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1787 | |
Shawn Bohrer | 005ec97 | 2013-10-07 11:01:38 -0500 | [diff] [blame] | 1788 | if (inet_sk(sk)->inet_daddr) { |
Tom Herbert | bdeab99 | 2011-08-14 19:45:55 +0000 | [diff] [blame] | 1789 | sock_rps_save_rxhash(sk, skb); |
Shawn Bohrer | 005ec97 | 2013-10-07 11:01:38 -0500 | [diff] [blame] | 1790 | sk_mark_napi_id(sk, skb); |
Eric Dumazet | 2c8c56e | 2014-11-11 05:54:28 -0800 | [diff] [blame] | 1791 | sk_incoming_cpu_update(sk); |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 1792 | } else { |
| 1793 | sk_mark_napi_id_once(sk, skb); |
Shawn Bohrer | 005ec97 | 2013-10-07 11:01:38 -0500 | [diff] [blame] | 1794 | } |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 1795 | |
Paolo Abeni | 850cbad | 2016-10-21 13:55:47 +0200 | [diff] [blame] | 1796 | rc = __udp_enqueue_schedule_skb(sk, skb); |
Eric Dumazet | 766e9037 | 2009-10-14 20:40:11 -0700 | [diff] [blame] | 1797 | if (rc < 0) { |
| 1798 | int is_udplite = IS_UDPLITE(sk); |
| 1799 | |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1800 | /* Note that an ENOMEM error is charged twice */ |
Eric Dumazet | 766e9037 | 2009-10-14 20:40:11 -0700 | [diff] [blame] | 1801 | if (rc == -ENOMEM) |
Eric Dumazet | e61da9e | 2016-04-29 14:16:50 -0700 | [diff] [blame] | 1802 | UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 1803 | is_udplite); |
Eric Dumazet | e61da9e | 2016-04-29 14:16:50 -0700 | [diff] [blame] | 1804 | UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
Eric Dumazet | 766e9037 | 2009-10-14 20:40:11 -0700 | [diff] [blame] | 1805 | kfree_skb(skb); |
Satoru Moriya | 296f7ea | 2011-06-17 11:58:39 +0000 | [diff] [blame] | 1806 | trace_udp_fail_queue_rcv_skb(rc, sk); |
Eric Dumazet | 766e9037 | 2009-10-14 20:40:11 -0700 | [diff] [blame] | 1807 | return -1; |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | return 0; |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Eric Dumazet | 447167b | 2012-04-11 23:05:28 +0000 | [diff] [blame] | 1813 | static struct static_key udp_encap_needed __read_mostly; |
| 1814 | void udp_encap_enable(void) |
| 1815 | { |
| 1816 | if (!static_key_enabled(&udp_encap_needed)) |
| 1817 | static_key_slow_inc(&udp_encap_needed); |
| 1818 | } |
| 1819 | EXPORT_SYMBOL(udp_encap_enable); |
| 1820 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1821 | /* returns: |
| 1822 | * -1: error |
| 1823 | * 0: success |
| 1824 | * >0: "udp encap" protocol resubmission |
| 1825 | * |
| 1826 | * Note that in the success and error cases, the skb is assumed to |
| 1827 | * have either been requeued or freed. |
| 1828 | */ |
Paolo Abeni | a3f96c4 | 2017-05-17 14:52:16 +0200 | [diff] [blame] | 1829 | static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1830 | { |
| 1831 | struct udp_sock *up = udp_sk(sk); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1832 | int is_udplite = IS_UDPLITE(sk); |
| 1833 | |
| 1834 | /* |
| 1835 | * Charge it to the socket, dropping if the queue is full. |
| 1836 | */ |
| 1837 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 1838 | goto drop; |
| 1839 | nf_reset(skb); |
| 1840 | |
Eric Dumazet | 447167b | 2012-04-11 23:05:28 +0000 | [diff] [blame] | 1841 | if (static_key_false(&udp_encap_needed) && up->encap_type) { |
Eric Dumazet | 0ad92ad | 2011-11-01 12:56:59 +0000 | [diff] [blame] | 1842 | int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); |
| 1843 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1844 | /* |
| 1845 | * This is an encapsulation socket so pass the skb to |
| 1846 | * the socket's udp_encap_rcv() hook. Otherwise, just |
| 1847 | * fall through and pass this up the UDP socket. |
| 1848 | * up->encap_rcv() returns the following value: |
| 1849 | * =0 if skb was successfully passed to the encap |
| 1850 | * handler or was discarded by it. |
| 1851 | * >0 if skb should be passed on to UDP. |
| 1852 | * <0 if skb should be resubmitted as proto -N |
| 1853 | */ |
| 1854 | |
| 1855 | /* if we're overly short, let UDP handle it */ |
Eric Dumazet | 0ad92ad | 2011-11-01 12:56:59 +0000 | [diff] [blame] | 1856 | encap_rcv = ACCESS_ONCE(up->encap_rcv); |
Hannes Frederic Sowa | e5aed00 | 2016-05-19 15:58:33 +0200 | [diff] [blame] | 1857 | if (encap_rcv) { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1858 | int ret; |
| 1859 | |
Tom Herbert | 0a80966 | 2014-05-07 16:52:39 -0700 | [diff] [blame] | 1860 | /* Verify checksum before giving to encap */ |
| 1861 | if (udp_lib_checksum_complete(skb)) |
| 1862 | goto csum_error; |
| 1863 | |
Eric Dumazet | 0ad92ad | 2011-11-01 12:56:59 +0000 | [diff] [blame] | 1864 | ret = encap_rcv(sk, skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1865 | if (ret <= 0) { |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 1866 | __UDP_INC_STATS(sock_net(sk), |
| 1867 | UDP_MIB_INDATAGRAMS, |
| 1868 | is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1869 | return -ret; |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | /* FALLTHROUGH -- it's a UDP Packet */ |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * UDP-Lite specific tests, ignored on UDP sockets |
| 1878 | */ |
| 1879 | if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { |
| 1880 | |
| 1881 | /* |
| 1882 | * MIB statistics other than incrementing the error count are |
| 1883 | * disabled for the following two types of errors: these depend |
| 1884 | * on the application settings, not on the functioning of the |
| 1885 | * protocol stack as such. |
| 1886 | * |
| 1887 | * RFC 3828 here recommends (sec 3.3): "There should also be a |
| 1888 | * way ... to ... at least let the receiving application block |
| 1889 | * delivery of packets with coverage values less than a value |
| 1890 | * provided by the application." |
| 1891 | */ |
| 1892 | if (up->pcrlen == 0) { /* full coverage was set */ |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 1893 | net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", |
| 1894 | UDP_SKB_CB(skb)->cscov, skb->len); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1895 | goto drop; |
| 1896 | } |
| 1897 | /* The next case involves violating the min. coverage requested |
| 1898 | * by the receiver. This is subtle: if receiver wants x and x is |
| 1899 | * greater than the buffersize/MTU then receiver will complain |
| 1900 | * that it wants x while sender emits packets of smaller size y. |
| 1901 | * Therefore the above ...()->partial_cov statement is essential. |
| 1902 | */ |
| 1903 | if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 1904 | net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", |
| 1905 | UDP_SKB_CB(skb)->cscov, up->pcrlen); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1906 | goto drop; |
| 1907 | } |
| 1908 | } |
| 1909 | |
Paolo Abeni | dd99e42 | 2017-06-21 10:24:40 +0200 | [diff] [blame] | 1910 | prefetch(&sk->sk_rmem_alloc); |
Eric Dumazet | ce25d66 | 2016-06-02 14:52:43 -0700 | [diff] [blame] | 1911 | if (rcu_access_pointer(sk->sk_filter) && |
| 1912 | udp_lib_checksum_complete(skb)) |
samanthakumar | e6afc8a | 2016-04-05 12:41:15 -0400 | [diff] [blame] | 1913 | goto csum_error; |
Eric Dumazet | ce25d66 | 2016-06-02 14:52:43 -0700 | [diff] [blame] | 1914 | |
Daniel Borkmann | ba66bbe | 2016-07-25 18:06:12 +0200 | [diff] [blame] | 1915 | if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) |
Michal Kubeček | a612769 | 2016-07-08 17:52:33 +0200 | [diff] [blame] | 1916 | goto drop; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1917 | |
samanthakumar | e6afc8a | 2016-04-05 12:41:15 -0400 | [diff] [blame] | 1918 | udp_csum_pull_header(skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1919 | |
Shawn Bohrer | fbf8866 | 2013-10-07 11:01:40 -0500 | [diff] [blame] | 1920 | ipv4_pktinfo_prepare(sk, skb); |
Paolo Abeni | 850cbad | 2016-10-21 13:55:47 +0200 | [diff] [blame] | 1921 | return __udp_queue_rcv_skb(sk, skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1922 | |
Eric Dumazet | 6a5dc9e | 2013-04-29 08:39:56 +0000 | [diff] [blame] | 1923 | csum_error: |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 1924 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1925 | drop: |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 1926 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
Eric Dumazet | 8edf19c | 2009-10-15 00:12:40 +0000 | [diff] [blame] | 1927 | atomic_inc(&sk->sk_drops); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1928 | kfree_skb(skb); |
| 1929 | return -1; |
| 1930 | } |
| 1931 | |
Eric Dumazet | 9750223 | 2013-12-11 14:46:51 -0800 | [diff] [blame] | 1932 | /* For TCP sockets, sk_rx_dst is protected by socket lock |
Eric Dumazet | e47eb5d | 2013-12-15 10:53:46 -0800 | [diff] [blame] | 1933 | * For UDP, we use xchg() to guard against concurrent changes. |
Eric Dumazet | 9750223 | 2013-12-11 14:46:51 -0800 | [diff] [blame] | 1934 | */ |
Paolo Abeni | c9f2c1a | 2017-07-27 14:45:09 +0200 | [diff] [blame] | 1935 | void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 1936 | { |
Eric Dumazet | 9750223 | 2013-12-11 14:46:51 -0800 | [diff] [blame] | 1937 | struct dst_entry *old; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 1938 | |
Wei Wang | d24406c | 2017-06-17 10:42:25 -0700 | [diff] [blame] | 1939 | if (dst_hold_safe(dst)) { |
| 1940 | old = xchg(&sk->sk_rx_dst, dst); |
| 1941 | dst_release(old); |
| 1942 | } |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 1943 | } |
Paolo Abeni | c9f2c1a | 2017-07-27 14:45:09 +0200 | [diff] [blame] | 1944 | EXPORT_SYMBOL(udp_sk_rx_dst_set); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 1945 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1946 | /* |
| 1947 | * Multicasts and broadcasts go to each listener. |
| 1948 | * |
Eric Dumazet | 1240d13 | 2009-11-08 10:18:44 +0000 | [diff] [blame] | 1949 | * Note: called only from the BH handler context. |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1950 | */ |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 1951 | static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1952 | struct udphdr *uh, |
| 1953 | __be32 saddr, __be32 daddr, |
Rick Jones | 36cbb24 | 2014-11-06 10:37:54 -0800 | [diff] [blame] | 1954 | struct udp_table *udptable, |
| 1955 | int proto) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1956 | { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1957 | struct sock *sk, *first = NULL; |
David Held | 5cf3d46 | 2014-07-15 23:28:31 -0400 | [diff] [blame] | 1958 | unsigned short hnum = ntohs(uh->dest); |
| 1959 | struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); |
David Held | 2dc41cf | 2014-07-15 23:28:32 -0400 | [diff] [blame] | 1960 | unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1961 | unsigned int offset = offsetof(typeof(*sk), sk_node); |
| 1962 | int dif = skb->dev->ifindex; |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 1963 | int sdif = inet_sdif(skb); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1964 | struct hlist_node *node; |
| 1965 | struct sk_buff *nskb; |
David Held | 2dc41cf | 2014-07-15 23:28:32 -0400 | [diff] [blame] | 1966 | |
| 1967 | if (use_hash2) { |
| 1968 | hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & |
Pablo Neira | 73e2d5e | 2016-11-14 23:40:30 +0100 | [diff] [blame] | 1969 | udptable->mask; |
| 1970 | hash2 = udp4_portaddr_hash(net, daddr, hnum) & udptable->mask; |
David Held | 2dc41cf | 2014-07-15 23:28:32 -0400 | [diff] [blame] | 1971 | start_lookup: |
Pablo Neira | 73e2d5e | 2016-11-14 23:40:30 +0100 | [diff] [blame] | 1972 | hslot = &udptable->hash2[hash2]; |
David Held | 2dc41cf | 2014-07-15 23:28:32 -0400 | [diff] [blame] | 1973 | offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); |
| 1974 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1975 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1976 | sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { |
| 1977 | if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 1978 | uh->source, saddr, dif, sdif, hnum)) |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1979 | continue; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1980 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1981 | if (!first) { |
| 1982 | first = sk; |
| 1983 | continue; |
| 1984 | } |
| 1985 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 1986 | |
| 1987 | if (unlikely(!nskb)) { |
| 1988 | atomic_inc(&sk->sk_drops); |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 1989 | __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, |
| 1990 | IS_UDPLITE(sk)); |
| 1991 | __UDP_INC_STATS(net, UDP_MIB_INERRORS, |
| 1992 | IS_UDPLITE(sk)); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 1993 | continue; |
| 1994 | } |
| 1995 | if (udp_queue_rcv_skb(sk, nskb) > 0) |
| 1996 | consume_skb(nskb); |
| 1997 | } |
Eric Dumazet | 1240d13 | 2009-11-08 10:18:44 +0000 | [diff] [blame] | 1998 | |
David Held | 2dc41cf | 2014-07-15 23:28:32 -0400 | [diff] [blame] | 1999 | /* Also lookup *:port if we are using hash2 and haven't done so yet. */ |
| 2000 | if (use_hash2 && hash2 != hash2_any) { |
| 2001 | hash2 = hash2_any; |
| 2002 | goto start_lookup; |
| 2003 | } |
| 2004 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2005 | if (first) { |
| 2006 | if (udp_queue_rcv_skb(first, skb) > 0) |
| 2007 | consume_skb(skb); |
Eric Dumazet | 1240d13 | 2009-11-08 10:18:44 +0000 | [diff] [blame] | 2008 | } else { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2009 | kfree_skb(skb); |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 2010 | __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, |
| 2011 | proto == IPPROTO_UDPLITE); |
Eric Dumazet | 1240d13 | 2009-11-08 10:18:44 +0000 | [diff] [blame] | 2012 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | /* Initialize UDP checksum. If exited with zero value (success), |
| 2017 | * CHECKSUM_UNNECESSARY means, that no more checks are required. |
| 2018 | * Otherwise, csum completion requires chacksumming packet body, |
| 2019 | * including udp header and folding it to skb->csum. |
| 2020 | */ |
| 2021 | static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, |
| 2022 | int proto) |
| 2023 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2024 | int err; |
| 2025 | |
| 2026 | UDP_SKB_CB(skb)->partial_cov = 0; |
| 2027 | UDP_SKB_CB(skb)->cscov = skb->len; |
| 2028 | |
| 2029 | if (proto == IPPROTO_UDPLITE) { |
| 2030 | err = udplite_checksum_init(skb, uh); |
| 2031 | if (err) |
| 2032 | return err; |
| 2033 | } |
| 2034 | |
Hannes Frederic Sowa | b46d9f6 | 2016-06-12 12:02:46 +0200 | [diff] [blame] | 2035 | /* Note, we are only interested in != 0 or == 0, thus the |
| 2036 | * force to int. |
| 2037 | */ |
| 2038 | return (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, |
| 2039 | inet_compute_pseudo); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * All we need to do is get the socket, and then do a checksum. |
| 2044 | */ |
| 2045 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2046 | int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2047 | int proto) |
| 2048 | { |
| 2049 | struct sock *sk; |
Jesper Dangaard Brouer | 7b5e56f | 2009-02-05 15:05:45 -0800 | [diff] [blame] | 2050 | struct udphdr *uh; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2051 | unsigned short ulen; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2052 | struct rtable *rt = skb_rtable(skb); |
Jesper Dangaard Brouer | 2783ef2 | 2009-02-06 01:59:12 -0800 | [diff] [blame] | 2053 | __be32 saddr, daddr; |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 2054 | struct net *net = dev_net(skb->dev); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2055 | |
| 2056 | /* |
| 2057 | * Validate the packet. |
| 2058 | */ |
| 2059 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 2060 | goto drop; /* No space for header. */ |
| 2061 | |
Jesper Dangaard Brouer | 7b5e56f | 2009-02-05 15:05:45 -0800 | [diff] [blame] | 2062 | uh = udp_hdr(skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2063 | ulen = ntohs(uh->len); |
Bjørn Mork | ccc2d97 | 2010-05-06 03:44:34 +0000 | [diff] [blame] | 2064 | saddr = ip_hdr(skb)->saddr; |
| 2065 | daddr = ip_hdr(skb)->daddr; |
| 2066 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2067 | if (ulen > skb->len) |
| 2068 | goto short_packet; |
| 2069 | |
| 2070 | if (proto == IPPROTO_UDP) { |
| 2071 | /* UDP validates ulen. */ |
| 2072 | if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) |
| 2073 | goto short_packet; |
| 2074 | uh = udp_hdr(skb); |
| 2075 | } |
| 2076 | |
| 2077 | if (udp4_csum_init(skb, uh, proto)) |
| 2078 | goto csum_error; |
| 2079 | |
Eric Dumazet | 8afdd99 | 2013-12-10 18:07:23 -0800 | [diff] [blame] | 2080 | sk = skb_steal_sock(skb); |
| 2081 | if (sk) { |
Eric Dumazet | 9750223 | 2013-12-11 14:46:51 -0800 | [diff] [blame] | 2082 | struct dst_entry *dst = skb_dst(skb); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2083 | int ret; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2084 | |
Eric Dumazet | 9750223 | 2013-12-11 14:46:51 -0800 | [diff] [blame] | 2085 | if (unlikely(sk->sk_rx_dst != dst)) |
| 2086 | udp_sk_rx_dst_set(sk, dst); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2087 | |
| 2088 | ret = udp_queue_rcv_skb(sk, skb); |
Eric Dumazet | 8afdd99 | 2013-12-10 18:07:23 -0800 | [diff] [blame] | 2089 | sock_put(sk); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2090 | /* a return value > 0 means to resubmit the input, but |
| 2091 | * it wants the return to be -protocol, or 0 |
| 2092 | */ |
| 2093 | if (ret > 0) |
| 2094 | return -ret; |
| 2095 | return 0; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2096 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2097 | |
Fabian Frederick | c18450a | 2014-11-04 20:48:41 +0100 | [diff] [blame] | 2098 | if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) |
| 2099 | return __udp4_lib_mcast_deliver(net, skb, uh, |
Rick Jones | 36cbb24 | 2014-11-06 10:37:54 -0800 | [diff] [blame] | 2100 | saddr, daddr, udptable, proto); |
Fabian Frederick | c18450a | 2014-11-04 20:48:41 +0100 | [diff] [blame] | 2101 | |
| 2102 | sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 2103 | if (sk) { |
Eliezer Tamir | a5b5047 | 2013-06-10 11:40:00 +0300 | [diff] [blame] | 2104 | int ret; |
| 2105 | |
Tom Herbert | 224d019 | 2015-01-05 13:56:14 -0800 | [diff] [blame] | 2106 | if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) |
Tom Herbert | 2abb7cd | 2014-08-31 15:12:43 -0700 | [diff] [blame] | 2107 | skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, |
| 2108 | inet_compute_pseudo); |
| 2109 | |
Eliezer Tamir | a5b5047 | 2013-06-10 11:40:00 +0300 | [diff] [blame] | 2110 | ret = udp_queue_rcv_skb(sk, skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2111 | |
| 2112 | /* a return value > 0 means to resubmit the input, but |
| 2113 | * it wants the return to be -protocol, or 0 |
| 2114 | */ |
| 2115 | if (ret > 0) |
| 2116 | return -ret; |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
| 2120 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 2121 | goto drop; |
| 2122 | nf_reset(skb); |
| 2123 | |
| 2124 | /* No socket. Drop packet silently, if checksum is wrong */ |
| 2125 | if (udp_lib_checksum_complete(skb)) |
| 2126 | goto csum_error; |
| 2127 | |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 2128 | __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2129 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 2130 | |
| 2131 | /* |
| 2132 | * Hmm. We got an UDP packet to a port to which we |
| 2133 | * don't wanna listen. Ignore it. |
| 2134 | */ |
| 2135 | kfree_skb(skb); |
| 2136 | return 0; |
| 2137 | |
| 2138 | short_packet: |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 2139 | net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", |
| 2140 | proto == IPPROTO_UDPLITE ? "Lite" : "", |
| 2141 | &saddr, ntohs(uh->source), |
| 2142 | ulen, skb->len, |
| 2143 | &daddr, ntohs(uh->dest)); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2144 | goto drop; |
| 2145 | |
| 2146 | csum_error: |
| 2147 | /* |
| 2148 | * RFC1122: OK. Discards the bad packet silently (as far as |
| 2149 | * the network is concerned, anyway) as per 4.1.3.4 (MUST). |
| 2150 | */ |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 2151 | net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", |
| 2152 | proto == IPPROTO_UDPLITE ? "Lite" : "", |
| 2153 | &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), |
| 2154 | ulen); |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 2155 | __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2156 | drop: |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 2157 | __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2158 | kfree_skb(skb); |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2162 | /* We can only early demux multicast if there is a single matching socket. |
| 2163 | * If more than one socket found returns NULL |
| 2164 | */ |
| 2165 | static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, |
| 2166 | __be16 loc_port, __be32 loc_addr, |
| 2167 | __be16 rmt_port, __be32 rmt_addr, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 2168 | int dif, int sdif) |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2169 | { |
| 2170 | struct sock *sk, *result; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2171 | unsigned short hnum = ntohs(loc_port); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2172 | unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2173 | struct udp_hslot *hslot = &udp_table.hash[slot]; |
| 2174 | |
Eric Dumazet | 63c6f81 | 2014-06-12 16:13:06 -0700 | [diff] [blame] | 2175 | /* Do not bother scanning a too big list */ |
| 2176 | if (hslot->count > 10) |
| 2177 | return NULL; |
| 2178 | |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2179 | result = NULL; |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2180 | sk_for_each_rcu(sk, &hslot->head) { |
| 2181 | if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 2182 | rmt_port, rmt_addr, dif, sdif, hnum)) { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2183 | if (result) |
| 2184 | return NULL; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2185 | result = sk; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2186 | } |
| 2187 | } |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2188 | |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2189 | return result; |
| 2190 | } |
| 2191 | |
| 2192 | /* For unicast we should only early demux connected sockets or we can |
| 2193 | * break forwarding setups. The chains here can be long so only check |
| 2194 | * if the first socket is an exact match and if not move on. |
| 2195 | */ |
| 2196 | static struct sock *__udp4_lib_demux_lookup(struct net *net, |
| 2197 | __be16 loc_port, __be32 loc_addr, |
| 2198 | __be16 rmt_port, __be32 rmt_addr, |
| 2199 | int dif) |
| 2200 | { |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2201 | unsigned short hnum = ntohs(loc_port); |
| 2202 | unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum); |
| 2203 | unsigned int slot2 = hash2 & udp_table.mask; |
| 2204 | struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; |
Joe Perches | c722831 | 2014-05-13 20:30:07 -0700 | [diff] [blame] | 2205 | INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2206 | const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2207 | struct sock *sk; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2208 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2209 | udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { |
| 2210 | if (INET_MATCH(sk, net, acookie, rmt_addr, |
| 2211 | loc_addr, ports, dif)) |
| 2212 | return sk; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2213 | /* Only check first socket in chain */ |
| 2214 | break; |
| 2215 | } |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2216 | return NULL; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | void udp_v4_early_demux(struct sk_buff *skb) |
| 2220 | { |
Eric Dumazet | 610438b | 2013-12-11 08:10:05 -0800 | [diff] [blame] | 2221 | struct net *net = dev_net(skb->dev); |
| 2222 | const struct iphdr *iph; |
| 2223 | const struct udphdr *uh; |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2224 | struct sock *sk = NULL; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2225 | struct dst_entry *dst; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2226 | int dif = skb->dev->ifindex; |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 2227 | int sdif = inet_sdif(skb); |
Shawn Bohrer | 6e54030 | 2015-06-03 16:27:38 -0500 | [diff] [blame] | 2228 | int ours; |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2229 | |
| 2230 | /* validate the packet */ |
| 2231 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) |
| 2232 | return; |
| 2233 | |
Eric Dumazet | 610438b | 2013-12-11 08:10:05 -0800 | [diff] [blame] | 2234 | iph = ip_hdr(skb); |
| 2235 | uh = udp_hdr(skb); |
| 2236 | |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2237 | if (skb->pkt_type == PACKET_BROADCAST || |
Shawn Bohrer | 6e54030 | 2015-06-03 16:27:38 -0500 | [diff] [blame] | 2238 | skb->pkt_type == PACKET_MULTICAST) { |
| 2239 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); |
| 2240 | |
| 2241 | if (!in_dev) |
| 2242 | return; |
| 2243 | |
Paolo Abeni | ad0ea19 | 2016-03-22 09:19:38 +0100 | [diff] [blame] | 2244 | /* we are supposed to accept bcast packets */ |
| 2245 | if (skb->pkt_type == PACKET_MULTICAST) { |
| 2246 | ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, |
| 2247 | iph->protocol); |
| 2248 | if (!ours) |
| 2249 | return; |
| 2250 | } |
| 2251 | |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2252 | sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, |
David Ahern | fb74c27 | 2017-08-07 08:44:16 -0700 | [diff] [blame^] | 2253 | uh->source, iph->saddr, |
| 2254 | dif, sdif); |
Shawn Bohrer | 6e54030 | 2015-06-03 16:27:38 -0500 | [diff] [blame] | 2255 | } else if (skb->pkt_type == PACKET_HOST) { |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2256 | sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, |
| 2257 | uh->source, iph->saddr, dif); |
Shawn Bohrer | 6e54030 | 2015-06-03 16:27:38 -0500 | [diff] [blame] | 2258 | } |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2259 | |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 2260 | if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2261 | return; |
| 2262 | |
| 2263 | skb->sk = sk; |
Alexander Duyck | 82eabd9 | 2014-09-04 13:32:11 -0400 | [diff] [blame] | 2264 | skb->destructor = sock_efree; |
Eric Dumazet | 10e2eb8 | 2015-08-01 12:14:33 +0200 | [diff] [blame] | 2265 | dst = READ_ONCE(sk->sk_rx_dst); |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2266 | |
| 2267 | if (dst) |
| 2268 | dst = dst_check(dst, 0); |
Eric Dumazet | 10e2eb8 | 2015-08-01 12:14:33 +0200 | [diff] [blame] | 2269 | if (dst) { |
Wei Wang | d24406c | 2017-06-17 10:42:25 -0700 | [diff] [blame] | 2270 | /* set noref for now. |
| 2271 | * any place which wants to hold dst has to call |
| 2272 | * dst_hold_safe() |
| 2273 | */ |
| 2274 | skb_dst_set_noref(skb, dst); |
Eric Dumazet | 10e2eb8 | 2015-08-01 12:14:33 +0200 | [diff] [blame] | 2275 | } |
Shawn Bohrer | 421b388 | 2013-10-07 11:01:39 -0500 | [diff] [blame] | 2276 | } |
| 2277 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2278 | int udp_rcv(struct sk_buff *skb) |
| 2279 | { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2280 | return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 2283 | void udp_destroy_sock(struct sock *sk) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2284 | { |
Tom Parkin | 44046a5 | 2013-03-19 06:11:12 +0000 | [diff] [blame] | 2285 | struct udp_sock *up = udp_sk(sk); |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 2286 | bool slow = lock_sock_fast(sk); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2287 | udp_flush_pending_frames(sk); |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 2288 | unlock_sock_fast(sk, slow); |
Tom Parkin | 44046a5 | 2013-03-19 06:11:12 +0000 | [diff] [blame] | 2289 | if (static_key_false(&udp_encap_needed) && up->encap_type) { |
| 2290 | void (*encap_destroy)(struct sock *sk); |
| 2291 | encap_destroy = ACCESS_ONCE(up->encap_destroy); |
| 2292 | if (encap_destroy) |
| 2293 | encap_destroy(sk); |
| 2294 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2295 | } |
| 2296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | /* |
| 2298 | * Socket option code for UDP |
| 2299 | */ |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 2300 | int udp_lib_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2301 | char __user *optval, unsigned int optlen, |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 2302 | int (*push_pending_frames)(struct sock *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | { |
| 2304 | struct udp_sock *up = udp_sk(sk); |
Tom Herbert | 1c19448 | 2014-05-23 08:47:32 -0700 | [diff] [blame] | 2305 | int val, valbool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | int err = 0; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 2307 | int is_udplite = IS_UDPLITE(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2309 | if (optlen < sizeof(int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | return -EINVAL; |
| 2311 | |
| 2312 | if (get_user(val, (int __user *)optval)) |
| 2313 | return -EFAULT; |
| 2314 | |
Tom Herbert | 1c19448 | 2014-05-23 08:47:32 -0700 | [diff] [blame] | 2315 | valbool = val ? 1 : 0; |
| 2316 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2317 | switch (optname) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | case UDP_CORK: |
| 2319 | if (val != 0) { |
| 2320 | up->corkflag = 1; |
| 2321 | } else { |
| 2322 | up->corkflag = 0; |
| 2323 | lock_sock(sk); |
Joe Perches | 4243cdc | 2014-11-11 21:59:20 -0800 | [diff] [blame] | 2324 | push_pending_frames(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | release_sock(sk); |
| 2326 | } |
| 2327 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | case UDP_ENCAP: |
| 2330 | switch (val) { |
| 2331 | case 0: |
| 2332 | case UDP_ENCAP_ESPINUDP: |
| 2333 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
James Chapman | 067b207 | 2007-07-05 17:08:05 -0700 | [diff] [blame] | 2334 | up->encap_rcv = xfrm4_udp_encap_rcv; |
| 2335 | /* FALLTHROUGH */ |
James Chapman | 342f023 | 2007-06-27 15:37:46 -0700 | [diff] [blame] | 2336 | case UDP_ENCAP_L2TPINUDP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | up->encap_type = val; |
Eric Dumazet | 447167b | 2012-04-11 23:05:28 +0000 | [diff] [blame] | 2338 | udp_encap_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | break; |
| 2340 | default: |
| 2341 | err = -ENOPROTOOPT; |
| 2342 | break; |
| 2343 | } |
| 2344 | break; |
| 2345 | |
Tom Herbert | 1c19448 | 2014-05-23 08:47:32 -0700 | [diff] [blame] | 2346 | case UDP_NO_CHECK6_TX: |
| 2347 | up->no_check6_tx = valbool; |
| 2348 | break; |
| 2349 | |
| 2350 | case UDP_NO_CHECK6_RX: |
| 2351 | up->no_check6_rx = valbool; |
| 2352 | break; |
| 2353 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2354 | /* |
| 2355 | * UDP-Lite's partial checksum coverage (RFC 3828). |
| 2356 | */ |
| 2357 | /* The sender sets actual checksum coverage length via this option. |
| 2358 | * The case coverage > packet length is handled by send module. */ |
| 2359 | case UDPLITE_SEND_CSCOV: |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 2360 | if (!is_udplite) /* Disable the option on UDP sockets */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2361 | return -ENOPROTOOPT; |
| 2362 | if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ |
| 2363 | val = 8; |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 2364 | else if (val > USHRT_MAX) |
| 2365 | val = USHRT_MAX; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2366 | up->pcslen = val; |
| 2367 | up->pcflag |= UDPLITE_SEND_CC; |
| 2368 | break; |
| 2369 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2370 | /* The receiver specifies a minimum checksum coverage value. To make |
| 2371 | * sense, this should be set to at least 8 (as done below). If zero is |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2372 | * used, this again means full checksum coverage. */ |
| 2373 | case UDPLITE_RECV_CSCOV: |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 2374 | if (!is_udplite) /* Disable the option on UDP sockets */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2375 | return -ENOPROTOOPT; |
| 2376 | if (val != 0 && val < 8) /* Avoid silly minimal values. */ |
| 2377 | val = 8; |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 2378 | else if (val > USHRT_MAX) |
| 2379 | val = USHRT_MAX; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2380 | up->pcrlen = val; |
| 2381 | up->pcflag |= UDPLITE_RECV_CC; |
| 2382 | break; |
| 2383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | default: |
| 2385 | err = -ENOPROTOOPT; |
| 2386 | break; |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
| 2389 | return err; |
| 2390 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2391 | EXPORT_SYMBOL(udp_lib_setsockopt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2393 | int udp_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2394 | char __user *optval, unsigned int optlen) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2395 | { |
| 2396 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 2397 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 2398 | udp_push_pending_frames); |
| 2399 | return ip_setsockopt(sk, level, optname, optval, optlen); |
| 2400 | } |
| 2401 | |
| 2402 | #ifdef CONFIG_COMPAT |
| 2403 | int compat_udp_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2404 | char __user *optval, unsigned int optlen) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2405 | { |
| 2406 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 2407 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 2408 | udp_push_pending_frames); |
| 2409 | return compat_ip_setsockopt(sk, level, optname, optval, optlen); |
| 2410 | } |
| 2411 | #endif |
| 2412 | |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 2413 | int udp_lib_getsockopt(struct sock *sk, int level, int optname, |
| 2414 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | { |
| 2416 | struct udp_sock *up = udp_sk(sk); |
| 2417 | int val, len; |
| 2418 | |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2419 | if (get_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | return -EFAULT; |
| 2421 | |
| 2422 | len = min_t(unsigned int, len, sizeof(int)); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2423 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2424 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | return -EINVAL; |
| 2426 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2427 | switch (optname) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | case UDP_CORK: |
| 2429 | val = up->corkflag; |
| 2430 | break; |
| 2431 | |
| 2432 | case UDP_ENCAP: |
| 2433 | val = up->encap_type; |
| 2434 | break; |
| 2435 | |
Tom Herbert | 1c19448 | 2014-05-23 08:47:32 -0700 | [diff] [blame] | 2436 | case UDP_NO_CHECK6_TX: |
| 2437 | val = up->no_check6_tx; |
| 2438 | break; |
| 2439 | |
| 2440 | case UDP_NO_CHECK6_RX: |
| 2441 | val = up->no_check6_rx; |
| 2442 | break; |
| 2443 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2444 | /* The following two cannot be changed on UDP sockets, the return is |
| 2445 | * always 0 (which corresponds to the full checksum coverage of UDP). */ |
| 2446 | case UDPLITE_SEND_CSCOV: |
| 2447 | val = up->pcslen; |
| 2448 | break; |
| 2449 | |
| 2450 | case UDPLITE_RECV_CSCOV: |
| 2451 | val = up->pcrlen; |
| 2452 | break; |
| 2453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | default: |
| 2455 | return -ENOPROTOOPT; |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2458 | if (put_user(len, optlen)) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2459 | return -EFAULT; |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2460 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | return -EFAULT; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2462 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2464 | EXPORT_SYMBOL(udp_lib_getsockopt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2466 | int udp_getsockopt(struct sock *sk, int level, int optname, |
| 2467 | char __user *optval, int __user *optlen) |
| 2468 | { |
| 2469 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 2470 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
| 2471 | return ip_getsockopt(sk, level, optname, optval, optlen); |
| 2472 | } |
| 2473 | |
| 2474 | #ifdef CONFIG_COMPAT |
| 2475 | int compat_udp_getsockopt(struct sock *sk, int level, int optname, |
| 2476 | char __user *optval, int __user *optlen) |
| 2477 | { |
| 2478 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 2479 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
| 2480 | return compat_ip_getsockopt(sk, level, optname, optval, optlen); |
| 2481 | } |
| 2482 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | /** |
| 2484 | * udp_poll - wait for a UDP event. |
| 2485 | * @file - file struct |
| 2486 | * @sock - socket |
| 2487 | * @wait - poll table |
| 2488 | * |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2489 | * This is same as datagram poll, except for the special case of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | * blocking sockets. If application is using a blocking fd |
| 2491 | * and a packet with checksum error is in the queue; |
| 2492 | * then it could get return from select indicating data available |
| 2493 | * but then block when reading it. Add special case code |
| 2494 | * to work around these arguably broken applications. |
| 2495 | */ |
| 2496 | unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) |
| 2497 | { |
| 2498 | unsigned int mask = datagram_poll(file, sock, wait); |
| 2499 | struct sock *sk = sock->sk; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 2500 | |
Paolo Abeni | 2276f58 | 2017-05-16 11:20:14 +0200 | [diff] [blame] | 2501 | if (!skb_queue_empty(&udp_sk(sk)->reader_queue)) |
| 2502 | mask |= POLLIN | POLLRDNORM; |
| 2503 | |
David Majnemer | c3f1dba | 2013-05-31 13:15:38 +0000 | [diff] [blame] | 2504 | sock_rps_record_flow(sk); |
| 2505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | /* Check for false positives due to checksum errors */ |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 2507 | if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) && |
Eric Dumazet | e83c674 | 2016-08-23 13:59:33 -0700 | [diff] [blame] | 2508 | !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) |
Eric Dumazet | 8558467 | 2009-10-09 04:43:40 +0000 | [diff] [blame] | 2509 | mask &= ~(POLLIN | POLLRDNORM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | |
| 2511 | return mask; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2514 | EXPORT_SYMBOL(udp_poll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | |
David Ahern | 5d77dca | 2016-08-23 21:06:33 -0700 | [diff] [blame] | 2516 | int udp_abort(struct sock *sk, int err) |
| 2517 | { |
| 2518 | lock_sock(sk); |
| 2519 | |
| 2520 | sk->sk_err = err; |
| 2521 | sk->sk_error_report(sk); |
Eric Dumazet | 286c72d | 2016-10-20 09:39:40 -0700 | [diff] [blame] | 2522 | __udp_disconnect(sk, 0); |
David Ahern | 5d77dca | 2016-08-23 21:06:33 -0700 | [diff] [blame] | 2523 | |
| 2524 | release_sock(sk); |
| 2525 | |
| 2526 | return 0; |
| 2527 | } |
| 2528 | EXPORT_SYMBOL_GPL(udp_abort); |
| 2529 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2530 | struct proto udp_prot = { |
| 2531 | .name = "UDP", |
| 2532 | .owner = THIS_MODULE, |
| 2533 | .close = udp_lib_close, |
| 2534 | .connect = ip4_datagram_connect, |
| 2535 | .disconnect = udp_disconnect, |
| 2536 | .ioctl = udp_ioctl, |
Paolo Abeni | 850cbad | 2016-10-21 13:55:47 +0200 | [diff] [blame] | 2537 | .init = udp_init_sock, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2538 | .destroy = udp_destroy_sock, |
| 2539 | .setsockopt = udp_setsockopt, |
| 2540 | .getsockopt = udp_getsockopt, |
| 2541 | .sendmsg = udp_sendmsg, |
| 2542 | .recvmsg = udp_recvmsg, |
| 2543 | .sendpage = udp_sendpage, |
Steffen Klassert | 8141ed9 | 2013-01-21 02:00:03 +0000 | [diff] [blame] | 2544 | .release_cb = ip4_datagram_release_cb, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2545 | .hash = udp_lib_hash, |
| 2546 | .unhash = udp_lib_unhash, |
Eric Dumazet | 719f835 | 2010-09-08 05:08:44 +0000 | [diff] [blame] | 2547 | .rehash = udp_v4_rehash, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2548 | .get_port = udp_v4_get_port, |
| 2549 | .memory_allocated = &udp_memory_allocated, |
| 2550 | .sysctl_mem = sysctl_udp_mem, |
| 2551 | .sysctl_wmem = &sysctl_udp_wmem_min, |
| 2552 | .sysctl_rmem = &sysctl_udp_rmem_min, |
| 2553 | .obj_size = sizeof(struct udp_sock), |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2554 | .h.udp_table = &udp_table, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2555 | #ifdef CONFIG_COMPAT |
| 2556 | .compat_setsockopt = compat_udp_setsockopt, |
| 2557 | .compat_getsockopt = compat_udp_getsockopt, |
| 2558 | #endif |
David Ahern | 5d77dca | 2016-08-23 21:06:33 -0700 | [diff] [blame] | 2559 | .diag_destroy = udp_abort, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2560 | }; |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2561 | EXPORT_SYMBOL(udp_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | |
| 2563 | /* ------------------------------------------------------------------------ */ |
| 2564 | #ifdef CONFIG_PROC_FS |
| 2565 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2566 | static struct sock *udp_get_first(struct seq_file *seq, int start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | { |
| 2568 | struct sock *sk; |
| 2569 | struct udp_iter_state *state = seq->private; |
Denis V. Lunev | 6f191ef | 2008-03-28 18:23:33 -0700 | [diff] [blame] | 2570 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2572 | for (state->bucket = start; state->bucket <= state->udp_table->mask; |
| 2573 | ++state->bucket) { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2574 | struct udp_hslot *hslot = &state->udp_table->hash[state->bucket]; |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2575 | |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2576 | if (hlist_empty(&hslot->head)) |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2577 | continue; |
| 2578 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2579 | spin_lock_bh(&hslot->lock); |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2580 | sk_for_each(sk, &hslot->head) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 2581 | if (!net_eq(sock_net(sk), net)) |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 2582 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | if (sk->sk_family == state->family) |
| 2584 | goto found; |
| 2585 | } |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2586 | spin_unlock_bh(&hslot->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | } |
| 2588 | sk = NULL; |
| 2589 | found: |
| 2590 | return sk; |
| 2591 | } |
| 2592 | |
| 2593 | static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) |
| 2594 | { |
| 2595 | struct udp_iter_state *state = seq->private; |
Denis V. Lunev | 6f191ef | 2008-03-28 18:23:33 -0700 | [diff] [blame] | 2596 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | |
| 2598 | do { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2599 | sk = sk_next(sk); |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 2600 | } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2602 | if (!sk) { |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2603 | if (state->bucket <= state->udp_table->mask) |
Vitaly Mayatskikh | 30842f2 | 2009-03-23 15:22:33 -0700 | [diff] [blame] | 2604 | spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2605 | return udp_get_first(seq, state->bucket + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | } |
| 2607 | return sk; |
| 2608 | } |
| 2609 | |
| 2610 | static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) |
| 2611 | { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2612 | struct sock *sk = udp_get_first(seq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | |
| 2614 | if (sk) |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 2615 | while (pos && (sk = udp_get_next(seq, sk)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | --pos; |
| 2617 | return pos ? NULL : sk; |
| 2618 | } |
| 2619 | |
| 2620 | static void *udp_seq_start(struct seq_file *seq, loff_t *pos) |
| 2621 | { |
Vitaly Mayatskikh | 30842f2 | 2009-03-23 15:22:33 -0700 | [diff] [blame] | 2622 | struct udp_iter_state *state = seq->private; |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2623 | state->bucket = MAX_UDP_PORTS; |
Vitaly Mayatskikh | 30842f2 | 2009-03-23 15:22:33 -0700 | [diff] [blame] | 2624 | |
YOSHIFUJI Hideaki | b50660f | 2008-03-31 19:38:15 -0700 | [diff] [blame] | 2625 | return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2629 | { |
| 2630 | struct sock *sk; |
| 2631 | |
YOSHIFUJI Hideaki | b50660f | 2008-03-31 19:38:15 -0700 | [diff] [blame] | 2632 | if (v == SEQ_START_TOKEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | sk = udp_get_idx(seq, 0); |
| 2634 | else |
| 2635 | sk = udp_get_next(seq, v); |
| 2636 | |
| 2637 | ++*pos; |
| 2638 | return sk; |
| 2639 | } |
| 2640 | |
| 2641 | static void udp_seq_stop(struct seq_file *seq, void *v) |
| 2642 | { |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2643 | struct udp_iter_state *state = seq->private; |
| 2644 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2645 | if (state->bucket <= state->udp_table->mask) |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2646 | spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } |
| 2648 | |
Arjan van de Ven | 73cb88e | 2011-10-30 06:46:30 +0000 | [diff] [blame] | 2649 | int udp_seq_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 2651 | struct udp_seq_afinfo *afinfo = PDE_DATA(inode); |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 2652 | struct udp_iter_state *s; |
| 2653 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 2655 | err = seq_open_net(inode, file, &afinfo->seq_ops, |
| 2656 | sizeof(struct udp_iter_state)); |
| 2657 | if (err < 0) |
| 2658 | return err; |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 2659 | |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 2660 | s = ((struct seq_file *)file->private_data)->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | s->family = afinfo->family; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2662 | s->udp_table = afinfo->udp_table; |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 2663 | return err; |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 2664 | } |
Arjan van de Ven | 73cb88e | 2011-10-30 06:46:30 +0000 | [diff] [blame] | 2665 | EXPORT_SYMBOL(udp_seq_open); |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 2666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | /* ------------------------------------------------------------------------ */ |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 2668 | int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | { |
| 2670 | struct proc_dir_entry *p; |
| 2671 | int rc = 0; |
| 2672 | |
Denis V. Lunev | dda6192 | 2008-03-28 18:24:26 -0700 | [diff] [blame] | 2673 | afinfo->seq_ops.start = udp_seq_start; |
| 2674 | afinfo->seq_ops.next = udp_seq_next; |
| 2675 | afinfo->seq_ops.stop = udp_seq_stop; |
| 2676 | |
Denis V. Lunev | 84841c3 | 2008-05-02 04:10:08 -0700 | [diff] [blame] | 2677 | p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, |
Arjan van de Ven | 73cb88e | 2011-10-30 06:46:30 +0000 | [diff] [blame] | 2678 | afinfo->seq_fops, afinfo); |
Denis V. Lunev | 84841c3 | 2008-05-02 04:10:08 -0700 | [diff] [blame] | 2679 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | rc = -ENOMEM; |
| 2681 | return rc; |
| 2682 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2683 | EXPORT_SYMBOL(udp_proc_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 2685 | void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2687 | remove_proc_entry(afinfo->name, net->proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | } |
Eric Dumazet | c482c56 | 2009-07-17 00:26:32 +0000 | [diff] [blame] | 2689 | EXPORT_SYMBOL(udp_proc_unregister); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2690 | |
| 2691 | /* ------------------------------------------------------------------------ */ |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2692 | static void udp4_format_sock(struct sock *sp, struct seq_file *f, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2693 | int bucket) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2694 | { |
| 2695 | struct inet_sock *inet = inet_sk(sp); |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 2696 | __be32 dest = inet->inet_daddr; |
| 2697 | __be32 src = inet->inet_rcv_saddr; |
| 2698 | __u16 destp = ntohs(inet->inet_dport); |
| 2699 | __u16 srcp = ntohs(inet->inet_sport); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2700 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2701 | seq_printf(f, "%5d: %08X:%04X %08X:%04X" |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2702 | " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d", |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2703 | bucket, src, srcp, dest, destp, sp->sk_state, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 2704 | sk_wmem_alloc_get(sp), |
| 2705 | sk_rmem_alloc_get(sp), |
Eric W. Biederman | a7cb5a4 | 2012-05-24 01:10:10 -0600 | [diff] [blame] | 2706 | 0, 0L, 0, |
| 2707 | from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), |
| 2708 | 0, sock_i_ino(sp), |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 2709 | refcount_read(&sp->sk_refcnt), sp, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2710 | atomic_read(&sp->sk_drops)); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | int udp4_seq_show(struct seq_file *seq, void *v) |
| 2714 | { |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2715 | seq_setwidth(seq, 127); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2716 | if (v == SEQ_START_TOKEN) |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2717 | seq_puts(seq, " sl local_address rem_address st tx_queue " |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2718 | "rx_queue tr tm->when retrnsmt uid timeout " |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 2719 | "inode ref pointer drops"); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2720 | else { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2721 | struct udp_iter_state *state = seq->private; |
| 2722 | |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2723 | udp4_format_sock(v, seq, state->bucket); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2724 | } |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2725 | seq_pad(seq, '\n'); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2726 | return 0; |
| 2727 | } |
| 2728 | |
Arjan van de Ven | 73cb88e | 2011-10-30 06:46:30 +0000 | [diff] [blame] | 2729 | static const struct file_operations udp_afinfo_seq_fops = { |
| 2730 | .owner = THIS_MODULE, |
| 2731 | .open = udp_seq_open, |
| 2732 | .read = seq_read, |
| 2733 | .llseek = seq_lseek, |
| 2734 | .release = seq_release_net |
| 2735 | }; |
| 2736 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2737 | /* ------------------------------------------------------------------------ */ |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2738 | static struct udp_seq_afinfo udp4_seq_afinfo = { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2739 | .name = "udp", |
| 2740 | .family = AF_INET, |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2741 | .udp_table = &udp_table, |
Arjan van de Ven | 73cb88e | 2011-10-30 06:46:30 +0000 | [diff] [blame] | 2742 | .seq_fops = &udp_afinfo_seq_fops, |
Denis V. Lunev | dda6192 | 2008-03-28 18:24:26 -0700 | [diff] [blame] | 2743 | .seq_ops = { |
| 2744 | .show = udp4_seq_show, |
| 2745 | }, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2746 | }; |
| 2747 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2748 | static int __net_init udp4_proc_init_net(struct net *net) |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 2749 | { |
| 2750 | return udp_proc_register(net, &udp4_seq_afinfo); |
| 2751 | } |
| 2752 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2753 | static void __net_exit udp4_proc_exit_net(struct net *net) |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 2754 | { |
| 2755 | udp_proc_unregister(net, &udp4_seq_afinfo); |
| 2756 | } |
| 2757 | |
| 2758 | static struct pernet_operations udp4_net_ops = { |
| 2759 | .init = udp4_proc_init_net, |
| 2760 | .exit = udp4_proc_exit_net, |
| 2761 | }; |
| 2762 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2763 | int __init udp4_proc_init(void) |
| 2764 | { |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 2765 | return register_pernet_subsys(&udp4_net_ops); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | void udp4_proc_exit(void) |
| 2769 | { |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 2770 | unregister_pernet_subsys(&udp4_net_ops); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 2771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | #endif /* CONFIG_PROC_FS */ |
| 2773 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2774 | static __initdata unsigned long uhash_entries; |
| 2775 | static int __init set_uhash_entries(char *str) |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2776 | { |
Eldad Zack | 413c27d | 2012-05-19 14:13:18 +0000 | [diff] [blame] | 2777 | ssize_t ret; |
| 2778 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2779 | if (!str) |
| 2780 | return 0; |
Eldad Zack | 413c27d | 2012-05-19 14:13:18 +0000 | [diff] [blame] | 2781 | |
| 2782 | ret = kstrtoul(str, 0, &uhash_entries); |
| 2783 | if (ret) |
| 2784 | return 0; |
| 2785 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2786 | if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) |
| 2787 | uhash_entries = UDP_HTABLE_SIZE_MIN; |
| 2788 | return 1; |
| 2789 | } |
| 2790 | __setup("uhash_entries=", set_uhash_entries); |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2791 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2792 | void __init udp_table_init(struct udp_table *table, const char *name) |
| 2793 | { |
| 2794 | unsigned int i; |
| 2795 | |
Tim Bird | 31fe62b | 2012-05-23 13:33:35 +0000 | [diff] [blame] | 2796 | table->hash = alloc_large_system_hash(name, |
| 2797 | 2 * sizeof(struct udp_hslot), |
| 2798 | uhash_entries, |
| 2799 | 21, /* one slot per 2 MB */ |
| 2800 | 0, |
| 2801 | &table->log, |
| 2802 | &table->mask, |
| 2803 | UDP_HTABLE_SIZE_MIN, |
| 2804 | 64 * 1024); |
| 2805 | |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 2806 | table->hash2 = table->hash + (table->mask + 1); |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2807 | for (i = 0; i <= table->mask; i++) { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2808 | INIT_HLIST_HEAD(&table->hash[i].head); |
Eric Dumazet | fdcc8aa9 | 2009-11-08 10:17:05 +0000 | [diff] [blame] | 2809 | table->hash[i].count = 0; |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2810 | spin_lock_init(&table->hash[i].lock); |
| 2811 | } |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 2812 | for (i = 0; i <= table->mask; i++) { |
Eric Dumazet | ca065d0 | 2016-04-01 08:52:13 -0700 | [diff] [blame] | 2813 | INIT_HLIST_HEAD(&table->hash2[i].head); |
Eric Dumazet | 512615b | 2009-11-08 10:17:58 +0000 | [diff] [blame] | 2814 | table->hash2[i].count = 0; |
| 2815 | spin_lock_init(&table->hash2[i].lock); |
| 2816 | } |
Eric Dumazet | 645ca70 | 2008-10-29 01:41:45 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
Tom Herbert | 723b8e4 | 2015-02-24 09:17:31 -0800 | [diff] [blame] | 2819 | u32 udp_flow_hashrnd(void) |
| 2820 | { |
| 2821 | static u32 hashrnd __read_mostly; |
| 2822 | |
| 2823 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 2824 | |
| 2825 | return hashrnd; |
| 2826 | } |
| 2827 | EXPORT_SYMBOL(udp_flow_hashrnd); |
| 2828 | |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 2829 | void __init udp_init(void) |
| 2830 | { |
Eric Dumazet | f03d78d | 2011-07-07 00:27:05 -0700 | [diff] [blame] | 2831 | unsigned long limit; |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 2832 | unsigned int i; |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 2833 | |
Eric Dumazet | f86dcc5 | 2009-10-07 00:37:59 +0000 | [diff] [blame] | 2834 | udp_table_init(&udp_table, "UDP"); |
Eric Dumazet | f03d78d | 2011-07-07 00:27:05 -0700 | [diff] [blame] | 2835 | limit = nr_free_buffer_pages() / 8; |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 2836 | limit = max(limit, 128UL); |
| 2837 | sysctl_udp_mem[0] = limit / 4 * 3; |
| 2838 | sysctl_udp_mem[1] = limit; |
| 2839 | sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; |
| 2840 | |
| 2841 | sysctl_udp_rmem_min = SK_MEM_QUANTUM; |
| 2842 | sysctl_udp_wmem_min = SK_MEM_QUANTUM; |
Eric Dumazet | 4b27275 | 2016-12-08 11:41:54 -0800 | [diff] [blame] | 2843 | |
| 2844 | /* 16 spinlocks per cpu */ |
| 2845 | udp_busylocks_log = ilog2(nr_cpu_ids) + 4; |
| 2846 | udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, |
| 2847 | GFP_KERNEL); |
| 2848 | if (!udp_busylocks) |
| 2849 | panic("UDP: failed to alloc udp_busylocks\n"); |
| 2850 | for (i = 0; i < (1U << udp_busylocks_log); i++) |
| 2851 | spin_lock_init(udp_busylocks + i); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 2852 | } |