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