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