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