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