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