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