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