xref: /linux/include/net/sock.h (revision 7ebdfaa52d15b947503f76474477f92854796d96)
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/aio.h>
6140401530SAl Viro #include <linux/sched.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #include <linux/filter.h>
6488ab1932SEric Dumazet #include <linux/rculist_nulls.h>
65a57de0b4SJiri Olsa #include <linux/poll.h>
661da177e4SLinus Torvalds 
67c31504dcSEric Dumazet #include <linux/atomic.h>
681da177e4SLinus Torvalds #include <net/dst.h>
691da177e4SLinus Torvalds #include <net/checksum.h>
70b9f40e21SWillem de Bruijn #include <linux/net_tstamp.h>
711da177e4SLinus Torvalds 
729f048bfbSEric Dumazet struct cgroup;
739f048bfbSEric Dumazet struct cgroup_subsys;
74c607b2edSGlauber Costa #ifdef CONFIG_NET
751d62e436SGlauber Costa int mem_cgroup_sockets_init(struct mem_cgroup *memcg, struct cgroup_subsys *ss);
761d62e436SGlauber Costa void mem_cgroup_sockets_destroy(struct mem_cgroup *memcg);
77c607b2edSGlauber Costa #else
78c607b2edSGlauber Costa static inline
791d62e436SGlauber Costa int mem_cgroup_sockets_init(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
80c607b2edSGlauber Costa {
81c607b2edSGlauber Costa 	return 0;
82c607b2edSGlauber Costa }
83c607b2edSGlauber Costa static inline
841d62e436SGlauber Costa void mem_cgroup_sockets_destroy(struct mem_cgroup *memcg)
85c607b2edSGlauber Costa {
86c607b2edSGlauber Costa }
87c607b2edSGlauber Costa #endif
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * This structure really needs to be cleaned up.
901da177e4SLinus Torvalds  * Most of it is for TCP, and not used by any of
911da177e4SLinus Torvalds  * the other protocols.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Define this to get the SOCK_DBG debugging facility. */
951da177e4SLinus Torvalds #define SOCK_DEBUGGING
961da177e4SLinus Torvalds #ifdef SOCK_DEBUGGING
971da177e4SLinus Torvalds #define SOCK_DEBUG(sk, msg...) do { if ((sk) && sock_flag((sk), SOCK_DBG)) \
981da177e4SLinus Torvalds 					printk(KERN_DEBUG msg); } while (0)
991da177e4SLinus Torvalds #else
1004cd9029dSStephen Hemminger /* Validate arguments and do nothing */
101b9075fa9SJoe Perches static inline __printf(2, 3)
102dc6b9b78SEric Dumazet void SOCK_DEBUG(const struct sock *sk, const char *msg, ...)
1034cd9029dSStephen Hemminger {
1044cd9029dSStephen Hemminger }
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /* This is the per-socket lock.  The spinlock provides a synchronization
1081da177e4SLinus Torvalds  * between user contexts and software interrupt processing, whereas the
1091da177e4SLinus Torvalds  * mini-semaphore synchronizes multiple users amongst themselves.
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds typedef struct {
1121da177e4SLinus Torvalds 	spinlock_t		slock;
113d2e9117cSJohn Heffner 	int			owned;
1141da177e4SLinus Torvalds 	wait_queue_head_t	wq;
115a5b5bb9aSIngo Molnar 	/*
116a5b5bb9aSIngo Molnar 	 * We express the mutex-alike socket_lock semantics
117a5b5bb9aSIngo Molnar 	 * to the lock validator by explicitly managing
118a5b5bb9aSIngo Molnar 	 * the slock as a lock variant (in addition to
119a5b5bb9aSIngo Molnar 	 * the slock itself):
120a5b5bb9aSIngo Molnar 	 */
121a5b5bb9aSIngo Molnar #ifdef CONFIG_DEBUG_LOCK_ALLOC
122a5b5bb9aSIngo Molnar 	struct lockdep_map dep_map;
123a5b5bb9aSIngo Molnar #endif
1241da177e4SLinus Torvalds } socket_lock_t;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds struct sock;
1278feaf0c0SArnaldo Carvalho de Melo struct proto;
1280eeb8ffcSDenis V. Lunev struct net;
1291da177e4SLinus Torvalds 
130077b393dSEric Dumazet typedef __u32 __bitwise __portpair;
131077b393dSEric Dumazet typedef __u64 __bitwise __addrpair;
132077b393dSEric Dumazet 
1331da177e4SLinus Torvalds /**
1341da177e4SLinus Torvalds  *	struct sock_common - minimal network layer representation of sockets
13568835abaSEric Dumazet  *	@skc_daddr: Foreign IPv4 addr
13668835abaSEric Dumazet  *	@skc_rcv_saddr: Bound local IPv4 addr
1374dc6dc71SEric Dumazet  *	@skc_hash: hash value used with various protocol lookup tables
138d4cada4aSEric Dumazet  *	@skc_u16hashes: two u16 hash values used by UDP lookup tables
139ce43b03eSEric Dumazet  *	@skc_dport: placeholder for inet_dport/tw_dport
140ce43b03eSEric Dumazet  *	@skc_num: placeholder for inet_num/tw_num
1414dc3b16bSPavel Pisa  *	@skc_family: network address family
1424dc3b16bSPavel Pisa  *	@skc_state: Connection state
1434dc3b16bSPavel Pisa  *	@skc_reuse: %SO_REUSEADDR setting
144055dc21aSTom Herbert  *	@skc_reuseport: %SO_REUSEPORT setting
1454dc3b16bSPavel Pisa  *	@skc_bound_dev_if: bound device index if != 0
1464dc3b16bSPavel Pisa  *	@skc_bind_node: bind hash linkage for various protocol lookup tables
147512615b6SEric Dumazet  *	@skc_portaddr_node: second hash linkage for UDP/UDP-Lite protocol
1488feaf0c0SArnaldo Carvalho de Melo  *	@skc_prot: protocol handlers inside a network family
14907feaebfSEric W. Biederman  *	@skc_net: reference to the network namespace of this socket
15068835abaSEric Dumazet  *	@skc_node: main hash linkage for various protocol lookup tables
15168835abaSEric Dumazet  *	@skc_nulls_node: main hash linkage for TCP/UDP/UDP-Lite protocol
15268835abaSEric Dumazet  *	@skc_tx_queue_mapping: tx queue number for this connection
15368835abaSEric Dumazet  *	@skc_refcnt: reference count
1541da177e4SLinus Torvalds  *
1551da177e4SLinus Torvalds  *	This is the minimal network layer representation of sockets, the header
1568feaf0c0SArnaldo Carvalho de Melo  *	for struct sock and struct inet_timewait_sock.
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds struct sock_common {
159ce43b03eSEric Dumazet 	/* skc_daddr and skc_rcv_saddr must be grouped on a 8 bytes aligned
16005dbc7b5SEric Dumazet 	 * address on 64bit arches : cf INET_MATCH()
1614dc6dc71SEric Dumazet 	 */
162ce43b03eSEric Dumazet 	union {
163077b393dSEric Dumazet 		__addrpair	skc_addrpair;
164ce43b03eSEric Dumazet 		struct {
16568835abaSEric Dumazet 			__be32	skc_daddr;
16668835abaSEric Dumazet 			__be32	skc_rcv_saddr;
167ce43b03eSEric Dumazet 		};
168ce43b03eSEric Dumazet 	};
169d4cada4aSEric Dumazet 	union  {
17081c3d547SEric Dumazet 		unsigned int	skc_hash;
171d4cada4aSEric Dumazet 		__u16		skc_u16hashes[2];
172d4cada4aSEric Dumazet 	};
173ce43b03eSEric Dumazet 	/* skc_dport && skc_num must be grouped as well */
174ce43b03eSEric Dumazet 	union {
175077b393dSEric Dumazet 		__portpair	skc_portpair;
176ce43b03eSEric Dumazet 		struct {
177ce43b03eSEric Dumazet 			__be16	skc_dport;
178ce43b03eSEric Dumazet 			__u16	skc_num;
179ce43b03eSEric Dumazet 		};
180ce43b03eSEric Dumazet 	};
181ce43b03eSEric Dumazet 
1824dc6dc71SEric Dumazet 	unsigned short		skc_family;
1834dc6dc71SEric Dumazet 	volatile unsigned char	skc_state;
184055dc21aSTom Herbert 	unsigned char		skc_reuse:4;
1859fe516baSEric Dumazet 	unsigned char		skc_reuseport:1;
1869fe516baSEric Dumazet 	unsigned char		skc_ipv6only:1;
1874dc6dc71SEric Dumazet 	int			skc_bound_dev_if;
188512615b6SEric Dumazet 	union {
1894dc6dc71SEric Dumazet 		struct hlist_node	skc_bind_node;
190512615b6SEric Dumazet 		struct hlist_nulls_node skc_portaddr_node;
191512615b6SEric Dumazet 	};
1928feaf0c0SArnaldo Carvalho de Melo 	struct proto		*skc_prot;
1933b1e0a65SYOSHIFUJI Hideaki #ifdef CONFIG_NET_NS
19407feaebfSEric W. Biederman 	struct net	 	*skc_net;
1953b1e0a65SYOSHIFUJI Hideaki #endif
196efe4208fSEric Dumazet 
197efe4208fSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
198efe4208fSEric Dumazet 	struct in6_addr		skc_v6_daddr;
199efe4208fSEric Dumazet 	struct in6_addr		skc_v6_rcv_saddr;
200efe4208fSEric Dumazet #endif
201efe4208fSEric Dumazet 
20268835abaSEric Dumazet 	/*
20368835abaSEric Dumazet 	 * fields between dontcopy_begin/dontcopy_end
20468835abaSEric Dumazet 	 * are not copied in sock_copy()
20568835abaSEric Dumazet 	 */
206928c41e7SRandy Dunlap 	/* private: */
20768835abaSEric Dumazet 	int			skc_dontcopy_begin[0];
208928c41e7SRandy Dunlap 	/* public: */
20968835abaSEric Dumazet 	union {
21068835abaSEric Dumazet 		struct hlist_node	skc_node;
21168835abaSEric Dumazet 		struct hlist_nulls_node skc_nulls_node;
21268835abaSEric Dumazet 	};
21368835abaSEric Dumazet 	int			skc_tx_queue_mapping;
21468835abaSEric Dumazet 	atomic_t		skc_refcnt;
215928c41e7SRandy Dunlap 	/* private: */
21668835abaSEric Dumazet 	int                     skc_dontcopy_end[0];
217928c41e7SRandy Dunlap 	/* public: */
2181da177e4SLinus Torvalds };
2191da177e4SLinus Torvalds 
220e1aab161SGlauber Costa struct cg_proto;
2211da177e4SLinus Torvalds /**
2221da177e4SLinus Torvalds   *	struct sock - network layer representation of sockets
2238feaf0c0SArnaldo Carvalho de Melo   *	@__sk_common: shared layout with inet_timewait_sock
2244dc3b16bSPavel Pisa   *	@sk_shutdown: mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN
2254dc3b16bSPavel Pisa   *	@sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings
2264dc3b16bSPavel Pisa   *	@sk_lock:	synchronizer
2274dc3b16bSPavel Pisa   *	@sk_rcvbuf: size of receive buffer in bytes
22843815482SEric Dumazet   *	@sk_wq: sock wait queue and async head
229421b3885SShawn Bohrer   *	@sk_rx_dst: receive input route used by early demux
2304dc3b16bSPavel Pisa   *	@sk_dst_cache: destination cache
2314dc3b16bSPavel Pisa   *	@sk_dst_lock: destination cache lock
2324dc3b16bSPavel Pisa   *	@sk_policy: flow policy
2334dc3b16bSPavel Pisa   *	@sk_receive_queue: incoming packets
2344dc3b16bSPavel Pisa   *	@sk_wmem_alloc: transmit queue bytes committed
2354dc3b16bSPavel Pisa   *	@sk_write_queue: Packet sending queue
2364dc3b16bSPavel Pisa   *	@sk_omem_alloc: "o" is "option" or "other"
2374dc3b16bSPavel Pisa   *	@sk_wmem_queued: persistent queue size
2384dc3b16bSPavel Pisa   *	@sk_forward_alloc: space allocated forward
23906021292SEliezer Tamir   *	@sk_napi_id: id of the last napi context to receive data for sk
240dafcc438SEliezer Tamir   *	@sk_ll_usec: usecs to busypoll when there is no data
2414dc3b16bSPavel Pisa   *	@sk_allocation: allocation mode
24295bd09ebSEric Dumazet   *	@sk_pacing_rate: Pacing rate (if supported by transport/packet scheduler)
243c3f40d7cSEric Dumazet   *	@sk_max_pacing_rate: Maximum pacing rate (%SO_MAX_PACING_RATE)
2444dc3b16bSPavel Pisa   *	@sk_sndbuf: size of send buffer in bytes
24533c732c3SWang Chen   *	@sk_flags: %SO_LINGER (l_onoff), %SO_BROADCAST, %SO_KEEPALIVE,
24620d49473SPatrick Ohly   *		   %SO_OOBINLINE settings, %SO_TIMESTAMPING settings
24728448b80STom Herbert   *	@sk_no_check_tx: %SO_NO_CHECK setting, set checksum in TX packets
24828448b80STom Herbert   *	@sk_no_check_rx: allow zero checksum in RX packets
2494dc3b16bSPavel Pisa   *	@sk_route_caps: route capabilities (e.g. %NETIF_F_TSO)
250a465419bSEric Dumazet   *	@sk_route_nocaps: forbidden route capabilities (e.g NETIF_F_GSO_MASK)
251bcd76111SHerbert Xu   *	@sk_gso_type: GSO type (e.g. %SKB_GSO_TCPV4)
25282cc1a7aSPeter P Waskiewicz Jr   *	@sk_gso_max_size: Maximum GSO segment size to build
2531485348dSBen Hutchings   *	@sk_gso_max_segs: Maximum number of GSO segments
2544dc3b16bSPavel Pisa   *	@sk_lingertime: %SO_LINGER l_linger setting
2554dc3b16bSPavel Pisa   *	@sk_backlog: always used with the per-socket spinlock held
2564dc3b16bSPavel Pisa   *	@sk_callback_lock: used with the callbacks in the end of this struct
2574dc3b16bSPavel Pisa   *	@sk_error_queue: rarely used
25833c732c3SWang Chen   *	@sk_prot_creator: sk_prot of original sock creator (see ipv6_setsockopt,
25933c732c3SWang Chen   *			  IPV6_ADDRFORM for instance)
2604dc3b16bSPavel Pisa   *	@sk_err: last error
26133c732c3SWang Chen   *	@sk_err_soft: errors that don't cause failure but are the cause of a
26233c732c3SWang Chen   *		      persistent failure not just 'timed out'
263cb61cb9bSEric Dumazet   *	@sk_drops: raw/udp drops counter
2644dc3b16bSPavel Pisa   *	@sk_ack_backlog: current listen backlog
2654dc3b16bSPavel Pisa   *	@sk_max_ack_backlog: listen backlog set in listen()
2664dc3b16bSPavel Pisa   *	@sk_priority: %SO_PRIORITY setting
2671a3bc369SRandy Dunlap   *	@sk_cgrp_prioidx: socket group's priority map index
2684dc3b16bSPavel Pisa   *	@sk_type: socket type (%SOCK_STREAM, etc)
2694dc3b16bSPavel Pisa   *	@sk_protocol: which protocol this socket belongs in this network family
27053c3fa20SRandy Dunlap   *	@sk_peer_pid: &struct pid for this socket's peer
27153c3fa20SRandy Dunlap   *	@sk_peer_cred: %SO_PEERCRED setting
2724dc3b16bSPavel Pisa   *	@sk_rcvlowat: %SO_RCVLOWAT setting
2734dc3b16bSPavel Pisa   *	@sk_rcvtimeo: %SO_RCVTIMEO setting
2744dc3b16bSPavel Pisa   *	@sk_sndtimeo: %SO_SNDTIMEO setting
275c58dc01bSDavid S. Miller   *	@sk_rxhash: flow hash received from netif layer
2762c8c56e1SEric Dumazet   *	@sk_incoming_cpu: record cpu processing incoming packets
277b73c3d0eSTom Herbert   *	@sk_txhash: computed flow hash for use on transmit
2784dc3b16bSPavel Pisa   *	@sk_filter: socket filtering instructions
2794dc3b16bSPavel Pisa   *	@sk_protinfo: private area, net family specific, when not using slab
2804dc3b16bSPavel Pisa   *	@sk_timer: sock cleanup timer
2814dc3b16bSPavel Pisa   *	@sk_stamp: time stamp of last packet received
282b9f40e21SWillem de Bruijn   *	@sk_tsflags: SO_TIMESTAMPING socket options
28309c2d251SWillem de Bruijn   *	@sk_tskey: counter to disambiguate concurrent tstamp requests
2844dc3b16bSPavel Pisa   *	@sk_socket: Identd and reporting IO signals
2854dc3b16bSPavel Pisa   *	@sk_user_data: RPC layer private data
2865640f768SEric Dumazet   *	@sk_frag: cached page frag
287d3d4f0a0SRandy Dunlap   *	@sk_peek_off: current peek_offset value
2884dc3b16bSPavel Pisa   *	@sk_send_head: front of stuff to transmit
28967be2dd1SMartin Waitz   *	@sk_security: used by security modules
29031729363SRandy Dunlap   *	@sk_mark: generic packet mark
29153c3fa20SRandy Dunlap   *	@sk_classid: this socket's cgroup classid
292e1aab161SGlauber Costa   *	@sk_cgrp: this socket's cgroup-specific proto data
2934dc3b16bSPavel Pisa   *	@sk_write_pending: a write to stream socket waits to start
2944dc3b16bSPavel Pisa   *	@sk_state_change: callback to indicate change in the state of the sock
2954dc3b16bSPavel Pisa   *	@sk_data_ready: callback to indicate there is data to be processed
2964dc3b16bSPavel Pisa   *	@sk_write_space: callback to indicate there is bf sending space available
2974dc3b16bSPavel Pisa   *	@sk_error_report: callback to indicate errors (e.g. %MSG_ERRQUEUE)
2984dc3b16bSPavel Pisa   *	@sk_backlog_rcv: callback to process the backlog
2994dc3b16bSPavel Pisa   *	@sk_destruct: called at sock freeing time, i.e. when all refcnt == 0
3001da177e4SLinus Torvalds  */
3011da177e4SLinus Torvalds struct sock {
3021da177e4SLinus Torvalds 	/*
3038feaf0c0SArnaldo Carvalho de Melo 	 * Now struct inet_timewait_sock also uses sock_common, so please just
3041da177e4SLinus Torvalds 	 * don't add nothing before this first member (__sk_common) --acme
3051da177e4SLinus Torvalds 	 */
3061da177e4SLinus Torvalds 	struct sock_common	__sk_common;
3074dc6dc71SEric Dumazet #define sk_node			__sk_common.skc_node
3084dc6dc71SEric Dumazet #define sk_nulls_node		__sk_common.skc_nulls_node
3094dc6dc71SEric Dumazet #define sk_refcnt		__sk_common.skc_refcnt
310e022f0b4SKrishna Kumar #define sk_tx_queue_mapping	__sk_common.skc_tx_queue_mapping
3114dc6dc71SEric Dumazet 
31268835abaSEric Dumazet #define sk_dontcopy_begin	__sk_common.skc_dontcopy_begin
31368835abaSEric Dumazet #define sk_dontcopy_end		__sk_common.skc_dontcopy_end
3144dc6dc71SEric Dumazet #define sk_hash			__sk_common.skc_hash
31550805466SEric Dumazet #define sk_portpair		__sk_common.skc_portpair
31605dbc7b5SEric Dumazet #define sk_num			__sk_common.skc_num
31705dbc7b5SEric Dumazet #define sk_dport		__sk_common.skc_dport
31850805466SEric Dumazet #define sk_addrpair		__sk_common.skc_addrpair
31950805466SEric Dumazet #define sk_daddr		__sk_common.skc_daddr
32050805466SEric Dumazet #define sk_rcv_saddr		__sk_common.skc_rcv_saddr
3211da177e4SLinus Torvalds #define sk_family		__sk_common.skc_family
3221da177e4SLinus Torvalds #define sk_state		__sk_common.skc_state
3231da177e4SLinus Torvalds #define sk_reuse		__sk_common.skc_reuse
324055dc21aSTom Herbert #define sk_reuseport		__sk_common.skc_reuseport
3259fe516baSEric Dumazet #define sk_ipv6only		__sk_common.skc_ipv6only
3261da177e4SLinus Torvalds #define sk_bound_dev_if		__sk_common.skc_bound_dev_if
3271da177e4SLinus Torvalds #define sk_bind_node		__sk_common.skc_bind_node
3288feaf0c0SArnaldo Carvalho de Melo #define sk_prot			__sk_common.skc_prot
32907feaebfSEric W. Biederman #define sk_net			__sk_common.skc_net
330efe4208fSEric Dumazet #define sk_v6_daddr		__sk_common.skc_v6_daddr
331efe4208fSEric Dumazet #define sk_v6_rcv_saddr	__sk_common.skc_v6_rcv_saddr
332efe4208fSEric Dumazet 
333b178bb3dSEric Dumazet 	socket_lock_t		sk_lock;
334b178bb3dSEric Dumazet 	struct sk_buff_head	sk_receive_queue;
335b178bb3dSEric Dumazet 	/*
336b178bb3dSEric Dumazet 	 * The backlog queue is special, it is always used with
337b178bb3dSEric Dumazet 	 * the per-socket spinlock held and requires low latency
338b178bb3dSEric Dumazet 	 * access. Therefore we special case it's implementation.
339b178bb3dSEric Dumazet 	 * Note : rmem_alloc is in this structure to fill a hole
340b178bb3dSEric Dumazet 	 * on 64bit arches, not because its logically part of
341b178bb3dSEric Dumazet 	 * backlog.
342b178bb3dSEric Dumazet 	 */
343b178bb3dSEric Dumazet 	struct {
344b178bb3dSEric Dumazet 		atomic_t	rmem_alloc;
345b178bb3dSEric Dumazet 		int		len;
346b178bb3dSEric Dumazet 		struct sk_buff	*head;
347b178bb3dSEric Dumazet 		struct sk_buff	*tail;
348b178bb3dSEric Dumazet 	} sk_backlog;
349b178bb3dSEric Dumazet #define sk_rmem_alloc sk_backlog.rmem_alloc
350b178bb3dSEric Dumazet 	int			sk_forward_alloc;
351b178bb3dSEric Dumazet #ifdef CONFIG_RPS
352b178bb3dSEric Dumazet 	__u32			sk_rxhash;
353b178bb3dSEric Dumazet #endif
3542c8c56e1SEric Dumazet 	u16			sk_incoming_cpu;
3552c8c56e1SEric Dumazet 	/* 16bit hole
3562c8c56e1SEric Dumazet 	 * Warned : sk_incoming_cpu can be set from softirq,
3572c8c56e1SEric Dumazet 	 * Do not use this hole without fully understanding possible issues.
3582c8c56e1SEric Dumazet 	 */
3592c8c56e1SEric Dumazet 
360b73c3d0eSTom Herbert 	__u32			sk_txhash;
361e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
36206021292SEliezer Tamir 	unsigned int		sk_napi_id;
363dafcc438SEliezer Tamir 	unsigned int		sk_ll_usec;
36406021292SEliezer Tamir #endif
365b178bb3dSEric Dumazet 	atomic_t		sk_drops;
366b178bb3dSEric Dumazet 	int			sk_rcvbuf;
367b178bb3dSEric Dumazet 
368b178bb3dSEric Dumazet 	struct sk_filter __rcu	*sk_filter;
369eaefd110SEric Dumazet 	struct socket_wq __rcu	*sk_wq;
370b178bb3dSEric Dumazet 
371b178bb3dSEric Dumazet #ifdef CONFIG_XFRM
372b178bb3dSEric Dumazet 	struct xfrm_policy	*sk_policy[2];
373b178bb3dSEric Dumazet #endif
374b178bb3dSEric Dumazet 	unsigned long 		sk_flags;
375deaa5854SEric Dumazet 	struct dst_entry	*sk_rx_dst;
3760e36cbb3SCong Wang 	struct dst_entry __rcu	*sk_dst_cache;
377b178bb3dSEric Dumazet 	spinlock_t		sk_dst_lock;
378b178bb3dSEric Dumazet 	atomic_t		sk_wmem_alloc;
379b178bb3dSEric Dumazet 	atomic_t		sk_omem_alloc;
380b178bb3dSEric Dumazet 	int			sk_sndbuf;
381b178bb3dSEric Dumazet 	struct sk_buff_head	sk_write_queue;
382a98b65a3SVegard Nossum 	kmemcheck_bitfield_begin(flags);
3835fdb9973SEric Dumazet 	unsigned int		sk_shutdown  : 2,
38428448b80STom Herbert 				sk_no_check_tx : 1,
38528448b80STom Herbert 				sk_no_check_rx : 1,
3865fdb9973SEric Dumazet 				sk_userlocks : 4,
3875fdb9973SEric Dumazet 				sk_protocol  : 8,
3885fdb9973SEric Dumazet 				sk_type      : 16;
389a98b65a3SVegard Nossum 	kmemcheck_bitfield_end(flags);
3901da177e4SLinus Torvalds 	int			sk_wmem_queued;
3917d877f3bSAl Viro 	gfp_t			sk_allocation;
39295bd09ebSEric Dumazet 	u32			sk_pacing_rate; /* bytes per second */
39362748f32SEric Dumazet 	u32			sk_max_pacing_rate;
394c8f44affSMichał Mirosław 	netdev_features_t	sk_route_caps;
395c8f44affSMichał Mirosław 	netdev_features_t	sk_route_nocaps;
396bcd76111SHerbert Xu 	int			sk_gso_type;
39782cc1a7aSPeter P Waskiewicz Jr 	unsigned int		sk_gso_max_size;
3981485348dSBen Hutchings 	u16			sk_gso_max_segs;
3999932cf95SDavid S. Miller 	int			sk_rcvlowat;
4001da177e4SLinus Torvalds 	unsigned long	        sk_lingertime;
4011da177e4SLinus Torvalds 	struct sk_buff_head	sk_error_queue;
402476e19cfSArnaldo Carvalho de Melo 	struct proto		*sk_prot_creator;
4031da177e4SLinus Torvalds 	rwlock_t		sk_callback_lock;
4041da177e4SLinus Torvalds 	int			sk_err,
4051da177e4SLinus Torvalds 				sk_err_soft;
4061da177e4SLinus Torvalds 	unsigned short		sk_ack_backlog;
4071da177e4SLinus Torvalds 	unsigned short		sk_max_ack_backlog;
4081da177e4SLinus Torvalds 	__u32			sk_priority;
409*86f8515fSDaniel Borkmann #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
4105bc1421eSNeil Horman 	__u32			sk_cgrp_prioidx;
4115bc1421eSNeil Horman #endif
412109f6e39SEric W. Biederman 	struct pid		*sk_peer_pid;
413109f6e39SEric W. Biederman 	const struct cred	*sk_peer_cred;
4141da177e4SLinus Torvalds 	long			sk_rcvtimeo;
4151da177e4SLinus Torvalds 	long			sk_sndtimeo;
4161da177e4SLinus Torvalds 	void			*sk_protinfo;
4171da177e4SLinus Torvalds 	struct timer_list	sk_timer;
418b7aa0bf7SEric Dumazet 	ktime_t			sk_stamp;
419b9f40e21SWillem de Bruijn 	u16			sk_tsflags;
42009c2d251SWillem de Bruijn 	u32			sk_tskey;
4211da177e4SLinus Torvalds 	struct socket		*sk_socket;
4221da177e4SLinus Torvalds 	void			*sk_user_data;
4235640f768SEric Dumazet 	struct page_frag	sk_frag;
4241da177e4SLinus Torvalds 	struct sk_buff		*sk_send_head;
425ef64a54fSPavel Emelyanov 	__s32			sk_peek_off;
4261da177e4SLinus Torvalds 	int			sk_write_pending;
427d5f64238SAlexey Dobriyan #ifdef CONFIG_SECURITY
4281da177e4SLinus Torvalds 	void			*sk_security;
429d5f64238SAlexey Dobriyan #endif
4304a19ec58SLaszlo Attila Toth 	__u32			sk_mark;
431f8451725SHerbert Xu 	u32			sk_classid;
432e1aab161SGlauber Costa 	struct cg_proto		*sk_cgrp;
4331da177e4SLinus Torvalds 	void			(*sk_state_change)(struct sock *sk);
434676d2369SDavid S. Miller 	void			(*sk_data_ready)(struct sock *sk);
4351da177e4SLinus Torvalds 	void			(*sk_write_space)(struct sock *sk);
4361da177e4SLinus Torvalds 	void			(*sk_error_report)(struct sock *sk);
4371da177e4SLinus Torvalds 	int			(*sk_backlog_rcv)(struct sock *sk,
4381da177e4SLinus Torvalds 						  struct sk_buff *skb);
4391da177e4SLinus Torvalds 	void                    (*sk_destruct)(struct sock *sk);
4401da177e4SLinus Torvalds };
4411da177e4SLinus Torvalds 
442559835eaSPravin B Shelar #define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data)))
443559835eaSPravin B Shelar 
444559835eaSPravin B Shelar #define rcu_dereference_sk_user_data(sk)	rcu_dereference(__sk_user_data((sk)))
445559835eaSPravin B Shelar #define rcu_assign_sk_user_data(sk, ptr)	rcu_assign_pointer(__sk_user_data((sk)), ptr)
446559835eaSPravin B Shelar 
4474a17fd52SPavel Emelyanov /*
4484a17fd52SPavel Emelyanov  * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
4494a17fd52SPavel Emelyanov  * or not whether his port will be reused by someone else. SK_FORCE_REUSE
4504a17fd52SPavel Emelyanov  * on a socket means that the socket will reuse everybody else's port
4514a17fd52SPavel Emelyanov  * without looking at the other's sk_reuse value.
4524a17fd52SPavel Emelyanov  */
4534a17fd52SPavel Emelyanov 
4544a17fd52SPavel Emelyanov #define SK_NO_REUSE	0
4554a17fd52SPavel Emelyanov #define SK_CAN_REUSE	1
4564a17fd52SPavel Emelyanov #define SK_FORCE_REUSE	2
4574a17fd52SPavel Emelyanov 
458ef64a54fSPavel Emelyanov static inline int sk_peek_offset(struct sock *sk, int flags)
459ef64a54fSPavel Emelyanov {
460ef64a54fSPavel Emelyanov 	if ((flags & MSG_PEEK) && (sk->sk_peek_off >= 0))
461ef64a54fSPavel Emelyanov 		return sk->sk_peek_off;
462ef64a54fSPavel Emelyanov 	else
463ef64a54fSPavel Emelyanov 		return 0;
464ef64a54fSPavel Emelyanov }
465ef64a54fSPavel Emelyanov 
466ef64a54fSPavel Emelyanov static inline void sk_peek_offset_bwd(struct sock *sk, int val)
467ef64a54fSPavel Emelyanov {
468ef64a54fSPavel Emelyanov 	if (sk->sk_peek_off >= 0) {
469ef64a54fSPavel Emelyanov 		if (sk->sk_peek_off >= val)
470ef64a54fSPavel Emelyanov 			sk->sk_peek_off -= val;
471ef64a54fSPavel Emelyanov 		else
472ef64a54fSPavel Emelyanov 			sk->sk_peek_off = 0;
473ef64a54fSPavel Emelyanov 	}
474ef64a54fSPavel Emelyanov }
475ef64a54fSPavel Emelyanov 
476ef64a54fSPavel Emelyanov static inline void sk_peek_offset_fwd(struct sock *sk, int val)
477ef64a54fSPavel Emelyanov {
478ef64a54fSPavel Emelyanov 	if (sk->sk_peek_off >= 0)
479ef64a54fSPavel Emelyanov 		sk->sk_peek_off += val;
480ef64a54fSPavel Emelyanov }
481ef64a54fSPavel Emelyanov 
4821da177e4SLinus Torvalds /*
4831da177e4SLinus Torvalds  * Hashed lists helper routines
4841da177e4SLinus Torvalds  */
485c4146644SLi Zefan static inline struct sock *sk_entry(const struct hlist_node *node)
486c4146644SLi Zefan {
487c4146644SLi Zefan 	return hlist_entry(node, struct sock, sk_node);
488c4146644SLi Zefan }
489c4146644SLi Zefan 
490e48c414eSArnaldo Carvalho de Melo static inline struct sock *__sk_head(const struct hlist_head *head)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	return hlist_entry(head->first, struct sock, sk_node);
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
495e48c414eSArnaldo Carvalho de Melo static inline struct sock *sk_head(const struct hlist_head *head)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	return hlist_empty(head) ? NULL : __sk_head(head);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
50088ab1932SEric Dumazet static inline struct sock *__sk_nulls_head(const struct hlist_nulls_head *head)
50188ab1932SEric Dumazet {
50288ab1932SEric Dumazet 	return hlist_nulls_entry(head->first, struct sock, sk_nulls_node);
50388ab1932SEric Dumazet }
50488ab1932SEric Dumazet 
50588ab1932SEric Dumazet static inline struct sock *sk_nulls_head(const struct hlist_nulls_head *head)
50688ab1932SEric Dumazet {
50788ab1932SEric Dumazet 	return hlist_nulls_empty(head) ? NULL : __sk_nulls_head(head);
50888ab1932SEric Dumazet }
50988ab1932SEric Dumazet 
510e48c414eSArnaldo Carvalho de Melo static inline struct sock *sk_next(const struct sock *sk)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	return sk->sk_node.next ?
5131da177e4SLinus Torvalds 		hlist_entry(sk->sk_node.next, struct sock, sk_node) : NULL;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
51688ab1932SEric Dumazet static inline struct sock *sk_nulls_next(const struct sock *sk)
51788ab1932SEric Dumazet {
51888ab1932SEric Dumazet 	return (!is_a_nulls(sk->sk_nulls_node.next)) ?
51988ab1932SEric Dumazet 		hlist_nulls_entry(sk->sk_nulls_node.next,
52088ab1932SEric Dumazet 				  struct sock, sk_nulls_node) :
52188ab1932SEric Dumazet 		NULL;
52288ab1932SEric Dumazet }
52388ab1932SEric Dumazet 
524dc6b9b78SEric Dumazet static inline bool sk_unhashed(const struct sock *sk)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	return hlist_unhashed(&sk->sk_node);
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
529dc6b9b78SEric Dumazet static inline bool sk_hashed(const struct sock *sk)
5301da177e4SLinus Torvalds {
531da753beaSAkinobu Mita 	return !sk_unhashed(sk);
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
534dc6b9b78SEric Dumazet static inline void sk_node_init(struct hlist_node *node)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	node->pprev = NULL;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
539dc6b9b78SEric Dumazet static inline void sk_nulls_node_init(struct hlist_nulls_node *node)
54088ab1932SEric Dumazet {
54188ab1932SEric Dumazet 	node->pprev = NULL;
54288ab1932SEric Dumazet }
54388ab1932SEric Dumazet 
544dc6b9b78SEric Dumazet static inline void __sk_del_node(struct sock *sk)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	__hlist_del(&sk->sk_node);
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
549808f5114Sstephen hemminger /* NB: equivalent to hlist_del_init_rcu */
550dc6b9b78SEric Dumazet static inline bool __sk_del_node_init(struct sock *sk)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	if (sk_hashed(sk)) {
5531da177e4SLinus Torvalds 		__sk_del_node(sk);
5541da177e4SLinus Torvalds 		sk_node_init(&sk->sk_node);
555dc6b9b78SEric Dumazet 		return true;
5561da177e4SLinus Torvalds 	}
557dc6b9b78SEric Dumazet 	return false;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds /* Grab socket reference count. This operation is valid only
5611da177e4SLinus Torvalds    when sk is ALREADY grabbed f.e. it is found in hash table
5621da177e4SLinus Torvalds    or a list and the lookup is made under lock preventing hash table
5631da177e4SLinus Torvalds    modifications.
5641da177e4SLinus Torvalds  */
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds static inline void sock_hold(struct sock *sk)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	atomic_inc(&sk->sk_refcnt);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /* Ungrab socket in the context, which assumes that socket refcnt
5721da177e4SLinus Torvalds    cannot hit zero, f.e. it is true in context of any socketcall.
5731da177e4SLinus Torvalds  */
5741da177e4SLinus Torvalds static inline void __sock_put(struct sock *sk)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	atomic_dec(&sk->sk_refcnt);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
579dc6b9b78SEric Dumazet static inline bool sk_del_node_init(struct sock *sk)
5801da177e4SLinus Torvalds {
581dc6b9b78SEric Dumazet 	bool rc = __sk_del_node_init(sk);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (rc) {
5841da177e4SLinus Torvalds 		/* paranoid for a while -acme */
5851da177e4SLinus Torvalds 		WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
5861da177e4SLinus Torvalds 		__sock_put(sk);
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 	return rc;
5891da177e4SLinus Torvalds }
590808f5114Sstephen hemminger #define sk_del_node_init_rcu(sk)	sk_del_node_init(sk)
5911da177e4SLinus Torvalds 
592dc6b9b78SEric Dumazet static inline bool __sk_nulls_del_node_init_rcu(struct sock *sk)
593271b72c7SEric Dumazet {
594271b72c7SEric Dumazet 	if (sk_hashed(sk)) {
59588ab1932SEric Dumazet 		hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
596dc6b9b78SEric Dumazet 		return true;
597271b72c7SEric Dumazet 	}
598dc6b9b78SEric Dumazet 	return false;
599271b72c7SEric Dumazet }
600271b72c7SEric Dumazet 
601dc6b9b78SEric Dumazet static inline bool sk_nulls_del_node_init_rcu(struct sock *sk)
602271b72c7SEric Dumazet {
603dc6b9b78SEric Dumazet 	bool rc = __sk_nulls_del_node_init_rcu(sk);
604271b72c7SEric Dumazet 
605271b72c7SEric Dumazet 	if (rc) {
606271b72c7SEric Dumazet 		/* paranoid for a while -acme */
607271b72c7SEric Dumazet 		WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
608271b72c7SEric Dumazet 		__sock_put(sk);
609271b72c7SEric Dumazet 	}
610271b72c7SEric Dumazet 	return rc;
611271b72c7SEric Dumazet }
612271b72c7SEric Dumazet 
613dc6b9b78SEric Dumazet static inline void __sk_add_node(struct sock *sk, struct hlist_head *list)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	hlist_add_head(&sk->sk_node, list);
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
618dc6b9b78SEric Dumazet static inline void sk_add_node(struct sock *sk, struct hlist_head *list)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	sock_hold(sk);
6211da177e4SLinus Torvalds 	__sk_add_node(sk, list);
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
624dc6b9b78SEric Dumazet static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
625808f5114Sstephen hemminger {
626808f5114Sstephen hemminger 	sock_hold(sk);
627808f5114Sstephen hemminger 	hlist_add_head_rcu(&sk->sk_node, list);
628808f5114Sstephen hemminger }
629808f5114Sstephen hemminger 
630dc6b9b78SEric Dumazet static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
631271b72c7SEric Dumazet {
63288ab1932SEric Dumazet 	hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
633271b72c7SEric Dumazet }
634271b72c7SEric Dumazet 
635dc6b9b78SEric Dumazet static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
636271b72c7SEric Dumazet {
637271b72c7SEric Dumazet 	sock_hold(sk);
63888ab1932SEric Dumazet 	__sk_nulls_add_node_rcu(sk, list);
639271b72c7SEric Dumazet }
640271b72c7SEric Dumazet 
641dc6b9b78SEric Dumazet static inline void __sk_del_bind_node(struct sock *sk)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	__hlist_del(&sk->sk_bind_node);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
646dc6b9b78SEric Dumazet static inline void sk_add_bind_node(struct sock *sk,
6471da177e4SLinus Torvalds 					struct hlist_head *list)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	hlist_add_head(&sk->sk_bind_node, list);
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
652b67bfe0dSSasha Levin #define sk_for_each(__sk, list) \
653b67bfe0dSSasha Levin 	hlist_for_each_entry(__sk, list, sk_node)
654b67bfe0dSSasha Levin #define sk_for_each_rcu(__sk, list) \
655b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(__sk, list, sk_node)
65688ab1932SEric Dumazet #define sk_nulls_for_each(__sk, node, list) \
65788ab1932SEric Dumazet 	hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
65888ab1932SEric Dumazet #define sk_nulls_for_each_rcu(__sk, node, list) \
65988ab1932SEric Dumazet 	hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node)
660b67bfe0dSSasha Levin #define sk_for_each_from(__sk) \
661b67bfe0dSSasha Levin 	hlist_for_each_entry_from(__sk, sk_node)
66288ab1932SEric Dumazet #define sk_nulls_for_each_from(__sk, node) \
66388ab1932SEric Dumazet 	if (__sk && ({ node = &(__sk)->sk_nulls_node; 1; })) \
66488ab1932SEric Dumazet 		hlist_nulls_for_each_entry_from(__sk, node, sk_nulls_node)
665b67bfe0dSSasha Levin #define sk_for_each_safe(__sk, tmp, list) \
666b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(__sk, tmp, list, sk_node)
667b67bfe0dSSasha Levin #define sk_for_each_bound(__sk, list) \
668b67bfe0dSSasha Levin 	hlist_for_each_entry(__sk, list, sk_bind_node)
6691da177e4SLinus Torvalds 
6702dc41cffSDavid Held /**
6712dc41cffSDavid Held  * sk_nulls_for_each_entry_offset - iterate over a list at a given struct offset
6722dc41cffSDavid Held  * @tpos:	the type * to use as a loop cursor.
6732dc41cffSDavid Held  * @pos:	the &struct hlist_node to use as a loop cursor.
6742dc41cffSDavid Held  * @head:	the head for your list.
6752dc41cffSDavid Held  * @offset:	offset of hlist_node within the struct.
6762dc41cffSDavid Held  *
6772dc41cffSDavid Held  */
6782dc41cffSDavid Held #define sk_nulls_for_each_entry_offset(tpos, pos, head, offset)		       \
6792dc41cffSDavid Held 	for (pos = (head)->first;					       \
6802dc41cffSDavid Held 	     (!is_a_nulls(pos)) &&					       \
6812dc41cffSDavid Held 		({ tpos = (typeof(*tpos) *)((void *)pos - offset); 1;});       \
6822dc41cffSDavid Held 	     pos = pos->next)
6832dc41cffSDavid Held 
684c336d148SEric W. Biederman static inline struct user_namespace *sk_user_ns(struct sock *sk)
685c336d148SEric W. Biederman {
686c336d148SEric W. Biederman 	/* Careful only use this in a context where these parameters
687c336d148SEric W. Biederman 	 * can not change and must all be valid, such as recvmsg from
688c336d148SEric W. Biederman 	 * userspace.
689c336d148SEric W. Biederman 	 */
690c336d148SEric W. Biederman 	return sk->sk_socket->file->f_cred->user_ns;
691c336d148SEric W. Biederman }
692c336d148SEric W. Biederman 
6931da177e4SLinus Torvalds /* Sock flags */
6941da177e4SLinus Torvalds enum sock_flags {
6951da177e4SLinus Torvalds 	SOCK_DEAD,
6961da177e4SLinus Torvalds 	SOCK_DONE,
6971da177e4SLinus Torvalds 	SOCK_URGINLINE,
6981da177e4SLinus Torvalds 	SOCK_KEEPOPEN,
6991da177e4SLinus Torvalds 	SOCK_LINGER,
7001da177e4SLinus Torvalds 	SOCK_DESTROY,
7011da177e4SLinus Torvalds 	SOCK_BROADCAST,
7021da177e4SLinus Torvalds 	SOCK_TIMESTAMP,
7031da177e4SLinus Torvalds 	SOCK_ZAPPED,
7041da177e4SLinus Torvalds 	SOCK_USE_WRITE_QUEUE, /* whether to call sk->sk_write_space in sock_wfree */
7051da177e4SLinus Torvalds 	SOCK_DBG, /* %SO_DEBUG setting */
7061da177e4SLinus Torvalds 	SOCK_RCVTSTAMP, /* %SO_TIMESTAMP setting */
70792f37fd2SEric Dumazet 	SOCK_RCVTSTAMPNS, /* %SO_TIMESTAMPNS setting */
7081da177e4SLinus Torvalds 	SOCK_LOCALROUTE, /* route locally only, %SO_DONTROUTE setting */
7091da177e4SLinus Torvalds 	SOCK_QUEUE_SHRUNK, /* write queue has been shrunk recently */
7107cb02404SMel Gorman 	SOCK_MEMALLOC, /* VM depends on this socket for swapping */
71120d49473SPatrick Ohly 	SOCK_TIMESTAMPING_RX_SOFTWARE,  /* %SOF_TIMESTAMPING_RX_SOFTWARE */
712bcdce719SEric Dumazet 	SOCK_FASYNC, /* fasync() active */
7133b885787SNeil Horman 	SOCK_RXQ_OVFL,
7141cdebb42SShirley Ma 	SOCK_ZEROCOPY, /* buffers from userspace */
7156e3e939fSJohannes Berg 	SOCK_WIFI_STATUS, /* push wifi status to userspace */
7163bdc0ebaSBen Greear 	SOCK_NOFCS, /* Tell NIC not to do the Ethernet FCS.
7173bdc0ebaSBen Greear 		     * Will use last 4 bytes of packet sent from
7183bdc0ebaSBen Greear 		     * user-space instead.
7193bdc0ebaSBen Greear 		     */
720d59577b6SVincent Bernat 	SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */
7217d4c04fcSKeller, Jacob E 	SOCK_SELECT_ERR_QUEUE, /* Wake select on error queue */
7221da177e4SLinus Torvalds };
7231da177e4SLinus Torvalds 
72453b924b3SRalf Baechle static inline void sock_copy_flags(struct sock *nsk, struct sock *osk)
72553b924b3SRalf Baechle {
72653b924b3SRalf Baechle 	nsk->sk_flags = osk->sk_flags;
72753b924b3SRalf Baechle }
72853b924b3SRalf Baechle 
7291da177e4SLinus Torvalds static inline void sock_set_flag(struct sock *sk, enum sock_flags flag)
7301da177e4SLinus Torvalds {
7311da177e4SLinus Torvalds 	__set_bit(flag, &sk->sk_flags);
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds static inline void sock_reset_flag(struct sock *sk, enum sock_flags flag)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	__clear_bit(flag, &sk->sk_flags);
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
7391b23a5dfSEric Dumazet static inline bool sock_flag(const struct sock *sk, enum sock_flags flag)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	return test_bit(flag, &sk->sk_flags);
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744c93bdd0eSMel Gorman #ifdef CONFIG_NET
745c93bdd0eSMel Gorman extern struct static_key memalloc_socks;
746c93bdd0eSMel Gorman static inline int sk_memalloc_socks(void)
747c93bdd0eSMel Gorman {
748c93bdd0eSMel Gorman 	return static_key_false(&memalloc_socks);
749c93bdd0eSMel Gorman }
750c93bdd0eSMel Gorman #else
751c93bdd0eSMel Gorman 
752c93bdd0eSMel Gorman static inline int sk_memalloc_socks(void)
753c93bdd0eSMel Gorman {
754c93bdd0eSMel Gorman 	return 0;
755c93bdd0eSMel Gorman }
756c93bdd0eSMel Gorman 
757c93bdd0eSMel Gorman #endif
758c93bdd0eSMel Gorman 
75999a1dec7SMel Gorman static inline gfp_t sk_gfp_atomic(struct sock *sk, gfp_t gfp_mask)
76099a1dec7SMel Gorman {
7617cb02404SMel Gorman 	return GFP_ATOMIC | (sk->sk_allocation & __GFP_MEMALLOC);
76299a1dec7SMel Gorman }
76399a1dec7SMel Gorman 
7641da177e4SLinus Torvalds static inline void sk_acceptq_removed(struct sock *sk)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	sk->sk_ack_backlog--;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds static inline void sk_acceptq_added(struct sock *sk)
7701da177e4SLinus Torvalds {
7711da177e4SLinus Torvalds 	sk->sk_ack_backlog++;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
774dc6b9b78SEric Dumazet static inline bool sk_acceptq_is_full(const struct sock *sk)
7751da177e4SLinus Torvalds {
77664a14651SDavid S. Miller 	return sk->sk_ack_backlog > sk->sk_max_ack_backlog;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds /*
7801da177e4SLinus Torvalds  * Compute minimal free write space needed to queue new packets.
7811da177e4SLinus Torvalds  */
782dc6b9b78SEric Dumazet static inline int sk_stream_min_wspace(const struct sock *sk)
7831da177e4SLinus Torvalds {
7848df09ea3SEric Dumazet 	return sk->sk_wmem_queued >> 1;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
787dc6b9b78SEric Dumazet static inline int sk_stream_wspace(const struct sock *sk)
7881da177e4SLinus Torvalds {
7891da177e4SLinus Torvalds 	return sk->sk_sndbuf - sk->sk_wmem_queued;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
79269336bd2SJoe Perches void sk_stream_write_space(struct sock *sk);
7931da177e4SLinus Torvalds 
7948eae939fSZhu Yi /* OOB backlog add */
795a3a858ffSZhu Yi static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
7969ee6b535SStephen Hemminger {
7977fee226aSEric Dumazet 	/* dont let skb dst not refcounted, we are going to leave rcu lock */
7987fee226aSEric Dumazet 	skb_dst_force(skb);
7997fee226aSEric Dumazet 
8007fee226aSEric Dumazet 	if (!sk->sk_backlog.tail)
8017fee226aSEric Dumazet 		sk->sk_backlog.head = skb;
8027fee226aSEric Dumazet 	else
8039ee6b535SStephen Hemminger 		sk->sk_backlog.tail->next = skb;
8047fee226aSEric Dumazet 
8059ee6b535SStephen Hemminger 	sk->sk_backlog.tail = skb;
8069ee6b535SStephen Hemminger 	skb->next = NULL;
8079ee6b535SStephen Hemminger }
8081da177e4SLinus Torvalds 
809c377411fSEric Dumazet /*
810c377411fSEric Dumazet  * Take into account size of receive queue and backlog queue
8110fd7bac6SEric Dumazet  * Do not take into account this skb truesize,
8120fd7bac6SEric Dumazet  * to allow even a single big packet to come.
813c377411fSEric Dumazet  */
814274f482dSSorin Dumitru static inline bool sk_rcvqueues_full(const struct sock *sk, unsigned int limit)
815c377411fSEric Dumazet {
816c377411fSEric Dumazet 	unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc);
817c377411fSEric Dumazet 
818f545a38fSEric Dumazet 	return qsize > limit;
819c377411fSEric Dumazet }
820c377411fSEric Dumazet 
8218eae939fSZhu Yi /* The per-socket spinlock must be held here. */
822f545a38fSEric Dumazet static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb,
823f545a38fSEric Dumazet 					      unsigned int limit)
8248eae939fSZhu Yi {
825274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, limit))
8268eae939fSZhu Yi 		return -ENOBUFS;
8278eae939fSZhu Yi 
828a3a858ffSZhu Yi 	__sk_add_backlog(sk, skb);
8298eae939fSZhu Yi 	sk->sk_backlog.len += skb->truesize;
8308eae939fSZhu Yi 	return 0;
8318eae939fSZhu Yi }
8328eae939fSZhu Yi 
83369336bd2SJoe Perches int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
834b4b9e355SMel Gorman 
835c57943a1SPeter Zijlstra static inline int sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
836c57943a1SPeter Zijlstra {
837b4b9e355SMel Gorman 	if (sk_memalloc_socks() && skb_pfmemalloc(skb))
838b4b9e355SMel Gorman 		return __sk_backlog_rcv(sk, skb);
839b4b9e355SMel Gorman 
840c57943a1SPeter Zijlstra 	return sk->sk_backlog_rcv(sk, skb);
841c57943a1SPeter Zijlstra }
842c57943a1SPeter Zijlstra 
8432c8c56e1SEric Dumazet static inline void sk_incoming_cpu_update(struct sock *sk)
8442c8c56e1SEric Dumazet {
8452c8c56e1SEric Dumazet 	sk->sk_incoming_cpu = raw_smp_processor_id();
8462c8c56e1SEric Dumazet }
8472c8c56e1SEric Dumazet 
848fe477558STom Herbert static inline void sock_rps_record_flow_hash(__u32 hash)
849c58dc01bSDavid S. Miller {
850c58dc01bSDavid S. Miller #ifdef CONFIG_RPS
851c58dc01bSDavid S. Miller 	struct rps_sock_flow_table *sock_flow_table;
852c58dc01bSDavid S. Miller 
853c58dc01bSDavid S. Miller 	rcu_read_lock();
854c58dc01bSDavid S. Miller 	sock_flow_table = rcu_dereference(rps_sock_flow_table);
855fe477558STom Herbert 	rps_record_sock_flow(sock_flow_table, hash);
856c58dc01bSDavid S. Miller 	rcu_read_unlock();
857c58dc01bSDavid S. Miller #endif
858c58dc01bSDavid S. Miller }
859c58dc01bSDavid S. Miller 
860fe477558STom Herbert static inline void sock_rps_reset_flow_hash(__u32 hash)
861c58dc01bSDavid S. Miller {
862c58dc01bSDavid S. Miller #ifdef CONFIG_RPS
863c58dc01bSDavid S. Miller 	struct rps_sock_flow_table *sock_flow_table;
864c58dc01bSDavid S. Miller 
865c58dc01bSDavid S. Miller 	rcu_read_lock();
866c58dc01bSDavid S. Miller 	sock_flow_table = rcu_dereference(rps_sock_flow_table);
867fe477558STom Herbert 	rps_reset_sock_flow(sock_flow_table, hash);
868c58dc01bSDavid S. Miller 	rcu_read_unlock();
869c58dc01bSDavid S. Miller #endif
870c58dc01bSDavid S. Miller }
871c58dc01bSDavid S. Miller 
872fe477558STom Herbert static inline void sock_rps_record_flow(const struct sock *sk)
873fe477558STom Herbert {
874c9d8ca04SZhi Yong Wu #ifdef CONFIG_RPS
875fe477558STom Herbert 	sock_rps_record_flow_hash(sk->sk_rxhash);
876c9d8ca04SZhi Yong Wu #endif
877fe477558STom Herbert }
878fe477558STom Herbert 
879fe477558STom Herbert static inline void sock_rps_reset_flow(const struct sock *sk)
880fe477558STom Herbert {
881c9d8ca04SZhi Yong Wu #ifdef CONFIG_RPS
882fe477558STom Herbert 	sock_rps_reset_flow_hash(sk->sk_rxhash);
883c9d8ca04SZhi Yong Wu #endif
884fe477558STom Herbert }
885fe477558STom Herbert 
886bdeab991STom Herbert static inline void sock_rps_save_rxhash(struct sock *sk,
887bdeab991STom Herbert 					const struct sk_buff *skb)
888c58dc01bSDavid S. Miller {
889c58dc01bSDavid S. Miller #ifdef CONFIG_RPS
89061b905daSTom Herbert 	if (unlikely(sk->sk_rxhash != skb->hash)) {
891c58dc01bSDavid S. Miller 		sock_rps_reset_flow(sk);
89261b905daSTom Herbert 		sk->sk_rxhash = skb->hash;
893c58dc01bSDavid S. Miller 	}
894c58dc01bSDavid S. Miller #endif
895c58dc01bSDavid S. Miller }
896c58dc01bSDavid S. Miller 
897bdeab991STom Herbert static inline void sock_rps_reset_rxhash(struct sock *sk)
898bdeab991STom Herbert {
899bdeab991STom Herbert #ifdef CONFIG_RPS
900bdeab991STom Herbert 	sock_rps_reset_flow(sk);
901bdeab991STom Herbert 	sk->sk_rxhash = 0;
902bdeab991STom Herbert #endif
903bdeab991STom Herbert }
904bdeab991STom Herbert 
9051da177e4SLinus Torvalds #define sk_wait_event(__sk, __timeo, __condition)			\
906cfcabdccSStephen Hemminger 	({	int __rc;						\
9071da177e4SLinus Torvalds 		release_sock(__sk);					\
908cfcabdccSStephen Hemminger 		__rc = __condition;					\
909cfcabdccSStephen Hemminger 		if (!__rc) {						\
9101da177e4SLinus Torvalds 			*(__timeo) = schedule_timeout(*(__timeo));	\
9111da177e4SLinus Torvalds 		}							\
91226cabd31SPeter Zijlstra 		sched_annotate_sleep();						\
9131da177e4SLinus Torvalds 		lock_sock(__sk);					\
914cfcabdccSStephen Hemminger 		__rc = __condition;					\
915cfcabdccSStephen Hemminger 		__rc;							\
9161da177e4SLinus Torvalds 	})
9171da177e4SLinus Torvalds 
91869336bd2SJoe Perches int sk_stream_wait_connect(struct sock *sk, long *timeo_p);
91969336bd2SJoe Perches int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
92069336bd2SJoe Perches void sk_stream_wait_close(struct sock *sk, long timeo_p);
92169336bd2SJoe Perches int sk_stream_error(struct sock *sk, int flags, int err);
92269336bd2SJoe Perches void sk_stream_kill_queues(struct sock *sk);
92369336bd2SJoe Perches void sk_set_memalloc(struct sock *sk);
92469336bd2SJoe Perches void sk_clear_memalloc(struct sock *sk);
9251da177e4SLinus Torvalds 
92669336bd2SJoe Perches int sk_wait_data(struct sock *sk, long *timeo);
9271da177e4SLinus Torvalds 
92860236fddSArnaldo Carvalho de Melo struct request_sock_ops;
9296d6ee43eSArnaldo Carvalho de Melo struct timewait_sock_ops;
930ab1e0a13SArnaldo Carvalho de Melo struct inet_hashinfo;
931fc8717baSPavel Emelyanov struct raw_hashinfo;
932de477254SPaul Gortmaker struct module;
9332e6599cbSArnaldo Carvalho de Melo 
934f77d6021SEric Dumazet /*
935f77d6021SEric Dumazet  * caches using SLAB_DESTROY_BY_RCU should let .next pointer from nulls nodes
936f77d6021SEric Dumazet  * un-modified. Special care is taken when initializing object to zero.
937f77d6021SEric Dumazet  */
938f77d6021SEric Dumazet static inline void sk_prot_clear_nulls(struct sock *sk, int size)
939f77d6021SEric Dumazet {
940f77d6021SEric Dumazet 	if (offsetof(struct sock, sk_node.next) != 0)
941f77d6021SEric Dumazet 		memset(sk, 0, offsetof(struct sock, sk_node.next));
942f77d6021SEric Dumazet 	memset(&sk->sk_node.pprev, 0,
943f77d6021SEric Dumazet 	       size - offsetof(struct sock, sk_node.pprev));
944f77d6021SEric Dumazet }
945f77d6021SEric Dumazet 
9461da177e4SLinus Torvalds /* Networking protocol blocks we attach to sockets.
9471da177e4SLinus Torvalds  * socket layer -> transport layer interface
9481da177e4SLinus Torvalds  * transport -> network interface is defined by struct inet_proto
9491da177e4SLinus Torvalds  */
9501da177e4SLinus Torvalds struct proto {
9511da177e4SLinus Torvalds 	void			(*close)(struct sock *sk,
9521da177e4SLinus Torvalds 					long timeout);
9531da177e4SLinus Torvalds 	int			(*connect)(struct sock *sk,
9541da177e4SLinus Torvalds 					struct sockaddr *uaddr,
9551da177e4SLinus Torvalds 					int addr_len);
9561da177e4SLinus Torvalds 	int			(*disconnect)(struct sock *sk, int flags);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	struct sock *		(*accept)(struct sock *sk, int flags, int *err);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	int			(*ioctl)(struct sock *sk, int cmd,
9611da177e4SLinus Torvalds 					 unsigned long arg);
9621da177e4SLinus Torvalds 	int			(*init)(struct sock *sk);
9637d06b2e0SBrian Haley 	void			(*destroy)(struct sock *sk);
9641da177e4SLinus Torvalds 	void			(*shutdown)(struct sock *sk, int how);
9651da177e4SLinus Torvalds 	int			(*setsockopt)(struct sock *sk, int level,
9661da177e4SLinus Torvalds 					int optname, char __user *optval,
967b7058842SDavid S. Miller 					unsigned int optlen);
9681da177e4SLinus Torvalds 	int			(*getsockopt)(struct sock *sk, int level,
9691da177e4SLinus Torvalds 					int optname, char __user *optval,
9701da177e4SLinus Torvalds 					int __user *option);
971af01d537SAlexey Dobriyan #ifdef CONFIG_COMPAT
9723fdadf7dSDmitry Mishin 	int			(*compat_setsockopt)(struct sock *sk,
9733fdadf7dSDmitry Mishin 					int level,
9743fdadf7dSDmitry Mishin 					int optname, char __user *optval,
975b7058842SDavid S. Miller 					unsigned int optlen);
9763fdadf7dSDmitry Mishin 	int			(*compat_getsockopt)(struct sock *sk,
9773fdadf7dSDmitry Mishin 					int level,
9783fdadf7dSDmitry Mishin 					int optname, char __user *optval,
9793fdadf7dSDmitry Mishin 					int __user *option);
980709b46e8SEric W. Biederman 	int			(*compat_ioctl)(struct sock *sk,
981709b46e8SEric W. Biederman 					unsigned int cmd, unsigned long arg);
982af01d537SAlexey Dobriyan #endif
9831da177e4SLinus Torvalds 	int			(*sendmsg)(struct kiocb *iocb, struct sock *sk,
9841da177e4SLinus Torvalds 					   struct msghdr *msg, size_t len);
9851da177e4SLinus Torvalds 	int			(*recvmsg)(struct kiocb *iocb, struct sock *sk,
9861da177e4SLinus Torvalds 					   struct msghdr *msg,
9871da177e4SLinus Torvalds 					   size_t len, int noblock, int flags,
9881da177e4SLinus Torvalds 					   int *addr_len);
9891da177e4SLinus Torvalds 	int			(*sendpage)(struct sock *sk, struct page *page,
9901da177e4SLinus Torvalds 					int offset, size_t size, int flags);
9911da177e4SLinus Torvalds 	int			(*bind)(struct sock *sk,
9921da177e4SLinus Torvalds 					struct sockaddr *uaddr, int addr_len);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	int			(*backlog_rcv) (struct sock *sk,
9951da177e4SLinus Torvalds 						struct sk_buff *skb);
9961da177e4SLinus Torvalds 
99746d3ceabSEric Dumazet 	void		(*release_cb)(struct sock *sk);
99846d3ceabSEric Dumazet 
9991da177e4SLinus Torvalds 	/* Keeping track of sk's, looking them up, and port selection methods. */
10001da177e4SLinus Torvalds 	void			(*hash)(struct sock *sk);
10011da177e4SLinus Torvalds 	void			(*unhash)(struct sock *sk);
1002719f8358SEric Dumazet 	void			(*rehash)(struct sock *sk);
10031da177e4SLinus Torvalds 	int			(*get_port)(struct sock *sk, unsigned short snum);
1004fcbdf09dSOctavian Purdila 	void			(*clear_sk)(struct sock *sk, int size);
10051da177e4SLinus Torvalds 
1006286ab3d4SEric Dumazet 	/* Keeping track of sockets in use */
100765f76517SEric Dumazet #ifdef CONFIG_PROC_FS
100813ff3d6fSPavel Emelyanov 	unsigned int		inuse_idx;
100965f76517SEric Dumazet #endif
1010ebb53d75SArnaldo Carvalho de Melo 
1011c9bee3b7SEric Dumazet 	bool			(*stream_memory_free)(const struct sock *sk);
10121da177e4SLinus Torvalds 	/* Memory pressure */
10135c52ba17SPavel Emelyanov 	void			(*enter_memory_pressure)(struct sock *sk);
10148d987e5cSEric Dumazet 	atomic_long_t		*memory_allocated;	/* Current allocated memory. */
10151748376bSEric Dumazet 	struct percpu_counter	*sockets_allocated;	/* Current number of sockets. */
10161da177e4SLinus Torvalds 	/*
10171da177e4SLinus Torvalds 	 * Pressure flag: try to collapse.
10181da177e4SLinus Torvalds 	 * Technical note: it is used by multiple contexts non atomically.
10193ab224beSHideo Aoki 	 * All the __sk_mem_schedule() is of this nature: accounting
10201da177e4SLinus Torvalds 	 * is strict, actions are advisory and have some latency.
10211da177e4SLinus Torvalds 	 */
10221da177e4SLinus Torvalds 	int			*memory_pressure;
10238d987e5cSEric Dumazet 	long			*sysctl_mem;
10241da177e4SLinus Torvalds 	int			*sysctl_wmem;
10251da177e4SLinus Torvalds 	int			*sysctl_rmem;
10261da177e4SLinus Torvalds 	int			max_header;
10277ba42910SChangli Gao 	bool			no_autobind;
10281da177e4SLinus Torvalds 
1029e18b890bSChristoph Lameter 	struct kmem_cache	*slab;
10301da177e4SLinus Torvalds 	unsigned int		obj_size;
1031271b72c7SEric Dumazet 	int			slab_flags;
10321da177e4SLinus Torvalds 
1033dd24c001SEric Dumazet 	struct percpu_counter	*orphan_count;
10348feaf0c0SArnaldo Carvalho de Melo 
103560236fddSArnaldo Carvalho de Melo 	struct request_sock_ops	*rsk_prot;
10366d6ee43eSArnaldo Carvalho de Melo 	struct timewait_sock_ops *twsk_prot;
10372e6599cbSArnaldo Carvalho de Melo 
103839d8cda7SPavel Emelyanov 	union {
1039ab1e0a13SArnaldo Carvalho de Melo 		struct inet_hashinfo	*hashinfo;
1040645ca708SEric Dumazet 		struct udp_table	*udp_table;
1041fc8717baSPavel Emelyanov 		struct raw_hashinfo	*raw_hash;
104239d8cda7SPavel Emelyanov 	} h;
1043ab1e0a13SArnaldo Carvalho de Melo 
10441da177e4SLinus Torvalds 	struct module		*owner;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	char			name[32];
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	struct list_head	node;
1049e6848976SArnaldo Carvalho de Melo #ifdef SOCK_REFCNT_DEBUG
1050e6848976SArnaldo Carvalho de Melo 	atomic_t		socks;
1051e6848976SArnaldo Carvalho de Melo #endif
1052c255a458SAndrew Morton #ifdef CONFIG_MEMCG_KMEM
1053e1aab161SGlauber Costa 	/*
1054e1aab161SGlauber Costa 	 * cgroup specific init/deinit functions. Called once for all
1055e1aab161SGlauber Costa 	 * protocols that implement it, from cgroups populate function.
1056e1aab161SGlauber Costa 	 * This function has to setup any files the protocol want to
1057e1aab161SGlauber Costa 	 * appear in the kmem cgroup filesystem.
1058e1aab161SGlauber Costa 	 */
10591d62e436SGlauber Costa 	int			(*init_cgroup)(struct mem_cgroup *memcg,
1060e1aab161SGlauber Costa 					       struct cgroup_subsys *ss);
10611d62e436SGlauber Costa 	void			(*destroy_cgroup)(struct mem_cgroup *memcg);
1062e1aab161SGlauber Costa 	struct cg_proto		*(*proto_cgroup)(struct mem_cgroup *memcg);
1063e1aab161SGlauber Costa #endif
1064e1aab161SGlauber Costa };
1065e1aab161SGlauber Costa 
10663f134619SGlauber Costa /*
10673f134619SGlauber Costa  * Bits in struct cg_proto.flags
10683f134619SGlauber Costa  */
10693f134619SGlauber Costa enum cg_proto_flags {
10703f134619SGlauber Costa 	/* Currently active and new sockets should be assigned to cgroups */
10713f134619SGlauber Costa 	MEMCG_SOCK_ACTIVE,
10723f134619SGlauber Costa 	/* It was ever activated; we must disarm static keys on destruction */
10733f134619SGlauber Costa 	MEMCG_SOCK_ACTIVATED,
10743f134619SGlauber Costa };
10753f134619SGlauber Costa 
1076e1aab161SGlauber Costa struct cg_proto {
10773e32cb2eSJohannes Weiner 	struct page_counter	memory_allocated;	/* Current allocated memory. */
10782e685cadSEric W. Biederman 	struct percpu_counter	sockets_allocated;	/* Current number of sockets. */
10792e685cadSEric W. Biederman 	int			memory_pressure;
10802e685cadSEric W. Biederman 	long			sysctl_mem[3];
10813f134619SGlauber Costa 	unsigned long		flags;
1082e1aab161SGlauber Costa 	/*
1083e1aab161SGlauber Costa 	 * memcg field is used to find which memcg we belong directly
1084e1aab161SGlauber Costa 	 * Each memcg struct can hold more than one cg_proto, so container_of
1085e1aab161SGlauber Costa 	 * won't really cut.
1086e1aab161SGlauber Costa 	 *
1087e1aab161SGlauber Costa 	 * The elegant solution would be having an inverse function to
1088e1aab161SGlauber Costa 	 * proto_cgroup in struct proto, but that means polluting the structure
1089e1aab161SGlauber Costa 	 * for everybody, instead of just for memcg users.
1090e1aab161SGlauber Costa 	 */
1091e1aab161SGlauber Costa 	struct mem_cgroup	*memcg;
10921da177e4SLinus Torvalds };
10931da177e4SLinus Torvalds 
109469336bd2SJoe Perches int proto_register(struct proto *prot, int alloc_slab);
109569336bd2SJoe Perches void proto_unregister(struct proto *prot);
10961da177e4SLinus Torvalds 
10973f134619SGlauber Costa static inline bool memcg_proto_active(struct cg_proto *cg_proto)
10983f134619SGlauber Costa {
10993f134619SGlauber Costa 	return test_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
11003f134619SGlauber Costa }
11013f134619SGlauber Costa 
11023f134619SGlauber Costa static inline bool memcg_proto_activated(struct cg_proto *cg_proto)
11033f134619SGlauber Costa {
11043f134619SGlauber Costa 	return test_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags);
11053f134619SGlauber Costa }
11063f134619SGlauber Costa 
1107e6848976SArnaldo Carvalho de Melo #ifdef SOCK_REFCNT_DEBUG
1108e6848976SArnaldo Carvalho de Melo static inline void sk_refcnt_debug_inc(struct sock *sk)
1109e6848976SArnaldo Carvalho de Melo {
1110e6848976SArnaldo Carvalho de Melo 	atomic_inc(&sk->sk_prot->socks);
1111e6848976SArnaldo Carvalho de Melo }
1112e6848976SArnaldo Carvalho de Melo 
1113e6848976SArnaldo Carvalho de Melo static inline void sk_refcnt_debug_dec(struct sock *sk)
1114e6848976SArnaldo Carvalho de Melo {
1115e6848976SArnaldo Carvalho de Melo 	atomic_dec(&sk->sk_prot->socks);
1116e6848976SArnaldo Carvalho de Melo 	printk(KERN_DEBUG "%s socket %p released, %d are still alive\n",
1117e6848976SArnaldo Carvalho de Melo 	       sk->sk_prot->name, sk, atomic_read(&sk->sk_prot->socks));
1118e6848976SArnaldo Carvalho de Melo }
1119e6848976SArnaldo Carvalho de Melo 
1120dec34fb0SYing Xue static inline void sk_refcnt_debug_release(const struct sock *sk)
1121e6848976SArnaldo Carvalho de Melo {
1122e6848976SArnaldo Carvalho de Melo 	if (atomic_read(&sk->sk_refcnt) != 1)
1123e6848976SArnaldo Carvalho de Melo 		printk(KERN_DEBUG "Destruction of the %s socket %p delayed, refcnt=%d\n",
1124e6848976SArnaldo Carvalho de Melo 		       sk->sk_prot->name, sk, atomic_read(&sk->sk_refcnt));
1125e6848976SArnaldo Carvalho de Melo }
1126e6848976SArnaldo Carvalho de Melo #else /* SOCK_REFCNT_DEBUG */
1127e6848976SArnaldo Carvalho de Melo #define sk_refcnt_debug_inc(sk) do { } while (0)
1128e6848976SArnaldo Carvalho de Melo #define sk_refcnt_debug_dec(sk) do { } while (0)
1129e6848976SArnaldo Carvalho de Melo #define sk_refcnt_debug_release(sk) do { } while (0)
1130e6848976SArnaldo Carvalho de Melo #endif /* SOCK_REFCNT_DEBUG */
1131e6848976SArnaldo Carvalho de Melo 
1132c255a458SAndrew Morton #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_NET)
1133c5905afbSIngo Molnar extern struct static_key memcg_socket_limit_enabled;
1134e1aab161SGlauber Costa static inline struct cg_proto *parent_cg_proto(struct proto *proto,
1135e1aab161SGlauber Costa 					       struct cg_proto *cg_proto)
1136e1aab161SGlauber Costa {
1137e1aab161SGlauber Costa 	return proto->proto_cgroup(parent_mem_cgroup(cg_proto->memcg));
1138e1aab161SGlauber Costa }
1139c5905afbSIngo Molnar #define mem_cgroup_sockets_enabled static_key_false(&memcg_socket_limit_enabled)
1140e1aab161SGlauber Costa #else
1141e1aab161SGlauber Costa #define mem_cgroup_sockets_enabled 0
1142e1aab161SGlauber Costa static inline struct cg_proto *parent_cg_proto(struct proto *proto,
1143e1aab161SGlauber Costa 					       struct cg_proto *cg_proto)
1144e1aab161SGlauber Costa {
1145e1aab161SGlauber Costa 	return NULL;
1146e1aab161SGlauber Costa }
1147e1aab161SGlauber Costa #endif
1148e1aab161SGlauber Costa 
1149c9bee3b7SEric Dumazet static inline bool sk_stream_memory_free(const struct sock *sk)
1150c9bee3b7SEric Dumazet {
1151c9bee3b7SEric Dumazet 	if (sk->sk_wmem_queued >= sk->sk_sndbuf)
1152c9bee3b7SEric Dumazet 		return false;
1153c9bee3b7SEric Dumazet 
1154c9bee3b7SEric Dumazet 	return sk->sk_prot->stream_memory_free ?
1155c9bee3b7SEric Dumazet 		sk->sk_prot->stream_memory_free(sk) : true;
1156c9bee3b7SEric Dumazet }
1157c9bee3b7SEric Dumazet 
115864dc6130SEric Dumazet static inline bool sk_stream_is_writeable(const struct sock *sk)
115964dc6130SEric Dumazet {
1160c9bee3b7SEric Dumazet 	return sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) &&
1161c9bee3b7SEric Dumazet 	       sk_stream_memory_free(sk);
116264dc6130SEric Dumazet }
1163e1aab161SGlauber Costa 
1164c9bee3b7SEric Dumazet 
1165180d8cd9SGlauber Costa static inline bool sk_has_memory_pressure(const struct sock *sk)
1166180d8cd9SGlauber Costa {
1167180d8cd9SGlauber Costa 	return sk->sk_prot->memory_pressure != NULL;
1168180d8cd9SGlauber Costa }
1169180d8cd9SGlauber Costa 
1170180d8cd9SGlauber Costa static inline bool sk_under_memory_pressure(const struct sock *sk)
1171180d8cd9SGlauber Costa {
1172180d8cd9SGlauber Costa 	if (!sk->sk_prot->memory_pressure)
1173180d8cd9SGlauber Costa 		return false;
1174e1aab161SGlauber Costa 
1175e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
11762e685cadSEric W. Biederman 		return !!sk->sk_cgrp->memory_pressure;
1177e1aab161SGlauber Costa 
117835b87f6cSChristoph Paasch 	return !!*sk->sk_prot->memory_pressure;
1179180d8cd9SGlauber Costa }
1180180d8cd9SGlauber Costa 
1181180d8cd9SGlauber Costa static inline void sk_leave_memory_pressure(struct sock *sk)
1182180d8cd9SGlauber Costa {
1183180d8cd9SGlauber Costa 	int *memory_pressure = sk->sk_prot->memory_pressure;
1184180d8cd9SGlauber Costa 
1185e1aab161SGlauber Costa 	if (!memory_pressure)
1186e1aab161SGlauber Costa 		return;
1187e1aab161SGlauber Costa 
1188e1aab161SGlauber Costa 	if (*memory_pressure)
1189180d8cd9SGlauber Costa 		*memory_pressure = 0;
1190e1aab161SGlauber Costa 
1191e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
1192e1aab161SGlauber Costa 		struct cg_proto *cg_proto = sk->sk_cgrp;
1193e1aab161SGlauber Costa 		struct proto *prot = sk->sk_prot;
1194e1aab161SGlauber Costa 
1195e1aab161SGlauber Costa 		for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
11962e685cadSEric W. Biederman 			cg_proto->memory_pressure = 0;
1197e1aab161SGlauber Costa 	}
1198e1aab161SGlauber Costa 
1199180d8cd9SGlauber Costa }
1200180d8cd9SGlauber Costa 
1201180d8cd9SGlauber Costa static inline void sk_enter_memory_pressure(struct sock *sk)
1202180d8cd9SGlauber Costa {
1203e1aab161SGlauber Costa 	if (!sk->sk_prot->enter_memory_pressure)
1204e1aab161SGlauber Costa 		return;
1205e1aab161SGlauber Costa 
1206e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
1207e1aab161SGlauber Costa 		struct cg_proto *cg_proto = sk->sk_cgrp;
1208e1aab161SGlauber Costa 		struct proto *prot = sk->sk_prot;
1209e1aab161SGlauber Costa 
1210e1aab161SGlauber Costa 		for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
12117f2cbdc2SEric W. Biederman 			cg_proto->memory_pressure = 1;
1212e1aab161SGlauber Costa 	}
1213e1aab161SGlauber Costa 
1214180d8cd9SGlauber Costa 	sk->sk_prot->enter_memory_pressure(sk);
1215180d8cd9SGlauber Costa }
1216180d8cd9SGlauber Costa 
1217180d8cd9SGlauber Costa static inline long sk_prot_mem_limits(const struct sock *sk, int index)
1218180d8cd9SGlauber Costa {
1219180d8cd9SGlauber Costa 	long *prot = sk->sk_prot->sysctl_mem;
1220e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
1221e1aab161SGlauber Costa 		prot = sk->sk_cgrp->sysctl_mem;
1222180d8cd9SGlauber Costa 	return prot[index];
1223180d8cd9SGlauber Costa }
1224180d8cd9SGlauber Costa 
1225e1aab161SGlauber Costa static inline void memcg_memory_allocated_add(struct cg_proto *prot,
1226e1aab161SGlauber Costa 					      unsigned long amt,
1227e1aab161SGlauber Costa 					      int *parent_status)
1228e1aab161SGlauber Costa {
12293e32cb2eSJohannes Weiner 	page_counter_charge(&prot->memory_allocated, amt);
1230e1aab161SGlauber Costa 
12313e32cb2eSJohannes Weiner 	if (page_counter_read(&prot->memory_allocated) >
12323e32cb2eSJohannes Weiner 	    prot->memory_allocated.limit)
1233e1aab161SGlauber Costa 		*parent_status = OVER_LIMIT;
1234e1aab161SGlauber Costa }
1235e1aab161SGlauber Costa 
1236e1aab161SGlauber Costa static inline void memcg_memory_allocated_sub(struct cg_proto *prot,
1237e1aab161SGlauber Costa 					      unsigned long amt)
1238e1aab161SGlauber Costa {
12393e32cb2eSJohannes Weiner 	page_counter_uncharge(&prot->memory_allocated, amt);
1240e1aab161SGlauber Costa }
1241e1aab161SGlauber Costa 
1242180d8cd9SGlauber Costa static inline long
1243180d8cd9SGlauber Costa sk_memory_allocated(const struct sock *sk)
1244180d8cd9SGlauber Costa {
1245180d8cd9SGlauber Costa 	struct proto *prot = sk->sk_prot;
12463e32cb2eSJohannes Weiner 
1247e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
12483e32cb2eSJohannes Weiner 		return page_counter_read(&sk->sk_cgrp->memory_allocated);
1249e1aab161SGlauber Costa 
1250180d8cd9SGlauber Costa 	return atomic_long_read(prot->memory_allocated);
1251180d8cd9SGlauber Costa }
1252180d8cd9SGlauber Costa 
1253180d8cd9SGlauber Costa static inline long
1254e1aab161SGlauber Costa sk_memory_allocated_add(struct sock *sk, int amt, int *parent_status)
1255180d8cd9SGlauber Costa {
1256180d8cd9SGlauber Costa 	struct proto *prot = sk->sk_prot;
1257e1aab161SGlauber Costa 
1258e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
1259e1aab161SGlauber Costa 		memcg_memory_allocated_add(sk->sk_cgrp, amt, parent_status);
1260e1aab161SGlauber Costa 		/* update the root cgroup regardless */
1261e1aab161SGlauber Costa 		atomic_long_add_return(amt, prot->memory_allocated);
12623e32cb2eSJohannes Weiner 		return page_counter_read(&sk->sk_cgrp->memory_allocated);
1263e1aab161SGlauber Costa 	}
1264e1aab161SGlauber Costa 
1265180d8cd9SGlauber Costa 	return atomic_long_add_return(amt, prot->memory_allocated);
1266180d8cd9SGlauber Costa }
1267180d8cd9SGlauber Costa 
1268180d8cd9SGlauber Costa static inline void
12690e90b31fSGlauber Costa sk_memory_allocated_sub(struct sock *sk, int amt)
1270180d8cd9SGlauber Costa {
1271180d8cd9SGlauber Costa 	struct proto *prot = sk->sk_prot;
1272e1aab161SGlauber Costa 
12730e90b31fSGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
1274e1aab161SGlauber Costa 		memcg_memory_allocated_sub(sk->sk_cgrp, amt);
1275e1aab161SGlauber Costa 
1276180d8cd9SGlauber Costa 	atomic_long_sub(amt, prot->memory_allocated);
1277180d8cd9SGlauber Costa }
1278180d8cd9SGlauber Costa 
1279180d8cd9SGlauber Costa static inline void sk_sockets_allocated_dec(struct sock *sk)
1280180d8cd9SGlauber Costa {
1281180d8cd9SGlauber Costa 	struct proto *prot = sk->sk_prot;
1282e1aab161SGlauber Costa 
1283e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
1284e1aab161SGlauber Costa 		struct cg_proto *cg_proto = sk->sk_cgrp;
1285e1aab161SGlauber Costa 
1286e1aab161SGlauber Costa 		for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
12872e685cadSEric W. Biederman 			percpu_counter_dec(&cg_proto->sockets_allocated);
1288e1aab161SGlauber Costa 	}
1289e1aab161SGlauber Costa 
1290180d8cd9SGlauber Costa 	percpu_counter_dec(prot->sockets_allocated);
1291180d8cd9SGlauber Costa }
1292180d8cd9SGlauber Costa 
1293180d8cd9SGlauber Costa static inline void sk_sockets_allocated_inc(struct sock *sk)
1294180d8cd9SGlauber Costa {
1295180d8cd9SGlauber Costa 	struct proto *prot = sk->sk_prot;
1296e1aab161SGlauber Costa 
1297e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
1298e1aab161SGlauber Costa 		struct cg_proto *cg_proto = sk->sk_cgrp;
1299e1aab161SGlauber Costa 
1300e1aab161SGlauber Costa 		for (; cg_proto; cg_proto = parent_cg_proto(prot, cg_proto))
13012e685cadSEric W. Biederman 			percpu_counter_inc(&cg_proto->sockets_allocated);
1302e1aab161SGlauber Costa 	}
1303e1aab161SGlauber Costa 
1304180d8cd9SGlauber Costa 	percpu_counter_inc(prot->sockets_allocated);
1305180d8cd9SGlauber Costa }
1306180d8cd9SGlauber Costa 
1307180d8cd9SGlauber Costa static inline int
1308180d8cd9SGlauber Costa sk_sockets_allocated_read_positive(struct sock *sk)
1309180d8cd9SGlauber Costa {
1310180d8cd9SGlauber Costa 	struct proto *prot = sk->sk_prot;
1311180d8cd9SGlauber Costa 
1312e1aab161SGlauber Costa 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
13132e685cadSEric W. Biederman 		return percpu_counter_read_positive(&sk->sk_cgrp->sockets_allocated);
1314e1aab161SGlauber Costa 
1315518fbf9cSEric Dumazet 	return percpu_counter_read_positive(prot->sockets_allocated);
1316180d8cd9SGlauber Costa }
1317180d8cd9SGlauber Costa 
1318180d8cd9SGlauber Costa static inline int
1319180d8cd9SGlauber Costa proto_sockets_allocated_sum_positive(struct proto *prot)
1320180d8cd9SGlauber Costa {
1321180d8cd9SGlauber Costa 	return percpu_counter_sum_positive(prot->sockets_allocated);
1322180d8cd9SGlauber Costa }
1323180d8cd9SGlauber Costa 
1324180d8cd9SGlauber Costa static inline long
1325180d8cd9SGlauber Costa proto_memory_allocated(struct proto *prot)
1326180d8cd9SGlauber Costa {
1327180d8cd9SGlauber Costa 	return atomic_long_read(prot->memory_allocated);
1328180d8cd9SGlauber Costa }
1329180d8cd9SGlauber Costa 
1330180d8cd9SGlauber Costa static inline bool
1331180d8cd9SGlauber Costa proto_memory_pressure(struct proto *prot)
1332180d8cd9SGlauber Costa {
1333180d8cd9SGlauber Costa 	if (!prot->memory_pressure)
1334180d8cd9SGlauber Costa 		return false;
1335180d8cd9SGlauber Costa 	return !!*prot->memory_pressure;
1336180d8cd9SGlauber Costa }
1337180d8cd9SGlauber Costa 
133865f76517SEric Dumazet 
133965f76517SEric Dumazet #ifdef CONFIG_PROC_FS
13401da177e4SLinus Torvalds /* Called with local bh disabled */
134169336bd2SJoe Perches void sock_prot_inuse_add(struct net *net, struct proto *prot, int inc);
134269336bd2SJoe Perches int sock_prot_inuse_get(struct net *net, struct proto *proto);
134365f76517SEric Dumazet #else
1344dc6b9b78SEric Dumazet static inline void sock_prot_inuse_add(struct net *net, struct proto *prot,
1345c29a0bc4SPavel Emelyanov 		int inc)
134665f76517SEric Dumazet {
134765f76517SEric Dumazet }
134865f76517SEric Dumazet #endif
134965f76517SEric Dumazet 
13501da177e4SLinus Torvalds 
1351614c6cb4SArnaldo Carvalho de Melo /* With per-bucket locks this operation is not-atomic, so that
1352614c6cb4SArnaldo Carvalho de Melo  * this version is not worse.
1353614c6cb4SArnaldo Carvalho de Melo  */
1354614c6cb4SArnaldo Carvalho de Melo static inline void __sk_prot_rehash(struct sock *sk)
1355614c6cb4SArnaldo Carvalho de Melo {
1356614c6cb4SArnaldo Carvalho de Melo 	sk->sk_prot->unhash(sk);
1357614c6cb4SArnaldo Carvalho de Melo 	sk->sk_prot->hash(sk);
1358614c6cb4SArnaldo Carvalho de Melo }
1359614c6cb4SArnaldo Carvalho de Melo 
1360fcbdf09dSOctavian Purdila void sk_prot_clear_portaddr_nulls(struct sock *sk, int size);
1361fcbdf09dSOctavian Purdila 
13621da177e4SLinus Torvalds /* About 10 seconds */
13631da177e4SLinus Torvalds #define SOCK_DESTROY_TIME (10*HZ)
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds /* Sockets 0-1023 can't be bound to unless you are superuser */
13661da177e4SLinus Torvalds #define PROT_SOCK	1024
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds #define SHUTDOWN_MASK	3
13691da177e4SLinus Torvalds #define RCV_SHUTDOWN	1
13701da177e4SLinus Torvalds #define SEND_SHUTDOWN	2
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds #define SOCK_SNDBUF_LOCK	1
13731da177e4SLinus Torvalds #define SOCK_RCVBUF_LOCK	2
13741da177e4SLinus Torvalds #define SOCK_BINDADDR_LOCK	4
13751da177e4SLinus Torvalds #define SOCK_BINDPORT_LOCK	8
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds /* sock_iocb: used to kick off async processing of socket ios */
13781da177e4SLinus Torvalds struct sock_iocb {
13791da177e4SLinus Torvalds 	struct list_head	list;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	int			flags;
13821da177e4SLinus Torvalds 	int			size;
13831da177e4SLinus Torvalds 	struct socket		*sock;
13841da177e4SLinus Torvalds 	struct sock		*sk;
13851da177e4SLinus Torvalds 	struct scm_cookie	*scm;
13861da177e4SLinus Torvalds 	struct msghdr		*msg, async_msg;
13871da177e4SLinus Torvalds 	struct kiocb		*kiocb;
13881da177e4SLinus Torvalds };
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds static inline struct sock_iocb *kiocb_to_siocb(struct kiocb *iocb)
13911da177e4SLinus Torvalds {
13921da177e4SLinus Torvalds 	return (struct sock_iocb *)iocb->private;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds static inline struct kiocb *siocb_to_kiocb(struct sock_iocb *si)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	return si->kiocb;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds struct socket_alloc {
14011da177e4SLinus Torvalds 	struct socket socket;
14021da177e4SLinus Torvalds 	struct inode vfs_inode;
14031da177e4SLinus Torvalds };
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds static inline struct socket *SOCKET_I(struct inode *inode)
14061da177e4SLinus Torvalds {
14071da177e4SLinus Torvalds 	return &container_of(inode, struct socket_alloc, vfs_inode)->socket;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds static inline struct inode *SOCK_INODE(struct socket *socket)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	return &container_of(socket, struct socket_alloc, socket)->vfs_inode;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14153ab224beSHideo Aoki /*
14163ab224beSHideo Aoki  * Functions for memory accounting
14173ab224beSHideo Aoki  */
141869336bd2SJoe Perches int __sk_mem_schedule(struct sock *sk, int size, int kind);
141969336bd2SJoe Perches void __sk_mem_reclaim(struct sock *sk);
14201da177e4SLinus Torvalds 
14213ab224beSHideo Aoki #define SK_MEM_QUANTUM ((int)PAGE_SIZE)
14223ab224beSHideo Aoki #define SK_MEM_QUANTUM_SHIFT ilog2(SK_MEM_QUANTUM)
14233ab224beSHideo Aoki #define SK_MEM_SEND	0
14243ab224beSHideo Aoki #define SK_MEM_RECV	1
14251da177e4SLinus Torvalds 
14263ab224beSHideo Aoki static inline int sk_mem_pages(int amt)
14271da177e4SLinus Torvalds {
14283ab224beSHideo Aoki 	return (amt + SK_MEM_QUANTUM - 1) >> SK_MEM_QUANTUM_SHIFT;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
1431dc6b9b78SEric Dumazet static inline bool sk_has_account(struct sock *sk)
14321da177e4SLinus Torvalds {
14333ab224beSHideo Aoki 	/* return true if protocol supports memory accounting */
14343ab224beSHideo Aoki 	return !!sk->sk_prot->memory_allocated;
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds 
1437dc6b9b78SEric Dumazet static inline bool sk_wmem_schedule(struct sock *sk, int size)
14381da177e4SLinus Torvalds {
14393ab224beSHideo Aoki 	if (!sk_has_account(sk))
1440dc6b9b78SEric Dumazet 		return true;
1441d80d99d6SHerbert Xu 	return size <= sk->sk_forward_alloc ||
14423ab224beSHideo Aoki 		__sk_mem_schedule(sk, size, SK_MEM_SEND);
14433ab224beSHideo Aoki }
14443ab224beSHideo Aoki 
1445c76562b6SMel Gorman static inline bool
144635c448a8SChuck Lever sk_rmem_schedule(struct sock *sk, struct sk_buff *skb, int size)
14473ab224beSHideo Aoki {
14483ab224beSHideo Aoki 	if (!sk_has_account(sk))
1449dc6b9b78SEric Dumazet 		return true;
14503ab224beSHideo Aoki 	return size<= sk->sk_forward_alloc ||
1451c76562b6SMel Gorman 		__sk_mem_schedule(sk, size, SK_MEM_RECV) ||
1452c76562b6SMel Gorman 		skb_pfmemalloc(skb);
14533ab224beSHideo Aoki }
14543ab224beSHideo Aoki 
14553ab224beSHideo Aoki static inline void sk_mem_reclaim(struct sock *sk)
14563ab224beSHideo Aoki {
14573ab224beSHideo Aoki 	if (!sk_has_account(sk))
14583ab224beSHideo Aoki 		return;
14593ab224beSHideo Aoki 	if (sk->sk_forward_alloc >= SK_MEM_QUANTUM)
14603ab224beSHideo Aoki 		__sk_mem_reclaim(sk);
14613ab224beSHideo Aoki }
14623ab224beSHideo Aoki 
14639993e7d3SDavid S. Miller static inline void sk_mem_reclaim_partial(struct sock *sk)
14649993e7d3SDavid S. Miller {
14659993e7d3SDavid S. Miller 	if (!sk_has_account(sk))
14669993e7d3SDavid S. Miller 		return;
14679993e7d3SDavid S. Miller 	if (sk->sk_forward_alloc > SK_MEM_QUANTUM)
14689993e7d3SDavid S. Miller 		__sk_mem_reclaim(sk);
14699993e7d3SDavid S. Miller }
14709993e7d3SDavid S. Miller 
14713ab224beSHideo Aoki static inline void sk_mem_charge(struct sock *sk, int size)
14723ab224beSHideo Aoki {
14733ab224beSHideo Aoki 	if (!sk_has_account(sk))
14743ab224beSHideo Aoki 		return;
14753ab224beSHideo Aoki 	sk->sk_forward_alloc -= size;
14763ab224beSHideo Aoki }
14773ab224beSHideo Aoki 
14783ab224beSHideo Aoki static inline void sk_mem_uncharge(struct sock *sk, int size)
14793ab224beSHideo Aoki {
14803ab224beSHideo Aoki 	if (!sk_has_account(sk))
14813ab224beSHideo Aoki 		return;
14823ab224beSHideo Aoki 	sk->sk_forward_alloc += size;
14833ab224beSHideo Aoki }
14843ab224beSHideo Aoki 
14853ab224beSHideo Aoki static inline void sk_wmem_free_skb(struct sock *sk, struct sk_buff *skb)
14863ab224beSHideo Aoki {
14873ab224beSHideo Aoki 	sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
14883ab224beSHideo Aoki 	sk->sk_wmem_queued -= skb->truesize;
14893ab224beSHideo Aoki 	sk_mem_uncharge(sk, skb->truesize);
14903ab224beSHideo Aoki 	__kfree_skb(skb);
1491d80d99d6SHerbert Xu }
1492d80d99d6SHerbert Xu 
14931da177e4SLinus Torvalds /* Used by processes to "lock" a socket state, so that
14941da177e4SLinus Torvalds  * interrupts and bottom half handlers won't change it
14951da177e4SLinus Torvalds  * from under us. It essentially blocks any incoming
14961da177e4SLinus Torvalds  * packets, so that we won't get any new data or any
14971da177e4SLinus Torvalds  * packets that change the state of the socket.
14981da177e4SLinus Torvalds  *
14991da177e4SLinus Torvalds  * While locked, BH processing will add new packets to
15001da177e4SLinus Torvalds  * the backlog queue.  This queue is processed by the
15011da177e4SLinus Torvalds  * owner of the socket lock right before it is released.
15021da177e4SLinus Torvalds  *
15031da177e4SLinus Torvalds  * Since ~2.3.5 it is also exclusive sleep lock serializing
15041da177e4SLinus Torvalds  * accesses from user process context.
15051da177e4SLinus Torvalds  */
1506d2e9117cSJohn Heffner #define sock_owned_by_user(sk)	((sk)->sk_lock.owned)
15071da177e4SLinus Torvalds 
1508c3f9b018SEric Dumazet static inline void sock_release_ownership(struct sock *sk)
1509c3f9b018SEric Dumazet {
1510c3f9b018SEric Dumazet 	sk->sk_lock.owned = 0;
1511c3f9b018SEric Dumazet }
1512c3f9b018SEric Dumazet 
1513ed07536eSPeter Zijlstra /*
1514ed07536eSPeter Zijlstra  * Macro so as to not evaluate some arguments when
1515ed07536eSPeter Zijlstra  * lockdep is not enabled.
1516ed07536eSPeter Zijlstra  *
1517ed07536eSPeter Zijlstra  * Mark both the sk_lock and the sk_lock.slock as a
1518ed07536eSPeter Zijlstra  * per-address-family lock class.
1519ed07536eSPeter Zijlstra  */
1520ed07536eSPeter Zijlstra #define sock_lock_init_class_and_name(sk, sname, skey, name, key)	\
1521ed07536eSPeter Zijlstra do {									\
1522d2e9117cSJohn Heffner 	sk->sk_lock.owned = 0;						\
1523ed07536eSPeter Zijlstra 	init_waitqueue_head(&sk->sk_lock.wq);				\
1524ed07536eSPeter Zijlstra 	spin_lock_init(&(sk)->sk_lock.slock);				\
1525ed07536eSPeter Zijlstra 	debug_check_no_locks_freed((void *)&(sk)->sk_lock,		\
1526ed07536eSPeter Zijlstra 			sizeof((sk)->sk_lock));				\
1527ed07536eSPeter Zijlstra 	lockdep_set_class_and_name(&(sk)->sk_lock.slock,		\
1528ed07536eSPeter Zijlstra 				(skey), (sname));				\
1529ed07536eSPeter Zijlstra 	lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0);	\
1530ed07536eSPeter Zijlstra } while (0)
1531ed07536eSPeter Zijlstra 
153269336bd2SJoe Perches void lock_sock_nested(struct sock *sk, int subclass);
1533fcc70d5fSPeter Zijlstra 
1534fcc70d5fSPeter Zijlstra static inline void lock_sock(struct sock *sk)
1535fcc70d5fSPeter Zijlstra {
1536fcc70d5fSPeter Zijlstra 	lock_sock_nested(sk, 0);
1537fcc70d5fSPeter Zijlstra }
1538fcc70d5fSPeter Zijlstra 
153969336bd2SJoe Perches void release_sock(struct sock *sk);
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds /* BH context may only use the following locking interface. */
15421da177e4SLinus Torvalds #define bh_lock_sock(__sk)	spin_lock(&((__sk)->sk_lock.slock))
1543c6366184SIngo Molnar #define bh_lock_sock_nested(__sk) \
1544c6366184SIngo Molnar 				spin_lock_nested(&((__sk)->sk_lock.slock), \
1545c6366184SIngo Molnar 				SINGLE_DEPTH_NESTING)
15461da177e4SLinus Torvalds #define bh_unlock_sock(__sk)	spin_unlock(&((__sk)->sk_lock.slock))
15471da177e4SLinus Torvalds 
154869336bd2SJoe Perches bool lock_sock_fast(struct sock *sk);
15498a74ad60SEric Dumazet /**
15508a74ad60SEric Dumazet  * unlock_sock_fast - complement of lock_sock_fast
15518a74ad60SEric Dumazet  * @sk: socket
15528a74ad60SEric Dumazet  * @slow: slow mode
15538a74ad60SEric Dumazet  *
15548a74ad60SEric Dumazet  * fast unlock socket for user context.
15558a74ad60SEric Dumazet  * If slow mode is on, we call regular release_sock()
15568a74ad60SEric Dumazet  */
15578a74ad60SEric Dumazet static inline void unlock_sock_fast(struct sock *sk, bool slow)
15584b0b72f7SEric Dumazet {
15598a74ad60SEric Dumazet 	if (slow)
15608a74ad60SEric Dumazet 		release_sock(sk);
15618a74ad60SEric Dumazet 	else
15624b0b72f7SEric Dumazet 		spin_unlock_bh(&sk->sk_lock.slock);
15634b0b72f7SEric Dumazet }
15644b0b72f7SEric Dumazet 
15658a74ad60SEric Dumazet 
156669336bd2SJoe Perches struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
15676257ff21SPavel Emelyanov 		      struct proto *prot);
156869336bd2SJoe Perches void sk_free(struct sock *sk);
156969336bd2SJoe Perches void sk_release_kernel(struct sock *sk);
157069336bd2SJoe Perches struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority);
15711da177e4SLinus Torvalds 
157269336bd2SJoe Perches struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
1573dd0fc66fSAl Viro 			     gfp_t priority);
157469336bd2SJoe Perches void sock_wfree(struct sk_buff *skb);
157569336bd2SJoe Perches void skb_orphan_partial(struct sk_buff *skb);
157669336bd2SJoe Perches void sock_rfree(struct sk_buff *skb);
157762bccb8cSAlexander Duyck void sock_efree(struct sk_buff *skb);
157882eabd9eSAlexander Duyck #ifdef CONFIG_INET
157969336bd2SJoe Perches void sock_edemux(struct sk_buff *skb);
158082eabd9eSAlexander Duyck #else
158182eabd9eSAlexander Duyck #define sock_edemux(skb) sock_efree(skb)
158282eabd9eSAlexander Duyck #endif
15831da177e4SLinus Torvalds 
158469336bd2SJoe Perches int sock_setsockopt(struct socket *sock, int level, int op,
158569336bd2SJoe Perches 		    char __user *optval, unsigned int optlen);
15861da177e4SLinus Torvalds 
158769336bd2SJoe Perches int sock_getsockopt(struct socket *sock, int level, int op,
158869336bd2SJoe Perches 		    char __user *optval, int __user *optlen);
158969336bd2SJoe Perches struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
159069336bd2SJoe Perches 				    int noblock, int *errcode);
159169336bd2SJoe Perches struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
159269336bd2SJoe Perches 				     unsigned long data_len, int noblock,
159369336bd2SJoe Perches 				     int *errcode, int max_page_order);
159469336bd2SJoe Perches void *sock_kmalloc(struct sock *sk, int size, gfp_t priority);
159569336bd2SJoe Perches void sock_kfree_s(struct sock *sk, void *mem, int size);
159679e88659SDaniel Borkmann void sock_kzfree_s(struct sock *sk, void *mem, int size);
159769336bd2SJoe Perches void sk_send_sigurg(struct sock *sk);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds /*
16001da177e4SLinus Torvalds  * Functions to fill in entries in struct proto_ops when a protocol
16011da177e4SLinus Torvalds  * does not implement a particular function.
16021da177e4SLinus Torvalds  */
160369336bd2SJoe Perches int sock_no_bind(struct socket *, struct sockaddr *, int);
160469336bd2SJoe Perches int sock_no_connect(struct socket *, struct sockaddr *, int, int);
160569336bd2SJoe Perches int sock_no_socketpair(struct socket *, struct socket *);
160669336bd2SJoe Perches int sock_no_accept(struct socket *, struct socket *, int);
160769336bd2SJoe Perches int sock_no_getname(struct socket *, struct sockaddr *, int *, int);
160869336bd2SJoe Perches unsigned int sock_no_poll(struct file *, struct socket *,
16091da177e4SLinus Torvalds 			  struct poll_table_struct *);
161069336bd2SJoe Perches int sock_no_ioctl(struct socket *, unsigned int, unsigned long);
161169336bd2SJoe Perches int sock_no_listen(struct socket *, int);
161269336bd2SJoe Perches int sock_no_shutdown(struct socket *, int);
161369336bd2SJoe Perches int sock_no_getsockopt(struct socket *, int , int, char __user *, int __user *);
161469336bd2SJoe Perches int sock_no_setsockopt(struct socket *, int, int, char __user *, unsigned int);
161569336bd2SJoe Perches int sock_no_sendmsg(struct kiocb *, struct socket *, struct msghdr *, size_t);
161669336bd2SJoe Perches int sock_no_recvmsg(struct kiocb *, struct socket *, struct msghdr *, size_t,
161769336bd2SJoe Perches 		    int);
161869336bd2SJoe Perches int sock_no_mmap(struct file *file, struct socket *sock,
16191da177e4SLinus Torvalds 		 struct vm_area_struct *vma);
162069336bd2SJoe Perches ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset,
162169336bd2SJoe Perches 			 size_t size, int flags);
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds /*
16241da177e4SLinus Torvalds  * Functions to fill in entries in struct proto_ops when a protocol
16251da177e4SLinus Torvalds  * uses the inet style.
16261da177e4SLinus Torvalds  */
162769336bd2SJoe Perches int sock_common_getsockopt(struct socket *sock, int level, int optname,
16281da177e4SLinus Torvalds 				  char __user *optval, int __user *optlen);
162969336bd2SJoe Perches int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock,
16301da177e4SLinus Torvalds 			       struct msghdr *msg, size_t size, int flags);
163169336bd2SJoe Perches int sock_common_setsockopt(struct socket *sock, int level, int optname,
1632b7058842SDavid S. Miller 				  char __user *optval, unsigned int optlen);
163369336bd2SJoe Perches int compat_sock_common_getsockopt(struct socket *sock, int level,
16343fdadf7dSDmitry Mishin 		int optname, char __user *optval, int __user *optlen);
163569336bd2SJoe Perches int compat_sock_common_setsockopt(struct socket *sock, int level,
1636b7058842SDavid S. Miller 		int optname, char __user *optval, unsigned int optlen);
16371da177e4SLinus Torvalds 
163869336bd2SJoe Perches void sk_common_release(struct sock *sk);
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds /*
16411da177e4SLinus Torvalds  *	Default socket callbacks and setup code
16421da177e4SLinus Torvalds  */
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds /* Initialise core socket variables */
164569336bd2SJoe Perches void sock_init_data(struct socket *sock, struct sock *sk);
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds /*
16481da177e4SLinus Torvalds  * Socket reference counting postulates.
16491da177e4SLinus Torvalds  *
16501da177e4SLinus Torvalds  * * Each user of socket SHOULD hold a reference count.
16511da177e4SLinus Torvalds  * * Each access point to socket (an hash table bucket, reference from a list,
16521da177e4SLinus Torvalds  *   running timer, skb in flight MUST hold a reference count.
16531da177e4SLinus Torvalds  * * When reference count hits 0, it means it will never increase back.
16541da177e4SLinus Torvalds  * * When reference count hits 0, it means that no references from
16551da177e4SLinus Torvalds  *   outside exist to this socket and current process on current CPU
16561da177e4SLinus Torvalds  *   is last user and may/should destroy this socket.
16571da177e4SLinus Torvalds  * * sk_free is called from any context: process, BH, IRQ. When
16581da177e4SLinus Torvalds  *   it is called, socket has no references from outside -> sk_free
16591da177e4SLinus Torvalds  *   may release descendant resources allocated by the socket, but
16601da177e4SLinus Torvalds  *   to the time when it is called, socket is NOT referenced by any
16611da177e4SLinus Torvalds  *   hash tables, lists etc.
16621da177e4SLinus Torvalds  * * Packets, delivered from outside (from network or from another process)
16631da177e4SLinus Torvalds  *   and enqueued on receive/error queues SHOULD NOT grab reference count,
16641da177e4SLinus Torvalds  *   when they sit in queue. Otherwise, packets will leak to hole, when
16651da177e4SLinus Torvalds  *   socket is looked up by one cpu and unhasing is made by another CPU.
16661da177e4SLinus Torvalds  *   It is true for udp/raw, netlink (leak to receive and error queues), tcp
16671da177e4SLinus Torvalds  *   (leak to backlog). Packet socket does all the processing inside
16681da177e4SLinus Torvalds  *   BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets
16691da177e4SLinus Torvalds  *   use separate SMP lock, so that they are prone too.
16701da177e4SLinus Torvalds  */
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds /* Ungrab socket and destroy it, if it was the last reference. */
16731da177e4SLinus Torvalds static inline void sock_put(struct sock *sk)
16741da177e4SLinus Torvalds {
16751da177e4SLinus Torvalds 	if (atomic_dec_and_test(&sk->sk_refcnt))
16761da177e4SLinus Torvalds 		sk_free(sk);
16771da177e4SLinus Torvalds }
167805dbc7b5SEric Dumazet /* Generic version of sock_put(), dealing with all sockets
167905dbc7b5SEric Dumazet  * (TCP_TIMEWAIT, ESTABLISHED...)
168005dbc7b5SEric Dumazet  */
168105dbc7b5SEric Dumazet void sock_gen_put(struct sock *sk);
16821da177e4SLinus Torvalds 
168369336bd2SJoe Perches int sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested);
168425995ff5SArnaldo Carvalho de Melo 
1685e022f0b4SKrishna Kumar static inline void sk_tx_queue_set(struct sock *sk, int tx_queue)
1686e022f0b4SKrishna Kumar {
1687e022f0b4SKrishna Kumar 	sk->sk_tx_queue_mapping = tx_queue;
1688e022f0b4SKrishna Kumar }
1689e022f0b4SKrishna Kumar 
1690e022f0b4SKrishna Kumar static inline void sk_tx_queue_clear(struct sock *sk)
1691e022f0b4SKrishna Kumar {
1692e022f0b4SKrishna Kumar 	sk->sk_tx_queue_mapping = -1;
1693e022f0b4SKrishna Kumar }
1694e022f0b4SKrishna Kumar 
1695e022f0b4SKrishna Kumar static inline int sk_tx_queue_get(const struct sock *sk)
1696e022f0b4SKrishna Kumar {
1697b0f77d0eSTom Herbert 	return sk ? sk->sk_tx_queue_mapping : -1;
1698e022f0b4SKrishna Kumar }
1699e022f0b4SKrishna Kumar 
1700972692e0SDavid S. Miller static inline void sk_set_socket(struct sock *sk, struct socket *sock)
1701972692e0SDavid S. Miller {
1702e022f0b4SKrishna Kumar 	sk_tx_queue_clear(sk);
1703972692e0SDavid S. Miller 	sk->sk_socket = sock;
1704972692e0SDavid S. Miller }
1705972692e0SDavid S. Miller 
1706aa395145SEric Dumazet static inline wait_queue_head_t *sk_sleep(struct sock *sk)
1707aa395145SEric Dumazet {
1708eaefd110SEric Dumazet 	BUILD_BUG_ON(offsetof(struct socket_wq, wait) != 0);
1709eaefd110SEric Dumazet 	return &rcu_dereference_raw(sk->sk_wq)->wait;
1710aa395145SEric Dumazet }
17111da177e4SLinus Torvalds /* Detach socket from process context.
17121da177e4SLinus Torvalds  * Announce socket dead, detach it from wait queue and inode.
17131da177e4SLinus Torvalds  * Note that parent inode held reference count on this struct sock,
17141da177e4SLinus Torvalds  * we do not release it in this function, because protocol
17151da177e4SLinus Torvalds  * probably wants some additional cleanups or even continuing
17161da177e4SLinus Torvalds  * to work with this socket (TCP).
17171da177e4SLinus Torvalds  */
17181da177e4SLinus Torvalds static inline void sock_orphan(struct sock *sk)
17191da177e4SLinus Torvalds {
17201da177e4SLinus Torvalds 	write_lock_bh(&sk->sk_callback_lock);
17211da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_DEAD);
1722972692e0SDavid S. Miller 	sk_set_socket(sk, NULL);
172343815482SEric Dumazet 	sk->sk_wq  = NULL;
17241da177e4SLinus Torvalds 	write_unlock_bh(&sk->sk_callback_lock);
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds static inline void sock_graft(struct sock *sk, struct socket *parent)
17281da177e4SLinus Torvalds {
17291da177e4SLinus Torvalds 	write_lock_bh(&sk->sk_callback_lock);
1730eaefd110SEric Dumazet 	sk->sk_wq = parent->wq;
17311da177e4SLinus Torvalds 	parent->sk = sk;
1732972692e0SDavid S. Miller 	sk_set_socket(sk, parent);
17334237c75cSVenkat Yekkirala 	security_sock_graft(sk, parent);
17341da177e4SLinus Torvalds 	write_unlock_bh(&sk->sk_callback_lock);
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
173769336bd2SJoe Perches kuid_t sock_i_uid(struct sock *sk);
173869336bd2SJoe Perches unsigned long sock_i_ino(struct sock *sk);
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds static inline struct dst_entry *
17411da177e4SLinus Torvalds __sk_dst_get(struct sock *sk)
17421da177e4SLinus Torvalds {
1743d8bf4ca9SMichal Hocko 	return rcu_dereference_check(sk->sk_dst_cache, sock_owned_by_user(sk) ||
1744f68c224fSEric Dumazet 						       lockdep_is_held(&sk->sk_lock.slock));
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds static inline struct dst_entry *
17481da177e4SLinus Torvalds sk_dst_get(struct sock *sk)
17491da177e4SLinus Torvalds {
17501da177e4SLinus Torvalds 	struct dst_entry *dst;
17511da177e4SLinus Torvalds 
1752b6c6712aSEric Dumazet 	rcu_read_lock();
1753b6c6712aSEric Dumazet 	dst = rcu_dereference(sk->sk_dst_cache);
1754f8864972SEric Dumazet 	if (dst && !atomic_inc_not_zero(&dst->__refcnt))
1755f8864972SEric Dumazet 		dst = NULL;
1756b6c6712aSEric Dumazet 	rcu_read_unlock();
17571da177e4SLinus Torvalds 	return dst;
17581da177e4SLinus Torvalds }
17591da177e4SLinus Torvalds 
1760b6c6712aSEric Dumazet static inline void dst_negative_advice(struct sock *sk)
1761b6c6712aSEric Dumazet {
1762b6c6712aSEric Dumazet 	struct dst_entry *ndst, *dst = __sk_dst_get(sk);
1763b6c6712aSEric Dumazet 
1764b6c6712aSEric Dumazet 	if (dst && dst->ops->negative_advice) {
1765b6c6712aSEric Dumazet 		ndst = dst->ops->negative_advice(dst);
1766b6c6712aSEric Dumazet 
1767b6c6712aSEric Dumazet 		if (ndst != dst) {
1768b6c6712aSEric Dumazet 			rcu_assign_pointer(sk->sk_dst_cache, ndst);
17690a6957e7SZHAO Gang 			sk_tx_queue_clear(sk);
1770b6c6712aSEric Dumazet 		}
1771b6c6712aSEric Dumazet 	}
1772b6c6712aSEric Dumazet }
1773b6c6712aSEric Dumazet 
17741da177e4SLinus Torvalds static inline void
17751da177e4SLinus Torvalds __sk_dst_set(struct sock *sk, struct dst_entry *dst)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	struct dst_entry *old_dst;
17781da177e4SLinus Torvalds 
1779e022f0b4SKrishna Kumar 	sk_tx_queue_clear(sk);
17800b53ff2eSEric Dumazet 	/*
17810b53ff2eSEric Dumazet 	 * This can be called while sk is owned by the caller only,
17820b53ff2eSEric Dumazet 	 * with no state that can be checked in a rcu_dereference_check() cond
17830b53ff2eSEric Dumazet 	 */
17840b53ff2eSEric Dumazet 	old_dst = rcu_dereference_raw(sk->sk_dst_cache);
1785b6c6712aSEric Dumazet 	rcu_assign_pointer(sk->sk_dst_cache, dst);
17861da177e4SLinus Torvalds 	dst_release(old_dst);
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds static inline void
17901da177e4SLinus Torvalds sk_dst_set(struct sock *sk, struct dst_entry *dst)
17911da177e4SLinus Torvalds {
17927f502361SEric Dumazet 	struct dst_entry *old_dst;
17937f502361SEric Dumazet 
17947f502361SEric Dumazet 	sk_tx_queue_clear(sk);
17955925a055SEric Dumazet 	old_dst = xchg((__force struct dst_entry **)&sk->sk_dst_cache, dst);
17967f502361SEric Dumazet 	dst_release(old_dst);
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds static inline void
18001da177e4SLinus Torvalds __sk_dst_reset(struct sock *sk)
18011da177e4SLinus Torvalds {
1802b6c6712aSEric Dumazet 	__sk_dst_set(sk, NULL);
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds static inline void
18061da177e4SLinus Torvalds sk_dst_reset(struct sock *sk)
18071da177e4SLinus Torvalds {
18087f502361SEric Dumazet 	sk_dst_set(sk, NULL);
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
181169336bd2SJoe Perches struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);
18121da177e4SLinus Torvalds 
181369336bd2SJoe Perches struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
18141da177e4SLinus Torvalds 
1815dc6b9b78SEric Dumazet static inline bool sk_can_gso(const struct sock *sk)
1816bcd76111SHerbert Xu {
1817bcd76111SHerbert Xu 	return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type);
1818bcd76111SHerbert Xu }
1819bcd76111SHerbert Xu 
182069336bd2SJoe Perches void sk_setup_caps(struct sock *sk, struct dst_entry *dst);
18216cbb0df7SArnaldo Carvalho de Melo 
1822c8f44affSMichał Mirosław static inline void sk_nocaps_add(struct sock *sk, netdev_features_t flags)
1823a465419bSEric Dumazet {
1824a465419bSEric Dumazet 	sk->sk_route_nocaps |= flags;
1825a465419bSEric Dumazet 	sk->sk_route_caps &= ~flags;
1826a465419bSEric Dumazet }
1827a465419bSEric Dumazet 
1828c6e1a0d1STom Herbert static inline int skb_do_copy_data_nocache(struct sock *sk, struct sk_buff *skb,
1829c6e1a0d1STom Herbert 					   char __user *from, char *to,
1830912d398dSWei Yongjun 					   int copy, int offset)
1831c6e1a0d1STom Herbert {
1832c6e1a0d1STom Herbert 	if (skb->ip_summed == CHECKSUM_NONE) {
1833c6e1a0d1STom Herbert 		int err = 0;
1834c6e1a0d1STom Herbert 		__wsum csum = csum_and_copy_from_user(from, to, copy, 0, &err);
1835c6e1a0d1STom Herbert 		if (err)
1836c6e1a0d1STom Herbert 			return err;
1837912d398dSWei Yongjun 		skb->csum = csum_block_add(skb->csum, csum, offset);
1838c6e1a0d1STom Herbert 	} else if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
1839c6e1a0d1STom Herbert 		if (!access_ok(VERIFY_READ, from, copy) ||
1840c6e1a0d1STom Herbert 		    __copy_from_user_nocache(to, from, copy))
1841c6e1a0d1STom Herbert 			return -EFAULT;
1842c6e1a0d1STom Herbert 	} else if (copy_from_user(to, from, copy))
1843c6e1a0d1STom Herbert 		return -EFAULT;
1844c6e1a0d1STom Herbert 
1845c6e1a0d1STom Herbert 	return 0;
1846c6e1a0d1STom Herbert }
1847c6e1a0d1STom Herbert 
1848c6e1a0d1STom Herbert static inline int skb_add_data_nocache(struct sock *sk, struct sk_buff *skb,
1849c6e1a0d1STom Herbert 				       char __user *from, int copy)
1850c6e1a0d1STom Herbert {
1851912d398dSWei Yongjun 	int err, offset = skb->len;
1852c6e1a0d1STom Herbert 
1853912d398dSWei Yongjun 	err = skb_do_copy_data_nocache(sk, skb, from, skb_put(skb, copy),
1854912d398dSWei Yongjun 				       copy, offset);
1855c6e1a0d1STom Herbert 	if (err)
1856912d398dSWei Yongjun 		__skb_trim(skb, offset);
1857c6e1a0d1STom Herbert 
1858c6e1a0d1STom Herbert 	return err;
1859c6e1a0d1STom Herbert }
1860c6e1a0d1STom Herbert 
1861c6e1a0d1STom Herbert static inline int skb_copy_to_page_nocache(struct sock *sk, char __user *from,
1862c6e1a0d1STom Herbert 					   struct sk_buff *skb,
1863c6e1a0d1STom Herbert 					   struct page *page,
1864c6e1a0d1STom Herbert 					   int off, int copy)
1865c6e1a0d1STom Herbert {
1866c6e1a0d1STom Herbert 	int err;
1867c6e1a0d1STom Herbert 
1868912d398dSWei Yongjun 	err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) + off,
1869912d398dSWei Yongjun 				       copy, skb->len);
1870c6e1a0d1STom Herbert 	if (err)
1871c6e1a0d1STom Herbert 		return err;
1872c6e1a0d1STom Herbert 
1873c6e1a0d1STom Herbert 	skb->len	     += copy;
1874c6e1a0d1STom Herbert 	skb->data_len	     += copy;
1875c6e1a0d1STom Herbert 	skb->truesize	     += copy;
1876c6e1a0d1STom Herbert 	sk->sk_wmem_queued   += copy;
1877c6e1a0d1STom Herbert 	sk_mem_charge(sk, copy);
1878c6e1a0d1STom Herbert 	return 0;
1879c6e1a0d1STom Herbert }
1880c6e1a0d1STom Herbert 
1881c564039fSEric Dumazet /**
1882c564039fSEric Dumazet  * sk_wmem_alloc_get - returns write allocations
1883c564039fSEric Dumazet  * @sk: socket
1884c564039fSEric Dumazet  *
1885c564039fSEric Dumazet  * Returns sk_wmem_alloc minus initial offset of one
1886c564039fSEric Dumazet  */
1887c564039fSEric Dumazet static inline int sk_wmem_alloc_get(const struct sock *sk)
1888c564039fSEric Dumazet {
1889c564039fSEric Dumazet 	return atomic_read(&sk->sk_wmem_alloc) - 1;
1890c564039fSEric Dumazet }
1891c564039fSEric Dumazet 
1892c564039fSEric Dumazet /**
1893c564039fSEric Dumazet  * sk_rmem_alloc_get - returns read allocations
1894c564039fSEric Dumazet  * @sk: socket
1895c564039fSEric Dumazet  *
1896c564039fSEric Dumazet  * Returns sk_rmem_alloc
1897c564039fSEric Dumazet  */
1898c564039fSEric Dumazet static inline int sk_rmem_alloc_get(const struct sock *sk)
1899c564039fSEric Dumazet {
1900c564039fSEric Dumazet 	return atomic_read(&sk->sk_rmem_alloc);
1901c564039fSEric Dumazet }
1902c564039fSEric Dumazet 
1903c564039fSEric Dumazet /**
1904c564039fSEric Dumazet  * sk_has_allocations - check if allocations are outstanding
1905c564039fSEric Dumazet  * @sk: socket
1906c564039fSEric Dumazet  *
1907c564039fSEric Dumazet  * Returns true if socket has write or read allocations
1908c564039fSEric Dumazet  */
1909dc6b9b78SEric Dumazet static inline bool sk_has_allocations(const struct sock *sk)
1910c564039fSEric Dumazet {
1911c564039fSEric Dumazet 	return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
1912c564039fSEric Dumazet }
1913c564039fSEric Dumazet 
1914a57de0b4SJiri Olsa /**
191543815482SEric Dumazet  * wq_has_sleeper - check if there are any waiting processes
1916acfbe96aSRandy Dunlap  * @wq: struct socket_wq
1917a57de0b4SJiri Olsa  *
191843815482SEric Dumazet  * Returns true if socket_wq has waiting processes
1919a57de0b4SJiri Olsa  *
192043815482SEric Dumazet  * The purpose of the wq_has_sleeper and sock_poll_wait is to wrap the memory
1921a57de0b4SJiri Olsa  * barrier call. They were added due to the race found within the tcp code.
1922a57de0b4SJiri Olsa  *
1923a57de0b4SJiri Olsa  * Consider following tcp code paths:
1924a57de0b4SJiri Olsa  *
1925a57de0b4SJiri Olsa  * CPU1                  CPU2
1926a57de0b4SJiri Olsa  *
1927a57de0b4SJiri Olsa  * sys_select            receive packet
1928a57de0b4SJiri Olsa  *   ...                 ...
1929a57de0b4SJiri Olsa  *   __add_wait_queue    update tp->rcv_nxt
1930a57de0b4SJiri Olsa  *   ...                 ...
1931a57de0b4SJiri Olsa  *   tp->rcv_nxt check   sock_def_readable
1932a57de0b4SJiri Olsa  *   ...                 {
193343815482SEric Dumazet  *   schedule               rcu_read_lock();
193443815482SEric Dumazet  *                          wq = rcu_dereference(sk->sk_wq);
193543815482SEric Dumazet  *                          if (wq && waitqueue_active(&wq->wait))
193643815482SEric Dumazet  *                              wake_up_interruptible(&wq->wait)
1937a57de0b4SJiri Olsa  *                          ...
1938a57de0b4SJiri Olsa  *                       }
1939a57de0b4SJiri Olsa  *
1940a57de0b4SJiri Olsa  * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
1941a57de0b4SJiri Olsa  * in its cache, and so does the tp->rcv_nxt update on CPU2 side.  The CPU1
1942a57de0b4SJiri Olsa  * could then endup calling schedule and sleep forever if there are no more
1943a57de0b4SJiri Olsa  * data on the socket.
1944ad462769SJiri Olsa  *
1945a57de0b4SJiri Olsa  */
194643815482SEric Dumazet static inline bool wq_has_sleeper(struct socket_wq *wq)
1947a57de0b4SJiri Olsa {
1948dc6b9b78SEric Dumazet 	/* We need to be sure we are in sync with the
1949a57de0b4SJiri Olsa 	 * add_wait_queue modifications to the wait queue.
1950a57de0b4SJiri Olsa 	 *
1951a57de0b4SJiri Olsa 	 * This memory barrier is paired in the sock_poll_wait.
1952a57de0b4SJiri Olsa 	 */
195343815482SEric Dumazet 	smp_mb();
195443815482SEric Dumazet 	return wq && waitqueue_active(&wq->wait);
1955a57de0b4SJiri Olsa }
1956a57de0b4SJiri Olsa 
1957a57de0b4SJiri Olsa /**
1958a57de0b4SJiri Olsa  * sock_poll_wait - place memory barrier behind the poll_wait call.
1959a57de0b4SJiri Olsa  * @filp:           file
1960a57de0b4SJiri Olsa  * @wait_address:   socket wait queue
1961a57de0b4SJiri Olsa  * @p:              poll_table
1962a57de0b4SJiri Olsa  *
196343815482SEric Dumazet  * See the comments in the wq_has_sleeper function.
1964a57de0b4SJiri Olsa  */
1965a57de0b4SJiri Olsa static inline void sock_poll_wait(struct file *filp,
1966a57de0b4SJiri Olsa 		wait_queue_head_t *wait_address, poll_table *p)
1967a57de0b4SJiri Olsa {
1968626cf236SHans Verkuil 	if (!poll_does_not_wait(p) && wait_address) {
1969a57de0b4SJiri Olsa 		poll_wait(filp, wait_address, p);
1970dc6b9b78SEric Dumazet 		/* We need to be sure we are in sync with the
1971a57de0b4SJiri Olsa 		 * socket flags modification.
1972a57de0b4SJiri Olsa 		 *
197343815482SEric Dumazet 		 * This memory barrier is paired in the wq_has_sleeper.
1974a57de0b4SJiri Olsa 		 */
1975a57de0b4SJiri Olsa 		smp_mb();
1976a57de0b4SJiri Olsa 	}
1977a57de0b4SJiri Olsa }
1978a57de0b4SJiri Olsa 
1979b73c3d0eSTom Herbert static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
1980b73c3d0eSTom Herbert {
1981b73c3d0eSTom Herbert 	if (sk->sk_txhash) {
1982b73c3d0eSTom Herbert 		skb->l4_hash = 1;
1983b73c3d0eSTom Herbert 		skb->hash = sk->sk_txhash;
1984b73c3d0eSTom Herbert 	}
1985b73c3d0eSTom Herbert }
1986b73c3d0eSTom Herbert 
19871da177e4SLinus Torvalds /*
19881da177e4SLinus Torvalds  *	Queue a received datagram if it will fit. Stream and sequenced
19891da177e4SLinus Torvalds  *	protocols can't normally use this as they need to fit buffers in
19901da177e4SLinus Torvalds  *	and play with them.
19911da177e4SLinus Torvalds  *
19921da177e4SLinus Torvalds  *	Inlined as it's very short and called for pretty much every
19931da177e4SLinus Torvalds  *	packet ever received.
19941da177e4SLinus Torvalds  */
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds static inline void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
19971da177e4SLinus Torvalds {
1998d55d87fdSHerbert Xu 	skb_orphan(skb);
19991da177e4SLinus Torvalds 	skb->sk = sk;
20001da177e4SLinus Torvalds 	skb->destructor = sock_wfree;
2001b73c3d0eSTom Herbert 	skb_set_hash_from_sk(skb, sk);
20022b85a34eSEric Dumazet 	/*
20032b85a34eSEric Dumazet 	 * We used to take a refcount on sk, but following operation
20042b85a34eSEric Dumazet 	 * is enough to guarantee sk_free() wont free this sock until
20052b85a34eSEric Dumazet 	 * all in-flight packets are completed
20062b85a34eSEric Dumazet 	 */
20071da177e4SLinus Torvalds 	atomic_add(skb->truesize, &sk->sk_wmem_alloc);
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
20111da177e4SLinus Torvalds {
2012d55d87fdSHerbert Xu 	skb_orphan(skb);
20131da177e4SLinus Torvalds 	skb->sk = sk;
20141da177e4SLinus Torvalds 	skb->destructor = sock_rfree;
20151da177e4SLinus Torvalds 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
20163ab224beSHideo Aoki 	sk_mem_charge(sk, skb->truesize);
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
201969336bd2SJoe Perches void sk_reset_timer(struct sock *sk, struct timer_list *timer,
20201da177e4SLinus Torvalds 		    unsigned long expires);
20211da177e4SLinus Torvalds 
202269336bd2SJoe Perches void sk_stop_timer(struct sock *sk, struct timer_list *timer);
20231da177e4SLinus Torvalds 
202469336bd2SJoe Perches int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
20251da177e4SLinus Torvalds 
202669336bd2SJoe Perches int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb);
2027364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds /*
20301da177e4SLinus Torvalds  *	Recover an error report and clear atomically
20311da177e4SLinus Torvalds  */
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds static inline int sock_error(struct sock *sk)
20341da177e4SLinus Torvalds {
2035c1cbe4b7SBenjamin LaHaise 	int err;
2036c1cbe4b7SBenjamin LaHaise 	if (likely(!sk->sk_err))
2037c1cbe4b7SBenjamin LaHaise 		return 0;
2038c1cbe4b7SBenjamin LaHaise 	err = xchg(&sk->sk_err, 0);
20391da177e4SLinus Torvalds 	return -err;
20401da177e4SLinus Torvalds }
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds static inline unsigned long sock_wspace(struct sock *sk)
20431da177e4SLinus Torvalds {
20441da177e4SLinus Torvalds 	int amt = 0;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
20471da177e4SLinus Torvalds 		amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
20481da177e4SLinus Torvalds 		if (amt < 0)
20491da177e4SLinus Torvalds 			amt = 0;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 	return amt;
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds static inline void sk_wake_async(struct sock *sk, int how, int band)
20551da177e4SLinus Torvalds {
2056bcdce719SEric Dumazet 	if (sock_flag(sk, SOCK_FASYNC))
20571da177e4SLinus Torvalds 		sock_wake_async(sk->sk_socket, how, band);
20581da177e4SLinus Torvalds }
20591da177e4SLinus Torvalds 
2060eea86af6SDaniel Borkmann /* Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
2061eea86af6SDaniel Borkmann  * need sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak.
2062eea86af6SDaniel Borkmann  * Note: for send buffers, TCP works better if we can build two skbs at
2063eea86af6SDaniel Borkmann  * minimum.
20647a91b434SEric Dumazet  */
20659eb5bf83SEric Dumazet #define TCP_SKB_MIN_TRUESIZE	(2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
2066eea86af6SDaniel Borkmann 
2067eea86af6SDaniel Borkmann #define SOCK_MIN_SNDBUF		(TCP_SKB_MIN_TRUESIZE * 2)
2068eea86af6SDaniel Borkmann #define SOCK_MIN_RCVBUF		 TCP_SKB_MIN_TRUESIZE
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds static inline void sk_stream_moderate_sndbuf(struct sock *sk)
20711da177e4SLinus Torvalds {
20721da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) {
20738df09ea3SEric Dumazet 		sk->sk_sndbuf = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
2074eea86af6SDaniel Borkmann 		sk->sk_sndbuf = max_t(u32, sk->sk_sndbuf, SOCK_MIN_SNDBUF);
20751da177e4SLinus Torvalds 	}
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
2078df97c708SPavel Emelyanov struct sk_buff *sk_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp);
20791da177e4SLinus Torvalds 
20805640f768SEric Dumazet /**
20815640f768SEric Dumazet  * sk_page_frag - return an appropriate page_frag
20825640f768SEric Dumazet  * @sk: socket
20835640f768SEric Dumazet  *
20845640f768SEric Dumazet  * If socket allocation mode allows current thread to sleep, it means its
20855640f768SEric Dumazet  * safe to use the per task page_frag instead of the per socket one.
20865640f768SEric Dumazet  */
20875640f768SEric Dumazet static inline struct page_frag *sk_page_frag(struct sock *sk)
20881da177e4SLinus Torvalds {
20895640f768SEric Dumazet 	if (sk->sk_allocation & __GFP_WAIT)
20905640f768SEric Dumazet 		return &current->task_frag;
20911da177e4SLinus Torvalds 
20925640f768SEric Dumazet 	return &sk->sk_frag;
20931da177e4SLinus Torvalds }
20945640f768SEric Dumazet 
209569336bd2SJoe Perches bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag);
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds /*
20981da177e4SLinus Torvalds  *	Default write policy as shown to user space via poll/select/SIGIO
20991da177e4SLinus Torvalds  */
2100dc6b9b78SEric Dumazet static inline bool sock_writeable(const struct sock *sk)
21011da177e4SLinus Torvalds {
21028df09ea3SEric Dumazet 	return atomic_read(&sk->sk_wmem_alloc) < (sk->sk_sndbuf >> 1);
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
2105dd0fc66fSAl Viro static inline gfp_t gfp_any(void)
21061da177e4SLinus Torvalds {
210799709372SAndrew Morton 	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
21081da177e4SLinus Torvalds }
21091da177e4SLinus Torvalds 
2110dc6b9b78SEric Dumazet static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
21111da177e4SLinus Torvalds {
21121da177e4SLinus Torvalds 	return noblock ? 0 : sk->sk_rcvtimeo;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115dc6b9b78SEric Dumazet static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
21161da177e4SLinus Torvalds {
21171da177e4SLinus Torvalds 	return noblock ? 0 : sk->sk_sndtimeo;
21181da177e4SLinus Torvalds }
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
21211da177e4SLinus Torvalds {
21221da177e4SLinus Torvalds 	return (waitall ? len : min_t(int, sk->sk_rcvlowat, len)) ? : 1;
21231da177e4SLinus Torvalds }
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds /* Alas, with timeout socket operations are not restartable.
21261da177e4SLinus Torvalds  * Compare this to poll().
21271da177e4SLinus Torvalds  */
21281da177e4SLinus Torvalds static inline int sock_intr_errno(long timeo)
21291da177e4SLinus Torvalds {
21301da177e4SLinus Torvalds 	return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
213369336bd2SJoe Perches void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
213492f37fd2SEric Dumazet 			   struct sk_buff *skb);
213569336bd2SJoe Perches void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
21366e3e939fSJohannes Berg 			     struct sk_buff *skb);
213792f37fd2SEric Dumazet 
2138dc6b9b78SEric Dumazet static inline void
21391da177e4SLinus Torvalds sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
21401da177e4SLinus Torvalds {
2141b7aa0bf7SEric Dumazet 	ktime_t kt = skb->tstamp;
214220d49473SPatrick Ohly 	struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
2143a61bbcf2SPatrick McHardy 
214420d49473SPatrick Ohly 	/*
214520d49473SPatrick Ohly 	 * generate control messages if
2146b9f40e21SWillem de Bruijn 	 * - receive time stamping in software requested
214720d49473SPatrick Ohly 	 * - software time stamp available and wanted
214820d49473SPatrick Ohly 	 * - hardware time stamps available and wanted
214920d49473SPatrick Ohly 	 */
215020d49473SPatrick Ohly 	if (sock_flag(sk, SOCK_RCVTSTAMP) ||
2151b9f40e21SWillem de Bruijn 	    (sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) ||
2152c199105dSWillem de Bruijn 	    (kt.tv64 && sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) ||
215320d49473SPatrick Ohly 	    (hwtstamps->hwtstamp.tv64 &&
2154b9f40e21SWillem de Bruijn 	     (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)))
215592f37fd2SEric Dumazet 		__sock_recv_timestamp(msg, sk, skb);
215692f37fd2SEric Dumazet 	else
2157b7aa0bf7SEric Dumazet 		sk->sk_stamp = kt;
21586e3e939fSJohannes Berg 
21596e3e939fSJohannes Berg 	if (sock_flag(sk, SOCK_WIFI_STATUS) && skb->wifi_acked_valid)
21606e3e939fSJohannes Berg 		__sock_recv_wifi_status(msg, sk, skb);
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
216369336bd2SJoe Perches void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
2164767dd033SEric Dumazet 			      struct sk_buff *skb);
2165767dd033SEric Dumazet 
2166767dd033SEric Dumazet static inline void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
2167767dd033SEric Dumazet 					  struct sk_buff *skb)
2168767dd033SEric Dumazet {
2169767dd033SEric Dumazet #define FLAGS_TS_OR_DROPS ((1UL << SOCK_RXQ_OVFL)			| \
2170b9f40e21SWillem de Bruijn 			   (1UL << SOCK_RCVTSTAMP))
2171b9f40e21SWillem de Bruijn #define TSFLAGS_ANY	  (SOF_TIMESTAMPING_SOFTWARE			| \
2172b9f40e21SWillem de Bruijn 			   SOF_TIMESTAMPING_RAW_HARDWARE)
2173767dd033SEric Dumazet 
2174b9f40e21SWillem de Bruijn 	if (sk->sk_flags & FLAGS_TS_OR_DROPS || sk->sk_tsflags & TSFLAGS_ANY)
2175767dd033SEric Dumazet 		__sock_recv_ts_and_drops(msg, sk, skb);
2176767dd033SEric Dumazet 	else
2177767dd033SEric Dumazet 		sk->sk_stamp = skb->tstamp;
2178767dd033SEric Dumazet }
21793b885787SNeil Horman 
218067cc0d40SWillem de Bruijn void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags);
218167cc0d40SWillem de Bruijn 
21821da177e4SLinus Torvalds /**
218320d49473SPatrick Ohly  * sock_tx_timestamp - checks whether the outgoing packet is to be time stamped
218420d49473SPatrick Ohly  * @sk:		socket sending this packet
2185140c55d4SEric Dumazet  * @tx_flags:	completed with instructions for time stamping
2186140c55d4SEric Dumazet  *
2187140c55d4SEric Dumazet  * Note : callers should take care of initial *tx_flags value (usually 0)
218820d49473SPatrick Ohly  */
218967cc0d40SWillem de Bruijn static inline void sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
219067cc0d40SWillem de Bruijn {
219167cc0d40SWillem de Bruijn 	if (unlikely(sk->sk_tsflags))
219267cc0d40SWillem de Bruijn 		__sock_tx_timestamp(sk, tx_flags);
219367cc0d40SWillem de Bruijn 	if (unlikely(sock_flag(sk, SOCK_WIFI_STATUS)))
219467cc0d40SWillem de Bruijn 		*tx_flags |= SKBTX_WIFI_STATUS;
219567cc0d40SWillem de Bruijn }
219620d49473SPatrick Ohly 
219720d49473SPatrick Ohly /**
21981da177e4SLinus Torvalds  * sk_eat_skb - Release a skb if it is no longer needed
21994dc3b16bSPavel Pisa  * @sk: socket to eat this skb from
22004dc3b16bSPavel Pisa  * @skb: socket buffer to eat
22011da177e4SLinus Torvalds  *
22021da177e4SLinus Torvalds  * This routine must be called with interrupts disabled or with the socket
22031da177e4SLinus Torvalds  * locked so that the sk_buff queue operation is ok.
22041da177e4SLinus Torvalds */
22057bced397SDan Williams static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb)
22061da177e4SLinus Torvalds {
22071da177e4SLinus Torvalds 	__skb_unlink(skb, &sk->sk_receive_queue);
22081da177e4SLinus Torvalds 	__kfree_skb(skb);
22091da177e4SLinus Torvalds }
22101da177e4SLinus Torvalds 
22113b1e0a65SYOSHIFUJI Hideaki static inline
22123b1e0a65SYOSHIFUJI Hideaki struct net *sock_net(const struct sock *sk)
22133b1e0a65SYOSHIFUJI Hideaki {
2214c2d9ba9bSEric Dumazet 	return read_pnet(&sk->sk_net);
22153b1e0a65SYOSHIFUJI Hideaki }
22163b1e0a65SYOSHIFUJI Hideaki 
22173b1e0a65SYOSHIFUJI Hideaki static inline
2218f5aa23fdSDenis V. Lunev void sock_net_set(struct sock *sk, struct net *net)
22193b1e0a65SYOSHIFUJI Hideaki {
2220c2d9ba9bSEric Dumazet 	write_pnet(&sk->sk_net, net);
22213b1e0a65SYOSHIFUJI Hideaki }
22223b1e0a65SYOSHIFUJI Hideaki 
2223edf02087SDenis V. Lunev /*
2224edf02087SDenis V. Lunev  * Kernel sockets, f.e. rtnl or icmp_socket, are a part of a namespace.
222525985edcSLucas De Marchi  * They should not hold a reference to a namespace in order to allow
2226edf02087SDenis V. Lunev  * to stop it.
2227edf02087SDenis V. Lunev  * Sockets after sk_change_net should be released using sk_release_kernel
2228edf02087SDenis V. Lunev  */
2229edf02087SDenis V. Lunev static inline void sk_change_net(struct sock *sk, struct net *net)
2230edf02087SDenis V. Lunev {
22315812521bSGu Zheng 	struct net *current_net = sock_net(sk);
22325812521bSGu Zheng 
22335812521bSGu Zheng 	if (!net_eq(current_net, net)) {
22345812521bSGu Zheng 		put_net(current_net);
223565a18ec5SDenis V. Lunev 		sock_net_set(sk, hold_net(net));
2236edf02087SDenis V. Lunev 	}
22375812521bSGu Zheng }
2238edf02087SDenis V. Lunev 
223923542618SKOVACS Krisztian static inline struct sock *skb_steal_sock(struct sk_buff *skb)
224023542618SKOVACS Krisztian {
2241efc27f8cSVijay Subramanian 	if (skb->sk) {
224223542618SKOVACS Krisztian 		struct sock *sk = skb->sk;
224323542618SKOVACS Krisztian 
224423542618SKOVACS Krisztian 		skb->destructor = NULL;
224523542618SKOVACS Krisztian 		skb->sk = NULL;
224623542618SKOVACS Krisztian 		return sk;
224723542618SKOVACS Krisztian 	}
224823542618SKOVACS Krisztian 	return NULL;
224923542618SKOVACS Krisztian }
225023542618SKOVACS Krisztian 
225169336bd2SJoe Perches void sock_enable_timestamp(struct sock *sk, int flag);
225269336bd2SJoe Perches int sock_get_timestamp(struct sock *, struct timeval __user *);
225369336bd2SJoe Perches int sock_get_timestampns(struct sock *, struct timespec __user *);
225469336bd2SJoe Perches int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
225569336bd2SJoe Perches 		       int type);
22561da177e4SLinus Torvalds 
2257a3b299daSEric W. Biederman bool sk_ns_capable(const struct sock *sk,
2258a3b299daSEric W. Biederman 		   struct user_namespace *user_ns, int cap);
2259a3b299daSEric W. Biederman bool sk_capable(const struct sock *sk, int cap);
2260a3b299daSEric W. Biederman bool sk_net_capable(const struct sock *sk, int cap);
2261a3b299daSEric W. Biederman 
22621da177e4SLinus Torvalds extern __u32 sysctl_wmem_max;
22631da177e4SLinus Torvalds extern __u32 sysctl_rmem_max;
22641da177e4SLinus Torvalds 
22656baf1f41SDavid S. Miller extern int sysctl_optmem_max;
22666baf1f41SDavid S. Miller 
226720380731SArnaldo Carvalho de Melo extern __u32 sysctl_wmem_default;
226820380731SArnaldo Carvalho de Melo extern __u32 sysctl_rmem_default;
226920380731SArnaldo Carvalho de Melo 
22701da177e4SLinus Torvalds #endif	/* _SOCK_H */
2271