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