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