2019-05-27 08:55:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* INET An implementation of the TCP/IP protocol suite for the LINUX
|
|
|
|
|
* operating system. INET is implemented using the BSD Socket
|
|
|
|
|
* interface as the means of communication with the user level.
|
|
|
|
|
*
|
|
|
|
|
* Generic socket support routines. Memory allocators, socket lock/release
|
|
|
|
|
* handler for protocols to use and generic option handler.
|
|
|
|
|
*
|
2005-05-05 16:16:16 -07:00
|
|
|
* Authors: Ross Biro
|
2005-04-16 15:20:36 -07:00
|
|
|
* Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
|
|
|
|
|
* Florian La Roche, <flla@stud.uni-sb.de>
|
|
|
|
|
* Alan Cox, <A.Cox@swansea.ac.uk>
|
|
|
|
|
*
|
|
|
|
|
* Fixes:
|
|
|
|
|
* Alan Cox : Numerous verify_area() problems
|
|
|
|
|
* Alan Cox : Connecting on a connecting socket
|
|
|
|
|
* now returns an error for tcp.
|
|
|
|
|
* Alan Cox : sock->protocol is set correctly.
|
|
|
|
|
* and is not sometimes left as 0.
|
|
|
|
|
* Alan Cox : connect handles icmp errors on a
|
|
|
|
|
* connect properly. Unfortunately there
|
|
|
|
|
* is a restart syscall nasty there. I
|
|
|
|
|
* can't match BSD without hacking the C
|
|
|
|
|
* library. Ideas urgently sought!
|
|
|
|
|
* Alan Cox : Disallow bind() to addresses that are
|
|
|
|
|
* not ours - especially broadcast ones!!
|
|
|
|
|
* Alan Cox : Socket 1024 _IS_ ok for users. (fencepost)
|
|
|
|
|
* Alan Cox : sock_wfree/sock_rfree don't destroy sockets,
|
|
|
|
|
* instead they leave that for the DESTROY timer.
|
|
|
|
|
* Alan Cox : Clean up error flag in accept
|
|
|
|
|
* Alan Cox : TCP ack handling is buggy, the DESTROY timer
|
|
|
|
|
* was buggy. Put a remove_sock() in the handler
|
|
|
|
|
* for memory when we hit 0. Also altered the timer
|
2007-02-09 23:24:36 +09:00
|
|
|
* code. The ACK stuff can wait and needs major
|
2005-04-16 15:20:36 -07:00
|
|
|
* TCP layer surgery.
|
|
|
|
|
* Alan Cox : Fixed TCP ack bug, removed remove sock
|
|
|
|
|
* and fixed timer/inet_bh race.
|
|
|
|
|
* Alan Cox : Added zapped flag for TCP
|
|
|
|
|
* Alan Cox : Move kfree_skb into skbuff.c and tidied up surplus code
|
|
|
|
|
* Alan Cox : for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
|
|
|
|
|
* Alan Cox : kfree_s calls now are kfree_skbmem so we can track skb resources
|
|
|
|
|
* Alan Cox : Supports socket option broadcast now as does udp. Packet and raw need fixing.
|
|
|
|
|
* Alan Cox : Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
|
|
|
|
|
* Rick Sladkey : Relaxed UDP rules for matching packets.
|
|
|
|
|
* C.E.Hawkins : IFF_PROMISC/SIOCGHWADDR support
|
|
|
|
|
* Pauline Middelink : identd support
|
|
|
|
|
* Alan Cox : Fixed connect() taking signals I think.
|
|
|
|
|
* Alan Cox : SO_LINGER supported
|
|
|
|
|
* Alan Cox : Error reporting fixes
|
|
|
|
|
* Anonymous : inet_create tidied up (sk->reuse setting)
|
|
|
|
|
* Alan Cox : inet sockets don't set sk->type!
|
|
|
|
|
* Alan Cox : Split socket option code
|
|
|
|
|
* Alan Cox : Callbacks
|
|
|
|
|
* Alan Cox : Nagle flag for Charles & Johannes stuff
|
|
|
|
|
* Alex : Removed restriction on inet fioctl
|
|
|
|
|
* Alan Cox : Splitting INET from NET core
|
|
|
|
|
* Alan Cox : Fixed bogus SO_TYPE handling in getsockopt()
|
|
|
|
|
* Adam Caldwell : Missing return in SO_DONTROUTE/SO_DEBUG code
|
|
|
|
|
* Alan Cox : Split IP from generic code
|
|
|
|
|
* Alan Cox : New kfree_skbmem()
|
|
|
|
|
* Alan Cox : Make SO_DEBUG superuser only.
|
|
|
|
|
* Alan Cox : Allow anyone to clear SO_DEBUG
|
|
|
|
|
* (compatibility fix)
|
|
|
|
|
* Alan Cox : Added optimistic memory grabbing for AF_UNIX throughput.
|
|
|
|
|
* Alan Cox : Allocator for a socket is settable.
|
|
|
|
|
* Alan Cox : SO_ERROR includes soft errors.
|
|
|
|
|
* Alan Cox : Allow NULL arguments on some SO_ opts
|
|
|
|
|
* Alan Cox : Generic socket allocation to make hooks
|
|
|
|
|
* easier (suggested by Craig Metz).
|
|
|
|
|
* Michael Pall : SO_ERROR returns positive errno again
|
|
|
|
|
* Steve Whitehouse: Added default destructor to free
|
|
|
|
|
* protocol private data.
|
|
|
|
|
* Steve Whitehouse: Added various other default routines
|
|
|
|
|
* common to several socket families.
|
|
|
|
|
* Chris Evans : Call suser() check last on F_SETOWN
|
|
|
|
|
* Jay Schulist : Added SO_ATTACH_FILTER and SO_DETACH_FILTER.
|
|
|
|
|
* Andi Kleen : Add sock_kmalloc()/sock_kfree_s()
|
|
|
|
|
* Andi Kleen : Fix write_space callback
|
|
|
|
|
* Chris Evans : Security fixes - signedness again
|
|
|
|
|
* Arnaldo C. Melo : cleanups, use skb_queue_purge
|
|
|
|
|
*
|
|
|
|
|
* To Fix:
|
|
|
|
|
*/
|
|
|
|
|
|
2012-05-16 19:58:40 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
2024-10-01 15:35:57 -04:00
|
|
|
#include <linux/unaligned.h>
|
2006-01-11 12:17:47 -08:00
|
|
|
#include <linux/capability.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/errno.h>
|
2013-07-19 19:40:09 +02:00
|
|
|
#include <linux/errqueue.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/types.h>
|
|
|
|
|
#include <linux/socket.h>
|
|
|
|
|
#include <linux/in.h>
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
|
#include <linux/sched.h>
|
2017-05-08 15:59:53 -07:00
|
|
|
#include <linux/sched/mm.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/timer.h>
|
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
|
#include <linux/net.h>
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
|
#include <linux/poll.h>
|
|
|
|
|
#include <linux/tcp.h>
|
2024-01-18 20:17:49 +00:00
|
|
|
#include <linux/udp.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/init.h>
|
2006-10-19 16:08:53 -04:00
|
|
|
#include <linux/highmem.h>
|
2010-06-13 03:28:59 +00:00
|
|
|
#include <linux/user_namespace.h>
|
2012-02-24 08:31:31 +01:00
|
|
|
#include <linux/static_key.h>
|
2012-01-09 13:44:23 -08:00
|
|
|
#include <linux/memcontrol.h>
|
2012-05-03 02:25:55 -04:00
|
|
|
#include <linux/prefetch.h>
|
2020-07-22 09:40:27 +02:00
|
|
|
#include <linux/compat.h>
|
2023-06-09 08:27:42 -07:00
|
|
|
#include <linux/mroute.h>
|
|
|
|
|
#include <linux/mroute6.h>
|
|
|
|
|
#include <linux/icmpv6.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2016-12-24 11:46:01 -08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
#include <net/protocol.h>
|
|
|
|
|
#include <linux/skbuff.h>
|
2024-09-10 17:14:54 +00:00
|
|
|
#include <linux/skbuff_ref.h>
|
2007-09-12 12:01:34 +02:00
|
|
|
#include <net/net_namespace.h>
|
2005-06-18 22:46:52 -07:00
|
|
|
#include <net/request_sock.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <net/sock.h>
|
2024-04-29 13:40:24 +00:00
|
|
|
#include <net/proto_memory.h>
|
2009-02-12 05:03:38 +00:00
|
|
|
#include <linux/net_tstamp.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <net/xfrm.h>
|
|
|
|
|
#include <linux/ipsec.h>
|
2010-05-24 00:12:34 -07:00
|
|
|
#include <net/cls_cgroup.h>
|
2011-11-22 05:10:51 +00:00
|
|
|
#include <net/netprio_cgroup.h>
|
2015-06-15 11:26:18 -04:00
|
|
|
#include <linux/sock_diag.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
#include <linux/filter.h>
|
2016-01-04 17:41:47 -05:00
|
|
|
#include <net/sock_reuseport.h>
|
2019-04-26 16:39:39 -07:00
|
|
|
#include <net/bpf_sk_storage.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2011-06-17 12:00:03 +00:00
|
|
|
#include <trace/events/sock.h>
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <net/tcp.h>
|
2013-07-10 17:13:17 +03:00
|
|
|
#include <net/busy_poll.h>
|
2023-06-09 08:27:42 -07:00
|
|
|
#include <net/phonet/phonet.h>
|
2013-06-10 11:39:50 +03:00
|
|
|
|
2021-06-30 16:11:59 +08:00
|
|
|
#include <linux/ethtool.h>
|
|
|
|
|
|
2025-04-25 10:11:33 +02:00
|
|
|
#include <uapi/linux/pidfd.h>
|
|
|
|
|
|
2022-04-06 14:37:54 -07:00
|
|
|
#include "dev.h"
|
|
|
|
|
|
2011-12-16 00:51:59 +00:00
|
|
|
static DEFINE_MUTEX(proto_list_mutex);
|
2011-12-11 21:47:04 +00:00
|
|
|
static LIST_HEAD(proto_list);
|
|
|
|
|
|
2025-10-17 13:37:12 +00:00
|
|
|
static void sock_def_write_space_wfree(struct sock *sk, int wmem_alloc);
|
2022-04-28 11:58:18 +01:00
|
|
|
static void sock_def_write_space(struct sock *sk);
|
|
|
|
|
|
2014-04-23 14:26:56 -07:00
|
|
|
/**
|
|
|
|
|
* sk_ns_capable - General socket capability test
|
|
|
|
|
* @sk: Socket to use a capability on or through
|
|
|
|
|
* @user_ns: The user namespace of the capability to use
|
|
|
|
|
* @cap: The capability to use
|
|
|
|
|
*
|
|
|
|
|
* Test to see if the opener of the socket had when the socket was
|
|
|
|
|
* created and the current process has the capability @cap in the user
|
|
|
|
|
* namespace @user_ns.
|
|
|
|
|
*/
|
|
|
|
|
bool sk_ns_capable(const struct sock *sk,
|
|
|
|
|
struct user_namespace *user_ns, int cap)
|
|
|
|
|
{
|
|
|
|
|
return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
|
|
|
|
|
ns_capable(user_ns, cap);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_ns_capable);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* sk_capable - Socket global capability test
|
|
|
|
|
* @sk: Socket to use a capability on or through
|
2014-09-04 23:44:36 +09:00
|
|
|
* @cap: The global capability to use
|
2014-04-23 14:26:56 -07:00
|
|
|
*
|
|
|
|
|
* Test to see if the opener of the socket had when the socket was
|
|
|
|
|
* created and the current process has the capability @cap in all user
|
|
|
|
|
* namespaces.
|
|
|
|
|
*/
|
|
|
|
|
bool sk_capable(const struct sock *sk, int cap)
|
|
|
|
|
{
|
|
|
|
|
return sk_ns_capable(sk, &init_user_ns, cap);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_capable);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* sk_net_capable - Network namespace socket capability test
|
|
|
|
|
* @sk: Socket to use a capability on or through
|
|
|
|
|
* @cap: The capability to use
|
|
|
|
|
*
|
2014-09-04 23:44:36 +09:00
|
|
|
* Test to see if the opener of the socket had when the socket was created
|
2014-04-23 14:26:56 -07:00
|
|
|
* and the current process has the capability @cap over the network namespace
|
|
|
|
|
* the socket is a member of.
|
|
|
|
|
*/
|
|
|
|
|
bool sk_net_capable(const struct sock *sk, int cap)
|
|
|
|
|
{
|
|
|
|
|
return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_net_capable);
|
|
|
|
|
|
2006-07-03 00:25:12 -07:00
|
|
|
/*
|
|
|
|
|
* Each address family might have different locking rules, so we have
|
2017-03-09 08:09:05 +00:00
|
|
|
* one slock key per address family and separate keys for internal and
|
|
|
|
|
* userspace sockets.
|
2006-07-03 00:25:12 -07:00
|
|
|
*/
|
2006-07-03 00:25:35 -07:00
|
|
|
static struct lock_class_key af_family_keys[AF_MAX];
|
2017-03-09 08:09:05 +00:00
|
|
|
static struct lock_class_key af_family_kern_keys[AF_MAX];
|
2006-07-03 00:25:35 -07:00
|
|
|
static struct lock_class_key af_family_slock_keys[AF_MAX];
|
2017-03-09 08:09:05 +00:00
|
|
|
static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
|
2006-07-03 00:25:35 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make lock validator output more readable. (we pre-construct these
|
|
|
|
|
* strings build-time, so that runtime initialization of socket
|
|
|
|
|
* locks is fast):
|
|
|
|
|
*/
|
2017-03-09 08:09:05 +00:00
|
|
|
|
|
|
|
|
#define _sock_locks(x) \
|
|
|
|
|
x "AF_UNSPEC", x "AF_UNIX" , x "AF_INET" , \
|
|
|
|
|
x "AF_AX25" , x "AF_IPX" , x "AF_APPLETALK", \
|
|
|
|
|
x "AF_NETROM", x "AF_BRIDGE" , x "AF_ATMPVC" , \
|
|
|
|
|
x "AF_X25" , x "AF_INET6" , x "AF_ROSE" , \
|
|
|
|
|
x "AF_DECnet", x "AF_NETBEUI" , x "AF_SECURITY" , \
|
|
|
|
|
x "AF_KEY" , x "AF_NETLINK" , x "AF_PACKET" , \
|
|
|
|
|
x "AF_ASH" , x "AF_ECONET" , x "AF_ATMSVC" , \
|
|
|
|
|
x "AF_RDS" , x "AF_SNA" , x "AF_IRDA" , \
|
|
|
|
|
x "AF_PPPOX" , x "AF_WANPIPE" , x "AF_LLC" , \
|
|
|
|
|
x "27" , x "28" , x "AF_CAN" , \
|
|
|
|
|
x "AF_TIPC" , x "AF_BLUETOOTH", x "IUCV" , \
|
|
|
|
|
x "AF_RXRPC" , x "AF_ISDN" , x "AF_PHONET" , \
|
|
|
|
|
x "AF_IEEE802154", x "AF_CAIF" , x "AF_ALG" , \
|
|
|
|
|
x "AF_NFC" , x "AF_VSOCK" , x "AF_KCM" , \
|
2018-05-02 13:01:22 +02:00
|
|
|
x "AF_QIPCRTR", x "AF_SMC" , x "AF_XDP" , \
|
2021-07-29 10:20:39 +08:00
|
|
|
x "AF_MCTP" , \
|
2018-05-02 13:01:22 +02:00
|
|
|
x "AF_MAX"
|
2017-03-09 08:09:05 +00:00
|
|
|
|
2009-08-05 10:42:58 -07:00
|
|
|
static const char *const af_family_key_strings[AF_MAX+1] = {
|
2017-03-09 08:09:05 +00:00
|
|
|
_sock_locks("sk_lock-")
|
2006-07-03 00:25:35 -07:00
|
|
|
};
|
2009-08-05 10:42:58 -07:00
|
|
|
static const char *const af_family_slock_key_strings[AF_MAX+1] = {
|
2017-03-09 08:09:05 +00:00
|
|
|
_sock_locks("slock-")
|
2006-07-03 00:25:35 -07:00
|
|
|
};
|
2009-08-05 10:42:58 -07:00
|
|
|
static const char *const af_family_clock_key_strings[AF_MAX+1] = {
|
2017-03-09 08:09:05 +00:00
|
|
|
_sock_locks("clock-")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char *const af_family_kern_key_strings[AF_MAX+1] = {
|
|
|
|
|
_sock_locks("k-sk_lock-")
|
|
|
|
|
};
|
|
|
|
|
static const char *const af_family_kern_slock_key_strings[AF_MAX+1] = {
|
|
|
|
|
_sock_locks("k-slock-")
|
|
|
|
|
};
|
|
|
|
|
static const char *const af_family_kern_clock_key_strings[AF_MAX+1] = {
|
|
|
|
|
_sock_locks("k-clock-")
|
2007-07-19 01:49:00 -07:00
|
|
|
};
|
2017-03-09 13:54:08 +01:00
|
|
|
static const char *const af_family_rlock_key_strings[AF_MAX+1] = {
|
2018-08-02 18:14:33 +02:00
|
|
|
_sock_locks("rlock-")
|
2017-03-09 13:54:08 +01:00
|
|
|
};
|
|
|
|
|
static const char *const af_family_wlock_key_strings[AF_MAX+1] = {
|
2018-08-02 18:14:33 +02:00
|
|
|
_sock_locks("wlock-")
|
2017-03-09 13:54:08 +01:00
|
|
|
};
|
|
|
|
|
static const char *const af_family_elock_key_strings[AF_MAX+1] = {
|
2018-08-02 18:14:33 +02:00
|
|
|
_sock_locks("elock-")
|
2017-03-09 13:54:08 +01:00
|
|
|
};
|
2006-07-03 00:25:12 -07:00
|
|
|
|
|
|
|
|
/*
|
2017-03-09 13:54:08 +01:00
|
|
|
* sk_callback_lock and sk queues locking rules are per-address-family,
|
2006-07-03 00:25:12 -07:00
|
|
|
* so split the lock classes by using a per-AF key:
|
|
|
|
|
*/
|
|
|
|
|
static struct lock_class_key af_callback_keys[AF_MAX];
|
2017-03-09 13:54:08 +01:00
|
|
|
static struct lock_class_key af_rlock_keys[AF_MAX];
|
|
|
|
|
static struct lock_class_key af_wlock_keys[AF_MAX];
|
|
|
|
|
static struct lock_class_key af_elock_keys[AF_MAX];
|
2017-03-09 08:09:05 +00:00
|
|
|
static struct lock_class_key af_kern_callback_keys[AF_MAX];
|
2006-07-03 00:25:12 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/* Run time adjustable parameters. */
|
2025-08-19 17:40:30 +00:00
|
|
|
__u32 sysctl_wmem_max __read_mostly = 4 << 20;
|
2012-04-30 08:13:50 +02:00
|
|
|
EXPORT_SYMBOL(sysctl_wmem_max);
|
2025-08-19 17:40:30 +00:00
|
|
|
__u32 sysctl_rmem_max __read_mostly = 4 << 20;
|
2012-04-30 08:13:50 +02:00
|
|
|
EXPORT_SYMBOL(sysctl_rmem_max);
|
2025-08-19 17:40:30 +00:00
|
|
|
__u32 sysctl_wmem_default __read_mostly = SK_WMEM_DEFAULT;
|
|
|
|
|
__u32 sysctl_rmem_default __read_mostly = SK_RMEM_DEFAULT;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2018-05-08 09:06:59 -07:00
|
|
|
DEFINE_STATIC_KEY_FALSE(memalloc_socks_key);
|
|
|
|
|
EXPORT_SYMBOL_GPL(memalloc_socks_key);
|
2012-07-31 16:44:19 -07:00
|
|
|
|
2012-07-31 16:44:16 -07:00
|
|
|
/**
|
|
|
|
|
* sk_set_memalloc - sets %SOCK_MEMALLOC
|
|
|
|
|
* @sk: socket to set it on
|
|
|
|
|
*
|
|
|
|
|
* Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
|
|
|
|
|
* It's the responsibility of the admin to adjust min_free_kbytes
|
|
|
|
|
* to meet the requirements
|
|
|
|
|
*/
|
|
|
|
|
void sk_set_memalloc(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
sock_set_flag(sk, SOCK_MEMALLOC);
|
|
|
|
|
sk->sk_allocation |= __GFP_MEMALLOC;
|
2018-05-08 09:06:59 -07:00
|
|
|
static_branch_inc(&memalloc_socks_key);
|
2012-07-31 16:44:16 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(sk_set_memalloc);
|
|
|
|
|
|
|
|
|
|
void sk_clear_memalloc(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
sock_reset_flag(sk, SOCK_MEMALLOC);
|
|
|
|
|
sk->sk_allocation &= ~__GFP_MEMALLOC;
|
2018-05-08 09:06:59 -07:00
|
|
|
static_branch_dec(&memalloc_socks_key);
|
2012-07-31 16:44:41 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
|
2015-06-10 21:02:04 -04:00
|
|
|
* progress of swapping. SOCK_MEMALLOC may be cleared while
|
|
|
|
|
* it has rmem allocations due to the last swapfile being deactivated
|
|
|
|
|
* but there is a risk that the socket is unusable due to exceeding
|
|
|
|
|
* the rmem limits. Reclaim the reserves and obey rmem limits again.
|
2012-07-31 16:44:41 -07:00
|
|
|
*/
|
2015-06-10 21:02:04 -04:00
|
|
|
sk_mem_reclaim(sk);
|
2012-07-31 16:44:16 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(sk_clear_memalloc);
|
|
|
|
|
|
2012-07-31 16:44:26 -07:00
|
|
|
int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
2017-05-08 15:59:53 -07:00
|
|
|
unsigned int noreclaim_flag;
|
2012-07-31 16:44:26 -07:00
|
|
|
|
|
|
|
|
/* these should have been dropped before queueing */
|
|
|
|
|
BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
|
|
|
|
|
|
2017-05-08 15:59:53 -07:00
|
|
|
noreclaim_flag = memalloc_noreclaim_save();
|
2021-11-15 11:02:41 -08:00
|
|
|
ret = INDIRECT_CALL_INET(sk->sk_backlog_rcv,
|
|
|
|
|
tcp_v6_do_rcv,
|
|
|
|
|
tcp_v4_do_rcv,
|
|
|
|
|
sk, skb);
|
2017-05-08 15:59:53 -07:00
|
|
|
memalloc_noreclaim_restore(noreclaim_flag);
|
2012-07-31 16:44:26 -07:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(__sk_backlog_rcv);
|
|
|
|
|
|
2021-06-27 18:48:21 -04:00
|
|
|
void sk_error_report(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
sk->sk_error_report(sk);
|
2021-06-27 18:48:22 -04:00
|
|
|
|
|
|
|
|
switch (sk->sk_family) {
|
|
|
|
|
case AF_INET:
|
|
|
|
|
fallthrough;
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
trace_inet_sk_error_report(sk);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-06-27 18:48:21 -04:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_error_report);
|
|
|
|
|
|
2021-10-08 11:00:53 +01:00
|
|
|
int sock_get_timeout(long timeo, void *optval, bool old_timeval)
|
2019-02-02 07:34:44 -08:00
|
|
|
{
|
2019-02-02 07:34:54 -08:00
|
|
|
struct __kernel_sock_timeval tv;
|
2019-02-02 07:34:44 -08:00
|
|
|
|
|
|
|
|
if (timeo == MAX_SCHEDULE_TIMEOUT) {
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
} else {
|
|
|
|
|
tv.tv_sec = timeo / HZ;
|
|
|
|
|
tv.tv_usec = ((timeo % HZ) * USEC_PER_SEC) / HZ;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-16 22:31:14 +02:00
|
|
|
if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
|
2019-02-02 07:34:44 -08:00
|
|
|
struct old_timeval32 tv32 = { tv.tv_sec, tv.tv_usec };
|
|
|
|
|
*(struct old_timeval32 *)optval = tv32;
|
|
|
|
|
return sizeof(tv32);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 07:34:54 -08:00
|
|
|
if (old_timeval) {
|
|
|
|
|
struct __kernel_old_timeval old_tv;
|
|
|
|
|
old_tv.tv_sec = tv.tv_sec;
|
|
|
|
|
old_tv.tv_usec = tv.tv_usec;
|
|
|
|
|
*(struct __kernel_old_timeval *)optval = old_tv;
|
2019-10-09 21:08:24 -07:00
|
|
|
return sizeof(old_tv);
|
2019-02-02 07:34:54 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-09 21:08:24 -07:00
|
|
|
*(struct __kernel_sock_timeval *)optval = tv;
|
|
|
|
|
return sizeof(tv);
|
2019-02-02 07:34:44 -08:00
|
|
|
}
|
2021-10-08 11:00:53 +01:00
|
|
|
EXPORT_SYMBOL(sock_get_timeout);
|
2019-02-02 07:34:44 -08:00
|
|
|
|
2021-10-08 11:00:53 +01:00
|
|
|
int sock_copy_user_timeval(struct __kernel_sock_timeval *tv,
|
|
|
|
|
sockptr_t optval, int optlen, bool old_timeval)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2019-04-16 22:31:14 +02:00
|
|
|
if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
|
2019-02-02 07:34:44 -08:00
|
|
|
struct old_timeval32 tv32;
|
|
|
|
|
|
|
|
|
|
if (optlen < sizeof(tv32))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2020-07-23 08:08:49 +02:00
|
|
|
if (copy_from_sockptr(&tv32, optval, sizeof(tv32)))
|
2019-02-02 07:34:44 -08:00
|
|
|
return -EFAULT;
|
2021-10-08 11:00:53 +01:00
|
|
|
tv->tv_sec = tv32.tv_sec;
|
|
|
|
|
tv->tv_usec = tv32.tv_usec;
|
2019-02-02 07:34:54 -08:00
|
|
|
} else if (old_timeval) {
|
|
|
|
|
struct __kernel_old_timeval old_tv;
|
|
|
|
|
|
|
|
|
|
if (optlen < sizeof(old_tv))
|
|
|
|
|
return -EINVAL;
|
2020-07-23 08:08:49 +02:00
|
|
|
if (copy_from_sockptr(&old_tv, optval, sizeof(old_tv)))
|
2019-02-02 07:34:54 -08:00
|
|
|
return -EFAULT;
|
2021-10-08 11:00:53 +01:00
|
|
|
tv->tv_sec = old_tv.tv_sec;
|
|
|
|
|
tv->tv_usec = old_tv.tv_usec;
|
2019-02-02 07:34:44 -08:00
|
|
|
} else {
|
2021-10-08 11:00:53 +01:00
|
|
|
if (optlen < sizeof(*tv))
|
2019-02-02 07:34:44 -08:00
|
|
|
return -EINVAL;
|
2021-10-08 11:00:53 +01:00
|
|
|
if (copy_from_sockptr(tv, optval, sizeof(*tv)))
|
2019-02-02 07:34:44 -08:00
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
2021-10-08 11:00:53 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_copy_user_timeval);
|
|
|
|
|
|
|
|
|
|
static int sock_set_timeout(long *timeo_p, sockptr_t optval, int optlen,
|
|
|
|
|
bool old_timeval)
|
|
|
|
|
{
|
|
|
|
|
struct __kernel_sock_timeval tv;
|
|
|
|
|
int err = sock_copy_user_timeval(&tv, optval, optlen, old_timeval);
|
2023-07-28 15:03:12 +00:00
|
|
|
long val;
|
2021-10-08 11:00:53 +01:00
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
2007-05-24 16:58:54 -07:00
|
|
|
if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
|
|
|
|
|
return -EDOM;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-05-24 16:58:54 -07:00
|
|
|
if (tv.tv_sec < 0) {
|
2007-07-09 13:16:00 -07:00
|
|
|
static int warned __read_mostly;
|
|
|
|
|
|
2023-07-28 15:03:12 +00:00
|
|
|
WRITE_ONCE(*timeo_p, 0);
|
2008-05-02 16:20:10 -07:00
|
|
|
if (warned < 10 && net_ratelimit()) {
|
2007-05-24 16:58:54 -07:00
|
|
|
warned++;
|
2012-05-16 19:58:40 +00:00
|
|
|
pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
|
|
|
|
|
__func__, current->comm, task_pid_nr(current));
|
2008-05-02 16:20:10 -07:00
|
|
|
}
|
2007-05-24 16:58:54 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2023-07-28 15:03:12 +00:00
|
|
|
val = MAX_SCHEDULE_TIMEOUT;
|
|
|
|
|
if ((tv.tv_sec || tv.tv_usec) &&
|
|
|
|
|
(tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)))
|
|
|
|
|
val = tv.tv_sec * HZ + DIV_ROUND_UP((unsigned long)tv.tv_usec,
|
|
|
|
|
USEC_PER_SEC / HZ);
|
|
|
|
|
WRITE_ONCE(*timeo_p, val);
|
2005-04-16 15:20:36 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 09:44:54 +01:00
|
|
|
static bool sk_set_prio_allowed(const struct sock *sk, int val)
|
|
|
|
|
{
|
|
|
|
|
return ((val >= TC_PRIO_BESTEFFORT && val <= TC_PRIO_INTERACTIVE) ||
|
|
|
|
|
sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
|
|
|
|
|
sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN));
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 13:51:37 +01:00
|
|
|
static bool sock_needs_netstamp(const struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
switch (sk->sk_family) {
|
|
|
|
|
case AF_UNSPEC:
|
|
|
|
|
case AF_UNIX:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-28 12:04:18 +00:00
|
|
|
static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
|
2007-02-09 23:24:36 +09:00
|
|
|
{
|
2011-11-28 12:04:18 +00:00
|
|
|
if (sk->sk_flags & flags) {
|
|
|
|
|
sk->sk_flags &= ~flags;
|
2015-10-26 13:51:37 +01:00
|
|
|
if (sock_needs_netstamp(sk) &&
|
|
|
|
|
!(sk->sk_flags & SK_FLAGS_TIMESTAMP))
|
2009-02-12 05:03:38 +00:00
|
|
|
net_disable_timestamp();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-05 12:41:15 -04:00
|
|
|
int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
|
2006-03-28 01:08:21 -08:00
|
|
|
{
|
2009-10-12 13:26:31 -07:00
|
|
|
unsigned long flags;
|
|
|
|
|
struct sk_buff_head *list = &sk->sk_receive_queue;
|
2006-03-28 01:08:21 -08:00
|
|
|
|
2024-03-21 16:44:10 +08:00
|
|
|
if (atomic_read(&sk->sk_rmem_alloc) >= READ_ONCE(sk->sk_rcvbuf)) {
|
2025-08-26 12:50:27 +00:00
|
|
|
sk_drops_inc(sk);
|
2011-06-17 12:00:03 +00:00
|
|
|
trace_sock_rcvqueue_full(sk, skb);
|
2009-10-14 20:40:11 -07:00
|
|
|
return -ENOMEM;
|
2006-03-28 01:08:21 -08:00
|
|
|
}
|
|
|
|
|
|
2012-07-31 16:44:41 -07:00
|
|
|
if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
|
2025-08-26 12:50:27 +00:00
|
|
|
sk_drops_inc(sk);
|
2009-10-14 20:40:11 -07:00
|
|
|
return -ENOBUFS;
|
2007-12-31 00:11:19 -08:00
|
|
|
}
|
|
|
|
|
|
2006-03-28 01:08:21 -08:00
|
|
|
skb->dev = NULL;
|
|
|
|
|
skb_set_owner_r(skb, sk);
|
2008-12-17 22:11:38 -08:00
|
|
|
|
2010-05-11 23:19:48 +00:00
|
|
|
/* we escape from rcu protected region, make sure we dont leak
|
|
|
|
|
* a norefcounted dst
|
|
|
|
|
*/
|
|
|
|
|
skb_dst_force(skb);
|
|
|
|
|
|
2009-10-12 13:26:31 -07:00
|
|
|
spin_lock_irqsave(&list->lock, flags);
|
2015-03-01 14:58:30 +02:00
|
|
|
sock_skb_set_dropcount(sk, skb);
|
2009-10-12 13:26:31 -07:00
|
|
|
__skb_queue_tail(list, skb);
|
|
|
|
|
spin_unlock_irqrestore(&list->lock, flags);
|
2006-03-28 01:08:21 -08:00
|
|
|
|
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD))
|
2014-04-11 16:15:36 -04:00
|
|
|
sk->sk_data_ready(sk);
|
2009-10-14 20:40:11 -07:00
|
|
|
return 0;
|
2006-03-28 01:08:21 -08:00
|
|
|
}
|
2016-04-05 12:41:15 -04:00
|
|
|
EXPORT_SYMBOL(__sock_queue_rcv_skb);
|
|
|
|
|
|
2022-04-07 14:20:49 +08:00
|
|
|
int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
|
|
|
|
|
enum skb_drop_reason *reason)
|
2016-04-05 12:41:15 -04:00
|
|
|
{
|
2022-04-07 14:20:49 +08:00
|
|
|
enum skb_drop_reason drop_reason;
|
2016-04-05 12:41:15 -04:00
|
|
|
int err;
|
|
|
|
|
|
2025-07-16 18:26:53 +02:00
|
|
|
err = sk_filter_reason(sk, skb, &drop_reason);
|
|
|
|
|
if (err)
|
2022-04-07 14:20:49 +08:00
|
|
|
goto out;
|
2025-07-16 18:26:53 +02:00
|
|
|
|
2022-04-07 14:20:49 +08:00
|
|
|
err = __sock_queue_rcv_skb(sk, skb);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case -ENOMEM:
|
|
|
|
|
drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
|
|
|
|
|
break;
|
|
|
|
|
case -ENOBUFS:
|
|
|
|
|
drop_reason = SKB_DROP_REASON_PROTO_MEM;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
drop_reason = SKB_NOT_DROPPED_YET;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
out:
|
|
|
|
|
if (reason)
|
|
|
|
|
*reason = drop_reason;
|
|
|
|
|
return err;
|
2016-04-05 12:41:15 -04:00
|
|
|
}
|
2022-04-07 14:20:49 +08:00
|
|
|
EXPORT_SYMBOL(sock_queue_rcv_skb_reason);
|
2006-03-28 01:08:21 -08:00
|
|
|
|
2016-07-12 18:18:57 -04:00
|
|
|
int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
|
2016-11-02 17:14:41 -07:00
|
|
|
const int nested, unsigned int trim_cap, bool refcounted)
|
2006-03-28 01:08:21 -08:00
|
|
|
{
|
2025-07-16 18:26:53 +02:00
|
|
|
enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
|
2006-03-28 01:08:21 -08:00
|
|
|
int rc = NET_RX_SUCCESS;
|
2025-07-16 18:26:53 +02:00
|
|
|
int err;
|
2006-03-28 01:08:21 -08:00
|
|
|
|
2025-07-16 18:26:53 +02:00
|
|
|
if (sk_filter_trim_cap(sk, skb, trim_cap, &reason))
|
2006-03-28 01:08:21 -08:00
|
|
|
goto discard_and_relse;
|
|
|
|
|
|
|
|
|
|
skb->dev = NULL;
|
|
|
|
|
|
2024-03-21 16:44:10 +08:00
|
|
|
if (sk_rcvqueues_full(sk, READ_ONCE(sk->sk_rcvbuf))) {
|
2025-08-26 12:50:27 +00:00
|
|
|
sk_drops_inc(sk);
|
2025-07-16 18:26:53 +02:00
|
|
|
reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
|
2010-04-27 15:13:20 -07:00
|
|
|
goto discard_and_relse;
|
|
|
|
|
}
|
2006-11-16 14:06:06 -02:00
|
|
|
if (nested)
|
|
|
|
|
bh_lock_sock_nested(sk);
|
|
|
|
|
else
|
|
|
|
|
bh_lock_sock(sk);
|
2006-07-03 00:25:35 -07:00
|
|
|
if (!sock_owned_by_user(sk)) {
|
|
|
|
|
/*
|
|
|
|
|
* trylock + unlock semantics:
|
|
|
|
|
*/
|
|
|
|
|
mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
|
|
|
|
|
|
2008-10-07 14:18:42 -07:00
|
|
|
rc = sk_backlog_rcv(sk, skb);
|
2006-07-03 00:25:35 -07:00
|
|
|
|
2019-09-19 12:09:40 -04:00
|
|
|
mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
|
2025-07-16 18:26:53 +02:00
|
|
|
} else if ((err = sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))) {
|
2010-03-04 18:01:40 +00:00
|
|
|
bh_unlock_sock(sk);
|
2025-07-16 18:26:53 +02:00
|
|
|
if (err == -ENOMEM)
|
|
|
|
|
reason = SKB_DROP_REASON_PFMEMALLOC;
|
|
|
|
|
if (err == -ENOBUFS)
|
|
|
|
|
reason = SKB_DROP_REASON_SOCKET_BACKLOG;
|
2025-08-26 12:50:27 +00:00
|
|
|
sk_drops_inc(sk);
|
2010-03-04 18:01:40 +00:00
|
|
|
goto discard_and_relse;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-28 01:08:21 -08:00
|
|
|
bh_unlock_sock(sk);
|
|
|
|
|
out:
|
2016-11-02 17:14:41 -07:00
|
|
|
if (refcounted)
|
|
|
|
|
sock_put(sk);
|
2006-03-28 01:08:21 -08:00
|
|
|
return rc;
|
|
|
|
|
discard_and_relse:
|
2025-07-16 18:26:53 +02:00
|
|
|
sk_skb_reason_drop(sk, skb, reason);
|
2006-03-28 01:08:21 -08:00
|
|
|
goto out;
|
|
|
|
|
}
|
2016-07-12 18:18:57 -04:00
|
|
|
EXPORT_SYMBOL(__sk_receive_skb);
|
2006-03-28 01:08:21 -08:00
|
|
|
|
2021-02-01 17:41:32 +00:00
|
|
|
INDIRECT_CALLABLE_DECLARE(struct dst_entry *ip6_dst_check(struct dst_entry *,
|
|
|
|
|
u32));
|
|
|
|
|
INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
|
|
|
|
|
u32));
|
2006-03-28 01:08:21 -08:00
|
|
|
struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
|
|
|
|
|
{
|
2010-04-08 23:03:29 +00:00
|
|
|
struct dst_entry *dst = __sk_dst_get(sk);
|
2006-03-28 01:08:21 -08:00
|
|
|
|
2025-06-30 12:19:25 +00:00
|
|
|
if (dst && READ_ONCE(dst->obsolete) &&
|
2021-02-01 17:41:32 +00:00
|
|
|
INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check, ipv4_dst_check,
|
|
|
|
|
dst, cookie) == NULL) {
|
2009-10-19 23:46:20 +00:00
|
|
|
sk_tx_queue_clear(sk);
|
2023-09-21 20:28:18 +00:00
|
|
|
WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
|
2011-08-01 16:19:00 +00:00
|
|
|
RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
|
2006-03-28 01:08:21 -08:00
|
|
|
dst_release(dst);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(__sk_dst_check);
|
|
|
|
|
|
|
|
|
|
struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
|
|
|
|
|
{
|
|
|
|
|
struct dst_entry *dst = sk_dst_get(sk);
|
|
|
|
|
|
2025-06-30 12:19:25 +00:00
|
|
|
if (dst && READ_ONCE(dst->obsolete) &&
|
2021-02-01 17:41:32 +00:00
|
|
|
INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check, ipv4_dst_check,
|
|
|
|
|
dst, cookie) == NULL) {
|
2006-03-28 01:08:21 -08:00
|
|
|
sk_dst_reset(sk);
|
|
|
|
|
dst_release(dst);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_dst_check);
|
|
|
|
|
|
2020-05-28 07:12:13 +02:00
|
|
|
static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
|
2019-01-15 14:42:14 +01:00
|
|
|
{
|
|
|
|
|
int ret = -ENOPROTOOPT;
|
|
|
|
|
#ifdef CONFIG_NETDEVICES
|
|
|
|
|
struct net *net = sock_net(sk);
|
|
|
|
|
|
|
|
|
|
/* Sorry... */
|
|
|
|
|
ret = -EPERM;
|
2020-03-31 15:20:10 +02:00
|
|
|
if (sk->sk_bound_dev_if && !ns_capable(net->user_ns, CAP_NET_RAW))
|
2019-01-15 14:42:14 +01:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
if (ifindex < 0)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2022-05-13 11:55:44 -07:00
|
|
|
/* Paired with all READ_ONCE() done locklessly. */
|
|
|
|
|
WRITE_ONCE(sk->sk_bound_dev_if, ifindex);
|
|
|
|
|
|
2019-01-15 14:42:14 +01:00
|
|
|
if (sk->sk_prot->rehash)
|
|
|
|
|
sk->sk_prot->rehash(sk);
|
|
|
|
|
sk_dst_reset(sk);
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-30 23:09:00 +02:00
|
|
|
int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk)
|
2020-05-28 07:12:13 +02:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
2020-05-30 23:09:00 +02:00
|
|
|
if (lock_sk)
|
|
|
|
|
lock_sock(sk);
|
2020-05-28 07:12:13 +02:00
|
|
|
ret = sock_bindtoindex_locked(sk, ifindex);
|
2020-05-30 23:09:00 +02:00
|
|
|
if (lock_sk)
|
|
|
|
|
release_sock(sk);
|
2020-05-28 07:12:13 +02:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_bindtoindex);
|
|
|
|
|
|
2020-07-23 08:08:48 +02:00
|
|
|
static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen)
|
2007-09-14 16:41:03 -07:00
|
|
|
{
|
|
|
|
|
int ret = -ENOPROTOOPT;
|
|
|
|
|
#ifdef CONFIG_NETDEVICES
|
2008-03-26 02:26:21 +09:00
|
|
|
struct net *net = sock_net(sk);
|
2007-09-14 16:41:03 -07:00
|
|
|
char devname[IFNAMSIZ];
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
if (optlen < 0)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
/* Bind this socket to a particular device like "eth0",
|
|
|
|
|
* as specified in the passed interface name. If the
|
|
|
|
|
* name is "" or the option length is zero the socket
|
|
|
|
|
* is not bound.
|
|
|
|
|
*/
|
|
|
|
|
if (optlen > IFNAMSIZ - 1)
|
|
|
|
|
optlen = IFNAMSIZ - 1;
|
|
|
|
|
memset(devname, 0, sizeof(devname));
|
|
|
|
|
|
|
|
|
|
ret = -EFAULT;
|
2020-07-23 08:08:48 +02:00
|
|
|
if (copy_from_sockptr(devname, optval, optlen))
|
2007-09-14 16:41:03 -07:00
|
|
|
goto out;
|
|
|
|
|
|
2009-11-05 22:37:11 -08:00
|
|
|
index = 0;
|
|
|
|
|
if (devname[0] != '\0') {
|
2009-11-05 21:03:39 -08:00
|
|
|
struct net_device *dev;
|
2007-09-14 16:41:03 -07:00
|
|
|
|
2009-11-05 21:03:39 -08:00
|
|
|
rcu_read_lock();
|
|
|
|
|
dev = dev_get_by_name_rcu(net, devname);
|
|
|
|
|
if (dev)
|
|
|
|
|
index = dev->ifindex;
|
|
|
|
|
rcu_read_unlock();
|
2007-09-14 16:41:03 -07:00
|
|
|
ret = -ENODEV;
|
|
|
|
|
if (!dev)
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-16 23:17:17 -07:00
|
|
|
sockopt_lock_sock(sk);
|
|
|
|
|
ret = sock_bindtoindex_locked(sk, index);
|
|
|
|
|
sockopt_release_sock(sk);
|
2007-09-14 16:41:03 -07:00
|
|
|
out:
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 17:28:02 -07:00
|
|
|
static int sock_getbindtodevice(struct sock *sk, sockptr_t optval,
|
|
|
|
|
sockptr_t optlen, int len)
|
2012-11-26 05:21:08 +00:00
|
|
|
{
|
|
|
|
|
int ret = -ENOPROTOOPT;
|
|
|
|
|
#ifdef CONFIG_NETDEVICES
|
2022-05-13 11:55:44 -07:00
|
|
|
int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
|
2012-11-26 05:21:08 +00:00
|
|
|
struct net *net = sock_net(sk);
|
|
|
|
|
char devname[IFNAMSIZ];
|
|
|
|
|
|
2022-05-13 11:55:44 -07:00
|
|
|
if (bound_dev_if == 0) {
|
2012-11-26 05:21:08 +00:00
|
|
|
len = 0;
|
|
|
|
|
goto zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
if (len < IFNAMSIZ)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2022-05-13 11:55:44 -07:00
|
|
|
ret = netdev_get_name(net, devname, bound_dev_if);
|
2013-06-26 17:23:42 +02:00
|
|
|
if (ret)
|
2012-11-26 05:21:08 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
len = strlen(devname) + 1;
|
|
|
|
|
|
|
|
|
|
ret = -EFAULT;
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_to_sockptr(optval, devname, len))
|
2012-11-26 05:21:08 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
zero:
|
|
|
|
|
ret = -EFAULT;
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_to_sockptr(optlen, &len, sizeof(int)))
|
2012-11-26 05:21:08 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 16:02:00 +00:00
|
|
|
bool sk_mc_loop(const struct sock *sk)
|
2015-04-01 17:07:44 +02:00
|
|
|
{
|
|
|
|
|
if (dev_recursion_level())
|
|
|
|
|
return false;
|
|
|
|
|
if (!sk)
|
|
|
|
|
return true;
|
2023-08-30 10:12:44 +00:00
|
|
|
/* IPV6_ADDRFORM can change sk->sk_family under us. */
|
|
|
|
|
switch (READ_ONCE(sk->sk_family)) {
|
2015-04-01 17:07:44 +02:00
|
|
|
case AF_INET:
|
2023-08-16 08:15:39 +00:00
|
|
|
return inet_test_bit(MC_LOOP, sk);
|
2015-04-01 17:07:44 +02:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
|
|
|
case AF_INET6:
|
2023-09-12 16:02:00 +00:00
|
|
|
return inet6_test_bit(MC6_LOOP, sk);
|
2015-04-01 17:07:44 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
2020-06-17 22:23:25 -07:00
|
|
|
WARN_ON_ONCE(1);
|
2015-04-01 17:07:44 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_mc_loop);
|
|
|
|
|
|
2020-05-28 07:12:09 +02:00
|
|
|
void sock_set_reuseaddr(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
sk->sk_reuse = SK_CAN_REUSE;
|
|
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_reuseaddr);
|
|
|
|
|
|
2020-05-28 07:12:17 +02:00
|
|
|
void sock_set_reuseport(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
sk->sk_reuseport = true;
|
|
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_reuseport);
|
|
|
|
|
|
2020-05-28 07:12:10 +02:00
|
|
|
void sock_no_linger(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
lock_sock(sk);
|
2023-08-19 04:06:46 +00:00
|
|
|
WRITE_ONCE(sk->sk_lingertime, 0);
|
2020-05-28 07:12:10 +02:00
|
|
|
sock_set_flag(sk, SOCK_LINGER);
|
|
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_no_linger);
|
|
|
|
|
|
2020-05-28 07:12:11 +02:00
|
|
|
void sock_set_priority(struct sock *sk, u32 priority)
|
|
|
|
|
{
|
2023-07-28 15:03:18 +00:00
|
|
|
WRITE_ONCE(sk->sk_priority, priority);
|
2020-05-28 07:12:11 +02:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_priority);
|
|
|
|
|
|
2020-05-28 07:12:12 +02:00
|
|
|
void sock_set_sndtimeo(struct sock *sk, s64 secs)
|
|
|
|
|
{
|
|
|
|
|
if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
|
2023-07-28 15:03:12 +00:00
|
|
|
WRITE_ONCE(sk->sk_sndtimeo, secs * HZ);
|
2020-05-28 07:12:12 +02:00
|
|
|
else
|
2023-07-28 15:03:12 +00:00
|
|
|
WRITE_ONCE(sk->sk_sndtimeo, MAX_SCHEDULE_TIMEOUT);
|
2020-05-28 07:12:12 +02:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_sndtimeo);
|
|
|
|
|
|
2020-05-28 07:12:14 +02:00
|
|
|
static void __sock_set_timestamps(struct sock *sk, bool val, bool new, bool ns)
|
|
|
|
|
{
|
2024-10-17 21:34:35 +08:00
|
|
|
sock_valbool_flag(sk, SOCK_RCVTSTAMP, val);
|
|
|
|
|
sock_valbool_flag(sk, SOCK_RCVTSTAMPNS, val && ns);
|
2020-05-28 07:12:14 +02:00
|
|
|
if (val) {
|
|
|
|
|
sock_valbool_flag(sk, SOCK_TSTAMP_NEW, new);
|
|
|
|
|
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 16:24:27 -07:00
|
|
|
void sock_set_timestamp(struct sock *sk, int optname, bool valbool)
|
|
|
|
|
{
|
|
|
|
|
switch (optname) {
|
|
|
|
|
case SO_TIMESTAMP_OLD:
|
|
|
|
|
__sock_set_timestamps(sk, valbool, false, false);
|
|
|
|
|
break;
|
|
|
|
|
case SO_TIMESTAMP_NEW:
|
|
|
|
|
__sock_set_timestamps(sk, valbool, true, false);
|
|
|
|
|
break;
|
|
|
|
|
case SO_TIMESTAMPNS_OLD:
|
|
|
|
|
__sock_set_timestamps(sk, valbool, false, true);
|
|
|
|
|
break;
|
|
|
|
|
case SO_TIMESTAMPNS_NEW:
|
|
|
|
|
__sock_set_timestamps(sk, valbool, true, true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 16:11:59 +08:00
|
|
|
static int sock_timestamping_bind_phc(struct sock *sk, int phc_index)
|
2021-06-03 16:24:28 -07:00
|
|
|
{
|
2021-06-30 16:11:59 +08:00
|
|
|
struct net *net = sock_net(sk);
|
|
|
|
|
struct net_device *dev = NULL;
|
|
|
|
|
bool match = false;
|
|
|
|
|
int *vclock_index;
|
|
|
|
|
int i, num;
|
|
|
|
|
|
|
|
|
|
if (sk->sk_bound_dev_if)
|
|
|
|
|
dev = dev_get_by_index(net, sk->sk_bound_dev_if);
|
|
|
|
|
|
|
|
|
|
if (!dev) {
|
|
|
|
|
pr_err("%s: sock not bind to device\n", __func__);
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num = ethtool_get_phc_vclocks(dev, &vclock_index);
|
2022-01-11 16:10:53 +01:00
|
|
|
dev_put(dev);
|
|
|
|
|
|
2021-06-30 16:11:59 +08:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
|
if (*(vclock_index + i) == phc_index) {
|
|
|
|
|
match = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (num > 0)
|
|
|
|
|
kfree(vclock_index);
|
|
|
|
|
|
|
|
|
|
if (!match)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2023-08-31 13:52:12 +00:00
|
|
|
WRITE_ONCE(sk->sk_bind_phc, phc_index);
|
2021-06-30 16:11:59 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sock_set_timestamping(struct sock *sk, int optname,
|
|
|
|
|
struct so_timestamping timestamping)
|
|
|
|
|
{
|
|
|
|
|
int val = timestamping.flags;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2021-06-03 16:24:28 -07:00
|
|
|
if (val & ~SOF_TIMESTAMPING_MASK)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2022-12-07 09:37:01 -05:00
|
|
|
if (val & SOF_TIMESTAMPING_OPT_ID_TCP &&
|
|
|
|
|
!(val & SOF_TIMESTAMPING_OPT_ID))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2021-06-03 16:24:28 -07:00
|
|
|
if (val & SOF_TIMESTAMPING_OPT_ID &&
|
|
|
|
|
!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
|
2021-11-15 11:02:33 -08:00
|
|
|
if (sk_is_tcp(sk)) {
|
2021-06-03 16:24:28 -07:00
|
|
|
if ((1 << sk->sk_state) &
|
|
|
|
|
(TCPF_CLOSE | TCPF_LISTEN))
|
|
|
|
|
return -EINVAL;
|
2022-12-07 09:37:01 -05:00
|
|
|
if (val & SOF_TIMESTAMPING_OPT_ID_TCP)
|
|
|
|
|
atomic_set(&sk->sk_tskey, tcp_sk(sk)->write_seq);
|
|
|
|
|
else
|
|
|
|
|
atomic_set(&sk->sk_tskey, tcp_sk(sk)->snd_una);
|
2021-06-03 16:24:28 -07:00
|
|
|
} else {
|
2022-02-17 09:05:02 -08:00
|
|
|
atomic_set(&sk->sk_tskey, 0);
|
2021-06-03 16:24:28 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val & SOF_TIMESTAMPING_OPT_STATS &&
|
|
|
|
|
!(val & SOF_TIMESTAMPING_OPT_TSONLY))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2021-06-30 16:11:59 +08:00
|
|
|
if (val & SOF_TIMESTAMPING_BIND_PHC) {
|
|
|
|
|
ret = sock_timestamping_bind_phc(sk, timestamping.bind_phc);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 13:52:11 +00:00
|
|
|
WRITE_ONCE(sk->sk_tsflags, val);
|
2021-06-03 16:24:28 -07:00
|
|
|
sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW);
|
2025-02-11 18:17:31 +01:00
|
|
|
sock_valbool_flag(sk, SOCK_TIMESTAMPING_ANY, !!(val & TSFLAGS_ANY));
|
2021-06-03 16:24:28 -07:00
|
|
|
|
|
|
|
|
if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
|
|
|
|
|
sock_enable_timestamp(sk,
|
|
|
|
|
SOCK_TIMESTAMPING_RX_SOFTWARE);
|
|
|
|
|
else
|
|
|
|
|
sock_disable_timestamp(sk,
|
|
|
|
|
(1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 15:29:30 +08:00
|
|
|
#if defined(CONFIG_CGROUP_BPF)
|
|
|
|
|
void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op)
|
|
|
|
|
{
|
|
|
|
|
struct bpf_sock_ops_kern sock_ops;
|
|
|
|
|
|
|
|
|
|
memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
|
|
|
|
|
sock_ops.op = op;
|
|
|
|
|
sock_ops.is_fullsock = 1;
|
|
|
|
|
sock_ops.sk = sk;
|
|
|
|
|
bpf_skops_init_skb(&sock_ops, skb, 0);
|
|
|
|
|
__cgroup_bpf_run_filter_sock_ops(sk, &sock_ops, CGROUP_SOCK_OPS);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-05-28 07:12:15 +02:00
|
|
|
void sock_set_keepalive(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
if (sk->sk_prot->keepalive)
|
|
|
|
|
sk->sk_prot->keepalive(sk, true);
|
|
|
|
|
sock_valbool_flag(sk, SOCK_KEEPOPEN, true);
|
|
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_keepalive);
|
|
|
|
|
|
2020-05-28 07:12:16 +02:00
|
|
|
static void __sock_set_rcvbuf(struct sock *sk, int val)
|
|
|
|
|
{
|
|
|
|
|
/* Ensure val * 2 fits into an int, to prevent max_t() from treating it
|
|
|
|
|
* as a negative value.
|
|
|
|
|
*/
|
|
|
|
|
val = min_t(int, val, INT_MAX / 2);
|
|
|
|
|
sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
|
|
|
|
|
|
|
|
|
|
/* We double it on the way in to account for "struct sk_buff" etc.
|
|
|
|
|
* overhead. Applications assume that the SO_RCVBUF setting they make
|
|
|
|
|
* will allow that much actual data to be received on that socket.
|
|
|
|
|
*
|
|
|
|
|
* Applications are unaware that "struct sk_buff" and other overheads
|
|
|
|
|
* allocate from the receive buffer during socket buffer allocation.
|
|
|
|
|
*
|
|
|
|
|
* And after considering the possible alternatives, returning the value
|
|
|
|
|
* we actually used in getsockopt is the most desirable behavior.
|
|
|
|
|
*/
|
|
|
|
|
WRITE_ONCE(sk->sk_rcvbuf, max_t(int, val * 2, SOCK_MIN_RCVBUF));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sock_set_rcvbuf(struct sock *sk, int val)
|
|
|
|
|
{
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
__sock_set_rcvbuf(sk, val);
|
|
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_rcvbuf);
|
|
|
|
|
|
2021-05-31 17:00:30 -04:00
|
|
|
static void __sock_set_mark(struct sock *sk, u32 val)
|
|
|
|
|
{
|
|
|
|
|
if (val != sk->sk_mark) {
|
2023-07-28 15:03:15 +00:00
|
|
|
WRITE_ONCE(sk->sk_mark, val);
|
2021-05-31 17:00:30 -04:00
|
|
|
sk_dst_reset(sk);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-26 13:26:48 -04:00
|
|
|
void sock_set_mark(struct sock *sk, u32 val)
|
|
|
|
|
{
|
|
|
|
|
lock_sock(sk);
|
2021-05-31 17:00:30 -04:00
|
|
|
__sock_set_mark(sk, val);
|
2020-06-26 13:26:48 -04:00
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_set_mark);
|
|
|
|
|
|
2021-09-29 10:25:11 -07:00
|
|
|
static void sock_release_reserved_memory(struct sock *sk, int bytes)
|
|
|
|
|
{
|
|
|
|
|
/* Round down bytes to multiple of pages */
|
2022-06-08 23:34:07 -07:00
|
|
|
bytes = round_down(bytes, PAGE_SIZE);
|
2021-09-29 10:25:11 -07:00
|
|
|
|
|
|
|
|
WARN_ON(bytes > sk->sk_reserved_mem);
|
2023-07-28 15:03:08 +00:00
|
|
|
WRITE_ONCE(sk->sk_reserved_mem, sk->sk_reserved_mem - bytes);
|
2021-09-29 10:25:11 -07:00
|
|
|
sk_mem_reclaim(sk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sock_reserve_memory(struct sock *sk, int bytes)
|
|
|
|
|
{
|
|
|
|
|
long allocated;
|
|
|
|
|
bool charged;
|
|
|
|
|
int pages;
|
|
|
|
|
|
2025-08-15 20:16:15 +00:00
|
|
|
if (!mem_cgroup_sk_enabled(sk) || !sk_has_account(sk))
|
2021-09-29 10:25:11 -07:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
pages = sk_mem_pages(bytes);
|
|
|
|
|
|
|
|
|
|
/* pre-charge to memcg */
|
2025-08-15 20:16:16 +00:00
|
|
|
charged = mem_cgroup_sk_charge(sk, pages,
|
|
|
|
|
GFP_KERNEL | __GFP_RETRY_MAYFAIL);
|
2021-09-29 10:25:11 -07:00
|
|
|
if (!charged)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
2025-10-14 23:54:55 +00:00
|
|
|
if (sk->sk_bypass_prot_mem)
|
|
|
|
|
goto success;
|
|
|
|
|
|
2021-09-29 10:25:11 -07:00
|
|
|
/* pre-charge to forward_alloc */
|
2022-06-10 20:30:16 -07:00
|
|
|
sk_memory_allocated_add(sk, pages);
|
|
|
|
|
allocated = sk_memory_allocated(sk);
|
2025-10-14 23:54:55 +00:00
|
|
|
|
2021-09-29 10:25:11 -07:00
|
|
|
/* If the system goes into memory pressure with this
|
|
|
|
|
* precharge, give up and return error.
|
|
|
|
|
*/
|
|
|
|
|
if (allocated > sk_prot_mem_limits(sk, 1)) {
|
|
|
|
|
sk_memory_allocated_sub(sk, pages);
|
2025-08-15 20:16:16 +00:00
|
|
|
mem_cgroup_sk_uncharge(sk, pages);
|
2021-09-29 10:25:11 -07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
2025-10-14 23:54:55 +00:00
|
|
|
|
|
|
|
|
success:
|
2023-08-31 13:52:09 +00:00
|
|
|
sk_forward_alloc_add(sk, pages << PAGE_SHIFT);
|
2021-09-29 10:25:11 -07:00
|
|
|
|
2023-07-28 15:03:08 +00:00
|
|
|
WRITE_ONCE(sk->sk_reserved_mem,
|
|
|
|
|
sk->sk_reserved_mem + (pages << PAGE_SHIFT));
|
2021-09-29 10:25:11 -07:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 17:14:54 +00:00
|
|
|
#ifdef CONFIG_PAGE_POOL
|
|
|
|
|
|
2024-11-07 21:03:30 +00:00
|
|
|
/* This is the number of tokens and frags that the user can SO_DEVMEM_DONTNEED
|
|
|
|
|
* in 1 syscall. The limit exists to limit the amount of memory the kernel
|
|
|
|
|
* allocates to copy these tokens, and to prevent looping over the frags for
|
|
|
|
|
* too long.
|
2024-09-10 17:14:54 +00:00
|
|
|
*/
|
|
|
|
|
#define MAX_DONTNEED_TOKENS 128
|
2024-11-07 21:03:30 +00:00
|
|
|
#define MAX_DONTNEED_FRAGS 1024
|
2024-09-10 17:14:54 +00:00
|
|
|
|
|
|
|
|
static noinline_for_stack int
|
|
|
|
|
sock_devmem_dontneed(struct sock *sk, sockptr_t optval, unsigned int optlen)
|
|
|
|
|
{
|
|
|
|
|
unsigned int num_tokens, i, j, k, netmem_num = 0;
|
|
|
|
|
struct dmabuf_token *tokens;
|
2024-11-07 21:03:30 +00:00
|
|
|
int ret = 0, num_frags = 0;
|
2024-09-10 17:14:54 +00:00
|
|
|
netmem_ref netmems[16];
|
|
|
|
|
|
|
|
|
|
if (!sk_is_tcp(sk))
|
|
|
|
|
return -EBADF;
|
|
|
|
|
|
2024-11-07 21:03:30 +00:00
|
|
|
if (optlen % sizeof(*tokens) ||
|
2024-09-10 17:14:54 +00:00
|
|
|
optlen > sizeof(*tokens) * MAX_DONTNEED_TOKENS)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2024-11-07 21:03:30 +00:00
|
|
|
num_tokens = optlen / sizeof(*tokens);
|
|
|
|
|
tokens = kvmalloc_array(num_tokens, sizeof(*tokens), GFP_KERNEL);
|
2024-09-10 17:14:54 +00:00
|
|
|
if (!tokens)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
if (copy_from_sockptr(tokens, optval, optlen)) {
|
|
|
|
|
kvfree(tokens);
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xa_lock_bh(&sk->sk_user_frags);
|
|
|
|
|
for (i = 0; i < num_tokens; i++) {
|
|
|
|
|
for (j = 0; j < tokens[i].token_count; j++) {
|
2024-11-07 21:03:30 +00:00
|
|
|
if (++num_frags > MAX_DONTNEED_FRAGS)
|
|
|
|
|
goto frag_limit_reached;
|
|
|
|
|
|
2024-09-10 17:14:54 +00:00
|
|
|
netmem_ref netmem = (__force netmem_ref)__xa_erase(
|
|
|
|
|
&sk->sk_user_frags, tokens[i].token_start + j);
|
|
|
|
|
|
2024-11-07 21:03:30 +00:00
|
|
|
if (!netmem || WARN_ON_ONCE(!netmem_is_net_iov(netmem)))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
netmems[netmem_num++] = netmem;
|
|
|
|
|
if (netmem_num == ARRAY_SIZE(netmems)) {
|
|
|
|
|
xa_unlock_bh(&sk->sk_user_frags);
|
|
|
|
|
for (k = 0; k < netmem_num; k++)
|
|
|
|
|
WARN_ON_ONCE(!napi_pp_put_page(netmems[k]));
|
|
|
|
|
netmem_num = 0;
|
|
|
|
|
xa_lock_bh(&sk->sk_user_frags);
|
2024-09-10 17:14:54 +00:00
|
|
|
}
|
2024-11-07 21:03:30 +00:00
|
|
|
ret++;
|
2024-09-10 17:14:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 21:03:30 +00:00
|
|
|
frag_limit_reached:
|
2024-09-10 17:14:54 +00:00
|
|
|
xa_unlock_bh(&sk->sk_user_frags);
|
|
|
|
|
for (k = 0; k < netmem_num; k++)
|
|
|
|
|
WARN_ON_ONCE(!napi_pp_put_page(netmems[k]));
|
|
|
|
|
|
|
|
|
|
kvfree(tokens);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-08-16 23:17:17 -07:00
|
|
|
void sockopt_lock_sock(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
/* When current->bpf_ctx is set, the setsockopt is called from
|
|
|
|
|
* a bpf prog. bpf has ensured the sk lock has been
|
|
|
|
|
* acquired before calling setsockopt().
|
|
|
|
|
*/
|
|
|
|
|
if (has_current_bpf_ctx())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sockopt_lock_sock);
|
|
|
|
|
|
|
|
|
|
void sockopt_release_sock(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
if (has_current_bpf_ctx())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
release_sock(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sockopt_release_sock);
|
|
|
|
|
|
2022-08-16 23:17:23 -07:00
|
|
|
bool sockopt_ns_capable(struct user_namespace *ns, int cap)
|
|
|
|
|
{
|
|
|
|
|
return has_current_bpf_ctx() || ns_capable(ns, cap);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sockopt_ns_capable);
|
|
|
|
|
|
|
|
|
|
bool sockopt_capable(int cap)
|
|
|
|
|
{
|
|
|
|
|
return has_current_bpf_ctx() || capable(cap);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sockopt_capable);
|
|
|
|
|
|
2024-05-29 11:31:30 -07:00
|
|
|
static int sockopt_validate_clockid(__kernel_clockid_t value)
|
|
|
|
|
{
|
|
|
|
|
switch (value) {
|
|
|
|
|
case CLOCK_REALTIME:
|
|
|
|
|
case CLOCK_MONOTONIC:
|
|
|
|
|
case CLOCK_TAI:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* This is meant for all protocols to use and covers goings on
|
|
|
|
|
* at the socket level. Everything here is generic.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-08-16 23:18:04 -07:00
|
|
|
int sk_setsockopt(struct sock *sk, int level, int optname,
|
|
|
|
|
sockptr_t optval, unsigned int optlen)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2021-06-30 16:11:59 +08:00
|
|
|
struct so_timestamping timestamping;
|
2022-08-16 23:17:11 -07:00
|
|
|
struct socket *sock = sk->sk_socket;
|
2018-07-03 15:42:48 -07:00
|
|
|
struct sock_txtime sk_txtime;
|
2005-04-16 15:20:36 -07:00
|
|
|
int val;
|
|
|
|
|
int valbool;
|
|
|
|
|
struct linger ling;
|
|
|
|
|
int ret = 0;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Options without arguments
|
|
|
|
|
*/
|
|
|
|
|
|
2007-09-14 16:41:03 -07:00
|
|
|
if (optname == SO_BINDTODEVICE)
|
2012-11-26 05:21:08 +00:00
|
|
|
return sock_setbindtodevice(sk, optval, optlen);
|
2007-09-14 16:41:03 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
if (optlen < sizeof(int))
|
|
|
|
|
return -EINVAL;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2020-07-23 08:08:50 +02:00
|
|
|
if (copy_from_sockptr(&val, optval, sizeof(val)))
|
2005-04-16 15:20:36 -07:00
|
|
|
return -EFAULT;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2009-05-27 11:30:05 +00:00
|
|
|
valbool = val ? 1 : 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2023-09-21 20:28:11 +00:00
|
|
|
/* handle options which do not require locking the socket. */
|
|
|
|
|
switch (optname) {
|
|
|
|
|
case SO_PRIORITY:
|
2024-12-13 09:44:54 +01:00
|
|
|
if (sk_set_prio_allowed(sk, val)) {
|
2023-09-21 20:28:11 +00:00
|
|
|
sock_set_priority(sk, val);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -EPERM;
|
2023-09-21 20:28:13 +00:00
|
|
|
case SO_TYPE:
|
|
|
|
|
case SO_PROTOCOL:
|
|
|
|
|
case SO_DOMAIN:
|
|
|
|
|
case SO_ERROR:
|
|
|
|
|
return -ENOPROTOOPT;
|
2023-09-21 20:28:14 +00:00
|
|
|
#ifdef CONFIG_NET_RX_BUSY_POLL
|
|
|
|
|
case SO_BUSY_POLL:
|
|
|
|
|
if (val < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
WRITE_ONCE(sk->sk_ll_usec, val);
|
|
|
|
|
return 0;
|
|
|
|
|
case SO_PREFER_BUSY_POLL:
|
|
|
|
|
if (valbool && !sockopt_capable(CAP_NET_ADMIN))
|
|
|
|
|
return -EPERM;
|
|
|
|
|
WRITE_ONCE(sk->sk_prefer_busy_poll, valbool);
|
|
|
|
|
return 0;
|
|
|
|
|
case SO_BUSY_POLL_BUDGET:
|
|
|
|
|
if (val > READ_ONCE(sk->sk_busy_poll_budget) &&
|
|
|
|
|
!sockopt_capable(CAP_NET_ADMIN))
|
|
|
|
|
return -EPERM;
|
|
|
|
|
if (val < 0 || val > U16_MAX)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
WRITE_ONCE(sk->sk_busy_poll_budget, val);
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
2023-09-21 20:28:15 +00:00
|
|
|
case SO_MAX_PACING_RATE:
|
|
|
|
|
{
|
|
|
|
|
unsigned long ulval = (val == ~0U) ? ~0UL : (unsigned int)val;
|
|
|
|
|
unsigned long pacing_rate;
|
|
|
|
|
|
|
|
|
|
if (sizeof(ulval) != sizeof(val) &&
|
|
|
|
|
optlen >= sizeof(ulval) &&
|
|
|
|
|
copy_from_sockptr(&ulval, optval, sizeof(ulval))) {
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
|
|
|
|
if (ulval != ~0UL)
|
|
|
|
|
cmpxchg(&sk->sk_pacing_status,
|
|
|
|
|
SK_PACING_NONE,
|
|
|
|
|
SK_PACING_NEEDED);
|
|
|
|
|
/* Pairs with READ_ONCE() from sk_getsockopt() */
|
|
|
|
|
WRITE_ONCE(sk->sk_max_pacing_rate, ulval);
|
|
|
|
|
pacing_rate = READ_ONCE(sk->sk_pacing_rate);
|
|
|
|
|
if (ulval < pacing_rate)
|
|
|
|
|
WRITE_ONCE(sk->sk_pacing_rate, ulval);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-09-21 20:28:16 +00:00
|
|
|
case SO_TXREHASH:
|
2025-05-19 13:57:55 -07:00
|
|
|
if (!sk_is_tcp(sk))
|
|
|
|
|
return -EOPNOTSUPP;
|
2023-09-21 20:28:16 +00:00
|
|
|
if (val < -1 || val > 1)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if ((u8)val == SOCK_TXREHASH_DEFAULT)
|
|
|
|
|
val = READ_ONCE(sock_net(sk)->core.sysctl_txrehash);
|
|
|
|
|
/* Paired with READ_ONCE() in tcp_rtx_synack()
|
|
|
|
|
* and sk_getsockopt().
|
|
|
|
|
*/
|
|
|
|
|
WRITE_ONCE(sk->sk_txrehash, (u8)val);
|
|
|
|
|
return 0;
|
2024-02-19 14:12:20 +00:00
|
|
|
case SO_PEEK_OFF:
|
|
|
|
|
{
|
|
|
|
|
int (*set_peek_off)(struct sock *sk, int val);
|
|
|
|
|
|
|
|
|
|
set_peek_off = READ_ONCE(sock->ops)->set_peek_off;
|
|
|
|
|
if (set_peek_off)
|
|
|
|
|
ret = set_peek_off(sk, val);
|
|
|
|
|
else
|
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2024-09-10 17:14:54 +00:00
|
|
|
#ifdef CONFIG_PAGE_POOL
|
|
|
|
|
case SO_DEVMEM_DONTNEED:
|
|
|
|
|
return sock_devmem_dontneed(sk, optval, optlen);
|
|
|
|
|
#endif
|
2025-06-20 15:55:35 +00:00
|
|
|
case SO_SNDTIMEO_OLD:
|
|
|
|
|
case SO_SNDTIMEO_NEW:
|
|
|
|
|
return sock_set_timeout(&sk->sk_sndtimeo, optval,
|
|
|
|
|
optlen, optname == SO_SNDTIMEO_OLD);
|
2025-06-20 15:55:36 +00:00
|
|
|
case SO_RCVTIMEO_OLD:
|
|
|
|
|
case SO_RCVTIMEO_NEW:
|
|
|
|
|
return sock_set_timeout(&sk->sk_rcvtimeo, optval,
|
|
|
|
|
optlen, optname == SO_RCVTIMEO_OLD);
|
2023-09-21 20:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-16 23:17:17 -07:00
|
|
|
sockopt_lock_sock(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2009-05-27 11:30:05 +00:00
|
|
|
switch (optname) {
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_DEBUG:
|
2022-08-16 23:17:23 -07:00
|
|
|
if (val && !sockopt_capable(CAP_NET_ADMIN))
|
2007-04-10 20:10:33 -07:00
|
|
|
ret = -EACCES;
|
2009-05-27 11:30:05 +00:00
|
|
|
else
|
2007-11-15 03:03:19 -08:00
|
|
|
sock_valbool_flag(sk, SOCK_DBG, valbool);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
case SO_REUSEADDR:
|
2018-06-12 10:05:55 -07:00
|
|
|
sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2013-01-22 09:49:50 +00:00
|
|
|
case SO_REUSEPORT:
|
2024-12-31 16:05:27 +00:00
|
|
|
if (valbool && !sk_is_inet(sk))
|
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
|
else
|
|
|
|
|
sk->sk_reuseport = valbool;
|
2013-01-22 09:49:50 +00:00
|
|
|
break;
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_DONTROUTE:
|
2007-11-15 03:03:19 -08:00
|
|
|
sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
|
2018-12-05 18:56:28 -08:00
|
|
|
sk_dst_reset(sk);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
case SO_BROADCAST:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
|
|
|
|
|
break;
|
|
|
|
|
case SO_SNDBUF:
|
|
|
|
|
/* Don't error on this BSD doesn't and if you think
|
2012-04-26 20:07:59 +00:00
|
|
|
* about it this is right. Otherwise apps have to
|
|
|
|
|
* play 'guess the biggest size' games. RCVBUF/SNDBUF
|
|
|
|
|
* are treated in BSD as hints
|
|
|
|
|
*/
|
2022-08-23 10:46:44 -07:00
|
|
|
val = min_t(u32, val, READ_ONCE(sysctl_wmem_max));
|
2005-08-09 19:30:51 -07:00
|
|
|
set_sndbuf:
|
2019-02-13 04:30:34 +01:00
|
|
|
/* Ensure val * 2 fits into an int, to prevent max_t()
|
|
|
|
|
* from treating it as a negative value.
|
|
|
|
|
*/
|
|
|
|
|
val = min_t(int, val, INT_MAX / 2);
|
2007-04-10 20:10:33 -07:00
|
|
|
sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
|
2019-10-10 20:17:45 -07:00
|
|
|
WRITE_ONCE(sk->sk_sndbuf,
|
|
|
|
|
max_t(int, val * 2, SOCK_MIN_SNDBUF));
|
2012-04-26 20:07:59 +00:00
|
|
|
/* Wake up sending tasks if we upped the value. */
|
2007-04-10 20:10:33 -07:00
|
|
|
sk->sk_write_space(sk);
|
|
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_SNDBUFFORCE:
|
2022-08-16 23:17:23 -07:00
|
|
|
if (!sockopt_capable(CAP_NET_ADMIN)) {
|
2007-04-10 20:10:33 -07:00
|
|
|
ret = -EPERM;
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2019-02-13 04:30:34 +01:00
|
|
|
|
|
|
|
|
/* No negative values (to prevent underflow, as val will be
|
|
|
|
|
* multiplied by 2).
|
|
|
|
|
*/
|
|
|
|
|
if (val < 0)
|
|
|
|
|
val = 0;
|
2007-04-10 20:10:33 -07:00
|
|
|
goto set_sndbuf;
|
|
|
|
|
|
|
|
|
|
case SO_RCVBUF:
|
|
|
|
|
/* Don't error on this BSD doesn't and if you think
|
2012-04-26 20:07:59 +00:00
|
|
|
* about it this is right. Otherwise apps have to
|
|
|
|
|
* play 'guess the biggest size' games. RCVBUF/SNDBUF
|
|
|
|
|
* are treated in BSD as hints
|
|
|
|
|
*/
|
2022-08-23 10:46:44 -07:00
|
|
|
__sock_set_rcvbuf(sk, min_t(u32, val, READ_ONCE(sysctl_rmem_max)));
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_RCVBUFFORCE:
|
2022-08-16 23:17:23 -07:00
|
|
|
if (!sockopt_capable(CAP_NET_ADMIN)) {
|
2007-04-10 20:10:33 -07:00
|
|
|
ret = -EPERM;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-02-13 04:30:34 +01:00
|
|
|
|
|
|
|
|
/* No negative values (to prevent underflow, as val will be
|
|
|
|
|
* multiplied by 2).
|
|
|
|
|
*/
|
2020-05-28 07:12:16 +02:00
|
|
|
__sock_set_rcvbuf(sk, max(val, 0));
|
|
|
|
|
break;
|
2007-04-10 20:10:33 -07:00
|
|
|
|
|
|
|
|
case SO_KEEPALIVE:
|
2017-01-09 16:55:12 +01:00
|
|
|
if (sk->sk_prot->keepalive)
|
|
|
|
|
sk->sk_prot->keepalive(sk, valbool);
|
2007-04-10 20:10:33 -07:00
|
|
|
sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_OOBINLINE:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_NO_CHECK:
|
2014-05-23 08:47:19 -07:00
|
|
|
sk->sk_no_check_tx = valbool;
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_LINGER:
|
|
|
|
|
if (optlen < sizeof(ling)) {
|
|
|
|
|
ret = -EINVAL; /* 1003.1g */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-07-23 08:08:50 +02:00
|
|
|
if (copy_from_sockptr(&ling, optval, sizeof(ling))) {
|
2007-04-10 20:10:33 -07:00
|
|
|
ret = -EFAULT;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-08-19 04:06:46 +00:00
|
|
|
if (!ling.l_onoff) {
|
2007-04-10 20:10:33 -07:00
|
|
|
sock_reset_flag(sk, SOCK_LINGER);
|
2023-08-19 04:06:46 +00:00
|
|
|
} else {
|
|
|
|
|
unsigned long t_sec = ling.l_linger;
|
|
|
|
|
|
|
|
|
|
if (t_sec >= MAX_SCHEDULE_TIMEOUT / HZ)
|
|
|
|
|
WRITE_ONCE(sk->sk_lingertime, MAX_SCHEDULE_TIMEOUT);
|
2007-04-10 20:10:33 -07:00
|
|
|
else
|
2023-08-19 04:06:46 +00:00
|
|
|
WRITE_ONCE(sk->sk_lingertime, t_sec * HZ);
|
2007-04-10 20:10:33 -07:00
|
|
|
sock_set_flag(sk, SOCK_LINGER);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_BSDCOMPAT:
|
|
|
|
|
break;
|
|
|
|
|
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMP_OLD:
|
2019-02-02 07:34:50 -08:00
|
|
|
case SO_TIMESTAMP_NEW:
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMPNS_OLD:
|
2019-02-02 07:34:50 -08:00
|
|
|
case SO_TIMESTAMPNS_NEW:
|
2021-07-02 07:41:01 -07:00
|
|
|
sock_set_timestamp(sk, optname, valbool);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2021-06-03 16:24:28 -07:00
|
|
|
|
2019-02-02 07:34:51 -08:00
|
|
|
case SO_TIMESTAMPING_NEW:
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMPING_OLD:
|
2021-06-30 16:11:59 +08:00
|
|
|
if (optlen == sizeof(timestamping)) {
|
|
|
|
|
if (copy_from_sockptr(×tamping, optval,
|
2021-07-07 13:01:00 +03:00
|
|
|
sizeof(timestamping))) {
|
|
|
|
|
ret = -EFAULT;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-06-30 16:11:59 +08:00
|
|
|
} else {
|
|
|
|
|
memset(×tamping, 0, sizeof(timestamping));
|
|
|
|
|
timestamping.flags = val;
|
|
|
|
|
}
|
|
|
|
|
ret = sock_set_timestamping(sk, optname, timestamping);
|
2009-02-12 05:03:38 +00:00
|
|
|
break;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_RCVLOWAT:
|
2023-08-08 13:58:09 +00:00
|
|
|
{
|
|
|
|
|
int (*set_rcvlowat)(struct sock *sk, int val) = NULL;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
if (val < 0)
|
|
|
|
|
val = INT_MAX;
|
2023-08-08 13:58:09 +00:00
|
|
|
if (sock)
|
|
|
|
|
set_rcvlowat = READ_ONCE(sock->ops)->set_rcvlowat;
|
|
|
|
|
if (set_rcvlowat)
|
|
|
|
|
ret = set_rcvlowat(sk, val);
|
2018-04-16 10:33:35 -07:00
|
|
|
else
|
2019-10-09 15:32:35 -07:00
|
|
|
WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2023-08-08 13:58:09 +00:00
|
|
|
}
|
2020-07-17 08:23:13 +02:00
|
|
|
case SO_ATTACH_FILTER: {
|
|
|
|
|
struct sock_fprog fprog;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2020-07-23 08:08:50 +02:00
|
|
|
ret = copy_bpf_fprog_from_user(&fprog, optval, optlen);
|
2020-07-17 08:23:13 +02:00
|
|
|
if (!ret)
|
2007-04-10 20:10:33 -07:00
|
|
|
ret = sk_attach_filter(&fprog, sk);
|
|
|
|
|
break;
|
2020-07-17 08:23:13 +02:00
|
|
|
}
|
2014-12-01 15:06:35 -08:00
|
|
|
case SO_ATTACH_BPF:
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
if (optlen == sizeof(u32)) {
|
|
|
|
|
u32 ufd;
|
|
|
|
|
|
|
|
|
|
ret = -EFAULT;
|
2020-07-23 08:08:50 +02:00
|
|
|
if (copy_from_sockptr(&ufd, optval, sizeof(ufd)))
|
2014-12-01 15:06:35 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
ret = sk_attach_bpf(ufd, sk);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2020-07-17 08:23:13 +02:00
|
|
|
case SO_ATTACH_REUSEPORT_CBPF: {
|
|
|
|
|
struct sock_fprog fprog;
|
2016-01-04 17:41:47 -05:00
|
|
|
|
2020-07-23 08:08:50 +02:00
|
|
|
ret = copy_bpf_fprog_from_user(&fprog, optval, optlen);
|
2020-07-17 08:23:13 +02:00
|
|
|
if (!ret)
|
2016-01-04 17:41:47 -05:00
|
|
|
ret = sk_reuseport_attach_filter(&fprog, sk);
|
|
|
|
|
break;
|
2020-07-17 08:23:13 +02:00
|
|
|
}
|
2016-01-04 17:41:47 -05:00
|
|
|
case SO_ATTACH_REUSEPORT_EBPF:
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
if (optlen == sizeof(u32)) {
|
|
|
|
|
u32 ufd;
|
|
|
|
|
|
|
|
|
|
ret = -EFAULT;
|
2020-07-23 08:08:50 +02:00
|
|
|
if (copy_from_sockptr(&ufd, optval, sizeof(ufd)))
|
2016-01-04 17:41:47 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
ret = sk_reuseport_attach_bpf(ufd, sk);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2019-06-13 15:00:01 -07:00
|
|
|
case SO_DETACH_REUSEPORT_BPF:
|
|
|
|
|
ret = reuseport_detach_prog(sk);
|
|
|
|
|
break;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_DETACH_FILTER:
|
2007-10-17 21:21:26 -07:00
|
|
|
ret = sk_detach_filter(sk);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
|
2013-01-16 22:55:49 +01:00
|
|
|
case SO_LOCK_FILTER:
|
|
|
|
|
if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
|
|
|
|
|
ret = -EPERM;
|
|
|
|
|
else
|
|
|
|
|
sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
2008-01-30 19:08:16 -08:00
|
|
|
case SO_MARK:
|
2022-08-16 23:17:23 -07:00
|
|
|
if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
|
|
|
|
|
!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
|
2008-01-30 19:08:16 -08:00
|
|
|
ret = -EPERM;
|
2021-05-31 17:00:30 -04:00
|
|
|
break;
|
2018-11-08 08:13:35 -06:00
|
|
|
}
|
2021-05-31 17:00:30 -04:00
|
|
|
|
|
|
|
|
__sock_set_mark(sk, val);
|
2008-01-30 19:08:16 -08:00
|
|
|
break;
|
2022-04-27 16:02:37 -04:00
|
|
|
case SO_RCVMARK:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_RCVMARK, valbool);
|
|
|
|
|
break;
|
2006-06-29 12:27:47 -07:00
|
|
|
|
2024-12-13 09:44:57 +01:00
|
|
|
case SO_RCVPRIORITY:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_RCVPRIORITY, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
2009-10-12 13:26:31 -07:00
|
|
|
case SO_RXQ_OVFL:
|
2011-10-07 03:30:20 +00:00
|
|
|
sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
|
2009-10-12 13:26:31 -07:00
|
|
|
break;
|
2011-11-09 10:15:42 +01:00
|
|
|
|
|
|
|
|
case SO_WIFI_STATUS:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
2012-02-11 15:39:30 +00:00
|
|
|
case SO_NOFCS:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_NOFCS, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
2013-03-28 11:19:25 +00:00
|
|
|
case SO_SELECT_ERR_QUEUE:
|
|
|
|
|
sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
|
|
|
|
|
break;
|
|
|
|
|
|
2025-05-19 13:57:57 -07:00
|
|
|
case SO_PASSCRED:
|
|
|
|
|
if (sk_may_scm_recv(sk))
|
|
|
|
|
sk->sk_scm_credentials = valbool;
|
|
|
|
|
else
|
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_PASSSEC:
|
|
|
|
|
if (IS_ENABLED(CONFIG_SECURITY_NETWORK) && sk_may_scm_recv(sk))
|
|
|
|
|
sk->sk_scm_security = valbool;
|
|
|
|
|
else
|
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_PASSPIDFD:
|
|
|
|
|
if (sk_is_unix(sk))
|
|
|
|
|
sk->sk_scm_pidfd = valbool;
|
|
|
|
|
else
|
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
|
break;
|
2013-09-24 08:20:52 -07:00
|
|
|
|
2025-05-19 13:57:59 -07:00
|
|
|
case SO_PASSRIGHTS:
|
|
|
|
|
if (sk_is_unix(sk))
|
|
|
|
|
sk->sk_scm_rights = valbool;
|
|
|
|
|
else
|
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
|
break;
|
2013-09-24 08:20:52 -07:00
|
|
|
|
2015-10-08 19:33:21 -07:00
|
|
|
case SO_INCOMING_CPU:
|
2022-10-21 13:44:34 -07:00
|
|
|
reuseport_update_incoming_cpu(sk, val);
|
2015-10-08 19:33:21 -07:00
|
|
|
break;
|
|
|
|
|
|
2016-02-24 10:02:52 -08:00
|
|
|
case SO_CNX_ADVICE:
|
|
|
|
|
if (val == 1)
|
|
|
|
|
dst_negative_advice(sk);
|
|
|
|
|
break;
|
2017-08-03 16:29:40 -04:00
|
|
|
|
|
|
|
|
case SO_ZEROCOPY:
|
2018-02-15 10:49:34 -08:00
|
|
|
if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6) {
|
2021-11-15 11:02:33 -08:00
|
|
|
if (!(sk_is_tcp(sk) ||
|
2018-11-30 15:32:39 -05:00
|
|
|
(sk->sk_type == SOCK_DGRAM &&
|
|
|
|
|
sk->sk_protocol == IPPROTO_UDP)))
|
2022-03-07 23:31:26 +01:00
|
|
|
ret = -EOPNOTSUPP;
|
2018-02-15 10:49:34 -08:00
|
|
|
} else if (sk->sk_family != PF_RDS) {
|
2022-03-07 23:31:26 +01:00
|
|
|
ret = -EOPNOTSUPP;
|
2018-02-15 10:49:34 -08:00
|
|
|
}
|
|
|
|
|
if (!ret) {
|
|
|
|
|
if (val < 0 || val > 1)
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
else
|
|
|
|
|
sock_valbool_flag(sk, SOCK_ZEROCOPY, valbool);
|
|
|
|
|
}
|
2018-03-07 09:40:57 -08:00
|
|
|
break;
|
|
|
|
|
|
2018-07-03 15:42:48 -07:00
|
|
|
case SO_TXTIME:
|
2020-05-07 10:05:39 -07:00
|
|
|
if (optlen != sizeof(struct sock_txtime)) {
|
2018-07-03 15:42:48 -07:00
|
|
|
ret = -EINVAL;
|
2020-05-07 10:05:39 -07:00
|
|
|
break;
|
2020-07-23 08:08:50 +02:00
|
|
|
} else if (copy_from_sockptr(&sk_txtime, optval,
|
2018-07-03 15:42:48 -07:00
|
|
|
sizeof(struct sock_txtime))) {
|
|
|
|
|
ret = -EFAULT;
|
2020-05-07 10:05:39 -07:00
|
|
|
break;
|
2018-07-03 15:42:48 -07:00
|
|
|
} else if (sk_txtime.flags & ~SOF_TXTIME_FLAGS_MASK) {
|
|
|
|
|
ret = -EINVAL;
|
2020-05-07 10:05:39 -07:00
|
|
|
break;
|
2018-07-03 15:42:48 -07:00
|
|
|
}
|
2020-05-07 10:05:39 -07:00
|
|
|
/* CLOCK_MONOTONIC is only used by sch_fq, and this packet
|
|
|
|
|
* scheduler has enough safe guards.
|
|
|
|
|
*/
|
|
|
|
|
if (sk_txtime.clockid != CLOCK_MONOTONIC &&
|
2022-08-16 23:17:23 -07:00
|
|
|
!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
|
2020-05-07 10:05:39 -07:00
|
|
|
ret = -EPERM;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-05-29 11:31:30 -07:00
|
|
|
|
|
|
|
|
ret = sockopt_validate_clockid(sk_txtime.clockid);
|
|
|
|
|
if (ret)
|
|
|
|
|
break;
|
|
|
|
|
|
2020-05-07 10:05:39 -07:00
|
|
|
sock_valbool_flag(sk, SOCK_TXTIME, true);
|
|
|
|
|
sk->sk_clockid = sk_txtime.clockid;
|
|
|
|
|
sk->sk_txtime_deadline_mode =
|
|
|
|
|
!!(sk_txtime.flags & SOF_TXTIME_DEADLINE_MODE);
|
|
|
|
|
sk->sk_txtime_report_errors =
|
|
|
|
|
!!(sk_txtime.flags & SOF_TXTIME_REPORT_ERRORS);
|
2018-07-03 15:42:48 -07:00
|
|
|
break;
|
|
|
|
|
|
2019-01-15 14:42:14 +01:00
|
|
|
case SO_BINDTOIFINDEX:
|
2020-05-28 07:12:13 +02:00
|
|
|
ret = sock_bindtoindex_locked(sk, val);
|
2019-01-15 14:42:14 +01:00
|
|
|
break;
|
|
|
|
|
|
2021-08-04 10:55:56 +03:00
|
|
|
case SO_BUF_LOCK:
|
|
|
|
|
if (val & ~SOCK_BUF_LOCK_MASK) {
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
sk->sk_userlocks = val | (sk->sk_userlocks &
|
|
|
|
|
~SOCK_BUF_LOCK_MASK);
|
|
|
|
|
break;
|
|
|
|
|
|
2021-09-29 10:25:11 -07:00
|
|
|
case SO_RESERVE_MEM:
|
|
|
|
|
{
|
|
|
|
|
int delta;
|
|
|
|
|
|
|
|
|
|
if (val < 0) {
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delta = val - sk->sk_reserved_mem;
|
|
|
|
|
if (delta < 0)
|
|
|
|
|
sock_release_reserved_memory(sk, -delta);
|
|
|
|
|
else
|
|
|
|
|
ret = sock_reserve_memory(sk, delta);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
default:
|
|
|
|
|
ret = -ENOPROTOOPT;
|
|
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
}
|
2022-08-16 23:17:17 -07:00
|
|
|
sockopt_release_sock(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
return ret;
|
|
|
|
|
}
|
2022-08-16 23:17:11 -07:00
|
|
|
|
|
|
|
|
int sock_setsockopt(struct socket *sock, int level, int optname,
|
|
|
|
|
sockptr_t optval, unsigned int optlen)
|
|
|
|
|
{
|
|
|
|
|
return sk_setsockopt(sock->sk, level, optname,
|
|
|
|
|
optval, optlen);
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_setsockopt);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2021-09-29 15:57:50 -07:00
|
|
|
static const struct cred *sk_get_peer_cred(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
const struct cred *cred;
|
|
|
|
|
|
|
|
|
|
spin_lock(&sk->sk_peer_lock);
|
|
|
|
|
cred = get_cred(sk->sk_peer_cred);
|
|
|
|
|
spin_unlock(&sk->sk_peer_lock);
|
|
|
|
|
|
|
|
|
|
return cred;
|
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2014-01-03 09:17:14 -08:00
|
|
|
static void cred_to_ucred(struct pid *pid, const struct cred *cred,
|
|
|
|
|
struct ucred *ucred)
|
2010-06-13 03:28:59 +00:00
|
|
|
{
|
|
|
|
|
ucred->pid = pid_vnr(pid);
|
|
|
|
|
ucred->uid = ucred->gid = -1;
|
|
|
|
|
if (cred) {
|
|
|
|
|
struct user_namespace *current_ns = current_user_ns();
|
|
|
|
|
|
2012-05-23 16:39:45 -06:00
|
|
|
ucred->uid = from_kuid_munged(current_ns, cred->euid);
|
|
|
|
|
ucred->gid = from_kgid_munged(current_ns, cred->egid);
|
2010-06-13 03:28:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 17:28:02 -07:00
|
|
|
static int groups_to_user(sockptr_t dst, const struct group_info *src)
|
2017-06-21 10:47:15 +02:00
|
|
|
{
|
|
|
|
|
struct user_namespace *user_ns = current_user_ns();
|
|
|
|
|
int i;
|
|
|
|
|
|
2022-09-01 17:28:02 -07:00
|
|
|
for (i = 0; i < src->ngroups; i++) {
|
|
|
|
|
gid_t gid = from_kgid_munged(user_ns, src->gid[i]);
|
|
|
|
|
|
|
|
|
|
if (copy_to_sockptr_offset(dst, i * sizeof(gid), &gid, sizeof(gid)))
|
2017-06-21 10:47:15 +02:00
|
|
|
return -EFAULT;
|
2022-09-01 17:28:02 -07:00
|
|
|
}
|
2017-06-21 10:47:15 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 17:29:12 -07:00
|
|
|
int sk_getsockopt(struct sock *sk, int level, int optname,
|
|
|
|
|
sockptr_t optval, sockptr_t optlen)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2022-09-01 17:27:56 -07:00
|
|
|
struct socket *sock = sk->sk_socket;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
union {
|
2007-02-09 23:24:36 +09:00
|
|
|
int val;
|
2017-04-05 19:00:55 -07:00
|
|
|
u64 val64;
|
2019-02-28 15:17:28 -08:00
|
|
|
unsigned long ulval;
|
2007-02-09 23:24:36 +09:00
|
|
|
struct linger ling;
|
2019-02-02 07:34:44 -08:00
|
|
|
struct old_timeval32 tm32;
|
|
|
|
|
struct __kernel_old_timeval tm;
|
2019-02-02 07:34:54 -08:00
|
|
|
struct __kernel_sock_timeval stm;
|
2018-07-03 15:42:48 -07:00
|
|
|
struct sock_txtime txtime;
|
2021-06-30 16:11:59 +08:00
|
|
|
struct so_timestamping timestamping;
|
2005-04-16 15:20:36 -07:00
|
|
|
} v;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2010-01-15 01:08:58 -08:00
|
|
|
int lv = sizeof(int);
|
2005-04-16 15:20:36 -07:00
|
|
|
int len;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_from_sockptr(&len, optlen, sizeof(int)))
|
2007-02-09 23:24:36 +09:00
|
|
|
return -EFAULT;
|
2007-04-10 20:10:33 -07:00
|
|
|
if (len < 0)
|
2005-04-16 15:20:36 -07:00
|
|
|
return -EINVAL;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2009-02-23 15:38:41 -08:00
|
|
|
memset(&v, 0, sizeof(v));
|
2009-02-12 16:59:09 -08:00
|
|
|
|
2009-05-27 11:30:05 +00:00
|
|
|
switch (optname) {
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_DEBUG:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_DBG);
|
|
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_DONTROUTE:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_LOCALROUTE);
|
|
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_BROADCAST:
|
2012-05-16 05:57:07 +00:00
|
|
|
v.val = sock_flag(sk, SOCK_BROADCAST);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_SNDBUF:
|
2023-07-28 15:03:13 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_sndbuf);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_RCVBUF:
|
2023-07-28 15:03:14 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_rcvbuf);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_REUSEADDR:
|
|
|
|
|
v.val = sk->sk_reuse;
|
|
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-01-22 09:49:50 +00:00
|
|
|
case SO_REUSEPORT:
|
|
|
|
|
v.val = sk->sk_reuseport;
|
|
|
|
|
break;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_KEEPALIVE:
|
2012-05-16 05:57:07 +00:00
|
|
|
v.val = sock_flag(sk, SOCK_KEEPOPEN);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_TYPE:
|
|
|
|
|
v.val = sk->sk_type;
|
|
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2009-08-04 07:28:28 +00:00
|
|
|
case SO_PROTOCOL:
|
|
|
|
|
v.val = sk->sk_protocol;
|
|
|
|
|
break;
|
|
|
|
|
|
2009-08-04 07:28:29 +00:00
|
|
|
case SO_DOMAIN:
|
|
|
|
|
v.val = sk->sk_family;
|
|
|
|
|
break;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_ERROR:
|
|
|
|
|
v.val = -sock_error(sk);
|
2009-05-27 11:30:05 +00:00
|
|
|
if (v.val == 0)
|
2007-04-10 20:10:33 -07:00
|
|
|
v.val = xchg(&sk->sk_err_soft, 0);
|
|
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_OOBINLINE:
|
2012-05-16 05:57:07 +00:00
|
|
|
v.val = sock_flag(sk, SOCK_URGINLINE);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_NO_CHECK:
|
2014-05-23 08:47:19 -07:00
|
|
|
v.val = sk->sk_no_check_tx;
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_PRIORITY:
|
2023-07-28 15:03:18 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_priority);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_LINGER:
|
|
|
|
|
lv = sizeof(v.ling);
|
2012-05-16 05:57:07 +00:00
|
|
|
v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
|
2023-08-19 04:06:46 +00:00
|
|
|
v.ling.l_linger = READ_ONCE(sk->sk_lingertime) / HZ;
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_BSDCOMPAT:
|
|
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMP_OLD:
|
2007-03-25 22:14:49 -07:00
|
|
|
v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
|
2019-02-02 07:34:50 -08:00
|
|
|
!sock_flag(sk, SOCK_TSTAMP_NEW) &&
|
2007-03-25 22:14:49 -07:00
|
|
|
!sock_flag(sk, SOCK_RCVTSTAMPNS);
|
|
|
|
|
break;
|
|
|
|
|
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMPNS_OLD:
|
2019-02-02 07:34:50 -08:00
|
|
|
v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && !sock_flag(sk, SOCK_TSTAMP_NEW);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_TIMESTAMP_NEW:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_RCVTSTAMP) && sock_flag(sk, SOCK_TSTAMP_NEW);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_TIMESTAMPNS_NEW:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && sock_flag(sk, SOCK_TSTAMP_NEW);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMPING_OLD:
|
2023-12-22 00:19:01 +01:00
|
|
|
case SO_TIMESTAMPING_NEW:
|
2021-06-30 16:11:59 +08:00
|
|
|
lv = sizeof(v.timestamping);
|
2023-12-22 00:19:01 +01:00
|
|
|
/* For the later-added case SO_TIMESTAMPING_NEW: Be strict about only
|
|
|
|
|
* returning the flags when they were set through the same option.
|
|
|
|
|
* Don't change the beviour for the old case SO_TIMESTAMPING_OLD.
|
|
|
|
|
*/
|
|
|
|
|
if (optname == SO_TIMESTAMPING_OLD || sock_flag(sk, SOCK_TSTAMP_NEW)) {
|
|
|
|
|
v.timestamping.flags = READ_ONCE(sk->sk_tsflags);
|
|
|
|
|
v.timestamping.bind_phc = READ_ONCE(sk->sk_bind_phc);
|
|
|
|
|
}
|
2009-02-12 05:03:38 +00:00
|
|
|
break;
|
|
|
|
|
|
2019-02-02 07:34:54 -08:00
|
|
|
case SO_RCVTIMEO_OLD:
|
|
|
|
|
case SO_RCVTIMEO_NEW:
|
2023-07-28 15:03:12 +00:00
|
|
|
lv = sock_get_timeout(READ_ONCE(sk->sk_rcvtimeo), &v,
|
|
|
|
|
SO_RCVTIMEO_OLD == optname);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2019-02-02 07:34:54 -08:00
|
|
|
case SO_SNDTIMEO_OLD:
|
|
|
|
|
case SO_SNDTIMEO_NEW:
|
2023-07-28 15:03:12 +00:00
|
|
|
lv = sock_get_timeout(READ_ONCE(sk->sk_sndtimeo), &v,
|
|
|
|
|
SO_SNDTIMEO_OLD == optname);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_RCVLOWAT:
|
2023-07-28 15:03:11 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_rcvlowat);
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2006-06-29 12:27:47 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_SNDLOWAT:
|
2009-05-27 11:30:05 +00:00
|
|
|
v.val = 1;
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_PASSCRED:
|
2025-05-19 13:57:56 -07:00
|
|
|
if (!sk_may_scm_recv(sk))
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
2025-05-19 13:57:57 -07:00
|
|
|
v.val = sk->sk_scm_credentials;
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
|
2023-06-08 22:26:25 +02:00
|
|
|
case SO_PASSPIDFD:
|
2025-05-19 13:57:56 -07:00
|
|
|
if (!sk_is_unix(sk))
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
2025-05-19 13:57:57 -07:00
|
|
|
v.val = sk->sk_scm_pidfd;
|
2023-06-08 22:26:25 +02:00
|
|
|
break;
|
|
|
|
|
|
2025-05-19 13:57:59 -07:00
|
|
|
case SO_PASSRIGHTS:
|
|
|
|
|
if (!sk_is_unix(sk))
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
|
|
v.val = sk->sk_scm_rights;
|
2023-06-08 22:26:25 +02:00
|
|
|
break;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_PEERCRED:
|
2010-06-13 03:30:14 +00:00
|
|
|
{
|
|
|
|
|
struct ucred peercred;
|
|
|
|
|
if (len > sizeof(peercred))
|
|
|
|
|
len = sizeof(peercred);
|
2021-09-29 15:57:50 -07:00
|
|
|
|
|
|
|
|
spin_lock(&sk->sk_peer_lock);
|
2010-06-13 03:30:14 +00:00
|
|
|
cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
|
2021-09-29 15:57:50 -07:00
|
|
|
spin_unlock(&sk->sk_peer_lock);
|
|
|
|
|
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_to_sockptr(optval, &peercred, len))
|
2007-04-10 20:10:33 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
goto lenout;
|
2010-06-13 03:30:14 +00:00
|
|
|
}
|
2007-04-10 20:10:33 -07:00
|
|
|
|
2023-06-08 22:26:26 +02:00
|
|
|
case SO_PEERPIDFD:
|
|
|
|
|
{
|
|
|
|
|
struct pid *peer_pid;
|
|
|
|
|
struct file *pidfd_file = NULL;
|
2025-04-25 10:11:33 +02:00
|
|
|
unsigned int flags = 0;
|
2023-06-08 22:26:26 +02:00
|
|
|
int pidfd;
|
|
|
|
|
|
|
|
|
|
if (len > sizeof(pidfd))
|
|
|
|
|
len = sizeof(pidfd);
|
|
|
|
|
|
|
|
|
|
spin_lock(&sk->sk_peer_lock);
|
|
|
|
|
peer_pid = get_pid(sk->sk_peer_pid);
|
|
|
|
|
spin_unlock(&sk->sk_peer_lock);
|
|
|
|
|
|
|
|
|
|
if (!peer_pid)
|
2023-08-07 10:12:25 +02:00
|
|
|
return -ENODATA;
|
2023-06-08 22:26:26 +02:00
|
|
|
|
2025-04-25 10:11:33 +02:00
|
|
|
/* The use of PIDFD_STALE requires stashing of struct pid
|
|
|
|
|
* on pidfs with pidfs_register_pid() and only AF_UNIX
|
|
|
|
|
* were prepared for this.
|
|
|
|
|
*/
|
|
|
|
|
if (sk->sk_family == AF_UNIX)
|
|
|
|
|
flags = PIDFD_STALE;
|
|
|
|
|
|
|
|
|
|
pidfd = pidfd_prepare(peer_pid, flags, &pidfd_file);
|
2023-06-08 22:26:26 +02:00
|
|
|
put_pid(peer_pid);
|
|
|
|
|
if (pidfd < 0)
|
|
|
|
|
return pidfd;
|
|
|
|
|
|
|
|
|
|
if (copy_to_sockptr(optval, &pidfd, len) ||
|
|
|
|
|
copy_to_sockptr(optlen, &len, sizeof(int))) {
|
|
|
|
|
put_unused_fd(pidfd);
|
|
|
|
|
fput(pidfd_file);
|
|
|
|
|
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd_install(pidfd, pidfd_file);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-21 10:47:15 +02:00
|
|
|
case SO_PEERGROUPS:
|
|
|
|
|
{
|
2021-09-29 15:57:50 -07:00
|
|
|
const struct cred *cred;
|
2017-06-21 10:47:15 +02:00
|
|
|
int ret, n;
|
|
|
|
|
|
2021-09-29 15:57:50 -07:00
|
|
|
cred = sk_get_peer_cred(sk);
|
|
|
|
|
if (!cred)
|
2017-06-21 10:47:15 +02:00
|
|
|
return -ENODATA;
|
|
|
|
|
|
2021-09-29 15:57:50 -07:00
|
|
|
n = cred->group_info->ngroups;
|
2017-06-21 10:47:15 +02:00
|
|
|
if (len < n * sizeof(gid_t)) {
|
|
|
|
|
len = n * sizeof(gid_t);
|
2021-09-29 15:57:50 -07:00
|
|
|
put_cred(cred);
|
2022-09-01 17:28:02 -07:00
|
|
|
return copy_to_sockptr(optlen, &len, sizeof(int)) ? -EFAULT : -ERANGE;
|
2017-06-21 10:47:15 +02:00
|
|
|
}
|
|
|
|
|
len = n * sizeof(gid_t);
|
|
|
|
|
|
2022-09-01 17:28:02 -07:00
|
|
|
ret = groups_to_user(optval, cred->group_info);
|
2021-09-29 15:57:50 -07:00
|
|
|
put_cred(cred);
|
2017-06-21 10:47:15 +02:00
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
goto lenout;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
case SO_PEERNAME:
|
|
|
|
|
{
|
2023-07-28 17:48:13 -07:00
|
|
|
struct sockaddr_storage address;
|
2007-04-10 20:10:33 -07:00
|
|
|
|
2023-08-08 13:58:09 +00:00
|
|
|
lv = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 2);
|
2018-02-12 20:00:20 +01:00
|
|
|
if (lv < 0)
|
2007-04-10 20:10:33 -07:00
|
|
|
return -ENOTCONN;
|
|
|
|
|
if (lv < len)
|
|
|
|
|
return -EINVAL;
|
2023-07-28 17:48:13 -07:00
|
|
|
if (copy_to_sockptr(optval, &address, len))
|
2007-04-10 20:10:33 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
goto lenout;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2007-04-10 20:10:33 -07:00
|
|
|
|
|
|
|
|
/* Dubious BSD thing... Probably nobody even uses it, but
|
|
|
|
|
* the UNIX standard wants it for whatever reason... -DaveM
|
|
|
|
|
*/
|
|
|
|
|
case SO_ACCEPTCONN:
|
|
|
|
|
v.val = sk->sk_state == TCP_LISTEN;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_PASSSEC:
|
2025-05-19 13:57:56 -07:00
|
|
|
if (!IS_ENABLED(CONFIG_SECURITY_NETWORK) || !sk_may_scm_recv(sk))
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
2025-05-19 13:57:57 -07:00
|
|
|
v.val = sk->sk_scm_security;
|
2007-04-10 20:10:33 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SO_PEERSEC:
|
2022-10-10 12:31:21 -04:00
|
|
|
return security_socket_getpeersec_stream(sock,
|
|
|
|
|
optval, optlen, len);
|
2007-04-10 20:10:33 -07:00
|
|
|
|
2008-01-30 19:08:16 -08:00
|
|
|
case SO_MARK:
|
2023-07-28 15:03:15 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_mark);
|
2008-01-30 19:08:16 -08:00
|
|
|
break;
|
|
|
|
|
|
2022-04-27 16:02:37 -04:00
|
|
|
case SO_RCVMARK:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_RCVMARK);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-12-13 09:44:57 +01:00
|
|
|
case SO_RCVPRIORITY:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_RCVPRIORITY);
|
|
|
|
|
break;
|
|
|
|
|
|
2009-10-12 13:26:31 -07:00
|
|
|
case SO_RXQ_OVFL:
|
2012-05-16 05:57:07 +00:00
|
|
|
v.val = sock_flag(sk, SOCK_RXQ_OVFL);
|
2009-10-12 13:26:31 -07:00
|
|
|
break;
|
|
|
|
|
|
2011-11-09 10:15:42 +01:00
|
|
|
case SO_WIFI_STATUS:
|
2012-05-16 05:57:07 +00:00
|
|
|
v.val = sock_flag(sk, SOCK_WIFI_STATUS);
|
2011-11-09 10:15:42 +01:00
|
|
|
break;
|
|
|
|
|
|
2012-02-21 07:31:34 +00:00
|
|
|
case SO_PEEK_OFF:
|
2023-08-08 13:58:09 +00:00
|
|
|
if (!READ_ONCE(sock->ops)->set_peek_off)
|
2012-02-21 07:31:34 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
2023-07-28 15:03:16 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_peek_off);
|
2012-02-21 07:31:34 +00:00
|
|
|
break;
|
2012-02-24 14:48:34 -05:00
|
|
|
case SO_NOFCS:
|
2012-05-16 05:57:07 +00:00
|
|
|
v.val = sock_flag(sk, SOCK_NOFCS);
|
2012-02-24 14:48:34 -05:00
|
|
|
break;
|
2012-11-26 05:21:08 +00:00
|
|
|
|
2012-10-18 23:55:56 +00:00
|
|
|
case SO_BINDTODEVICE:
|
2012-11-26 05:21:08 +00:00
|
|
|
return sock_getbindtodevice(sk, optval, optlen, len);
|
|
|
|
|
|
2012-11-01 02:01:48 +00:00
|
|
|
case SO_GET_FILTER:
|
2022-09-01 17:28:02 -07:00
|
|
|
len = sk_get_filter(sk, optval, len);
|
2012-11-01 02:01:48 +00:00
|
|
|
if (len < 0)
|
|
|
|
|
return len;
|
|
|
|
|
|
|
|
|
|
goto lenout;
|
2012-11-26 05:21:08 +00:00
|
|
|
|
2013-01-16 22:55:49 +01:00
|
|
|
case SO_LOCK_FILTER:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
|
|
|
|
|
break;
|
|
|
|
|
|
2014-01-17 17:09:45 +01:00
|
|
|
case SO_BPF_EXTENSIONS:
|
|
|
|
|
v.val = bpf_tell_extensions();
|
|
|
|
|
break;
|
|
|
|
|
|
2013-03-28 11:19:25 +00:00
|
|
|
case SO_SELECT_ERR_QUEUE:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
|
|
|
|
|
break;
|
|
|
|
|
|
2013-08-01 11:10:25 +08:00
|
|
|
#ifdef CONFIG_NET_RX_BUSY_POLL
|
2013-07-10 17:13:36 +03:00
|
|
|
case SO_BUSY_POLL:
|
2023-07-28 15:03:17 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_ll_usec);
|
2013-06-14 16:33:57 +03:00
|
|
|
break;
|
2020-11-30 19:51:56 +01:00
|
|
|
case SO_PREFER_BUSY_POLL:
|
|
|
|
|
v.val = READ_ONCE(sk->sk_prefer_busy_poll);
|
|
|
|
|
break;
|
2013-06-14 16:33:57 +03:00
|
|
|
#endif
|
|
|
|
|
|
2013-09-24 08:20:52 -07:00
|
|
|
case SO_MAX_PACING_RATE:
|
2023-07-28 15:03:10 +00:00
|
|
|
/* The READ_ONCE() pair with the WRITE_ONCE() in sk_setsockopt() */
|
2019-02-28 15:17:28 -08:00
|
|
|
if (sizeof(v.ulval) != sizeof(v.val) && len >= sizeof(v.ulval)) {
|
|
|
|
|
lv = sizeof(v.ulval);
|
2023-07-28 15:03:10 +00:00
|
|
|
v.ulval = READ_ONCE(sk->sk_max_pacing_rate);
|
2019-02-28 15:17:28 -08:00
|
|
|
} else {
|
|
|
|
|
/* 32bit version */
|
2023-07-28 15:03:10 +00:00
|
|
|
v.val = min_t(unsigned long, ~0U,
|
|
|
|
|
READ_ONCE(sk->sk_max_pacing_rate));
|
2019-02-28 15:17:28 -08:00
|
|
|
}
|
2013-09-24 08:20:52 -07:00
|
|
|
break;
|
|
|
|
|
|
2014-11-11 05:54:28 -08:00
|
|
|
case SO_INCOMING_CPU:
|
2019-10-30 13:00:04 -07:00
|
|
|
v.val = READ_ONCE(sk->sk_incoming_cpu);
|
2014-11-11 05:54:28 -08:00
|
|
|
break;
|
|
|
|
|
|
2017-03-20 15:22:03 -04:00
|
|
|
case SO_MEMINFO:
|
|
|
|
|
{
|
|
|
|
|
u32 meminfo[SK_MEMINFO_VARS];
|
|
|
|
|
|
|
|
|
|
sk_get_meminfo(sk, meminfo);
|
|
|
|
|
|
|
|
|
|
len = min_t(unsigned int, len, sizeof(meminfo));
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_to_sockptr(optval, &meminfo, len))
|
2017-03-20 15:22:03 -04:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
|
|
goto lenout;
|
|
|
|
|
}
|
2017-03-24 10:08:36 -07:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_RX_BUSY_POLL
|
|
|
|
|
case SO_INCOMING_NAPI_ID:
|
|
|
|
|
v.val = READ_ONCE(sk->sk_napi_id);
|
|
|
|
|
|
|
|
|
|
/* aggregate non-NAPI IDs down to 0 */
|
2025-02-14 18:17:51 +00:00
|
|
|
if (!napi_id_valid(v.val))
|
2017-03-24 10:08:36 -07:00
|
|
|
v.val = 0;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-04-05 19:00:55 -07:00
|
|
|
case SO_COOKIE:
|
|
|
|
|
lv = sizeof(u64);
|
|
|
|
|
if (len < lv)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
v.val64 = sock_gen_cookie(sk);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-08-03 16:29:40 -04:00
|
|
|
case SO_ZEROCOPY:
|
|
|
|
|
v.val = sock_flag(sk, SOCK_ZEROCOPY);
|
|
|
|
|
break;
|
|
|
|
|
|
2018-07-03 15:42:48 -07:00
|
|
|
case SO_TXTIME:
|
|
|
|
|
lv = sizeof(v.txtime);
|
|
|
|
|
v.txtime.clockid = sk->sk_clockid;
|
|
|
|
|
v.txtime.flags |= sk->sk_txtime_deadline_mode ?
|
|
|
|
|
SOF_TXTIME_DEADLINE_MODE : 0;
|
2018-07-03 15:43:00 -07:00
|
|
|
v.txtime.flags |= sk->sk_txtime_report_errors ?
|
|
|
|
|
SOF_TXTIME_REPORT_ERRORS : 0;
|
2018-07-03 15:42:48 -07:00
|
|
|
break;
|
|
|
|
|
|
2019-01-15 14:42:14 +01:00
|
|
|
case SO_BINDTOIFINDEX:
|
2022-05-13 11:55:44 -07:00
|
|
|
v.val = READ_ONCE(sk->sk_bound_dev_if);
|
2019-01-15 14:42:14 +01:00
|
|
|
break;
|
|
|
|
|
|
2021-06-23 15:56:45 +02:00
|
|
|
case SO_NETNS_COOKIE:
|
|
|
|
|
lv = sizeof(u64);
|
|
|
|
|
if (len != lv)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
v.val64 = sock_net(sk)->net_cookie;
|
|
|
|
|
break;
|
|
|
|
|
|
2021-08-04 10:55:56 +03:00
|
|
|
case SO_BUF_LOCK:
|
|
|
|
|
v.val = sk->sk_userlocks & SOCK_BUF_LOCK_MASK;
|
|
|
|
|
break;
|
|
|
|
|
|
2021-09-29 10:25:11 -07:00
|
|
|
case SO_RESERVE_MEM:
|
2023-07-28 15:03:08 +00:00
|
|
|
v.val = READ_ONCE(sk->sk_reserved_mem);
|
2021-09-29 10:25:11 -07:00
|
|
|
break;
|
|
|
|
|
|
2022-01-31 16:31:22 +03:00
|
|
|
case SO_TXREHASH:
|
2025-05-19 13:57:55 -07:00
|
|
|
if (!sk_is_tcp(sk))
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
2023-07-28 15:03:09 +00:00
|
|
|
/* Paired with WRITE_ONCE() in sk_setsockopt() */
|
|
|
|
|
v.val = READ_ONCE(sk->sk_txrehash);
|
2022-01-31 16:31:22 +03:00
|
|
|
break;
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
default:
|
2015-03-23 18:04:13 +09:00
|
|
|
/* We implement the SO_SNDLOWAT etc to not be settable
|
|
|
|
|
* (1003.1g 7).
|
|
|
|
|
*/
|
2007-04-10 20:10:33 -07:00
|
|
|
return -ENOPROTOOPT;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
if (len > lv)
|
|
|
|
|
len = lv;
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_to_sockptr(optval, &v, len))
|
2005-04-16 15:20:36 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
lenout:
|
2022-09-01 17:28:02 -07:00
|
|
|
if (copy_to_sockptr(optlen, &len, sizeof(int)))
|
2007-02-09 23:24:36 +09:00
|
|
|
return -EFAULT;
|
|
|
|
|
return 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2006-07-03 00:25:35 -07:00
|
|
|
/*
|
|
|
|
|
* Initialize an sk_lock.
|
|
|
|
|
*
|
|
|
|
|
* (We also register the sk_lock with the lock validator.)
|
|
|
|
|
*/
|
2007-03-22 12:27:49 -07:00
|
|
|
static inline void sock_lock_init(struct sock *sk)
|
2006-07-03 00:25:35 -07:00
|
|
|
{
|
2025-04-07 09:33:11 -07:00
|
|
|
sk_owner_clear(sk);
|
|
|
|
|
|
2017-03-09 08:09:05 +00:00
|
|
|
if (sk->sk_kern_sock)
|
|
|
|
|
sock_lock_init_class_and_name(
|
|
|
|
|
sk,
|
|
|
|
|
af_family_kern_slock_key_strings[sk->sk_family],
|
|
|
|
|
af_family_kern_slock_keys + sk->sk_family,
|
|
|
|
|
af_family_kern_key_strings[sk->sk_family],
|
|
|
|
|
af_family_kern_keys + sk->sk_family);
|
|
|
|
|
else
|
|
|
|
|
sock_lock_init_class_and_name(
|
|
|
|
|
sk,
|
2006-12-06 20:35:24 -08:00
|
|
|
af_family_slock_key_strings[sk->sk_family],
|
|
|
|
|
af_family_slock_keys + sk->sk_family,
|
|
|
|
|
af_family_key_strings[sk->sk_family],
|
|
|
|
|
af_family_keys + sk->sk_family);
|
2006-07-03 00:25:35 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 23:13:10 +00:00
|
|
|
/*
|
|
|
|
|
* Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
|
2024-08-22 13:57:34 +01:00
|
|
|
* even temporarily, because of RCU lookups. sk_node should also be left as is.
|
2010-11-30 19:04:07 +00:00
|
|
|
* We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
|
2009-07-15 23:13:10 +00:00
|
|
|
*/
|
2007-11-01 00:29:45 -07:00
|
|
|
static void sock_copy(struct sock *nsk, const struct sock *osk)
|
|
|
|
|
{
|
2020-02-18 17:10:13 +00:00
|
|
|
const struct proto *prot = READ_ONCE(osk->sk_prot);
|
2007-11-01 00:29:45 -07:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
|
|
|
|
void *sptr = nsk->sk_security;
|
|
|
|
|
#endif
|
2021-01-29 00:02:17 +09:00
|
|
|
|
|
|
|
|
/* If we move sk_tx_queue_mapping out of the private section,
|
|
|
|
|
* we must check if sk_tx_queue_clear() is called after
|
|
|
|
|
* sock_copy() in sk_clone_lock().
|
|
|
|
|
*/
|
|
|
|
|
BUILD_BUG_ON(offsetof(struct sock, sk_tx_queue_mapping) <
|
|
|
|
|
offsetof(struct sock, sk_dontcopy_begin) ||
|
|
|
|
|
offsetof(struct sock, sk_tx_queue_mapping) >=
|
|
|
|
|
offsetof(struct sock, sk_dontcopy_end));
|
|
|
|
|
|
2010-11-30 19:04:07 +00:00
|
|
|
memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
|
|
|
|
|
|
2024-03-04 13:29:31 -08:00
|
|
|
unsafe_memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
|
|
|
|
|
prot->obj_size - offsetof(struct sock, sk_dontcopy_end),
|
|
|
|
|
/* alloc is larger than struct, see sk_prot_alloc() */);
|
2010-11-30 19:04:07 +00:00
|
|
|
|
2007-11-01 00:29:45 -07:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
|
|
|
|
nsk->sk_security = sptr;
|
|
|
|
|
security_sk_clone(osk, nsk);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-01 00:36:26 -07:00
|
|
|
static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
|
|
|
|
|
int family)
|
2007-11-01 00:33:50 -07:00
|
|
|
{
|
|
|
|
|
struct sock *sk;
|
|
|
|
|
struct kmem_cache *slab;
|
|
|
|
|
|
|
|
|
|
slab = prot->slab;
|
2009-07-08 19:36:05 +00:00
|
|
|
if (slab != NULL) {
|
|
|
|
|
sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
|
|
|
|
|
if (!sk)
|
|
|
|
|
return sk;
|
2019-07-11 20:59:19 -07:00
|
|
|
if (want_init_on_alloc(priority))
|
2016-08-23 11:39:29 -07:00
|
|
|
sk_prot_clear_nulls(sk, prot->obj_size);
|
2010-12-16 14:26:56 -08:00
|
|
|
} else
|
2007-11-01 00:33:50 -07:00
|
|
|
sk = kmalloc(prot->obj_size, priority);
|
|
|
|
|
|
2007-11-01 00:36:26 -07:00
|
|
|
if (sk != NULL) {
|
|
|
|
|
if (security_sk_alloc(sk, family, priority))
|
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
|
|
if (!try_module_get(prot->owner))
|
|
|
|
|
goto out_free_sec;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-01 00:33:50 -07:00
|
|
|
return sk;
|
2007-11-01 00:36:26 -07:00
|
|
|
|
|
|
|
|
out_free_sec:
|
|
|
|
|
security_sk_free(sk);
|
|
|
|
|
out_free:
|
|
|
|
|
if (slab != NULL)
|
|
|
|
|
kmem_cache_free(slab, sk);
|
|
|
|
|
else
|
|
|
|
|
kfree(sk);
|
|
|
|
|
return NULL;
|
2007-11-01 00:33:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sk_prot_free(struct proto *prot, struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
struct kmem_cache *slab;
|
2007-11-01 00:36:26 -07:00
|
|
|
struct module *owner;
|
2007-11-01 00:33:50 -07:00
|
|
|
|
2007-11-01 00:36:26 -07:00
|
|
|
owner = prot->owner;
|
2007-11-01 00:33:50 -07:00
|
|
|
slab = prot->slab;
|
2007-11-01 00:36:26 -07:00
|
|
|
|
2015-12-07 17:38:53 -05:00
|
|
|
cgroup_sk_free(&sk->sk_cgrp_data);
|
2016-10-07 17:00:58 -07:00
|
|
|
mem_cgroup_sk_free(sk);
|
2007-11-01 00:36:26 -07:00
|
|
|
security_sk_free(sk);
|
2025-04-07 09:33:11 -07:00
|
|
|
|
|
|
|
|
sk_owner_put(sk);
|
|
|
|
|
|
2007-11-01 00:33:50 -07:00
|
|
|
if (slab != NULL)
|
|
|
|
|
kmem_cache_free(slab, sk);
|
|
|
|
|
else
|
|
|
|
|
kfree(sk);
|
2007-11-01 00:36:26 -07:00
|
|
|
module_put(owner);
|
2007-11-01 00:33:50 -07:00
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/**
|
|
|
|
|
* sk_alloc - All socket objects are allocated here
|
2007-10-12 21:17:49 -07:00
|
|
|
* @net: the applicable net namespace
|
2005-05-01 08:59:25 -07:00
|
|
|
* @family: protocol family
|
|
|
|
|
* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
|
|
|
|
|
* @prot: struct proto associated with this new sock instance
|
2015-05-08 21:09:13 -05:00
|
|
|
* @kern: is this to be a kernel socket?
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2007-10-08 23:24:22 -07:00
|
|
|
struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
|
2015-05-08 21:09:13 -05:00
|
|
|
struct proto *prot, int kern)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2007-11-01 00:33:50 -07:00
|
|
|
struct sock *sk;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-11-01 00:38:43 -07:00
|
|
|
sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (sk) {
|
2007-11-01 00:38:43 -07:00
|
|
|
sk->sk_family = family;
|
|
|
|
|
/*
|
|
|
|
|
* See comment in struct sock definition to understand
|
|
|
|
|
* why we need sk_prot_creator -acme
|
|
|
|
|
*/
|
|
|
|
|
sk->sk_prot = sk->sk_prot_creator = prot;
|
2025-10-14 23:54:56 +00:00
|
|
|
|
|
|
|
|
if (READ_ONCE(net->core.sysctl_bypass_prot_mem))
|
|
|
|
|
sk->sk_bypass_prot_mem = 1;
|
|
|
|
|
|
2017-03-09 08:09:05 +00:00
|
|
|
sk->sk_kern_sock = kern;
|
2007-11-01 00:38:43 -07:00
|
|
|
sock_lock_init(sk);
|
2025-10-14 23:54:56 +00:00
|
|
|
|
2015-05-08 21:10:31 -05:00
|
|
|
sk->sk_net_refcnt = kern ? 0 : 1;
|
2017-12-14 05:51:58 -08:00
|
|
|
if (likely(sk->sk_net_refcnt)) {
|
2021-12-09 23:44:22 -08:00
|
|
|
get_net_track(net, &sk->ns_tracker, priority);
|
2017-12-14 05:51:58 -08:00
|
|
|
sock_inuse_add(net, 1);
|
2022-10-20 23:20:18 +00:00
|
|
|
} else {
|
2025-02-20 13:18:54 +00:00
|
|
|
net_passive_inc(net);
|
2022-10-20 23:20:18 +00:00
|
|
|
__netns_tracker_alloc(net, &sk->ns_tracker,
|
|
|
|
|
false, priority);
|
2017-12-14 05:51:58 -08:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 21:10:31 -05:00
|
|
|
sock_net_set(sk, net);
|
2025-10-13 15:22:31 +00:00
|
|
|
refcount_set(&sk->sk_wmem_alloc, SK_WMEM_ALLOC_BIAS);
|
2010-05-24 00:12:34 -07:00
|
|
|
|
2016-10-07 17:00:58 -07:00
|
|
|
mem_cgroup_sk_alloc(sk);
|
2016-09-19 14:44:38 -07:00
|
|
|
cgroup_sk_alloc(&sk->sk_cgrp_data);
|
2015-12-07 17:38:52 -05:00
|
|
|
sock_update_classid(&sk->sk_cgrp_data);
|
|
|
|
|
sock_update_netprioidx(&sk->sk_cgrp_data);
|
2020-06-22 23:26:04 +03:00
|
|
|
sk_tx_queue_clear(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2005-09-27 15:23:38 -07:00
|
|
|
|
2007-11-01 00:36:26 -07:00
|
|
|
return sk;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sk_alloc);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2016-04-01 08:52:12 -07:00
|
|
|
/* Sockets having SOCK_RCU_FREE will call this function after one RCU
|
|
|
|
|
* grace period. This is the case for UDP sockets and TCP listeners.
|
|
|
|
|
*/
|
|
|
|
|
static void __sk_destruct(struct rcu_head *head)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2016-04-01 08:52:12 -07:00
|
|
|
struct sock *sk = container_of(head, struct sock, sk_rcu);
|
2025-02-20 13:18:54 +00:00
|
|
|
struct net *net = sock_net(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
struct sk_filter *filter;
|
|
|
|
|
|
|
|
|
|
if (sk->sk_destruct)
|
|
|
|
|
sk->sk_destruct(sk);
|
|
|
|
|
|
2010-02-22 17:04:49 -08:00
|
|
|
filter = rcu_dereference_check(sk->sk_filter,
|
2017-06-30 13:08:00 +03:00
|
|
|
refcount_read(&sk->sk_wmem_alloc) == 0);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (filter) {
|
2007-10-17 21:21:51 -07:00
|
|
|
sk_filter_uncharge(sk, filter);
|
2011-08-01 16:19:00 +00:00
|
|
|
RCU_INIT_POINTER(sk->sk_filter, NULL);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 12:04:18 +00:00
|
|
|
sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2019-04-26 16:39:39 -07:00
|
|
|
#ifdef CONFIG_BPF_SYSCALL
|
|
|
|
|
bpf_sk_storage_free(sk);
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
if (atomic_read(&sk->sk_omem_alloc))
|
2012-05-16 19:58:40 +00:00
|
|
|
pr_debug("%s: optmem leakage (%d bytes) detected\n",
|
|
|
|
|
__func__, atomic_read(&sk->sk_omem_alloc));
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2017-03-15 13:21:28 -07:00
|
|
|
if (sk->sk_frag.page) {
|
|
|
|
|
put_page(sk->sk_frag.page);
|
|
|
|
|
sk->sk_frag.page = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 15:57:50 -07:00
|
|
|
/* We do not need to acquire sk->sk_peer_lock, we are the last user. */
|
|
|
|
|
put_cred(sk->sk_peer_cred);
|
2010-06-13 03:30:14 +00:00
|
|
|
put_pid(sk->sk_peer_pid);
|
2021-09-29 15:57:50 -07:00
|
|
|
|
2025-02-20 13:18:54 +00:00
|
|
|
if (likely(sk->sk_net_refcnt)) {
|
|
|
|
|
put_net_track(net, &sk->ns_tracker);
|
|
|
|
|
} else {
|
|
|
|
|
__netns_tracker_free(net, &sk->ns_tracker, false);
|
|
|
|
|
net_passive_dec(net);
|
|
|
|
|
}
|
2007-11-01 00:33:50 -07:00
|
|
|
sk_prot_free(sk->sk_prot_creator, sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2009-06-11 02:55:43 -07:00
|
|
|
|
2025-02-20 13:18:54 +00:00
|
|
|
void sk_net_refcnt_upgrade(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
struct net *net = sock_net(sk);
|
|
|
|
|
|
|
|
|
|
WARN_ON_ONCE(sk->sk_net_refcnt);
|
|
|
|
|
__netns_tracker_free(net, &sk->ns_tracker, false);
|
|
|
|
|
net_passive_dec(net);
|
|
|
|
|
sk->sk_net_refcnt = 1;
|
|
|
|
|
get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
|
|
|
|
|
sock_inuse_add(net, 1);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(sk_net_refcnt_upgrade);
|
|
|
|
|
|
2016-04-01 08:52:12 -07:00
|
|
|
void sk_destruct(struct sock *sk)
|
|
|
|
|
{
|
2019-09-27 16:00:31 -07:00
|
|
|
bool use_call_rcu = sock_flag(sk, SOCK_RCU_FREE);
|
|
|
|
|
|
|
|
|
|
if (rcu_access_pointer(sk->sk_reuseport_cb)) {
|
|
|
|
|
reuseport_detach_sock(sk);
|
|
|
|
|
use_call_rcu = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (use_call_rcu)
|
2016-04-01 08:52:12 -07:00
|
|
|
call_rcu(&sk->sk_rcu, __sk_destruct);
|
|
|
|
|
else
|
|
|
|
|
__sk_destruct(&sk->sk_rcu);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-15 11:26:18 -04:00
|
|
|
static void __sk_free(struct sock *sk)
|
|
|
|
|
{
|
2017-12-14 05:51:58 -08:00
|
|
|
if (likely(sk->sk_net_refcnt))
|
|
|
|
|
sock_inuse_add(sock_net(sk), -1);
|
|
|
|
|
|
2018-05-18 04:47:55 -07:00
|
|
|
if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
|
2015-06-15 11:26:18 -04:00
|
|
|
sock_diag_broadcast_destroy(sk);
|
|
|
|
|
else
|
|
|
|
|
sk_destruct(sk);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-11 02:55:43 -07:00
|
|
|
void sk_free(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
/*
|
2011-03-30 22:57:33 -03:00
|
|
|
* We subtract one from sk_wmem_alloc and can know if
|
2009-06-11 02:55:43 -07:00
|
|
|
* some packets are still in some tx queue.
|
|
|
|
|
* If not null, sock_wfree() will call __sk_free(sk) later
|
|
|
|
|
*/
|
2017-06-30 13:08:00 +03:00
|
|
|
if (refcount_dec_and_test(&sk->sk_wmem_alloc))
|
2009-06-11 02:55:43 -07:00
|
|
|
__sk_free(sk);
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sk_free);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2017-03-09 13:54:08 +01:00
|
|
|
static void sk_init_common(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
skb_queue_head_init(&sk->sk_receive_queue);
|
|
|
|
|
skb_queue_head_init(&sk->sk_write_queue);
|
|
|
|
|
skb_queue_head_init(&sk->sk_error_queue);
|
|
|
|
|
|
|
|
|
|
rwlock_init(&sk->sk_callback_lock);
|
|
|
|
|
lockdep_set_class_and_name(&sk->sk_receive_queue.lock,
|
|
|
|
|
af_rlock_keys + sk->sk_family,
|
|
|
|
|
af_family_rlock_key_strings[sk->sk_family]);
|
|
|
|
|
lockdep_set_class_and_name(&sk->sk_write_queue.lock,
|
|
|
|
|
af_wlock_keys + sk->sk_family,
|
|
|
|
|
af_family_wlock_key_strings[sk->sk_family]);
|
|
|
|
|
lockdep_set_class_and_name(&sk->sk_error_queue.lock,
|
|
|
|
|
af_elock_keys + sk->sk_family,
|
|
|
|
|
af_family_elock_key_strings[sk->sk_family]);
|
2024-05-26 22:57:18 +08:00
|
|
|
if (sk->sk_kern_sock)
|
|
|
|
|
lockdep_set_class_and_name(&sk->sk_callback_lock,
|
|
|
|
|
af_kern_callback_keys + sk->sk_family,
|
|
|
|
|
af_family_kern_clock_key_strings[sk->sk_family]);
|
|
|
|
|
else
|
|
|
|
|
lockdep_set_class_and_name(&sk->sk_callback_lock,
|
2017-03-09 13:54:08 +01:00
|
|
|
af_callback_keys + sk->sk_family,
|
|
|
|
|
af_family_clock_key_strings[sk->sk_family]);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 17:07:07 -05:00
|
|
|
/**
|
2025-10-23 23:16:53 +00:00
|
|
|
* sk_clone - clone a socket
|
|
|
|
|
* @sk: the socket to clone
|
|
|
|
|
* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
|
|
|
|
|
* @lock: if true, lock the cloned sk
|
2011-11-08 17:07:07 -05:00
|
|
|
*
|
2025-10-23 23:16:53 +00:00
|
|
|
* If @lock is true, the clone is locked by bh_lock_sock(), and
|
|
|
|
|
* caller must unlock socket even in error path by bh_unlock_sock().
|
2011-11-08 17:07:07 -05:00
|
|
|
*/
|
2025-10-23 23:16:53 +00:00
|
|
|
struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
|
|
|
|
|
bool lock)
|
2005-08-09 20:10:12 -07:00
|
|
|
{
|
2020-02-18 17:10:13 +00:00
|
|
|
struct proto *prot = READ_ONCE(sk->sk_prot);
|
2021-01-27 07:27:31 -08:00
|
|
|
struct sk_filter *filter;
|
2014-07-30 20:34:12 -07:00
|
|
|
bool is_charged = true;
|
2021-01-27 07:27:31 -08:00
|
|
|
struct sock *newsk;
|
2005-08-09 20:10:12 -07:00
|
|
|
|
2020-02-18 17:10:13 +00:00
|
|
|
newsk = sk_prot_alloc(prot, priority, sk->sk_family);
|
2021-01-27 07:27:31 -08:00
|
|
|
if (!newsk)
|
|
|
|
|
goto out;
|
2005-08-09 20:10:12 -07:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
sock_copy(newsk, sk);
|
2005-08-09 20:10:12 -07:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
newsk->sk_prot_creator = prot;
|
2017-09-26 17:38:50 -07:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
/* SANITY */
|
2021-11-15 19:16:56 +09:00
|
|
|
if (likely(newsk->sk_net_refcnt)) {
|
2021-12-09 23:44:22 -08:00
|
|
|
get_net_track(sock_net(newsk), &newsk->ns_tracker, priority);
|
2021-11-15 19:16:56 +09:00
|
|
|
sock_inuse_add(sock_net(newsk), 1);
|
2022-10-20 23:20:18 +00:00
|
|
|
} else {
|
|
|
|
|
/* Kernel sockets are not elevating the struct net refcount.
|
|
|
|
|
* Instead, use a tracker to more easily detect if a layer
|
|
|
|
|
* is not properly dismantling its kernel sockets at netns
|
|
|
|
|
* destroy time.
|
|
|
|
|
*/
|
2025-02-20 13:18:54 +00:00
|
|
|
net_passive_inc(sock_net(newsk));
|
2022-10-20 23:20:18 +00:00
|
|
|
__netns_tracker_alloc(sock_net(newsk), &newsk->ns_tracker,
|
|
|
|
|
false, priority);
|
2021-11-15 19:16:56 +09:00
|
|
|
}
|
2025-10-23 23:16:53 +00:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
sk_node_init(&newsk->sk_node);
|
|
|
|
|
sock_lock_init(newsk);
|
2025-10-23 23:16:53 +00:00
|
|
|
|
|
|
|
|
if (lock)
|
|
|
|
|
bh_lock_sock(newsk);
|
|
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
|
|
|
|
|
newsk->sk_backlog.len = 0;
|
2005-08-09 20:10:12 -07:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
atomic_set(&newsk->sk_rmem_alloc, 0);
|
|
|
|
|
|
2025-10-13 15:22:31 +00:00
|
|
|
refcount_set(&newsk->sk_wmem_alloc, SK_WMEM_ALLOC_BIAS);
|
2021-01-27 07:27:31 -08:00
|
|
|
|
|
|
|
|
atomic_set(&newsk->sk_omem_alloc, 0);
|
|
|
|
|
sk_init_common(newsk);
|
|
|
|
|
|
|
|
|
|
newsk->sk_dst_cache = NULL;
|
|
|
|
|
newsk->sk_dst_pending_confirm = 0;
|
|
|
|
|
newsk->sk_wmem_queued = 0;
|
|
|
|
|
newsk->sk_forward_alloc = 0;
|
2021-09-29 10:25:11 -07:00
|
|
|
newsk->sk_reserved_mem = 0;
|
2025-08-26 12:50:29 +00:00
|
|
|
DEBUG_NET_WARN_ON_ONCE(newsk->sk_drop_counters);
|
2025-08-26 12:50:27 +00:00
|
|
|
sk_drops_reset(newsk);
|
2021-01-27 07:27:31 -08:00
|
|
|
newsk->sk_send_head = NULL;
|
|
|
|
|
newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
|
|
|
|
|
atomic_set(&newsk->sk_zckey, 0);
|
|
|
|
|
|
|
|
|
|
sock_reset_flag(newsk, SOCK_DONE);
|
|
|
|
|
|
2025-08-15 20:16:18 +00:00
|
|
|
#ifdef CONFIG_MEMCG
|
2021-01-27 07:27:31 -08:00
|
|
|
/* sk->sk_memcg will be populated at accept() time */
|
|
|
|
|
newsk->sk_memcg = NULL;
|
2025-08-15 20:16:18 +00:00
|
|
|
#endif
|
2021-01-27 07:27:31 -08:00
|
|
|
|
|
|
|
|
cgroup_sk_clone(&newsk->sk_cgrp_data);
|
|
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
|
filter = rcu_dereference(sk->sk_filter);
|
|
|
|
|
if (filter != NULL)
|
|
|
|
|
/* though it's an empty new sock, the charging may fail
|
|
|
|
|
* if sysctl_optmem_max was changed between creation of
|
|
|
|
|
* original socket and cloning
|
2009-06-11 02:55:43 -07:00
|
|
|
*/
|
2021-01-27 07:27:31 -08:00
|
|
|
is_charged = sk_filter_charge(newsk, filter);
|
|
|
|
|
RCU_INIT_POINTER(newsk->sk_filter, filter);
|
|
|
|
|
rcu_read_unlock();
|
2005-08-09 20:10:12 -07:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
|
|
|
|
|
/* We need to make sure that we don't uncharge the new
|
|
|
|
|
* socket if we couldn't charge it in the first place
|
|
|
|
|
* as otherwise we uncharge the parent's filter.
|
2020-02-18 17:10:14 +00:00
|
|
|
*/
|
2021-01-27 07:27:31 -08:00
|
|
|
if (!is_charged)
|
|
|
|
|
RCU_INIT_POINTER(newsk->sk_filter, NULL);
|
2025-04-09 19:36:46 -07:00
|
|
|
|
|
|
|
|
goto free;
|
2005-08-09 20:10:12 -07:00
|
|
|
}
|
2025-04-09 19:36:46 -07:00
|
|
|
|
2021-01-27 07:27:31 -08:00
|
|
|
RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
|
|
|
|
|
|
2025-04-09 19:36:46 -07:00
|
|
|
if (bpf_sk_storage_clone(sk, newsk))
|
|
|
|
|
goto free;
|
2021-01-27 07:27:31 -08:00
|
|
|
|
|
|
|
|
/* Clear sk_user_data if parent had the pointer tagged
|
|
|
|
|
* as not suitable for copying when cloning.
|
|
|
|
|
*/
|
|
|
|
|
if (sk_user_data_is_nocopy(newsk))
|
|
|
|
|
newsk->sk_user_data = NULL;
|
|
|
|
|
|
|
|
|
|
newsk->sk_err = 0;
|
|
|
|
|
newsk->sk_err_soft = 0;
|
|
|
|
|
newsk->sk_priority = 0;
|
|
|
|
|
newsk->sk_incoming_cpu = raw_smp_processor_id();
|
|
|
|
|
|
|
|
|
|
/* Before updating sk_refcnt, we must commit prior changes to memory
|
|
|
|
|
* (Documentation/RCU/rculist_nulls.rst for details)
|
|
|
|
|
*/
|
|
|
|
|
smp_wmb();
|
|
|
|
|
refcount_set(&newsk->sk_refcnt, 2);
|
|
|
|
|
|
|
|
|
|
sk_set_socket(newsk, NULL);
|
|
|
|
|
sk_tx_queue_clear(newsk);
|
|
|
|
|
RCU_INIT_POINTER(newsk->sk_wq, NULL);
|
|
|
|
|
|
|
|
|
|
if (newsk->sk_prot->sockets_allocated)
|
|
|
|
|
sk_sockets_allocated_inc(newsk);
|
|
|
|
|
|
|
|
|
|
if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
|
|
|
|
|
net_enable_timestamp();
|
2005-08-09 20:10:12 -07:00
|
|
|
out:
|
|
|
|
|
return newsk;
|
2025-04-09 19:36:46 -07:00
|
|
|
free:
|
|
|
|
|
/* It is still raw copy of parent, so invalidate
|
|
|
|
|
* destructor and make plain sk_free()
|
|
|
|
|
*/
|
|
|
|
|
newsk->sk_destruct = NULL;
|
2025-10-23 23:16:53 +00:00
|
|
|
if (lock)
|
|
|
|
|
bh_unlock_sock(newsk);
|
2025-04-09 19:36:46 -07:00
|
|
|
sk_free(newsk);
|
|
|
|
|
newsk = NULL;
|
|
|
|
|
goto out;
|
2005-08-09 20:10:12 -07:00
|
|
|
}
|
2025-10-23 23:16:53 +00:00
|
|
|
EXPORT_SYMBOL_GPL(sk_clone);
|
2005-08-09 20:10:12 -07:00
|
|
|
|
2025-08-28 19:58:20 +00:00
|
|
|
static u32 sk_dst_gso_max_size(struct sock *sk, const struct net_device *dev)
|
2022-05-13 11:33:57 -07:00
|
|
|
{
|
2023-01-28 10:58:39 -05:00
|
|
|
bool is_ipv6 = false;
|
|
|
|
|
u32 max_size;
|
|
|
|
|
|
2022-05-13 11:33:57 -07:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
2023-01-28 10:58:39 -05:00
|
|
|
is_ipv6 = (sk->sk_family == AF_INET6 &&
|
|
|
|
|
!ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr));
|
2022-05-13 11:33:57 -07:00
|
|
|
#endif
|
2023-01-28 10:58:39 -05:00
|
|
|
/* pairs with the WRITE_ONCE() in netif_set_gso(_ipv4)_max_size() */
|
2025-08-28 19:58:20 +00:00
|
|
|
max_size = is_ipv6 ? READ_ONCE(dev->gso_max_size) :
|
|
|
|
|
READ_ONCE(dev->gso_ipv4_max_size);
|
2023-01-28 10:58:39 -05:00
|
|
|
if (max_size > GSO_LEGACY_MAX_SIZE && !sk_is_tcp(sk))
|
|
|
|
|
max_size = GSO_LEGACY_MAX_SIZE;
|
|
|
|
|
|
|
|
|
|
return max_size - (MAX_TCP_HEADER + 1);
|
2022-05-13 11:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2007-04-20 17:12:43 -07:00
|
|
|
void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
|
|
|
|
|
{
|
2025-08-28 19:58:20 +00:00
|
|
|
const struct net_device *dev;
|
2015-05-26 08:55:28 -07:00
|
|
|
u32 max_segs = 1;
|
|
|
|
|
|
2025-08-28 19:58:20 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
|
dev = dst_dev_rcu(dst);
|
|
|
|
|
sk->sk_route_caps = dev->features;
|
2025-03-12 08:39:07 +00:00
|
|
|
if (sk_is_tcp(sk)) {
|
|
|
|
|
struct inet_connection_sock *icsk = inet_csk(sk);
|
|
|
|
|
|
2021-11-15 11:02:34 -08:00
|
|
|
sk->sk_route_caps |= NETIF_F_GSO;
|
2025-03-12 08:39:07 +00:00
|
|
|
icsk->icsk_ack.dst_quick_ack = dst_metric(dst, RTAX_QUICKACK);
|
|
|
|
|
}
|
2007-04-20 17:12:43 -07:00
|
|
|
if (sk->sk_route_caps & NETIF_F_GSO)
|
2007-05-31 22:15:50 -07:00
|
|
|
sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
|
2021-11-15 11:02:35 -08:00
|
|
|
if (unlikely(sk->sk_gso_disabled))
|
|
|
|
|
sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
|
2007-04-20 17:12:43 -07:00
|
|
|
if (sk_can_gso(sk)) {
|
2017-08-01 12:49:10 +03:00
|
|
|
if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
|
2007-04-20 17:12:43 -07:00
|
|
|
sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
|
2008-03-21 03:43:19 -07:00
|
|
|
} else {
|
2007-04-20 17:12:43 -07:00
|
|
|
sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
|
2025-08-28 19:58:20 +00:00
|
|
|
sk->sk_gso_max_size = sk_dst_gso_max_size(sk, dev);
|
2021-11-19 07:43:32 -08:00
|
|
|
/* pairs with the WRITE_ONCE() in netif_set_gso_max_segs() */
|
2025-08-28 19:58:20 +00:00
|
|
|
max_segs = max_t(u32, READ_ONCE(dev->gso_max_segs), 1);
|
2008-03-21 03:43:19 -07:00
|
|
|
}
|
2007-04-20 17:12:43 -07:00
|
|
|
}
|
2015-05-26 08:55:28 -07:00
|
|
|
sk->sk_gso_max_segs = max_segs;
|
2023-05-30 14:39:41 +03:00
|
|
|
sk_dst_set(sk, dst);
|
2025-08-28 19:58:20 +00:00
|
|
|
rcu_read_unlock();
|
2007-04-20 17:12:43 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(sk_setup_caps);
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Simple resource managers for sockets.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2007-02-09 23:24:36 +09:00
|
|
|
/*
|
|
|
|
|
* Write buffer destructor automatically called from kfree_skb.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
|
|
|
|
void sock_wfree(struct sk_buff *skb)
|
|
|
|
|
{
|
2009-09-24 10:49:24 +00:00
|
|
|
unsigned int len = skb->truesize;
|
2025-10-17 13:37:12 +00:00
|
|
|
struct sock *sk = skb->sk;
|
2022-04-28 11:58:18 +01:00
|
|
|
bool free;
|
2025-10-17 13:37:12 +00:00
|
|
|
int old;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2009-09-24 10:49:24 +00:00
|
|
|
if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
|
2022-04-28 11:58:18 +01:00
|
|
|
if (sock_flag(sk, SOCK_RCU_FREE) &&
|
|
|
|
|
sk->sk_write_space == sock_def_write_space) {
|
|
|
|
|
rcu_read_lock();
|
2025-10-17 13:37:12 +00:00
|
|
|
free = __refcount_sub_and_test(len, &sk->sk_wmem_alloc,
|
|
|
|
|
&old);
|
|
|
|
|
sock_def_write_space_wfree(sk, old - len);
|
2022-04-28 11:58:18 +01:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
if (unlikely(free))
|
|
|
|
|
__sk_free(sk);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 10:49:24 +00:00
|
|
|
/*
|
|
|
|
|
* Keep a reference on sk_wmem_alloc, this will be released
|
|
|
|
|
* after sk_write_space() call
|
|
|
|
|
*/
|
2017-06-30 13:08:00 +03:00
|
|
|
WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc));
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_write_space(sk);
|
2009-09-24 10:49:24 +00:00
|
|
|
len = 1;
|
|
|
|
|
}
|
2009-06-11 02:55:43 -07:00
|
|
|
/*
|
2009-09-24 10:49:24 +00:00
|
|
|
* if sk_wmem_alloc reaches 0, we must finish what sk_free()
|
|
|
|
|
* could not do because of in-flight packets
|
2009-06-11 02:55:43 -07:00
|
|
|
*/
|
2017-06-30 13:08:00 +03:00
|
|
|
if (refcount_sub_and_test(len, &sk->sk_wmem_alloc))
|
2009-06-11 02:55:43 -07:00
|
|
|
__sk_free(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_wfree);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2016-05-02 10:56:27 -07:00
|
|
|
/* This variant of sock_wfree() is used by TCP,
|
|
|
|
|
* since it sets SOCK_USE_WRITE_QUEUE.
|
|
|
|
|
*/
|
|
|
|
|
void __sock_wfree(struct sk_buff *skb)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sk = skb->sk;
|
|
|
|
|
|
2017-06-30 13:08:00 +03:00
|
|
|
if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc))
|
2016-05-02 10:56:27 -07:00
|
|
|
__sk_free(sk);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-01 15:36:55 -08:00
|
|
|
void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
|
|
|
|
|
{
|
2025-10-13 15:22:32 +00:00
|
|
|
int old_wmem;
|
|
|
|
|
|
2015-11-01 15:36:55 -08:00
|
|
|
skb_orphan(skb);
|
|
|
|
|
#ifdef CONFIG_INET
|
2024-10-10 17:48:15 +00:00
|
|
|
if (unlikely(!sk_fullsock(sk)))
|
|
|
|
|
return skb_set_owner_edemux(skb, sk);
|
2015-11-01 15:36:55 -08:00
|
|
|
#endif
|
2024-10-10 17:48:15 +00:00
|
|
|
skb->sk = sk;
|
2015-11-01 15:36:55 -08:00
|
|
|
skb->destructor = sock_wfree;
|
|
|
|
|
skb_set_hash_from_sk(skb, sk);
|
|
|
|
|
/*
|
|
|
|
|
* We used to take a refcount on sk, but following operation
|
2024-08-22 13:57:34 +01:00
|
|
|
* is enough to guarantee sk_free() won't free this sock until
|
2015-11-01 15:36:55 -08:00
|
|
|
* all in-flight packets are completed
|
|
|
|
|
*/
|
2025-10-13 15:22:32 +00:00
|
|
|
__refcount_add(skb->truesize, &sk->sk_wmem_alloc, &old_wmem);
|
|
|
|
|
|
|
|
|
|
/* (old_wmem == SK_WMEM_ALLOC_BIAS) if no other TX packet for this socket
|
|
|
|
|
* is in a host queue (qdisc, NIC queue).
|
|
|
|
|
* Set skb->ooo_okay so that netdev_pick_tx() can choose a TX queue
|
|
|
|
|
* based on XPS for better performance.
|
|
|
|
|
* Otherwise clear ooo_okay to not risk Out Of Order delivery.
|
|
|
|
|
*/
|
|
|
|
|
skb->ooo_okay = (old_wmem == SK_WMEM_ALLOC_BIAS);
|
2015-11-01 15:36:55 -08:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(skb_set_owner_w);
|
|
|
|
|
|
2019-08-07 17:03:59 -07:00
|
|
|
static bool can_skb_orphan_partial(const struct sk_buff *skb)
|
|
|
|
|
{
|
|
|
|
|
/* Drivers depend on in-order delivery for crypto offload,
|
|
|
|
|
* partial orphan breaks out-of-order-OK logic.
|
|
|
|
|
*/
|
2024-04-03 13:21:39 -07:00
|
|
|
if (skb_is_decrypted(skb))
|
2019-08-07 17:03:59 -07:00
|
|
|
return false;
|
2024-04-03 13:21:39 -07:00
|
|
|
|
2019-08-07 17:03:59 -07:00
|
|
|
return (skb->destructor == sock_wfree ||
|
|
|
|
|
(IS_ENABLED(CONFIG_INET) && skb->destructor == tcp_wfree));
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 10:56:27 -07:00
|
|
|
/* This helper is used by netem, as it can hold packets in its
|
|
|
|
|
* delay queue. We want to allow the owner socket to send more
|
|
|
|
|
* packets, as if they were already TX completed by a typical driver.
|
|
|
|
|
* But we also want to keep skb->sk set because some packet schedulers
|
2017-05-11 15:24:41 -07:00
|
|
|
* rely on it (sch_fq for example).
|
2016-05-02 10:56:27 -07:00
|
|
|
*/
|
2013-07-30 17:55:08 -07:00
|
|
|
void skb_orphan_partial(struct sk_buff *skb)
|
|
|
|
|
{
|
2017-05-11 15:24:41 -07:00
|
|
|
if (skb_is_tcp_pure_ack(skb))
|
2016-05-02 10:56:27 -07:00
|
|
|
return;
|
|
|
|
|
|
2021-05-11 10:35:21 +02:00
|
|
|
if (can_skb_orphan_partial(skb) && skb_set_owner_sk_safe(skb, skb->sk))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
skb_orphan(skb);
|
2013-07-30 17:55:08 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(skb_orphan_partial);
|
|
|
|
|
|
2007-02-09 23:24:36 +09:00
|
|
|
/*
|
|
|
|
|
* Read buffer destructor automatically called from kfree_skb.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
|
|
|
|
void sock_rfree(struct sk_buff *skb)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sk = skb->sk;
|
2010-07-10 22:45:17 +00:00
|
|
|
unsigned int len = skb->truesize;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2010-07-10 22:45:17 +00:00
|
|
|
atomic_sub(len, &sk->sk_rmem_alloc);
|
|
|
|
|
sk_mem_uncharge(sk, len);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_rfree);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2015-03-10 19:03:46 +01:00
|
|
|
/*
|
|
|
|
|
* Buffer destructor for skbs that are not used directly in read or write
|
|
|
|
|
* path, e.g. for error handler skbs. Automatically called from kfree_skb.
|
|
|
|
|
*/
|
2014-09-04 13:31:35 -04:00
|
|
|
void sock_efree(struct sk_buff *skb)
|
|
|
|
|
{
|
|
|
|
|
sock_put(skb->sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_efree);
|
|
|
|
|
|
2020-03-29 15:53:38 -07:00
|
|
|
/* Buffer destructor for prefetch/receive path where reference count may
|
|
|
|
|
* not be held, e.g. for listen sockets.
|
|
|
|
|
*/
|
|
|
|
|
#ifdef CONFIG_INET
|
|
|
|
|
void sock_pfree(struct sk_buff *skb)
|
|
|
|
|
{
|
2024-01-15 12:55:13 -08:00
|
|
|
struct sock *sk = skb->sk;
|
|
|
|
|
|
|
|
|
|
if (!sk_is_refcounted(sk))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) {
|
|
|
|
|
inet_reqsk(sk)->rsk_listener = NULL;
|
|
|
|
|
reqsk_free(inet_reqsk(sk));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sock_gen_put(sk);
|
2020-03-29 15:53:38 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_pfree);
|
|
|
|
|
#endif /* CONFIG_INET */
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Allocate a skb from the socket's send buffer.
|
|
|
|
|
*/
|
2005-07-08 14:57:47 -07:00
|
|
|
struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
|
2005-10-07 07:46:04 +01:00
|
|
|
gfp_t priority)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2019-10-10 20:17:45 -07:00
|
|
|
if (force ||
|
|
|
|
|
refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf)) {
|
2009-05-27 11:30:05 +00:00
|
|
|
struct sk_buff *skb = alloc_skb(size, priority);
|
2019-10-10 20:17:45 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
if (skb) {
|
|
|
|
|
skb_set_owner_w(skb, sk);
|
|
|
|
|
return skb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_wmalloc);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2017-08-03 16:29:37 -04:00
|
|
|
static void sock_ofree(struct sk_buff *skb)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sk = skb->sk;
|
|
|
|
|
|
|
|
|
|
atomic_sub(skb->truesize, &sk->sk_omem_alloc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
|
|
|
|
|
gfp_t priority)
|
|
|
|
|
{
|
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
|
|
/* small safe race: SKB_TRUESIZE may differ from final skb->truesize */
|
|
|
|
|
if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) >
|
2023-12-14 10:49:00 +00:00
|
|
|
READ_ONCE(sock_net(sk)->core.sysctl_optmem_max))
|
2017-08-03 16:29:37 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
skb = alloc_skb(size, priority);
|
|
|
|
|
if (!skb)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
atomic_add(skb->truesize, &sk->sk_omem_alloc);
|
|
|
|
|
skb->sk = sk;
|
|
|
|
|
skb->destructor = sock_ofree;
|
|
|
|
|
return skb;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-09 23:24:36 +09:00
|
|
|
/*
|
2005-04-16 15:20:36 -07:00
|
|
|
* Allocate a memory block from the socket's option memory buffer.
|
2007-02-09 23:24:36 +09:00
|
|
|
*/
|
2005-10-07 07:46:04 +01:00
|
|
|
void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2023-12-14 10:49:00 +00:00
|
|
|
int optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
|
2022-08-23 10:46:49 -07:00
|
|
|
|
|
|
|
|
if ((unsigned int)size <= optmem_max &&
|
|
|
|
|
atomic_read(&sk->sk_omem_alloc) + size < optmem_max) {
|
2005-04-16 15:20:36 -07:00
|
|
|
void *mem;
|
|
|
|
|
/* First do the add, to avoid the race if kmalloc
|
2007-02-09 23:24:36 +09:00
|
|
|
* might sleep.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
|
|
|
|
atomic_add(size, &sk->sk_omem_alloc);
|
|
|
|
|
mem = kmalloc(size, priority);
|
|
|
|
|
if (mem)
|
|
|
|
|
return mem;
|
|
|
|
|
atomic_sub(size, &sk->sk_omem_alloc);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_kmalloc);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2025-02-28 18:01:31 +08:00
|
|
|
/*
|
|
|
|
|
* Duplicate the input "src" memory block using the socket's
|
|
|
|
|
* option memory buffer.
|
|
|
|
|
*/
|
|
|
|
|
void *sock_kmemdup(struct sock *sk, const void *src,
|
|
|
|
|
int size, gfp_t priority)
|
|
|
|
|
{
|
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
|
|
mem = sock_kmalloc(sk, size, priority);
|
|
|
|
|
if (mem)
|
|
|
|
|
memcpy(mem, src, size);
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_kmemdup);
|
|
|
|
|
|
2014-11-19 17:13:11 +01:00
|
|
|
/* Free an option memory block. Note, we actually want the inline
|
|
|
|
|
* here as this allows gcc to detect the nullify and fold away the
|
|
|
|
|
* condition entirely.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2014-11-19 17:13:11 +01:00
|
|
|
static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
|
|
|
|
|
const bool nullify)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2014-10-14 17:02:37 -04:00
|
|
|
if (WARN_ON_ONCE(!mem))
|
|
|
|
|
return;
|
2014-11-19 17:13:11 +01:00
|
|
|
if (nullify)
|
2020-08-06 23:18:13 -07:00
|
|
|
kfree_sensitive(mem);
|
2014-11-19 17:13:11 +01:00
|
|
|
else
|
|
|
|
|
kfree(mem);
|
2005-04-16 15:20:36 -07:00
|
|
|
atomic_sub(size, &sk->sk_omem_alloc);
|
|
|
|
|
}
|
2014-11-19 17:13:11 +01:00
|
|
|
|
|
|
|
|
void sock_kfree_s(struct sock *sk, void *mem, int size)
|
|
|
|
|
{
|
|
|
|
|
__sock_kfree_s(sk, mem, size, false);
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_kfree_s);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2014-11-19 17:13:11 +01:00
|
|
|
void sock_kzfree_s(struct sock *sk, void *mem, int size)
|
|
|
|
|
{
|
|
|
|
|
__sock_kfree_s(sk, mem, size, true);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_kzfree_s);
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
|
|
|
|
|
I think, these locks should be removed for datagram sockets.
|
|
|
|
|
*/
|
2009-05-27 11:30:05 +00:00
|
|
|
static long sock_wait_for_wmem(struct sock *sk, long timeo)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
|
|
2015-11-29 20:03:10 -08:00
|
|
|
sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
for (;;) {
|
|
|
|
|
if (!timeo)
|
|
|
|
|
break;
|
|
|
|
|
if (signal_pending(current))
|
|
|
|
|
break;
|
|
|
|
|
set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
|
2010-04-20 13:03:51 +00:00
|
|
|
prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
|
2019-10-10 20:17:45 -07:00
|
|
|
if (refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf))
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
2023-09-01 17:27:07 -07:00
|
|
|
if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN)
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
2023-09-01 17:27:08 -07:00
|
|
|
if (READ_ONCE(sk->sk_err))
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
|
|
|
|
timeo = schedule_timeout(timeo);
|
|
|
|
|
}
|
2010-04-20 13:03:51 +00:00
|
|
|
finish_wait(sk_sleep(sk), &wait);
|
2005-04-16 15:20:36 -07:00
|
|
|
return timeo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generic send/receive buffer handlers
|
|
|
|
|
*/
|
|
|
|
|
|
2009-02-04 16:55:54 -08:00
|
|
|
struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
|
|
|
|
|
unsigned long data_len, int noblock,
|
2013-08-08 14:38:47 -07:00
|
|
|
int *errcode, int max_page_order)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2014-09-17 04:49:49 -07:00
|
|
|
struct sk_buff *skb;
|
2005-04-16 15:20:36 -07:00
|
|
|
long timeo;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
timeo = sock_sndtimeo(sk, noblock);
|
2014-09-17 04:49:49 -07:00
|
|
|
for (;;) {
|
2005-04-16 15:20:36 -07:00
|
|
|
err = sock_error(sk);
|
|
|
|
|
if (err != 0)
|
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
|
|
err = -EPIPE;
|
2023-09-01 17:27:07 -07:00
|
|
|
if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN)
|
2005-04-16 15:20:36 -07:00
|
|
|
goto failure;
|
|
|
|
|
|
2019-10-10 20:17:45 -07:00
|
|
|
if (sk_wmem_alloc_get(sk) < READ_ONCE(sk->sk_sndbuf))
|
2014-09-17 04:49:49 -07:00
|
|
|
break;
|
2013-08-08 14:38:47 -07:00
|
|
|
|
2015-11-29 20:03:10 -08:00
|
|
|
sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
|
2014-09-17 04:49:49 -07:00
|
|
|
set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
|
|
|
|
|
err = -EAGAIN;
|
|
|
|
|
if (!timeo)
|
2005-04-16 15:20:36 -07:00
|
|
|
goto failure;
|
2014-09-17 04:49:49 -07:00
|
|
|
if (signal_pending(current))
|
|
|
|
|
goto interrupted;
|
|
|
|
|
timeo = sock_wait_for_wmem(sk, timeo);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2014-09-17 04:49:49 -07:00
|
|
|
skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
|
|
|
|
|
errcode, sk->sk_allocation);
|
|
|
|
|
if (skb)
|
|
|
|
|
skb_set_owner_w(skb, sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
return skb;
|
|
|
|
|
|
|
|
|
|
interrupted:
|
|
|
|
|
err = sock_intr_errno(timeo);
|
|
|
|
|
failure:
|
|
|
|
|
*errcode = err;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-02-04 16:55:54 -08:00
|
|
|
EXPORT_SYMBOL(sock_alloc_send_pskb);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2022-10-20 06:54:41 +00:00
|
|
|
int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg,
|
2016-04-02 23:08:06 -04:00
|
|
|
struct sockcm_cookie *sockc)
|
|
|
|
|
{
|
2016-04-02 23:08:09 -04:00
|
|
|
u32 tsflags;
|
|
|
|
|
|
2024-10-01 05:57:14 -07:00
|
|
|
BUILD_BUG_ON(SOF_TIMESTAMPING_LAST == (1 << 31));
|
|
|
|
|
|
2016-04-02 23:08:06 -04:00
|
|
|
switch (cmsg->cmsg_type) {
|
|
|
|
|
case SO_MARK:
|
2022-01-31 15:33:57 -08:00
|
|
|
if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
|
|
|
|
|
!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
|
2016-04-02 23:08:06 -04:00
|
|
|
return -EPERM;
|
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
sockc->mark = *(u32 *)CMSG_DATA(cmsg);
|
|
|
|
|
break;
|
2019-02-02 07:34:46 -08:00
|
|
|
case SO_TIMESTAMPING_OLD:
|
2024-01-04 09:57:44 +01:00
|
|
|
case SO_TIMESTAMPING_NEW:
|
2016-04-02 23:08:09 -04:00
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
tsflags = *(u32 *)CMSG_DATA(cmsg);
|
|
|
|
|
if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
|
|
|
|
|
sockc->tsflags |= tsflags;
|
|
|
|
|
break;
|
2018-07-03 15:42:48 -07:00
|
|
|
case SCM_TXTIME:
|
|
|
|
|
if (!sock_flag(sk, SOCK_TXTIME))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u64)))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
sockc->transmit_time = get_unaligned((u64 *)CMSG_DATA(cmsg));
|
|
|
|
|
break;
|
2024-10-01 05:57:14 -07:00
|
|
|
case SCM_TS_OPT_ID:
|
|
|
|
|
if (sk_is_tcp(sk))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
tsflags = READ_ONCE(sk->sk_tsflags);
|
|
|
|
|
if (!(tsflags & SOF_TIMESTAMPING_OPT_ID))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
sockc->ts_opt_id = *(u32 *)CMSG_DATA(cmsg);
|
|
|
|
|
sockc->tsflags |= SOCKCM_FLAG_TS_OPT_ID;
|
|
|
|
|
break;
|
2016-07-11 16:51:26 -04:00
|
|
|
/* SCM_RIGHTS and SCM_CREDENTIALS are semantically in SOL_UNIX. */
|
|
|
|
|
case SCM_RIGHTS:
|
|
|
|
|
case SCM_CREDENTIALS:
|
|
|
|
|
break;
|
2024-12-13 09:44:55 +01:00
|
|
|
case SO_PRIORITY:
|
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (!sk_set_prio_allowed(sk, *(u32 *)CMSG_DATA(cmsg)))
|
|
|
|
|
return -EPERM;
|
|
|
|
|
sockc->priority = *(u32 *)CMSG_DATA(cmsg);
|
|
|
|
|
break;
|
2025-05-08 00:48:24 +00:00
|
|
|
case SCM_DEVMEM_DMABUF:
|
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
sockc->dmabuf_id = *(u32 *)CMSG_DATA(cmsg);
|
|
|
|
|
break;
|
2016-04-02 23:08:06 -04:00
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(__sock_cmsg_send);
|
|
|
|
|
|
2015-10-08 14:56:48 -07:00
|
|
|
int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
|
|
|
|
|
struct sockcm_cookie *sockc)
|
|
|
|
|
{
|
|
|
|
|
struct cmsghdr *cmsg;
|
2016-04-02 23:08:06 -04:00
|
|
|
int ret;
|
2015-10-08 14:56:48 -07:00
|
|
|
|
|
|
|
|
for_each_cmsghdr(cmsg, msg) {
|
|
|
|
|
if (!CMSG_OK(msg, cmsg))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET)
|
|
|
|
|
continue;
|
2022-10-20 06:54:41 +00:00
|
|
|
ret = __sock_cmsg_send(sk, cmsg, sockc);
|
2016-04-02 23:08:06 -04:00
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
2015-10-08 14:56:48 -07:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_cmsg_send);
|
|
|
|
|
|
2017-06-07 13:29:12 -07:00
|
|
|
static void sk_enter_memory_pressure(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
if (!sk->sk_prot->enter_memory_pressure)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
sk->sk_prot->enter_memory_pressure(sk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sk_leave_memory_pressure(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
if (sk->sk_prot->leave_memory_pressure) {
|
2023-03-01 13:32:47 +00:00
|
|
|
INDIRECT_CALL_INET_1(sk->sk_prot->leave_memory_pressure,
|
|
|
|
|
tcp_leave_memory_pressure, sk);
|
2017-06-07 13:29:12 -07:00
|
|
|
} else {
|
|
|
|
|
unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
|
|
|
|
|
|
2019-10-09 12:55:53 -07:00
|
|
|
if (memory_pressure && READ_ONCE(*memory_pressure))
|
|
|
|
|
WRITE_ONCE(*memory_pressure, 0);
|
2017-06-07 13:29:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 16:22:21 -07:00
|
|
|
DEFINE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key);
|
2012-09-23 23:04:42 +00:00
|
|
|
|
2013-10-17 16:27:07 -07:00
|
|
|
/**
|
|
|
|
|
* skb_page_frag_refill - check that a page_frag contains enough room
|
|
|
|
|
* @sz: minimum size of the fragment we want to get
|
|
|
|
|
* @pfrag: pointer to page_frag
|
2014-09-08 04:00:00 -07:00
|
|
|
* @gfp: priority for memory allocation
|
2013-10-17 16:27:07 -07:00
|
|
|
*
|
|
|
|
|
* Note: While this allocator tries to use high order pages, there is
|
|
|
|
|
* no guarantee that allocations succeed. Therefore, @sz MUST be
|
|
|
|
|
* less or equal than PAGE_SIZE.
|
|
|
|
|
*/
|
2014-08-27 20:49:34 -07:00
|
|
|
bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
|
2012-09-23 23:04:42 +00:00
|
|
|
{
|
|
|
|
|
if (pfrag->page) {
|
2016-03-17 14:19:26 -07:00
|
|
|
if (page_ref_count(pfrag->page) == 1) {
|
2012-09-23 23:04:42 +00:00
|
|
|
pfrag->offset = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-10-17 16:27:07 -07:00
|
|
|
if (pfrag->offset + sz <= pfrag->size)
|
2012-09-23 23:04:42 +00:00
|
|
|
return true;
|
|
|
|
|
put_page(pfrag->page);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 20:49:34 -07:00
|
|
|
pfrag->offset = 0;
|
2019-06-14 16:22:21 -07:00
|
|
|
if (SKB_FRAG_PAGE_ORDER &&
|
|
|
|
|
!static_branch_unlikely(&net_high_order_alloc_disable_key)) {
|
2015-11-06 16:28:21 -08:00
|
|
|
/* Avoid direct reclaim but allow kswapd to wake */
|
|
|
|
|
pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
|
|
|
|
|
__GFP_COMP | __GFP_NOWARN |
|
|
|
|
|
__GFP_NORETRY,
|
2014-08-27 20:49:34 -07:00
|
|
|
SKB_FRAG_PAGE_ORDER);
|
2012-09-23 23:04:42 +00:00
|
|
|
if (likely(pfrag->page)) {
|
2014-08-27 20:49:34 -07:00
|
|
|
pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
|
2012-09-23 23:04:42 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-27 20:49:34 -07:00
|
|
|
}
|
|
|
|
|
pfrag->page = alloc_page(gfp);
|
|
|
|
|
if (likely(pfrag->page)) {
|
|
|
|
|
pfrag->size = PAGE_SIZE;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-10-17 16:27:07 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(skb_page_frag_refill);
|
|
|
|
|
|
|
|
|
|
bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
|
|
|
|
|
{
|
|
|
|
|
if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-10-14 23:54:55 +00:00
|
|
|
if (!sk->sk_bypass_prot_mem)
|
|
|
|
|
sk_enter_memory_pressure(sk);
|
|
|
|
|
|
2012-09-23 23:04:42 +00:00
|
|
|
sk_stream_moderate_sndbuf(sk);
|
2025-10-14 23:54:55 +00:00
|
|
|
|
2012-09-23 23:04:42 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_page_frag_refill);
|
|
|
|
|
|
2020-11-27 11:10:22 +01:00
|
|
|
void __lock_sock(struct sock *sk)
|
2010-09-08 03:48:48 +00:00
|
|
|
__releases(&sk->sk_lock.slock)
|
|
|
|
|
__acquires(&sk->sk_lock.slock)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
for (;;) {
|
2005-04-16 15:20:36 -07:00
|
|
|
prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
|
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
|
|
|
|
spin_unlock_bh(&sk->sk_lock.slock);
|
|
|
|
|
schedule();
|
|
|
|
|
spin_lock_bh(&sk->sk_lock.slock);
|
2007-04-10 20:10:33 -07:00
|
|
|
if (!sock_owned_by_user(sk))
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
finish_wait(&sk->sk_lock.wq, &wait);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-01 23:24:26 -07:00
|
|
|
void __release_sock(struct sock *sk)
|
2010-09-08 03:48:48 +00:00
|
|
|
__releases(&sk->sk_lock.slock)
|
|
|
|
|
__acquires(&sk->sk_lock.slock)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2016-04-29 14:16:52 -07:00
|
|
|
struct sk_buff *skb, *next;
|
2025-09-03 17:48:10 +00:00
|
|
|
int nb = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2016-04-29 14:16:52 -07:00
|
|
|
while ((skb = sk->sk_backlog.head) != NULL) {
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
|
2016-04-29 14:16:52 -07:00
|
|
|
|
|
|
|
|
spin_unlock_bh(&sk->sk_lock.slock);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2025-09-03 17:48:10 +00:00
|
|
|
while (1) {
|
2016-04-29 14:16:52 -07:00
|
|
|
next = skb->next;
|
2012-04-30 16:07:09 +00:00
|
|
|
prefetch(next);
|
2022-06-08 09:04:31 -07:00
|
|
|
DEBUG_NET_WARN_ON_ONCE(skb_dst_is_noref(skb));
|
2018-07-29 20:42:53 -07:00
|
|
|
skb_mark_not_on_list(skb);
|
2008-10-07 14:18:42 -07:00
|
|
|
sk_backlog_rcv(sk, skb);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
skb = next;
|
2025-09-03 17:48:10 +00:00
|
|
|
if (!skb)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (!(++nb & 15))
|
|
|
|
|
cond_resched();
|
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2016-04-29 14:16:52 -07:00
|
|
|
spin_lock_bh(&sk->sk_lock.slock);
|
|
|
|
|
}
|
2010-03-04 18:01:40 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Doing the zeroing here guarantee we can not loop forever
|
|
|
|
|
* while a wild producer attempts to flood us.
|
|
|
|
|
*/
|
|
|
|
|
sk->sk_backlog.len = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 14:16:53 -07:00
|
|
|
void __sk_flush_backlog(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
spin_lock_bh(&sk->sk_lock.slock);
|
|
|
|
|
__release_sock(sk);
|
2023-09-11 17:05:30 +00:00
|
|
|
|
|
|
|
|
if (sk->sk_prot->release_cb)
|
2023-09-13 12:58:35 +00:00
|
|
|
INDIRECT_CALL_INET_1(sk->sk_prot->release_cb,
|
|
|
|
|
tcp_release_cb, sk);
|
|
|
|
|
|
2016-04-29 14:16:53 -07:00
|
|
|
spin_unlock_bh(&sk->sk_lock.slock);
|
|
|
|
|
}
|
2022-07-05 16:59:26 -07:00
|
|
|
EXPORT_SYMBOL_GPL(__sk_flush_backlog);
|
2016-04-29 14:16:53 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/**
|
|
|
|
|
* sk_wait_data - wait for data to arrive at sk_receive_queue
|
2005-05-01 08:59:25 -07:00
|
|
|
* @sk: sock to wait on
|
|
|
|
|
* @timeo: for how long
|
2015-07-24 18:19:25 +02:00
|
|
|
* @skb: last skb seen on sk_receive_queue
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
|
* Now socket state including sk->sk_err is changed only under lock,
|
|
|
|
|
* hence we may omit checks after joining wait queue.
|
|
|
|
|
* We check receive queue before schedule() only as optimization;
|
|
|
|
|
* it is very likely that release_sock() added new data.
|
|
|
|
|
*/
|
2015-07-24 18:19:25 +02:00
|
|
|
int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2016-11-11 10:20:50 -08:00
|
|
|
DEFINE_WAIT_FUNC(wait, woken_wake_function);
|
2005-04-16 15:20:36 -07:00
|
|
|
int rc;
|
|
|
|
|
|
2016-11-11 10:20:50 -08:00
|
|
|
add_wait_queue(sk_sleep(sk), &wait);
|
2015-11-29 20:03:10 -08:00
|
|
|
sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
|
2016-11-11 10:20:50 -08:00
|
|
|
rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait);
|
2015-11-29 20:03:10 -08:00
|
|
|
sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
|
2016-11-11 10:20:50 -08:00
|
|
|
remove_wait_queue(sk_sleep(sk), &wait);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_wait_data);
|
|
|
|
|
|
2007-12-31 00:11:19 -08:00
|
|
|
/**
|
2016-10-21 13:55:45 +02:00
|
|
|
* __sk_mem_raise_allocated - increase memory_allocated
|
2007-12-31 00:11:19 -08:00
|
|
|
* @sk: socket
|
|
|
|
|
* @size: memory size to allocate
|
2016-10-21 13:55:45 +02:00
|
|
|
* @amt: pages to allocate
|
2007-12-31 00:11:19 -08:00
|
|
|
* @kind: allocation type
|
|
|
|
|
*
|
2023-10-19 20:00:26 +08:00
|
|
|
* Similar to __sk_mem_schedule(), but does not update sk_forward_alloc.
|
|
|
|
|
*
|
|
|
|
|
* Unlike the globally shared limits among the sockets under same protocol,
|
|
|
|
|
* consuming the budget of a memcg won't have direct effect on other ones.
|
|
|
|
|
* So be optimistic about memcg's tolerance, and leave the callers to decide
|
|
|
|
|
* whether or not to raise allocated through sk_under_memory_pressure() or
|
|
|
|
|
* its variants.
|
2007-12-31 00:11:19 -08:00
|
|
|
*/
|
2016-10-21 13:55:45 +02:00
|
|
|
int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
|
2007-12-31 00:11:19 -08:00
|
|
|
{
|
2025-08-15 20:16:16 +00:00
|
|
|
bool memcg_enabled = false, charged = false;
|
2022-06-10 20:30:16 -07:00
|
|
|
struct proto *prot = sk->sk_prot;
|
2025-10-14 23:54:55 +00:00
|
|
|
long allocated = 0;
|
2016-01-14 15:21:14 -08:00
|
|
|
|
2025-10-14 23:54:55 +00:00
|
|
|
if (!sk->sk_bypass_prot_mem) {
|
|
|
|
|
sk_memory_allocated_add(sk, amt);
|
|
|
|
|
allocated = sk_memory_allocated(sk);
|
|
|
|
|
}
|
2023-10-19 20:00:24 +08:00
|
|
|
|
2025-08-15 20:16:15 +00:00
|
|
|
if (mem_cgroup_sk_enabled(sk)) {
|
2025-08-15 20:16:16 +00:00
|
|
|
memcg_enabled = true;
|
|
|
|
|
charged = mem_cgroup_sk_charge(sk, amt, gfp_memcg_charge());
|
2025-05-27 11:04:19 +08:00
|
|
|
if (!charged)
|
2023-10-19 20:00:24 +08:00
|
|
|
goto suppress_allocation;
|
|
|
|
|
}
|
2007-12-31 00:11:19 -08:00
|
|
|
|
2025-10-14 23:54:55 +00:00
|
|
|
if (!allocated)
|
|
|
|
|
return 1;
|
|
|
|
|
|
2007-12-31 00:11:19 -08:00
|
|
|
/* Under limit. */
|
2016-01-14 15:21:14 -08:00
|
|
|
if (allocated <= sk_prot_mem_limits(sk, 0)) {
|
2011-12-11 21:47:02 +00:00
|
|
|
sk_leave_memory_pressure(sk);
|
2007-12-31 00:11:19 -08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-14 15:21:14 -08:00
|
|
|
/* Under pressure. */
|
|
|
|
|
if (allocated > sk_prot_mem_limits(sk, 1))
|
2011-12-11 21:47:02 +00:00
|
|
|
sk_enter_memory_pressure(sk);
|
2007-12-31 00:11:19 -08:00
|
|
|
|
2016-01-14 15:21:14 -08:00
|
|
|
/* Over hard limit. */
|
|
|
|
|
if (allocated > sk_prot_mem_limits(sk, 2))
|
2007-12-31 00:11:19 -08:00
|
|
|
goto suppress_allocation;
|
|
|
|
|
|
2023-10-19 20:00:25 +08:00
|
|
|
/* Guarantee minimum buffer size under pressure (either global
|
|
|
|
|
* or memcg) to make sure features described in RFC 7323 (TCP
|
|
|
|
|
* Extensions for High Performance) work properly.
|
|
|
|
|
*
|
|
|
|
|
* This rule does NOT stand when exceeds global or memcg's hard
|
|
|
|
|
* limit, or else a DoS attack can be taken place by spawning
|
|
|
|
|
* lots of sockets whose usage are under minimum buffer size.
|
|
|
|
|
*/
|
2007-12-31 00:11:19 -08:00
|
|
|
if (kind == SK_MEM_RECV) {
|
2017-11-07 00:29:27 -08:00
|
|
|
if (atomic_read(&sk->sk_rmem_alloc) < sk_get_rmem0(sk, prot))
|
2007-12-31 00:11:19 -08:00
|
|
|
return 1;
|
2011-12-11 21:47:02 +00:00
|
|
|
|
2007-12-31 00:11:19 -08:00
|
|
|
} else { /* SK_MEM_SEND */
|
2017-11-07 00:29:27 -08:00
|
|
|
int wmem0 = sk_get_wmem0(sk, prot);
|
|
|
|
|
|
2007-12-31 00:11:19 -08:00
|
|
|
if (sk->sk_type == SOCK_STREAM) {
|
2017-11-07 00:29:27 -08:00
|
|
|
if (sk->sk_wmem_queued < wmem0)
|
2007-12-31 00:11:19 -08:00
|
|
|
return 1;
|
2017-11-07 00:29:27 -08:00
|
|
|
} else if (refcount_read(&sk->sk_wmem_alloc) < wmem0) {
|
2007-12-31 00:11:19 -08:00
|
|
|
return 1;
|
2017-11-07 00:29:27 -08:00
|
|
|
}
|
2007-12-31 00:11:19 -08:00
|
|
|
}
|
|
|
|
|
|
2011-12-11 21:47:02 +00:00
|
|
|
if (sk_has_memory_pressure(sk)) {
|
2019-02-12 12:26:27 -08:00
|
|
|
u64 alloc;
|
2008-11-25 21:16:35 -08:00
|
|
|
|
2023-10-19 20:00:26 +08:00
|
|
|
/* The following 'average' heuristic is within the
|
|
|
|
|
* scope of global accounting, so it only makes
|
|
|
|
|
* sense for global memory pressure.
|
|
|
|
|
*/
|
|
|
|
|
if (!sk_under_global_memory_pressure(sk))
|
2008-11-25 21:16:35 -08:00
|
|
|
return 1;
|
2023-10-19 20:00:25 +08:00
|
|
|
|
|
|
|
|
/* Try to be fair among all the sockets under global
|
|
|
|
|
* pressure by allowing the ones that below average
|
|
|
|
|
* usage to raise.
|
|
|
|
|
*/
|
2011-12-11 21:47:02 +00:00
|
|
|
alloc = sk_sockets_allocated_read_positive(sk);
|
|
|
|
|
if (sk_prot_mem_limits(sk, 2) > alloc *
|
2007-12-31 00:11:19 -08:00
|
|
|
sk_mem_pages(sk->sk_wmem_queued +
|
|
|
|
|
atomic_read(&sk->sk_rmem_alloc) +
|
|
|
|
|
sk->sk_forward_alloc))
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suppress_allocation:
|
|
|
|
|
|
|
|
|
|
if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
|
|
|
|
|
sk_stream_moderate_sndbuf(sk);
|
|
|
|
|
|
|
|
|
|
/* Fail only if socket is _under_ its sndbuf.
|
|
|
|
|
* In this case we cannot block, so that we have to fail.
|
|
|
|
|
*/
|
2021-08-17 12:40:03 -07:00
|
|
|
if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) {
|
|
|
|
|
/* Force charge with __GFP_NOFAIL */
|
2025-08-15 20:16:16 +00:00
|
|
|
if (memcg_enabled && !charged)
|
|
|
|
|
mem_cgroup_sk_charge(sk, amt,
|
|
|
|
|
gfp_memcg_charge() | __GFP_NOFAIL);
|
2007-12-31 00:11:19 -08:00
|
|
|
return 1;
|
2021-08-17 12:40:03 -07:00
|
|
|
}
|
2007-12-31 00:11:19 -08:00
|
|
|
}
|
|
|
|
|
|
2025-08-15 20:16:12 +00:00
|
|
|
trace_sock_exceed_buf_limit(sk, prot, allocated, kind);
|
2011-06-17 12:00:03 +00:00
|
|
|
|
2025-10-14 23:54:55 +00:00
|
|
|
if (allocated)
|
|
|
|
|
sk_memory_allocated_sub(sk, amt);
|
2011-12-11 21:47:02 +00:00
|
|
|
|
2025-08-15 20:16:13 +00:00
|
|
|
if (charged)
|
2025-08-15 20:16:16 +00:00
|
|
|
mem_cgroup_sk_uncharge(sk, amt);
|
2016-01-14 15:21:14 -08:00
|
|
|
|
2007-12-31 00:11:19 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-10-21 13:55:45 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
|
|
|
|
|
* @sk: socket
|
|
|
|
|
* @size: memory size to allocate
|
|
|
|
|
* @kind: allocation type
|
|
|
|
|
*
|
|
|
|
|
* If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
|
|
|
|
|
* rmem allocation. This function assumes that protocols which have
|
|
|
|
|
* memory_pressure use sk_wmem_queued as write buffer accounting.
|
|
|
|
|
*/
|
|
|
|
|
int __sk_mem_schedule(struct sock *sk, int size, int kind)
|
|
|
|
|
{
|
|
|
|
|
int ret, amt = sk_mem_pages(size);
|
|
|
|
|
|
2023-08-31 13:52:09 +00:00
|
|
|
sk_forward_alloc_add(sk, amt << PAGE_SHIFT);
|
2016-10-21 13:55:45 +02:00
|
|
|
ret = __sk_mem_raise_allocated(sk, size, amt, kind);
|
|
|
|
|
if (!ret)
|
2023-08-31 13:52:09 +00:00
|
|
|
sk_forward_alloc_add(sk, -(amt << PAGE_SHIFT));
|
2016-10-21 13:55:45 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
2007-12-31 00:11:19 -08:00
|
|
|
EXPORT_SYMBOL(__sk_mem_schedule);
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-21 13:55:45 +02:00
|
|
|
* __sk_mem_reduce_allocated - reclaim memory_allocated
|
2007-12-31 00:11:19 -08:00
|
|
|
* @sk: socket
|
2016-10-21 13:55:45 +02:00
|
|
|
* @amount: number of quanta
|
|
|
|
|
*
|
|
|
|
|
* Similar to __sk_mem_reclaim(), but does not update sk_forward_alloc
|
2007-12-31 00:11:19 -08:00
|
|
|
*/
|
2016-10-21 13:55:45 +02:00
|
|
|
void __sk_mem_reduce_allocated(struct sock *sk, int amount)
|
2007-12-31 00:11:19 -08:00
|
|
|
{
|
2025-08-15 20:16:15 +00:00
|
|
|
if (mem_cgroup_sk_enabled(sk))
|
2025-08-15 20:16:16 +00:00
|
|
|
mem_cgroup_sk_uncharge(sk, amount);
|
2016-01-14 15:21:14 -08:00
|
|
|
|
2025-10-14 23:54:55 +00:00
|
|
|
if (sk->sk_bypass_prot_mem)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
sk_memory_allocated_sub(sk, amount);
|
|
|
|
|
|
2023-08-16 17:12:22 +08:00
|
|
|
if (sk_under_global_memory_pressure(sk) &&
|
2011-12-11 21:47:02 +00:00
|
|
|
(sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
|
|
|
|
|
sk_leave_memory_pressure(sk);
|
2007-12-31 00:11:19 -08:00
|
|
|
}
|
2016-10-21 13:55:45 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* __sk_mem_reclaim - reclaim sk_forward_alloc and memory_allocated
|
|
|
|
|
* @sk: socket
|
2022-06-08 23:34:07 -07:00
|
|
|
* @amount: number of bytes (rounded down to a PAGE_SIZE multiple)
|
2016-10-21 13:55:45 +02:00
|
|
|
*/
|
|
|
|
|
void __sk_mem_reclaim(struct sock *sk, int amount)
|
|
|
|
|
{
|
2022-06-08 23:34:07 -07:00
|
|
|
amount >>= PAGE_SHIFT;
|
2023-08-31 13:52:09 +00:00
|
|
|
sk_forward_alloc_add(sk, -(amount << PAGE_SHIFT));
|
2016-10-21 13:55:45 +02:00
|
|
|
__sk_mem_reduce_allocated(sk, amount);
|
|
|
|
|
}
|
2007-12-31 00:11:19 -08:00
|
|
|
EXPORT_SYMBOL(__sk_mem_reclaim);
|
|
|
|
|
|
2016-04-05 12:41:16 -04:00
|
|
|
int sk_set_peek_off(struct sock *sk, int val)
|
|
|
|
|
{
|
2023-07-28 15:03:16 +00:00
|
|
|
WRITE_ONCE(sk->sk_peek_off, val);
|
2016-04-05 12:41:16 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(sk_set_peek_off);
|
2007-12-31 00:11:19 -08:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Set of default routines for initialising struct proto_ops when
|
|
|
|
|
* the protocol does not support a particular function. In certain
|
|
|
|
|
* cases where it makes no sense for a protocol to have a "do nothing"
|
|
|
|
|
* function, some default processing is provided.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-03 16:26:10 -08:00
|
|
|
int sock_no_bind(struct socket *sock, struct sockaddr_unsized *saddr, int len)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_bind);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2025-11-03 16:26:11 -08:00
|
|
|
int sock_no_connect(struct socket *sock, struct sockaddr_unsized *saddr,
|
2005-04-16 15:20:36 -07:00
|
|
|
int len, int flags)
|
|
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_connect);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
|
|
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_socketpair);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2024-05-09 09:20:08 -06:00
|
|
|
int sock_no_accept(struct socket *sock, struct socket *newsock,
|
|
|
|
|
struct proto_accept_arg *arg)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_accept);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-02-09 23:24:36 +09:00
|
|
|
int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
|
2018-02-12 20:00:20 +01:00
|
|
|
int peer)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_getname);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
|
|
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_ioctl);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int sock_no_listen(struct socket *sock, int backlog)
|
|
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_listen);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int sock_no_shutdown(struct socket *sock, int how)
|
|
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_shutdown);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2015-03-02 15:37:48 +08:00
|
|
|
int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_sendmsg);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2017-07-28 16:22:41 -07:00
|
|
|
int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len)
|
|
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_no_sendmsg_locked);
|
|
|
|
|
|
2015-03-02 15:37:48 +08:00
|
|
|
int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
|
|
|
|
|
int flags)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_recvmsg);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
|
|
|
|
|
{
|
|
|
|
|
/* Mirror missing mmap method error code */
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_no_mmap);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2020-06-09 16:11:29 -07:00
|
|
|
/*
|
|
|
|
|
* When a file is received (via SCM_RIGHTS, etc), we must bump the
|
|
|
|
|
* various sock-based usage counts.
|
|
|
|
|
*/
|
|
|
|
|
void __receive_sock(struct file *file)
|
|
|
|
|
{
|
|
|
|
|
struct socket *sock;
|
|
|
|
|
|
2020-12-04 12:36:04 +01:00
|
|
|
sock = sock_from_file(file);
|
2020-06-09 16:11:29 -07:00
|
|
|
if (sock) {
|
|
|
|
|
sock_update_netprioidx(&sock->sk->sk_cgrp_data);
|
|
|
|
|
sock_update_classid(&sock->sk->sk_cgrp_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Default Socket Callbacks
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void sock_def_wakeup(struct sock *sk)
|
|
|
|
|
{
|
2010-04-29 11:01:49 +00:00
|
|
|
struct socket_wq *wq;
|
|
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
|
wq = rcu_dereference(sk->sk_wq);
|
2015-11-26 13:55:39 +08:00
|
|
|
if (skwq_has_sleeper(wq))
|
2010-04-29 11:01:49 +00:00
|
|
|
wake_up_interruptible_all(&wq->wait);
|
|
|
|
|
rcu_read_unlock();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sock_def_error_report(struct sock *sk)
|
|
|
|
|
{
|
2010-04-29 11:01:49 +00:00
|
|
|
struct socket_wq *wq;
|
|
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
|
wq = rcu_dereference(sk->sk_wq);
|
2015-11-26 13:55:39 +08:00
|
|
|
if (skwq_has_sleeper(wq))
|
2018-02-11 14:34:03 -08:00
|
|
|
wake_up_interruptible_poll(&wq->wait, EPOLLERR);
|
2024-03-28 14:40:32 +00:00
|
|
|
sk_wake_async_rcu(sk, SOCK_WAKE_IO, POLL_ERR);
|
2010-04-29 11:01:49 +00:00
|
|
|
rcu_read_unlock();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2020-01-20 10:29:17 +01:00
|
|
|
void sock_def_readable(struct sock *sk)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2010-04-29 11:01:49 +00:00
|
|
|
struct socket_wq *wq;
|
|
|
|
|
|
2023-01-19 16:45:16 -08:00
|
|
|
trace_sk_data_ready(sk);
|
|
|
|
|
|
2010-04-29 11:01:49 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
|
wq = rcu_dereference(sk->sk_wq);
|
2015-11-26 13:55:39 +08:00
|
|
|
if (skwq_has_sleeper(wq))
|
2018-02-11 14:34:03 -08:00
|
|
|
wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
|
|
|
|
|
EPOLLRDNORM | EPOLLRDBAND);
|
2024-03-28 14:40:32 +00:00
|
|
|
sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN);
|
2010-04-29 11:01:49 +00:00
|
|
|
rcu_read_unlock();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sock_def_write_space(struct sock *sk)
|
|
|
|
|
{
|
2010-04-29 11:01:49 +00:00
|
|
|
struct socket_wq *wq;
|
|
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
/* Do not wake up a writer until he can make "significant"
|
|
|
|
|
* progress. --DaveM
|
|
|
|
|
*/
|
2022-04-28 11:58:17 +01:00
|
|
|
if (sock_writeable(sk)) {
|
2010-04-29 11:01:49 +00:00
|
|
|
wq = rcu_dereference(sk->sk_wq);
|
2015-11-26 13:55:39 +08:00
|
|
|
if (skwq_has_sleeper(wq))
|
2018-02-11 14:34:03 -08:00
|
|
|
wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
|
|
|
|
|
EPOLLWRNORM | EPOLLWRBAND);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
/* Should agree with poll, otherwise some programs break */
|
2024-03-28 14:40:32 +00:00
|
|
|
sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2010-04-29 11:01:49 +00:00
|
|
|
rcu_read_unlock();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-28 11:58:19 +01:00
|
|
|
/* An optimised version of sock_def_write_space(), should only be called
|
|
|
|
|
* for SOCK_RCU_FREE sockets under RCU read section and after putting
|
|
|
|
|
* ->sk_wmem_alloc.
|
|
|
|
|
*/
|
2025-10-17 13:37:12 +00:00
|
|
|
static void sock_def_write_space_wfree(struct sock *sk, int wmem_alloc)
|
2022-04-28 11:58:19 +01:00
|
|
|
{
|
|
|
|
|
/* Do not wake up a writer until he can make "significant"
|
|
|
|
|
* progress. --DaveM
|
|
|
|
|
*/
|
2025-10-17 13:37:12 +00:00
|
|
|
if (__sock_writeable(sk, wmem_alloc)) {
|
2022-04-28 11:58:19 +01:00
|
|
|
struct socket_wq *wq = rcu_dereference(sk->sk_wq);
|
|
|
|
|
|
|
|
|
|
/* rely on refcount_sub from sock_wfree() */
|
|
|
|
|
smp_mb__after_atomic();
|
|
|
|
|
if (wq && waitqueue_active(&wq->wait))
|
|
|
|
|
wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
|
|
|
|
|
EPOLLWRNORM | EPOLLWRBAND);
|
|
|
|
|
|
|
|
|
|
/* Should agree with poll, otherwise some programs break */
|
2024-03-28 14:40:32 +00:00
|
|
|
sk_wake_async_rcu(sk, SOCK_WAKE_SPACE, POLL_OUT);
|
2022-04-28 11:58:19 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
static void sock_def_destruct(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sk_send_sigurg(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
if (sk->sk_socket && sk->sk_socket->file)
|
2024-08-09 18:00:01 +02:00
|
|
|
if (send_sigurg(sk->sk_socket->file))
|
2007-11-26 20:10:50 +08:00
|
|
|
sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sk_send_sigurg);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
void sk_reset_timer(struct sock *sk, struct timer_list* timer,
|
|
|
|
|
unsigned long expires)
|
|
|
|
|
{
|
|
|
|
|
if (!mod_timer(timer, expires))
|
|
|
|
|
sock_hold(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_reset_timer);
|
|
|
|
|
|
|
|
|
|
void sk_stop_timer(struct sock *sk, struct timer_list* timer)
|
|
|
|
|
{
|
2025-04-05 10:17:26 +02:00
|
|
|
if (timer_delete(timer))
|
2005-04-16 15:20:36 -07:00
|
|
|
__sock_put(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_stop_timer);
|
|
|
|
|
|
2020-09-24 08:30:01 +08:00
|
|
|
void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer)
|
|
|
|
|
{
|
2025-04-05 10:17:26 +02:00
|
|
|
if (timer_delete_sync(timer))
|
2020-09-24 08:30:01 +08:00
|
|
|
__sock_put(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_stop_timer_sync);
|
|
|
|
|
|
2023-02-04 17:39:20 +00:00
|
|
|
void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2017-03-09 13:54:08 +01:00
|
|
|
sk_init_common(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_send_head = NULL;
|
|
|
|
|
|
2017-10-16 17:29:36 -07:00
|
|
|
timer_setup(&sk->sk_timer, NULL, 0);
|
2007-02-09 23:24:36 +09:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_allocation = GFP_KERNEL;
|
2022-08-23 10:46:44 -07:00
|
|
|
sk->sk_rcvbuf = READ_ONCE(sysctl_rmem_default);
|
|
|
|
|
sk->sk_sndbuf = READ_ONCE(sysctl_wmem_default);
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_state = TCP_CLOSE;
|
2022-12-16 07:45:26 -05:00
|
|
|
sk->sk_use_task_frag = true;
|
2008-06-17 22:41:38 -07:00
|
|
|
sk_set_socket(sk, sock);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
sock_set_flag(sk, SOCK_ZAPPED);
|
|
|
|
|
|
2007-04-10 20:10:33 -07:00
|
|
|
if (sock) {
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_type = sock->type;
|
2019-07-05 20:14:16 +01:00
|
|
|
RCU_INIT_POINTER(sk->sk_wq, &sock->wq);
|
2005-04-16 15:20:36 -07:00
|
|
|
sock->sk = sk;
|
2016-11-04 02:23:41 +09:00
|
|
|
} else {
|
2019-02-22 17:08:22 +08:00
|
|
|
RCU_INIT_POINTER(sk->sk_wq, NULL);
|
2016-11-04 02:23:41 +09:00
|
|
|
}
|
2023-02-04 17:39:20 +00:00
|
|
|
sk->sk_uid = uid;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
sk->sk_state_change = sock_def_wakeup;
|
|
|
|
|
sk->sk_data_ready = sock_def_readable;
|
|
|
|
|
sk->sk_write_space = sock_def_write_space;
|
|
|
|
|
sk->sk_error_report = sock_def_error_report;
|
|
|
|
|
sk->sk_destruct = sock_def_destruct;
|
|
|
|
|
|
2012-09-23 23:04:42 +00:00
|
|
|
sk->sk_frag.page = NULL;
|
|
|
|
|
sk->sk_frag.offset = 0;
|
2012-02-21 07:31:34 +00:00
|
|
|
sk->sk_peek_off = -1;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2010-06-13 03:30:14 +00:00
|
|
|
sk->sk_peer_pid = NULL;
|
|
|
|
|
sk->sk_peer_cred = NULL;
|
2021-09-29 15:57:50 -07:00
|
|
|
spin_lock_init(&sk->sk_peer_lock);
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
sk->sk_write_pending = 0;
|
|
|
|
|
sk->sk_rcvlowat = 1;
|
|
|
|
|
sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
|
|
|
|
|
sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
|
|
|
|
|
|
2017-03-30 14:03:06 +02:00
|
|
|
sk->sk_stamp = SK_DEFAULT_STAMP;
|
2018-12-27 18:55:09 -08:00
|
|
|
#if BITS_PER_LONG==32
|
|
|
|
|
seqlock_init(&sk->sk_stamp_seq);
|
|
|
|
|
#endif
|
2017-08-03 16:29:39 -04:00
|
|
|
atomic_set(&sk->sk_zckey, 0);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-08-01 11:10:25 +08:00
|
|
|
#ifdef CONFIG_NET_RX_BUSY_POLL
|
2013-06-10 11:39:50 +03:00
|
|
|
sk->sk_napi_id = 0;
|
2022-08-23 10:46:52 -07:00
|
|
|
sk->sk_ll_usec = READ_ONCE(sysctl_net_busy_read);
|
2013-06-10 11:39:50 +03:00
|
|
|
#endif
|
|
|
|
|
|
2018-10-15 09:37:53 -07:00
|
|
|
sk->sk_max_pacing_rate = ~0UL;
|
|
|
|
|
sk->sk_pacing_rate = ~0UL;
|
2019-12-16 18:51:03 -08:00
|
|
|
WRITE_ONCE(sk->sk_pacing_shift, 10);
|
2015-10-08 19:33:21 -07:00
|
|
|
sk->sk_incoming_cpu = -1;
|
2018-06-29 21:26:57 -07:00
|
|
|
|
|
|
|
|
sk_rx_queue_clear(sk);
|
2009-07-15 23:13:10 +00:00
|
|
|
/*
|
|
|
|
|
* Before updating sk_refcnt, we must commit prior changes to memory
|
2020-04-21 19:04:05 +02:00
|
|
|
* (Documentation/RCU/rculist_nulls.rst for details)
|
2009-07-15 23:13:10 +00:00
|
|
|
*/
|
|
|
|
|
smp_wmb();
|
2017-06-30 13:08:01 +03:00
|
|
|
refcount_set(&sk->sk_refcnt, 1);
|
2025-08-26 12:50:27 +00:00
|
|
|
sk_drops_reset(sk);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2023-02-04 17:39:20 +00:00
|
|
|
EXPORT_SYMBOL(sock_init_data_uid);
|
|
|
|
|
|
|
|
|
|
void sock_init_data(struct socket *sock, struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
kuid_t uid = sock ?
|
|
|
|
|
SOCK_INODE(sock)->i_uid :
|
|
|
|
|
make_kuid(sock_net(sk)->user_ns, 0);
|
|
|
|
|
|
|
|
|
|
sock_init_data_uid(sock, sk, uid);
|
|
|
|
|
}
|
2009-05-27 11:30:05 +00:00
|
|
|
EXPORT_SYMBOL(sock_init_data);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-02-13 15:03:16 -08:00
|
|
|
void lock_sock_nested(struct sock *sk, int subclass)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2021-09-18 14:42:35 +02:00
|
|
|
/* The sk_lock has mutex_lock() semantics here. */
|
|
|
|
|
mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
might_sleep();
|
2006-07-03 00:25:35 -07:00
|
|
|
spin_lock_bh(&sk->sk_lock.slock);
|
2021-12-08 15:21:58 +09:00
|
|
|
if (sock_owned_by_user_nocheck(sk))
|
2005-04-16 15:20:36 -07:00
|
|
|
__lock_sock(sk);
|
2007-09-12 10:44:19 +02:00
|
|
|
sk->sk_lock.owned = 1;
|
2021-09-18 14:42:35 +02:00
|
|
|
spin_unlock_bh(&sk->sk_lock.slock);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2006-11-08 22:44:35 -08:00
|
|
|
EXPORT_SYMBOL(lock_sock_nested);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-02-13 15:03:16 -08:00
|
|
|
void release_sock(struct sock *sk)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2006-07-03 00:25:35 -07:00
|
|
|
spin_lock_bh(&sk->sk_lock.slock);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (sk->sk_backlog.tail)
|
|
|
|
|
__release_sock(sk);
|
2012-07-11 05:50:31 +00:00
|
|
|
|
|
|
|
|
if (sk->sk_prot->release_cb)
|
2023-09-13 12:58:35 +00:00
|
|
|
INDIRECT_CALL_INET_1(sk->sk_prot->release_cb,
|
|
|
|
|
tcp_release_cb, sk);
|
2012-07-11 05:50:31 +00:00
|
|
|
|
2014-03-10 09:50:11 -07:00
|
|
|
sock_release_ownership(sk);
|
2006-07-03 00:25:35 -07:00
|
|
|
if (waitqueue_active(&sk->sk_lock.wq))
|
|
|
|
|
wake_up(&sk->sk_lock.wq);
|
|
|
|
|
spin_unlock_bh(&sk->sk_lock.slock);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(release_sock);
|
|
|
|
|
|
2021-09-29 11:59:17 +02:00
|
|
|
bool __lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock)
|
2010-05-26 19:20:18 +00:00
|
|
|
{
|
|
|
|
|
might_sleep();
|
|
|
|
|
spin_lock_bh(&sk->sk_lock.slock);
|
|
|
|
|
|
2021-12-08 15:21:58 +09:00
|
|
|
if (!sock_owned_by_user_nocheck(sk)) {
|
2010-05-26 19:20:18 +00:00
|
|
|
/*
|
2021-09-18 14:42:35 +02:00
|
|
|
* Fast path return with bottom halves disabled and
|
|
|
|
|
* sock::sk_lock.slock held.
|
|
|
|
|
*
|
|
|
|
|
* The 'mutex' is not contended and holding
|
|
|
|
|
* sock::sk_lock.slock prevents all other lockers to
|
|
|
|
|
* proceed so the corresponding unlock_sock_fast() can
|
|
|
|
|
* avoid the slow path of release_sock() completely and
|
|
|
|
|
* just release slock.
|
|
|
|
|
*
|
|
|
|
|
* From a semantical POV this is equivalent to 'acquiring'
|
|
|
|
|
* the 'mutex', hence the corresponding lockdep
|
|
|
|
|
* mutex_release() has to happen in the fast path of
|
|
|
|
|
* unlock_sock_fast().
|
2010-05-26 19:20:18 +00:00
|
|
|
*/
|
|
|
|
|
return false;
|
2021-09-18 14:42:35 +02:00
|
|
|
}
|
2010-05-26 19:20:18 +00:00
|
|
|
|
|
|
|
|
__lock_sock(sk);
|
|
|
|
|
sk->sk_lock.owned = 1;
|
2020-11-17 19:43:49 +01:00
|
|
|
__acquire(&sk->sk_lock.slock);
|
2021-09-18 14:42:35 +02:00
|
|
|
spin_unlock_bh(&sk->sk_lock.slock);
|
2010-05-26 19:20:18 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2021-09-29 11:59:17 +02:00
|
|
|
EXPORT_SYMBOL(__lock_sock_fast);
|
2010-05-26 19:20:18 +00:00
|
|
|
|
2019-04-17 22:51:48 +02:00
|
|
|
int sock_gettstamp(struct socket *sock, void __user *userstamp,
|
|
|
|
|
bool timeval, bool time32)
|
2007-02-09 23:24:36 +09:00
|
|
|
{
|
2019-04-17 22:51:48 +02:00
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
struct timespec64 ts;
|
2018-08-06 11:57:02 +08:00
|
|
|
|
|
|
|
|
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
|
2019-04-17 22:51:48 +02:00
|
|
|
ts = ktime_to_timespec64(sock_read_timestamp(sk));
|
2007-03-18 17:33:16 -07:00
|
|
|
if (ts.tv_sec == -1)
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
if (ts.tv_sec == 0) {
|
2018-12-27 18:55:09 -08:00
|
|
|
ktime_t kt = ktime_get_real();
|
2019-10-25 17:18:36 +08:00
|
|
|
sock_write_timestamp(sk, kt);
|
2019-04-17 22:51:48 +02:00
|
|
|
ts = ktime_to_timespec64(kt);
|
2007-03-18 17:33:16 -07:00
|
|
|
}
|
2019-04-17 22:51:48 +02:00
|
|
|
|
|
|
|
|
if (timeval)
|
|
|
|
|
ts.tv_nsec /= 1000;
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT_32BIT_TIME
|
|
|
|
|
if (time32)
|
|
|
|
|
return put_old_timespec32(&ts, userstamp);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef CONFIG_SPARC64
|
|
|
|
|
/* beware of padding in sparc64 timeval */
|
|
|
|
|
if (timeval && !in_compat_syscall()) {
|
|
|
|
|
struct __kernel_old_timeval __user tv = {
|
2019-04-23 17:25:24 +10:00
|
|
|
.tv_sec = ts.tv_sec,
|
|
|
|
|
.tv_usec = ts.tv_nsec,
|
2019-04-17 22:51:48 +02:00
|
|
|
};
|
2019-04-23 17:25:24 +10:00
|
|
|
if (copy_to_user(userstamp, &tv, sizeof(tv)))
|
2019-04-17 22:51:48 +02:00
|
|
|
return -EFAULT;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return put_timespec64(&ts, userstamp);
|
2007-03-18 17:33:16 -07:00
|
|
|
}
|
2019-04-17 22:51:48 +02:00
|
|
|
EXPORT_SYMBOL(sock_gettstamp);
|
2007-03-18 17:33:16 -07:00
|
|
|
|
2019-10-03 23:56:37 +03:00
|
|
|
void sock_enable_timestamp(struct sock *sk, enum sock_flags flag)
|
2007-02-09 23:24:36 +09:00
|
|
|
{
|
2009-02-12 05:03:38 +00:00
|
|
|
if (!sock_flag(sk, flag)) {
|
2011-11-28 12:04:18 +00:00
|
|
|
unsigned long previous_flags = sk->sk_flags;
|
|
|
|
|
|
2009-02-12 05:03:38 +00:00
|
|
|
sock_set_flag(sk, flag);
|
|
|
|
|
/*
|
|
|
|
|
* we just set one of the two flags which require net
|
|
|
|
|
* time stamping, but time stamping might have been on
|
|
|
|
|
* already because of the other one
|
|
|
|
|
*/
|
2015-10-26 13:51:37 +01:00
|
|
|
if (sock_needs_netstamp(sk) &&
|
|
|
|
|
!(previous_flags & SK_FLAGS_TIMESTAMP))
|
2009-02-12 05:03:38 +00:00
|
|
|
net_enable_timestamp();
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 19:40:09 +02:00
|
|
|
int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
|
|
|
|
|
int level, int type)
|
|
|
|
|
{
|
|
|
|
|
struct sock_exterr_skb *serr;
|
2014-08-31 21:30:27 -04:00
|
|
|
struct sk_buff *skb;
|
2013-07-19 19:40:09 +02:00
|
|
|
int copied, err;
|
|
|
|
|
|
|
|
|
|
err = -EAGAIN;
|
2014-08-31 21:30:27 -04:00
|
|
|
skb = sock_dequeue_err_skb(sk);
|
2013-07-19 19:40:09 +02:00
|
|
|
if (skb == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
copied = skb->len;
|
|
|
|
|
if (copied > len) {
|
|
|
|
|
msg->msg_flags |= MSG_TRUNC;
|
|
|
|
|
copied = len;
|
|
|
|
|
}
|
2014-11-05 16:46:40 -05:00
|
|
|
err = skb_copy_datagram_msg(skb, 0, msg, copied);
|
2013-07-19 19:40:09 +02:00
|
|
|
if (err)
|
|
|
|
|
goto out_free_skb;
|
|
|
|
|
|
|
|
|
|
sock_recv_timestamp(msg, sk, skb);
|
|
|
|
|
|
|
|
|
|
serr = SKB_EXT_ERR(skb);
|
|
|
|
|
put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
|
|
|
|
|
|
|
|
|
|
msg->msg_flags |= MSG_ERRQUEUE;
|
|
|
|
|
err = copied;
|
|
|
|
|
|
|
|
|
|
out_free_skb:
|
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
out:
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_recv_errqueue);
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Get a socket option on an socket.
|
|
|
|
|
*
|
|
|
|
|
* FIX: POSIX 1003.1g is very ambiguous here. It states that
|
|
|
|
|
* asynchronous errors should be reported by getsockopt. We assume
|
2024-08-22 13:57:34 +01:00
|
|
|
* this means if you specify SO_ERROR (otherwise what is the point of it).
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
|
|
|
|
int sock_common_getsockopt(struct socket *sock, int level, int optname,
|
|
|
|
|
char __user *optval, int __user *optlen)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
2022-10-06 11:53:48 -07:00
|
|
|
/* IPV6_ADDRFORM can change sk->sk_prot under us. */
|
|
|
|
|
return READ_ONCE(sk->sk_prot)->getsockopt(sk, level, optname, optval, optlen);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_common_getsockopt);
|
|
|
|
|
|
2015-03-02 15:37:48 +08:00
|
|
|
int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
|
|
|
|
|
int flags)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
int addr_len = 0;
|
|
|
|
|
int err;
|
|
|
|
|
|
2022-04-11 14:49:55 +02:00
|
|
|
err = sk->sk_prot->recvmsg(sk, msg, size, flags, &addr_len);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (err >= 0)
|
|
|
|
|
msg->msg_namelen = addr_len;
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_common_recvmsg);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Set socket options on an inet socket.
|
|
|
|
|
*/
|
|
|
|
|
int sock_common_setsockopt(struct socket *sock, int level, int optname,
|
2020-07-23 08:09:07 +02:00
|
|
|
sockptr_t optval, unsigned int optlen)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
2022-10-06 11:53:48 -07:00
|
|
|
/* IPV6_ADDRFORM can change sk->sk_prot under us. */
|
|
|
|
|
return READ_ONCE(sk->sk_prot)->setsockopt(sk, level, optname, optval, optlen);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_common_setsockopt);
|
|
|
|
|
|
|
|
|
|
void sk_common_release(struct sock *sk)
|
|
|
|
|
{
|
|
|
|
|
if (sk->sk_prot->destroy)
|
|
|
|
|
sk->sk_prot->destroy(sk);
|
|
|
|
|
|
|
|
|
|
/*
|
2020-08-27 07:27:49 -04:00
|
|
|
* Observation: when sk_common_release is called, processes have
|
2005-04-16 15:20:36 -07:00
|
|
|
* no access to socket. But net still has.
|
|
|
|
|
* Step one, detach it from networking:
|
|
|
|
|
*
|
|
|
|
|
* A. Remove from hash tables.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
sk->sk_prot->unhash(sk);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* In this point socket cannot receive new packets, but it is possible
|
|
|
|
|
* that some packets are in flight because some CPU runs receiver and
|
|
|
|
|
* did hash table lookup before we unhashed socket. They will achieve
|
|
|
|
|
* receive queue and will be purged by socket destructor.
|
|
|
|
|
*
|
|
|
|
|
* Also we still have packets pending on receive queue and probably,
|
|
|
|
|
* our own packets waiting in device queues. sock_destroy will drain
|
|
|
|
|
* receive queue, but transmitted packets will delay socket destruction
|
|
|
|
|
* until the last reference will be released.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
sock_orphan(sk);
|
|
|
|
|
|
|
|
|
|
xfrm_sk_free_policy(sk);
|
|
|
|
|
|
|
|
|
|
sock_put(sk);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_common_release);
|
|
|
|
|
|
2017-03-20 15:22:03 -04:00
|
|
|
void sk_get_meminfo(const struct sock *sk, u32 *mem)
|
|
|
|
|
{
|
|
|
|
|
memset(mem, 0, sizeof(*mem) * SK_MEMINFO_VARS);
|
|
|
|
|
|
|
|
|
|
mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
|
2019-10-10 20:17:44 -07:00
|
|
|
mem[SK_MEMINFO_RCVBUF] = READ_ONCE(sk->sk_rcvbuf);
|
2017-03-20 15:22:03 -04:00
|
|
|
mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
|
2019-10-10 20:17:45 -07:00
|
|
|
mem[SK_MEMINFO_SNDBUF] = READ_ONCE(sk->sk_sndbuf);
|
2025-02-18 19:36:16 +01:00
|
|
|
mem[SK_MEMINFO_FWD_ALLOC] = READ_ONCE(sk->sk_forward_alloc);
|
2019-10-10 20:17:46 -07:00
|
|
|
mem[SK_MEMINFO_WMEM_QUEUED] = READ_ONCE(sk->sk_wmem_queued);
|
2017-03-20 15:22:03 -04:00
|
|
|
mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
|
2019-10-09 15:41:03 -07:00
|
|
|
mem[SK_MEMINFO_BACKLOG] = READ_ONCE(sk->sk_backlog.len);
|
2025-08-26 12:50:27 +00:00
|
|
|
mem[SK_MEMINFO_DROPS] = sk_drops_read(sk);
|
2017-03-20 15:22:03 -04:00
|
|
|
}
|
|
|
|
|
|
2008-03-28 16:38:17 -07:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
|
static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
|
2008-03-31 19:42:16 -07:00
|
|
|
|
|
|
|
|
int sock_prot_inuse_get(struct net *net, struct proto *prot)
|
|
|
|
|
{
|
|
|
|
|
int cpu, idx = prot->inuse_idx;
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu)
|
2017-12-14 05:51:57 -08:00
|
|
|
res += per_cpu_ptr(net->core.prot_inuse, cpu)->val[idx];
|
2008-03-31 19:42:16 -07:00
|
|
|
|
|
|
|
|
return res >= 0 ? res : 0;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
|
|
|
|
|
|
2017-12-14 05:51:58 -08:00
|
|
|
int sock_inuse_get(struct net *net)
|
|
|
|
|
{
|
|
|
|
|
int cpu, res = 0;
|
|
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu)
|
2021-11-15 09:11:49 -08:00
|
|
|
res += per_cpu_ptr(net->core.prot_inuse, cpu)->all;
|
2017-12-14 05:51:58 -08:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPORT_SYMBOL_GPL(sock_inuse_get);
|
|
|
|
|
|
2010-01-17 03:35:32 +00:00
|
|
|
static int __net_init sock_inuse_init_net(struct net *net)
|
2008-03-31 19:42:16 -07:00
|
|
|
{
|
2017-12-14 05:51:57 -08:00
|
|
|
net->core.prot_inuse = alloc_percpu(struct prot_inuse);
|
2017-12-14 05:51:58 -08:00
|
|
|
if (net->core.prot_inuse == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
return 0;
|
2008-03-31 19:42:16 -07:00
|
|
|
}
|
|
|
|
|
|
2010-01-17 03:35:32 +00:00
|
|
|
static void __net_exit sock_inuse_exit_net(struct net *net)
|
2008-03-31 19:42:16 -07:00
|
|
|
{
|
2017-12-14 05:51:57 -08:00
|
|
|
free_percpu(net->core.prot_inuse);
|
2008-03-31 19:42:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct pernet_operations net_inuse_ops = {
|
|
|
|
|
.init = sock_inuse_init_net,
|
|
|
|
|
.exit = sock_inuse_exit_net,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static __init int net_inuse_init(void)
|
|
|
|
|
{
|
|
|
|
|
if (register_pernet_subsys(&net_inuse_ops))
|
|
|
|
|
panic("Cannot initialize net inuse counters");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
core_initcall(net_inuse_init);
|
2008-03-28 16:38:17 -07:00
|
|
|
|
2019-08-23 09:14:11 +08:00
|
|
|
static int assign_proto_idx(struct proto *prot)
|
2008-03-28 16:38:17 -07:00
|
|
|
{
|
|
|
|
|
prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
|
|
|
|
|
|
2025-04-10 09:01:27 +08:00
|
|
|
if (unlikely(prot->inuse_idx == PROTO_INUSE_NR)) {
|
2012-05-16 19:58:40 +00:00
|
|
|
pr_err("PROTO_INUSE_NR exhausted\n");
|
2019-08-23 09:14:11 +08:00
|
|
|
return -ENOSPC;
|
2008-03-28 16:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_bit(prot->inuse_idx, proto_inuse_idx);
|
2019-08-23 09:14:11 +08:00
|
|
|
return 0;
|
2008-03-28 16:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void release_proto_idx(struct proto *prot)
|
|
|
|
|
{
|
2025-04-10 09:01:27 +08:00
|
|
|
if (prot->inuse_idx != PROTO_INUSE_NR)
|
2008-03-28 16:38:17 -07:00
|
|
|
clear_bit(prot->inuse_idx, proto_inuse_idx);
|
|
|
|
|
}
|
|
|
|
|
#else
|
2019-08-23 09:14:11 +08:00
|
|
|
static inline int assign_proto_idx(struct proto *prot)
|
2008-03-28 16:38:17 -07:00
|
|
|
{
|
2019-08-23 09:14:11 +08:00
|
|
|
return 0;
|
2008-03-28 16:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void release_proto_idx(struct proto *prot)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-12-14 05:51:58 -08:00
|
|
|
|
2008-03-28 16:38:17 -07:00
|
|
|
#endif
|
|
|
|
|
|
2020-08-10 08:16:58 -04:00
|
|
|
static void tw_prot_cleanup(struct timewait_sock_ops *twsk_prot)
|
|
|
|
|
{
|
|
|
|
|
if (!twsk_prot)
|
|
|
|
|
return;
|
|
|
|
|
kfree(twsk_prot->twsk_slab_name);
|
|
|
|
|
twsk_prot->twsk_slab_name = NULL;
|
|
|
|
|
kmem_cache_destroy(twsk_prot->twsk_slab);
|
|
|
|
|
twsk_prot->twsk_slab = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 10:57:36 +08:00
|
|
|
static int tw_prot_init(const struct proto *prot)
|
|
|
|
|
{
|
|
|
|
|
struct timewait_sock_ops *twsk_prot = prot->twsk_prot;
|
|
|
|
|
|
|
|
|
|
if (!twsk_prot)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s",
|
|
|
|
|
prot->name);
|
|
|
|
|
if (!twsk_prot->twsk_slab_name)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
twsk_prot->twsk_slab =
|
|
|
|
|
kmem_cache_create(twsk_prot->twsk_slab_name,
|
|
|
|
|
twsk_prot->twsk_obj_size, 0,
|
|
|
|
|
SLAB_ACCOUNT | prot->slab_flags,
|
|
|
|
|
NULL);
|
|
|
|
|
if (!twsk_prot->twsk_slab) {
|
|
|
|
|
pr_crit("%s: Can't create timewait sock SLAB cache!\n",
|
|
|
|
|
prot->name);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 16:44:07 -07:00
|
|
|
static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
|
|
|
|
|
{
|
|
|
|
|
if (!rsk_prot)
|
|
|
|
|
return;
|
|
|
|
|
kfree(rsk_prot->slab_name);
|
|
|
|
|
rsk_prot->slab_name = NULL;
|
2015-09-13 14:15:18 +02:00
|
|
|
kmem_cache_destroy(rsk_prot->slab);
|
|
|
|
|
rsk_prot->slab = NULL;
|
2015-03-12 16:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int req_prot_init(const struct proto *prot)
|
|
|
|
|
{
|
|
|
|
|
struct request_sock_ops *rsk_prot = prot->rsk_prot;
|
|
|
|
|
|
|
|
|
|
if (!rsk_prot)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
|
|
|
|
|
prot->name);
|
|
|
|
|
if (!rsk_prot->slab_name)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
|
|
|
|
|
rsk_prot->obj_size, 0,
|
2018-06-27 15:16:42 -07:00
|
|
|
SLAB_ACCOUNT | prot->slab_flags,
|
|
|
|
|
NULL);
|
2015-03-12 16:44:07 -07:00
|
|
|
|
|
|
|
|
if (!rsk_prot->slab) {
|
|
|
|
|
pr_crit("%s: Can't create request sock SLAB cache!\n",
|
|
|
|
|
prot->name);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
int proto_register(struct proto *prot, int alloc_slab)
|
|
|
|
|
{
|
2019-08-23 09:14:11 +08:00
|
|
|
int ret = -ENOBUFS;
|
|
|
|
|
|
2022-02-16 09:18:01 -08:00
|
|
|
if (prot->memory_allocated && !prot->sysctl_mem) {
|
|
|
|
|
pr_err("%s: missing sysctl_mem\n", prot->name);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2022-06-08 23:34:08 -07:00
|
|
|
if (prot->memory_allocated && !prot->per_cpu_fw_alloc) {
|
|
|
|
|
pr_err("%s: missing per_cpu_fw_alloc\n", prot->name);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
if (alloc_slab) {
|
2017-06-10 22:50:42 -04:00
|
|
|
prot->slab = kmem_cache_create_usercopy(prot->name,
|
|
|
|
|
prot->obj_size, 0,
|
2018-06-27 15:16:42 -07:00
|
|
|
SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT |
|
|
|
|
|
prot->slab_flags,
|
2017-08-24 16:59:38 -07:00
|
|
|
prot->useroffset, prot->usersize,
|
2008-10-29 02:11:14 -07:00
|
|
|
NULL);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
if (prot->slab == NULL) {
|
2012-05-16 19:58:40 +00:00
|
|
|
pr_crit("%s: Can't create sock SLAB cache!\n",
|
|
|
|
|
prot->name);
|
2008-03-28 16:39:10 -07:00
|
|
|
goto out;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2005-06-18 22:46:52 -07:00
|
|
|
|
2015-03-12 16:44:07 -07:00
|
|
|
if (req_prot_init(prot))
|
|
|
|
|
goto out_free_request_sock_slab;
|
2005-08-09 20:09:30 -07:00
|
|
|
|
2021-03-11 10:57:36 +08:00
|
|
|
if (tw_prot_init(prot))
|
|
|
|
|
goto out_free_timewait_sock_slab;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
2011-12-16 00:51:59 +00:00
|
|
|
mutex_lock(&proto_list_mutex);
|
2019-08-23 09:14:11 +08:00
|
|
|
ret = assign_proto_idx(prot);
|
|
|
|
|
if (ret) {
|
|
|
|
|
mutex_unlock(&proto_list_mutex);
|
2020-08-10 08:16:58 -04:00
|
|
|
goto out_free_timewait_sock_slab;
|
2019-08-23 09:14:11 +08:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
list_add(&prot->node, &proto_list);
|
2011-12-16 00:51:59 +00:00
|
|
|
mutex_unlock(&proto_list_mutex);
|
2019-08-23 09:14:11 +08:00
|
|
|
return ret;
|
2007-11-07 02:23:38 -08:00
|
|
|
|
2020-08-10 08:16:58 -04:00
|
|
|
out_free_timewait_sock_slab:
|
2021-04-22 21:41:51 +08:00
|
|
|
if (alloc_slab)
|
2020-08-10 08:16:58 -04:00
|
|
|
tw_prot_cleanup(prot->twsk_prot);
|
2005-08-09 20:09:30 -07:00
|
|
|
out_free_request_sock_slab:
|
2019-08-23 09:14:11 +08:00
|
|
|
if (alloc_slab) {
|
|
|
|
|
req_prot_cleanup(prot->rsk_prot);
|
2015-03-12 16:44:07 -07:00
|
|
|
|
2019-08-23 09:14:11 +08:00
|
|
|
kmem_cache_destroy(prot->slab);
|
|
|
|
|
prot->slab = NULL;
|
|
|
|
|
}
|
2007-11-07 02:23:38 -08:00
|
|
|
out:
|
2019-08-23 09:14:11 +08:00
|
|
|
return ret;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(proto_register);
|
|
|
|
|
|
|
|
|
|
void proto_unregister(struct proto *prot)
|
|
|
|
|
{
|
2011-12-16 00:51:59 +00:00
|
|
|
mutex_lock(&proto_list_mutex);
|
2008-03-28 16:38:17 -07:00
|
|
|
release_proto_idx(prot);
|
2005-09-06 19:47:50 -07:00
|
|
|
list_del(&prot->node);
|
2011-12-16 00:51:59 +00:00
|
|
|
mutex_unlock(&proto_list_mutex);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2015-09-13 14:15:18 +02:00
|
|
|
kmem_cache_destroy(prot->slab);
|
|
|
|
|
prot->slab = NULL;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2015-03-12 16:44:07 -07:00
|
|
|
req_prot_cleanup(prot->rsk_prot);
|
2020-08-10 08:16:58 -04:00
|
|
|
tw_prot_cleanup(prot->twsk_prot);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(proto_unregister);
|
|
|
|
|
|
2018-03-10 18:57:50 +08:00
|
|
|
int sock_load_diag_module(int family, int protocol)
|
|
|
|
|
{
|
|
|
|
|
if (!protocol) {
|
|
|
|
|
if (!sock_is_registered(family))
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
|
|
return request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
|
|
|
|
|
NETLINK_SOCK_DIAG, family);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_INET
|
|
|
|
|
if (family == AF_INET &&
|
2018-11-04 22:37:15 -08:00
|
|
|
protocol != IPPROTO_RAW &&
|
2020-07-09 15:12:39 +02:00
|
|
|
protocol < MAX_INET_PROTOS &&
|
2018-03-10 18:57:50 +08:00
|
|
|
!rcu_access_pointer(inet_protos[protocol]))
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
|
|
|
|
|
NETLINK_SOCK_DIAG, family, protocol);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_load_diag_module);
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
|
static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
|
2011-12-16 00:51:59 +00:00
|
|
|
__acquires(proto_list_mutex)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2011-12-16 00:51:59 +00:00
|
|
|
mutex_lock(&proto_list_mutex);
|
2007-07-09 13:15:14 -07:00
|
|
|
return seq_list_start_head(&proto_list, *pos);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
|
{
|
2007-07-09 13:15:14 -07:00
|
|
|
return seq_list_next(v, &proto_list, pos);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void proto_seq_stop(struct seq_file *seq, void *v)
|
2011-12-16 00:51:59 +00:00
|
|
|
__releases(proto_list_mutex)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2011-12-16 00:51:59 +00:00
|
|
|
mutex_unlock(&proto_list_mutex);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char proto_method_implemented(const void *method)
|
|
|
|
|
{
|
|
|
|
|
return method == NULL ? 'n' : 'y';
|
|
|
|
|
}
|
2011-12-11 21:47:02 +00:00
|
|
|
static long sock_prot_memory_allocated(struct proto *proto)
|
|
|
|
|
{
|
2012-04-25 19:17:29 +05:30
|
|
|
return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
|
2011-12-11 21:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 00:44:40 +03:00
|
|
|
static const char *sock_prot_memory_pressure(struct proto *proto)
|
2011-12-11 21:47:02 +00:00
|
|
|
{
|
|
|
|
|
return proto->memory_pressure != NULL ?
|
|
|
|
|
proto_memory_pressure(proto) ? "yes" : "no" : "NI";
|
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
|
|
|
|
|
{
|
2011-12-11 21:47:02 +00:00
|
|
|
|
2010-11-09 23:24:26 +00:00
|
|
|
seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
|
2023-06-23 23:55:12 +01:00
|
|
|
"%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
|
2005-04-16 15:20:36 -07:00
|
|
|
proto->name,
|
|
|
|
|
proto->obj_size,
|
2008-11-19 15:14:01 -08:00
|
|
|
sock_prot_inuse_get(seq_file_net(seq), proto),
|
2011-12-11 21:47:02 +00:00
|
|
|
sock_prot_memory_allocated(proto),
|
|
|
|
|
sock_prot_memory_pressure(proto),
|
2005-04-16 15:20:36 -07:00
|
|
|
proto->max_header,
|
|
|
|
|
proto->slab == NULL ? "no" : "yes",
|
|
|
|
|
module_name(proto->owner),
|
|
|
|
|
proto_method_implemented(proto->close),
|
|
|
|
|
proto_method_implemented(proto->connect),
|
|
|
|
|
proto_method_implemented(proto->disconnect),
|
|
|
|
|
proto_method_implemented(proto->accept),
|
|
|
|
|
proto_method_implemented(proto->ioctl),
|
|
|
|
|
proto_method_implemented(proto->init),
|
|
|
|
|
proto_method_implemented(proto->destroy),
|
|
|
|
|
proto_method_implemented(proto->shutdown),
|
|
|
|
|
proto_method_implemented(proto->setsockopt),
|
|
|
|
|
proto_method_implemented(proto->getsockopt),
|
|
|
|
|
proto_method_implemented(proto->sendmsg),
|
|
|
|
|
proto_method_implemented(proto->recvmsg),
|
|
|
|
|
proto_method_implemented(proto->bind),
|
|
|
|
|
proto_method_implemented(proto->backlog_rcv),
|
|
|
|
|
proto_method_implemented(proto->hash),
|
|
|
|
|
proto_method_implemented(proto->unhash),
|
|
|
|
|
proto_method_implemented(proto->get_port),
|
|
|
|
|
proto_method_implemented(proto->enter_memory_pressure));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int proto_seq_show(struct seq_file *seq, void *v)
|
|
|
|
|
{
|
2007-07-09 13:15:14 -07:00
|
|
|
if (v == &proto_list)
|
2005-04-16 15:20:36 -07:00
|
|
|
seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
|
|
|
|
|
"protocol",
|
|
|
|
|
"size",
|
|
|
|
|
"sockets",
|
|
|
|
|
"memory",
|
|
|
|
|
"press",
|
|
|
|
|
"maxhdr",
|
|
|
|
|
"slab",
|
|
|
|
|
"module",
|
2023-06-23 23:55:12 +01:00
|
|
|
"cl co di ac io in de sh ss gs se re bi br ha uh gp em\n");
|
2005-04-16 15:20:36 -07:00
|
|
|
else
|
2007-07-09 13:15:14 -07:00
|
|
|
proto_seq_printf(seq, list_entry(v, struct proto, node));
|
2005-04-16 15:20:36 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-12 14:34:29 -07:00
|
|
|
static const struct seq_operations proto_seq_ops = {
|
2005-04-16 15:20:36 -07:00
|
|
|
.start = proto_seq_start,
|
|
|
|
|
.next = proto_seq_next,
|
|
|
|
|
.stop = proto_seq_stop,
|
|
|
|
|
.show = proto_seq_show,
|
|
|
|
|
};
|
|
|
|
|
|
2008-11-19 15:14:01 -08:00
|
|
|
static __net_init int proto_init_net(struct net *net)
|
|
|
|
|
{
|
2018-04-10 19:42:55 +02:00
|
|
|
if (!proc_create_net("protocols", 0444, net->proc_net, &proto_seq_ops,
|
|
|
|
|
sizeof(struct seq_net_private)))
|
2008-11-19 15:14:01 -08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __net_exit void proto_exit_net(struct net *net)
|
|
|
|
|
{
|
2013-02-18 01:34:56 +00:00
|
|
|
remove_proc_entry("protocols", net->proc_net);
|
2008-11-19 15:14:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static __net_initdata struct pernet_operations proto_net_ops = {
|
|
|
|
|
.init = proto_init_net,
|
|
|
|
|
.exit = proto_exit_net,
|
2005-04-16 15:20:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int __init proto_init(void)
|
|
|
|
|
{
|
2008-11-19 15:14:01 -08:00
|
|
|
return register_pernet_subsys(&proto_net_ops);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subsys_initcall(proto_init);
|
|
|
|
|
|
|
|
|
|
#endif /* PROC_FS */
|
2017-03-24 10:08:24 -07:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_RX_BUSY_POLL
|
|
|
|
|
bool sk_busy_loop_end(void *p, unsigned long start_time)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sk = p;
|
|
|
|
|
|
2024-01-18 20:17:49 +00:00
|
|
|
if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (sk_is_udp(sk) &&
|
|
|
|
|
!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return sk_busy_loop_timeout(sk, start_time);
|
2017-03-24 10:08:24 -07:00
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_busy_loop_end);
|
|
|
|
|
#endif /* CONFIG_NET_RX_BUSY_POLL */
|
2020-05-29 14:09:42 +02:00
|
|
|
|
2025-11-03 16:26:13 -08:00
|
|
|
int sock_bind_add(struct sock *sk, struct sockaddr_unsized *addr, int addr_len)
|
2020-05-29 14:09:42 +02:00
|
|
|
{
|
|
|
|
|
if (!sk->sk_prot->bind_add)
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
return sk->sk_prot->bind_add(sk, addr, addr_len);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_bind_add);
|
2023-06-09 08:27:42 -07:00
|
|
|
|
|
|
|
|
/* Copy 'size' bytes from userspace and return `size` back to userspace */
|
|
|
|
|
int sock_ioctl_inout(struct sock *sk, unsigned int cmd,
|
|
|
|
|
void __user *arg, void *karg, size_t size)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (copy_from_user(karg, arg, size))
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
|
|
ret = READ_ONCE(sk->sk_prot)->ioctl(sk, cmd, karg);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
if (copy_to_user(arg, karg, size))
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sock_ioctl_inout);
|
|
|
|
|
|
|
|
|
|
/* This is the most common ioctl prep function, where the result (4 bytes) is
|
|
|
|
|
* copied back to userspace if the ioctl() returns successfully. No input is
|
|
|
|
|
* copied from userspace as input argument.
|
|
|
|
|
*/
|
|
|
|
|
static int sock_ioctl_out(struct sock *sk, unsigned int cmd, void __user *arg)
|
|
|
|
|
{
|
|
|
|
|
int ret, karg = 0;
|
|
|
|
|
|
|
|
|
|
ret = READ_ONCE(sk->sk_prot)->ioctl(sk, cmd, &karg);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
return put_user(karg, (int __user *)arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* A wrapper around sock ioctls, which copies the data from userspace
|
|
|
|
|
* (depending on the protocol/ioctl), and copies back the result to userspace.
|
|
|
|
|
* The main motivation for this function is to pass kernel memory to the
|
|
|
|
|
* protocol ioctl callbacks, instead of userspace memory.
|
|
|
|
|
*/
|
|
|
|
|
int sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
|
|
|
|
|
{
|
|
|
|
|
int rc = 1;
|
|
|
|
|
|
2023-06-19 12:43:35 +00:00
|
|
|
if (sk->sk_type == SOCK_RAW && sk->sk_family == AF_INET)
|
2023-06-09 08:27:42 -07:00
|
|
|
rc = ipmr_sk_ioctl(sk, cmd, arg);
|
2023-06-19 12:43:35 +00:00
|
|
|
else if (sk->sk_type == SOCK_RAW && sk->sk_family == AF_INET6)
|
2023-06-09 08:27:42 -07:00
|
|
|
rc = ip6mr_sk_ioctl(sk, cmd, arg);
|
|
|
|
|
else if (sk_is_phonet(sk))
|
|
|
|
|
rc = phonet_sk_ioctl(sk, cmd, arg);
|
|
|
|
|
|
|
|
|
|
/* If ioctl was processed, returns its value */
|
|
|
|
|
if (rc <= 0)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
/* Otherwise call the default handler */
|
|
|
|
|
return sock_ioctl_out(sk, cmd, arg);
|
|
|
|
|
}
|
|
|
|
|
EXPORT_SYMBOL(sk_ioctl);
|
2024-02-16 16:20:06 +00:00
|
|
|
|
|
|
|
|
static int __init sock_struct_check(void)
|
|
|
|
|
{
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rx, sk_drops);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rx, sk_peek_off);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rx, sk_error_queue);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rx, sk_receive_queue);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rx, sk_backlog);
|
|
|
|
|
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_rx_dst);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_rx_dst_ifindex);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_rx_dst_cookie);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_rcvbuf);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_filter);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_wq);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_data_ready);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_rcvtimeo);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rx, sk_rcvlowat);
|
|
|
|
|
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rxtx, sk_err);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rxtx, sk_socket);
|
2025-08-15 20:16:18 +00:00
|
|
|
#ifdef CONFIG_MEMCG
|
2024-02-16 16:20:06 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_rxtx, sk_memcg);
|
2025-08-15 20:16:18 +00:00
|
|
|
#endif
|
2024-02-16 16:20:06 +00:00
|
|
|
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rxtx, sk_lock);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rxtx, sk_reserved_mem);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rxtx, sk_forward_alloc);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_rxtx, sk_tsflags);
|
|
|
|
|
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_omem_alloc);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_omem_alloc);
|
2025-09-19 20:48:50 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_err_soft);
|
2024-02-16 16:20:06 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_wmem_queued);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_wmem_alloc);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_tsq_flags);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_send_head);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_write_queue);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_write_pending);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_dst_pending_confirm);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_pacing_status);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_frag);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_timer);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_pacing_rate);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_zckey);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_write_tx, sk_tskey);
|
|
|
|
|
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_max_pacing_rate);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_sndtimeo);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_priority);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_mark);
|
2025-09-19 20:48:49 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_uid);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_protocol);
|
2024-02-16 16:20:06 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_dst_cache);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_route_caps);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_gso_type);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_gso_max_size);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_allocation);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_txhash);
|
2025-09-19 20:48:50 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_sndbuf);
|
2024-02-16 16:20:06 +00:00
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_gso_max_segs);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_pacing_shift);
|
|
|
|
|
CACHELINE_ASSERT_GROUP_MEMBER(struct sock, sock_read_tx, sk_use_task_frag);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
core_initcall(sock_struct_check);
|