Linux 6.16-rc4
[linux-block.git] / include / net / sock.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Definitions for the AF_INET socket handler.
8 *
9 * Version: @(#)sock.h 1.0.4 05/13/93
10 *
02c30a84 11 * Authors: Ross Biro
1da177e4
LT
12 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13 * Corey Minyard <wf-rch!minyard@relay.EU.net>
14 * Florian La Roche <flla@stud.uni-sb.de>
15 *
16 * Fixes:
17 * Alan Cox : Volatiles in skbuff pointers. See
18 * skbuff comments. May be overdone,
19 * better to prove they can be removed
20 * than the reverse.
21 * Alan Cox : Added a zapped field for tcp to note
22 * a socket is reset and must stay shut up
23 * Alan Cox : New fields for options
24 * Pauline Middelink : identd support
25 * Alan Cox : Eliminate low level recv/recvfrom
26 * David S. Miller : New socket lookup architecture.
27 * Steve Whitehouse: Default routines for sock_ops
28 * Arnaldo C. Melo : removed net_pinfo, tp_pinfo and made
29 * protinfo be just a void pointer, as the
30 * protocol specific parts were moved to
31 * respective headers and ipv4/v6, etc now
32 * use private slabcaches for its socks
33 * Pedro Hortas : New flags field for socket options
1da177e4
LT
34 */
35#ifndef _SOCK_H
36#define _SOCK_H
37
a6b7a407 38#include <linux/hardirq.h>
172589cc 39#include <linux/kernel.h>
1da177e4 40#include <linux/list.h>
88ab1932 41#include <linux/list_nulls.h>
1da177e4
LT
42#include <linux/timer.h>
43#include <linux/cache.h>
3f134619 44#include <linux/bitops.h>
a5b5bb9a 45#include <linux/lockdep.h>
1da177e4
LT
46#include <linux/netdevice.h>
47#include <linux/skbuff.h> /* struct sk_buff */
d7fe0f24 48#include <linux/mm.h>
1da177e4 49#include <linux/security.h>
5a0e3ad6 50#include <linux/slab.h>
c6e1a0d1 51#include <linux/uaccess.h>
3e32cb2e 52#include <linux/page_counter.h>
180d8cd9 53#include <linux/memcontrol.h>
c5905afb 54#include <linux/static_key.h>
40401530 55#include <linux/sched.h>
1ce0bf50 56#include <linux/wait.h>
2a56a1fe 57#include <linux/cgroup-defs.h>
75c119af 58#include <linux/rbtree.h>
88ab1932 59#include <linux/rculist_nulls.h>
a57de0b4 60#include <linux/poll.h>
c8c1bbb6 61#include <linux/sockptr.h>
1c5f2ced 62#include <linux/indirect_call_wrapper.h>
c31504dc 63#include <linux/atomic.h>
41c6d650 64#include <linux/refcount.h>
f35f8219 65#include <linux/llist.h>
1da177e4
LT
66#include <net/dst.h>
67#include <net/checksum.h>
1d0ab253 68#include <net/tcp_states.h>
b9f40e21 69#include <linux/net_tstamp.h>
54dc3e33 70#include <net/l3mdev.h>
04190bf8 71#include <uapi/linux/socket.h>
1da177e4
LT
72
73/*
74 * This structure really needs to be cleaned up.
75 * Most of it is for TCP, and not used by any of
76 * the other protocols.
77 */
78
1da177e4
LT
79/* This is the per-socket lock. The spinlock provides a synchronization
80 * between user contexts and software interrupt processing, whereas the
81 * mini-semaphore synchronizes multiple users amongst themselves.
82 */
1da177e4
LT
83typedef struct {
84 spinlock_t slock;
d2e9117c 85 int owned;
1da177e4 86 wait_queue_head_t wq;
a5b5bb9a
IM
87 /*
88 * We express the mutex-alike socket_lock semantics
89 * to the lock validator by explicitly managing
90 * the slock as a lock variant (in addition to
91 * the slock itself):
92 */
93#ifdef CONFIG_DEBUG_LOCK_ALLOC
94 struct lockdep_map dep_map;
95#endif
1da177e4
LT
96} socket_lock_t;
97
1da177e4 98struct sock;
8feaf0c0 99struct proto;
0eeb8ffc 100struct net;
1da177e4 101
077b393d
ED
102typedef __u32 __bitwise __portpair;
103typedef __u64 __bitwise __addrpair;
104
1da177e4 105/**
4dc3b16b 106 * struct sock_common - minimal network layer representation of sockets
68835aba
ED
107 * @skc_daddr: Foreign IPv4 addr
108 * @skc_rcv_saddr: Bound local IPv4 addr
66256e0b 109 * @skc_addrpair: 8-byte-aligned __u64 union of @skc_daddr & @skc_rcv_saddr
4dc6dc71 110 * @skc_hash: hash value used with various protocol lookup tables
d4cada4a 111 * @skc_u16hashes: two u16 hash values used by UDP lookup tables
ce43b03e
ED
112 * @skc_dport: placeholder for inet_dport/tw_dport
113 * @skc_num: placeholder for inet_num/tw_num
66256e0b 114 * @skc_portpair: __u32 union of @skc_dport & @skc_num
4dc3b16b
PP
115 * @skc_family: network address family
116 * @skc_state: Connection state
117 * @skc_reuse: %SO_REUSEADDR setting
055dc21a 118 * @skc_reuseport: %SO_REUSEPORT setting
66256e0b
RD
119 * @skc_ipv6only: socket is IPV6 only
120 * @skc_net_refcnt: socket is using net ref counting
4dc3b16b 121 * @skc_bound_dev_if: bound device index if != 0
4dc3b16b 122 * @skc_bind_node: bind hash linkage for various protocol lookup tables
512615b6 123 * @skc_portaddr_node: second hash linkage for UDP/UDP-Lite protocol
8feaf0c0 124 * @skc_prot: protocol handlers inside a network family
07feaebf 125 * @skc_net: reference to the network namespace of this socket
66256e0b
RD
126 * @skc_v6_daddr: IPV6 destination address
127 * @skc_v6_rcv_saddr: IPV6 source address
128 * @skc_cookie: socket's cookie value
68835aba
ED
129 * @skc_node: main hash linkage for various protocol lookup tables
130 * @skc_nulls_node: main hash linkage for TCP/UDP/UDP-Lite protocol
131 * @skc_tx_queue_mapping: tx queue number for this connection
c6345ce7 132 * @skc_rx_queue_mapping: rx queue number for this connection
8e5eb54d
ED
133 * @skc_flags: place holder for sk_flags
134 * %SO_LINGER (l_onoff), %SO_BROADCAST, %SO_KEEPALIVE,
135 * %SO_OOBINLINE settings, %SO_TIMESTAMPING settings
66256e0b
RD
136 * @skc_listener: connection request listener socket (aka rsk_listener)
137 * [union with @skc_flags]
138 * @skc_tw_dr: (aka tw_dr) ptr to &struct inet_timewait_death_row
139 * [union with @skc_flags]
70da268b 140 * @skc_incoming_cpu: record/match cpu processing incoming packets
66256e0b
RD
141 * @skc_rcv_wnd: (aka rsk_rcv_wnd) TCP receive window size (possibly scaled)
142 * [union with @skc_incoming_cpu]
143 * @skc_tw_rcv_nxt: (aka tw_rcv_nxt) TCP window next expected seq number
144 * [union with @skc_incoming_cpu]
68835aba 145 * @skc_refcnt: reference count
4dc3b16b
PP
146 *
147 * This is the minimal network layer representation of sockets, the header
8feaf0c0
ACM
148 * for struct sock and struct inet_timewait_sock.
149 */
1da177e4 150struct sock_common {
ce43b03e 151 union {
077b393d 152 __addrpair skc_addrpair;
ce43b03e
ED
153 struct {
154 __be32 skc_daddr;
155 __be32 skc_rcv_saddr;
156 };
157 };
d4cada4a
ED
158 union {
159 unsigned int skc_hash;
160 __u16 skc_u16hashes[2];
161 };
ce43b03e
ED
162 /* skc_dport && skc_num must be grouped as well */
163 union {
077b393d 164 __portpair skc_portpair;
ce43b03e
ED
165 struct {
166 __be16 skc_dport;
167 __u16 skc_num;
168 };
169 };
170
4dc6dc71
ED
171 unsigned short skc_family;
172 volatile unsigned char skc_state;
055dc21a 173 unsigned char skc_reuse:4;
9fe516ba
ED
174 unsigned char skc_reuseport:1;
175 unsigned char skc_ipv6only:1;
26abe143 176 unsigned char skc_net_refcnt:1;
4dc6dc71 177 int skc_bound_dev_if;
512615b6
ED
178 union {
179 struct hlist_node skc_bind_node;
ca065d0c 180 struct hlist_node skc_portaddr_node;
512615b6 181 };
8feaf0c0 182 struct proto *skc_prot;
0c5c9fb5 183 possible_net_t skc_net;
efe4208f
ED
184
185#if IS_ENABLED(CONFIG_IPV6)
186 struct in6_addr skc_v6_daddr;
187 struct in6_addr skc_v6_rcv_saddr;
188#endif
189
33cf7c90
ED
190 atomic64_t skc_cookie;
191
8e5eb54d
ED
192 /* following fields are padding to force
193 * offset(struct sock, sk_refcnt) == 128 on 64bit arches
194 * assuming IPV6 is enabled. We use this padding differently
195 * for different kind of 'sockets'
196 */
197 union {
198 unsigned long skc_flags;
199 struct sock *skc_listener; /* request_sock */
200 struct inet_timewait_death_row *skc_tw_dr; /* inet_timewait_sock */
201 };
68835aba
ED
202 /*
203 * fields between dontcopy_begin/dontcopy_end
204 * are not copied in sock_copy()
205 */
928c41e7 206 /* private: */
68835aba 207 int skc_dontcopy_begin[0];
928c41e7 208 /* public: */
68835aba
ED
209 union {
210 struct hlist_node skc_node;
211 struct hlist_nulls_node skc_nulls_node;
212 };
755c31cd 213 unsigned short skc_tx_queue_mapping;
4e1beecc 214#ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
c6345ce7
AN
215 unsigned short skc_rx_queue_mapping;
216#endif
ed53d0ab
ED
217 union {
218 int skc_incoming_cpu;
219 u32 skc_rcv_wnd;
d475f090 220 u32 skc_tw_rcv_nxt; /* struct tcp_timewait_sock */
ed53d0ab 221 };
70da268b 222
41c6d650 223 refcount_t skc_refcnt;
928c41e7 224 /* private: */
68835aba 225 int skc_dontcopy_end[0];
ed53d0ab
ED
226 union {
227 u32 skc_rxhash;
228 u32 skc_window_clamp;
d475f090 229 u32 skc_tw_snd_nxt; /* struct tcp_timewait_sock */
ed53d0ab 230 };
928c41e7 231 /* public: */
1da177e4
LT
232};
233
1f00d375 234struct bpf_local_storage;
b6459415 235struct sk_filter;
6ac99e8f 236
1da177e4
LT
237/**
238 * struct sock - network layer representation of sockets
8feaf0c0 239 * @__sk_common: shared layout with inet_timewait_sock
4dc3b16b
PP
240 * @sk_shutdown: mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN
241 * @sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings
242 * @sk_lock: synchronizer
cdfbabfb 243 * @sk_kern_sock: True if sock is using kernel lock classes
4dc3b16b 244 * @sk_rcvbuf: size of receive buffer in bytes
43815482 245 * @sk_wq: sock wait queue and async head
421b3885 246 * @sk_rx_dst: receive input route used by early demux
0c0a5ef8 247 * @sk_rx_dst_ifindex: ifindex for @sk_rx_dst
ef57c161 248 * @sk_rx_dst_cookie: cookie for @sk_rx_dst
4dc3b16b 249 * @sk_dst_cache: destination cache
9b8805a3 250 * @sk_dst_pending_confirm: need to confirm neighbour
4dc3b16b 251 * @sk_policy: flow policy
4dc3b16b
PP
252 * @sk_receive_queue: incoming packets
253 * @sk_wmem_alloc: transmit queue bytes committed
771edcaf 254 * @sk_tsq_flags: TCP Small Queues flags
4dc3b16b
PP
255 * @sk_write_queue: Packet sending queue
256 * @sk_omem_alloc: "o" is "option" or "other"
257 * @sk_wmem_queued: persistent queue size
258 * @sk_forward_alloc: space allocated forward
2bb2f5fb 259 * @sk_reserved_mem: space reserved and non-reclaimable for the socket
06021292 260 * @sk_napi_id: id of the last napi context to receive data for sk
dafcc438 261 * @sk_ll_usec: usecs to busypoll when there is no data
4dc3b16b 262 * @sk_allocation: allocation mode
95bd09eb 263 * @sk_pacing_rate: Pacing rate (if supported by transport/packet scheduler)
218af599 264 * @sk_pacing_status: Pacing status (requested, handled by sch_fq)
c3f40d7c 265 * @sk_max_pacing_rate: Maximum pacing rate (%SO_MAX_PACING_RATE)
4dc3b16b 266 * @sk_sndbuf: size of send buffer in bytes
28448b80
TH
267 * @sk_no_check_tx: %SO_NO_CHECK setting, set checksum in TX packets
268 * @sk_no_check_rx: allow zero checksum in RX packets
4dc3b16b 269 * @sk_route_caps: route capabilities (e.g. %NETIF_F_TSO)
aba54656 270 * @sk_gso_disabled: if set, NETIF_F_GSO_MASK is forbidden.
bcd76111 271 * @sk_gso_type: GSO type (e.g. %SKB_GSO_TCPV4)
82cc1a7a 272 * @sk_gso_max_size: Maximum GSO segment size to build
1485348d 273 * @sk_gso_max_segs: Maximum number of GSO segments
3a9b76fd 274 * @sk_pacing_shift: scaling factor for TCP Small Queues
4dc3b16b 275 * @sk_lingertime: %SO_LINGER l_linger setting
4dc3b16b
PP
276 * @sk_backlog: always used with the per-socket spinlock held
277 * @sk_callback_lock: used with the callbacks in the end of this struct
278 * @sk_error_queue: rarely used
33c732c3
WC
279 * @sk_prot_creator: sk_prot of original sock creator (see ipv6_setsockopt,
280 * IPV6_ADDRFORM for instance)
4dc3b16b 281 * @sk_err: last error
33c732c3
WC
282 * @sk_err_soft: errors that don't cause failure but are the cause of a
283 * persistent failure not just 'timed out'
cb61cb9b 284 * @sk_drops: raw/udp drops counter
4dc3b16b
PP
285 * @sk_ack_backlog: current listen backlog
286 * @sk_max_ack_backlog: listen backlog set in listen()
771edcaf 287 * @sk_uid: user id of owner
7fd3253a 288 * @sk_prefer_busy_poll: prefer busypolling over softirq processing
7c951caf 289 * @sk_busy_poll_budget: napi processing budget when busypolling
4dc3b16b
PP
290 * @sk_priority: %SO_PRIORITY setting
291 * @sk_type: socket type (%SOCK_STREAM, etc)
292 * @sk_protocol: which protocol this socket belongs in this network family
5fb14d20 293 * @sk_peer_lock: lock protecting @sk_peer_pid and @sk_peer_cred
53c3fa20
RD
294 * @sk_peer_pid: &struct pid for this socket's peer
295 * @sk_peer_cred: %SO_PEERCRED setting
4dc3b16b
PP
296 * @sk_rcvlowat: %SO_RCVLOWAT setting
297 * @sk_rcvtimeo: %SO_RCVTIMEO setting
298 * @sk_sndtimeo: %SO_SNDTIMEO setting
b73c3d0e 299 * @sk_txhash: computed flow hash for use on transmit
26859240 300 * @sk_txrehash: enable TX hash rethink
4dc3b16b 301 * @sk_filter: socket filtering instructions
4dc3b16b
PP
302 * @sk_timer: sock cleanup timer
303 * @sk_stamp: time stamp of last packet received
3a0ed3e9 304 * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only
d463126e 305 * @sk_tsflags: SO_TIMESTAMPING flags
24e82b7c 306 * @sk_bpf_cb_flags: used in bpf_setsockopt()
fb87bd47
GN
307 * @sk_use_task_frag: allow sk_page_frag() to use current->task_frag.
308 * Sockets that can be used under memory reclaim should
309 * set this to false.
d463126e
YL
310 * @sk_bind_phc: SO_TIMESTAMPING bind PHC index of PTP virtual clock
311 * for timestamping
09c2d251 312 * @sk_tskey: counter to disambiguate concurrent tstamp requests
52267790 313 * @sk_zckey: counter to order MSG_ZEROCOPY notifications
4dc3b16b 314 * @sk_socket: Identd and reporting IO signals
b68777d5 315 * @sk_user_data: RPC layer private data. Write-protected by @sk_callback_lock.
5640f768 316 * @sk_frag: cached page frag
d3d4f0a0 317 * @sk_peek_off: current peek_offset value
4dc3b16b 318 * @sk_send_head: front of stuff to transmit
66256e0b 319 * @tcp_rtx_queue: TCP re-transmit queue [union with @sk_send_head]
67be2dd1 320 * @sk_security: used by security modules
31729363 321 * @sk_mark: generic packet mark
2a56a1fe 322 * @sk_cgrp_data: cgroup data for this cgroup
baac50bb 323 * @sk_memcg: this socket's memory cgroup association
4dc3b16b 324 * @sk_write_pending: a write to stream socket waits to start
419ce133 325 * @sk_disconnects: number of disconnect operations performed on this sock
4dc3b16b
PP
326 * @sk_state_change: callback to indicate change in the state of the sock
327 * @sk_data_ready: callback to indicate there is data to be processed
328 * @sk_write_space: callback to indicate there is bf sending space available
329 * @sk_error_report: callback to indicate errors (e.g. %MSG_ERRQUEUE)
330 * @sk_backlog_rcv: callback to process the backlog
66256e0b 331 * @sk_validate_xmit_skb: ptr to an optional validate function
4dc3b16b 332 * @sk_destruct: called at sock freeing time, i.e. when all refcnt == 0
ef456144 333 * @sk_reuseport_cb: reuseport group container
66256e0b 334 * @sk_bpf_storage: ptr to cache and control for bpf_sk_storage
293de7de 335 * @sk_rcu: used during RCU grace period
80b14dee
RC
336 * @sk_clockid: clockid used by time-based scheduling (SO_TXTIME)
337 * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
66256e0b 338 * @sk_txtime_report_errors: set report errors mode for SO_TXTIME
80b14dee 339 * @sk_txtime_unused: unused txtime flags
0e81cfd9
KI
340 * @sk_scm_recv_flags: all flags used by scm_recv()
341 * @sk_scm_credentials: flagged by SO_PASSCRED to recv SCM_CREDENTIALS
342 * @sk_scm_security: flagged by SO_PASSSEC to recv SCM_SECURITY
343 * @sk_scm_pidfd: flagged by SO_PASSPIDFD to recv SCM_PIDFD
77cbe1a6 344 * @sk_scm_rights: flagged by SO_PASSRIGHTS to recv SCM_RIGHTS
0e81cfd9 345 * @sk_scm_unused: unused flags for scm_recv()
ffa84b5f 346 * @ns_tracker: tracker for netns reference
8f0b3cc9 347 * @sk_user_frags: xarray of pages the user is holding a reference on.
0bb2f7a1
KI
348 * @sk_owner: reference to the real owner of the socket that calls
349 * sock_lock_init_class_and_name().
293de7de 350 */
1da177e4
LT
351struct sock {
352 /*
8feaf0c0 353 * Now struct inet_timewait_sock also uses sock_common, so please just
1da177e4
LT
354 * don't add nothing before this first member (__sk_common) --acme
355 */
356 struct sock_common __sk_common;
4dc6dc71
ED
357#define sk_node __sk_common.skc_node
358#define sk_nulls_node __sk_common.skc_nulls_node
359#define sk_refcnt __sk_common.skc_refcnt
e022f0b4 360#define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping
4e1beecc 361#ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
c6345ce7
AN
362#define sk_rx_queue_mapping __sk_common.skc_rx_queue_mapping
363#endif
4dc6dc71 364
68835aba
ED
365#define sk_dontcopy_begin __sk_common.skc_dontcopy_begin
366#define sk_dontcopy_end __sk_common.skc_dontcopy_end
4dc6dc71 367#define sk_hash __sk_common.skc_hash
50805466 368#define sk_portpair __sk_common.skc_portpair
05dbc7b5
ED
369#define sk_num __sk_common.skc_num
370#define sk_dport __sk_common.skc_dport
50805466
ED
371#define sk_addrpair __sk_common.skc_addrpair
372#define sk_daddr __sk_common.skc_daddr
373#define sk_rcv_saddr __sk_common.skc_rcv_saddr
1da177e4
LT
374#define sk_family __sk_common.skc_family
375#define sk_state __sk_common.skc_state
376#define sk_reuse __sk_common.skc_reuse
055dc21a 377#define sk_reuseport __sk_common.skc_reuseport
9fe516ba 378#define sk_ipv6only __sk_common.skc_ipv6only
26abe143 379#define sk_net_refcnt __sk_common.skc_net_refcnt
1da177e4 380#define sk_bound_dev_if __sk_common.skc_bound_dev_if
1da177e4 381#define sk_bind_node __sk_common.skc_bind_node
8feaf0c0 382#define sk_prot __sk_common.skc_prot
07feaebf 383#define sk_net __sk_common.skc_net
efe4208f
ED
384#define sk_v6_daddr __sk_common.skc_v6_daddr
385#define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
33cf7c90 386#define sk_cookie __sk_common.skc_cookie
70da268b 387#define sk_incoming_cpu __sk_common.skc_incoming_cpu
8e5eb54d 388#define sk_flags __sk_common.skc_flags
ed53d0ab 389#define sk_rxhash __sk_common.skc_rxhash
efe4208f 390
5d4cc874 391 __cacheline_group_begin(sock_write_rx);
43f51df4 392
9115e8cd 393 atomic_t sk_drops;
5d4cc874 394 __s32 sk_peek_off;
9115e8cd 395 struct sk_buff_head sk_error_queue;
b178bb3d 396 struct sk_buff_head sk_receive_queue;
fa438ccf
ED
397 /*
398 * The backlog queue is special, it is always used with
399 * the per-socket spinlock held and requires low latency
400 * access. Therefore we special case it's implementation.
b178bb3d
ED
401 * Note : rmem_alloc is in this structure to fill a hole
402 * on 64bit arches, not because its logically part of
403 * backlog.
fa438ccf
ED
404 */
405 struct {
b178bb3d
ED
406 atomic_t rmem_alloc;
407 int len;
408 struct sk_buff *head;
409 struct sk_buff *tail;
fa438ccf 410 } sk_backlog;
b178bb3d 411#define sk_rmem_alloc sk_backlog.rmem_alloc
2c8c56e1 412
5d4cc874
ED
413 __cacheline_group_end(sock_write_rx);
414
415 __cacheline_group_begin(sock_read_rx);
416 /* early demux fields */
417 struct dst_entry __rcu *sk_rx_dst;
418 int sk_rx_dst_ifindex;
419 u32 sk_rx_dst_cookie;
420
e0d1095a 421#ifdef CONFIG_NET_RX_BUSY_POLL
dafcc438 422 unsigned int sk_ll_usec;
9115e8cd 423 unsigned int sk_napi_id;
5d4cc874
ED
424 u16 sk_busy_poll_budget;
425 u8 sk_prefer_busy_poll;
b178bb3d 426#endif
5d4cc874 427 u8 sk_userlocks;
b178bb3d
ED
428 int sk_rcvbuf;
429
430 struct sk_filter __rcu *sk_filter;
ceb5d58b
ED
431 union {
432 struct socket_wq __rcu *sk_wq;
66256e0b 433 /* private: */
ceb5d58b 434 struct socket_wq *sk_wq_raw;
66256e0b 435 /* public: */
ceb5d58b 436 };
5d4cc874
ED
437
438 void (*sk_data_ready)(struct sock *sk);
439 long sk_rcvtimeo;
440 int sk_rcvlowat;
441 __cacheline_group_end(sock_read_rx);
442
443 __cacheline_group_begin(sock_read_rxtx);
444 int sk_err;
445 struct socket *sk_socket;
446 struct mem_cgroup *sk_memcg;
def8b4fa 447#ifdef CONFIG_XFRM
d188ba86 448 struct xfrm_policy __rcu *sk_policy[2];
def8b4fa 449#endif
5d4cc874 450 __cacheline_group_end(sock_read_rxtx);
0c0a5ef8 451
5d4cc874
ED
452 __cacheline_group_begin(sock_write_rxtx);
453 socket_lock_t sk_lock;
454 u32 sk_reserved_mem;
455 int sk_forward_alloc;
456 u32 sk_tsflags;
457 __cacheline_group_end(sock_write_rxtx);
458
459 __cacheline_group_begin(sock_write_tx);
460 int sk_write_pending;
1da177e4 461 atomic_t sk_omem_alloc;
4e07a91c 462 int sk_sndbuf;
9115e8cd 463
9115e8cd 464 int sk_wmem_queued;
14afee4b 465 refcount_t sk_wmem_alloc;
9115e8cd 466 unsigned long sk_tsq_flags;
75c119af
ED
467 union {
468 struct sk_buff *sk_send_head;
469 struct rb_root tcp_rtx_queue;
470 };
1da177e4 471 struct sk_buff_head sk_write_queue;
5d4cc874 472 u32 sk_dst_pending_confirm;
218af599 473 u32 sk_pacing_status; /* see enum sk_pacing */
5d4cc874 474 struct page_frag sk_frag;
9115e8cd 475 struct timer_list sk_timer;
5d4cc874 476
76a9ebe8 477 unsigned long sk_pacing_rate; /* bytes per second */
5d4cc874
ED
478 atomic_t sk_zckey;
479 atomic_t sk_tskey;
480 __cacheline_group_end(sock_write_tx);
481
482 __cacheline_group_begin(sock_read_tx);
76a9ebe8 483 unsigned long sk_max_pacing_rate;
5d4cc874
ED
484 long sk_sndtimeo;
485 u32 sk_priority;
486 u32 sk_mark;
487 struct dst_entry __rcu *sk_dst_cache;
9115e8cd 488 netdev_features_t sk_route_caps;
5d4cc874
ED
489#ifdef CONFIG_SOCK_VALIDATE_XMIT
490 struct sk_buff* (*sk_validate_xmit_skb)(struct sock *sk,
491 struct net_device *dev,
492 struct sk_buff *skb);
493#endif
494 u16 sk_gso_type;
495 u16 sk_gso_max_segs;
9115e8cd
ED
496 unsigned int sk_gso_max_size;
497 gfp_t sk_allocation;
5d4cc874
ED
498 u32 sk_txhash;
499 u8 sk_pacing_shift;
500 bool sk_use_task_frag;
501 __cacheline_group_end(sock_read_tx);
fc64869c
AR
502
503 /*
504 * Because of non atomicity rules, all
505 * changes are protected by socket lock.
506 */
aba54656 507 u8 sk_gso_disabled : 1,
cdfbabfb 508 sk_kern_sock : 1,
28448b80 509 sk_no_check_tx : 1,
5d4cc874
ED
510 sk_no_check_rx : 1;
511 u8 sk_shutdown;
bf976514
MM
512 u16 sk_type;
513 u16 sk_protocol;
1da177e4 514 unsigned long sk_lingertime;
476e19cf 515 struct proto *sk_prot_creator;
1da177e4 516 rwlock_t sk_callback_lock;
5d4cc874 517 int sk_err_soft;
becb74f0
ED
518 u32 sk_ack_backlog;
519 u32 sk_max_ack_backlog;
86741ec2 520 kuid_t sk_uid;
35306eb2 521 spinlock_t sk_peer_lock;
1ace2b4d 522 int sk_bind_phc;
109f6e39
EB
523 struct pid *sk_peer_pid;
524 const struct cred *sk_peer_cred;
35306eb2 525
b7aa0bf7 526 ktime_t sk_stamp;
3a0ed3e9
DD
527#if BITS_PER_LONG==32
528 seqlock_t sk_stamp_seq;
529#endif
5d4cc874 530 int sk_disconnects;
80b14dee 531
0e81cfd9
KI
532 union {
533 u8 sk_txrehash;
534 u8 sk_scm_recv_flags;
535 struct {
536 u8 sk_scm_credentials : 1,
537 sk_scm_security : 1,
538 sk_scm_pidfd : 1,
77cbe1a6
KI
539 sk_scm_rights : 1,
540 sk_scm_unused : 4;
0e81cfd9
KI
541 };
542 };
80b14dee
RC
543 u8 sk_clockid;
544 u8 sk_txtime_deadline_mode : 1,
4b15c707
JSP
545 sk_txtime_report_errors : 1,
546 sk_txtime_unused : 6;
24e82b7c
JX
547#define SK_BPF_CB_FLAG_TEST(SK, FLAG) ((SK)->sk_bpf_cb_flags & (FLAG))
548 u8 sk_bpf_cb_flags;
80b14dee 549
1da177e4 550 void *sk_user_data;
d5f64238 551#ifdef CONFIG_SECURITY
1da177e4 552 void *sk_security;
d5f64238 553#endif
2a56a1fe 554 struct sock_cgroup_data sk_cgrp_data;
1da177e4 555 void (*sk_state_change)(struct sock *sk);
1da177e4
LT
556 void (*sk_write_space)(struct sock *sk);
557 void (*sk_error_report)(struct sock *sk);
dc6b9b78
ED
558 int (*sk_backlog_rcv)(struct sock *sk,
559 struct sk_buff *skb);
1da177e4 560 void (*sk_destruct)(struct sock *sk);
ef456144 561 struct sock_reuseport __rcu *sk_reuseport_cb;
6ac99e8f 562#ifdef CONFIG_BPF_SYSCALL
1f00d375 563 struct bpf_local_storage __rcu *sk_bpf_storage;
6ac99e8f 564#endif
a4298e45 565 struct rcu_head sk_rcu;
ffa84b5f 566 netns_tracker ns_tracker;
8f0b3cc9 567 struct xarray sk_user_frags;
0bb2f7a1
KI
568
569#if IS_ENABLED(CONFIG_PROVE_LOCKING) && IS_ENABLED(CONFIG_MODULES)
570 struct module *sk_owner;
571#endif
1da177e4
LT
572};
573
ebad6d03
SAS
574struct sock_bh_locked {
575 struct sock *sock;
576 local_lock_t bh_lock;
577};
578
218af599
ED
579enum sk_pacing {
580 SK_PACING_NONE = 0,
581 SK_PACING_NEEDED = 1,
582 SK_PACING_FQ = 2,
583};
584
2a013372
HJ
585/* flag bits in sk_user_data
586 *
587 * - SK_USER_DATA_NOCOPY: Pointer stored in sk_user_data might
588 * not be suitable for copying when cloning the socket. For instance,
589 * it can point to a reference counted object. sk_user_data bottom
590 * bit is set if pointer must not be copied.
591 *
592 * - SK_USER_DATA_BPF: Mark whether sk_user_data field is
593 * managed/owned by a BPF reuseport array. This bit should be set
594 * when sk_user_data's sk is added to the bpf's reuseport_array.
595 *
596 * - SK_USER_DATA_PSOCK: Mark whether pointer stored in
597 * sk_user_data points to psock type. This bit should be set
598 * when sk_user_data is assigned to a psock object.
f1ff5ce2
JS
599 */
600#define SK_USER_DATA_NOCOPY 1UL
2a013372
HJ
601#define SK_USER_DATA_BPF 2UL
602#define SK_USER_DATA_PSOCK 4UL
603#define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY | SK_USER_DATA_BPF |\
604 SK_USER_DATA_PSOCK)
f1ff5ce2
JS
605
606/**
607 * sk_user_data_is_nocopy - Test if sk_user_data pointer must not be copied
608 * @sk: socket
609 */
610static inline bool sk_user_data_is_nocopy(const struct sock *sk)
611{
612 return ((uintptr_t)sk->sk_user_data & SK_USER_DATA_NOCOPY);
613}
614
559835ea
PS
615#define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data)))
616
fc4aaf9f
DH
617/**
618 * __locked_read_sk_user_data_with_flags - return the pointer
619 * only if argument flags all has been set in sk_user_data. Otherwise
620 * return NULL
621 *
622 * @sk: socket
623 * @flags: flag bits
624 *
625 * The caller must be holding sk->sk_callback_lock.
626 */
627static inline void *
628__locked_read_sk_user_data_with_flags(const struct sock *sk,
629 uintptr_t flags)
630{
631 uintptr_t sk_user_data =
632 (uintptr_t)rcu_dereference_check(__sk_user_data(sk),
633 lockdep_is_held(&sk->sk_callback_lock));
634
635 WARN_ON_ONCE(flags & SK_USER_DATA_PTRMASK);
636
637 if ((sk_user_data & flags) == flags)
638 return (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
639 return NULL;
640}
641
2a013372
HJ
642/**
643 * __rcu_dereference_sk_user_data_with_flags - return the pointer
644 * only if argument flags all has been set in sk_user_data. Otherwise
645 * return NULL
646 *
647 * @sk: socket
648 * @flags: flag bits
649 */
650static inline void *
651__rcu_dereference_sk_user_data_with_flags(const struct sock *sk,
652 uintptr_t flags)
653{
654 uintptr_t sk_user_data = (uintptr_t)rcu_dereference(__sk_user_data(sk));
655
656 WARN_ON_ONCE(flags & SK_USER_DATA_PTRMASK);
657
658 if ((sk_user_data & flags) == flags)
659 return (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
660 return NULL;
661}
662
f1ff5ce2 663#define rcu_dereference_sk_user_data(sk) \
2a013372
HJ
664 __rcu_dereference_sk_user_data_with_flags(sk, 0)
665#define __rcu_assign_sk_user_data_with_flags(sk, ptr, flags) \
f1ff5ce2 666({ \
2a013372
HJ
667 uintptr_t __tmp1 = (uintptr_t)(ptr), \
668 __tmp2 = (uintptr_t)(flags); \
669 WARN_ON_ONCE(__tmp1 & ~SK_USER_DATA_PTRMASK); \
670 WARN_ON_ONCE(__tmp2 & SK_USER_DATA_PTRMASK); \
f1ff5ce2 671 rcu_assign_pointer(__sk_user_data((sk)), \
2a013372 672 __tmp1 | __tmp2); \
f1ff5ce2 673})
2a013372
HJ
674#define rcu_assign_sk_user_data(sk, ptr) \
675 __rcu_assign_sk_user_data_with_flags(sk, ptr, 0)
559835ea 676
e187013a
AK
677static inline
678struct net *sock_net(const struct sock *sk)
679{
680 return read_pnet(&sk->sk_net);
681}
682
683static inline
684void sock_net_set(struct sock *sk, struct net *net)
685{
686 write_pnet(&sk->sk_net, net);
687}
688
4a17fd52
PE
689/*
690 * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
691 * or not whether his port will be reused by someone else. SK_FORCE_REUSE
692 * on a socket means that the socket will reuse everybody else's port
693 * without looking at the other's sk_reuse value.
694 */
695
696#define SK_NO_REUSE 0
697#define SK_CAN_REUSE 1
698#define SK_FORCE_REUSE 2
699
627d2d6b 700int sk_set_peek_off(struct sock *sk, int val);
701
a84a434b 702static inline int sk_peek_offset(const struct sock *sk, int flags)
ef64a54f 703{
b9bb53f3 704 if (unlikely(flags & MSG_PEEK)) {
a0917e0b 705 return READ_ONCE(sk->sk_peek_off);
b9bb53f3
WB
706 }
707
708 return 0;
ef64a54f
PE
709}
710
711static inline void sk_peek_offset_bwd(struct sock *sk, int val)
712{
b9bb53f3
WB
713 s32 off = READ_ONCE(sk->sk_peek_off);
714
715 if (unlikely(off >= 0)) {
716 off = max_t(s32, off - val, 0);
717 WRITE_ONCE(sk->sk_peek_off, off);
ef64a54f
PE
718 }
719}
720
721static inline void sk_peek_offset_fwd(struct sock *sk, int val)
722{
b9bb53f3 723 sk_peek_offset_bwd(sk, -val);
ef64a54f
PE
724}
725
1da177e4
LT
726/*
727 * Hashed lists helper routines
728 */
c4146644
LZ
729static inline struct sock *sk_entry(const struct hlist_node *node)
730{
731 return hlist_entry(node, struct sock, sk_node);
732}
733
e48c414e 734static inline struct sock *__sk_head(const struct hlist_head *head)
1da177e4
LT
735{
736 return hlist_entry(head->first, struct sock, sk_node);
737}
738
e48c414e 739static inline struct sock *sk_head(const struct hlist_head *head)
1da177e4
LT
740{
741 return hlist_empty(head) ? NULL : __sk_head(head);
742}
743
88ab1932
ED
744static inline struct sock *__sk_nulls_head(const struct hlist_nulls_head *head)
745{
746 return hlist_nulls_entry(head->first, struct sock, sk_nulls_node);
747}
748
749static inline struct sock *sk_nulls_head(const struct hlist_nulls_head *head)
750{
751 return hlist_nulls_empty(head) ? NULL : __sk_nulls_head(head);
752}
753
e48c414e 754static inline struct sock *sk_next(const struct sock *sk)
1da177e4 755{
6c59ebd3 756 return hlist_entry_safe(sk->sk_node.next, struct sock, sk_node);
1da177e4
LT
757}
758
88ab1932
ED
759static inline struct sock *sk_nulls_next(const struct sock *sk)
760{
761 return (!is_a_nulls(sk->sk_nulls_node.next)) ?
762 hlist_nulls_entry(sk->sk_nulls_node.next,
763 struct sock, sk_nulls_node) :
764 NULL;
765}
766
dc6b9b78 767static inline bool sk_unhashed(const struct sock *sk)
1da177e4
LT
768{
769 return hlist_unhashed(&sk->sk_node);
770}
771
dc6b9b78 772static inline bool sk_hashed(const struct sock *sk)
1da177e4 773{
da753bea 774 return !sk_unhashed(sk);
1da177e4
LT
775}
776
dc6b9b78 777static inline void sk_node_init(struct hlist_node *node)
1da177e4
LT
778{
779 node->pprev = NULL;
780}
781
dc6b9b78 782static inline void __sk_del_node(struct sock *sk)
1da177e4
LT
783{
784 __hlist_del(&sk->sk_node);
785}
786
808f5114 787/* NB: equivalent to hlist_del_init_rcu */
dc6b9b78 788static inline bool __sk_del_node_init(struct sock *sk)
1da177e4
LT
789{
790 if (sk_hashed(sk)) {
791 __sk_del_node(sk);
792 sk_node_init(&sk->sk_node);
dc6b9b78 793 return true;
1da177e4 794 }
dc6b9b78 795 return false;
1da177e4
LT
796}
797
798/* Grab socket reference count. This operation is valid only
799 when sk is ALREADY grabbed f.e. it is found in hash table
800 or a list and the lookup is made under lock preventing hash table
801 modifications.
802 */
803
f9a7cbbf 804static __always_inline void sock_hold(struct sock *sk)
1da177e4 805{
41c6d650 806 refcount_inc(&sk->sk_refcnt);
1da177e4
LT
807}
808
809/* Ungrab socket in the context, which assumes that socket refcnt
810 cannot hit zero, f.e. it is true in context of any socketcall.
811 */
f9a7cbbf 812static __always_inline void __sock_put(struct sock *sk)
1da177e4 813{
41c6d650 814 refcount_dec(&sk->sk_refcnt);
1da177e4
LT
815}
816
dc6b9b78 817static inline bool sk_del_node_init(struct sock *sk)
1da177e4 818{
dc6b9b78 819 bool rc = __sk_del_node_init(sk);
1da177e4
LT
820
821 if (rc) {
822 /* paranoid for a while -acme */
41c6d650 823 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
1da177e4
LT
824 __sock_put(sk);
825 }
826 return rc;
827}
808f5114 828#define sk_del_node_init_rcu(sk) sk_del_node_init(sk)
1da177e4 829
dc6b9b78 830static inline bool __sk_nulls_del_node_init_rcu(struct sock *sk)
271b72c7
ED
831{
832 if (sk_hashed(sk)) {
88ab1932 833 hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
dc6b9b78 834 return true;
271b72c7 835 }
dc6b9b78 836 return false;
271b72c7
ED
837}
838
dc6b9b78 839static inline bool sk_nulls_del_node_init_rcu(struct sock *sk)
271b72c7 840{
dc6b9b78 841 bool rc = __sk_nulls_del_node_init_rcu(sk);
271b72c7
ED
842
843 if (rc) {
844 /* paranoid for a while -acme */
41c6d650 845 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
271b72c7
ED
846 __sock_put(sk);
847 }
848 return rc;
849}
850
dc6b9b78 851static inline void __sk_add_node(struct sock *sk, struct hlist_head *list)
1da177e4
LT
852{
853 hlist_add_head(&sk->sk_node, list);
854}
855
dc6b9b78 856static inline void sk_add_node(struct sock *sk, struct hlist_head *list)
1da177e4
LT
857{
858 sock_hold(sk);
859 __sk_add_node(sk, list);
860}
861
dc6b9b78 862static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
808f5114 863{
864 sock_hold(sk);
d296ba60
CG
865 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
866 sk->sk_family == AF_INET6)
867 hlist_add_tail_rcu(&sk->sk_node, list);
868 else
869 hlist_add_head_rcu(&sk->sk_node, list);
808f5114 870}
871
a4dc6a49
MC
872static inline void sk_add_node_tail_rcu(struct sock *sk, struct hlist_head *list)
873{
874 sock_hold(sk);
875 hlist_add_tail_rcu(&sk->sk_node, list);
876}
877
dc6b9b78 878static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
271b72c7 879{
d7efc6c1 880 hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
271b72c7
ED
881}
882
8dbd76e7
ED
883static inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nulls_head *list)
884{
885 hlist_nulls_add_tail_rcu(&sk->sk_nulls_node, list);
886}
887
dc6b9b78 888static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
271b72c7
ED
889{
890 sock_hold(sk);
88ab1932 891 __sk_nulls_add_node_rcu(sk, list);
271b72c7
ED
892}
893
dc6b9b78 894static inline void __sk_del_bind_node(struct sock *sk)
1da177e4
LT
895{
896 __hlist_del(&sk->sk_bind_node);
897}
898
dc6b9b78 899static inline void sk_add_bind_node(struct sock *sk,
1da177e4
LT
900 struct hlist_head *list)
901{
902 hlist_add_head(&sk->sk_bind_node, list);
903}
904
b67bfe0d
SL
905#define sk_for_each(__sk, list) \
906 hlist_for_each_entry(__sk, list, sk_node)
907#define sk_for_each_rcu(__sk, list) \
908 hlist_for_each_entry_rcu(__sk, list, sk_node)
88ab1932
ED
909#define sk_nulls_for_each(__sk, node, list) \
910 hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
911#define sk_nulls_for_each_rcu(__sk, node, list) \
912 hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node)
b67bfe0d
SL
913#define sk_for_each_from(__sk) \
914 hlist_for_each_entry_from(__sk, sk_node)
88ab1932
ED
915#define sk_nulls_for_each_from(__sk, node) \
916 if (__sk && ({ node = &(__sk)->sk_nulls_node; 1; })) \
917 hlist_nulls_for_each_entry_from(__sk, node, sk_nulls_node)
b67bfe0d
SL
918#define sk_for_each_safe(__sk, tmp, list) \
919 hlist_for_each_entry_safe(__sk, tmp, list, sk_node)
920#define sk_for_each_bound(__sk, list) \
921 hlist_for_each_entry(__sk, list, sk_bind_node)
1dae9f11
AK
922#define sk_for_each_bound_safe(__sk, tmp, list) \
923 hlist_for_each_entry_safe(__sk, tmp, list, sk_bind_node)
1da177e4 924
2dc41cff 925/**
ca065d0c 926 * sk_for_each_entry_offset_rcu - iterate over a list at a given struct offset
2dc41cff
DH
927 * @tpos: the type * to use as a loop cursor.
928 * @pos: the &struct hlist_node to use as a loop cursor.
929 * @head: the head for your list.
930 * @offset: offset of hlist_node within the struct.
931 *
932 */
ca065d0c 933#define sk_for_each_entry_offset_rcu(tpos, pos, head, offset) \
b6f4f848 934 for (pos = rcu_dereference(hlist_first_rcu(head)); \
ca065d0c 935 pos != NULL && \
2dc41cff 936 ({ tpos = (typeof(*tpos) *)((void *)pos - offset); 1;}); \
b6f4f848 937 pos = rcu_dereference(hlist_next_rcu(pos)))
2dc41cff 938
a84a434b 939static inline struct user_namespace *sk_user_ns(const struct sock *sk)
c336d148
EB
940{
941 /* Careful only use this in a context where these parameters
942 * can not change and must all be valid, such as recvmsg from
943 * userspace.
944 */
945 return sk->sk_socket->file->f_cred->user_ns;
946}
947
1da177e4
LT
948/* Sock flags */
949enum sock_flags {
950 SOCK_DEAD,
951 SOCK_DONE,
952 SOCK_URGINLINE,
953 SOCK_KEEPOPEN,
954 SOCK_LINGER,
955 SOCK_DESTROY,
956 SOCK_BROADCAST,
957 SOCK_TIMESTAMP,
958 SOCK_ZAPPED,
959 SOCK_USE_WRITE_QUEUE, /* whether to call sk->sk_write_space in sock_wfree */
960 SOCK_DBG, /* %SO_DEBUG setting */
961 SOCK_RCVTSTAMP, /* %SO_TIMESTAMP setting */
92f37fd2 962 SOCK_RCVTSTAMPNS, /* %SO_TIMESTAMPNS setting */
1da177e4 963 SOCK_LOCALROUTE, /* route locally only, %SO_DONTROUTE setting */
7cb02404 964 SOCK_MEMALLOC, /* VM depends on this socket for swapping */
20d49473 965 SOCK_TIMESTAMPING_RX_SOFTWARE, /* %SOF_TIMESTAMPING_RX_SOFTWARE */
bcdce719 966 SOCK_FASYNC, /* fasync() active */
3b885787 967 SOCK_RXQ_OVFL,
1cdebb42 968 SOCK_ZEROCOPY, /* buffers from userspace */
6e3e939f 969 SOCK_WIFI_STATUS, /* push wifi status to userspace */
3bdc0eba
BG
970 SOCK_NOFCS, /* Tell NIC not to do the Ethernet FCS.
971 * Will use last 4 bytes of packet sent from
972 * user-space instead.
973 */
d59577b6 974 SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */
7d4c04fc 975 SOCK_SELECT_ERR_QUEUE, /* Wake select on error queue */
a4298e45 976 SOCK_RCU_FREE, /* wait rcu grace period in sk_destruct() */
80b14dee 977 SOCK_TXTIME,
e4a2a304 978 SOCK_XDP, /* XDP is attached */
887feae3 979 SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */
6fd1d51c 980 SOCK_RCVMARK, /* Receive SO_MARK ancillary data with packet */
e45469e5 981 SOCK_RCVPRIORITY, /* Receive SO_PRIORITY ancillary data with packet */
f0e70409 982 SOCK_TIMESTAMPING_ANY, /* Copy of sk_tsflags & TSFLAGS_ANY */
1da177e4
LT
983};
984
01ce63c9 985#define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
4aecca4c
VF
986/*
987 * The highest bit of sk_tsflags is reserved for kernel-internal
988 * SOCKCM_FLAG_TS_OPT_ID. There is a check in core/sock.c to control that
989 * SOF_TIMESTAMPING* values do not reach this reserved area
990 */
991#define SOCKCM_FLAG_TS_OPT_ID BIT(31)
01ce63c9 992
a84a434b 993static inline void sock_copy_flags(struct sock *nsk, const struct sock *osk)
53b924b3
RB
994{
995 nsk->sk_flags = osk->sk_flags;
996}
997
1da177e4
LT
998static inline void sock_set_flag(struct sock *sk, enum sock_flags flag)
999{
1000 __set_bit(flag, &sk->sk_flags);
1001}
1002
1003static inline void sock_reset_flag(struct sock *sk, enum sock_flags flag)
1004{
1005 __clear_bit(flag, &sk->sk_flags);
1006}
1007
dfde1d7d
DY
1008static inline void sock_valbool_flag(struct sock *sk, enum sock_flags bit,
1009 int valbool)
1010{
1011 if (valbool)
1012 sock_set_flag(sk, bit);
1013 else
1014 sock_reset_flag(sk, bit);
1015}
1016
1b23a5df 1017static inline bool sock_flag(const struct sock *sk, enum sock_flags flag)
1da177e4
LT
1018{
1019 return test_bit(flag, &sk->sk_flags);
1020}
1021
c93bdd0e 1022#ifdef CONFIG_NET
a7950ae8 1023DECLARE_STATIC_KEY_FALSE(memalloc_socks_key);
c93bdd0e
MG
1024static inline int sk_memalloc_socks(void)
1025{
a7950ae8 1026 return static_branch_unlikely(&memalloc_socks_key);
c93bdd0e 1027}
d9539752
KC
1028
1029void __receive_sock(struct file *file);
c93bdd0e
MG
1030#else
1031
1032static inline int sk_memalloc_socks(void)
1033{
1034 return 0;
1035}
1036
d9539752
KC
1037static inline void __receive_sock(struct file *file)
1038{ }
c93bdd0e
MG
1039#endif
1040
7450aaf6 1041static inline gfp_t sk_gfp_mask(const struct sock *sk, gfp_t gfp_mask)
99a1dec7 1042{
7450aaf6 1043 return gfp_mask | (sk->sk_allocation & __GFP_MEMALLOC);
99a1dec7
MG
1044}
1045
1da177e4
LT
1046static inline void sk_acceptq_removed(struct sock *sk)
1047{
288efe86 1048 WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog - 1);
1da177e4
LT
1049}
1050
1051static inline void sk_acceptq_added(struct sock *sk)
1052{
288efe86 1053 WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog + 1);
1da177e4
LT
1054}
1055
c609e6aa
ED
1056/* Note: If you think the test should be:
1057 * return READ_ONCE(sk->sk_ack_backlog) >= READ_ONCE(sk->sk_max_ack_backlog);
1058 * Then please take a look at commit 64a146513f8f ("[NET]: Revert incorrect accept queue backlog changes.")
1059 */
dc6b9b78 1060static inline bool sk_acceptq_is_full(const struct sock *sk)
1da177e4 1061{
c609e6aa 1062 return READ_ONCE(sk->sk_ack_backlog) > READ_ONCE(sk->sk_max_ack_backlog);
1da177e4
LT
1063}
1064
1065/*
1066 * Compute minimal free write space needed to queue new packets.
1067 */
dc6b9b78 1068static inline int sk_stream_min_wspace(const struct sock *sk)
1da177e4 1069{
ab4e846a 1070 return READ_ONCE(sk->sk_wmem_queued) >> 1;
1da177e4
LT
1071}
1072
dc6b9b78 1073static inline int sk_stream_wspace(const struct sock *sk)
1da177e4 1074{
ab4e846a
ED
1075 return READ_ONCE(sk->sk_sndbuf) - READ_ONCE(sk->sk_wmem_queued);
1076}
1077
1078static inline void sk_wmem_queued_add(struct sock *sk, int val)
1079{
1080 WRITE_ONCE(sk->sk_wmem_queued, sk->sk_wmem_queued + val);
1da177e4
LT
1081}
1082
5e6300e7
ED
1083static inline void sk_forward_alloc_add(struct sock *sk, int val)
1084{
1085 /* Paired with lockless reads of sk->sk_forward_alloc */
1086 WRITE_ONCE(sk->sk_forward_alloc, sk->sk_forward_alloc + val);
1087}
1088
69336bd2 1089void sk_stream_write_space(struct sock *sk);
1da177e4 1090
8eae939f 1091/* OOB backlog add */
a3a858ff 1092static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
9ee6b535 1093{
7fee226a 1094 /* dont let skb dst not refcounted, we are going to leave rcu lock */
222d7dbd 1095 skb_dst_force(skb);
7fee226a
ED
1096
1097 if (!sk->sk_backlog.tail)
9ed498c6 1098 WRITE_ONCE(sk->sk_backlog.head, skb);
7fee226a 1099 else
9ee6b535 1100 sk->sk_backlog.tail->next = skb;
7fee226a 1101
9ed498c6 1102 WRITE_ONCE(sk->sk_backlog.tail, skb);
9ee6b535
SH
1103 skb->next = NULL;
1104}
1da177e4 1105
c377411f
ED
1106/*
1107 * Take into account size of receive queue and backlog queue
0fd7bac6
ED
1108 * Do not take into account this skb truesize,
1109 * to allow even a single big packet to come.
c377411f 1110 */
274f482d 1111static inline bool sk_rcvqueues_full(const struct sock *sk, unsigned int limit)
c377411f
ED
1112{
1113 unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc);
1114
f545a38f 1115 return qsize > limit;
c377411f
ED
1116}
1117
8eae939f 1118/* The per-socket spinlock must be held here. */
f545a38f
ED
1119static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb,
1120 unsigned int limit)
8eae939f 1121{
274f482d 1122 if (sk_rcvqueues_full(sk, limit))
8eae939f
ZY
1123 return -ENOBUFS;
1124
c7c49b8f
ED
1125 /*
1126 * If the skb was allocated from pfmemalloc reserves, only
1127 * allow SOCK_MEMALLOC sockets to use it as this socket is
1128 * helping free memory
1129 */
1130 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
1131 return -ENOMEM;
1132
a3a858ff 1133 __sk_add_backlog(sk, skb);
8eae939f
ZY
1134 sk->sk_backlog.len += skb->truesize;
1135 return 0;
1136}
1137
69336bd2 1138int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
b4b9e355 1139
d2489c7b
ED
1140INDIRECT_CALLABLE_DECLARE(int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb));
1141INDIRECT_CALLABLE_DECLARE(int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb));
1142
c57943a1
PZ
1143static inline int sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1144{
b4b9e355
MG
1145 if (sk_memalloc_socks() && skb_pfmemalloc(skb))
1146 return __sk_backlog_rcv(sk, skb);
1147
d2489c7b
ED
1148 return INDIRECT_CALL_INET(sk->sk_backlog_rcv,
1149 tcp_v6_do_rcv,
1150 tcp_v4_do_rcv,
1151 sk, skb);
c57943a1
PZ
1152}
1153
2c8c56e1
ED
1154static inline void sk_incoming_cpu_update(struct sock *sk)
1155{
34cfb542
PA
1156 int cpu = raw_smp_processor_id();
1157
7170a977
ED
1158 if (unlikely(READ_ONCE(sk->sk_incoming_cpu) != cpu))
1159 WRITE_ONCE(sk->sk_incoming_cpu, cpu);
2c8c56e1
ED
1160}
1161
fe477558 1162
bdeab991
TH
1163static inline void sock_rps_save_rxhash(struct sock *sk,
1164 const struct sk_buff *skb)
c58dc01b
DM
1165{
1166#ifdef CONFIG_RPS
1e5c647c
ED
1167 /* The following WRITE_ONCE() is paired with the READ_ONCE()
1168 * here, and another one in sock_rps_record_flow().
1169 */
1170 if (unlikely(READ_ONCE(sk->sk_rxhash) != skb->hash))
1171 WRITE_ONCE(sk->sk_rxhash, skb->hash);
c58dc01b
DM
1172#endif
1173}
1174
bdeab991
TH
1175static inline void sock_rps_reset_rxhash(struct sock *sk)
1176{
1177#ifdef CONFIG_RPS
1e5c647c
ED
1178 /* Paired with READ_ONCE() in sock_rps_record_flow() */
1179 WRITE_ONCE(sk->sk_rxhash, 0);
bdeab991
TH
1180#endif
1181}
1182
d9dc8b0f 1183#define sk_wait_event(__sk, __timeo, __condition, __wait) \
419ce133 1184 ({ int __rc, __dis = __sk->sk_disconnects; \
cfcabdcc
SH
1185 release_sock(__sk); \
1186 __rc = __condition; \
1187 if (!__rc) { \
d9dc8b0f
WC
1188 *(__timeo) = wait_woken(__wait, \
1189 TASK_INTERRUPTIBLE, \
1190 *(__timeo)); \
cfcabdcc 1191 } \
d9dc8b0f 1192 sched_annotate_sleep(); \
cfcabdcc 1193 lock_sock(__sk); \
419ce133 1194 __rc = __dis == __sk->sk_disconnects ? __condition : -EPIPE; \
cfcabdcc
SH
1195 __rc; \
1196 })
1da177e4 1197
69336bd2
JP
1198int sk_stream_wait_connect(struct sock *sk, long *timeo_p);
1199int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
1200void sk_stream_wait_close(struct sock *sk, long timeo_p);
1201int sk_stream_error(struct sock *sk, int flags, int err);
1202void sk_stream_kill_queues(struct sock *sk);
1203void sk_set_memalloc(struct sock *sk);
1204void sk_clear_memalloc(struct sock *sk);
1da177e4 1205
d41a69f1
ED
1206void __sk_flush_backlog(struct sock *sk);
1207
1208static inline bool sk_flush_backlog(struct sock *sk)
1209{
1210 if (unlikely(READ_ONCE(sk->sk_backlog.tail))) {
1211 __sk_flush_backlog(sk);
1212 return true;
1213 }
1214 return false;
1215}
1216
dfbafc99 1217int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb);
1da177e4 1218
60236fdd 1219struct request_sock_ops;
6d6ee43e 1220struct timewait_sock_ops;
ab1e0a13 1221struct inet_hashinfo;
fc8717ba 1222struct raw_hashinfo;
f16a7dd5 1223struct smc_hashinfo;
de477254 1224struct module;
51e0158a 1225struct sk_psock;
2e6599cb 1226
f77d6021 1227/*
5f0d5a3a 1228 * caches using SLAB_TYPESAFE_BY_RCU should let .next pointer from nulls nodes
f77d6021
ED
1229 * un-modified. Special care is taken when initializing object to zero.
1230 */
1231static inline void sk_prot_clear_nulls(struct sock *sk, int size)
1232{
1233 if (offsetof(struct sock, sk_node.next) != 0)
1234 memset(sk, 0, offsetof(struct sock, sk_node.next));
1235 memset(&sk->sk_node.pprev, 0,
1236 size - offsetof(struct sock, sk_node.pprev));
1237}
1238
92ef0fd5
JA
1239struct proto_accept_arg {
1240 int flags;
1241 int err;
7951e36a 1242 int is_empty;
92ef0fd5
JA
1243 bool kern;
1244};
1245
1da177e4
LT
1246/* Networking protocol blocks we attach to sockets.
1247 * socket layer -> transport layer interface
1da177e4
LT
1248 */
1249struct proto {
dc6b9b78 1250 void (*close)(struct sock *sk,
1da177e4 1251 long timeout);
d74bad4e
AI
1252 int (*pre_connect)(struct sock *sk,
1253 struct sockaddr *uaddr,
1254 int addr_len);
1da177e4 1255 int (*connect)(struct sock *sk,
dc6b9b78 1256 struct sockaddr *uaddr,
1da177e4
LT
1257 int addr_len);
1258 int (*disconnect)(struct sock *sk, int flags);
1259
92ef0fd5
JA
1260 struct sock * (*accept)(struct sock *sk,
1261 struct proto_accept_arg *arg);
1da177e4
LT
1262
1263 int (*ioctl)(struct sock *sk, int cmd,
e1d001fa 1264 int *karg);
1da177e4 1265 int (*init)(struct sock *sk);
7d06b2e0 1266 void (*destroy)(struct sock *sk);
1da177e4 1267 void (*shutdown)(struct sock *sk, int how);
dc6b9b78 1268 int (*setsockopt)(struct sock *sk, int level,
a7b75c5a 1269 int optname, sockptr_t optval,
b7058842 1270 unsigned int optlen);
dc6b9b78
ED
1271 int (*getsockopt)(struct sock *sk, int level,
1272 int optname, char __user *optval,
1273 int __user *option);
4b9d07a4 1274 void (*keepalive)(struct sock *sk, int valbool);
af01d537 1275#ifdef CONFIG_COMPAT
709b46e8
EB
1276 int (*compat_ioctl)(struct sock *sk,
1277 unsigned int cmd, unsigned long arg);
af01d537 1278#endif
1b784140
YX
1279 int (*sendmsg)(struct sock *sk, struct msghdr *msg,
1280 size_t len);
1281 int (*recvmsg)(struct sock *sk, struct msghdr *msg,
ec095263 1282 size_t len, int flags, int *addr_len);
2bfc6685 1283 void (*splice_eof)(struct socket *sock);
dc6b9b78 1284 int (*bind)(struct sock *sk,
c0425a42
CH
1285 struct sockaddr *addr, int addr_len);
1286 int (*bind_add)(struct sock *sk,
1287 struct sockaddr *addr, int addr_len);
1da177e4 1288
dc6b9b78 1289 int (*backlog_rcv) (struct sock *sk,
1da177e4 1290 struct sk_buff *skb);
9cacf81f
SF
1291 bool (*bpf_bypass_getsockopt)(int level,
1292 int optname);
1da177e4 1293
46d3ceab
ED
1294 void (*release_cb)(struct sock *sk);
1295
1da177e4 1296 /* Keeping track of sk's, looking them up, and port selection methods. */
086c653f 1297 int (*hash)(struct sock *sk);
1da177e4 1298 void (*unhash)(struct sock *sk);
719f8358 1299 void (*rehash)(struct sock *sk);
1da177e4 1300 int (*get_port)(struct sock *sk, unsigned short snum);
91a760b2 1301 void (*put_port)(struct sock *sk);
8a59f9d1 1302#ifdef CONFIG_BPF_SYSCALL
51e0158a
CW
1303 int (*psock_update_sk_prot)(struct sock *sk,
1304 struct sk_psock *psock,
1305 bool restore);
8a59f9d1 1306#endif
1da177e4 1307
286ab3d4 1308 /* Keeping track of sockets in use */
65f76517 1309#ifdef CONFIG_PROC_FS
13ff3d6f 1310 unsigned int inuse_idx;
65f76517 1311#endif
ebb53d75 1312
a74f0fa0 1313 bool (*stream_memory_free)(const struct sock *sk, int wake);
7b50ecfc 1314 bool (*sock_is_readable)(struct sock *sk);
1da177e4 1315 /* Memory pressure */
5c52ba17 1316 void (*enter_memory_pressure)(struct sock *sk);
06044751 1317 void (*leave_memory_pressure)(struct sock *sk);
8d987e5c 1318 atomic_long_t *memory_allocated; /* Current allocated memory. */
0defbb0a 1319 int __percpu *per_cpu_fw_alloc;
1748376b 1320 struct percpu_counter *sockets_allocated; /* Current number of sockets. */
292e6077 1321
1da177e4
LT
1322 /*
1323 * Pressure flag: try to collapse.
1324 * Technical note: it is used by multiple contexts non atomically.
76f33296 1325 * Make sure to use READ_ONCE()/WRITE_ONCE() for all reads/writes.
3ab224be 1326 * All the __sk_mem_schedule() is of this nature: accounting
1da177e4
LT
1327 * is strict, actions are advisory and have some latency.
1328 */
06044751 1329 unsigned long *memory_pressure;
8d987e5c 1330 long *sysctl_mem;
a3dcaf17 1331
1da177e4
LT
1332 int *sysctl_wmem;
1333 int *sysctl_rmem;
a3dcaf17
ED
1334 u32 sysctl_wmem_offset;
1335 u32 sysctl_rmem_offset;
1336
1da177e4 1337 int max_header;
7ba42910 1338 bool no_autobind;
1da177e4 1339
271b72c7 1340 struct kmem_cache *slab;
1da177e4 1341 unsigned int obj_size;
f5f80e32 1342 unsigned int ipv6_pinfo_offset;
d50112ed 1343 slab_flags_t slab_flags;
7bbdb81e
AD
1344 unsigned int useroffset; /* Usercopy region offset */
1345 unsigned int usersize; /* Usercopy region size */
1da177e4 1346
19757ceb 1347 unsigned int __percpu *orphan_count;
8feaf0c0 1348
60236fdd 1349 struct request_sock_ops *rsk_prot;
6d6ee43e 1350 struct timewait_sock_ops *twsk_prot;
2e6599cb 1351
39d8cda7
PE
1352 union {
1353 struct inet_hashinfo *hashinfo;
645ca708 1354 struct udp_table *udp_table;
fc8717ba 1355 struct raw_hashinfo *raw_hash;
f16a7dd5 1356 struct smc_hashinfo *smc_hash;
39d8cda7 1357 } h;
ab1e0a13 1358
1da177e4
LT
1359 struct module *owner;
1360
1361 char name[32];
1362
1363 struct list_head node;
64be0aed 1364 int (*diag_destroy)(struct sock *sk, int err);
3859a271 1365} __randomize_layout;
e1aab161 1366
69336bd2
JP
1367int proto_register(struct proto *prot, int alloc_slab);
1368void proto_unregister(struct proto *prot);
bf2ae2e4 1369int sock_load_diag_module(int family, int protocol);
1da177e4 1370
1c5f2ced
ED
1371INDIRECT_CALLABLE_DECLARE(bool tcp_stream_memory_free(const struct sock *sk, int wake));
1372
a74f0fa0 1373static inline bool __sk_stream_memory_free(const struct sock *sk, int wake)
c9bee3b7 1374{
ab4e846a 1375 if (READ_ONCE(sk->sk_wmem_queued) >= READ_ONCE(sk->sk_sndbuf))
c9bee3b7
ED
1376 return false;
1377
1378 return sk->sk_prot->stream_memory_free ?
a406290a
ED
1379 INDIRECT_CALL_INET_1(sk->sk_prot->stream_memory_free,
1380 tcp_stream_memory_free, sk, wake) : true;
c9bee3b7
ED
1381}
1382
a74f0fa0
ED
1383static inline bool sk_stream_memory_free(const struct sock *sk)
1384{
1385 return __sk_stream_memory_free(sk, 0);
1386}
1387
1388static inline bool __sk_stream_is_writeable(const struct sock *sk, int wake)
64dc6130 1389{
c9bee3b7 1390 return sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) &&
a74f0fa0
ED
1391 __sk_stream_memory_free(sk, wake);
1392}
1393
1394static inline bool sk_stream_is_writeable(const struct sock *sk)
1395{
1396 return __sk_stream_is_writeable(sk, 0);
64dc6130 1397}
e1aab161 1398
54fd9c2d
DB
1399static inline int sk_under_cgroup_hierarchy(struct sock *sk,
1400 struct cgroup *ancestor)
1401{
1402#ifdef CONFIG_SOCK_CGROUP_DATA
1403 return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data),
1404 ancestor);
1405#else
1406 return -ENOTSUPP;
1407#endif
1408}
c9bee3b7 1409
f5a5589c
WW
1410#define SK_ALLOC_PERCPU_COUNTER_BATCH 16
1411
180d8cd9
GC
1412static inline void sk_sockets_allocated_dec(struct sock *sk)
1413{
f5a5589c
WW
1414 percpu_counter_add_batch(sk->sk_prot->sockets_allocated, -1,
1415 SK_ALLOC_PERCPU_COUNTER_BATCH);
180d8cd9
GC
1416}
1417
1418static inline void sk_sockets_allocated_inc(struct sock *sk)
1419{
f5a5589c
WW
1420 percpu_counter_add_batch(sk->sk_prot->sockets_allocated, 1,
1421 SK_ALLOC_PERCPU_COUNTER_BATCH);
180d8cd9
GC
1422}
1423
5bf325a5 1424static inline u64
180d8cd9
GC
1425sk_sockets_allocated_read_positive(struct sock *sk)
1426{
af95d7df 1427 return percpu_counter_read_positive(sk->sk_prot->sockets_allocated);
180d8cd9
GC
1428}
1429
1430static inline int
1431proto_sockets_allocated_sum_positive(struct proto *prot)
1432{
1433 return percpu_counter_sum_positive(prot->sockets_allocated);
1434}
1435
65f76517 1436#ifdef CONFIG_PROC_FS
2a12ae5d
ED
1437#define PROTO_INUSE_NR 64 /* should be enough for the first time */
1438struct prot_inuse {
4199bae1 1439 int all;
2a12ae5d
ED
1440 int val[PROTO_INUSE_NR];
1441};
b3cb764a 1442
2a12ae5d
ED
1443static inline void sock_prot_inuse_add(const struct net *net,
1444 const struct proto *prot, int val)
1445{
b3cb764a 1446 this_cpu_add(net->core.prot_inuse->val[prot->inuse_idx], val);
2a12ae5d 1447}
d477eb90
ED
1448
1449static inline void sock_inuse_add(const struct net *net, int val)
1450{
4199bae1 1451 this_cpu_add(net->core.prot_inuse->all, val);
d477eb90
ED
1452}
1453
69336bd2 1454int sock_prot_inuse_get(struct net *net, struct proto *proto);
648845ab 1455int sock_inuse_get(struct net *net);
65f76517 1456#else
2a12ae5d
ED
1457static inline void sock_prot_inuse_add(const struct net *net,
1458 const struct proto *prot, int val)
65f76517
ED
1459{
1460}
d477eb90
ED
1461
1462static inline void sock_inuse_add(const struct net *net, int val)
1463{
1464}
65f76517
ED
1465#endif
1466
1da177e4 1467
614c6cb4
ACM
1468/* With per-bucket locks this operation is not-atomic, so that
1469 * this version is not worse.
1470 */
086c653f 1471static inline int __sk_prot_rehash(struct sock *sk)
614c6cb4
ACM
1472{
1473 sk->sk_prot->unhash(sk);
086c653f 1474 return sk->sk_prot->hash(sk);
614c6cb4
ACM
1475}
1476
1da177e4
LT
1477/* About 10 seconds */
1478#define SOCK_DESTROY_TIME (10*HZ)
1479
1480/* Sockets 0-1023 can't be bound to unless you are superuser */
1481#define PROT_SOCK 1024
1482
1483#define SHUTDOWN_MASK 3
1484#define RCV_SHUTDOWN 1
1485#define SEND_SHUTDOWN 2
1486
1da177e4
LT
1487#define SOCK_BINDADDR_LOCK 4
1488#define SOCK_BINDPORT_LOCK 8
1489
1da177e4
LT
1490struct socket_alloc {
1491 struct socket socket;
1492 struct inode vfs_inode;
1493};
1494
1495static inline struct socket *SOCKET_I(struct inode *inode)
1496{
1497 return &container_of(inode, struct socket_alloc, vfs_inode)->socket;
1498}
1499
1500static inline struct inode *SOCK_INODE(struct socket *socket)
1501{
1502 return &container_of(socket, struct socket_alloc, socket)->vfs_inode;
1503}
1504
3ab224be
HA
1505/*
1506 * Functions for memory accounting
1507 */
f8c3bf00 1508int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind);
69336bd2 1509int __sk_mem_schedule(struct sock *sk, int size, int kind);
f8c3bf00 1510void __sk_mem_reduce_allocated(struct sock *sk, int amount);
1a24e04e 1511void __sk_mem_reclaim(struct sock *sk, int amount);
1da177e4 1512
3ab224be
HA
1513#define SK_MEM_SEND 0
1514#define SK_MEM_RECV 1
1da177e4 1515
e70f3c70 1516/* sysctl_mem values are in pages */
bd68a2a8
ED
1517static inline long sk_prot_mem_limits(const struct sock *sk, int index)
1518{
816cd168 1519 return READ_ONCE(sk->sk_prot->sysctl_mem[index]);
bd68a2a8
ED
1520}
1521
3ab224be 1522static inline int sk_mem_pages(int amt)
1da177e4 1523{
100fdd1f 1524 return (amt + PAGE_SIZE - 1) >> PAGE_SHIFT;
1da177e4
LT
1525}
1526
dc6b9b78 1527static inline bool sk_has_account(struct sock *sk)
1da177e4 1528{
3ab224be
HA
1529 /* return true if protocol supports memory accounting */
1530 return !!sk->sk_prot->memory_allocated;
1da177e4
LT
1531}
1532
dc6b9b78 1533static inline bool sk_wmem_schedule(struct sock *sk, int size)
1da177e4 1534{
7c80b038
ED
1535 int delta;
1536
3ab224be 1537 if (!sk_has_account(sk))
dc6b9b78 1538 return true;
7c80b038
ED
1539 delta = size - sk->sk_forward_alloc;
1540 return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_SEND);
1da177e4
LT
1541}
1542
c76562b6 1543static inline bool
54f89b31 1544__sk_rmem_schedule(struct sock *sk, int size, bool pfmemalloc)
d80d99d6 1545{
7c80b038
ED
1546 int delta;
1547
3ab224be 1548 if (!sk_has_account(sk))
dc6b9b78 1549 return true;
7c80b038
ED
1550 delta = size - sk->sk_forward_alloc;
1551 return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_RECV) ||
54f89b31
CW
1552 pfmemalloc;
1553}
1554
1555static inline bool
1556sk_rmem_schedule(struct sock *sk, struct sk_buff *skb, int size)
1557{
1558 return __sk_rmem_schedule(sk, size, skb_pfmemalloc(skb));
3ab224be
HA
1559}
1560
2bb2f5fb
WW
1561static inline int sk_unused_reserved_mem(const struct sock *sk)
1562{
1563 int unused_mem;
1564
1565 if (likely(!sk->sk_reserved_mem))
1566 return 0;
1567
1568 unused_mem = sk->sk_reserved_mem - sk->sk_wmem_queued -
1569 atomic_read(&sk->sk_rmem_alloc);
1570
1571 return unused_mem > 0 ? unused_mem : 0;
1572}
1573
3ab224be
HA
1574static inline void sk_mem_reclaim(struct sock *sk)
1575{
2bb2f5fb
WW
1576 int reclaimable;
1577
3ab224be
HA
1578 if (!sk_has_account(sk))
1579 return;
2bb2f5fb
WW
1580
1581 reclaimable = sk->sk_forward_alloc - sk_unused_reserved_mem(sk);
1582
100fdd1f 1583 if (reclaimable >= (int)PAGE_SIZE)
2bb2f5fb
WW
1584 __sk_mem_reclaim(sk, reclaimable);
1585}
1586
1587static inline void sk_mem_reclaim_final(struct sock *sk)
1588{
1589 sk->sk_reserved_mem = 0;
1590 sk_mem_reclaim(sk);
3ab224be
HA
1591}
1592
1593static inline void sk_mem_charge(struct sock *sk, int size)
1594{
1595 if (!sk_has_account(sk))
1596 return;
5e6300e7 1597 sk_forward_alloc_add(sk, -size);
3ab224be
HA
1598}
1599
1600static inline void sk_mem_uncharge(struct sock *sk, int size)
1601{
1602 if (!sk_has_account(sk))
1603 return;
5e6300e7 1604 sk_forward_alloc_add(sk, size);
4890b686 1605 sk_mem_reclaim(sk);
3ab224be
HA
1606}
1607
0bb2f7a1
KI
1608#if IS_ENABLED(CONFIG_PROVE_LOCKING) && IS_ENABLED(CONFIG_MODULES)
1609static inline void sk_owner_set(struct sock *sk, struct module *owner)
1610{
1611 __module_get(owner);
1612 sk->sk_owner = owner;
1613}
1614
1615static inline void sk_owner_clear(struct sock *sk)
1616{
1617 sk->sk_owner = NULL;
1618}
1619
1620static inline void sk_owner_put(struct sock *sk)
1621{
1622 module_put(sk->sk_owner);
1623}
1624#else
1625static inline void sk_owner_set(struct sock *sk, struct module *owner)
1626{
1627}
1628
1629static inline void sk_owner_clear(struct sock *sk)
1630{
1631}
1632
1633static inline void sk_owner_put(struct sock *sk)
1634{
1635}
1636#endif
ed07536e
PZ
1637/*
1638 * Macro so as to not evaluate some arguments when
1639 * lockdep is not enabled.
1640 *
1641 * Mark both the sk_lock and the sk_lock.slock as a
1642 * per-address-family lock class.
1643 */
dc6b9b78 1644#define sock_lock_init_class_and_name(sk, sname, skey, name, key) \
ed07536e 1645do { \
0bb2f7a1 1646 sk_owner_set(sk, THIS_MODULE); \
e8f6fbf6 1647 sk->sk_lock.owned = 0; \
ed07536e
PZ
1648 init_waitqueue_head(&sk->sk_lock.wq); \
1649 spin_lock_init(&(sk)->sk_lock.slock); \
1650 debug_check_no_locks_freed((void *)&(sk)->sk_lock, \
0bb2f7a1 1651 sizeof((sk)->sk_lock)); \
ed07536e 1652 lockdep_set_class_and_name(&(sk)->sk_lock.slock, \
0bb2f7a1 1653 (skey), (sname)); \
ed07536e
PZ
1654 lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0); \
1655} while (0)
1656
05b93801 1657static inline bool lockdep_sock_is_held(const struct sock *sk)
1e1d04e6 1658{
1e1d04e6
HFS
1659 return lockdep_is_held(&sk->sk_lock) ||
1660 lockdep_is_held(&sk->sk_lock.slock);
1661}
1662
69336bd2 1663void lock_sock_nested(struct sock *sk, int subclass);
fcc70d5f
PZ
1664
1665static inline void lock_sock(struct sock *sk)
1666{
1667 lock_sock_nested(sk, 0);
1668}
1669
ad80b0fc 1670void __lock_sock(struct sock *sk);
8873c064 1671void __release_sock(struct sock *sk);
69336bd2 1672void release_sock(struct sock *sk);
1da177e4
LT
1673
1674/* BH context may only use the following locking interface. */
1675#define bh_lock_sock(__sk) spin_lock(&((__sk)->sk_lock.slock))
c6366184
IM
1676#define bh_lock_sock_nested(__sk) \
1677 spin_lock_nested(&((__sk)->sk_lock.slock), \
1678 SINGLE_DEPTH_NESTING)
1da177e4
LT
1679#define bh_unlock_sock(__sk) spin_unlock(&((__sk)->sk_lock.slock))
1680
49054556
PA
1681bool __lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock);
1682
1683/**
1684 * lock_sock_fast - fast version of lock_sock
1685 * @sk: socket
1686 *
70d0bb45 1687 * This version should be used for very small section, where process won't block
49054556
PA
1688 * return false if fast path is taken:
1689 *
1690 * sk_lock.slock locked, owned = 0, BH disabled
1691 *
1692 * return true if slow path is taken:
1693 *
1694 * sk_lock.slock unlocked, owned = 1, BH enabled
1695 */
1696static inline bool lock_sock_fast(struct sock *sk)
1697{
1698 /* The sk_lock has mutex_lock() semantics here. */
1699 mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
1700
1701 return __lock_sock_fast(sk);
1702}
1703
1704/* fast socket lock variant for caller already holding a [different] socket lock */
1705static inline bool lock_sock_fast_nested(struct sock *sk)
1706{
1707 mutex_acquire(&sk->sk_lock.dep_map, SINGLE_DEPTH_NESTING, 0, _RET_IP_);
1708
1709 return __lock_sock_fast(sk);
1710}
12f4bd86 1711
8a74ad60
ED
1712/**
1713 * unlock_sock_fast - complement of lock_sock_fast
1714 * @sk: socket
1715 * @slow: slow mode
1716 *
1717 * fast unlock socket for user context.
1718 * If slow mode is on, we call regular release_sock()
1719 */
1720static inline void unlock_sock_fast(struct sock *sk, bool slow)
12f4bd86 1721 __releases(&sk->sk_lock.slock)
4b0b72f7 1722{
12f4bd86 1723 if (slow) {
8a74ad60 1724 release_sock(sk);
12f4bd86
PA
1725 __release(&sk->sk_lock.slock);
1726 } else {
2dcb96ba 1727 mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
8a74ad60 1728 spin_unlock_bh(&sk->sk_lock.slock);
12f4bd86 1729 }
4b0b72f7
ED
1730}
1731
24426654
MKL
1732void sockopt_lock_sock(struct sock *sk);
1733void sockopt_release_sock(struct sock *sk);
e42c7bee
MKL
1734bool sockopt_ns_capable(struct user_namespace *ns, int cap);
1735bool sockopt_capable(int cap);
24426654 1736
fafc4e1e
HFS
1737/* Used by processes to "lock" a socket state, so that
1738 * interrupts and bottom half handlers won't change it
1739 * from under us. It essentially blocks any incoming
1740 * packets, so that we won't get any new data or any
1741 * packets that change the state of the socket.
1742 *
1743 * While locked, BH processing will add new packets to
1744 * the backlog queue. This queue is processed by the
1745 * owner of the socket lock right before it is released.
1746 *
1747 * Since ~2.3.5 it is also exclusive sleep lock serializing
1748 * accesses from user process context.
1749 */
1750
46cc6e49 1751static inline void sock_owned_by_me(const struct sock *sk)
fafc4e1e
HFS
1752{
1753#ifdef CONFIG_LOCKDEP
5e91f6ce 1754 WARN_ON_ONCE(!lockdep_sock_is_held(sk) && debug_locks);
fafc4e1e 1755#endif
46cc6e49
ED
1756}
1757
151c9c72
ED
1758static inline void sock_not_owned_by_me(const struct sock *sk)
1759{
1760#ifdef CONFIG_LOCKDEP
1761 WARN_ON_ONCE(lockdep_sock_is_held(sk) && debug_locks);
1762#endif
1763}
1764
46cc6e49
ED
1765static inline bool sock_owned_by_user(const struct sock *sk)
1766{
1767 sock_owned_by_me(sk);
fafc4e1e
HFS
1768 return sk->sk_lock.owned;
1769}
1770
602f7a27
TH
1771static inline bool sock_owned_by_user_nocheck(const struct sock *sk)
1772{
1773 return sk->sk_lock.owned;
1774}
1775
33d60fbd
KI
1776static inline void sock_release_ownership(struct sock *sk)
1777{
11445469
ED
1778 DEBUG_NET_WARN_ON_ONCE(!sock_owned_by_user_nocheck(sk));
1779 sk->sk_lock.owned = 0;
33d60fbd 1780
11445469
ED
1781 /* The sk_lock has mutex_unlock() semantics: */
1782 mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
33d60fbd
KI
1783}
1784
fafc4e1e
HFS
1785/* no reclassification while locks are held */
1786static inline bool sock_allow_reclassification(const struct sock *csk)
1787{
1788 struct sock *sk = (struct sock *)csk;
1789
33d60fbd
KI
1790 return !sock_owned_by_user_nocheck(sk) &&
1791 !spin_is_locked(&sk->sk_lock.slock);
fafc4e1e 1792}
4b0b72f7 1793
69336bd2 1794struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
11aa9c28 1795 struct proto *prot, int kern);
69336bd2 1796void sk_free(struct sock *sk);
5c70eb5c 1797void sk_net_refcnt_upgrade(struct sock *sk);
eb4cb008 1798void sk_destruct(struct sock *sk);
69336bd2
JP
1799struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority);
1800
1801struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
1802 gfp_t priority);
1d2077ac 1803void __sock_wfree(struct sk_buff *skb);
69336bd2 1804void sock_wfree(struct sk_buff *skb);
98ba0bd5
WB
1805struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
1806 gfp_t priority);
69336bd2
JP
1807void skb_orphan_partial(struct sk_buff *skb);
1808void sock_rfree(struct sk_buff *skb);
62bccb8c 1809void sock_efree(struct sk_buff *skb);
82eabd9e 1810#ifdef CONFIG_INET
69336bd2 1811void sock_edemux(struct sk_buff *skb);
cf7fbe66 1812void sock_pfree(struct sk_buff *skb);
5ced52fa
ED
1813
1814static inline void skb_set_owner_edemux(struct sk_buff *skb, struct sock *sk)
1815{
1816 skb_orphan(skb);
1817 if (refcount_inc_not_zero(&sk->sk_refcnt)) {
1818 skb->sk = sk;
1819 skb->destructor = sock_edemux;
1820 }
1821}
82eabd9e 1822#else
158f323b 1823#define sock_edemux sock_efree
82eabd9e 1824#endif
69336bd2 1825
29003875
MKL
1826int sk_setsockopt(struct sock *sk, int level, int optname,
1827 sockptr_t optval, unsigned int optlen);
69336bd2 1828int sock_setsockopt(struct socket *sock, int level, int op,
c8c1bbb6 1829 sockptr_t optval, unsigned int optlen);
1406245c
BL
1830int do_sock_setsockopt(struct socket *sock, bool compat, int level,
1831 int optname, sockptr_t optval, int optlen);
0b05b0cd
BL
1832int do_sock_getsockopt(struct socket *sock, bool compat, int level,
1833 int optname, sockptr_t optval, sockptr_t optlen);
69336bd2 1834
65ddc82d
MKL
1835int sk_getsockopt(struct sock *sk, int level, int optname,
1836 sockptr_t optval, sockptr_t optlen);
c7cbdbf2
AB
1837int sock_gettstamp(struct socket *sock, void __user *userstamp,
1838 bool timeval, bool time32);
69336bd2
JP
1839struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
1840 unsigned long data_len, int noblock,
1841 int *errcode, int max_page_order);
de32bc6a
PB
1842
1843static inline struct sk_buff *sock_alloc_send_skb(struct sock *sk,
1844 unsigned long size,
1845 int noblock, int *errcode)
1846{
1847 return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
1848}
1849
69336bd2 1850void *sock_kmalloc(struct sock *sk, int size, gfp_t priority);
456cc675
GT
1851void *sock_kmemdup(struct sock *sk, const void *src,
1852 int size, gfp_t priority);
69336bd2 1853void sock_kfree_s(struct sock *sk, void *mem, int size);
79e88659 1854void sock_kzfree_s(struct sock *sk, void *mem, int size);
69336bd2 1855void sk_send_sigurg(struct sock *sk);
1da177e4 1856
fee9ac06
PB
1857static inline void sock_replace_proto(struct sock *sk, struct proto *proto)
1858{
1859 if (sk->sk_socket)
1860 clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
1861 WRITE_ONCE(sk->sk_prot, proto);
1862}
1863
f28ea365 1864struct sockcm_cookie {
80b14dee 1865 u64 transmit_time;
f28ea365 1866 u32 mark;
b534dc46 1867 u32 tsflags;
4aecca4c 1868 u32 ts_opt_id;
a32f3e9d 1869 u32 priority;
bd618489 1870 u32 dmabuf_id;
f28ea365
EJ
1871};
1872
657a0667
WB
1873static inline void sockcm_init(struct sockcm_cookie *sockc,
1874 const struct sock *sk)
1875{
e3390b30 1876 *sockc = (struct sockcm_cookie) {
6ad86151 1877 .mark = READ_ONCE(sk->sk_mark),
a32f3e9d
AEN
1878 .tsflags = READ_ONCE(sk->sk_tsflags),
1879 .priority = READ_ONCE(sk->sk_priority),
e3390b30 1880 };
657a0667
WB
1881}
1882
233baf9a 1883int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg,
39771b12 1884 struct sockcm_cookie *sockc);
f28ea365
EJ
1885int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
1886 struct sockcm_cookie *sockc);
1887
1da177e4
LT
1888/*
1889 * Functions to fill in entries in struct proto_ops when a protocol
1890 * does not implement a particular function.
1891 */
69336bd2
JP
1892int sock_no_bind(struct socket *, struct sockaddr *, int);
1893int sock_no_connect(struct socket *, struct sockaddr *, int, int);
1894int sock_no_socketpair(struct socket *, struct socket *);
92ef0fd5 1895int sock_no_accept(struct socket *, struct socket *, struct proto_accept_arg *);
9b2c45d4 1896int sock_no_getname(struct socket *, struct sockaddr *, int);
69336bd2
JP
1897int sock_no_ioctl(struct socket *, unsigned int, unsigned long);
1898int sock_no_listen(struct socket *, int);
1899int sock_no_shutdown(struct socket *, int);
1b784140 1900int sock_no_sendmsg(struct socket *, struct msghdr *, size_t);
306b13eb 1901int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len);
1b784140 1902int sock_no_recvmsg(struct socket *, struct msghdr *, size_t, int);
69336bd2
JP
1903int sock_no_mmap(struct file *file, struct socket *sock,
1904 struct vm_area_struct *vma);
1da177e4
LT
1905
1906/*
1907 * Functions to fill in entries in struct proto_ops when a protocol
1908 * uses the inet style.
1909 */
69336bd2 1910int sock_common_getsockopt(struct socket *sock, int level, int optname,
1da177e4 1911 char __user *optval, int __user *optlen);
1b784140
YX
1912int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1913 int flags);
69336bd2 1914int sock_common_setsockopt(struct socket *sock, int level, int optname,
a7b75c5a 1915 sockptr_t optval, unsigned int optlen);
1da177e4 1916
69336bd2 1917void sk_common_release(struct sock *sk);
1da177e4
LT
1918
1919/*
1920 * Default socket callbacks and setup code
1921 */
dc6b9b78 1922
584f3742
PB
1923/* Initialise core socket variables using an explicit uid. */
1924void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid);
1925
1926/* Initialise core socket variables.
1927 * Assumes struct socket *sock is embedded in a struct socket_alloc.
1928 */
69336bd2 1929void sock_init_data(struct socket *sock, struct sock *sk);
1da177e4 1930
1da177e4
LT
1931/*
1932 * Socket reference counting postulates.
1933 *
1934 * * Each user of socket SHOULD hold a reference count.
1935 * * Each access point to socket (an hash table bucket, reference from a list,
1936 * running timer, skb in flight MUST hold a reference count.
1937 * * When reference count hits 0, it means it will never increase back.
1938 * * When reference count hits 0, it means that no references from
1939 * outside exist to this socket and current process on current CPU
1940 * is last user and may/should destroy this socket.
1941 * * sk_free is called from any context: process, BH, IRQ. When
1942 * it is called, socket has no references from outside -> sk_free
1943 * may release descendant resources allocated by the socket, but
1944 * to the time when it is called, socket is NOT referenced by any
1945 * hash tables, lists etc.
1946 * * Packets, delivered from outside (from network or from another process)
1947 * and enqueued on receive/error queues SHOULD NOT grab reference count,
1948 * when they sit in queue. Otherwise, packets will leak to hole, when
1949 * socket is looked up by one cpu and unhasing is made by another CPU.
1950 * It is true for udp/raw, netlink (leak to receive and error queues), tcp
1951 * (leak to backlog). Packet socket does all the processing inside
1952 * BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets
1953 * use separate SMP lock, so that they are prone too.
1954 */
1955
1956/* Ungrab socket and destroy it, if it was the last reference. */
1957static inline void sock_put(struct sock *sk)
1958{
41c6d650 1959 if (refcount_dec_and_test(&sk->sk_refcnt))
1da177e4
LT
1960 sk_free(sk);
1961}
05dbc7b5 1962/* Generic version of sock_put(), dealing with all sockets
41b822c5 1963 * (TCP_TIMEWAIT, TCP_NEW_SYN_RECV, ESTABLISHED...)
05dbc7b5
ED
1964 */
1965void sock_gen_put(struct sock *sk);
1da177e4 1966
4f0c40d9 1967int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested,
c3f24cfb 1968 unsigned int trim_cap, bool refcounted);
4f0c40d9
WB
1969static inline int sk_receive_skb(struct sock *sk, struct sk_buff *skb,
1970 const int nested)
1971{
c3f24cfb 1972 return __sk_receive_skb(sk, skb, nested, 1, true);
4f0c40d9 1973}
25995ff5 1974
e022f0b4
KK
1975static inline void sk_tx_queue_set(struct sock *sk, int tx_queue)
1976{
755c31cd
AN
1977 /* sk_tx_queue_mapping accept only upto a 16-bit value */
1978 if (WARN_ON_ONCE((unsigned short)tx_queue >= USHRT_MAX))
1979 return;
0bb4d124
ED
1980 /* Paired with READ_ONCE() in sk_tx_queue_get() and
1981 * other WRITE_ONCE() because socket lock might be not held.
1982 */
1983 WRITE_ONCE(sk->sk_tx_queue_mapping, tx_queue);
e022f0b4
KK
1984}
1985
755c31cd
AN
1986#define NO_QUEUE_MAPPING USHRT_MAX
1987
e022f0b4
KK
1988static inline void sk_tx_queue_clear(struct sock *sk)
1989{
0bb4d124
ED
1990 /* Paired with READ_ONCE() in sk_tx_queue_get() and
1991 * other WRITE_ONCE() because socket lock might be not held.
1992 */
1993 WRITE_ONCE(sk->sk_tx_queue_mapping, NO_QUEUE_MAPPING);
e022f0b4
KK
1994}
1995
1996static inline int sk_tx_queue_get(const struct sock *sk)
1997{
0bb4d124
ED
1998 if (sk) {
1999 /* Paired with WRITE_ONCE() in sk_tx_queue_clear()
2000 * and sk_tx_queue_set().
2001 */
2002 int val = READ_ONCE(sk->sk_tx_queue_mapping);
755c31cd 2003
0bb4d124
ED
2004 if (val != NO_QUEUE_MAPPING)
2005 return val;
2006 }
755c31cd 2007 return -1;
e022f0b4
KK
2008}
2009
a37a0ee4
ED
2010static inline void __sk_rx_queue_set(struct sock *sk,
2011 const struct sk_buff *skb,
2012 bool force_set)
c6345ce7 2013{
4e1beecc 2014#ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
c6345ce7
AN
2015 if (skb_rx_queue_recorded(skb)) {
2016 u16 rx_queue = skb_get_rx_queue(skb);
2017
a37a0ee4
ED
2018 if (force_set ||
2019 unlikely(READ_ONCE(sk->sk_rx_queue_mapping) != rx_queue))
342159ee 2020 WRITE_ONCE(sk->sk_rx_queue_mapping, rx_queue);
c6345ce7
AN
2021 }
2022#endif
2023}
2024
a37a0ee4
ED
2025static inline void sk_rx_queue_set(struct sock *sk, const struct sk_buff *skb)
2026{
2027 __sk_rx_queue_set(sk, skb, true);
2028}
2029
2030static inline void sk_rx_queue_update(struct sock *sk, const struct sk_buff *skb)
2031{
2032 __sk_rx_queue_set(sk, skb, false);
2033}
2034
c6345ce7
AN
2035static inline void sk_rx_queue_clear(struct sock *sk)
2036{
4e1beecc 2037#ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
09b89846 2038 WRITE_ONCE(sk->sk_rx_queue_mapping, NO_QUEUE_MAPPING);
c6345ce7
AN
2039#endif
2040}
2041
fc9bab24
AN
2042static inline int sk_rx_queue_get(const struct sock *sk)
2043{
4e1beecc 2044#ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
09b89846
ED
2045 if (sk) {
2046 int res = READ_ONCE(sk->sk_rx_queue_mapping);
2047
2048 if (res != NO_QUEUE_MAPPING)
2049 return res;
2050 }
4e1beecc 2051#endif
fc9bab24
AN
2052
2053 return -1;
2054}
fc9bab24 2055
972692e0
DM
2056static inline void sk_set_socket(struct sock *sk, struct socket *sock)
2057{
2058 sk->sk_socket = sock;
2059}
2060
aa395145
ED
2061static inline wait_queue_head_t *sk_sleep(struct sock *sk)
2062{
eaefd110
ED
2063 BUILD_BUG_ON(offsetof(struct socket_wq, wait) != 0);
2064 return &rcu_dereference_raw(sk->sk_wq)->wait;
aa395145 2065}
1da177e4
LT
2066/* Detach socket from process context.
2067 * Announce socket dead, detach it from wait queue and inode.
2068 * Note that parent inode held reference count on this struct sock,
2069 * we do not release it in this function, because protocol
2070 * probably wants some additional cleanups or even continuing
2071 * to work with this socket (TCP).
2072 */
2073static inline void sock_orphan(struct sock *sk)
2074{
2075 write_lock_bh(&sk->sk_callback_lock);
2076 sock_set_flag(sk, SOCK_DEAD);
972692e0 2077 sk_set_socket(sk, NULL);
43815482 2078 sk->sk_wq = NULL;
1da177e4
LT
2079 write_unlock_bh(&sk->sk_callback_lock);
2080}
2081
2082static inline void sock_graft(struct sock *sk, struct socket *parent)
2083{
0ffdaf5b 2084 WARN_ON(parent->sk);
1da177e4 2085 write_lock_bh(&sk->sk_callback_lock);
333f7909 2086 rcu_assign_pointer(sk->sk_wq, &parent->wq);
1da177e4 2087 parent->sk = sk;
972692e0 2088 sk_set_socket(sk, parent);
86741ec2 2089 sk->sk_uid = SOCK_INODE(parent)->i_uid;
4237c75c 2090 security_sock_graft(sk, parent);
1da177e4
LT
2091 write_unlock_bh(&sk->sk_callback_lock);
2092}
2093
69336bd2 2094kuid_t sock_i_uid(struct sock *sk);
25a9c8a4 2095unsigned long __sock_i_ino(struct sock *sk);
69336bd2 2096unsigned long sock_i_ino(struct sock *sk);
1da177e4 2097
86741ec2
LC
2098static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
2099{
2100 return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
2101}
2102
58d607d3 2103static inline u32 net_tx_rndhash(void)
877d1f62 2104{
a251c17a 2105 u32 v = get_random_u32();
58d607d3
ED
2106
2107 return v ?: 1;
2108}
877d1f62 2109
58d607d3
ED
2110static inline void sk_set_txhash(struct sock *sk)
2111{
b71eaed8
ED
2112 /* This pairs with READ_ONCE() in skb_set_hash_from_sk() */
2113 WRITE_ONCE(sk->sk_txhash, net_tx_rndhash());
877d1f62
TH
2114}
2115
9c30ae83 2116static inline bool sk_rethink_txhash(struct sock *sk)
265f94ff 2117{
26859240 2118 if (sk->sk_txhash && sk->sk_txrehash == SOCK_TXREHASH_ENABLED) {
265f94ff 2119 sk_set_txhash(sk);
9c30ae83
YC
2120 return true;
2121 }
2122 return false;
265f94ff
TH
2123}
2124
1da177e4 2125static inline struct dst_entry *
5033f58d 2126__sk_dst_get(const struct sock *sk)
1da177e4 2127{
1e1d04e6
HFS
2128 return rcu_dereference_check(sk->sk_dst_cache,
2129 lockdep_sock_is_held(sk));
1da177e4
LT
2130}
2131
2132static inline struct dst_entry *
5033f58d 2133sk_dst_get(const struct sock *sk)
1da177e4
LT
2134{
2135 struct dst_entry *dst;
2136
b6c6712a
ED
2137 rcu_read_lock();
2138 dst = rcu_dereference(sk->sk_dst_cache);
bc9d3a9f 2139 if (dst && !rcuref_get(&dst->__rcuref))
f8864972 2140 dst = NULL;
b6c6712a 2141 rcu_read_unlock();
1da177e4
LT
2142 return dst;
2143}
2144
9c30ae83 2145static inline void __dst_negative_advice(struct sock *sk)
b6c6712a 2146{
92f1655a 2147 struct dst_entry *dst = __sk_dst_get(sk);
b6c6712a 2148
92f1655a
ED
2149 if (dst && dst->ops->negative_advice)
2150 dst->ops->negative_advice(sk, dst);
b6c6712a
ED
2151}
2152
9c30ae83
YC
2153static inline void dst_negative_advice(struct sock *sk)
2154{
2155 sk_rethink_txhash(sk);
2156 __dst_negative_advice(sk);
2157}
2158
1da177e4
LT
2159static inline void
2160__sk_dst_set(struct sock *sk, struct dst_entry *dst)
2161{
2162 struct dst_entry *old_dst;
2163
e022f0b4 2164 sk_tx_queue_clear(sk);
eb44ad4e 2165 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
95964c6d
ED
2166 old_dst = rcu_dereference_protected(sk->sk_dst_cache,
2167 lockdep_sock_is_held(sk));
b6c6712a 2168 rcu_assign_pointer(sk->sk_dst_cache, dst);
1da177e4
LT
2169 dst_release(old_dst);
2170}
2171
2172static inline void
2173sk_dst_set(struct sock *sk, struct dst_entry *dst)
2174{
7f502361
ED
2175 struct dst_entry *old_dst;
2176
2177 sk_tx_queue_clear(sk);
eb44ad4e 2178 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
b4cb4a13 2179 old_dst = unrcu_pointer(xchg(&sk->sk_dst_cache, RCU_INITIALIZER(dst)));
7f502361 2180 dst_release(old_dst);
1da177e4
LT
2181}
2182
2183static inline void
2184__sk_dst_reset(struct sock *sk)
2185{
b6c6712a 2186 __sk_dst_set(sk, NULL);
1da177e4
LT
2187}
2188
2189static inline void
2190sk_dst_reset(struct sock *sk)
2191{
7f502361 2192 sk_dst_set(sk, NULL);
1da177e4
LT
2193}
2194
69336bd2 2195struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);
1da177e4 2196
69336bd2 2197struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
1da177e4 2198
9b8805a3
JA
2199static inline void sk_dst_confirm(struct sock *sk)
2200{
25c7a6d1
ED
2201 if (!READ_ONCE(sk->sk_dst_pending_confirm))
2202 WRITE_ONCE(sk->sk_dst_pending_confirm, 1);
9b8805a3
JA
2203}
2204
4ff06203
JA
2205static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n)
2206{
2207 if (skb_get_dst_pending_confirm(skb)) {
2208 struct sock *sk = skb->sk;
4ff06203 2209
25c7a6d1
ED
2210 if (sk && READ_ONCE(sk->sk_dst_pending_confirm))
2211 WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
1e84dc6b 2212 neigh_confirm(n);
4ff06203
JA
2213 }
2214}
2215
d986f521 2216bool sk_mc_loop(const struct sock *sk);
f60e5990 2217
dc6b9b78 2218static inline bool sk_can_gso(const struct sock *sk)
bcd76111
HX
2219{
2220 return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type);
2221}
2222
69336bd2 2223void sk_setup_caps(struct sock *sk, struct dst_entry *dst);
6cbb0df7 2224
aba54656 2225static inline void sk_gso_disable(struct sock *sk)
a465419b 2226{
aba54656
ED
2227 sk->sk_gso_disabled = 1;
2228 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
a465419b
ED
2229}
2230
c6e1a0d1 2231static inline int skb_do_copy_data_nocache(struct sock *sk, struct sk_buff *skb,
57be5bda 2232 struct iov_iter *from, char *to,
912d398d 2233 int copy, int offset)
c6e1a0d1
TH
2234{
2235 if (skb->ip_summed == CHECKSUM_NONE) {
57be5bda 2236 __wsum csum = 0;
15e6cb46 2237 if (!csum_and_copy_from_iter_full(to, copy, &csum, from))
57be5bda 2238 return -EFAULT;
912d398d 2239 skb->csum = csum_block_add(skb->csum, csum, offset);
c6e1a0d1 2240 } else if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
15e6cb46 2241 if (!copy_from_iter_full_nocache(to, copy, from))
c6e1a0d1 2242 return -EFAULT;
15e6cb46 2243 } else if (!copy_from_iter_full(to, copy, from))
c6e1a0d1
TH
2244 return -EFAULT;
2245
2246 return 0;
2247}
2248
2249static inline int skb_add_data_nocache(struct sock *sk, struct sk_buff *skb,
57be5bda 2250 struct iov_iter *from, int copy)
c6e1a0d1 2251{
912d398d 2252 int err, offset = skb->len;
c6e1a0d1 2253
912d398d
WY
2254 err = skb_do_copy_data_nocache(sk, skb, from, skb_put(skb, copy),
2255 copy, offset);
c6e1a0d1 2256 if (err)
912d398d 2257 __skb_trim(skb, offset);
c6e1a0d1
TH
2258
2259 return err;
2260}
2261
57be5bda 2262static inline int skb_copy_to_page_nocache(struct sock *sk, struct iov_iter *from,
c6e1a0d1
TH
2263 struct sk_buff *skb,
2264 struct page *page,
2265 int off, int copy)
2266{
2267 int err;
2268
912d398d
WY
2269 err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) + off,
2270 copy, skb->len);
c6e1a0d1
TH
2271 if (err)
2272 return err;
2273
ede57d58 2274 skb_len_add(skb, copy);
ab4e846a 2275 sk_wmem_queued_add(sk, copy);
c6e1a0d1
TH
2276 sk_mem_charge(sk, copy);
2277 return 0;
2278}
2279
c564039f
ED
2280/**
2281 * sk_wmem_alloc_get - returns write allocations
2282 * @sk: socket
2283 *
66256e0b 2284 * Return: sk_wmem_alloc minus initial offset of one
c564039f
ED
2285 */
2286static inline int sk_wmem_alloc_get(const struct sock *sk)
2287{
14afee4b 2288 return refcount_read(&sk->sk_wmem_alloc) - 1;
c564039f
ED
2289}
2290
2291/**
2292 * sk_rmem_alloc_get - returns read allocations
2293 * @sk: socket
2294 *
66256e0b 2295 * Return: sk_rmem_alloc
c564039f
ED
2296 */
2297static inline int sk_rmem_alloc_get(const struct sock *sk)
2298{
2299 return atomic_read(&sk->sk_rmem_alloc);
2300}
2301
2302/**
2303 * sk_has_allocations - check if allocations are outstanding
2304 * @sk: socket
2305 *
66256e0b 2306 * Return: true if socket has write or read allocations
c564039f 2307 */
dc6b9b78 2308static inline bool sk_has_allocations(const struct sock *sk)
c564039f
ED
2309{
2310 return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
2311}
2312
a57de0b4 2313/**
1ce0bf50 2314 * skwq_has_sleeper - check if there are any waiting processes
acfbe96a 2315 * @wq: struct socket_wq
a57de0b4 2316 *
66256e0b 2317 * Return: true if socket_wq has waiting processes
a57de0b4 2318 *
1ce0bf50 2319 * The purpose of the skwq_has_sleeper and sock_poll_wait is to wrap the memory
a57de0b4
JO
2320 * barrier call. They were added due to the race found within the tcp code.
2321 *
d651983d 2322 * Consider following tcp code paths::
a57de0b4 2323 *
d651983d
MCC
2324 * CPU1 CPU2
2325 * sys_select receive packet
a57de0b4
JO
2326 * ... ...
2327 * __add_wait_queue update tp->rcv_nxt
2328 * ... ...
2329 * tp->rcv_nxt check sock_def_readable
2330 * ... {
43815482
ED
2331 * schedule rcu_read_lock();
2332 * wq = rcu_dereference(sk->sk_wq);
2333 * if (wq && waitqueue_active(&wq->wait))
2334 * wake_up_interruptible(&wq->wait)
a57de0b4
JO
2335 * ...
2336 * }
2337 *
2338 * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
2339 * in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1
2340 * could then endup calling schedule and sleep forever if there are no more
2341 * data on the socket.
ad462769 2342 *
a57de0b4 2343 */
1ce0bf50 2344static inline bool skwq_has_sleeper(struct socket_wq *wq)
a57de0b4 2345{
1ce0bf50 2346 return wq && wq_has_sleeper(&wq->wait);
a57de0b4
JO
2347}
2348
2349/**
b2849867 2350 * sock_poll_wait - wrapper for the poll_wait call.
a57de0b4 2351 * @filp: file
89ab066d 2352 * @sock: socket to wait on
a57de0b4
JO
2353 * @p: poll_table
2354 *
43815482 2355 * See the comments in the wq_has_sleeper function.
a57de0b4 2356 */
89ab066d
KG
2357static inline void sock_poll_wait(struct file *filp, struct socket *sock,
2358 poll_table *p)
a57de0b4 2359{
b2849867
ON
2360 /* Provides a barrier we need to be sure we are in sync
2361 * with the socket flags modification.
2362 *
2363 * This memory barrier is paired in the wq_has_sleeper.
2364 */
2365 poll_wait(filp, &sock->wq.wait, p);
a57de0b4
JO
2366}
2367
b73c3d0e
TH
2368static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
2369{
b71eaed8
ED
2370 /* This pairs with WRITE_ONCE() in sk_set_txhash() */
2371 u32 txhash = READ_ONCE(sk->sk_txhash);
2372
2373 if (txhash) {
b73c3d0e 2374 skb->l4_hash = 1;
b71eaed8 2375 skb->hash = txhash;
b73c3d0e
TH
2376 }
2377}
2378
9e17f8a4
ED
2379void skb_set_owner_w(struct sk_buff *skb, struct sock *sk);
2380
1da177e4 2381/*
dc6b9b78 2382 * Queue a received datagram if it will fit. Stream and sequenced
1da177e4
LT
2383 * protocols can't normally use this as they need to fit buffers in
2384 * and play with them.
2385 *
dc6b9b78 2386 * Inlined as it's very short and called for pretty much every
1da177e4
LT
2387 * packet ever received.
2388 */
1da177e4
LT
2389static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
2390{
d55d87fd 2391 skb_orphan(skb);
1da177e4
LT
2392 skb->sk = sk;
2393 skb->destructor = sock_rfree;
2394 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3ab224be 2395 sk_mem_charge(sk, skb->truesize);
1da177e4
LT
2396}
2397
098116e7 2398static inline __must_check bool skb_set_owner_sk_safe(struct sk_buff *skb, struct sock *sk)
9adc89af
PA
2399{
2400 if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
2401 skb_orphan(skb);
2402 skb->destructor = sock_efree;
2403 skb->sk = sk;
098116e7 2404 return true;
9adc89af 2405 }
098116e7 2406 return false;
9adc89af
PA
2407}
2408
ca43ccf4
KI
2409static inline struct sk_buff *skb_clone_and_charge_r(struct sk_buff *skb, struct sock *sk)
2410{
2411 skb = skb_clone(skb, sk_gfp_mask(sk, GFP_ATOMIC));
2412 if (skb) {
2413 if (sk_rmem_schedule(sk, skb, skb->truesize)) {
2414 skb_set_owner_r(skb, sk);
2415 return skb;
2416 }
2417 __kfree_skb(skb);
2418 }
2419 return NULL;
2420}
2421
5e10da53
PA
2422static inline void skb_prepare_for_gro(struct sk_buff *skb)
2423{
2424 if (skb->destructor != sock_wfree) {
2425 skb_orphan(skb);
2426 return;
2427 }
2428 skb->slow_gro = 1;
2429}
2430
69336bd2
JP
2431void sk_reset_timer(struct sock *sk, struct timer_list *timer,
2432 unsigned long expires);
1da177e4 2433
69336bd2 2434void sk_stop_timer(struct sock *sk, struct timer_list *timer);
1da177e4 2435
08b81d87
GT
2436void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer);
2437
65101aec
PA
2438int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
2439 struct sk_buff *skb, unsigned int flags,
69629464
ED
2440 void (*destructor)(struct sock *sk,
2441 struct sk_buff *skb));
e6afc8ac 2442int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
c1b8a567
MD
2443
2444int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
2445 enum skb_drop_reason *reason);
2446
2447static inline int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2448{
2449 return sock_queue_rcv_skb_reason(sk, skb, NULL);
2450}
1da177e4 2451
69336bd2 2452int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb);
364a9e93 2453struct sk_buff *sock_dequeue_err_skb(struct sock *sk);
1da177e4
LT
2454
2455/*
2456 * Recover an error report and clear atomically
2457 */
dc6b9b78 2458
1da177e4
LT
2459static inline int sock_error(struct sock *sk)
2460{
c1cbe4b7 2461 int err;
f13ef100
ED
2462
2463 /* Avoid an atomic operation for the common case.
2464 * This is racy since another cpu/thread can change sk_err under us.
2465 */
2466 if (likely(data_race(!sk->sk_err)))
c1cbe4b7 2467 return 0;
f13ef100 2468
c1cbe4b7 2469 err = xchg(&sk->sk_err, 0);
1da177e4
LT
2470 return -err;
2471}
2472
e3ae2365
AA
2473void sk_error_report(struct sock *sk);
2474
1da177e4
LT
2475static inline unsigned long sock_wspace(struct sock *sk)
2476{
2477 int amt = 0;
2478
2479 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
14afee4b 2480 amt = sk->sk_sndbuf - refcount_read(&sk->sk_wmem_alloc);
dc6b9b78 2481 if (amt < 0)
1da177e4
LT
2482 amt = 0;
2483 }
2484 return amt;
2485}
2486
ceb5d58b
ED
2487/* Note:
2488 * We use sk->sk_wq_raw, from contexts knowing this
2489 * pointer is not NULL and cannot disappear/change.
2490 */
9cd3e072 2491static inline void sk_set_bit(int nr, struct sock *sk)
1da177e4 2492{
4be73522
ED
2493 if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) &&
2494 !sock_flag(sk, SOCK_FASYNC))
9317bb69
ED
2495 return;
2496
ceb5d58b 2497 set_bit(nr, &sk->sk_wq_raw->flags);
9cd3e072
ED
2498}
2499
2500static inline void sk_clear_bit(int nr, struct sock *sk)
2501{
4be73522
ED
2502 if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) &&
2503 !sock_flag(sk, SOCK_FASYNC))
9317bb69
ED
2504 return;
2505
ceb5d58b 2506 clear_bit(nr, &sk->sk_wq_raw->flags);
9cd3e072
ED
2507}
2508
ceb5d58b 2509static inline void sk_wake_async(const struct sock *sk, int how, int band)
1da177e4 2510{
ceb5d58b
ED
2511 if (sock_flag(sk, SOCK_FASYNC)) {
2512 rcu_read_lock();
2513 sock_wake_async(rcu_dereference(sk->sk_wq), how, band);
2514 rcu_read_unlock();
2515 }
1da177e4
LT
2516}
2517
1abe267f
ED
2518static inline void sk_wake_async_rcu(const struct sock *sk, int how, int band)
2519{
2520 if (unlikely(sock_flag(sk, SOCK_FASYNC)))
2521 sock_wake_async(rcu_dereference(sk->sk_wq), how, band);
2522}
2523
eea86af6
DB
2524/* Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
2525 * need sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak.
2526 * Note: for send buffers, TCP works better if we can build two skbs at
2527 * minimum.
7a91b434 2528 */
9eb5bf83 2529#define TCP_SKB_MIN_TRUESIZE (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
eea86af6
DB
2530
2531#define SOCK_MIN_SNDBUF (TCP_SKB_MIN_TRUESIZE * 2)
2532#define SOCK_MIN_RCVBUF TCP_SKB_MIN_TRUESIZE
1da177e4
LT
2533
2534static inline void sk_stream_moderate_sndbuf(struct sock *sk)
2535{
e292f05e
ED
2536 u32 val;
2537
2538 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
2539 return;
2540
2541 val = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
ca057051 2542 val = max_t(u32, val, sk_unused_reserved_mem(sk));
e292f05e
ED
2543
2544 WRITE_ONCE(sk->sk_sndbuf, max_t(u32, val, SOCK_MIN_SNDBUF));
1da177e4
LT
2545}
2546
5640f768
ED
2547/**
2548 * sk_page_frag - return an appropriate page_frag
2549 * @sk: socket
2550 *
20eb4f29 2551 * Use the per task page_frag instead of the per socket one for
dacb5d88 2552 * optimization when we know that we're in process context and own
20eb4f29
TH
2553 * everything that's associated with %current.
2554 *
dacb5d88
PA
2555 * Both direct reclaim and page faults can nest inside other
2556 * socket operations and end up recursing into sk_page_frag()
2557 * while it's already in use: explicitly avoid task page_frag
08f65892 2558 * when users disable sk_use_task_frag.
66256e0b
RD
2559 *
2560 * Return: a per task page_frag if context allows that,
2561 * otherwise a per socket one.
5640f768
ED
2562 */
2563static inline struct page_frag *sk_page_frag(struct sock *sk)
1da177e4 2564{
08f65892 2565 if (sk->sk_use_task_frag)
5640f768 2566 return &current->task_frag;
1da177e4 2567
5640f768 2568 return &sk->sk_frag;
1da177e4
LT
2569}
2570
69336bd2 2571bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag);
5640f768 2572
1da177e4
LT
2573/*
2574 * Default write policy as shown to user space via poll/select/SIGIO
2575 */
dc6b9b78 2576static inline bool sock_writeable(const struct sock *sk)
1da177e4 2577{
e292f05e 2578 return refcount_read(&sk->sk_wmem_alloc) < (READ_ONCE(sk->sk_sndbuf) >> 1);
1da177e4
LT
2579}
2580
dd0fc66f 2581static inline gfp_t gfp_any(void)
1da177e4 2582{
99709372 2583 return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
1da177e4
LT
2584}
2585
4b1327be
WW
2586static inline gfp_t gfp_memcg_charge(void)
2587{
720ca52b 2588 return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
4b1327be
WW
2589}
2590
dc6b9b78 2591static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
1da177e4
LT
2592{
2593 return noblock ? 0 : sk->sk_rcvtimeo;
2594}
2595
dc6b9b78 2596static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
1da177e4
LT
2597{
2598 return noblock ? 0 : sk->sk_sndtimeo;
2599}
2600
2601static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
2602{
eac66402
ED
2603 int v = waitall ? len : min_t(int, READ_ONCE(sk->sk_rcvlowat), len);
2604
2605 return v ?: 1;
1da177e4
LT
2606}
2607
2608/* Alas, with timeout socket operations are not restartable.
2609 * Compare this to poll().
2610 */
2611static inline int sock_intr_errno(long timeo)
2612{
2613 return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
2614}
2615
744d5a3e
EB
2616struct sock_skb_cb {
2617 u32 dropcount;
2618};
2619
2620/* Store sock_skb_cb at the end of skb->cb[] so protocol families
2621 * using skb->cb[] would keep using it directly and utilize its
70d0bb45 2622 * alignment guarantee.
744d5a3e 2623 */
2b905deb
ZH
2624#define SOCK_SKB_CB_OFFSET (sizeof_field(struct sk_buff, cb) - \
2625 sizeof(struct sock_skb_cb))
744d5a3e
EB
2626
2627#define SOCK_SKB_CB(__skb) ((struct sock_skb_cb *)((__skb)->cb + \
2628 SOCK_SKB_CB_OFFSET))
2629
b4772ef8 2630#define sock_skb_cb_check_size(size) \
744d5a3e 2631 BUILD_BUG_ON((size) > SOCK_SKB_CB_OFFSET)
b4772ef8 2632
3bc3b96f
EB
2633static inline void
2634sock_skb_set_dropcount(const struct sock *sk, struct sk_buff *skb)
2635{
3665f381
ED
2636 SOCK_SKB_CB(skb)->dropcount = sock_flag(sk, SOCK_RXQ_OVFL) ?
2637 atomic_read(&sk->sk_drops) : 0;
3bc3b96f
EB
2638}
2639
532182cd
ED
2640static inline void sk_drops_add(struct sock *sk, const struct sk_buff *skb)
2641{
2642 int segs = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
2643
2644 atomic_add(segs, &sk->sk_drops);
2645}
2646
3a0ed3e9
DD
2647static inline ktime_t sock_read_timestamp(struct sock *sk)
2648{
2649#if BITS_PER_LONG==32
2650 unsigned int seq;
2651 ktime_t kt;
2652
2653 do {
2654 seq = read_seqbegin(&sk->sk_stamp_seq);
2655 kt = sk->sk_stamp;
2656 } while (read_seqretry(&sk->sk_stamp_seq, seq));
2657
2658 return kt;
2659#else
f75359f3 2660 return READ_ONCE(sk->sk_stamp);
3a0ed3e9
DD
2661#endif
2662}
2663
2664static inline void sock_write_timestamp(struct sock *sk, ktime_t kt)
2665{
2666#if BITS_PER_LONG==32
2667 write_seqlock(&sk->sk_stamp_seq);
2668 sk->sk_stamp = kt;
2669 write_sequnlock(&sk->sk_stamp_seq);
2670#else
f75359f3 2671 WRITE_ONCE(sk->sk_stamp, kt);
3a0ed3e9
DD
2672#endif
2673}
2674
69336bd2
JP
2675void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
2676 struct sk_buff *skb);
2677void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
2678 struct sk_buff *skb);
92f37fd2 2679
dc6b9b78 2680static inline void
1da177e4
LT
2681sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
2682{
20d49473 2683 struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
e3390b30
ED
2684 u32 tsflags = READ_ONCE(sk->sk_tsflags);
2685 ktime_t kt = skb->tstamp;
20d49473
PO
2686 /*
2687 * generate control messages if
b9f40e21 2688 * - receive time stamping in software requested
20d49473 2689 * - software time stamp available and wanted
20d49473 2690 * - hardware time stamps available and wanted
20d49473
PO
2691 */
2692 if (sock_flag(sk, SOCK_RCVTSTAMP) ||
e3390b30
ED
2693 (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) ||
2694 (kt && tsflags & SOF_TIMESTAMPING_SOFTWARE) ||
2456e855 2695 (hwtstamps->hwtstamp &&
e3390b30 2696 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)))
92f37fd2
ED
2697 __sock_recv_timestamp(msg, sk, skb);
2698 else
3a0ed3e9 2699 sock_write_timestamp(sk, kt);
6e3e939f 2700
eb6fba75 2701 if (sock_flag(sk, SOCK_WIFI_STATUS) && skb_wifi_acked_valid(skb))
6e3e939f 2702 __sock_recv_wifi_status(msg, sk, skb);
1da177e4
LT
2703}
2704
6fd1d51c
EM
2705void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
2706 struct sk_buff *skb);
767dd033 2707
6c7c98ba 2708#define SK_DEFAULT_STAMP (-1L * NSEC_PER_SEC)
6fd1d51c
EM
2709static inline void sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
2710 struct sk_buff *skb)
767dd033 2711{
6fd1d51c
EM
2712#define FLAGS_RECV_CMSGS ((1UL << SOCK_RXQ_OVFL) | \
2713 (1UL << SOCK_RCVTSTAMP) | \
f0e70409
PA
2714 (1UL << SOCK_RCVMARK) | \
2715 (1UL << SOCK_RCVPRIORITY) | \
2716 (1UL << SOCK_TIMESTAMPING_ANY))
b9f40e21
WB
2717#define TSFLAGS_ANY (SOF_TIMESTAMPING_SOFTWARE | \
2718 SOF_TIMESTAMPING_RAW_HARDWARE)
767dd033 2719
f0e70409 2720 if (READ_ONCE(sk->sk_flags) & FLAGS_RECV_CMSGS)
6fd1d51c 2721 __sock_recv_cmsgs(msg, sk, skb);
d3fbff30 2722 else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
3a0ed3e9 2723 sock_write_timestamp(sk, skb->tstamp);
dfd9248c 2724 else if (unlikely(sock_read_timestamp(sk) == SK_DEFAULT_STAMP))
3a0ed3e9 2725 sock_write_timestamp(sk, 0);
767dd033 2726}
3b885787 2727
822b5bc6 2728void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags);
67cc0d40 2729
20d49473 2730/**
8f932f76 2731 * _sock_tx_timestamp - checks whether the outgoing packet is to be time stamped
20d49473 2732 * @sk: socket sending this packet
822b5bc6 2733 * @sockc: pointer to socket cmsg cookie to get timestamping info
140c55d4 2734 * @tx_flags: completed with instructions for time stamping
8f932f76 2735 * @tskey: filled in with next sk_tskey (not for TCP, which uses seqno)
140c55d4 2736 *
d651983d 2737 * Note: callers should take care of initial ``*tx_flags`` value (usually 0)
20d49473 2738 */
822b5bc6
VF
2739static inline void _sock_tx_timestamp(struct sock *sk,
2740 const struct sockcm_cookie *sockc,
8f932f76 2741 __u8 *tx_flags, __u32 *tskey)
67cc0d40 2742{
822b5bc6
VF
2743 __u32 tsflags = sockc->tsflags;
2744
8f932f76 2745 if (unlikely(tsflags)) {
c14ac945 2746 __sock_tx_timestamp(tsflags, tx_flags);
8f932f76 2747 if (tsflags & SOF_TIMESTAMPING_OPT_ID && tskey &&
822b5bc6
VF
2748 tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) {
2749 if (tsflags & SOCKCM_FLAG_TS_OPT_ID)
2750 *tskey = sockc->ts_opt_id;
2751 else
2752 *tskey = atomic_inc_return(&sk->sk_tskey) - 1;
2753 }
8f932f76 2754 }
67cc0d40 2755}
20d49473 2756
822b5bc6
VF
2757static inline void sock_tx_timestamp(struct sock *sk,
2758 const struct sockcm_cookie *sockc,
8f932f76
WB
2759 __u8 *tx_flags)
2760{
822b5bc6 2761 _sock_tx_timestamp(sk, sockc, tx_flags, NULL);
8f932f76
WB
2762}
2763
822b5bc6
VF
2764static inline void skb_setup_tx_timestamp(struct sk_buff *skb,
2765 const struct sockcm_cookie *sockc)
8f932f76 2766{
822b5bc6 2767 _sock_tx_timestamp(skb->sk, sockc, &skb_shinfo(skb)->tx_flags,
8f932f76
WB
2768 &skb_shinfo(skb)->tskey);
2769}
2770
a54d51fb
ED
2771static inline bool sk_is_inet(const struct sock *sk)
2772{
2773 int family = READ_ONCE(sk->sk_family);
2774
2775 return family == AF_INET || family == AF_INET6;
2776}
2777
42f67eea
ED
2778static inline bool sk_is_tcp(const struct sock *sk)
2779{
a54d51fb
ED
2780 return sk_is_inet(sk) &&
2781 sk->sk_type == SOCK_STREAM &&
2782 sk->sk_protocol == IPPROTO_TCP;
2783}
2784
2785static inline bool sk_is_udp(const struct sock *sk)
2786{
2787 return sk_is_inet(sk) &&
2788 sk->sk_type == SOCK_DGRAM &&
2789 sk->sk_protocol == IPPROTO_UDP;
42f67eea
ED
2790}
2791
7d8d93fd
KI
2792static inline bool sk_is_unix(const struct sock *sk)
2793{
2794 return sk->sk_family == AF_UNIX;
2795}
2796
8d665064
JF
2797static inline bool sk_is_stream_unix(const struct sock *sk)
2798{
7d8d93fd 2799 return sk_is_unix(sk) && sk->sk_type == SOCK_STREAM;
8d665064
JF
2800}
2801
9c5bd93e
ML
2802static inline bool sk_is_vsock(const struct sock *sk)
2803{
2804 return sk->sk_family == AF_VSOCK;
2805}
2806
7d8d93fd
KI
2807static inline bool sk_may_scm_recv(const struct sock *sk)
2808{
2809 return (IS_ENABLED(CONFIG_UNIX) && sk->sk_family == AF_UNIX) ||
2810 sk->sk_family == AF_NETLINK ||
2811 (IS_ENABLED(CONFIG_BT) && sk->sk_family == AF_BLUETOOTH);
2812}
2813
1da177e4
LT
2814/**
2815 * sk_eat_skb - Release a skb if it is no longer needed
4dc3b16b
PP
2816 * @sk: socket to eat this skb from
2817 * @skb: socket buffer to eat
1da177e4
LT
2818 *
2819 * This routine must be called with interrupts disabled or with the socket
2820 * locked so that the sk_buff queue operation is ok.
2821*/
7bced397 2822static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb)
1da177e4
LT
2823{
2824 __skb_unlink(skb, &sk->sk_receive_queue);
2825 __kfree_skb(skb);
2826}
2827
cf7fbe66
JS
2828static inline bool
2829skb_sk_is_prefetched(struct sk_buff *skb)
2830{
2831#ifdef CONFIG_INET
2832 return skb->destructor == sock_pfree;
2833#else
2834 return false;
2835#endif /* CONFIG_INET */
2836}
2837
7ae215d2
JS
2838/* This helper checks if a socket is a full socket,
2839 * ie _not_ a timewait or request socket.
2840 */
2841static inline bool sk_fullsock(const struct sock *sk)
2842{
2843 return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV);
2844}
2845
2846static inline bool
2847sk_is_refcounted(struct sock *sk)
2848{
2849 /* Only full sockets have sk->sk_flags. */
2850 return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE);
2851}
2852
d7500fbf
BK
2853static inline bool
2854sk_requests_wifi_status(struct sock *sk)
2855{
2856 return sk && sk_fullsock(sk) && sock_flag(sk, SOCK_WIFI_STATUS);
2857}
2858
ebf4e808
IL
2859/* Checks if this SKB belongs to an HW offloaded socket
2860 * and whether any SW fallbacks are required based on dev.
41477662 2861 * Check decrypted mark in case skb_orphan() cleared socket.
ebf4e808
IL
2862 */
2863static inline struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
2864 struct net_device *dev)
2865{
2866#ifdef CONFIG_SOCK_VALIDATE_XMIT
2867 struct sock *sk = skb->sk;
2868
41477662 2869 if (sk && sk_fullsock(sk) && sk->sk_validate_xmit_skb) {
ebf4e808 2870 skb = sk->sk_validate_xmit_skb(sk, dev, skb);
9f06f87f 2871 } else if (unlikely(skb_is_decrypted(skb))) {
41477662
JK
2872 pr_warn_ratelimited("unencrypted skb with no associated socket - dropping\n");
2873 kfree_skb(skb);
2874 skb = NULL;
41477662 2875 }
ebf4e808
IL
2876#endif
2877
2878 return skb;
2879}
2880
e446f9df
ED
2881/* This helper checks if a socket is a LISTEN or NEW_SYN_RECV
2882 * SYNACK messages can be attached to either ones (depending on SYNCOOKIE)
2883 */
2884static inline bool sk_listener(const struct sock *sk)
2885{
2886 return (1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV);
2887}
2888
bc43a3c8
ED
2889/* This helper checks if a socket is a LISTEN or NEW_SYN_RECV or TIME_WAIT
2890 * TCP SYNACK messages can be attached to LISTEN or NEW_SYN_RECV (depending on SYNCOOKIE)
2891 * TCP RST and ACK can be attached to TIME_WAIT.
2892 */
2893static inline bool sk_listener_or_tw(const struct sock *sk)
2894{
2895 return (1 << READ_ONCE(sk->sk_state)) &
2896 (TCPF_LISTEN | TCPF_NEW_SYN_RECV | TCPF_TIME_WAIT);
2897}
2898
193d357d 2899void sock_enable_timestamp(struct sock *sk, enum sock_flags flag);
69336bd2
JP
2900int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
2901 int type);
1da177e4 2902
a3b299da
EB
2903bool sk_ns_capable(const struct sock *sk,
2904 struct user_namespace *user_ns, int cap);
2905bool sk_capable(const struct sock *sk, int cap);
2906bool sk_net_capable(const struct sock *sk, int cap);
2907
a2d133b1
JH
2908void sk_get_meminfo(const struct sock *sk, u32 *meminfo);
2909
eaa72dc4
ED
2910/* Take into consideration the size of the struct sk_buff overhead in the
2911 * determination of these values, since that is non-constant across
2912 * platforms. This makes socket queueing behavior and performance
2913 * not depend upon such differences.
2914 */
2915#define _SK_MEM_PACKETS 256
2916#define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
2917#define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
2918#define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
2919
1da177e4
LT
2920extern __u32 sysctl_wmem_max;
2921extern __u32 sysctl_rmem_max;
2922
20380731
ACM
2923extern __u32 sysctl_wmem_default;
2924extern __u32 sysctl_rmem_default;
20380731 2925
723783d0 2926#define SKB_FRAG_PAGE_ORDER get_order(32768)
ce27ec60
ED
2927DECLARE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key);
2928
a3dcaf17
ED
2929static inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto)
2930{
2931 /* Does this proto have per netns sysctl_wmem ? */
2932 if (proto->sysctl_wmem_offset)
02739545 2933 return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_wmem_offset));
a3dcaf17 2934
02739545 2935 return READ_ONCE(*proto->sysctl_wmem);
a3dcaf17
ED
2936}
2937
2938static inline int sk_get_rmem0(const struct sock *sk, const struct proto *proto)
2939{
2940 /* Does this proto have per netns sysctl_rmem ? */
2941 if (proto->sysctl_rmem_offset)
02739545 2942 return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_rmem_offset));
a3dcaf17 2943
02739545 2944 return READ_ONCE(*proto->sysctl_rmem);
a3dcaf17
ED
2945}
2946
c9f1f58d
ED
2947/* Default TCP Small queue budget is ~1 ms of data (1sec >> 10)
2948 * Some wifi drivers need to tweak it to get more chunks.
2949 * They can use this helper from their ndo_start_xmit()
2950 */
2951static inline void sk_pacing_shift_update(struct sock *sk, int val)
2952{
7c68fa2b 2953 if (!sk || !sk_fullsock(sk) || READ_ONCE(sk->sk_pacing_shift) == val)
c9f1f58d 2954 return;
7c68fa2b 2955 WRITE_ONCE(sk->sk_pacing_shift, val);
c9f1f58d
ED
2956}
2957
54dc3e33
DA
2958/* if a socket is bound to a device, check that the given device
2959 * index is either the same or that the socket is bound to an L3
2960 * master device and the given device index is also enslaved to
2961 * that L3 master
2962 */
2963static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
2964{
4c971d2f 2965 int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
54dc3e33
DA
2966 int mdif;
2967
4c971d2f 2968 if (!bound_dev_if || bound_dev_if == dif)
54dc3e33
DA
2969 return true;
2970
2971 mdif = l3mdev_master_ifindex_by_index(sock_net(sk), dif);
4c971d2f 2972 if (mdif && mdif == bound_dev_if)
54dc3e33
DA
2973 return true;
2974
2975 return false;
2976}
2977
43a825af
BT
2978void sock_def_readable(struct sock *sk);
2979
8ea204c2 2980int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk);
371087aa 2981void sock_set_timestamp(struct sock *sk, int optname, bool valbool);
d463126e
YL
2982int sock_set_timestamping(struct sock *sk, int optname,
2983 struct so_timestamping timestamping);
ced122d9 2984
783da70e 2985void sock_enable_timestamps(struct sock *sk);
df600f3b
JX
2986#if defined(CONFIG_CGROUP_BPF)
2987void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op);
2988#else
2989static inline void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op)
2990{
2991}
2992#endif
c433594c 2993void sock_no_linger(struct sock *sk);
ce3d9544 2994void sock_set_keepalive(struct sock *sk);
6e434967 2995void sock_set_priority(struct sock *sk, u32 priority);
26cfabf9 2996void sock_set_rcvbuf(struct sock *sk, int val);
84d1c617 2997void sock_set_mark(struct sock *sk, u32 val);
b58f0e8f 2998void sock_set_reuseaddr(struct sock *sk);
fe31a326 2999void sock_set_reuseport(struct sock *sk);
76ee0785 3000void sock_set_sndtimeo(struct sock *sk, s64 secs);
b58f0e8f 3001
c0425a42
CH
3002int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len);
3003
4c1e34c0
RP
3004int sock_get_timeout(long timeo, void *optval, bool old_timeval);
3005int sock_copy_user_timeval(struct __kernel_sock_timeval *tv,
3006 sockptr_t optval, int optlen, bool old_timeval);
3007
e1d001fa
BL
3008int sock_ioctl_inout(struct sock *sk, unsigned int cmd,
3009 void __user *arg, void *karg, size_t size);
3010int sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
7b50ecfc
CW
3011static inline bool sk_is_readable(struct sock *sk)
3012{
2660a544
ML
3013 const struct proto *prot = READ_ONCE(sk->sk_prot);
3014
3015 if (prot->sock_is_readable)
3016 return prot->sock_is_readable(sk);
3017
7b50ecfc
CW
3018 return false;
3019}
1da177e4 3020#endif /* _SOCK_H */