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