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