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