18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * INET An implementation of the TCP/IP protocol suite for the LINUX 48c2ecf20Sopenharmony_ci * operating system. INET is implemented using the BSD Socket 58c2ecf20Sopenharmony_ci * interface as the means of communication with the user level. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Definitions for the AF_INET socket handler. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Version: @(#)sock.h 1.0.4 05/13/93 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Authors: Ross Biro 128c2ecf20Sopenharmony_ci * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 138c2ecf20Sopenharmony_ci * Corey Minyard <wf-rch!minyard@relay.EU.net> 148c2ecf20Sopenharmony_ci * Florian La Roche <flla@stud.uni-sb.de> 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * Fixes: 178c2ecf20Sopenharmony_ci * Alan Cox : Volatiles in skbuff pointers. See 188c2ecf20Sopenharmony_ci * skbuff comments. May be overdone, 198c2ecf20Sopenharmony_ci * better to prove they can be removed 208c2ecf20Sopenharmony_ci * than the reverse. 218c2ecf20Sopenharmony_ci * Alan Cox : Added a zapped field for tcp to note 228c2ecf20Sopenharmony_ci * a socket is reset and must stay shut up 238c2ecf20Sopenharmony_ci * Alan Cox : New fields for options 248c2ecf20Sopenharmony_ci * Pauline Middelink : identd support 258c2ecf20Sopenharmony_ci * Alan Cox : Eliminate low level recv/recvfrom 268c2ecf20Sopenharmony_ci * David S. Miller : New socket lookup architecture. 278c2ecf20Sopenharmony_ci * Steve Whitehouse: Default routines for sock_ops 288c2ecf20Sopenharmony_ci * Arnaldo C. Melo : removed net_pinfo, tp_pinfo and made 298c2ecf20Sopenharmony_ci * protinfo be just a void pointer, as the 308c2ecf20Sopenharmony_ci * protocol specific parts were moved to 318c2ecf20Sopenharmony_ci * respective headers and ipv4/v6, etc now 328c2ecf20Sopenharmony_ci * use private slabcaches for its socks 338c2ecf20Sopenharmony_ci * Pedro Hortas : New flags field for socket options 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_ci#ifndef _SOCK_H 368c2ecf20Sopenharmony_ci#define _SOCK_H 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#include <linux/hardirq.h> 398c2ecf20Sopenharmony_ci#include <linux/kernel.h> 408c2ecf20Sopenharmony_ci#include <linux/list.h> 418c2ecf20Sopenharmony_ci#include <linux/list_nulls.h> 428c2ecf20Sopenharmony_ci#include <linux/timer.h> 438c2ecf20Sopenharmony_ci#include <linux/cache.h> 448c2ecf20Sopenharmony_ci#include <linux/bitops.h> 458c2ecf20Sopenharmony_ci#include <linux/lockdep.h> 468c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 478c2ecf20Sopenharmony_ci#include <linux/skbuff.h> /* struct sk_buff */ 488c2ecf20Sopenharmony_ci#include <linux/mm.h> 498c2ecf20Sopenharmony_ci#include <linux/security.h> 508c2ecf20Sopenharmony_ci#include <linux/slab.h> 518c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 528c2ecf20Sopenharmony_ci#include <linux/page_counter.h> 538c2ecf20Sopenharmony_ci#include <linux/memcontrol.h> 548c2ecf20Sopenharmony_ci#include <linux/static_key.h> 558c2ecf20Sopenharmony_ci#include <linux/sched.h> 568c2ecf20Sopenharmony_ci#include <linux/wait.h> 578c2ecf20Sopenharmony_ci#include <linux/cgroup-defs.h> 588c2ecf20Sopenharmony_ci#include <linux/rbtree.h> 598c2ecf20Sopenharmony_ci#include <linux/filter.h> 608c2ecf20Sopenharmony_ci#include <linux/rculist_nulls.h> 618c2ecf20Sopenharmony_ci#include <linux/poll.h> 628c2ecf20Sopenharmony_ci#include <linux/sockptr.h> 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#include <linux/atomic.h> 658c2ecf20Sopenharmony_ci#include <linux/refcount.h> 668c2ecf20Sopenharmony_ci#include <net/dst.h> 678c2ecf20Sopenharmony_ci#include <net/checksum.h> 688c2ecf20Sopenharmony_ci#include <net/tcp_states.h> 698c2ecf20Sopenharmony_ci#include <linux/net_tstamp.h> 708c2ecf20Sopenharmony_ci#include <net/l3mdev.h> 718c2ecf20Sopenharmony_ci#ifdef CONFIG_NEWIP 728c2ecf20Sopenharmony_ci#include <uapi/linux/nip_addr.h> 738c2ecf20Sopenharmony_ci#endif 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/* 768c2ecf20Sopenharmony_ci * This structure really needs to be cleaned up. 778c2ecf20Sopenharmony_ci * Most of it is for TCP, and not used by any of 788c2ecf20Sopenharmony_ci * the other protocols. 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* Define this to get the SOCK_DBG debugging facility. */ 828c2ecf20Sopenharmony_ci#define SOCK_DEBUGGING 838c2ecf20Sopenharmony_ci#ifdef SOCK_DEBUGGING 848c2ecf20Sopenharmony_ci#define SOCK_DEBUG(sk, msg...) do { if ((sk) && sock_flag((sk), SOCK_DBG)) \ 858c2ecf20Sopenharmony_ci printk(KERN_DEBUG msg); } while (0) 868c2ecf20Sopenharmony_ci#else 878c2ecf20Sopenharmony_ci/* Validate arguments and do nothing */ 888c2ecf20Sopenharmony_cistatic inline __printf(2, 3) 898c2ecf20Sopenharmony_civoid SOCK_DEBUG(const struct sock *sk, const char *msg, ...) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci#endif 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/* This is the per-socket lock. The spinlock provides a synchronization 958c2ecf20Sopenharmony_ci * between user contexts and software interrupt processing, whereas the 968c2ecf20Sopenharmony_ci * mini-semaphore synchronizes multiple users amongst themselves. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_citypedef struct { 998c2ecf20Sopenharmony_ci spinlock_t slock; 1008c2ecf20Sopenharmony_ci int owned; 1018c2ecf20Sopenharmony_ci wait_queue_head_t wq; 1028c2ecf20Sopenharmony_ci /* 1038c2ecf20Sopenharmony_ci * We express the mutex-alike socket_lock semantics 1048c2ecf20Sopenharmony_ci * to the lock validator by explicitly managing 1058c2ecf20Sopenharmony_ci * the slock as a lock variant (in addition to 1068c2ecf20Sopenharmony_ci * the slock itself): 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_LOCK_ALLOC 1098c2ecf20Sopenharmony_ci struct lockdep_map dep_map; 1108c2ecf20Sopenharmony_ci#endif 1118c2ecf20Sopenharmony_ci} socket_lock_t; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistruct sock; 1148c2ecf20Sopenharmony_cistruct proto; 1158c2ecf20Sopenharmony_cistruct net; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_citypedef __u32 __bitwise __portpair; 1188c2ecf20Sopenharmony_citypedef __u64 __bitwise __addrpair; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/** 1218c2ecf20Sopenharmony_ci * struct sock_common - minimal network layer representation of sockets 1228c2ecf20Sopenharmony_ci * @skc_daddr: Foreign IPv4 addr 1238c2ecf20Sopenharmony_ci * @skc_rcv_saddr: Bound local IPv4 addr 1248c2ecf20Sopenharmony_ci * @skc_addrpair: 8-byte-aligned __u64 union of @skc_daddr & @skc_rcv_saddr 1258c2ecf20Sopenharmony_ci * @skc_hash: hash value used with various protocol lookup tables 1268c2ecf20Sopenharmony_ci * @skc_u16hashes: two u16 hash values used by UDP lookup tables 1278c2ecf20Sopenharmony_ci * @skc_dport: placeholder for inet_dport/tw_dport 1288c2ecf20Sopenharmony_ci * @skc_num: placeholder for inet_num/tw_num 1298c2ecf20Sopenharmony_ci * @skc_portpair: __u32 union of @skc_dport & @skc_num 1308c2ecf20Sopenharmony_ci * @skc_family: network address family 1318c2ecf20Sopenharmony_ci * @skc_state: Connection state 1328c2ecf20Sopenharmony_ci * @skc_reuse: %SO_REUSEADDR setting 1338c2ecf20Sopenharmony_ci * @skc_reuseport: %SO_REUSEPORT setting 1348c2ecf20Sopenharmony_ci * @skc_ipv6only: socket is IPV6 only 1358c2ecf20Sopenharmony_ci * @skc_net_refcnt: socket is using net ref counting 1368c2ecf20Sopenharmony_ci * @skc_bound_dev_if: bound device index if != 0 1378c2ecf20Sopenharmony_ci * @skc_bind_node: bind hash linkage for various protocol lookup tables 1388c2ecf20Sopenharmony_ci * @skc_portaddr_node: second hash linkage for UDP/UDP-Lite protocol 1398c2ecf20Sopenharmony_ci * @skc_prot: protocol handlers inside a network family 1408c2ecf20Sopenharmony_ci * @skc_net: reference to the network namespace of this socket 1418c2ecf20Sopenharmony_ci * @skc_v6_daddr: IPV6 destination address 1428c2ecf20Sopenharmony_ci * @skc_v6_rcv_saddr: IPV6 source address 1438c2ecf20Sopenharmony_ci * @skc_cookie: socket's cookie value 1448c2ecf20Sopenharmony_ci * @skc_node: main hash linkage for various protocol lookup tables 1458c2ecf20Sopenharmony_ci * @skc_nulls_node: main hash linkage for TCP/UDP/UDP-Lite protocol 1468c2ecf20Sopenharmony_ci * @skc_tx_queue_mapping: tx queue number for this connection 1478c2ecf20Sopenharmony_ci * @skc_rx_queue_mapping: rx queue number for this connection 1488c2ecf20Sopenharmony_ci * @skc_flags: place holder for sk_flags 1498c2ecf20Sopenharmony_ci * %SO_LINGER (l_onoff), %SO_BROADCAST, %SO_KEEPALIVE, 1508c2ecf20Sopenharmony_ci * %SO_OOBINLINE settings, %SO_TIMESTAMPING settings 1518c2ecf20Sopenharmony_ci * @skc_listener: connection request listener socket (aka rsk_listener) 1528c2ecf20Sopenharmony_ci * [union with @skc_flags] 1538c2ecf20Sopenharmony_ci * @skc_tw_dr: (aka tw_dr) ptr to &struct inet_timewait_death_row 1548c2ecf20Sopenharmony_ci * [union with @skc_flags] 1558c2ecf20Sopenharmony_ci * @skc_incoming_cpu: record/match cpu processing incoming packets 1568c2ecf20Sopenharmony_ci * @skc_rcv_wnd: (aka rsk_rcv_wnd) TCP receive window size (possibly scaled) 1578c2ecf20Sopenharmony_ci * [union with @skc_incoming_cpu] 1588c2ecf20Sopenharmony_ci * @skc_tw_rcv_nxt: (aka tw_rcv_nxt) TCP window next expected seq number 1598c2ecf20Sopenharmony_ci * [union with @skc_incoming_cpu] 1608c2ecf20Sopenharmony_ci * @skc_refcnt: reference count 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * This is the minimal network layer representation of sockets, the header 1638c2ecf20Sopenharmony_ci * for struct sock and struct inet_timewait_sock. 1648c2ecf20Sopenharmony_ci */ 1658c2ecf20Sopenharmony_cistruct sock_common { 1668c2ecf20Sopenharmony_ci union { 1678c2ecf20Sopenharmony_ci __addrpair skc_addrpair; 1688c2ecf20Sopenharmony_ci struct { 1698c2ecf20Sopenharmony_ci __be32 skc_daddr; 1708c2ecf20Sopenharmony_ci __be32 skc_rcv_saddr; 1718c2ecf20Sopenharmony_ci }; 1728c2ecf20Sopenharmony_ci }; 1738c2ecf20Sopenharmony_ci union { 1748c2ecf20Sopenharmony_ci unsigned int skc_hash; 1758c2ecf20Sopenharmony_ci __u16 skc_u16hashes[2]; 1768c2ecf20Sopenharmony_ci }; 1778c2ecf20Sopenharmony_ci /* skc_dport && skc_num must be grouped as well */ 1788c2ecf20Sopenharmony_ci union { 1798c2ecf20Sopenharmony_ci __portpair skc_portpair; 1808c2ecf20Sopenharmony_ci struct { 1818c2ecf20Sopenharmony_ci __be16 skc_dport; 1828c2ecf20Sopenharmony_ci __u16 skc_num; 1838c2ecf20Sopenharmony_ci }; 1848c2ecf20Sopenharmony_ci }; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci unsigned short skc_family; 1878c2ecf20Sopenharmony_ci volatile unsigned char skc_state; 1888c2ecf20Sopenharmony_ci unsigned char skc_reuse:4; 1898c2ecf20Sopenharmony_ci unsigned char skc_reuseport:1; 1908c2ecf20Sopenharmony_ci unsigned char skc_ipv6only:1; 1918c2ecf20Sopenharmony_ci unsigned char skc_net_refcnt:1; 1928c2ecf20Sopenharmony_ci int skc_bound_dev_if; 1938c2ecf20Sopenharmony_ci union { 1948c2ecf20Sopenharmony_ci struct hlist_node skc_bind_node; 1958c2ecf20Sopenharmony_ci struct hlist_node skc_portaddr_node; 1968c2ecf20Sopenharmony_ci }; 1978c2ecf20Sopenharmony_ci struct proto *skc_prot; 1988c2ecf20Sopenharmony_ci possible_net_t skc_net; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 2018c2ecf20Sopenharmony_ci struct in6_addr skc_v6_daddr; 2028c2ecf20Sopenharmony_ci struct in6_addr skc_v6_rcv_saddr; 2038c2ecf20Sopenharmony_ci#endif 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_NEWIP) 2068c2ecf20Sopenharmony_ci struct nip_addr nip_daddr; /* NIP */ 2078c2ecf20Sopenharmony_ci struct nip_addr nip_rcv_saddr; /* NIP */ 2088c2ecf20Sopenharmony_ci#endif 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci atomic64_t skc_cookie; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* following fields are padding to force 2138c2ecf20Sopenharmony_ci * offset(struct sock, sk_refcnt) == 128 on 64bit arches 2148c2ecf20Sopenharmony_ci * assuming IPV6 is enabled. We use this padding differently 2158c2ecf20Sopenharmony_ci * for different kind of 'sockets' 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_ci union { 2188c2ecf20Sopenharmony_ci unsigned long skc_flags; 2198c2ecf20Sopenharmony_ci struct sock *skc_listener; /* request_sock */ 2208c2ecf20Sopenharmony_ci struct inet_timewait_death_row *skc_tw_dr; /* inet_timewait_sock */ 2218c2ecf20Sopenharmony_ci }; 2228c2ecf20Sopenharmony_ci /* 2238c2ecf20Sopenharmony_ci * fields between dontcopy_begin/dontcopy_end 2248c2ecf20Sopenharmony_ci * are not copied in sock_copy() 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ci /* private: */ 2278c2ecf20Sopenharmony_ci int skc_dontcopy_begin[0]; 2288c2ecf20Sopenharmony_ci /* public: */ 2298c2ecf20Sopenharmony_ci union { 2308c2ecf20Sopenharmony_ci struct hlist_node skc_node; 2318c2ecf20Sopenharmony_ci struct hlist_nulls_node skc_nulls_node; 2328c2ecf20Sopenharmony_ci }; 2338c2ecf20Sopenharmony_ci unsigned short skc_tx_queue_mapping; 2348c2ecf20Sopenharmony_ci#ifdef CONFIG_XPS 2358c2ecf20Sopenharmony_ci unsigned short skc_rx_queue_mapping; 2368c2ecf20Sopenharmony_ci#endif 2378c2ecf20Sopenharmony_ci union { 2388c2ecf20Sopenharmony_ci int skc_incoming_cpu; 2398c2ecf20Sopenharmony_ci u32 skc_rcv_wnd; 2408c2ecf20Sopenharmony_ci u32 skc_tw_rcv_nxt; /* struct tcp_timewait_sock */ 2418c2ecf20Sopenharmony_ci }; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci refcount_t skc_refcnt; 2448c2ecf20Sopenharmony_ci /* private: */ 2458c2ecf20Sopenharmony_ci int skc_dontcopy_end[0]; 2468c2ecf20Sopenharmony_ci union { 2478c2ecf20Sopenharmony_ci u32 skc_rxhash; 2488c2ecf20Sopenharmony_ci u32 skc_window_clamp; 2498c2ecf20Sopenharmony_ci u32 skc_tw_snd_nxt; /* struct tcp_timewait_sock */ 2508c2ecf20Sopenharmony_ci }; 2518c2ecf20Sopenharmony_ci /* public: */ 2528c2ecf20Sopenharmony_ci}; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistruct bpf_local_storage; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci/** 2578c2ecf20Sopenharmony_ci * struct sock - network layer representation of sockets 2588c2ecf20Sopenharmony_ci * @__sk_common: shared layout with inet_timewait_sock 2598c2ecf20Sopenharmony_ci * @sk_shutdown: mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN 2608c2ecf20Sopenharmony_ci * @sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings 2618c2ecf20Sopenharmony_ci * @sk_lock: synchronizer 2628c2ecf20Sopenharmony_ci * @sk_kern_sock: True if sock is using kernel lock classes 2638c2ecf20Sopenharmony_ci * @sk_rcvbuf: size of receive buffer in bytes 2648c2ecf20Sopenharmony_ci * @sk_wq: sock wait queue and async head 2658c2ecf20Sopenharmony_ci * @sk_rx_dst: receive input route used by early demux 2668c2ecf20Sopenharmony_ci * @sk_dst_cache: destination cache 2678c2ecf20Sopenharmony_ci * @sk_dst_pending_confirm: need to confirm neighbour 2688c2ecf20Sopenharmony_ci * @sk_policy: flow policy 2698c2ecf20Sopenharmony_ci * @sk_rx_skb_cache: cache copy of recently accessed RX skb 2708c2ecf20Sopenharmony_ci * @sk_receive_queue: incoming packets 2718c2ecf20Sopenharmony_ci * @sk_wmem_alloc: transmit queue bytes committed 2728c2ecf20Sopenharmony_ci * @sk_tsq_flags: TCP Small Queues flags 2738c2ecf20Sopenharmony_ci * @sk_write_queue: Packet sending queue 2748c2ecf20Sopenharmony_ci * @sk_omem_alloc: "o" is "option" or "other" 2758c2ecf20Sopenharmony_ci * @sk_wmem_queued: persistent queue size 2768c2ecf20Sopenharmony_ci * @sk_forward_alloc: space allocated forward 2778c2ecf20Sopenharmony_ci * @sk_napi_id: id of the last napi context to receive data for sk 2788c2ecf20Sopenharmony_ci * @sk_ll_usec: usecs to busypoll when there is no data 2798c2ecf20Sopenharmony_ci * @sk_allocation: allocation mode 2808c2ecf20Sopenharmony_ci * @sk_pacing_rate: Pacing rate (if supported by transport/packet scheduler) 2818c2ecf20Sopenharmony_ci * @sk_pacing_status: Pacing status (requested, handled by sch_fq) 2828c2ecf20Sopenharmony_ci * @sk_max_pacing_rate: Maximum pacing rate (%SO_MAX_PACING_RATE) 2838c2ecf20Sopenharmony_ci * @sk_sndbuf: size of send buffer in bytes 2848c2ecf20Sopenharmony_ci * @__sk_flags_offset: empty field used to determine location of bitfield 2858c2ecf20Sopenharmony_ci * @sk_padding: unused element for alignment 2868c2ecf20Sopenharmony_ci * @sk_no_check_tx: %SO_NO_CHECK setting, set checksum in TX packets 2878c2ecf20Sopenharmony_ci * @sk_no_check_rx: allow zero checksum in RX packets 2888c2ecf20Sopenharmony_ci * @sk_route_caps: route capabilities (e.g. %NETIF_F_TSO) 2898c2ecf20Sopenharmony_ci * @sk_route_nocaps: forbidden route capabilities (e.g NETIF_F_GSO_MASK) 2908c2ecf20Sopenharmony_ci * @sk_route_forced_caps: static, forced route capabilities 2918c2ecf20Sopenharmony_ci * (set in tcp_init_sock()) 2928c2ecf20Sopenharmony_ci * @sk_gso_type: GSO type (e.g. %SKB_GSO_TCPV4) 2938c2ecf20Sopenharmony_ci * @sk_gso_max_size: Maximum GSO segment size to build 2948c2ecf20Sopenharmony_ci * @sk_gso_max_segs: Maximum number of GSO segments 2958c2ecf20Sopenharmony_ci * @sk_pacing_shift: scaling factor for TCP Small Queues 2968c2ecf20Sopenharmony_ci * @sk_lingertime: %SO_LINGER l_linger setting 2978c2ecf20Sopenharmony_ci * @sk_backlog: always used with the per-socket spinlock held 2988c2ecf20Sopenharmony_ci * @sk_callback_lock: used with the callbacks in the end of this struct 2998c2ecf20Sopenharmony_ci * @sk_error_queue: rarely used 3008c2ecf20Sopenharmony_ci * @sk_prot_creator: sk_prot of original sock creator (see ipv6_setsockopt, 3018c2ecf20Sopenharmony_ci * IPV6_ADDRFORM for instance) 3028c2ecf20Sopenharmony_ci * @sk_err: last error 3038c2ecf20Sopenharmony_ci * @sk_err_soft: errors that don't cause failure but are the cause of a 3048c2ecf20Sopenharmony_ci * persistent failure not just 'timed out' 3058c2ecf20Sopenharmony_ci * @sk_drops: raw/udp drops counter 3068c2ecf20Sopenharmony_ci * @sk_ack_backlog: current listen backlog 3078c2ecf20Sopenharmony_ci * @sk_max_ack_backlog: listen backlog set in listen() 3088c2ecf20Sopenharmony_ci * @sk_uid: user id of owner 3098c2ecf20Sopenharmony_ci * @sk_priority: %SO_PRIORITY setting 3108c2ecf20Sopenharmony_ci * @sk_type: socket type (%SOCK_STREAM, etc) 3118c2ecf20Sopenharmony_ci * @sk_protocol: which protocol this socket belongs in this network family 3128c2ecf20Sopenharmony_ci * @sk_peer_pid: &struct pid for this socket's peer 3138c2ecf20Sopenharmony_ci * @sk_peer_cred: %SO_PEERCRED setting 3148c2ecf20Sopenharmony_ci * @sk_rcvlowat: %SO_RCVLOWAT setting 3158c2ecf20Sopenharmony_ci * @sk_rcvtimeo: %SO_RCVTIMEO setting 3168c2ecf20Sopenharmony_ci * @sk_sndtimeo: %SO_SNDTIMEO setting 3178c2ecf20Sopenharmony_ci * @sk_txhash: computed flow hash for use on transmit 3188c2ecf20Sopenharmony_ci * @sk_filter: socket filtering instructions 3198c2ecf20Sopenharmony_ci * @sk_timer: sock cleanup timer 3208c2ecf20Sopenharmony_ci * @sk_stamp: time stamp of last packet received 3218c2ecf20Sopenharmony_ci * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only 3228c2ecf20Sopenharmony_ci * @sk_tsflags: SO_TIMESTAMPING socket options 3238c2ecf20Sopenharmony_ci * @sk_tskey: counter to disambiguate concurrent tstamp requests 3248c2ecf20Sopenharmony_ci * @sk_zckey: counter to order MSG_ZEROCOPY notifications 3258c2ecf20Sopenharmony_ci * @sk_socket: Identd and reporting IO signals 3268c2ecf20Sopenharmony_ci * @sk_user_data: RPC layer private data. Write-protected by @sk_callback_lock. 3278c2ecf20Sopenharmony_ci * @sk_frag: cached page frag 3288c2ecf20Sopenharmony_ci * @sk_peek_off: current peek_offset value 3298c2ecf20Sopenharmony_ci * @sk_send_head: front of stuff to transmit 3308c2ecf20Sopenharmony_ci * @tcp_rtx_queue: TCP re-transmit queue [union with @sk_send_head] 3318c2ecf20Sopenharmony_ci * @sk_tx_skb_cache: cache copy of recently accessed TX skb 3328c2ecf20Sopenharmony_ci * @sk_security: used by security modules 3338c2ecf20Sopenharmony_ci * @sk_mark: generic packet mark 3348c2ecf20Sopenharmony_ci * @sk_cgrp_data: cgroup data for this cgroup 3358c2ecf20Sopenharmony_ci * @sk_memcg: this socket's memory cgroup association 3368c2ecf20Sopenharmony_ci * @sk_write_pending: a write to stream socket waits to start 3378c2ecf20Sopenharmony_ci * @sk_wait_pending: number of threads blocked on this socket 3388c2ecf20Sopenharmony_ci * @sk_state_change: callback to indicate change in the state of the sock 3398c2ecf20Sopenharmony_ci * @sk_data_ready: callback to indicate there is data to be processed 3408c2ecf20Sopenharmony_ci * @sk_write_space: callback to indicate there is bf sending space available 3418c2ecf20Sopenharmony_ci * @sk_error_report: callback to indicate errors (e.g. %MSG_ERRQUEUE) 3428c2ecf20Sopenharmony_ci * @sk_backlog_rcv: callback to process the backlog 3438c2ecf20Sopenharmony_ci * @sk_validate_xmit_skb: ptr to an optional validate function 3448c2ecf20Sopenharmony_ci * @sk_destruct: called at sock freeing time, i.e. when all refcnt == 0 3458c2ecf20Sopenharmony_ci * @sk_reuseport_cb: reuseport group container 3468c2ecf20Sopenharmony_ci * @sk_bpf_storage: ptr to cache and control for bpf_sk_storage 3478c2ecf20Sopenharmony_ci * @sk_rcu: used during RCU grace period 3488c2ecf20Sopenharmony_ci * @sk_clockid: clockid used by time-based scheduling (SO_TXTIME) 3498c2ecf20Sopenharmony_ci * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME 3508c2ecf20Sopenharmony_ci * @sk_txtime_report_errors: set report errors mode for SO_TXTIME 3518c2ecf20Sopenharmony_ci * @sk_txtime_unused: unused txtime flags 3528c2ecf20Sopenharmony_ci */ 3538c2ecf20Sopenharmony_cistruct sock { 3548c2ecf20Sopenharmony_ci /* 3558c2ecf20Sopenharmony_ci * Now struct inet_timewait_sock also uses sock_common, so please just 3568c2ecf20Sopenharmony_ci * don't add nothing before this first member (__sk_common) --acme 3578c2ecf20Sopenharmony_ci */ 3588c2ecf20Sopenharmony_ci struct sock_common __sk_common; 3598c2ecf20Sopenharmony_ci#define sk_node __sk_common.skc_node 3608c2ecf20Sopenharmony_ci#define sk_nulls_node __sk_common.skc_nulls_node 3618c2ecf20Sopenharmony_ci#define sk_refcnt __sk_common.skc_refcnt 3628c2ecf20Sopenharmony_ci#define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping 3638c2ecf20Sopenharmony_ci#ifdef CONFIG_XPS 3648c2ecf20Sopenharmony_ci#define sk_rx_queue_mapping __sk_common.skc_rx_queue_mapping 3658c2ecf20Sopenharmony_ci#endif 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci#define sk_dontcopy_begin __sk_common.skc_dontcopy_begin 3688c2ecf20Sopenharmony_ci#define sk_dontcopy_end __sk_common.skc_dontcopy_end 3698c2ecf20Sopenharmony_ci#define sk_hash __sk_common.skc_hash 3708c2ecf20Sopenharmony_ci#define sk_portpair __sk_common.skc_portpair 3718c2ecf20Sopenharmony_ci#define sk_num __sk_common.skc_num 3728c2ecf20Sopenharmony_ci#define sk_dport __sk_common.skc_dport 3738c2ecf20Sopenharmony_ci#define sk_addrpair __sk_common.skc_addrpair 3748c2ecf20Sopenharmony_ci#define sk_daddr __sk_common.skc_daddr 3758c2ecf20Sopenharmony_ci#define sk_rcv_saddr __sk_common.skc_rcv_saddr 3768c2ecf20Sopenharmony_ci#define sk_family __sk_common.skc_family 3778c2ecf20Sopenharmony_ci#define sk_state __sk_common.skc_state 3788c2ecf20Sopenharmony_ci#define sk_reuse __sk_common.skc_reuse 3798c2ecf20Sopenharmony_ci#define sk_reuseport __sk_common.skc_reuseport 3808c2ecf20Sopenharmony_ci#define sk_ipv6only __sk_common.skc_ipv6only 3818c2ecf20Sopenharmony_ci#define sk_net_refcnt __sk_common.skc_net_refcnt 3828c2ecf20Sopenharmony_ci#define sk_bound_dev_if __sk_common.skc_bound_dev_if 3838c2ecf20Sopenharmony_ci#define sk_bind_node __sk_common.skc_bind_node 3848c2ecf20Sopenharmony_ci#define sk_prot __sk_common.skc_prot 3858c2ecf20Sopenharmony_ci#define sk_net __sk_common.skc_net 3868c2ecf20Sopenharmony_ci#define sk_v6_daddr __sk_common.skc_v6_daddr 3878c2ecf20Sopenharmony_ci#define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr 3888c2ecf20Sopenharmony_ci#define sk_cookie __sk_common.skc_cookie 3898c2ecf20Sopenharmony_ci#define sk_incoming_cpu __sk_common.skc_incoming_cpu 3908c2ecf20Sopenharmony_ci#define sk_flags __sk_common.skc_flags 3918c2ecf20Sopenharmony_ci#define sk_rxhash __sk_common.skc_rxhash 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci socket_lock_t sk_lock; 3948c2ecf20Sopenharmony_ci atomic_t sk_drops; 3958c2ecf20Sopenharmony_ci int sk_rcvlowat; 3968c2ecf20Sopenharmony_ci struct sk_buff_head sk_error_queue; 3978c2ecf20Sopenharmony_ci struct sk_buff *sk_rx_skb_cache; 3988c2ecf20Sopenharmony_ci struct sk_buff_head sk_receive_queue; 3998c2ecf20Sopenharmony_ci /* 4008c2ecf20Sopenharmony_ci * The backlog queue is special, it is always used with 4018c2ecf20Sopenharmony_ci * the per-socket spinlock held and requires low latency 4028c2ecf20Sopenharmony_ci * access. Therefore we special case it's implementation. 4038c2ecf20Sopenharmony_ci * Note : rmem_alloc is in this structure to fill a hole 4048c2ecf20Sopenharmony_ci * on 64bit arches, not because its logically part of 4058c2ecf20Sopenharmony_ci * backlog. 4068c2ecf20Sopenharmony_ci */ 4078c2ecf20Sopenharmony_ci struct { 4088c2ecf20Sopenharmony_ci atomic_t rmem_alloc; 4098c2ecf20Sopenharmony_ci int len; 4108c2ecf20Sopenharmony_ci struct sk_buff *head; 4118c2ecf20Sopenharmony_ci struct sk_buff *tail; 4128c2ecf20Sopenharmony_ci } sk_backlog; 4138c2ecf20Sopenharmony_ci#define sk_rmem_alloc sk_backlog.rmem_alloc 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci int sk_forward_alloc; 4168c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_RX_BUSY_POLL 4178c2ecf20Sopenharmony_ci unsigned int sk_ll_usec; 4188c2ecf20Sopenharmony_ci /* ===== mostly read cache line ===== */ 4198c2ecf20Sopenharmony_ci unsigned int sk_napi_id; 4208c2ecf20Sopenharmony_ci#endif 4218c2ecf20Sopenharmony_ci int sk_rcvbuf; 4228c2ecf20Sopenharmony_ci int sk_wait_pending; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci struct sk_filter __rcu *sk_filter; 4258c2ecf20Sopenharmony_ci union { 4268c2ecf20Sopenharmony_ci struct socket_wq __rcu *sk_wq; 4278c2ecf20Sopenharmony_ci /* private: */ 4288c2ecf20Sopenharmony_ci struct socket_wq *sk_wq_raw; 4298c2ecf20Sopenharmony_ci /* public: */ 4308c2ecf20Sopenharmony_ci }; 4318c2ecf20Sopenharmony_ci#ifdef CONFIG_XFRM 4328c2ecf20Sopenharmony_ci struct xfrm_policy __rcu *sk_policy[2]; 4338c2ecf20Sopenharmony_ci#endif 4348c2ecf20Sopenharmony_ci struct dst_entry __rcu *sk_rx_dst; 4358c2ecf20Sopenharmony_ci struct dst_entry __rcu *sk_dst_cache; 4368c2ecf20Sopenharmony_ci atomic_t sk_omem_alloc; 4378c2ecf20Sopenharmony_ci int sk_sndbuf; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* ===== cache line for TX ===== */ 4408c2ecf20Sopenharmony_ci int sk_wmem_queued; 4418c2ecf20Sopenharmony_ci refcount_t sk_wmem_alloc; 4428c2ecf20Sopenharmony_ci unsigned long sk_tsq_flags; 4438c2ecf20Sopenharmony_ci union { 4448c2ecf20Sopenharmony_ci struct sk_buff *sk_send_head; 4458c2ecf20Sopenharmony_ci struct rb_root tcp_rtx_queue; 4468c2ecf20Sopenharmony_ci }; 4478c2ecf20Sopenharmony_ci struct sk_buff *sk_tx_skb_cache; 4488c2ecf20Sopenharmony_ci struct sk_buff_head sk_write_queue; 4498c2ecf20Sopenharmony_ci __s32 sk_peek_off; 4508c2ecf20Sopenharmony_ci int sk_write_pending; 4518c2ecf20Sopenharmony_ci __u32 sk_dst_pending_confirm; 4528c2ecf20Sopenharmony_ci u32 sk_pacing_status; /* see enum sk_pacing */ 4538c2ecf20Sopenharmony_ci long sk_sndtimeo; 4548c2ecf20Sopenharmony_ci struct timer_list sk_timer; 4558c2ecf20Sopenharmony_ci __u32 sk_priority; 4568c2ecf20Sopenharmony_ci __u32 sk_mark; 4578c2ecf20Sopenharmony_ci unsigned long sk_pacing_rate; /* bytes per second */ 4588c2ecf20Sopenharmony_ci unsigned long sk_max_pacing_rate; 4598c2ecf20Sopenharmony_ci struct page_frag sk_frag; 4608c2ecf20Sopenharmony_ci netdev_features_t sk_route_caps; 4618c2ecf20Sopenharmony_ci netdev_features_t sk_route_nocaps; 4628c2ecf20Sopenharmony_ci netdev_features_t sk_route_forced_caps; 4638c2ecf20Sopenharmony_ci int sk_gso_type; 4648c2ecf20Sopenharmony_ci unsigned int sk_gso_max_size; 4658c2ecf20Sopenharmony_ci gfp_t sk_allocation; 4668c2ecf20Sopenharmony_ci __u32 sk_txhash; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci /* 4698c2ecf20Sopenharmony_ci * Because of non atomicity rules, all 4708c2ecf20Sopenharmony_ci * changes are protected by socket lock. 4718c2ecf20Sopenharmony_ci */ 4728c2ecf20Sopenharmony_ci u8 sk_padding : 1, 4738c2ecf20Sopenharmony_ci sk_kern_sock : 1, 4748c2ecf20Sopenharmony_ci sk_no_check_tx : 1, 4758c2ecf20Sopenharmony_ci sk_no_check_rx : 1, 4768c2ecf20Sopenharmony_ci sk_userlocks : 4; 4778c2ecf20Sopenharmony_ci u8 sk_pacing_shift; 4788c2ecf20Sopenharmony_ci u16 sk_type; 4798c2ecf20Sopenharmony_ci u16 sk_protocol; 4808c2ecf20Sopenharmony_ci u16 sk_gso_max_segs; 4818c2ecf20Sopenharmony_ci unsigned long sk_lingertime; 4828c2ecf20Sopenharmony_ci struct proto *sk_prot_creator; 4838c2ecf20Sopenharmony_ci rwlock_t sk_callback_lock; 4848c2ecf20Sopenharmony_ci int sk_err, 4858c2ecf20Sopenharmony_ci sk_err_soft; 4868c2ecf20Sopenharmony_ci u32 sk_ack_backlog; 4878c2ecf20Sopenharmony_ci u32 sk_max_ack_backlog; 4888c2ecf20Sopenharmony_ci kuid_t sk_uid; 4898c2ecf20Sopenharmony_ci spinlock_t sk_peer_lock; 4908c2ecf20Sopenharmony_ci struct pid *sk_peer_pid; 4918c2ecf20Sopenharmony_ci const struct cred *sk_peer_cred; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci long sk_rcvtimeo; 4948c2ecf20Sopenharmony_ci ktime_t sk_stamp; 4958c2ecf20Sopenharmony_ci#if BITS_PER_LONG==32 4968c2ecf20Sopenharmony_ci seqlock_t sk_stamp_seq; 4978c2ecf20Sopenharmony_ci#endif 4988c2ecf20Sopenharmony_ci u16 sk_tsflags; 4998c2ecf20Sopenharmony_ci u8 sk_shutdown; 5008c2ecf20Sopenharmony_ci u32 sk_tskey; 5018c2ecf20Sopenharmony_ci atomic_t sk_zckey; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci u8 sk_clockid; 5048c2ecf20Sopenharmony_ci u8 sk_txtime_deadline_mode : 1, 5058c2ecf20Sopenharmony_ci sk_txtime_report_errors : 1, 5068c2ecf20Sopenharmony_ci sk_txtime_unused : 6; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci struct socket *sk_socket; 5098c2ecf20Sopenharmony_ci void *sk_user_data; 5108c2ecf20Sopenharmony_ci#ifdef CONFIG_SECURITY 5118c2ecf20Sopenharmony_ci void *sk_security; 5128c2ecf20Sopenharmony_ci#endif 5138c2ecf20Sopenharmony_ci struct sock_cgroup_data sk_cgrp_data; 5148c2ecf20Sopenharmony_ci struct mem_cgroup *sk_memcg; 5158c2ecf20Sopenharmony_ci void (*sk_state_change)(struct sock *sk); 5168c2ecf20Sopenharmony_ci void (*sk_data_ready)(struct sock *sk); 5178c2ecf20Sopenharmony_ci void (*sk_write_space)(struct sock *sk); 5188c2ecf20Sopenharmony_ci void (*sk_error_report)(struct sock *sk); 5198c2ecf20Sopenharmony_ci int (*sk_backlog_rcv)(struct sock *sk, 5208c2ecf20Sopenharmony_ci struct sk_buff *skb); 5218c2ecf20Sopenharmony_ci#ifdef CONFIG_SOCK_VALIDATE_XMIT 5228c2ecf20Sopenharmony_ci struct sk_buff* (*sk_validate_xmit_skb)(struct sock *sk, 5238c2ecf20Sopenharmony_ci struct net_device *dev, 5248c2ecf20Sopenharmony_ci struct sk_buff *skb); 5258c2ecf20Sopenharmony_ci#endif 5268c2ecf20Sopenharmony_ci void (*sk_destruct)(struct sock *sk); 5278c2ecf20Sopenharmony_ci struct sock_reuseport __rcu *sk_reuseport_cb; 5288c2ecf20Sopenharmony_ci#ifdef CONFIG_BPF_SYSCALL 5298c2ecf20Sopenharmony_ci struct bpf_local_storage __rcu *sk_bpf_storage; 5308c2ecf20Sopenharmony_ci#endif 5318c2ecf20Sopenharmony_ci struct rcu_head sk_rcu; 5328c2ecf20Sopenharmony_ci}; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_cienum sk_pacing { 5358c2ecf20Sopenharmony_ci SK_PACING_NONE = 0, 5368c2ecf20Sopenharmony_ci SK_PACING_NEEDED = 1, 5378c2ecf20Sopenharmony_ci SK_PACING_FQ = 2, 5388c2ecf20Sopenharmony_ci}; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci/* flag bits in sk_user_data 5418c2ecf20Sopenharmony_ci * 5428c2ecf20Sopenharmony_ci * - SK_USER_DATA_NOCOPY: Pointer stored in sk_user_data might 5438c2ecf20Sopenharmony_ci * not be suitable for copying when cloning the socket. For instance, 5448c2ecf20Sopenharmony_ci * it can point to a reference counted object. sk_user_data bottom 5458c2ecf20Sopenharmony_ci * bit is set if pointer must not be copied. 5468c2ecf20Sopenharmony_ci * 5478c2ecf20Sopenharmony_ci * - SK_USER_DATA_BPF: Mark whether sk_user_data field is 5488c2ecf20Sopenharmony_ci * managed/owned by a BPF reuseport array. This bit should be set 5498c2ecf20Sopenharmony_ci * when sk_user_data's sk is added to the bpf's reuseport_array. 5508c2ecf20Sopenharmony_ci * 5518c2ecf20Sopenharmony_ci * - SK_USER_DATA_PSOCK: Mark whether pointer stored in 5528c2ecf20Sopenharmony_ci * sk_user_data points to psock type. This bit should be set 5538c2ecf20Sopenharmony_ci * when sk_user_data is assigned to a psock object. 5548c2ecf20Sopenharmony_ci */ 5558c2ecf20Sopenharmony_ci#define SK_USER_DATA_NOCOPY 1UL 5568c2ecf20Sopenharmony_ci#define SK_USER_DATA_BPF 2UL 5578c2ecf20Sopenharmony_ci#define SK_USER_DATA_PSOCK 4UL 5588c2ecf20Sopenharmony_ci#define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY | SK_USER_DATA_BPF |\ 5598c2ecf20Sopenharmony_ci SK_USER_DATA_PSOCK) 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci/** 5628c2ecf20Sopenharmony_ci * sk_user_data_is_nocopy - Test if sk_user_data pointer must not be copied 5638c2ecf20Sopenharmony_ci * @sk: socket 5648c2ecf20Sopenharmony_ci */ 5658c2ecf20Sopenharmony_cistatic inline bool sk_user_data_is_nocopy(const struct sock *sk) 5668c2ecf20Sopenharmony_ci{ 5678c2ecf20Sopenharmony_ci return ((uintptr_t)sk->sk_user_data & SK_USER_DATA_NOCOPY); 5688c2ecf20Sopenharmony_ci} 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci#define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data))) 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci/** 5738c2ecf20Sopenharmony_ci * __rcu_dereference_sk_user_data_with_flags - return the pointer 5748c2ecf20Sopenharmony_ci * only if argument flags all has been set in sk_user_data. Otherwise 5758c2ecf20Sopenharmony_ci * return NULL 5768c2ecf20Sopenharmony_ci * 5778c2ecf20Sopenharmony_ci * @sk: socket 5788c2ecf20Sopenharmony_ci * @flags: flag bits 5798c2ecf20Sopenharmony_ci */ 5808c2ecf20Sopenharmony_cistatic inline void * 5818c2ecf20Sopenharmony_ci__rcu_dereference_sk_user_data_with_flags(const struct sock *sk, 5828c2ecf20Sopenharmony_ci uintptr_t flags) 5838c2ecf20Sopenharmony_ci{ 5848c2ecf20Sopenharmony_ci uintptr_t sk_user_data = (uintptr_t)rcu_dereference(__sk_user_data(sk)); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci WARN_ON_ONCE(flags & SK_USER_DATA_PTRMASK); 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci if ((sk_user_data & flags) == flags) 5898c2ecf20Sopenharmony_ci return (void *)(sk_user_data & SK_USER_DATA_PTRMASK); 5908c2ecf20Sopenharmony_ci return NULL; 5918c2ecf20Sopenharmony_ci} 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci#define rcu_dereference_sk_user_data(sk) \ 5948c2ecf20Sopenharmony_ci __rcu_dereference_sk_user_data_with_flags(sk, 0) 5958c2ecf20Sopenharmony_ci#define __rcu_assign_sk_user_data_with_flags(sk, ptr, flags) \ 5968c2ecf20Sopenharmony_ci({ \ 5978c2ecf20Sopenharmony_ci uintptr_t __tmp1 = (uintptr_t)(ptr), \ 5988c2ecf20Sopenharmony_ci __tmp2 = (uintptr_t)(flags); \ 5998c2ecf20Sopenharmony_ci WARN_ON_ONCE(__tmp1 & ~SK_USER_DATA_PTRMASK); \ 6008c2ecf20Sopenharmony_ci WARN_ON_ONCE(__tmp2 & SK_USER_DATA_PTRMASK); \ 6018c2ecf20Sopenharmony_ci rcu_assign_pointer(__sk_user_data((sk)), \ 6028c2ecf20Sopenharmony_ci __tmp1 | __tmp2); \ 6038c2ecf20Sopenharmony_ci}) 6048c2ecf20Sopenharmony_ci#define rcu_assign_sk_user_data(sk, ptr) \ 6058c2ecf20Sopenharmony_ci __rcu_assign_sk_user_data_with_flags(sk, ptr, 0) 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ci/* 6088c2ecf20Sopenharmony_ci * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK 6098c2ecf20Sopenharmony_ci * or not whether his port will be reused by someone else. SK_FORCE_REUSE 6108c2ecf20Sopenharmony_ci * on a socket means that the socket will reuse everybody else's port 6118c2ecf20Sopenharmony_ci * without looking at the other's sk_reuse value. 6128c2ecf20Sopenharmony_ci */ 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci#define SK_NO_REUSE 0 6158c2ecf20Sopenharmony_ci#define SK_CAN_REUSE 1 6168c2ecf20Sopenharmony_ci#define SK_FORCE_REUSE 2 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ciint sk_set_peek_off(struct sock *sk, int val); 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_cistatic inline int sk_peek_offset(struct sock *sk, int flags) 6218c2ecf20Sopenharmony_ci{ 6228c2ecf20Sopenharmony_ci if (unlikely(flags & MSG_PEEK)) { 6238c2ecf20Sopenharmony_ci return READ_ONCE(sk->sk_peek_off); 6248c2ecf20Sopenharmony_ci } 6258c2ecf20Sopenharmony_ci 6268c2ecf20Sopenharmony_ci return 0; 6278c2ecf20Sopenharmony_ci} 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_cistatic inline void sk_peek_offset_bwd(struct sock *sk, int val) 6308c2ecf20Sopenharmony_ci{ 6318c2ecf20Sopenharmony_ci s32 off = READ_ONCE(sk->sk_peek_off); 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci if (unlikely(off >= 0)) { 6348c2ecf20Sopenharmony_ci off = max_t(s32, off - val, 0); 6358c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_peek_off, off); 6368c2ecf20Sopenharmony_ci } 6378c2ecf20Sopenharmony_ci} 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_cistatic inline void sk_peek_offset_fwd(struct sock *sk, int val) 6408c2ecf20Sopenharmony_ci{ 6418c2ecf20Sopenharmony_ci sk_peek_offset_bwd(sk, -val); 6428c2ecf20Sopenharmony_ci} 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci/* 6458c2ecf20Sopenharmony_ci * Hashed lists helper routines 6468c2ecf20Sopenharmony_ci */ 6478c2ecf20Sopenharmony_cistatic inline struct sock *sk_entry(const struct hlist_node *node) 6488c2ecf20Sopenharmony_ci{ 6498c2ecf20Sopenharmony_ci return hlist_entry(node, struct sock, sk_node); 6508c2ecf20Sopenharmony_ci} 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_cistatic inline struct sock *__sk_head(const struct hlist_head *head) 6538c2ecf20Sopenharmony_ci{ 6548c2ecf20Sopenharmony_ci return hlist_entry(head->first, struct sock, sk_node); 6558c2ecf20Sopenharmony_ci} 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_cistatic inline struct sock *sk_head(const struct hlist_head *head) 6588c2ecf20Sopenharmony_ci{ 6598c2ecf20Sopenharmony_ci return hlist_empty(head) ? NULL : __sk_head(head); 6608c2ecf20Sopenharmony_ci} 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_cistatic inline struct sock *__sk_nulls_head(const struct hlist_nulls_head *head) 6638c2ecf20Sopenharmony_ci{ 6648c2ecf20Sopenharmony_ci return hlist_nulls_entry(head->first, struct sock, sk_nulls_node); 6658c2ecf20Sopenharmony_ci} 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cistatic inline struct sock *sk_nulls_head(const struct hlist_nulls_head *head) 6688c2ecf20Sopenharmony_ci{ 6698c2ecf20Sopenharmony_ci return hlist_nulls_empty(head) ? NULL : __sk_nulls_head(head); 6708c2ecf20Sopenharmony_ci} 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_cistatic inline struct sock *sk_next(const struct sock *sk) 6738c2ecf20Sopenharmony_ci{ 6748c2ecf20Sopenharmony_ci return hlist_entry_safe(sk->sk_node.next, struct sock, sk_node); 6758c2ecf20Sopenharmony_ci} 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_cistatic inline struct sock *sk_nulls_next(const struct sock *sk) 6788c2ecf20Sopenharmony_ci{ 6798c2ecf20Sopenharmony_ci return (!is_a_nulls(sk->sk_nulls_node.next)) ? 6808c2ecf20Sopenharmony_ci hlist_nulls_entry(sk->sk_nulls_node.next, 6818c2ecf20Sopenharmony_ci struct sock, sk_nulls_node) : 6828c2ecf20Sopenharmony_ci NULL; 6838c2ecf20Sopenharmony_ci} 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_cistatic inline bool sk_unhashed(const struct sock *sk) 6868c2ecf20Sopenharmony_ci{ 6878c2ecf20Sopenharmony_ci return hlist_unhashed(&sk->sk_node); 6888c2ecf20Sopenharmony_ci} 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_cistatic inline bool sk_hashed(const struct sock *sk) 6918c2ecf20Sopenharmony_ci{ 6928c2ecf20Sopenharmony_ci return !sk_unhashed(sk); 6938c2ecf20Sopenharmony_ci} 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_cistatic inline void sk_node_init(struct hlist_node *node) 6968c2ecf20Sopenharmony_ci{ 6978c2ecf20Sopenharmony_ci node->pprev = NULL; 6988c2ecf20Sopenharmony_ci} 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_cistatic inline void sk_nulls_node_init(struct hlist_nulls_node *node) 7018c2ecf20Sopenharmony_ci{ 7028c2ecf20Sopenharmony_ci node->pprev = NULL; 7038c2ecf20Sopenharmony_ci} 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_cistatic inline void __sk_del_node(struct sock *sk) 7068c2ecf20Sopenharmony_ci{ 7078c2ecf20Sopenharmony_ci __hlist_del(&sk->sk_node); 7088c2ecf20Sopenharmony_ci} 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci/* NB: equivalent to hlist_del_init_rcu */ 7118c2ecf20Sopenharmony_cistatic inline bool __sk_del_node_init(struct sock *sk) 7128c2ecf20Sopenharmony_ci{ 7138c2ecf20Sopenharmony_ci if (sk_hashed(sk)) { 7148c2ecf20Sopenharmony_ci __sk_del_node(sk); 7158c2ecf20Sopenharmony_ci sk_node_init(&sk->sk_node); 7168c2ecf20Sopenharmony_ci return true; 7178c2ecf20Sopenharmony_ci } 7188c2ecf20Sopenharmony_ci return false; 7198c2ecf20Sopenharmony_ci} 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci/* Grab socket reference count. This operation is valid only 7228c2ecf20Sopenharmony_ci when sk is ALREADY grabbed f.e. it is found in hash table 7238c2ecf20Sopenharmony_ci or a list and the lookup is made under lock preventing hash table 7248c2ecf20Sopenharmony_ci modifications. 7258c2ecf20Sopenharmony_ci */ 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_cistatic __always_inline void sock_hold(struct sock *sk) 7288c2ecf20Sopenharmony_ci{ 7298c2ecf20Sopenharmony_ci refcount_inc(&sk->sk_refcnt); 7308c2ecf20Sopenharmony_ci} 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci/* Ungrab socket in the context, which assumes that socket refcnt 7338c2ecf20Sopenharmony_ci cannot hit zero, f.e. it is true in context of any socketcall. 7348c2ecf20Sopenharmony_ci */ 7358c2ecf20Sopenharmony_cistatic __always_inline void __sock_put(struct sock *sk) 7368c2ecf20Sopenharmony_ci{ 7378c2ecf20Sopenharmony_ci refcount_dec(&sk->sk_refcnt); 7388c2ecf20Sopenharmony_ci} 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_cistatic inline bool sk_del_node_init(struct sock *sk) 7418c2ecf20Sopenharmony_ci{ 7428c2ecf20Sopenharmony_ci bool rc = __sk_del_node_init(sk); 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci if (rc) { 7458c2ecf20Sopenharmony_ci /* paranoid for a while -acme */ 7468c2ecf20Sopenharmony_ci WARN_ON(refcount_read(&sk->sk_refcnt) == 1); 7478c2ecf20Sopenharmony_ci __sock_put(sk); 7488c2ecf20Sopenharmony_ci } 7498c2ecf20Sopenharmony_ci return rc; 7508c2ecf20Sopenharmony_ci} 7518c2ecf20Sopenharmony_ci#define sk_del_node_init_rcu(sk) sk_del_node_init(sk) 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_cistatic inline bool __sk_nulls_del_node_init_rcu(struct sock *sk) 7548c2ecf20Sopenharmony_ci{ 7558c2ecf20Sopenharmony_ci if (sk_hashed(sk)) { 7568c2ecf20Sopenharmony_ci hlist_nulls_del_init_rcu(&sk->sk_nulls_node); 7578c2ecf20Sopenharmony_ci return true; 7588c2ecf20Sopenharmony_ci } 7598c2ecf20Sopenharmony_ci return false; 7608c2ecf20Sopenharmony_ci} 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_cistatic inline bool sk_nulls_del_node_init_rcu(struct sock *sk) 7638c2ecf20Sopenharmony_ci{ 7648c2ecf20Sopenharmony_ci bool rc = __sk_nulls_del_node_init_rcu(sk); 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci if (rc) { 7678c2ecf20Sopenharmony_ci /* paranoid for a while -acme */ 7688c2ecf20Sopenharmony_ci WARN_ON(refcount_read(&sk->sk_refcnt) == 1); 7698c2ecf20Sopenharmony_ci __sock_put(sk); 7708c2ecf20Sopenharmony_ci } 7718c2ecf20Sopenharmony_ci return rc; 7728c2ecf20Sopenharmony_ci} 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_cistatic inline void __sk_add_node(struct sock *sk, struct hlist_head *list) 7758c2ecf20Sopenharmony_ci{ 7768c2ecf20Sopenharmony_ci hlist_add_head(&sk->sk_node, list); 7778c2ecf20Sopenharmony_ci} 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_cistatic inline void sk_add_node(struct sock *sk, struct hlist_head *list) 7808c2ecf20Sopenharmony_ci{ 7818c2ecf20Sopenharmony_ci sock_hold(sk); 7828c2ecf20Sopenharmony_ci __sk_add_node(sk, list); 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_cistatic inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list) 7868c2ecf20Sopenharmony_ci{ 7878c2ecf20Sopenharmony_ci sock_hold(sk); 7888c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 7898c2ecf20Sopenharmony_ci sk->sk_family == AF_INET6) 7908c2ecf20Sopenharmony_ci hlist_add_tail_rcu(&sk->sk_node, list); 7918c2ecf20Sopenharmony_ci else 7928c2ecf20Sopenharmony_ci hlist_add_head_rcu(&sk->sk_node, list); 7938c2ecf20Sopenharmony_ci} 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_cistatic inline void sk_add_node_tail_rcu(struct sock *sk, struct hlist_head *list) 7968c2ecf20Sopenharmony_ci{ 7978c2ecf20Sopenharmony_ci sock_hold(sk); 7988c2ecf20Sopenharmony_ci hlist_add_tail_rcu(&sk->sk_node, list); 7998c2ecf20Sopenharmony_ci} 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_cistatic inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list) 8028c2ecf20Sopenharmony_ci{ 8038c2ecf20Sopenharmony_ci hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list); 8048c2ecf20Sopenharmony_ci} 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_cistatic inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nulls_head *list) 8078c2ecf20Sopenharmony_ci{ 8088c2ecf20Sopenharmony_ci hlist_nulls_add_tail_rcu(&sk->sk_nulls_node, list); 8098c2ecf20Sopenharmony_ci} 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_cistatic inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list) 8128c2ecf20Sopenharmony_ci{ 8138c2ecf20Sopenharmony_ci sock_hold(sk); 8148c2ecf20Sopenharmony_ci __sk_nulls_add_node_rcu(sk, list); 8158c2ecf20Sopenharmony_ci} 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_cistatic inline void __sk_del_bind_node(struct sock *sk) 8188c2ecf20Sopenharmony_ci{ 8198c2ecf20Sopenharmony_ci __hlist_del(&sk->sk_bind_node); 8208c2ecf20Sopenharmony_ci} 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_cistatic inline void sk_add_bind_node(struct sock *sk, 8238c2ecf20Sopenharmony_ci struct hlist_head *list) 8248c2ecf20Sopenharmony_ci{ 8258c2ecf20Sopenharmony_ci hlist_add_head(&sk->sk_bind_node, list); 8268c2ecf20Sopenharmony_ci} 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci#define sk_for_each(__sk, list) \ 8298c2ecf20Sopenharmony_ci hlist_for_each_entry(__sk, list, sk_node) 8308c2ecf20Sopenharmony_ci#define sk_for_each_rcu(__sk, list) \ 8318c2ecf20Sopenharmony_ci hlist_for_each_entry_rcu(__sk, list, sk_node) 8328c2ecf20Sopenharmony_ci#define sk_nulls_for_each(__sk, node, list) \ 8338c2ecf20Sopenharmony_ci hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node) 8348c2ecf20Sopenharmony_ci#define sk_nulls_for_each_rcu(__sk, node, list) \ 8358c2ecf20Sopenharmony_ci hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node) 8368c2ecf20Sopenharmony_ci#define sk_for_each_from(__sk) \ 8378c2ecf20Sopenharmony_ci hlist_for_each_entry_from(__sk, sk_node) 8388c2ecf20Sopenharmony_ci#define sk_nulls_for_each_from(__sk, node) \ 8398c2ecf20Sopenharmony_ci if (__sk && ({ node = &(__sk)->sk_nulls_node; 1; })) \ 8408c2ecf20Sopenharmony_ci hlist_nulls_for_each_entry_from(__sk, node, sk_nulls_node) 8418c2ecf20Sopenharmony_ci#define sk_for_each_safe(__sk, tmp, list) \ 8428c2ecf20Sopenharmony_ci hlist_for_each_entry_safe(__sk, tmp, list, sk_node) 8438c2ecf20Sopenharmony_ci#define sk_for_each_bound(__sk, list) \ 8448c2ecf20Sopenharmony_ci hlist_for_each_entry(__sk, list, sk_bind_node) 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci/** 8478c2ecf20Sopenharmony_ci * sk_for_each_entry_offset_rcu - iterate over a list at a given struct offset 8488c2ecf20Sopenharmony_ci * @tpos: the type * to use as a loop cursor. 8498c2ecf20Sopenharmony_ci * @pos: the &struct hlist_node to use as a loop cursor. 8508c2ecf20Sopenharmony_ci * @head: the head for your list. 8518c2ecf20Sopenharmony_ci * @offset: offset of hlist_node within the struct. 8528c2ecf20Sopenharmony_ci * 8538c2ecf20Sopenharmony_ci */ 8548c2ecf20Sopenharmony_ci#define sk_for_each_entry_offset_rcu(tpos, pos, head, offset) \ 8558c2ecf20Sopenharmony_ci for (pos = rcu_dereference(hlist_first_rcu(head)); \ 8568c2ecf20Sopenharmony_ci pos != NULL && \ 8578c2ecf20Sopenharmony_ci ({ tpos = (typeof(*tpos) *)((void *)pos - offset); 1;}); \ 8588c2ecf20Sopenharmony_ci pos = rcu_dereference(hlist_next_rcu(pos))) 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_cistatic inline struct user_namespace *sk_user_ns(struct sock *sk) 8618c2ecf20Sopenharmony_ci{ 8628c2ecf20Sopenharmony_ci /* Careful only use this in a context where these parameters 8638c2ecf20Sopenharmony_ci * can not change and must all be valid, such as recvmsg from 8648c2ecf20Sopenharmony_ci * userspace. 8658c2ecf20Sopenharmony_ci */ 8668c2ecf20Sopenharmony_ci return sk->sk_socket->file->f_cred->user_ns; 8678c2ecf20Sopenharmony_ci} 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci/* Sock flags */ 8708c2ecf20Sopenharmony_cienum sock_flags { 8718c2ecf20Sopenharmony_ci SOCK_DEAD, 8728c2ecf20Sopenharmony_ci SOCK_DONE, 8738c2ecf20Sopenharmony_ci SOCK_URGINLINE, 8748c2ecf20Sopenharmony_ci SOCK_KEEPOPEN, 8758c2ecf20Sopenharmony_ci SOCK_LINGER, 8768c2ecf20Sopenharmony_ci SOCK_DESTROY, 8778c2ecf20Sopenharmony_ci SOCK_BROADCAST, 8788c2ecf20Sopenharmony_ci SOCK_TIMESTAMP, 8798c2ecf20Sopenharmony_ci SOCK_ZAPPED, 8808c2ecf20Sopenharmony_ci SOCK_USE_WRITE_QUEUE, /* whether to call sk->sk_write_space in sock_wfree */ 8818c2ecf20Sopenharmony_ci SOCK_DBG, /* %SO_DEBUG setting */ 8828c2ecf20Sopenharmony_ci SOCK_RCVTSTAMP, /* %SO_TIMESTAMP setting */ 8838c2ecf20Sopenharmony_ci SOCK_RCVTSTAMPNS, /* %SO_TIMESTAMPNS setting */ 8848c2ecf20Sopenharmony_ci SOCK_LOCALROUTE, /* route locally only, %SO_DONTROUTE setting */ 8858c2ecf20Sopenharmony_ci SOCK_MEMALLOC, /* VM depends on this socket for swapping */ 8868c2ecf20Sopenharmony_ci SOCK_TIMESTAMPING_RX_SOFTWARE, /* %SOF_TIMESTAMPING_RX_SOFTWARE */ 8878c2ecf20Sopenharmony_ci SOCK_FASYNC, /* fasync() active */ 8888c2ecf20Sopenharmony_ci SOCK_RXQ_OVFL, 8898c2ecf20Sopenharmony_ci SOCK_ZEROCOPY, /* buffers from userspace */ 8908c2ecf20Sopenharmony_ci SOCK_WIFI_STATUS, /* push wifi status to userspace */ 8918c2ecf20Sopenharmony_ci SOCK_NOFCS, /* Tell NIC not to do the Ethernet FCS. 8928c2ecf20Sopenharmony_ci * Will use last 4 bytes of packet sent from 8938c2ecf20Sopenharmony_ci * user-space instead. 8948c2ecf20Sopenharmony_ci */ 8958c2ecf20Sopenharmony_ci SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */ 8968c2ecf20Sopenharmony_ci SOCK_SELECT_ERR_QUEUE, /* Wake select on error queue */ 8978c2ecf20Sopenharmony_ci SOCK_RCU_FREE, /* wait rcu grace period in sk_destruct() */ 8988c2ecf20Sopenharmony_ci SOCK_TXTIME, 8998c2ecf20Sopenharmony_ci SOCK_XDP, /* XDP is attached */ 9008c2ecf20Sopenharmony_ci SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */ 9018c2ecf20Sopenharmony_ci}; 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ci#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE)) 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_cistatic inline void sock_copy_flags(struct sock *nsk, struct sock *osk) 9068c2ecf20Sopenharmony_ci{ 9078c2ecf20Sopenharmony_ci nsk->sk_flags = osk->sk_flags; 9088c2ecf20Sopenharmony_ci} 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_cistatic inline void sock_set_flag(struct sock *sk, enum sock_flags flag) 9118c2ecf20Sopenharmony_ci{ 9128c2ecf20Sopenharmony_ci __set_bit(flag, &sk->sk_flags); 9138c2ecf20Sopenharmony_ci} 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_cistatic inline void sock_reset_flag(struct sock *sk, enum sock_flags flag) 9168c2ecf20Sopenharmony_ci{ 9178c2ecf20Sopenharmony_ci __clear_bit(flag, &sk->sk_flags); 9188c2ecf20Sopenharmony_ci} 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_cistatic inline void sock_valbool_flag(struct sock *sk, enum sock_flags bit, 9218c2ecf20Sopenharmony_ci int valbool) 9228c2ecf20Sopenharmony_ci{ 9238c2ecf20Sopenharmony_ci if (valbool) 9248c2ecf20Sopenharmony_ci sock_set_flag(sk, bit); 9258c2ecf20Sopenharmony_ci else 9268c2ecf20Sopenharmony_ci sock_reset_flag(sk, bit); 9278c2ecf20Sopenharmony_ci} 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_cistatic inline bool sock_flag(const struct sock *sk, enum sock_flags flag) 9308c2ecf20Sopenharmony_ci{ 9318c2ecf20Sopenharmony_ci return test_bit(flag, &sk->sk_flags); 9328c2ecf20Sopenharmony_ci} 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci#ifdef CONFIG_NET 9358c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(memalloc_socks_key); 9368c2ecf20Sopenharmony_cistatic inline int sk_memalloc_socks(void) 9378c2ecf20Sopenharmony_ci{ 9388c2ecf20Sopenharmony_ci return static_branch_unlikely(&memalloc_socks_key); 9398c2ecf20Sopenharmony_ci} 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_civoid __receive_sock(struct file *file); 9428c2ecf20Sopenharmony_ci#else 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_cistatic inline int sk_memalloc_socks(void) 9458c2ecf20Sopenharmony_ci{ 9468c2ecf20Sopenharmony_ci return 0; 9478c2ecf20Sopenharmony_ci} 9488c2ecf20Sopenharmony_ci 9498c2ecf20Sopenharmony_cistatic inline void __receive_sock(struct file *file) 9508c2ecf20Sopenharmony_ci{ } 9518c2ecf20Sopenharmony_ci#endif 9528c2ecf20Sopenharmony_ci 9538c2ecf20Sopenharmony_cistatic inline gfp_t sk_gfp_mask(const struct sock *sk, gfp_t gfp_mask) 9548c2ecf20Sopenharmony_ci{ 9558c2ecf20Sopenharmony_ci return gfp_mask | (sk->sk_allocation & __GFP_MEMALLOC); 9568c2ecf20Sopenharmony_ci} 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_cistatic inline void sk_acceptq_removed(struct sock *sk) 9598c2ecf20Sopenharmony_ci{ 9608c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog - 1); 9618c2ecf20Sopenharmony_ci} 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_cistatic inline void sk_acceptq_added(struct sock *sk) 9648c2ecf20Sopenharmony_ci{ 9658c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog + 1); 9668c2ecf20Sopenharmony_ci} 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_cistatic inline bool sk_acceptq_is_full(const struct sock *sk) 9698c2ecf20Sopenharmony_ci{ 9708c2ecf20Sopenharmony_ci return READ_ONCE(sk->sk_ack_backlog) > READ_ONCE(sk->sk_max_ack_backlog); 9718c2ecf20Sopenharmony_ci} 9728c2ecf20Sopenharmony_ci 9738c2ecf20Sopenharmony_ci/* 9748c2ecf20Sopenharmony_ci * Compute minimal free write space needed to queue new packets. 9758c2ecf20Sopenharmony_ci */ 9768c2ecf20Sopenharmony_cistatic inline int sk_stream_min_wspace(const struct sock *sk) 9778c2ecf20Sopenharmony_ci{ 9788c2ecf20Sopenharmony_ci return READ_ONCE(sk->sk_wmem_queued) >> 1; 9798c2ecf20Sopenharmony_ci} 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_cistatic inline int sk_stream_wspace(const struct sock *sk) 9828c2ecf20Sopenharmony_ci{ 9838c2ecf20Sopenharmony_ci return READ_ONCE(sk->sk_sndbuf) - READ_ONCE(sk->sk_wmem_queued); 9848c2ecf20Sopenharmony_ci} 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_cistatic inline void sk_wmem_queued_add(struct sock *sk, int val) 9878c2ecf20Sopenharmony_ci{ 9888c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_wmem_queued, sk->sk_wmem_queued + val); 9898c2ecf20Sopenharmony_ci} 9908c2ecf20Sopenharmony_ci 9918c2ecf20Sopenharmony_civoid sk_stream_write_space(struct sock *sk); 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci/* OOB backlog add */ 9948c2ecf20Sopenharmony_cistatic inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb) 9958c2ecf20Sopenharmony_ci{ 9968c2ecf20Sopenharmony_ci /* dont let skb dst not refcounted, we are going to leave rcu lock */ 9978c2ecf20Sopenharmony_ci skb_dst_force(skb); 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci if (!sk->sk_backlog.tail) 10008c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_backlog.head, skb); 10018c2ecf20Sopenharmony_ci else 10028c2ecf20Sopenharmony_ci sk->sk_backlog.tail->next = skb; 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_backlog.tail, skb); 10058c2ecf20Sopenharmony_ci skb->next = NULL; 10068c2ecf20Sopenharmony_ci} 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci/* 10098c2ecf20Sopenharmony_ci * Take into account size of receive queue and backlog queue 10108c2ecf20Sopenharmony_ci * Do not take into account this skb truesize, 10118c2ecf20Sopenharmony_ci * to allow even a single big packet to come. 10128c2ecf20Sopenharmony_ci */ 10138c2ecf20Sopenharmony_cistatic inline bool sk_rcvqueues_full(const struct sock *sk, unsigned int limit) 10148c2ecf20Sopenharmony_ci{ 10158c2ecf20Sopenharmony_ci unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc); 10168c2ecf20Sopenharmony_ci 10178c2ecf20Sopenharmony_ci return qsize > limit; 10188c2ecf20Sopenharmony_ci} 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci/* The per-socket spinlock must be held here. */ 10218c2ecf20Sopenharmony_cistatic inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb, 10228c2ecf20Sopenharmony_ci unsigned int limit) 10238c2ecf20Sopenharmony_ci{ 10248c2ecf20Sopenharmony_ci if (sk_rcvqueues_full(sk, limit)) 10258c2ecf20Sopenharmony_ci return -ENOBUFS; 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci /* 10288c2ecf20Sopenharmony_ci * If the skb was allocated from pfmemalloc reserves, only 10298c2ecf20Sopenharmony_ci * allow SOCK_MEMALLOC sockets to use it as this socket is 10308c2ecf20Sopenharmony_ci * helping free memory 10318c2ecf20Sopenharmony_ci */ 10328c2ecf20Sopenharmony_ci if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) 10338c2ecf20Sopenharmony_ci return -ENOMEM; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci __sk_add_backlog(sk, skb); 10368c2ecf20Sopenharmony_ci sk->sk_backlog.len += skb->truesize; 10378c2ecf20Sopenharmony_ci return 0; 10388c2ecf20Sopenharmony_ci} 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ciint __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb); 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_cistatic inline int sk_backlog_rcv(struct sock *sk, struct sk_buff *skb) 10438c2ecf20Sopenharmony_ci{ 10448c2ecf20Sopenharmony_ci if (sk_memalloc_socks() && skb_pfmemalloc(skb)) 10458c2ecf20Sopenharmony_ci return __sk_backlog_rcv(sk, skb); 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci return sk->sk_backlog_rcv(sk, skb); 10488c2ecf20Sopenharmony_ci} 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_cistatic inline void sk_incoming_cpu_update(struct sock *sk) 10518c2ecf20Sopenharmony_ci{ 10528c2ecf20Sopenharmony_ci int cpu = raw_smp_processor_id(); 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ci if (unlikely(READ_ONCE(sk->sk_incoming_cpu) != cpu)) 10558c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_incoming_cpu, cpu); 10568c2ecf20Sopenharmony_ci} 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_cistatic inline void sock_rps_record_flow_hash(__u32 hash) 10598c2ecf20Sopenharmony_ci{ 10608c2ecf20Sopenharmony_ci#ifdef CONFIG_RPS 10618c2ecf20Sopenharmony_ci struct rps_sock_flow_table *sock_flow_table; 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci rcu_read_lock(); 10648c2ecf20Sopenharmony_ci sock_flow_table = rcu_dereference(rps_sock_flow_table); 10658c2ecf20Sopenharmony_ci rps_record_sock_flow(sock_flow_table, hash); 10668c2ecf20Sopenharmony_ci rcu_read_unlock(); 10678c2ecf20Sopenharmony_ci#endif 10688c2ecf20Sopenharmony_ci} 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_cistatic inline void sock_rps_record_flow(const struct sock *sk) 10718c2ecf20Sopenharmony_ci{ 10728c2ecf20Sopenharmony_ci#ifdef CONFIG_RPS 10738c2ecf20Sopenharmony_ci if (static_branch_unlikely(&rfs_needed)) { 10748c2ecf20Sopenharmony_ci /* Reading sk->sk_rxhash might incur an expensive cache line 10758c2ecf20Sopenharmony_ci * miss. 10768c2ecf20Sopenharmony_ci * 10778c2ecf20Sopenharmony_ci * TCP_ESTABLISHED does cover almost all states where RFS 10788c2ecf20Sopenharmony_ci * might be useful, and is cheaper [1] than testing : 10798c2ecf20Sopenharmony_ci * IPv4: inet_sk(sk)->inet_daddr 10808c2ecf20Sopenharmony_ci * IPv6: ipv6_addr_any(&sk->sk_v6_daddr) 10818c2ecf20Sopenharmony_ci * OR an additional socket flag 10828c2ecf20Sopenharmony_ci * [1] : sk_state and sk_prot are in the same cache line. 10838c2ecf20Sopenharmony_ci */ 10848c2ecf20Sopenharmony_ci if (sk->sk_state == TCP_ESTABLISHED) { 10858c2ecf20Sopenharmony_ci /* This READ_ONCE() is paired with the WRITE_ONCE() 10868c2ecf20Sopenharmony_ci * from sock_rps_save_rxhash() and sock_rps_reset_rxhash(). 10878c2ecf20Sopenharmony_ci */ 10888c2ecf20Sopenharmony_ci sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash)); 10898c2ecf20Sopenharmony_ci } 10908c2ecf20Sopenharmony_ci } 10918c2ecf20Sopenharmony_ci#endif 10928c2ecf20Sopenharmony_ci} 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_cistatic inline void sock_rps_save_rxhash(struct sock *sk, 10958c2ecf20Sopenharmony_ci const struct sk_buff *skb) 10968c2ecf20Sopenharmony_ci{ 10978c2ecf20Sopenharmony_ci#ifdef CONFIG_RPS 10988c2ecf20Sopenharmony_ci /* The following WRITE_ONCE() is paired with the READ_ONCE() 10998c2ecf20Sopenharmony_ci * here, and another one in sock_rps_record_flow(). 11008c2ecf20Sopenharmony_ci */ 11018c2ecf20Sopenharmony_ci if (unlikely(READ_ONCE(sk->sk_rxhash) != skb->hash)) 11028c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_rxhash, skb->hash); 11038c2ecf20Sopenharmony_ci#endif 11048c2ecf20Sopenharmony_ci} 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_cistatic inline void sock_rps_reset_rxhash(struct sock *sk) 11078c2ecf20Sopenharmony_ci{ 11088c2ecf20Sopenharmony_ci#ifdef CONFIG_RPS 11098c2ecf20Sopenharmony_ci /* Paired with READ_ONCE() in sock_rps_record_flow() */ 11108c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_rxhash, 0); 11118c2ecf20Sopenharmony_ci#endif 11128c2ecf20Sopenharmony_ci} 11138c2ecf20Sopenharmony_ci 11148c2ecf20Sopenharmony_ci#define sk_wait_event(__sk, __timeo, __condition, __wait) \ 11158c2ecf20Sopenharmony_ci ({ int __rc; \ 11168c2ecf20Sopenharmony_ci __sk->sk_wait_pending++; \ 11178c2ecf20Sopenharmony_ci release_sock(__sk); \ 11188c2ecf20Sopenharmony_ci __rc = __condition; \ 11198c2ecf20Sopenharmony_ci if (!__rc) { \ 11208c2ecf20Sopenharmony_ci *(__timeo) = wait_woken(__wait, \ 11218c2ecf20Sopenharmony_ci TASK_INTERRUPTIBLE, \ 11228c2ecf20Sopenharmony_ci *(__timeo)); \ 11238c2ecf20Sopenharmony_ci } \ 11248c2ecf20Sopenharmony_ci sched_annotate_sleep(); \ 11258c2ecf20Sopenharmony_ci lock_sock(__sk); \ 11268c2ecf20Sopenharmony_ci __sk->sk_wait_pending--; \ 11278c2ecf20Sopenharmony_ci __rc = __condition; \ 11288c2ecf20Sopenharmony_ci __rc; \ 11298c2ecf20Sopenharmony_ci }) 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ciint sk_stream_wait_connect(struct sock *sk, long *timeo_p); 11328c2ecf20Sopenharmony_ciint sk_stream_wait_memory(struct sock *sk, long *timeo_p); 11338c2ecf20Sopenharmony_civoid sk_stream_wait_close(struct sock *sk, long timeo_p); 11348c2ecf20Sopenharmony_ciint sk_stream_error(struct sock *sk, int flags, int err); 11358c2ecf20Sopenharmony_civoid sk_stream_kill_queues(struct sock *sk); 11368c2ecf20Sopenharmony_civoid sk_set_memalloc(struct sock *sk); 11378c2ecf20Sopenharmony_civoid sk_clear_memalloc(struct sock *sk); 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_civoid __sk_flush_backlog(struct sock *sk); 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_cistatic inline bool sk_flush_backlog(struct sock *sk) 11428c2ecf20Sopenharmony_ci{ 11438c2ecf20Sopenharmony_ci if (unlikely(READ_ONCE(sk->sk_backlog.tail))) { 11448c2ecf20Sopenharmony_ci __sk_flush_backlog(sk); 11458c2ecf20Sopenharmony_ci return true; 11468c2ecf20Sopenharmony_ci } 11478c2ecf20Sopenharmony_ci return false; 11488c2ecf20Sopenharmony_ci} 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ciint sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb); 11518c2ecf20Sopenharmony_ci 11528c2ecf20Sopenharmony_cistruct request_sock_ops; 11538c2ecf20Sopenharmony_cistruct timewait_sock_ops; 11548c2ecf20Sopenharmony_cistruct inet_hashinfo; 11558c2ecf20Sopenharmony_cistruct raw_hashinfo; 11568c2ecf20Sopenharmony_cistruct smc_hashinfo; 11578c2ecf20Sopenharmony_cistruct module; 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci/* 11608c2ecf20Sopenharmony_ci * caches using SLAB_TYPESAFE_BY_RCU should let .next pointer from nulls nodes 11618c2ecf20Sopenharmony_ci * un-modified. Special care is taken when initializing object to zero. 11628c2ecf20Sopenharmony_ci */ 11638c2ecf20Sopenharmony_cistatic inline void sk_prot_clear_nulls(struct sock *sk, int size) 11648c2ecf20Sopenharmony_ci{ 11658c2ecf20Sopenharmony_ci if (offsetof(struct sock, sk_node.next) != 0) 11668c2ecf20Sopenharmony_ci memset(sk, 0, offsetof(struct sock, sk_node.next)); 11678c2ecf20Sopenharmony_ci memset(&sk->sk_node.pprev, 0, 11688c2ecf20Sopenharmony_ci size - offsetof(struct sock, sk_node.pprev)); 11698c2ecf20Sopenharmony_ci} 11708c2ecf20Sopenharmony_ci 11718c2ecf20Sopenharmony_ci/* Networking protocol blocks we attach to sockets. 11728c2ecf20Sopenharmony_ci * socket layer -> transport layer interface 11738c2ecf20Sopenharmony_ci */ 11748c2ecf20Sopenharmony_cistruct proto { 11758c2ecf20Sopenharmony_ci void (*close)(struct sock *sk, 11768c2ecf20Sopenharmony_ci long timeout); 11778c2ecf20Sopenharmony_ci int (*pre_connect)(struct sock *sk, 11788c2ecf20Sopenharmony_ci struct sockaddr *uaddr, 11798c2ecf20Sopenharmony_ci int addr_len); 11808c2ecf20Sopenharmony_ci int (*connect)(struct sock *sk, 11818c2ecf20Sopenharmony_ci struct sockaddr *uaddr, 11828c2ecf20Sopenharmony_ci int addr_len); 11838c2ecf20Sopenharmony_ci int (*disconnect)(struct sock *sk, int flags); 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_ci struct sock * (*accept)(struct sock *sk, int flags, int *err, 11868c2ecf20Sopenharmony_ci bool kern); 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_ci int (*ioctl)(struct sock *sk, int cmd, 11898c2ecf20Sopenharmony_ci unsigned long arg); 11908c2ecf20Sopenharmony_ci int (*init)(struct sock *sk); 11918c2ecf20Sopenharmony_ci void (*destroy)(struct sock *sk); 11928c2ecf20Sopenharmony_ci void (*shutdown)(struct sock *sk, int how); 11938c2ecf20Sopenharmony_ci int (*setsockopt)(struct sock *sk, int level, 11948c2ecf20Sopenharmony_ci int optname, sockptr_t optval, 11958c2ecf20Sopenharmony_ci unsigned int optlen); 11968c2ecf20Sopenharmony_ci int (*getsockopt)(struct sock *sk, int level, 11978c2ecf20Sopenharmony_ci int optname, char __user *optval, 11988c2ecf20Sopenharmony_ci int __user *option); 11998c2ecf20Sopenharmony_ci void (*keepalive)(struct sock *sk, int valbool); 12008c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT 12018c2ecf20Sopenharmony_ci int (*compat_ioctl)(struct sock *sk, 12028c2ecf20Sopenharmony_ci unsigned int cmd, unsigned long arg); 12038c2ecf20Sopenharmony_ci#endif 12048c2ecf20Sopenharmony_ci int (*sendmsg)(struct sock *sk, struct msghdr *msg, 12058c2ecf20Sopenharmony_ci size_t len); 12068c2ecf20Sopenharmony_ci int (*recvmsg)(struct sock *sk, struct msghdr *msg, 12078c2ecf20Sopenharmony_ci size_t len, int noblock, int flags, 12088c2ecf20Sopenharmony_ci int *addr_len); 12098c2ecf20Sopenharmony_ci int (*sendpage)(struct sock *sk, struct page *page, 12108c2ecf20Sopenharmony_ci int offset, size_t size, int flags); 12118c2ecf20Sopenharmony_ci int (*bind)(struct sock *sk, 12128c2ecf20Sopenharmony_ci struct sockaddr *addr, int addr_len); 12138c2ecf20Sopenharmony_ci int (*bind_add)(struct sock *sk, 12148c2ecf20Sopenharmony_ci struct sockaddr *addr, int addr_len); 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ci int (*backlog_rcv) (struct sock *sk, 12178c2ecf20Sopenharmony_ci struct sk_buff *skb); 12188c2ecf20Sopenharmony_ci bool (*bpf_bypass_getsockopt)(int level, 12198c2ecf20Sopenharmony_ci int optname); 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ci void (*release_cb)(struct sock *sk); 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_ci /* Keeping track of sk's, looking them up, and port selection methods. */ 12248c2ecf20Sopenharmony_ci int (*hash)(struct sock *sk); 12258c2ecf20Sopenharmony_ci void (*unhash)(struct sock *sk); 12268c2ecf20Sopenharmony_ci void (*rehash)(struct sock *sk); 12278c2ecf20Sopenharmony_ci int (*get_port)(struct sock *sk, unsigned short snum); 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_ci /* Keeping track of sockets in use */ 12308c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS 12318c2ecf20Sopenharmony_ci unsigned int inuse_idx; 12328c2ecf20Sopenharmony_ci#endif 12338c2ecf20Sopenharmony_ci 12348c2ecf20Sopenharmony_ci bool (*stream_memory_free)(const struct sock *sk, int wake); 12358c2ecf20Sopenharmony_ci bool (*stream_memory_read)(const struct sock *sk); 12368c2ecf20Sopenharmony_ci /* Memory pressure */ 12378c2ecf20Sopenharmony_ci void (*enter_memory_pressure)(struct sock *sk); 12388c2ecf20Sopenharmony_ci void (*leave_memory_pressure)(struct sock *sk); 12398c2ecf20Sopenharmony_ci atomic_long_t *memory_allocated; /* Current allocated memory. */ 12408c2ecf20Sopenharmony_ci struct percpu_counter *sockets_allocated; /* Current number of sockets. */ 12418c2ecf20Sopenharmony_ci /* 12428c2ecf20Sopenharmony_ci * Pressure flag: try to collapse. 12438c2ecf20Sopenharmony_ci * Technical note: it is used by multiple contexts non atomically. 12448c2ecf20Sopenharmony_ci * Make sure to use READ_ONCE()/WRITE_ONCE() for all reads/writes. 12458c2ecf20Sopenharmony_ci * All the __sk_mem_schedule() is of this nature: accounting 12468c2ecf20Sopenharmony_ci * is strict, actions are advisory and have some latency. 12478c2ecf20Sopenharmony_ci */ 12488c2ecf20Sopenharmony_ci unsigned long *memory_pressure; 12498c2ecf20Sopenharmony_ci long *sysctl_mem; 12508c2ecf20Sopenharmony_ci 12518c2ecf20Sopenharmony_ci int *sysctl_wmem; 12528c2ecf20Sopenharmony_ci int *sysctl_rmem; 12538c2ecf20Sopenharmony_ci u32 sysctl_wmem_offset; 12548c2ecf20Sopenharmony_ci u32 sysctl_rmem_offset; 12558c2ecf20Sopenharmony_ci 12568c2ecf20Sopenharmony_ci int max_header; 12578c2ecf20Sopenharmony_ci bool no_autobind; 12588c2ecf20Sopenharmony_ci 12598c2ecf20Sopenharmony_ci struct kmem_cache *slab; 12608c2ecf20Sopenharmony_ci unsigned int obj_size; 12618c2ecf20Sopenharmony_ci slab_flags_t slab_flags; 12628c2ecf20Sopenharmony_ci unsigned int useroffset; /* Usercopy region offset */ 12638c2ecf20Sopenharmony_ci unsigned int usersize; /* Usercopy region size */ 12648c2ecf20Sopenharmony_ci 12658c2ecf20Sopenharmony_ci unsigned int __percpu *orphan_count; 12668c2ecf20Sopenharmony_ci 12678c2ecf20Sopenharmony_ci struct request_sock_ops *rsk_prot; 12688c2ecf20Sopenharmony_ci struct timewait_sock_ops *twsk_prot; 12698c2ecf20Sopenharmony_ci 12708c2ecf20Sopenharmony_ci union { 12718c2ecf20Sopenharmony_ci struct inet_hashinfo *hashinfo; 12728c2ecf20Sopenharmony_ci struct udp_table *udp_table; 12738c2ecf20Sopenharmony_ci struct raw_hashinfo *raw_hash; 12748c2ecf20Sopenharmony_ci struct smc_hashinfo *smc_hash; 12758c2ecf20Sopenharmony_ci } h; 12768c2ecf20Sopenharmony_ci 12778c2ecf20Sopenharmony_ci struct module *owner; 12788c2ecf20Sopenharmony_ci 12798c2ecf20Sopenharmony_ci char name[32]; 12808c2ecf20Sopenharmony_ci 12818c2ecf20Sopenharmony_ci struct list_head node; 12828c2ecf20Sopenharmony_ci#ifdef SOCK_REFCNT_DEBUG 12838c2ecf20Sopenharmony_ci atomic_t socks; 12848c2ecf20Sopenharmony_ci#endif 12858c2ecf20Sopenharmony_ci int (*diag_destroy)(struct sock *sk, int err); 12868c2ecf20Sopenharmony_ci} __randomize_layout; 12878c2ecf20Sopenharmony_ci 12888c2ecf20Sopenharmony_ciint proto_register(struct proto *prot, int alloc_slab); 12898c2ecf20Sopenharmony_civoid proto_unregister(struct proto *prot); 12908c2ecf20Sopenharmony_ciint sock_load_diag_module(int family, int protocol); 12918c2ecf20Sopenharmony_ci 12928c2ecf20Sopenharmony_ci#ifdef SOCK_REFCNT_DEBUG 12938c2ecf20Sopenharmony_cistatic inline void sk_refcnt_debug_inc(struct sock *sk) 12948c2ecf20Sopenharmony_ci{ 12958c2ecf20Sopenharmony_ci atomic_inc(&sk->sk_prot->socks); 12968c2ecf20Sopenharmony_ci} 12978c2ecf20Sopenharmony_ci 12988c2ecf20Sopenharmony_cistatic inline void sk_refcnt_debug_dec(struct sock *sk) 12998c2ecf20Sopenharmony_ci{ 13008c2ecf20Sopenharmony_ci atomic_dec(&sk->sk_prot->socks); 13018c2ecf20Sopenharmony_ci printk(KERN_DEBUG "%s socket %p released, %d are still alive\n", 13028c2ecf20Sopenharmony_ci sk->sk_prot->name, sk, atomic_read(&sk->sk_prot->socks)); 13038c2ecf20Sopenharmony_ci} 13048c2ecf20Sopenharmony_ci 13058c2ecf20Sopenharmony_cistatic inline void sk_refcnt_debug_release(const struct sock *sk) 13068c2ecf20Sopenharmony_ci{ 13078c2ecf20Sopenharmony_ci if (refcount_read(&sk->sk_refcnt) != 1) 13088c2ecf20Sopenharmony_ci printk(KERN_DEBUG "Destruction of the %s socket %p delayed, refcnt=%d\n", 13098c2ecf20Sopenharmony_ci sk->sk_prot->name, sk, refcount_read(&sk->sk_refcnt)); 13108c2ecf20Sopenharmony_ci} 13118c2ecf20Sopenharmony_ci#else /* SOCK_REFCNT_DEBUG */ 13128c2ecf20Sopenharmony_ci#define sk_refcnt_debug_inc(sk) do { } while (0) 13138c2ecf20Sopenharmony_ci#define sk_refcnt_debug_dec(sk) do { } while (0) 13148c2ecf20Sopenharmony_ci#define sk_refcnt_debug_release(sk) do { } while (0) 13158c2ecf20Sopenharmony_ci#endif /* SOCK_REFCNT_DEBUG */ 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_cistatic inline bool __sk_stream_memory_free(const struct sock *sk, int wake) 13188c2ecf20Sopenharmony_ci{ 13198c2ecf20Sopenharmony_ci if (READ_ONCE(sk->sk_wmem_queued) >= READ_ONCE(sk->sk_sndbuf)) 13208c2ecf20Sopenharmony_ci return false; 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci return sk->sk_prot->stream_memory_free ? 13238c2ecf20Sopenharmony_ci sk->sk_prot->stream_memory_free(sk, wake) : true; 13248c2ecf20Sopenharmony_ci} 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_cistatic inline bool sk_stream_memory_free(const struct sock *sk) 13278c2ecf20Sopenharmony_ci{ 13288c2ecf20Sopenharmony_ci return __sk_stream_memory_free(sk, 0); 13298c2ecf20Sopenharmony_ci} 13308c2ecf20Sopenharmony_ci 13318c2ecf20Sopenharmony_cistatic inline bool __sk_stream_is_writeable(const struct sock *sk, int wake) 13328c2ecf20Sopenharmony_ci{ 13338c2ecf20Sopenharmony_ci return sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && 13348c2ecf20Sopenharmony_ci __sk_stream_memory_free(sk, wake); 13358c2ecf20Sopenharmony_ci} 13368c2ecf20Sopenharmony_ci 13378c2ecf20Sopenharmony_cistatic inline bool sk_stream_is_writeable(const struct sock *sk) 13388c2ecf20Sopenharmony_ci{ 13398c2ecf20Sopenharmony_ci return __sk_stream_is_writeable(sk, 0); 13408c2ecf20Sopenharmony_ci} 13418c2ecf20Sopenharmony_ci 13428c2ecf20Sopenharmony_cistatic inline int sk_under_cgroup_hierarchy(struct sock *sk, 13438c2ecf20Sopenharmony_ci struct cgroup *ancestor) 13448c2ecf20Sopenharmony_ci{ 13458c2ecf20Sopenharmony_ci#ifdef CONFIG_SOCK_CGROUP_DATA 13468c2ecf20Sopenharmony_ci return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data), 13478c2ecf20Sopenharmony_ci ancestor); 13488c2ecf20Sopenharmony_ci#else 13498c2ecf20Sopenharmony_ci return -ENOTSUPP; 13508c2ecf20Sopenharmony_ci#endif 13518c2ecf20Sopenharmony_ci} 13528c2ecf20Sopenharmony_ci 13538c2ecf20Sopenharmony_cistatic inline bool sk_has_memory_pressure(const struct sock *sk) 13548c2ecf20Sopenharmony_ci{ 13558c2ecf20Sopenharmony_ci return sk->sk_prot->memory_pressure != NULL; 13568c2ecf20Sopenharmony_ci} 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_cistatic inline bool sk_under_global_memory_pressure(const struct sock *sk) 13598c2ecf20Sopenharmony_ci{ 13608c2ecf20Sopenharmony_ci return sk->sk_prot->memory_pressure && 13618c2ecf20Sopenharmony_ci !!READ_ONCE(*sk->sk_prot->memory_pressure); 13628c2ecf20Sopenharmony_ci} 13638c2ecf20Sopenharmony_ci 13648c2ecf20Sopenharmony_cistatic inline bool sk_under_memory_pressure(const struct sock *sk) 13658c2ecf20Sopenharmony_ci{ 13668c2ecf20Sopenharmony_ci if (!sk->sk_prot->memory_pressure) 13678c2ecf20Sopenharmony_ci return false; 13688c2ecf20Sopenharmony_ci 13698c2ecf20Sopenharmony_ci if (mem_cgroup_sockets_enabled && sk->sk_memcg && 13708c2ecf20Sopenharmony_ci mem_cgroup_under_socket_pressure(sk->sk_memcg)) 13718c2ecf20Sopenharmony_ci return true; 13728c2ecf20Sopenharmony_ci 13738c2ecf20Sopenharmony_ci return !!READ_ONCE(*sk->sk_prot->memory_pressure); 13748c2ecf20Sopenharmony_ci} 13758c2ecf20Sopenharmony_ci 13768c2ecf20Sopenharmony_cistatic inline long 13778c2ecf20Sopenharmony_cisk_memory_allocated(const struct sock *sk) 13788c2ecf20Sopenharmony_ci{ 13798c2ecf20Sopenharmony_ci return atomic_long_read(sk->sk_prot->memory_allocated); 13808c2ecf20Sopenharmony_ci} 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_cistatic inline long 13838c2ecf20Sopenharmony_cisk_memory_allocated_add(struct sock *sk, int amt) 13848c2ecf20Sopenharmony_ci{ 13858c2ecf20Sopenharmony_ci return atomic_long_add_return(amt, sk->sk_prot->memory_allocated); 13868c2ecf20Sopenharmony_ci} 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_cistatic inline void 13898c2ecf20Sopenharmony_cisk_memory_allocated_sub(struct sock *sk, int amt) 13908c2ecf20Sopenharmony_ci{ 13918c2ecf20Sopenharmony_ci atomic_long_sub(amt, sk->sk_prot->memory_allocated); 13928c2ecf20Sopenharmony_ci} 13938c2ecf20Sopenharmony_ci 13948c2ecf20Sopenharmony_cistatic inline void sk_sockets_allocated_dec(struct sock *sk) 13958c2ecf20Sopenharmony_ci{ 13968c2ecf20Sopenharmony_ci percpu_counter_dec(sk->sk_prot->sockets_allocated); 13978c2ecf20Sopenharmony_ci} 13988c2ecf20Sopenharmony_ci 13998c2ecf20Sopenharmony_cistatic inline void sk_sockets_allocated_inc(struct sock *sk) 14008c2ecf20Sopenharmony_ci{ 14018c2ecf20Sopenharmony_ci percpu_counter_inc(sk->sk_prot->sockets_allocated); 14028c2ecf20Sopenharmony_ci} 14038c2ecf20Sopenharmony_ci 14048c2ecf20Sopenharmony_cistatic inline u64 14058c2ecf20Sopenharmony_cisk_sockets_allocated_read_positive(struct sock *sk) 14068c2ecf20Sopenharmony_ci{ 14078c2ecf20Sopenharmony_ci return percpu_counter_read_positive(sk->sk_prot->sockets_allocated); 14088c2ecf20Sopenharmony_ci} 14098c2ecf20Sopenharmony_ci 14108c2ecf20Sopenharmony_cistatic inline int 14118c2ecf20Sopenharmony_ciproto_sockets_allocated_sum_positive(struct proto *prot) 14128c2ecf20Sopenharmony_ci{ 14138c2ecf20Sopenharmony_ci return percpu_counter_sum_positive(prot->sockets_allocated); 14148c2ecf20Sopenharmony_ci} 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_cistatic inline long 14178c2ecf20Sopenharmony_ciproto_memory_allocated(struct proto *prot) 14188c2ecf20Sopenharmony_ci{ 14198c2ecf20Sopenharmony_ci return atomic_long_read(prot->memory_allocated); 14208c2ecf20Sopenharmony_ci} 14218c2ecf20Sopenharmony_ci 14228c2ecf20Sopenharmony_cistatic inline bool 14238c2ecf20Sopenharmony_ciproto_memory_pressure(struct proto *prot) 14248c2ecf20Sopenharmony_ci{ 14258c2ecf20Sopenharmony_ci if (!prot->memory_pressure) 14268c2ecf20Sopenharmony_ci return false; 14278c2ecf20Sopenharmony_ci return !!READ_ONCE(*prot->memory_pressure); 14288c2ecf20Sopenharmony_ci} 14298c2ecf20Sopenharmony_ci 14308c2ecf20Sopenharmony_ci 14318c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS 14328c2ecf20Sopenharmony_ci/* Called with local bh disabled */ 14338c2ecf20Sopenharmony_civoid sock_prot_inuse_add(struct net *net, struct proto *prot, int inc); 14348c2ecf20Sopenharmony_ciint sock_prot_inuse_get(struct net *net, struct proto *proto); 14358c2ecf20Sopenharmony_ciint sock_inuse_get(struct net *net); 14368c2ecf20Sopenharmony_ci#else 14378c2ecf20Sopenharmony_cistatic inline void sock_prot_inuse_add(struct net *net, struct proto *prot, 14388c2ecf20Sopenharmony_ci int inc) 14398c2ecf20Sopenharmony_ci{ 14408c2ecf20Sopenharmony_ci} 14418c2ecf20Sopenharmony_ci#endif 14428c2ecf20Sopenharmony_ci 14438c2ecf20Sopenharmony_ci 14448c2ecf20Sopenharmony_ci/* With per-bucket locks this operation is not-atomic, so that 14458c2ecf20Sopenharmony_ci * this version is not worse. 14468c2ecf20Sopenharmony_ci */ 14478c2ecf20Sopenharmony_cistatic inline int __sk_prot_rehash(struct sock *sk) 14488c2ecf20Sopenharmony_ci{ 14498c2ecf20Sopenharmony_ci sk->sk_prot->unhash(sk); 14508c2ecf20Sopenharmony_ci return sk->sk_prot->hash(sk); 14518c2ecf20Sopenharmony_ci} 14528c2ecf20Sopenharmony_ci 14538c2ecf20Sopenharmony_ci/* About 10 seconds */ 14548c2ecf20Sopenharmony_ci#define SOCK_DESTROY_TIME (10*HZ) 14558c2ecf20Sopenharmony_ci 14568c2ecf20Sopenharmony_ci/* Sockets 0-1023 can't be bound to unless you are superuser */ 14578c2ecf20Sopenharmony_ci#define PROT_SOCK 1024 14588c2ecf20Sopenharmony_ci 14598c2ecf20Sopenharmony_ci#define SHUTDOWN_MASK 3 14608c2ecf20Sopenharmony_ci#define RCV_SHUTDOWN 1 14618c2ecf20Sopenharmony_ci#define SEND_SHUTDOWN 2 14628c2ecf20Sopenharmony_ci 14638c2ecf20Sopenharmony_ci#define SOCK_SNDBUF_LOCK 1 14648c2ecf20Sopenharmony_ci#define SOCK_RCVBUF_LOCK 2 14658c2ecf20Sopenharmony_ci#define SOCK_BINDADDR_LOCK 4 14668c2ecf20Sopenharmony_ci#define SOCK_BINDPORT_LOCK 8 14678c2ecf20Sopenharmony_ci 14688c2ecf20Sopenharmony_cistruct socket_alloc { 14698c2ecf20Sopenharmony_ci struct socket socket; 14708c2ecf20Sopenharmony_ci struct inode vfs_inode; 14718c2ecf20Sopenharmony_ci}; 14728c2ecf20Sopenharmony_ci 14738c2ecf20Sopenharmony_cistatic inline struct socket *SOCKET_I(struct inode *inode) 14748c2ecf20Sopenharmony_ci{ 14758c2ecf20Sopenharmony_ci return &container_of(inode, struct socket_alloc, vfs_inode)->socket; 14768c2ecf20Sopenharmony_ci} 14778c2ecf20Sopenharmony_ci 14788c2ecf20Sopenharmony_cistatic inline struct inode *SOCK_INODE(struct socket *socket) 14798c2ecf20Sopenharmony_ci{ 14808c2ecf20Sopenharmony_ci return &container_of(socket, struct socket_alloc, socket)->vfs_inode; 14818c2ecf20Sopenharmony_ci} 14828c2ecf20Sopenharmony_ci 14838c2ecf20Sopenharmony_ci/* 14848c2ecf20Sopenharmony_ci * Functions for memory accounting 14858c2ecf20Sopenharmony_ci */ 14868c2ecf20Sopenharmony_ciint __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind); 14878c2ecf20Sopenharmony_ciint __sk_mem_schedule(struct sock *sk, int size, int kind); 14888c2ecf20Sopenharmony_civoid __sk_mem_reduce_allocated(struct sock *sk, int amount); 14898c2ecf20Sopenharmony_civoid __sk_mem_reclaim(struct sock *sk, int amount); 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_ci/* We used to have PAGE_SIZE here, but systems with 64KB pages 14928c2ecf20Sopenharmony_ci * do not necessarily have 16x time more memory than 4KB ones. 14938c2ecf20Sopenharmony_ci */ 14948c2ecf20Sopenharmony_ci#define SK_MEM_QUANTUM 4096 14958c2ecf20Sopenharmony_ci#define SK_MEM_QUANTUM_SHIFT ilog2(SK_MEM_QUANTUM) 14968c2ecf20Sopenharmony_ci#define SK_MEM_SEND 0 14978c2ecf20Sopenharmony_ci#define SK_MEM_RECV 1 14988c2ecf20Sopenharmony_ci 14998c2ecf20Sopenharmony_ci/* sysctl_mem values are in pages, we convert them in SK_MEM_QUANTUM units */ 15008c2ecf20Sopenharmony_cistatic inline long sk_prot_mem_limits(const struct sock *sk, int index) 15018c2ecf20Sopenharmony_ci{ 15028c2ecf20Sopenharmony_ci long val = READ_ONCE(sk->sk_prot->sysctl_mem[index]); 15038c2ecf20Sopenharmony_ci 15048c2ecf20Sopenharmony_ci#if PAGE_SIZE > SK_MEM_QUANTUM 15058c2ecf20Sopenharmony_ci val <<= PAGE_SHIFT - SK_MEM_QUANTUM_SHIFT; 15068c2ecf20Sopenharmony_ci#elif PAGE_SIZE < SK_MEM_QUANTUM 15078c2ecf20Sopenharmony_ci val >>= SK_MEM_QUANTUM_SHIFT - PAGE_SHIFT; 15088c2ecf20Sopenharmony_ci#endif 15098c2ecf20Sopenharmony_ci return val; 15108c2ecf20Sopenharmony_ci} 15118c2ecf20Sopenharmony_ci 15128c2ecf20Sopenharmony_cistatic inline int sk_mem_pages(int amt) 15138c2ecf20Sopenharmony_ci{ 15148c2ecf20Sopenharmony_ci return (amt + SK_MEM_QUANTUM - 1) >> SK_MEM_QUANTUM_SHIFT; 15158c2ecf20Sopenharmony_ci} 15168c2ecf20Sopenharmony_ci 15178c2ecf20Sopenharmony_cistatic inline bool sk_has_account(struct sock *sk) 15188c2ecf20Sopenharmony_ci{ 15198c2ecf20Sopenharmony_ci /* return true if protocol supports memory accounting */ 15208c2ecf20Sopenharmony_ci return !!sk->sk_prot->memory_allocated; 15218c2ecf20Sopenharmony_ci} 15228c2ecf20Sopenharmony_ci 15238c2ecf20Sopenharmony_cistatic inline bool sk_wmem_schedule(struct sock *sk, int size) 15248c2ecf20Sopenharmony_ci{ 15258c2ecf20Sopenharmony_ci int delta; 15268c2ecf20Sopenharmony_ci 15278c2ecf20Sopenharmony_ci if (!sk_has_account(sk)) 15288c2ecf20Sopenharmony_ci return true; 15298c2ecf20Sopenharmony_ci delta = size - sk->sk_forward_alloc; 15308c2ecf20Sopenharmony_ci return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_SEND); 15318c2ecf20Sopenharmony_ci} 15328c2ecf20Sopenharmony_ci 15338c2ecf20Sopenharmony_cistatic inline bool 15348c2ecf20Sopenharmony_cisk_rmem_schedule(struct sock *sk, struct sk_buff *skb, int size) 15358c2ecf20Sopenharmony_ci{ 15368c2ecf20Sopenharmony_ci int delta; 15378c2ecf20Sopenharmony_ci 15388c2ecf20Sopenharmony_ci if (!sk_has_account(sk)) 15398c2ecf20Sopenharmony_ci return true; 15408c2ecf20Sopenharmony_ci delta = size - sk->sk_forward_alloc; 15418c2ecf20Sopenharmony_ci return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_RECV) || 15428c2ecf20Sopenharmony_ci skb_pfmemalloc(skb); 15438c2ecf20Sopenharmony_ci} 15448c2ecf20Sopenharmony_ci 15458c2ecf20Sopenharmony_cistatic inline void sk_mem_reclaim(struct sock *sk) 15468c2ecf20Sopenharmony_ci{ 15478c2ecf20Sopenharmony_ci if (!sk_has_account(sk)) 15488c2ecf20Sopenharmony_ci return; 15498c2ecf20Sopenharmony_ci if (sk->sk_forward_alloc >= SK_MEM_QUANTUM) 15508c2ecf20Sopenharmony_ci __sk_mem_reclaim(sk, sk->sk_forward_alloc); 15518c2ecf20Sopenharmony_ci} 15528c2ecf20Sopenharmony_ci 15538c2ecf20Sopenharmony_cistatic inline void sk_mem_reclaim_partial(struct sock *sk) 15548c2ecf20Sopenharmony_ci{ 15558c2ecf20Sopenharmony_ci if (!sk_has_account(sk)) 15568c2ecf20Sopenharmony_ci return; 15578c2ecf20Sopenharmony_ci if (sk->sk_forward_alloc > SK_MEM_QUANTUM) 15588c2ecf20Sopenharmony_ci __sk_mem_reclaim(sk, sk->sk_forward_alloc - 1); 15598c2ecf20Sopenharmony_ci} 15608c2ecf20Sopenharmony_ci 15618c2ecf20Sopenharmony_cistatic inline void sk_mem_charge(struct sock *sk, int size) 15628c2ecf20Sopenharmony_ci{ 15638c2ecf20Sopenharmony_ci if (!sk_has_account(sk)) 15648c2ecf20Sopenharmony_ci return; 15658c2ecf20Sopenharmony_ci sk->sk_forward_alloc -= size; 15668c2ecf20Sopenharmony_ci} 15678c2ecf20Sopenharmony_ci 15688c2ecf20Sopenharmony_cistatic inline void sk_mem_uncharge(struct sock *sk, int size) 15698c2ecf20Sopenharmony_ci{ 15708c2ecf20Sopenharmony_ci if (!sk_has_account(sk)) 15718c2ecf20Sopenharmony_ci return; 15728c2ecf20Sopenharmony_ci sk->sk_forward_alloc += size; 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci /* Avoid a possible overflow. 15758c2ecf20Sopenharmony_ci * TCP send queues can make this happen, if sk_mem_reclaim() 15768c2ecf20Sopenharmony_ci * is not called and more than 2 GBytes are released at once. 15778c2ecf20Sopenharmony_ci * 15788c2ecf20Sopenharmony_ci * If we reach 2 MBytes, reclaim 1 MBytes right now, there is 15798c2ecf20Sopenharmony_ci * no need to hold that much forward allocation anyway. 15808c2ecf20Sopenharmony_ci */ 15818c2ecf20Sopenharmony_ci if (unlikely(sk->sk_forward_alloc >= 1 << 21)) 15828c2ecf20Sopenharmony_ci __sk_mem_reclaim(sk, 1 << 20); 15838c2ecf20Sopenharmony_ci} 15848c2ecf20Sopenharmony_ci 15858c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(tcp_tx_skb_cache_key); 15868c2ecf20Sopenharmony_cistatic inline void sk_wmem_free_skb(struct sock *sk, struct sk_buff *skb) 15878c2ecf20Sopenharmony_ci{ 15888c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, -skb->truesize); 15898c2ecf20Sopenharmony_ci sk_mem_uncharge(sk, skb->truesize); 15908c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_tx_skb_cache_key) && 15918c2ecf20Sopenharmony_ci !sk->sk_tx_skb_cache && !skb_cloned(skb)) { 15928c2ecf20Sopenharmony_ci skb_ext_reset(skb); 15938c2ecf20Sopenharmony_ci skb_zcopy_clear(skb, true); 15948c2ecf20Sopenharmony_ci sk->sk_tx_skb_cache = skb; 15958c2ecf20Sopenharmony_ci return; 15968c2ecf20Sopenharmony_ci } 15978c2ecf20Sopenharmony_ci __kfree_skb(skb); 15988c2ecf20Sopenharmony_ci} 15998c2ecf20Sopenharmony_ci 16008c2ecf20Sopenharmony_cistatic inline void sock_release_ownership(struct sock *sk) 16018c2ecf20Sopenharmony_ci{ 16028c2ecf20Sopenharmony_ci if (sk->sk_lock.owned) { 16038c2ecf20Sopenharmony_ci sk->sk_lock.owned = 0; 16048c2ecf20Sopenharmony_ci 16058c2ecf20Sopenharmony_ci /* The sk_lock has mutex_unlock() semantics: */ 16068c2ecf20Sopenharmony_ci mutex_release(&sk->sk_lock.dep_map, _RET_IP_); 16078c2ecf20Sopenharmony_ci } 16088c2ecf20Sopenharmony_ci} 16098c2ecf20Sopenharmony_ci 16108c2ecf20Sopenharmony_ci/* 16118c2ecf20Sopenharmony_ci * Macro so as to not evaluate some arguments when 16128c2ecf20Sopenharmony_ci * lockdep is not enabled. 16138c2ecf20Sopenharmony_ci * 16148c2ecf20Sopenharmony_ci * Mark both the sk_lock and the sk_lock.slock as a 16158c2ecf20Sopenharmony_ci * per-address-family lock class. 16168c2ecf20Sopenharmony_ci */ 16178c2ecf20Sopenharmony_ci#define sock_lock_init_class_and_name(sk, sname, skey, name, key) \ 16188c2ecf20Sopenharmony_cido { \ 16198c2ecf20Sopenharmony_ci sk->sk_lock.owned = 0; \ 16208c2ecf20Sopenharmony_ci init_waitqueue_head(&sk->sk_lock.wq); \ 16218c2ecf20Sopenharmony_ci spin_lock_init(&(sk)->sk_lock.slock); \ 16228c2ecf20Sopenharmony_ci debug_check_no_locks_freed((void *)&(sk)->sk_lock, \ 16238c2ecf20Sopenharmony_ci sizeof((sk)->sk_lock)); \ 16248c2ecf20Sopenharmony_ci lockdep_set_class_and_name(&(sk)->sk_lock.slock, \ 16258c2ecf20Sopenharmony_ci (skey), (sname)); \ 16268c2ecf20Sopenharmony_ci lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0); \ 16278c2ecf20Sopenharmony_ci} while (0) 16288c2ecf20Sopenharmony_ci 16298c2ecf20Sopenharmony_ci#ifdef CONFIG_LOCKDEP 16308c2ecf20Sopenharmony_cistatic inline bool lockdep_sock_is_held(const struct sock *sk) 16318c2ecf20Sopenharmony_ci{ 16328c2ecf20Sopenharmony_ci return lockdep_is_held(&sk->sk_lock) || 16338c2ecf20Sopenharmony_ci lockdep_is_held(&sk->sk_lock.slock); 16348c2ecf20Sopenharmony_ci} 16358c2ecf20Sopenharmony_ci#endif 16368c2ecf20Sopenharmony_ci 16378c2ecf20Sopenharmony_civoid lock_sock_nested(struct sock *sk, int subclass); 16388c2ecf20Sopenharmony_ci 16398c2ecf20Sopenharmony_cistatic inline void lock_sock(struct sock *sk) 16408c2ecf20Sopenharmony_ci{ 16418c2ecf20Sopenharmony_ci lock_sock_nested(sk, 0); 16428c2ecf20Sopenharmony_ci} 16438c2ecf20Sopenharmony_ci 16448c2ecf20Sopenharmony_civoid __release_sock(struct sock *sk); 16458c2ecf20Sopenharmony_civoid release_sock(struct sock *sk); 16468c2ecf20Sopenharmony_ci 16478c2ecf20Sopenharmony_ci/* BH context may only use the following locking interface. */ 16488c2ecf20Sopenharmony_ci#define bh_lock_sock(__sk) spin_lock(&((__sk)->sk_lock.slock)) 16498c2ecf20Sopenharmony_ci#define bh_lock_sock_nested(__sk) \ 16508c2ecf20Sopenharmony_ci spin_lock_nested(&((__sk)->sk_lock.slock), \ 16518c2ecf20Sopenharmony_ci SINGLE_DEPTH_NESTING) 16528c2ecf20Sopenharmony_ci#define bh_unlock_sock(__sk) spin_unlock(&((__sk)->sk_lock.slock)) 16538c2ecf20Sopenharmony_ci 16548c2ecf20Sopenharmony_cibool lock_sock_fast(struct sock *sk); 16558c2ecf20Sopenharmony_ci/** 16568c2ecf20Sopenharmony_ci * unlock_sock_fast - complement of lock_sock_fast 16578c2ecf20Sopenharmony_ci * @sk: socket 16588c2ecf20Sopenharmony_ci * @slow: slow mode 16598c2ecf20Sopenharmony_ci * 16608c2ecf20Sopenharmony_ci * fast unlock socket for user context. 16618c2ecf20Sopenharmony_ci * If slow mode is on, we call regular release_sock() 16628c2ecf20Sopenharmony_ci */ 16638c2ecf20Sopenharmony_cistatic inline void unlock_sock_fast(struct sock *sk, bool slow) 16648c2ecf20Sopenharmony_ci{ 16658c2ecf20Sopenharmony_ci if (slow) 16668c2ecf20Sopenharmony_ci release_sock(sk); 16678c2ecf20Sopenharmony_ci else 16688c2ecf20Sopenharmony_ci spin_unlock_bh(&sk->sk_lock.slock); 16698c2ecf20Sopenharmony_ci} 16708c2ecf20Sopenharmony_ci 16718c2ecf20Sopenharmony_ci/* Used by processes to "lock" a socket state, so that 16728c2ecf20Sopenharmony_ci * interrupts and bottom half handlers won't change it 16738c2ecf20Sopenharmony_ci * from under us. It essentially blocks any incoming 16748c2ecf20Sopenharmony_ci * packets, so that we won't get any new data or any 16758c2ecf20Sopenharmony_ci * packets that change the state of the socket. 16768c2ecf20Sopenharmony_ci * 16778c2ecf20Sopenharmony_ci * While locked, BH processing will add new packets to 16788c2ecf20Sopenharmony_ci * the backlog queue. This queue is processed by the 16798c2ecf20Sopenharmony_ci * owner of the socket lock right before it is released. 16808c2ecf20Sopenharmony_ci * 16818c2ecf20Sopenharmony_ci * Since ~2.3.5 it is also exclusive sleep lock serializing 16828c2ecf20Sopenharmony_ci * accesses from user process context. 16838c2ecf20Sopenharmony_ci */ 16848c2ecf20Sopenharmony_ci 16858c2ecf20Sopenharmony_cistatic inline void sock_owned_by_me(const struct sock *sk) 16868c2ecf20Sopenharmony_ci{ 16878c2ecf20Sopenharmony_ci#ifdef CONFIG_LOCKDEP 16888c2ecf20Sopenharmony_ci WARN_ON_ONCE(!lockdep_sock_is_held(sk) && debug_locks); 16898c2ecf20Sopenharmony_ci#endif 16908c2ecf20Sopenharmony_ci} 16918c2ecf20Sopenharmony_ci 16928c2ecf20Sopenharmony_cistatic inline void sock_not_owned_by_me(const struct sock *sk) 16938c2ecf20Sopenharmony_ci{ 16948c2ecf20Sopenharmony_ci#ifdef CONFIG_LOCKDEP 16958c2ecf20Sopenharmony_ci WARN_ON_ONCE(lockdep_sock_is_held(sk) && debug_locks); 16968c2ecf20Sopenharmony_ci#endif 16978c2ecf20Sopenharmony_ci} 16988c2ecf20Sopenharmony_ci 16998c2ecf20Sopenharmony_cistatic inline bool sock_owned_by_user(const struct sock *sk) 17008c2ecf20Sopenharmony_ci{ 17018c2ecf20Sopenharmony_ci sock_owned_by_me(sk); 17028c2ecf20Sopenharmony_ci return sk->sk_lock.owned; 17038c2ecf20Sopenharmony_ci} 17048c2ecf20Sopenharmony_ci 17058c2ecf20Sopenharmony_cistatic inline bool sock_owned_by_user_nocheck(const struct sock *sk) 17068c2ecf20Sopenharmony_ci{ 17078c2ecf20Sopenharmony_ci return sk->sk_lock.owned; 17088c2ecf20Sopenharmony_ci} 17098c2ecf20Sopenharmony_ci 17108c2ecf20Sopenharmony_ci/* no reclassification while locks are held */ 17118c2ecf20Sopenharmony_cistatic inline bool sock_allow_reclassification(const struct sock *csk) 17128c2ecf20Sopenharmony_ci{ 17138c2ecf20Sopenharmony_ci struct sock *sk = (struct sock *)csk; 17148c2ecf20Sopenharmony_ci 17158c2ecf20Sopenharmony_ci return !sk->sk_lock.owned && !spin_is_locked(&sk->sk_lock.slock); 17168c2ecf20Sopenharmony_ci} 17178c2ecf20Sopenharmony_ci 17188c2ecf20Sopenharmony_cistruct sock *sk_alloc(struct net *net, int family, gfp_t priority, 17198c2ecf20Sopenharmony_ci struct proto *prot, int kern); 17208c2ecf20Sopenharmony_civoid sk_free(struct sock *sk); 17218c2ecf20Sopenharmony_civoid sk_destruct(struct sock *sk); 17228c2ecf20Sopenharmony_cistruct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority); 17238c2ecf20Sopenharmony_civoid sk_free_unlock_clone(struct sock *sk); 17248c2ecf20Sopenharmony_ci 17258c2ecf20Sopenharmony_cistruct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, 17268c2ecf20Sopenharmony_ci gfp_t priority); 17278c2ecf20Sopenharmony_civoid __sock_wfree(struct sk_buff *skb); 17288c2ecf20Sopenharmony_civoid sock_wfree(struct sk_buff *skb); 17298c2ecf20Sopenharmony_cistruct sk_buff *sock_omalloc(struct sock *sk, unsigned long size, 17308c2ecf20Sopenharmony_ci gfp_t priority); 17318c2ecf20Sopenharmony_civoid skb_orphan_partial(struct sk_buff *skb); 17328c2ecf20Sopenharmony_civoid sock_rfree(struct sk_buff *skb); 17338c2ecf20Sopenharmony_civoid sock_efree(struct sk_buff *skb); 17348c2ecf20Sopenharmony_ci#ifdef CONFIG_INET 17358c2ecf20Sopenharmony_civoid sock_edemux(struct sk_buff *skb); 17368c2ecf20Sopenharmony_civoid sock_pfree(struct sk_buff *skb); 17378c2ecf20Sopenharmony_ci#else 17388c2ecf20Sopenharmony_ci#define sock_edemux sock_efree 17398c2ecf20Sopenharmony_ci#endif 17408c2ecf20Sopenharmony_ci 17418c2ecf20Sopenharmony_ciint sock_setsockopt(struct socket *sock, int level, int op, 17428c2ecf20Sopenharmony_ci sockptr_t optval, unsigned int optlen); 17438c2ecf20Sopenharmony_ci 17448c2ecf20Sopenharmony_ciint sock_getsockopt(struct socket *sock, int level, int op, 17458c2ecf20Sopenharmony_ci char __user *optval, int __user *optlen); 17468c2ecf20Sopenharmony_ciint sock_gettstamp(struct socket *sock, void __user *userstamp, 17478c2ecf20Sopenharmony_ci bool timeval, bool time32); 17488c2ecf20Sopenharmony_cistruct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size, 17498c2ecf20Sopenharmony_ci int noblock, int *errcode); 17508c2ecf20Sopenharmony_cistruct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len, 17518c2ecf20Sopenharmony_ci unsigned long data_len, int noblock, 17528c2ecf20Sopenharmony_ci int *errcode, int max_page_order); 17538c2ecf20Sopenharmony_civoid *sock_kmalloc(struct sock *sk, int size, gfp_t priority); 17548c2ecf20Sopenharmony_civoid sock_kfree_s(struct sock *sk, void *mem, int size); 17558c2ecf20Sopenharmony_civoid sock_kzfree_s(struct sock *sk, void *mem, int size); 17568c2ecf20Sopenharmony_civoid sk_send_sigurg(struct sock *sk); 17578c2ecf20Sopenharmony_ci 17588c2ecf20Sopenharmony_cistruct sockcm_cookie { 17598c2ecf20Sopenharmony_ci u64 transmit_time; 17608c2ecf20Sopenharmony_ci u32 mark; 17618c2ecf20Sopenharmony_ci u16 tsflags; 17628c2ecf20Sopenharmony_ci}; 17638c2ecf20Sopenharmony_ci 17648c2ecf20Sopenharmony_cistatic inline void sockcm_init(struct sockcm_cookie *sockc, 17658c2ecf20Sopenharmony_ci const struct sock *sk) 17668c2ecf20Sopenharmony_ci{ 17678c2ecf20Sopenharmony_ci *sockc = (struct sockcm_cookie) { .tsflags = sk->sk_tsflags }; 17688c2ecf20Sopenharmony_ci} 17698c2ecf20Sopenharmony_ci 17708c2ecf20Sopenharmony_ciint __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg, 17718c2ecf20Sopenharmony_ci struct sockcm_cookie *sockc); 17728c2ecf20Sopenharmony_ciint sock_cmsg_send(struct sock *sk, struct msghdr *msg, 17738c2ecf20Sopenharmony_ci struct sockcm_cookie *sockc); 17748c2ecf20Sopenharmony_ci 17758c2ecf20Sopenharmony_ci/* 17768c2ecf20Sopenharmony_ci * Functions to fill in entries in struct proto_ops when a protocol 17778c2ecf20Sopenharmony_ci * does not implement a particular function. 17788c2ecf20Sopenharmony_ci */ 17798c2ecf20Sopenharmony_ciint sock_no_bind(struct socket *, struct sockaddr *, int); 17808c2ecf20Sopenharmony_ciint sock_no_connect(struct socket *, struct sockaddr *, int, int); 17818c2ecf20Sopenharmony_ciint sock_no_socketpair(struct socket *, struct socket *); 17828c2ecf20Sopenharmony_ciint sock_no_accept(struct socket *, struct socket *, int, bool); 17838c2ecf20Sopenharmony_ciint sock_no_getname(struct socket *, struct sockaddr *, int); 17848c2ecf20Sopenharmony_ciint sock_no_ioctl(struct socket *, unsigned int, unsigned long); 17858c2ecf20Sopenharmony_ciint sock_no_listen(struct socket *, int); 17868c2ecf20Sopenharmony_ciint sock_no_shutdown(struct socket *, int); 17878c2ecf20Sopenharmony_ciint sock_no_sendmsg(struct socket *, struct msghdr *, size_t); 17888c2ecf20Sopenharmony_ciint sock_no_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len); 17898c2ecf20Sopenharmony_ciint sock_no_recvmsg(struct socket *, struct msghdr *, size_t, int); 17908c2ecf20Sopenharmony_ciint sock_no_mmap(struct file *file, struct socket *sock, 17918c2ecf20Sopenharmony_ci struct vm_area_struct *vma); 17928c2ecf20Sopenharmony_cissize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, 17938c2ecf20Sopenharmony_ci size_t size, int flags); 17948c2ecf20Sopenharmony_cissize_t sock_no_sendpage_locked(struct sock *sk, struct page *page, 17958c2ecf20Sopenharmony_ci int offset, size_t size, int flags); 17968c2ecf20Sopenharmony_ci 17978c2ecf20Sopenharmony_ci/* 17988c2ecf20Sopenharmony_ci * Functions to fill in entries in struct proto_ops when a protocol 17998c2ecf20Sopenharmony_ci * uses the inet style. 18008c2ecf20Sopenharmony_ci */ 18018c2ecf20Sopenharmony_ciint sock_common_getsockopt(struct socket *sock, int level, int optname, 18028c2ecf20Sopenharmony_ci char __user *optval, int __user *optlen); 18038c2ecf20Sopenharmony_ciint sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 18048c2ecf20Sopenharmony_ci int flags); 18058c2ecf20Sopenharmony_ciint sock_common_setsockopt(struct socket *sock, int level, int optname, 18068c2ecf20Sopenharmony_ci sockptr_t optval, unsigned int optlen); 18078c2ecf20Sopenharmony_ci 18088c2ecf20Sopenharmony_civoid sk_common_release(struct sock *sk); 18098c2ecf20Sopenharmony_ci 18108c2ecf20Sopenharmony_ci/* 18118c2ecf20Sopenharmony_ci * Default socket callbacks and setup code 18128c2ecf20Sopenharmony_ci */ 18138c2ecf20Sopenharmony_ci 18148c2ecf20Sopenharmony_ci/* Initialise core socket variables using an explicit uid. */ 18158c2ecf20Sopenharmony_civoid sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid); 18168c2ecf20Sopenharmony_ci 18178c2ecf20Sopenharmony_ci/* Initialise core socket variables. 18188c2ecf20Sopenharmony_ci * Assumes struct socket *sock is embedded in a struct socket_alloc. 18198c2ecf20Sopenharmony_ci */ 18208c2ecf20Sopenharmony_civoid sock_init_data(struct socket *sock, struct sock *sk); 18218c2ecf20Sopenharmony_ci 18228c2ecf20Sopenharmony_ci/* 18238c2ecf20Sopenharmony_ci * Socket reference counting postulates. 18248c2ecf20Sopenharmony_ci * 18258c2ecf20Sopenharmony_ci * * Each user of socket SHOULD hold a reference count. 18268c2ecf20Sopenharmony_ci * * Each access point to socket (an hash table bucket, reference from a list, 18278c2ecf20Sopenharmony_ci * running timer, skb in flight MUST hold a reference count. 18288c2ecf20Sopenharmony_ci * * When reference count hits 0, it means it will never increase back. 18298c2ecf20Sopenharmony_ci * * When reference count hits 0, it means that no references from 18308c2ecf20Sopenharmony_ci * outside exist to this socket and current process on current CPU 18318c2ecf20Sopenharmony_ci * is last user and may/should destroy this socket. 18328c2ecf20Sopenharmony_ci * * sk_free is called from any context: process, BH, IRQ. When 18338c2ecf20Sopenharmony_ci * it is called, socket has no references from outside -> sk_free 18348c2ecf20Sopenharmony_ci * may release descendant resources allocated by the socket, but 18358c2ecf20Sopenharmony_ci * to the time when it is called, socket is NOT referenced by any 18368c2ecf20Sopenharmony_ci * hash tables, lists etc. 18378c2ecf20Sopenharmony_ci * * Packets, delivered from outside (from network or from another process) 18388c2ecf20Sopenharmony_ci * and enqueued on receive/error queues SHOULD NOT grab reference count, 18398c2ecf20Sopenharmony_ci * when they sit in queue. Otherwise, packets will leak to hole, when 18408c2ecf20Sopenharmony_ci * socket is looked up by one cpu and unhasing is made by another CPU. 18418c2ecf20Sopenharmony_ci * It is true for udp/raw, netlink (leak to receive and error queues), tcp 18428c2ecf20Sopenharmony_ci * (leak to backlog). Packet socket does all the processing inside 18438c2ecf20Sopenharmony_ci * BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets 18448c2ecf20Sopenharmony_ci * use separate SMP lock, so that they are prone too. 18458c2ecf20Sopenharmony_ci */ 18468c2ecf20Sopenharmony_ci 18478c2ecf20Sopenharmony_ci/* Ungrab socket and destroy it, if it was the last reference. */ 18488c2ecf20Sopenharmony_cistatic inline void sock_put(struct sock *sk) 18498c2ecf20Sopenharmony_ci{ 18508c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&sk->sk_refcnt)) 18518c2ecf20Sopenharmony_ci sk_free(sk); 18528c2ecf20Sopenharmony_ci} 18538c2ecf20Sopenharmony_ci/* Generic version of sock_put(), dealing with all sockets 18548c2ecf20Sopenharmony_ci * (TCP_TIMEWAIT, TCP_NEW_SYN_RECV, ESTABLISHED...) 18558c2ecf20Sopenharmony_ci */ 18568c2ecf20Sopenharmony_civoid sock_gen_put(struct sock *sk); 18578c2ecf20Sopenharmony_ci 18588c2ecf20Sopenharmony_ciint __sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested, 18598c2ecf20Sopenharmony_ci unsigned int trim_cap, bool refcounted); 18608c2ecf20Sopenharmony_cistatic inline int sk_receive_skb(struct sock *sk, struct sk_buff *skb, 18618c2ecf20Sopenharmony_ci const int nested) 18628c2ecf20Sopenharmony_ci{ 18638c2ecf20Sopenharmony_ci return __sk_receive_skb(sk, skb, nested, 1, true); 18648c2ecf20Sopenharmony_ci} 18658c2ecf20Sopenharmony_ci 18668c2ecf20Sopenharmony_cistatic inline void sk_tx_queue_set(struct sock *sk, int tx_queue) 18678c2ecf20Sopenharmony_ci{ 18688c2ecf20Sopenharmony_ci /* sk_tx_queue_mapping accept only upto a 16-bit value */ 18698c2ecf20Sopenharmony_ci if (WARN_ON_ONCE((unsigned short)tx_queue >= USHRT_MAX)) 18708c2ecf20Sopenharmony_ci return; 18718c2ecf20Sopenharmony_ci /* Paired with READ_ONCE() in sk_tx_queue_get() and 18728c2ecf20Sopenharmony_ci * other WRITE_ONCE() because socket lock might be not held. 18738c2ecf20Sopenharmony_ci */ 18748c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_tx_queue_mapping, tx_queue); 18758c2ecf20Sopenharmony_ci} 18768c2ecf20Sopenharmony_ci 18778c2ecf20Sopenharmony_ci#define NO_QUEUE_MAPPING USHRT_MAX 18788c2ecf20Sopenharmony_ci 18798c2ecf20Sopenharmony_cistatic inline void sk_tx_queue_clear(struct sock *sk) 18808c2ecf20Sopenharmony_ci{ 18818c2ecf20Sopenharmony_ci /* Paired with READ_ONCE() in sk_tx_queue_get() and 18828c2ecf20Sopenharmony_ci * other WRITE_ONCE() because socket lock might be not held. 18838c2ecf20Sopenharmony_ci */ 18848c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_tx_queue_mapping, NO_QUEUE_MAPPING); 18858c2ecf20Sopenharmony_ci} 18868c2ecf20Sopenharmony_ci 18878c2ecf20Sopenharmony_cistatic inline int sk_tx_queue_get(const struct sock *sk) 18888c2ecf20Sopenharmony_ci{ 18898c2ecf20Sopenharmony_ci if (sk) { 18908c2ecf20Sopenharmony_ci /* Paired with WRITE_ONCE() in sk_tx_queue_clear() 18918c2ecf20Sopenharmony_ci * and sk_tx_queue_set(). 18928c2ecf20Sopenharmony_ci */ 18938c2ecf20Sopenharmony_ci int val = READ_ONCE(sk->sk_tx_queue_mapping); 18948c2ecf20Sopenharmony_ci 18958c2ecf20Sopenharmony_ci if (val != NO_QUEUE_MAPPING) 18968c2ecf20Sopenharmony_ci return val; 18978c2ecf20Sopenharmony_ci } 18988c2ecf20Sopenharmony_ci return -1; 18998c2ecf20Sopenharmony_ci} 19008c2ecf20Sopenharmony_ci 19018c2ecf20Sopenharmony_cistatic inline void sk_rx_queue_set(struct sock *sk, const struct sk_buff *skb) 19028c2ecf20Sopenharmony_ci{ 19038c2ecf20Sopenharmony_ci#ifdef CONFIG_XPS 19048c2ecf20Sopenharmony_ci if (skb_rx_queue_recorded(skb)) { 19058c2ecf20Sopenharmony_ci u16 rx_queue = skb_get_rx_queue(skb); 19068c2ecf20Sopenharmony_ci 19078c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(rx_queue == NO_QUEUE_MAPPING)) 19088c2ecf20Sopenharmony_ci return; 19098c2ecf20Sopenharmony_ci 19108c2ecf20Sopenharmony_ci sk->sk_rx_queue_mapping = rx_queue; 19118c2ecf20Sopenharmony_ci } 19128c2ecf20Sopenharmony_ci#endif 19138c2ecf20Sopenharmony_ci} 19148c2ecf20Sopenharmony_ci 19158c2ecf20Sopenharmony_cistatic inline void sk_rx_queue_clear(struct sock *sk) 19168c2ecf20Sopenharmony_ci{ 19178c2ecf20Sopenharmony_ci#ifdef CONFIG_XPS 19188c2ecf20Sopenharmony_ci sk->sk_rx_queue_mapping = NO_QUEUE_MAPPING; 19198c2ecf20Sopenharmony_ci#endif 19208c2ecf20Sopenharmony_ci} 19218c2ecf20Sopenharmony_ci 19228c2ecf20Sopenharmony_ci#ifdef CONFIG_XPS 19238c2ecf20Sopenharmony_cistatic inline int sk_rx_queue_get(const struct sock *sk) 19248c2ecf20Sopenharmony_ci{ 19258c2ecf20Sopenharmony_ci if (sk && sk->sk_rx_queue_mapping != NO_QUEUE_MAPPING) 19268c2ecf20Sopenharmony_ci return sk->sk_rx_queue_mapping; 19278c2ecf20Sopenharmony_ci 19288c2ecf20Sopenharmony_ci return -1; 19298c2ecf20Sopenharmony_ci} 19308c2ecf20Sopenharmony_ci#endif 19318c2ecf20Sopenharmony_ci 19328c2ecf20Sopenharmony_cistatic inline void sk_set_socket(struct sock *sk, struct socket *sock) 19338c2ecf20Sopenharmony_ci{ 19348c2ecf20Sopenharmony_ci sk->sk_socket = sock; 19358c2ecf20Sopenharmony_ci} 19368c2ecf20Sopenharmony_ci 19378c2ecf20Sopenharmony_cistatic inline wait_queue_head_t *sk_sleep(struct sock *sk) 19388c2ecf20Sopenharmony_ci{ 19398c2ecf20Sopenharmony_ci BUILD_BUG_ON(offsetof(struct socket_wq, wait) != 0); 19408c2ecf20Sopenharmony_ci return &rcu_dereference_raw(sk->sk_wq)->wait; 19418c2ecf20Sopenharmony_ci} 19428c2ecf20Sopenharmony_ci/* Detach socket from process context. 19438c2ecf20Sopenharmony_ci * Announce socket dead, detach it from wait queue and inode. 19448c2ecf20Sopenharmony_ci * Note that parent inode held reference count on this struct sock, 19458c2ecf20Sopenharmony_ci * we do not release it in this function, because protocol 19468c2ecf20Sopenharmony_ci * probably wants some additional cleanups or even continuing 19478c2ecf20Sopenharmony_ci * to work with this socket (TCP). 19488c2ecf20Sopenharmony_ci */ 19498c2ecf20Sopenharmony_cistatic inline void sock_orphan(struct sock *sk) 19508c2ecf20Sopenharmony_ci{ 19518c2ecf20Sopenharmony_ci write_lock_bh(&sk->sk_callback_lock); 19528c2ecf20Sopenharmony_ci sock_set_flag(sk, SOCK_DEAD); 19538c2ecf20Sopenharmony_ci sk_set_socket(sk, NULL); 19548c2ecf20Sopenharmony_ci sk->sk_wq = NULL; 19558c2ecf20Sopenharmony_ci write_unlock_bh(&sk->sk_callback_lock); 19568c2ecf20Sopenharmony_ci} 19578c2ecf20Sopenharmony_ci 19588c2ecf20Sopenharmony_cistatic inline void sock_graft(struct sock *sk, struct socket *parent) 19598c2ecf20Sopenharmony_ci{ 19608c2ecf20Sopenharmony_ci WARN_ON(parent->sk); 19618c2ecf20Sopenharmony_ci write_lock_bh(&sk->sk_callback_lock); 19628c2ecf20Sopenharmony_ci rcu_assign_pointer(sk->sk_wq, &parent->wq); 19638c2ecf20Sopenharmony_ci parent->sk = sk; 19648c2ecf20Sopenharmony_ci sk_set_socket(sk, parent); 19658c2ecf20Sopenharmony_ci sk->sk_uid = SOCK_INODE(parent)->i_uid; 19668c2ecf20Sopenharmony_ci security_sock_graft(sk, parent); 19678c2ecf20Sopenharmony_ci write_unlock_bh(&sk->sk_callback_lock); 19688c2ecf20Sopenharmony_ci} 19698c2ecf20Sopenharmony_ci 19708c2ecf20Sopenharmony_cikuid_t sock_i_uid(struct sock *sk); 19718c2ecf20Sopenharmony_ciunsigned long __sock_i_ino(struct sock *sk); 19728c2ecf20Sopenharmony_ciunsigned long sock_i_ino(struct sock *sk); 19738c2ecf20Sopenharmony_ci 19748c2ecf20Sopenharmony_cistatic inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk) 19758c2ecf20Sopenharmony_ci{ 19768c2ecf20Sopenharmony_ci return sk ? sk->sk_uid : make_kuid(net->user_ns, 0); 19778c2ecf20Sopenharmony_ci} 19788c2ecf20Sopenharmony_ci 19798c2ecf20Sopenharmony_cistatic inline u32 net_tx_rndhash(void) 19808c2ecf20Sopenharmony_ci{ 19818c2ecf20Sopenharmony_ci u32 v = prandom_u32(); 19828c2ecf20Sopenharmony_ci 19838c2ecf20Sopenharmony_ci return v ?: 1; 19848c2ecf20Sopenharmony_ci} 19858c2ecf20Sopenharmony_ci 19868c2ecf20Sopenharmony_cistatic inline void sk_set_txhash(struct sock *sk) 19878c2ecf20Sopenharmony_ci{ 19888c2ecf20Sopenharmony_ci /* This pairs with READ_ONCE() in skb_set_hash_from_sk() */ 19898c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_txhash, net_tx_rndhash()); 19908c2ecf20Sopenharmony_ci} 19918c2ecf20Sopenharmony_ci 19928c2ecf20Sopenharmony_cistatic inline bool sk_rethink_txhash(struct sock *sk) 19938c2ecf20Sopenharmony_ci{ 19948c2ecf20Sopenharmony_ci if (sk->sk_txhash) { 19958c2ecf20Sopenharmony_ci sk_set_txhash(sk); 19968c2ecf20Sopenharmony_ci return true; 19978c2ecf20Sopenharmony_ci } 19988c2ecf20Sopenharmony_ci return false; 19998c2ecf20Sopenharmony_ci} 20008c2ecf20Sopenharmony_ci 20018c2ecf20Sopenharmony_cistatic inline struct dst_entry * 20028c2ecf20Sopenharmony_ci__sk_dst_get(struct sock *sk) 20038c2ecf20Sopenharmony_ci{ 20048c2ecf20Sopenharmony_ci return rcu_dereference_check(sk->sk_dst_cache, 20058c2ecf20Sopenharmony_ci lockdep_sock_is_held(sk)); 20068c2ecf20Sopenharmony_ci} 20078c2ecf20Sopenharmony_ci 20088c2ecf20Sopenharmony_cistatic inline struct dst_entry * 20098c2ecf20Sopenharmony_cisk_dst_get(struct sock *sk) 20108c2ecf20Sopenharmony_ci{ 20118c2ecf20Sopenharmony_ci struct dst_entry *dst; 20128c2ecf20Sopenharmony_ci 20138c2ecf20Sopenharmony_ci rcu_read_lock(); 20148c2ecf20Sopenharmony_ci dst = rcu_dereference(sk->sk_dst_cache); 20158c2ecf20Sopenharmony_ci if (dst && !atomic_inc_not_zero(&dst->__refcnt)) 20168c2ecf20Sopenharmony_ci dst = NULL; 20178c2ecf20Sopenharmony_ci rcu_read_unlock(); 20188c2ecf20Sopenharmony_ci return dst; 20198c2ecf20Sopenharmony_ci} 20208c2ecf20Sopenharmony_ci 20218c2ecf20Sopenharmony_cistatic inline void __dst_negative_advice(struct sock *sk) 20228c2ecf20Sopenharmony_ci{ 20238c2ecf20Sopenharmony_ci struct dst_entry *dst = __sk_dst_get(sk); 20248c2ecf20Sopenharmony_ci 20258c2ecf20Sopenharmony_ci if (dst && dst->ops->negative_advice) 20268c2ecf20Sopenharmony_ci dst->ops->negative_advice(sk, dst); 20278c2ecf20Sopenharmony_ci} 20288c2ecf20Sopenharmony_ci 20298c2ecf20Sopenharmony_cistatic inline void dst_negative_advice(struct sock *sk) 20308c2ecf20Sopenharmony_ci{ 20318c2ecf20Sopenharmony_ci sk_rethink_txhash(sk); 20328c2ecf20Sopenharmony_ci __dst_negative_advice(sk); 20338c2ecf20Sopenharmony_ci} 20348c2ecf20Sopenharmony_ci 20358c2ecf20Sopenharmony_cistatic inline void 20368c2ecf20Sopenharmony_ci__sk_dst_set(struct sock *sk, struct dst_entry *dst) 20378c2ecf20Sopenharmony_ci{ 20388c2ecf20Sopenharmony_ci struct dst_entry *old_dst; 20398c2ecf20Sopenharmony_ci 20408c2ecf20Sopenharmony_ci sk_tx_queue_clear(sk); 20418c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_dst_pending_confirm, 0); 20428c2ecf20Sopenharmony_ci old_dst = rcu_dereference_protected(sk->sk_dst_cache, 20438c2ecf20Sopenharmony_ci lockdep_sock_is_held(sk)); 20448c2ecf20Sopenharmony_ci rcu_assign_pointer(sk->sk_dst_cache, dst); 20458c2ecf20Sopenharmony_ci dst_release(old_dst); 20468c2ecf20Sopenharmony_ci} 20478c2ecf20Sopenharmony_ci 20488c2ecf20Sopenharmony_cistatic inline void 20498c2ecf20Sopenharmony_cisk_dst_set(struct sock *sk, struct dst_entry *dst) 20508c2ecf20Sopenharmony_ci{ 20518c2ecf20Sopenharmony_ci struct dst_entry *old_dst; 20528c2ecf20Sopenharmony_ci 20538c2ecf20Sopenharmony_ci sk_tx_queue_clear(sk); 20548c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_dst_pending_confirm, 0); 20558c2ecf20Sopenharmony_ci old_dst = xchg((__force struct dst_entry **)&sk->sk_dst_cache, dst); 20568c2ecf20Sopenharmony_ci dst_release(old_dst); 20578c2ecf20Sopenharmony_ci} 20588c2ecf20Sopenharmony_ci 20598c2ecf20Sopenharmony_cistatic inline void 20608c2ecf20Sopenharmony_ci__sk_dst_reset(struct sock *sk) 20618c2ecf20Sopenharmony_ci{ 20628c2ecf20Sopenharmony_ci __sk_dst_set(sk, NULL); 20638c2ecf20Sopenharmony_ci} 20648c2ecf20Sopenharmony_ci 20658c2ecf20Sopenharmony_cistatic inline void 20668c2ecf20Sopenharmony_cisk_dst_reset(struct sock *sk) 20678c2ecf20Sopenharmony_ci{ 20688c2ecf20Sopenharmony_ci sk_dst_set(sk, NULL); 20698c2ecf20Sopenharmony_ci} 20708c2ecf20Sopenharmony_ci 20718c2ecf20Sopenharmony_cistruct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie); 20728c2ecf20Sopenharmony_ci 20738c2ecf20Sopenharmony_cistruct dst_entry *sk_dst_check(struct sock *sk, u32 cookie); 20748c2ecf20Sopenharmony_ci 20758c2ecf20Sopenharmony_cistatic inline void sk_dst_confirm(struct sock *sk) 20768c2ecf20Sopenharmony_ci{ 20778c2ecf20Sopenharmony_ci if (!READ_ONCE(sk->sk_dst_pending_confirm)) 20788c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_dst_pending_confirm, 1); 20798c2ecf20Sopenharmony_ci} 20808c2ecf20Sopenharmony_ci 20818c2ecf20Sopenharmony_cistatic inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n) 20828c2ecf20Sopenharmony_ci{ 20838c2ecf20Sopenharmony_ci if (skb_get_dst_pending_confirm(skb)) { 20848c2ecf20Sopenharmony_ci struct sock *sk = skb->sk; 20858c2ecf20Sopenharmony_ci unsigned long now = jiffies; 20868c2ecf20Sopenharmony_ci 20878c2ecf20Sopenharmony_ci /* avoid dirtying neighbour */ 20888c2ecf20Sopenharmony_ci if (READ_ONCE(n->confirmed) != now) 20898c2ecf20Sopenharmony_ci WRITE_ONCE(n->confirmed, now); 20908c2ecf20Sopenharmony_ci if (sk && READ_ONCE(sk->sk_dst_pending_confirm)) 20918c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_dst_pending_confirm, 0); 20928c2ecf20Sopenharmony_ci } 20938c2ecf20Sopenharmony_ci} 20948c2ecf20Sopenharmony_ci 20958c2ecf20Sopenharmony_cibool sk_mc_loop(struct sock *sk); 20968c2ecf20Sopenharmony_ci 20978c2ecf20Sopenharmony_cistatic inline bool sk_can_gso(const struct sock *sk) 20988c2ecf20Sopenharmony_ci{ 20998c2ecf20Sopenharmony_ci return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type); 21008c2ecf20Sopenharmony_ci} 21018c2ecf20Sopenharmony_ci 21028c2ecf20Sopenharmony_civoid sk_setup_caps(struct sock *sk, struct dst_entry *dst); 21038c2ecf20Sopenharmony_ci 21048c2ecf20Sopenharmony_cistatic inline void sk_nocaps_add(struct sock *sk, netdev_features_t flags) 21058c2ecf20Sopenharmony_ci{ 21068c2ecf20Sopenharmony_ci sk->sk_route_nocaps |= flags; 21078c2ecf20Sopenharmony_ci sk->sk_route_caps &= ~flags; 21088c2ecf20Sopenharmony_ci} 21098c2ecf20Sopenharmony_ci 21108c2ecf20Sopenharmony_cistatic inline int skb_do_copy_data_nocache(struct sock *sk, struct sk_buff *skb, 21118c2ecf20Sopenharmony_ci struct iov_iter *from, char *to, 21128c2ecf20Sopenharmony_ci int copy, int offset) 21138c2ecf20Sopenharmony_ci{ 21148c2ecf20Sopenharmony_ci if (skb->ip_summed == CHECKSUM_NONE) { 21158c2ecf20Sopenharmony_ci __wsum csum = 0; 21168c2ecf20Sopenharmony_ci if (!csum_and_copy_from_iter_full(to, copy, &csum, from)) 21178c2ecf20Sopenharmony_ci return -EFAULT; 21188c2ecf20Sopenharmony_ci skb->csum = csum_block_add(skb->csum, csum, offset); 21198c2ecf20Sopenharmony_ci } else if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) { 21208c2ecf20Sopenharmony_ci if (!copy_from_iter_full_nocache(to, copy, from)) 21218c2ecf20Sopenharmony_ci return -EFAULT; 21228c2ecf20Sopenharmony_ci } else if (!copy_from_iter_full(to, copy, from)) 21238c2ecf20Sopenharmony_ci return -EFAULT; 21248c2ecf20Sopenharmony_ci 21258c2ecf20Sopenharmony_ci return 0; 21268c2ecf20Sopenharmony_ci} 21278c2ecf20Sopenharmony_ci 21288c2ecf20Sopenharmony_cistatic inline int skb_add_data_nocache(struct sock *sk, struct sk_buff *skb, 21298c2ecf20Sopenharmony_ci struct iov_iter *from, int copy) 21308c2ecf20Sopenharmony_ci{ 21318c2ecf20Sopenharmony_ci int err, offset = skb->len; 21328c2ecf20Sopenharmony_ci 21338c2ecf20Sopenharmony_ci err = skb_do_copy_data_nocache(sk, skb, from, skb_put(skb, copy), 21348c2ecf20Sopenharmony_ci copy, offset); 21358c2ecf20Sopenharmony_ci if (err) 21368c2ecf20Sopenharmony_ci __skb_trim(skb, offset); 21378c2ecf20Sopenharmony_ci 21388c2ecf20Sopenharmony_ci return err; 21398c2ecf20Sopenharmony_ci} 21408c2ecf20Sopenharmony_ci 21418c2ecf20Sopenharmony_cistatic inline int skb_copy_to_page_nocache(struct sock *sk, struct iov_iter *from, 21428c2ecf20Sopenharmony_ci struct sk_buff *skb, 21438c2ecf20Sopenharmony_ci struct page *page, 21448c2ecf20Sopenharmony_ci int off, int copy) 21458c2ecf20Sopenharmony_ci{ 21468c2ecf20Sopenharmony_ci int err; 21478c2ecf20Sopenharmony_ci 21488c2ecf20Sopenharmony_ci err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) + off, 21498c2ecf20Sopenharmony_ci copy, skb->len); 21508c2ecf20Sopenharmony_ci if (err) 21518c2ecf20Sopenharmony_ci return err; 21528c2ecf20Sopenharmony_ci 21538c2ecf20Sopenharmony_ci skb->len += copy; 21548c2ecf20Sopenharmony_ci skb->data_len += copy; 21558c2ecf20Sopenharmony_ci skb->truesize += copy; 21568c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, copy); 21578c2ecf20Sopenharmony_ci sk_mem_charge(sk, copy); 21588c2ecf20Sopenharmony_ci return 0; 21598c2ecf20Sopenharmony_ci} 21608c2ecf20Sopenharmony_ci 21618c2ecf20Sopenharmony_ci/** 21628c2ecf20Sopenharmony_ci * sk_wmem_alloc_get - returns write allocations 21638c2ecf20Sopenharmony_ci * @sk: socket 21648c2ecf20Sopenharmony_ci * 21658c2ecf20Sopenharmony_ci * Return: sk_wmem_alloc minus initial offset of one 21668c2ecf20Sopenharmony_ci */ 21678c2ecf20Sopenharmony_cistatic inline int sk_wmem_alloc_get(const struct sock *sk) 21688c2ecf20Sopenharmony_ci{ 21698c2ecf20Sopenharmony_ci return refcount_read(&sk->sk_wmem_alloc) - 1; 21708c2ecf20Sopenharmony_ci} 21718c2ecf20Sopenharmony_ci 21728c2ecf20Sopenharmony_ci/** 21738c2ecf20Sopenharmony_ci * sk_rmem_alloc_get - returns read allocations 21748c2ecf20Sopenharmony_ci * @sk: socket 21758c2ecf20Sopenharmony_ci * 21768c2ecf20Sopenharmony_ci * Return: sk_rmem_alloc 21778c2ecf20Sopenharmony_ci */ 21788c2ecf20Sopenharmony_cistatic inline int sk_rmem_alloc_get(const struct sock *sk) 21798c2ecf20Sopenharmony_ci{ 21808c2ecf20Sopenharmony_ci return atomic_read(&sk->sk_rmem_alloc); 21818c2ecf20Sopenharmony_ci} 21828c2ecf20Sopenharmony_ci 21838c2ecf20Sopenharmony_ci/** 21848c2ecf20Sopenharmony_ci * sk_has_allocations - check if allocations are outstanding 21858c2ecf20Sopenharmony_ci * @sk: socket 21868c2ecf20Sopenharmony_ci * 21878c2ecf20Sopenharmony_ci * Return: true if socket has write or read allocations 21888c2ecf20Sopenharmony_ci */ 21898c2ecf20Sopenharmony_cistatic inline bool sk_has_allocations(const struct sock *sk) 21908c2ecf20Sopenharmony_ci{ 21918c2ecf20Sopenharmony_ci return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk); 21928c2ecf20Sopenharmony_ci} 21938c2ecf20Sopenharmony_ci 21948c2ecf20Sopenharmony_ci/** 21958c2ecf20Sopenharmony_ci * skwq_has_sleeper - check if there are any waiting processes 21968c2ecf20Sopenharmony_ci * @wq: struct socket_wq 21978c2ecf20Sopenharmony_ci * 21988c2ecf20Sopenharmony_ci * Return: true if socket_wq has waiting processes 21998c2ecf20Sopenharmony_ci * 22008c2ecf20Sopenharmony_ci * The purpose of the skwq_has_sleeper and sock_poll_wait is to wrap the memory 22018c2ecf20Sopenharmony_ci * barrier call. They were added due to the race found within the tcp code. 22028c2ecf20Sopenharmony_ci * 22038c2ecf20Sopenharmony_ci * Consider following tcp code paths:: 22048c2ecf20Sopenharmony_ci * 22058c2ecf20Sopenharmony_ci * CPU1 CPU2 22068c2ecf20Sopenharmony_ci * sys_select receive packet 22078c2ecf20Sopenharmony_ci * ... ... 22088c2ecf20Sopenharmony_ci * __add_wait_queue update tp->rcv_nxt 22098c2ecf20Sopenharmony_ci * ... ... 22108c2ecf20Sopenharmony_ci * tp->rcv_nxt check sock_def_readable 22118c2ecf20Sopenharmony_ci * ... { 22128c2ecf20Sopenharmony_ci * schedule rcu_read_lock(); 22138c2ecf20Sopenharmony_ci * wq = rcu_dereference(sk->sk_wq); 22148c2ecf20Sopenharmony_ci * if (wq && waitqueue_active(&wq->wait)) 22158c2ecf20Sopenharmony_ci * wake_up_interruptible(&wq->wait) 22168c2ecf20Sopenharmony_ci * ... 22178c2ecf20Sopenharmony_ci * } 22188c2ecf20Sopenharmony_ci * 22198c2ecf20Sopenharmony_ci * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay 22208c2ecf20Sopenharmony_ci * in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1 22218c2ecf20Sopenharmony_ci * could then endup calling schedule and sleep forever if there are no more 22228c2ecf20Sopenharmony_ci * data on the socket. 22238c2ecf20Sopenharmony_ci * 22248c2ecf20Sopenharmony_ci */ 22258c2ecf20Sopenharmony_cistatic inline bool skwq_has_sleeper(struct socket_wq *wq) 22268c2ecf20Sopenharmony_ci{ 22278c2ecf20Sopenharmony_ci return wq && wq_has_sleeper(&wq->wait); 22288c2ecf20Sopenharmony_ci} 22298c2ecf20Sopenharmony_ci 22308c2ecf20Sopenharmony_ci/** 22318c2ecf20Sopenharmony_ci * sock_poll_wait - place memory barrier behind the poll_wait call. 22328c2ecf20Sopenharmony_ci * @filp: file 22338c2ecf20Sopenharmony_ci * @sock: socket to wait on 22348c2ecf20Sopenharmony_ci * @p: poll_table 22358c2ecf20Sopenharmony_ci * 22368c2ecf20Sopenharmony_ci * See the comments in the wq_has_sleeper function. 22378c2ecf20Sopenharmony_ci */ 22388c2ecf20Sopenharmony_cistatic inline void sock_poll_wait(struct file *filp, struct socket *sock, 22398c2ecf20Sopenharmony_ci poll_table *p) 22408c2ecf20Sopenharmony_ci{ 22418c2ecf20Sopenharmony_ci if (!poll_does_not_wait(p)) { 22428c2ecf20Sopenharmony_ci poll_wait(filp, &sock->wq.wait, p); 22438c2ecf20Sopenharmony_ci /* We need to be sure we are in sync with the 22448c2ecf20Sopenharmony_ci * socket flags modification. 22458c2ecf20Sopenharmony_ci * 22468c2ecf20Sopenharmony_ci * This memory barrier is paired in the wq_has_sleeper. 22478c2ecf20Sopenharmony_ci */ 22488c2ecf20Sopenharmony_ci smp_mb(); 22498c2ecf20Sopenharmony_ci } 22508c2ecf20Sopenharmony_ci} 22518c2ecf20Sopenharmony_ci 22528c2ecf20Sopenharmony_cistatic inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk) 22538c2ecf20Sopenharmony_ci{ 22548c2ecf20Sopenharmony_ci /* This pairs with WRITE_ONCE() in sk_set_txhash() */ 22558c2ecf20Sopenharmony_ci u32 txhash = READ_ONCE(sk->sk_txhash); 22568c2ecf20Sopenharmony_ci 22578c2ecf20Sopenharmony_ci if (txhash) { 22588c2ecf20Sopenharmony_ci skb->l4_hash = 1; 22598c2ecf20Sopenharmony_ci skb->hash = txhash; 22608c2ecf20Sopenharmony_ci } 22618c2ecf20Sopenharmony_ci} 22628c2ecf20Sopenharmony_ci 22638c2ecf20Sopenharmony_civoid skb_set_owner_w(struct sk_buff *skb, struct sock *sk); 22648c2ecf20Sopenharmony_ci 22658c2ecf20Sopenharmony_ci/* 22668c2ecf20Sopenharmony_ci * Queue a received datagram if it will fit. Stream and sequenced 22678c2ecf20Sopenharmony_ci * protocols can't normally use this as they need to fit buffers in 22688c2ecf20Sopenharmony_ci * and play with them. 22698c2ecf20Sopenharmony_ci * 22708c2ecf20Sopenharmony_ci * Inlined as it's very short and called for pretty much every 22718c2ecf20Sopenharmony_ci * packet ever received. 22728c2ecf20Sopenharmony_ci */ 22738c2ecf20Sopenharmony_cistatic inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk) 22748c2ecf20Sopenharmony_ci{ 22758c2ecf20Sopenharmony_ci skb_orphan(skb); 22768c2ecf20Sopenharmony_ci skb->sk = sk; 22778c2ecf20Sopenharmony_ci skb->destructor = sock_rfree; 22788c2ecf20Sopenharmony_ci atomic_add(skb->truesize, &sk->sk_rmem_alloc); 22798c2ecf20Sopenharmony_ci sk_mem_charge(sk, skb->truesize); 22808c2ecf20Sopenharmony_ci} 22818c2ecf20Sopenharmony_ci 22828c2ecf20Sopenharmony_cistatic inline __must_check bool skb_set_owner_sk_safe(struct sk_buff *skb, struct sock *sk) 22838c2ecf20Sopenharmony_ci{ 22848c2ecf20Sopenharmony_ci if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) { 22858c2ecf20Sopenharmony_ci skb_orphan(skb); 22868c2ecf20Sopenharmony_ci skb->destructor = sock_efree; 22878c2ecf20Sopenharmony_ci skb->sk = sk; 22888c2ecf20Sopenharmony_ci return true; 22898c2ecf20Sopenharmony_ci } 22908c2ecf20Sopenharmony_ci return false; 22918c2ecf20Sopenharmony_ci} 22928c2ecf20Sopenharmony_ci 22938c2ecf20Sopenharmony_cistatic inline struct sk_buff *skb_clone_and_charge_r(struct sk_buff *skb, struct sock *sk) 22948c2ecf20Sopenharmony_ci{ 22958c2ecf20Sopenharmony_ci skb = skb_clone(skb, sk_gfp_mask(sk, GFP_ATOMIC)); 22968c2ecf20Sopenharmony_ci if (skb) { 22978c2ecf20Sopenharmony_ci if (sk_rmem_schedule(sk, skb, skb->truesize)) { 22988c2ecf20Sopenharmony_ci skb_set_owner_r(skb, sk); 22998c2ecf20Sopenharmony_ci return skb; 23008c2ecf20Sopenharmony_ci } 23018c2ecf20Sopenharmony_ci __kfree_skb(skb); 23028c2ecf20Sopenharmony_ci } 23038c2ecf20Sopenharmony_ci return NULL; 23048c2ecf20Sopenharmony_ci} 23058c2ecf20Sopenharmony_ci 23068c2ecf20Sopenharmony_civoid sk_reset_timer(struct sock *sk, struct timer_list *timer, 23078c2ecf20Sopenharmony_ci unsigned long expires); 23088c2ecf20Sopenharmony_ci 23098c2ecf20Sopenharmony_civoid sk_stop_timer(struct sock *sk, struct timer_list *timer); 23108c2ecf20Sopenharmony_ci 23118c2ecf20Sopenharmony_civoid sk_stop_timer_sync(struct sock *sk, struct timer_list *timer); 23128c2ecf20Sopenharmony_ci 23138c2ecf20Sopenharmony_ciint __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue, 23148c2ecf20Sopenharmony_ci struct sk_buff *skb, unsigned int flags, 23158c2ecf20Sopenharmony_ci void (*destructor)(struct sock *sk, 23168c2ecf20Sopenharmony_ci struct sk_buff *skb)); 23178c2ecf20Sopenharmony_ciint __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 23188c2ecf20Sopenharmony_ciint sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 23198c2ecf20Sopenharmony_ci 23208c2ecf20Sopenharmony_ciint sock_queue_err_skb(struct sock *sk, struct sk_buff *skb); 23218c2ecf20Sopenharmony_cistruct sk_buff *sock_dequeue_err_skb(struct sock *sk); 23228c2ecf20Sopenharmony_ci 23238c2ecf20Sopenharmony_ci/* 23248c2ecf20Sopenharmony_ci * Recover an error report and clear atomically 23258c2ecf20Sopenharmony_ci */ 23268c2ecf20Sopenharmony_ci 23278c2ecf20Sopenharmony_cistatic inline int sock_error(struct sock *sk) 23288c2ecf20Sopenharmony_ci{ 23298c2ecf20Sopenharmony_ci int err; 23308c2ecf20Sopenharmony_ci 23318c2ecf20Sopenharmony_ci /* Avoid an atomic operation for the common case. 23328c2ecf20Sopenharmony_ci * This is racy since another cpu/thread can change sk_err under us. 23338c2ecf20Sopenharmony_ci */ 23348c2ecf20Sopenharmony_ci if (likely(data_race(!sk->sk_err))) 23358c2ecf20Sopenharmony_ci return 0; 23368c2ecf20Sopenharmony_ci 23378c2ecf20Sopenharmony_ci err = xchg(&sk->sk_err, 0); 23388c2ecf20Sopenharmony_ci return -err; 23398c2ecf20Sopenharmony_ci} 23408c2ecf20Sopenharmony_ci 23418c2ecf20Sopenharmony_cistatic inline unsigned long sock_wspace(struct sock *sk) 23428c2ecf20Sopenharmony_ci{ 23438c2ecf20Sopenharmony_ci int amt = 0; 23448c2ecf20Sopenharmony_ci 23458c2ecf20Sopenharmony_ci if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 23468c2ecf20Sopenharmony_ci amt = sk->sk_sndbuf - refcount_read(&sk->sk_wmem_alloc); 23478c2ecf20Sopenharmony_ci if (amt < 0) 23488c2ecf20Sopenharmony_ci amt = 0; 23498c2ecf20Sopenharmony_ci } 23508c2ecf20Sopenharmony_ci return amt; 23518c2ecf20Sopenharmony_ci} 23528c2ecf20Sopenharmony_ci 23538c2ecf20Sopenharmony_ci/* Note: 23548c2ecf20Sopenharmony_ci * We use sk->sk_wq_raw, from contexts knowing this 23558c2ecf20Sopenharmony_ci * pointer is not NULL and cannot disappear/change. 23568c2ecf20Sopenharmony_ci */ 23578c2ecf20Sopenharmony_cistatic inline void sk_set_bit(int nr, struct sock *sk) 23588c2ecf20Sopenharmony_ci{ 23598c2ecf20Sopenharmony_ci if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) && 23608c2ecf20Sopenharmony_ci !sock_flag(sk, SOCK_FASYNC)) 23618c2ecf20Sopenharmony_ci return; 23628c2ecf20Sopenharmony_ci 23638c2ecf20Sopenharmony_ci set_bit(nr, &sk->sk_wq_raw->flags); 23648c2ecf20Sopenharmony_ci} 23658c2ecf20Sopenharmony_ci 23668c2ecf20Sopenharmony_cistatic inline void sk_clear_bit(int nr, struct sock *sk) 23678c2ecf20Sopenharmony_ci{ 23688c2ecf20Sopenharmony_ci if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) && 23698c2ecf20Sopenharmony_ci !sock_flag(sk, SOCK_FASYNC)) 23708c2ecf20Sopenharmony_ci return; 23718c2ecf20Sopenharmony_ci 23728c2ecf20Sopenharmony_ci clear_bit(nr, &sk->sk_wq_raw->flags); 23738c2ecf20Sopenharmony_ci} 23748c2ecf20Sopenharmony_ci 23758c2ecf20Sopenharmony_cistatic inline void sk_wake_async(const struct sock *sk, int how, int band) 23768c2ecf20Sopenharmony_ci{ 23778c2ecf20Sopenharmony_ci if (sock_flag(sk, SOCK_FASYNC)) { 23788c2ecf20Sopenharmony_ci rcu_read_lock(); 23798c2ecf20Sopenharmony_ci sock_wake_async(rcu_dereference(sk->sk_wq), how, band); 23808c2ecf20Sopenharmony_ci rcu_read_unlock(); 23818c2ecf20Sopenharmony_ci } 23828c2ecf20Sopenharmony_ci} 23838c2ecf20Sopenharmony_ci 23848c2ecf20Sopenharmony_ci/* Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might 23858c2ecf20Sopenharmony_ci * need sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak. 23868c2ecf20Sopenharmony_ci * Note: for send buffers, TCP works better if we can build two skbs at 23878c2ecf20Sopenharmony_ci * minimum. 23888c2ecf20Sopenharmony_ci */ 23898c2ecf20Sopenharmony_ci#define TCP_SKB_MIN_TRUESIZE (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff))) 23908c2ecf20Sopenharmony_ci 23918c2ecf20Sopenharmony_ci#define SOCK_MIN_SNDBUF (TCP_SKB_MIN_TRUESIZE * 2) 23928c2ecf20Sopenharmony_ci#define SOCK_MIN_RCVBUF TCP_SKB_MIN_TRUESIZE 23938c2ecf20Sopenharmony_ci 23948c2ecf20Sopenharmony_cistatic inline void sk_stream_moderate_sndbuf(struct sock *sk) 23958c2ecf20Sopenharmony_ci{ 23968c2ecf20Sopenharmony_ci u32 val; 23978c2ecf20Sopenharmony_ci 23988c2ecf20Sopenharmony_ci if (sk->sk_userlocks & SOCK_SNDBUF_LOCK) 23998c2ecf20Sopenharmony_ci return; 24008c2ecf20Sopenharmony_ci 24018c2ecf20Sopenharmony_ci val = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1); 24028c2ecf20Sopenharmony_ci 24038c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_sndbuf, max_t(u32, val, SOCK_MIN_SNDBUF)); 24048c2ecf20Sopenharmony_ci} 24058c2ecf20Sopenharmony_ci 24068c2ecf20Sopenharmony_cistruct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp, 24078c2ecf20Sopenharmony_ci bool force_schedule); 24088c2ecf20Sopenharmony_ci 24098c2ecf20Sopenharmony_ci/** 24108c2ecf20Sopenharmony_ci * sk_page_frag - return an appropriate page_frag 24118c2ecf20Sopenharmony_ci * @sk: socket 24128c2ecf20Sopenharmony_ci * 24138c2ecf20Sopenharmony_ci * Use the per task page_frag instead of the per socket one for 24148c2ecf20Sopenharmony_ci * optimization when we know that we're in process context and own 24158c2ecf20Sopenharmony_ci * everything that's associated with %current. 24168c2ecf20Sopenharmony_ci * 24178c2ecf20Sopenharmony_ci * Both direct reclaim and page faults can nest inside other 24188c2ecf20Sopenharmony_ci * socket operations and end up recursing into sk_page_frag() 24198c2ecf20Sopenharmony_ci * while it's already in use: explicitly avoid task page_frag 24208c2ecf20Sopenharmony_ci * usage if the caller is potentially doing any of them. 24218c2ecf20Sopenharmony_ci * This assumes that page fault handlers use the GFP_NOFS flags. 24228c2ecf20Sopenharmony_ci * 24238c2ecf20Sopenharmony_ci * Return: a per task page_frag if context allows that, 24248c2ecf20Sopenharmony_ci * otherwise a per socket one. 24258c2ecf20Sopenharmony_ci */ 24268c2ecf20Sopenharmony_cistatic inline struct page_frag *sk_page_frag(struct sock *sk) 24278c2ecf20Sopenharmony_ci{ 24288c2ecf20Sopenharmony_ci if ((sk->sk_allocation & (__GFP_DIRECT_RECLAIM | __GFP_MEMALLOC | __GFP_FS)) == 24298c2ecf20Sopenharmony_ci (__GFP_DIRECT_RECLAIM | __GFP_FS)) 24308c2ecf20Sopenharmony_ci return ¤t->task_frag; 24318c2ecf20Sopenharmony_ci 24328c2ecf20Sopenharmony_ci return &sk->sk_frag; 24338c2ecf20Sopenharmony_ci} 24348c2ecf20Sopenharmony_ci 24358c2ecf20Sopenharmony_cibool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag); 24368c2ecf20Sopenharmony_ci 24378c2ecf20Sopenharmony_ci/* 24388c2ecf20Sopenharmony_ci * Default write policy as shown to user space via poll/select/SIGIO 24398c2ecf20Sopenharmony_ci */ 24408c2ecf20Sopenharmony_cistatic inline bool sock_writeable(const struct sock *sk) 24418c2ecf20Sopenharmony_ci{ 24428c2ecf20Sopenharmony_ci return refcount_read(&sk->sk_wmem_alloc) < (READ_ONCE(sk->sk_sndbuf) >> 1); 24438c2ecf20Sopenharmony_ci} 24448c2ecf20Sopenharmony_ci 24458c2ecf20Sopenharmony_cistatic inline gfp_t gfp_any(void) 24468c2ecf20Sopenharmony_ci{ 24478c2ecf20Sopenharmony_ci return in_softirq() ? GFP_ATOMIC : GFP_KERNEL; 24488c2ecf20Sopenharmony_ci} 24498c2ecf20Sopenharmony_ci 24508c2ecf20Sopenharmony_cistatic inline long sock_rcvtimeo(const struct sock *sk, bool noblock) 24518c2ecf20Sopenharmony_ci{ 24528c2ecf20Sopenharmony_ci return noblock ? 0 : sk->sk_rcvtimeo; 24538c2ecf20Sopenharmony_ci} 24548c2ecf20Sopenharmony_ci 24558c2ecf20Sopenharmony_cistatic inline long sock_sndtimeo(const struct sock *sk, bool noblock) 24568c2ecf20Sopenharmony_ci{ 24578c2ecf20Sopenharmony_ci return noblock ? 0 : sk->sk_sndtimeo; 24588c2ecf20Sopenharmony_ci} 24598c2ecf20Sopenharmony_ci 24608c2ecf20Sopenharmony_cistatic inline int sock_rcvlowat(const struct sock *sk, int waitall, int len) 24618c2ecf20Sopenharmony_ci{ 24628c2ecf20Sopenharmony_ci int v = waitall ? len : min_t(int, READ_ONCE(sk->sk_rcvlowat), len); 24638c2ecf20Sopenharmony_ci 24648c2ecf20Sopenharmony_ci return v ?: 1; 24658c2ecf20Sopenharmony_ci} 24668c2ecf20Sopenharmony_ci 24678c2ecf20Sopenharmony_ci/* Alas, with timeout socket operations are not restartable. 24688c2ecf20Sopenharmony_ci * Compare this to poll(). 24698c2ecf20Sopenharmony_ci */ 24708c2ecf20Sopenharmony_cistatic inline int sock_intr_errno(long timeo) 24718c2ecf20Sopenharmony_ci{ 24728c2ecf20Sopenharmony_ci return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR; 24738c2ecf20Sopenharmony_ci} 24748c2ecf20Sopenharmony_ci 24758c2ecf20Sopenharmony_cistruct sock_skb_cb { 24768c2ecf20Sopenharmony_ci u32 dropcount; 24778c2ecf20Sopenharmony_ci}; 24788c2ecf20Sopenharmony_ci 24798c2ecf20Sopenharmony_ci/* Store sock_skb_cb at the end of skb->cb[] so protocol families 24808c2ecf20Sopenharmony_ci * using skb->cb[] would keep using it directly and utilize its 24818c2ecf20Sopenharmony_ci * alignement guarantee. 24828c2ecf20Sopenharmony_ci */ 24838c2ecf20Sopenharmony_ci#define SOCK_SKB_CB_OFFSET ((sizeof_field(struct sk_buff, cb) - \ 24848c2ecf20Sopenharmony_ci sizeof(struct sock_skb_cb))) 24858c2ecf20Sopenharmony_ci 24868c2ecf20Sopenharmony_ci#define SOCK_SKB_CB(__skb) ((struct sock_skb_cb *)((__skb)->cb + \ 24878c2ecf20Sopenharmony_ci SOCK_SKB_CB_OFFSET)) 24888c2ecf20Sopenharmony_ci 24898c2ecf20Sopenharmony_ci#define sock_skb_cb_check_size(size) \ 24908c2ecf20Sopenharmony_ci BUILD_BUG_ON((size) > SOCK_SKB_CB_OFFSET) 24918c2ecf20Sopenharmony_ci 24928c2ecf20Sopenharmony_cistatic inline void 24938c2ecf20Sopenharmony_cisock_skb_set_dropcount(const struct sock *sk, struct sk_buff *skb) 24948c2ecf20Sopenharmony_ci{ 24958c2ecf20Sopenharmony_ci SOCK_SKB_CB(skb)->dropcount = sock_flag(sk, SOCK_RXQ_OVFL) ? 24968c2ecf20Sopenharmony_ci atomic_read(&sk->sk_drops) : 0; 24978c2ecf20Sopenharmony_ci} 24988c2ecf20Sopenharmony_ci 24998c2ecf20Sopenharmony_cistatic inline void sk_drops_add(struct sock *sk, const struct sk_buff *skb) 25008c2ecf20Sopenharmony_ci{ 25018c2ecf20Sopenharmony_ci int segs = max_t(u16, 1, skb_shinfo(skb)->gso_segs); 25028c2ecf20Sopenharmony_ci 25038c2ecf20Sopenharmony_ci atomic_add(segs, &sk->sk_drops); 25048c2ecf20Sopenharmony_ci} 25058c2ecf20Sopenharmony_ci 25068c2ecf20Sopenharmony_cistatic inline ktime_t sock_read_timestamp(struct sock *sk) 25078c2ecf20Sopenharmony_ci{ 25088c2ecf20Sopenharmony_ci#if BITS_PER_LONG==32 25098c2ecf20Sopenharmony_ci unsigned int seq; 25108c2ecf20Sopenharmony_ci ktime_t kt; 25118c2ecf20Sopenharmony_ci 25128c2ecf20Sopenharmony_ci do { 25138c2ecf20Sopenharmony_ci seq = read_seqbegin(&sk->sk_stamp_seq); 25148c2ecf20Sopenharmony_ci kt = sk->sk_stamp; 25158c2ecf20Sopenharmony_ci } while (read_seqretry(&sk->sk_stamp_seq, seq)); 25168c2ecf20Sopenharmony_ci 25178c2ecf20Sopenharmony_ci return kt; 25188c2ecf20Sopenharmony_ci#else 25198c2ecf20Sopenharmony_ci return READ_ONCE(sk->sk_stamp); 25208c2ecf20Sopenharmony_ci#endif 25218c2ecf20Sopenharmony_ci} 25228c2ecf20Sopenharmony_ci 25238c2ecf20Sopenharmony_cistatic inline void sock_write_timestamp(struct sock *sk, ktime_t kt) 25248c2ecf20Sopenharmony_ci{ 25258c2ecf20Sopenharmony_ci#if BITS_PER_LONG==32 25268c2ecf20Sopenharmony_ci write_seqlock(&sk->sk_stamp_seq); 25278c2ecf20Sopenharmony_ci sk->sk_stamp = kt; 25288c2ecf20Sopenharmony_ci write_sequnlock(&sk->sk_stamp_seq); 25298c2ecf20Sopenharmony_ci#else 25308c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_stamp, kt); 25318c2ecf20Sopenharmony_ci#endif 25328c2ecf20Sopenharmony_ci} 25338c2ecf20Sopenharmony_ci 25348c2ecf20Sopenharmony_civoid __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, 25358c2ecf20Sopenharmony_ci struct sk_buff *skb); 25368c2ecf20Sopenharmony_civoid __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk, 25378c2ecf20Sopenharmony_ci struct sk_buff *skb); 25388c2ecf20Sopenharmony_ci 25398c2ecf20Sopenharmony_cistatic inline void 25408c2ecf20Sopenharmony_cisock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb) 25418c2ecf20Sopenharmony_ci{ 25428c2ecf20Sopenharmony_ci ktime_t kt = skb->tstamp; 25438c2ecf20Sopenharmony_ci struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb); 25448c2ecf20Sopenharmony_ci 25458c2ecf20Sopenharmony_ci /* 25468c2ecf20Sopenharmony_ci * generate control messages if 25478c2ecf20Sopenharmony_ci * - receive time stamping in software requested 25488c2ecf20Sopenharmony_ci * - software time stamp available and wanted 25498c2ecf20Sopenharmony_ci * - hardware time stamps available and wanted 25508c2ecf20Sopenharmony_ci */ 25518c2ecf20Sopenharmony_ci if (sock_flag(sk, SOCK_RCVTSTAMP) || 25528c2ecf20Sopenharmony_ci (sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) || 25538c2ecf20Sopenharmony_ci (kt && sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) || 25548c2ecf20Sopenharmony_ci (hwtstamps->hwtstamp && 25558c2ecf20Sopenharmony_ci (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE))) 25568c2ecf20Sopenharmony_ci __sock_recv_timestamp(msg, sk, skb); 25578c2ecf20Sopenharmony_ci else 25588c2ecf20Sopenharmony_ci sock_write_timestamp(sk, kt); 25598c2ecf20Sopenharmony_ci 25608c2ecf20Sopenharmony_ci if (sock_flag(sk, SOCK_WIFI_STATUS) && skb->wifi_acked_valid) 25618c2ecf20Sopenharmony_ci __sock_recv_wifi_status(msg, sk, skb); 25628c2ecf20Sopenharmony_ci} 25638c2ecf20Sopenharmony_ci 25648c2ecf20Sopenharmony_civoid __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk, 25658c2ecf20Sopenharmony_ci struct sk_buff *skb); 25668c2ecf20Sopenharmony_ci 25678c2ecf20Sopenharmony_ci#define SK_DEFAULT_STAMP (-1L * NSEC_PER_SEC) 25688c2ecf20Sopenharmony_cistatic inline void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk, 25698c2ecf20Sopenharmony_ci struct sk_buff *skb) 25708c2ecf20Sopenharmony_ci{ 25718c2ecf20Sopenharmony_ci#define FLAGS_TS_OR_DROPS ((1UL << SOCK_RXQ_OVFL) | \ 25728c2ecf20Sopenharmony_ci (1UL << SOCK_RCVTSTAMP)) 25738c2ecf20Sopenharmony_ci#define TSFLAGS_ANY (SOF_TIMESTAMPING_SOFTWARE | \ 25748c2ecf20Sopenharmony_ci SOF_TIMESTAMPING_RAW_HARDWARE) 25758c2ecf20Sopenharmony_ci 25768c2ecf20Sopenharmony_ci if (sk->sk_flags & FLAGS_TS_OR_DROPS || sk->sk_tsflags & TSFLAGS_ANY) 25778c2ecf20Sopenharmony_ci __sock_recv_ts_and_drops(msg, sk, skb); 25788c2ecf20Sopenharmony_ci else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP))) 25798c2ecf20Sopenharmony_ci sock_write_timestamp(sk, skb->tstamp); 25808c2ecf20Sopenharmony_ci else if (unlikely(sock_read_timestamp(sk) == SK_DEFAULT_STAMP)) 25818c2ecf20Sopenharmony_ci sock_write_timestamp(sk, 0); 25828c2ecf20Sopenharmony_ci} 25838c2ecf20Sopenharmony_ci 25848c2ecf20Sopenharmony_civoid __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags); 25858c2ecf20Sopenharmony_ci 25868c2ecf20Sopenharmony_ci/** 25878c2ecf20Sopenharmony_ci * _sock_tx_timestamp - checks whether the outgoing packet is to be time stamped 25888c2ecf20Sopenharmony_ci * @sk: socket sending this packet 25898c2ecf20Sopenharmony_ci * @tsflags: timestamping flags to use 25908c2ecf20Sopenharmony_ci * @tx_flags: completed with instructions for time stamping 25918c2ecf20Sopenharmony_ci * @tskey: filled in with next sk_tskey (not for TCP, which uses seqno) 25928c2ecf20Sopenharmony_ci * 25938c2ecf20Sopenharmony_ci * Note: callers should take care of initial ``*tx_flags`` value (usually 0) 25948c2ecf20Sopenharmony_ci */ 25958c2ecf20Sopenharmony_cistatic inline void _sock_tx_timestamp(struct sock *sk, __u16 tsflags, 25968c2ecf20Sopenharmony_ci __u8 *tx_flags, __u32 *tskey) 25978c2ecf20Sopenharmony_ci{ 25988c2ecf20Sopenharmony_ci if (unlikely(tsflags)) { 25998c2ecf20Sopenharmony_ci __sock_tx_timestamp(tsflags, tx_flags); 26008c2ecf20Sopenharmony_ci if (tsflags & SOF_TIMESTAMPING_OPT_ID && tskey && 26018c2ecf20Sopenharmony_ci tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) 26028c2ecf20Sopenharmony_ci *tskey = sk->sk_tskey++; 26038c2ecf20Sopenharmony_ci } 26048c2ecf20Sopenharmony_ci if (unlikely(sock_flag(sk, SOCK_WIFI_STATUS))) 26058c2ecf20Sopenharmony_ci *tx_flags |= SKBTX_WIFI_STATUS; 26068c2ecf20Sopenharmony_ci} 26078c2ecf20Sopenharmony_ci 26088c2ecf20Sopenharmony_cistatic inline void sock_tx_timestamp(struct sock *sk, __u16 tsflags, 26098c2ecf20Sopenharmony_ci __u8 *tx_flags) 26108c2ecf20Sopenharmony_ci{ 26118c2ecf20Sopenharmony_ci _sock_tx_timestamp(sk, tsflags, tx_flags, NULL); 26128c2ecf20Sopenharmony_ci} 26138c2ecf20Sopenharmony_ci 26148c2ecf20Sopenharmony_cistatic inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags) 26158c2ecf20Sopenharmony_ci{ 26168c2ecf20Sopenharmony_ci _sock_tx_timestamp(skb->sk, tsflags, &skb_shinfo(skb)->tx_flags, 26178c2ecf20Sopenharmony_ci &skb_shinfo(skb)->tskey); 26188c2ecf20Sopenharmony_ci} 26198c2ecf20Sopenharmony_ci 26208c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(tcp_rx_skb_cache_key); 26218c2ecf20Sopenharmony_ci/** 26228c2ecf20Sopenharmony_ci * sk_eat_skb - Release a skb if it is no longer needed 26238c2ecf20Sopenharmony_ci * @sk: socket to eat this skb from 26248c2ecf20Sopenharmony_ci * @skb: socket buffer to eat 26258c2ecf20Sopenharmony_ci * 26268c2ecf20Sopenharmony_ci * This routine must be called with interrupts disabled or with the socket 26278c2ecf20Sopenharmony_ci * locked so that the sk_buff queue operation is ok. 26288c2ecf20Sopenharmony_ci*/ 26298c2ecf20Sopenharmony_cistatic inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb) 26308c2ecf20Sopenharmony_ci{ 26318c2ecf20Sopenharmony_ci __skb_unlink(skb, &sk->sk_receive_queue); 26328c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_rx_skb_cache_key) && 26338c2ecf20Sopenharmony_ci !sk->sk_rx_skb_cache) { 26348c2ecf20Sopenharmony_ci sk->sk_rx_skb_cache = skb; 26358c2ecf20Sopenharmony_ci skb_orphan(skb); 26368c2ecf20Sopenharmony_ci return; 26378c2ecf20Sopenharmony_ci } 26388c2ecf20Sopenharmony_ci __kfree_skb(skb); 26398c2ecf20Sopenharmony_ci} 26408c2ecf20Sopenharmony_ci 26418c2ecf20Sopenharmony_cistatic inline 26428c2ecf20Sopenharmony_cistruct net *sock_net(const struct sock *sk) 26438c2ecf20Sopenharmony_ci{ 26448c2ecf20Sopenharmony_ci return read_pnet(&sk->sk_net); 26458c2ecf20Sopenharmony_ci} 26468c2ecf20Sopenharmony_ci 26478c2ecf20Sopenharmony_cistatic inline 26488c2ecf20Sopenharmony_civoid sock_net_set(struct sock *sk, struct net *net) 26498c2ecf20Sopenharmony_ci{ 26508c2ecf20Sopenharmony_ci write_pnet(&sk->sk_net, net); 26518c2ecf20Sopenharmony_ci} 26528c2ecf20Sopenharmony_ci 26538c2ecf20Sopenharmony_cistatic inline bool 26548c2ecf20Sopenharmony_ciskb_sk_is_prefetched(struct sk_buff *skb) 26558c2ecf20Sopenharmony_ci{ 26568c2ecf20Sopenharmony_ci#ifdef CONFIG_INET 26578c2ecf20Sopenharmony_ci return skb->destructor == sock_pfree; 26588c2ecf20Sopenharmony_ci#else 26598c2ecf20Sopenharmony_ci return false; 26608c2ecf20Sopenharmony_ci#endif /* CONFIG_INET */ 26618c2ecf20Sopenharmony_ci} 26628c2ecf20Sopenharmony_ci 26638c2ecf20Sopenharmony_ci/* This helper checks if a socket is a full socket, 26648c2ecf20Sopenharmony_ci * ie _not_ a timewait or request socket. 26658c2ecf20Sopenharmony_ci */ 26668c2ecf20Sopenharmony_cistatic inline bool sk_fullsock(const struct sock *sk) 26678c2ecf20Sopenharmony_ci{ 26688c2ecf20Sopenharmony_ci return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV); 26698c2ecf20Sopenharmony_ci} 26708c2ecf20Sopenharmony_ci 26718c2ecf20Sopenharmony_cistatic inline bool 26728c2ecf20Sopenharmony_cisk_is_refcounted(struct sock *sk) 26738c2ecf20Sopenharmony_ci{ 26748c2ecf20Sopenharmony_ci /* Only full sockets have sk->sk_flags. */ 26758c2ecf20Sopenharmony_ci return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE); 26768c2ecf20Sopenharmony_ci} 26778c2ecf20Sopenharmony_ci 26788c2ecf20Sopenharmony_ci/** 26798c2ecf20Sopenharmony_ci * skb_steal_sock - steal a socket from an sk_buff 26808c2ecf20Sopenharmony_ci * @skb: sk_buff to steal the socket from 26818c2ecf20Sopenharmony_ci * @refcounted: is set to true if the socket is reference-counted 26828c2ecf20Sopenharmony_ci */ 26838c2ecf20Sopenharmony_cistatic inline struct sock * 26848c2ecf20Sopenharmony_ciskb_steal_sock(struct sk_buff *skb, bool *refcounted) 26858c2ecf20Sopenharmony_ci{ 26868c2ecf20Sopenharmony_ci if (skb->sk) { 26878c2ecf20Sopenharmony_ci struct sock *sk = skb->sk; 26888c2ecf20Sopenharmony_ci 26898c2ecf20Sopenharmony_ci *refcounted = true; 26908c2ecf20Sopenharmony_ci if (skb_sk_is_prefetched(skb)) 26918c2ecf20Sopenharmony_ci *refcounted = sk_is_refcounted(sk); 26928c2ecf20Sopenharmony_ci skb->destructor = NULL; 26938c2ecf20Sopenharmony_ci skb->sk = NULL; 26948c2ecf20Sopenharmony_ci return sk; 26958c2ecf20Sopenharmony_ci } 26968c2ecf20Sopenharmony_ci *refcounted = false; 26978c2ecf20Sopenharmony_ci return NULL; 26988c2ecf20Sopenharmony_ci} 26998c2ecf20Sopenharmony_ci 27008c2ecf20Sopenharmony_ci/* Checks if this SKB belongs to an HW offloaded socket 27018c2ecf20Sopenharmony_ci * and whether any SW fallbacks are required based on dev. 27028c2ecf20Sopenharmony_ci * Check decrypted mark in case skb_orphan() cleared socket. 27038c2ecf20Sopenharmony_ci */ 27048c2ecf20Sopenharmony_cistatic inline struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb, 27058c2ecf20Sopenharmony_ci struct net_device *dev) 27068c2ecf20Sopenharmony_ci{ 27078c2ecf20Sopenharmony_ci#ifdef CONFIG_SOCK_VALIDATE_XMIT 27088c2ecf20Sopenharmony_ci struct sock *sk = skb->sk; 27098c2ecf20Sopenharmony_ci 27108c2ecf20Sopenharmony_ci if (sk && sk_fullsock(sk) && sk->sk_validate_xmit_skb) { 27118c2ecf20Sopenharmony_ci skb = sk->sk_validate_xmit_skb(sk, dev, skb); 27128c2ecf20Sopenharmony_ci#ifdef CONFIG_TLS_DEVICE 27138c2ecf20Sopenharmony_ci } else if (unlikely(skb->decrypted)) { 27148c2ecf20Sopenharmony_ci pr_warn_ratelimited("unencrypted skb with no associated socket - dropping\n"); 27158c2ecf20Sopenharmony_ci kfree_skb(skb); 27168c2ecf20Sopenharmony_ci skb = NULL; 27178c2ecf20Sopenharmony_ci#endif 27188c2ecf20Sopenharmony_ci } 27198c2ecf20Sopenharmony_ci#endif 27208c2ecf20Sopenharmony_ci 27218c2ecf20Sopenharmony_ci return skb; 27228c2ecf20Sopenharmony_ci} 27238c2ecf20Sopenharmony_ci 27248c2ecf20Sopenharmony_ci/* This helper checks if a socket is a LISTEN or NEW_SYN_RECV 27258c2ecf20Sopenharmony_ci * SYNACK messages can be attached to either ones (depending on SYNCOOKIE) 27268c2ecf20Sopenharmony_ci */ 27278c2ecf20Sopenharmony_cistatic inline bool sk_listener(const struct sock *sk) 27288c2ecf20Sopenharmony_ci{ 27298c2ecf20Sopenharmony_ci return (1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV); 27308c2ecf20Sopenharmony_ci} 27318c2ecf20Sopenharmony_ci 27328c2ecf20Sopenharmony_civoid sock_enable_timestamp(struct sock *sk, enum sock_flags flag); 27338c2ecf20Sopenharmony_ciint sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level, 27348c2ecf20Sopenharmony_ci int type); 27358c2ecf20Sopenharmony_ci 27368c2ecf20Sopenharmony_cibool sk_ns_capable(const struct sock *sk, 27378c2ecf20Sopenharmony_ci struct user_namespace *user_ns, int cap); 27388c2ecf20Sopenharmony_cibool sk_capable(const struct sock *sk, int cap); 27398c2ecf20Sopenharmony_cibool sk_net_capable(const struct sock *sk, int cap); 27408c2ecf20Sopenharmony_ci 27418c2ecf20Sopenharmony_civoid sk_get_meminfo(const struct sock *sk, u32 *meminfo); 27428c2ecf20Sopenharmony_ci 27438c2ecf20Sopenharmony_ci/* Take into consideration the size of the struct sk_buff overhead in the 27448c2ecf20Sopenharmony_ci * determination of these values, since that is non-constant across 27458c2ecf20Sopenharmony_ci * platforms. This makes socket queueing behavior and performance 27468c2ecf20Sopenharmony_ci * not depend upon such differences. 27478c2ecf20Sopenharmony_ci */ 27488c2ecf20Sopenharmony_ci#define _SK_MEM_PACKETS 256 27498c2ecf20Sopenharmony_ci#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256) 27508c2ecf20Sopenharmony_ci#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS) 27518c2ecf20Sopenharmony_ci#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS) 27528c2ecf20Sopenharmony_ci 27538c2ecf20Sopenharmony_ciextern __u32 sysctl_wmem_max; 27548c2ecf20Sopenharmony_ciextern __u32 sysctl_rmem_max; 27558c2ecf20Sopenharmony_ci 27568c2ecf20Sopenharmony_ciextern int sysctl_tstamp_allow_data; 27578c2ecf20Sopenharmony_ciextern int sysctl_optmem_max; 27588c2ecf20Sopenharmony_ci 27598c2ecf20Sopenharmony_ciextern __u32 sysctl_wmem_default; 27608c2ecf20Sopenharmony_ciextern __u32 sysctl_rmem_default; 27618c2ecf20Sopenharmony_ci 27628c2ecf20Sopenharmony_ci#define SKB_FRAG_PAGE_ORDER get_order(32768) 27638c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key); 27648c2ecf20Sopenharmony_ci 27658c2ecf20Sopenharmony_cistatic inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto) 27668c2ecf20Sopenharmony_ci{ 27678c2ecf20Sopenharmony_ci /* Does this proto have per netns sysctl_wmem ? */ 27688c2ecf20Sopenharmony_ci if (proto->sysctl_wmem_offset) 27698c2ecf20Sopenharmony_ci return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_wmem_offset)); 27708c2ecf20Sopenharmony_ci 27718c2ecf20Sopenharmony_ci return READ_ONCE(*proto->sysctl_wmem); 27728c2ecf20Sopenharmony_ci} 27738c2ecf20Sopenharmony_ci 27748c2ecf20Sopenharmony_cistatic inline int sk_get_rmem0(const struct sock *sk, const struct proto *proto) 27758c2ecf20Sopenharmony_ci{ 27768c2ecf20Sopenharmony_ci /* Does this proto have per netns sysctl_rmem ? */ 27778c2ecf20Sopenharmony_ci if (proto->sysctl_rmem_offset) 27788c2ecf20Sopenharmony_ci return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_rmem_offset)); 27798c2ecf20Sopenharmony_ci 27808c2ecf20Sopenharmony_ci return READ_ONCE(*proto->sysctl_rmem); 27818c2ecf20Sopenharmony_ci} 27828c2ecf20Sopenharmony_ci 27838c2ecf20Sopenharmony_ci/* Default TCP Small queue budget is ~1 ms of data (1sec >> 10) 27848c2ecf20Sopenharmony_ci * Some wifi drivers need to tweak it to get more chunks. 27858c2ecf20Sopenharmony_ci * They can use this helper from their ndo_start_xmit() 27868c2ecf20Sopenharmony_ci */ 27878c2ecf20Sopenharmony_cistatic inline void sk_pacing_shift_update(struct sock *sk, int val) 27888c2ecf20Sopenharmony_ci{ 27898c2ecf20Sopenharmony_ci if (!sk || !sk_fullsock(sk) || READ_ONCE(sk->sk_pacing_shift) == val) 27908c2ecf20Sopenharmony_ci return; 27918c2ecf20Sopenharmony_ci WRITE_ONCE(sk->sk_pacing_shift, val); 27928c2ecf20Sopenharmony_ci} 27938c2ecf20Sopenharmony_ci 27948c2ecf20Sopenharmony_ci/* if a socket is bound to a device, check that the given device 27958c2ecf20Sopenharmony_ci * index is either the same or that the socket is bound to an L3 27968c2ecf20Sopenharmony_ci * master device and the given device index is also enslaved to 27978c2ecf20Sopenharmony_ci * that L3 master 27988c2ecf20Sopenharmony_ci */ 27998c2ecf20Sopenharmony_cistatic inline bool sk_dev_equal_l3scope(struct sock *sk, int dif) 28008c2ecf20Sopenharmony_ci{ 28018c2ecf20Sopenharmony_ci int mdif; 28028c2ecf20Sopenharmony_ci 28038c2ecf20Sopenharmony_ci if (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif) 28048c2ecf20Sopenharmony_ci return true; 28058c2ecf20Sopenharmony_ci 28068c2ecf20Sopenharmony_ci mdif = l3mdev_master_ifindex_by_index(sock_net(sk), dif); 28078c2ecf20Sopenharmony_ci if (mdif && mdif == sk->sk_bound_dev_if) 28088c2ecf20Sopenharmony_ci return true; 28098c2ecf20Sopenharmony_ci 28108c2ecf20Sopenharmony_ci return false; 28118c2ecf20Sopenharmony_ci} 28128c2ecf20Sopenharmony_ci 28138c2ecf20Sopenharmony_civoid sock_def_readable(struct sock *sk); 28148c2ecf20Sopenharmony_ci 28158c2ecf20Sopenharmony_ciint sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk); 28168c2ecf20Sopenharmony_civoid sock_enable_timestamps(struct sock *sk); 28178c2ecf20Sopenharmony_civoid sock_no_linger(struct sock *sk); 28188c2ecf20Sopenharmony_civoid sock_set_keepalive(struct sock *sk); 28198c2ecf20Sopenharmony_civoid sock_set_priority(struct sock *sk, u32 priority); 28208c2ecf20Sopenharmony_civoid sock_set_rcvbuf(struct sock *sk, int val); 28218c2ecf20Sopenharmony_civoid sock_set_mark(struct sock *sk, u32 val); 28228c2ecf20Sopenharmony_civoid sock_set_reuseaddr(struct sock *sk); 28238c2ecf20Sopenharmony_civoid sock_set_reuseport(struct sock *sk); 28248c2ecf20Sopenharmony_civoid sock_set_sndtimeo(struct sock *sk, s64 secs); 28258c2ecf20Sopenharmony_ci 28268c2ecf20Sopenharmony_ciint sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len); 28278c2ecf20Sopenharmony_ci 28288c2ecf20Sopenharmony_ci#endif /* _SOCK_H */ 2829