1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *	Definitions for the 'struct sk_buff' memory handlers.
4  *
5  *	Authors:
6  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
7  *		Florian La Roche, <rzsfl@rz.uni-sb.de>
8  */
9 
10 #ifndef _LINUX_SKBUFF_H
11 #define _LINUX_SKBUFF_H
12 
13 #include <linux/kernel.h>
14 #include <linux/compiler.h>
15 #include <linux/time.h>
16 #include <linux/bug.h>
17 #include <linux/bvec.h>
18 #include <linux/cache.h>
19 #include <linux/rbtree.h>
20 #include <linux/socket.h>
21 #include <linux/refcount.h>
22 
23 #include <linux/atomic.h>
24 #include <asm/types.h>
25 #include <linux/spinlock.h>
26 #include <net/checksum.h>
27 #include <linux/rcupdate.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/netdev_features.h>
30 #include <net/flow_dissector.h>
31 #include <linux/in6.h>
32 #include <linux/if_packet.h>
33 #include <linux/llist.h>
34 #include <net/flow.h>
35 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
36 #include <linux/netfilter/nf_conntrack_common.h>
37 #endif
38 #include <net/net_debug.h>
39 #include <net/dropreason-core.h>
40 #include <net/netmem.h>
41 #include <linux/android_vendor.h>
42 #include <linux/android_kabi.h>
43 
44 /**
45  * DOC: skb checksums
46  *
47  * The interface for checksum offload between the stack and networking drivers
48  * is as follows...
49  *
50  * IP checksum related features
51  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52  *
53  * Drivers advertise checksum offload capabilities in the features of a device.
54  * From the stack's point of view these are capabilities offered by the driver.
55  * A driver typically only advertises features that it is capable of offloading
56  * to its device.
57  *
58  * .. flat-table:: Checksum related device features
59  *   :widths: 1 10
60  *
61  *   * - %NETIF_F_HW_CSUM
62  *     - The driver (or its device) is able to compute one
63  *	 IP (one's complement) checksum for any combination
64  *	 of protocols or protocol layering. The checksum is
65  *	 computed and set in a packet per the CHECKSUM_PARTIAL
66  *	 interface (see below).
67  *
68  *   * - %NETIF_F_IP_CSUM
69  *     - Driver (device) is only able to checksum plain
70  *	 TCP or UDP packets over IPv4. These are specifically
71  *	 unencapsulated packets of the form IPv4|TCP or
72  *	 IPv4|UDP where the Protocol field in the IPv4 header
73  *	 is TCP or UDP. The IPv4 header may contain IP options.
74  *	 This feature cannot be set in features for a device
75  *	 with NETIF_F_HW_CSUM also set. This feature is being
76  *	 DEPRECATED (see below).
77  *
78  *   * - %NETIF_F_IPV6_CSUM
79  *     - Driver (device) is only able to checksum plain
80  *	 TCP or UDP packets over IPv6. These are specifically
81  *	 unencapsulated packets of the form IPv6|TCP or
82  *	 IPv6|UDP where the Next Header field in the IPv6
83  *	 header is either TCP or UDP. IPv6 extension headers
84  *	 are not supported with this feature. This feature
85  *	 cannot be set in features for a device with
86  *	 NETIF_F_HW_CSUM also set. This feature is being
87  *	 DEPRECATED (see below).
88  *
89  *   * - %NETIF_F_RXCSUM
90  *     - Driver (device) performs receive checksum offload.
91  *	 This flag is only used to disable the RX checksum
92  *	 feature for a device. The stack will accept receive
93  *	 checksum indication in packets received on a device
94  *	 regardless of whether NETIF_F_RXCSUM is set.
95  *
96  * Checksumming of received packets by device
97  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98  *
99  * Indication of checksum verification is set in &sk_buff.ip_summed.
100  * Possible values are:
101  *
102  * - %CHECKSUM_NONE
103  *
104  *   Device did not checksum this packet e.g. due to lack of capabilities.
105  *   The packet contains full (though not verified) checksum in packet but
106  *   not in skb->csum. Thus, skb->csum is undefined in this case.
107  *
108  * - %CHECKSUM_UNNECESSARY
109  *
110  *   The hardware you're dealing with doesn't calculate the full checksum
111  *   (as in %CHECKSUM_COMPLETE), but it does parse headers and verify checksums
112  *   for specific protocols. For such packets it will set %CHECKSUM_UNNECESSARY
113  *   if their checksums are okay. &sk_buff.csum is still undefined in this case
114  *   though. A driver or device must never modify the checksum field in the
115  *   packet even if checksum is verified.
116  *
117  *   %CHECKSUM_UNNECESSARY is applicable to following protocols:
118  *
119  *     - TCP: IPv6 and IPv4.
120  *     - UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a
121  *       zero UDP checksum for either IPv4 or IPv6, the networking stack
122  *       may perform further validation in this case.
123  *     - GRE: only if the checksum is present in the header.
124  *     - SCTP: indicates the CRC in SCTP header has been validated.
125  *     - FCOE: indicates the CRC in FC frame has been validated.
126  *
127  *   &sk_buff.csum_level indicates the number of consecutive checksums found in
128  *   the packet minus one that have been verified as %CHECKSUM_UNNECESSARY.
129  *   For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet
130  *   and a device is able to verify the checksums for UDP (possibly zero),
131  *   GRE (checksum flag is set) and TCP, &sk_buff.csum_level would be set to
132  *   two. If the device were only able to verify the UDP checksum and not
133  *   GRE, either because it doesn't support GRE checksum or because GRE
134  *   checksum is bad, skb->csum_level would be set to zero (TCP checksum is
135  *   not considered in this case).
136  *
137  * - %CHECKSUM_COMPLETE
138  *
139  *   This is the most generic way. The device supplied checksum of the _whole_
140  *   packet as seen by netif_rx() and fills in &sk_buff.csum. This means the
141  *   hardware doesn't need to parse L3/L4 headers to implement this.
142  *
143  *   Notes:
144  *
145  *   - Even if device supports only some protocols, but is able to produce
146  *     skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY.
147  *   - CHECKSUM_COMPLETE is not applicable to SCTP and FCoE protocols.
148  *
149  * - %CHECKSUM_PARTIAL
150  *
151  *   A checksum is set up to be offloaded to a device as described in the
152  *   output description for CHECKSUM_PARTIAL. This may occur on a packet
153  *   received directly from another Linux OS, e.g., a virtualized Linux kernel
154  *   on the same host, or it may be set in the input path in GRO or remote
155  *   checksum offload. For the purposes of checksum verification, the checksum
156  *   referred to by skb->csum_start + skb->csum_offset and any preceding
157  *   checksums in the packet are considered verified. Any checksums in the
158  *   packet that are after the checksum being offloaded are not considered to
159  *   be verified.
160  *
161  * Checksumming on transmit for non-GSO
162  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163  *
164  * The stack requests checksum offload in the &sk_buff.ip_summed for a packet.
165  * Values are:
166  *
167  * - %CHECKSUM_PARTIAL
168  *
169  *   The driver is required to checksum the packet as seen by hard_start_xmit()
170  *   from &sk_buff.csum_start up to the end, and to record/write the checksum at
171  *   offset &sk_buff.csum_start + &sk_buff.csum_offset.
172  *   A driver may verify that the
173  *   csum_start and csum_offset values are valid values given the length and
174  *   offset of the packet, but it should not attempt to validate that the
175  *   checksum refers to a legitimate transport layer checksum -- it is the
176  *   purview of the stack to validate that csum_start and csum_offset are set
177  *   correctly.
178  *
179  *   When the stack requests checksum offload for a packet, the driver MUST
180  *   ensure that the checksum is set correctly. A driver can either offload the
181  *   checksum calculation to the device, or call skb_checksum_help (in the case
182  *   that the device does not support offload for a particular checksum).
183  *
184  *   %NETIF_F_IP_CSUM and %NETIF_F_IPV6_CSUM are being deprecated in favor of
185  *   %NETIF_F_HW_CSUM. New devices should use %NETIF_F_HW_CSUM to indicate
186  *   checksum offload capability.
187  *   skb_csum_hwoffload_help() can be called to resolve %CHECKSUM_PARTIAL based
188  *   on network device checksumming capabilities: if a packet does not match
189  *   them, skb_checksum_help() or skb_crc32c_help() (depending on the value of
190  *   &sk_buff.csum_not_inet, see :ref:`crc`)
191  *   is called to resolve the checksum.
192  *
193  * - %CHECKSUM_NONE
194  *
195  *   The skb was already checksummed by the protocol, or a checksum is not
196  *   required.
197  *
198  * - %CHECKSUM_UNNECESSARY
199  *
200  *   This has the same meaning as CHECKSUM_NONE for checksum offload on
201  *   output.
202  *
203  * - %CHECKSUM_COMPLETE
204  *
205  *   Not used in checksum output. If a driver observes a packet with this value
206  *   set in skbuff, it should treat the packet as if %CHECKSUM_NONE were set.
207  *
208  * .. _crc:
209  *
210  * Non-IP checksum (CRC) offloads
211  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212  *
213  * .. flat-table::
214  *   :widths: 1 10
215  *
216  *   * - %NETIF_F_SCTP_CRC
217  *     - This feature indicates that a device is capable of
218  *	 offloading the SCTP CRC in a packet. To perform this offload the stack
219  *	 will set csum_start and csum_offset accordingly, set ip_summed to
220  *	 %CHECKSUM_PARTIAL and set csum_not_inet to 1, to provide an indication
221  *	 in the skbuff that the %CHECKSUM_PARTIAL refers to CRC32c.
222  *	 A driver that supports both IP checksum offload and SCTP CRC32c offload
223  *	 must verify which offload is configured for a packet by testing the
224  *	 value of &sk_buff.csum_not_inet; skb_crc32c_csum_help() is provided to
225  *	 resolve %CHECKSUM_PARTIAL on skbs where csum_not_inet is set to 1.
226  *
227  *   * - %NETIF_F_FCOE_CRC
228  *     - This feature indicates that a device is capable of offloading the FCOE
229  *	 CRC in a packet. To perform this offload the stack will set ip_summed
230  *	 to %CHECKSUM_PARTIAL and set csum_start and csum_offset
231  *	 accordingly. Note that there is no indication in the skbuff that the
232  *	 %CHECKSUM_PARTIAL refers to an FCOE checksum, so a driver that supports
233  *	 both IP checksum offload and FCOE CRC offload must verify which offload
234  *	 is configured for a packet, presumably by inspecting packet headers.
235  *
236  * Checksumming on output with GSO
237  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
238  *
239  * In the case of a GSO packet (skb_is_gso() is true), checksum offload
240  * is implied by the SKB_GSO_* flags in gso_type. Most obviously, if the
241  * gso_type is %SKB_GSO_TCPV4 or %SKB_GSO_TCPV6, TCP checksum offload as
242  * part of the GSO operation is implied. If a checksum is being offloaded
243  * with GSO then ip_summed is %CHECKSUM_PARTIAL, and both csum_start and
244  * csum_offset are set to refer to the outermost checksum being offloaded
245  * (two offloaded checksums are possible with UDP encapsulation).
246  */
247 
248 /* Don't change this without changing skb_csum_unnecessary! */
249 #define CHECKSUM_NONE		0
250 #define CHECKSUM_UNNECESSARY	1
251 #define CHECKSUM_COMPLETE	2
252 #define CHECKSUM_PARTIAL	3
253 
254 /* Maximum value in skb->csum_level */
255 #define SKB_MAX_CSUM_LEVEL	3
256 
257 #define SKB_DATA_ALIGN(X)	ALIGN(X, SMP_CACHE_BYTES)
258 #define SKB_WITH_OVERHEAD(X)	\
259 	((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
260 
261 /* For X bytes available in skb->head, what is the minimal
262  * allocation needed, knowing struct skb_shared_info needs
263  * to be aligned.
264  */
265 #define SKB_HEAD_ALIGN(X) (SKB_DATA_ALIGN(X) + \
266 	SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
267 
268 #define SKB_MAX_ORDER(X, ORDER) \
269 	SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
270 #define SKB_MAX_HEAD(X)		(SKB_MAX_ORDER((X), 0))
271 #define SKB_MAX_ALLOC		(SKB_MAX_ORDER(0, 2))
272 
273 /* return minimum truesize of one skb containing X bytes of data */
274 #define SKB_TRUESIZE(X) ((X) +						\
275 			 SKB_DATA_ALIGN(sizeof(struct sk_buff)) +	\
276 			 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
277 
278 struct ahash_request;
279 struct net_device;
280 struct scatterlist;
281 struct pipe_inode_info;
282 struct iov_iter;
283 struct napi_struct;
284 struct bpf_prog;
285 union bpf_attr;
286 struct skb_ext;
287 struct ts_config;
288 
289 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
290 struct nf_bridge_info {
291 	enum {
292 		BRNF_PROTO_UNCHANGED,
293 		BRNF_PROTO_8021Q,
294 		BRNF_PROTO_PPPOE
295 	} orig_proto:8;
296 	u8			pkt_otherhost:1;
297 	u8			in_prerouting:1;
298 	u8			bridged_dnat:1;
299 	u8			sabotage_in_done:1;
300 	__u16			frag_max_size;
301 	int			physinif;
302 
303 	/* always valid & non-NULL from FORWARD on, for physdev match */
304 	struct net_device	*physoutdev;
305 	union {
306 		/* prerouting: detect dnat in orig/reply direction */
307 		__be32          ipv4_daddr;
308 		struct in6_addr ipv6_daddr;
309 
310 		/* after prerouting + nat detected: store original source
311 		 * mac since neigh resolution overwrites it, only used while
312 		 * skb is out in neigh layer.
313 		 */
314 		char neigh_header[8];
315 	};
316 };
317 #endif
318 
319 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
320 /* Chain in tc_skb_ext will be used to share the tc chain with
321  * ovs recirc_id. It will be set to the current chain by tc
322  * and read by ovs to recirc_id.
323  */
324 struct tc_skb_ext {
325 	union {
326 		u64 act_miss_cookie;
327 		__u32 chain;
328 	};
329 	__u16 mru;
330 	__u16 zone;
331 	u8 post_ct:1;
332 	u8 post_ct_snat:1;
333 	u8 post_ct_dnat:1;
334 	u8 act_miss:1; /* Set if act_miss_cookie is used */
335 	u8 l2_miss:1; /* Set by bridge upon FDB or MDB miss */
336 };
337 #endif
338 
339 struct sk_buff_head {
340 	/* These two members must be first to match sk_buff. */
341 	struct_group_tagged(sk_buff_list, list,
342 		struct sk_buff	*next;
343 		struct sk_buff	*prev;
344 	);
345 
346 	__u32		qlen;
347 	spinlock_t	lock;
348 };
349 
350 struct sk_buff;
351 
352 #ifndef CONFIG_MAX_SKB_FRAGS
353 # define CONFIG_MAX_SKB_FRAGS 17
354 #endif
355 
356 #define MAX_SKB_FRAGS CONFIG_MAX_SKB_FRAGS
357 
358 /* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to
359  * segment using its current segmentation instead.
360  */
361 #define GSO_BY_FRAGS	0xFFFF
362 
363 typedef struct skb_frag {
364 	netmem_ref netmem;
365 	unsigned int len;
366 	unsigned int offset;
367 } skb_frag_t;
368 
369 /**
370  * skb_frag_size() - Returns the size of a skb fragment
371  * @frag: skb fragment
372  */
skb_frag_size(const skb_frag_t * frag)373 static inline unsigned int skb_frag_size(const skb_frag_t *frag)
374 {
375 	return frag->len;
376 }
377 
378 /**
379  * skb_frag_size_set() - Sets the size of a skb fragment
380  * @frag: skb fragment
381  * @size: size of fragment
382  */
skb_frag_size_set(skb_frag_t * frag,unsigned int size)383 static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
384 {
385 	frag->len = size;
386 }
387 
388 /**
389  * skb_frag_size_add() - Increments the size of a skb fragment by @delta
390  * @frag: skb fragment
391  * @delta: value to add
392  */
skb_frag_size_add(skb_frag_t * frag,int delta)393 static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
394 {
395 	frag->len += delta;
396 }
397 
398 /**
399  * skb_frag_size_sub() - Decrements the size of a skb fragment by @delta
400  * @frag: skb fragment
401  * @delta: value to subtract
402  */
skb_frag_size_sub(skb_frag_t * frag,int delta)403 static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
404 {
405 	frag->len -= delta;
406 }
407 
408 /**
409  * skb_frag_must_loop - Test if %p is a high memory page
410  * @p: fragment's page
411  */
skb_frag_must_loop(struct page * p)412 static inline bool skb_frag_must_loop(struct page *p)
413 {
414 #if defined(CONFIG_HIGHMEM)
415 	if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) || PageHighMem(p))
416 		return true;
417 #endif
418 	return false;
419 }
420 
421 /**
422  *	skb_frag_foreach_page - loop over pages in a fragment
423  *
424  *	@f:		skb frag to operate on
425  *	@f_off:		offset from start of f->netmem
426  *	@f_len:		length from f_off to loop over
427  *	@p:		(temp var) current page
428  *	@p_off:		(temp var) offset from start of current page,
429  *	                           non-zero only on first page.
430  *	@p_len:		(temp var) length in current page,
431  *				   < PAGE_SIZE only on first and last page.
432  *	@copied:	(temp var) length so far, excluding current p_len.
433  *
434  *	A fragment can hold a compound page, in which case per-page
435  *	operations, notably kmap_atomic, must be called for each
436  *	regular page.
437  */
438 #define skb_frag_foreach_page(f, f_off, f_len, p, p_off, p_len, copied)	\
439 	for (p = skb_frag_page(f) + ((f_off) >> PAGE_SHIFT),		\
440 	     p_off = (f_off) & (PAGE_SIZE - 1),				\
441 	     p_len = skb_frag_must_loop(p) ?				\
442 	     min_t(u32, f_len, PAGE_SIZE - p_off) : f_len,		\
443 	     copied = 0;						\
444 	     copied < f_len;						\
445 	     copied += p_len, p++, p_off = 0,				\
446 	     p_len = min_t(u32, f_len - copied, PAGE_SIZE))		\
447 
448 /**
449  * struct skb_shared_hwtstamps - hardware time stamps
450  * @hwtstamp:		hardware time stamp transformed into duration
451  *			since arbitrary point in time
452  * @netdev_data:	address/cookie of network device driver used as
453  *			reference to actual hardware time stamp
454  *
455  * Software time stamps generated by ktime_get_real() are stored in
456  * skb->tstamp.
457  *
458  * hwtstamps can only be compared against other hwtstamps from
459  * the same device.
460  *
461  * This structure is attached to packets as part of the
462  * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
463  */
464 struct skb_shared_hwtstamps {
465 	union {
466 		ktime_t	hwtstamp;
467 		void *netdev_data;
468 	};
469 };
470 
471 /* Definitions for tx_flags in struct skb_shared_info */
472 enum {
473 	/* generate hardware time stamp */
474 	SKBTX_HW_TSTAMP = 1 << 0,
475 
476 	/* generate software time stamp when queueing packet to NIC */
477 	SKBTX_SW_TSTAMP = 1 << 1,
478 
479 	/* device driver is going to provide hardware time stamp */
480 	SKBTX_IN_PROGRESS = 1 << 2,
481 
482 	/* generate hardware time stamp based on cycles if supported */
483 	SKBTX_HW_TSTAMP_USE_CYCLES = 1 << 3,
484 
485 	/* generate wifi status information (where possible) */
486 	SKBTX_WIFI_STATUS = 1 << 4,
487 
488 	/* determine hardware time stamp based on time or cycles */
489 	SKBTX_HW_TSTAMP_NETDEV = 1 << 5,
490 
491 	/* generate software time stamp when entering packet scheduling */
492 	SKBTX_SCHED_TSTAMP = 1 << 6,
493 };
494 
495 #define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
496 				 SKBTX_SCHED_TSTAMP)
497 #define SKBTX_ANY_TSTAMP	(SKBTX_HW_TSTAMP | \
498 				 SKBTX_HW_TSTAMP_USE_CYCLES | \
499 				 SKBTX_ANY_SW_TSTAMP)
500 
501 /* Definitions for flags in struct skb_shared_info */
502 enum {
503 	/* use zcopy routines */
504 	SKBFL_ZEROCOPY_ENABLE = BIT(0),
505 
506 	/* This indicates at least one fragment might be overwritten
507 	 * (as in vmsplice(), sendfile() ...)
508 	 * If we need to compute a TX checksum, we'll need to copy
509 	 * all frags to avoid possible bad checksum
510 	 */
511 	SKBFL_SHARED_FRAG = BIT(1),
512 
513 	/* segment contains only zerocopy data and should not be
514 	 * charged to the kernel memory.
515 	 */
516 	SKBFL_PURE_ZEROCOPY = BIT(2),
517 
518 	SKBFL_DONT_ORPHAN = BIT(3),
519 
520 	/* page references are managed by the ubuf_info, so it's safe to
521 	 * use frags only up until ubuf_info is released
522 	 */
523 	SKBFL_MANAGED_FRAG_REFS = BIT(4),
524 };
525 
526 #define SKBFL_ZEROCOPY_FRAG	(SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG)
527 #define SKBFL_ALL_ZEROCOPY	(SKBFL_ZEROCOPY_FRAG | SKBFL_PURE_ZEROCOPY | \
528 				 SKBFL_DONT_ORPHAN | SKBFL_MANAGED_FRAG_REFS)
529 
530 struct ubuf_info_ops {
531 	void (*complete)(struct sk_buff *, struct ubuf_info *,
532 			 bool zerocopy_success);
533 	/* has to be compatible with skb_zcopy_set() */
534 	int (*link_skb)(struct sk_buff *skb, struct ubuf_info *uarg);
535 };
536 
537 /*
538  * The callback notifies userspace to release buffers when skb DMA is done in
539  * lower device, the skb last reference should be 0 when calling this.
540  * The zerocopy_success argument is true if zero copy transmit occurred,
541  * false on data copy or out of memory error caused by data copy attempt.
542  * The ctx field is used to track device context.
543  * The desc field is used to track userspace buffer index.
544  */
545 struct ubuf_info {
546 	const struct ubuf_info_ops *ops;
547 	refcount_t refcnt;
548 	u8 flags;
549 };
550 
551 struct ubuf_info_msgzc {
552 	struct ubuf_info ubuf;
553 
554 	union {
555 		struct {
556 			unsigned long desc;
557 			void *ctx;
558 		};
559 		struct {
560 			u32 id;
561 			u16 len;
562 			u16 zerocopy:1;
563 			u32 bytelen;
564 		};
565 	};
566 
567 	struct mmpin {
568 		struct user_struct *user;
569 		unsigned int num_pg;
570 	} mmp;
571 };
572 
573 #define skb_uarg(SKB)	((struct ubuf_info *)(skb_shinfo(SKB)->destructor_arg))
574 #define uarg_to_msgzc(ubuf_ptr)	container_of((ubuf_ptr), struct ubuf_info_msgzc, \
575 					     ubuf)
576 
577 int mm_account_pinned_pages(struct mmpin *mmp, size_t size);
578 void mm_unaccount_pinned_pages(struct mmpin *mmp);
579 
580 /* Preserve some data across TX submission and completion.
581  *
582  * Note, this state is stored in the driver. Extending the layout
583  * might need some special care.
584  */
585 struct xsk_tx_metadata_compl {
586 	__u64 *tx_timestamp;
587 };
588 
589 /* This data is invariant across clones and lives at
590  * the end of the header data, ie. at skb->end.
591  */
592 struct skb_shared_info {
593 	__u8		flags;
594 	__u8		meta_len;
595 	__u8		nr_frags;
596 	__u8		tx_flags;
597 	unsigned short	gso_size;
598 	/* Warning: this field is not always filled in (UFO)! */
599 	unsigned short	gso_segs;
600 	struct sk_buff	*frag_list;
601 	union {
602 		struct skb_shared_hwtstamps hwtstamps;
603 		struct xsk_tx_metadata_compl xsk_meta;
604 	};
605 	unsigned int	gso_type;
606 	u32		tskey;
607 
608 	/*
609 	 * Warning : all fields before dataref are cleared in __alloc_skb()
610 	 */
611 	atomic_t	dataref;
612 	unsigned int	xdp_frags_size;
613 
614 	/* Intermediate layers must ensure that destructor_arg
615 	 * remains valid until skb destructor */
616 	void *		destructor_arg;
617 
618 	ANDROID_KABI_RESERVE(1);
619 	ANDROID_KABI_RESERVE(2);
620 	ANDROID_OEM_DATA_ARRAY(1, 3);
621 
622 	/* must be last field, see pskb_expand_head() */
623 	skb_frag_t	frags[MAX_SKB_FRAGS];
624 };
625 
626 /**
627  * DOC: dataref and headerless skbs
628  *
629  * Transport layers send out clones of payload skbs they hold for
630  * retransmissions. To allow lower layers of the stack to prepend their headers
631  * we split &skb_shared_info.dataref into two halves.
632  * The lower 16 bits count the overall number of references.
633  * The higher 16 bits indicate how many of the references are payload-only.
634  * skb_header_cloned() checks if skb is allowed to add / write the headers.
635  *
636  * The creator of the skb (e.g. TCP) marks its skb as &sk_buff.nohdr
637  * (via __skb_header_release()). Any clone created from marked skb will get
638  * &sk_buff.hdr_len populated with the available headroom.
639  * If there's the only clone in existence it's able to modify the headroom
640  * at will. The sequence of calls inside the transport layer is::
641  *
642  *  <alloc skb>
643  *  skb_reserve()
644  *  __skb_header_release()
645  *  skb_clone()
646  *  // send the clone down the stack
647  *
648  * This is not a very generic construct and it depends on the transport layers
649  * doing the right thing. In practice there's usually only one payload-only skb.
650  * Having multiple payload-only skbs with different lengths of hdr_len is not
651  * possible. The payload-only skbs should never leave their owner.
652  */
653 #define SKB_DATAREF_SHIFT 16
654 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
655 
656 
657 enum {
658 	SKB_FCLONE_UNAVAILABLE,	/* skb has no fclone (from head_cache) */
659 	SKB_FCLONE_ORIG,	/* orig skb (from fclone_cache) */
660 	SKB_FCLONE_CLONE,	/* companion fclone skb (from fclone_cache) */
661 };
662 
663 enum {
664 	SKB_GSO_TCPV4 = 1 << 0,
665 
666 	/* This indicates the skb is from an untrusted source. */
667 	SKB_GSO_DODGY = 1 << 1,
668 
669 	/* This indicates the tcp segment has CWR set. */
670 	SKB_GSO_TCP_ECN = 1 << 2,
671 
672 	SKB_GSO_TCP_FIXEDID = 1 << 3,
673 
674 	SKB_GSO_TCPV6 = 1 << 4,
675 
676 	SKB_GSO_FCOE = 1 << 5,
677 
678 	SKB_GSO_GRE = 1 << 6,
679 
680 	SKB_GSO_GRE_CSUM = 1 << 7,
681 
682 	SKB_GSO_IPXIP4 = 1 << 8,
683 
684 	SKB_GSO_IPXIP6 = 1 << 9,
685 
686 	SKB_GSO_UDP_TUNNEL = 1 << 10,
687 
688 	SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11,
689 
690 	SKB_GSO_PARTIAL = 1 << 12,
691 
692 	SKB_GSO_TUNNEL_REMCSUM = 1 << 13,
693 
694 	SKB_GSO_SCTP = 1 << 14,
695 
696 	SKB_GSO_ESP = 1 << 15,
697 
698 	SKB_GSO_UDP = 1 << 16,
699 
700 	SKB_GSO_UDP_L4 = 1 << 17,
701 
702 	SKB_GSO_FRAGLIST = 1 << 18,
703 };
704 
705 #if BITS_PER_LONG > 32
706 #define NET_SKBUFF_DATA_USES_OFFSET 1
707 #endif
708 
709 #ifdef NET_SKBUFF_DATA_USES_OFFSET
710 typedef unsigned int sk_buff_data_t;
711 #else
712 typedef unsigned char *sk_buff_data_t;
713 #endif
714 
715 enum skb_tstamp_type {
716 	SKB_CLOCK_REALTIME,
717 	SKB_CLOCK_MONOTONIC,
718 	SKB_CLOCK_TAI,
719 	__SKB_CLOCK_MAX = SKB_CLOCK_TAI,
720 };
721 
722 /**
723  * DOC: Basic sk_buff geometry
724  *
725  * struct sk_buff itself is a metadata structure and does not hold any packet
726  * data. All the data is held in associated buffers.
727  *
728  * &sk_buff.head points to the main "head" buffer. The head buffer is divided
729  * into two parts:
730  *
731  *  - data buffer, containing headers and sometimes payload;
732  *    this is the part of the skb operated on by the common helpers
733  *    such as skb_put() or skb_pull();
734  *  - shared info (struct skb_shared_info) which holds an array of pointers
735  *    to read-only data in the (page, offset, length) format.
736  *
737  * Optionally &skb_shared_info.frag_list may point to another skb.
738  *
739  * Basic diagram may look like this::
740  *
741  *                                  ---------------
742  *                                 | sk_buff       |
743  *                                  ---------------
744  *     ,---------------------------  + head
745  *    /          ,-----------------  + data
746  *   /          /      ,-----------  + tail
747  *  |          |      |            , + end
748  *  |          |      |           |
749  *  v          v      v           v
750  *   -----------------------------------------------
751  *  | headroom | data |  tailroom | skb_shared_info |
752  *   -----------------------------------------------
753  *                                 + [page frag]
754  *                                 + [page frag]
755  *                                 + [page frag]
756  *                                 + [page frag]       ---------
757  *                                 + frag_list    --> | sk_buff |
758  *                                                     ---------
759  *
760  */
761 
762 /**
763  *	struct sk_buff - socket buffer
764  *	@next: Next buffer in list
765  *	@prev: Previous buffer in list
766  *	@tstamp: Time we arrived/left
767  *	@skb_mstamp_ns: (aka @tstamp) earliest departure time; start point
768  *		for retransmit timer
769  *	@rbnode: RB tree node, alternative to next/prev for netem/tcp
770  *	@list: queue head
771  *	@ll_node: anchor in an llist (eg socket defer_list)
772  *	@sk: Socket we are owned by
773  *	@dev: Device we arrived on/are leaving by
774  *	@dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL
775  *	@cb: Control buffer. Free for use by every layer. Put private vars here
776  *	@_skb_refdst: destination entry (with norefcount bit)
777  *	@len: Length of actual data
778  *	@data_len: Data length
779  *	@mac_len: Length of link layer header
780  *	@hdr_len: writable header length of cloned skb
781  *	@csum: Checksum (must include start/offset pair)
782  *	@csum_start: Offset from skb->head where checksumming should start
783  *	@csum_offset: Offset from csum_start where checksum should be stored
784  *	@priority: Packet queueing priority
785  *	@ignore_df: allow local fragmentation
786  *	@cloned: Head may be cloned (check refcnt to be sure)
787  *	@ip_summed: Driver fed us an IP checksum
788  *	@nohdr: Payload reference only, must not modify header
789  *	@pkt_type: Packet class
790  *	@fclone: skbuff clone status
791  *	@ipvs_property: skbuff is owned by ipvs
792  *	@inner_protocol_type: whether the inner protocol is
793  *		ENCAP_TYPE_ETHER or ENCAP_TYPE_IPPROTO
794  *	@remcsum_offload: remote checksum offload is enabled
795  *	@offload_fwd_mark: Packet was L2-forwarded in hardware
796  *	@offload_l3_fwd_mark: Packet was L3-forwarded in hardware
797  *	@tc_skip_classify: do not classify packet. set by IFB device
798  *	@tc_at_ingress: used within tc_classify to distinguish in/egress
799  *	@redirected: packet was redirected by packet classifier
800  *	@from_ingress: packet was redirected from the ingress path
801  *	@nf_skip_egress: packet shall skip nf egress - see netfilter_netdev.h
802  *	@peeked: this packet has been seen already, so stats have been
803  *		done for it, don't do them again
804  *	@nf_trace: netfilter packet trace flag
805  *	@protocol: Packet protocol from driver
806  *	@destructor: Destruct function
807  *	@tcp_tsorted_anchor: list structure for TCP (tp->tsorted_sent_queue)
808  *	@_sk_redir: socket redirection information for skmsg
809  *	@_nfct: Associated connection, if any (with nfctinfo bits)
810  *	@skb_iif: ifindex of device we arrived on
811  *	@tc_index: Traffic control index
812  *	@hash: the packet hash
813  *	@queue_mapping: Queue mapping for multiqueue devices
814  *	@head_frag: skb was allocated from page fragments,
815  *		not allocated by kmalloc() or vmalloc().
816  *	@pfmemalloc: skbuff was allocated from PFMEMALLOC reserves
817  *	@pp_recycle: mark the packet for recycling instead of freeing (implies
818  *		page_pool support on driver)
819  *	@active_extensions: active extensions (skb_ext_id types)
820  *	@ndisc_nodetype: router type (from link layer)
821  *	@ooo_okay: allow the mapping of a socket to a queue to be changed
822  *	@l4_hash: indicate hash is a canonical 4-tuple hash over transport
823  *		ports.
824  *	@sw_hash: indicates hash was computed in software stack
825  *	@wifi_acked_valid: wifi_acked was set
826  *	@wifi_acked: whether frame was acked on wifi or not
827  *	@no_fcs:  Request NIC to treat last 4 bytes as Ethernet FCS
828  *	@encapsulation: indicates the inner headers in the skbuff are valid
829  *	@encap_hdr_csum: software checksum is needed
830  *	@csum_valid: checksum is already valid
831  *	@csum_not_inet: use CRC32c to resolve CHECKSUM_PARTIAL
832  *	@csum_complete_sw: checksum was completed by software
833  *	@csum_level: indicates the number of consecutive checksums found in
834  *		the packet minus one that have been verified as
835  *		CHECKSUM_UNNECESSARY (max 3)
836  *	@unreadable: indicates that at least 1 of the fragments in this skb is
837  *		unreadable.
838  *	@dst_pending_confirm: need to confirm neighbour
839  *	@decrypted: Decrypted SKB
840  *	@slow_gro: state present at GRO time, slower prepare step required
841  *	@tstamp_type: When set, skb->tstamp has the
842  *		delivery_time clock base of skb->tstamp.
843  *	@napi_id: id of the NAPI struct this skb came from
844  *	@sender_cpu: (aka @napi_id) source CPU in XPS
845  *	@alloc_cpu: CPU which did the skb allocation.
846  *	@secmark: security marking
847  *	@mark: Generic packet mark
848  *	@reserved_tailroom: (aka @mark) number of bytes of free space available
849  *		at the tail of an sk_buff
850  *	@vlan_all: vlan fields (proto & tci)
851  *	@vlan_proto: vlan encapsulation protocol
852  *	@vlan_tci: vlan tag control information
853  *	@inner_protocol: Protocol (encapsulation)
854  *	@inner_ipproto: (aka @inner_protocol) stores ipproto when
855  *		skb->inner_protocol_type == ENCAP_TYPE_IPPROTO;
856  *	@inner_transport_header: Inner transport layer header (encapsulation)
857  *	@inner_network_header: Network layer header (encapsulation)
858  *	@inner_mac_header: Link layer header (encapsulation)
859  *	@transport_header: Transport layer header
860  *	@network_header: Network layer header
861  *	@mac_header: Link layer header
862  *	@kcov_handle: KCOV remote handle for remote coverage collection
863  *	@tail: Tail pointer
864  *	@end: End pointer
865  *	@head: Head of buffer
866  *	@data: Data head pointer
867  *	@truesize: Buffer size
868  *	@users: User count - see {datagram,tcp}.c
869  *	@extensions: allocated extensions, valid if active_extensions is nonzero
870  */
871 
872 struct sk_buff {
873 	union {
874 		struct {
875 			/* These two members must be first to match sk_buff_head. */
876 			struct sk_buff		*next;
877 			struct sk_buff		*prev;
878 
879 			union {
880 				struct net_device	*dev;
881 				/* Some protocols might use this space to store information,
882 				 * while device pointer would be NULL.
883 				 * UDP receive path is one user.
884 				 */
885 				unsigned long		dev_scratch;
886 			};
887 		};
888 		struct rb_node		rbnode; /* used in netem, ip4 defrag, and tcp stack */
889 		struct list_head	list;
890 		struct llist_node	ll_node;
891 	};
892 
893 	struct sock		*sk;
894 
895 	union {
896 		ktime_t		tstamp;
897 		u64		skb_mstamp_ns; /* earliest departure time */
898 	};
899 	/*
900 	 * This is the control buffer. It is free to use for every
901 	 * layer. Please put your private variables there. If you
902 	 * want to keep them across layers you have to do a skb_clone()
903 	 * first. This is owned by whoever has the skb queued ATM.
904 	 */
905 	char			cb[48] __aligned(8);
906 
907 	union {
908 		struct {
909 			unsigned long	_skb_refdst;
910 			void		(*destructor)(struct sk_buff *skb);
911 		};
912 		struct list_head	tcp_tsorted_anchor;
913 #ifdef CONFIG_NET_SOCK_MSG
914 		unsigned long		_sk_redir;
915 #endif
916 	};
917 
918 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
919 	unsigned long		 _nfct;
920 #endif
921 	unsigned int		len,
922 				data_len;
923 	__u16			mac_len,
924 				hdr_len;
925 
926 	/* Following fields are _not_ copied in __copy_skb_header()
927 	 * Note that queue_mapping is here mostly to fill a hole.
928 	 */
929 	__u16			queue_mapping;
930 
931 /* if you move cloned around you also must adapt those constants */
932 #ifdef __BIG_ENDIAN_BITFIELD
933 #define CLONED_MASK	(1 << 7)
934 #else
935 #define CLONED_MASK	1
936 #endif
937 #define CLONED_OFFSET		offsetof(struct sk_buff, __cloned_offset)
938 
939 	/* private: */
940 	__u8			__cloned_offset[0];
941 	/* public: */
942 	__u8			cloned:1,
943 				nohdr:1,
944 				fclone:2,
945 				peeked:1,
946 				head_frag:1,
947 				pfmemalloc:1,
948 				pp_recycle:1; /* page_pool recycle indicator */
949 #ifdef CONFIG_SKB_EXTENSIONS
950 	__u8			active_extensions;
951 #endif
952 
953 	/* Fields enclosed in headers group are copied
954 	 * using a single memcpy() in __copy_skb_header()
955 	 */
956 	struct_group(headers,
957 
958 	/* private: */
959 	__u8			__pkt_type_offset[0];
960 	/* public: */
961 	__u8			pkt_type:3; /* see PKT_TYPE_MAX */
962 	__u8			ignore_df:1;
963 	__u8			dst_pending_confirm:1;
964 	__u8			ip_summed:2;
965 	__u8			ooo_okay:1;
966 
967 	/* private: */
968 	__u8			__mono_tc_offset[0];
969 	/* public: */
970 	__u8			tstamp_type:2;	/* See skb_tstamp_type */
971 #ifdef CONFIG_NET_XGRESS
972 	__u8			tc_at_ingress:1;	/* See TC_AT_INGRESS_MASK */
973 	__u8			tc_skip_classify:1;
974 #endif
975 	__u8			remcsum_offload:1;
976 	__u8			csum_complete_sw:1;
977 	__u8			csum_level:2;
978 	__u8			inner_protocol_type:1;
979 
980 	__u8			l4_hash:1;
981 	__u8			sw_hash:1;
982 #ifdef CONFIG_WIRELESS
983 	__u8			wifi_acked_valid:1;
984 	__u8			wifi_acked:1;
985 #endif
986 	__u8			no_fcs:1;
987 	/* Indicates the inner headers are valid in the skbuff. */
988 	__u8			encapsulation:1;
989 	__u8			encap_hdr_csum:1;
990 	__u8			csum_valid:1;
991 #ifdef CONFIG_IPV6_NDISC_NODETYPE
992 	__u8			ndisc_nodetype:2;
993 #endif
994 
995 #if IS_ENABLED(CONFIG_IP_VS)
996 	__u8			ipvs_property:1;
997 #endif
998 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || IS_ENABLED(CONFIG_NF_TABLES)
999 	__u8			nf_trace:1;
1000 #endif
1001 #ifdef CONFIG_NET_SWITCHDEV
1002 	__u8			offload_fwd_mark:1;
1003 	__u8			offload_l3_fwd_mark:1;
1004 #endif
1005 	__u8			redirected:1;
1006 #ifdef CONFIG_NET_REDIRECT
1007 	__u8			from_ingress:1;
1008 #endif
1009 #ifdef CONFIG_NETFILTER_SKIP_EGRESS
1010 	__u8			nf_skip_egress:1;
1011 #endif
1012 #ifdef CONFIG_SKB_DECRYPTED
1013 	__u8			decrypted:1;
1014 #endif
1015 	__u8			slow_gro:1;
1016 #if IS_ENABLED(CONFIG_IP_SCTP)
1017 	__u8			csum_not_inet:1;
1018 #endif
1019 	__u8			unreadable:1;
1020 #if defined(CONFIG_NET_SCHED) || defined(CONFIG_NET_XGRESS)
1021 	__u16			tc_index;	/* traffic control index */
1022 #endif
1023 
1024 	u16			alloc_cpu;
1025 
1026 	union {
1027 		__wsum		csum;
1028 		struct {
1029 			__u16	csum_start;
1030 			__u16	csum_offset;
1031 		};
1032 	};
1033 	__u32			priority;
1034 	int			skb_iif;
1035 	__u32			hash;
1036 	union {
1037 		u32		vlan_all;
1038 		struct {
1039 			__be16	vlan_proto;
1040 			__u16	vlan_tci;
1041 		};
1042 	};
1043 #if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS)
1044 	union {
1045 		unsigned int	napi_id;
1046 		unsigned int	sender_cpu;
1047 	};
1048 #endif
1049 #ifdef CONFIG_NETWORK_SECMARK
1050 	__u32		secmark;
1051 #endif
1052 
1053 	union {
1054 		__u32		mark;
1055 		__u32		reserved_tailroom;
1056 	};
1057 
1058 	union {
1059 		__be16		inner_protocol;
1060 		__u8		inner_ipproto;
1061 	};
1062 
1063 	__u16			inner_transport_header;
1064 	__u16			inner_network_header;
1065 	__u16			inner_mac_header;
1066 
1067 	__be16			protocol;
1068 	__u16			transport_header;
1069 	__u16			network_header;
1070 	__u16			mac_header;
1071 
1072 #ifdef CONFIG_KCOV
1073 	u64			kcov_handle;
1074 #endif
1075 
1076 	ANDROID_KABI_RESERVE(1);
1077 	ANDROID_KABI_RESERVE(2);
1078 
1079 	); /* end headers group */
1080 
1081 	/* These elements must be at the end, see alloc_skb() for details.  */
1082 	sk_buff_data_t		tail;
1083 	sk_buff_data_t		end;
1084 	unsigned char		*head,
1085 				*data;
1086 	unsigned int		truesize;
1087 	refcount_t		users;
1088 
1089 #ifdef CONFIG_SKB_EXTENSIONS
1090 	/* only usable after checking ->active_extensions != 0 */
1091 	struct skb_ext		*extensions;
1092 #endif
1093 };
1094 
1095 /* if you move pkt_type around you also must adapt those constants */
1096 #ifdef __BIG_ENDIAN_BITFIELD
1097 #define PKT_TYPE_MAX	(7 << 5)
1098 #else
1099 #define PKT_TYPE_MAX	7
1100 #endif
1101 #define PKT_TYPE_OFFSET		offsetof(struct sk_buff, __pkt_type_offset)
1102 
1103 /* if you move tc_at_ingress or tstamp_type
1104  * around, you also must adapt these constants.
1105  */
1106 #ifdef __BIG_ENDIAN_BITFIELD
1107 #define SKB_TSTAMP_TYPE_MASK		(3 << 6)
1108 #define SKB_TSTAMP_TYPE_RSHIFT		(6)
1109 #define TC_AT_INGRESS_MASK		(1 << 5)
1110 #else
1111 #define SKB_TSTAMP_TYPE_MASK		(3)
1112 #define TC_AT_INGRESS_MASK		(1 << 2)
1113 #endif
1114 #define SKB_BF_MONO_TC_OFFSET		offsetof(struct sk_buff, __mono_tc_offset)
1115 
1116 #ifdef __KERNEL__
1117 /*
1118  *	Handling routines are only of interest to the kernel
1119  */
1120 
1121 #define SKB_ALLOC_FCLONE	0x01
1122 #define SKB_ALLOC_RX		0x02
1123 #define SKB_ALLOC_NAPI		0x04
1124 
1125 /**
1126  * skb_pfmemalloc - Test if the skb was allocated from PFMEMALLOC reserves
1127  * @skb: buffer
1128  */
skb_pfmemalloc(const struct sk_buff * skb)1129 static inline bool skb_pfmemalloc(const struct sk_buff *skb)
1130 {
1131 	return unlikely(skb->pfmemalloc);
1132 }
1133 
1134 /*
1135  * skb might have a dst pointer attached, refcounted or not.
1136  * _skb_refdst low order bit is set if refcount was _not_ taken
1137  */
1138 #define SKB_DST_NOREF	1UL
1139 #define SKB_DST_PTRMASK	~(SKB_DST_NOREF)
1140 
1141 /**
1142  * skb_dst - returns skb dst_entry
1143  * @skb: buffer
1144  *
1145  * Returns skb dst_entry, regardless of reference taken or not.
1146  */
skb_dst(const struct sk_buff * skb)1147 static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
1148 {
1149 	/* If refdst was not refcounted, check we still are in a
1150 	 * rcu_read_lock section
1151 	 */
1152 	WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
1153 		!rcu_read_lock_held() &&
1154 		!rcu_read_lock_bh_held());
1155 	return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
1156 }
1157 
1158 /**
1159  * skb_dst_set - sets skb dst
1160  * @skb: buffer
1161  * @dst: dst entry
1162  *
1163  * Sets skb dst, assuming a reference was taken on dst and should
1164  * be released by skb_dst_drop()
1165  */
skb_dst_set(struct sk_buff * skb,struct dst_entry * dst)1166 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
1167 {
1168 	skb->slow_gro |= !!dst;
1169 	skb->_skb_refdst = (unsigned long)dst;
1170 }
1171 
1172 /**
1173  * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
1174  * @skb: buffer
1175  * @dst: dst entry
1176  *
1177  * Sets skb dst, assuming a reference was not taken on dst.
1178  * If dst entry is cached, we do not take reference and dst_release
1179  * will be avoided by refdst_drop. If dst entry is not cached, we take
1180  * reference, so that last dst_release can destroy the dst immediately.
1181  */
skb_dst_set_noref(struct sk_buff * skb,struct dst_entry * dst)1182 static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
1183 {
1184 	WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
1185 	skb->slow_gro |= !!dst;
1186 	skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF;
1187 }
1188 
1189 /**
1190  * skb_dst_is_noref - Test if skb dst isn't refcounted
1191  * @skb: buffer
1192  */
skb_dst_is_noref(const struct sk_buff * skb)1193 static inline bool skb_dst_is_noref(const struct sk_buff *skb)
1194 {
1195 	return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
1196 }
1197 
1198 /* For mangling skb->pkt_type from user space side from applications
1199  * such as nft, tc, etc, we only allow a conservative subset of
1200  * possible pkt_types to be set.
1201 */
skb_pkt_type_ok(u32 ptype)1202 static inline bool skb_pkt_type_ok(u32 ptype)
1203 {
1204 	return ptype <= PACKET_OTHERHOST;
1205 }
1206 
1207 /**
1208  * skb_napi_id - Returns the skb's NAPI id
1209  * @skb: buffer
1210  */
skb_napi_id(const struct sk_buff * skb)1211 static inline unsigned int skb_napi_id(const struct sk_buff *skb)
1212 {
1213 #ifdef CONFIG_NET_RX_BUSY_POLL
1214 	return skb->napi_id;
1215 #else
1216 	return 0;
1217 #endif
1218 }
1219 
skb_wifi_acked_valid(const struct sk_buff * skb)1220 static inline bool skb_wifi_acked_valid(const struct sk_buff *skb)
1221 {
1222 #ifdef CONFIG_WIRELESS
1223 	return skb->wifi_acked_valid;
1224 #else
1225 	return 0;
1226 #endif
1227 }
1228 
1229 /**
1230  * skb_unref - decrement the skb's reference count
1231  * @skb: buffer
1232  *
1233  * Returns true if we can free the skb.
1234  */
skb_unref(struct sk_buff * skb)1235 static inline bool skb_unref(struct sk_buff *skb)
1236 {
1237 	if (unlikely(!skb))
1238 		return false;
1239 	if (!IS_ENABLED(CONFIG_DEBUG_NET) && likely(refcount_read(&skb->users) == 1))
1240 		smp_rmb();
1241 	else if (likely(!refcount_dec_and_test(&skb->users)))
1242 		return false;
1243 
1244 	return true;
1245 }
1246 
skb_data_unref(const struct sk_buff * skb,struct skb_shared_info * shinfo)1247 static inline bool skb_data_unref(const struct sk_buff *skb,
1248 				  struct skb_shared_info *shinfo)
1249 {
1250 	int bias;
1251 
1252 	if (!skb->cloned)
1253 		return true;
1254 
1255 	bias = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
1256 
1257 	if (atomic_read(&shinfo->dataref) == bias)
1258 		smp_rmb();
1259 	else if (atomic_sub_return(bias, &shinfo->dataref))
1260 		return false;
1261 
1262 	return true;
1263 }
1264 
1265 void __fix_address sk_skb_reason_drop(struct sock *sk, struct sk_buff *skb,
1266 				      enum skb_drop_reason reason);
1267 
1268 static inline void
kfree_skb_reason(struct sk_buff * skb,enum skb_drop_reason reason)1269 kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
1270 {
1271 	sk_skb_reason_drop(NULL, skb, reason);
1272 }
1273 
1274 /**
1275  *	kfree_skb - free an sk_buff with 'NOT_SPECIFIED' reason
1276  *	@skb: buffer to free
1277  */
kfree_skb(struct sk_buff * skb)1278 static inline void kfree_skb(struct sk_buff *skb)
1279 {
1280 	kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
1281 }
1282 
1283 void skb_release_head_state(struct sk_buff *skb);
1284 void kfree_skb_list_reason(struct sk_buff *segs,
1285 			   enum skb_drop_reason reason);
1286 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt);
1287 void skb_tx_error(struct sk_buff *skb);
1288 
kfree_skb_list(struct sk_buff * segs)1289 static inline void kfree_skb_list(struct sk_buff *segs)
1290 {
1291 	kfree_skb_list_reason(segs, SKB_DROP_REASON_NOT_SPECIFIED);
1292 }
1293 
1294 #ifdef CONFIG_TRACEPOINTS
1295 void consume_skb(struct sk_buff *skb);
1296 #else
consume_skb(struct sk_buff * skb)1297 static inline void consume_skb(struct sk_buff *skb)
1298 {
1299 	return kfree_skb(skb);
1300 }
1301 #endif
1302 
1303 void __consume_stateless_skb(struct sk_buff *skb);
1304 void  __kfree_skb(struct sk_buff *skb);
1305 
1306 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
1307 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
1308 		      bool *fragstolen, int *delta_truesize);
1309 
1310 struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
1311 			    int node);
1312 struct sk_buff *__build_skb(void *data, unsigned int frag_size);
1313 struct sk_buff *build_skb(void *data, unsigned int frag_size);
1314 struct sk_buff *build_skb_around(struct sk_buff *skb,
1315 				 void *data, unsigned int frag_size);
1316 void skb_attempt_defer_free(struct sk_buff *skb);
1317 
1318 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
1319 struct sk_buff *slab_build_skb(void *data);
1320 
1321 /**
1322  * alloc_skb - allocate a network buffer
1323  * @size: size to allocate
1324  * @priority: allocation mask
1325  *
1326  * This function is a convenient wrapper around __alloc_skb().
1327  */
alloc_skb(unsigned int size,gfp_t priority)1328 static inline struct sk_buff *alloc_skb(unsigned int size,
1329 					gfp_t priority)
1330 {
1331 	return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
1332 }
1333 
1334 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
1335 				     unsigned long data_len,
1336 				     int max_page_order,
1337 				     int *errcode,
1338 				     gfp_t gfp_mask);
1339 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first);
1340 
1341 /* Layout of fast clones : [skb1][skb2][fclone_ref] */
1342 struct sk_buff_fclones {
1343 	struct sk_buff	skb1;
1344 
1345 	struct sk_buff	skb2;
1346 
1347 	refcount_t	fclone_ref;
1348 };
1349 
1350 /**
1351  *	skb_fclone_busy - check if fclone is busy
1352  *	@sk: socket
1353  *	@skb: buffer
1354  *
1355  * Returns true if skb is a fast clone, and its clone is not freed.
1356  * Some drivers call skb_orphan() in their ndo_start_xmit(),
1357  * so we also check that didn't happen.
1358  */
skb_fclone_busy(const struct sock * sk,const struct sk_buff * skb)1359 static inline bool skb_fclone_busy(const struct sock *sk,
1360 				   const struct sk_buff *skb)
1361 {
1362 	const struct sk_buff_fclones *fclones;
1363 
1364 	fclones = container_of(skb, struct sk_buff_fclones, skb1);
1365 
1366 	return skb->fclone == SKB_FCLONE_ORIG &&
1367 	       refcount_read(&fclones->fclone_ref) > 1 &&
1368 	       READ_ONCE(fclones->skb2.sk) == sk;
1369 }
1370 
1371 /**
1372  * alloc_skb_fclone - allocate a network buffer from fclone cache
1373  * @size: size to allocate
1374  * @priority: allocation mask
1375  *
1376  * This function is a convenient wrapper around __alloc_skb().
1377  */
alloc_skb_fclone(unsigned int size,gfp_t priority)1378 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
1379 					       gfp_t priority)
1380 {
1381 	return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
1382 }
1383 
1384 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
1385 void skb_headers_offset_update(struct sk_buff *skb, int off);
1386 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
1387 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
1388 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old);
1389 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
1390 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1391 				   gfp_t gfp_mask, bool fclone);
__pskb_copy(struct sk_buff * skb,int headroom,gfp_t gfp_mask)1392 static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom,
1393 					  gfp_t gfp_mask)
1394 {
1395 	return __pskb_copy_fclone(skb, headroom, gfp_mask, false);
1396 }
1397 
1398 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask);
1399 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
1400 				     unsigned int headroom);
1401 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom);
1402 struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom,
1403 				int newtailroom, gfp_t priority);
1404 int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
1405 				     int offset, int len);
1406 int __must_check skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg,
1407 			      int offset, int len);
1408 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer);
1409 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error);
1410 
1411 /**
1412  *	skb_pad			-	zero pad the tail of an skb
1413  *	@skb: buffer to pad
1414  *	@pad: space to pad
1415  *
1416  *	Ensure that a buffer is followed by a padding area that is zero
1417  *	filled. Used by network drivers which may DMA or transfer data
1418  *	beyond the buffer end onto the wire.
1419  *
1420  *	May return error in out of memory cases. The skb is freed on error.
1421  */
skb_pad(struct sk_buff * skb,int pad)1422 static inline int skb_pad(struct sk_buff *skb, int pad)
1423 {
1424 	return __skb_pad(skb, pad, true);
1425 }
1426 #define dev_kfree_skb(a)	consume_skb(a)
1427 
1428 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
1429 			 int offset, size_t size, size_t max_frags);
1430 
1431 struct skb_seq_state {
1432 	__u32		lower_offset;
1433 	__u32		upper_offset;
1434 	__u32		frag_idx;
1435 	__u32		stepped_offset;
1436 	struct sk_buff	*root_skb;
1437 	struct sk_buff	*cur_skb;
1438 	__u8		*frag_data;
1439 	__u32		frag_off;
1440 };
1441 
1442 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1443 			  unsigned int to, struct skb_seq_state *st);
1444 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1445 			  struct skb_seq_state *st);
1446 void skb_abort_seq_read(struct skb_seq_state *st);
1447 int skb_copy_seq_read(struct skb_seq_state *st, int offset, void *to, int len);
1448 
1449 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1450 			   unsigned int to, struct ts_config *config);
1451 
1452 /*
1453  * Packet hash types specify the type of hash in skb_set_hash.
1454  *
1455  * Hash types refer to the protocol layer addresses which are used to
1456  * construct a packet's hash. The hashes are used to differentiate or identify
1457  * flows of the protocol layer for the hash type. Hash types are either
1458  * layer-2 (L2), layer-3 (L3), or layer-4 (L4).
1459  *
1460  * Properties of hashes:
1461  *
1462  * 1) Two packets in different flows have different hash values
1463  * 2) Two packets in the same flow should have the same hash value
1464  *
1465  * A hash at a higher layer is considered to be more specific. A driver should
1466  * set the most specific hash possible.
1467  *
1468  * A driver cannot indicate a more specific hash than the layer at which a hash
1469  * was computed. For instance an L3 hash cannot be set as an L4 hash.
1470  *
1471  * A driver may indicate a hash level which is less specific than the
1472  * actual layer the hash was computed on. For instance, a hash computed
1473  * at L4 may be considered an L3 hash. This should only be done if the
1474  * driver can't unambiguously determine that the HW computed the hash at
1475  * the higher layer. Note that the "should" in the second property above
1476  * permits this.
1477  */
1478 enum pkt_hash_types {
1479 	PKT_HASH_TYPE_NONE,	/* Undefined type */
1480 	PKT_HASH_TYPE_L2,	/* Input: src_MAC, dest_MAC */
1481 	PKT_HASH_TYPE_L3,	/* Input: src_IP, dst_IP */
1482 	PKT_HASH_TYPE_L4,	/* Input: src_IP, dst_IP, src_port, dst_port */
1483 };
1484 
skb_clear_hash(struct sk_buff * skb)1485 static inline void skb_clear_hash(struct sk_buff *skb)
1486 {
1487 	skb->hash = 0;
1488 	skb->sw_hash = 0;
1489 	skb->l4_hash = 0;
1490 }
1491 
skb_clear_hash_if_not_l4(struct sk_buff * skb)1492 static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
1493 {
1494 	if (!skb->l4_hash)
1495 		skb_clear_hash(skb);
1496 }
1497 
1498 static inline void
__skb_set_hash(struct sk_buff * skb,__u32 hash,bool is_sw,bool is_l4)1499 __skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4)
1500 {
1501 	skb->l4_hash = is_l4;
1502 	skb->sw_hash = is_sw;
1503 	skb->hash = hash;
1504 }
1505 
1506 static inline void
skb_set_hash(struct sk_buff * skb,__u32 hash,enum pkt_hash_types type)1507 skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
1508 {
1509 	/* Used by drivers to set hash from HW */
1510 	__skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4);
1511 }
1512 
1513 static inline void
__skb_set_sw_hash(struct sk_buff * skb,__u32 hash,bool is_l4)1514 __skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4)
1515 {
1516 	__skb_set_hash(skb, hash, true, is_l4);
1517 }
1518 
1519 u32 __skb_get_hash_symmetric_net(const struct net *net, const struct sk_buff *skb);
1520 
__skb_get_hash_symmetric(const struct sk_buff * skb)1521 static inline u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1522 {
1523 	return __skb_get_hash_symmetric_net(NULL, skb);
1524 }
1525 
1526 void __skb_get_hash_net(const struct net *net, struct sk_buff *skb);
1527 u32 skb_get_poff(const struct sk_buff *skb);
1528 u32 __skb_get_poff(const struct sk_buff *skb, const void *data,
1529 		   const struct flow_keys_basic *keys, int hlen);
1530 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
1531 			    const void *data, int hlen_proto);
1532 
skb_flow_get_ports(const struct sk_buff * skb,int thoff,u8 ip_proto)1533 static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
1534 					int thoff, u8 ip_proto)
1535 {
1536 	return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
1537 }
1538 
1539 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
1540 			     const struct flow_dissector_key *key,
1541 			     unsigned int key_count);
1542 
1543 struct bpf_flow_dissector;
1544 u32 bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
1545 		     __be16 proto, int nhoff, int hlen, unsigned int flags);
1546 
1547 bool __skb_flow_dissect(const struct net *net,
1548 			const struct sk_buff *skb,
1549 			struct flow_dissector *flow_dissector,
1550 			void *target_container, const void *data,
1551 			__be16 proto, int nhoff, int hlen, unsigned int flags);
1552 
skb_flow_dissect(const struct sk_buff * skb,struct flow_dissector * flow_dissector,void * target_container,unsigned int flags)1553 static inline bool skb_flow_dissect(const struct sk_buff *skb,
1554 				    struct flow_dissector *flow_dissector,
1555 				    void *target_container, unsigned int flags)
1556 {
1557 	return __skb_flow_dissect(NULL, skb, flow_dissector,
1558 				  target_container, NULL, 0, 0, 0, flags);
1559 }
1560 
skb_flow_dissect_flow_keys(const struct sk_buff * skb,struct flow_keys * flow,unsigned int flags)1561 static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
1562 					      struct flow_keys *flow,
1563 					      unsigned int flags)
1564 {
1565 	memset(flow, 0, sizeof(*flow));
1566 	return __skb_flow_dissect(NULL, skb, &flow_keys_dissector,
1567 				  flow, NULL, 0, 0, 0, flags);
1568 }
1569 
1570 static inline bool
skb_flow_dissect_flow_keys_basic(const struct net * net,const struct sk_buff * skb,struct flow_keys_basic * flow,const void * data,__be16 proto,int nhoff,int hlen,unsigned int flags)1571 skb_flow_dissect_flow_keys_basic(const struct net *net,
1572 				 const struct sk_buff *skb,
1573 				 struct flow_keys_basic *flow,
1574 				 const void *data, __be16 proto,
1575 				 int nhoff, int hlen, unsigned int flags)
1576 {
1577 	memset(flow, 0, sizeof(*flow));
1578 	return __skb_flow_dissect(net, skb, &flow_keys_basic_dissector, flow,
1579 				  data, proto, nhoff, hlen, flags);
1580 }
1581 
1582 void skb_flow_dissect_meta(const struct sk_buff *skb,
1583 			   struct flow_dissector *flow_dissector,
1584 			   void *target_container);
1585 
1586 /* Gets a skb connection tracking info, ctinfo map should be a
1587  * map of mapsize to translate enum ip_conntrack_info states
1588  * to user states.
1589  */
1590 void
1591 skb_flow_dissect_ct(const struct sk_buff *skb,
1592 		    struct flow_dissector *flow_dissector,
1593 		    void *target_container,
1594 		    u16 *ctinfo_map, size_t mapsize,
1595 		    bool post_ct, u16 zone);
1596 void
1597 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
1598 			     struct flow_dissector *flow_dissector,
1599 			     void *target_container);
1600 
1601 void skb_flow_dissect_hash(const struct sk_buff *skb,
1602 			   struct flow_dissector *flow_dissector,
1603 			   void *target_container);
1604 
skb_get_hash_net(const struct net * net,struct sk_buff * skb)1605 static inline __u32 skb_get_hash_net(const struct net *net, struct sk_buff *skb)
1606 {
1607 	if (!skb->l4_hash && !skb->sw_hash)
1608 		__skb_get_hash_net(net, skb);
1609 
1610 	return skb->hash;
1611 }
1612 
skb_get_hash(struct sk_buff * skb)1613 static inline __u32 skb_get_hash(struct sk_buff *skb)
1614 {
1615 	if (!skb->l4_hash && !skb->sw_hash)
1616 		__skb_get_hash_net(NULL, skb);
1617 
1618 	return skb->hash;
1619 }
1620 
skb_get_hash_flowi6(struct sk_buff * skb,const struct flowi6 * fl6)1621 static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
1622 {
1623 	if (!skb->l4_hash && !skb->sw_hash) {
1624 		struct flow_keys keys;
1625 		__u32 hash = __get_hash_from_flowi6(fl6, &keys);
1626 
1627 		__skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1628 	}
1629 
1630 	return skb->hash;
1631 }
1632 
1633 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1634 			   const siphash_key_t *perturb);
1635 
skb_get_hash_raw(const struct sk_buff * skb)1636 static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
1637 {
1638 	return skb->hash;
1639 }
1640 
skb_copy_hash(struct sk_buff * to,const struct sk_buff * from)1641 static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
1642 {
1643 	to->hash = from->hash;
1644 	to->sw_hash = from->sw_hash;
1645 	to->l4_hash = from->l4_hash;
1646 };
1647 
skb_cmp_decrypted(const struct sk_buff * skb1,const struct sk_buff * skb2)1648 static inline int skb_cmp_decrypted(const struct sk_buff *skb1,
1649 				    const struct sk_buff *skb2)
1650 {
1651 #ifdef CONFIG_SKB_DECRYPTED
1652 	return skb2->decrypted - skb1->decrypted;
1653 #else
1654 	return 0;
1655 #endif
1656 }
1657 
skb_is_decrypted(const struct sk_buff * skb)1658 static inline bool skb_is_decrypted(const struct sk_buff *skb)
1659 {
1660 #ifdef CONFIG_SKB_DECRYPTED
1661 	return skb->decrypted;
1662 #else
1663 	return false;
1664 #endif
1665 }
1666 
skb_copy_decrypted(struct sk_buff * to,const struct sk_buff * from)1667 static inline void skb_copy_decrypted(struct sk_buff *to,
1668 				      const struct sk_buff *from)
1669 {
1670 #ifdef CONFIG_SKB_DECRYPTED
1671 	to->decrypted = from->decrypted;
1672 #endif
1673 }
1674 
1675 #ifdef NET_SKBUFF_DATA_USES_OFFSET
skb_end_pointer(const struct sk_buff * skb)1676 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
1677 {
1678 	return skb->head + skb->end;
1679 }
1680 
skb_end_offset(const struct sk_buff * skb)1681 static inline unsigned int skb_end_offset(const struct sk_buff *skb)
1682 {
1683 	return skb->end;
1684 }
1685 
skb_set_end_offset(struct sk_buff * skb,unsigned int offset)1686 static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset)
1687 {
1688 	skb->end = offset;
1689 }
1690 #else
skb_end_pointer(const struct sk_buff * skb)1691 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
1692 {
1693 	return skb->end;
1694 }
1695 
skb_end_offset(const struct sk_buff * skb)1696 static inline unsigned int skb_end_offset(const struct sk_buff *skb)
1697 {
1698 	return skb->end - skb->head;
1699 }
1700 
skb_set_end_offset(struct sk_buff * skb,unsigned int offset)1701 static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset)
1702 {
1703 	skb->end = skb->head + offset;
1704 }
1705 #endif
1706 
1707 extern const struct ubuf_info_ops msg_zerocopy_ubuf_ops;
1708 
1709 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1710 				       struct ubuf_info *uarg);
1711 
1712 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref);
1713 
1714 int __zerocopy_sg_from_iter(struct msghdr *msg, struct sock *sk,
1715 			    struct sk_buff *skb, struct iov_iter *from,
1716 			    size_t length);
1717 
1718 int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
1719 				struct iov_iter *from, size_t length);
1720 
skb_zerocopy_iter_dgram(struct sk_buff * skb,struct msghdr * msg,int len)1721 static inline int skb_zerocopy_iter_dgram(struct sk_buff *skb,
1722 					  struct msghdr *msg, int len)
1723 {
1724 	return __zerocopy_sg_from_iter(msg, skb->sk, skb, &msg->msg_iter, len);
1725 }
1726 
1727 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1728 			     struct msghdr *msg, int len,
1729 			     struct ubuf_info *uarg);
1730 
1731 /* Internal */
1732 #define skb_shinfo(SKB)	((struct skb_shared_info *)(skb_end_pointer(SKB)))
1733 
skb_hwtstamps(struct sk_buff * skb)1734 static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
1735 {
1736 	return &skb_shinfo(skb)->hwtstamps;
1737 }
1738 
skb_zcopy(struct sk_buff * skb)1739 static inline struct ubuf_info *skb_zcopy(struct sk_buff *skb)
1740 {
1741 	bool is_zcopy = skb && skb_shinfo(skb)->flags & SKBFL_ZEROCOPY_ENABLE;
1742 
1743 	return is_zcopy ? skb_uarg(skb) : NULL;
1744 }
1745 
skb_zcopy_pure(const struct sk_buff * skb)1746 static inline bool skb_zcopy_pure(const struct sk_buff *skb)
1747 {
1748 	return skb_shinfo(skb)->flags & SKBFL_PURE_ZEROCOPY;
1749 }
1750 
skb_zcopy_managed(const struct sk_buff * skb)1751 static inline bool skb_zcopy_managed(const struct sk_buff *skb)
1752 {
1753 	return skb_shinfo(skb)->flags & SKBFL_MANAGED_FRAG_REFS;
1754 }
1755 
skb_pure_zcopy_same(const struct sk_buff * skb1,const struct sk_buff * skb2)1756 static inline bool skb_pure_zcopy_same(const struct sk_buff *skb1,
1757 				       const struct sk_buff *skb2)
1758 {
1759 	return skb_zcopy_pure(skb1) == skb_zcopy_pure(skb2);
1760 }
1761 
net_zcopy_get(struct ubuf_info * uarg)1762 static inline void net_zcopy_get(struct ubuf_info *uarg)
1763 {
1764 	refcount_inc(&uarg->refcnt);
1765 }
1766 
skb_zcopy_init(struct sk_buff * skb,struct ubuf_info * uarg)1767 static inline void skb_zcopy_init(struct sk_buff *skb, struct ubuf_info *uarg)
1768 {
1769 	skb_shinfo(skb)->destructor_arg = uarg;
1770 	skb_shinfo(skb)->flags |= uarg->flags;
1771 }
1772 
skb_zcopy_set(struct sk_buff * skb,struct ubuf_info * uarg,bool * have_ref)1773 static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg,
1774 				 bool *have_ref)
1775 {
1776 	if (skb && uarg && !skb_zcopy(skb)) {
1777 		if (unlikely(have_ref && *have_ref))
1778 			*have_ref = false;
1779 		else
1780 			net_zcopy_get(uarg);
1781 		skb_zcopy_init(skb, uarg);
1782 	}
1783 }
1784 
skb_zcopy_set_nouarg(struct sk_buff * skb,void * val)1785 static inline void skb_zcopy_set_nouarg(struct sk_buff *skb, void *val)
1786 {
1787 	skb_shinfo(skb)->destructor_arg = (void *)((uintptr_t) val | 0x1UL);
1788 	skb_shinfo(skb)->flags |= SKBFL_ZEROCOPY_FRAG;
1789 }
1790 
skb_zcopy_is_nouarg(struct sk_buff * skb)1791 static inline bool skb_zcopy_is_nouarg(struct sk_buff *skb)
1792 {
1793 	return (uintptr_t) skb_shinfo(skb)->destructor_arg & 0x1UL;
1794 }
1795 
skb_zcopy_get_nouarg(struct sk_buff * skb)1796 static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb)
1797 {
1798 	return (void *)((uintptr_t) skb_shinfo(skb)->destructor_arg & ~0x1UL);
1799 }
1800 
net_zcopy_put(struct ubuf_info * uarg)1801 static inline void net_zcopy_put(struct ubuf_info *uarg)
1802 {
1803 	if (uarg)
1804 		uarg->ops->complete(NULL, uarg, true);
1805 }
1806 
net_zcopy_put_abort(struct ubuf_info * uarg,bool have_uref)1807 static inline void net_zcopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1808 {
1809 	if (uarg) {
1810 		if (uarg->ops == &msg_zerocopy_ubuf_ops)
1811 			msg_zerocopy_put_abort(uarg, have_uref);
1812 		else if (have_uref)
1813 			net_zcopy_put(uarg);
1814 	}
1815 }
1816 
1817 /* Release a reference on a zerocopy structure */
skb_zcopy_clear(struct sk_buff * skb,bool zerocopy_success)1818 static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success)
1819 {
1820 	struct ubuf_info *uarg = skb_zcopy(skb);
1821 
1822 	if (uarg) {
1823 		if (!skb_zcopy_is_nouarg(skb))
1824 			uarg->ops->complete(skb, uarg, zerocopy_success);
1825 
1826 		skb_shinfo(skb)->flags &= ~SKBFL_ALL_ZEROCOPY;
1827 	}
1828 }
1829 
1830 void __skb_zcopy_downgrade_managed(struct sk_buff *skb);
1831 
skb_zcopy_downgrade_managed(struct sk_buff * skb)1832 static inline void skb_zcopy_downgrade_managed(struct sk_buff *skb)
1833 {
1834 	if (unlikely(skb_zcopy_managed(skb)))
1835 		__skb_zcopy_downgrade_managed(skb);
1836 }
1837 
1838 /* Return true if frags in this skb are readable by the host. */
skb_frags_readable(const struct sk_buff * skb)1839 static inline bool skb_frags_readable(const struct sk_buff *skb)
1840 {
1841 	return !skb->unreadable;
1842 }
1843 
skb_mark_not_on_list(struct sk_buff * skb)1844 static inline void skb_mark_not_on_list(struct sk_buff *skb)
1845 {
1846 	skb->next = NULL;
1847 }
1848 
skb_poison_list(struct sk_buff * skb)1849 static inline void skb_poison_list(struct sk_buff *skb)
1850 {
1851 #ifdef CONFIG_DEBUG_NET
1852 	skb->next = SKB_LIST_POISON_NEXT;
1853 #endif
1854 }
1855 
1856 /* Iterate through singly-linked GSO fragments of an skb. */
1857 #define skb_list_walk_safe(first, skb, next_skb)                               \
1858 	for ((skb) = (first), (next_skb) = (skb) ? (skb)->next : NULL; (skb);  \
1859 	     (skb) = (next_skb), (next_skb) = (skb) ? (skb)->next : NULL)
1860 
skb_list_del_init(struct sk_buff * skb)1861 static inline void skb_list_del_init(struct sk_buff *skb)
1862 {
1863 	__list_del_entry(&skb->list);
1864 	skb_mark_not_on_list(skb);
1865 }
1866 
1867 /**
1868  *	skb_queue_empty - check if a queue is empty
1869  *	@list: queue head
1870  *
1871  *	Returns true if the queue is empty, false otherwise.
1872  */
skb_queue_empty(const struct sk_buff_head * list)1873 static inline int skb_queue_empty(const struct sk_buff_head *list)
1874 {
1875 	return list->next == (const struct sk_buff *) list;
1876 }
1877 
1878 /**
1879  *	skb_queue_empty_lockless - check if a queue is empty
1880  *	@list: queue head
1881  *
1882  *	Returns true if the queue is empty, false otherwise.
1883  *	This variant can be used in lockless contexts.
1884  */
skb_queue_empty_lockless(const struct sk_buff_head * list)1885 static inline bool skb_queue_empty_lockless(const struct sk_buff_head *list)
1886 {
1887 	return READ_ONCE(list->next) == (const struct sk_buff *) list;
1888 }
1889 
1890 
1891 /**
1892  *	skb_queue_is_last - check if skb is the last entry in the queue
1893  *	@list: queue head
1894  *	@skb: buffer
1895  *
1896  *	Returns true if @skb is the last buffer on the list.
1897  */
skb_queue_is_last(const struct sk_buff_head * list,const struct sk_buff * skb)1898 static inline bool skb_queue_is_last(const struct sk_buff_head *list,
1899 				     const struct sk_buff *skb)
1900 {
1901 	return skb->next == (const struct sk_buff *) list;
1902 }
1903 
1904 /**
1905  *	skb_queue_is_first - check if skb is the first entry in the queue
1906  *	@list: queue head
1907  *	@skb: buffer
1908  *
1909  *	Returns true if @skb is the first buffer on the list.
1910  */
skb_queue_is_first(const struct sk_buff_head * list,const struct sk_buff * skb)1911 static inline bool skb_queue_is_first(const struct sk_buff_head *list,
1912 				      const struct sk_buff *skb)
1913 {
1914 	return skb->prev == (const struct sk_buff *) list;
1915 }
1916 
1917 /**
1918  *	skb_queue_next - return the next packet in the queue
1919  *	@list: queue head
1920  *	@skb: current buffer
1921  *
1922  *	Return the next packet in @list after @skb.  It is only valid to
1923  *	call this if skb_queue_is_last() evaluates to false.
1924  */
skb_queue_next(const struct sk_buff_head * list,const struct sk_buff * skb)1925 static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
1926 					     const struct sk_buff *skb)
1927 {
1928 	/* This BUG_ON may seem severe, but if we just return then we
1929 	 * are going to dereference garbage.
1930 	 */
1931 	BUG_ON(skb_queue_is_last(list, skb));
1932 	return skb->next;
1933 }
1934 
1935 /**
1936  *	skb_queue_prev - return the prev packet in the queue
1937  *	@list: queue head
1938  *	@skb: current buffer
1939  *
1940  *	Return the prev packet in @list before @skb.  It is only valid to
1941  *	call this if skb_queue_is_first() evaluates to false.
1942  */
skb_queue_prev(const struct sk_buff_head * list,const struct sk_buff * skb)1943 static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
1944 					     const struct sk_buff *skb)
1945 {
1946 	/* This BUG_ON may seem severe, but if we just return then we
1947 	 * are going to dereference garbage.
1948 	 */
1949 	BUG_ON(skb_queue_is_first(list, skb));
1950 	return skb->prev;
1951 }
1952 
1953 /**
1954  *	skb_get - reference buffer
1955  *	@skb: buffer to reference
1956  *
1957  *	Makes another reference to a socket buffer and returns a pointer
1958  *	to the buffer.
1959  */
skb_get(struct sk_buff * skb)1960 static inline struct sk_buff *skb_get(struct sk_buff *skb)
1961 {
1962 	refcount_inc(&skb->users);
1963 	return skb;
1964 }
1965 
1966 /*
1967  * If users == 1, we are the only owner and can avoid redundant atomic changes.
1968  */
1969 
1970 /**
1971  *	skb_cloned - is the buffer a clone
1972  *	@skb: buffer to check
1973  *
1974  *	Returns true if the buffer was generated with skb_clone() and is
1975  *	one of multiple shared copies of the buffer. Cloned buffers are
1976  *	shared data so must not be written to under normal circumstances.
1977  */
skb_cloned(const struct sk_buff * skb)1978 static inline int skb_cloned(const struct sk_buff *skb)
1979 {
1980 	return skb->cloned &&
1981 	       (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
1982 }
1983 
skb_unclone(struct sk_buff * skb,gfp_t pri)1984 static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
1985 {
1986 	might_sleep_if(gfpflags_allow_blocking(pri));
1987 
1988 	if (skb_cloned(skb))
1989 		return pskb_expand_head(skb, 0, 0, pri);
1990 
1991 	return 0;
1992 }
1993 
1994 /* This variant of skb_unclone() makes sure skb->truesize
1995  * and skb_end_offset() are not changed, whenever a new skb->head is needed.
1996  *
1997  * Indeed there is no guarantee that ksize(kmalloc(X)) == ksize(kmalloc(X))
1998  * when various debugging features are in place.
1999  */
2000 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri);
skb_unclone_keeptruesize(struct sk_buff * skb,gfp_t pri)2001 static inline int skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
2002 {
2003 	might_sleep_if(gfpflags_allow_blocking(pri));
2004 
2005 	if (skb_cloned(skb))
2006 		return __skb_unclone_keeptruesize(skb, pri);
2007 	return 0;
2008 }
2009 
2010 /**
2011  *	skb_header_cloned - is the header a clone
2012  *	@skb: buffer to check
2013  *
2014  *	Returns true if modifying the header part of the buffer requires
2015  *	the data to be copied.
2016  */
skb_header_cloned(const struct sk_buff * skb)2017 static inline int skb_header_cloned(const struct sk_buff *skb)
2018 {
2019 	int dataref;
2020 
2021 	if (!skb->cloned)
2022 		return 0;
2023 
2024 	dataref = atomic_read(&skb_shinfo(skb)->dataref);
2025 	dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
2026 	return dataref != 1;
2027 }
2028 
skb_header_unclone(struct sk_buff * skb,gfp_t pri)2029 static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri)
2030 {
2031 	might_sleep_if(gfpflags_allow_blocking(pri));
2032 
2033 	if (skb_header_cloned(skb))
2034 		return pskb_expand_head(skb, 0, 0, pri);
2035 
2036 	return 0;
2037 }
2038 
2039 /**
2040  * __skb_header_release() - allow clones to use the headroom
2041  * @skb: buffer to operate on
2042  *
2043  * See "DOC: dataref and headerless skbs".
2044  */
__skb_header_release(struct sk_buff * skb)2045 static inline void __skb_header_release(struct sk_buff *skb)
2046 {
2047 	skb->nohdr = 1;
2048 	atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT));
2049 }
2050 
2051 
2052 /**
2053  *	skb_shared - is the buffer shared
2054  *	@skb: buffer to check
2055  *
2056  *	Returns true if more than one person has a reference to this
2057  *	buffer.
2058  */
skb_shared(const struct sk_buff * skb)2059 static inline int skb_shared(const struct sk_buff *skb)
2060 {
2061 	return refcount_read(&skb->users) != 1;
2062 }
2063 
2064 /**
2065  *	skb_share_check - check if buffer is shared and if so clone it
2066  *	@skb: buffer to check
2067  *	@pri: priority for memory allocation
2068  *
2069  *	If the buffer is shared the buffer is cloned and the old copy
2070  *	drops a reference. A new clone with a single reference is returned.
2071  *	If the buffer is not shared the original buffer is returned. When
2072  *	being called from interrupt status or with spinlocks held pri must
2073  *	be GFP_ATOMIC.
2074  *
2075  *	NULL is returned on a memory allocation failure.
2076  */
skb_share_check(struct sk_buff * skb,gfp_t pri)2077 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
2078 {
2079 	might_sleep_if(gfpflags_allow_blocking(pri));
2080 	if (skb_shared(skb)) {
2081 		struct sk_buff *nskb = skb_clone(skb, pri);
2082 
2083 		if (likely(nskb))
2084 			consume_skb(skb);
2085 		else
2086 			kfree_skb(skb);
2087 		skb = nskb;
2088 	}
2089 	return skb;
2090 }
2091 
2092 /*
2093  *	Copy shared buffers into a new sk_buff. We effectively do COW on
2094  *	packets to handle cases where we have a local reader and forward
2095  *	and a couple of other messy ones. The normal one is tcpdumping
2096  *	a packet that's being forwarded.
2097  */
2098 
2099 /**
2100  *	skb_unshare - make a copy of a shared buffer
2101  *	@skb: buffer to check
2102  *	@pri: priority for memory allocation
2103  *
2104  *	If the socket buffer is a clone then this function creates a new
2105  *	copy of the data, drops a reference count on the old copy and returns
2106  *	the new copy with the reference count at 1. If the buffer is not a clone
2107  *	the original buffer is returned. When called with a spinlock held or
2108  *	from interrupt state @pri must be %GFP_ATOMIC
2109  *
2110  *	%NULL is returned on a memory allocation failure.
2111  */
skb_unshare(struct sk_buff * skb,gfp_t pri)2112 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
2113 					  gfp_t pri)
2114 {
2115 	might_sleep_if(gfpflags_allow_blocking(pri));
2116 	if (skb_cloned(skb)) {
2117 		struct sk_buff *nskb = skb_copy(skb, pri);
2118 
2119 		/* Free our shared copy */
2120 		if (likely(nskb))
2121 			consume_skb(skb);
2122 		else
2123 			kfree_skb(skb);
2124 		skb = nskb;
2125 	}
2126 	return skb;
2127 }
2128 
2129 /**
2130  *	skb_peek - peek at the head of an &sk_buff_head
2131  *	@list_: list to peek at
2132  *
2133  *	Peek an &sk_buff. Unlike most other operations you _MUST_
2134  *	be careful with this one. A peek leaves the buffer on the
2135  *	list and someone else may run off with it. You must hold
2136  *	the appropriate locks or have a private queue to do this.
2137  *
2138  *	Returns %NULL for an empty list or a pointer to the head element.
2139  *	The reference count is not incremented and the reference is therefore
2140  *	volatile. Use with caution.
2141  */
skb_peek(const struct sk_buff_head * list_)2142 static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
2143 {
2144 	struct sk_buff *skb = list_->next;
2145 
2146 	if (skb == (struct sk_buff *)list_)
2147 		skb = NULL;
2148 	return skb;
2149 }
2150 
2151 /**
2152  *	__skb_peek - peek at the head of a non-empty &sk_buff_head
2153  *	@list_: list to peek at
2154  *
2155  *	Like skb_peek(), but the caller knows that the list is not empty.
2156  */
__skb_peek(const struct sk_buff_head * list_)2157 static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_)
2158 {
2159 	return list_->next;
2160 }
2161 
2162 /**
2163  *	skb_peek_next - peek skb following the given one from a queue
2164  *	@skb: skb to start from
2165  *	@list_: list to peek at
2166  *
2167  *	Returns %NULL when the end of the list is met or a pointer to the
2168  *	next element. The reference count is not incremented and the
2169  *	reference is therefore volatile. Use with caution.
2170  */
skb_peek_next(struct sk_buff * skb,const struct sk_buff_head * list_)2171 static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
2172 		const struct sk_buff_head *list_)
2173 {
2174 	struct sk_buff *next = skb->next;
2175 
2176 	if (next == (struct sk_buff *)list_)
2177 		next = NULL;
2178 	return next;
2179 }
2180 
2181 /**
2182  *	skb_peek_tail - peek at the tail of an &sk_buff_head
2183  *	@list_: list to peek at
2184  *
2185  *	Peek an &sk_buff. Unlike most other operations you _MUST_
2186  *	be careful with this one. A peek leaves the buffer on the
2187  *	list and someone else may run off with it. You must hold
2188  *	the appropriate locks or have a private queue to do this.
2189  *
2190  *	Returns %NULL for an empty list or a pointer to the tail element.
2191  *	The reference count is not incremented and the reference is therefore
2192  *	volatile. Use with caution.
2193  */
skb_peek_tail(const struct sk_buff_head * list_)2194 static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
2195 {
2196 	struct sk_buff *skb = READ_ONCE(list_->prev);
2197 
2198 	if (skb == (struct sk_buff *)list_)
2199 		skb = NULL;
2200 	return skb;
2201 
2202 }
2203 
2204 /**
2205  *	skb_queue_len	- get queue length
2206  *	@list_: list to measure
2207  *
2208  *	Return the length of an &sk_buff queue.
2209  */
skb_queue_len(const struct sk_buff_head * list_)2210 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
2211 {
2212 	return list_->qlen;
2213 }
2214 
2215 /**
2216  *	skb_queue_len_lockless	- get queue length
2217  *	@list_: list to measure
2218  *
2219  *	Return the length of an &sk_buff queue.
2220  *	This variant can be used in lockless contexts.
2221  */
skb_queue_len_lockless(const struct sk_buff_head * list_)2222 static inline __u32 skb_queue_len_lockless(const struct sk_buff_head *list_)
2223 {
2224 	return READ_ONCE(list_->qlen);
2225 }
2226 
2227 /**
2228  *	__skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
2229  *	@list: queue to initialize
2230  *
2231  *	This initializes only the list and queue length aspects of
2232  *	an sk_buff_head object.  This allows to initialize the list
2233  *	aspects of an sk_buff_head without reinitializing things like
2234  *	the spinlock.  It can also be used for on-stack sk_buff_head
2235  *	objects where the spinlock is known to not be used.
2236  */
__skb_queue_head_init(struct sk_buff_head * list)2237 static inline void __skb_queue_head_init(struct sk_buff_head *list)
2238 {
2239 	list->prev = list->next = (struct sk_buff *)list;
2240 	list->qlen = 0;
2241 }
2242 
2243 /*
2244  * This function creates a split out lock class for each invocation;
2245  * this is needed for now since a whole lot of users of the skb-queue
2246  * infrastructure in drivers have different locking usage (in hardirq)
2247  * than the networking core (in softirq only). In the long run either the
2248  * network layer or drivers should need annotation to consolidate the
2249  * main types of usage into 3 classes.
2250  */
skb_queue_head_init(struct sk_buff_head * list)2251 static inline void skb_queue_head_init(struct sk_buff_head *list)
2252 {
2253 	spin_lock_init(&list->lock);
2254 	__skb_queue_head_init(list);
2255 }
2256 
skb_queue_head_init_class(struct sk_buff_head * list,struct lock_class_key * class)2257 static inline void skb_queue_head_init_class(struct sk_buff_head *list,
2258 		struct lock_class_key *class)
2259 {
2260 	skb_queue_head_init(list);
2261 	lockdep_set_class(&list->lock, class);
2262 }
2263 
2264 /*
2265  *	Insert an sk_buff on a list.
2266  *
2267  *	The "__skb_xxxx()" functions are the non-atomic ones that
2268  *	can only be called with interrupts disabled.
2269  */
__skb_insert(struct sk_buff * newsk,struct sk_buff * prev,struct sk_buff * next,struct sk_buff_head * list)2270 static inline void __skb_insert(struct sk_buff *newsk,
2271 				struct sk_buff *prev, struct sk_buff *next,
2272 				struct sk_buff_head *list)
2273 {
2274 	/* See skb_queue_empty_lockless() and skb_peek_tail()
2275 	 * for the opposite READ_ONCE()
2276 	 */
2277 	WRITE_ONCE(newsk->next, next);
2278 	WRITE_ONCE(newsk->prev, prev);
2279 	WRITE_ONCE(((struct sk_buff_list *)next)->prev, newsk);
2280 	WRITE_ONCE(((struct sk_buff_list *)prev)->next, newsk);
2281 	WRITE_ONCE(list->qlen, list->qlen + 1);
2282 }
2283 
__skb_queue_splice(const struct sk_buff_head * list,struct sk_buff * prev,struct sk_buff * next)2284 static inline void __skb_queue_splice(const struct sk_buff_head *list,
2285 				      struct sk_buff *prev,
2286 				      struct sk_buff *next)
2287 {
2288 	struct sk_buff *first = list->next;
2289 	struct sk_buff *last = list->prev;
2290 
2291 	WRITE_ONCE(first->prev, prev);
2292 	WRITE_ONCE(prev->next, first);
2293 
2294 	WRITE_ONCE(last->next, next);
2295 	WRITE_ONCE(next->prev, last);
2296 }
2297 
2298 /**
2299  *	skb_queue_splice - join two skb lists, this is designed for stacks
2300  *	@list: the new list to add
2301  *	@head: the place to add it in the first list
2302  */
skb_queue_splice(const struct sk_buff_head * list,struct sk_buff_head * head)2303 static inline void skb_queue_splice(const struct sk_buff_head *list,
2304 				    struct sk_buff_head *head)
2305 {
2306 	if (!skb_queue_empty(list)) {
2307 		__skb_queue_splice(list, (struct sk_buff *) head, head->next);
2308 		head->qlen += list->qlen;
2309 	}
2310 }
2311 
2312 /**
2313  *	skb_queue_splice_init - join two skb lists and reinitialise the emptied list
2314  *	@list: the new list to add
2315  *	@head: the place to add it in the first list
2316  *
2317  *	The list at @list is reinitialised
2318  */
skb_queue_splice_init(struct sk_buff_head * list,struct sk_buff_head * head)2319 static inline void skb_queue_splice_init(struct sk_buff_head *list,
2320 					 struct sk_buff_head *head)
2321 {
2322 	if (!skb_queue_empty(list)) {
2323 		__skb_queue_splice(list, (struct sk_buff *) head, head->next);
2324 		head->qlen += list->qlen;
2325 		__skb_queue_head_init(list);
2326 	}
2327 }
2328 
2329 /**
2330  *	skb_queue_splice_tail - join two skb lists, each list being a queue
2331  *	@list: the new list to add
2332  *	@head: the place to add it in the first list
2333  */
skb_queue_splice_tail(const struct sk_buff_head * list,struct sk_buff_head * head)2334 static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
2335 					 struct sk_buff_head *head)
2336 {
2337 	if (!skb_queue_empty(list)) {
2338 		__skb_queue_splice(list, head->prev, (struct sk_buff *) head);
2339 		head->qlen += list->qlen;
2340 	}
2341 }
2342 
2343 /**
2344  *	skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
2345  *	@list: the new list to add
2346  *	@head: the place to add it in the first list
2347  *
2348  *	Each of the lists is a queue.
2349  *	The list at @list is reinitialised
2350  */
skb_queue_splice_tail_init(struct sk_buff_head * list,struct sk_buff_head * head)2351 static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
2352 					      struct sk_buff_head *head)
2353 {
2354 	if (!skb_queue_empty(list)) {
2355 		__skb_queue_splice(list, head->prev, (struct sk_buff *) head);
2356 		head->qlen += list->qlen;
2357 		__skb_queue_head_init(list);
2358 	}
2359 }
2360 
2361 /**
2362  *	__skb_queue_after - queue a buffer at the list head
2363  *	@list: list to use
2364  *	@prev: place after this buffer
2365  *	@newsk: buffer to queue
2366  *
2367  *	Queue a buffer int the middle of a list. This function takes no locks
2368  *	and you must therefore hold required locks before calling it.
2369  *
2370  *	A buffer cannot be placed on two lists at the same time.
2371  */
__skb_queue_after(struct sk_buff_head * list,struct sk_buff * prev,struct sk_buff * newsk)2372 static inline void __skb_queue_after(struct sk_buff_head *list,
2373 				     struct sk_buff *prev,
2374 				     struct sk_buff *newsk)
2375 {
2376 	__skb_insert(newsk, prev, ((struct sk_buff_list *)prev)->next, list);
2377 }
2378 
2379 void skb_append(struct sk_buff *old, struct sk_buff *newsk,
2380 		struct sk_buff_head *list);
2381 
__skb_queue_before(struct sk_buff_head * list,struct sk_buff * next,struct sk_buff * newsk)2382 static inline void __skb_queue_before(struct sk_buff_head *list,
2383 				      struct sk_buff *next,
2384 				      struct sk_buff *newsk)
2385 {
2386 	__skb_insert(newsk, ((struct sk_buff_list *)next)->prev, next, list);
2387 }
2388 
2389 /**
2390  *	__skb_queue_head - queue a buffer at the list head
2391  *	@list: list to use
2392  *	@newsk: buffer to queue
2393  *
2394  *	Queue a buffer at the start of a list. This function takes no locks
2395  *	and you must therefore hold required locks before calling it.
2396  *
2397  *	A buffer cannot be placed on two lists at the same time.
2398  */
__skb_queue_head(struct sk_buff_head * list,struct sk_buff * newsk)2399 static inline void __skb_queue_head(struct sk_buff_head *list,
2400 				    struct sk_buff *newsk)
2401 {
2402 	__skb_queue_after(list, (struct sk_buff *)list, newsk);
2403 }
2404 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
2405 
2406 /**
2407  *	__skb_queue_tail - queue a buffer at the list tail
2408  *	@list: list to use
2409  *	@newsk: buffer to queue
2410  *
2411  *	Queue a buffer at the end of a list. This function takes no locks
2412  *	and you must therefore hold required locks before calling it.
2413  *
2414  *	A buffer cannot be placed on two lists at the same time.
2415  */
__skb_queue_tail(struct sk_buff_head * list,struct sk_buff * newsk)2416 static inline void __skb_queue_tail(struct sk_buff_head *list,
2417 				   struct sk_buff *newsk)
2418 {
2419 	__skb_queue_before(list, (struct sk_buff *)list, newsk);
2420 }
2421 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
2422 
2423 /*
2424  * remove sk_buff from list. _Must_ be called atomically, and with
2425  * the list known..
2426  */
2427 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
__skb_unlink(struct sk_buff * skb,struct sk_buff_head * list)2428 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2429 {
2430 	struct sk_buff *next, *prev;
2431 
2432 	WRITE_ONCE(list->qlen, list->qlen - 1);
2433 	next	   = skb->next;
2434 	prev	   = skb->prev;
2435 	skb->next  = skb->prev = NULL;
2436 	WRITE_ONCE(next->prev, prev);
2437 	WRITE_ONCE(prev->next, next);
2438 }
2439 
2440 /**
2441  *	__skb_dequeue - remove from the head of the queue
2442  *	@list: list to dequeue from
2443  *
2444  *	Remove the head of the list. This function does not take any locks
2445  *	so must be used with appropriate locks held only. The head item is
2446  *	returned or %NULL if the list is empty.
2447  */
__skb_dequeue(struct sk_buff_head * list)2448 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
2449 {
2450 	struct sk_buff *skb = skb_peek(list);
2451 	if (skb)
2452 		__skb_unlink(skb, list);
2453 	return skb;
2454 }
2455 struct sk_buff *skb_dequeue(struct sk_buff_head *list);
2456 
2457 /**
2458  *	__skb_dequeue_tail - remove from the tail of the queue
2459  *	@list: list to dequeue from
2460  *
2461  *	Remove the tail of the list. This function does not take any locks
2462  *	so must be used with appropriate locks held only. The tail item is
2463  *	returned or %NULL if the list is empty.
2464  */
__skb_dequeue_tail(struct sk_buff_head * list)2465 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
2466 {
2467 	struct sk_buff *skb = skb_peek_tail(list);
2468 	if (skb)
2469 		__skb_unlink(skb, list);
2470 	return skb;
2471 }
2472 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
2473 
2474 
skb_is_nonlinear(const struct sk_buff * skb)2475 static inline bool skb_is_nonlinear(const struct sk_buff *skb)
2476 {
2477 	return skb->data_len;
2478 }
2479 
skb_headlen(const struct sk_buff * skb)2480 static inline unsigned int skb_headlen(const struct sk_buff *skb)
2481 {
2482 	return skb->len - skb->data_len;
2483 }
2484 
__skb_pagelen(const struct sk_buff * skb)2485 static inline unsigned int __skb_pagelen(const struct sk_buff *skb)
2486 {
2487 	unsigned int i, len = 0;
2488 
2489 	for (i = skb_shinfo(skb)->nr_frags - 1; (int)i >= 0; i--)
2490 		len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2491 	return len;
2492 }
2493 
skb_pagelen(const struct sk_buff * skb)2494 static inline unsigned int skb_pagelen(const struct sk_buff *skb)
2495 {
2496 	return skb_headlen(skb) + __skb_pagelen(skb);
2497 }
2498 
skb_frag_fill_netmem_desc(skb_frag_t * frag,netmem_ref netmem,int off,int size)2499 static inline void skb_frag_fill_netmem_desc(skb_frag_t *frag,
2500 					     netmem_ref netmem, int off,
2501 					     int size)
2502 {
2503 	frag->netmem = netmem;
2504 	frag->offset = off;
2505 	skb_frag_size_set(frag, size);
2506 }
2507 
skb_frag_fill_page_desc(skb_frag_t * frag,struct page * page,int off,int size)2508 static inline void skb_frag_fill_page_desc(skb_frag_t *frag,
2509 					   struct page *page,
2510 					   int off, int size)
2511 {
2512 	skb_frag_fill_netmem_desc(frag, page_to_netmem(page), off, size);
2513 }
2514 
__skb_fill_netmem_desc_noacc(struct skb_shared_info * shinfo,int i,netmem_ref netmem,int off,int size)2515 static inline void __skb_fill_netmem_desc_noacc(struct skb_shared_info *shinfo,
2516 						int i, netmem_ref netmem,
2517 						int off, int size)
2518 {
2519 	skb_frag_t *frag = &shinfo->frags[i];
2520 
2521 	skb_frag_fill_netmem_desc(frag, netmem, off, size);
2522 }
2523 
__skb_fill_page_desc_noacc(struct skb_shared_info * shinfo,int i,struct page * page,int off,int size)2524 static inline void __skb_fill_page_desc_noacc(struct skb_shared_info *shinfo,
2525 					      int i, struct page *page,
2526 					      int off, int size)
2527 {
2528 	__skb_fill_netmem_desc_noacc(shinfo, i, page_to_netmem(page), off,
2529 				     size);
2530 }
2531 
2532 /**
2533  * skb_len_add - adds a number to len fields of skb
2534  * @skb: buffer to add len to
2535  * @delta: number of bytes to add
2536  */
skb_len_add(struct sk_buff * skb,int delta)2537 static inline void skb_len_add(struct sk_buff *skb, int delta)
2538 {
2539 	skb->len += delta;
2540 	skb->data_len += delta;
2541 	skb->truesize += delta;
2542 }
2543 
2544 /**
2545  * __skb_fill_netmem_desc - initialise a fragment in an skb
2546  * @skb: buffer containing fragment to be initialised
2547  * @i: fragment index to initialise
2548  * @netmem: the netmem to use for this fragment
2549  * @off: the offset to the data with @page
2550  * @size: the length of the data
2551  *
2552  * Initialises the @i'th fragment of @skb to point to &size bytes at
2553  * offset @off within @page.
2554  *
2555  * Does not take any additional reference on the fragment.
2556  */
__skb_fill_netmem_desc(struct sk_buff * skb,int i,netmem_ref netmem,int off,int size)2557 static inline void __skb_fill_netmem_desc(struct sk_buff *skb, int i,
2558 					  netmem_ref netmem, int off, int size)
2559 {
2560 	struct page *page;
2561 
2562 	__skb_fill_netmem_desc_noacc(skb_shinfo(skb), i, netmem, off, size);
2563 
2564 	if (netmem_is_net_iov(netmem)) {
2565 		skb->unreadable = true;
2566 		return;
2567 	}
2568 
2569 	page = netmem_to_page(netmem);
2570 
2571 	/* Propagate page pfmemalloc to the skb if we can. The problem is
2572 	 * that not all callers have unique ownership of the page but rely
2573 	 * on page_is_pfmemalloc doing the right thing(tm).
2574 	 */
2575 	page = compound_head(page);
2576 	if (page_is_pfmemalloc(page))
2577 		skb->pfmemalloc = true;
2578 }
2579 
__skb_fill_page_desc(struct sk_buff * skb,int i,struct page * page,int off,int size)2580 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
2581 					struct page *page, int off, int size)
2582 {
2583 	__skb_fill_netmem_desc(skb, i, page_to_netmem(page), off, size);
2584 }
2585 
skb_fill_netmem_desc(struct sk_buff * skb,int i,netmem_ref netmem,int off,int size)2586 static inline void skb_fill_netmem_desc(struct sk_buff *skb, int i,
2587 					netmem_ref netmem, int off, int size)
2588 {
2589 	__skb_fill_netmem_desc(skb, i, netmem, off, size);
2590 	skb_shinfo(skb)->nr_frags = i + 1;
2591 }
2592 
2593 /**
2594  * skb_fill_page_desc - initialise a paged fragment in an skb
2595  * @skb: buffer containing fragment to be initialised
2596  * @i: paged fragment index to initialise
2597  * @page: the page to use for this fragment
2598  * @off: the offset to the data with @page
2599  * @size: the length of the data
2600  *
2601  * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
2602  * @skb to point to @size bytes at offset @off within @page. In
2603  * addition updates @skb such that @i is the last fragment.
2604  *
2605  * Does not take any additional reference on the fragment.
2606  */
skb_fill_page_desc(struct sk_buff * skb,int i,struct page * page,int off,int size)2607 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
2608 				      struct page *page, int off, int size)
2609 {
2610 	skb_fill_netmem_desc(skb, i, page_to_netmem(page), off, size);
2611 }
2612 
2613 /**
2614  * skb_fill_page_desc_noacc - initialise a paged fragment in an skb
2615  * @skb: buffer containing fragment to be initialised
2616  * @i: paged fragment index to initialise
2617  * @page: the page to use for this fragment
2618  * @off: the offset to the data with @page
2619  * @size: the length of the data
2620  *
2621  * Variant of skb_fill_page_desc() which does not deal with
2622  * pfmemalloc, if page is not owned by us.
2623  */
skb_fill_page_desc_noacc(struct sk_buff * skb,int i,struct page * page,int off,int size)2624 static inline void skb_fill_page_desc_noacc(struct sk_buff *skb, int i,
2625 					    struct page *page, int off,
2626 					    int size)
2627 {
2628 	struct skb_shared_info *shinfo = skb_shinfo(skb);
2629 
2630 	__skb_fill_page_desc_noacc(shinfo, i, page, off, size);
2631 	shinfo->nr_frags = i + 1;
2632 }
2633 
2634 void skb_add_rx_frag_netmem(struct sk_buff *skb, int i, netmem_ref netmem,
2635 			    int off, int size, unsigned int truesize);
2636 
skb_add_rx_frag(struct sk_buff * skb,int i,struct page * page,int off,int size,unsigned int truesize)2637 static inline void skb_add_rx_frag(struct sk_buff *skb, int i,
2638 				   struct page *page, int off, int size,
2639 				   unsigned int truesize)
2640 {
2641 	skb_add_rx_frag_netmem(skb, i, page_to_netmem(page), off, size,
2642 			       truesize);
2643 }
2644 
2645 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
2646 			  unsigned int truesize);
2647 
2648 #define SKB_LINEAR_ASSERT(skb)  BUG_ON(skb_is_nonlinear(skb))
2649 
2650 #ifdef NET_SKBUFF_DATA_USES_OFFSET
skb_tail_pointer(const struct sk_buff * skb)2651 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
2652 {
2653 	return skb->head + skb->tail;
2654 }
2655 
skb_reset_tail_pointer(struct sk_buff * skb)2656 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
2657 {
2658 	skb->tail = skb->data - skb->head;
2659 }
2660 
skb_set_tail_pointer(struct sk_buff * skb,const int offset)2661 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
2662 {
2663 	skb_reset_tail_pointer(skb);
2664 	skb->tail += offset;
2665 }
2666 
2667 #else /* NET_SKBUFF_DATA_USES_OFFSET */
skb_tail_pointer(const struct sk_buff * skb)2668 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
2669 {
2670 	return skb->tail;
2671 }
2672 
skb_reset_tail_pointer(struct sk_buff * skb)2673 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
2674 {
2675 	skb->tail = skb->data;
2676 }
2677 
skb_set_tail_pointer(struct sk_buff * skb,const int offset)2678 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
2679 {
2680 	skb->tail = skb->data + offset;
2681 }
2682 
2683 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
2684 
skb_assert_len(struct sk_buff * skb)2685 static inline void skb_assert_len(struct sk_buff *skb)
2686 {
2687 #ifdef CONFIG_DEBUG_NET
2688 	if (WARN_ONCE(!skb->len, "%s\n", __func__))
2689 		DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
2690 #endif /* CONFIG_DEBUG_NET */
2691 }
2692 
2693 /*
2694  *	Add data to an sk_buff
2695  */
2696 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len);
2697 void *skb_put(struct sk_buff *skb, unsigned int len);
__skb_put(struct sk_buff * skb,unsigned int len)2698 static inline void *__skb_put(struct sk_buff *skb, unsigned int len)
2699 {
2700 	void *tmp = skb_tail_pointer(skb);
2701 	SKB_LINEAR_ASSERT(skb);
2702 	skb->tail += len;
2703 	skb->len  += len;
2704 	return tmp;
2705 }
2706 
__skb_put_zero(struct sk_buff * skb,unsigned int len)2707 static inline void *__skb_put_zero(struct sk_buff *skb, unsigned int len)
2708 {
2709 	void *tmp = __skb_put(skb, len);
2710 
2711 	memset(tmp, 0, len);
2712 	return tmp;
2713 }
2714 
__skb_put_data(struct sk_buff * skb,const void * data,unsigned int len)2715 static inline void *__skb_put_data(struct sk_buff *skb, const void *data,
2716 				   unsigned int len)
2717 {
2718 	void *tmp = __skb_put(skb, len);
2719 
2720 	memcpy(tmp, data, len);
2721 	return tmp;
2722 }
2723 
__skb_put_u8(struct sk_buff * skb,u8 val)2724 static inline void __skb_put_u8(struct sk_buff *skb, u8 val)
2725 {
2726 	*(u8 *)__skb_put(skb, 1) = val;
2727 }
2728 
skb_put_zero(struct sk_buff * skb,unsigned int len)2729 static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len)
2730 {
2731 	void *tmp = skb_put(skb, len);
2732 
2733 	memset(tmp, 0, len);
2734 
2735 	return tmp;
2736 }
2737 
skb_put_data(struct sk_buff * skb,const void * data,unsigned int len)2738 static inline void *skb_put_data(struct sk_buff *skb, const void *data,
2739 				 unsigned int len)
2740 {
2741 	void *tmp = skb_put(skb, len);
2742 
2743 	memcpy(tmp, data, len);
2744 
2745 	return tmp;
2746 }
2747 
skb_put_u8(struct sk_buff * skb,u8 val)2748 static inline void skb_put_u8(struct sk_buff *skb, u8 val)
2749 {
2750 	*(u8 *)skb_put(skb, 1) = val;
2751 }
2752 
2753 void *skb_push(struct sk_buff *skb, unsigned int len);
__skb_push(struct sk_buff * skb,unsigned int len)2754 static inline void *__skb_push(struct sk_buff *skb, unsigned int len)
2755 {
2756 	DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
2757 
2758 	skb->data -= len;
2759 	skb->len  += len;
2760 	return skb->data;
2761 }
2762 
2763 void *skb_pull(struct sk_buff *skb, unsigned int len);
__skb_pull(struct sk_buff * skb,unsigned int len)2764 static inline void *__skb_pull(struct sk_buff *skb, unsigned int len)
2765 {
2766 	DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
2767 
2768 	skb->len -= len;
2769 	if (unlikely(skb->len < skb->data_len)) {
2770 #if defined(CONFIG_DEBUG_NET)
2771 		skb->len += len;
2772 		pr_err("__skb_pull(len=%u)\n", len);
2773 		skb_dump(KERN_ERR, skb, false);
2774 #endif
2775 		BUG();
2776 	}
2777 	return skb->data += len;
2778 }
2779 
skb_pull_inline(struct sk_buff * skb,unsigned int len)2780 static inline void *skb_pull_inline(struct sk_buff *skb, unsigned int len)
2781 {
2782 	return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
2783 }
2784 
2785 void *skb_pull_data(struct sk_buff *skb, size_t len);
2786 
2787 void *__pskb_pull_tail(struct sk_buff *skb, int delta);
2788 
2789 static inline enum skb_drop_reason
pskb_may_pull_reason(struct sk_buff * skb,unsigned int len)2790 pskb_may_pull_reason(struct sk_buff *skb, unsigned int len)
2791 {
2792 	DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
2793 
2794 	if (likely(len <= skb_headlen(skb)))
2795 		return SKB_NOT_DROPPED_YET;
2796 
2797 	if (unlikely(len > skb->len))
2798 		return SKB_DROP_REASON_PKT_TOO_SMALL;
2799 
2800 	if (unlikely(!__pskb_pull_tail(skb, len - skb_headlen(skb))))
2801 		return SKB_DROP_REASON_NOMEM;
2802 
2803 	return SKB_NOT_DROPPED_YET;
2804 }
2805 
pskb_may_pull(struct sk_buff * skb,unsigned int len)2806 static inline bool pskb_may_pull(struct sk_buff *skb, unsigned int len)
2807 {
2808 	return pskb_may_pull_reason(skb, len) == SKB_NOT_DROPPED_YET;
2809 }
2810 
pskb_pull(struct sk_buff * skb,unsigned int len)2811 static inline void *pskb_pull(struct sk_buff *skb, unsigned int len)
2812 {
2813 	if (!pskb_may_pull(skb, len))
2814 		return NULL;
2815 
2816 	skb->len -= len;
2817 	return skb->data += len;
2818 }
2819 
2820 void skb_condense(struct sk_buff *skb);
2821 
2822 /**
2823  *	skb_headroom - bytes at buffer head
2824  *	@skb: buffer to check
2825  *
2826  *	Return the number of bytes of free space at the head of an &sk_buff.
2827  */
skb_headroom(const struct sk_buff * skb)2828 static inline unsigned int skb_headroom(const struct sk_buff *skb)
2829 {
2830 	return skb->data - skb->head;
2831 }
2832 
2833 /**
2834  *	skb_tailroom - bytes at buffer end
2835  *	@skb: buffer to check
2836  *
2837  *	Return the number of bytes of free space at the tail of an sk_buff
2838  */
skb_tailroom(const struct sk_buff * skb)2839 static inline int skb_tailroom(const struct sk_buff *skb)
2840 {
2841 	return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
2842 }
2843 
2844 /**
2845  *	skb_availroom - bytes at buffer end
2846  *	@skb: buffer to check
2847  *
2848  *	Return the number of bytes of free space at the tail of an sk_buff
2849  *	allocated by sk_stream_alloc()
2850  */
skb_availroom(const struct sk_buff * skb)2851 static inline int skb_availroom(const struct sk_buff *skb)
2852 {
2853 	if (skb_is_nonlinear(skb))
2854 		return 0;
2855 
2856 	return skb->end - skb->tail - skb->reserved_tailroom;
2857 }
2858 
2859 /**
2860  *	skb_reserve - adjust headroom
2861  *	@skb: buffer to alter
2862  *	@len: bytes to move
2863  *
2864  *	Increase the headroom of an empty &sk_buff by reducing the tail
2865  *	room. This is only allowed for an empty buffer.
2866  */
skb_reserve(struct sk_buff * skb,int len)2867 static inline void skb_reserve(struct sk_buff *skb, int len)
2868 {
2869 	skb->data += len;
2870 	skb->tail += len;
2871 }
2872 
2873 /**
2874  *	skb_tailroom_reserve - adjust reserved_tailroom
2875  *	@skb: buffer to alter
2876  *	@mtu: maximum amount of headlen permitted
2877  *	@needed_tailroom: minimum amount of reserved_tailroom
2878  *
2879  *	Set reserved_tailroom so that headlen can be as large as possible but
2880  *	not larger than mtu and tailroom cannot be smaller than
2881  *	needed_tailroom.
2882  *	The required headroom should already have been reserved before using
2883  *	this function.
2884  */
skb_tailroom_reserve(struct sk_buff * skb,unsigned int mtu,unsigned int needed_tailroom)2885 static inline void skb_tailroom_reserve(struct sk_buff *skb, unsigned int mtu,
2886 					unsigned int needed_tailroom)
2887 {
2888 	SKB_LINEAR_ASSERT(skb);
2889 	if (mtu < skb_tailroom(skb) - needed_tailroom)
2890 		/* use at most mtu */
2891 		skb->reserved_tailroom = skb_tailroom(skb) - mtu;
2892 	else
2893 		/* use up to all available space */
2894 		skb->reserved_tailroom = needed_tailroom;
2895 }
2896 
2897 #define ENCAP_TYPE_ETHER	0
2898 #define ENCAP_TYPE_IPPROTO	1
2899 
skb_set_inner_protocol(struct sk_buff * skb,__be16 protocol)2900 static inline void skb_set_inner_protocol(struct sk_buff *skb,
2901 					  __be16 protocol)
2902 {
2903 	skb->inner_protocol = protocol;
2904 	skb->inner_protocol_type = ENCAP_TYPE_ETHER;
2905 }
2906 
skb_set_inner_ipproto(struct sk_buff * skb,__u8 ipproto)2907 static inline void skb_set_inner_ipproto(struct sk_buff *skb,
2908 					 __u8 ipproto)
2909 {
2910 	skb->inner_ipproto = ipproto;
2911 	skb->inner_protocol_type = ENCAP_TYPE_IPPROTO;
2912 }
2913 
skb_reset_inner_headers(struct sk_buff * skb)2914 static inline void skb_reset_inner_headers(struct sk_buff *skb)
2915 {
2916 	skb->inner_mac_header = skb->mac_header;
2917 	skb->inner_network_header = skb->network_header;
2918 	skb->inner_transport_header = skb->transport_header;
2919 }
2920 
skb_reset_mac_len(struct sk_buff * skb)2921 static inline void skb_reset_mac_len(struct sk_buff *skb)
2922 {
2923 	skb->mac_len = skb->network_header - skb->mac_header;
2924 }
2925 
skb_inner_transport_header(const struct sk_buff * skb)2926 static inline unsigned char *skb_inner_transport_header(const struct sk_buff
2927 							*skb)
2928 {
2929 	return skb->head + skb->inner_transport_header;
2930 }
2931 
skb_inner_transport_offset(const struct sk_buff * skb)2932 static inline int skb_inner_transport_offset(const struct sk_buff *skb)
2933 {
2934 	return skb_inner_transport_header(skb) - skb->data;
2935 }
2936 
skb_reset_inner_transport_header(struct sk_buff * skb)2937 static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
2938 {
2939 	skb->inner_transport_header = skb->data - skb->head;
2940 }
2941 
skb_set_inner_transport_header(struct sk_buff * skb,const int offset)2942 static inline void skb_set_inner_transport_header(struct sk_buff *skb,
2943 						   const int offset)
2944 {
2945 	skb_reset_inner_transport_header(skb);
2946 	skb->inner_transport_header += offset;
2947 }
2948 
skb_inner_network_header(const struct sk_buff * skb)2949 static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
2950 {
2951 	return skb->head + skb->inner_network_header;
2952 }
2953 
skb_reset_inner_network_header(struct sk_buff * skb)2954 static inline void skb_reset_inner_network_header(struct sk_buff *skb)
2955 {
2956 	skb->inner_network_header = skb->data - skb->head;
2957 }
2958 
skb_set_inner_network_header(struct sk_buff * skb,const int offset)2959 static inline void skb_set_inner_network_header(struct sk_buff *skb,
2960 						const int offset)
2961 {
2962 	skb_reset_inner_network_header(skb);
2963 	skb->inner_network_header += offset;
2964 }
2965 
skb_inner_network_header_was_set(const struct sk_buff * skb)2966 static inline bool skb_inner_network_header_was_set(const struct sk_buff *skb)
2967 {
2968 	return skb->inner_network_header > 0;
2969 }
2970 
skb_inner_mac_header(const struct sk_buff * skb)2971 static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb)
2972 {
2973 	return skb->head + skb->inner_mac_header;
2974 }
2975 
skb_reset_inner_mac_header(struct sk_buff * skb)2976 static inline void skb_reset_inner_mac_header(struct sk_buff *skb)
2977 {
2978 	skb->inner_mac_header = skb->data - skb->head;
2979 }
2980 
skb_set_inner_mac_header(struct sk_buff * skb,const int offset)2981 static inline void skb_set_inner_mac_header(struct sk_buff *skb,
2982 					    const int offset)
2983 {
2984 	skb_reset_inner_mac_header(skb);
2985 	skb->inner_mac_header += offset;
2986 }
skb_transport_header_was_set(const struct sk_buff * skb)2987 static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
2988 {
2989 	return skb->transport_header != (typeof(skb->transport_header))~0U;
2990 }
2991 
skb_transport_header(const struct sk_buff * skb)2992 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
2993 {
2994 	DEBUG_NET_WARN_ON_ONCE(!skb_transport_header_was_set(skb));
2995 	return skb->head + skb->transport_header;
2996 }
2997 
skb_reset_transport_header(struct sk_buff * skb)2998 static inline void skb_reset_transport_header(struct sk_buff *skb)
2999 {
3000 	skb->transport_header = skb->data - skb->head;
3001 }
3002 
3003 /**
3004  * skb_reset_transport_header_careful - conditionally reset transport header
3005  * @skb: buffer to alter
3006  *
3007  * Hardened version of skb_reset_transport_header().
3008  *
3009  * Returns: true if the operation was a success.
3010  */
3011 static inline bool __must_check
skb_reset_transport_header_careful(struct sk_buff * skb)3012 skb_reset_transport_header_careful(struct sk_buff *skb)
3013 {
3014 	long offset = skb->data - skb->head;
3015 
3016 	if (unlikely(offset != (typeof(skb->transport_header))offset))
3017 		return false;
3018 
3019 	if (unlikely(offset == (typeof(skb->transport_header))~0U))
3020 		return false;
3021 
3022 	skb->transport_header = offset;
3023 	return true;
3024 }
3025 
skb_set_transport_header(struct sk_buff * skb,const int offset)3026 static inline void skb_set_transport_header(struct sk_buff *skb,
3027 					    const int offset)
3028 {
3029 	skb_reset_transport_header(skb);
3030 	skb->transport_header += offset;
3031 }
3032 
skb_network_header(const struct sk_buff * skb)3033 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
3034 {
3035 	return skb->head + skb->network_header;
3036 }
3037 
skb_reset_network_header(struct sk_buff * skb)3038 static inline void skb_reset_network_header(struct sk_buff *skb)
3039 {
3040 	skb->network_header = skb->data - skb->head;
3041 }
3042 
skb_set_network_header(struct sk_buff * skb,const int offset)3043 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
3044 {
3045 	skb_reset_network_header(skb);
3046 	skb->network_header += offset;
3047 }
3048 
skb_mac_header_was_set(const struct sk_buff * skb)3049 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
3050 {
3051 	return skb->mac_header != (typeof(skb->mac_header))~0U;
3052 }
3053 
skb_mac_header(const struct sk_buff * skb)3054 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
3055 {
3056 	DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));
3057 	return skb->head + skb->mac_header;
3058 }
3059 
skb_mac_offset(const struct sk_buff * skb)3060 static inline int skb_mac_offset(const struct sk_buff *skb)
3061 {
3062 	return skb_mac_header(skb) - skb->data;
3063 }
3064 
skb_mac_header_len(const struct sk_buff * skb)3065 static inline u32 skb_mac_header_len(const struct sk_buff *skb)
3066 {
3067 	DEBUG_NET_WARN_ON_ONCE(!skb_mac_header_was_set(skb));
3068 	return skb->network_header - skb->mac_header;
3069 }
3070 
skb_unset_mac_header(struct sk_buff * skb)3071 static inline void skb_unset_mac_header(struct sk_buff *skb)
3072 {
3073 	skb->mac_header = (typeof(skb->mac_header))~0U;
3074 }
3075 
skb_reset_mac_header(struct sk_buff * skb)3076 static inline void skb_reset_mac_header(struct sk_buff *skb)
3077 {
3078 	skb->mac_header = skb->data - skb->head;
3079 }
3080 
skb_set_mac_header(struct sk_buff * skb,const int offset)3081 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
3082 {
3083 	skb_reset_mac_header(skb);
3084 	skb->mac_header += offset;
3085 }
3086 
skb_pop_mac_header(struct sk_buff * skb)3087 static inline void skb_pop_mac_header(struct sk_buff *skb)
3088 {
3089 	skb->mac_header = skb->network_header;
3090 }
3091 
skb_probe_transport_header(struct sk_buff * skb)3092 static inline void skb_probe_transport_header(struct sk_buff *skb)
3093 {
3094 	struct flow_keys_basic keys;
3095 
3096 	if (skb_transport_header_was_set(skb))
3097 		return;
3098 
3099 	if (skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
3100 					     NULL, 0, 0, 0, 0))
3101 		skb_set_transport_header(skb, keys.control.thoff);
3102 }
3103 
skb_mac_header_rebuild(struct sk_buff * skb)3104 static inline void skb_mac_header_rebuild(struct sk_buff *skb)
3105 {
3106 	if (skb_mac_header_was_set(skb)) {
3107 		const unsigned char *old_mac = skb_mac_header(skb);
3108 
3109 		skb_set_mac_header(skb, -skb->mac_len);
3110 		memmove(skb_mac_header(skb), old_mac, skb->mac_len);
3111 	}
3112 }
3113 
3114 /* Move the full mac header up to current network_header.
3115  * Leaves skb->data pointing at offset skb->mac_len into the mac_header.
3116  * Must be provided the complete mac header length.
3117  */
skb_mac_header_rebuild_full(struct sk_buff * skb,u32 full_mac_len)3118 static inline void skb_mac_header_rebuild_full(struct sk_buff *skb, u32 full_mac_len)
3119 {
3120 	if (skb_mac_header_was_set(skb)) {
3121 		const unsigned char *old_mac = skb_mac_header(skb);
3122 
3123 		skb_set_mac_header(skb, -full_mac_len);
3124 		memmove(skb_mac_header(skb), old_mac, full_mac_len);
3125 		__skb_push(skb, full_mac_len - skb->mac_len);
3126 	}
3127 }
3128 
skb_checksum_start_offset(const struct sk_buff * skb)3129 static inline int skb_checksum_start_offset(const struct sk_buff *skb)
3130 {
3131 	return skb->csum_start - skb_headroom(skb);
3132 }
3133 
skb_checksum_start(const struct sk_buff * skb)3134 static inline unsigned char *skb_checksum_start(const struct sk_buff *skb)
3135 {
3136 	return skb->head + skb->csum_start;
3137 }
3138 
skb_transport_offset(const struct sk_buff * skb)3139 static inline int skb_transport_offset(const struct sk_buff *skb)
3140 {
3141 	return skb_transport_header(skb) - skb->data;
3142 }
3143 
skb_network_header_len(const struct sk_buff * skb)3144 static inline u32 skb_network_header_len(const struct sk_buff *skb)
3145 {
3146 	DEBUG_NET_WARN_ON_ONCE(!skb_transport_header_was_set(skb));
3147 	return skb->transport_header - skb->network_header;
3148 }
3149 
skb_inner_network_header_len(const struct sk_buff * skb)3150 static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
3151 {
3152 	return skb->inner_transport_header - skb->inner_network_header;
3153 }
3154 
skb_network_offset(const struct sk_buff * skb)3155 static inline int skb_network_offset(const struct sk_buff *skb)
3156 {
3157 	return skb_network_header(skb) - skb->data;
3158 }
3159 
skb_inner_network_offset(const struct sk_buff * skb)3160 static inline int skb_inner_network_offset(const struct sk_buff *skb)
3161 {
3162 	return skb_inner_network_header(skb) - skb->data;
3163 }
3164 
pskb_network_may_pull(struct sk_buff * skb,unsigned int len)3165 static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
3166 {
3167 	return pskb_may_pull(skb, skb_network_offset(skb) + len);
3168 }
3169 
3170 /*
3171  * CPUs often take a performance hit when accessing unaligned memory
3172  * locations. The actual performance hit varies, it can be small if the
3173  * hardware handles it or large if we have to take an exception and fix it
3174  * in software.
3175  *
3176  * Since an ethernet header is 14 bytes network drivers often end up with
3177  * the IP header at an unaligned offset. The IP header can be aligned by
3178  * shifting the start of the packet by 2 bytes. Drivers should do this
3179  * with:
3180  *
3181  * skb_reserve(skb, NET_IP_ALIGN);
3182  *
3183  * The downside to this alignment of the IP header is that the DMA is now
3184  * unaligned. On some architectures the cost of an unaligned DMA is high
3185  * and this cost outweighs the gains made by aligning the IP header.
3186  *
3187  * Since this trade off varies between architectures, we allow NET_IP_ALIGN
3188  * to be overridden.
3189  */
3190 #ifndef NET_IP_ALIGN
3191 #define NET_IP_ALIGN	2
3192 #endif
3193 
3194 /*
3195  * The networking layer reserves some headroom in skb data (via
3196  * dev_alloc_skb). This is used to avoid having to reallocate skb data when
3197  * the header has to grow. In the default case, if the header has to grow
3198  * 32 bytes or less we avoid the reallocation.
3199  *
3200  * Unfortunately this headroom changes the DMA alignment of the resulting
3201  * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
3202  * on some architectures. An architecture can override this value,
3203  * perhaps setting it to a cacheline in size (since that will maintain
3204  * cacheline alignment of the DMA). It must be a power of 2.
3205  *
3206  * Various parts of the networking layer expect at least 32 bytes of
3207  * headroom, you should not reduce this.
3208  *
3209  * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
3210  * to reduce average number of cache lines per packet.
3211  * get_rps_cpu() for example only access one 64 bytes aligned block :
3212  * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
3213  */
3214 #ifndef NET_SKB_PAD
3215 #define NET_SKB_PAD	max(32, L1_CACHE_BYTES)
3216 #endif
3217 
3218 int ___pskb_trim(struct sk_buff *skb, unsigned int len);
3219 
__skb_set_length(struct sk_buff * skb,unsigned int len)3220 static inline void __skb_set_length(struct sk_buff *skb, unsigned int len)
3221 {
3222 	if (WARN_ON(skb_is_nonlinear(skb)))
3223 		return;
3224 	skb->len = len;
3225 	skb_set_tail_pointer(skb, len);
3226 }
3227 
__skb_trim(struct sk_buff * skb,unsigned int len)3228 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
3229 {
3230 	__skb_set_length(skb, len);
3231 }
3232 
3233 void skb_trim(struct sk_buff *skb, unsigned int len);
3234 
__pskb_trim(struct sk_buff * skb,unsigned int len)3235 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
3236 {
3237 	if (skb->data_len)
3238 		return ___pskb_trim(skb, len);
3239 	__skb_trim(skb, len);
3240 	return 0;
3241 }
3242 
pskb_trim(struct sk_buff * skb,unsigned int len)3243 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
3244 {
3245 	return (len < skb->len) ? __pskb_trim(skb, len) : 0;
3246 }
3247 
3248 /**
3249  *	pskb_trim_unique - remove end from a paged unique (not cloned) buffer
3250  *	@skb: buffer to alter
3251  *	@len: new length
3252  *
3253  *	This is identical to pskb_trim except that the caller knows that
3254  *	the skb is not cloned so we should never get an error due to out-
3255  *	of-memory.
3256  */
pskb_trim_unique(struct sk_buff * skb,unsigned int len)3257 static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
3258 {
3259 	int err = pskb_trim(skb, len);
3260 	BUG_ON(err);
3261 }
3262 
__skb_grow(struct sk_buff * skb,unsigned int len)3263 static inline int __skb_grow(struct sk_buff *skb, unsigned int len)
3264 {
3265 	unsigned int diff = len - skb->len;
3266 
3267 	if (skb_tailroom(skb) < diff) {
3268 		int ret = pskb_expand_head(skb, 0, diff - skb_tailroom(skb),
3269 					   GFP_ATOMIC);
3270 		if (ret)
3271 			return ret;
3272 	}
3273 	__skb_set_length(skb, len);
3274 	return 0;
3275 }
3276 
3277 /**
3278  *	skb_orphan - orphan a buffer
3279  *	@skb: buffer to orphan
3280  *
3281  *	If a buffer currently has an owner then we call the owner's
3282  *	destructor function and make the @skb unowned. The buffer continues
3283  *	to exist but is no longer charged to its former owner.
3284  */
skb_orphan(struct sk_buff * skb)3285 static inline void skb_orphan(struct sk_buff *skb)
3286 {
3287 	if (skb->destructor) {
3288 		skb->destructor(skb);
3289 		skb->destructor = NULL;
3290 		skb->sk		= NULL;
3291 	} else {
3292 		BUG_ON(skb->sk);
3293 	}
3294 }
3295 
3296 /**
3297  *	skb_orphan_frags - orphan the frags contained in a buffer
3298  *	@skb: buffer to orphan frags from
3299  *	@gfp_mask: allocation mask for replacement pages
3300  *
3301  *	For each frag in the SKB which needs a destructor (i.e. has an
3302  *	owner) create a copy of that frag and release the original
3303  *	page by calling the destructor.
3304  */
skb_orphan_frags(struct sk_buff * skb,gfp_t gfp_mask)3305 static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
3306 {
3307 	if (likely(!skb_zcopy(skb)))
3308 		return 0;
3309 	if (skb_shinfo(skb)->flags & SKBFL_DONT_ORPHAN)
3310 		return 0;
3311 	return skb_copy_ubufs(skb, gfp_mask);
3312 }
3313 
3314 /* Frags must be orphaned, even if refcounted, if skb might loop to rx path */
skb_orphan_frags_rx(struct sk_buff * skb,gfp_t gfp_mask)3315 static inline int skb_orphan_frags_rx(struct sk_buff *skb, gfp_t gfp_mask)
3316 {
3317 	if (likely(!skb_zcopy(skb)))
3318 		return 0;
3319 	return skb_copy_ubufs(skb, gfp_mask);
3320 }
3321 
3322 /**
3323  *	__skb_queue_purge_reason - empty a list
3324  *	@list: list to empty
3325  *	@reason: drop reason
3326  *
3327  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
3328  *	the list and one reference dropped. This function does not take the
3329  *	list lock and the caller must hold the relevant locks to use it.
3330  */
__skb_queue_purge_reason(struct sk_buff_head * list,enum skb_drop_reason reason)3331 static inline void __skb_queue_purge_reason(struct sk_buff_head *list,
3332 					    enum skb_drop_reason reason)
3333 {
3334 	struct sk_buff *skb;
3335 
3336 	while ((skb = __skb_dequeue(list)) != NULL)
3337 		kfree_skb_reason(skb, reason);
3338 }
3339 
__skb_queue_purge(struct sk_buff_head * list)3340 static inline void __skb_queue_purge(struct sk_buff_head *list)
3341 {
3342 	__skb_queue_purge_reason(list, SKB_DROP_REASON_QUEUE_PURGE);
3343 }
3344 
3345 void skb_queue_purge_reason(struct sk_buff_head *list,
3346 			    enum skb_drop_reason reason);
3347 
skb_queue_purge(struct sk_buff_head * list)3348 static inline void skb_queue_purge(struct sk_buff_head *list)
3349 {
3350 	skb_queue_purge_reason(list, SKB_DROP_REASON_QUEUE_PURGE);
3351 }
3352 
3353 unsigned int skb_rbtree_purge(struct rb_root *root);
3354 void skb_errqueue_purge(struct sk_buff_head *list);
3355 
3356 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask);
3357 
3358 /**
3359  * netdev_alloc_frag - allocate a page fragment
3360  * @fragsz: fragment size
3361  *
3362  * Allocates a frag from a page for receive buffer.
3363  * Uses GFP_ATOMIC allocations.
3364  */
netdev_alloc_frag(unsigned int fragsz)3365 static inline void *netdev_alloc_frag(unsigned int fragsz)
3366 {
3367 	return __netdev_alloc_frag_align(fragsz, ~0u);
3368 }
3369 
netdev_alloc_frag_align(unsigned int fragsz,unsigned int align)3370 static inline void *netdev_alloc_frag_align(unsigned int fragsz,
3371 					    unsigned int align)
3372 {
3373 	WARN_ON_ONCE(!is_power_of_2(align));
3374 	return __netdev_alloc_frag_align(fragsz, -align);
3375 }
3376 
3377 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
3378 				   gfp_t gfp_mask);
3379 
3380 /**
3381  *	netdev_alloc_skb - allocate an skbuff for rx on a specific device
3382  *	@dev: network device to receive on
3383  *	@length: length to allocate
3384  *
3385  *	Allocate a new &sk_buff and assign it a usage count of one. The
3386  *	buffer has unspecified headroom built in. Users should allocate
3387  *	the headroom they think they need without accounting for the
3388  *	built in space. The built in space is used for optimisations.
3389  *
3390  *	%NULL is returned if there is no free memory. Although this function
3391  *	allocates memory it can be called from an interrupt.
3392  */
netdev_alloc_skb(struct net_device * dev,unsigned int length)3393 static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
3394 					       unsigned int length)
3395 {
3396 	return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
3397 }
3398 
3399 /* legacy helper around __netdev_alloc_skb() */
__dev_alloc_skb(unsigned int length,gfp_t gfp_mask)3400 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
3401 					      gfp_t gfp_mask)
3402 {
3403 	return __netdev_alloc_skb(NULL, length, gfp_mask);
3404 }
3405 
3406 /* legacy helper around netdev_alloc_skb() */
dev_alloc_skb(unsigned int length)3407 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
3408 {
3409 	return netdev_alloc_skb(NULL, length);
3410 }
3411 
3412 
__netdev_alloc_skb_ip_align(struct net_device * dev,unsigned int length,gfp_t gfp)3413 static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
3414 		unsigned int length, gfp_t gfp)
3415 {
3416 	struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
3417 
3418 	if (NET_IP_ALIGN && skb)
3419 		skb_reserve(skb, NET_IP_ALIGN);
3420 	return skb;
3421 }
3422 
netdev_alloc_skb_ip_align(struct net_device * dev,unsigned int length)3423 static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
3424 		unsigned int length)
3425 {
3426 	return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
3427 }
3428 
skb_free_frag(void * addr)3429 static inline void skb_free_frag(void *addr)
3430 {
3431 	page_frag_free(addr);
3432 }
3433 
3434 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask);
3435 
napi_alloc_frag(unsigned int fragsz)3436 static inline void *napi_alloc_frag(unsigned int fragsz)
3437 {
3438 	return __napi_alloc_frag_align(fragsz, ~0u);
3439 }
3440 
napi_alloc_frag_align(unsigned int fragsz,unsigned int align)3441 static inline void *napi_alloc_frag_align(unsigned int fragsz,
3442 					  unsigned int align)
3443 {
3444 	WARN_ON_ONCE(!is_power_of_2(align));
3445 	return __napi_alloc_frag_align(fragsz, -align);
3446 }
3447 
3448 struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int length);
3449 void napi_consume_skb(struct sk_buff *skb, int budget);
3450 
3451 void napi_skb_free_stolen_head(struct sk_buff *skb);
3452 void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason);
3453 
3454 /**
3455  * __dev_alloc_pages - allocate page for network Rx
3456  * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
3457  * @order: size of the allocation
3458  *
3459  * Allocate a new page.
3460  *
3461  * %NULL is returned if there is no free memory.
3462 */
__dev_alloc_pages_noprof(gfp_t gfp_mask,unsigned int order)3463 static inline struct page *__dev_alloc_pages_noprof(gfp_t gfp_mask,
3464 					     unsigned int order)
3465 {
3466 	/* This piece of code contains several assumptions.
3467 	 * 1.  This is for device Rx, therefore a cold page is preferred.
3468 	 * 2.  The expectation is the user wants a compound page.
3469 	 * 3.  If requesting a order 0 page it will not be compound
3470 	 *     due to the check to see if order has a value in prep_new_page
3471 	 * 4.  __GFP_MEMALLOC is ignored if __GFP_NOMEMALLOC is set due to
3472 	 *     code in gfp_to_alloc_flags that should be enforcing this.
3473 	 */
3474 	gfp_mask |= __GFP_COMP | __GFP_MEMALLOC;
3475 
3476 	return alloc_pages_node_noprof(NUMA_NO_NODE, gfp_mask, order);
3477 }
3478 #define __dev_alloc_pages(...)	alloc_hooks(__dev_alloc_pages_noprof(__VA_ARGS__))
3479 
3480 /*
3481  * This specialized allocator has to be a macro for its allocations to be
3482  * accounted separately (to have a separate alloc_tag).
3483  */
3484 #define dev_alloc_pages(_order) __dev_alloc_pages(GFP_ATOMIC | __GFP_NOWARN, _order)
3485 
3486 /**
3487  * __dev_alloc_page - allocate a page for network Rx
3488  * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
3489  *
3490  * Allocate a new page.
3491  *
3492  * %NULL is returned if there is no free memory.
3493  */
__dev_alloc_page_noprof(gfp_t gfp_mask)3494 static inline struct page *__dev_alloc_page_noprof(gfp_t gfp_mask)
3495 {
3496 	return __dev_alloc_pages_noprof(gfp_mask, 0);
3497 }
3498 #define __dev_alloc_page(...)	alloc_hooks(__dev_alloc_page_noprof(__VA_ARGS__))
3499 
3500 /*
3501  * This specialized allocator has to be a macro for its allocations to be
3502  * accounted separately (to have a separate alloc_tag).
3503  */
3504 #define dev_alloc_page()	dev_alloc_pages(0)
3505 
3506 /**
3507  * dev_page_is_reusable - check whether a page can be reused for network Rx
3508  * @page: the page to test
3509  *
3510  * A page shouldn't be considered for reusing/recycling if it was allocated
3511  * under memory pressure or at a distant memory node.
3512  *
3513  * Returns false if this page should be returned to page allocator, true
3514  * otherwise.
3515  */
dev_page_is_reusable(const struct page * page)3516 static inline bool dev_page_is_reusable(const struct page *page)
3517 {
3518 	return likely(page_to_nid(page) == numa_mem_id() &&
3519 		      !page_is_pfmemalloc(page));
3520 }
3521 
3522 /**
3523  *	skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page
3524  *	@page: The page that was allocated from skb_alloc_page
3525  *	@skb: The skb that may need pfmemalloc set
3526  */
skb_propagate_pfmemalloc(const struct page * page,struct sk_buff * skb)3527 static inline void skb_propagate_pfmemalloc(const struct page *page,
3528 					    struct sk_buff *skb)
3529 {
3530 	if (page_is_pfmemalloc(page))
3531 		skb->pfmemalloc = true;
3532 }
3533 
3534 /**
3535  * skb_frag_off() - Returns the offset of a skb fragment
3536  * @frag: the paged fragment
3537  */
skb_frag_off(const skb_frag_t * frag)3538 static inline unsigned int skb_frag_off(const skb_frag_t *frag)
3539 {
3540 	return frag->offset;
3541 }
3542 
3543 /**
3544  * skb_frag_off_add() - Increments the offset of a skb fragment by @delta
3545  * @frag: skb fragment
3546  * @delta: value to add
3547  */
skb_frag_off_add(skb_frag_t * frag,int delta)3548 static inline void skb_frag_off_add(skb_frag_t *frag, int delta)
3549 {
3550 	frag->offset += delta;
3551 }
3552 
3553 /**
3554  * skb_frag_off_set() - Sets the offset of a skb fragment
3555  * @frag: skb fragment
3556  * @offset: offset of fragment
3557  */
skb_frag_off_set(skb_frag_t * frag,unsigned int offset)3558 static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset)
3559 {
3560 	frag->offset = offset;
3561 }
3562 
3563 /**
3564  * skb_frag_off_copy() - Sets the offset of a skb fragment from another fragment
3565  * @fragto: skb fragment where offset is set
3566  * @fragfrom: skb fragment offset is copied from
3567  */
skb_frag_off_copy(skb_frag_t * fragto,const skb_frag_t * fragfrom)3568 static inline void skb_frag_off_copy(skb_frag_t *fragto,
3569 				     const skb_frag_t *fragfrom)
3570 {
3571 	fragto->offset = fragfrom->offset;
3572 }
3573 
3574 /* Return: true if the skb_frag contains a net_iov. */
skb_frag_is_net_iov(const skb_frag_t * frag)3575 static inline bool skb_frag_is_net_iov(const skb_frag_t *frag)
3576 {
3577 	return netmem_is_net_iov(frag->netmem);
3578 }
3579 
3580 /**
3581  * skb_frag_net_iov - retrieve the net_iov referred to by fragment
3582  * @frag: the fragment
3583  *
3584  * Return: the &struct net_iov associated with @frag. Returns NULL if this
3585  * frag has no associated net_iov.
3586  */
skb_frag_net_iov(const skb_frag_t * frag)3587 static inline struct net_iov *skb_frag_net_iov(const skb_frag_t *frag)
3588 {
3589 	if (!skb_frag_is_net_iov(frag))
3590 		return NULL;
3591 
3592 	return netmem_to_net_iov(frag->netmem);
3593 }
3594 
3595 /**
3596  * skb_frag_page - retrieve the page referred to by a paged fragment
3597  * @frag: the paged fragment
3598  *
3599  * Return: the &struct page associated with @frag. Returns NULL if this frag
3600  * has no associated page.
3601  */
skb_frag_page(const skb_frag_t * frag)3602 static inline struct page *skb_frag_page(const skb_frag_t *frag)
3603 {
3604 	if (skb_frag_is_net_iov(frag))
3605 		return NULL;
3606 
3607 	return netmem_to_page(frag->netmem);
3608 }
3609 
3610 /**
3611  * skb_frag_netmem - retrieve the netmem referred to by a fragment
3612  * @frag: the fragment
3613  *
3614  * Return: the &netmem_ref associated with @frag.
3615  */
skb_frag_netmem(const skb_frag_t * frag)3616 static inline netmem_ref skb_frag_netmem(const skb_frag_t *frag)
3617 {
3618 	return frag->netmem;
3619 }
3620 
3621 int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,
3622 		    unsigned int headroom);
3623 int skb_cow_data_for_xdp(struct page_pool *pool, struct sk_buff **pskb,
3624 			 struct bpf_prog *prog);
3625 
3626 /**
3627  * skb_frag_address - gets the address of the data contained in a paged fragment
3628  * @frag: the paged fragment buffer
3629  *
3630  * Returns the address of the data within @frag. The page must already
3631  * be mapped.
3632  */
skb_frag_address(const skb_frag_t * frag)3633 static inline void *skb_frag_address(const skb_frag_t *frag)
3634 {
3635 	if (!skb_frag_page(frag))
3636 		return NULL;
3637 
3638 	return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
3639 }
3640 
3641 /**
3642  * skb_frag_address_safe - gets the address of the data contained in a paged fragment
3643  * @frag: the paged fragment buffer
3644  *
3645  * Returns the address of the data within @frag. Checks that the page
3646  * is mapped and returns %NULL otherwise.
3647  */
skb_frag_address_safe(const skb_frag_t * frag)3648 static inline void *skb_frag_address_safe(const skb_frag_t *frag)
3649 {
3650 	struct page *page = skb_frag_page(frag);
3651 	void *ptr;
3652 
3653 	if (!page)
3654 		return NULL;
3655 
3656 	ptr = page_address(page);
3657 	if (unlikely(!ptr))
3658 		return NULL;
3659 
3660 	return ptr + skb_frag_off(frag);
3661 }
3662 
3663 /**
3664  * skb_frag_page_copy() - sets the page in a fragment from another fragment
3665  * @fragto: skb fragment where page is set
3666  * @fragfrom: skb fragment page is copied from
3667  */
skb_frag_page_copy(skb_frag_t * fragto,const skb_frag_t * fragfrom)3668 static inline void skb_frag_page_copy(skb_frag_t *fragto,
3669 				      const skb_frag_t *fragfrom)
3670 {
3671 	fragto->netmem = fragfrom->netmem;
3672 }
3673 
3674 bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
3675 
3676 /**
3677  * skb_frag_dma_map - maps a paged fragment via the DMA API
3678  * @dev: the device to map the fragment to
3679  * @frag: the paged fragment to map
3680  * @offset: the offset within the fragment (starting at the
3681  *          fragment's own offset)
3682  * @size: the number of bytes to map
3683  * @dir: the direction of the mapping (``PCI_DMA_*``)
3684  *
3685  * Maps the page associated with @frag to @device.
3686  */
skb_frag_dma_map(struct device * dev,const skb_frag_t * frag,size_t offset,size_t size,enum dma_data_direction dir)3687 static inline dma_addr_t skb_frag_dma_map(struct device *dev,
3688 					  const skb_frag_t *frag,
3689 					  size_t offset, size_t size,
3690 					  enum dma_data_direction dir)
3691 {
3692 	return dma_map_page(dev, skb_frag_page(frag),
3693 			    skb_frag_off(frag) + offset, size, dir);
3694 }
3695 
pskb_copy(struct sk_buff * skb,gfp_t gfp_mask)3696 static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
3697 					gfp_t gfp_mask)
3698 {
3699 	return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
3700 }
3701 
3702 
pskb_copy_for_clone(struct sk_buff * skb,gfp_t gfp_mask)3703 static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb,
3704 						  gfp_t gfp_mask)
3705 {
3706 	return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true);
3707 }
3708 
3709 
3710 /**
3711  *	skb_clone_writable - is the header of a clone writable
3712  *	@skb: buffer to check
3713  *	@len: length up to which to write
3714  *
3715  *	Returns true if modifying the header part of the cloned buffer
3716  *	does not requires the data to be copied.
3717  */
skb_clone_writable(const struct sk_buff * skb,unsigned int len)3718 static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
3719 {
3720 	return !skb_header_cloned(skb) &&
3721 	       skb_headroom(skb) + len <= skb->hdr_len;
3722 }
3723 
skb_try_make_writable(struct sk_buff * skb,unsigned int write_len)3724 static inline int skb_try_make_writable(struct sk_buff *skb,
3725 					unsigned int write_len)
3726 {
3727 	return skb_cloned(skb) && !skb_clone_writable(skb, write_len) &&
3728 	       pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3729 }
3730 
__skb_cow(struct sk_buff * skb,unsigned int headroom,int cloned)3731 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
3732 			    int cloned)
3733 {
3734 	int delta = 0;
3735 
3736 	if (headroom > skb_headroom(skb))
3737 		delta = headroom - skb_headroom(skb);
3738 
3739 	if (delta || cloned)
3740 		return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
3741 					GFP_ATOMIC);
3742 	return 0;
3743 }
3744 
3745 /**
3746  *	skb_cow - copy header of skb when it is required
3747  *	@skb: buffer to cow
3748  *	@headroom: needed headroom
3749  *
3750  *	If the skb passed lacks sufficient headroom or its data part
3751  *	is shared, data is reallocated. If reallocation fails, an error
3752  *	is returned and original skb is not changed.
3753  *
3754  *	The result is skb with writable area skb->head...skb->tail
3755  *	and at least @headroom of space at head.
3756  */
skb_cow(struct sk_buff * skb,unsigned int headroom)3757 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
3758 {
3759 	return __skb_cow(skb, headroom, skb_cloned(skb));
3760 }
3761 
3762 /**
3763  *	skb_cow_head - skb_cow but only making the head writable
3764  *	@skb: buffer to cow
3765  *	@headroom: needed headroom
3766  *
3767  *	This function is identical to skb_cow except that we replace the
3768  *	skb_cloned check by skb_header_cloned.  It should be used when
3769  *	you only need to push on some header and do not need to modify
3770  *	the data.
3771  */
skb_cow_head(struct sk_buff * skb,unsigned int headroom)3772 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
3773 {
3774 	return __skb_cow(skb, headroom, skb_header_cloned(skb));
3775 }
3776 
3777 /**
3778  *	skb_padto	- pad an skbuff up to a minimal size
3779  *	@skb: buffer to pad
3780  *	@len: minimal length
3781  *
3782  *	Pads up a buffer to ensure the trailing bytes exist and are
3783  *	blanked. If the buffer already contains sufficient data it
3784  *	is untouched. Otherwise it is extended. Returns zero on
3785  *	success. The skb is freed on error.
3786  */
skb_padto(struct sk_buff * skb,unsigned int len)3787 static inline int skb_padto(struct sk_buff *skb, unsigned int len)
3788 {
3789 	unsigned int size = skb->len;
3790 	if (likely(size >= len))
3791 		return 0;
3792 	return skb_pad(skb, len - size);
3793 }
3794 
3795 /**
3796  *	__skb_put_padto - increase size and pad an skbuff up to a minimal size
3797  *	@skb: buffer to pad
3798  *	@len: minimal length
3799  *	@free_on_error: free buffer on error
3800  *
3801  *	Pads up a buffer to ensure the trailing bytes exist and are
3802  *	blanked. If the buffer already contains sufficient data it
3803  *	is untouched. Otherwise it is extended. Returns zero on
3804  *	success. The skb is freed on error if @free_on_error is true.
3805  */
__skb_put_padto(struct sk_buff * skb,unsigned int len,bool free_on_error)3806 static inline int __must_check __skb_put_padto(struct sk_buff *skb,
3807 					       unsigned int len,
3808 					       bool free_on_error)
3809 {
3810 	unsigned int size = skb->len;
3811 
3812 	if (unlikely(size < len)) {
3813 		len -= size;
3814 		if (__skb_pad(skb, len, free_on_error))
3815 			return -ENOMEM;
3816 		__skb_put(skb, len);
3817 	}
3818 	return 0;
3819 }
3820 
3821 /**
3822  *	skb_put_padto - increase size and pad an skbuff up to a minimal size
3823  *	@skb: buffer to pad
3824  *	@len: minimal length
3825  *
3826  *	Pads up a buffer to ensure the trailing bytes exist and are
3827  *	blanked. If the buffer already contains sufficient data it
3828  *	is untouched. Otherwise it is extended. Returns zero on
3829  *	success. The skb is freed on error.
3830  */
skb_put_padto(struct sk_buff * skb,unsigned int len)3831 static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int len)
3832 {
3833 	return __skb_put_padto(skb, len, true);
3834 }
3835 
3836 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i)
3837 	__must_check;
3838 
skb_add_data(struct sk_buff * skb,struct iov_iter * from,int copy)3839 static inline int skb_add_data(struct sk_buff *skb,
3840 			       struct iov_iter *from, int copy)
3841 {
3842 	const int off = skb->len;
3843 
3844 	if (skb->ip_summed == CHECKSUM_NONE) {
3845 		__wsum csum = 0;
3846 		if (csum_and_copy_from_iter_full(skb_put(skb, copy), copy,
3847 					         &csum, from)) {
3848 			skb->csum = csum_block_add(skb->csum, csum, off);
3849 			return 0;
3850 		}
3851 	} else if (copy_from_iter_full(skb_put(skb, copy), copy, from))
3852 		return 0;
3853 
3854 	__skb_trim(skb, off);
3855 	return -EFAULT;
3856 }
3857 
skb_can_coalesce(struct sk_buff * skb,int i,const struct page * page,int off)3858 static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
3859 				    const struct page *page, int off)
3860 {
3861 	if (skb_zcopy(skb))
3862 		return false;
3863 	if (i) {
3864 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
3865 
3866 		return page == skb_frag_page(frag) &&
3867 		       off == skb_frag_off(frag) + skb_frag_size(frag);
3868 	}
3869 	return false;
3870 }
3871 
__skb_linearize(struct sk_buff * skb)3872 static inline int __skb_linearize(struct sk_buff *skb)
3873 {
3874 	return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
3875 }
3876 
3877 /**
3878  *	skb_linearize - convert paged skb to linear one
3879  *	@skb: buffer to linarize
3880  *
3881  *	If there is no free memory -ENOMEM is returned, otherwise zero
3882  *	is returned and the old skb data released.
3883  */
skb_linearize(struct sk_buff * skb)3884 static inline int skb_linearize(struct sk_buff *skb)
3885 {
3886 	return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
3887 }
3888 
3889 /**
3890  * skb_has_shared_frag - can any frag be overwritten
3891  * @skb: buffer to test
3892  *
3893  * Return true if the skb has at least one frag that might be modified
3894  * by an external entity (as in vmsplice()/sendfile())
3895  */
skb_has_shared_frag(const struct sk_buff * skb)3896 static inline bool skb_has_shared_frag(const struct sk_buff *skb)
3897 {
3898 	return skb_is_nonlinear(skb) &&
3899 	       skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
3900 }
3901 
3902 /**
3903  *	skb_linearize_cow - make sure skb is linear and writable
3904  *	@skb: buffer to process
3905  *
3906  *	If there is no free memory -ENOMEM is returned, otherwise zero
3907  *	is returned and the old skb data released.
3908  */
skb_linearize_cow(struct sk_buff * skb)3909 static inline int skb_linearize_cow(struct sk_buff *skb)
3910 {
3911 	return skb_is_nonlinear(skb) || skb_cloned(skb) ?
3912 	       __skb_linearize(skb) : 0;
3913 }
3914 
3915 static __always_inline void
__skb_postpull_rcsum(struct sk_buff * skb,const void * start,unsigned int len,unsigned int off)3916 __skb_postpull_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
3917 		     unsigned int off)
3918 {
3919 	if (skb->ip_summed == CHECKSUM_COMPLETE)
3920 		skb->csum = csum_block_sub(skb->csum,
3921 					   csum_partial(start, len, 0), off);
3922 	else if (skb->ip_summed == CHECKSUM_PARTIAL &&
3923 		 skb_checksum_start_offset(skb) < 0)
3924 		skb->ip_summed = CHECKSUM_NONE;
3925 }
3926 
3927 /**
3928  *	skb_postpull_rcsum - update checksum for received skb after pull
3929  *	@skb: buffer to update
3930  *	@start: start of data before pull
3931  *	@len: length of data pulled
3932  *
3933  *	After doing a pull on a received packet, you need to call this to
3934  *	update the CHECKSUM_COMPLETE checksum, or set ip_summed to
3935  *	CHECKSUM_NONE so that it can be recomputed from scratch.
3936  */
skb_postpull_rcsum(struct sk_buff * skb,const void * start,unsigned int len)3937 static inline void skb_postpull_rcsum(struct sk_buff *skb,
3938 				      const void *start, unsigned int len)
3939 {
3940 	if (skb->ip_summed == CHECKSUM_COMPLETE)
3941 		skb->csum = wsum_negate(csum_partial(start, len,
3942 						     wsum_negate(skb->csum)));
3943 	else if (skb->ip_summed == CHECKSUM_PARTIAL &&
3944 		 skb_checksum_start_offset(skb) < 0)
3945 		skb->ip_summed = CHECKSUM_NONE;
3946 }
3947 
3948 static __always_inline void
__skb_postpush_rcsum(struct sk_buff * skb,const void * start,unsigned int len,unsigned int off)3949 __skb_postpush_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
3950 		     unsigned int off)
3951 {
3952 	if (skb->ip_summed == CHECKSUM_COMPLETE)
3953 		skb->csum = csum_block_add(skb->csum,
3954 					   csum_partial(start, len, 0), off);
3955 }
3956 
3957 /**
3958  *	skb_postpush_rcsum - update checksum for received skb after push
3959  *	@skb: buffer to update
3960  *	@start: start of data after push
3961  *	@len: length of data pushed
3962  *
3963  *	After doing a push on a received packet, you need to call this to
3964  *	update the CHECKSUM_COMPLETE checksum.
3965  */
skb_postpush_rcsum(struct sk_buff * skb,const void * start,unsigned int len)3966 static inline void skb_postpush_rcsum(struct sk_buff *skb,
3967 				      const void *start, unsigned int len)
3968 {
3969 	__skb_postpush_rcsum(skb, start, len, 0);
3970 }
3971 
3972 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
3973 
3974 /**
3975  *	skb_push_rcsum - push skb and update receive checksum
3976  *	@skb: buffer to update
3977  *	@len: length of data pulled
3978  *
3979  *	This function performs an skb_push on the packet and updates
3980  *	the CHECKSUM_COMPLETE checksum.  It should be used on
3981  *	receive path processing instead of skb_push unless you know
3982  *	that the checksum difference is zero (e.g., a valid IP header)
3983  *	or you are setting ip_summed to CHECKSUM_NONE.
3984  */
skb_push_rcsum(struct sk_buff * skb,unsigned int len)3985 static inline void *skb_push_rcsum(struct sk_buff *skb, unsigned int len)
3986 {
3987 	skb_push(skb, len);
3988 	skb_postpush_rcsum(skb, skb->data, len);
3989 	return skb->data;
3990 }
3991 
3992 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len);
3993 /**
3994  *	pskb_trim_rcsum - trim received skb and update checksum
3995  *	@skb: buffer to trim
3996  *	@len: new length
3997  *
3998  *	This is exactly the same as pskb_trim except that it ensures the
3999  *	checksum of received packets are still valid after the operation.
4000  *	It can change skb pointers.
4001  */
4002 
pskb_trim_rcsum(struct sk_buff * skb,unsigned int len)4003 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
4004 {
4005 	if (likely(len >= skb->len))
4006 		return 0;
4007 	return pskb_trim_rcsum_slow(skb, len);
4008 }
4009 
__skb_trim_rcsum(struct sk_buff * skb,unsigned int len)4010 static inline int __skb_trim_rcsum(struct sk_buff *skb, unsigned int len)
4011 {
4012 	if (skb->ip_summed == CHECKSUM_COMPLETE)
4013 		skb->ip_summed = CHECKSUM_NONE;
4014 	__skb_trim(skb, len);
4015 	return 0;
4016 }
4017 
__skb_grow_rcsum(struct sk_buff * skb,unsigned int len)4018 static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len)
4019 {
4020 	if (skb->ip_summed == CHECKSUM_COMPLETE)
4021 		skb->ip_summed = CHECKSUM_NONE;
4022 	return __skb_grow(skb, len);
4023 }
4024 
4025 #define rb_to_skb(rb) rb_entry_safe(rb, struct sk_buff, rbnode)
4026 #define skb_rb_first(root) rb_to_skb(rb_first(root))
4027 #define skb_rb_last(root)  rb_to_skb(rb_last(root))
4028 #define skb_rb_next(skb)   rb_to_skb(rb_next(&(skb)->rbnode))
4029 #define skb_rb_prev(skb)   rb_to_skb(rb_prev(&(skb)->rbnode))
4030 
4031 #define skb_queue_walk(queue, skb) \
4032 		for (skb = (queue)->next;					\
4033 		     skb != (struct sk_buff *)(queue);				\
4034 		     skb = skb->next)
4035 
4036 #define skb_queue_walk_safe(queue, skb, tmp)					\
4037 		for (skb = (queue)->next, tmp = skb->next;			\
4038 		     skb != (struct sk_buff *)(queue);				\
4039 		     skb = tmp, tmp = skb->next)
4040 
4041 #define skb_queue_walk_from(queue, skb)						\
4042 		for (; skb != (struct sk_buff *)(queue);			\
4043 		     skb = skb->next)
4044 
4045 #define skb_rbtree_walk(skb, root)						\
4046 		for (skb = skb_rb_first(root); skb != NULL;			\
4047 		     skb = skb_rb_next(skb))
4048 
4049 #define skb_rbtree_walk_from(skb)						\
4050 		for (; skb != NULL;						\
4051 		     skb = skb_rb_next(skb))
4052 
4053 #define skb_rbtree_walk_from_safe(skb, tmp)					\
4054 		for (; tmp = skb ? skb_rb_next(skb) : NULL, (skb != NULL);	\
4055 		     skb = tmp)
4056 
4057 #define skb_queue_walk_from_safe(queue, skb, tmp)				\
4058 		for (tmp = skb->next;						\
4059 		     skb != (struct sk_buff *)(queue);				\
4060 		     skb = tmp, tmp = skb->next)
4061 
4062 #define skb_queue_reverse_walk(queue, skb) \
4063 		for (skb = (queue)->prev;					\
4064 		     skb != (struct sk_buff *)(queue);				\
4065 		     skb = skb->prev)
4066 
4067 #define skb_queue_reverse_walk_safe(queue, skb, tmp)				\
4068 		for (skb = (queue)->prev, tmp = skb->prev;			\
4069 		     skb != (struct sk_buff *)(queue);				\
4070 		     skb = tmp, tmp = skb->prev)
4071 
4072 #define skb_queue_reverse_walk_from_safe(queue, skb, tmp)			\
4073 		for (tmp = skb->prev;						\
4074 		     skb != (struct sk_buff *)(queue);				\
4075 		     skb = tmp, tmp = skb->prev)
4076 
skb_has_frag_list(const struct sk_buff * skb)4077 static inline bool skb_has_frag_list(const struct sk_buff *skb)
4078 {
4079 	return skb_shinfo(skb)->frag_list != NULL;
4080 }
4081 
skb_frag_list_init(struct sk_buff * skb)4082 static inline void skb_frag_list_init(struct sk_buff *skb)
4083 {
4084 	skb_shinfo(skb)->frag_list = NULL;
4085 }
4086 
4087 #define skb_walk_frags(skb, iter)	\
4088 	for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
4089 
4090 
4091 int __skb_wait_for_more_packets(struct sock *sk, struct sk_buff_head *queue,
4092 				int *err, long *timeo_p,
4093 				const struct sk_buff *skb);
4094 struct sk_buff *__skb_try_recv_from_queue(struct sock *sk,
4095 					  struct sk_buff_head *queue,
4096 					  unsigned int flags,
4097 					  int *off, int *err,
4098 					  struct sk_buff **last);
4099 struct sk_buff *__skb_try_recv_datagram(struct sock *sk,
4100 					struct sk_buff_head *queue,
4101 					unsigned int flags, int *off, int *err,
4102 					struct sk_buff **last);
4103 struct sk_buff *__skb_recv_datagram(struct sock *sk,
4104 				    struct sk_buff_head *sk_queue,
4105 				    unsigned int flags, int *off, int *err);
4106 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags, int *err);
4107 __poll_t datagram_poll(struct file *file, struct socket *sock,
4108 			   struct poll_table_struct *wait);
4109 int skb_copy_datagram_iter(const struct sk_buff *from, int offset,
4110 			   struct iov_iter *to, int size);
skb_copy_datagram_msg(const struct sk_buff * from,int offset,struct msghdr * msg,int size)4111 static inline int skb_copy_datagram_msg(const struct sk_buff *from, int offset,
4112 					struct msghdr *msg, int size)
4113 {
4114 	return skb_copy_datagram_iter(from, offset, &msg->msg_iter, size);
4115 }
4116 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, int hlen,
4117 				   struct msghdr *msg);
4118 int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
4119 			   struct iov_iter *to, int len,
4120 			   struct ahash_request *hash);
4121 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
4122 				 struct iov_iter *from, int len);
4123 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *frm);
4124 void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
4125 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags);
4126 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
4127 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len);
4128 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to,
4129 			      int len);
4130 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
4131 		    struct pipe_inode_info *pipe, unsigned int len,
4132 		    unsigned int flags);
4133 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
4134 			 int len);
4135 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len);
4136 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
4137 unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
4138 int skb_zerocopy(struct sk_buff *to, struct sk_buff *from,
4139 		 int len, int hlen);
4140 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len);
4141 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen);
4142 void skb_scrub_packet(struct sk_buff *skb, bool xnet);
4143 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features);
4144 struct sk_buff *skb_segment_list(struct sk_buff *skb, netdev_features_t features,
4145 				 unsigned int offset);
4146 struct sk_buff *skb_vlan_untag(struct sk_buff *skb);
4147 int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len);
4148 int skb_ensure_writable_head_tail(struct sk_buff *skb, struct net_device *dev);
4149 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci);
4150 int skb_vlan_pop(struct sk_buff *skb);
4151 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci);
4152 int skb_eth_pop(struct sk_buff *skb);
4153 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
4154 		 const unsigned char *src);
4155 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
4156 		  int mac_len, bool ethernet);
4157 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
4158 		 bool ethernet);
4159 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse);
4160 int skb_mpls_dec_ttl(struct sk_buff *skb);
4161 struct sk_buff *pskb_extract(struct sk_buff *skb, int off, int to_copy,
4162 			     gfp_t gfp);
4163 
memcpy_from_msg(void * data,struct msghdr * msg,int len)4164 static inline int memcpy_from_msg(void *data, struct msghdr *msg, int len)
4165 {
4166 	return copy_from_iter_full(data, len, &msg->msg_iter) ? 0 : -EFAULT;
4167 }
4168 
memcpy_to_msg(struct msghdr * msg,void * data,int len)4169 static inline int memcpy_to_msg(struct msghdr *msg, void *data, int len)
4170 {
4171 	return copy_to_iter(data, len, &msg->msg_iter) == len ? 0 : -EFAULT;
4172 }
4173 
4174 struct skb_checksum_ops {
4175 	__wsum (*update)(const void *mem, int len, __wsum wsum);
4176 	__wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len);
4177 };
4178 
4179 extern const struct skb_checksum_ops *crc32c_csum_stub __read_mostly;
4180 
4181 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
4182 		      __wsum csum, const struct skb_checksum_ops *ops);
4183 __wsum skb_checksum(const struct sk_buff *skb, int offset, int len,
4184 		    __wsum csum);
4185 
4186 static inline void * __must_check
__skb_header_pointer(const struct sk_buff * skb,int offset,int len,const void * data,int hlen,void * buffer)4187 __skb_header_pointer(const struct sk_buff *skb, int offset, int len,
4188 		     const void *data, int hlen, void *buffer)
4189 {
4190 	if (likely(hlen - offset >= len))
4191 		return (void *)data + offset;
4192 
4193 	if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0))
4194 		return NULL;
4195 
4196 	return buffer;
4197 }
4198 
4199 static inline void * __must_check
skb_header_pointer(const struct sk_buff * skb,int offset,int len,void * buffer)4200 skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
4201 {
4202 	return __skb_header_pointer(skb, offset, len, skb->data,
4203 				    skb_headlen(skb), buffer);
4204 }
4205 
4206 static inline void * __must_check
skb_pointer_if_linear(const struct sk_buff * skb,int offset,int len)4207 skb_pointer_if_linear(const struct sk_buff *skb, int offset, int len)
4208 {
4209 	if (likely(skb_headlen(skb) - offset >= len))
4210 		return skb->data + offset;
4211 	return NULL;
4212 }
4213 
4214 /**
4215  *	skb_needs_linearize - check if we need to linearize a given skb
4216  *			      depending on the given device features.
4217  *	@skb: socket buffer to check
4218  *	@features: net device features
4219  *
4220  *	Returns true if either:
4221  *	1. skb has frag_list and the device doesn't support FRAGLIST, or
4222  *	2. skb is fragmented and the device does not support SG.
4223  */
skb_needs_linearize(struct sk_buff * skb,netdev_features_t features)4224 static inline bool skb_needs_linearize(struct sk_buff *skb,
4225 				       netdev_features_t features)
4226 {
4227 	return skb_is_nonlinear(skb) &&
4228 	       ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) ||
4229 		(skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG)));
4230 }
4231 
skb_copy_from_linear_data(const struct sk_buff * skb,void * to,const unsigned int len)4232 static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
4233 					     void *to,
4234 					     const unsigned int len)
4235 {
4236 	memcpy(to, skb->data, len);
4237 }
4238 
skb_copy_from_linear_data_offset(const struct sk_buff * skb,const int offset,void * to,const unsigned int len)4239 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
4240 						    const int offset, void *to,
4241 						    const unsigned int len)
4242 {
4243 	memcpy(to, skb->data + offset, len);
4244 }
4245 
skb_copy_to_linear_data(struct sk_buff * skb,const void * from,const unsigned int len)4246 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
4247 					   const void *from,
4248 					   const unsigned int len)
4249 {
4250 	memcpy(skb->data, from, len);
4251 }
4252 
skb_copy_to_linear_data_offset(struct sk_buff * skb,const int offset,const void * from,const unsigned int len)4253 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
4254 						  const int offset,
4255 						  const void *from,
4256 						  const unsigned int len)
4257 {
4258 	memcpy(skb->data + offset, from, len);
4259 }
4260 
4261 void skb_init(void);
4262 
skb_get_ktime(const struct sk_buff * skb)4263 static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
4264 {
4265 	return skb->tstamp;
4266 }
4267 
4268 /**
4269  *	skb_get_timestamp - get timestamp from a skb
4270  *	@skb: skb to get stamp from
4271  *	@stamp: pointer to struct __kernel_old_timeval to store stamp in
4272  *
4273  *	Timestamps are stored in the skb as offsets to a base timestamp.
4274  *	This function converts the offset back to a struct timeval and stores
4275  *	it in stamp.
4276  */
skb_get_timestamp(const struct sk_buff * skb,struct __kernel_old_timeval * stamp)4277 static inline void skb_get_timestamp(const struct sk_buff *skb,
4278 				     struct __kernel_old_timeval *stamp)
4279 {
4280 	*stamp = ns_to_kernel_old_timeval(skb->tstamp);
4281 }
4282 
skb_get_new_timestamp(const struct sk_buff * skb,struct __kernel_sock_timeval * stamp)4283 static inline void skb_get_new_timestamp(const struct sk_buff *skb,
4284 					 struct __kernel_sock_timeval *stamp)
4285 {
4286 	struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4287 
4288 	stamp->tv_sec = ts.tv_sec;
4289 	stamp->tv_usec = ts.tv_nsec / 1000;
4290 }
4291 
skb_get_timestampns(const struct sk_buff * skb,struct __kernel_old_timespec * stamp)4292 static inline void skb_get_timestampns(const struct sk_buff *skb,
4293 				       struct __kernel_old_timespec *stamp)
4294 {
4295 	struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4296 
4297 	stamp->tv_sec = ts.tv_sec;
4298 	stamp->tv_nsec = ts.tv_nsec;
4299 }
4300 
skb_get_new_timestampns(const struct sk_buff * skb,struct __kernel_timespec * stamp)4301 static inline void skb_get_new_timestampns(const struct sk_buff *skb,
4302 					   struct __kernel_timespec *stamp)
4303 {
4304 	struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4305 
4306 	stamp->tv_sec = ts.tv_sec;
4307 	stamp->tv_nsec = ts.tv_nsec;
4308 }
4309 
__net_timestamp(struct sk_buff * skb)4310 static inline void __net_timestamp(struct sk_buff *skb)
4311 {
4312 	skb->tstamp = ktime_get_real();
4313 	skb->tstamp_type = SKB_CLOCK_REALTIME;
4314 }
4315 
net_timedelta(ktime_t t)4316 static inline ktime_t net_timedelta(ktime_t t)
4317 {
4318 	return ktime_sub(ktime_get_real(), t);
4319 }
4320 
skb_set_delivery_time(struct sk_buff * skb,ktime_t kt,u8 tstamp_type)4321 static inline void skb_set_delivery_time(struct sk_buff *skb, ktime_t kt,
4322 					 u8 tstamp_type)
4323 {
4324 	skb->tstamp = kt;
4325 
4326 	if (kt)
4327 		skb->tstamp_type = tstamp_type;
4328 	else
4329 		skb->tstamp_type = SKB_CLOCK_REALTIME;
4330 }
4331 
skb_set_delivery_type_by_clockid(struct sk_buff * skb,ktime_t kt,clockid_t clockid)4332 static inline void skb_set_delivery_type_by_clockid(struct sk_buff *skb,
4333 						    ktime_t kt, clockid_t clockid)
4334 {
4335 	u8 tstamp_type = SKB_CLOCK_REALTIME;
4336 
4337 	switch (clockid) {
4338 	case CLOCK_REALTIME:
4339 		break;
4340 	case CLOCK_MONOTONIC:
4341 		tstamp_type = SKB_CLOCK_MONOTONIC;
4342 		break;
4343 	case CLOCK_TAI:
4344 		tstamp_type = SKB_CLOCK_TAI;
4345 		break;
4346 	default:
4347 		WARN_ON_ONCE(1);
4348 		kt = 0;
4349 	}
4350 
4351 	skb_set_delivery_time(skb, kt, tstamp_type);
4352 }
4353 
4354 DECLARE_STATIC_KEY_FALSE(netstamp_needed_key);
4355 
4356 /* It is used in the ingress path to clear the delivery_time.
4357  * If needed, set the skb->tstamp to the (rcv) timestamp.
4358  */
skb_clear_delivery_time(struct sk_buff * skb)4359 static inline void skb_clear_delivery_time(struct sk_buff *skb)
4360 {
4361 	if (skb->tstamp_type) {
4362 		skb->tstamp_type = SKB_CLOCK_REALTIME;
4363 		if (static_branch_unlikely(&netstamp_needed_key))
4364 			skb->tstamp = ktime_get_real();
4365 		else
4366 			skb->tstamp = 0;
4367 	}
4368 }
4369 
skb_clear_tstamp(struct sk_buff * skb)4370 static inline void skb_clear_tstamp(struct sk_buff *skb)
4371 {
4372 	if (skb->tstamp_type)
4373 		return;
4374 
4375 	skb->tstamp = 0;
4376 }
4377 
skb_tstamp(const struct sk_buff * skb)4378 static inline ktime_t skb_tstamp(const struct sk_buff *skb)
4379 {
4380 	if (skb->tstamp_type)
4381 		return 0;
4382 
4383 	return skb->tstamp;
4384 }
4385 
skb_tstamp_cond(const struct sk_buff * skb,bool cond)4386 static inline ktime_t skb_tstamp_cond(const struct sk_buff *skb, bool cond)
4387 {
4388 	if (skb->tstamp_type != SKB_CLOCK_MONOTONIC && skb->tstamp)
4389 		return skb->tstamp;
4390 
4391 	if (static_branch_unlikely(&netstamp_needed_key) || cond)
4392 		return ktime_get_real();
4393 
4394 	return 0;
4395 }
4396 
skb_metadata_len(const struct sk_buff * skb)4397 static inline u8 skb_metadata_len(const struct sk_buff *skb)
4398 {
4399 	return skb_shinfo(skb)->meta_len;
4400 }
4401 
skb_metadata_end(const struct sk_buff * skb)4402 static inline void *skb_metadata_end(const struct sk_buff *skb)
4403 {
4404 	return skb_mac_header(skb);
4405 }
4406 
__skb_metadata_differs(const struct sk_buff * skb_a,const struct sk_buff * skb_b,u8 meta_len)4407 static inline bool __skb_metadata_differs(const struct sk_buff *skb_a,
4408 					  const struct sk_buff *skb_b,
4409 					  u8 meta_len)
4410 {
4411 	const void *a = skb_metadata_end(skb_a);
4412 	const void *b = skb_metadata_end(skb_b);
4413 	u64 diffs = 0;
4414 
4415 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
4416 	    BITS_PER_LONG != 64)
4417 		goto slow;
4418 
4419 	/* Using more efficient variant than plain call to memcmp(). */
4420 	switch (meta_len) {
4421 #define __it(x, op) (x -= sizeof(u##op))
4422 #define __it_diff(a, b, op) (*(u##op *)__it(a, op)) ^ (*(u##op *)__it(b, op))
4423 	case 32: diffs |= __it_diff(a, b, 64);
4424 		fallthrough;
4425 	case 24: diffs |= __it_diff(a, b, 64);
4426 		fallthrough;
4427 	case 16: diffs |= __it_diff(a, b, 64);
4428 		fallthrough;
4429 	case  8: diffs |= __it_diff(a, b, 64);
4430 		break;
4431 	case 28: diffs |= __it_diff(a, b, 64);
4432 		fallthrough;
4433 	case 20: diffs |= __it_diff(a, b, 64);
4434 		fallthrough;
4435 	case 12: diffs |= __it_diff(a, b, 64);
4436 		fallthrough;
4437 	case  4: diffs |= __it_diff(a, b, 32);
4438 		break;
4439 	default:
4440 slow:
4441 		return memcmp(a - meta_len, b - meta_len, meta_len);
4442 	}
4443 	return diffs;
4444 }
4445 
skb_metadata_differs(const struct sk_buff * skb_a,const struct sk_buff * skb_b)4446 static inline bool skb_metadata_differs(const struct sk_buff *skb_a,
4447 					const struct sk_buff *skb_b)
4448 {
4449 	u8 len_a = skb_metadata_len(skb_a);
4450 	u8 len_b = skb_metadata_len(skb_b);
4451 
4452 	if (!(len_a | len_b))
4453 		return false;
4454 
4455 	return len_a != len_b ?
4456 	       true : __skb_metadata_differs(skb_a, skb_b, len_a);
4457 }
4458 
skb_metadata_set(struct sk_buff * skb,u8 meta_len)4459 static inline void skb_metadata_set(struct sk_buff *skb, u8 meta_len)
4460 {
4461 	skb_shinfo(skb)->meta_len = meta_len;
4462 }
4463 
skb_metadata_clear(struct sk_buff * skb)4464 static inline void skb_metadata_clear(struct sk_buff *skb)
4465 {
4466 	skb_metadata_set(skb, 0);
4467 }
4468 
4469 struct sk_buff *skb_clone_sk(struct sk_buff *skb);
4470 
4471 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
4472 
4473 void skb_clone_tx_timestamp(struct sk_buff *skb);
4474 bool skb_defer_rx_timestamp(struct sk_buff *skb);
4475 
4476 #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
4477 
skb_clone_tx_timestamp(struct sk_buff * skb)4478 static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
4479 {
4480 }
4481 
skb_defer_rx_timestamp(struct sk_buff * skb)4482 static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
4483 {
4484 	return false;
4485 }
4486 
4487 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
4488 
4489 /**
4490  * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
4491  *
4492  * PHY drivers may accept clones of transmitted packets for
4493  * timestamping via their phy_driver.txtstamp method. These drivers
4494  * must call this function to return the skb back to the stack with a
4495  * timestamp.
4496  *
4497  * @skb: clone of the original outgoing packet
4498  * @hwtstamps: hardware time stamps
4499  *
4500  */
4501 void skb_complete_tx_timestamp(struct sk_buff *skb,
4502 			       struct skb_shared_hwtstamps *hwtstamps);
4503 
4504 void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
4505 		     struct skb_shared_hwtstamps *hwtstamps,
4506 		     struct sock *sk, int tstype);
4507 
4508 /**
4509  * skb_tstamp_tx - queue clone of skb with send time stamps
4510  * @orig_skb:	the original outgoing packet
4511  * @hwtstamps:	hardware time stamps, may be NULL if not available
4512  *
4513  * If the skb has a socket associated, then this function clones the
4514  * skb (thus sharing the actual data and optional structures), stores
4515  * the optional hardware time stamping information (if non NULL) or
4516  * generates a software time stamp (otherwise), then queues the clone
4517  * to the error queue of the socket.  Errors are silently ignored.
4518  */
4519 void skb_tstamp_tx(struct sk_buff *orig_skb,
4520 		   struct skb_shared_hwtstamps *hwtstamps);
4521 
4522 /**
4523  * skb_tx_timestamp() - Driver hook for transmit timestamping
4524  *
4525  * Ethernet MAC Drivers should call this function in their hard_xmit()
4526  * function immediately before giving the sk_buff to the MAC hardware.
4527  *
4528  * Specifically, one should make absolutely sure that this function is
4529  * called before TX completion of this packet can trigger.  Otherwise
4530  * the packet could potentially already be freed.
4531  *
4532  * @skb: A socket buffer.
4533  */
skb_tx_timestamp(struct sk_buff * skb)4534 static inline void skb_tx_timestamp(struct sk_buff *skb)
4535 {
4536 	skb_clone_tx_timestamp(skb);
4537 	if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
4538 		skb_tstamp_tx(skb, NULL);
4539 }
4540 
4541 /**
4542  * skb_complete_wifi_ack - deliver skb with wifi status
4543  *
4544  * @skb: the original outgoing packet
4545  * @acked: ack status
4546  *
4547  */
4548 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
4549 
4550 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
4551 __sum16 __skb_checksum_complete(struct sk_buff *skb);
4552 
skb_csum_unnecessary(const struct sk_buff * skb)4553 static inline int skb_csum_unnecessary(const struct sk_buff *skb)
4554 {
4555 	return ((skb->ip_summed == CHECKSUM_UNNECESSARY) ||
4556 		skb->csum_valid ||
4557 		(skb->ip_summed == CHECKSUM_PARTIAL &&
4558 		 skb_checksum_start_offset(skb) >= 0));
4559 }
4560 
4561 /**
4562  *	skb_checksum_complete - Calculate checksum of an entire packet
4563  *	@skb: packet to process
4564  *
4565  *	This function calculates the checksum over the entire packet plus
4566  *	the value of skb->csum.  The latter can be used to supply the
4567  *	checksum of a pseudo header as used by TCP/UDP.  It returns the
4568  *	checksum.
4569  *
4570  *	For protocols that contain complete checksums such as ICMP/TCP/UDP,
4571  *	this function can be used to verify that checksum on received
4572  *	packets.  In that case the function should return zero if the
4573  *	checksum is correct.  In particular, this function will return zero
4574  *	if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
4575  *	hardware has already verified the correctness of the checksum.
4576  */
skb_checksum_complete(struct sk_buff * skb)4577 static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
4578 {
4579 	return skb_csum_unnecessary(skb) ?
4580 	       0 : __skb_checksum_complete(skb);
4581 }
4582 
__skb_decr_checksum_unnecessary(struct sk_buff * skb)4583 static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb)
4584 {
4585 	if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4586 		if (skb->csum_level == 0)
4587 			skb->ip_summed = CHECKSUM_NONE;
4588 		else
4589 			skb->csum_level--;
4590 	}
4591 }
4592 
__skb_incr_checksum_unnecessary(struct sk_buff * skb)4593 static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb)
4594 {
4595 	if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4596 		if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
4597 			skb->csum_level++;
4598 	} else if (skb->ip_summed == CHECKSUM_NONE) {
4599 		skb->ip_summed = CHECKSUM_UNNECESSARY;
4600 		skb->csum_level = 0;
4601 	}
4602 }
4603 
__skb_reset_checksum_unnecessary(struct sk_buff * skb)4604 static inline void __skb_reset_checksum_unnecessary(struct sk_buff *skb)
4605 {
4606 	if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4607 		skb->ip_summed = CHECKSUM_NONE;
4608 		skb->csum_level = 0;
4609 	}
4610 }
4611 
4612 /* Check if we need to perform checksum complete validation.
4613  *
4614  * Returns true if checksum complete is needed, false otherwise
4615  * (either checksum is unnecessary or zero checksum is allowed).
4616  */
__skb_checksum_validate_needed(struct sk_buff * skb,bool zero_okay,__sum16 check)4617 static inline bool __skb_checksum_validate_needed(struct sk_buff *skb,
4618 						  bool zero_okay,
4619 						  __sum16 check)
4620 {
4621 	if (skb_csum_unnecessary(skb) || (zero_okay && !check)) {
4622 		skb->csum_valid = 1;
4623 		__skb_decr_checksum_unnecessary(skb);
4624 		return false;
4625 	}
4626 
4627 	return true;
4628 }
4629 
4630 /* For small packets <= CHECKSUM_BREAK perform checksum complete directly
4631  * in checksum_init.
4632  */
4633 #define CHECKSUM_BREAK 76
4634 
4635 /* Unset checksum-complete
4636  *
4637  * Unset checksum complete can be done when packet is being modified
4638  * (uncompressed for instance) and checksum-complete value is
4639  * invalidated.
4640  */
skb_checksum_complete_unset(struct sk_buff * skb)4641 static inline void skb_checksum_complete_unset(struct sk_buff *skb)
4642 {
4643 	if (skb->ip_summed == CHECKSUM_COMPLETE)
4644 		skb->ip_summed = CHECKSUM_NONE;
4645 }
4646 
4647 /* Validate (init) checksum based on checksum complete.
4648  *
4649  * Return values:
4650  *   0: checksum is validated or try to in skb_checksum_complete. In the latter
4651  *	case the ip_summed will not be CHECKSUM_UNNECESSARY and the pseudo
4652  *	checksum is stored in skb->csum for use in __skb_checksum_complete
4653  *   non-zero: value of invalid checksum
4654  *
4655  */
__skb_checksum_validate_complete(struct sk_buff * skb,bool complete,__wsum psum)4656 static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb,
4657 						       bool complete,
4658 						       __wsum psum)
4659 {
4660 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
4661 		if (!csum_fold(csum_add(psum, skb->csum))) {
4662 			skb->csum_valid = 1;
4663 			return 0;
4664 		}
4665 	}
4666 
4667 	skb->csum = psum;
4668 
4669 	if (complete || skb->len <= CHECKSUM_BREAK) {
4670 		__sum16 csum;
4671 
4672 		csum = __skb_checksum_complete(skb);
4673 		skb->csum_valid = !csum;
4674 		return csum;
4675 	}
4676 
4677 	return 0;
4678 }
4679 
null_compute_pseudo(struct sk_buff * skb,int proto)4680 static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto)
4681 {
4682 	return 0;
4683 }
4684 
4685 /* Perform checksum validate (init). Note that this is a macro since we only
4686  * want to calculate the pseudo header which is an input function if necessary.
4687  * First we try to validate without any computation (checksum unnecessary) and
4688  * then calculate based on checksum complete calling the function to compute
4689  * pseudo header.
4690  *
4691  * Return values:
4692  *   0: checksum is validated or try to in skb_checksum_complete
4693  *   non-zero: value of invalid checksum
4694  */
4695 #define __skb_checksum_validate(skb, proto, complete,			\
4696 				zero_okay, check, compute_pseudo)	\
4697 ({									\
4698 	__sum16 __ret = 0;						\
4699 	skb->csum_valid = 0;						\
4700 	if (__skb_checksum_validate_needed(skb, zero_okay, check))	\
4701 		__ret = __skb_checksum_validate_complete(skb,		\
4702 				complete, compute_pseudo(skb, proto));	\
4703 	__ret;								\
4704 })
4705 
4706 #define skb_checksum_init(skb, proto, compute_pseudo)			\
4707 	__skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo)
4708 
4709 #define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo)	\
4710 	__skb_checksum_validate(skb, proto, false, true, check, compute_pseudo)
4711 
4712 #define skb_checksum_validate(skb, proto, compute_pseudo)		\
4713 	__skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo)
4714 
4715 #define skb_checksum_validate_zero_check(skb, proto, check,		\
4716 					 compute_pseudo)		\
4717 	__skb_checksum_validate(skb, proto, true, true, check, compute_pseudo)
4718 
4719 #define skb_checksum_simple_validate(skb)				\
4720 	__skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo)
4721 
__skb_checksum_convert_check(struct sk_buff * skb)4722 static inline bool __skb_checksum_convert_check(struct sk_buff *skb)
4723 {
4724 	return (skb->ip_summed == CHECKSUM_NONE && skb->csum_valid);
4725 }
4726 
__skb_checksum_convert(struct sk_buff * skb,__wsum pseudo)4727 static inline void __skb_checksum_convert(struct sk_buff *skb, __wsum pseudo)
4728 {
4729 	skb->csum = ~pseudo;
4730 	skb->ip_summed = CHECKSUM_COMPLETE;
4731 }
4732 
4733 #define skb_checksum_try_convert(skb, proto, compute_pseudo)	\
4734 do {									\
4735 	if (__skb_checksum_convert_check(skb))				\
4736 		__skb_checksum_convert(skb, compute_pseudo(skb, proto)); \
4737 } while (0)
4738 
skb_remcsum_adjust_partial(struct sk_buff * skb,void * ptr,u16 start,u16 offset)4739 static inline void skb_remcsum_adjust_partial(struct sk_buff *skb, void *ptr,
4740 					      u16 start, u16 offset)
4741 {
4742 	skb->ip_summed = CHECKSUM_PARTIAL;
4743 	skb->csum_start = ((unsigned char *)ptr + start) - skb->head;
4744 	skb->csum_offset = offset - start;
4745 }
4746 
4747 /* Update skbuf and packet to reflect the remote checksum offload operation.
4748  * When called, ptr indicates the starting point for skb->csum when
4749  * ip_summed is CHECKSUM_COMPLETE. If we need create checksum complete
4750  * here, skb_postpull_rcsum is done so skb->csum start is ptr.
4751  */
skb_remcsum_process(struct sk_buff * skb,void * ptr,int start,int offset,bool nopartial)4752 static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr,
4753 				       int start, int offset, bool nopartial)
4754 {
4755 	__wsum delta;
4756 
4757 	if (!nopartial) {
4758 		skb_remcsum_adjust_partial(skb, ptr, start, offset);
4759 		return;
4760 	}
4761 
4762 	if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) {
4763 		__skb_checksum_complete(skb);
4764 		skb_postpull_rcsum(skb, skb->data, ptr - (void *)skb->data);
4765 	}
4766 
4767 	delta = remcsum_adjust(ptr, skb->csum, start, offset);
4768 
4769 	/* Adjust skb->csum since we changed the packet */
4770 	skb->csum = csum_add(skb->csum, delta);
4771 }
4772 
skb_nfct(const struct sk_buff * skb)4773 static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb)
4774 {
4775 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4776 	return (void *)(skb->_nfct & NFCT_PTRMASK);
4777 #else
4778 	return NULL;
4779 #endif
4780 }
4781 
skb_get_nfct(const struct sk_buff * skb)4782 static inline unsigned long skb_get_nfct(const struct sk_buff *skb)
4783 {
4784 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4785 	return skb->_nfct;
4786 #else
4787 	return 0UL;
4788 #endif
4789 }
4790 
skb_set_nfct(struct sk_buff * skb,unsigned long nfct)4791 static inline void skb_set_nfct(struct sk_buff *skb, unsigned long nfct)
4792 {
4793 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4794 	skb->slow_gro |= !!nfct;
4795 	skb->_nfct = nfct;
4796 #endif
4797 }
4798 
4799 #ifdef CONFIG_SKB_EXTENSIONS
4800 enum skb_ext_id {
4801 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4802 	SKB_EXT_BRIDGE_NF,
4803 #endif
4804 #ifdef CONFIG_XFRM
4805 	SKB_EXT_SEC_PATH,
4806 #endif
4807 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4808 	TC_SKB_EXT,
4809 #endif
4810 #if IS_ENABLED(CONFIG_MPTCP)
4811 	SKB_EXT_MPTCP,
4812 #endif
4813 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
4814 	SKB_EXT_MCTP,
4815 #endif
4816 	SKB_EXT_NUM, /* must be last */
4817 };
4818 
4819 /**
4820  *	struct skb_ext - sk_buff extensions
4821  *	@refcnt: 1 on allocation, deallocated on 0
4822  *	@offset: offset to add to @data to obtain extension address
4823  *	@chunks: size currently allocated, stored in SKB_EXT_ALIGN_SHIFT units
4824  *	@data: start of extension data, variable sized
4825  *
4826  *	Note: offsets/lengths are stored in chunks of 8 bytes, this allows
4827  *	to use 'u8' types while allowing up to 2kb worth of extension data.
4828  */
4829 struct skb_ext {
4830 	refcount_t refcnt;
4831 	u8 offset[SKB_EXT_NUM]; /* in chunks of 8 bytes */
4832 	u8 chunks;		/* same */
4833 	char data[] __aligned(8);
4834 };
4835 
4836 struct skb_ext *__skb_ext_alloc(gfp_t flags);
4837 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
4838 		    struct skb_ext *ext);
4839 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
4840 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id);
4841 void __skb_ext_put(struct skb_ext *ext);
4842 
skb_ext_put(struct sk_buff * skb)4843 static inline void skb_ext_put(struct sk_buff *skb)
4844 {
4845 	if (skb->active_extensions)
4846 		__skb_ext_put(skb->extensions);
4847 }
4848 
__skb_ext_copy(struct sk_buff * dst,const struct sk_buff * src)4849 static inline void __skb_ext_copy(struct sk_buff *dst,
4850 				  const struct sk_buff *src)
4851 {
4852 	dst->active_extensions = src->active_extensions;
4853 
4854 	if (src->active_extensions) {
4855 		struct skb_ext *ext = src->extensions;
4856 
4857 		refcount_inc(&ext->refcnt);
4858 		dst->extensions = ext;
4859 	}
4860 }
4861 
skb_ext_copy(struct sk_buff * dst,const struct sk_buff * src)4862 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *src)
4863 {
4864 	skb_ext_put(dst);
4865 	__skb_ext_copy(dst, src);
4866 }
4867 
__skb_ext_exist(const struct skb_ext * ext,enum skb_ext_id i)4868 static inline bool __skb_ext_exist(const struct skb_ext *ext, enum skb_ext_id i)
4869 {
4870 	return !!ext->offset[i];
4871 }
4872 
skb_ext_exist(const struct sk_buff * skb,enum skb_ext_id id)4873 static inline bool skb_ext_exist(const struct sk_buff *skb, enum skb_ext_id id)
4874 {
4875 	return skb->active_extensions & (1 << id);
4876 }
4877 
skb_ext_del(struct sk_buff * skb,enum skb_ext_id id)4878 static inline void skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
4879 {
4880 	if (skb_ext_exist(skb, id))
4881 		__skb_ext_del(skb, id);
4882 }
4883 
skb_ext_find(const struct sk_buff * skb,enum skb_ext_id id)4884 static inline void *skb_ext_find(const struct sk_buff *skb, enum skb_ext_id id)
4885 {
4886 	if (skb_ext_exist(skb, id)) {
4887 		struct skb_ext *ext = skb->extensions;
4888 
4889 		return (void *)ext + (ext->offset[id] << 3);
4890 	}
4891 
4892 	return NULL;
4893 }
4894 
skb_ext_reset(struct sk_buff * skb)4895 static inline void skb_ext_reset(struct sk_buff *skb)
4896 {
4897 	if (unlikely(skb->active_extensions)) {
4898 		__skb_ext_put(skb->extensions);
4899 		skb->active_extensions = 0;
4900 	}
4901 }
4902 
skb_has_extensions(struct sk_buff * skb)4903 static inline bool skb_has_extensions(struct sk_buff *skb)
4904 {
4905 	return unlikely(skb->active_extensions);
4906 }
4907 #else
skb_ext_put(struct sk_buff * skb)4908 static inline void skb_ext_put(struct sk_buff *skb) {}
skb_ext_reset(struct sk_buff * skb)4909 static inline void skb_ext_reset(struct sk_buff *skb) {}
skb_ext_del(struct sk_buff * skb,int unused)4910 static inline void skb_ext_del(struct sk_buff *skb, int unused) {}
__skb_ext_copy(struct sk_buff * d,const struct sk_buff * s)4911 static inline void __skb_ext_copy(struct sk_buff *d, const struct sk_buff *s) {}
skb_ext_copy(struct sk_buff * dst,const struct sk_buff * s)4912 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {}
skb_has_extensions(struct sk_buff * skb)4913 static inline bool skb_has_extensions(struct sk_buff *skb) { return false; }
4914 #endif /* CONFIG_SKB_EXTENSIONS */
4915 
nf_reset_ct(struct sk_buff * skb)4916 static inline void nf_reset_ct(struct sk_buff *skb)
4917 {
4918 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4919 	nf_conntrack_put(skb_nfct(skb));
4920 	skb->_nfct = 0;
4921 #endif
4922 }
4923 
nf_reset_trace(struct sk_buff * skb)4924 static inline void nf_reset_trace(struct sk_buff *skb)
4925 {
4926 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || IS_ENABLED(CONFIG_NF_TABLES)
4927 	skb->nf_trace = 0;
4928 #endif
4929 }
4930 
ipvs_reset(struct sk_buff * skb)4931 static inline void ipvs_reset(struct sk_buff *skb)
4932 {
4933 #if IS_ENABLED(CONFIG_IP_VS)
4934 	skb->ipvs_property = 0;
4935 #endif
4936 }
4937 
4938 /* Note: This doesn't put any conntrack info in dst. */
__nf_copy(struct sk_buff * dst,const struct sk_buff * src,bool copy)4939 static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src,
4940 			     bool copy)
4941 {
4942 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4943 	dst->_nfct = src->_nfct;
4944 	nf_conntrack_get(skb_nfct(src));
4945 #endif
4946 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || IS_ENABLED(CONFIG_NF_TABLES)
4947 	if (copy)
4948 		dst->nf_trace = src->nf_trace;
4949 #endif
4950 }
4951 
nf_copy(struct sk_buff * dst,const struct sk_buff * src)4952 static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
4953 {
4954 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4955 	nf_conntrack_put(skb_nfct(dst));
4956 #endif
4957 	dst->slow_gro = src->slow_gro;
4958 	__nf_copy(dst, src, true);
4959 }
4960 
4961 #ifdef CONFIG_NETWORK_SECMARK
skb_copy_secmark(struct sk_buff * to,const struct sk_buff * from)4962 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
4963 {
4964 	to->secmark = from->secmark;
4965 }
4966 
skb_init_secmark(struct sk_buff * skb)4967 static inline void skb_init_secmark(struct sk_buff *skb)
4968 {
4969 	skb->secmark = 0;
4970 }
4971 #else
skb_copy_secmark(struct sk_buff * to,const struct sk_buff * from)4972 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
4973 { }
4974 
skb_init_secmark(struct sk_buff * skb)4975 static inline void skb_init_secmark(struct sk_buff *skb)
4976 { }
4977 #endif
4978 
secpath_exists(const struct sk_buff * skb)4979 static inline int secpath_exists(const struct sk_buff *skb)
4980 {
4981 #ifdef CONFIG_XFRM
4982 	return skb_ext_exist(skb, SKB_EXT_SEC_PATH);
4983 #else
4984 	return 0;
4985 #endif
4986 }
4987 
skb_irq_freeable(const struct sk_buff * skb)4988 static inline bool skb_irq_freeable(const struct sk_buff *skb)
4989 {
4990 	return !skb->destructor &&
4991 		!secpath_exists(skb) &&
4992 		!skb_nfct(skb) &&
4993 		!skb->_skb_refdst &&
4994 		!skb_has_frag_list(skb);
4995 }
4996 
skb_set_queue_mapping(struct sk_buff * skb,u16 queue_mapping)4997 static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
4998 {
4999 	skb->queue_mapping = queue_mapping;
5000 }
5001 
skb_get_queue_mapping(const struct sk_buff * skb)5002 static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
5003 {
5004 	return skb->queue_mapping;
5005 }
5006 
skb_copy_queue_mapping(struct sk_buff * to,const struct sk_buff * from)5007 static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
5008 {
5009 	to->queue_mapping = from->queue_mapping;
5010 }
5011 
skb_record_rx_queue(struct sk_buff * skb,u16 rx_queue)5012 static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
5013 {
5014 	skb->queue_mapping = rx_queue + 1;
5015 }
5016 
skb_get_rx_queue(const struct sk_buff * skb)5017 static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
5018 {
5019 	return skb->queue_mapping - 1;
5020 }
5021 
skb_rx_queue_recorded(const struct sk_buff * skb)5022 static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
5023 {
5024 	return skb->queue_mapping != 0;
5025 }
5026 
skb_set_dst_pending_confirm(struct sk_buff * skb,u32 val)5027 static inline void skb_set_dst_pending_confirm(struct sk_buff *skb, u32 val)
5028 {
5029 	skb->dst_pending_confirm = val;
5030 }
5031 
skb_get_dst_pending_confirm(const struct sk_buff * skb)5032 static inline bool skb_get_dst_pending_confirm(const struct sk_buff *skb)
5033 {
5034 	return skb->dst_pending_confirm != 0;
5035 }
5036 
skb_sec_path(const struct sk_buff * skb)5037 static inline struct sec_path *skb_sec_path(const struct sk_buff *skb)
5038 {
5039 #ifdef CONFIG_XFRM
5040 	return skb_ext_find(skb, SKB_EXT_SEC_PATH);
5041 #else
5042 	return NULL;
5043 #endif
5044 }
5045 
skb_is_gso(const struct sk_buff * skb)5046 static inline bool skb_is_gso(const struct sk_buff *skb)
5047 {
5048 	return skb_shinfo(skb)->gso_size;
5049 }
5050 
5051 /* Note: Should be called only if skb_is_gso(skb) is true */
skb_is_gso_v6(const struct sk_buff * skb)5052 static inline bool skb_is_gso_v6(const struct sk_buff *skb)
5053 {
5054 	return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
5055 }
5056 
5057 /* Note: Should be called only if skb_is_gso(skb) is true */
skb_is_gso_sctp(const struct sk_buff * skb)5058 static inline bool skb_is_gso_sctp(const struct sk_buff *skb)
5059 {
5060 	return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
5061 }
5062 
5063 /* Note: Should be called only if skb_is_gso(skb) is true */
skb_is_gso_tcp(const struct sk_buff * skb)5064 static inline bool skb_is_gso_tcp(const struct sk_buff *skb)
5065 {
5066 	return skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6);
5067 }
5068 
skb_gso_reset(struct sk_buff * skb)5069 static inline void skb_gso_reset(struct sk_buff *skb)
5070 {
5071 	skb_shinfo(skb)->gso_size = 0;
5072 	skb_shinfo(skb)->gso_segs = 0;
5073 	skb_shinfo(skb)->gso_type = 0;
5074 }
5075 
skb_increase_gso_size(struct skb_shared_info * shinfo,u16 increment)5076 static inline void skb_increase_gso_size(struct skb_shared_info *shinfo,
5077 					 u16 increment)
5078 {
5079 	if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
5080 		return;
5081 	shinfo->gso_size += increment;
5082 }
5083 
skb_decrease_gso_size(struct skb_shared_info * shinfo,u16 decrement)5084 static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo,
5085 					 u16 decrement)
5086 {
5087 	if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
5088 		return;
5089 	shinfo->gso_size -= decrement;
5090 }
5091 
5092 void __skb_warn_lro_forwarding(const struct sk_buff *skb);
5093 
skb_warn_if_lro(const struct sk_buff * skb)5094 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
5095 {
5096 	/* LRO sets gso_size but not gso_type, whereas if GSO is really
5097 	 * wanted then gso_type will be set. */
5098 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5099 
5100 	if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
5101 	    unlikely(shinfo->gso_type == 0)) {
5102 		__skb_warn_lro_forwarding(skb);
5103 		return true;
5104 	}
5105 	return false;
5106 }
5107 
skb_forward_csum(struct sk_buff * skb)5108 static inline void skb_forward_csum(struct sk_buff *skb)
5109 {
5110 	/* Unfortunately we don't support this one.  Any brave souls? */
5111 	if (skb->ip_summed == CHECKSUM_COMPLETE)
5112 		skb->ip_summed = CHECKSUM_NONE;
5113 }
5114 
5115 /**
5116  * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
5117  * @skb: skb to check
5118  *
5119  * fresh skbs have their ip_summed set to CHECKSUM_NONE.
5120  * Instead of forcing ip_summed to CHECKSUM_NONE, we can
5121  * use this helper, to document places where we make this assertion.
5122  */
skb_checksum_none_assert(const struct sk_buff * skb)5123 static inline void skb_checksum_none_assert(const struct sk_buff *skb)
5124 {
5125 	DEBUG_NET_WARN_ON_ONCE(skb->ip_summed != CHECKSUM_NONE);
5126 }
5127 
5128 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
5129 
5130 int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
5131 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5132 				     unsigned int transport_len,
5133 				     __sum16(*skb_chkf)(struct sk_buff *skb));
5134 
5135 /**
5136  * skb_head_is_locked - Determine if the skb->head is locked down
5137  * @skb: skb to check
5138  *
5139  * The head on skbs build around a head frag can be removed if they are
5140  * not cloned.  This function returns true if the skb head is locked down
5141  * due to either being allocated via kmalloc, or by being a clone with
5142  * multiple references to the head.
5143  */
skb_head_is_locked(const struct sk_buff * skb)5144 static inline bool skb_head_is_locked(const struct sk_buff *skb)
5145 {
5146 	return !skb->head_frag || skb_cloned(skb);
5147 }
5148 
5149 /* Local Checksum Offload.
5150  * Compute outer checksum based on the assumption that the
5151  * inner checksum will be offloaded later.
5152  * See Documentation/networking/checksum-offloads.rst for
5153  * explanation of how this works.
5154  * Fill in outer checksum adjustment (e.g. with sum of outer
5155  * pseudo-header) before calling.
5156  * Also ensure that inner checksum is in linear data area.
5157  */
lco_csum(struct sk_buff * skb)5158 static inline __wsum lco_csum(struct sk_buff *skb)
5159 {
5160 	unsigned char *csum_start = skb_checksum_start(skb);
5161 	unsigned char *l4_hdr = skb_transport_header(skb);
5162 	__wsum partial;
5163 
5164 	/* Start with complement of inner checksum adjustment */
5165 	partial = ~csum_unfold(*(__force __sum16 *)(csum_start +
5166 						    skb->csum_offset));
5167 
5168 	/* Add in checksum of our headers (incl. outer checksum
5169 	 * adjustment filled in by caller) and return result.
5170 	 */
5171 	return csum_partial(l4_hdr, csum_start - l4_hdr, partial);
5172 }
5173 
skb_is_redirected(const struct sk_buff * skb)5174 static inline bool skb_is_redirected(const struct sk_buff *skb)
5175 {
5176 	return skb->redirected;
5177 }
5178 
skb_set_redirected(struct sk_buff * skb,bool from_ingress)5179 static inline void skb_set_redirected(struct sk_buff *skb, bool from_ingress)
5180 {
5181 	skb->redirected = 1;
5182 #ifdef CONFIG_NET_REDIRECT
5183 	skb->from_ingress = from_ingress;
5184 	if (skb->from_ingress)
5185 		skb_clear_tstamp(skb);
5186 #endif
5187 }
5188 
skb_reset_redirect(struct sk_buff * skb)5189 static inline void skb_reset_redirect(struct sk_buff *skb)
5190 {
5191 	skb->redirected = 0;
5192 }
5193 
skb_set_redirected_noclear(struct sk_buff * skb,bool from_ingress)5194 static inline void skb_set_redirected_noclear(struct sk_buff *skb,
5195 					      bool from_ingress)
5196 {
5197 	skb->redirected = 1;
5198 #ifdef CONFIG_NET_REDIRECT
5199 	skb->from_ingress = from_ingress;
5200 #endif
5201 }
5202 
skb_csum_is_sctp(struct sk_buff * skb)5203 static inline bool skb_csum_is_sctp(struct sk_buff *skb)
5204 {
5205 #if IS_ENABLED(CONFIG_IP_SCTP)
5206 	return skb->csum_not_inet;
5207 #else
5208 	return 0;
5209 #endif
5210 }
5211 
skb_reset_csum_not_inet(struct sk_buff * skb)5212 static inline void skb_reset_csum_not_inet(struct sk_buff *skb)
5213 {
5214 	skb->ip_summed = CHECKSUM_NONE;
5215 #if IS_ENABLED(CONFIG_IP_SCTP)
5216 	skb->csum_not_inet = 0;
5217 #endif
5218 }
5219 
skb_set_kcov_handle(struct sk_buff * skb,const u64 kcov_handle)5220 static inline void skb_set_kcov_handle(struct sk_buff *skb,
5221 				       const u64 kcov_handle)
5222 {
5223 #ifdef CONFIG_KCOV
5224 	skb->kcov_handle = kcov_handle;
5225 #endif
5226 }
5227 
skb_get_kcov_handle(struct sk_buff * skb)5228 static inline u64 skb_get_kcov_handle(struct sk_buff *skb)
5229 {
5230 #ifdef CONFIG_KCOV
5231 	return skb->kcov_handle;
5232 #else
5233 	return 0;
5234 #endif
5235 }
5236 
skb_mark_for_recycle(struct sk_buff * skb)5237 static inline void skb_mark_for_recycle(struct sk_buff *skb)
5238 {
5239 #ifdef CONFIG_PAGE_POOL
5240 	skb->pp_recycle = 1;
5241 #endif
5242 }
5243 
5244 ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
5245 			     ssize_t maxsize, gfp_t gfp);
5246 
5247 #endif	/* __KERNEL__ */
5248 #endif	/* _LINUX_SKBUFF_H */
5249