xref: /openbmc/linux/include/net/sock.h (revision c7cbdbf29f488a19982cd9f4a109887f18028bbb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the AF_INET socket handler.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)sock.h	1.0.4	05/13/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *		Corey Minyard <wf-rch!minyard@relay.EU.net>
131da177e4SLinus Torvalds  *		Florian La Roche <flla@stud.uni-sb.de>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Fixes:
161da177e4SLinus Torvalds  *		Alan Cox	:	Volatiles in skbuff pointers. See
171da177e4SLinus Torvalds  *					skbuff comments. May be overdone,
181da177e4SLinus Torvalds  *					better to prove they can be removed
191da177e4SLinus Torvalds  *					than the reverse.
201da177e4SLinus Torvalds  *		Alan Cox	:	Added a zapped field for tcp to note
211da177e4SLinus Torvalds  *					a socket is reset and must stay shut up
221da177e4SLinus Torvalds  *		Alan Cox	:	New fields for options
231da177e4SLinus Torvalds  *	Pauline Middelink	:	identd support
241da177e4SLinus Torvalds  *		Alan Cox	:	Eliminate low level recv/recvfrom
251da177e4SLinus Torvalds  *		David S. Miller	:	New socket lookup architecture.
261da177e4SLinus Torvalds  *              Steve Whitehouse:       Default routines for sock_ops
271da177e4SLinus Torvalds  *              Arnaldo C. Melo :	removed net_pinfo, tp_pinfo and made
281da177e4SLinus Torvalds  *              			protinfo be just a void pointer, as the
291da177e4SLinus Torvalds  *              			protocol specific parts were moved to
301da177e4SLinus Torvalds  *              			respective headers and ipv4/v6, etc now
311da177e4SLinus Torvalds  *              			use private slabcaches for its socks
321da177e4SLinus Torvalds  *              Pedro Hortas	:	New flags field for socket options
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
361da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
371da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
381da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds #ifndef _SOCK_H
411da177e4SLinus Torvalds #define _SOCK_H
421da177e4SLinus Torvalds 
43a6b7a407SAlexey Dobriyan #include <linux/hardirq.h>
44172589ccSIlpo Järvinen #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/list.h>
4688ab1932SEric Dumazet #include <linux/list_nulls.h>
471da177e4SLinus Torvalds #include <linux/timer.h>
481da177e4SLinus Torvalds #include <linux/cache.h>
493f134619SGlauber Costa #include <linux/bitops.h>
50a5b5bb9aSIngo Molnar #include <linux/lockdep.h>
511da177e4SLinus Torvalds #include <linux/netdevice.h>
521da177e4SLinus Torvalds #include <linux/skbuff.h>	/* struct sk_buff */
53d7fe0f24SAl Viro #include <linux/mm.h>
541da177e4SLinus Torvalds #include <linux/security.h>
555a0e3ad6STejun Heo #include <linux/slab.h>
56c6e1a0d1STom Herbert #include <linux/uaccess.h>
573e32cb2eSJohannes Weiner #include <linux/page_counter.h>
58180d8cd9SGlauber Costa #include <linux/memcontrol.h>
59c5905afbSIngo Molnar #include <linux/static_key.h>
6040401530SAl Viro #include <linux/sched.h>
611ce0bf50SHerbert Xu #include <linux/wait.h>
622a56a1feSTejun Heo #include <linux/cgroup-defs.h>
6375c119afSEric Dumazet #include <linux/rbtree.h>
641da177e4SLinus Torvalds #include <linux/filter.h>
6588ab1932SEric Dumazet #include <linux/rculist_nulls.h>
66a57de0b4SJiri Olsa #include <linux/poll.h>
671da177e4SLinus Torvalds 
68c31504dcSEric Dumazet #include <linux/atomic.h>
6941c6d650SReshetova, Elena #include <linux/refcount.h>
701da177e4SLinus Torvalds #include <net/dst.h>
711da177e4SLinus Torvalds #include <net/checksum.h>
721d0ab253SEric Dumazet #include <net/tcp_states.h>
73b9f40e21SWillem de Bruijn #include <linux/net_tstamp.h>
74f16a7dd5SUrsula Braun #include <net/smc.h>
7554dc3e33SDavid Ahern #include <net/l3mdev.h>
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * This structure really needs to be cleaned up.
791da177e4SLinus Torvalds  * Most of it is for TCP, and not used by any of
801da177e4SLinus Torvalds  * the other protocols.
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* Define this to get the SOCK_DBG debugging facility. */
841da177e4SLinus Torvalds #define SOCK_DEBUGGING
851da177e4SLinus Torvalds #ifdef SOCK_DEBUGGING
861da177e4SLinus Torvalds #define SOCK_DEBUG(sk, msg...) do { if ((sk) && sock_flag((sk), SOCK_DBG)) \
871da177e4SLinus Torvalds 					printk(KERN_DEBUG msg); } while (0)
881da177e4SLinus Torvalds #else
894cd9029dSStephen Hemminger /* Validate arguments and do nothing */
90b9075fa9SJoe Perches static inline __printf(2, 3)
91dc6b9b78SEric Dumazet void SOCK_DEBUG(const struct sock *sk, const char *msg, ...)
924cd9029dSStephen Hemminger {
934cd9029dSStephen Hemminger }
941da177e4SLinus Torvalds #endif
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* This is the per-socket lock.  The spinlock provides a synchronization
971da177e4SLinus Torvalds  * between user contexts and software interrupt processing, whereas the
981da177e4SLinus Torvalds  * mini-semaphore synchronizes multiple users amongst themselves.
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds typedef struct {
1011da177e4SLinus Torvalds 	spinlock_t		slock;
102d2e9117cSJohn Heffner 	int			owned;
1031da177e4SLinus Torvalds 	wait_queue_head_t	wq;
104a5b5bb9aSIngo Molnar 	/*
105a5b5bb9aSIngo Molnar 	 * We express the mutex-alike socket_lock semantics
106a5b5bb9aSIngo Molnar 	 * to the lock validator by explicitly managing
107a5b5bb9aSIngo Molnar 	 * the slock as a lock variant (in addition to
108a5b5bb9aSIngo Molnar 	 * the slock itself):
109a5b5bb9aSIngo Molnar 	 */
110a5b5bb9aSIngo Molnar #ifdef CONFIG_DEBUG_LOCK_ALLOC
111a5b5bb9aSIngo Molnar 	struct lockdep_map dep_map;
112a5b5bb9aSIngo Molnar #endif
1131da177e4SLinus Torvalds } socket_lock_t;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds struct sock;
1168feaf0c0SArnaldo Carvalho de Melo struct proto;
1170eeb8ffcSDenis V. Lunev struct net;
1181da177e4SLinus Torvalds 
119077b393dSEric Dumazet typedef __u32 __bitwise __portpair;
120077b393dSEric Dumazet typedef __u64 __bitwise __addrpair;
121077b393dSEric Dumazet 
1221da177e4SLinus Torvalds /**
1231da177e4SLinus Torvalds  *	struct sock_common - minimal network layer representation of sockets
12468835abaSEric Dumazet  *	@skc_daddr: Foreign IPv4 addr
12568835abaSEric Dumazet  *	@skc_rcv_saddr: Bound local IPv4 addr
1264dc6dc71SEric Dumazet  *	@skc_hash: hash value used with various protocol lookup tables
127d4cada4aSEric Dumazet  *	@skc_u16hashes: two u16 hash values used by UDP lookup tables
128ce43b03eSEric Dumazet  *	@skc_dport: placeholder for inet_dport/tw_dport
129ce43b03eSEric Dumazet  *	@skc_num: placeholder for inet_num/tw_num
1304dc3b16bSPavel Pisa  *	@skc_family: network address family
1314dc3b16bSPavel Pisa  *	@skc_state: Connection state
1324dc3b16bSPavel Pisa  *	@skc_reuse: %SO_REUSEADDR setting
133055dc21aSTom Herbert  *	@skc_reuseport: %SO_REUSEPORT setting
1344dc3b16bSPavel Pisa  *	@skc_bound_dev_if: bound device index if != 0
1354dc3b16bSPavel Pisa  *	@skc_bind_node: bind hash linkage for various protocol lookup tables
136512615b6SEric Dumazet  *	@skc_portaddr_node: second hash linkage for UDP/UDP-Lite protocol
1378feaf0c0SArnaldo Carvalho de Melo  *	@skc_prot: protocol handlers inside a network family
13807feaebfSEric W. Biederman  *	@skc_net: reference to the network namespace of this socket
13968835abaSEric Dumazet  *	@skc_node: main hash linkage for various protocol lookup tables
14068835abaSEric Dumazet  *	@skc_nulls_node: main hash linkage for TCP/UDP/UDP-Lite protocol
14168835abaSEric Dumazet  *	@skc_tx_queue_mapping: tx queue number for this connection
142c6345ce7SAmritha Nambiar  *	@skc_rx_queue_mapping: rx queue number for this connection
1438e5eb54dSEric Dumazet  *	@skc_flags: place holder for sk_flags
1448e5eb54dSEric Dumazet  *		%SO_LINGER (l_onoff), %SO_BROADCAST, %SO_KEEPALIVE,
1458e5eb54dSEric Dumazet  *		%SO_OOBINLINE settings, %SO_TIMESTAMPING settings
14670da268bSEric Dumazet  *	@skc_incoming_cpu: record/match cpu processing incoming packets
14768835abaSEric Dumazet  *	@skc_refcnt: reference count
1481da177e4SLinus Torvalds  *
1491da177e4SLinus Torvalds  *	This is the minimal network layer representation of sockets, the header
1508feaf0c0SArnaldo Carvalho de Melo  *	for struct sock and struct inet_timewait_sock.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds struct sock_common {
153ce43b03eSEric Dumazet 	/* skc_daddr and skc_rcv_saddr must be grouped on a 8 bytes aligned
15405dbc7b5SEric Dumazet 	 * address on 64bit arches : cf INET_MATCH()
1554dc6dc71SEric Dumazet 	 */
156ce43b03eSEric Dumazet 	union {
157077b393dSEric Dumazet 		__addrpair	skc_addrpair;
158ce43b03eSEric Dumazet 		struct {
15968835abaSEric Dumazet 			__be32	skc_daddr;
16068835abaSEric Dumazet 			__be32	skc_rcv_saddr;
161ce43b03eSEric Dumazet 		};
162ce43b03eSEric Dumazet 	};
163d4cada4aSEric Dumazet 	union  {
16481c3d547SEric Dumazet 		unsigned int	skc_hash;
165d4cada4aSEric Dumazet 		__u16		skc_u16hashes[2];
166d4cada4aSEric Dumazet 	};
167ce43b03eSEric Dumazet 	/* skc_dport && skc_num must be grouped as well */
168ce43b03eSEric Dumazet 	union {
169077b393dSEric Dumazet 		__portpair	skc_portpair;
170ce43b03eSEric Dumazet 		struct {
171ce43b03eSEric Dumazet 			__be16	skc_dport;
172ce43b03eSEric Dumazet 			__u16	skc_num;
173ce43b03eSEric Dumazet 		};
174ce43b03eSEric Dumazet 	};
175ce43b03eSEric Dumazet 
1764dc6dc71SEric Dumazet 	unsigned short		skc_family;
1774dc6dc71SEric Dumazet 	volatile unsigned char	skc_state;
178055dc21aSTom Herbert 	unsigned char		skc_reuse:4;
1799fe516baSEric Dumazet 	unsigned char		skc_reuseport:1;
1809fe516baSEric Dumazet 	unsigned char		skc_ipv6only:1;
18126abe143SEric W. Biederman 	unsigned char		skc_net_refcnt:1;
1824dc6dc71SEric Dumazet 	int			skc_bound_dev_if;
183512615b6SEric Dumazet 	union {
1844dc6dc71SEric Dumazet 		struct hlist_node	skc_bind_node;
185ca065d0cSEric Dumazet 		struct hlist_node	skc_portaddr_node;
186512615b6SEric Dumazet 	};
1878feaf0c0SArnaldo Carvalho de Melo 	struct proto		*skc_prot;
1880c5c9fb5SEric W. Biederman 	possible_net_t		skc_net;
189efe4208fSEric Dumazet 
190efe4208fSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
191efe4208fSEric Dumazet 	struct in6_addr		skc_v6_daddr;
192efe4208fSEric Dumazet 	struct in6_addr		skc_v6_rcv_saddr;
193efe4208fSEric Dumazet #endif
194efe4208fSEric Dumazet 
19533cf7c90SEric Dumazet 	atomic64_t		skc_cookie;
19633cf7c90SEric Dumazet 
1978e5eb54dSEric Dumazet 	/* following fields are padding to force
1988e5eb54dSEric Dumazet 	 * offset(struct sock, sk_refcnt) == 128 on 64bit arches
1998e5eb54dSEric Dumazet 	 * assuming IPV6 is enabled. We use this padding differently
2008e5eb54dSEric Dumazet 	 * for different kind of 'sockets'
2018e5eb54dSEric Dumazet 	 */
2028e5eb54dSEric Dumazet 	union {
2038e5eb54dSEric Dumazet 		unsigned long	skc_flags;
2048e5eb54dSEric Dumazet 		struct sock	*skc_listener; /* request_sock */
2058e5eb54dSEric Dumazet 		struct inet_timewait_death_row *skc_tw_dr; /* inet_timewait_sock */
2068e5eb54dSEric Dumazet 	};
20768835abaSEric Dumazet 	/*
20868835abaSEric Dumazet 	 * fields between dontcopy_begin/dontcopy_end
20968835abaSEric Dumazet 	 * are not copied in sock_copy()
21068835abaSEric Dumazet 	 */
211928c41e7SRandy Dunlap 	/* private: */
21268835abaSEric Dumazet 	int			skc_dontcopy_begin[0];
213928c41e7SRandy Dunlap 	/* public: */
21468835abaSEric Dumazet 	union {
21568835abaSEric Dumazet 		struct hlist_node	skc_node;
21668835abaSEric Dumazet 		struct hlist_nulls_node skc_nulls_node;
21768835abaSEric Dumazet 	};
218755c31cdSAmritha Nambiar 	unsigned short		skc_tx_queue_mapping;
219c6345ce7SAmritha Nambiar #ifdef CONFIG_XPS
220c6345ce7SAmritha Nambiar 	unsigned short		skc_rx_queue_mapping;
221c6345ce7SAmritha Nambiar #endif
222ed53d0abSEric Dumazet 	union {
22370da268bSEric Dumazet 		int		skc_incoming_cpu;
224ed53d0abSEric Dumazet 		u32		skc_rcv_wnd;
225d475f090SEric Dumazet 		u32		skc_tw_rcv_nxt; /* struct tcp_timewait_sock  */
226ed53d0abSEric Dumazet 	};
22770da268bSEric Dumazet 
22841c6d650SReshetova, Elena 	refcount_t		skc_refcnt;
229928c41e7SRandy Dunlap 	/* private: */
23068835abaSEric Dumazet 	int                     skc_dontcopy_end[0];
231ed53d0abSEric Dumazet 	union {
232ed53d0abSEric Dumazet 		u32		skc_rxhash;
233ed53d0abSEric Dumazet 		u32		skc_window_clamp;
234d475f090SEric Dumazet 		u32		skc_tw_snd_nxt; /* struct tcp_timewait_sock */
235ed53d0abSEric Dumazet 	};
236928c41e7SRandy Dunlap 	/* public: */
2371da177e4SLinus Torvalds };
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2401da177e4SLinus Torvalds   *	struct sock - network layer representation of sockets
2418feaf0c0SArnaldo Carvalho de Melo   *	@__sk_common: shared layout with inet_timewait_sock
2424dc3b16bSPavel Pisa   *	@sk_shutdown: mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN
2434dc3b16bSPavel Pisa   *	@sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings
2444dc3b16bSPavel Pisa   *	@sk_lock:	synchronizer
245cdfbabfbSDavid Howells   *	@sk_kern_sock: True if sock is using kernel lock classes
2464dc3b16bSPavel Pisa   *	@sk_rcvbuf: size of receive buffer in bytes
24743815482SEric Dumazet   *	@sk_wq: sock wait queue and async head
248421b3885SShawn Bohrer   *	@sk_rx_dst: receive input route used by early demux
2494dc3b16bSPavel Pisa   *	@sk_dst_cache: destination cache
2509b8805a3SJulian Anastasov   *	@sk_dst_pending_confirm: need to confirm neighbour
2514dc3b16bSPavel Pisa   *	@sk_policy: flow policy
2524dc3b16bSPavel Pisa   *	@sk_receive_queue: incoming packets
2534dc3b16bSPavel Pisa   *	@sk_wmem_alloc: transmit queue bytes committed
254771edcafSstephen hemminger   *	@sk_tsq_flags: TCP Small Queues flags
2554dc3b16bSPavel Pisa   *	@sk_write_queue: Packet sending queue
2564dc3b16bSPavel Pisa   *	@sk_omem_alloc: "o" is "option" or "other"
2574dc3b16bSPavel Pisa   *	@sk_wmem_queued: persistent queue size
2584dc3b16bSPavel Pisa   *	@sk_forward_alloc: space allocated forward
25906021292SEliezer Tamir   *	@sk_napi_id: id of the last napi context to receive data for sk
260dafcc438SEliezer Tamir   *	@sk_ll_usec: usecs to busypoll when there is no data
2614dc3b16bSPavel Pisa   *	@sk_allocation: allocation mode
26295bd09ebSEric Dumazet   *	@sk_pacing_rate: Pacing rate (if supported by transport/packet scheduler)
263218af599SEric Dumazet   *	@sk_pacing_status: Pacing status (requested, handled by sch_fq)
264c3f40d7cSEric Dumazet   *	@sk_max_pacing_rate: Maximum pacing rate (%SO_MAX_PACING_RATE)
2654dc3b16bSPavel Pisa   *	@sk_sndbuf: size of send buffer in bytes
266771edcafSstephen hemminger   *	@__sk_flags_offset: empty field used to determine location of bitfield
267293de7deSStephen Hemminger   *	@sk_padding: unused element for alignment
26828448b80STom Herbert   *	@sk_no_check_tx: %SO_NO_CHECK setting, set checksum in TX packets
26928448b80STom Herbert   *	@sk_no_check_rx: allow zero checksum in RX packets
2704dc3b16bSPavel Pisa   *	@sk_route_caps: route capabilities (e.g. %NETIF_F_TSO)
271a465419bSEric Dumazet   *	@sk_route_nocaps: forbidden route capabilities (e.g NETIF_F_GSO_MASK)
272bcd76111SHerbert Xu   *	@sk_gso_type: GSO type (e.g. %SKB_GSO_TCPV4)
27382cc1a7aSPeter P Waskiewicz Jr   *	@sk_gso_max_size: Maximum GSO segment size to build
2741485348dSBen Hutchings   *	@sk_gso_max_segs: Maximum number of GSO segments
2753a9b76fdSEric Dumazet   *	@sk_pacing_shift: scaling factor for TCP Small Queues
2764dc3b16bSPavel Pisa   *	@sk_lingertime: %SO_LINGER l_linger setting
2774dc3b16bSPavel Pisa   *	@sk_backlog: always used with the per-socket spinlock held
2784dc3b16bSPavel Pisa   *	@sk_callback_lock: used with the callbacks in the end of this struct
2794dc3b16bSPavel Pisa   *	@sk_error_queue: rarely used
28033c732c3SWang Chen   *	@sk_prot_creator: sk_prot of original sock creator (see ipv6_setsockopt,
28133c732c3SWang Chen   *			  IPV6_ADDRFORM for instance)
2824dc3b16bSPavel Pisa   *	@sk_err: last error
28333c732c3SWang Chen   *	@sk_err_soft: errors that don't cause failure but are the cause of a
28433c732c3SWang Chen   *		      persistent failure not just 'timed out'
285cb61cb9bSEric Dumazet   *	@sk_drops: raw/udp drops counter
2864dc3b16bSPavel Pisa   *	@sk_ack_backlog: current listen backlog
2874dc3b16bSPavel Pisa   *	@sk_max_ack_backlog: listen backlog set in listen()
288771edcafSstephen hemminger   *	@sk_uid: user id of owner
2894dc3b16bSPavel Pisa   *	@sk_priority: %SO_PRIORITY setting
2904dc3b16bSPavel Pisa   *	@sk_type: socket type (%SOCK_STREAM, etc)
2914dc3b16bSPavel Pisa   *	@sk_protocol: which protocol this socket belongs in this network family
29253c3fa20SRandy Dunlap   *	@sk_peer_pid: &struct pid for this socket's peer
29353c3fa20SRandy Dunlap   *	@sk_peer_cred: %SO_PEERCRED setting
2944dc3b16bSPavel Pisa   *	@sk_rcvlowat: %SO_RCVLOWAT setting
2954dc3b16bSPavel Pisa   *	@sk_rcvtimeo: %SO_RCVTIMEO setting
2964dc3b16bSPavel Pisa   *	@sk_sndtimeo: %SO_SNDTIMEO setting
297b73c3d0eSTom Herbert   *	@sk_txhash: computed flow hash for use on transmit
2984dc3b16bSPavel Pisa   *	@sk_filter: socket filtering instructions
2994dc3b16bSPavel Pisa   *	@sk_timer: sock cleanup timer
3004dc3b16bSPavel Pisa   *	@sk_stamp: time stamp of last packet received
3013a0ed3e9SDeepa Dinamani   *	@sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only
302b9f40e21SWillem de Bruijn   *	@sk_tsflags: SO_TIMESTAMPING socket options
30309c2d251SWillem de Bruijn   *	@sk_tskey: counter to disambiguate concurrent tstamp requests
30452267790SWillem de Bruijn   *	@sk_zckey: counter to order MSG_ZEROCOPY notifications
3054dc3b16bSPavel Pisa   *	@sk_socket: Identd and reporting IO signals
3064dc3b16bSPavel Pisa   *	@sk_user_data: RPC layer private data
3075640f768SEric Dumazet   *	@sk_frag: cached page frag
308d3d4f0a0SRandy Dunlap   *	@sk_peek_off: current peek_offset value
3094dc3b16bSPavel Pisa   *	@sk_send_head: front of stuff to transmit
31067be2dd1SMartin Waitz   *	@sk_security: used by security modules
31131729363SRandy Dunlap   *	@sk_mark: generic packet mark
3122a56a1feSTejun Heo   *	@sk_cgrp_data: cgroup data for this cgroup
313baac50bbSJohannes Weiner   *	@sk_memcg: this socket's memory cgroup association
3144dc3b16bSPavel Pisa   *	@sk_write_pending: a write to stream socket waits to start
3154dc3b16bSPavel Pisa   *	@sk_state_change: callback to indicate change in the state of the sock
3164dc3b16bSPavel Pisa   *	@sk_data_ready: callback to indicate there is data to be processed
3174dc3b16bSPavel Pisa   *	@sk_write_space: callback to indicate there is bf sending space available
3184dc3b16bSPavel Pisa   *	@sk_error_report: callback to indicate errors (e.g. %MSG_ERRQUEUE)
3194dc3b16bSPavel Pisa   *	@sk_backlog_rcv: callback to process the backlog
3204dc3b16bSPavel Pisa   *	@sk_destruct: called at sock freeing time, i.e. when all refcnt == 0
321ef456144SCraig Gallek   *	@sk_reuseport_cb: reuseport group container
322293de7deSStephen Hemminger   *	@sk_rcu: used during RCU grace period
32380b14deeSRichard Cochran   *	@sk_clockid: clockid used by time-based scheduling (SO_TXTIME)
32480b14deeSRichard Cochran   *	@sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
32580b14deeSRichard Cochran   *	@sk_txtime_unused: unused txtime flags
3261da177e4SLinus Torvalds   */
3271da177e4SLinus Torvalds struct sock {
3281da177e4SLinus Torvalds 	/*
3298feaf0c0SArnaldo Carvalho de Melo 	 * Now struct inet_timewait_sock also uses sock_common, so please just
3301da177e4SLinus Torvalds 	 * don't add nothing before this first member (__sk_common) --acme
3311da177e4SLinus Torvalds 	 */
3321da177e4SLinus Torvalds 	struct sock_common	__sk_common;
3334dc6dc71SEric Dumazet #define sk_node			__sk_common.skc_node
3344dc6dc71SEric Dumazet #define sk_nulls_node		__sk_common.skc_nulls_node
3354dc6dc71SEric Dumazet #define sk_refcnt		__sk_common.skc_refcnt
336e022f0b4SKrishna Kumar #define sk_tx_queue_mapping	__sk_common.skc_tx_queue_mapping
337c6345ce7SAmritha Nambiar #ifdef CONFIG_XPS
338c6345ce7SAmritha Nambiar #define sk_rx_queue_mapping	__sk_common.skc_rx_queue_mapping
339c6345ce7SAmritha Nambiar #endif
3404dc6dc71SEric Dumazet 
34168835abaSEric Dumazet #define sk_dontcopy_begin	__sk_common.skc_dontcopy_begin
34268835abaSEric Dumazet #define sk_dontcopy_end		__sk_common.skc_dontcopy_end
3434dc6dc71SEric Dumazet #define sk_hash			__sk_common.skc_hash
34450805466SEric Dumazet #define sk_portpair		__sk_common.skc_portpair
34505dbc7b5SEric Dumazet #define sk_num			__sk_common.skc_num
34605dbc7b5SEric Dumazet #define sk_dport		__sk_common.skc_dport
34750805466SEric Dumazet #define sk_addrpair		__sk_common.skc_addrpair
34850805466SEric Dumazet #define sk_daddr		__sk_common.skc_daddr
34950805466SEric Dumazet #define sk_rcv_saddr		__sk_common.skc_rcv_saddr
3501da177e4SLinus Torvalds #define sk_family		__sk_common.skc_family
3511da177e4SLinus Torvalds #define sk_state		__sk_common.skc_state
3521da177e4SLinus Torvalds #define sk_reuse		__sk_common.skc_reuse
353055dc21aSTom Herbert #define sk_reuseport		__sk_common.skc_reuseport
3549fe516baSEric Dumazet #define sk_ipv6only		__sk_common.skc_ipv6only
35526abe143SEric W. Biederman #define sk_net_refcnt		__sk_common.skc_net_refcnt
3561da177e4SLinus Torvalds #define sk_bound_dev_if		__sk_common.skc_bound_dev_if
3571da177e4SLinus Torvalds #define sk_bind_node		__sk_common.skc_bind_node
3588feaf0c0SArnaldo Carvalho de Melo #define sk_prot			__sk_common.skc_prot
35907feaebfSEric W. Biederman #define sk_net			__sk_common.skc_net
360efe4208fSEric Dumazet #define sk_v6_daddr		__sk_common.skc_v6_daddr
361efe4208fSEric Dumazet #define sk_v6_rcv_saddr	__sk_common.skc_v6_rcv_saddr
36233cf7c90SEric Dumazet #define sk_cookie		__sk_common.skc_cookie
36370da268bSEric Dumazet #define sk_incoming_cpu		__sk_common.skc_incoming_cpu
3648e5eb54dSEric Dumazet #define sk_flags		__sk_common.skc_flags
365ed53d0abSEric Dumazet #define sk_rxhash		__sk_common.skc_rxhash
366efe4208fSEric Dumazet 
367b178bb3dSEric Dumazet 	socket_lock_t		sk_lock;
3689115e8cdSEric Dumazet 	atomic_t		sk_drops;
3699115e8cdSEric Dumazet 	int			sk_rcvlowat;
3709115e8cdSEric Dumazet 	struct sk_buff_head	sk_error_queue;
3718b27dae5SEric Dumazet 	struct sk_buff		*sk_rx_skb_cache;
372b178bb3dSEric Dumazet 	struct sk_buff_head	sk_receive_queue;
373b178bb3dSEric Dumazet 	/*
374b178bb3dSEric Dumazet 	 * The backlog queue is special, it is always used with
375b178bb3dSEric Dumazet 	 * the per-socket spinlock held and requires low latency
376b178bb3dSEric Dumazet 	 * access. Therefore we special case it's implementation.
377b178bb3dSEric Dumazet 	 * Note : rmem_alloc is in this structure to fill a hole
378b178bb3dSEric Dumazet 	 * on 64bit arches, not because its logically part of
379b178bb3dSEric Dumazet 	 * backlog.
380b178bb3dSEric Dumazet 	 */
381b178bb3dSEric Dumazet 	struct {
382b178bb3dSEric Dumazet 		atomic_t	rmem_alloc;
383b178bb3dSEric Dumazet 		int		len;
384b178bb3dSEric Dumazet 		struct sk_buff	*head;
385b178bb3dSEric Dumazet 		struct sk_buff	*tail;
386b178bb3dSEric Dumazet 	} sk_backlog;
387b178bb3dSEric Dumazet #define sk_rmem_alloc sk_backlog.rmem_alloc
3882c8c56e1SEric Dumazet 
3899115e8cdSEric Dumazet 	int			sk_forward_alloc;
390e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
391dafcc438SEliezer Tamir 	unsigned int		sk_ll_usec;
3929115e8cdSEric Dumazet 	/* ===== mostly read cache line ===== */
3939115e8cdSEric Dumazet 	unsigned int		sk_napi_id;
39406021292SEliezer Tamir #endif
395b178bb3dSEric Dumazet 	int			sk_rcvbuf;
396b178bb3dSEric Dumazet 
397b178bb3dSEric Dumazet 	struct sk_filter __rcu	*sk_filter;
398ceb5d58bSEric Dumazet 	union {
399eaefd110SEric Dumazet 		struct socket_wq __rcu	*sk_wq;
400ceb5d58bSEric Dumazet 		struct socket_wq	*sk_wq_raw;
401ceb5d58bSEric Dumazet 	};
402b178bb3dSEric Dumazet #ifdef CONFIG_XFRM
403d188ba86SEric Dumazet 	struct xfrm_policy __rcu *sk_policy[2];
404b178bb3dSEric Dumazet #endif
405deaa5854SEric Dumazet 	struct dst_entry	*sk_rx_dst;
4060e36cbb3SCong Wang 	struct dst_entry __rcu	*sk_dst_cache;
407b178bb3dSEric Dumazet 	atomic_t		sk_omem_alloc;
408b178bb3dSEric Dumazet 	int			sk_sndbuf;
4099115e8cdSEric Dumazet 
4109115e8cdSEric Dumazet 	/* ===== cache line for TX ===== */
4119115e8cdSEric Dumazet 	int			sk_wmem_queued;
41214afee4bSReshetova, Elena 	refcount_t		sk_wmem_alloc;
4139115e8cdSEric Dumazet 	unsigned long		sk_tsq_flags;
41475c119afSEric Dumazet 	union {
4159115e8cdSEric Dumazet 		struct sk_buff	*sk_send_head;
41675c119afSEric Dumazet 		struct rb_root	tcp_rtx_queue;
41775c119afSEric Dumazet 	};
418472c2e07SEric Dumazet 	struct sk_buff		*sk_tx_skb_cache;
419b178bb3dSEric Dumazet 	struct sk_buff_head	sk_write_queue;
4209115e8cdSEric Dumazet 	__s32			sk_peek_off;
4219115e8cdSEric Dumazet 	int			sk_write_pending;
4229b8805a3SJulian Anastasov 	__u32			sk_dst_pending_confirm;
423218af599SEric Dumazet 	u32			sk_pacing_status; /* see enum sk_pacing */
4249115e8cdSEric Dumazet 	long			sk_sndtimeo;
4259115e8cdSEric Dumazet 	struct timer_list	sk_timer;
4269115e8cdSEric Dumazet 	__u32			sk_priority;
4279115e8cdSEric Dumazet 	__u32			sk_mark;
42876a9ebe8SEric Dumazet 	unsigned long		sk_pacing_rate; /* bytes per second */
42976a9ebe8SEric Dumazet 	unsigned long		sk_max_pacing_rate;
4309115e8cdSEric Dumazet 	struct page_frag	sk_frag;
4319115e8cdSEric Dumazet 	netdev_features_t	sk_route_caps;
4329115e8cdSEric Dumazet 	netdev_features_t	sk_route_nocaps;
4330a6b2a1dSEric Dumazet 	netdev_features_t	sk_route_forced_caps;
4349115e8cdSEric Dumazet 	int			sk_gso_type;
4359115e8cdSEric Dumazet 	unsigned int		sk_gso_max_size;
4369115e8cdSEric Dumazet 	gfp_t			sk_allocation;
4379115e8cdSEric Dumazet 	__u32			sk_txhash;
438fc64869cSAndrey Ryabinin 
439fc64869cSAndrey Ryabinin 	/*
440fc64869cSAndrey Ryabinin 	 * Because of non atomicity rules, all
441fc64869cSAndrey Ryabinin 	 * changes are protected by socket lock.
442fc64869cSAndrey Ryabinin 	 */
443aa4c1037SDavid Ahern 	unsigned int		__sk_flags_offset[0];
444aa4c1037SDavid Ahern #ifdef __BIG_ENDIAN_BITFIELD
445aa4c1037SDavid Ahern #define SK_FL_PROTO_SHIFT  16
446aa4c1037SDavid Ahern #define SK_FL_PROTO_MASK   0x00ff0000
447aa4c1037SDavid Ahern 
448aa4c1037SDavid Ahern #define SK_FL_TYPE_SHIFT   0
449aa4c1037SDavid Ahern #define SK_FL_TYPE_MASK    0x0000ffff
450aa4c1037SDavid Ahern #else
451aa4c1037SDavid Ahern #define SK_FL_PROTO_SHIFT  8
452aa4c1037SDavid Ahern #define SK_FL_PROTO_MASK   0x0000ff00
453aa4c1037SDavid Ahern 
454aa4c1037SDavid Ahern #define SK_FL_TYPE_SHIFT   16
455aa4c1037SDavid Ahern #define SK_FL_TYPE_MASK    0xffff0000
456aa4c1037SDavid Ahern #endif
457aa4c1037SDavid Ahern 
458cdfbabfbSDavid Howells 	unsigned int		sk_padding : 1,
459cdfbabfbSDavid Howells 				sk_kern_sock : 1,
46028448b80STom Herbert 				sk_no_check_tx : 1,
46128448b80STom Herbert 				sk_no_check_rx : 1,
4625fdb9973SEric Dumazet 				sk_userlocks : 4,
4635fdb9973SEric Dumazet 				sk_protocol  : 8,
4645fdb9973SEric Dumazet 				sk_type      : 16;
4657bbadd2dSHannes Frederic Sowa #define SK_PROTOCOL_MAX U8_MAX
4661485348dSBen Hutchings 	u16			sk_gso_max_segs;
4673a9b76fdSEric Dumazet 	u8			sk_pacing_shift;
4681da177e4SLinus Torvalds 	unsigned long	        sk_lingertime;
469476e19cfSArnaldo Carvalho de Melo 	struct proto		*sk_prot_creator;
4701da177e4SLinus Torvalds 	rwlock_t		sk_callback_lock;
4711da177e4SLinus Torvalds 	int			sk_err,
4721da177e4SLinus Torvalds 				sk_err_soft;
473becb74f0SEric Dumazet 	u32			sk_ack_backlog;
474becb74f0SEric Dumazet 	u32			sk_max_ack_backlog;
47586741ec2SLorenzo Colitti 	kuid_t			sk_uid;
476109f6e39SEric W. Biederman 	struct pid		*sk_peer_pid;
477109f6e39SEric W. Biederman 	const struct cred	*sk_peer_cred;
4781da177e4SLinus Torvalds 	long			sk_rcvtimeo;
479b7aa0bf7SEric Dumazet 	ktime_t			sk_stamp;
4803a0ed3e9SDeepa Dinamani #if BITS_PER_LONG==32
4813a0ed3e9SDeepa Dinamani 	seqlock_t		sk_stamp_seq;
4823a0ed3e9SDeepa Dinamani #endif
483b9f40e21SWillem de Bruijn 	u16			sk_tsflags;
484fc64869cSAndrey Ryabinin 	u8			sk_shutdown;
48509c2d251SWillem de Bruijn 	u32			sk_tskey;
48652267790SWillem de Bruijn 	atomic_t		sk_zckey;
48780b14deeSRichard Cochran 
48880b14deeSRichard Cochran 	u8			sk_clockid;
48980b14deeSRichard Cochran 	u8			sk_txtime_deadline_mode : 1,
4904b15c707SJesus Sanchez-Palencia 				sk_txtime_report_errors : 1,
4914b15c707SJesus Sanchez-Palencia 				sk_txtime_unused : 6;
49280b14deeSRichard Cochran 
4931da177e4SLinus Torvalds 	struct socket		*sk_socket;
4941da177e4SLinus Torvalds 	void			*sk_user_data;
495d5f64238SAlexey Dobriyan #ifdef CONFIG_SECURITY
4961da177e4SLinus Torvalds 	void			*sk_security;
497d5f64238SAlexey Dobriyan #endif
4982a56a1feSTejun Heo 	struct sock_cgroup_data	sk_cgrp_data;
499baac50bbSJohannes Weiner 	struct mem_cgroup	*sk_memcg;
5001da177e4SLinus Torvalds 	void			(*sk_state_change)(struct sock *sk);
501676d2369SDavid S. Miller 	void			(*sk_data_ready)(struct sock *sk);
5021da177e4SLinus Torvalds 	void			(*sk_write_space)(struct sock *sk);
5031da177e4SLinus Torvalds 	void			(*sk_error_report)(struct sock *sk);
5041da177e4SLinus Torvalds 	int			(*sk_backlog_rcv)(struct sock *sk,
5051da177e4SLinus Torvalds 						  struct sk_buff *skb);
506ebf4e808SIlya Lesokhin #ifdef CONFIG_SOCK_VALIDATE_XMIT
507ebf4e808SIlya Lesokhin 	struct sk_buff*		(*sk_validate_xmit_skb)(struct sock *sk,
508ebf4e808SIlya Lesokhin 							struct net_device *dev,
509ebf4e808SIlya Lesokhin 							struct sk_buff *skb);
510ebf4e808SIlya Lesokhin #endif
5111da177e4SLinus Torvalds 	void                    (*sk_destruct)(struct sock *sk);
512ef456144SCraig Gallek 	struct sock_reuseport __rcu	*sk_reuseport_cb;
513a4298e45SEric Dumazet 	struct rcu_head		sk_rcu;
5141da177e4SLinus Torvalds };
5151da177e4SLinus Torvalds 
516218af599SEric Dumazet enum sk_pacing {
517218af599SEric Dumazet 	SK_PACING_NONE		= 0,
518218af599SEric Dumazet 	SK_PACING_NEEDED	= 1,
519218af599SEric Dumazet 	SK_PACING_FQ		= 2,
520218af599SEric Dumazet };
521218af599SEric Dumazet 
522559835eaSPravin B Shelar #define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data)))
523559835eaSPravin B Shelar 
524559835eaSPravin B Shelar #define rcu_dereference_sk_user_data(sk)	rcu_dereference(__sk_user_data((sk)))
525559835eaSPravin B Shelar #define rcu_assign_sk_user_data(sk, ptr)	rcu_assign_pointer(__sk_user_data((sk)), ptr)
526559835eaSPravin B Shelar 
5274a17fd52SPavel Emelyanov /*
5284a17fd52SPavel Emelyanov  * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
5294a17fd52SPavel Emelyanov  * or not whether his port will be reused by someone else. SK_FORCE_REUSE
5304a17fd52SPavel Emelyanov  * on a socket means that the socket will reuse everybody else's port
5314a17fd52SPavel Emelyanov  * without looking at the other's sk_reuse value.
5324a17fd52SPavel Emelyanov  */
5334a17fd52SPavel Emelyanov 
5344a17fd52SPavel Emelyanov #define SK_NO_REUSE	0
5354a17fd52SPavel Emelyanov #define SK_CAN_REUSE	1
5364a17fd52SPavel Emelyanov #define SK_FORCE_REUSE	2
5374a17fd52SPavel Emelyanov 
538627d2d6bSsamanthakumar int sk_set_peek_off(struct sock *sk, int val);
539627d2d6bSsamanthakumar 
540ef64a54fSPavel Emelyanov static inline int sk_peek_offset(struct sock *sk, int flags)
541ef64a54fSPavel Emelyanov {
542b9bb53f3SWillem de Bruijn 	if (unlikely(flags & MSG_PEEK)) {
543a0917e0bSMatthew Dawson 		return READ_ONCE(sk->sk_peek_off);
544b9bb53f3SWillem de Bruijn 	}
545b9bb53f3SWillem de Bruijn 
546ef64a54fSPavel Emelyanov 	return 0;
547ef64a54fSPavel Emelyanov }
548ef64a54fSPavel Emelyanov 
549ef64a54fSPavel Emelyanov static inline void sk_peek_offset_bwd(struct sock *sk, int val)
550ef64a54fSPavel Emelyanov {
551b9bb53f3SWillem de Bruijn 	s32 off = READ_ONCE(sk->sk_peek_off);
552b9bb53f3SWillem de Bruijn 
553b9bb53f3SWillem de Bruijn 	if (unlikely(off >= 0)) {
554b9bb53f3SWillem de Bruijn 		off = max_t(s32, off - val, 0);
555b9bb53f3SWillem de Bruijn 		WRITE_ONCE(sk->sk_peek_off, off);
556ef64a54fSPavel Emelyanov 	}
557ef64a54fSPavel Emelyanov }
558ef64a54fSPavel Emelyanov 
559ef64a54fSPavel Emelyanov static inline void sk_peek_offset_fwd(struct sock *sk, int val)
560ef64a54fSPavel Emelyanov {
561b9bb53f3SWillem de Bruijn 	sk_peek_offset_bwd(sk, -val);
562ef64a54fSPavel Emelyanov }
563ef64a54fSPavel Emelyanov 
5641da177e4SLinus Torvalds /*
5651da177e4SLinus Torvalds  * Hashed lists helper routines
5661da177e4SLinus Torvalds  */
567c4146644SLi Zefan static inline struct sock *sk_entry(const struct hlist_node *node)
568c4146644SLi Zefan {
569c4146644SLi Zefan 	return hlist_entry(node, struct sock, sk_node);
570c4146644SLi Zefan }
571c4146644SLi Zefan 
572e48c414eSArnaldo Carvalho de Melo static inline struct sock *__sk_head(const struct hlist_head *head)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	return hlist_entry(head->first, struct sock, sk_node);
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
577e48c414eSArnaldo Carvalho de Melo static inline struct sock *sk_head(const struct hlist_head *head)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	return hlist_empty(head) ? NULL : __sk_head(head);
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
58288ab1932SEric Dumazet static inline struct sock *__sk_nulls_head(const struct hlist_nulls_head *head)
58388ab1932SEric Dumazet {
58488ab1932SEric Dumazet 	return hlist_nulls_entry(head->first, struct sock, sk_nulls_node);
58588ab1932SEric Dumazet }
58688ab1932SEric Dumazet 
58788ab1932SEric Dumazet static inline struct sock *sk_nulls_head(const struct hlist_nulls_head *head)
58888ab1932SEric Dumazet {
58988ab1932SEric Dumazet 	return hlist_nulls_empty(head) ? NULL : __sk_nulls_head(head);
59088ab1932SEric Dumazet }
59188ab1932SEric Dumazet 
592e48c414eSArnaldo Carvalho de Melo static inline struct sock *sk_next(const struct sock *sk)
5931da177e4SLinus Torvalds {
5946c59ebd3SGeliang Tang 	return hlist_entry_safe(sk->sk_node.next, struct sock, sk_node);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
59788ab1932SEric Dumazet static inline struct sock *sk_nulls_next(const struct sock *sk)
59888ab1932SEric Dumazet {
59988ab1932SEric Dumazet 	return (!is_a_nulls(sk->sk_nulls_node.next)) ?
60088ab1932SEric Dumazet 		hlist_nulls_entry(sk->sk_nulls_node.next,
60188ab1932SEric Dumazet 				  struct sock, sk_nulls_node) :
60288ab1932SEric Dumazet 		NULL;
60388ab1932SEric Dumazet }
60488ab1932SEric Dumazet 
605dc6b9b78SEric Dumazet static inline bool sk_unhashed(const struct sock *sk)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	return hlist_unhashed(&sk->sk_node);
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610dc6b9b78SEric Dumazet static inline bool sk_hashed(const struct sock *sk)
6111da177e4SLinus Torvalds {
612da753beaSAkinobu Mita 	return !sk_unhashed(sk);
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
615dc6b9b78SEric Dumazet static inline void sk_node_init(struct hlist_node *node)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	node->pprev = NULL;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
620dc6b9b78SEric Dumazet static inline void sk_nulls_node_init(struct hlist_nulls_node *node)
62188ab1932SEric Dumazet {
62288ab1932SEric Dumazet 	node->pprev = NULL;
62388ab1932SEric Dumazet }
62488ab1932SEric Dumazet 
625dc6b9b78SEric Dumazet static inline void __sk_del_node(struct sock *sk)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	__hlist_del(&sk->sk_node);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
630808f5114Sstephen hemminger /* NB: equivalent to hlist_del_init_rcu */
631dc6b9b78SEric Dumazet static inline bool __sk_del_node_init(struct sock *sk)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	if (sk_hashed(sk)) {
6341da177e4SLinus Torvalds 		__sk_del_node(sk);
6351da177e4SLinus Torvalds 		sk_node_init(&sk->sk_node);
636dc6b9b78SEric Dumazet 		return true;
6371da177e4SLinus Torvalds 	}
638dc6b9b78SEric Dumazet 	return false;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds /* Grab socket reference count. This operation is valid only
6421da177e4SLinus Torvalds    when sk is ALREADY grabbed f.e. it is found in hash table
6431da177e4SLinus Torvalds    or a list and the lookup is made under lock preventing hash table
6441da177e4SLinus Torvalds    modifications.
6451da177e4SLinus Torvalds  */
6461da177e4SLinus Torvalds 
647f9a7cbbfSDenys Vlasenko static __always_inline void sock_hold(struct sock *sk)
6481da177e4SLinus Torvalds {
64941c6d650SReshetova, Elena 	refcount_inc(&sk->sk_refcnt);
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds /* Ungrab socket in the context, which assumes that socket refcnt
6531da177e4SLinus Torvalds    cannot hit zero, f.e. it is true in context of any socketcall.
6541da177e4SLinus Torvalds  */
655f9a7cbbfSDenys Vlasenko static __always_inline void __sock_put(struct sock *sk)
6561da177e4SLinus Torvalds {
65741c6d650SReshetova, Elena 	refcount_dec(&sk->sk_refcnt);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
660dc6b9b78SEric Dumazet static inline bool sk_del_node_init(struct sock *sk)
6611da177e4SLinus Torvalds {
662dc6b9b78SEric Dumazet 	bool rc = __sk_del_node_init(sk);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	if (rc) {
6651da177e4SLinus Torvalds 		/* paranoid for a while -acme */
66641c6d650SReshetova, Elena 		WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
6671da177e4SLinus Torvalds 		__sock_put(sk);
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 	return rc;
6701da177e4SLinus Torvalds }
671808f5114Sstephen hemminger #define sk_del_node_init_rcu(sk)	sk_del_node_init(sk)
6721da177e4SLinus Torvalds 
673dc6b9b78SEric Dumazet static inline bool __sk_nulls_del_node_init_rcu(struct sock *sk)
674271b72c7SEric Dumazet {
675271b72c7SEric Dumazet 	if (sk_hashed(sk)) {
67688ab1932SEric Dumazet 		hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
677dc6b9b78SEric Dumazet 		return true;
678271b72c7SEric Dumazet 	}
679dc6b9b78SEric Dumazet 	return false;
680271b72c7SEric Dumazet }
681271b72c7SEric Dumazet 
682dc6b9b78SEric Dumazet static inline bool sk_nulls_del_node_init_rcu(struct sock *sk)
683271b72c7SEric Dumazet {
684dc6b9b78SEric Dumazet 	bool rc = __sk_nulls_del_node_init_rcu(sk);
685271b72c7SEric Dumazet 
686271b72c7SEric Dumazet 	if (rc) {
687271b72c7SEric Dumazet 		/* paranoid for a while -acme */
68841c6d650SReshetova, Elena 		WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
689271b72c7SEric Dumazet 		__sock_put(sk);
690271b72c7SEric Dumazet 	}
691271b72c7SEric Dumazet 	return rc;
692271b72c7SEric Dumazet }
693271b72c7SEric Dumazet 
694dc6b9b78SEric Dumazet static inline void __sk_add_node(struct sock *sk, struct hlist_head *list)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	hlist_add_head(&sk->sk_node, list);
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
699dc6b9b78SEric Dumazet static inline void sk_add_node(struct sock *sk, struct hlist_head *list)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	sock_hold(sk);
7021da177e4SLinus Torvalds 	__sk_add_node(sk, list);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705dc6b9b78SEric Dumazet static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
706808f5114Sstephen hemminger {
707808f5114Sstephen hemminger 	sock_hold(sk);
708d296ba60SCraig Gallek 	if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
709d296ba60SCraig Gallek 	    sk->sk_family == AF_INET6)
710d296ba60SCraig Gallek 		hlist_add_tail_rcu(&sk->sk_node, list);
711d296ba60SCraig Gallek 	else
712808f5114Sstephen hemminger 		hlist_add_head_rcu(&sk->sk_node, list);
713808f5114Sstephen hemminger }
714808f5114Sstephen hemminger 
715a4dc6a49SMaxime Chevallier static inline void sk_add_node_tail_rcu(struct sock *sk, struct hlist_head *list)
716a4dc6a49SMaxime Chevallier {
717a4dc6a49SMaxime Chevallier 	sock_hold(sk);
718a4dc6a49SMaxime Chevallier 	hlist_add_tail_rcu(&sk->sk_node, list);
719a4dc6a49SMaxime Chevallier }
720a4dc6a49SMaxime Chevallier 
721dc6b9b78SEric Dumazet static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
722271b72c7SEric Dumazet {
72388ab1932SEric Dumazet 	hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
724271b72c7SEric Dumazet }
725271b72c7SEric Dumazet 
726dc6b9b78SEric Dumazet static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
727271b72c7SEric Dumazet {
728271b72c7SEric Dumazet 	sock_hold(sk);
72988ab1932SEric Dumazet 	__sk_nulls_add_node_rcu(sk, list);
730271b72c7SEric Dumazet }
731271b72c7SEric Dumazet 
732dc6b9b78SEric Dumazet static inline void __sk_del_bind_node(struct sock *sk)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	__hlist_del(&sk->sk_bind_node);
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
737dc6b9b78SEric Dumazet static inline void sk_add_bind_node(struct sock *sk,
7381da177e4SLinus Torvalds 					struct hlist_head *list)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	hlist_add_head(&sk->sk_bind_node, list);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
743b67bfe0dSSasha Levin #define sk_for_each(__sk, list) \
744b67bfe0dSSasha Levin 	hlist_for_each_entry(__sk, list, sk_node)
745b67bfe0dSSasha Levin #define sk_for_each_rcu(__sk, list) \
746b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(__sk, list, sk_node)
74788ab1932SEric Dumazet #define sk_nulls_for_each(__sk, node, list) \
74888ab1932SEric Dumazet 	hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
74988ab1932SEric Dumazet #define sk_nulls_for_each_rcu(__sk, node, list) \
75088ab1932SEric Dumazet 	hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node)
751b67bfe0dSSasha Levin #define sk_for_each_from(__sk) \
752b67bfe0dSSasha Levin 	hlist_for_each_entry_from(__sk, sk_node)
75388ab1932SEric Dumazet #define sk_nulls_for_each_from(__sk, node) \
75488ab1932SEric Dumazet 	if (__sk && ({ node = &(__sk)->sk_nulls_node; 1; })) \
75588ab1932SEric Dumazet 		hlist_nulls_for_each_entry_from(__sk, node, sk_nulls_node)
756b67bfe0dSSasha Levin #define sk_for_each_safe(__sk, tmp, list) \
757b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(__sk, tmp, list, sk_node)
758b67bfe0dSSasha Levin #define sk_for_each_bound(__sk, list) \
759b67bfe0dSSasha Levin 	hlist_for_each_entry(__sk, list, sk_bind_node)
7601da177e4SLinus Torvalds 
7612dc41cffSDavid Held /**
762ca065d0cSEric Dumazet  * sk_for_each_entry_offset_rcu - iterate over a list at a given struct offset
7632dc41cffSDavid Held  * @tpos:	the type * to use as a loop cursor.
7642dc41cffSDavid Held  * @pos:	the &struct hlist_node to use as a loop cursor.
7652dc41cffSDavid Held  * @head:	the head for your list.
7662dc41cffSDavid Held  * @offset:	offset of hlist_node within the struct.
7672dc41cffSDavid Held  *
7682dc41cffSDavid Held  */
769ca065d0cSEric Dumazet #define sk_for_each_entry_offset_rcu(tpos, pos, head, offset)		       \
770b6f4f848STim Hansen 	for (pos = rcu_dereference(hlist_first_rcu(head));		       \
771ca065d0cSEric Dumazet 	     pos != NULL &&						       \
7722dc41cffSDavid Held 		({ tpos = (typeof(*tpos) *)((void *)pos - offset); 1;});       \
773b6f4f848STim Hansen 	     pos = rcu_dereference(hlist_next_rcu(pos)))
7742dc41cffSDavid Held 
775c336d148SEric W. Biederman static inline struct user_namespace *sk_user_ns(struct sock *sk)
776c336d148SEric W. Biederman {
777c336d148SEric W. Biederman 	/* Careful only use this in a context where these parameters
778c336d148SEric W. Biederman 	 * can not change and must all be valid, such as recvmsg from
779c336d148SEric W. Biederman 	 * userspace.
780c336d148SEric W. Biederman 	 */
781c336d148SEric W. Biederman 	return sk->sk_socket->file->f_cred->user_ns;
782c336d148SEric W. Biederman }
783c336d148SEric W. Biederman 
7841da177e4SLinus Torvalds /* Sock flags */
7851da177e4SLinus Torvalds enum sock_flags {
7861da177e4SLinus Torvalds 	SOCK_DEAD,
7871da177e4SLinus Torvalds 	SOCK_DONE,
7881da177e4SLinus Torvalds 	SOCK_URGINLINE,
7891da177e4SLinus Torvalds 	SOCK_KEEPOPEN,
7901da177e4SLinus Torvalds 	SOCK_LINGER,
7911da177e4SLinus Torvalds 	SOCK_DESTROY,
7921da177e4SLinus Torvalds 	SOCK_BROADCAST,
7931da177e4SLinus Torvalds 	SOCK_TIMESTAMP,
7941da177e4SLinus Torvalds 	SOCK_ZAPPED,
7951da177e4SLinus Torvalds 	SOCK_USE_WRITE_QUEUE, /* whether to call sk->sk_write_space in sock_wfree */
7961da177e4SLinus Torvalds 	SOCK_DBG, /* %SO_DEBUG setting */
7971da177e4SLinus Torvalds 	SOCK_RCVTSTAMP, /* %SO_TIMESTAMP setting */
79892f37fd2SEric Dumazet 	SOCK_RCVTSTAMPNS, /* %SO_TIMESTAMPNS setting */
7991da177e4SLinus Torvalds 	SOCK_LOCALROUTE, /* route locally only, %SO_DONTROUTE setting */
8001da177e4SLinus Torvalds 	SOCK_QUEUE_SHRUNK, /* write queue has been shrunk recently */
8017cb02404SMel Gorman 	SOCK_MEMALLOC, /* VM depends on this socket for swapping */
80220d49473SPatrick Ohly 	SOCK_TIMESTAMPING_RX_SOFTWARE,  /* %SOF_TIMESTAMPING_RX_SOFTWARE */
803bcdce719SEric Dumazet 	SOCK_FASYNC, /* fasync() active */
8043b885787SNeil Horman 	SOCK_RXQ_OVFL,
8051cdebb42SShirley Ma 	SOCK_ZEROCOPY, /* buffers from userspace */
8066e3e939fSJohannes Berg 	SOCK_WIFI_STATUS, /* push wifi status to userspace */
8073bdc0ebaSBen Greear 	SOCK_NOFCS, /* Tell NIC not to do the Ethernet FCS.
8083bdc0ebaSBen Greear 		     * Will use last 4 bytes of packet sent from
8093bdc0ebaSBen Greear 		     * user-space instead.
8103bdc0ebaSBen Greear 		     */
811d59577b6SVincent Bernat 	SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */
8127d4c04fcSKeller, Jacob E 	SOCK_SELECT_ERR_QUEUE, /* Wake select on error queue */
813a4298e45SEric Dumazet 	SOCK_RCU_FREE, /* wait rcu grace period in sk_destruct() */
81480b14deeSRichard Cochran 	SOCK_TXTIME,
815e4a2a304SJason Wang 	SOCK_XDP, /* XDP is attached */
816887feae3SDeepa Dinamani 	SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */
8171da177e4SLinus Torvalds };
8181da177e4SLinus Torvalds 
81901ce63c9SMarcelo Ricardo Leitner #define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
82001ce63c9SMarcelo Ricardo Leitner 
82153b924b3SRalf Baechle static inline void sock_copy_flags(struct sock *nsk, struct sock *osk)
82253b924b3SRalf Baechle {
82353b924b3SRalf Baechle 	nsk->sk_flags = osk->sk_flags;
82453b924b3SRalf Baechle }
82553b924b3SRalf Baechle 
8261da177e4SLinus Torvalds static inline void sock_set_flag(struct sock *sk, enum sock_flags flag)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	__set_bit(flag, &sk->sk_flags);
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds static inline void sock_reset_flag(struct sock *sk, enum sock_flags flag)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	__clear_bit(flag, &sk->sk_flags);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361b23a5dfSEric Dumazet static inline bool sock_flag(const struct sock *sk, enum sock_flags flag)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	return test_bit(flag, &sk->sk_flags);
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
841c93bdd0eSMel Gorman #ifdef CONFIG_NET
842a7950ae8SDavidlohr Bueso DECLARE_STATIC_KEY_FALSE(memalloc_socks_key);
843c93bdd0eSMel Gorman static inline int sk_memalloc_socks(void)
844c93bdd0eSMel Gorman {
845a7950ae8SDavidlohr Bueso 	return static_branch_unlikely(&memalloc_socks_key);
846c93bdd0eSMel Gorman }
847c93bdd0eSMel Gorman #else
848c93bdd0eSMel Gorman 
849c93bdd0eSMel Gorman static inline int sk_memalloc_socks(void)
850c93bdd0eSMel Gorman {
851c93bdd0eSMel Gorman 	return 0;
852c93bdd0eSMel Gorman }
853c93bdd0eSMel Gorman 
854c93bdd0eSMel Gorman #endif
855c93bdd0eSMel Gorman 
8567450aaf6SEric Dumazet static inline gfp_t sk_gfp_mask(const struct sock *sk, gfp_t gfp_mask)
85799a1dec7SMel Gorman {
8587450aaf6SEric Dumazet 	return gfp_mask | (sk->sk_allocation & __GFP_MEMALLOC);
85999a1dec7SMel Gorman }
86099a1dec7SMel Gorman 
8611da177e4SLinus Torvalds static inline void sk_acceptq_removed(struct sock *sk)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	sk->sk_ack_backlog--;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds static inline void sk_acceptq_added(struct sock *sk)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	sk->sk_ack_backlog++;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
871dc6b9b78SEric Dumazet static inline bool sk_acceptq_is_full(const struct sock *sk)
8721da177e4SLinus Torvalds {
87364a14651SDavid S. Miller 	return sk->sk_ack_backlog > sk->sk_max_ack_backlog;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /*
8771da177e4SLinus Torvalds  * Compute minimal free write space needed to queue new packets.
8781da177e4SLinus Torvalds  */
879dc6b9b78SEric Dumazet static inline int sk_stream_min_wspace(const struct sock *sk)
8801da177e4SLinus Torvalds {
8818df09ea3SEric Dumazet 	return sk->sk_wmem_queued >> 1;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
884dc6b9b78SEric Dumazet static inline int sk_stream_wspace(const struct sock *sk)
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	return sk->sk_sndbuf - sk->sk_wmem_queued;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
88969336bd2SJoe Perches void sk_stream_write_space(struct sock *sk);
8901da177e4SLinus Torvalds 
8918eae939fSZhu Yi /* OOB backlog add */
892a3a858ffSZhu Yi static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
8939ee6b535SStephen Hemminger {
8947fee226aSEric Dumazet 	/* dont let skb dst not refcounted, we are going to leave rcu lock */
895222d7dbdSEric Dumazet 	skb_dst_force(skb);
8967fee226aSEric Dumazet 
8977fee226aSEric Dumazet 	if (!sk->sk_backlog.tail)
8987fee226aSEric Dumazet 		sk->sk_backlog.head = skb;
8997fee226aSEric Dumazet 	else
9009ee6b535SStephen Hemminger 		sk->sk_backlog.tail->next = skb;
9017fee226aSEric Dumazet 
9029ee6b535SStephen Hemminger 	sk->sk_backlog.tail = skb;
9039ee6b535SStephen Hemminger 	skb->next = NULL;
9049ee6b535SStephen Hemminger }
9051da177e4SLinus Torvalds 
906c377411fSEric Dumazet /*
907c377411fSEric Dumazet  * Take into account size of receive queue and backlog queue
9080fd7bac6SEric Dumazet  * Do not take into account this skb truesize,
9090fd7bac6SEric Dumazet  * to allow even a single big packet to come.
910c377411fSEric Dumazet  */
911274f482dSSorin Dumitru static inline bool sk_rcvqueues_full(const struct sock *sk, unsigned int limit)
912c377411fSEric Dumazet {
913c377411fSEric Dumazet 	unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc);
914c377411fSEric Dumazet 
915f545a38fSEric Dumazet 	return qsize > limit;
916c377411fSEric Dumazet }
917c377411fSEric Dumazet 
9188eae939fSZhu Yi /* The per-socket spinlock must be held here. */
919f545a38fSEric Dumazet static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb,
920f545a38fSEric Dumazet 					      unsigned int limit)
9218eae939fSZhu Yi {
922274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, limit))
9238eae939fSZhu Yi 		return -ENOBUFS;
9248eae939fSZhu Yi 
925c7c49b8fSEric Dumazet 	/*
926c7c49b8fSEric Dumazet 	 * If the skb was allocated from pfmemalloc reserves, only
927c7c49b8fSEric Dumazet 	 * allow SOCK_MEMALLOC sockets to use it as this socket is
928c7c49b8fSEric Dumazet 	 * helping free memory
929c7c49b8fSEric Dumazet 	 */
930c7c49b8fSEric Dumazet 	if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
931c7c49b8fSEric Dumazet 		return -ENOMEM;
932c7c49b8fSEric Dumazet 
933a3a858ffSZhu Yi 	__sk_add_backlog(sk, skb);
9348eae939fSZhu Yi 	sk->sk_backlog.len += skb->truesize;
9358eae939fSZhu Yi 	return 0;
9368eae939fSZhu Yi }
9378eae939fSZhu Yi 
93869336bd2SJoe Perches int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
939b4b9e355SMel Gorman 
940c57943a1SPeter Zijlstra static inline int sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
941c57943a1SPeter Zijlstra {
942b4b9e355SMel Gorman 	if (sk_memalloc_socks() && skb_pfmemalloc(skb))
943b4b9e355SMel Gorman 		return __sk_backlog_rcv(sk, skb);
944b4b9e355SMel Gorman 
945c57943a1SPeter Zijlstra 	return sk->sk_backlog_rcv(sk, skb);
946c57943a1SPeter Zijlstra }
947c57943a1SPeter Zijlstra 
9482c8c56e1SEric Dumazet static inline void sk_incoming_cpu_update(struct sock *sk)
9492c8c56e1SEric Dumazet {
95034cfb542SPaolo Abeni 	int cpu = raw_smp_processor_id();
95134cfb542SPaolo Abeni 
95234cfb542SPaolo Abeni 	if (unlikely(sk->sk_incoming_cpu != cpu))
95334cfb542SPaolo Abeni 		sk->sk_incoming_cpu = cpu;
9542c8c56e1SEric Dumazet }
9552c8c56e1SEric Dumazet 
956fe477558STom Herbert static inline void sock_rps_record_flow_hash(__u32 hash)
957c58dc01bSDavid S. Miller {
958c58dc01bSDavid S. Miller #ifdef CONFIG_RPS
959c58dc01bSDavid S. Miller 	struct rps_sock_flow_table *sock_flow_table;
960c58dc01bSDavid S. Miller 
961c58dc01bSDavid S. Miller 	rcu_read_lock();
962c58dc01bSDavid S. Miller 	sock_flow_table = rcu_dereference(rps_sock_flow_table);
963fe477558STom Herbert 	rps_record_sock_flow(sock_flow_table, hash);
964c58dc01bSDavid S. Miller 	rcu_read_unlock();
965c58dc01bSDavid S. Miller #endif
966c58dc01bSDavid S. Miller }
967c58dc01bSDavid S. Miller 
968fe477558STom Herbert static inline void sock_rps_record_flow(const struct sock *sk)
969fe477558STom Herbert {
970c9d8ca04SZhi Yong Wu #ifdef CONFIG_RPS
971dc05360fSEric Dumazet 	if (static_branch_unlikely(&rfs_needed)) {
97213bfff25SEric Dumazet 		/* Reading sk->sk_rxhash might incur an expensive cache line
97313bfff25SEric Dumazet 		 * miss.
9745b8e2f61SEric Dumazet 		 *
9755b8e2f61SEric Dumazet 		 * TCP_ESTABLISHED does cover almost all states where RFS
9765b8e2f61SEric Dumazet 		 * might be useful, and is cheaper [1] than testing :
9775b8e2f61SEric Dumazet 		 *	IPv4: inet_sk(sk)->inet_daddr
9785b8e2f61SEric Dumazet 		 * 	IPv6: ipv6_addr_any(&sk->sk_v6_daddr)
9795b8e2f61SEric Dumazet 		 * OR	an additional socket flag
9805b8e2f61SEric Dumazet 		 * [1] : sk_state and sk_prot are in the same cache line.
9815b8e2f61SEric Dumazet 		 */
9825b8e2f61SEric Dumazet 		if (sk->sk_state == TCP_ESTABLISHED)
983fe477558STom Herbert 			sock_rps_record_flow_hash(sk->sk_rxhash);
98413bfff25SEric Dumazet 	}
985c9d8ca04SZhi Yong Wu #endif
986fe477558STom Herbert }
987fe477558STom Herbert 
988bdeab991STom Herbert static inline void sock_rps_save_rxhash(struct sock *sk,
989bdeab991STom Herbert 					const struct sk_buff *skb)
990c58dc01bSDavid S. Miller {
991c58dc01bSDavid S. Miller #ifdef CONFIG_RPS
992567e4b79SEric Dumazet 	if (unlikely(sk->sk_rxhash != skb->hash))
99361b905daSTom Herbert 		sk->sk_rxhash = skb->hash;
994c58dc01bSDavid S. Miller #endif
995c58dc01bSDavid S. Miller }
996c58dc01bSDavid S. Miller 
997bdeab991STom Herbert static inline void sock_rps_reset_rxhash(struct sock *sk)
998bdeab991STom Herbert {
999bdeab991STom Herbert #ifdef CONFIG_RPS
1000bdeab991STom Herbert 	sk->sk_rxhash = 0;
1001bdeab991STom Herbert #endif
1002bdeab991STom Herbert }
1003bdeab991STom Herbert 
1004d9dc8b0fSWANG Cong #define sk_wait_event(__sk, __timeo, __condition, __wait)		\
1005cfcabdccSStephen Hemminger 	({	int __rc;						\
10061da177e4SLinus Torvalds 		release_sock(__sk);					\
1007cfcabdccSStephen Hemminger 		__rc = __condition;					\
1008cfcabdccSStephen Hemminger 		if (!__rc) {						\
1009d9dc8b0fSWANG Cong 			*(__timeo) = wait_woken(__wait,			\
1010d9dc8b0fSWANG Cong 						TASK_INTERRUPTIBLE,	\
1011d9dc8b0fSWANG Cong 						*(__timeo));		\
10121da177e4SLinus Torvalds 		}							\
101326cabd31SPeter Zijlstra 		sched_annotate_sleep();					\
10141da177e4SLinus Torvalds 		lock_sock(__sk);					\
1015cfcabdccSStephen Hemminger 		__rc = __condition;					\
1016cfcabdccSStephen Hemminger 		__rc;							\
10171da177e4SLinus Torvalds 	})
10181da177e4SLinus Torvalds 
101969336bd2SJoe Perches int sk_stream_wait_connect(struct sock *sk, long *timeo_p);
102069336bd2SJoe Perches int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
102169336bd2SJoe Perches void sk_stream_wait_close(struct sock *sk, long timeo_p);
102269336bd2SJoe Perches int sk_stream_error(struct sock *sk, int flags, int err);
102369336bd2SJoe Perches void sk_stream_kill_queues(struct sock *sk);
102469336bd2SJoe Perches void sk_set_memalloc(struct sock *sk);
102569336bd2SJoe Perches void sk_clear_memalloc(struct sock *sk);
10261da177e4SLinus Torvalds 
1027d41a69f1SEric Dumazet void __sk_flush_backlog(struct sock *sk);
1028d41a69f1SEric Dumazet 
1029d41a69f1SEric Dumazet static inline bool sk_flush_backlog(struct sock *sk)
1030d41a69f1SEric Dumazet {
1031d41a69f1SEric Dumazet 	if (unlikely(READ_ONCE(sk->sk_backlog.tail))) {
1032d41a69f1SEric Dumazet 		__sk_flush_backlog(sk);
1033d41a69f1SEric Dumazet 		return true;
1034d41a69f1SEric Dumazet 	}
1035d41a69f1SEric Dumazet 	return false;
1036d41a69f1SEric Dumazet }
1037d41a69f1SEric Dumazet 
1038dfbafc99SSabrina Dubroca int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb);
10391da177e4SLinus Torvalds 
104060236fddSArnaldo Carvalho de Melo struct request_sock_ops;
10416d6ee43eSArnaldo Carvalho de Melo struct timewait_sock_ops;
1042ab1e0a13SArnaldo Carvalho de Melo struct inet_hashinfo;
1043fc8717baSPavel Emelyanov struct raw_hashinfo;
1044f16a7dd5SUrsula Braun struct smc_hashinfo;
1045de477254SPaul Gortmaker struct module;
10462e6599cbSArnaldo Carvalho de Melo 
1047f77d6021SEric Dumazet /*
10485f0d5a3aSPaul E. McKenney  * caches using SLAB_TYPESAFE_BY_RCU should let .next pointer from nulls nodes
1049f77d6021SEric Dumazet  * un-modified. Special care is taken when initializing object to zero.
1050f77d6021SEric Dumazet  */
1051f77d6021SEric Dumazet static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1052f77d6021SEric Dumazet {
1053f77d6021SEric Dumazet 	if (offsetof(struct sock, sk_node.next) != 0)
1054f77d6021SEric Dumazet 		memset(sk, 0, offsetof(struct sock, sk_node.next));
1055f77d6021SEric Dumazet 	memset(&sk->sk_node.pprev, 0,
1056f77d6021SEric Dumazet 	       size - offsetof(struct sock, sk_node.pprev));
1057f77d6021SEric Dumazet }
1058f77d6021SEric Dumazet 
10591da177e4SLinus Torvalds /* Networking protocol blocks we attach to sockets.
10601da177e4SLinus Torvalds  * socket layer -> transport layer interface
10611da177e4SLinus Torvalds  */
10621da177e4SLinus Torvalds struct proto {
10631da177e4SLinus Torvalds 	void			(*close)(struct sock *sk,
10641da177e4SLinus Torvalds 					long timeout);
1065d74bad4eSAndrey Ignatov 	int			(*pre_connect)(struct sock *sk,
1066d74bad4eSAndrey Ignatov 					struct sockaddr *uaddr,
1067d74bad4eSAndrey Ignatov 					int addr_len);
10681da177e4SLinus Torvalds 	int			(*connect)(struct sock *sk,
10691da177e4SLinus Torvalds 					struct sockaddr *uaddr,
10701da177e4SLinus Torvalds 					int addr_len);
10711da177e4SLinus Torvalds 	int			(*disconnect)(struct sock *sk, int flags);
10721da177e4SLinus Torvalds 
1073cdfbabfbSDavid Howells 	struct sock *		(*accept)(struct sock *sk, int flags, int *err,
1074cdfbabfbSDavid Howells 					  bool kern);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	int			(*ioctl)(struct sock *sk, int cmd,
10771da177e4SLinus Torvalds 					 unsigned long arg);
10781da177e4SLinus Torvalds 	int			(*init)(struct sock *sk);
10797d06b2e0SBrian Haley 	void			(*destroy)(struct sock *sk);
10801da177e4SLinus Torvalds 	void			(*shutdown)(struct sock *sk, int how);
10811da177e4SLinus Torvalds 	int			(*setsockopt)(struct sock *sk, int level,
10821da177e4SLinus Torvalds 					int optname, char __user *optval,
1083b7058842SDavid S. Miller 					unsigned int optlen);
10841da177e4SLinus Torvalds 	int			(*getsockopt)(struct sock *sk, int level,
10851da177e4SLinus Torvalds 					int optname, char __user *optval,
10861da177e4SLinus Torvalds 					int __user *option);
10874b9d07a4SUrsula Braun 	void			(*keepalive)(struct sock *sk, int valbool);
1088af01d537SAlexey Dobriyan #ifdef CONFIG_COMPAT
10893fdadf7dSDmitry Mishin 	int			(*compat_setsockopt)(struct sock *sk,
10903fdadf7dSDmitry Mishin 					int level,
10913fdadf7dSDmitry Mishin 					int optname, char __user *optval,
1092b7058842SDavid S. Miller 					unsigned int optlen);
10933fdadf7dSDmitry Mishin 	int			(*compat_getsockopt)(struct sock *sk,
10943fdadf7dSDmitry Mishin 					int level,
10953fdadf7dSDmitry Mishin 					int optname, char __user *optval,
10963fdadf7dSDmitry Mishin 					int __user *option);
1097709b46e8SEric W. Biederman 	int			(*compat_ioctl)(struct sock *sk,
1098709b46e8SEric W. Biederman 					unsigned int cmd, unsigned long arg);
1099af01d537SAlexey Dobriyan #endif
11001b784140SYing Xue 	int			(*sendmsg)(struct sock *sk, struct msghdr *msg,
11011b784140SYing Xue 					   size_t len);
11021b784140SYing Xue 	int			(*recvmsg)(struct sock *sk, struct msghdr *msg,
11031da177e4SLinus Torvalds 					   size_t len, int noblock, int flags,
11041da177e4SLinus Torvalds 					   int *addr_len);
11051da177e4SLinus Torvalds 	int			(*sendpage)(struct sock *sk, struct page *page,
11061da177e4SLinus Torvalds 					int offset, size_t size, int flags);
11071da177e4SLinus Torvalds 	int			(*bind)(struct sock *sk,
11081da177e4SLinus Torvalds 					struct sockaddr *uaddr, int addr_len);
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	int			(*backlog_rcv) (struct sock *sk,
11111da177e4SLinus Torvalds 						struct sk_buff *skb);
11121da177e4SLinus Torvalds 
111346d3ceabSEric Dumazet 	void		(*release_cb)(struct sock *sk);
111446d3ceabSEric Dumazet 
11151da177e4SLinus Torvalds 	/* Keeping track of sk's, looking them up, and port selection methods. */
1116086c653fSCraig Gallek 	int			(*hash)(struct sock *sk);
11171da177e4SLinus Torvalds 	void			(*unhash)(struct sock *sk);
1118719f8358SEric Dumazet 	void			(*rehash)(struct sock *sk);
11191da177e4SLinus Torvalds 	int			(*get_port)(struct sock *sk, unsigned short snum);
11201da177e4SLinus Torvalds 
1121286ab3d4SEric Dumazet 	/* Keeping track of sockets in use */
112265f76517SEric Dumazet #ifdef CONFIG_PROC_FS
112313ff3d6fSPavel Emelyanov 	unsigned int		inuse_idx;
112465f76517SEric Dumazet #endif
1125ebb53d75SArnaldo Carvalho de Melo 
1126a74f0fa0SEric Dumazet 	bool			(*stream_memory_free)(const struct sock *sk, int wake);
11278934ce2fSJohn Fastabend 	bool			(*stream_memory_read)(const struct sock *sk);
11281da177e4SLinus Torvalds 	/* Memory pressure */
11295c52ba17SPavel Emelyanov 	void			(*enter_memory_pressure)(struct sock *sk);
113006044751SEric Dumazet 	void			(*leave_memory_pressure)(struct sock *sk);
11318d987e5cSEric Dumazet 	atomic_long_t		*memory_allocated;	/* Current allocated memory. */
11321748376bSEric Dumazet 	struct percpu_counter	*sockets_allocated;	/* Current number of sockets. */
11331da177e4SLinus Torvalds 	/*
11341da177e4SLinus Torvalds 	 * Pressure flag: try to collapse.
11351da177e4SLinus Torvalds 	 * Technical note: it is used by multiple contexts non atomically.
11363ab224beSHideo Aoki 	 * All the __sk_mem_schedule() is of this nature: accounting
11371da177e4SLinus Torvalds 	 * is strict, actions are advisory and have some latency.
11381da177e4SLinus Torvalds 	 */
113906044751SEric Dumazet 	unsigned long		*memory_pressure;
11408d987e5cSEric Dumazet 	long			*sysctl_mem;
1141a3dcaf17SEric Dumazet 
11421da177e4SLinus Torvalds 	int			*sysctl_wmem;
11431da177e4SLinus Torvalds 	int			*sysctl_rmem;
1144a3dcaf17SEric Dumazet 	u32			sysctl_wmem_offset;
1145a3dcaf17SEric Dumazet 	u32			sysctl_rmem_offset;
1146a3dcaf17SEric Dumazet 
11471da177e4SLinus Torvalds 	int			max_header;
11487ba42910SChangli Gao 	bool			no_autobind;
11491da177e4SLinus Torvalds 
1150e18b890bSChristoph Lameter 	struct kmem_cache	*slab;
11511da177e4SLinus Torvalds 	unsigned int		obj_size;
1152d50112edSAlexey Dobriyan 	slab_flags_t		slab_flags;
11537bbdb81eSAlexey Dobriyan 	unsigned int		useroffset;	/* Usercopy region offset */
11547bbdb81eSAlexey Dobriyan 	unsigned int		usersize;	/* Usercopy region size */
11551da177e4SLinus Torvalds 
1156dd24c001SEric Dumazet 	struct percpu_counter	*orphan_count;
11578feaf0c0SArnaldo Carvalho de Melo 
115860236fddSArnaldo Carvalho de Melo 	struct request_sock_ops	*rsk_prot;
11596d6ee43eSArnaldo Carvalho de Melo 	struct timewait_sock_ops *twsk_prot;
11602e6599cbSArnaldo Carvalho de Melo 
116139d8cda7SPavel Emelyanov 	union {
1162ab1e0a13SArnaldo Carvalho de Melo 		struct inet_hashinfo	*hashinfo;
1163645ca708SEric Dumazet 		struct udp_table	*udp_table;
1164fc8717baSPavel Emelyanov 		struct raw_hashinfo	*raw_hash;
1165f16a7dd5SUrsula Braun 		struct smc_hashinfo	*smc_hash;
116639d8cda7SPavel Emelyanov 	} h;
1167ab1e0a13SArnaldo Carvalho de Melo 
11681da177e4SLinus Torvalds 	struct module		*owner;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	char			name[32];
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	struct list_head	node;
1173e6848976SArnaldo Carvalho de Melo #ifdef SOCK_REFCNT_DEBUG
1174e6848976SArnaldo Carvalho de Melo 	atomic_t		socks;
1175e6848976SArnaldo Carvalho de Melo #endif
117664be0aedSLorenzo Colitti 	int			(*diag_destroy)(struct sock *sk, int err);
11773859a271SKees Cook } __randomize_layout;
1178e1aab161SGlauber Costa 
117969336bd2SJoe Perches int proto_register(struct proto *prot, int alloc_slab);
118069336bd2SJoe Perches void proto_unregister(struct proto *prot);
1181bf2ae2e4SXin Long int sock_load_diag_module(int family, int protocol);
11821da177e4SLinus Torvalds 
1183e6848976SArnaldo Carvalho de Melo #ifdef SOCK_REFCNT_DEBUG
1184e6848976SArnaldo Carvalho de Melo static inline void sk_refcnt_debug_inc(struct sock *sk)
1185e6848976SArnaldo Carvalho de Melo {
1186e6848976SArnaldo Carvalho de Melo 	atomic_inc(&sk->sk_prot->socks);
1187e6848976SArnaldo Carvalho de Melo }
1188e6848976SArnaldo Carvalho de Melo 
1189e6848976SArnaldo Carvalho de Melo static inline void sk_refcnt_debug_dec(struct sock *sk)
1190e6848976SArnaldo Carvalho de Melo {
1191e6848976SArnaldo Carvalho de Melo 	atomic_dec(&sk->sk_prot->socks);
1192e6848976SArnaldo Carvalho de Melo 	printk(KERN_DEBUG "%s socket %p released, %d are still alive\n",
1193e6848976SArnaldo Carvalho de Melo 	       sk->sk_prot->name, sk, atomic_read(&sk->sk_prot->socks));
1194e6848976SArnaldo Carvalho de Melo }
1195e6848976SArnaldo Carvalho de Melo 
1196dec34fb0SYing Xue static inline void sk_refcnt_debug_release(const struct sock *sk)
1197e6848976SArnaldo Carvalho de Melo {
119841c6d650SReshetova, Elena 	if (refcount_read(&sk->sk_refcnt) != 1)
1199e6848976SArnaldo Carvalho de Melo 		printk(KERN_DEBUG "Destruction of the %s socket %p delayed, refcnt=%d\n",
120041c6d650SReshetova, Elena 		       sk->sk_prot->name, sk, refcount_read(&sk->sk_refcnt));
1201e6848976SArnaldo Carvalho de Melo }
1202e6848976SArnaldo Carvalho de Melo #else /* SOCK_REFCNT_DEBUG */
1203e6848976SArnaldo Carvalho de Melo #define sk_refcnt_debug_inc(sk) do { } while (0)
1204e6848976SArnaldo Carvalho de Melo #define sk_refcnt_debug_dec(sk) do { } while (0)
1205e6848976SArnaldo Carvalho de Melo #define sk_refcnt_debug_release(sk) do { } while (0)
1206e6848976SArnaldo Carvalho de Melo #endif /* SOCK_REFCNT_DEBUG */
1207e6848976SArnaldo Carvalho de Melo 
1208a74f0fa0SEric Dumazet static inline bool __sk_stream_memory_free(const struct sock *sk, int wake)
1209c9bee3b7SEric Dumazet {
1210c9bee3b7SEric Dumazet 	if (sk->sk_wmem_queued >= sk->sk_sndbuf)
1211c9bee3b7SEric Dumazet 		return false;
1212c9bee3b7SEric Dumazet 
1213c9bee3b7SEric Dumazet 	return sk->sk_prot->stream_memory_free ?
1214a74f0fa0SEric Dumazet 		sk->sk_prot->stream_memory_free(sk, wake) : true;
1215a74f0fa0SEric Dumazet }
1216a74f0fa0SEric Dumazet 
1217a74f0fa0SEric Dumazet static inline bool sk_stream_memory_free(const struct sock *sk)
1218a74f0fa0SEric Dumazet {
1219a74f0fa0SEric Dumazet 	return __sk_stream_memory_free(sk, 0);
1220a74f0fa0SEric Dumazet }
1221a74f0fa0SEric Dumazet 
1222a74f0fa0SEric Dumazet static inline bool __sk_stream_is_writeable(const struct sock *sk, int wake)
1223a74f0fa0SEric Dumazet {
1224a74f0fa0SEric Dumazet 	return sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) &&
1225a74f0fa0SEric Dumazet 	       __sk_stream_memory_free(sk, wake);
1226c9bee3b7SEric Dumazet }
1227c9bee3b7SEric Dumazet 
122864dc6130SEric Dumazet static inline bool sk_stream_is_writeable(const struct sock *sk)
122964dc6130SEric Dumazet {
1230a74f0fa0SEric Dumazet 	return __sk_stream_is_writeable(sk, 0);
123164dc6130SEric Dumazet }
1232e1aab161SGlauber Costa 
123354fd9c2dSDaniel Borkmann static inline int sk_under_cgroup_hierarchy(struct sock *sk,
123454fd9c2dSDaniel Borkmann 					    struct cgroup *ancestor)
123554fd9c2dSDaniel Borkmann {
123654fd9c2dSDaniel Borkmann #ifdef CONFIG_SOCK_CGROUP_DATA
123754fd9c2dSDaniel Borkmann 	return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data),
123854fd9c2dSDaniel Borkmann 				    ancestor);
123954fd9c2dSDaniel Borkmann #else
124054fd9c2dSDaniel Borkmann 	return -ENOTSUPP;
124154fd9c2dSDaniel Borkmann #endif
124254fd9c2dSDaniel Borkmann }
1243c9bee3b7SEric Dumazet 
1244180d8cd9SGlauber Costa static inline bool sk_has_memory_pressure(const struct sock *sk)
1245180d8cd9SGlauber Costa {
1246180d8cd9SGlauber Costa 	return sk->sk_prot->memory_pressure != NULL;
1247180d8cd9SGlauber Costa }
1248180d8cd9SGlauber Costa 
1249180d8cd9SGlauber Costa static inline bool sk_under_memory_pressure(const struct sock *sk)
1250180d8cd9SGlauber Costa {
1251180d8cd9SGlauber Costa 	if (!sk->sk_prot->memory_pressure)
1252180d8cd9SGlauber Costa 		return false;
1253e1aab161SGlauber Costa 
1254baac50bbSJohannes Weiner 	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
1255baac50bbSJohannes Weiner 	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
1256e805605cSJohannes Weiner 		return true;
1257e1aab161SGlauber Costa 
125835b87f6cSChristoph Paasch 	return !!*sk->sk_prot->memory_pressure;
1259180d8cd9SGlauber Costa }
1260180d8cd9SGlauber Costa 
1261180d8cd9SGlauber Costa static inline long
1262180d8cd9SGlauber Costa sk_memory_allocated(const struct sock *sk)
1263180d8cd9SGlauber Costa {
1264e805605cSJohannes Weiner 	return atomic_long_read(sk->sk_prot->memory_allocated);
1265180d8cd9SGlauber Costa }
1266180d8cd9SGlauber Costa 
1267180d8cd9SGlauber Costa static inline long
1268e805605cSJohannes Weiner sk_memory_allocated_add(struct sock *sk, int amt)
1269180d8cd9SGlauber Costa {
1270e805605cSJohannes Weiner 	return atomic_long_add_return(amt, sk->sk_prot->memory_allocated);
1271180d8cd9SGlauber Costa }
1272180d8cd9SGlauber Costa 
1273180d8cd9SGlauber Costa static inline void
12740e90b31fSGlauber Costa sk_memory_allocated_sub(struct sock *sk, int amt)
1275180d8cd9SGlauber Costa {
1276e805605cSJohannes Weiner 	atomic_long_sub(amt, sk->sk_prot->memory_allocated);
1277180d8cd9SGlauber Costa }
1278180d8cd9SGlauber Costa 
1279180d8cd9SGlauber Costa static inline void sk_sockets_allocated_dec(struct sock *sk)
1280180d8cd9SGlauber Costa {
1281af95d7dfSJohannes Weiner 	percpu_counter_dec(sk->sk_prot->sockets_allocated);
1282180d8cd9SGlauber Costa }
1283180d8cd9SGlauber Costa 
1284180d8cd9SGlauber Costa static inline void sk_sockets_allocated_inc(struct sock *sk)
1285180d8cd9SGlauber Costa {
1286af95d7dfSJohannes Weiner 	percpu_counter_inc(sk->sk_prot->sockets_allocated);
1287180d8cd9SGlauber Costa }
1288180d8cd9SGlauber Costa 
12895bf325a5SEric Dumazet static inline u64
1290180d8cd9SGlauber Costa sk_sockets_allocated_read_positive(struct sock *sk)
1291180d8cd9SGlauber Costa {
1292af95d7dfSJohannes Weiner 	return percpu_counter_read_positive(sk->sk_prot->sockets_allocated);
1293180d8cd9SGlauber Costa }
1294180d8cd9SGlauber Costa 
1295180d8cd9SGlauber Costa static inline int
1296180d8cd9SGlauber Costa proto_sockets_allocated_sum_positive(struct proto *prot)
1297180d8cd9SGlauber Costa {
1298180d8cd9SGlauber Costa 	return percpu_counter_sum_positive(prot->sockets_allocated);
1299180d8cd9SGlauber Costa }
1300180d8cd9SGlauber Costa 
1301180d8cd9SGlauber Costa static inline long
1302180d8cd9SGlauber Costa proto_memory_allocated(struct proto *prot)
1303180d8cd9SGlauber Costa {
1304180d8cd9SGlauber Costa 	return atomic_long_read(prot->memory_allocated);
1305180d8cd9SGlauber Costa }
1306180d8cd9SGlauber Costa 
1307180d8cd9SGlauber Costa static inline bool
1308180d8cd9SGlauber Costa proto_memory_pressure(struct proto *prot)
1309180d8cd9SGlauber Costa {
1310180d8cd9SGlauber Costa 	if (!prot->memory_pressure)
1311180d8cd9SGlauber Costa 		return false;
1312180d8cd9SGlauber Costa 	return !!*prot->memory_pressure;
1313180d8cd9SGlauber Costa }
1314180d8cd9SGlauber Costa 
131565f76517SEric Dumazet 
131665f76517SEric Dumazet #ifdef CONFIG_PROC_FS
13171da177e4SLinus Torvalds /* Called with local bh disabled */
131869336bd2SJoe Perches void sock_prot_inuse_add(struct net *net, struct proto *prot, int inc);
131969336bd2SJoe Perches int sock_prot_inuse_get(struct net *net, struct proto *proto);
1320648845abSTonghao Zhang int sock_inuse_get(struct net *net);
132165f76517SEric Dumazet #else
1322dc6b9b78SEric Dumazet static inline void sock_prot_inuse_add(struct net *net, struct proto *prot,
1323c29a0bc4SPavel Emelyanov 		int inc)
132465f76517SEric Dumazet {
132565f76517SEric Dumazet }
132665f76517SEric Dumazet #endif
132765f76517SEric Dumazet 
13281da177e4SLinus Torvalds 
1329614c6cb4SArnaldo Carvalho de Melo /* With per-bucket locks this operation is not-atomic, so that
1330614c6cb4SArnaldo Carvalho de Melo  * this version is not worse.
1331614c6cb4SArnaldo Carvalho de Melo  */
1332086c653fSCraig Gallek static inline int __sk_prot_rehash(struct sock *sk)
1333614c6cb4SArnaldo Carvalho de Melo {
1334614c6cb4SArnaldo Carvalho de Melo 	sk->sk_prot->unhash(sk);
1335086c653fSCraig Gallek 	return sk->sk_prot->hash(sk);
1336614c6cb4SArnaldo Carvalho de Melo }
1337614c6cb4SArnaldo Carvalho de Melo 
13381da177e4SLinus Torvalds /* About 10 seconds */
13391da177e4SLinus Torvalds #define SOCK_DESTROY_TIME (10*HZ)
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds /* Sockets 0-1023 can't be bound to unless you are superuser */
13421da177e4SLinus Torvalds #define PROT_SOCK	1024
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds #define SHUTDOWN_MASK	3
13451da177e4SLinus Torvalds #define RCV_SHUTDOWN	1
13461da177e4SLinus Torvalds #define SEND_SHUTDOWN	2
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds #define SOCK_SNDBUF_LOCK	1
13491da177e4SLinus Torvalds #define SOCK_RCVBUF_LOCK	2
13501da177e4SLinus Torvalds #define SOCK_BINDADDR_LOCK	4
13511da177e4SLinus Torvalds #define SOCK_BINDPORT_LOCK	8
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds struct socket_alloc {
13541da177e4SLinus Torvalds 	struct socket socket;
13551da177e4SLinus Torvalds 	struct inode vfs_inode;
13561da177e4SLinus Torvalds };
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds static inline struct socket *SOCKET_I(struct inode *inode)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	return &container_of(inode, struct socket_alloc, vfs_inode)->socket;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds static inline struct inode *SOCK_INODE(struct socket *socket)
13641da177e4SLinus Torvalds {
13651da177e4SLinus Torvalds 	return &container_of(socket, struct socket_alloc, socket)->vfs_inode;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13683ab224beSHideo Aoki /*
13693ab224beSHideo Aoki  * Functions for memory accounting
13703ab224beSHideo Aoki  */
1371f8c3bf00SPaolo Abeni int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind);
137269336bd2SJoe Perches int __sk_mem_schedule(struct sock *sk, int size, int kind);
1373f8c3bf00SPaolo Abeni void __sk_mem_reduce_allocated(struct sock *sk, int amount);
13741a24e04eSEric Dumazet void __sk_mem_reclaim(struct sock *sk, int amount);
13751da177e4SLinus Torvalds 
1376bd68a2a8SEric Dumazet /* We used to have PAGE_SIZE here, but systems with 64KB pages
1377bd68a2a8SEric Dumazet  * do not necessarily have 16x time more memory than 4KB ones.
1378bd68a2a8SEric Dumazet  */
1379bd68a2a8SEric Dumazet #define SK_MEM_QUANTUM 4096
13803ab224beSHideo Aoki #define SK_MEM_QUANTUM_SHIFT ilog2(SK_MEM_QUANTUM)
13813ab224beSHideo Aoki #define SK_MEM_SEND	0
13823ab224beSHideo Aoki #define SK_MEM_RECV	1
13831da177e4SLinus Torvalds 
1384bd68a2a8SEric Dumazet /* sysctl_mem values are in pages, we convert them in SK_MEM_QUANTUM units */
1385bd68a2a8SEric Dumazet static inline long sk_prot_mem_limits(const struct sock *sk, int index)
1386bd68a2a8SEric Dumazet {
1387bd68a2a8SEric Dumazet 	long val = sk->sk_prot->sysctl_mem[index];
1388bd68a2a8SEric Dumazet 
1389bd68a2a8SEric Dumazet #if PAGE_SIZE > SK_MEM_QUANTUM
1390bd68a2a8SEric Dumazet 	val <<= PAGE_SHIFT - SK_MEM_QUANTUM_SHIFT;
1391bd68a2a8SEric Dumazet #elif PAGE_SIZE < SK_MEM_QUANTUM
1392bd68a2a8SEric Dumazet 	val >>= SK_MEM_QUANTUM_SHIFT - PAGE_SHIFT;
1393bd68a2a8SEric Dumazet #endif
1394bd68a2a8SEric Dumazet 	return val;
1395bd68a2a8SEric Dumazet }
1396bd68a2a8SEric Dumazet 
13973ab224beSHideo Aoki static inline int sk_mem_pages(int amt)
13981da177e4SLinus Torvalds {
13993ab224beSHideo Aoki 	return (amt + SK_MEM_QUANTUM - 1) >> SK_MEM_QUANTUM_SHIFT;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
1402dc6b9b78SEric Dumazet static inline bool sk_has_account(struct sock *sk)
14031da177e4SLinus Torvalds {
14043ab224beSHideo Aoki 	/* return true if protocol supports memory accounting */
14053ab224beSHideo Aoki 	return !!sk->sk_prot->memory_allocated;
14061da177e4SLinus Torvalds }
14071da177e4SLinus Torvalds 
1408dc6b9b78SEric Dumazet static inline bool sk_wmem_schedule(struct sock *sk, int size)
14091da177e4SLinus Torvalds {
14103ab224beSHideo Aoki 	if (!sk_has_account(sk))
1411dc6b9b78SEric Dumazet 		return true;
1412d80d99d6SHerbert Xu 	return size <= sk->sk_forward_alloc ||
14133ab224beSHideo Aoki 		__sk_mem_schedule(sk, size, SK_MEM_SEND);
14143ab224beSHideo Aoki }
14153ab224beSHideo Aoki 
1416c76562b6SMel Gorman static inline bool
141735c448a8SChuck Lever sk_rmem_schedule(struct sock *sk, struct sk_buff *skb, int size)
14183ab224beSHideo Aoki {
14193ab224beSHideo Aoki 	if (!sk_has_account(sk))
1420dc6b9b78SEric Dumazet 		return true;
14213ab224beSHideo Aoki 	return size<= sk->sk_forward_alloc ||
1422c76562b6SMel Gorman 		__sk_mem_schedule(sk, size, SK_MEM_RECV) ||
1423c76562b6SMel Gorman 		skb_pfmemalloc(skb);
14243ab224beSHideo Aoki }
14253ab224beSHideo Aoki 
14263ab224beSHideo Aoki static inline void sk_mem_reclaim(struct sock *sk)
14273ab224beSHideo Aoki {
14283ab224beSHideo Aoki 	if (!sk_has_account(sk))
14293ab224beSHideo Aoki 		return;
14303ab224beSHideo Aoki 	if (sk->sk_forward_alloc >= SK_MEM_QUANTUM)
14311a24e04eSEric Dumazet 		__sk_mem_reclaim(sk, sk->sk_forward_alloc);
14323ab224beSHideo Aoki }
14333ab224beSHideo Aoki 
14349993e7d3SDavid S. Miller static inline void sk_mem_reclaim_partial(struct sock *sk)
14359993e7d3SDavid S. Miller {
14369993e7d3SDavid S. Miller 	if (!sk_has_account(sk))
14379993e7d3SDavid S. Miller 		return;
14389993e7d3SDavid S. Miller 	if (sk->sk_forward_alloc > SK_MEM_QUANTUM)
14391a24e04eSEric Dumazet 		__sk_mem_reclaim(sk, sk->sk_forward_alloc - 1);
14409993e7d3SDavid S. Miller }
14419993e7d3SDavid S. Miller 
14423ab224beSHideo Aoki static inline void sk_mem_charge(struct sock *sk, int size)
14433ab224beSHideo Aoki {
14443ab224beSHideo Aoki 	if (!sk_has_account(sk))
14453ab224beSHideo Aoki 		return;
14463ab224beSHideo Aoki 	sk->sk_forward_alloc -= size;
14473ab224beSHideo Aoki }
14483ab224beSHideo Aoki 
14493ab224beSHideo Aoki static inline void sk_mem_uncharge(struct sock *sk, int size)
14503ab224beSHideo Aoki {
14513ab224beSHideo Aoki 	if (!sk_has_account(sk))
14523ab224beSHideo Aoki 		return;
14533ab224beSHideo Aoki 	sk->sk_forward_alloc += size;
145420c64d5cSEric Dumazet 
145520c64d5cSEric Dumazet 	/* Avoid a possible overflow.
145620c64d5cSEric Dumazet 	 * TCP send queues can make this happen, if sk_mem_reclaim()
145720c64d5cSEric Dumazet 	 * is not called and more than 2 GBytes are released at once.
145820c64d5cSEric Dumazet 	 *
145920c64d5cSEric Dumazet 	 * If we reach 2 MBytes, reclaim 1 MBytes right now, there is
146020c64d5cSEric Dumazet 	 * no need to hold that much forward allocation anyway.
146120c64d5cSEric Dumazet 	 */
146220c64d5cSEric Dumazet 	if (unlikely(sk->sk_forward_alloc >= 1 << 21))
146320c64d5cSEric Dumazet 		__sk_mem_reclaim(sk, 1 << 20);
14643ab224beSHideo Aoki }
14653ab224beSHideo Aoki 
14663ab224beSHideo Aoki static inline void sk_wmem_free_skb(struct sock *sk, struct sk_buff *skb)
14673ab224beSHideo Aoki {
14683ab224beSHideo Aoki 	sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
14693ab224beSHideo Aoki 	sk->sk_wmem_queued -= skb->truesize;
14703ab224beSHideo Aoki 	sk_mem_uncharge(sk, skb->truesize);
14714f661542SEric Dumazet 	if (!sk->sk_tx_skb_cache) {
14724f661542SEric Dumazet 		skb_zcopy_clear(skb, true);
14734f661542SEric Dumazet 		sk->sk_tx_skb_cache = skb;
14744f661542SEric Dumazet 		return;
14754f661542SEric Dumazet 	}
14763ab224beSHideo Aoki 	__kfree_skb(skb);
1477d80d99d6SHerbert Xu }
1478d80d99d6SHerbert Xu 
1479c3f9b018SEric Dumazet static inline void sock_release_ownership(struct sock *sk)
1480c3f9b018SEric Dumazet {
148161881cfbSHannes Frederic Sowa 	if (sk->sk_lock.owned) {
1482c3f9b018SEric Dumazet 		sk->sk_lock.owned = 0;
148361881cfbSHannes Frederic Sowa 
148461881cfbSHannes Frederic Sowa 		/* The sk_lock has mutex_unlock() semantics: */
148561881cfbSHannes Frederic Sowa 		mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
148661881cfbSHannes Frederic Sowa 	}
1487c3f9b018SEric Dumazet }
1488c3f9b018SEric Dumazet 
1489ed07536eSPeter Zijlstra /*
1490ed07536eSPeter Zijlstra  * Macro so as to not evaluate some arguments when
1491ed07536eSPeter Zijlstra  * lockdep is not enabled.
1492ed07536eSPeter Zijlstra  *
1493ed07536eSPeter Zijlstra  * Mark both the sk_lock and the sk_lock.slock as a
1494ed07536eSPeter Zijlstra  * per-address-family lock class.
1495ed07536eSPeter Zijlstra  */
1496ed07536eSPeter Zijlstra #define sock_lock_init_class_and_name(sk, sname, skey, name, key)	\
1497ed07536eSPeter Zijlstra do {									\
1498d2e9117cSJohn Heffner 	sk->sk_lock.owned = 0;						\
1499ed07536eSPeter Zijlstra 	init_waitqueue_head(&sk->sk_lock.wq);				\
1500ed07536eSPeter Zijlstra 	spin_lock_init(&(sk)->sk_lock.slock);				\
1501ed07536eSPeter Zijlstra 	debug_check_no_locks_freed((void *)&(sk)->sk_lock,		\
1502ed07536eSPeter Zijlstra 			sizeof((sk)->sk_lock));				\
1503ed07536eSPeter Zijlstra 	lockdep_set_class_and_name(&(sk)->sk_lock.slock,		\
1504ed07536eSPeter Zijlstra 				(skey), (sname));				\
1505ed07536eSPeter Zijlstra 	lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0);	\
1506ed07536eSPeter Zijlstra } while (0)
1507ed07536eSPeter Zijlstra 
1508b33b0a1bSDavid S. Miller #ifdef CONFIG_LOCKDEP
150905b93801SMatthew Wilcox static inline bool lockdep_sock_is_held(const struct sock *sk)
15101e1d04e6SHannes Frederic Sowa {
15111e1d04e6SHannes Frederic Sowa 	return lockdep_is_held(&sk->sk_lock) ||
15121e1d04e6SHannes Frederic Sowa 	       lockdep_is_held(&sk->sk_lock.slock);
15131e1d04e6SHannes Frederic Sowa }
1514b33b0a1bSDavid S. Miller #endif
15151e1d04e6SHannes Frederic Sowa 
151669336bd2SJoe Perches void lock_sock_nested(struct sock *sk, int subclass);
1517fcc70d5fSPeter Zijlstra 
1518fcc70d5fSPeter Zijlstra static inline void lock_sock(struct sock *sk)
1519fcc70d5fSPeter Zijlstra {
1520fcc70d5fSPeter Zijlstra 	lock_sock_nested(sk, 0);
1521fcc70d5fSPeter Zijlstra }
1522fcc70d5fSPeter Zijlstra 
15238873c064SEric Dumazet void __release_sock(struct sock *sk);
152469336bd2SJoe Perches void release_sock(struct sock *sk);
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds /* BH context may only use the following locking interface. */
15271da177e4SLinus Torvalds #define bh_lock_sock(__sk)	spin_lock(&((__sk)->sk_lock.slock))
1528c6366184SIngo Molnar #define bh_lock_sock_nested(__sk) \
1529c6366184SIngo Molnar 				spin_lock_nested(&((__sk)->sk_lock.slock), \
1530c6366184SIngo Molnar 				SINGLE_DEPTH_NESTING)
15311da177e4SLinus Torvalds #define bh_unlock_sock(__sk)	spin_unlock(&((__sk)->sk_lock.slock))
15321da177e4SLinus Torvalds 
153369336bd2SJoe Perches bool lock_sock_fast(struct sock *sk);
15348a74ad60SEric Dumazet /**
15358a74ad60SEric Dumazet  * unlock_sock_fast - complement of lock_sock_fast
15368a74ad60SEric Dumazet  * @sk: socket
15378a74ad60SEric Dumazet  * @slow: slow mode
15388a74ad60SEric Dumazet  *
15398a74ad60SEric Dumazet  * fast unlock socket for user context.
15408a74ad60SEric Dumazet  * If slow mode is on, we call regular release_sock()
15418a74ad60SEric Dumazet  */
15428a74ad60SEric Dumazet static inline void unlock_sock_fast(struct sock *sk, bool slow)
15434b0b72f7SEric Dumazet {
15448a74ad60SEric Dumazet 	if (slow)
15458a74ad60SEric Dumazet 		release_sock(sk);
15468a74ad60SEric Dumazet 	else
15474b0b72f7SEric Dumazet 		spin_unlock_bh(&sk->sk_lock.slock);
15484b0b72f7SEric Dumazet }
15494b0b72f7SEric Dumazet 
1550fafc4e1eSHannes Frederic Sowa /* Used by processes to "lock" a socket state, so that
1551fafc4e1eSHannes Frederic Sowa  * interrupts and bottom half handlers won't change it
1552fafc4e1eSHannes Frederic Sowa  * from under us. It essentially blocks any incoming
1553fafc4e1eSHannes Frederic Sowa  * packets, so that we won't get any new data or any
1554fafc4e1eSHannes Frederic Sowa  * packets that change the state of the socket.
1555fafc4e1eSHannes Frederic Sowa  *
1556fafc4e1eSHannes Frederic Sowa  * While locked, BH processing will add new packets to
1557fafc4e1eSHannes Frederic Sowa  * the backlog queue.  This queue is processed by the
1558fafc4e1eSHannes Frederic Sowa  * owner of the socket lock right before it is released.
1559fafc4e1eSHannes Frederic Sowa  *
1560fafc4e1eSHannes Frederic Sowa  * Since ~2.3.5 it is also exclusive sleep lock serializing
1561fafc4e1eSHannes Frederic Sowa  * accesses from user process context.
1562fafc4e1eSHannes Frederic Sowa  */
1563fafc4e1eSHannes Frederic Sowa 
156446cc6e49SEric Dumazet static inline void sock_owned_by_me(const struct sock *sk)
1565fafc4e1eSHannes Frederic Sowa {
1566fafc4e1eSHannes Frederic Sowa #ifdef CONFIG_LOCKDEP
15675e91f6ceSEric Dumazet 	WARN_ON_ONCE(!lockdep_sock_is_held(sk) && debug_locks);
1568fafc4e1eSHannes Frederic Sowa #endif
156946cc6e49SEric Dumazet }
157046cc6e49SEric Dumazet 
157146cc6e49SEric Dumazet static inline bool sock_owned_by_user(const struct sock *sk)
157246cc6e49SEric Dumazet {
157346cc6e49SEric Dumazet 	sock_owned_by_me(sk);
1574fafc4e1eSHannes Frederic Sowa 	return sk->sk_lock.owned;
1575fafc4e1eSHannes Frederic Sowa }
1576fafc4e1eSHannes Frederic Sowa 
1577602f7a27STom Herbert static inline bool sock_owned_by_user_nocheck(const struct sock *sk)
1578602f7a27STom Herbert {
1579602f7a27STom Herbert 	return sk->sk_lock.owned;
1580602f7a27STom Herbert }
1581602f7a27STom Herbert 
1582fafc4e1eSHannes Frederic Sowa /* no reclassification while locks are held */
1583fafc4e1eSHannes Frederic Sowa static inline bool sock_allow_reclassification(const struct sock *csk)
1584fafc4e1eSHannes Frederic Sowa {
1585fafc4e1eSHannes Frederic Sowa 	struct sock *sk = (struct sock *)csk;
1586fafc4e1eSHannes Frederic Sowa 
1587fafc4e1eSHannes Frederic Sowa 	return !sk->sk_lock.owned && !spin_is_locked(&sk->sk_lock.slock);
1588fafc4e1eSHannes Frederic Sowa }
15898a74ad60SEric Dumazet 
159069336bd2SJoe Perches struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
159111aa9c28SEric W. Biederman 		      struct proto *prot, int kern);
159269336bd2SJoe Perches void sk_free(struct sock *sk);
1593eb4cb008SCraig Gallek void sk_destruct(struct sock *sk);
159469336bd2SJoe Perches struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority);
159594352d45SArnaldo Carvalho de Melo void sk_free_unlock_clone(struct sock *sk);
15961da177e4SLinus Torvalds 
159769336bd2SJoe Perches struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
1598dd0fc66fSAl Viro 			     gfp_t priority);
15991d2077acSEric Dumazet void __sock_wfree(struct sk_buff *skb);
160069336bd2SJoe Perches void sock_wfree(struct sk_buff *skb);
160198ba0bd5SWillem de Bruijn struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
160298ba0bd5SWillem de Bruijn 			     gfp_t priority);
160369336bd2SJoe Perches void skb_orphan_partial(struct sk_buff *skb);
160469336bd2SJoe Perches void sock_rfree(struct sk_buff *skb);
160562bccb8cSAlexander Duyck void sock_efree(struct sk_buff *skb);
160682eabd9eSAlexander Duyck #ifdef CONFIG_INET
160769336bd2SJoe Perches void sock_edemux(struct sk_buff *skb);
160882eabd9eSAlexander Duyck #else
1609158f323bSEric Dumazet #define sock_edemux sock_efree
161082eabd9eSAlexander Duyck #endif
16111da177e4SLinus Torvalds 
161269336bd2SJoe Perches int sock_setsockopt(struct socket *sock, int level, int op,
161369336bd2SJoe Perches 		    char __user *optval, unsigned int optlen);
16141da177e4SLinus Torvalds 
161569336bd2SJoe Perches int sock_getsockopt(struct socket *sock, int level, int op,
161669336bd2SJoe Perches 		    char __user *optval, int __user *optlen);
1617*c7cbdbf2SArnd Bergmann int sock_gettstamp(struct socket *sock, void __user *userstamp,
1618*c7cbdbf2SArnd Bergmann 		   bool timeval, bool time32);
161969336bd2SJoe Perches struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
162069336bd2SJoe Perches 				    int noblock, int *errcode);
162169336bd2SJoe Perches struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
162269336bd2SJoe Perches 				     unsigned long data_len, int noblock,
162369336bd2SJoe Perches 				     int *errcode, int max_page_order);
162469336bd2SJoe Perches void *sock_kmalloc(struct sock *sk, int size, gfp_t priority);
162569336bd2SJoe Perches void sock_kfree_s(struct sock *sk, void *mem, int size);
162679e88659SDaniel Borkmann void sock_kzfree_s(struct sock *sk, void *mem, int size);
162769336bd2SJoe Perches void sk_send_sigurg(struct sock *sk);
16281da177e4SLinus Torvalds 
1629f28ea365SEdward Jee struct sockcm_cookie {
163080b14deeSRichard Cochran 	u64 transmit_time;
1631f28ea365SEdward Jee 	u32 mark;
16323dd17e63SSoheil Hassas Yeganeh 	u16 tsflags;
1633f28ea365SEdward Jee };
1634f28ea365SEdward Jee 
1635657a0667SWillem de Bruijn static inline void sockcm_init(struct sockcm_cookie *sockc,
1636657a0667SWillem de Bruijn 			       const struct sock *sk)
1637657a0667SWillem de Bruijn {
1638657a0667SWillem de Bruijn 	*sockc = (struct sockcm_cookie) { .tsflags = sk->sk_tsflags };
1639657a0667SWillem de Bruijn }
1640657a0667SWillem de Bruijn 
164139771b12SWillem de Bruijn int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
164239771b12SWillem de Bruijn 		     struct sockcm_cookie *sockc);
1643f28ea365SEdward Jee int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
1644f28ea365SEdward Jee 		   struct sockcm_cookie *sockc);
1645f28ea365SEdward Jee 
16461da177e4SLinus Torvalds /*
16471da177e4SLinus Torvalds  * Functions to fill in entries in struct proto_ops when a protocol
16481da177e4SLinus Torvalds  * does not implement a particular function.
16491da177e4SLinus Torvalds  */
165069336bd2SJoe Perches int sock_no_bind(struct socket *, struct sockaddr *, int);
165169336bd2SJoe Perches int sock_no_connect(struct socket *, struct sockaddr *, int, int);
165269336bd2SJoe Perches int sock_no_socketpair(struct socket *, struct socket *);
1653cdfbabfbSDavid Howells int sock_no_accept(struct socket *, struct socket *, int, bool);
16549b2c45d4SDenys Vlasenko int sock_no_getname(struct socket *, struct sockaddr *, int);
165569336bd2SJoe Perches int sock_no_ioctl(struct socket *, unsigned int, unsigned long);
165669336bd2SJoe Perches int sock_no_listen(struct socket *, int);
165769336bd2SJoe Perches int sock_no_shutdown(struct socket *, int);
165869336bd2SJoe Perches int sock_no_getsockopt(struct socket *, int , int, char __user *, int __user *);
165969336bd2SJoe Perches int sock_no_setsockopt(struct socket *, int, int, char __user *, unsigned int);
16601b784140SYing Xue int sock_no_sendmsg(struct socket *, struct msghdr *, size_t);
1661306b13ebSTom Herbert int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len);
16621b784140SYing Xue int sock_no_recvmsg(struct socket *, struct msghdr *, size_t, int);
166369336bd2SJoe Perches int sock_no_mmap(struct file *file, struct socket *sock,
16641da177e4SLinus Torvalds 		 struct vm_area_struct *vma);
166569336bd2SJoe Perches ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset,
166669336bd2SJoe Perches 			 size_t size, int flags);
1667306b13ebSTom Herbert ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
1668306b13ebSTom Herbert 				int offset, size_t size, int flags);
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds /*
16711da177e4SLinus Torvalds  * Functions to fill in entries in struct proto_ops when a protocol
16721da177e4SLinus Torvalds  * uses the inet style.
16731da177e4SLinus Torvalds  */
167469336bd2SJoe Perches int sock_common_getsockopt(struct socket *sock, int level, int optname,
16751da177e4SLinus Torvalds 				  char __user *optval, int __user *optlen);
16761b784140SYing Xue int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
16771b784140SYing Xue 			int flags);
167869336bd2SJoe Perches int sock_common_setsockopt(struct socket *sock, int level, int optname,
1679b7058842SDavid S. Miller 				  char __user *optval, unsigned int optlen);
168069336bd2SJoe Perches int compat_sock_common_getsockopt(struct socket *sock, int level,
16813fdadf7dSDmitry Mishin 		int optname, char __user *optval, int __user *optlen);
168269336bd2SJoe Perches int compat_sock_common_setsockopt(struct socket *sock, int level,
1683b7058842SDavid S. Miller 		int optname, char __user *optval, unsigned int optlen);
16841da177e4SLinus Torvalds 
168569336bd2SJoe Perches void sk_common_release(struct sock *sk);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds /*
16881da177e4SLinus Torvalds  *	Default socket callbacks and setup code
16891da177e4SLinus Torvalds  */
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds /* Initialise core socket variables */
169269336bd2SJoe Perches void sock_init_data(struct socket *sock, struct sock *sk);
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds /*
16951da177e4SLinus Torvalds  * Socket reference counting postulates.
16961da177e4SLinus Torvalds  *
16971da177e4SLinus Torvalds  * * Each user of socket SHOULD hold a reference count.
16981da177e4SLinus Torvalds  * * Each access point to socket (an hash table bucket, reference from a list,
16991da177e4SLinus Torvalds  *   running timer, skb in flight MUST hold a reference count.
17001da177e4SLinus Torvalds  * * When reference count hits 0, it means it will never increase back.
17011da177e4SLinus Torvalds  * * When reference count hits 0, it means that no references from
17021da177e4SLinus Torvalds  *   outside exist to this socket and current process on current CPU
17031da177e4SLinus Torvalds  *   is last user and may/should destroy this socket.
17041da177e4SLinus Torvalds  * * sk_free is called from any context: process, BH, IRQ. When
17051da177e4SLinus Torvalds  *   it is called, socket has no references from outside -> sk_free
17061da177e4SLinus Torvalds  *   may release descendant resources allocated by the socket, but
17071da177e4SLinus Torvalds  *   to the time when it is called, socket is NOT referenced by any
17081da177e4SLinus Torvalds  *   hash tables, lists etc.
17091da177e4SLinus Torvalds  * * Packets, delivered from outside (from network or from another process)
17101da177e4SLinus Torvalds  *   and enqueued on receive/error queues SHOULD NOT grab reference count,
17111da177e4SLinus Torvalds  *   when they sit in queue. Otherwise, packets will leak to hole, when
17121da177e4SLinus Torvalds  *   socket is looked up by one cpu and unhasing is made by another CPU.
17131da177e4SLinus Torvalds  *   It is true for udp/raw, netlink (leak to receive and error queues), tcp
17141da177e4SLinus Torvalds  *   (leak to backlog). Packet socket does all the processing inside
17151da177e4SLinus Torvalds  *   BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets
17161da177e4SLinus Torvalds  *   use separate SMP lock, so that they are prone too.
17171da177e4SLinus Torvalds  */
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds /* Ungrab socket and destroy it, if it was the last reference. */
17201da177e4SLinus Torvalds static inline void sock_put(struct sock *sk)
17211da177e4SLinus Torvalds {
172241c6d650SReshetova, Elena 	if (refcount_dec_and_test(&sk->sk_refcnt))
17231da177e4SLinus Torvalds 		sk_free(sk);
17241da177e4SLinus Torvalds }
172505dbc7b5SEric Dumazet /* Generic version of sock_put(), dealing with all sockets
172641b822c5SEric Dumazet  * (TCP_TIMEWAIT, TCP_NEW_SYN_RECV, ESTABLISHED...)
172705dbc7b5SEric Dumazet  */
172805dbc7b5SEric Dumazet void sock_gen_put(struct sock *sk);
17291da177e4SLinus Torvalds 
17304f0c40d9SWillem de Bruijn int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested,
1731c3f24cfbSEric Dumazet 		     unsigned int trim_cap, bool refcounted);
17324f0c40d9SWillem de Bruijn static inline int sk_receive_skb(struct sock *sk, struct sk_buff *skb,
17334f0c40d9SWillem de Bruijn 				 const int nested)
17344f0c40d9SWillem de Bruijn {
1735c3f24cfbSEric Dumazet 	return __sk_receive_skb(sk, skb, nested, 1, true);
17364f0c40d9SWillem de Bruijn }
173725995ff5SArnaldo Carvalho de Melo 
1738e022f0b4SKrishna Kumar static inline void sk_tx_queue_set(struct sock *sk, int tx_queue)
1739e022f0b4SKrishna Kumar {
1740755c31cdSAmritha Nambiar 	/* sk_tx_queue_mapping accept only upto a 16-bit value */
1741755c31cdSAmritha Nambiar 	if (WARN_ON_ONCE((unsigned short)tx_queue >= USHRT_MAX))
1742755c31cdSAmritha Nambiar 		return;
1743e022f0b4SKrishna Kumar 	sk->sk_tx_queue_mapping = tx_queue;
1744e022f0b4SKrishna Kumar }
1745e022f0b4SKrishna Kumar 
1746755c31cdSAmritha Nambiar #define NO_QUEUE_MAPPING	USHRT_MAX
1747755c31cdSAmritha Nambiar 
1748e022f0b4SKrishna Kumar static inline void sk_tx_queue_clear(struct sock *sk)
1749e022f0b4SKrishna Kumar {
1750755c31cdSAmritha Nambiar 	sk->sk_tx_queue_mapping = NO_QUEUE_MAPPING;
1751e022f0b4SKrishna Kumar }
1752e022f0b4SKrishna Kumar 
1753e022f0b4SKrishna Kumar static inline int sk_tx_queue_get(const struct sock *sk)
1754e022f0b4SKrishna Kumar {
1755755c31cdSAmritha Nambiar 	if (sk && sk->sk_tx_queue_mapping != NO_QUEUE_MAPPING)
1756755c31cdSAmritha Nambiar 		return sk->sk_tx_queue_mapping;
1757755c31cdSAmritha Nambiar 
1758755c31cdSAmritha Nambiar 	return -1;
1759e022f0b4SKrishna Kumar }
1760e022f0b4SKrishna Kumar 
1761c6345ce7SAmritha Nambiar static inline void sk_rx_queue_set(struct sock *sk, const struct sk_buff *skb)
1762c6345ce7SAmritha Nambiar {
1763c6345ce7SAmritha Nambiar #ifdef CONFIG_XPS
1764c6345ce7SAmritha Nambiar 	if (skb_rx_queue_recorded(skb)) {
1765c6345ce7SAmritha Nambiar 		u16 rx_queue = skb_get_rx_queue(skb);
1766c6345ce7SAmritha Nambiar 
1767c6345ce7SAmritha Nambiar 		if (WARN_ON_ONCE(rx_queue == NO_QUEUE_MAPPING))
1768c6345ce7SAmritha Nambiar 			return;
1769c6345ce7SAmritha Nambiar 
1770c6345ce7SAmritha Nambiar 		sk->sk_rx_queue_mapping = rx_queue;
1771c6345ce7SAmritha Nambiar 	}
1772c6345ce7SAmritha Nambiar #endif
1773c6345ce7SAmritha Nambiar }
1774c6345ce7SAmritha Nambiar 
1775c6345ce7SAmritha Nambiar static inline void sk_rx_queue_clear(struct sock *sk)
1776c6345ce7SAmritha Nambiar {
1777c6345ce7SAmritha Nambiar #ifdef CONFIG_XPS
1778c6345ce7SAmritha Nambiar 	sk->sk_rx_queue_mapping = NO_QUEUE_MAPPING;
1779c6345ce7SAmritha Nambiar #endif
1780c6345ce7SAmritha Nambiar }
1781c6345ce7SAmritha Nambiar 
1782fc9bab24SAmritha Nambiar #ifdef CONFIG_XPS
1783fc9bab24SAmritha Nambiar static inline int sk_rx_queue_get(const struct sock *sk)
1784fc9bab24SAmritha Nambiar {
1785fc9bab24SAmritha Nambiar 	if (sk && sk->sk_rx_queue_mapping != NO_QUEUE_MAPPING)
1786fc9bab24SAmritha Nambiar 		return sk->sk_rx_queue_mapping;
1787fc9bab24SAmritha Nambiar 
1788fc9bab24SAmritha Nambiar 	return -1;
1789fc9bab24SAmritha Nambiar }
1790fc9bab24SAmritha Nambiar #endif
1791fc9bab24SAmritha Nambiar 
1792972692e0SDavid S. Miller static inline void sk_set_socket(struct sock *sk, struct socket *sock)
1793972692e0SDavid S. Miller {
1794e022f0b4SKrishna Kumar 	sk_tx_queue_clear(sk);
1795972692e0SDavid S. Miller 	sk->sk_socket = sock;
1796972692e0SDavid S. Miller }
1797972692e0SDavid S. Miller 
1798aa395145SEric Dumazet static inline wait_queue_head_t *sk_sleep(struct sock *sk)
1799aa395145SEric Dumazet {
1800eaefd110SEric Dumazet 	BUILD_BUG_ON(offsetof(struct socket_wq, wait) != 0);
1801eaefd110SEric Dumazet 	return &rcu_dereference_raw(sk->sk_wq)->wait;
1802aa395145SEric Dumazet }
18031da177e4SLinus Torvalds /* Detach socket from process context.
18041da177e4SLinus Torvalds  * Announce socket dead, detach it from wait queue and inode.
18051da177e4SLinus Torvalds  * Note that parent inode held reference count on this struct sock,
18061da177e4SLinus Torvalds  * we do not release it in this function, because protocol
18071da177e4SLinus Torvalds  * probably wants some additional cleanups or even continuing
18081da177e4SLinus Torvalds  * to work with this socket (TCP).
18091da177e4SLinus Torvalds  */
18101da177e4SLinus Torvalds static inline void sock_orphan(struct sock *sk)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	write_lock_bh(&sk->sk_callback_lock);
18131da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_DEAD);
1814972692e0SDavid S. Miller 	sk_set_socket(sk, NULL);
181543815482SEric Dumazet 	sk->sk_wq  = NULL;
18161da177e4SLinus Torvalds 	write_unlock_bh(&sk->sk_callback_lock);
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds static inline void sock_graft(struct sock *sk, struct socket *parent)
18201da177e4SLinus Torvalds {
18210ffdaf5bSSowmini Varadhan 	WARN_ON(parent->sk);
18221da177e4SLinus Torvalds 	write_lock_bh(&sk->sk_callback_lock);
1823e6476c21SChristoph Hellwig 	rcu_assign_pointer(sk->sk_wq, parent->wq);
18241da177e4SLinus Torvalds 	parent->sk = sk;
1825972692e0SDavid S. Miller 	sk_set_socket(sk, parent);
182686741ec2SLorenzo Colitti 	sk->sk_uid = SOCK_INODE(parent)->i_uid;
18274237c75cSVenkat Yekkirala 	security_sock_graft(sk, parent);
18281da177e4SLinus Torvalds 	write_unlock_bh(&sk->sk_callback_lock);
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
183169336bd2SJoe Perches kuid_t sock_i_uid(struct sock *sk);
183269336bd2SJoe Perches unsigned long sock_i_ino(struct sock *sk);
18331da177e4SLinus Torvalds 
183486741ec2SLorenzo Colitti static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
183586741ec2SLorenzo Colitti {
183686741ec2SLorenzo Colitti 	return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
183786741ec2SLorenzo Colitti }
183886741ec2SLorenzo Colitti 
183958d607d3SEric Dumazet static inline u32 net_tx_rndhash(void)
184058d607d3SEric Dumazet {
184158d607d3SEric Dumazet 	u32 v = prandom_u32();
184258d607d3SEric Dumazet 
184358d607d3SEric Dumazet 	return v ?: 1;
184458d607d3SEric Dumazet }
184558d607d3SEric Dumazet 
1846877d1f62STom Herbert static inline void sk_set_txhash(struct sock *sk)
1847877d1f62STom Herbert {
184858d607d3SEric Dumazet 	sk->sk_txhash = net_tx_rndhash();
1849877d1f62STom Herbert }
1850877d1f62STom Herbert 
1851265f94ffSTom Herbert static inline void sk_rethink_txhash(struct sock *sk)
1852265f94ffSTom Herbert {
1853265f94ffSTom Herbert 	if (sk->sk_txhash)
1854265f94ffSTom Herbert 		sk_set_txhash(sk);
1855265f94ffSTom Herbert }
1856265f94ffSTom Herbert 
18571da177e4SLinus Torvalds static inline struct dst_entry *
18581da177e4SLinus Torvalds __sk_dst_get(struct sock *sk)
18591da177e4SLinus Torvalds {
18601e1d04e6SHannes Frederic Sowa 	return rcu_dereference_check(sk->sk_dst_cache,
18611e1d04e6SHannes Frederic Sowa 				     lockdep_sock_is_held(sk));
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds static inline struct dst_entry *
18651da177e4SLinus Torvalds sk_dst_get(struct sock *sk)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	struct dst_entry *dst;
18681da177e4SLinus Torvalds 
1869b6c6712aSEric Dumazet 	rcu_read_lock();
1870b6c6712aSEric Dumazet 	dst = rcu_dereference(sk->sk_dst_cache);
1871f8864972SEric Dumazet 	if (dst && !atomic_inc_not_zero(&dst->__refcnt))
1872f8864972SEric Dumazet 		dst = NULL;
1873b6c6712aSEric Dumazet 	rcu_read_unlock();
18741da177e4SLinus Torvalds 	return dst;
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
1877b6c6712aSEric Dumazet static inline void dst_negative_advice(struct sock *sk)
1878b6c6712aSEric Dumazet {
1879b6c6712aSEric Dumazet 	struct dst_entry *ndst, *dst = __sk_dst_get(sk);
1880b6c6712aSEric Dumazet 
1881265f94ffSTom Herbert 	sk_rethink_txhash(sk);
1882265f94ffSTom Herbert 
1883b6c6712aSEric Dumazet 	if (dst && dst->ops->negative_advice) {
1884b6c6712aSEric Dumazet 		ndst = dst->ops->negative_advice(dst);
1885b6c6712aSEric Dumazet 
1886b6c6712aSEric Dumazet 		if (ndst != dst) {
1887b6c6712aSEric Dumazet 			rcu_assign_pointer(sk->sk_dst_cache, ndst);
18880a6957e7SZHAO Gang 			sk_tx_queue_clear(sk);
18899b8805a3SJulian Anastasov 			sk->sk_dst_pending_confirm = 0;
1890b6c6712aSEric Dumazet 		}
1891b6c6712aSEric Dumazet 	}
1892b6c6712aSEric Dumazet }
1893b6c6712aSEric Dumazet 
18941da177e4SLinus Torvalds static inline void
18951da177e4SLinus Torvalds __sk_dst_set(struct sock *sk, struct dst_entry *dst)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct dst_entry *old_dst;
18981da177e4SLinus Torvalds 
1899e022f0b4SKrishna Kumar 	sk_tx_queue_clear(sk);
19009b8805a3SJulian Anastasov 	sk->sk_dst_pending_confirm = 0;
190195964c6dSEric Dumazet 	old_dst = rcu_dereference_protected(sk->sk_dst_cache,
190295964c6dSEric Dumazet 					    lockdep_sock_is_held(sk));
1903b6c6712aSEric Dumazet 	rcu_assign_pointer(sk->sk_dst_cache, dst);
19041da177e4SLinus Torvalds 	dst_release(old_dst);
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds static inline void
19081da177e4SLinus Torvalds sk_dst_set(struct sock *sk, struct dst_entry *dst)
19091da177e4SLinus Torvalds {
19107f502361SEric Dumazet 	struct dst_entry *old_dst;
19117f502361SEric Dumazet 
19127f502361SEric Dumazet 	sk_tx_queue_clear(sk);
19139b8805a3SJulian Anastasov 	sk->sk_dst_pending_confirm = 0;
19145925a055SEric Dumazet 	old_dst = xchg((__force struct dst_entry **)&sk->sk_dst_cache, dst);
19157f502361SEric Dumazet 	dst_release(old_dst);
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds static inline void
19191da177e4SLinus Torvalds __sk_dst_reset(struct sock *sk)
19201da177e4SLinus Torvalds {
1921b6c6712aSEric Dumazet 	__sk_dst_set(sk, NULL);
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds static inline void
19251da177e4SLinus Torvalds sk_dst_reset(struct sock *sk)
19261da177e4SLinus Torvalds {
19277f502361SEric Dumazet 	sk_dst_set(sk, NULL);
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
193069336bd2SJoe Perches struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);
19311da177e4SLinus Torvalds 
193269336bd2SJoe Perches struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
19331da177e4SLinus Torvalds 
19349b8805a3SJulian Anastasov static inline void sk_dst_confirm(struct sock *sk)
19359b8805a3SJulian Anastasov {
19369b8805a3SJulian Anastasov 	if (!sk->sk_dst_pending_confirm)
19379b8805a3SJulian Anastasov 		sk->sk_dst_pending_confirm = 1;
19389b8805a3SJulian Anastasov }
19399b8805a3SJulian Anastasov 
19404ff06203SJulian Anastasov static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n)
19414ff06203SJulian Anastasov {
19424ff06203SJulian Anastasov 	if (skb_get_dst_pending_confirm(skb)) {
19434ff06203SJulian Anastasov 		struct sock *sk = skb->sk;
19444ff06203SJulian Anastasov 		unsigned long now = jiffies;
19454ff06203SJulian Anastasov 
19464ff06203SJulian Anastasov 		/* avoid dirtying neighbour */
19474ff06203SJulian Anastasov 		if (n->confirmed != now)
19484ff06203SJulian Anastasov 			n->confirmed = now;
19494ff06203SJulian Anastasov 		if (sk && sk->sk_dst_pending_confirm)
19504ff06203SJulian Anastasov 			sk->sk_dst_pending_confirm = 0;
19514ff06203SJulian Anastasov 	}
19524ff06203SJulian Anastasov }
19534ff06203SJulian Anastasov 
1954f60e5990Shannes@stressinduktion.org bool sk_mc_loop(struct sock *sk);
1955f60e5990Shannes@stressinduktion.org 
1956dc6b9b78SEric Dumazet static inline bool sk_can_gso(const struct sock *sk)
1957bcd76111SHerbert Xu {
1958bcd76111SHerbert Xu 	return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type);
1959bcd76111SHerbert Xu }
1960bcd76111SHerbert Xu 
196169336bd2SJoe Perches void sk_setup_caps(struct sock *sk, struct dst_entry *dst);
19626cbb0df7SArnaldo Carvalho de Melo 
1963c8f44affSMichał Mirosław static inline void sk_nocaps_add(struct sock *sk, netdev_features_t flags)
1964a465419bSEric Dumazet {
1965a465419bSEric Dumazet 	sk->sk_route_nocaps |= flags;
1966a465419bSEric Dumazet 	sk->sk_route_caps &= ~flags;
1967a465419bSEric Dumazet }
1968a465419bSEric Dumazet 
1969c6e1a0d1STom Herbert static inline int skb_do_copy_data_nocache(struct sock *sk, struct sk_buff *skb,
197057be5bdaSAl Viro 					   struct iov_iter *from, char *to,
1971912d398dSWei Yongjun 					   int copy, int offset)
1972c6e1a0d1STom Herbert {
1973c6e1a0d1STom Herbert 	if (skb->ip_summed == CHECKSUM_NONE) {
197457be5bdaSAl Viro 		__wsum csum = 0;
197515e6cb46SAl Viro 		if (!csum_and_copy_from_iter_full(to, copy, &csum, from))
197657be5bdaSAl Viro 			return -EFAULT;
1977912d398dSWei Yongjun 		skb->csum = csum_block_add(skb->csum, csum, offset);
1978c6e1a0d1STom Herbert 	} else if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
197915e6cb46SAl Viro 		if (!copy_from_iter_full_nocache(to, copy, from))
1980c6e1a0d1STom Herbert 			return -EFAULT;
198115e6cb46SAl Viro 	} else if (!copy_from_iter_full(to, copy, from))
1982c6e1a0d1STom Herbert 		return -EFAULT;
1983c6e1a0d1STom Herbert 
1984c6e1a0d1STom Herbert 	return 0;
1985c6e1a0d1STom Herbert }
1986c6e1a0d1STom Herbert 
1987c6e1a0d1STom Herbert static inline int skb_add_data_nocache(struct sock *sk, struct sk_buff *skb,
198857be5bdaSAl Viro 				       struct iov_iter *from, int copy)
1989c6e1a0d1STom Herbert {
1990912d398dSWei Yongjun 	int err, offset = skb->len;
1991c6e1a0d1STom Herbert 
1992912d398dSWei Yongjun 	err = skb_do_copy_data_nocache(sk, skb, from, skb_put(skb, copy),
1993912d398dSWei Yongjun 				       copy, offset);
1994c6e1a0d1STom Herbert 	if (err)
1995912d398dSWei Yongjun 		__skb_trim(skb, offset);
1996c6e1a0d1STom Herbert 
1997c6e1a0d1STom Herbert 	return err;
1998c6e1a0d1STom Herbert }
1999c6e1a0d1STom Herbert 
200057be5bdaSAl Viro static inline int skb_copy_to_page_nocache(struct sock *sk, struct iov_iter *from,
2001c6e1a0d1STom Herbert 					   struct sk_buff *skb,
2002c6e1a0d1STom Herbert 					   struct page *page,
2003c6e1a0d1STom Herbert 					   int off, int copy)
2004c6e1a0d1STom Herbert {
2005c6e1a0d1STom Herbert 	int err;
2006c6e1a0d1STom Herbert 
2007912d398dSWei Yongjun 	err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) + off,
2008912d398dSWei Yongjun 				       copy, skb->len);
2009c6e1a0d1STom Herbert 	if (err)
2010c6e1a0d1STom Herbert 		return err;
2011c6e1a0d1STom Herbert 
2012c6e1a0d1STom Herbert 	skb->len	     += copy;
2013c6e1a0d1STom Herbert 	skb->data_len	     += copy;
2014c6e1a0d1STom Herbert 	skb->truesize	     += copy;
2015c6e1a0d1STom Herbert 	sk->sk_wmem_queued   += copy;
2016c6e1a0d1STom Herbert 	sk_mem_charge(sk, copy);
2017c6e1a0d1STom Herbert 	return 0;
2018c6e1a0d1STom Herbert }
2019c6e1a0d1STom Herbert 
2020c564039fSEric Dumazet /**
2021c564039fSEric Dumazet  * sk_wmem_alloc_get - returns write allocations
2022c564039fSEric Dumazet  * @sk: socket
2023c564039fSEric Dumazet  *
2024c564039fSEric Dumazet  * Returns sk_wmem_alloc minus initial offset of one
2025c564039fSEric Dumazet  */
2026c564039fSEric Dumazet static inline int sk_wmem_alloc_get(const struct sock *sk)
2027c564039fSEric Dumazet {
202814afee4bSReshetova, Elena 	return refcount_read(&sk->sk_wmem_alloc) - 1;
2029c564039fSEric Dumazet }
2030c564039fSEric Dumazet 
2031c564039fSEric Dumazet /**
2032c564039fSEric Dumazet  * sk_rmem_alloc_get - returns read allocations
2033c564039fSEric Dumazet  * @sk: socket
2034c564039fSEric Dumazet  *
2035c564039fSEric Dumazet  * Returns sk_rmem_alloc
2036c564039fSEric Dumazet  */
2037c564039fSEric Dumazet static inline int sk_rmem_alloc_get(const struct sock *sk)
2038c564039fSEric Dumazet {
2039c564039fSEric Dumazet 	return atomic_read(&sk->sk_rmem_alloc);
2040c564039fSEric Dumazet }
2041c564039fSEric Dumazet 
2042c564039fSEric Dumazet /**
2043c564039fSEric Dumazet  * sk_has_allocations - check if allocations are outstanding
2044c564039fSEric Dumazet  * @sk: socket
2045c564039fSEric Dumazet  *
2046c564039fSEric Dumazet  * Returns true if socket has write or read allocations
2047c564039fSEric Dumazet  */
2048dc6b9b78SEric Dumazet static inline bool sk_has_allocations(const struct sock *sk)
2049c564039fSEric Dumazet {
2050c564039fSEric Dumazet 	return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
2051c564039fSEric Dumazet }
2052c564039fSEric Dumazet 
2053a57de0b4SJiri Olsa /**
20541ce0bf50SHerbert Xu  * skwq_has_sleeper - check if there are any waiting processes
2055acfbe96aSRandy Dunlap  * @wq: struct socket_wq
2056a57de0b4SJiri Olsa  *
205743815482SEric Dumazet  * Returns true if socket_wq has waiting processes
2058a57de0b4SJiri Olsa  *
20591ce0bf50SHerbert Xu  * The purpose of the skwq_has_sleeper and sock_poll_wait is to wrap the memory
2060a57de0b4SJiri Olsa  * barrier call. They were added due to the race found within the tcp code.
2061a57de0b4SJiri Olsa  *
2062d651983dSMauro Carvalho Chehab  * Consider following tcp code paths::
2063a57de0b4SJiri Olsa  *
2064a57de0b4SJiri Olsa  *   CPU1                CPU2
2065a57de0b4SJiri Olsa  *   sys_select          receive packet
2066a57de0b4SJiri Olsa  *   ...                 ...
2067a57de0b4SJiri Olsa  *   __add_wait_queue    update tp->rcv_nxt
2068a57de0b4SJiri Olsa  *   ...                 ...
2069a57de0b4SJiri Olsa  *   tp->rcv_nxt check   sock_def_readable
2070a57de0b4SJiri Olsa  *   ...                 {
207143815482SEric Dumazet  *   schedule               rcu_read_lock();
207243815482SEric Dumazet  *                          wq = rcu_dereference(sk->sk_wq);
207343815482SEric Dumazet  *                          if (wq && waitqueue_active(&wq->wait))
207443815482SEric Dumazet  *                              wake_up_interruptible(&wq->wait)
2075a57de0b4SJiri Olsa  *                          ...
2076a57de0b4SJiri Olsa  *                       }
2077a57de0b4SJiri Olsa  *
2078a57de0b4SJiri Olsa  * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
2079a57de0b4SJiri Olsa  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
2080a57de0b4SJiri Olsa  * could then endup calling schedule and sleep forever if there are no more
2081a57de0b4SJiri Olsa  * data on the socket.
2082ad462769SJiri Olsa  *
2083a57de0b4SJiri Olsa  */
20841ce0bf50SHerbert Xu static inline bool skwq_has_sleeper(struct socket_wq *wq)
2085a57de0b4SJiri Olsa {
20861ce0bf50SHerbert Xu 	return wq && wq_has_sleeper(&wq->wait);
2087a57de0b4SJiri Olsa }
2088a57de0b4SJiri Olsa 
2089a57de0b4SJiri Olsa /**
2090a57de0b4SJiri Olsa  * sock_poll_wait - place memory barrier behind the poll_wait call.
2091a57de0b4SJiri Olsa  * @filp:           file
209289ab066dSKarsten Graul  * @sock:           socket to wait on
2093a57de0b4SJiri Olsa  * @p:              poll_table
2094a57de0b4SJiri Olsa  *
209543815482SEric Dumazet  * See the comments in the wq_has_sleeper function.
2096a57de0b4SJiri Olsa  */
209789ab066dSKarsten Graul static inline void sock_poll_wait(struct file *filp, struct socket *sock,
209889ab066dSKarsten Graul 				  poll_table *p)
2099a57de0b4SJiri Olsa {
2100d8bbd13bSChristoph Hellwig 	if (!poll_does_not_wait(p)) {
2101d8bbd13bSChristoph Hellwig 		poll_wait(filp, &sock->wq->wait, p);
2102dc6b9b78SEric Dumazet 		/* We need to be sure we are in sync with the
2103a57de0b4SJiri Olsa 		 * socket flags modification.
2104a57de0b4SJiri Olsa 		 *
210543815482SEric Dumazet 		 * This memory barrier is paired in the wq_has_sleeper.
2106a57de0b4SJiri Olsa 		 */
2107a57de0b4SJiri Olsa 		smp_mb();
2108a57de0b4SJiri Olsa 	}
2109a57de0b4SJiri Olsa }
2110a57de0b4SJiri Olsa 
2111b73c3d0eSTom Herbert static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
2112b73c3d0eSTom Herbert {
2113b73c3d0eSTom Herbert 	if (sk->sk_txhash) {
2114b73c3d0eSTom Herbert 		skb->l4_hash = 1;
2115b73c3d0eSTom Herbert 		skb->hash = sk->sk_txhash;
2116b73c3d0eSTom Herbert 	}
2117b73c3d0eSTom Herbert }
2118b73c3d0eSTom Herbert 
21199e17f8a4SEric Dumazet void skb_set_owner_w(struct sk_buff *skb, struct sock *sk);
21209e17f8a4SEric Dumazet 
21211da177e4SLinus Torvalds /*
21221da177e4SLinus Torvalds  *	Queue a received datagram if it will fit. Stream and sequenced
21231da177e4SLinus Torvalds  *	protocols can't normally use this as they need to fit buffers in
21241da177e4SLinus Torvalds  *	and play with them.
21251da177e4SLinus Torvalds  *
21261da177e4SLinus Torvalds  *	Inlined as it's very short and called for pretty much every
21271da177e4SLinus Torvalds  *	packet ever received.
21281da177e4SLinus Torvalds  */
21291da177e4SLinus Torvalds static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
21301da177e4SLinus Torvalds {
2131d55d87fdSHerbert Xu 	skb_orphan(skb);
21321da177e4SLinus Torvalds 	skb->sk = sk;
21331da177e4SLinus Torvalds 	skb->destructor = sock_rfree;
21341da177e4SLinus Torvalds 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
21353ab224beSHideo Aoki 	sk_mem_charge(sk, skb->truesize);
21361da177e4SLinus Torvalds }
21371da177e4SLinus Torvalds 
213869336bd2SJoe Perches void sk_reset_timer(struct sock *sk, struct timer_list *timer,
21391da177e4SLinus Torvalds 		    unsigned long expires);
21401da177e4SLinus Torvalds 
214169336bd2SJoe Perches void sk_stop_timer(struct sock *sk, struct timer_list *timer);
21421da177e4SLinus Torvalds 
214365101aecSPaolo Abeni int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
214465101aecSPaolo Abeni 			struct sk_buff *skb, unsigned int flags,
214569629464SEric Dumazet 			void (*destructor)(struct sock *sk,
214669629464SEric Dumazet 					   struct sk_buff *skb));
2147e6afc8acSsamanthakumar int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
214869336bd2SJoe Perches int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
21491da177e4SLinus Torvalds 
215069336bd2SJoe Perches int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb);
2151364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk);
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds /*
21541da177e4SLinus Torvalds  *	Recover an error report and clear atomically
21551da177e4SLinus Torvalds  */
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds static inline int sock_error(struct sock *sk)
21581da177e4SLinus Torvalds {
2159c1cbe4b7SBenjamin LaHaise 	int err;
2160c1cbe4b7SBenjamin LaHaise 	if (likely(!sk->sk_err))
2161c1cbe4b7SBenjamin LaHaise 		return 0;
2162c1cbe4b7SBenjamin LaHaise 	err = xchg(&sk->sk_err, 0);
21631da177e4SLinus Torvalds 	return -err;
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds static inline unsigned long sock_wspace(struct sock *sk)
21671da177e4SLinus Torvalds {
21681da177e4SLinus Torvalds 	int amt = 0;
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
217114afee4bSReshetova, Elena 		amt = sk->sk_sndbuf - refcount_read(&sk->sk_wmem_alloc);
21721da177e4SLinus Torvalds 		if (amt < 0)
21731da177e4SLinus Torvalds 			amt = 0;
21741da177e4SLinus Torvalds 	}
21751da177e4SLinus Torvalds 	return amt;
21761da177e4SLinus Torvalds }
21771da177e4SLinus Torvalds 
2178ceb5d58bSEric Dumazet /* Note:
2179ceb5d58bSEric Dumazet  *  We use sk->sk_wq_raw, from contexts knowing this
2180ceb5d58bSEric Dumazet  *  pointer is not NULL and cannot disappear/change.
2181ceb5d58bSEric Dumazet  */
21829cd3e072SEric Dumazet static inline void sk_set_bit(int nr, struct sock *sk)
21831da177e4SLinus Torvalds {
21844be73522SEric Dumazet 	if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) &&
21854be73522SEric Dumazet 	    !sock_flag(sk, SOCK_FASYNC))
21869317bb69SEric Dumazet 		return;
21879317bb69SEric Dumazet 
2188ceb5d58bSEric Dumazet 	set_bit(nr, &sk->sk_wq_raw->flags);
21899cd3e072SEric Dumazet }
21909cd3e072SEric Dumazet 
21919cd3e072SEric Dumazet static inline void sk_clear_bit(int nr, struct sock *sk)
21929cd3e072SEric Dumazet {
21934be73522SEric Dumazet 	if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) &&
21944be73522SEric Dumazet 	    !sock_flag(sk, SOCK_FASYNC))
21959317bb69SEric Dumazet 		return;
21969317bb69SEric Dumazet 
2197ceb5d58bSEric Dumazet 	clear_bit(nr, &sk->sk_wq_raw->flags);
21989cd3e072SEric Dumazet }
21999cd3e072SEric Dumazet 
2200ceb5d58bSEric Dumazet static inline void sk_wake_async(const struct sock *sk, int how, int band)
22011da177e4SLinus Torvalds {
2202ceb5d58bSEric Dumazet 	if (sock_flag(sk, SOCK_FASYNC)) {
2203ceb5d58bSEric Dumazet 		rcu_read_lock();
2204ceb5d58bSEric Dumazet 		sock_wake_async(rcu_dereference(sk->sk_wq), how, band);
2205ceb5d58bSEric Dumazet 		rcu_read_unlock();
2206ceb5d58bSEric Dumazet 	}
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
2209eea86af6SDaniel Borkmann /* Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
2210eea86af6SDaniel Borkmann  * need sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak.
2211eea86af6SDaniel Borkmann  * Note: for send buffers, TCP works better if we can build two skbs at
2212eea86af6SDaniel Borkmann  * minimum.
22137a91b434SEric Dumazet  */
22149eb5bf83SEric Dumazet #define TCP_SKB_MIN_TRUESIZE	(2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
2215eea86af6SDaniel Borkmann 
2216eea86af6SDaniel Borkmann #define SOCK_MIN_SNDBUF		(TCP_SKB_MIN_TRUESIZE * 2)
2217eea86af6SDaniel Borkmann #define SOCK_MIN_RCVBUF		 TCP_SKB_MIN_TRUESIZE
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds static inline void sk_stream_moderate_sndbuf(struct sock *sk)
22201da177e4SLinus Torvalds {
22211da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) {
22228df09ea3SEric Dumazet 		sk->sk_sndbuf = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
2223eea86af6SDaniel Borkmann 		sk->sk_sndbuf = max_t(u32, sk->sk_sndbuf, SOCK_MIN_SNDBUF);
22241da177e4SLinus Torvalds 	}
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
2227eb934478SEric Dumazet struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp,
2228eb934478SEric Dumazet 				    bool force_schedule);
22291da177e4SLinus Torvalds 
22305640f768SEric Dumazet /**
22315640f768SEric Dumazet  * sk_page_frag - return an appropriate page_frag
22325640f768SEric Dumazet  * @sk: socket
22335640f768SEric Dumazet  *
22345640f768SEric Dumazet  * If socket allocation mode allows current thread to sleep, it means its
22355640f768SEric Dumazet  * safe to use the per task page_frag instead of the per socket one.
22365640f768SEric Dumazet  */
22375640f768SEric Dumazet static inline struct page_frag *sk_page_frag(struct sock *sk)
22381da177e4SLinus Torvalds {
2239d0164adcSMel Gorman 	if (gfpflags_allow_blocking(sk->sk_allocation))
22405640f768SEric Dumazet 		return &current->task_frag;
22411da177e4SLinus Torvalds 
22425640f768SEric Dumazet 	return &sk->sk_frag;
22431da177e4SLinus Torvalds }
22445640f768SEric Dumazet 
224569336bd2SJoe Perches bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag);
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds /*
22481da177e4SLinus Torvalds  *	Default write policy as shown to user space via poll/select/SIGIO
22491da177e4SLinus Torvalds  */
2250dc6b9b78SEric Dumazet static inline bool sock_writeable(const struct sock *sk)
22511da177e4SLinus Torvalds {
225214afee4bSReshetova, Elena 	return refcount_read(&sk->sk_wmem_alloc) < (sk->sk_sndbuf >> 1);
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
2255dd0fc66fSAl Viro static inline gfp_t gfp_any(void)
22561da177e4SLinus Torvalds {
225799709372SAndrew Morton 	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
22581da177e4SLinus Torvalds }
22591da177e4SLinus Torvalds 
2260dc6b9b78SEric Dumazet static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
22611da177e4SLinus Torvalds {
22621da177e4SLinus Torvalds 	return noblock ? 0 : sk->sk_rcvtimeo;
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
2265dc6b9b78SEric Dumazet static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
22661da177e4SLinus Torvalds {
22671da177e4SLinus Torvalds 	return noblock ? 0 : sk->sk_sndtimeo;
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
22711da177e4SLinus Torvalds {
22721da177e4SLinus Torvalds 	return (waitall ? len : min_t(int, sk->sk_rcvlowat, len)) ? : 1;
22731da177e4SLinus Torvalds }
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds /* Alas, with timeout socket operations are not restartable.
22761da177e4SLinus Torvalds  * Compare this to poll().
22771da177e4SLinus Torvalds  */
22781da177e4SLinus Torvalds static inline int sock_intr_errno(long timeo)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
2283744d5a3eSEyal Birger struct sock_skb_cb {
2284744d5a3eSEyal Birger 	u32 dropcount;
2285744d5a3eSEyal Birger };
2286744d5a3eSEyal Birger 
2287744d5a3eSEyal Birger /* Store sock_skb_cb at the end of skb->cb[] so protocol families
2288744d5a3eSEyal Birger  * using skb->cb[] would keep using it directly and utilize its
2289744d5a3eSEyal Birger  * alignement guarantee.
2290744d5a3eSEyal Birger  */
2291744d5a3eSEyal Birger #define SOCK_SKB_CB_OFFSET ((FIELD_SIZEOF(struct sk_buff, cb) - \
2292744d5a3eSEyal Birger 			    sizeof(struct sock_skb_cb)))
2293744d5a3eSEyal Birger 
2294744d5a3eSEyal Birger #define SOCK_SKB_CB(__skb) ((struct sock_skb_cb *)((__skb)->cb + \
2295744d5a3eSEyal Birger 			    SOCK_SKB_CB_OFFSET))
2296744d5a3eSEyal Birger 
2297b4772ef8SEyal Birger #define sock_skb_cb_check_size(size) \
2298744d5a3eSEyal Birger 	BUILD_BUG_ON((size) > SOCK_SKB_CB_OFFSET)
2299b4772ef8SEyal Birger 
23003bc3b96fSEyal Birger static inline void
23013bc3b96fSEyal Birger sock_skb_set_dropcount(const struct sock *sk, struct sk_buff *skb)
23023bc3b96fSEyal Birger {
23033665f381SEric Dumazet 	SOCK_SKB_CB(skb)->dropcount = sock_flag(sk, SOCK_RXQ_OVFL) ?
23043665f381SEric Dumazet 						atomic_read(&sk->sk_drops) : 0;
23053bc3b96fSEyal Birger }
23063bc3b96fSEyal Birger 
2307532182cdSEric Dumazet static inline void sk_drops_add(struct sock *sk, const struct sk_buff *skb)
2308532182cdSEric Dumazet {
2309532182cdSEric Dumazet 	int segs = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
2310532182cdSEric Dumazet 
2311532182cdSEric Dumazet 	atomic_add(segs, &sk->sk_drops);
2312532182cdSEric Dumazet }
2313532182cdSEric Dumazet 
23143a0ed3e9SDeepa Dinamani static inline ktime_t sock_read_timestamp(struct sock *sk)
23153a0ed3e9SDeepa Dinamani {
23163a0ed3e9SDeepa Dinamani #if BITS_PER_LONG==32
23173a0ed3e9SDeepa Dinamani 	unsigned int seq;
23183a0ed3e9SDeepa Dinamani 	ktime_t kt;
23193a0ed3e9SDeepa Dinamani 
23203a0ed3e9SDeepa Dinamani 	do {
23213a0ed3e9SDeepa Dinamani 		seq = read_seqbegin(&sk->sk_stamp_seq);
23223a0ed3e9SDeepa Dinamani 		kt = sk->sk_stamp;
23233a0ed3e9SDeepa Dinamani 	} while (read_seqretry(&sk->sk_stamp_seq, seq));
23243a0ed3e9SDeepa Dinamani 
23253a0ed3e9SDeepa Dinamani 	return kt;
23263a0ed3e9SDeepa Dinamani #else
23273a0ed3e9SDeepa Dinamani 	return sk->sk_stamp;
23283a0ed3e9SDeepa Dinamani #endif
23293a0ed3e9SDeepa Dinamani }
23303a0ed3e9SDeepa Dinamani 
23313a0ed3e9SDeepa Dinamani static inline void sock_write_timestamp(struct sock *sk, ktime_t kt)
23323a0ed3e9SDeepa Dinamani {
23333a0ed3e9SDeepa Dinamani #if BITS_PER_LONG==32
23343a0ed3e9SDeepa Dinamani 	write_seqlock(&sk->sk_stamp_seq);
23353a0ed3e9SDeepa Dinamani 	sk->sk_stamp = kt;
23363a0ed3e9SDeepa Dinamani 	write_sequnlock(&sk->sk_stamp_seq);
23373a0ed3e9SDeepa Dinamani #else
23383a0ed3e9SDeepa Dinamani 	sk->sk_stamp = kt;
23393a0ed3e9SDeepa Dinamani #endif
23403a0ed3e9SDeepa Dinamani }
23413a0ed3e9SDeepa Dinamani 
234269336bd2SJoe Perches void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
234392f37fd2SEric Dumazet 			   struct sk_buff *skb);
234469336bd2SJoe Perches void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
23456e3e939fSJohannes Berg 			     struct sk_buff *skb);
234692f37fd2SEric Dumazet 
2347dc6b9b78SEric Dumazet static inline void
23481da177e4SLinus Torvalds sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
23491da177e4SLinus Torvalds {
2350b7aa0bf7SEric Dumazet 	ktime_t kt = skb->tstamp;
235120d49473SPatrick Ohly 	struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
2352a61bbcf2SPatrick McHardy 
235320d49473SPatrick Ohly 	/*
235420d49473SPatrick Ohly 	 * generate control messages if
2355b9f40e21SWillem de Bruijn 	 * - receive time stamping in software requested
235620d49473SPatrick Ohly 	 * - software time stamp available and wanted
235720d49473SPatrick Ohly 	 * - hardware time stamps available and wanted
235820d49473SPatrick Ohly 	 */
235920d49473SPatrick Ohly 	if (sock_flag(sk, SOCK_RCVTSTAMP) ||
2360b9f40e21SWillem de Bruijn 	    (sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) ||
23612456e855SThomas Gleixner 	    (kt && sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) ||
23622456e855SThomas Gleixner 	    (hwtstamps->hwtstamp &&
2363b9f40e21SWillem de Bruijn 	     (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)))
236492f37fd2SEric Dumazet 		__sock_recv_timestamp(msg, sk, skb);
236592f37fd2SEric Dumazet 	else
23663a0ed3e9SDeepa Dinamani 		sock_write_timestamp(sk, kt);
23676e3e939fSJohannes Berg 
23686e3e939fSJohannes Berg 	if (sock_flag(sk, SOCK_WIFI_STATUS) && skb->wifi_acked_valid)
23696e3e939fSJohannes Berg 		__sock_recv_wifi_status(msg, sk, skb);
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds 
237269336bd2SJoe Perches void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
2373767dd033SEric Dumazet 			      struct sk_buff *skb);
2374767dd033SEric Dumazet 
23756c7c98baSPaolo Abeni #define SK_DEFAULT_STAMP (-1L * NSEC_PER_SEC)
2376767dd033SEric Dumazet static inline void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
2377767dd033SEric Dumazet 					  struct sk_buff *skb)
2378767dd033SEric Dumazet {
2379767dd033SEric Dumazet #define FLAGS_TS_OR_DROPS ((1UL << SOCK_RXQ_OVFL)			| \
2380b9f40e21SWillem de Bruijn 			   (1UL << SOCK_RCVTSTAMP))
2381b9f40e21SWillem de Bruijn #define TSFLAGS_ANY	  (SOF_TIMESTAMPING_SOFTWARE			| \
2382b9f40e21SWillem de Bruijn 			   SOF_TIMESTAMPING_RAW_HARDWARE)
2383767dd033SEric Dumazet 
2384b9f40e21SWillem de Bruijn 	if (sk->sk_flags & FLAGS_TS_OR_DROPS || sk->sk_tsflags & TSFLAGS_ANY)
2385767dd033SEric Dumazet 		__sock_recv_ts_and_drops(msg, sk, skb);
2386d3fbff30SEric Dumazet 	else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
23873a0ed3e9SDeepa Dinamani 		sock_write_timestamp(sk, skb->tstamp);
23886c7c98baSPaolo Abeni 	else if (unlikely(sk->sk_stamp == SK_DEFAULT_STAMP))
23893a0ed3e9SDeepa Dinamani 		sock_write_timestamp(sk, 0);
2390767dd033SEric Dumazet }
23913b885787SNeil Horman 
2392c14ac945SSoheil Hassas Yeganeh void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags);
239367cc0d40SWillem de Bruijn 
23941da177e4SLinus Torvalds /**
23958f932f76SWillem de Bruijn  * _sock_tx_timestamp - checks whether the outgoing packet is to be time stamped
239620d49473SPatrick Ohly  * @sk:		socket sending this packet
2397c14ac945SSoheil Hassas Yeganeh  * @tsflags:	timestamping flags to use
2398140c55d4SEric Dumazet  * @tx_flags:	completed with instructions for time stamping
23998f932f76SWillem de Bruijn  * @tskey:      filled in with next sk_tskey (not for TCP, which uses seqno)
2400140c55d4SEric Dumazet  *
2401d651983dSMauro Carvalho Chehab  * Note: callers should take care of initial ``*tx_flags`` value (usually 0)
240220d49473SPatrick Ohly  */
24038f932f76SWillem de Bruijn static inline void _sock_tx_timestamp(struct sock *sk, __u16 tsflags,
24048f932f76SWillem de Bruijn 				      __u8 *tx_flags, __u32 *tskey)
240567cc0d40SWillem de Bruijn {
24068f932f76SWillem de Bruijn 	if (unlikely(tsflags)) {
2407c14ac945SSoheil Hassas Yeganeh 		__sock_tx_timestamp(tsflags, tx_flags);
24088f932f76SWillem de Bruijn 		if (tsflags & SOF_TIMESTAMPING_OPT_ID && tskey &&
24098f932f76SWillem de Bruijn 		    tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK)
24108f932f76SWillem de Bruijn 			*tskey = sk->sk_tskey++;
24118f932f76SWillem de Bruijn 	}
241267cc0d40SWillem de Bruijn 	if (unlikely(sock_flag(sk, SOCK_WIFI_STATUS)))
241367cc0d40SWillem de Bruijn 		*tx_flags |= SKBTX_WIFI_STATUS;
241467cc0d40SWillem de Bruijn }
241520d49473SPatrick Ohly 
24168f932f76SWillem de Bruijn static inline void sock_tx_timestamp(struct sock *sk, __u16 tsflags,
24178f932f76SWillem de Bruijn 				     __u8 *tx_flags)
24188f932f76SWillem de Bruijn {
24198f932f76SWillem de Bruijn 	_sock_tx_timestamp(sk, tsflags, tx_flags, NULL);
24208f932f76SWillem de Bruijn }
24218f932f76SWillem de Bruijn 
24228f932f76SWillem de Bruijn static inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags)
24238f932f76SWillem de Bruijn {
24248f932f76SWillem de Bruijn 	_sock_tx_timestamp(skb->sk, tsflags, &skb_shinfo(skb)->tx_flags,
24258f932f76SWillem de Bruijn 			   &skb_shinfo(skb)->tskey);
24268f932f76SWillem de Bruijn }
24278f932f76SWillem de Bruijn 
242820d49473SPatrick Ohly /**
24291da177e4SLinus Torvalds  * sk_eat_skb - Release a skb if it is no longer needed
24304dc3b16bSPavel Pisa  * @sk: socket to eat this skb from
24314dc3b16bSPavel Pisa  * @skb: socket buffer to eat
24321da177e4SLinus Torvalds  *
24331da177e4SLinus Torvalds  * This routine must be called with interrupts disabled or with the socket
24341da177e4SLinus Torvalds  * locked so that the sk_buff queue operation is ok.
24351da177e4SLinus Torvalds */
24367bced397SDan Williams static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb)
24371da177e4SLinus Torvalds {
24381da177e4SLinus Torvalds 	__skb_unlink(skb, &sk->sk_receive_queue);
24398b27dae5SEric Dumazet 	if (
24408b27dae5SEric Dumazet #ifdef CONFIG_RPS
24418b27dae5SEric Dumazet 	    !static_branch_unlikely(&rps_needed) &&
24428b27dae5SEric Dumazet #endif
24438b27dae5SEric Dumazet 	    !sk->sk_rx_skb_cache) {
24448b27dae5SEric Dumazet 		sk->sk_rx_skb_cache = skb;
24458b27dae5SEric Dumazet 		skb_orphan(skb);
24468b27dae5SEric Dumazet 		return;
24478b27dae5SEric Dumazet 	}
24481da177e4SLinus Torvalds 	__kfree_skb(skb);
24491da177e4SLinus Torvalds }
24501da177e4SLinus Torvalds 
24513b1e0a65SYOSHIFUJI Hideaki static inline
24523b1e0a65SYOSHIFUJI Hideaki struct net *sock_net(const struct sock *sk)
24533b1e0a65SYOSHIFUJI Hideaki {
2454c2d9ba9bSEric Dumazet 	return read_pnet(&sk->sk_net);
24553b1e0a65SYOSHIFUJI Hideaki }
24563b1e0a65SYOSHIFUJI Hideaki 
24573b1e0a65SYOSHIFUJI Hideaki static inline
2458f5aa23fdSDenis V. Lunev void sock_net_set(struct sock *sk, struct net *net)
24593b1e0a65SYOSHIFUJI Hideaki {
2460c2d9ba9bSEric Dumazet 	write_pnet(&sk->sk_net, net);
24613b1e0a65SYOSHIFUJI Hideaki }
24623b1e0a65SYOSHIFUJI Hideaki 
246323542618SKOVACS Krisztian static inline struct sock *skb_steal_sock(struct sk_buff *skb)
246423542618SKOVACS Krisztian {
2465efc27f8cSVijay Subramanian 	if (skb->sk) {
246623542618SKOVACS Krisztian 		struct sock *sk = skb->sk;
246723542618SKOVACS Krisztian 
246823542618SKOVACS Krisztian 		skb->destructor = NULL;
246923542618SKOVACS Krisztian 		skb->sk = NULL;
247023542618SKOVACS Krisztian 		return sk;
247123542618SKOVACS Krisztian 	}
247223542618SKOVACS Krisztian 	return NULL;
247323542618SKOVACS Krisztian }
247423542618SKOVACS Krisztian 
24751d0ab253SEric Dumazet /* This helper checks if a socket is a full socket,
24761d0ab253SEric Dumazet  * ie _not_ a timewait or request socket.
24771d0ab253SEric Dumazet  */
24781d0ab253SEric Dumazet static inline bool sk_fullsock(const struct sock *sk)
24791d0ab253SEric Dumazet {
24801d0ab253SEric Dumazet 	return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV);
24811d0ab253SEric Dumazet }
24821d0ab253SEric Dumazet 
2483ebf4e808SIlya Lesokhin /* Checks if this SKB belongs to an HW offloaded socket
2484ebf4e808SIlya Lesokhin  * and whether any SW fallbacks are required based on dev.
2485ebf4e808SIlya Lesokhin  */
2486ebf4e808SIlya Lesokhin static inline struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
2487ebf4e808SIlya Lesokhin 						   struct net_device *dev)
2488ebf4e808SIlya Lesokhin {
2489ebf4e808SIlya Lesokhin #ifdef CONFIG_SOCK_VALIDATE_XMIT
2490ebf4e808SIlya Lesokhin 	struct sock *sk = skb->sk;
2491ebf4e808SIlya Lesokhin 
2492ebf4e808SIlya Lesokhin 	if (sk && sk_fullsock(sk) && sk->sk_validate_xmit_skb)
2493ebf4e808SIlya Lesokhin 		skb = sk->sk_validate_xmit_skb(sk, dev, skb);
2494ebf4e808SIlya Lesokhin #endif
2495ebf4e808SIlya Lesokhin 
2496ebf4e808SIlya Lesokhin 	return skb;
2497ebf4e808SIlya Lesokhin }
2498ebf4e808SIlya Lesokhin 
2499e446f9dfSEric Dumazet /* This helper checks if a socket is a LISTEN or NEW_SYN_RECV
2500e446f9dfSEric Dumazet  * SYNACK messages can be attached to either ones (depending on SYNCOOKIE)
2501e446f9dfSEric Dumazet  */
2502e446f9dfSEric Dumazet static inline bool sk_listener(const struct sock *sk)
2503e446f9dfSEric Dumazet {
2504e446f9dfSEric Dumazet 	return (1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV);
2505e446f9dfSEric Dumazet }
2506e446f9dfSEric Dumazet 
250769336bd2SJoe Perches void sock_enable_timestamp(struct sock *sk, int flag);
250869336bd2SJoe Perches int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
250969336bd2SJoe Perches 		       int type);
25101da177e4SLinus Torvalds 
2511a3b299daSEric W. Biederman bool sk_ns_capable(const struct sock *sk,
2512a3b299daSEric W. Biederman 		   struct user_namespace *user_ns, int cap);
2513a3b299daSEric W. Biederman bool sk_capable(const struct sock *sk, int cap);
2514a3b299daSEric W. Biederman bool sk_net_capable(const struct sock *sk, int cap);
2515a3b299daSEric W. Biederman 
2516a2d133b1SJosh Hunt void sk_get_meminfo(const struct sock *sk, u32 *meminfo);
2517a2d133b1SJosh Hunt 
2518eaa72dc4SEric Dumazet /* Take into consideration the size of the struct sk_buff overhead in the
2519eaa72dc4SEric Dumazet  * determination of these values, since that is non-constant across
2520eaa72dc4SEric Dumazet  * platforms.  This makes socket queueing behavior and performance
2521eaa72dc4SEric Dumazet  * not depend upon such differences.
2522eaa72dc4SEric Dumazet  */
2523eaa72dc4SEric Dumazet #define _SK_MEM_PACKETS		256
2524eaa72dc4SEric Dumazet #define _SK_MEM_OVERHEAD	SKB_TRUESIZE(256)
2525eaa72dc4SEric Dumazet #define SK_WMEM_MAX		(_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
2526eaa72dc4SEric Dumazet #define SK_RMEM_MAX		(_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
2527eaa72dc4SEric Dumazet 
25281da177e4SLinus Torvalds extern __u32 sysctl_wmem_max;
25291da177e4SLinus Torvalds extern __u32 sysctl_rmem_max;
25301da177e4SLinus Torvalds 
2531b245be1fSWillem de Bruijn extern int sysctl_tstamp_allow_data;
25326baf1f41SDavid S. Miller extern int sysctl_optmem_max;
25336baf1f41SDavid S. Miller 
253420380731SArnaldo Carvalho de Melo extern __u32 sysctl_wmem_default;
253520380731SArnaldo Carvalho de Melo extern __u32 sysctl_rmem_default;
253620380731SArnaldo Carvalho de Melo 
2537a3dcaf17SEric Dumazet static inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto)
2538a3dcaf17SEric Dumazet {
2539a3dcaf17SEric Dumazet 	/* Does this proto have per netns sysctl_wmem ? */
2540a3dcaf17SEric Dumazet 	if (proto->sysctl_wmem_offset)
2541a3dcaf17SEric Dumazet 		return *(int *)((void *)sock_net(sk) + proto->sysctl_wmem_offset);
2542a3dcaf17SEric Dumazet 
2543a3dcaf17SEric Dumazet 	return *proto->sysctl_wmem;
2544a3dcaf17SEric Dumazet }
2545a3dcaf17SEric Dumazet 
2546a3dcaf17SEric Dumazet static inline int sk_get_rmem0(const struct sock *sk, const struct proto *proto)
2547a3dcaf17SEric Dumazet {
2548a3dcaf17SEric Dumazet 	/* Does this proto have per netns sysctl_rmem ? */
2549a3dcaf17SEric Dumazet 	if (proto->sysctl_rmem_offset)
2550a3dcaf17SEric Dumazet 		return *(int *)((void *)sock_net(sk) + proto->sysctl_rmem_offset);
2551a3dcaf17SEric Dumazet 
2552a3dcaf17SEric Dumazet 	return *proto->sysctl_rmem;
2553a3dcaf17SEric Dumazet }
2554a3dcaf17SEric Dumazet 
2555c9f1f58dSEric Dumazet /* Default TCP Small queue budget is ~1 ms of data (1sec >> 10)
2556c9f1f58dSEric Dumazet  * Some wifi drivers need to tweak it to get more chunks.
2557c9f1f58dSEric Dumazet  * They can use this helper from their ndo_start_xmit()
2558c9f1f58dSEric Dumazet  */
2559c9f1f58dSEric Dumazet static inline void sk_pacing_shift_update(struct sock *sk, int val)
2560c9f1f58dSEric Dumazet {
2561c9f1f58dSEric Dumazet 	if (!sk || !sk_fullsock(sk) || sk->sk_pacing_shift == val)
2562c9f1f58dSEric Dumazet 		return;
2563c9f1f58dSEric Dumazet 	sk->sk_pacing_shift = val;
2564c9f1f58dSEric Dumazet }
2565c9f1f58dSEric Dumazet 
256654dc3e33SDavid Ahern /* if a socket is bound to a device, check that the given device
256754dc3e33SDavid Ahern  * index is either the same or that the socket is bound to an L3
256854dc3e33SDavid Ahern  * master device and the given device index is also enslaved to
256954dc3e33SDavid Ahern  * that L3 master
257054dc3e33SDavid Ahern  */
257154dc3e33SDavid Ahern static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
257254dc3e33SDavid Ahern {
257354dc3e33SDavid Ahern 	int mdif;
257454dc3e33SDavid Ahern 
257554dc3e33SDavid Ahern 	if (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif)
257654dc3e33SDavid Ahern 		return true;
257754dc3e33SDavid Ahern 
257854dc3e33SDavid Ahern 	mdif = l3mdev_master_ifindex_by_index(sock_net(sk), dif);
257954dc3e33SDavid Ahern 	if (mdif && mdif == sk->sk_bound_dev_if)
258054dc3e33SDavid Ahern 		return true;
258154dc3e33SDavid Ahern 
258254dc3e33SDavid Ahern 	return false;
258354dc3e33SDavid Ahern }
258454dc3e33SDavid Ahern 
25851da177e4SLinus Torvalds #endif	/* _SOCK_H */
2586