David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * The User Datagram Protocol (UDP). |
| 8 | * |
| 9 | * Authors: Ross Biro |
| 10 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 11 | * Arnt Gulbrandsen, <agulbra@nvg.unit.no> |
| 12 | * Alan Cox, <alan@lxorguk.ukuu.org.uk> |
| 13 | * Hirokazu Takahashi, <taka@valinux.co.jp> |
| 14 | * |
| 15 | * Fixes: |
| 16 | * Alan Cox : verify_area() calls |
| 17 | * Alan Cox : stopped close while in use off icmp |
| 18 | * messages. Not a fix but a botch that |
| 19 | * for udp at least is 'valid'. |
| 20 | * Alan Cox : Fixed icmp handling properly |
| 21 | * Alan Cox : Correct error for oversized datagrams |
| 22 | * Alan Cox : Tidied select() semantics. |
| 23 | * Alan Cox : udp_err() fixed properly, also now |
| 24 | * select and read wake correctly on errors |
| 25 | * Alan Cox : udp_send verify_area moved to avoid mem leak |
| 26 | * Alan Cox : UDP can count its memory |
| 27 | * Alan Cox : send to an unknown connection causes |
| 28 | * an ECONNREFUSED off the icmp, but |
| 29 | * does NOT close. |
| 30 | * Alan Cox : Switched to new sk_buff handlers. No more backlog! |
| 31 | * Alan Cox : Using generic datagram code. Even smaller and the PEEK |
| 32 | * bug no longer crashes it. |
| 33 | * Fred Van Kempen : Net2e support for sk->broadcast. |
| 34 | * Alan Cox : Uses skb_free_datagram |
| 35 | * Alan Cox : Added get/set sockopt support. |
| 36 | * Alan Cox : Broadcasting without option set returns EACCES. |
| 37 | * Alan Cox : No wakeup calls. Instead we now use the callbacks. |
| 38 | * Alan Cox : Use ip_tos and ip_ttl |
| 39 | * Alan Cox : SNMP Mibs |
| 40 | * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. |
| 41 | * Matt Dillon : UDP length checks. |
| 42 | * Alan Cox : Smarter af_inet used properly. |
| 43 | * Alan Cox : Use new kernel side addressing. |
| 44 | * Alan Cox : Incorrect return on truncated datagram receive. |
| 45 | * Arnt Gulbrandsen : New udp_send and stuff |
| 46 | * Alan Cox : Cache last socket |
| 47 | * Alan Cox : Route cache |
| 48 | * Jon Peatfield : Minor efficiency fix to sendto(). |
| 49 | * Mike Shaver : RFC1122 checks. |
| 50 | * Alan Cox : Nonblocking error fix. |
| 51 | * Willy Konynenberg : Transparent proxying support. |
| 52 | * Mike McLagan : Routing by source |
| 53 | * David S. Miller : New socket lookup architecture. |
| 54 | * Last socket cache retained as it |
| 55 | * does have a high hit rate. |
| 56 | * Olaf Kirch : Don't linearise iovec on sendmsg. |
| 57 | * Andi Kleen : Some cleanups, cache destination entry |
| 58 | * for connect. |
| 59 | * Vitaly E. Lavrov : Transparent proxy revived after year coma. |
| 60 | * Melvin Smith : Check msg_name not msg_namelen in sendto(), |
| 61 | * return ENOTCONN for unconnected sockets (POSIX) |
| 62 | * Janos Farkas : don't deliver multi/broadcasts to a different |
| 63 | * bound-to-device socket |
| 64 | * Hirokazu Takahashi : HW checksumming for outgoing UDP |
| 65 | * datagrams. |
| 66 | * Hirokazu Takahashi : sendfile() on UDP works now. |
| 67 | * Arnaldo C. Melo : convert /proc/net/udp to seq_file |
| 68 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which |
| 69 | * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind |
| 70 | * a single port at the same time. |
| 71 | * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support |
| 72 | * James Chapman : Add L2TP encapsulation type. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 73 | */ |
| 74 | |
| 75 | #define pr_fmt(fmt) "UDP: " fmt |
| 76 | |
| 77 | #include <linux/uaccess.h> |
| 78 | #include <asm/ioctls.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 79 | #include <linux/memblock.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 80 | #include <linux/highmem.h> |
| 81 | #include <linux/swap.h> |
| 82 | #include <linux/types.h> |
| 83 | #include <linux/fcntl.h> |
| 84 | #include <linux/module.h> |
| 85 | #include <linux/socket.h> |
| 86 | #include <linux/sockios.h> |
| 87 | #include <linux/igmp.h> |
| 88 | #include <linux/inetdevice.h> |
| 89 | #include <linux/in.h> |
| 90 | #include <linux/errno.h> |
| 91 | #include <linux/timer.h> |
| 92 | #include <linux/mm.h> |
| 93 | #include <linux/inet.h> |
| 94 | #include <linux/netdevice.h> |
| 95 | #include <linux/slab.h> |
| 96 | #include <net/tcp_states.h> |
| 97 | #include <linux/skbuff.h> |
| 98 | #include <linux/proc_fs.h> |
| 99 | #include <linux/seq_file.h> |
| 100 | #include <net/net_namespace.h> |
| 101 | #include <net/icmp.h> |
| 102 | #include <net/inet_hashtables.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 103 | #include <net/ip_tunnels.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | #include <net/route.h> |
| 105 | #include <net/checksum.h> |
| 106 | #include <net/xfrm.h> |
| 107 | #include <trace/events/udp.h> |
| 108 | #include <linux/static_key.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 109 | #include <linux/btf_ids.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | #include <trace/events/skb.h> |
| 111 | #include <net/busy_poll.h> |
| 112 | #include "udp_impl.h" |
| 113 | #include <net/sock_reuseport.h> |
| 114 | #include <net/addrconf.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 115 | #include <net/udp_tunnel.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 116 | #if IS_ENABLED(CONFIG_IPV6) |
| 117 | #include <net/ipv6_stubs.h> |
| 118 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 119 | |
| 120 | struct udp_table udp_table __read_mostly; |
| 121 | EXPORT_SYMBOL(udp_table); |
| 122 | |
| 123 | long sysctl_udp_mem[3] __read_mostly; |
| 124 | EXPORT_SYMBOL(sysctl_udp_mem); |
| 125 | |
| 126 | atomic_long_t udp_memory_allocated; |
| 127 | EXPORT_SYMBOL(udp_memory_allocated); |
| 128 | |
| 129 | #define MAX_UDP_PORTS 65536 |
| 130 | #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) |
| 131 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 132 | static int udp_lib_lport_inuse(struct net *net, __u16 num, |
| 133 | const struct udp_hslot *hslot, |
| 134 | unsigned long *bitmap, |
| 135 | struct sock *sk, unsigned int log) |
| 136 | { |
| 137 | struct sock *sk2; |
| 138 | kuid_t uid = sock_i_uid(sk); |
| 139 | |
| 140 | sk_for_each(sk2, &hslot->head) { |
| 141 | if (net_eq(sock_net(sk2), net) && |
| 142 | sk2 != sk && |
| 143 | (bitmap || udp_sk(sk2)->udp_port_hash == num) && |
| 144 | (!sk2->sk_reuse || !sk->sk_reuse) && |
| 145 | (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || |
| 146 | sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
| 147 | inet_rcv_saddr_equal(sk, sk2, true)) { |
| 148 | if (sk2->sk_reuseport && sk->sk_reuseport && |
| 149 | !rcu_access_pointer(sk->sk_reuseport_cb) && |
| 150 | uid_eq(uid, sock_i_uid(sk2))) { |
| 151 | if (!bitmap) |
| 152 | return 0; |
| 153 | } else { |
| 154 | if (!bitmap) |
| 155 | return 1; |
| 156 | __set_bit(udp_sk(sk2)->udp_port_hash >> log, |
| 157 | bitmap); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Note: we still hold spinlock of primary hash chain, so no other writer |
| 166 | * can insert/delete a socket with local_port == num |
| 167 | */ |
| 168 | static int udp_lib_lport_inuse2(struct net *net, __u16 num, |
| 169 | struct udp_hslot *hslot2, |
| 170 | struct sock *sk) |
| 171 | { |
| 172 | struct sock *sk2; |
| 173 | kuid_t uid = sock_i_uid(sk); |
| 174 | int res = 0; |
| 175 | |
| 176 | spin_lock(&hslot2->lock); |
| 177 | udp_portaddr_for_each_entry(sk2, &hslot2->head) { |
| 178 | if (net_eq(sock_net(sk2), net) && |
| 179 | sk2 != sk && |
| 180 | (udp_sk(sk2)->udp_port_hash == num) && |
| 181 | (!sk2->sk_reuse || !sk->sk_reuse) && |
| 182 | (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || |
| 183 | sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
| 184 | inet_rcv_saddr_equal(sk, sk2, true)) { |
| 185 | if (sk2->sk_reuseport && sk->sk_reuseport && |
| 186 | !rcu_access_pointer(sk->sk_reuseport_cb) && |
| 187 | uid_eq(uid, sock_i_uid(sk2))) { |
| 188 | res = 0; |
| 189 | } else { |
| 190 | res = 1; |
| 191 | } |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | spin_unlock(&hslot2->lock); |
| 196 | return res; |
| 197 | } |
| 198 | |
| 199 | static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) |
| 200 | { |
| 201 | struct net *net = sock_net(sk); |
| 202 | kuid_t uid = sock_i_uid(sk); |
| 203 | struct sock *sk2; |
| 204 | |
| 205 | sk_for_each(sk2, &hslot->head) { |
| 206 | if (net_eq(sock_net(sk2), net) && |
| 207 | sk2 != sk && |
| 208 | sk2->sk_family == sk->sk_family && |
| 209 | ipv6_only_sock(sk2) == ipv6_only_sock(sk) && |
| 210 | (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) && |
| 211 | (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
| 212 | sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && |
| 213 | inet_rcv_saddr_equal(sk, sk2, false)) { |
| 214 | return reuseport_add_sock(sk, sk2, |
| 215 | inet_rcv_saddr_any(sk)); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 |
| 224 | * |
| 225 | * @sk: socket struct in question |
| 226 | * @snum: port number to look up |
| 227 | * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, |
| 228 | * with NULL address |
| 229 | */ |
| 230 | int udp_lib_get_port(struct sock *sk, unsigned short snum, |
| 231 | unsigned int hash2_nulladdr) |
| 232 | { |
| 233 | struct udp_hslot *hslot, *hslot2; |
| 234 | struct udp_table *udptable = sk->sk_prot->h.udp_table; |
| 235 | int error = 1; |
| 236 | struct net *net = sock_net(sk); |
| 237 | |
| 238 | if (!snum) { |
| 239 | int low, high, remaining; |
| 240 | unsigned int rand; |
| 241 | unsigned short first, last; |
| 242 | DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); |
| 243 | |
| 244 | inet_get_local_port_range(net, &low, &high); |
| 245 | remaining = (high - low) + 1; |
| 246 | |
| 247 | rand = prandom_u32(); |
| 248 | first = reciprocal_scale(rand, remaining) + low; |
| 249 | /* |
| 250 | * force rand to be an odd multiple of UDP_HTABLE_SIZE |
| 251 | */ |
| 252 | rand = (rand | 1) * (udptable->mask + 1); |
| 253 | last = first + udptable->mask + 1; |
| 254 | do { |
| 255 | hslot = udp_hashslot(udptable, net, first); |
| 256 | bitmap_zero(bitmap, PORTS_PER_CHAIN); |
| 257 | spin_lock_bh(&hslot->lock); |
| 258 | udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, |
| 259 | udptable->log); |
| 260 | |
| 261 | snum = first; |
| 262 | /* |
| 263 | * Iterate on all possible values of snum for this hash. |
| 264 | * Using steps of an odd multiple of UDP_HTABLE_SIZE |
| 265 | * give us randomization and full range coverage. |
| 266 | */ |
| 267 | do { |
| 268 | if (low <= snum && snum <= high && |
| 269 | !test_bit(snum >> udptable->log, bitmap) && |
| 270 | !inet_is_local_reserved_port(net, snum)) |
| 271 | goto found; |
| 272 | snum += rand; |
| 273 | } while (snum != first); |
| 274 | spin_unlock_bh(&hslot->lock); |
| 275 | cond_resched(); |
| 276 | } while (++first != last); |
| 277 | goto fail; |
| 278 | } else { |
| 279 | hslot = udp_hashslot(udptable, net, snum); |
| 280 | spin_lock_bh(&hslot->lock); |
| 281 | if (hslot->count > 10) { |
| 282 | int exist; |
| 283 | unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; |
| 284 | |
| 285 | slot2 &= udptable->mask; |
| 286 | hash2_nulladdr &= udptable->mask; |
| 287 | |
| 288 | hslot2 = udp_hashslot2(udptable, slot2); |
| 289 | if (hslot->count < hslot2->count) |
| 290 | goto scan_primary_hash; |
| 291 | |
| 292 | exist = udp_lib_lport_inuse2(net, snum, hslot2, sk); |
| 293 | if (!exist && (hash2_nulladdr != slot2)) { |
| 294 | hslot2 = udp_hashslot2(udptable, hash2_nulladdr); |
| 295 | exist = udp_lib_lport_inuse2(net, snum, hslot2, |
| 296 | sk); |
| 297 | } |
| 298 | if (exist) |
| 299 | goto fail_unlock; |
| 300 | else |
| 301 | goto found; |
| 302 | } |
| 303 | scan_primary_hash: |
| 304 | if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0)) |
| 305 | goto fail_unlock; |
| 306 | } |
| 307 | found: |
| 308 | inet_sk(sk)->inet_num = snum; |
| 309 | udp_sk(sk)->udp_port_hash = snum; |
| 310 | udp_sk(sk)->udp_portaddr_hash ^= snum; |
| 311 | if (sk_unhashed(sk)) { |
| 312 | if (sk->sk_reuseport && |
| 313 | udp_reuseport_add_sock(sk, hslot)) { |
| 314 | inet_sk(sk)->inet_num = 0; |
| 315 | udp_sk(sk)->udp_port_hash = 0; |
| 316 | udp_sk(sk)->udp_portaddr_hash ^= snum; |
| 317 | goto fail_unlock; |
| 318 | } |
| 319 | |
| 320 | sk_add_node_rcu(sk, &hslot->head); |
| 321 | hslot->count++; |
| 322 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 323 | |
| 324 | hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); |
| 325 | spin_lock(&hslot2->lock); |
| 326 | if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && |
| 327 | sk->sk_family == AF_INET6) |
| 328 | hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node, |
| 329 | &hslot2->head); |
| 330 | else |
| 331 | hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, |
| 332 | &hslot2->head); |
| 333 | hslot2->count++; |
| 334 | spin_unlock(&hslot2->lock); |
| 335 | } |
| 336 | sock_set_flag(sk, SOCK_RCU_FREE); |
| 337 | error = 0; |
| 338 | fail_unlock: |
| 339 | spin_unlock_bh(&hslot->lock); |
| 340 | fail: |
| 341 | return error; |
| 342 | } |
| 343 | EXPORT_SYMBOL(udp_lib_get_port); |
| 344 | |
| 345 | int udp_v4_get_port(struct sock *sk, unsigned short snum) |
| 346 | { |
| 347 | unsigned int hash2_nulladdr = |
| 348 | ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); |
| 349 | unsigned int hash2_partial = |
| 350 | ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); |
| 351 | |
| 352 | /* precompute partial secondary hash */ |
| 353 | udp_sk(sk)->udp_portaddr_hash = hash2_partial; |
| 354 | return udp_lib_get_port(sk, snum, hash2_nulladdr); |
| 355 | } |
| 356 | |
| 357 | static int compute_score(struct sock *sk, struct net *net, |
| 358 | __be32 saddr, __be16 sport, |
| 359 | __be32 daddr, unsigned short hnum, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 360 | int dif, int sdif) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 361 | { |
| 362 | int score; |
| 363 | struct inet_sock *inet; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 364 | bool dev_match; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 365 | |
| 366 | if (!net_eq(sock_net(sk), net) || |
| 367 | udp_sk(sk)->udp_port_hash != hnum || |
| 368 | ipv6_only_sock(sk)) |
| 369 | return -1; |
| 370 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 371 | if (sk->sk_rcv_saddr != daddr) |
| 372 | return -1; |
| 373 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 374 | score = (sk->sk_family == PF_INET) ? 2 : 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 375 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 376 | inet = inet_sk(sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 377 | if (inet->inet_daddr) { |
| 378 | if (inet->inet_daddr != saddr) |
| 379 | return -1; |
| 380 | score += 4; |
| 381 | } |
| 382 | |
| 383 | if (inet->inet_dport) { |
| 384 | if (inet->inet_dport != sport) |
| 385 | return -1; |
| 386 | score += 4; |
| 387 | } |
| 388 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 389 | dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, |
| 390 | dif, sdif); |
| 391 | if (!dev_match) |
| 392 | return -1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 393 | if (sk->sk_bound_dev_if) |
| 394 | score += 4; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 395 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 396 | if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 397 | score++; |
| 398 | return score; |
| 399 | } |
| 400 | |
| 401 | static u32 udp_ehashfn(const struct net *net, const __be32 laddr, |
| 402 | const __u16 lport, const __be32 faddr, |
| 403 | const __be16 fport) |
| 404 | { |
| 405 | static u32 udp_ehash_secret __read_mostly; |
| 406 | |
| 407 | net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret)); |
| 408 | |
| 409 | return __inet_ehashfn(laddr, lport, faddr, fport, |
| 410 | udp_ehash_secret + net_hash_mix(net)); |
| 411 | } |
| 412 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 413 | static struct sock *lookup_reuseport(struct net *net, struct sock *sk, |
| 414 | struct sk_buff *skb, |
| 415 | __be32 saddr, __be16 sport, |
| 416 | __be32 daddr, unsigned short hnum) |
| 417 | { |
| 418 | struct sock *reuse_sk = NULL; |
| 419 | u32 hash; |
| 420 | |
| 421 | if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { |
| 422 | hash = udp_ehashfn(net, daddr, hnum, saddr, sport); |
| 423 | reuse_sk = reuseport_select_sock(sk, hash, skb, |
| 424 | sizeof(struct udphdr)); |
| 425 | } |
| 426 | return reuse_sk; |
| 427 | } |
| 428 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 429 | /* called with rcu_read_lock() */ |
| 430 | static struct sock *udp4_lib_lookup2(struct net *net, |
| 431 | __be32 saddr, __be16 sport, |
| 432 | __be32 daddr, unsigned int hnum, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 433 | int dif, int sdif, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 434 | struct udp_hslot *hslot2, |
| 435 | struct sk_buff *skb) |
| 436 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 437 | struct sock *sk, *result; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 438 | int score, badness; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 439 | |
| 440 | result = NULL; |
| 441 | badness = 0; |
| 442 | udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { |
| 443 | score = compute_score(sk, net, saddr, sport, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 444 | daddr, hnum, dif, sdif); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 445 | if (score > badness) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 446 | result = lookup_reuseport(net, sk, skb, |
| 447 | saddr, sport, daddr, hnum); |
| 448 | /* Fall back to scoring if group has connections */ |
| 449 | if (result && !reuseport_has_conns(sk, false)) |
| 450 | return result; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 451 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 452 | result = result ? : sk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 453 | badness = score; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | return result; |
| 457 | } |
| 458 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 459 | static struct sock *udp4_lookup_run_bpf(struct net *net, |
| 460 | struct udp_table *udptable, |
| 461 | struct sk_buff *skb, |
| 462 | __be32 saddr, __be16 sport, |
| 463 | __be32 daddr, u16 hnum) |
| 464 | { |
| 465 | struct sock *sk, *reuse_sk; |
| 466 | bool no_reuseport; |
| 467 | |
| 468 | if (udptable != &udp_table) |
| 469 | return NULL; /* only UDP is supported */ |
| 470 | |
| 471 | no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP, |
| 472 | saddr, sport, daddr, hnum, &sk); |
| 473 | if (no_reuseport || IS_ERR_OR_NULL(sk)) |
| 474 | return sk; |
| 475 | |
| 476 | reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); |
| 477 | if (reuse_sk) |
| 478 | sk = reuse_sk; |
| 479 | return sk; |
| 480 | } |
| 481 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 482 | /* UDP is nearly always wildcards out the wazoo, it makes no sense to try |
| 483 | * harder than this. -DaveM |
| 484 | */ |
| 485 | struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, |
| 486 | __be16 sport, __be32 daddr, __be16 dport, int dif, |
| 487 | int sdif, struct udp_table *udptable, struct sk_buff *skb) |
| 488 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 489 | unsigned short hnum = ntohs(dport); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 490 | unsigned int hash2, slot2; |
| 491 | struct udp_hslot *hslot2; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 492 | struct sock *result, *sk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 493 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 494 | hash2 = ipv4_portaddr_hash(net, daddr, hnum); |
| 495 | slot2 = hash2 & udptable->mask; |
| 496 | hslot2 = &udptable->hash2[slot2]; |
| 497 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 498 | /* Lookup connected or non-wildcard socket */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 499 | result = udp4_lib_lookup2(net, saddr, sport, |
| 500 | daddr, hnum, dif, sdif, |
| 501 | hslot2, skb); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 502 | if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) |
| 503 | goto done; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 504 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 505 | /* Lookup redirect from BPF */ |
| 506 | if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { |
| 507 | sk = udp4_lookup_run_bpf(net, udptable, skb, |
| 508 | saddr, sport, daddr, hnum); |
| 509 | if (sk) { |
| 510 | result = sk; |
| 511 | goto done; |
| 512 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 513 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 514 | |
| 515 | /* Got non-wildcard socket or error on first lookup */ |
| 516 | if (result) |
| 517 | goto done; |
| 518 | |
| 519 | /* Lookup wildcard sockets */ |
| 520 | hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); |
| 521 | slot2 = hash2 & udptable->mask; |
| 522 | hslot2 = &udptable->hash2[slot2]; |
| 523 | |
| 524 | result = udp4_lib_lookup2(net, saddr, sport, |
| 525 | htonl(INADDR_ANY), hnum, dif, sdif, |
| 526 | hslot2, skb); |
| 527 | done: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 528 | if (IS_ERR(result)) |
| 529 | return NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 530 | return result; |
| 531 | } |
| 532 | EXPORT_SYMBOL_GPL(__udp4_lib_lookup); |
| 533 | |
| 534 | static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, |
| 535 | __be16 sport, __be16 dport, |
| 536 | struct udp_table *udptable) |
| 537 | { |
| 538 | const struct iphdr *iph = ip_hdr(skb); |
| 539 | |
| 540 | return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, |
| 541 | iph->daddr, dport, inet_iif(skb), |
| 542 | inet_sdif(skb), udptable, skb); |
| 543 | } |
| 544 | |
| 545 | struct sock *udp4_lib_lookup_skb(struct sk_buff *skb, |
| 546 | __be16 sport, __be16 dport) |
| 547 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 548 | const struct iphdr *iph = ip_hdr(skb); |
| 549 | |
| 550 | return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport, |
| 551 | iph->daddr, dport, inet_iif(skb), |
| 552 | inet_sdif(skb), &udp_table, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 553 | } |
| 554 | EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb); |
| 555 | |
| 556 | /* Must be called under rcu_read_lock(). |
| 557 | * Does increment socket refcount. |
| 558 | */ |
| 559 | #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4) |
| 560 | struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, |
| 561 | __be32 daddr, __be16 dport, int dif) |
| 562 | { |
| 563 | struct sock *sk; |
| 564 | |
| 565 | sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport, |
| 566 | dif, 0, &udp_table, NULL); |
| 567 | if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) |
| 568 | sk = NULL; |
| 569 | return sk; |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(udp4_lib_lookup); |
| 572 | #endif |
| 573 | |
| 574 | static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk, |
| 575 | __be16 loc_port, __be32 loc_addr, |
| 576 | __be16 rmt_port, __be32 rmt_addr, |
| 577 | int dif, int sdif, unsigned short hnum) |
| 578 | { |
| 579 | struct inet_sock *inet = inet_sk(sk); |
| 580 | |
| 581 | if (!net_eq(sock_net(sk), net) || |
| 582 | udp_sk(sk)->udp_port_hash != hnum || |
| 583 | (inet->inet_daddr && inet->inet_daddr != rmt_addr) || |
| 584 | (inet->inet_dport != rmt_port && inet->inet_dport) || |
| 585 | (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) || |
| 586 | ipv6_only_sock(sk) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 587 | !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 588 | return false; |
| 589 | if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif)) |
| 590 | return false; |
| 591 | return true; |
| 592 | } |
| 593 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 594 | DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key); |
| 595 | void udp_encap_enable(void) |
| 596 | { |
| 597 | static_branch_inc(&udp_encap_needed_key); |
| 598 | } |
| 599 | EXPORT_SYMBOL(udp_encap_enable); |
| 600 | |
| 601 | /* Handler for tunnels with arbitrary destination ports: no socket lookup, go |
| 602 | * through error handlers in encapsulations looking for a match. |
| 603 | */ |
| 604 | static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info) |
| 605 | { |
| 606 | int i; |
| 607 | |
| 608 | for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { |
| 609 | int (*handler)(struct sk_buff *skb, u32 info); |
| 610 | const struct ip_tunnel_encap_ops *encap; |
| 611 | |
| 612 | encap = rcu_dereference(iptun_encaps[i]); |
| 613 | if (!encap) |
| 614 | continue; |
| 615 | handler = encap->err_handler; |
| 616 | if (handler && !handler(skb, info)) |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | return -ENOENT; |
| 621 | } |
| 622 | |
| 623 | /* Try to match ICMP errors to UDP tunnels by looking up a socket without |
| 624 | * reversing source and destination port: this will match tunnels that force the |
| 625 | * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that |
| 626 | * lwtunnels might actually break this assumption by being configured with |
| 627 | * different destination ports on endpoints, in this case we won't be able to |
| 628 | * trace ICMP messages back to them. |
| 629 | * |
| 630 | * If this doesn't match any socket, probe tunnels with arbitrary destination |
| 631 | * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port |
| 632 | * we've sent packets to won't necessarily match the local destination port. |
| 633 | * |
| 634 | * Then ask the tunnel implementation to match the error against a valid |
| 635 | * association. |
| 636 | * |
| 637 | * Return an error if we can't find a match, the socket if we need further |
| 638 | * processing, zero otherwise. |
| 639 | */ |
| 640 | static struct sock *__udp4_lib_err_encap(struct net *net, |
| 641 | const struct iphdr *iph, |
| 642 | struct udphdr *uh, |
| 643 | struct udp_table *udptable, |
| 644 | struct sk_buff *skb, u32 info) |
| 645 | { |
| 646 | int network_offset, transport_offset; |
| 647 | struct sock *sk; |
| 648 | |
| 649 | network_offset = skb_network_offset(skb); |
| 650 | transport_offset = skb_transport_offset(skb); |
| 651 | |
| 652 | /* Network header needs to point to the outer IPv4 header inside ICMP */ |
| 653 | skb_reset_network_header(skb); |
| 654 | |
| 655 | /* Transport header needs to point to the UDP header */ |
| 656 | skb_set_transport_header(skb, iph->ihl << 2); |
| 657 | |
| 658 | sk = __udp4_lib_lookup(net, iph->daddr, uh->source, |
| 659 | iph->saddr, uh->dest, skb->dev->ifindex, 0, |
| 660 | udptable, NULL); |
| 661 | if (sk) { |
| 662 | int (*lookup)(struct sock *sk, struct sk_buff *skb); |
| 663 | struct udp_sock *up = udp_sk(sk); |
| 664 | |
| 665 | lookup = READ_ONCE(up->encap_err_lookup); |
| 666 | if (!lookup || lookup(sk, skb)) |
| 667 | sk = NULL; |
| 668 | } |
| 669 | |
| 670 | if (!sk) |
| 671 | sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info)); |
| 672 | |
| 673 | skb_set_transport_header(skb, transport_offset); |
| 674 | skb_set_network_header(skb, network_offset); |
| 675 | |
| 676 | return sk; |
| 677 | } |
| 678 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 679 | /* |
| 680 | * This routine is called by the ICMP module when it gets some |
| 681 | * sort of error condition. If err < 0 then the socket should |
| 682 | * be closed and the error returned to the user. If err > 0 |
| 683 | * it's just the icmp type << 8 | icmp code. |
| 684 | * Header points to the ip header of the error packet. We move |
| 685 | * on past this. Then (as it used to claim before adjustment) |
| 686 | * header points to the first 8 bytes of the udp header. We need |
| 687 | * to find the appropriate port. |
| 688 | */ |
| 689 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 690 | int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 691 | { |
| 692 | struct inet_sock *inet; |
| 693 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
| 694 | struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); |
| 695 | const int type = icmp_hdr(skb)->type; |
| 696 | const int code = icmp_hdr(skb)->code; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 697 | bool tunnel = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 698 | struct sock *sk; |
| 699 | int harderr; |
| 700 | int err; |
| 701 | struct net *net = dev_net(skb->dev); |
| 702 | |
| 703 | sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 704 | iph->saddr, uh->source, skb->dev->ifindex, |
| 705 | inet_sdif(skb), udptable, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 706 | if (!sk) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 707 | /* No socket for error: try tunnels before discarding */ |
| 708 | sk = ERR_PTR(-ENOENT); |
| 709 | if (static_branch_unlikely(&udp_encap_needed_key)) { |
| 710 | sk = __udp4_lib_err_encap(net, iph, uh, udptable, skb, |
| 711 | info); |
| 712 | if (!sk) |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | if (IS_ERR(sk)) { |
| 717 | __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); |
| 718 | return PTR_ERR(sk); |
| 719 | } |
| 720 | |
| 721 | tunnel = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | err = 0; |
| 725 | harderr = 0; |
| 726 | inet = inet_sk(sk); |
| 727 | |
| 728 | switch (type) { |
| 729 | default: |
| 730 | case ICMP_TIME_EXCEEDED: |
| 731 | err = EHOSTUNREACH; |
| 732 | break; |
| 733 | case ICMP_SOURCE_QUENCH: |
| 734 | goto out; |
| 735 | case ICMP_PARAMETERPROB: |
| 736 | err = EPROTO; |
| 737 | harderr = 1; |
| 738 | break; |
| 739 | case ICMP_DEST_UNREACH: |
| 740 | if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ |
| 741 | ipv4_sk_update_pmtu(skb, sk, info); |
| 742 | if (inet->pmtudisc != IP_PMTUDISC_DONT) { |
| 743 | err = EMSGSIZE; |
| 744 | harderr = 1; |
| 745 | break; |
| 746 | } |
| 747 | goto out; |
| 748 | } |
| 749 | err = EHOSTUNREACH; |
| 750 | if (code <= NR_ICMP_UNREACH) { |
| 751 | harderr = icmp_err_convert[code].fatal; |
| 752 | err = icmp_err_convert[code].errno; |
| 753 | } |
| 754 | break; |
| 755 | case ICMP_REDIRECT: |
| 756 | ipv4_sk_redirect(skb, sk); |
| 757 | goto out; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * RFC1122: OK. Passes ICMP errors back to application, as per |
| 762 | * 4.1.3.3. |
| 763 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 764 | if (tunnel) { |
| 765 | /* ...not for tunnels though: we don't have a sending socket */ |
| 766 | goto out; |
| 767 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 768 | if (!inet->recverr) { |
| 769 | if (!harderr || sk->sk_state != TCP_ESTABLISHED) |
| 770 | goto out; |
| 771 | } else |
| 772 | ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); |
| 773 | |
| 774 | sk->sk_err = err; |
| 775 | sk->sk_error_report(sk); |
| 776 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 777 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 778 | } |
| 779 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 780 | int udp_err(struct sk_buff *skb, u32 info) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 781 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 782 | return __udp4_lib_err(skb, info, &udp_table); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Throw away all pending data and cancel the corking. Socket is locked. |
| 787 | */ |
| 788 | void udp_flush_pending_frames(struct sock *sk) |
| 789 | { |
| 790 | struct udp_sock *up = udp_sk(sk); |
| 791 | |
| 792 | if (up->pending) { |
| 793 | up->len = 0; |
| 794 | up->pending = 0; |
| 795 | ip_flush_pending_frames(sk); |
| 796 | } |
| 797 | } |
| 798 | EXPORT_SYMBOL(udp_flush_pending_frames); |
| 799 | |
| 800 | /** |
| 801 | * udp4_hwcsum - handle outgoing HW checksumming |
| 802 | * @skb: sk_buff containing the filled-in UDP header |
| 803 | * (checksum field must be zeroed out) |
| 804 | * @src: source IP address |
| 805 | * @dst: destination IP address |
| 806 | */ |
| 807 | void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) |
| 808 | { |
| 809 | struct udphdr *uh = udp_hdr(skb); |
| 810 | int offset = skb_transport_offset(skb); |
| 811 | int len = skb->len - offset; |
| 812 | int hlen = len; |
| 813 | __wsum csum = 0; |
| 814 | |
| 815 | if (!skb_has_frag_list(skb)) { |
| 816 | /* |
| 817 | * Only one fragment on the socket. |
| 818 | */ |
| 819 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 820 | skb->csum_offset = offsetof(struct udphdr, check); |
| 821 | uh->check = ~csum_tcpudp_magic(src, dst, len, |
| 822 | IPPROTO_UDP, 0); |
| 823 | } else { |
| 824 | struct sk_buff *frags; |
| 825 | |
| 826 | /* |
| 827 | * HW-checksum won't work as there are two or more |
| 828 | * fragments on the socket so that all csums of sk_buffs |
| 829 | * should be together |
| 830 | */ |
| 831 | skb_walk_frags(skb, frags) { |
| 832 | csum = csum_add(csum, frags->csum); |
| 833 | hlen -= frags->len; |
| 834 | } |
| 835 | |
| 836 | csum = skb_checksum(skb, offset, hlen, csum); |
| 837 | skb->ip_summed = CHECKSUM_NONE; |
| 838 | |
| 839 | uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); |
| 840 | if (uh->check == 0) |
| 841 | uh->check = CSUM_MANGLED_0; |
| 842 | } |
| 843 | } |
| 844 | EXPORT_SYMBOL_GPL(udp4_hwcsum); |
| 845 | |
| 846 | /* Function to set UDP checksum for an IPv4 UDP packet. This is intended |
| 847 | * for the simple case like when setting the checksum for a UDP tunnel. |
| 848 | */ |
| 849 | void udp_set_csum(bool nocheck, struct sk_buff *skb, |
| 850 | __be32 saddr, __be32 daddr, int len) |
| 851 | { |
| 852 | struct udphdr *uh = udp_hdr(skb); |
| 853 | |
| 854 | if (nocheck) { |
| 855 | uh->check = 0; |
| 856 | } else if (skb_is_gso(skb)) { |
| 857 | uh->check = ~udp_v4_check(len, saddr, daddr, 0); |
| 858 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 859 | uh->check = 0; |
| 860 | uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb)); |
| 861 | if (uh->check == 0) |
| 862 | uh->check = CSUM_MANGLED_0; |
| 863 | } else { |
| 864 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 865 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 866 | skb->csum_offset = offsetof(struct udphdr, check); |
| 867 | uh->check = ~udp_v4_check(len, saddr, daddr, 0); |
| 868 | } |
| 869 | } |
| 870 | EXPORT_SYMBOL(udp_set_csum); |
| 871 | |
| 872 | static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4, |
| 873 | struct inet_cork *cork) |
| 874 | { |
| 875 | struct sock *sk = skb->sk; |
| 876 | struct inet_sock *inet = inet_sk(sk); |
| 877 | struct udphdr *uh; |
| 878 | int err = 0; |
| 879 | int is_udplite = IS_UDPLITE(sk); |
| 880 | int offset = skb_transport_offset(skb); |
| 881 | int len = skb->len - offset; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 882 | int datalen = len - sizeof(*uh); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 883 | __wsum csum = 0; |
| 884 | |
| 885 | /* |
| 886 | * Create a UDP header |
| 887 | */ |
| 888 | uh = udp_hdr(skb); |
| 889 | uh->source = inet->inet_sport; |
| 890 | uh->dest = fl4->fl4_dport; |
| 891 | uh->len = htons(len); |
| 892 | uh->check = 0; |
| 893 | |
| 894 | if (cork->gso_size) { |
| 895 | const int hlen = skb_network_header_len(skb) + |
| 896 | sizeof(struct udphdr); |
| 897 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 898 | if (hlen + cork->gso_size > cork->fragsize) { |
| 899 | kfree_skb(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 900 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 901 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 902 | if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 903 | kfree_skb(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 904 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 905 | } |
| 906 | if (sk->sk_no_check_tx) { |
| 907 | kfree_skb(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 908 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 909 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 910 | if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 911 | dst_xfrm(skb_dst(skb))) { |
| 912 | kfree_skb(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 913 | return -EIO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 914 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 915 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 916 | if (datalen > cork->gso_size) { |
| 917 | skb_shinfo(skb)->gso_size = cork->gso_size; |
| 918 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; |
| 919 | skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, |
| 920 | cork->gso_size); |
| 921 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 922 | goto csum_partial; |
| 923 | } |
| 924 | |
| 925 | if (is_udplite) /* UDP-Lite */ |
| 926 | csum = udplite_csum(skb); |
| 927 | |
| 928 | else if (sk->sk_no_check_tx) { /* UDP csum off */ |
| 929 | |
| 930 | skb->ip_summed = CHECKSUM_NONE; |
| 931 | goto send; |
| 932 | |
| 933 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ |
| 934 | csum_partial: |
| 935 | |
| 936 | udp4_hwcsum(skb, fl4->saddr, fl4->daddr); |
| 937 | goto send; |
| 938 | |
| 939 | } else |
| 940 | csum = udp_csum(skb); |
| 941 | |
| 942 | /* add protocol-dependent pseudo-header */ |
| 943 | uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, |
| 944 | sk->sk_protocol, csum); |
| 945 | if (uh->check == 0) |
| 946 | uh->check = CSUM_MANGLED_0; |
| 947 | |
| 948 | send: |
| 949 | err = ip_send_skb(sock_net(sk), skb); |
| 950 | if (err) { |
| 951 | if (err == -ENOBUFS && !inet->recverr) { |
| 952 | UDP_INC_STATS(sock_net(sk), |
| 953 | UDP_MIB_SNDBUFERRORS, is_udplite); |
| 954 | err = 0; |
| 955 | } |
| 956 | } else |
| 957 | UDP_INC_STATS(sock_net(sk), |
| 958 | UDP_MIB_OUTDATAGRAMS, is_udplite); |
| 959 | return err; |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * Push out all pending data as one UDP datagram. Socket is locked. |
| 964 | */ |
| 965 | int udp_push_pending_frames(struct sock *sk) |
| 966 | { |
| 967 | struct udp_sock *up = udp_sk(sk); |
| 968 | struct inet_sock *inet = inet_sk(sk); |
| 969 | struct flowi4 *fl4 = &inet->cork.fl.u.ip4; |
| 970 | struct sk_buff *skb; |
| 971 | int err = 0; |
| 972 | |
| 973 | skb = ip_finish_skb(sk, fl4); |
| 974 | if (!skb) |
| 975 | goto out; |
| 976 | |
| 977 | err = udp_send_skb(skb, fl4, &inet->cork.base); |
| 978 | |
| 979 | out: |
| 980 | up->len = 0; |
| 981 | up->pending = 0; |
| 982 | return err; |
| 983 | } |
| 984 | EXPORT_SYMBOL(udp_push_pending_frames); |
| 985 | |
| 986 | static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size) |
| 987 | { |
| 988 | switch (cmsg->cmsg_type) { |
| 989 | case UDP_SEGMENT: |
| 990 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16))) |
| 991 | return -EINVAL; |
| 992 | *gso_size = *(__u16 *)CMSG_DATA(cmsg); |
| 993 | return 0; |
| 994 | default: |
| 995 | return -EINVAL; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size) |
| 1000 | { |
| 1001 | struct cmsghdr *cmsg; |
| 1002 | bool need_ip = false; |
| 1003 | int err; |
| 1004 | |
| 1005 | for_each_cmsghdr(cmsg, msg) { |
| 1006 | if (!CMSG_OK(msg, cmsg)) |
| 1007 | return -EINVAL; |
| 1008 | |
| 1009 | if (cmsg->cmsg_level != SOL_UDP) { |
| 1010 | need_ip = true; |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | err = __udp_cmsg_send(cmsg, gso_size); |
| 1015 | if (err) |
| 1016 | return err; |
| 1017 | } |
| 1018 | |
| 1019 | return need_ip; |
| 1020 | } |
| 1021 | EXPORT_SYMBOL_GPL(udp_cmsg_send); |
| 1022 | |
| 1023 | int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) |
| 1024 | { |
| 1025 | struct inet_sock *inet = inet_sk(sk); |
| 1026 | struct udp_sock *up = udp_sk(sk); |
| 1027 | DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); |
| 1028 | struct flowi4 fl4_stack; |
| 1029 | struct flowi4 *fl4; |
| 1030 | int ulen = len; |
| 1031 | struct ipcm_cookie ipc; |
| 1032 | struct rtable *rt = NULL; |
| 1033 | int free = 0; |
| 1034 | int connected = 0; |
| 1035 | __be32 daddr, faddr, saddr; |
| 1036 | __be16 dport; |
| 1037 | u8 tos; |
| 1038 | int err, is_udplite = IS_UDPLITE(sk); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1039 | int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1040 | int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); |
| 1041 | struct sk_buff *skb; |
| 1042 | struct ip_options_data opt_copy; |
| 1043 | |
| 1044 | if (len > 0xFFFF) |
| 1045 | return -EMSGSIZE; |
| 1046 | |
| 1047 | /* |
| 1048 | * Check the flags. |
| 1049 | */ |
| 1050 | |
| 1051 | if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ |
| 1052 | return -EOPNOTSUPP; |
| 1053 | |
| 1054 | getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
| 1055 | |
| 1056 | fl4 = &inet->cork.fl.u.ip4; |
| 1057 | if (up->pending) { |
| 1058 | /* |
| 1059 | * There are pending frames. |
| 1060 | * The socket lock must be held while it's corked. |
| 1061 | */ |
| 1062 | lock_sock(sk); |
| 1063 | if (likely(up->pending)) { |
| 1064 | if (unlikely(up->pending != AF_INET)) { |
| 1065 | release_sock(sk); |
| 1066 | return -EINVAL; |
| 1067 | } |
| 1068 | goto do_append_data; |
| 1069 | } |
| 1070 | release_sock(sk); |
| 1071 | } |
| 1072 | ulen += sizeof(struct udphdr); |
| 1073 | |
| 1074 | /* |
| 1075 | * Get and verify the address. |
| 1076 | */ |
| 1077 | if (usin) { |
| 1078 | if (msg->msg_namelen < sizeof(*usin)) |
| 1079 | return -EINVAL; |
| 1080 | if (usin->sin_family != AF_INET) { |
| 1081 | if (usin->sin_family != AF_UNSPEC) |
| 1082 | return -EAFNOSUPPORT; |
| 1083 | } |
| 1084 | |
| 1085 | daddr = usin->sin_addr.s_addr; |
| 1086 | dport = usin->sin_port; |
| 1087 | if (dport == 0) |
| 1088 | return -EINVAL; |
| 1089 | } else { |
| 1090 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1091 | return -EDESTADDRREQ; |
| 1092 | daddr = inet->inet_daddr; |
| 1093 | dport = inet->inet_dport; |
| 1094 | /* Open fast path for connected socket. |
| 1095 | Route will not be used, if at least one option is set. |
| 1096 | */ |
| 1097 | connected = 1; |
| 1098 | } |
| 1099 | |
| 1100 | ipcm_init_sk(&ipc, inet); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1101 | ipc.gso_size = READ_ONCE(up->gso_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1102 | |
| 1103 | if (msg->msg_controllen) { |
| 1104 | err = udp_cmsg_send(sk, msg, &ipc.gso_size); |
| 1105 | if (err > 0) |
| 1106 | err = ip_cmsg_send(sk, msg, &ipc, |
| 1107 | sk->sk_family == AF_INET6); |
| 1108 | if (unlikely(err < 0)) { |
| 1109 | kfree(ipc.opt); |
| 1110 | return err; |
| 1111 | } |
| 1112 | if (ipc.opt) |
| 1113 | free = 1; |
| 1114 | connected = 0; |
| 1115 | } |
| 1116 | if (!ipc.opt) { |
| 1117 | struct ip_options_rcu *inet_opt; |
| 1118 | |
| 1119 | rcu_read_lock(); |
| 1120 | inet_opt = rcu_dereference(inet->inet_opt); |
| 1121 | if (inet_opt) { |
| 1122 | memcpy(&opt_copy, inet_opt, |
| 1123 | sizeof(*inet_opt) + inet_opt->opt.optlen); |
| 1124 | ipc.opt = &opt_copy.opt; |
| 1125 | } |
| 1126 | rcu_read_unlock(); |
| 1127 | } |
| 1128 | |
| 1129 | if (cgroup_bpf_enabled && !connected) { |
| 1130 | err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, |
| 1131 | (struct sockaddr *)usin, &ipc.addr); |
| 1132 | if (err) |
| 1133 | goto out_free; |
| 1134 | if (usin) { |
| 1135 | if (usin->sin_port == 0) { |
| 1136 | /* BPF program set invalid port. Reject it. */ |
| 1137 | err = -EINVAL; |
| 1138 | goto out_free; |
| 1139 | } |
| 1140 | daddr = usin->sin_addr.s_addr; |
| 1141 | dport = usin->sin_port; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | saddr = ipc.addr; |
| 1146 | ipc.addr = faddr = daddr; |
| 1147 | |
| 1148 | if (ipc.opt && ipc.opt->opt.srr) { |
| 1149 | if (!daddr) { |
| 1150 | err = -EINVAL; |
| 1151 | goto out_free; |
| 1152 | } |
| 1153 | faddr = ipc.opt->opt.faddr; |
| 1154 | connected = 0; |
| 1155 | } |
| 1156 | tos = get_rttos(&ipc, inet); |
| 1157 | if (sock_flag(sk, SOCK_LOCALROUTE) || |
| 1158 | (msg->msg_flags & MSG_DONTROUTE) || |
| 1159 | (ipc.opt && ipc.opt->opt.is_strictroute)) { |
| 1160 | tos |= RTO_ONLINK; |
| 1161 | connected = 0; |
| 1162 | } |
| 1163 | |
| 1164 | if (ipv4_is_multicast(daddr)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1165 | if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1166 | ipc.oif = inet->mc_index; |
| 1167 | if (!saddr) |
| 1168 | saddr = inet->mc_addr; |
| 1169 | connected = 0; |
| 1170 | } else if (!ipc.oif) { |
| 1171 | ipc.oif = inet->uc_index; |
| 1172 | } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { |
| 1173 | /* oif is set, packet is to local broadcast and |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1174 | * uc_index is set. oif is most likely set |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1175 | * by sk_bound_dev_if. If uc_index != oif check if the |
| 1176 | * oif is an L3 master and uc_index is an L3 slave. |
| 1177 | * If so, we want to allow the send using the uc_index. |
| 1178 | */ |
| 1179 | if (ipc.oif != inet->uc_index && |
| 1180 | ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), |
| 1181 | inet->uc_index)) { |
| 1182 | ipc.oif = inet->uc_index; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | if (connected) |
| 1187 | rt = (struct rtable *)sk_dst_check(sk, 0); |
| 1188 | |
| 1189 | if (!rt) { |
| 1190 | struct net *net = sock_net(sk); |
| 1191 | __u8 flow_flags = inet_sk_flowi_flags(sk); |
| 1192 | |
| 1193 | fl4 = &fl4_stack; |
| 1194 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1195 | flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1196 | RT_SCOPE_UNIVERSE, sk->sk_protocol, |
| 1197 | flow_flags, |
| 1198 | faddr, saddr, dport, inet->inet_sport, |
| 1199 | sk->sk_uid); |
| 1200 | |
| 1201 | security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); |
| 1202 | rt = ip_route_output_flow(net, fl4, sk); |
| 1203 | if (IS_ERR(rt)) { |
| 1204 | err = PTR_ERR(rt); |
| 1205 | rt = NULL; |
| 1206 | if (err == -ENETUNREACH) |
| 1207 | IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); |
| 1208 | goto out; |
| 1209 | } |
| 1210 | |
| 1211 | err = -EACCES; |
| 1212 | if ((rt->rt_flags & RTCF_BROADCAST) && |
| 1213 | !sock_flag(sk, SOCK_BROADCAST)) |
| 1214 | goto out; |
| 1215 | if (connected) |
| 1216 | sk_dst_set(sk, dst_clone(&rt->dst)); |
| 1217 | } |
| 1218 | |
| 1219 | if (msg->msg_flags&MSG_CONFIRM) |
| 1220 | goto do_confirm; |
| 1221 | back_from_confirm: |
| 1222 | |
| 1223 | saddr = fl4->saddr; |
| 1224 | if (!ipc.addr) |
| 1225 | daddr = ipc.addr = fl4->daddr; |
| 1226 | |
| 1227 | /* Lockless fast path for the non-corking case. */ |
| 1228 | if (!corkreq) { |
| 1229 | struct inet_cork cork; |
| 1230 | |
| 1231 | skb = ip_make_skb(sk, fl4, getfrag, msg, ulen, |
| 1232 | sizeof(struct udphdr), &ipc, &rt, |
| 1233 | &cork, msg->msg_flags); |
| 1234 | err = PTR_ERR(skb); |
| 1235 | if (!IS_ERR_OR_NULL(skb)) |
| 1236 | err = udp_send_skb(skb, fl4, &cork); |
| 1237 | goto out; |
| 1238 | } |
| 1239 | |
| 1240 | lock_sock(sk); |
| 1241 | if (unlikely(up->pending)) { |
| 1242 | /* The socket is already corked while preparing it. */ |
| 1243 | /* ... which is an evident application bug. --ANK */ |
| 1244 | release_sock(sk); |
| 1245 | |
| 1246 | net_dbg_ratelimited("socket already corked\n"); |
| 1247 | err = -EINVAL; |
| 1248 | goto out; |
| 1249 | } |
| 1250 | /* |
| 1251 | * Now cork the socket to pend data. |
| 1252 | */ |
| 1253 | fl4 = &inet->cork.fl.u.ip4; |
| 1254 | fl4->daddr = daddr; |
| 1255 | fl4->saddr = saddr; |
| 1256 | fl4->fl4_dport = dport; |
| 1257 | fl4->fl4_sport = inet->inet_sport; |
| 1258 | up->pending = AF_INET; |
| 1259 | |
| 1260 | do_append_data: |
| 1261 | up->len += ulen; |
| 1262 | err = ip_append_data(sk, fl4, getfrag, msg, ulen, |
| 1263 | sizeof(struct udphdr), &ipc, &rt, |
| 1264 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
| 1265 | if (err) |
| 1266 | udp_flush_pending_frames(sk); |
| 1267 | else if (!corkreq) |
| 1268 | err = udp_push_pending_frames(sk); |
| 1269 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) |
| 1270 | up->pending = 0; |
| 1271 | release_sock(sk); |
| 1272 | |
| 1273 | out: |
| 1274 | ip_rt_put(rt); |
| 1275 | out_free: |
| 1276 | if (free) |
| 1277 | kfree(ipc.opt); |
| 1278 | if (!err) |
| 1279 | return len; |
| 1280 | /* |
| 1281 | * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting |
| 1282 | * ENOBUFS might not be good (it's not tunable per se), but otherwise |
| 1283 | * we don't have a good statistic (IpOutDiscards but it can be too many |
| 1284 | * things). We could add another new stat but at least for now that |
| 1285 | * seems like overkill. |
| 1286 | */ |
| 1287 | if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { |
| 1288 | UDP_INC_STATS(sock_net(sk), |
| 1289 | UDP_MIB_SNDBUFERRORS, is_udplite); |
| 1290 | } |
| 1291 | return err; |
| 1292 | |
| 1293 | do_confirm: |
| 1294 | if (msg->msg_flags & MSG_PROBE) |
| 1295 | dst_confirm_neigh(&rt->dst, &fl4->daddr); |
| 1296 | if (!(msg->msg_flags&MSG_PROBE) || len) |
| 1297 | goto back_from_confirm; |
| 1298 | err = 0; |
| 1299 | goto out; |
| 1300 | } |
| 1301 | EXPORT_SYMBOL(udp_sendmsg); |
| 1302 | |
| 1303 | int udp_sendpage(struct sock *sk, struct page *page, int offset, |
| 1304 | size_t size, int flags) |
| 1305 | { |
| 1306 | struct inet_sock *inet = inet_sk(sk); |
| 1307 | struct udp_sock *up = udp_sk(sk); |
| 1308 | int ret; |
| 1309 | |
| 1310 | if (flags & MSG_SENDPAGE_NOTLAST) |
| 1311 | flags |= MSG_MORE; |
| 1312 | |
| 1313 | if (!up->pending) { |
| 1314 | struct msghdr msg = { .msg_flags = flags|MSG_MORE }; |
| 1315 | |
| 1316 | /* Call udp_sendmsg to specify destination address which |
| 1317 | * sendpage interface can't pass. |
| 1318 | * This will succeed only when the socket is connected. |
| 1319 | */ |
| 1320 | ret = udp_sendmsg(sk, &msg, 0); |
| 1321 | if (ret < 0) |
| 1322 | return ret; |
| 1323 | } |
| 1324 | |
| 1325 | lock_sock(sk); |
| 1326 | |
| 1327 | if (unlikely(!up->pending)) { |
| 1328 | release_sock(sk); |
| 1329 | |
| 1330 | net_dbg_ratelimited("cork failed\n"); |
| 1331 | return -EINVAL; |
| 1332 | } |
| 1333 | |
| 1334 | ret = ip_append_page(sk, &inet->cork.fl.u.ip4, |
| 1335 | page, offset, size, flags); |
| 1336 | if (ret == -EOPNOTSUPP) { |
| 1337 | release_sock(sk); |
| 1338 | return sock_no_sendpage(sk->sk_socket, page, offset, |
| 1339 | size, flags); |
| 1340 | } |
| 1341 | if (ret < 0) { |
| 1342 | udp_flush_pending_frames(sk); |
| 1343 | goto out; |
| 1344 | } |
| 1345 | |
| 1346 | up->len += size; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1347 | if (!(READ_ONCE(up->corkflag) || (flags&MSG_MORE))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1348 | ret = udp_push_pending_frames(sk); |
| 1349 | if (!ret) |
| 1350 | ret = size; |
| 1351 | out: |
| 1352 | release_sock(sk); |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | #define UDP_SKB_IS_STATELESS 0x80000000 |
| 1357 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1358 | /* all head states (dst, sk, nf conntrack) except skb extensions are |
| 1359 | * cleared by udp_rcv(). |
| 1360 | * |
| 1361 | * We need to preserve secpath, if present, to eventually process |
| 1362 | * IP_CMSG_PASSSEC at recvmsg() time. |
| 1363 | * |
| 1364 | * Other extensions can be cleared. |
| 1365 | */ |
| 1366 | static bool udp_try_make_stateless(struct sk_buff *skb) |
| 1367 | { |
| 1368 | if (!skb_has_extensions(skb)) |
| 1369 | return true; |
| 1370 | |
| 1371 | if (!secpath_exists(skb)) { |
| 1372 | skb_ext_reset(skb); |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1379 | static void udp_set_dev_scratch(struct sk_buff *skb) |
| 1380 | { |
| 1381 | struct udp_dev_scratch *scratch = udp_skb_scratch(skb); |
| 1382 | |
| 1383 | BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long)); |
| 1384 | scratch->_tsize_state = skb->truesize; |
| 1385 | #if BITS_PER_LONG == 64 |
| 1386 | scratch->len = skb->len; |
| 1387 | scratch->csum_unnecessary = !!skb_csum_unnecessary(skb); |
| 1388 | scratch->is_linear = !skb_is_nonlinear(skb); |
| 1389 | #endif |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1390 | if (udp_try_make_stateless(skb)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1391 | scratch->_tsize_state |= UDP_SKB_IS_STATELESS; |
| 1392 | } |
| 1393 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1394 | static void udp_skb_csum_unnecessary_set(struct sk_buff *skb) |
| 1395 | { |
| 1396 | /* We come here after udp_lib_checksum_complete() returned 0. |
| 1397 | * This means that __skb_checksum_complete() might have |
| 1398 | * set skb->csum_valid to 1. |
| 1399 | * On 64bit platforms, we can set csum_unnecessary |
| 1400 | * to true, but only if the skb is not shared. |
| 1401 | */ |
| 1402 | #if BITS_PER_LONG == 64 |
| 1403 | if (!skb_shared(skb)) |
| 1404 | udp_skb_scratch(skb)->csum_unnecessary = true; |
| 1405 | #endif |
| 1406 | } |
| 1407 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1408 | static int udp_skb_truesize(struct sk_buff *skb) |
| 1409 | { |
| 1410 | return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS; |
| 1411 | } |
| 1412 | |
| 1413 | static bool udp_skb_has_head_state(struct sk_buff *skb) |
| 1414 | { |
| 1415 | return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS); |
| 1416 | } |
| 1417 | |
| 1418 | /* fully reclaim rmem/fwd memory allocated for skb */ |
| 1419 | static void udp_rmem_release(struct sock *sk, int size, int partial, |
| 1420 | bool rx_queue_lock_held) |
| 1421 | { |
| 1422 | struct udp_sock *up = udp_sk(sk); |
| 1423 | struct sk_buff_head *sk_queue; |
| 1424 | int amt; |
| 1425 | |
| 1426 | if (likely(partial)) { |
| 1427 | up->forward_deficit += size; |
| 1428 | size = up->forward_deficit; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1429 | if (size < (sk->sk_rcvbuf >> 2) && |
| 1430 | !skb_queue_empty(&up->reader_queue)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1431 | return; |
| 1432 | } else { |
| 1433 | size += up->forward_deficit; |
| 1434 | } |
| 1435 | up->forward_deficit = 0; |
| 1436 | |
| 1437 | /* acquire the sk_receive_queue for fwd allocated memory scheduling, |
| 1438 | * if the called don't held it already |
| 1439 | */ |
| 1440 | sk_queue = &sk->sk_receive_queue; |
| 1441 | if (!rx_queue_lock_held) |
| 1442 | spin_lock(&sk_queue->lock); |
| 1443 | |
| 1444 | |
| 1445 | sk->sk_forward_alloc += size; |
| 1446 | amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1); |
| 1447 | sk->sk_forward_alloc -= amt; |
| 1448 | |
| 1449 | if (amt) |
| 1450 | __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT); |
| 1451 | |
| 1452 | atomic_sub(size, &sk->sk_rmem_alloc); |
| 1453 | |
| 1454 | /* this can save us from acquiring the rx queue lock on next receive */ |
| 1455 | skb_queue_splice_tail_init(sk_queue, &up->reader_queue); |
| 1456 | |
| 1457 | if (!rx_queue_lock_held) |
| 1458 | spin_unlock(&sk_queue->lock); |
| 1459 | } |
| 1460 | |
| 1461 | /* Note: called with reader_queue.lock held. |
| 1462 | * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch |
| 1463 | * This avoids a cache line miss while receive_queue lock is held. |
| 1464 | * Look at __udp_enqueue_schedule_skb() to find where this copy is done. |
| 1465 | */ |
| 1466 | void udp_skb_destructor(struct sock *sk, struct sk_buff *skb) |
| 1467 | { |
| 1468 | prefetch(&skb->data); |
| 1469 | udp_rmem_release(sk, udp_skb_truesize(skb), 1, false); |
| 1470 | } |
| 1471 | EXPORT_SYMBOL(udp_skb_destructor); |
| 1472 | |
| 1473 | /* as above, but the caller held the rx queue lock, too */ |
| 1474 | static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb) |
| 1475 | { |
| 1476 | prefetch(&skb->data); |
| 1477 | udp_rmem_release(sk, udp_skb_truesize(skb), 1, true); |
| 1478 | } |
| 1479 | |
| 1480 | /* Idea of busylocks is to let producers grab an extra spinlock |
| 1481 | * to relieve pressure on the receive_queue spinlock shared by consumer. |
| 1482 | * Under flood, this means that only one producer can be in line |
| 1483 | * trying to acquire the receive_queue spinlock. |
| 1484 | * These busylock can be allocated on a per cpu manner, instead of a |
| 1485 | * per socket one (that would consume a cache line per socket) |
| 1486 | */ |
| 1487 | static int udp_busylocks_log __read_mostly; |
| 1488 | static spinlock_t *udp_busylocks __read_mostly; |
| 1489 | |
| 1490 | static spinlock_t *busylock_acquire(void *ptr) |
| 1491 | { |
| 1492 | spinlock_t *busy; |
| 1493 | |
| 1494 | busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log); |
| 1495 | spin_lock(busy); |
| 1496 | return busy; |
| 1497 | } |
| 1498 | |
| 1499 | static void busylock_release(spinlock_t *busy) |
| 1500 | { |
| 1501 | if (busy) |
| 1502 | spin_unlock(busy); |
| 1503 | } |
| 1504 | |
| 1505 | int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) |
| 1506 | { |
| 1507 | struct sk_buff_head *list = &sk->sk_receive_queue; |
| 1508 | int rmem, delta, amt, err = -ENOMEM; |
| 1509 | spinlock_t *busy = NULL; |
| 1510 | int size; |
| 1511 | |
| 1512 | /* try to avoid the costly atomic add/sub pair when the receive |
| 1513 | * queue is full; always allow at least a packet |
| 1514 | */ |
| 1515 | rmem = atomic_read(&sk->sk_rmem_alloc); |
| 1516 | if (rmem > sk->sk_rcvbuf) |
| 1517 | goto drop; |
| 1518 | |
| 1519 | /* Under mem pressure, it might be helpful to help udp_recvmsg() |
| 1520 | * having linear skbs : |
| 1521 | * - Reduce memory overhead and thus increase receive queue capacity |
| 1522 | * - Less cache line misses at copyout() time |
| 1523 | * - Less work at consume_skb() (less alien page frag freeing) |
| 1524 | */ |
| 1525 | if (rmem > (sk->sk_rcvbuf >> 1)) { |
| 1526 | skb_condense(skb); |
| 1527 | |
| 1528 | busy = busylock_acquire(sk); |
| 1529 | } |
| 1530 | size = skb->truesize; |
| 1531 | udp_set_dev_scratch(skb); |
| 1532 | |
| 1533 | /* we drop only if the receive buf is full and the receive |
| 1534 | * queue contains some other skb |
| 1535 | */ |
| 1536 | rmem = atomic_add_return(size, &sk->sk_rmem_alloc); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1537 | if (rmem > (size + (unsigned int)sk->sk_rcvbuf)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1538 | goto uncharge_drop; |
| 1539 | |
| 1540 | spin_lock(&list->lock); |
| 1541 | if (size >= sk->sk_forward_alloc) { |
| 1542 | amt = sk_mem_pages(size); |
| 1543 | delta = amt << SK_MEM_QUANTUM_SHIFT; |
| 1544 | if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) { |
| 1545 | err = -ENOBUFS; |
| 1546 | spin_unlock(&list->lock); |
| 1547 | goto uncharge_drop; |
| 1548 | } |
| 1549 | |
| 1550 | sk->sk_forward_alloc += delta; |
| 1551 | } |
| 1552 | |
| 1553 | sk->sk_forward_alloc -= size; |
| 1554 | |
| 1555 | /* no need to setup a destructor, we will explicitly release the |
| 1556 | * forward allocated memory on dequeue |
| 1557 | */ |
| 1558 | sock_skb_set_dropcount(sk, skb); |
| 1559 | |
| 1560 | __skb_queue_tail(list, skb); |
| 1561 | spin_unlock(&list->lock); |
| 1562 | |
| 1563 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1564 | sk->sk_data_ready(sk); |
| 1565 | |
| 1566 | busylock_release(busy); |
| 1567 | return 0; |
| 1568 | |
| 1569 | uncharge_drop: |
| 1570 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 1571 | |
| 1572 | drop: |
| 1573 | atomic_inc(&sk->sk_drops); |
| 1574 | busylock_release(busy); |
| 1575 | return err; |
| 1576 | } |
| 1577 | EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb); |
| 1578 | |
| 1579 | void udp_destruct_sock(struct sock *sk) |
| 1580 | { |
| 1581 | /* reclaim completely the forward allocated memory */ |
| 1582 | struct udp_sock *up = udp_sk(sk); |
| 1583 | unsigned int total = 0; |
| 1584 | struct sk_buff *skb; |
| 1585 | |
| 1586 | skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue); |
| 1587 | while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) { |
| 1588 | total += skb->truesize; |
| 1589 | kfree_skb(skb); |
| 1590 | } |
| 1591 | udp_rmem_release(sk, total, 0, true); |
| 1592 | |
| 1593 | inet_sock_destruct(sk); |
| 1594 | } |
| 1595 | EXPORT_SYMBOL_GPL(udp_destruct_sock); |
| 1596 | |
| 1597 | int udp_init_sock(struct sock *sk) |
| 1598 | { |
| 1599 | skb_queue_head_init(&udp_sk(sk)->reader_queue); |
| 1600 | sk->sk_destruct = udp_destruct_sock; |
| 1601 | return 0; |
| 1602 | } |
| 1603 | EXPORT_SYMBOL_GPL(udp_init_sock); |
| 1604 | |
| 1605 | void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len) |
| 1606 | { |
| 1607 | if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) { |
| 1608 | bool slow = lock_sock_fast(sk); |
| 1609 | |
| 1610 | sk_peek_offset_bwd(sk, len); |
| 1611 | unlock_sock_fast(sk, slow); |
| 1612 | } |
| 1613 | |
| 1614 | if (!skb_unref(skb)) |
| 1615 | return; |
| 1616 | |
| 1617 | /* In the more common cases we cleared the head states previously, |
| 1618 | * see __udp_queue_rcv_skb(). |
| 1619 | */ |
| 1620 | if (unlikely(udp_skb_has_head_state(skb))) |
| 1621 | skb_release_head_state(skb); |
| 1622 | __consume_stateless_skb(skb); |
| 1623 | } |
| 1624 | EXPORT_SYMBOL_GPL(skb_consume_udp); |
| 1625 | |
| 1626 | static struct sk_buff *__first_packet_length(struct sock *sk, |
| 1627 | struct sk_buff_head *rcvq, |
| 1628 | int *total) |
| 1629 | { |
| 1630 | struct sk_buff *skb; |
| 1631 | |
| 1632 | while ((skb = skb_peek(rcvq)) != NULL) { |
| 1633 | if (udp_lib_checksum_complete(skb)) { |
| 1634 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, |
| 1635 | IS_UDPLITE(sk)); |
| 1636 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, |
| 1637 | IS_UDPLITE(sk)); |
| 1638 | atomic_inc(&sk->sk_drops); |
| 1639 | __skb_unlink(skb, rcvq); |
| 1640 | *total += skb->truesize; |
| 1641 | kfree_skb(skb); |
| 1642 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1643 | udp_skb_csum_unnecessary_set(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | return skb; |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * first_packet_length - return length of first packet in receive queue |
| 1652 | * @sk: socket |
| 1653 | * |
| 1654 | * Drops all bad checksum frames, until a valid one is found. |
| 1655 | * Returns the length of found skb, or -1 if none is found. |
| 1656 | */ |
| 1657 | static int first_packet_length(struct sock *sk) |
| 1658 | { |
| 1659 | struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue; |
| 1660 | struct sk_buff_head *sk_queue = &sk->sk_receive_queue; |
| 1661 | struct sk_buff *skb; |
| 1662 | int total = 0; |
| 1663 | int res; |
| 1664 | |
| 1665 | spin_lock_bh(&rcvq->lock); |
| 1666 | skb = __first_packet_length(sk, rcvq, &total); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1667 | if (!skb && !skb_queue_empty_lockless(sk_queue)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1668 | spin_lock(&sk_queue->lock); |
| 1669 | skb_queue_splice_tail_init(sk_queue, rcvq); |
| 1670 | spin_unlock(&sk_queue->lock); |
| 1671 | |
| 1672 | skb = __first_packet_length(sk, rcvq, &total); |
| 1673 | } |
| 1674 | res = skb ? skb->len : -1; |
| 1675 | if (total) |
| 1676 | udp_rmem_release(sk, total, 1, false); |
| 1677 | spin_unlock_bh(&rcvq->lock); |
| 1678 | return res; |
| 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * IOCTL requests applicable to the UDP protocol |
| 1683 | */ |
| 1684 | |
| 1685 | int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 1686 | { |
| 1687 | switch (cmd) { |
| 1688 | case SIOCOUTQ: |
| 1689 | { |
| 1690 | int amount = sk_wmem_alloc_get(sk); |
| 1691 | |
| 1692 | return put_user(amount, (int __user *)arg); |
| 1693 | } |
| 1694 | |
| 1695 | case SIOCINQ: |
| 1696 | { |
| 1697 | int amount = max_t(int, 0, first_packet_length(sk)); |
| 1698 | |
| 1699 | return put_user(amount, (int __user *)arg); |
| 1700 | } |
| 1701 | |
| 1702 | default: |
| 1703 | return -ENOIOCTLCMD; |
| 1704 | } |
| 1705 | |
| 1706 | return 0; |
| 1707 | } |
| 1708 | EXPORT_SYMBOL(udp_ioctl); |
| 1709 | |
| 1710 | struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1711 | int noblock, int *off, int *err) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1712 | { |
| 1713 | struct sk_buff_head *sk_queue = &sk->sk_receive_queue; |
| 1714 | struct sk_buff_head *queue; |
| 1715 | struct sk_buff *last; |
| 1716 | long timeo; |
| 1717 | int error; |
| 1718 | |
| 1719 | queue = &udp_sk(sk)->reader_queue; |
| 1720 | flags |= noblock ? MSG_DONTWAIT : 0; |
| 1721 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1722 | do { |
| 1723 | struct sk_buff *skb; |
| 1724 | |
| 1725 | error = sock_error(sk); |
| 1726 | if (error) |
| 1727 | break; |
| 1728 | |
| 1729 | error = -EAGAIN; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1730 | do { |
| 1731 | spin_lock_bh(&queue->lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1732 | skb = __skb_try_recv_from_queue(sk, queue, flags, off, |
| 1733 | err, &last); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1734 | if (skb) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1735 | if (!(flags & MSG_PEEK)) |
| 1736 | udp_skb_destructor(sk, skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1737 | spin_unlock_bh(&queue->lock); |
| 1738 | return skb; |
| 1739 | } |
| 1740 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1741 | if (skb_queue_empty_lockless(sk_queue)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1742 | spin_unlock_bh(&queue->lock); |
| 1743 | goto busy_check; |
| 1744 | } |
| 1745 | |
| 1746 | /* refill the reader queue and walk it again |
| 1747 | * keep both queues locked to avoid re-acquiring |
| 1748 | * the sk_receive_queue lock if fwd memory scheduling |
| 1749 | * is needed. |
| 1750 | */ |
| 1751 | spin_lock(&sk_queue->lock); |
| 1752 | skb_queue_splice_tail_init(sk_queue, queue); |
| 1753 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1754 | skb = __skb_try_recv_from_queue(sk, queue, flags, off, |
| 1755 | err, &last); |
| 1756 | if (skb && !(flags & MSG_PEEK)) |
| 1757 | udp_skb_dtor_locked(sk, skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1758 | spin_unlock(&sk_queue->lock); |
| 1759 | spin_unlock_bh(&queue->lock); |
| 1760 | if (skb) |
| 1761 | return skb; |
| 1762 | |
| 1763 | busy_check: |
| 1764 | if (!sk_can_busy_loop(sk)) |
| 1765 | break; |
| 1766 | |
| 1767 | sk_busy_loop(sk, flags & MSG_DONTWAIT); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1768 | } while (!skb_queue_empty_lockless(sk_queue)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1769 | |
| 1770 | /* sk_queue is empty, reader_queue may contain peeked packets */ |
| 1771 | } while (timeo && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1772 | !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, |
| 1773 | &error, &timeo, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1774 | (struct sk_buff *)sk_queue)); |
| 1775 | |
| 1776 | *err = error; |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | EXPORT_SYMBOL(__skb_recv_udp); |
| 1780 | |
| 1781 | /* |
| 1782 | * This should be easy, if there is something there we |
| 1783 | * return it, otherwise we block. |
| 1784 | */ |
| 1785 | |
| 1786 | int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, |
| 1787 | int flags, int *addr_len) |
| 1788 | { |
| 1789 | struct inet_sock *inet = inet_sk(sk); |
| 1790 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
| 1791 | struct sk_buff *skb; |
| 1792 | unsigned int ulen, copied; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1793 | int off, err, peeking = flags & MSG_PEEK; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1794 | int is_udplite = IS_UDPLITE(sk); |
| 1795 | bool checksum_valid = false; |
| 1796 | |
| 1797 | if (flags & MSG_ERRQUEUE) |
| 1798 | return ip_recv_error(sk, msg, len, addr_len); |
| 1799 | |
| 1800 | try_again: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1801 | off = sk_peek_offset(sk, flags); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1802 | skb = __skb_recv_udp(sk, flags, noblock, &off, &err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1803 | if (!skb) |
| 1804 | return err; |
| 1805 | |
| 1806 | ulen = udp_skb_len(skb); |
| 1807 | copied = len; |
| 1808 | if (copied > ulen - off) |
| 1809 | copied = ulen - off; |
| 1810 | else if (copied < ulen) |
| 1811 | msg->msg_flags |= MSG_TRUNC; |
| 1812 | |
| 1813 | /* |
| 1814 | * If checksum is needed at all, try to do it while copying the |
| 1815 | * data. If the data is truncated, or if we only want a partial |
| 1816 | * coverage checksum (UDP-Lite), do it before the copy. |
| 1817 | */ |
| 1818 | |
| 1819 | if (copied < ulen || peeking || |
| 1820 | (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { |
| 1821 | checksum_valid = udp_skb_csum_unnecessary(skb) || |
| 1822 | !__udp_lib_checksum_complete(skb); |
| 1823 | if (!checksum_valid) |
| 1824 | goto csum_copy_err; |
| 1825 | } |
| 1826 | |
| 1827 | if (checksum_valid || udp_skb_csum_unnecessary(skb)) { |
| 1828 | if (udp_skb_is_linear(skb)) |
| 1829 | err = copy_linear_skb(skb, copied, off, &msg->msg_iter); |
| 1830 | else |
| 1831 | err = skb_copy_datagram_msg(skb, off, msg, copied); |
| 1832 | } else { |
| 1833 | err = skb_copy_and_csum_datagram_msg(skb, off, msg); |
| 1834 | |
| 1835 | if (err == -EINVAL) |
| 1836 | goto csum_copy_err; |
| 1837 | } |
| 1838 | |
| 1839 | if (unlikely(err)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1840 | if (!peeking) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1841 | atomic_inc(&sk->sk_drops); |
| 1842 | UDP_INC_STATS(sock_net(sk), |
| 1843 | UDP_MIB_INERRORS, is_udplite); |
| 1844 | } |
| 1845 | kfree_skb(skb); |
| 1846 | return err; |
| 1847 | } |
| 1848 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1849 | if (!peeking) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1850 | UDP_INC_STATS(sock_net(sk), |
| 1851 | UDP_MIB_INDATAGRAMS, is_udplite); |
| 1852 | |
| 1853 | sock_recv_ts_and_drops(msg, sk, skb); |
| 1854 | |
| 1855 | /* Copy the address. */ |
| 1856 | if (sin) { |
| 1857 | sin->sin_family = AF_INET; |
| 1858 | sin->sin_port = udp_hdr(skb)->source; |
| 1859 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 1860 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 1861 | *addr_len = sizeof(*sin); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1862 | |
| 1863 | if (cgroup_bpf_enabled) |
| 1864 | BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, |
| 1865 | (struct sockaddr *)sin); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1866 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (udp_sk(sk)->gro_enabled) |
| 1869 | udp_cmsg_recv(msg, sk, skb); |
| 1870 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1871 | if (inet->cmsg_flags) |
| 1872 | ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off); |
| 1873 | |
| 1874 | err = copied; |
| 1875 | if (flags & MSG_TRUNC) |
| 1876 | err = ulen; |
| 1877 | |
| 1878 | skb_consume_udp(sk, skb, peeking ? -err : err); |
| 1879 | return err; |
| 1880 | |
| 1881 | csum_copy_err: |
| 1882 | if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, |
| 1883 | udp_skb_destructor)) { |
| 1884 | UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); |
| 1885 | UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
| 1886 | } |
| 1887 | kfree_skb(skb); |
| 1888 | |
| 1889 | /* starting over for a new packet, but check if we need to yield */ |
| 1890 | cond_resched(); |
| 1891 | msg->msg_flags &= ~MSG_TRUNC; |
| 1892 | goto try_again; |
| 1893 | } |
| 1894 | |
| 1895 | int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 1896 | { |
| 1897 | /* This check is replicated from __ip4_datagram_connect() and |
| 1898 | * intended to prevent BPF program called below from accessing bytes |
| 1899 | * that are out of the bound specified by user in addr_len. |
| 1900 | */ |
| 1901 | if (addr_len < sizeof(struct sockaddr_in)) |
| 1902 | return -EINVAL; |
| 1903 | |
| 1904 | return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr); |
| 1905 | } |
| 1906 | EXPORT_SYMBOL(udp_pre_connect); |
| 1907 | |
| 1908 | int __udp_disconnect(struct sock *sk, int flags) |
| 1909 | { |
| 1910 | struct inet_sock *inet = inet_sk(sk); |
| 1911 | /* |
| 1912 | * 1003.1g - break association. |
| 1913 | */ |
| 1914 | |
| 1915 | sk->sk_state = TCP_CLOSE; |
| 1916 | inet->inet_daddr = 0; |
| 1917 | inet->inet_dport = 0; |
| 1918 | sock_rps_reset_rxhash(sk); |
| 1919 | sk->sk_bound_dev_if = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1920 | if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1921 | inet_reset_saddr(sk); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1922 | if (sk->sk_prot->rehash && |
| 1923 | (sk->sk_userlocks & SOCK_BINDPORT_LOCK)) |
| 1924 | sk->sk_prot->rehash(sk); |
| 1925 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1926 | |
| 1927 | if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { |
| 1928 | sk->sk_prot->unhash(sk); |
| 1929 | inet->inet_sport = 0; |
| 1930 | } |
| 1931 | sk_dst_reset(sk); |
| 1932 | return 0; |
| 1933 | } |
| 1934 | EXPORT_SYMBOL(__udp_disconnect); |
| 1935 | |
| 1936 | int udp_disconnect(struct sock *sk, int flags) |
| 1937 | { |
| 1938 | lock_sock(sk); |
| 1939 | __udp_disconnect(sk, flags); |
| 1940 | release_sock(sk); |
| 1941 | return 0; |
| 1942 | } |
| 1943 | EXPORT_SYMBOL(udp_disconnect); |
| 1944 | |
| 1945 | void udp_lib_unhash(struct sock *sk) |
| 1946 | { |
| 1947 | if (sk_hashed(sk)) { |
| 1948 | struct udp_table *udptable = sk->sk_prot->h.udp_table; |
| 1949 | struct udp_hslot *hslot, *hslot2; |
| 1950 | |
| 1951 | hslot = udp_hashslot(udptable, sock_net(sk), |
| 1952 | udp_sk(sk)->udp_port_hash); |
| 1953 | hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); |
| 1954 | |
| 1955 | spin_lock_bh(&hslot->lock); |
| 1956 | if (rcu_access_pointer(sk->sk_reuseport_cb)) |
| 1957 | reuseport_detach_sock(sk); |
| 1958 | if (sk_del_node_init_rcu(sk)) { |
| 1959 | hslot->count--; |
| 1960 | inet_sk(sk)->inet_num = 0; |
| 1961 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 1962 | |
| 1963 | spin_lock(&hslot2->lock); |
| 1964 | hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); |
| 1965 | hslot2->count--; |
| 1966 | spin_unlock(&hslot2->lock); |
| 1967 | } |
| 1968 | spin_unlock_bh(&hslot->lock); |
| 1969 | } |
| 1970 | } |
| 1971 | EXPORT_SYMBOL(udp_lib_unhash); |
| 1972 | |
| 1973 | /* |
| 1974 | * inet_rcv_saddr was changed, we must rehash secondary hash |
| 1975 | */ |
| 1976 | void udp_lib_rehash(struct sock *sk, u16 newhash) |
| 1977 | { |
| 1978 | if (sk_hashed(sk)) { |
| 1979 | struct udp_table *udptable = sk->sk_prot->h.udp_table; |
| 1980 | struct udp_hslot *hslot, *hslot2, *nhslot2; |
| 1981 | |
| 1982 | hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); |
| 1983 | nhslot2 = udp_hashslot2(udptable, newhash); |
| 1984 | udp_sk(sk)->udp_portaddr_hash = newhash; |
| 1985 | |
| 1986 | if (hslot2 != nhslot2 || |
| 1987 | rcu_access_pointer(sk->sk_reuseport_cb)) { |
| 1988 | hslot = udp_hashslot(udptable, sock_net(sk), |
| 1989 | udp_sk(sk)->udp_port_hash); |
| 1990 | /* we must lock primary chain too */ |
| 1991 | spin_lock_bh(&hslot->lock); |
| 1992 | if (rcu_access_pointer(sk->sk_reuseport_cb)) |
| 1993 | reuseport_detach_sock(sk); |
| 1994 | |
| 1995 | if (hslot2 != nhslot2) { |
| 1996 | spin_lock(&hslot2->lock); |
| 1997 | hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); |
| 1998 | hslot2->count--; |
| 1999 | spin_unlock(&hslot2->lock); |
| 2000 | |
| 2001 | spin_lock(&nhslot2->lock); |
| 2002 | hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, |
| 2003 | &nhslot2->head); |
| 2004 | nhslot2->count++; |
| 2005 | spin_unlock(&nhslot2->lock); |
| 2006 | } |
| 2007 | |
| 2008 | spin_unlock_bh(&hslot->lock); |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | EXPORT_SYMBOL(udp_lib_rehash); |
| 2013 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2014 | void udp_v4_rehash(struct sock *sk) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2015 | { |
| 2016 | u16 new_hash = ipv4_portaddr_hash(sock_net(sk), |
| 2017 | inet_sk(sk)->inet_rcv_saddr, |
| 2018 | inet_sk(sk)->inet_num); |
| 2019 | udp_lib_rehash(sk, new_hash); |
| 2020 | } |
| 2021 | |
| 2022 | static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 2023 | { |
| 2024 | int rc; |
| 2025 | |
| 2026 | if (inet_sk(sk)->inet_daddr) { |
| 2027 | sock_rps_save_rxhash(sk, skb); |
| 2028 | sk_mark_napi_id(sk, skb); |
| 2029 | sk_incoming_cpu_update(sk); |
| 2030 | } else { |
| 2031 | sk_mark_napi_id_once(sk, skb); |
| 2032 | } |
| 2033 | |
| 2034 | rc = __udp_enqueue_schedule_skb(sk, skb); |
| 2035 | if (rc < 0) { |
| 2036 | int is_udplite = IS_UDPLITE(sk); |
| 2037 | |
| 2038 | /* Note that an ENOMEM error is charged twice */ |
| 2039 | if (rc == -ENOMEM) |
| 2040 | UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS, |
| 2041 | is_udplite); |
| 2042 | UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
| 2043 | kfree_skb(skb); |
| 2044 | trace_udp_fail_queue_rcv_skb(rc, sk); |
| 2045 | return -1; |
| 2046 | } |
| 2047 | |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2051 | /* returns: |
| 2052 | * -1: error |
| 2053 | * 0: success |
| 2054 | * >0: "udp encap" protocol resubmission |
| 2055 | * |
| 2056 | * Note that in the success and error cases, the skb is assumed to |
| 2057 | * have either been requeued or freed. |
| 2058 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2059 | static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2060 | { |
| 2061 | struct udp_sock *up = udp_sk(sk); |
| 2062 | int is_udplite = IS_UDPLITE(sk); |
| 2063 | |
| 2064 | /* |
| 2065 | * Charge it to the socket, dropping if the queue is full. |
| 2066 | */ |
| 2067 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 2068 | goto drop; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2069 | nf_reset_ct(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2070 | |
| 2071 | if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { |
| 2072 | int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); |
| 2073 | |
| 2074 | /* |
| 2075 | * This is an encapsulation socket so pass the skb to |
| 2076 | * the socket's udp_encap_rcv() hook. Otherwise, just |
| 2077 | * fall through and pass this up the UDP socket. |
| 2078 | * up->encap_rcv() returns the following value: |
| 2079 | * =0 if skb was successfully passed to the encap |
| 2080 | * handler or was discarded by it. |
| 2081 | * >0 if skb should be passed on to UDP. |
| 2082 | * <0 if skb should be resubmitted as proto -N |
| 2083 | */ |
| 2084 | |
| 2085 | /* if we're overly short, let UDP handle it */ |
| 2086 | encap_rcv = READ_ONCE(up->encap_rcv); |
| 2087 | if (encap_rcv) { |
| 2088 | int ret; |
| 2089 | |
| 2090 | /* Verify checksum before giving to encap */ |
| 2091 | if (udp_lib_checksum_complete(skb)) |
| 2092 | goto csum_error; |
| 2093 | |
| 2094 | ret = encap_rcv(sk, skb); |
| 2095 | if (ret <= 0) { |
| 2096 | __UDP_INC_STATS(sock_net(sk), |
| 2097 | UDP_MIB_INDATAGRAMS, |
| 2098 | is_udplite); |
| 2099 | return -ret; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | /* FALLTHROUGH -- it's a UDP Packet */ |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * UDP-Lite specific tests, ignored on UDP sockets |
| 2108 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2109 | if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2110 | |
| 2111 | /* |
| 2112 | * MIB statistics other than incrementing the error count are |
| 2113 | * disabled for the following two types of errors: these depend |
| 2114 | * on the application settings, not on the functioning of the |
| 2115 | * protocol stack as such. |
| 2116 | * |
| 2117 | * RFC 3828 here recommends (sec 3.3): "There should also be a |
| 2118 | * way ... to ... at least let the receiving application block |
| 2119 | * delivery of packets with coverage values less than a value |
| 2120 | * provided by the application." |
| 2121 | */ |
| 2122 | if (up->pcrlen == 0) { /* full coverage was set */ |
| 2123 | net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n", |
| 2124 | UDP_SKB_CB(skb)->cscov, skb->len); |
| 2125 | goto drop; |
| 2126 | } |
| 2127 | /* The next case involves violating the min. coverage requested |
| 2128 | * by the receiver. This is subtle: if receiver wants x and x is |
| 2129 | * greater than the buffersize/MTU then receiver will complain |
| 2130 | * that it wants x while sender emits packets of smaller size y. |
| 2131 | * Therefore the above ...()->partial_cov statement is essential. |
| 2132 | */ |
| 2133 | if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { |
| 2134 | net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n", |
| 2135 | UDP_SKB_CB(skb)->cscov, up->pcrlen); |
| 2136 | goto drop; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | prefetch(&sk->sk_rmem_alloc); |
| 2141 | if (rcu_access_pointer(sk->sk_filter) && |
| 2142 | udp_lib_checksum_complete(skb)) |
| 2143 | goto csum_error; |
| 2144 | |
| 2145 | if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) |
| 2146 | goto drop; |
| 2147 | |
| 2148 | udp_csum_pull_header(skb); |
| 2149 | |
| 2150 | ipv4_pktinfo_prepare(sk, skb); |
| 2151 | return __udp_queue_rcv_skb(sk, skb); |
| 2152 | |
| 2153 | csum_error: |
| 2154 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); |
| 2155 | drop: |
| 2156 | __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
| 2157 | atomic_inc(&sk->sk_drops); |
| 2158 | kfree_skb(skb); |
| 2159 | return -1; |
| 2160 | } |
| 2161 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2162 | static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 2163 | { |
| 2164 | struct sk_buff *next, *segs; |
| 2165 | int ret; |
| 2166 | |
| 2167 | if (likely(!udp_unexpected_gso(sk, skb))) |
| 2168 | return udp_queue_rcv_one_skb(sk, skb); |
| 2169 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2170 | BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2171 | __skb_push(skb, -skb_mac_offset(skb)); |
| 2172 | segs = udp_rcv_segment(sk, skb, true); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2173 | skb_list_walk_safe(segs, skb, next) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2174 | __skb_pull(skb, skb_transport_offset(skb)); |
| 2175 | ret = udp_queue_rcv_one_skb(sk, skb); |
| 2176 | if (ret > 0) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2177 | ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2178 | } |
| 2179 | return 0; |
| 2180 | } |
| 2181 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2182 | /* For TCP sockets, sk_rx_dst is protected by socket lock |
| 2183 | * For UDP, we use xchg() to guard against concurrent changes. |
| 2184 | */ |
| 2185 | bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) |
| 2186 | { |
| 2187 | struct dst_entry *old; |
| 2188 | |
| 2189 | if (dst_hold_safe(dst)) { |
| 2190 | old = xchg(&sk->sk_rx_dst, dst); |
| 2191 | dst_release(old); |
| 2192 | return old != dst; |
| 2193 | } |
| 2194 | return false; |
| 2195 | } |
| 2196 | EXPORT_SYMBOL(udp_sk_rx_dst_set); |
| 2197 | |
| 2198 | /* |
| 2199 | * Multicasts and broadcasts go to each listener. |
| 2200 | * |
| 2201 | * Note: called only from the BH handler context. |
| 2202 | */ |
| 2203 | static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
| 2204 | struct udphdr *uh, |
| 2205 | __be32 saddr, __be32 daddr, |
| 2206 | struct udp_table *udptable, |
| 2207 | int proto) |
| 2208 | { |
| 2209 | struct sock *sk, *first = NULL; |
| 2210 | unsigned short hnum = ntohs(uh->dest); |
| 2211 | struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); |
| 2212 | unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); |
| 2213 | unsigned int offset = offsetof(typeof(*sk), sk_node); |
| 2214 | int dif = skb->dev->ifindex; |
| 2215 | int sdif = inet_sdif(skb); |
| 2216 | struct hlist_node *node; |
| 2217 | struct sk_buff *nskb; |
| 2218 | |
| 2219 | if (use_hash2) { |
| 2220 | hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) & |
| 2221 | udptable->mask; |
| 2222 | hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask; |
| 2223 | start_lookup: |
| 2224 | hslot = &udptable->hash2[hash2]; |
| 2225 | offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); |
| 2226 | } |
| 2227 | |
| 2228 | sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { |
| 2229 | if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr, |
| 2230 | uh->source, saddr, dif, sdif, hnum)) |
| 2231 | continue; |
| 2232 | |
| 2233 | if (!first) { |
| 2234 | first = sk; |
| 2235 | continue; |
| 2236 | } |
| 2237 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 2238 | |
| 2239 | if (unlikely(!nskb)) { |
| 2240 | atomic_inc(&sk->sk_drops); |
| 2241 | __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS, |
| 2242 | IS_UDPLITE(sk)); |
| 2243 | __UDP_INC_STATS(net, UDP_MIB_INERRORS, |
| 2244 | IS_UDPLITE(sk)); |
| 2245 | continue; |
| 2246 | } |
| 2247 | if (udp_queue_rcv_skb(sk, nskb) > 0) |
| 2248 | consume_skb(nskb); |
| 2249 | } |
| 2250 | |
| 2251 | /* Also lookup *:port if we are using hash2 and haven't done so yet. */ |
| 2252 | if (use_hash2 && hash2 != hash2_any) { |
| 2253 | hash2 = hash2_any; |
| 2254 | goto start_lookup; |
| 2255 | } |
| 2256 | |
| 2257 | if (first) { |
| 2258 | if (udp_queue_rcv_skb(first, skb) > 0) |
| 2259 | consume_skb(skb); |
| 2260 | } else { |
| 2261 | kfree_skb(skb); |
| 2262 | __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI, |
| 2263 | proto == IPPROTO_UDPLITE); |
| 2264 | } |
| 2265 | return 0; |
| 2266 | } |
| 2267 | |
| 2268 | /* Initialize UDP checksum. If exited with zero value (success), |
| 2269 | * CHECKSUM_UNNECESSARY means, that no more checks are required. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2270 | * Otherwise, csum completion requires checksumming packet body, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2271 | * including udp header and folding it to skb->csum. |
| 2272 | */ |
| 2273 | static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, |
| 2274 | int proto) |
| 2275 | { |
| 2276 | int err; |
| 2277 | |
| 2278 | UDP_SKB_CB(skb)->partial_cov = 0; |
| 2279 | UDP_SKB_CB(skb)->cscov = skb->len; |
| 2280 | |
| 2281 | if (proto == IPPROTO_UDPLITE) { |
| 2282 | err = udplite_checksum_init(skb, uh); |
| 2283 | if (err) |
| 2284 | return err; |
| 2285 | |
| 2286 | if (UDP_SKB_CB(skb)->partial_cov) { |
| 2287 | skb->csum = inet_compute_pseudo(skb, proto); |
| 2288 | return 0; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | /* Note, we are only interested in != 0 or == 0, thus the |
| 2293 | * force to int. |
| 2294 | */ |
| 2295 | err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check, |
| 2296 | inet_compute_pseudo); |
| 2297 | if (err) |
| 2298 | return err; |
| 2299 | |
| 2300 | if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) { |
| 2301 | /* If SW calculated the value, we know it's bad */ |
| 2302 | if (skb->csum_complete_sw) |
| 2303 | return 1; |
| 2304 | |
| 2305 | /* HW says the value is bad. Let's validate that. |
| 2306 | * skb->csum is no longer the full packet checksum, |
| 2307 | * so don't treat it as such. |
| 2308 | */ |
| 2309 | skb_checksum_complete_unset(skb); |
| 2310 | } |
| 2311 | |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and |
| 2316 | * return code conversion for ip layer consumption |
| 2317 | */ |
| 2318 | static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, |
| 2319 | struct udphdr *uh) |
| 2320 | { |
| 2321 | int ret; |
| 2322 | |
| 2323 | if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2324 | skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2325 | |
| 2326 | ret = udp_queue_rcv_skb(sk, skb); |
| 2327 | |
| 2328 | /* a return value > 0 means to resubmit the input, but |
| 2329 | * it wants the return to be -protocol, or 0 |
| 2330 | */ |
| 2331 | if (ret > 0) |
| 2332 | return -ret; |
| 2333 | return 0; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * All we need to do is get the socket, and then do a checksum. |
| 2338 | */ |
| 2339 | |
| 2340 | int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, |
| 2341 | int proto) |
| 2342 | { |
| 2343 | struct sock *sk; |
| 2344 | struct udphdr *uh; |
| 2345 | unsigned short ulen; |
| 2346 | struct rtable *rt = skb_rtable(skb); |
| 2347 | __be32 saddr, daddr; |
| 2348 | struct net *net = dev_net(skb->dev); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2349 | bool refcounted; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2350 | |
| 2351 | /* |
| 2352 | * Validate the packet. |
| 2353 | */ |
| 2354 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 2355 | goto drop; /* No space for header. */ |
| 2356 | |
| 2357 | uh = udp_hdr(skb); |
| 2358 | ulen = ntohs(uh->len); |
| 2359 | saddr = ip_hdr(skb)->saddr; |
| 2360 | daddr = ip_hdr(skb)->daddr; |
| 2361 | |
| 2362 | if (ulen > skb->len) |
| 2363 | goto short_packet; |
| 2364 | |
| 2365 | if (proto == IPPROTO_UDP) { |
| 2366 | /* UDP validates ulen. */ |
| 2367 | if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) |
| 2368 | goto short_packet; |
| 2369 | uh = udp_hdr(skb); |
| 2370 | } |
| 2371 | |
| 2372 | if (udp4_csum_init(skb, uh, proto)) |
| 2373 | goto csum_error; |
| 2374 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2375 | sk = skb_steal_sock(skb, &refcounted); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2376 | if (sk) { |
| 2377 | struct dst_entry *dst = skb_dst(skb); |
| 2378 | int ret; |
| 2379 | |
| 2380 | if (unlikely(sk->sk_rx_dst != dst)) |
| 2381 | udp_sk_rx_dst_set(sk, dst); |
| 2382 | |
| 2383 | ret = udp_unicast_rcv_skb(sk, skb, uh); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2384 | if (refcounted) |
| 2385 | sock_put(sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2386 | return ret; |
| 2387 | } |
| 2388 | |
| 2389 | if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) |
| 2390 | return __udp4_lib_mcast_deliver(net, skb, uh, |
| 2391 | saddr, daddr, udptable, proto); |
| 2392 | |
| 2393 | sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); |
| 2394 | if (sk) |
| 2395 | return udp_unicast_rcv_skb(sk, skb, uh); |
| 2396 | |
| 2397 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 2398 | goto drop; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2399 | nf_reset_ct(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2400 | |
| 2401 | /* No socket. Drop packet silently, if checksum is wrong */ |
| 2402 | if (udp_lib_checksum_complete(skb)) |
| 2403 | goto csum_error; |
| 2404 | |
| 2405 | __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); |
| 2406 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 2407 | |
| 2408 | /* |
| 2409 | * Hmm. We got an UDP packet to a port to which we |
| 2410 | * don't wanna listen. Ignore it. |
| 2411 | */ |
| 2412 | kfree_skb(skb); |
| 2413 | return 0; |
| 2414 | |
| 2415 | short_packet: |
| 2416 | net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", |
| 2417 | proto == IPPROTO_UDPLITE ? "Lite" : "", |
| 2418 | &saddr, ntohs(uh->source), |
| 2419 | ulen, skb->len, |
| 2420 | &daddr, ntohs(uh->dest)); |
| 2421 | goto drop; |
| 2422 | |
| 2423 | csum_error: |
| 2424 | /* |
| 2425 | * RFC1122: OK. Discards the bad packet silently (as far as |
| 2426 | * the network is concerned, anyway) as per 4.1.3.4 (MUST). |
| 2427 | */ |
| 2428 | net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", |
| 2429 | proto == IPPROTO_UDPLITE ? "Lite" : "", |
| 2430 | &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), |
| 2431 | ulen); |
| 2432 | __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); |
| 2433 | drop: |
| 2434 | __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
| 2435 | kfree_skb(skb); |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
| 2439 | /* We can only early demux multicast if there is a single matching socket. |
| 2440 | * If more than one socket found returns NULL |
| 2441 | */ |
| 2442 | static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net, |
| 2443 | __be16 loc_port, __be32 loc_addr, |
| 2444 | __be16 rmt_port, __be32 rmt_addr, |
| 2445 | int dif, int sdif) |
| 2446 | { |
| 2447 | struct sock *sk, *result; |
| 2448 | unsigned short hnum = ntohs(loc_port); |
| 2449 | unsigned int slot = udp_hashfn(net, hnum, udp_table.mask); |
| 2450 | struct udp_hslot *hslot = &udp_table.hash[slot]; |
| 2451 | |
| 2452 | /* Do not bother scanning a too big list */ |
| 2453 | if (hslot->count > 10) |
| 2454 | return NULL; |
| 2455 | |
| 2456 | result = NULL; |
| 2457 | sk_for_each_rcu(sk, &hslot->head) { |
| 2458 | if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr, |
| 2459 | rmt_port, rmt_addr, dif, sdif, hnum)) { |
| 2460 | if (result) |
| 2461 | return NULL; |
| 2462 | result = sk; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | return result; |
| 2467 | } |
| 2468 | |
| 2469 | /* For unicast we should only early demux connected sockets or we can |
| 2470 | * break forwarding setups. The chains here can be long so only check |
| 2471 | * if the first socket is an exact match and if not move on. |
| 2472 | */ |
| 2473 | static struct sock *__udp4_lib_demux_lookup(struct net *net, |
| 2474 | __be16 loc_port, __be32 loc_addr, |
| 2475 | __be16 rmt_port, __be32 rmt_addr, |
| 2476 | int dif, int sdif) |
| 2477 | { |
| 2478 | unsigned short hnum = ntohs(loc_port); |
| 2479 | unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum); |
| 2480 | unsigned int slot2 = hash2 & udp_table.mask; |
| 2481 | struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; |
| 2482 | INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr); |
| 2483 | const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); |
| 2484 | struct sock *sk; |
| 2485 | |
| 2486 | udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { |
| 2487 | if (INET_MATCH(sk, net, acookie, rmt_addr, |
| 2488 | loc_addr, ports, dif, sdif)) |
| 2489 | return sk; |
| 2490 | /* Only check first socket in chain */ |
| 2491 | break; |
| 2492 | } |
| 2493 | return NULL; |
| 2494 | } |
| 2495 | |
| 2496 | int udp_v4_early_demux(struct sk_buff *skb) |
| 2497 | { |
| 2498 | struct net *net = dev_net(skb->dev); |
| 2499 | struct in_device *in_dev = NULL; |
| 2500 | const struct iphdr *iph; |
| 2501 | const struct udphdr *uh; |
| 2502 | struct sock *sk = NULL; |
| 2503 | struct dst_entry *dst; |
| 2504 | int dif = skb->dev->ifindex; |
| 2505 | int sdif = inet_sdif(skb); |
| 2506 | int ours; |
| 2507 | |
| 2508 | /* validate the packet */ |
| 2509 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr))) |
| 2510 | return 0; |
| 2511 | |
| 2512 | iph = ip_hdr(skb); |
| 2513 | uh = udp_hdr(skb); |
| 2514 | |
| 2515 | if (skb->pkt_type == PACKET_MULTICAST) { |
| 2516 | in_dev = __in_dev_get_rcu(skb->dev); |
| 2517 | |
| 2518 | if (!in_dev) |
| 2519 | return 0; |
| 2520 | |
| 2521 | ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr, |
| 2522 | iph->protocol); |
| 2523 | if (!ours) |
| 2524 | return 0; |
| 2525 | |
| 2526 | sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr, |
| 2527 | uh->source, iph->saddr, |
| 2528 | dif, sdif); |
| 2529 | } else if (skb->pkt_type == PACKET_HOST) { |
| 2530 | sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr, |
| 2531 | uh->source, iph->saddr, dif, sdif); |
| 2532 | } |
| 2533 | |
| 2534 | if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) |
| 2535 | return 0; |
| 2536 | |
| 2537 | skb->sk = sk; |
| 2538 | skb->destructor = sock_efree; |
| 2539 | dst = READ_ONCE(sk->sk_rx_dst); |
| 2540 | |
| 2541 | if (dst) |
| 2542 | dst = dst_check(dst, 0); |
| 2543 | if (dst) { |
| 2544 | u32 itag = 0; |
| 2545 | |
| 2546 | /* set noref for now. |
| 2547 | * any place which wants to hold dst has to call |
| 2548 | * dst_hold_safe() |
| 2549 | */ |
| 2550 | skb_dst_set_noref(skb, dst); |
| 2551 | |
| 2552 | /* for unconnected multicast sockets we need to validate |
| 2553 | * the source on each packet |
| 2554 | */ |
| 2555 | if (!inet_sk(sk)->inet_daddr && in_dev) |
| 2556 | return ip_mc_validate_source(skb, iph->daddr, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2557 | iph->saddr, |
| 2558 | iph->tos & IPTOS_RT_MASK, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2559 | skb->dev, in_dev, &itag); |
| 2560 | } |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | int udp_rcv(struct sk_buff *skb) |
| 2565 | { |
| 2566 | return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); |
| 2567 | } |
| 2568 | |
| 2569 | void udp_destroy_sock(struct sock *sk) |
| 2570 | { |
| 2571 | struct udp_sock *up = udp_sk(sk); |
| 2572 | bool slow = lock_sock_fast(sk); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2573 | |
| 2574 | /* protects from races with udp_abort() */ |
| 2575 | sock_set_flag(sk, SOCK_DEAD); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2576 | udp_flush_pending_frames(sk); |
| 2577 | unlock_sock_fast(sk, slow); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2578 | if (static_branch_unlikely(&udp_encap_needed_key)) { |
| 2579 | if (up->encap_type) { |
| 2580 | void (*encap_destroy)(struct sock *sk); |
| 2581 | encap_destroy = READ_ONCE(up->encap_destroy); |
| 2582 | if (encap_destroy) |
| 2583 | encap_destroy(sk); |
| 2584 | } |
| 2585 | if (up->encap_enabled) |
| 2586 | static_branch_dec(&udp_encap_needed_key); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Socket option code for UDP |
| 2592 | */ |
| 2593 | int udp_lib_setsockopt(struct sock *sk, int level, int optname, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2594 | sockptr_t optval, unsigned int optlen, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2595 | int (*push_pending_frames)(struct sock *)) |
| 2596 | { |
| 2597 | struct udp_sock *up = udp_sk(sk); |
| 2598 | int val, valbool; |
| 2599 | int err = 0; |
| 2600 | int is_udplite = IS_UDPLITE(sk); |
| 2601 | |
| 2602 | if (optlen < sizeof(int)) |
| 2603 | return -EINVAL; |
| 2604 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2605 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2606 | return -EFAULT; |
| 2607 | |
| 2608 | valbool = val ? 1 : 0; |
| 2609 | |
| 2610 | switch (optname) { |
| 2611 | case UDP_CORK: |
| 2612 | if (val != 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2613 | WRITE_ONCE(up->corkflag, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2614 | } else { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2615 | WRITE_ONCE(up->corkflag, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2616 | lock_sock(sk); |
| 2617 | push_pending_frames(sk); |
| 2618 | release_sock(sk); |
| 2619 | } |
| 2620 | break; |
| 2621 | |
| 2622 | case UDP_ENCAP: |
| 2623 | switch (val) { |
| 2624 | case 0: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2625 | #ifdef CONFIG_XFRM |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2626 | case UDP_ENCAP_ESPINUDP: |
| 2627 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2628 | #if IS_ENABLED(CONFIG_IPV6) |
| 2629 | if (sk->sk_family == AF_INET6) |
| 2630 | up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv; |
| 2631 | else |
| 2632 | #endif |
| 2633 | up->encap_rcv = xfrm4_udp_encap_rcv; |
| 2634 | #endif |
| 2635 | fallthrough; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2636 | case UDP_ENCAP_L2TPINUDP: |
| 2637 | up->encap_type = val; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2638 | lock_sock(sk); |
| 2639 | udp_tunnel_encap_enable(sk->sk_socket); |
| 2640 | release_sock(sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2641 | break; |
| 2642 | default: |
| 2643 | err = -ENOPROTOOPT; |
| 2644 | break; |
| 2645 | } |
| 2646 | break; |
| 2647 | |
| 2648 | case UDP_NO_CHECK6_TX: |
| 2649 | up->no_check6_tx = valbool; |
| 2650 | break; |
| 2651 | |
| 2652 | case UDP_NO_CHECK6_RX: |
| 2653 | up->no_check6_rx = valbool; |
| 2654 | break; |
| 2655 | |
| 2656 | case UDP_SEGMENT: |
| 2657 | if (val < 0 || val > USHRT_MAX) |
| 2658 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2659 | WRITE_ONCE(up->gso_size, val); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2660 | break; |
| 2661 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2662 | case UDP_GRO: |
| 2663 | lock_sock(sk); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2664 | |
| 2665 | /* when enabling GRO, accept the related GSO packet type */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2666 | if (valbool) |
| 2667 | udp_tunnel_encap_enable(sk->sk_socket); |
| 2668 | up->gro_enabled = valbool; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2669 | up->accept_udp_l4 = valbool; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2670 | release_sock(sk); |
| 2671 | break; |
| 2672 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2673 | /* |
| 2674 | * UDP-Lite's partial checksum coverage (RFC 3828). |
| 2675 | */ |
| 2676 | /* The sender sets actual checksum coverage length via this option. |
| 2677 | * The case coverage > packet length is handled by send module. */ |
| 2678 | case UDPLITE_SEND_CSCOV: |
| 2679 | if (!is_udplite) /* Disable the option on UDP sockets */ |
| 2680 | return -ENOPROTOOPT; |
| 2681 | if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ |
| 2682 | val = 8; |
| 2683 | else if (val > USHRT_MAX) |
| 2684 | val = USHRT_MAX; |
| 2685 | up->pcslen = val; |
| 2686 | up->pcflag |= UDPLITE_SEND_CC; |
| 2687 | break; |
| 2688 | |
| 2689 | /* The receiver specifies a minimum checksum coverage value. To make |
| 2690 | * sense, this should be set to at least 8 (as done below). If zero is |
| 2691 | * used, this again means full checksum coverage. */ |
| 2692 | case UDPLITE_RECV_CSCOV: |
| 2693 | if (!is_udplite) /* Disable the option on UDP sockets */ |
| 2694 | return -ENOPROTOOPT; |
| 2695 | if (val != 0 && val < 8) /* Avoid silly minimal values. */ |
| 2696 | val = 8; |
| 2697 | else if (val > USHRT_MAX) |
| 2698 | val = USHRT_MAX; |
| 2699 | up->pcrlen = val; |
| 2700 | up->pcflag |= UDPLITE_RECV_CC; |
| 2701 | break; |
| 2702 | |
| 2703 | default: |
| 2704 | err = -ENOPROTOOPT; |
| 2705 | break; |
| 2706 | } |
| 2707 | |
| 2708 | return err; |
| 2709 | } |
| 2710 | EXPORT_SYMBOL(udp_lib_setsockopt); |
| 2711 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2712 | int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, |
| 2713 | unsigned int optlen) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2714 | { |
| 2715 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2716 | return udp_lib_setsockopt(sk, level, optname, |
| 2717 | optval, optlen, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2718 | udp_push_pending_frames); |
| 2719 | return ip_setsockopt(sk, level, optname, optval, optlen); |
| 2720 | } |
| 2721 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2722 | int udp_lib_getsockopt(struct sock *sk, int level, int optname, |
| 2723 | char __user *optval, int __user *optlen) |
| 2724 | { |
| 2725 | struct udp_sock *up = udp_sk(sk); |
| 2726 | int val, len; |
| 2727 | |
| 2728 | if (get_user(len, optlen)) |
| 2729 | return -EFAULT; |
| 2730 | |
| 2731 | len = min_t(unsigned int, len, sizeof(int)); |
| 2732 | |
| 2733 | if (len < 0) |
| 2734 | return -EINVAL; |
| 2735 | |
| 2736 | switch (optname) { |
| 2737 | case UDP_CORK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2738 | val = READ_ONCE(up->corkflag); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2739 | break; |
| 2740 | |
| 2741 | case UDP_ENCAP: |
| 2742 | val = up->encap_type; |
| 2743 | break; |
| 2744 | |
| 2745 | case UDP_NO_CHECK6_TX: |
| 2746 | val = up->no_check6_tx; |
| 2747 | break; |
| 2748 | |
| 2749 | case UDP_NO_CHECK6_RX: |
| 2750 | val = up->no_check6_rx; |
| 2751 | break; |
| 2752 | |
| 2753 | case UDP_SEGMENT: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2754 | val = READ_ONCE(up->gso_size); |
| 2755 | break; |
| 2756 | |
| 2757 | case UDP_GRO: |
| 2758 | val = up->gro_enabled; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2759 | break; |
| 2760 | |
| 2761 | /* The following two cannot be changed on UDP sockets, the return is |
| 2762 | * always 0 (which corresponds to the full checksum coverage of UDP). */ |
| 2763 | case UDPLITE_SEND_CSCOV: |
| 2764 | val = up->pcslen; |
| 2765 | break; |
| 2766 | |
| 2767 | case UDPLITE_RECV_CSCOV: |
| 2768 | val = up->pcrlen; |
| 2769 | break; |
| 2770 | |
| 2771 | default: |
| 2772 | return -ENOPROTOOPT; |
| 2773 | } |
| 2774 | |
| 2775 | if (put_user(len, optlen)) |
| 2776 | return -EFAULT; |
| 2777 | if (copy_to_user(optval, &val, len)) |
| 2778 | return -EFAULT; |
| 2779 | return 0; |
| 2780 | } |
| 2781 | EXPORT_SYMBOL(udp_lib_getsockopt); |
| 2782 | |
| 2783 | int udp_getsockopt(struct sock *sk, int level, int optname, |
| 2784 | char __user *optval, int __user *optlen) |
| 2785 | { |
| 2786 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 2787 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
| 2788 | return ip_getsockopt(sk, level, optname, optval, optlen); |
| 2789 | } |
| 2790 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2791 | /** |
| 2792 | * udp_poll - wait for a UDP event. |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2793 | * @file: - file struct |
| 2794 | * @sock: - socket |
| 2795 | * @wait: - poll table |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2796 | * |
| 2797 | * This is same as datagram poll, except for the special case of |
| 2798 | * blocking sockets. If application is using a blocking fd |
| 2799 | * and a packet with checksum error is in the queue; |
| 2800 | * then it could get return from select indicating data available |
| 2801 | * but then block when reading it. Add special case code |
| 2802 | * to work around these arguably broken applications. |
| 2803 | */ |
| 2804 | __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) |
| 2805 | { |
| 2806 | __poll_t mask = datagram_poll(file, sock, wait); |
| 2807 | struct sock *sk = sock->sk; |
| 2808 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2809 | if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2810 | mask |= EPOLLIN | EPOLLRDNORM; |
| 2811 | |
| 2812 | /* Check for false positives due to checksum errors */ |
| 2813 | if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) && |
| 2814 | !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) |
| 2815 | mask &= ~(EPOLLIN | EPOLLRDNORM); |
| 2816 | |
| 2817 | return mask; |
| 2818 | |
| 2819 | } |
| 2820 | EXPORT_SYMBOL(udp_poll); |
| 2821 | |
| 2822 | int udp_abort(struct sock *sk, int err) |
| 2823 | { |
| 2824 | lock_sock(sk); |
| 2825 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2826 | /* udp{v6}_destroy_sock() sets it under the sk lock, avoid racing |
| 2827 | * with close() |
| 2828 | */ |
| 2829 | if (sock_flag(sk, SOCK_DEAD)) |
| 2830 | goto out; |
| 2831 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2832 | sk->sk_err = err; |
| 2833 | sk->sk_error_report(sk); |
| 2834 | __udp_disconnect(sk, 0); |
| 2835 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2836 | out: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2837 | release_sock(sk); |
| 2838 | |
| 2839 | return 0; |
| 2840 | } |
| 2841 | EXPORT_SYMBOL_GPL(udp_abort); |
| 2842 | |
| 2843 | struct proto udp_prot = { |
| 2844 | .name = "UDP", |
| 2845 | .owner = THIS_MODULE, |
| 2846 | .close = udp_lib_close, |
| 2847 | .pre_connect = udp_pre_connect, |
| 2848 | .connect = ip4_datagram_connect, |
| 2849 | .disconnect = udp_disconnect, |
| 2850 | .ioctl = udp_ioctl, |
| 2851 | .init = udp_init_sock, |
| 2852 | .destroy = udp_destroy_sock, |
| 2853 | .setsockopt = udp_setsockopt, |
| 2854 | .getsockopt = udp_getsockopt, |
| 2855 | .sendmsg = udp_sendmsg, |
| 2856 | .recvmsg = udp_recvmsg, |
| 2857 | .sendpage = udp_sendpage, |
| 2858 | .release_cb = ip4_datagram_release_cb, |
| 2859 | .hash = udp_lib_hash, |
| 2860 | .unhash = udp_lib_unhash, |
| 2861 | .rehash = udp_v4_rehash, |
| 2862 | .get_port = udp_v4_get_port, |
| 2863 | .memory_allocated = &udp_memory_allocated, |
| 2864 | .sysctl_mem = sysctl_udp_mem, |
| 2865 | .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), |
| 2866 | .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), |
| 2867 | .obj_size = sizeof(struct udp_sock), |
| 2868 | .h.udp_table = &udp_table, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2869 | .diag_destroy = udp_abort, |
| 2870 | }; |
| 2871 | EXPORT_SYMBOL(udp_prot); |
| 2872 | |
| 2873 | /* ------------------------------------------------------------------------ */ |
| 2874 | #ifdef CONFIG_PROC_FS |
| 2875 | |
| 2876 | static struct sock *udp_get_first(struct seq_file *seq, int start) |
| 2877 | { |
| 2878 | struct sock *sk; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2879 | struct udp_seq_afinfo *afinfo; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2880 | struct udp_iter_state *state = seq->private; |
| 2881 | struct net *net = seq_file_net(seq); |
| 2882 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2883 | if (state->bpf_seq_afinfo) |
| 2884 | afinfo = state->bpf_seq_afinfo; |
| 2885 | else |
| 2886 | afinfo = PDE_DATA(file_inode(seq->file)); |
| 2887 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2888 | for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; |
| 2889 | ++state->bucket) { |
| 2890 | struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; |
| 2891 | |
| 2892 | if (hlist_empty(&hslot->head)) |
| 2893 | continue; |
| 2894 | |
| 2895 | spin_lock_bh(&hslot->lock); |
| 2896 | sk_for_each(sk, &hslot->head) { |
| 2897 | if (!net_eq(sock_net(sk), net)) |
| 2898 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2899 | if (afinfo->family == AF_UNSPEC || |
| 2900 | sk->sk_family == afinfo->family) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2901 | goto found; |
| 2902 | } |
| 2903 | spin_unlock_bh(&hslot->lock); |
| 2904 | } |
| 2905 | sk = NULL; |
| 2906 | found: |
| 2907 | return sk; |
| 2908 | } |
| 2909 | |
| 2910 | static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) |
| 2911 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2912 | struct udp_seq_afinfo *afinfo; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2913 | struct udp_iter_state *state = seq->private; |
| 2914 | struct net *net = seq_file_net(seq); |
| 2915 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2916 | if (state->bpf_seq_afinfo) |
| 2917 | afinfo = state->bpf_seq_afinfo; |
| 2918 | else |
| 2919 | afinfo = PDE_DATA(file_inode(seq->file)); |
| 2920 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2921 | do { |
| 2922 | sk = sk_next(sk); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2923 | } while (sk && (!net_eq(sock_net(sk), net) || |
| 2924 | (afinfo->family != AF_UNSPEC && |
| 2925 | sk->sk_family != afinfo->family))); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2926 | |
| 2927 | if (!sk) { |
| 2928 | if (state->bucket <= afinfo->udp_table->mask) |
| 2929 | spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); |
| 2930 | return udp_get_first(seq, state->bucket + 1); |
| 2931 | } |
| 2932 | return sk; |
| 2933 | } |
| 2934 | |
| 2935 | static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) |
| 2936 | { |
| 2937 | struct sock *sk = udp_get_first(seq, 0); |
| 2938 | |
| 2939 | if (sk) |
| 2940 | while (pos && (sk = udp_get_next(seq, sk)) != NULL) |
| 2941 | --pos; |
| 2942 | return pos ? NULL : sk; |
| 2943 | } |
| 2944 | |
| 2945 | void *udp_seq_start(struct seq_file *seq, loff_t *pos) |
| 2946 | { |
| 2947 | struct udp_iter_state *state = seq->private; |
| 2948 | state->bucket = MAX_UDP_PORTS; |
| 2949 | |
| 2950 | return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; |
| 2951 | } |
| 2952 | EXPORT_SYMBOL(udp_seq_start); |
| 2953 | |
| 2954 | void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2955 | { |
| 2956 | struct sock *sk; |
| 2957 | |
| 2958 | if (v == SEQ_START_TOKEN) |
| 2959 | sk = udp_get_idx(seq, 0); |
| 2960 | else |
| 2961 | sk = udp_get_next(seq, v); |
| 2962 | |
| 2963 | ++*pos; |
| 2964 | return sk; |
| 2965 | } |
| 2966 | EXPORT_SYMBOL(udp_seq_next); |
| 2967 | |
| 2968 | void udp_seq_stop(struct seq_file *seq, void *v) |
| 2969 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2970 | struct udp_seq_afinfo *afinfo; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2971 | struct udp_iter_state *state = seq->private; |
| 2972 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2973 | if (state->bpf_seq_afinfo) |
| 2974 | afinfo = state->bpf_seq_afinfo; |
| 2975 | else |
| 2976 | afinfo = PDE_DATA(file_inode(seq->file)); |
| 2977 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2978 | if (state->bucket <= afinfo->udp_table->mask) |
| 2979 | spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); |
| 2980 | } |
| 2981 | EXPORT_SYMBOL(udp_seq_stop); |
| 2982 | |
| 2983 | /* ------------------------------------------------------------------------ */ |
| 2984 | static void udp4_format_sock(struct sock *sp, struct seq_file *f, |
| 2985 | int bucket) |
| 2986 | { |
| 2987 | struct inet_sock *inet = inet_sk(sp); |
| 2988 | __be32 dest = inet->inet_daddr; |
| 2989 | __be32 src = inet->inet_rcv_saddr; |
| 2990 | __u16 destp = ntohs(inet->inet_dport); |
| 2991 | __u16 srcp = ntohs(inet->inet_sport); |
| 2992 | |
| 2993 | seq_printf(f, "%5d: %08X:%04X %08X:%04X" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2994 | " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2995 | bucket, src, srcp, dest, destp, sp->sk_state, |
| 2996 | sk_wmem_alloc_get(sp), |
| 2997 | udp_rqueue_get(sp), |
| 2998 | 0, 0L, 0, |
| 2999 | from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), |
| 3000 | 0, sock_i_ino(sp), |
| 3001 | refcount_read(&sp->sk_refcnt), sp, |
| 3002 | atomic_read(&sp->sk_drops)); |
| 3003 | } |
| 3004 | |
| 3005 | int udp4_seq_show(struct seq_file *seq, void *v) |
| 3006 | { |
| 3007 | seq_setwidth(seq, 127); |
| 3008 | if (v == SEQ_START_TOKEN) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3009 | seq_puts(seq, " sl local_address rem_address st tx_queue " |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3010 | "rx_queue tr tm->when retrnsmt uid timeout " |
| 3011 | "inode ref pointer drops"); |
| 3012 | else { |
| 3013 | struct udp_iter_state *state = seq->private; |
| 3014 | |
| 3015 | udp4_format_sock(v, seq, state->bucket); |
| 3016 | } |
| 3017 | seq_pad(seq, '\n'); |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3021 | #ifdef CONFIG_BPF_SYSCALL |
| 3022 | struct bpf_iter__udp { |
| 3023 | __bpf_md_ptr(struct bpf_iter_meta *, meta); |
| 3024 | __bpf_md_ptr(struct udp_sock *, udp_sk); |
| 3025 | uid_t uid __aligned(8); |
| 3026 | int bucket __aligned(8); |
| 3027 | }; |
| 3028 | |
| 3029 | static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, |
| 3030 | struct udp_sock *udp_sk, uid_t uid, int bucket) |
| 3031 | { |
| 3032 | struct bpf_iter__udp ctx; |
| 3033 | |
| 3034 | meta->seq_num--; /* skip SEQ_START_TOKEN */ |
| 3035 | ctx.meta = meta; |
| 3036 | ctx.udp_sk = udp_sk; |
| 3037 | ctx.uid = uid; |
| 3038 | ctx.bucket = bucket; |
| 3039 | return bpf_iter_run_prog(prog, &ctx); |
| 3040 | } |
| 3041 | |
| 3042 | static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v) |
| 3043 | { |
| 3044 | struct udp_iter_state *state = seq->private; |
| 3045 | struct bpf_iter_meta meta; |
| 3046 | struct bpf_prog *prog; |
| 3047 | struct sock *sk = v; |
| 3048 | uid_t uid; |
| 3049 | |
| 3050 | if (v == SEQ_START_TOKEN) |
| 3051 | return 0; |
| 3052 | |
| 3053 | uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); |
| 3054 | meta.seq = seq; |
| 3055 | prog = bpf_iter_get_info(&meta, false); |
| 3056 | return udp_prog_seq_show(prog, &meta, v, uid, state->bucket); |
| 3057 | } |
| 3058 | |
| 3059 | static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v) |
| 3060 | { |
| 3061 | struct bpf_iter_meta meta; |
| 3062 | struct bpf_prog *prog; |
| 3063 | |
| 3064 | if (!v) { |
| 3065 | meta.seq = seq; |
| 3066 | prog = bpf_iter_get_info(&meta, true); |
| 3067 | if (prog) |
| 3068 | (void)udp_prog_seq_show(prog, &meta, v, 0, 0); |
| 3069 | } |
| 3070 | |
| 3071 | udp_seq_stop(seq, v); |
| 3072 | } |
| 3073 | |
| 3074 | static const struct seq_operations bpf_iter_udp_seq_ops = { |
| 3075 | .start = udp_seq_start, |
| 3076 | .next = udp_seq_next, |
| 3077 | .stop = bpf_iter_udp_seq_stop, |
| 3078 | .show = bpf_iter_udp_seq_show, |
| 3079 | }; |
| 3080 | #endif |
| 3081 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3082 | const struct seq_operations udp_seq_ops = { |
| 3083 | .start = udp_seq_start, |
| 3084 | .next = udp_seq_next, |
| 3085 | .stop = udp_seq_stop, |
| 3086 | .show = udp4_seq_show, |
| 3087 | }; |
| 3088 | EXPORT_SYMBOL(udp_seq_ops); |
| 3089 | |
| 3090 | static struct udp_seq_afinfo udp4_seq_afinfo = { |
| 3091 | .family = AF_INET, |
| 3092 | .udp_table = &udp_table, |
| 3093 | }; |
| 3094 | |
| 3095 | static int __net_init udp4_proc_init_net(struct net *net) |
| 3096 | { |
| 3097 | if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops, |
| 3098 | sizeof(struct udp_iter_state), &udp4_seq_afinfo)) |
| 3099 | return -ENOMEM; |
| 3100 | return 0; |
| 3101 | } |
| 3102 | |
| 3103 | static void __net_exit udp4_proc_exit_net(struct net *net) |
| 3104 | { |
| 3105 | remove_proc_entry("udp", net->proc_net); |
| 3106 | } |
| 3107 | |
| 3108 | static struct pernet_operations udp4_net_ops = { |
| 3109 | .init = udp4_proc_init_net, |
| 3110 | .exit = udp4_proc_exit_net, |
| 3111 | }; |
| 3112 | |
| 3113 | int __init udp4_proc_init(void) |
| 3114 | { |
| 3115 | return register_pernet_subsys(&udp4_net_ops); |
| 3116 | } |
| 3117 | |
| 3118 | void udp4_proc_exit(void) |
| 3119 | { |
| 3120 | unregister_pernet_subsys(&udp4_net_ops); |
| 3121 | } |
| 3122 | #endif /* CONFIG_PROC_FS */ |
| 3123 | |
| 3124 | static __initdata unsigned long uhash_entries; |
| 3125 | static int __init set_uhash_entries(char *str) |
| 3126 | { |
| 3127 | ssize_t ret; |
| 3128 | |
| 3129 | if (!str) |
| 3130 | return 0; |
| 3131 | |
| 3132 | ret = kstrtoul(str, 0, &uhash_entries); |
| 3133 | if (ret) |
| 3134 | return 0; |
| 3135 | |
| 3136 | if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) |
| 3137 | uhash_entries = UDP_HTABLE_SIZE_MIN; |
| 3138 | return 1; |
| 3139 | } |
| 3140 | __setup("uhash_entries=", set_uhash_entries); |
| 3141 | |
| 3142 | void __init udp_table_init(struct udp_table *table, const char *name) |
| 3143 | { |
| 3144 | unsigned int i; |
| 3145 | |
| 3146 | table->hash = alloc_large_system_hash(name, |
| 3147 | 2 * sizeof(struct udp_hslot), |
| 3148 | uhash_entries, |
| 3149 | 21, /* one slot per 2 MB */ |
| 3150 | 0, |
| 3151 | &table->log, |
| 3152 | &table->mask, |
| 3153 | UDP_HTABLE_SIZE_MIN, |
| 3154 | 64 * 1024); |
| 3155 | |
| 3156 | table->hash2 = table->hash + (table->mask + 1); |
| 3157 | for (i = 0; i <= table->mask; i++) { |
| 3158 | INIT_HLIST_HEAD(&table->hash[i].head); |
| 3159 | table->hash[i].count = 0; |
| 3160 | spin_lock_init(&table->hash[i].lock); |
| 3161 | } |
| 3162 | for (i = 0; i <= table->mask; i++) { |
| 3163 | INIT_HLIST_HEAD(&table->hash2[i].head); |
| 3164 | table->hash2[i].count = 0; |
| 3165 | spin_lock_init(&table->hash2[i].lock); |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | u32 udp_flow_hashrnd(void) |
| 3170 | { |
| 3171 | static u32 hashrnd __read_mostly; |
| 3172 | |
| 3173 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 3174 | |
| 3175 | return hashrnd; |
| 3176 | } |
| 3177 | EXPORT_SYMBOL(udp_flow_hashrnd); |
| 3178 | |
| 3179 | static void __udp_sysctl_init(struct net *net) |
| 3180 | { |
| 3181 | net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM; |
| 3182 | net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM; |
| 3183 | |
| 3184 | #ifdef CONFIG_NET_L3_MASTER_DEV |
| 3185 | net->ipv4.sysctl_udp_l3mdev_accept = 0; |
| 3186 | #endif |
| 3187 | } |
| 3188 | |
| 3189 | static int __net_init udp_sysctl_init(struct net *net) |
| 3190 | { |
| 3191 | __udp_sysctl_init(net); |
| 3192 | return 0; |
| 3193 | } |
| 3194 | |
| 3195 | static struct pernet_operations __net_initdata udp_sysctl_ops = { |
| 3196 | .init = udp_sysctl_init, |
| 3197 | }; |
| 3198 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3199 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 3200 | DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta, |
| 3201 | struct udp_sock *udp_sk, uid_t uid, int bucket) |
| 3202 | |
| 3203 | static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux) |
| 3204 | { |
| 3205 | struct udp_iter_state *st = priv_data; |
| 3206 | struct udp_seq_afinfo *afinfo; |
| 3207 | int ret; |
| 3208 | |
| 3209 | afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN); |
| 3210 | if (!afinfo) |
| 3211 | return -ENOMEM; |
| 3212 | |
| 3213 | afinfo->family = AF_UNSPEC; |
| 3214 | afinfo->udp_table = &udp_table; |
| 3215 | st->bpf_seq_afinfo = afinfo; |
| 3216 | ret = bpf_iter_init_seq_net(priv_data, aux); |
| 3217 | if (ret) |
| 3218 | kfree(afinfo); |
| 3219 | return ret; |
| 3220 | } |
| 3221 | |
| 3222 | static void bpf_iter_fini_udp(void *priv_data) |
| 3223 | { |
| 3224 | struct udp_iter_state *st = priv_data; |
| 3225 | |
| 3226 | kfree(st->bpf_seq_afinfo); |
| 3227 | bpf_iter_fini_seq_net(priv_data); |
| 3228 | } |
| 3229 | |
| 3230 | static const struct bpf_iter_seq_info udp_seq_info = { |
| 3231 | .seq_ops = &bpf_iter_udp_seq_ops, |
| 3232 | .init_seq_private = bpf_iter_init_udp, |
| 3233 | .fini_seq_private = bpf_iter_fini_udp, |
| 3234 | .seq_priv_size = sizeof(struct udp_iter_state), |
| 3235 | }; |
| 3236 | |
| 3237 | static struct bpf_iter_reg udp_reg_info = { |
| 3238 | .target = "udp", |
| 3239 | .ctx_arg_info_size = 1, |
| 3240 | .ctx_arg_info = { |
| 3241 | { offsetof(struct bpf_iter__udp, udp_sk), |
| 3242 | PTR_TO_BTF_ID_OR_NULL }, |
| 3243 | }, |
| 3244 | .seq_info = &udp_seq_info, |
| 3245 | }; |
| 3246 | |
| 3247 | static void __init bpf_iter_register(void) |
| 3248 | { |
| 3249 | udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP]; |
| 3250 | if (bpf_iter_reg_target(&udp_reg_info)) |
| 3251 | pr_warn("Warning: could not register bpf iterator udp\n"); |
| 3252 | } |
| 3253 | #endif |
| 3254 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3255 | void __init udp_init(void) |
| 3256 | { |
| 3257 | unsigned long limit; |
| 3258 | unsigned int i; |
| 3259 | |
| 3260 | udp_table_init(&udp_table, "UDP"); |
| 3261 | limit = nr_free_buffer_pages() / 8; |
| 3262 | limit = max(limit, 128UL); |
| 3263 | sysctl_udp_mem[0] = limit / 4 * 3; |
| 3264 | sysctl_udp_mem[1] = limit; |
| 3265 | sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; |
| 3266 | |
| 3267 | __udp_sysctl_init(&init_net); |
| 3268 | |
| 3269 | /* 16 spinlocks per cpu */ |
| 3270 | udp_busylocks_log = ilog2(nr_cpu_ids) + 4; |
| 3271 | udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log, |
| 3272 | GFP_KERNEL); |
| 3273 | if (!udp_busylocks) |
| 3274 | panic("UDP: failed to alloc udp_busylocks\n"); |
| 3275 | for (i = 0; i < (1U << udp_busylocks_log); i++) |
| 3276 | spin_lock_init(udp_busylocks + i); |
| 3277 | |
| 3278 | if (register_pernet_subsys(&udp_sysctl_ops)) |
| 3279 | panic("UDP: failed to init sysctl parameters.\n"); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3280 | |
| 3281 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 3282 | bpf_iter_register(); |
| 3283 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3284 | } |