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