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