net_dma: simple removal
[deliverable/linux.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 17#include <linux/kernel.h>
fe55f6d5 18#include <linux/kmemcheck.h>
1da177e4
LT
19#include <linux/compiler.h>
20#include <linux/time.h>
187f1882 21#include <linux/bug.h>
1da177e4
LT
22#include <linux/cache.h>
23
60063497 24#include <linux/atomic.h>
1da177e4
LT
25#include <asm/types.h>
26#include <linux/spinlock.h>
1da177e4 27#include <linux/net.h>
3fc7e8a6 28#include <linux/textsearch.h>
1da177e4 29#include <net/checksum.h>
a80958f4 30#include <linux/rcupdate.h>
b7aa0bf7 31#include <linux/hrtimer.h>
131ea667 32#include <linux/dma-mapping.h>
c8f44aff 33#include <linux/netdev_features.h>
5203cd28 34#include <net/flow_keys.h>
1da177e4 35
78ea85f1
DB
36/* A. Checksumming of received packets by device.
37 *
38 * CHECKSUM_NONE:
39 *
40 * Device failed to checksum this packet e.g. due to lack of capabilities.
41 * The packet contains full (though not verified) checksum in packet but
42 * not in skb->csum. Thus, skb->csum is undefined in this case.
43 *
44 * CHECKSUM_UNNECESSARY:
45 *
46 * The hardware you're dealing with doesn't calculate the full checksum
47 * (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums
48 * for specific protocols e.g. TCP/UDP/SCTP, then, for such packets it will
49 * set CHECKSUM_UNNECESSARY if their checksums are okay. skb->csum is still
50 * undefined in this case though. It is a bad option, but, unfortunately,
51 * nowadays most vendors do this. Apparently with the secret goal to sell
52 * you new devices, when you will add new protocol to your host, f.e. IPv6 8)
53 *
54 * CHECKSUM_COMPLETE:
55 *
56 * This is the most generic way. The device supplied checksum of the _whole_
57 * packet as seen by netif_rx() and fills out in skb->csum. Meaning, the
58 * hardware doesn't need to parse L3/L4 headers to implement this.
59 *
60 * Note: Even if device supports only some protocols, but is able to produce
61 * skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY.
62 *
63 * CHECKSUM_PARTIAL:
64 *
65 * This is identical to the case for output below. This may occur on a packet
66 * received directly from another Linux OS, e.g., a virtualized Linux kernel
67 * on the same host. The packet can be treated in the same way as
68 * CHECKSUM_UNNECESSARY, except that on output (i.e., forwarding) the
69 * checksum must be filled in by the OS or the hardware.
70 *
71 * B. Checksumming on output.
72 *
73 * CHECKSUM_NONE:
74 *
75 * The skb was already checksummed by the protocol, or a checksum is not
76 * required.
77 *
78 * CHECKSUM_PARTIAL:
79 *
80 * The device is required to checksum the packet as seen by hard_start_xmit()
81 * from skb->csum_start up to the end, and to record/write the checksum at
82 * offset skb->csum_start + skb->csum_offset.
83 *
84 * The device must show its capabilities in dev->features, set up at device
85 * setup time, e.g. netdev_features.h:
86 *
87 * NETIF_F_HW_CSUM - It's a clever device, it's able to checksum everything.
88 * NETIF_F_IP_CSUM - Device is dumb, it's able to checksum only TCP/UDP over
89 * IPv4. Sigh. Vendors like this way for an unknown reason.
90 * Though, see comment above about CHECKSUM_UNNECESSARY. 8)
91 * NETIF_F_IPV6_CSUM - About as dumb as the last one but does IPv6 instead.
92 * NETIF_F_... - Well, you get the picture.
93 *
94 * CHECKSUM_UNNECESSARY:
95 *
96 * Normally, the device will do per protocol specific checksumming. Protocol
97 * implementations that do not want the NIC to perform the checksum
98 * calculation should use this flag in their outgoing skbs.
99 *
100 * NETIF_F_FCOE_CRC - This indicates that the device can do FCoE FC CRC
101 * offload. Correspondingly, the FCoE protocol driver
102 * stack should use CHECKSUM_UNNECESSARY.
103 *
104 * Any questions? No questions, good. --ANK
105 */
106
60476372 107/* Don't change this without changing skb_csum_unnecessary! */
78ea85f1
DB
108#define CHECKSUM_NONE 0
109#define CHECKSUM_UNNECESSARY 1
110#define CHECKSUM_COMPLETE 2
111#define CHECKSUM_PARTIAL 3
1da177e4
LT
112
113#define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
114 ~(SMP_CACHE_BYTES - 1))
fc910a27 115#define SKB_WITH_OVERHEAD(X) \
deea84b0 116 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
fc910a27
DM
117#define SKB_MAX_ORDER(X, ORDER) \
118 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
1da177e4
LT
119#define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
120#define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
121
87fb4b7b
ED
122/* return minimum truesize of one skb containing X bytes of data */
123#define SKB_TRUESIZE(X) ((X) + \
124 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
125 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
126
1da177e4 127struct net_device;
716ea3a7 128struct scatterlist;
9c55e01c 129struct pipe_inode_info;
1da177e4 130
5f79e0f9 131#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1da177e4
LT
132struct nf_conntrack {
133 atomic_t use;
1da177e4 134};
5f79e0f9 135#endif
1da177e4
LT
136
137#ifdef CONFIG_BRIDGE_NETFILTER
138struct nf_bridge_info {
bf1ac5ca
ED
139 atomic_t use;
140 unsigned int mask;
141 struct net_device *physindev;
142 struct net_device *physoutdev;
143 unsigned long data[32 / sizeof(unsigned long)];
1da177e4
LT
144};
145#endif
146
1da177e4
LT
147struct sk_buff_head {
148 /* These two members must be first. */
149 struct sk_buff *next;
150 struct sk_buff *prev;
151
152 __u32 qlen;
153 spinlock_t lock;
154};
155
156struct sk_buff;
157
9d4dde52
IC
158/* To allow 64K frame to be packed as single skb without frag_list we
159 * require 64K/PAGE_SIZE pages plus 1 additional page to allow for
160 * buffers which do not start on a page boundary.
161 *
162 * Since GRO uses frags we allocate at least 16 regardless of page
163 * size.
a715dea3 164 */
9d4dde52 165#if (65536/PAGE_SIZE + 1) < 16
eec00954 166#define MAX_SKB_FRAGS 16UL
a715dea3 167#else
9d4dde52 168#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
a715dea3 169#endif
1da177e4
LT
170
171typedef struct skb_frag_struct skb_frag_t;
172
173struct skb_frag_struct {
a8605c60
IC
174 struct {
175 struct page *p;
176 } page;
cb4dfe56 177#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
a309bb07
DM
178 __u32 page_offset;
179 __u32 size;
cb4dfe56
ED
180#else
181 __u16 page_offset;
182 __u16 size;
183#endif
1da177e4
LT
184};
185
9e903e08
ED
186static inline unsigned int skb_frag_size(const skb_frag_t *frag)
187{
188 return frag->size;
189}
190
191static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
192{
193 frag->size = size;
194}
195
196static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
197{
198 frag->size += delta;
199}
200
201static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
202{
203 frag->size -= delta;
204}
205
ac45f602
PO
206#define HAVE_HW_TIME_STAMP
207
208/**
d3a21be8 209 * struct skb_shared_hwtstamps - hardware time stamps
ac45f602
PO
210 * @hwtstamp: hardware time stamp transformed into duration
211 * since arbitrary point in time
212 * @syststamp: hwtstamp transformed to system time base
213 *
214 * Software time stamps generated by ktime_get_real() are stored in
215 * skb->tstamp. The relation between the different kinds of time
216 * stamps is as follows:
217 *
218 * syststamp and tstamp can be compared against each other in
219 * arbitrary combinations. The accuracy of a
220 * syststamp/tstamp/"syststamp from other device" comparison is
221 * limited by the accuracy of the transformation into system time
222 * base. This depends on the device driver and its underlying
223 * hardware.
224 *
225 * hwtstamps can only be compared against other hwtstamps from
226 * the same device.
227 *
228 * This structure is attached to packets as part of the
229 * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
230 */
231struct skb_shared_hwtstamps {
232 ktime_t hwtstamp;
233 ktime_t syststamp;
234};
235
2244d07b
OH
236/* Definitions for tx_flags in struct skb_shared_info */
237enum {
238 /* generate hardware time stamp */
239 SKBTX_HW_TSTAMP = 1 << 0,
240
241 /* generate software time stamp */
242 SKBTX_SW_TSTAMP = 1 << 1,
243
244 /* device driver is going to provide hardware time stamp */
245 SKBTX_IN_PROGRESS = 1 << 2,
246
a6686f2f 247 /* device driver supports TX zero-copy buffers */
62b1a8ab 248 SKBTX_DEV_ZEROCOPY = 1 << 3,
6e3e939f
JB
249
250 /* generate wifi status information (where possible) */
62b1a8ab 251 SKBTX_WIFI_STATUS = 1 << 4,
c9af6db4
PS
252
253 /* This indicates at least one fragment might be overwritten
254 * (as in vmsplice(), sendfile() ...)
255 * If we need to compute a TX checksum, we'll need to copy
256 * all frags to avoid possible bad checksum
257 */
258 SKBTX_SHARED_FRAG = 1 << 5,
a6686f2f
SM
259};
260
261/*
262 * The callback notifies userspace to release buffers when skb DMA is done in
263 * lower device, the skb last reference should be 0 when calling this.
e19d6763
MT
264 * The zerocopy_success argument is true if zero copy transmit occurred,
265 * false on data copy or out of memory error caused by data copy attempt.
ca8f4fb2
MT
266 * The ctx field is used to track device context.
267 * The desc field is used to track userspace buffer index.
a6686f2f
SM
268 */
269struct ubuf_info {
e19d6763 270 void (*callback)(struct ubuf_info *, bool zerocopy_success);
ca8f4fb2 271 void *ctx;
a6686f2f 272 unsigned long desc;
ac45f602
PO
273};
274
1da177e4
LT
275/* This data is invariant across clones and lives at
276 * the end of the header data, ie. at skb->end.
277 */
278struct skb_shared_info {
9f42f126
IC
279 unsigned char nr_frags;
280 __u8 tx_flags;
7967168c
HX
281 unsigned short gso_size;
282 /* Warning: this field is not always filled in (UFO)! */
283 unsigned short gso_segs;
284 unsigned short gso_type;
1da177e4 285 struct sk_buff *frag_list;
ac45f602 286 struct skb_shared_hwtstamps hwtstamps;
9f42f126 287 __be32 ip6_frag_id;
ec7d2f2c
ED
288
289 /*
290 * Warning : all fields before dataref are cleared in __alloc_skb()
291 */
292 atomic_t dataref;
293
69e3c75f
JB
294 /* Intermediate layers must ensure that destructor_arg
295 * remains valid until skb destructor */
296 void * destructor_arg;
a6686f2f 297
fed66381
ED
298 /* must be last field, see pskb_expand_head() */
299 skb_frag_t frags[MAX_SKB_FRAGS];
1da177e4
LT
300};
301
302/* We divide dataref into two halves. The higher 16 bits hold references
303 * to the payload part of skb->data. The lower 16 bits hold references to
334a8132
PM
304 * the entire skb->data. A clone of a headerless skb holds the length of
305 * the header in skb->hdr_len.
1da177e4
LT
306 *
307 * All users must obey the rule that the skb->data reference count must be
308 * greater than or equal to the payload reference count.
309 *
310 * Holding a reference to the payload part means that the user does not
311 * care about modifications to the header part of skb->data.
312 */
313#define SKB_DATAREF_SHIFT 16
314#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
315
d179cd12
DM
316
317enum {
318 SKB_FCLONE_UNAVAILABLE,
319 SKB_FCLONE_ORIG,
320 SKB_FCLONE_CLONE,
321};
322
7967168c
HX
323enum {
324 SKB_GSO_TCPV4 = 1 << 0,
f83ef8c0 325 SKB_GSO_UDP = 1 << 1,
576a30eb
HX
326
327 /* This indicates the skb is from an untrusted source. */
328 SKB_GSO_DODGY = 1 << 2,
b0da8537
MC
329
330 /* This indicates the tcp segment has CWR set. */
f83ef8c0
HX
331 SKB_GSO_TCP_ECN = 1 << 3,
332
333 SKB_GSO_TCPV6 = 1 << 4,
01d5b2fc
CL
334
335 SKB_GSO_FCOE = 1 << 5,
68c33163
PS
336
337 SKB_GSO_GRE = 1 << 6,
73136267 338
cb32f511 339 SKB_GSO_IPIP = 1 << 7,
0d89d203 340
61c1db7f 341 SKB_GSO_SIT = 1 << 8,
cb32f511 342
61c1db7f
ED
343 SKB_GSO_UDP_TUNNEL = 1 << 9,
344
345 SKB_GSO_MPLS = 1 << 10,
7967168c
HX
346};
347
2e07fa9c
ACM
348#if BITS_PER_LONG > 32
349#define NET_SKBUFF_DATA_USES_OFFSET 1
350#endif
351
352#ifdef NET_SKBUFF_DATA_USES_OFFSET
353typedef unsigned int sk_buff_data_t;
354#else
355typedef unsigned char *sk_buff_data_t;
356#endif
357
1da177e4
LT
358/**
359 * struct sk_buff - socket buffer
360 * @next: Next buffer in list
361 * @prev: Previous buffer in list
325ed823 362 * @tstamp: Time we arrived
d84e0bd7 363 * @sk: Socket we are owned by
1da177e4 364 * @dev: Device we arrived on/are leaving by
d84e0bd7 365 * @cb: Control buffer. Free for use by every layer. Put private vars here
7fee226a 366 * @_skb_refdst: destination entry (with norefcount bit)
67be2dd1 367 * @sp: the security path, used for xfrm
1da177e4
LT
368 * @len: Length of actual data
369 * @data_len: Data length
370 * @mac_len: Length of link layer header
334a8132 371 * @hdr_len: writable header length of cloned skb
663ead3b
HX
372 * @csum: Checksum (must include start/offset pair)
373 * @csum_start: Offset from skb->head where checksumming should start
374 * @csum_offset: Offset from csum_start where checksum should be stored
d84e0bd7 375 * @priority: Packet queueing priority
67be2dd1 376 * @local_df: allow local fragmentation
1da177e4 377 * @cloned: Head may be cloned (check refcnt to be sure)
d84e0bd7 378 * @ip_summed: Driver fed us an IP checksum
1da177e4 379 * @nohdr: Payload reference only, must not modify header
d84e0bd7 380 * @nfctinfo: Relationship of this skb to the connection
1da177e4 381 * @pkt_type: Packet class
c83c2486 382 * @fclone: skbuff clone status
c83c2486 383 * @ipvs_property: skbuff is owned by ipvs
31729363
RD
384 * @peeked: this packet has been seen already, so stats have been
385 * done for it, don't do them again
ba9dda3a 386 * @nf_trace: netfilter packet trace flag
d84e0bd7
DB
387 * @protocol: Packet protocol from driver
388 * @destructor: Destruct function
389 * @nfct: Associated connection, if any
1da177e4 390 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
8964be4a 391 * @skb_iif: ifindex of device we arrived on
1da177e4
LT
392 * @tc_index: Traffic control index
393 * @tc_verd: traffic control verdict
d84e0bd7
DB
394 * @rxhash: the packet hash computed on receive
395 * @queue_mapping: Queue mapping for multiqueue devices
553a5672 396 * @ndisc_nodetype: router type (from link layer)
d84e0bd7 397 * @ooo_okay: allow the mapping of a socket to a queue to be changed
4ca2462e
CG
398 * @l4_rxhash: indicate rxhash is a canonical 4-tuple hash over transport
399 * ports.
6e3e939f
JB
400 * @wifi_acked_valid: wifi_acked was set
401 * @wifi_acked: whether frame was acked on wifi or not
3bdc0eba 402 * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS
f4b8ea78
RD
403 * @dma_cookie: a cookie to one of several possible DMA operations
404 * done by skb DMA functions
06021292 405 * @napi_id: id of the NAPI struct this skb came from
984bc16c 406 * @secmark: security marking
d84e0bd7
DB
407 * @mark: Generic packet mark
408 * @dropcount: total number of sk_receive_queue overflows
86a9bad3 409 * @vlan_proto: vlan encapsulation protocol
6aa895b0 410 * @vlan_tci: vlan tag control information
0d89d203 411 * @inner_protocol: Protocol (encapsulation)
6a674e9c
JG
412 * @inner_transport_header: Inner transport layer header (encapsulation)
413 * @inner_network_header: Network layer header (encapsulation)
aefbd2b3 414 * @inner_mac_header: Link layer header (encapsulation)
d84e0bd7
DB
415 * @transport_header: Transport layer header
416 * @network_header: Network layer header
417 * @mac_header: Link layer header
418 * @tail: Tail pointer
419 * @end: End pointer
420 * @head: Head of buffer
421 * @data: Data head pointer
422 * @truesize: Buffer size
423 * @users: User count - see {datagram,tcp}.c
1da177e4
LT
424 */
425
426struct sk_buff {
427 /* These two members must be first. */
428 struct sk_buff *next;
429 struct sk_buff *prev;
430
b7aa0bf7 431 ktime_t tstamp;
da3f5cf1
FF
432
433 struct sock *sk;
1da177e4 434 struct net_device *dev;
1da177e4 435
1da177e4
LT
436 /*
437 * This is the control buffer. It is free to use for every
438 * layer. Please put your private variables there. If you
439 * want to keep them across layers you have to do a skb_clone()
440 * first. This is owned by whoever has the skb queued ATM.
441 */
da3f5cf1 442 char cb[48] __aligned(8);
1da177e4 443
7fee226a 444 unsigned long _skb_refdst;
da3f5cf1
FF
445#ifdef CONFIG_XFRM
446 struct sec_path *sp;
447#endif
1da177e4 448 unsigned int len,
334a8132
PM
449 data_len;
450 __u16 mac_len,
451 hdr_len;
ff1dcadb
AV
452 union {
453 __wsum csum;
663ead3b
HX
454 struct {
455 __u16 csum_start;
456 __u16 csum_offset;
457 };
ff1dcadb 458 };
1da177e4 459 __u32 priority;
fe55f6d5 460 kmemcheck_bitfield_begin(flags1);
1cbb3380
TG
461 __u8 local_df:1,
462 cloned:1,
463 ip_summed:2,
6869c4d8
HW
464 nohdr:1,
465 nfctinfo:3;
d179cd12 466 __u8 pkt_type:3,
b84f4cc9 467 fclone:2,
ba9dda3a 468 ipvs_property:1,
a59322be 469 peeked:1,
ba9dda3a 470 nf_trace:1;
fe55f6d5 471 kmemcheck_bitfield_end(flags1);
4ab408de 472 __be16 protocol;
1da177e4
LT
473
474 void (*destructor)(struct sk_buff *skb);
9fb9cbb1 475#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
5f79e0f9 476 struct nf_conntrack *nfct;
2fc72c7b 477#endif
1da177e4
LT
478#ifdef CONFIG_BRIDGE_NETFILTER
479 struct nf_bridge_info *nf_bridge;
480#endif
f25f4e44 481
8964be4a 482 int skb_iif;
4031ae6e
AD
483
484 __u32 rxhash;
485
86a9bad3 486 __be16 vlan_proto;
4031ae6e
AD
487 __u16 vlan_tci;
488
1da177e4 489#ifdef CONFIG_NET_SCHED
b6b99eb5 490 __u16 tc_index; /* traffic control index */
1da177e4 491#ifdef CONFIG_NET_CLS_ACT
b6b99eb5 492 __u16 tc_verd; /* traffic control verdict */
1da177e4 493#endif
1da177e4 494#endif
fe55f6d5 495
0a14842f 496 __u16 queue_mapping;
fe55f6d5 497 kmemcheck_bitfield_begin(flags2);
de357cc0 498#ifdef CONFIG_IPV6_NDISC_NODETYPE
8a4eb573 499 __u8 ndisc_nodetype:2;
d0f09804 500#endif
c93bdd0e 501 __u8 pfmemalloc:1;
3853b584 502 __u8 ooo_okay:1;
bdeab991 503 __u8 l4_rxhash:1;
6e3e939f
JB
504 __u8 wifi_acked_valid:1;
505 __u8 wifi_acked:1;
3bdc0eba 506 __u8 no_fcs:1;
d3836f21 507 __u8 head_frag:1;
6a674e9c
JG
508 /* Encapsulation protocol and NIC drivers should use
509 * this flag to indicate to each other if the skb contains
510 * encapsulated packet or not and maybe use the inner packet
511 * headers if needed
512 */
513 __u8 encapsulation:1;
45906723 514 /* 6/8 bit hole (depending on ndisc_nodetype presence) */
fe55f6d5
VN
515 kmemcheck_bitfield_end(flags2);
516
7bced397
DW
517#ifdef CONFIG_NET_RX_BUSY_POLL
518 unsigned int napi_id;
97fc2f08 519#endif
984bc16c
JM
520#ifdef CONFIG_NETWORK_SECMARK
521 __u32 secmark;
522#endif
3b885787
NH
523 union {
524 __u32 mark;
525 __u32 dropcount;
16fad69c 526 __u32 reserved_tailroom;
3b885787 527 };
1da177e4 528
0d89d203 529 __be16 inner_protocol;
1a37e412
SH
530 __u16 inner_transport_header;
531 __u16 inner_network_header;
532 __u16 inner_mac_header;
533 __u16 transport_header;
534 __u16 network_header;
535 __u16 mac_header;
1da177e4 536 /* These elements must be at the end, see alloc_skb() for details. */
27a884dc 537 sk_buff_data_t tail;
4305b541 538 sk_buff_data_t end;
1da177e4 539 unsigned char *head,
4305b541 540 *data;
27a884dc
ACM
541 unsigned int truesize;
542 atomic_t users;
1da177e4
LT
543};
544
545#ifdef __KERNEL__
546/*
547 * Handling routines are only of interest to the kernel
548 */
549#include <linux/slab.h>
550
1da177e4 551
c93bdd0e
MG
552#define SKB_ALLOC_FCLONE 0x01
553#define SKB_ALLOC_RX 0x02
554
555/* Returns true if the skb was allocated from PFMEMALLOC reserves */
556static inline bool skb_pfmemalloc(const struct sk_buff *skb)
557{
558 return unlikely(skb->pfmemalloc);
559}
560
7fee226a
ED
561/*
562 * skb might have a dst pointer attached, refcounted or not.
563 * _skb_refdst low order bit is set if refcount was _not_ taken
564 */
565#define SKB_DST_NOREF 1UL
566#define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
567
568/**
569 * skb_dst - returns skb dst_entry
570 * @skb: buffer
571 *
572 * Returns skb dst_entry, regardless of reference taken or not.
573 */
adf30907
ED
574static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
575{
7fee226a
ED
576 /* If refdst was not refcounted, check we still are in a
577 * rcu_read_lock section
578 */
579 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
580 !rcu_read_lock_held() &&
581 !rcu_read_lock_bh_held());
582 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
adf30907
ED
583}
584
7fee226a
ED
585/**
586 * skb_dst_set - sets skb dst
587 * @skb: buffer
588 * @dst: dst entry
589 *
590 * Sets skb dst, assuming a reference was taken on dst and should
591 * be released by skb_dst_drop()
592 */
adf30907
ED
593static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
594{
7fee226a
ED
595 skb->_skb_refdst = (unsigned long)dst;
596}
597
7965bd4d
JP
598void __skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst,
599 bool force);
932bc4d7
JA
600
601/**
602 * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
603 * @skb: buffer
604 * @dst: dst entry
605 *
606 * Sets skb dst, assuming a reference was not taken on dst.
607 * If dst entry is cached, we do not take reference and dst_release
608 * will be avoided by refdst_drop. If dst entry is not cached, we take
609 * reference, so that last dst_release can destroy the dst immediately.
610 */
611static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
612{
613 __skb_dst_set_noref(skb, dst, false);
614}
615
616/**
617 * skb_dst_set_noref_force - sets skb dst, without taking reference
618 * @skb: buffer
619 * @dst: dst entry
620 *
621 * Sets skb dst, assuming a reference was not taken on dst.
622 * No reference is taken and no dst_release will be called. While for
623 * cached dsts deferred reclaim is a basic feature, for entries that are
624 * not cached it is caller's job to guarantee that last dst_release for
625 * provided dst happens when nobody uses it, eg. after a RCU grace period.
626 */
627static inline void skb_dst_set_noref_force(struct sk_buff *skb,
628 struct dst_entry *dst)
629{
630 __skb_dst_set_noref(skb, dst, true);
631}
7fee226a
ED
632
633/**
25985edc 634 * skb_dst_is_noref - Test if skb dst isn't refcounted
7fee226a
ED
635 * @skb: buffer
636 */
637static inline bool skb_dst_is_noref(const struct sk_buff *skb)
638{
639 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
adf30907
ED
640}
641
511c3f92
ED
642static inline struct rtable *skb_rtable(const struct sk_buff *skb)
643{
adf30907 644 return (struct rtable *)skb_dst(skb);
511c3f92
ED
645}
646
7965bd4d
JP
647void kfree_skb(struct sk_buff *skb);
648void kfree_skb_list(struct sk_buff *segs);
649void skb_tx_error(struct sk_buff *skb);
650void consume_skb(struct sk_buff *skb);
651void __kfree_skb(struct sk_buff *skb);
d7e8883c 652extern struct kmem_cache *skbuff_head_cache;
bad43ca8 653
7965bd4d
JP
654void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
655bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
656 bool *fragstolen, int *delta_truesize);
bad43ca8 657
7965bd4d
JP
658struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
659 int node);
660struct sk_buff *build_skb(void *data, unsigned int frag_size);
d179cd12 661static inline struct sk_buff *alloc_skb(unsigned int size,
dd0fc66f 662 gfp_t priority)
d179cd12 663{
564824b0 664 return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
d179cd12
DM
665}
666
667static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
dd0fc66f 668 gfp_t priority)
d179cd12 669{
c93bdd0e 670 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
d179cd12
DM
671}
672
7965bd4d 673struct sk_buff *__alloc_skb_head(gfp_t priority, int node);
0ebd0ac5
PM
674static inline struct sk_buff *alloc_skb_head(gfp_t priority)
675{
676 return __alloc_skb_head(priority, -1);
677}
678
7965bd4d
JP
679struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
680int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
681struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
682struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
683struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask);
684
685int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask);
686struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
687 unsigned int headroom);
688struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom,
689 int newtailroom, gfp_t priority);
690int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset,
691 int len);
692int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer);
693int skb_pad(struct sk_buff *skb, int pad);
ead2ceb0 694#define dev_kfree_skb(a) consume_skb(a)
1da177e4 695
7965bd4d
JP
696int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
697 int getfrag(void *from, char *to, int offset,
698 int len, int odd, struct sk_buff *skb),
699 void *from, int length);
e89e9cf5 700
d94d9fee 701struct skb_seq_state {
677e90ed
TG
702 __u32 lower_offset;
703 __u32 upper_offset;
704 __u32 frag_idx;
705 __u32 stepped_offset;
706 struct sk_buff *root_skb;
707 struct sk_buff *cur_skb;
708 __u8 *frag_data;
709};
710
7965bd4d
JP
711void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
712 unsigned int to, struct skb_seq_state *st);
713unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
714 struct skb_seq_state *st);
715void skb_abort_seq_read(struct skb_seq_state *st);
677e90ed 716
7965bd4d
JP
717unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
718 unsigned int to, struct ts_config *config,
719 struct ts_state *state);
3fc7e8a6 720
09323cc4
TH
721/*
722 * Packet hash types specify the type of hash in skb_set_hash.
723 *
724 * Hash types refer to the protocol layer addresses which are used to
725 * construct a packet's hash. The hashes are used to differentiate or identify
726 * flows of the protocol layer for the hash type. Hash types are either
727 * layer-2 (L2), layer-3 (L3), or layer-4 (L4).
728 *
729 * Properties of hashes:
730 *
731 * 1) Two packets in different flows have different hash values
732 * 2) Two packets in the same flow should have the same hash value
733 *
734 * A hash at a higher layer is considered to be more specific. A driver should
735 * set the most specific hash possible.
736 *
737 * A driver cannot indicate a more specific hash than the layer at which a hash
738 * was computed. For instance an L3 hash cannot be set as an L4 hash.
739 *
740 * A driver may indicate a hash level which is less specific than the
741 * actual layer the hash was computed on. For instance, a hash computed
742 * at L4 may be considered an L3 hash. This should only be done if the
743 * driver can't unambiguously determine that the HW computed the hash at
744 * the higher layer. Note that the "should" in the second property above
745 * permits this.
746 */
747enum pkt_hash_types {
748 PKT_HASH_TYPE_NONE, /* Undefined type */
749 PKT_HASH_TYPE_L2, /* Input: src_MAC, dest_MAC */
750 PKT_HASH_TYPE_L3, /* Input: src_IP, dst_IP */
751 PKT_HASH_TYPE_L4, /* Input: src_IP, dst_IP, src_port, dst_port */
752};
753
754static inline void
755skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
756{
757 skb->l4_rxhash = (type == PKT_HASH_TYPE_L4);
758 skb->rxhash = hash;
759}
760
3958afa1
TH
761void __skb_get_hash(struct sk_buff *skb);
762static inline __u32 skb_get_hash(struct sk_buff *skb)
bfb564e7 763{
ecd5cf5d 764 if (!skb->l4_rxhash)
3958afa1 765 __skb_get_hash(skb);
bfb564e7
KK
766
767 return skb->rxhash;
768}
769
57bdf7f4
TH
770static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
771{
772 return skb->rxhash;
773}
774
7539fadc
TH
775static inline void skb_clear_hash(struct sk_buff *skb)
776{
777 skb->rxhash = 0;
778 skb->l4_rxhash = 0;
779}
780
781static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
782{
783 if (!skb->l4_rxhash)
784 skb_clear_hash(skb);
785}
786
3df7a74e
TH
787static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
788{
789 to->rxhash = from->rxhash;
790 to->l4_rxhash = from->l4_rxhash;
791};
792
4305b541
ACM
793#ifdef NET_SKBUFF_DATA_USES_OFFSET
794static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
795{
796 return skb->head + skb->end;
797}
ec47ea82
AD
798
799static inline unsigned int skb_end_offset(const struct sk_buff *skb)
800{
801 return skb->end;
802}
4305b541
ACM
803#else
804static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
805{
806 return skb->end;
807}
ec47ea82
AD
808
809static inline unsigned int skb_end_offset(const struct sk_buff *skb)
810{
811 return skb->end - skb->head;
812}
4305b541
ACM
813#endif
814
1da177e4 815/* Internal */
4305b541 816#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
1da177e4 817
ac45f602
PO
818static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
819{
820 return &skb_shinfo(skb)->hwtstamps;
821}
822
1da177e4
LT
823/**
824 * skb_queue_empty - check if a queue is empty
825 * @list: queue head
826 *
827 * Returns true if the queue is empty, false otherwise.
828 */
829static inline int skb_queue_empty(const struct sk_buff_head *list)
830{
fd44b93c 831 return list->next == (const struct sk_buff *) list;
1da177e4
LT
832}
833
fc7ebb21
DM
834/**
835 * skb_queue_is_last - check if skb is the last entry in the queue
836 * @list: queue head
837 * @skb: buffer
838 *
839 * Returns true if @skb is the last buffer on the list.
840 */
841static inline bool skb_queue_is_last(const struct sk_buff_head *list,
842 const struct sk_buff *skb)
843{
fd44b93c 844 return skb->next == (const struct sk_buff *) list;
fc7ebb21
DM
845}
846
832d11c5
IJ
847/**
848 * skb_queue_is_first - check if skb is the first entry in the queue
849 * @list: queue head
850 * @skb: buffer
851 *
852 * Returns true if @skb is the first buffer on the list.
853 */
854static inline bool skb_queue_is_first(const struct sk_buff_head *list,
855 const struct sk_buff *skb)
856{
fd44b93c 857 return skb->prev == (const struct sk_buff *) list;
832d11c5
IJ
858}
859
249c8b42
DM
860/**
861 * skb_queue_next - return the next packet in the queue
862 * @list: queue head
863 * @skb: current buffer
864 *
865 * Return the next packet in @list after @skb. It is only valid to
866 * call this if skb_queue_is_last() evaluates to false.
867 */
868static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
869 const struct sk_buff *skb)
870{
871 /* This BUG_ON may seem severe, but if we just return then we
872 * are going to dereference garbage.
873 */
874 BUG_ON(skb_queue_is_last(list, skb));
875 return skb->next;
876}
877
832d11c5
IJ
878/**
879 * skb_queue_prev - return the prev packet in the queue
880 * @list: queue head
881 * @skb: current buffer
882 *
883 * Return the prev packet in @list before @skb. It is only valid to
884 * call this if skb_queue_is_first() evaluates to false.
885 */
886static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
887 const struct sk_buff *skb)
888{
889 /* This BUG_ON may seem severe, but if we just return then we
890 * are going to dereference garbage.
891 */
892 BUG_ON(skb_queue_is_first(list, skb));
893 return skb->prev;
894}
895
1da177e4
LT
896/**
897 * skb_get - reference buffer
898 * @skb: buffer to reference
899 *
900 * Makes another reference to a socket buffer and returns a pointer
901 * to the buffer.
902 */
903static inline struct sk_buff *skb_get(struct sk_buff *skb)
904{
905 atomic_inc(&skb->users);
906 return skb;
907}
908
909/*
910 * If users == 1, we are the only owner and are can avoid redundant
911 * atomic change.
912 */
913
1da177e4
LT
914/**
915 * skb_cloned - is the buffer a clone
916 * @skb: buffer to check
917 *
918 * Returns true if the buffer was generated with skb_clone() and is
919 * one of multiple shared copies of the buffer. Cloned buffers are
920 * shared data so must not be written to under normal circumstances.
921 */
922static inline int skb_cloned(const struct sk_buff *skb)
923{
924 return skb->cloned &&
925 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
926}
927
14bbd6a5
PS
928static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
929{
930 might_sleep_if(pri & __GFP_WAIT);
931
932 if (skb_cloned(skb))
933 return pskb_expand_head(skb, 0, 0, pri);
934
935 return 0;
936}
937
1da177e4
LT
938/**
939 * skb_header_cloned - is the header a clone
940 * @skb: buffer to check
941 *
942 * Returns true if modifying the header part of the buffer requires
943 * the data to be copied.
944 */
945static inline int skb_header_cloned(const struct sk_buff *skb)
946{
947 int dataref;
948
949 if (!skb->cloned)
950 return 0;
951
952 dataref = atomic_read(&skb_shinfo(skb)->dataref);
953 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
954 return dataref != 1;
955}
956
957/**
958 * skb_header_release - release reference to header
959 * @skb: buffer to operate on
960 *
961 * Drop a reference to the header part of the buffer. This is done
962 * by acquiring a payload reference. You must not read from the header
963 * part of skb->data after this.
964 */
965static inline void skb_header_release(struct sk_buff *skb)
966{
967 BUG_ON(skb->nohdr);
968 skb->nohdr = 1;
969 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
970}
971
972/**
973 * skb_shared - is the buffer shared
974 * @skb: buffer to check
975 *
976 * Returns true if more than one person has a reference to this
977 * buffer.
978 */
979static inline int skb_shared(const struct sk_buff *skb)
980{
981 return atomic_read(&skb->users) != 1;
982}
983
984/**
985 * skb_share_check - check if buffer is shared and if so clone it
986 * @skb: buffer to check
987 * @pri: priority for memory allocation
988 *
989 * If the buffer is shared the buffer is cloned and the old copy
990 * drops a reference. A new clone with a single reference is returned.
991 * If the buffer is not shared the original buffer is returned. When
992 * being called from interrupt status or with spinlocks held pri must
993 * be GFP_ATOMIC.
994 *
995 * NULL is returned on a memory allocation failure.
996 */
47061bc4 997static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
1da177e4
LT
998{
999 might_sleep_if(pri & __GFP_WAIT);
1000 if (skb_shared(skb)) {
1001 struct sk_buff *nskb = skb_clone(skb, pri);
47061bc4
ED
1002
1003 if (likely(nskb))
1004 consume_skb(skb);
1005 else
1006 kfree_skb(skb);
1da177e4
LT
1007 skb = nskb;
1008 }
1009 return skb;
1010}
1011
1012/*
1013 * Copy shared buffers into a new sk_buff. We effectively do COW on
1014 * packets to handle cases where we have a local reader and forward
1015 * and a couple of other messy ones. The normal one is tcpdumping
1016 * a packet thats being forwarded.
1017 */
1018
1019/**
1020 * skb_unshare - make a copy of a shared buffer
1021 * @skb: buffer to check
1022 * @pri: priority for memory allocation
1023 *
1024 * If the socket buffer is a clone then this function creates a new
1025 * copy of the data, drops a reference count on the old copy and returns
1026 * the new copy with the reference count at 1. If the buffer is not a clone
1027 * the original buffer is returned. When called with a spinlock held or
1028 * from interrupt state @pri must be %GFP_ATOMIC
1029 *
1030 * %NULL is returned on a memory allocation failure.
1031 */
e2bf521d 1032static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
dd0fc66f 1033 gfp_t pri)
1da177e4
LT
1034{
1035 might_sleep_if(pri & __GFP_WAIT);
1036 if (skb_cloned(skb)) {
1037 struct sk_buff *nskb = skb_copy(skb, pri);
1038 kfree_skb(skb); /* Free our shared copy */
1039 skb = nskb;
1040 }
1041 return skb;
1042}
1043
1044/**
1a5778aa 1045 * skb_peek - peek at the head of an &sk_buff_head
1da177e4
LT
1046 * @list_: list to peek at
1047 *
1048 * Peek an &sk_buff. Unlike most other operations you _MUST_
1049 * be careful with this one. A peek leaves the buffer on the
1050 * list and someone else may run off with it. You must hold
1051 * the appropriate locks or have a private queue to do this.
1052 *
1053 * Returns %NULL for an empty list or a pointer to the head element.
1054 * The reference count is not incremented and the reference is therefore
1055 * volatile. Use with caution.
1056 */
05bdd2f1 1057static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
1da177e4 1058{
18d07000
ED
1059 struct sk_buff *skb = list_->next;
1060
1061 if (skb == (struct sk_buff *)list_)
1062 skb = NULL;
1063 return skb;
1da177e4
LT
1064}
1065
da5ef6e5
PE
1066/**
1067 * skb_peek_next - peek skb following the given one from a queue
1068 * @skb: skb to start from
1069 * @list_: list to peek at
1070 *
1071 * Returns %NULL when the end of the list is met or a pointer to the
1072 * next element. The reference count is not incremented and the
1073 * reference is therefore volatile. Use with caution.
1074 */
1075static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
1076 const struct sk_buff_head *list_)
1077{
1078 struct sk_buff *next = skb->next;
18d07000 1079
da5ef6e5
PE
1080 if (next == (struct sk_buff *)list_)
1081 next = NULL;
1082 return next;
1083}
1084
1da177e4 1085/**
1a5778aa 1086 * skb_peek_tail - peek at the tail of an &sk_buff_head
1da177e4
LT
1087 * @list_: list to peek at
1088 *
1089 * Peek an &sk_buff. Unlike most other operations you _MUST_
1090 * be careful with this one. A peek leaves the buffer on the
1091 * list and someone else may run off with it. You must hold
1092 * the appropriate locks or have a private queue to do this.
1093 *
1094 * Returns %NULL for an empty list or a pointer to the tail element.
1095 * The reference count is not incremented and the reference is therefore
1096 * volatile. Use with caution.
1097 */
05bdd2f1 1098static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
1da177e4 1099{
18d07000
ED
1100 struct sk_buff *skb = list_->prev;
1101
1102 if (skb == (struct sk_buff *)list_)
1103 skb = NULL;
1104 return skb;
1105
1da177e4
LT
1106}
1107
1108/**
1109 * skb_queue_len - get queue length
1110 * @list_: list to measure
1111 *
1112 * Return the length of an &sk_buff queue.
1113 */
1114static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
1115{
1116 return list_->qlen;
1117}
1118
67fed459
DM
1119/**
1120 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
1121 * @list: queue to initialize
1122 *
1123 * This initializes only the list and queue length aspects of
1124 * an sk_buff_head object. This allows to initialize the list
1125 * aspects of an sk_buff_head without reinitializing things like
1126 * the spinlock. It can also be used for on-stack sk_buff_head
1127 * objects where the spinlock is known to not be used.
1128 */
1129static inline void __skb_queue_head_init(struct sk_buff_head *list)
1130{
1131 list->prev = list->next = (struct sk_buff *)list;
1132 list->qlen = 0;
1133}
1134
76f10ad0
AV
1135/*
1136 * This function creates a split out lock class for each invocation;
1137 * this is needed for now since a whole lot of users of the skb-queue
1138 * infrastructure in drivers have different locking usage (in hardirq)
1139 * than the networking core (in softirq only). In the long run either the
1140 * network layer or drivers should need annotation to consolidate the
1141 * main types of usage into 3 classes.
1142 */
1da177e4
LT
1143static inline void skb_queue_head_init(struct sk_buff_head *list)
1144{
1145 spin_lock_init(&list->lock);
67fed459 1146 __skb_queue_head_init(list);
1da177e4
LT
1147}
1148
c2ecba71
PE
1149static inline void skb_queue_head_init_class(struct sk_buff_head *list,
1150 struct lock_class_key *class)
1151{
1152 skb_queue_head_init(list);
1153 lockdep_set_class(&list->lock, class);
1154}
1155
1da177e4 1156/*
bf299275 1157 * Insert an sk_buff on a list.
1da177e4
LT
1158 *
1159 * The "__skb_xxxx()" functions are the non-atomic ones that
1160 * can only be called with interrupts disabled.
1161 */
7965bd4d
JP
1162void skb_insert(struct sk_buff *old, struct sk_buff *newsk,
1163 struct sk_buff_head *list);
bf299275
GR
1164static inline void __skb_insert(struct sk_buff *newsk,
1165 struct sk_buff *prev, struct sk_buff *next,
1166 struct sk_buff_head *list)
1167{
1168 newsk->next = next;
1169 newsk->prev = prev;
1170 next->prev = prev->next = newsk;
1171 list->qlen++;
1172}
1da177e4 1173
67fed459
DM
1174static inline void __skb_queue_splice(const struct sk_buff_head *list,
1175 struct sk_buff *prev,
1176 struct sk_buff *next)
1177{
1178 struct sk_buff *first = list->next;
1179 struct sk_buff *last = list->prev;
1180
1181 first->prev = prev;
1182 prev->next = first;
1183
1184 last->next = next;
1185 next->prev = last;
1186}
1187
1188/**
1189 * skb_queue_splice - join two skb lists, this is designed for stacks
1190 * @list: the new list to add
1191 * @head: the place to add it in the first list
1192 */
1193static inline void skb_queue_splice(const struct sk_buff_head *list,
1194 struct sk_buff_head *head)
1195{
1196 if (!skb_queue_empty(list)) {
1197 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
1d4a31dd 1198 head->qlen += list->qlen;
67fed459
DM
1199 }
1200}
1201
1202/**
d9619496 1203 * skb_queue_splice_init - join two skb lists and reinitialise the emptied list
67fed459
DM
1204 * @list: the new list to add
1205 * @head: the place to add it in the first list
1206 *
1207 * The list at @list is reinitialised
1208 */
1209static inline void skb_queue_splice_init(struct sk_buff_head *list,
1210 struct sk_buff_head *head)
1211{
1212 if (!skb_queue_empty(list)) {
1213 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
1d4a31dd 1214 head->qlen += list->qlen;
67fed459
DM
1215 __skb_queue_head_init(list);
1216 }
1217}
1218
1219/**
1220 * skb_queue_splice_tail - join two skb lists, each list being a queue
1221 * @list: the new list to add
1222 * @head: the place to add it in the first list
1223 */
1224static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
1225 struct sk_buff_head *head)
1226{
1227 if (!skb_queue_empty(list)) {
1228 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
1d4a31dd 1229 head->qlen += list->qlen;
67fed459
DM
1230 }
1231}
1232
1233/**
d9619496 1234 * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
67fed459
DM
1235 * @list: the new list to add
1236 * @head: the place to add it in the first list
1237 *
1238 * Each of the lists is a queue.
1239 * The list at @list is reinitialised
1240 */
1241static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
1242 struct sk_buff_head *head)
1243{
1244 if (!skb_queue_empty(list)) {
1245 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
1d4a31dd 1246 head->qlen += list->qlen;
67fed459
DM
1247 __skb_queue_head_init(list);
1248 }
1249}
1250
1da177e4 1251/**
300ce174 1252 * __skb_queue_after - queue a buffer at the list head
1da177e4 1253 * @list: list to use
300ce174 1254 * @prev: place after this buffer
1da177e4
LT
1255 * @newsk: buffer to queue
1256 *
300ce174 1257 * Queue a buffer int the middle of a list. This function takes no locks
1da177e4
LT
1258 * and you must therefore hold required locks before calling it.
1259 *
1260 * A buffer cannot be placed on two lists at the same time.
1261 */
300ce174
SH
1262static inline void __skb_queue_after(struct sk_buff_head *list,
1263 struct sk_buff *prev,
1264 struct sk_buff *newsk)
1da177e4 1265{
bf299275 1266 __skb_insert(newsk, prev, prev->next, list);
1da177e4
LT
1267}
1268
7965bd4d
JP
1269void skb_append(struct sk_buff *old, struct sk_buff *newsk,
1270 struct sk_buff_head *list);
7de6c033 1271
f5572855
GR
1272static inline void __skb_queue_before(struct sk_buff_head *list,
1273 struct sk_buff *next,
1274 struct sk_buff *newsk)
1275{
1276 __skb_insert(newsk, next->prev, next, list);
1277}
1278
300ce174
SH
1279/**
1280 * __skb_queue_head - queue a buffer at the list head
1281 * @list: list to use
1282 * @newsk: buffer to queue
1283 *
1284 * Queue a buffer at the start of a list. This function takes no locks
1285 * and you must therefore hold required locks before calling it.
1286 *
1287 * A buffer cannot be placed on two lists at the same time.
1288 */
7965bd4d 1289void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
300ce174
SH
1290static inline void __skb_queue_head(struct sk_buff_head *list,
1291 struct sk_buff *newsk)
1292{
1293 __skb_queue_after(list, (struct sk_buff *)list, newsk);
1294}
1295
1da177e4
LT
1296/**
1297 * __skb_queue_tail - queue a buffer at the list tail
1298 * @list: list to use
1299 * @newsk: buffer to queue
1300 *
1301 * Queue a buffer at the end of a list. This function takes no locks
1302 * and you must therefore hold required locks before calling it.
1303 *
1304 * A buffer cannot be placed on two lists at the same time.
1305 */
7965bd4d 1306void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
1da177e4
LT
1307static inline void __skb_queue_tail(struct sk_buff_head *list,
1308 struct sk_buff *newsk)
1309{
f5572855 1310 __skb_queue_before(list, (struct sk_buff *)list, newsk);
1da177e4
LT
1311}
1312
1da177e4
LT
1313/*
1314 * remove sk_buff from list. _Must_ be called atomically, and with
1315 * the list known..
1316 */
7965bd4d 1317void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
1da177e4
LT
1318static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1319{
1320 struct sk_buff *next, *prev;
1321
1322 list->qlen--;
1323 next = skb->next;
1324 prev = skb->prev;
1325 skb->next = skb->prev = NULL;
1da177e4
LT
1326 next->prev = prev;
1327 prev->next = next;
1328}
1329
f525c06d
GR
1330/**
1331 * __skb_dequeue - remove from the head of the queue
1332 * @list: list to dequeue from
1333 *
1334 * Remove the head of the list. This function does not take any locks
1335 * so must be used with appropriate locks held only. The head item is
1336 * returned or %NULL if the list is empty.
1337 */
7965bd4d 1338struct sk_buff *skb_dequeue(struct sk_buff_head *list);
f525c06d
GR
1339static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
1340{
1341 struct sk_buff *skb = skb_peek(list);
1342 if (skb)
1343 __skb_unlink(skb, list);
1344 return skb;
1345}
1da177e4
LT
1346
1347/**
1348 * __skb_dequeue_tail - remove from the tail of the queue
1349 * @list: list to dequeue from
1350 *
1351 * Remove the tail of the list. This function does not take any locks
1352 * so must be used with appropriate locks held only. The tail item is
1353 * returned or %NULL if the list is empty.
1354 */
7965bd4d 1355struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
1da177e4
LT
1356static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
1357{
1358 struct sk_buff *skb = skb_peek_tail(list);
1359 if (skb)
1360 __skb_unlink(skb, list);
1361 return skb;
1362}
1363
1364
bdcc0924 1365static inline bool skb_is_nonlinear(const struct sk_buff *skb)
1da177e4
LT
1366{
1367 return skb->data_len;
1368}
1369
1370static inline unsigned int skb_headlen(const struct sk_buff *skb)
1371{
1372 return skb->len - skb->data_len;
1373}
1374
1375static inline int skb_pagelen(const struct sk_buff *skb)
1376{
1377 int i, len = 0;
1378
1379 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
9e903e08 1380 len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
1da177e4
LT
1381 return len + skb_headlen(skb);
1382}
1383
131ea667
IC
1384/**
1385 * __skb_fill_page_desc - initialise a paged fragment in an skb
1386 * @skb: buffer containing fragment to be initialised
1387 * @i: paged fragment index to initialise
1388 * @page: the page to use for this fragment
1389 * @off: the offset to the data with @page
1390 * @size: the length of the data
1391 *
1392 * Initialises the @i'th fragment of @skb to point to &size bytes at
1393 * offset @off within @page.
1394 *
1395 * Does not take any additional reference on the fragment.
1396 */
1397static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
1398 struct page *page, int off, int size)
1da177e4
LT
1399{
1400 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1401
c48a11c7
MG
1402 /*
1403 * Propagate page->pfmemalloc to the skb if we can. The problem is
1404 * that not all callers have unique ownership of the page. If
1405 * pfmemalloc is set, we check the mapping as a mapping implies
1406 * page->index is set (index and pfmemalloc share space).
1407 * If it's a valid mapping, we cannot use page->pfmemalloc but we
1408 * do not lose pfmemalloc information as the pages would not be
1409 * allocated using __GFP_MEMALLOC.
1410 */
a8605c60 1411 frag->page.p = page;
1da177e4 1412 frag->page_offset = off;
9e903e08 1413 skb_frag_size_set(frag, size);
cca7af38
PE
1414
1415 page = compound_head(page);
1416 if (page->pfmemalloc && !page->mapping)
1417 skb->pfmemalloc = true;
131ea667
IC
1418}
1419
1420/**
1421 * skb_fill_page_desc - initialise a paged fragment in an skb
1422 * @skb: buffer containing fragment to be initialised
1423 * @i: paged fragment index to initialise
1424 * @page: the page to use for this fragment
1425 * @off: the offset to the data with @page
1426 * @size: the length of the data
1427 *
1428 * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
bc32383c 1429 * @skb to point to @size bytes at offset @off within @page. In
131ea667
IC
1430 * addition updates @skb such that @i is the last fragment.
1431 *
1432 * Does not take any additional reference on the fragment.
1433 */
1434static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
1435 struct page *page, int off, int size)
1436{
1437 __skb_fill_page_desc(skb, i, page, off, size);
1da177e4
LT
1438 skb_shinfo(skb)->nr_frags = i + 1;
1439}
1440
7965bd4d
JP
1441void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
1442 int size, unsigned int truesize);
654bed16 1443
f8e617e1
JW
1444void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
1445 unsigned int truesize);
1446
1da177e4 1447#define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
21dc3301 1448#define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
1da177e4
LT
1449#define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1450
27a884dc
ACM
1451#ifdef NET_SKBUFF_DATA_USES_OFFSET
1452static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1453{
1454 return skb->head + skb->tail;
1455}
1456
1457static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1458{
1459 skb->tail = skb->data - skb->head;
1460}
1461
1462static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1463{
1464 skb_reset_tail_pointer(skb);
1465 skb->tail += offset;
1466}
7cc46190 1467
27a884dc
ACM
1468#else /* NET_SKBUFF_DATA_USES_OFFSET */
1469static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1470{
1471 return skb->tail;
1472}
1473
1474static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1475{
1476 skb->tail = skb->data;
1477}
1478
1479static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1480{
1481 skb->tail = skb->data + offset;
1482}
4305b541 1483
27a884dc
ACM
1484#endif /* NET_SKBUFF_DATA_USES_OFFSET */
1485
1da177e4
LT
1486/*
1487 * Add data to an sk_buff
1488 */
0c7ddf36 1489unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len);
7965bd4d 1490unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
1da177e4
LT
1491static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
1492{
27a884dc 1493 unsigned char *tmp = skb_tail_pointer(skb);
1da177e4
LT
1494 SKB_LINEAR_ASSERT(skb);
1495 skb->tail += len;
1496 skb->len += len;
1497 return tmp;
1498}
1499
7965bd4d 1500unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
1da177e4
LT
1501static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
1502{
1503 skb->data -= len;
1504 skb->len += len;
1505 return skb->data;
1506}
1507
7965bd4d 1508unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
1da177e4
LT
1509static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
1510{
1511 skb->len -= len;
1512 BUG_ON(skb->len < skb->data_len);
1513 return skb->data += len;
1514}
1515
47d29646
DM
1516static inline unsigned char *skb_pull_inline(struct sk_buff *skb, unsigned int len)
1517{
1518 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1519}
1520
7965bd4d 1521unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
1da177e4
LT
1522
1523static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
1524{
1525 if (len > skb_headlen(skb) &&
987c402a 1526 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
1da177e4
LT
1527 return NULL;
1528 skb->len -= len;
1529 return skb->data += len;
1530}
1531
1532static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
1533{
1534 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
1535}
1536
1537static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
1538{
1539 if (likely(len <= skb_headlen(skb)))
1540 return 1;
1541 if (unlikely(len > skb->len))
1542 return 0;
987c402a 1543 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
1da177e4
LT
1544}
1545
1546/**
1547 * skb_headroom - bytes at buffer head
1548 * @skb: buffer to check
1549 *
1550 * Return the number of bytes of free space at the head of an &sk_buff.
1551 */
c2636b4d 1552static inline unsigned int skb_headroom(const struct sk_buff *skb)
1da177e4
LT
1553{
1554 return skb->data - skb->head;
1555}
1556
1557/**
1558 * skb_tailroom - bytes at buffer end
1559 * @skb: buffer to check
1560 *
1561 * Return the number of bytes of free space at the tail of an sk_buff
1562 */
1563static inline int skb_tailroom(const struct sk_buff *skb)
1564{
4305b541 1565 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
1da177e4
LT
1566}
1567
a21d4572
ED
1568/**
1569 * skb_availroom - bytes at buffer end
1570 * @skb: buffer to check
1571 *
1572 * Return the number of bytes of free space at the tail of an sk_buff
1573 * allocated by sk_stream_alloc()
1574 */
1575static inline int skb_availroom(const struct sk_buff *skb)
1576{
16fad69c
ED
1577 if (skb_is_nonlinear(skb))
1578 return 0;
1579
1580 return skb->end - skb->tail - skb->reserved_tailroom;
a21d4572
ED
1581}
1582
1da177e4
LT
1583/**
1584 * skb_reserve - adjust headroom
1585 * @skb: buffer to alter
1586 * @len: bytes to move
1587 *
1588 * Increase the headroom of an empty &sk_buff by reducing the tail
1589 * room. This is only allowed for an empty buffer.
1590 */
8243126c 1591static inline void skb_reserve(struct sk_buff *skb, int len)
1da177e4
LT
1592{
1593 skb->data += len;
1594 skb->tail += len;
1595}
1596
6a674e9c
JG
1597static inline void skb_reset_inner_headers(struct sk_buff *skb)
1598{
aefbd2b3 1599 skb->inner_mac_header = skb->mac_header;
6a674e9c
JG
1600 skb->inner_network_header = skb->network_header;
1601 skb->inner_transport_header = skb->transport_header;
1602}
1603
0b5c9db1
JP
1604static inline void skb_reset_mac_len(struct sk_buff *skb)
1605{
1606 skb->mac_len = skb->network_header - skb->mac_header;
1607}
1608
6a674e9c
JG
1609static inline unsigned char *skb_inner_transport_header(const struct sk_buff
1610 *skb)
1611{
1612 return skb->head + skb->inner_transport_header;
1613}
1614
1615static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
1616{
1617 skb->inner_transport_header = skb->data - skb->head;
1618}
1619
1620static inline void skb_set_inner_transport_header(struct sk_buff *skb,
1621 const int offset)
1622{
1623 skb_reset_inner_transport_header(skb);
1624 skb->inner_transport_header += offset;
1625}
1626
1627static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
1628{
1629 return skb->head + skb->inner_network_header;
1630}
1631
1632static inline void skb_reset_inner_network_header(struct sk_buff *skb)
1633{
1634 skb->inner_network_header = skb->data - skb->head;
1635}
1636
1637static inline void skb_set_inner_network_header(struct sk_buff *skb,
1638 const int offset)
1639{
1640 skb_reset_inner_network_header(skb);
1641 skb->inner_network_header += offset;
1642}
1643
aefbd2b3
PS
1644static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb)
1645{
1646 return skb->head + skb->inner_mac_header;
1647}
1648
1649static inline void skb_reset_inner_mac_header(struct sk_buff *skb)
1650{
1651 skb->inner_mac_header = skb->data - skb->head;
1652}
1653
1654static inline void skb_set_inner_mac_header(struct sk_buff *skb,
1655 const int offset)
1656{
1657 skb_reset_inner_mac_header(skb);
1658 skb->inner_mac_header += offset;
1659}
fda55eca
ED
1660static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
1661{
35d04610 1662 return skb->transport_header != (typeof(skb->transport_header))~0U;
fda55eca
ED
1663}
1664
9c70220b
ACM
1665static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1666{
2e07fa9c 1667 return skb->head + skb->transport_header;
9c70220b
ACM
1668}
1669
badff6d0
ACM
1670static inline void skb_reset_transport_header(struct sk_buff *skb)
1671{
2e07fa9c 1672 skb->transport_header = skb->data - skb->head;
badff6d0
ACM
1673}
1674
967b05f6
ACM
1675static inline void skb_set_transport_header(struct sk_buff *skb,
1676 const int offset)
1677{
2e07fa9c
ACM
1678 skb_reset_transport_header(skb);
1679 skb->transport_header += offset;
ea2ae17d
ACM
1680}
1681
d56f90a7
ACM
1682static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1683{
2e07fa9c 1684 return skb->head + skb->network_header;
d56f90a7
ACM
1685}
1686
c1d2bbe1
ACM
1687static inline void skb_reset_network_header(struct sk_buff *skb)
1688{
2e07fa9c 1689 skb->network_header = skb->data - skb->head;
c1d2bbe1
ACM
1690}
1691
c14d2450
ACM
1692static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1693{
2e07fa9c
ACM
1694 skb_reset_network_header(skb);
1695 skb->network_header += offset;
c14d2450
ACM
1696}
1697
2e07fa9c 1698static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
bbe735e4 1699{
2e07fa9c 1700 return skb->head + skb->mac_header;
bbe735e4
ACM
1701}
1702
2e07fa9c 1703static inline int skb_mac_header_was_set(const struct sk_buff *skb)
cfe1fc77 1704{
35d04610 1705 return skb->mac_header != (typeof(skb->mac_header))~0U;
2e07fa9c
ACM
1706}
1707
1708static inline void skb_reset_mac_header(struct sk_buff *skb)
1709{
1710 skb->mac_header = skb->data - skb->head;
1711}
1712
1713static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1714{
1715 skb_reset_mac_header(skb);
1716 skb->mac_header += offset;
1717}
1718
0e3da5bb
TT
1719static inline void skb_pop_mac_header(struct sk_buff *skb)
1720{
1721 skb->mac_header = skb->network_header;
1722}
1723
fbbdb8f0
YX
1724static inline void skb_probe_transport_header(struct sk_buff *skb,
1725 const int offset_hint)
1726{
1727 struct flow_keys keys;
1728
1729 if (skb_transport_header_was_set(skb))
1730 return;
1731 else if (skb_flow_dissect(skb, &keys))
1732 skb_set_transport_header(skb, keys.thoff);
1733 else
1734 skb_set_transport_header(skb, offset_hint);
1735}
1736
03606895
ED
1737static inline void skb_mac_header_rebuild(struct sk_buff *skb)
1738{
1739 if (skb_mac_header_was_set(skb)) {
1740 const unsigned char *old_mac = skb_mac_header(skb);
1741
1742 skb_set_mac_header(skb, -skb->mac_len);
1743 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
1744 }
1745}
1746
04fb451e
MM
1747static inline int skb_checksum_start_offset(const struct sk_buff *skb)
1748{
1749 return skb->csum_start - skb_headroom(skb);
1750}
1751
2e07fa9c
ACM
1752static inline int skb_transport_offset(const struct sk_buff *skb)
1753{
1754 return skb_transport_header(skb) - skb->data;
1755}
1756
1757static inline u32 skb_network_header_len(const struct sk_buff *skb)
1758{
1759 return skb->transport_header - skb->network_header;
1760}
1761
6a674e9c
JG
1762static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
1763{
1764 return skb->inner_transport_header - skb->inner_network_header;
1765}
1766
2e07fa9c
ACM
1767static inline int skb_network_offset(const struct sk_buff *skb)
1768{
1769 return skb_network_header(skb) - skb->data;
1770}
48d49d0c 1771
6a674e9c
JG
1772static inline int skb_inner_network_offset(const struct sk_buff *skb)
1773{
1774 return skb_inner_network_header(skb) - skb->data;
1775}
1776
f9599ce1
CG
1777static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
1778{
1779 return pskb_may_pull(skb, skb_network_offset(skb) + len);
1780}
1781
1da177e4
LT
1782/*
1783 * CPUs often take a performance hit when accessing unaligned memory
1784 * locations. The actual performance hit varies, it can be small if the
1785 * hardware handles it or large if we have to take an exception and fix it
1786 * in software.
1787 *
1788 * Since an ethernet header is 14 bytes network drivers often end up with
1789 * the IP header at an unaligned offset. The IP header can be aligned by
1790 * shifting the start of the packet by 2 bytes. Drivers should do this
1791 * with:
1792 *
8660c124 1793 * skb_reserve(skb, NET_IP_ALIGN);
1da177e4
LT
1794 *
1795 * The downside to this alignment of the IP header is that the DMA is now
1796 * unaligned. On some architectures the cost of an unaligned DMA is high
1797 * and this cost outweighs the gains made by aligning the IP header.
8660c124 1798 *
1da177e4
LT
1799 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
1800 * to be overridden.
1801 */
1802#ifndef NET_IP_ALIGN
1803#define NET_IP_ALIGN 2
1804#endif
1805
025be81e
AB
1806/*
1807 * The networking layer reserves some headroom in skb data (via
1808 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
1809 * the header has to grow. In the default case, if the header has to grow
d6301d3d 1810 * 32 bytes or less we avoid the reallocation.
025be81e
AB
1811 *
1812 * Unfortunately this headroom changes the DMA alignment of the resulting
1813 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
1814 * on some architectures. An architecture can override this value,
1815 * perhaps setting it to a cacheline in size (since that will maintain
1816 * cacheline alignment of the DMA). It must be a power of 2.
1817 *
d6301d3d 1818 * Various parts of the networking layer expect at least 32 bytes of
025be81e 1819 * headroom, you should not reduce this.
5933dd2f
ED
1820 *
1821 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
1822 * to reduce average number of cache lines per packet.
1823 * get_rps_cpus() for example only access one 64 bytes aligned block :
18e8c134 1824 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
025be81e
AB
1825 */
1826#ifndef NET_SKB_PAD
5933dd2f 1827#define NET_SKB_PAD max(32, L1_CACHE_BYTES)
025be81e
AB
1828#endif
1829
7965bd4d 1830int ___pskb_trim(struct sk_buff *skb, unsigned int len);
1da177e4
LT
1831
1832static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
1833{
c4264f27 1834 if (unlikely(skb_is_nonlinear(skb))) {
3cc0e873
HX
1835 WARN_ON(1);
1836 return;
1837 }
27a884dc
ACM
1838 skb->len = len;
1839 skb_set_tail_pointer(skb, len);
1da177e4
LT
1840}
1841
7965bd4d 1842void skb_trim(struct sk_buff *skb, unsigned int len);
1da177e4
LT
1843
1844static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
1845{
3cc0e873
HX
1846 if (skb->data_len)
1847 return ___pskb_trim(skb, len);
1848 __skb_trim(skb, len);
1849 return 0;
1da177e4
LT
1850}
1851
1852static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
1853{
1854 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
1855}
1856
e9fa4f7b
HX
1857/**
1858 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
1859 * @skb: buffer to alter
1860 * @len: new length
1861 *
1862 * This is identical to pskb_trim except that the caller knows that
1863 * the skb is not cloned so we should never get an error due to out-
1864 * of-memory.
1865 */
1866static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
1867{
1868 int err = pskb_trim(skb, len);
1869 BUG_ON(err);
1870}
1871
1da177e4
LT
1872/**
1873 * skb_orphan - orphan a buffer
1874 * @skb: buffer to orphan
1875 *
1876 * If a buffer currently has an owner then we call the owner's
1877 * destructor function and make the @skb unowned. The buffer continues
1878 * to exist but is no longer charged to its former owner.
1879 */
1880static inline void skb_orphan(struct sk_buff *skb)
1881{
c34a7612 1882 if (skb->destructor) {
1da177e4 1883 skb->destructor(skb);
c34a7612
ED
1884 skb->destructor = NULL;
1885 skb->sk = NULL;
376c7311
ED
1886 } else {
1887 BUG_ON(skb->sk);
c34a7612 1888 }
1da177e4
LT
1889}
1890
a353e0ce
MT
1891/**
1892 * skb_orphan_frags - orphan the frags contained in a buffer
1893 * @skb: buffer to orphan frags from
1894 * @gfp_mask: allocation mask for replacement pages
1895 *
1896 * For each frag in the SKB which needs a destructor (i.e. has an
1897 * owner) create a copy of that frag and release the original
1898 * page by calling the destructor.
1899 */
1900static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
1901{
1902 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY)))
1903 return 0;
1904 return skb_copy_ubufs(skb, gfp_mask);
1905}
1906
1da177e4
LT
1907/**
1908 * __skb_queue_purge - empty a list
1909 * @list: list to empty
1910 *
1911 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1912 * the list and one reference dropped. This function does not take the
1913 * list lock and the caller must hold the relevant locks to use it.
1914 */
7965bd4d 1915void skb_queue_purge(struct sk_buff_head *list);
1da177e4
LT
1916static inline void __skb_queue_purge(struct sk_buff_head *list)
1917{
1918 struct sk_buff *skb;
1919 while ((skb = __skb_dequeue(list)) != NULL)
1920 kfree_skb(skb);
1921}
1922
e5e67305
AD
1923#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
1924#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
1925#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
1926
7965bd4d 1927void *netdev_alloc_frag(unsigned int fragsz);
1da177e4 1928
7965bd4d
JP
1929struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
1930 gfp_t gfp_mask);
8af27456
CH
1931
1932/**
1933 * netdev_alloc_skb - allocate an skbuff for rx on a specific device
1934 * @dev: network device to receive on
1935 * @length: length to allocate
1936 *
1937 * Allocate a new &sk_buff and assign it a usage count of one. The
1938 * buffer has unspecified headroom built in. Users should allocate
1939 * the headroom they think they need without accounting for the
1940 * built in space. The built in space is used for optimisations.
1941 *
1942 * %NULL is returned if there is no free memory. Although this function
1943 * allocates memory it can be called from an interrupt.
1944 */
1945static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
6f532612 1946 unsigned int length)
8af27456
CH
1947{
1948 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
1949}
1950
6f532612
ED
1951/* legacy helper around __netdev_alloc_skb() */
1952static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1953 gfp_t gfp_mask)
1954{
1955 return __netdev_alloc_skb(NULL, length, gfp_mask);
1956}
1957
1958/* legacy helper around netdev_alloc_skb() */
1959static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1960{
1961 return netdev_alloc_skb(NULL, length);
1962}
1963
1964
4915a0de
ED
1965static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
1966 unsigned int length, gfp_t gfp)
61321bbd 1967{
4915a0de 1968 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
61321bbd
ED
1969
1970 if (NET_IP_ALIGN && skb)
1971 skb_reserve(skb, NET_IP_ALIGN);
1972 return skb;
1973}
1974
4915a0de
ED
1975static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
1976 unsigned int length)
1977{
1978 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
1979}
1980
bc6fc9fa
FF
1981/**
1982 * __skb_alloc_pages - allocate pages for ps-rx on a skb and preserve pfmemalloc data
0614002b
MG
1983 * @gfp_mask: alloc_pages_node mask. Set __GFP_NOMEMALLOC if not for network packet RX
1984 * @skb: skb to set pfmemalloc on if __GFP_MEMALLOC is used
1985 * @order: size of the allocation
1986 *
1987 * Allocate a new page.
1988 *
1989 * %NULL is returned if there is no free memory.
1990*/
1991static inline struct page *__skb_alloc_pages(gfp_t gfp_mask,
1992 struct sk_buff *skb,
1993 unsigned int order)
1994{
1995 struct page *page;
1996
1997 gfp_mask |= __GFP_COLD;
1998
1999 if (!(gfp_mask & __GFP_NOMEMALLOC))
2000 gfp_mask |= __GFP_MEMALLOC;
2001
2002 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
2003 if (skb && page && page->pfmemalloc)
2004 skb->pfmemalloc = true;
2005
2006 return page;
2007}
2008
2009/**
2010 * __skb_alloc_page - allocate a page for ps-rx for a given skb and preserve pfmemalloc data
2011 * @gfp_mask: alloc_pages_node mask. Set __GFP_NOMEMALLOC if not for network packet RX
2012 * @skb: skb to set pfmemalloc on if __GFP_MEMALLOC is used
2013 *
2014 * Allocate a new page.
2015 *
2016 * %NULL is returned if there is no free memory.
2017 */
2018static inline struct page *__skb_alloc_page(gfp_t gfp_mask,
2019 struct sk_buff *skb)
2020{
2021 return __skb_alloc_pages(gfp_mask, skb, 0);
2022}
2023
2024/**
2025 * skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page
2026 * @page: The page that was allocated from skb_alloc_page
2027 * @skb: The skb that may need pfmemalloc set
2028 */
2029static inline void skb_propagate_pfmemalloc(struct page *page,
2030 struct sk_buff *skb)
2031{
2032 if (page && page->pfmemalloc)
2033 skb->pfmemalloc = true;
2034}
2035
131ea667
IC
2036/**
2037 * skb_frag_page - retrieve the page refered to by a paged fragment
2038 * @frag: the paged fragment
2039 *
2040 * Returns the &struct page associated with @frag.
2041 */
2042static inline struct page *skb_frag_page(const skb_frag_t *frag)
2043{
a8605c60 2044 return frag->page.p;
131ea667
IC
2045}
2046
2047/**
2048 * __skb_frag_ref - take an addition reference on a paged fragment.
2049 * @frag: the paged fragment
2050 *
2051 * Takes an additional reference on the paged fragment @frag.
2052 */
2053static inline void __skb_frag_ref(skb_frag_t *frag)
2054{
2055 get_page(skb_frag_page(frag));
2056}
2057
2058/**
2059 * skb_frag_ref - take an addition reference on a paged fragment of an skb.
2060 * @skb: the buffer
2061 * @f: the fragment offset.
2062 *
2063 * Takes an additional reference on the @f'th paged fragment of @skb.
2064 */
2065static inline void skb_frag_ref(struct sk_buff *skb, int f)
2066{
2067 __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
2068}
2069
2070/**
2071 * __skb_frag_unref - release a reference on a paged fragment.
2072 * @frag: the paged fragment
2073 *
2074 * Releases a reference on the paged fragment @frag.
2075 */
2076static inline void __skb_frag_unref(skb_frag_t *frag)
2077{
2078 put_page(skb_frag_page(frag));
2079}
2080
2081/**
2082 * skb_frag_unref - release a reference on a paged fragment of an skb.
2083 * @skb: the buffer
2084 * @f: the fragment offset
2085 *
2086 * Releases a reference on the @f'th paged fragment of @skb.
2087 */
2088static inline void skb_frag_unref(struct sk_buff *skb, int f)
2089{
2090 __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
2091}
2092
2093/**
2094 * skb_frag_address - gets the address of the data contained in a paged fragment
2095 * @frag: the paged fragment buffer
2096 *
2097 * Returns the address of the data within @frag. The page must already
2098 * be mapped.
2099 */
2100static inline void *skb_frag_address(const skb_frag_t *frag)
2101{
2102 return page_address(skb_frag_page(frag)) + frag->page_offset;
2103}
2104
2105/**
2106 * skb_frag_address_safe - gets the address of the data contained in a paged fragment
2107 * @frag: the paged fragment buffer
2108 *
2109 * Returns the address of the data within @frag. Checks that the page
2110 * is mapped and returns %NULL otherwise.
2111 */
2112static inline void *skb_frag_address_safe(const skb_frag_t *frag)
2113{
2114 void *ptr = page_address(skb_frag_page(frag));
2115 if (unlikely(!ptr))
2116 return NULL;
2117
2118 return ptr + frag->page_offset;
2119}
2120
2121/**
2122 * __skb_frag_set_page - sets the page contained in a paged fragment
2123 * @frag: the paged fragment
2124 * @page: the page to set
2125 *
2126 * Sets the fragment @frag to contain @page.
2127 */
2128static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
2129{
a8605c60 2130 frag->page.p = page;
131ea667
IC
2131}
2132
2133/**
2134 * skb_frag_set_page - sets the page contained in a paged fragment of an skb
2135 * @skb: the buffer
2136 * @f: the fragment offset
2137 * @page: the page to set
2138 *
2139 * Sets the @f'th fragment of @skb to contain @page.
2140 */
2141static inline void skb_frag_set_page(struct sk_buff *skb, int f,
2142 struct page *page)
2143{
2144 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
2145}
2146
400dfd3a
ED
2147bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
2148
131ea667
IC
2149/**
2150 * skb_frag_dma_map - maps a paged fragment via the DMA API
f83347df 2151 * @dev: the device to map the fragment to
131ea667
IC
2152 * @frag: the paged fragment to map
2153 * @offset: the offset within the fragment (starting at the
2154 * fragment's own offset)
2155 * @size: the number of bytes to map
f83347df 2156 * @dir: the direction of the mapping (%PCI_DMA_*)
131ea667
IC
2157 *
2158 * Maps the page associated with @frag to @device.
2159 */
2160static inline dma_addr_t skb_frag_dma_map(struct device *dev,
2161 const skb_frag_t *frag,
2162 size_t offset, size_t size,
2163 enum dma_data_direction dir)
2164{
2165 return dma_map_page(dev, skb_frag_page(frag),
2166 frag->page_offset + offset, size, dir);
2167}
2168
117632e6
ED
2169static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
2170 gfp_t gfp_mask)
2171{
2172 return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
2173}
2174
334a8132
PM
2175/**
2176 * skb_clone_writable - is the header of a clone writable
2177 * @skb: buffer to check
2178 * @len: length up to which to write
2179 *
2180 * Returns true if modifying the header part of the cloned buffer
2181 * does not requires the data to be copied.
2182 */
05bdd2f1 2183static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
334a8132
PM
2184{
2185 return !skb_header_cloned(skb) &&
2186 skb_headroom(skb) + len <= skb->hdr_len;
2187}
2188
d9cc2048
HX
2189static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
2190 int cloned)
2191{
2192 int delta = 0;
2193
d9cc2048
HX
2194 if (headroom > skb_headroom(skb))
2195 delta = headroom - skb_headroom(skb);
2196
2197 if (delta || cloned)
2198 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
2199 GFP_ATOMIC);
2200 return 0;
2201}
2202
1da177e4
LT
2203/**
2204 * skb_cow - copy header of skb when it is required
2205 * @skb: buffer to cow
2206 * @headroom: needed headroom
2207 *
2208 * If the skb passed lacks sufficient headroom or its data part
2209 * is shared, data is reallocated. If reallocation fails, an error
2210 * is returned and original skb is not changed.
2211 *
2212 * The result is skb with writable area skb->head...skb->tail
2213 * and at least @headroom of space at head.
2214 */
2215static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
2216{
d9cc2048
HX
2217 return __skb_cow(skb, headroom, skb_cloned(skb));
2218}
1da177e4 2219
d9cc2048
HX
2220/**
2221 * skb_cow_head - skb_cow but only making the head writable
2222 * @skb: buffer to cow
2223 * @headroom: needed headroom
2224 *
2225 * This function is identical to skb_cow except that we replace the
2226 * skb_cloned check by skb_header_cloned. It should be used when
2227 * you only need to push on some header and do not need to modify
2228 * the data.
2229 */
2230static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
2231{
2232 return __skb_cow(skb, headroom, skb_header_cloned(skb));
1da177e4
LT
2233}
2234
2235/**
2236 * skb_padto - pad an skbuff up to a minimal size
2237 * @skb: buffer to pad
2238 * @len: minimal length
2239 *
2240 * Pads up a buffer to ensure the trailing bytes exist and are
2241 * blanked. If the buffer already contains sufficient data it
5b057c6b
HX
2242 * is untouched. Otherwise it is extended. Returns zero on
2243 * success. The skb is freed on error.
1da177e4
LT
2244 */
2245
5b057c6b 2246static inline int skb_padto(struct sk_buff *skb, unsigned int len)
1da177e4
LT
2247{
2248 unsigned int size = skb->len;
2249 if (likely(size >= len))
5b057c6b 2250 return 0;
987c402a 2251 return skb_pad(skb, len - size);
1da177e4
LT
2252}
2253
2254static inline int skb_add_data(struct sk_buff *skb,
2255 char __user *from, int copy)
2256{
2257 const int off = skb->len;
2258
2259 if (skb->ip_summed == CHECKSUM_NONE) {
2260 int err = 0;
5084205f 2261 __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
1da177e4
LT
2262 copy, 0, &err);
2263 if (!err) {
2264 skb->csum = csum_block_add(skb->csum, csum, off);
2265 return 0;
2266 }
2267 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
2268 return 0;
2269
2270 __skb_trim(skb, off);
2271 return -EFAULT;
2272}
2273
38ba0a65
ED
2274static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
2275 const struct page *page, int off)
1da177e4
LT
2276{
2277 if (i) {
9e903e08 2278 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1da177e4 2279
ea2ab693 2280 return page == skb_frag_page(frag) &&
9e903e08 2281 off == frag->page_offset + skb_frag_size(frag);
1da177e4 2282 }
38ba0a65 2283 return false;
1da177e4
LT
2284}
2285
364c6bad
HX
2286static inline int __skb_linearize(struct sk_buff *skb)
2287{
2288 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
2289}
2290
1da177e4
LT
2291/**
2292 * skb_linearize - convert paged skb to linear one
2293 * @skb: buffer to linarize
1da177e4
LT
2294 *
2295 * If there is no free memory -ENOMEM is returned, otherwise zero
2296 * is returned and the old skb data released.
2297 */
364c6bad
HX
2298static inline int skb_linearize(struct sk_buff *skb)
2299{
2300 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
2301}
2302
cef401de
ED
2303/**
2304 * skb_has_shared_frag - can any frag be overwritten
2305 * @skb: buffer to test
2306 *
2307 * Return true if the skb has at least one frag that might be modified
2308 * by an external entity (as in vmsplice()/sendfile())
2309 */
2310static inline bool skb_has_shared_frag(const struct sk_buff *skb)
2311{
c9af6db4
PS
2312 return skb_is_nonlinear(skb) &&
2313 skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
cef401de
ED
2314}
2315
364c6bad
HX
2316/**
2317 * skb_linearize_cow - make sure skb is linear and writable
2318 * @skb: buffer to process
2319 *
2320 * If there is no free memory -ENOMEM is returned, otherwise zero
2321 * is returned and the old skb data released.
2322 */
2323static inline int skb_linearize_cow(struct sk_buff *skb)
1da177e4 2324{
364c6bad
HX
2325 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
2326 __skb_linearize(skb) : 0;
1da177e4
LT
2327}
2328
2329/**
2330 * skb_postpull_rcsum - update checksum for received skb after pull
2331 * @skb: buffer to update
2332 * @start: start of data before pull
2333 * @len: length of data pulled
2334 *
2335 * After doing a pull on a received packet, you need to call this to
84fa7933
PM
2336 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
2337 * CHECKSUM_NONE so that it can be recomputed from scratch.
1da177e4
LT
2338 */
2339
2340static inline void skb_postpull_rcsum(struct sk_buff *skb,
cbb042f9 2341 const void *start, unsigned int len)
1da177e4 2342{
84fa7933 2343 if (skb->ip_summed == CHECKSUM_COMPLETE)
1da177e4
LT
2344 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
2345}
2346
cbb042f9
HX
2347unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
2348
7ce5a27f
DM
2349/**
2350 * pskb_trim_rcsum - trim received skb and update checksum
2351 * @skb: buffer to trim
2352 * @len: new length
2353 *
2354 * This is exactly the same as pskb_trim except that it ensures the
2355 * checksum of received packets are still valid after the operation.
2356 */
2357
2358static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
2359{
2360 if (likely(len >= skb->len))
2361 return 0;
2362 if (skb->ip_summed == CHECKSUM_COMPLETE)
2363 skb->ip_summed = CHECKSUM_NONE;
2364 return __pskb_trim(skb, len);
2365}
2366
1da177e4
LT
2367#define skb_queue_walk(queue, skb) \
2368 for (skb = (queue)->next; \
a1e4891f 2369 skb != (struct sk_buff *)(queue); \
1da177e4
LT
2370 skb = skb->next)
2371
46f8914e
JC
2372#define skb_queue_walk_safe(queue, skb, tmp) \
2373 for (skb = (queue)->next, tmp = skb->next; \
2374 skb != (struct sk_buff *)(queue); \
2375 skb = tmp, tmp = skb->next)
2376
1164f52a 2377#define skb_queue_walk_from(queue, skb) \
a1e4891f 2378 for (; skb != (struct sk_buff *)(queue); \
1164f52a
DM
2379 skb = skb->next)
2380
2381#define skb_queue_walk_from_safe(queue, skb, tmp) \
2382 for (tmp = skb->next; \
2383 skb != (struct sk_buff *)(queue); \
2384 skb = tmp, tmp = skb->next)
2385
300ce174
SH
2386#define skb_queue_reverse_walk(queue, skb) \
2387 for (skb = (queue)->prev; \
a1e4891f 2388 skb != (struct sk_buff *)(queue); \
300ce174
SH
2389 skb = skb->prev)
2390
686a2955
DM
2391#define skb_queue_reverse_walk_safe(queue, skb, tmp) \
2392 for (skb = (queue)->prev, tmp = skb->prev; \
2393 skb != (struct sk_buff *)(queue); \
2394 skb = tmp, tmp = skb->prev)
2395
2396#define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
2397 for (tmp = skb->prev; \
2398 skb != (struct sk_buff *)(queue); \
2399 skb = tmp, tmp = skb->prev)
1da177e4 2400
21dc3301 2401static inline bool skb_has_frag_list(const struct sk_buff *skb)
ee039871
DM
2402{
2403 return skb_shinfo(skb)->frag_list != NULL;
2404}
2405
2406static inline void skb_frag_list_init(struct sk_buff *skb)
2407{
2408 skb_shinfo(skb)->frag_list = NULL;
2409}
2410
2411static inline void skb_frag_add_head(struct sk_buff *skb, struct sk_buff *frag)
2412{
2413 frag->next = skb_shinfo(skb)->frag_list;
2414 skb_shinfo(skb)->frag_list = frag;
2415}
2416
2417#define skb_walk_frags(skb, iter) \
2418 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
2419
7965bd4d
JP
2420struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
2421 int *peeked, int *off, int *err);
2422struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock,
2423 int *err);
2424unsigned int datagram_poll(struct file *file, struct socket *sock,
2425 struct poll_table_struct *wait);
2426int skb_copy_datagram_iovec(const struct sk_buff *from, int offset,
2427 struct iovec *to, int size);
2428int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, int hlen,
2429 struct iovec *iov);
2430int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
2431 const struct iovec *from, int from_offset,
2432 int len);
2433int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *frm,
2434 int offset, size_t count);
2435int skb_copy_datagram_const_iovec(const struct sk_buff *from, int offset,
2436 const struct iovec *to, int to_offset,
2437 int size);
2438void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
2439void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb);
2440int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags);
7965bd4d
JP
2441int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
2442int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len);
2443__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to,
2444 int len, __wsum csum);
2445int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
2446 struct pipe_inode_info *pipe, unsigned int len,
2447 unsigned int flags);
2448void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
af2806f8
TG
2449unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
2450void skb_zerocopy(struct sk_buff *to, const struct sk_buff *from,
2451 int len, int hlen);
7965bd4d
JP
2452void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len);
2453int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen);
2454void skb_scrub_packet(struct sk_buff *skb, bool xnet);
de960aa9 2455unsigned int skb_gso_transport_seglen(const struct sk_buff *skb);
7965bd4d 2456struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features);
20380731 2457
2817a336
DB
2458struct skb_checksum_ops {
2459 __wsum (*update)(const void *mem, int len, __wsum wsum);
2460 __wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len);
2461};
2462
2463__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2464 __wsum csum, const struct skb_checksum_ops *ops);
2465__wsum skb_checksum(const struct sk_buff *skb, int offset, int len,
2466 __wsum csum);
2467
1da177e4
LT
2468static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
2469 int len, void *buffer)
2470{
2471 int hlen = skb_headlen(skb);
2472
55820ee2 2473 if (hlen - offset >= len)
1da177e4
LT
2474 return skb->data + offset;
2475
2476 if (skb_copy_bits(skb, offset, buffer, len) < 0)
2477 return NULL;
2478
2479 return buffer;
2480}
2481
4262e5cc
DB
2482/**
2483 * skb_needs_linearize - check if we need to linearize a given skb
2484 * depending on the given device features.
2485 * @skb: socket buffer to check
2486 * @features: net device features
2487 *
2488 * Returns true if either:
2489 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
2490 * 2. skb is fragmented and the device does not support SG.
2491 */
2492static inline bool skb_needs_linearize(struct sk_buff *skb,
2493 netdev_features_t features)
2494{
2495 return skb_is_nonlinear(skb) &&
2496 ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) ||
2497 (skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG)));
2498}
2499
d626f62b
ACM
2500static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
2501 void *to,
2502 const unsigned int len)
2503{
2504 memcpy(to, skb->data, len);
2505}
2506
2507static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
2508 const int offset, void *to,
2509 const unsigned int len)
2510{
2511 memcpy(to, skb->data + offset, len);
2512}
2513
27d7ff46
ACM
2514static inline void skb_copy_to_linear_data(struct sk_buff *skb,
2515 const void *from,
2516 const unsigned int len)
2517{
2518 memcpy(skb->data, from, len);
2519}
2520
2521static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
2522 const int offset,
2523 const void *from,
2524 const unsigned int len)
2525{
2526 memcpy(skb->data + offset, from, len);
2527}
2528
7965bd4d 2529void skb_init(void);
1da177e4 2530
ac45f602
PO
2531static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
2532{
2533 return skb->tstamp;
2534}
2535
a61bbcf2
PM
2536/**
2537 * skb_get_timestamp - get timestamp from a skb
2538 * @skb: skb to get stamp from
2539 * @stamp: pointer to struct timeval to store stamp in
2540 *
2541 * Timestamps are stored in the skb as offsets to a base timestamp.
2542 * This function converts the offset back to a struct timeval and stores
2543 * it in stamp.
2544 */
ac45f602
PO
2545static inline void skb_get_timestamp(const struct sk_buff *skb,
2546 struct timeval *stamp)
a61bbcf2 2547{
b7aa0bf7 2548 *stamp = ktime_to_timeval(skb->tstamp);
a61bbcf2
PM
2549}
2550
ac45f602
PO
2551static inline void skb_get_timestampns(const struct sk_buff *skb,
2552 struct timespec *stamp)
2553{
2554 *stamp = ktime_to_timespec(skb->tstamp);
2555}
2556
b7aa0bf7 2557static inline void __net_timestamp(struct sk_buff *skb)
a61bbcf2 2558{
b7aa0bf7 2559 skb->tstamp = ktime_get_real();
a61bbcf2
PM
2560}
2561
164891aa
SH
2562static inline ktime_t net_timedelta(ktime_t t)
2563{
2564 return ktime_sub(ktime_get_real(), t);
2565}
2566
b9ce204f
IJ
2567static inline ktime_t net_invalid_timestamp(void)
2568{
2569 return ktime_set(0, 0);
2570}
a61bbcf2 2571
7965bd4d 2572void skb_timestamping_init(void);
c1f19b51
RC
2573
2574#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2575
7965bd4d
JP
2576void skb_clone_tx_timestamp(struct sk_buff *skb);
2577bool skb_defer_rx_timestamp(struct sk_buff *skb);
c1f19b51
RC
2578
2579#else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
2580
2581static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
2582{
2583}
2584
2585static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
2586{
2587 return false;
2588}
2589
2590#endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
2591
2592/**
2593 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
2594 *
da92b194
RC
2595 * PHY drivers may accept clones of transmitted packets for
2596 * timestamping via their phy_driver.txtstamp method. These drivers
2597 * must call this function to return the skb back to the stack, with
2598 * or without a timestamp.
2599 *
c1f19b51 2600 * @skb: clone of the the original outgoing packet
da92b194 2601 * @hwtstamps: hardware time stamps, may be NULL if not available
c1f19b51
RC
2602 *
2603 */
2604void skb_complete_tx_timestamp(struct sk_buff *skb,
2605 struct skb_shared_hwtstamps *hwtstamps);
2606
ac45f602
PO
2607/**
2608 * skb_tstamp_tx - queue clone of skb with send time stamps
2609 * @orig_skb: the original outgoing packet
2610 * @hwtstamps: hardware time stamps, may be NULL if not available
2611 *
2612 * If the skb has a socket associated, then this function clones the
2613 * skb (thus sharing the actual data and optional structures), stores
2614 * the optional hardware time stamping information (if non NULL) or
2615 * generates a software time stamp (otherwise), then queues the clone
2616 * to the error queue of the socket. Errors are silently ignored.
2617 */
7965bd4d
JP
2618void skb_tstamp_tx(struct sk_buff *orig_skb,
2619 struct skb_shared_hwtstamps *hwtstamps);
ac45f602 2620
4507a715
RC
2621static inline void sw_tx_timestamp(struct sk_buff *skb)
2622{
2244d07b
OH
2623 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
2624 !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
4507a715
RC
2625 skb_tstamp_tx(skb, NULL);
2626}
2627
2628/**
2629 * skb_tx_timestamp() - Driver hook for transmit timestamping
2630 *
2631 * Ethernet MAC Drivers should call this function in their hard_xmit()
4ff75b7c 2632 * function immediately before giving the sk_buff to the MAC hardware.
4507a715 2633 *
73409f3b
DM
2634 * Specifically, one should make absolutely sure that this function is
2635 * called before TX completion of this packet can trigger. Otherwise
2636 * the packet could potentially already be freed.
2637 *
4507a715
RC
2638 * @skb: A socket buffer.
2639 */
2640static inline void skb_tx_timestamp(struct sk_buff *skb)
2641{
c1f19b51 2642 skb_clone_tx_timestamp(skb);
4507a715
RC
2643 sw_tx_timestamp(skb);
2644}
2645
6e3e939f
JB
2646/**
2647 * skb_complete_wifi_ack - deliver skb with wifi status
2648 *
2649 * @skb: the original outgoing packet
2650 * @acked: ack status
2651 *
2652 */
2653void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
2654
7965bd4d
JP
2655__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
2656__sum16 __skb_checksum_complete(struct sk_buff *skb);
fb286bb2 2657
60476372
HX
2658static inline int skb_csum_unnecessary(const struct sk_buff *skb)
2659{
2660 return skb->ip_summed & CHECKSUM_UNNECESSARY;
2661}
2662
fb286bb2
HX
2663/**
2664 * skb_checksum_complete - Calculate checksum of an entire packet
2665 * @skb: packet to process
2666 *
2667 * This function calculates the checksum over the entire packet plus
2668 * the value of skb->csum. The latter can be used to supply the
2669 * checksum of a pseudo header as used by TCP/UDP. It returns the
2670 * checksum.
2671 *
2672 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
2673 * this function can be used to verify that checksum on received
2674 * packets. In that case the function should return zero if the
2675 * checksum is correct. In particular, this function will return zero
2676 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
2677 * hardware has already verified the correctness of the checksum.
2678 */
4381ca3c 2679static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
fb286bb2 2680{
60476372
HX
2681 return skb_csum_unnecessary(skb) ?
2682 0 : __skb_checksum_complete(skb);
fb286bb2
HX
2683}
2684
5f79e0f9 2685#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
7965bd4d 2686void nf_conntrack_destroy(struct nf_conntrack *nfct);
1da177e4
LT
2687static inline void nf_conntrack_put(struct nf_conntrack *nfct)
2688{
2689 if (nfct && atomic_dec_and_test(&nfct->use))
de6e05c4 2690 nf_conntrack_destroy(nfct);
1da177e4
LT
2691}
2692static inline void nf_conntrack_get(struct nf_conntrack *nfct)
2693{
2694 if (nfct)
2695 atomic_inc(&nfct->use);
2696}
2fc72c7b 2697#endif
1da177e4
LT
2698#ifdef CONFIG_BRIDGE_NETFILTER
2699static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
2700{
2701 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
2702 kfree(nf_bridge);
2703}
2704static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
2705{
2706 if (nf_bridge)
2707 atomic_inc(&nf_bridge->use);
2708}
2709#endif /* CONFIG_BRIDGE_NETFILTER */
a193a4ab
PM
2710static inline void nf_reset(struct sk_buff *skb)
2711{
5f79e0f9 2712#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
a193a4ab
PM
2713 nf_conntrack_put(skb->nfct);
2714 skb->nfct = NULL;
2fc72c7b 2715#endif
a193a4ab
PM
2716#ifdef CONFIG_BRIDGE_NETFILTER
2717 nf_bridge_put(skb->nf_bridge);
2718 skb->nf_bridge = NULL;
2719#endif
2720}
2721
124dff01
PM
2722static inline void nf_reset_trace(struct sk_buff *skb)
2723{
478b360a 2724#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
130549fe
G
2725 skb->nf_trace = 0;
2726#endif
a193a4ab
PM
2727}
2728
edda553c
YK
2729/* Note: This doesn't put any conntrack and bridge info in dst. */
2730static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2731{
5f79e0f9 2732#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
edda553c
YK
2733 dst->nfct = src->nfct;
2734 nf_conntrack_get(src->nfct);
2735 dst->nfctinfo = src->nfctinfo;
2fc72c7b 2736#endif
edda553c
YK
2737#ifdef CONFIG_BRIDGE_NETFILTER
2738 dst->nf_bridge = src->nf_bridge;
2739 nf_bridge_get(src->nf_bridge);
2740#endif
478b360a
FW
2741#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
2742 dst->nf_trace = src->nf_trace;
2743#endif
edda553c
YK
2744}
2745
e7ac05f3
YK
2746static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2747{
e7ac05f3 2748#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
5f79e0f9 2749 nf_conntrack_put(dst->nfct);
2fc72c7b 2750#endif
e7ac05f3
YK
2751#ifdef CONFIG_BRIDGE_NETFILTER
2752 nf_bridge_put(dst->nf_bridge);
2753#endif
2754 __nf_copy(dst, src);
2755}
2756
984bc16c
JM
2757#ifdef CONFIG_NETWORK_SECMARK
2758static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
2759{
2760 to->secmark = from->secmark;
2761}
2762
2763static inline void skb_init_secmark(struct sk_buff *skb)
2764{
2765 skb->secmark = 0;
2766}
2767#else
2768static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
2769{ }
2770
2771static inline void skb_init_secmark(struct sk_buff *skb)
2772{ }
2773#endif
2774
f25f4e44
PWJ
2775static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
2776{
f25f4e44 2777 skb->queue_mapping = queue_mapping;
f25f4e44
PWJ
2778}
2779
9247744e 2780static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
4e3ab47a 2781{
4e3ab47a 2782 return skb->queue_mapping;
4e3ab47a
PE
2783}
2784
f25f4e44
PWJ
2785static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
2786{
f25f4e44 2787 to->queue_mapping = from->queue_mapping;
f25f4e44
PWJ
2788}
2789
d5a9e24a
DM
2790static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
2791{
2792 skb->queue_mapping = rx_queue + 1;
2793}
2794
9247744e 2795static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
d5a9e24a
DM
2796{
2797 return skb->queue_mapping - 1;
2798}
2799
9247744e 2800static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
d5a9e24a 2801{
a02cec21 2802 return skb->queue_mapping != 0;
d5a9e24a
DM
2803}
2804
7965bd4d
JP
2805u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
2806 unsigned int num_tx_queues);
9247744e 2807
def8b4fa
AD
2808static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
2809{
0b3d8e08 2810#ifdef CONFIG_XFRM
def8b4fa 2811 return skb->sp;
def8b4fa 2812#else
def8b4fa 2813 return NULL;
def8b4fa 2814#endif
0b3d8e08 2815}
def8b4fa 2816
68c33163
PS
2817/* Keeps track of mac header offset relative to skb->head.
2818 * It is useful for TSO of Tunneling protocol. e.g. GRE.
2819 * For non-tunnel skb it points to skb_mac_header() and for
3347c960
ED
2820 * tunnel skb it points to outer mac header.
2821 * Keeps track of level of encapsulation of network headers.
2822 */
68c33163 2823struct skb_gso_cb {
3347c960
ED
2824 int mac_offset;
2825 int encap_level;
68c33163
PS
2826};
2827#define SKB_GSO_CB(skb) ((struct skb_gso_cb *)(skb)->cb)
2828
2829static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
2830{
2831 return (skb_mac_header(inner_skb) - inner_skb->head) -
2832 SKB_GSO_CB(inner_skb)->mac_offset;
2833}
2834
1e2bd517
PS
2835static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
2836{
2837 int new_headroom, headroom;
2838 int ret;
2839
2840 headroom = skb_headroom(skb);
2841 ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
2842 if (ret)
2843 return ret;
2844
2845 new_headroom = skb_headroom(skb);
2846 SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
2847 return 0;
2848}
2849
bdcc0924 2850static inline bool skb_is_gso(const struct sk_buff *skb)
89114afd
HX
2851{
2852 return skb_shinfo(skb)->gso_size;
2853}
2854
36a8f39e 2855/* Note: Should be called only if skb_is_gso(skb) is true */
bdcc0924 2856static inline bool skb_is_gso_v6(const struct sk_buff *skb)
eabd7e35
BG
2857{
2858 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
2859}
2860
7965bd4d 2861void __skb_warn_lro_forwarding(const struct sk_buff *skb);
4497b076
BH
2862
2863static inline bool skb_warn_if_lro(const struct sk_buff *skb)
2864{
2865 /* LRO sets gso_size but not gso_type, whereas if GSO is really
2866 * wanted then gso_type will be set. */
05bdd2f1
ED
2867 const struct skb_shared_info *shinfo = skb_shinfo(skb);
2868
b78462eb
AD
2869 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
2870 unlikely(shinfo->gso_type == 0)) {
4497b076
BH
2871 __skb_warn_lro_forwarding(skb);
2872 return true;
2873 }
2874 return false;
2875}
2876
35fc92a9
HX
2877static inline void skb_forward_csum(struct sk_buff *skb)
2878{
2879 /* Unfortunately we don't support this one. Any brave souls? */
2880 if (skb->ip_summed == CHECKSUM_COMPLETE)
2881 skb->ip_summed = CHECKSUM_NONE;
2882}
2883
bc8acf2c
ED
2884/**
2885 * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
2886 * @skb: skb to check
2887 *
2888 * fresh skbs have their ip_summed set to CHECKSUM_NONE.
2889 * Instead of forcing ip_summed to CHECKSUM_NONE, we can
2890 * use this helper, to document places where we make this assertion.
2891 */
05bdd2f1 2892static inline void skb_checksum_none_assert(const struct sk_buff *skb)
bc8acf2c
ED
2893{
2894#ifdef DEBUG
2895 BUG_ON(skb->ip_summed != CHECKSUM_NONE);
2896#endif
2897}
2898
f35d9d8a 2899bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
a6686f2f 2900
ed1f50c3
PD
2901int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
2902
f77668dc
DB
2903u32 __skb_get_poff(const struct sk_buff *skb);
2904
3a7c1ee4
AD
2905/**
2906 * skb_head_is_locked - Determine if the skb->head is locked down
2907 * @skb: skb to check
2908 *
2909 * The head on skbs build around a head frag can be removed if they are
2910 * not cloned. This function returns true if the skb head is locked down
2911 * due to either being allocated via kmalloc, or by being a clone with
2912 * multiple references to the head.
2913 */
2914static inline bool skb_head_is_locked(const struct sk_buff *skb)
2915{
2916 return !skb->head_frag || skb_cloned(skb);
2917}
fe6cc55f
FW
2918
2919/**
2920 * skb_gso_network_seglen - Return length of individual segments of a gso packet
2921 *
2922 * @skb: GSO skb
2923 *
2924 * skb_gso_network_seglen is used to determine the real size of the
2925 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
2926 *
2927 * The MAC/L2 header is not accounted for.
2928 */
2929static inline unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
2930{
2931 unsigned int hdr_len = skb_transport_header(skb) -
2932 skb_network_header(skb);
2933 return hdr_len + skb_gso_transport_seglen(skb);
2934}
1da177e4
LT
2935#endif /* __KERNEL__ */
2936#endif /* _LINUX_SKBUFF_H */
This page took 1.496626 seconds and 5 git commands to generate.