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