blob: bdbf7afad6b72a48f328f9eb1775979fcc4731e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020018#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/compiler.h>
20#include <linux/time.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050021#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/cache.h>
23
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/types.h>
26#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/net.h>
Thomas Graf3fc7e8a2005-06-23 21:00:17 -070028#include <linux/textsearch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/checksum.h>
Al Viroa80958f2006-12-04 20:41:19 +000030#include <linux/rcupdate.h>
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -070031#include <linux/hrtimer.h>
Ian Campbell131ea662011-08-19 06:25:00 +000032#include <linux/dma-mapping.h>
Michał Mirosławc8f44af2011-11-15 15:29:55 +000033#include <linux/netdev_features.h>
Jason Wang5203cd22013-03-26 23:11:21 +000034#include <net/flow_keys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Daniel Borkmann78ea85f2013-12-16 23:27:09 +010036/* 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
Herbert Xu60476372007-04-09 11:59:39 -0700107/* Don't change this without changing skb_csum_unnecessary! */
Daniel Borkmann78ea85f2013-12-16 23:27:09 +0100108#define CHECKSUM_NONE 0
109#define CHECKSUM_UNNECESSARY 1
110#define CHECKSUM_COMPLETE 2
111#define CHECKSUM_PARTIAL 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
114 ~(SMP_CACHE_BYTES - 1))
David S. Millerfc910a22007-03-25 20:27:59 -0700115#define SKB_WITH_OVERHEAD(X) \
Herbert Xudeea84b2007-10-21 16:27:46 -0700116 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
David S. Millerfc910a22007-03-25 20:27:59 -0700117#define SKB_MAX_ORDER(X, ORDER) \
118 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
120#define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
121
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000122/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127struct net_device;
David Howells716ea3a2007-04-02 20:19:53 -0700128struct scatterlist;
Jens Axboe9c55e012007-11-06 23:30:13 -0800129struct pipe_inode_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700131#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132struct nf_conntrack {
133 atomic_t use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#ifdef CONFIG_BRIDGE_NETFILTER
138struct nf_bridge_info {
Eric Dumazetbf1ac5c2012-04-18 23:19:25 +0000139 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)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145#endif
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147struct 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
Ian Campbell9d4dde52011-12-22 23:39:14 +0000158/* 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.
Anton Blancharda715dea2011-03-27 14:57:26 +0000164 */
Ian Campbell9d4dde52011-12-22 23:39:14 +0000165#if (65536/PAGE_SIZE + 1) < 16
David S. Millereec00952011-03-29 23:34:08 -0700166#define MAX_SKB_FRAGS 16UL
Anton Blancharda715dea2011-03-27 14:57:26 +0000167#else
Ian Campbell9d4dde52011-12-22 23:39:14 +0000168#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
Anton Blancharda715dea2011-03-27 14:57:26 +0000169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171typedef struct skb_frag_struct skb_frag_t;
172
173struct skb_frag_struct {
Ian Campbella8605c62011-10-19 23:01:49 +0000174 struct {
175 struct page *p;
176 } page;
Eric Dumazetcb4dfe52010-09-23 05:06:54 +0000177#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
David S. Millera309bb02007-07-30 18:47:03 -0700178 __u32 page_offset;
179 __u32 size;
Eric Dumazetcb4dfe52010-09-23 05:06:54 +0000180#else
181 __u16 page_offset;
182 __u16 size;
183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Eric Dumazet9e903e02011-10-18 21:00:24 +0000186static 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
Patrick Ohlyac45f602009-02-12 05:03:37 +0000206#define HAVE_HW_TIME_STAMP
207
208/**
Randy Dunlapd3a21be2009-03-02 03:15:58 -0800209 * struct skb_shared_hwtstamps - hardware time stamps
Patrick Ohlyac45f602009-02-12 05:03:37 +0000210 * @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
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000236/* 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
Shirley Maa6686f22011-07-06 12:22:12 +0000247 /* device driver supports TX zero-copy buffers */
Eric Dumazet62b1a8a2012-06-14 06:42:44 +0000248 SKBTX_DEV_ZEROCOPY = 1 << 3,
Johannes Berg6e3e9392011-11-09 10:15:42 +0100249
250 /* generate wifi status information (where possible) */
Eric Dumazet62b1a8a2012-06-14 06:42:44 +0000251 SKBTX_WIFI_STATUS = 1 << 4,
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000252
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,
Shirley Maa6686f22011-07-06 12:22:12 +0000259};
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.
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000264 * 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.
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +0000266 * The ctx field is used to track device context.
267 * The desc field is used to track userspace buffer index.
Shirley Maa6686f22011-07-06 12:22:12 +0000268 */
269struct ubuf_info {
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000270 void (*callback)(struct ubuf_info *, bool zerocopy_success);
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +0000271 void *ctx;
Shirley Maa6686f22011-07-06 12:22:12 +0000272 unsigned long desc;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/* 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 {
Ian Campbell9f42f122012-01-05 07:13:39 +0000279 unsigned char nr_frags;
280 __u8 tx_flags;
Herbert Xu79671682006-06-22 02:40:14 -0700281 unsigned short gso_size;
282 /* Warning: this field is not always filled in (UFO)! */
283 unsigned short gso_segs;
284 unsigned short gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct sk_buff *frag_list;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000286 struct skb_shared_hwtstamps hwtstamps;
Ian Campbell9f42f122012-01-05 07:13:39 +0000287 __be32 ip6_frag_id;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700288
289 /*
290 * Warning : all fields before dataref are cleared in __alloc_skb()
291 */
292 atomic_t dataref;
293
Johann Baudy69e3c752009-05-18 22:11:22 -0700294 /* Intermediate layers must ensure that destructor_arg
295 * remains valid until skb destructor */
296 void * destructor_arg;
Shirley Maa6686f22011-07-06 12:22:12 +0000297
Eric Dumazetfed66382010-07-22 19:09:08 +0000298 /* must be last field, see pskb_expand_head() */
299 skb_frag_t frags[MAX_SKB_FRAGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
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
Patrick McHardy334a8132007-06-25 04:35:20 -0700304 * the entire skb->data. A clone of a headerless skb holds the length of
305 * the header in skb->hdr_len.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
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
David S. Millerd179cd12005-08-17 14:57:30 -0700316
317enum {
318 SKB_FCLONE_UNAVAILABLE,
319 SKB_FCLONE_ORIG,
320 SKB_FCLONE_CLONE,
321};
322
Herbert Xu79671682006-06-22 02:40:14 -0700323enum {
324 SKB_GSO_TCPV4 = 1 << 0,
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700325 SKB_GSO_UDP = 1 << 1,
Herbert Xu576a30e2006-06-27 13:22:38 -0700326
327 /* This indicates the skb is from an untrusted source. */
328 SKB_GSO_DODGY = 1 << 2,
Michael Chanb0da85372006-06-29 12:30:00 -0700329
330 /* This indicates the tcp segment has CWR set. */
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700331 SKB_GSO_TCP_ECN = 1 << 3,
332
333 SKB_GSO_TCPV6 = 1 << 4,
Chris Leech01d5b2f2009-02-27 14:06:49 -0800334
335 SKB_GSO_FCOE = 1 << 5,
Pravin B Shelar68c33162013-02-14 14:02:41 +0000336
337 SKB_GSO_GRE = 1 << 6,
Pravin B Shelar73136262013-03-07 13:21:51 +0000338
Eric Dumazetcb32f512013-10-19 11:42:57 -0700339 SKB_GSO_IPIP = 1 << 7,
Simon Horman0d89d202013-05-23 21:02:52 +0000340
Eric Dumazet61c1db72013-10-20 20:47:30 -0700341 SKB_GSO_SIT = 1 << 8,
Eric Dumazetcb32f512013-10-19 11:42:57 -0700342
Eric Dumazet61c1db72013-10-20 20:47:30 -0700343 SKB_GSO_UDP_TUNNEL = 1 << 9,
344
345 SKB_GSO_MPLS = 1 << 10,
Herbert Xu79671682006-06-22 02:40:14 -0700346};
347
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700348#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
359 * struct sk_buff - socket buffer
360 * @next: Next buffer in list
361 * @prev: Previous buffer in list
Herbert Xu325ed822005-10-03 13:57:23 -0700362 * @tstamp: Time we arrived
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700363 * @sk: Socket we are owned by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * @dev: Device we arrived on/are leaving by
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700365 * @cb: Control buffer. Free for use by every layer. Put private vars here
Eric Dumazet7fee2262010-05-11 23:19:48 +0000366 * @_skb_refdst: destination entry (with norefcount bit)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700367 * @sp: the security path, used for xfrm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @len: Length of actual data
369 * @data_len: Data length
370 * @mac_len: Length of link layer header
Patrick McHardy334a8132007-06-25 04:35:20 -0700371 * @hdr_len: writable header length of cloned skb
Herbert Xu663ead32007-04-09 11:59:07 -0700372 * @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
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700375 * @priority: Packet queueing priority
Martin Waitz67be2dd2005-05-01 08:59:26 -0700376 * @local_df: allow local fragmentation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * @cloned: Head may be cloned (check refcnt to be sure)
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700378 * @ip_summed: Driver fed us an IP checksum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * @nohdr: Payload reference only, must not modify header
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700380 * @nfctinfo: Relationship of this skb to the connection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * @pkt_type: Packet class
Randy Dunlapc83c2482005-10-18 22:07:41 -0700382 * @fclone: skbuff clone status
Randy Dunlapc83c2482005-10-18 22:07:41 -0700383 * @ipvs_property: skbuff is owned by ipvs
Randy Dunlap31729362008-02-18 20:52:13 -0800384 * @peeked: this packet has been seen already, so stats have been
385 * done for it, don't do them again
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700386 * @nf_trace: netfilter packet trace flag
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700387 * @protocol: Packet protocol from driver
388 * @destructor: Destruct function
389 * @nfct: Associated connection, if any
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
Eric Dumazet8964be42009-11-20 15:35:04 -0800391 * @skb_iif: ifindex of device we arrived on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * @tc_index: Traffic control index
393 * @tc_verd: traffic control verdict
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700394 * @rxhash: the packet hash computed on receive
395 * @queue_mapping: Queue mapping for multiqueue devices
Randy Dunlap553a5672008-04-20 10:51:01 -0700396 * @ndisc_nodetype: router type (from link layer)
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700397 * @ooo_okay: allow the mapping of a socket to a queue to be changed
Changli Gao4ca24622011-08-19 07:26:44 -0700398 * @l4_rxhash: indicate rxhash is a canonical 4-tuple hash over transport
399 * ports.
Johannes Berg6e3e9392011-11-09 10:15:42 +0100400 * @wifi_acked_valid: wifi_acked was set
401 * @wifi_acked: whether frame was acked on wifi or not
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000402 * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS
Randy Dunlapf4b8ea72006-06-22 16:00:11 -0700403 * @dma_cookie: a cookie to one of several possible DMA operations
404 * done by skb DMA functions
Eliezer Tamir06021292013-06-10 11:39:50 +0300405 * @napi_id: id of the NAPI struct this skb came from
James Morris984bc162006-06-09 00:29:17 -0700406 * @secmark: security marking
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700407 * @mark: Generic packet mark
408 * @dropcount: total number of sk_receive_queue overflows
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000409 * @vlan_proto: vlan encapsulation protocol
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700410 * @vlan_tci: vlan tag control information
Simon Horman0d89d202013-05-23 21:02:52 +0000411 * @inner_protocol: Protocol (encapsulation)
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000412 * @inner_transport_header: Inner transport layer header (encapsulation)
413 * @inner_network_header: Network layer header (encapsulation)
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000414 * @inner_mac_header: Link layer header (encapsulation)
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700415 * @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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
425
426struct sk_buff {
427 /* These two members must be first. */
428 struct sk_buff *next;
429 struct sk_buff *prev;
430
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700431 ktime_t tstamp;
Felix Fietkauda3f5cf2010-02-23 11:45:51 +0000432
433 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
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 */
Felix Fietkauda3f5cf2010-02-23 11:45:51 +0000442 char cb[48] __aligned(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Eric Dumazet7fee2262010-05-11 23:19:48 +0000444 unsigned long _skb_refdst;
Felix Fietkauda3f5cf2010-02-23 11:45:51 +0000445#ifdef CONFIG_XFRM
446 struct sec_path *sp;
447#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned int len,
Patrick McHardy334a8132007-06-25 04:35:20 -0700449 data_len;
450 __u16 mac_len,
451 hdr_len;
Al Viroff1dcad2006-11-20 18:07:29 -0800452 union {
453 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -0700454 struct {
455 __u16 csum_start;
456 __u16 csum_offset;
457 };
Al Viroff1dcad2006-11-20 18:07:29 -0800458 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 __u32 priority;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200460 kmemcheck_bitfield_begin(flags1);
Thomas Graf1cbb3382005-07-05 14:13:41 -0700461 __u8 local_df:1,
462 cloned:1,
463 ip_summed:2,
Harald Welte6869c4d2005-08-09 19:24:19 -0700464 nohdr:1,
465 nfctinfo:3;
David S. Millerd179cd12005-08-17 14:57:30 -0700466 __u8 pkt_type:3,
Patrick McHardyb84f4cc2005-11-20 21:19:21 -0800467 fclone:2,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700468 ipvs_property:1,
Herbert Xua59322b2007-12-05 01:53:40 -0800469 peeked:1,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700470 nf_trace:1;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200471 kmemcheck_bitfield_end(flags1);
Eric Dumazet4ab408d2010-03-01 03:09:26 +0000472 __be16 protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 void (*destructor)(struct sk_buff *skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800475#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700476 struct nf_conntrack *nfct;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#ifdef CONFIG_BRIDGE_NETFILTER
479 struct nf_bridge_info *nf_bridge;
480#endif
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -0700481
Eric Dumazet8964be42009-11-20 15:35:04 -0800482 int skb_iif;
Alexander Duyck4031ae62012-01-27 06:22:53 +0000483
484 __u32 rxhash;
485
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000486 __be16 vlan_proto;
Alexander Duyck4031ae62012-01-27 06:22:53 +0000487 __u16 vlan_tci;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#ifdef CONFIG_NET_SCHED
Patrick McHardyb6b99eb2005-08-09 19:33:51 -0700490 __u16 tc_index; /* traffic control index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#ifdef CONFIG_NET_CLS_ACT
Patrick McHardyb6b99eb2005-08-09 19:33:51 -0700492 __u16 tc_verd; /* traffic control verdict */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#endif
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200495
Eric Dumazet0a148422011-04-20 09:27:32 +0000496 __u16 queue_mapping;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200497 kmemcheck_bitfield_begin(flags2);
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -0400498#ifdef CONFIG_IPV6_NDISC_NODETYPE
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000499 __u8 ndisc_nodetype:2;
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -0400500#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700501 __u8 pfmemalloc:1;
Tom Herbert3853b582010-11-21 13:17:29 +0000502 __u8 ooo_okay:1;
Tom Herbertbdeab992011-08-14 19:45:55 +0000503 __u8 l4_rxhash:1;
Johannes Berg6e3e9392011-11-09 10:15:42 +0100504 __u8 wifi_acked_valid:1;
505 __u8 wifi_acked:1;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000506 __u8 no_fcs:1;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000507 __u8 head_frag:1;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000508 /* 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;
Nicolas Dichtel45906722013-09-30 14:16:41 +0200514 /* 6/8 bit hole (depending on ndisc_nodetype presence) */
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200515 kmemcheck_bitfield_end(flags2);
516
Dan Williams7bced392013-12-30 12:37:29 -0800517#ifdef CONFIG_NET_RX_BUSY_POLL
518 unsigned int napi_id;
Chris Leech97fc2f02006-05-23 17:55:33 -0700519#endif
James Morris984bc162006-06-09 00:29:17 -0700520#ifdef CONFIG_NETWORK_SECMARK
521 __u32 secmark;
522#endif
Neil Horman3b885782009-10-12 13:26:31 -0700523 union {
524 __u32 mark;
525 __u32 dropcount;
Eric Dumazet16fad692013-03-14 05:40:32 +0000526 __u32 reserved_tailroom;
Neil Horman3b885782009-10-12 13:26:31 -0700527 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Simon Horman0d89d202013-05-23 21:02:52 +0000529 __be16 inner_protocol;
Simon Horman1a37e412013-05-23 21:02:51 +0000530 __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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* These elements must be at the end, see alloc_skb() for details. */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700537 sk_buff_data_t tail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700538 sk_buff_data_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 unsigned char *head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700540 *data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700541 unsigned int truesize;
542 atomic_t users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543};
544
545#ifdef __KERNEL__
546/*
547 * Handling routines are only of interest to the kernel
548 */
549#include <linux/slab.h>
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Mel Gormanc93bdd02012-07-31 16:44:19 -0700552#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
Eric Dumazet7fee2262010-05-11 23:19:48 +0000561/*
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 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000574static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
575{
Eric Dumazet7fee2262010-05-11 23:19:48 +0000576 /* 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);
Eric Dumazetadf30902009-06-02 05:19:30 +0000583}
584
Eric Dumazet7fee2262010-05-11 23:19:48 +0000585/**
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 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000593static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
594{
Eric Dumazet7fee2262010-05-11 23:19:48 +0000595 skb->_skb_refdst = (unsigned long)dst;
596}
597
Joe Perches7965bd42013-09-26 14:48:15 -0700598void __skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst,
599 bool force);
Julian Anastasov932bc4d2013-03-21 11:57:58 +0200600
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}
Eric Dumazet7fee2262010-05-11 23:19:48 +0000632
633/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300634 * skb_dst_is_noref - Test if skb dst isn't refcounted
Eric Dumazet7fee2262010-05-11 23:19:48 +0000635 * @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);
Eric Dumazetadf30902009-06-02 05:19:30 +0000640}
641
Eric Dumazet511c3f92009-06-02 05:14:27 +0000642static inline struct rtable *skb_rtable(const struct sk_buff *skb)
643{
Eric Dumazetadf30902009-06-02 05:19:30 +0000644 return (struct rtable *)skb_dst(skb);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000645}
646
Joe Perches7965bd42013-09-26 14:48:15 -0700647void 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);
Eric Dumazetd7e88832012-04-30 08:10:34 +0000652extern struct kmem_cache *skbuff_head_cache;
Eric Dumazetbad43ca2012-05-19 03:02:02 +0000653
Joe Perches7965bd42013-09-26 14:48:15 -0700654void 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);
Eric Dumazetbad43ca2012-05-19 03:02:02 +0000657
Joe Perches7965bd42013-09-26 14:48:15 -0700658struct 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);
David S. Millerd179cd12005-08-17 14:57:30 -0700661static inline struct sk_buff *alloc_skb(unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100662 gfp_t priority)
David S. Millerd179cd12005-08-17 14:57:30 -0700663{
Eric Dumazet564824b2010-10-11 19:05:25 +0000664 return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
David S. Millerd179cd12005-08-17 14:57:30 -0700665}
666
667static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100668 gfp_t priority)
David S. Millerd179cd12005-08-17 14:57:30 -0700669{
Mel Gormanc93bdd02012-07-31 16:44:19 -0700670 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
David S. Millerd179cd12005-08-17 14:57:30 -0700671}
672
Joe Perches7965bd42013-09-26 14:48:15 -0700673struct sk_buff *__alloc_skb_head(gfp_t priority, int node);
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000674static inline struct sk_buff *alloc_skb_head(gfp_t priority)
675{
676 return __alloc_skb_head(priority, -1);
677}
678
Joe Perches7965bd42013-09-26 14:48:15 -0700679struct 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);
Eric Dumazet117632e2011-12-03 21:39:53 +0000684
Joe Perches7965bd42013-09-26 14:48:15 -0700685int 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);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000694#define dev_kfree_skb(a) consume_skb(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Joe Perches7965bd42013-09-26 14:48:15 -0700696int 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);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700700
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800701struct skb_seq_state {
Thomas Graf677e90e2005-06-23 20:59:51 -0700702 __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
Joe Perches7965bd42013-09-26 14:48:15 -0700711void 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);
Thomas Graf677e90e2005-06-23 20:59:51 -0700716
Joe Perches7965bd42013-09-26 14:48:15 -0700717unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
718 unsigned int to, struct ts_config *config,
719 struct ts_state *state);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -0700720
Tom Herbert09323cc2013-12-15 22:16:19 -0800721/*
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
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800761void __skb_get_hash(struct sk_buff *skb);
762static inline __u32 skb_get_hash(struct sk_buff *skb)
Krishna Kumarbfb564e2010-08-04 06:15:52 +0000763{
Willem de Bruijnecd5cf52012-10-26 11:52:08 +0000764 if (!skb->l4_rxhash)
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800765 __skb_get_hash(skb);
Krishna Kumarbfb564e2010-08-04 06:15:52 +0000766
767 return skb->rxhash;
768}
769
Tom Herbert57bdf7f42014-01-15 08:57:54 -0800770static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
771{
772 return skb->rxhash;
773}
774
Tom Herbert7539fad2013-12-15 22:12:18 -0800775static 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
Tom Herbert3df7a742013-12-15 22:16:29 -0800787static 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
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700793#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}
Alexander Duyckec47ea82012-05-04 14:26:56 +0000798
799static inline unsigned int skb_end_offset(const struct sk_buff *skb)
800{
801 return skb->end;
802}
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700803#else
804static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
805{
806 return skb->end;
807}
Alexander Duyckec47ea82012-05-04 14:26:56 +0000808
809static inline unsigned int skb_end_offset(const struct sk_buff *skb)
810{
811 return skb->end - skb->head;
812}
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700813#endif
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815/* Internal */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700816#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Patrick Ohlyac45f602009-02-12 05:03:37 +0000818static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
819{
820 return &skb_shinfo(skb)->hwtstamps;
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/**
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{
Daniel Borkmannfd44b932014-01-07 23:23:44 +0100831 return list->next == (const struct sk_buff *) list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/**
David S. Millerfc7ebb22008-09-23 00:34:07 -0700835 * 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{
Daniel Borkmannfd44b932014-01-07 23:23:44 +0100844 return skb->next == (const struct sk_buff *) list;
David S. Millerfc7ebb22008-09-23 00:34:07 -0700845}
846
847/**
Ilpo Järvinen832d11c2008-11-24 21:20:15 -0800848 * 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{
Daniel Borkmannfd44b932014-01-07 23:23:44 +0100857 return skb->prev == (const struct sk_buff *) list;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -0800858}
859
860/**
David S. Miller249c8b42008-09-23 00:44:42 -0700861 * 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
878/**
Ilpo Järvinen832d11c2008-11-24 21:20:15 -0800879 * 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
896/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * 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
914/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * 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
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000928static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/**
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 */
Eric Dumazet47061bc2012-08-03 20:54:15 +0000997static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 might_sleep_if(pri & __GFP_WAIT);
1000 if (skb_shared(skb)) {
1001 struct sk_buff *nskb = skb_clone(skb, pri);
Eric Dumazet47061bc2012-08-03 20:54:15 +00001002
1003 if (likely(nskb))
1004 consume_skb(skb);
1005 else
1006 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 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 */
Victor Fuscoe2bf5212005-07-18 13:36:38 -07001032static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +01001033 gfp_t pri)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
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/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -08001045 * skb_peek - peek at the head of an &sk_buff_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 * @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 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04001057static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Eric Dumazet18d07002012-04-30 16:31:46 +00001059 struct sk_buff *skb = list_->next;
1060
1061 if (skb == (struct sk_buff *)list_)
1062 skb = NULL;
1063 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/**
Pavel Emelyanovda5ef6e2012-02-21 07:31:18 +00001067 * 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;
Eric Dumazet18d07002012-04-30 16:31:46 +00001079
Pavel Emelyanovda5ef6e2012-02-21 07:31:18 +00001080 if (next == (struct sk_buff *)list_)
1081 next = NULL;
1082 return next;
1083}
1084
1085/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -08001086 * skb_peek_tail - peek at the tail of an &sk_buff_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * @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 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04001098static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Eric Dumazet18d07002012-04-30 16:31:46 +00001100 struct sk_buff *skb = list_->prev;
1101
1102 if (skb == (struct sk_buff *)list_)
1103 skb = NULL;
1104 return skb;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
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
David S. Miller67fed452008-09-21 22:36:24 -07001119/**
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
Arjan van de Ven76f10ad2006-08-02 14:06:55 -07001135/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static inline void skb_queue_head_init(struct sk_buff_head *list)
1144{
1145 spin_lock_init(&list->lock);
David S. Miller67fed452008-09-21 22:36:24 -07001146 __skb_queue_head_init(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Pavel Emelianovc2ecba72007-04-17 12:45:31 -07001149static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
Gerrit Renkerbf299272008-04-14 00:04:51 -07001157 * Insert an sk_buff on a list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 *
1159 * The "__skb_xxxx()" functions are the non-atomic ones that
1160 * can only be called with interrupts disabled.
1161 */
Joe Perches7965bd42013-09-26 14:48:15 -07001162void skb_insert(struct sk_buff *old, struct sk_buff *newsk,
1163 struct sk_buff_head *list);
Gerrit Renkerbf299272008-04-14 00:04:51 -07001164static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
David S. Miller67fed452008-09-21 22:36:24 -07001174static 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);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001198 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001199 }
1200}
1201
1202/**
Eric Dumazetd961949662012-04-30 21:29:16 +00001203 * skb_queue_splice_init - join two skb lists and reinitialise the emptied list
David S. Miller67fed452008-09-21 22:36:24 -07001204 * @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);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001214 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001215 __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);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001229 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001230 }
1231}
1232
1233/**
Eric Dumazetd961949662012-04-30 21:29:16 +00001234 * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
David S. Miller67fed452008-09-21 22:36:24 -07001235 * @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);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001246 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001247 __skb_queue_head_init(list);
1248 }
1249}
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/**
Stephen Hemminger300ce172005-10-30 13:47:34 -08001252 * __skb_queue_after - queue a buffer at the list head
1253 * @list: list to use
1254 * @prev: place after this buffer
1255 * @newsk: buffer to queue
1256 *
1257 * Queue a buffer int the middle of a list. This function takes no locks
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 */
1262static inline void __skb_queue_after(struct sk_buff_head *list,
1263 struct sk_buff *prev,
1264 struct sk_buff *newsk)
1265{
Gerrit Renkerbf299272008-04-14 00:04:51 -07001266 __skb_insert(newsk, prev, prev->next, list);
Stephen Hemminger300ce172005-10-30 13:47:34 -08001267}
1268
Joe Perches7965bd42013-09-26 14:48:15 -07001269void skb_append(struct sk_buff *old, struct sk_buff *newsk,
1270 struct sk_buff_head *list);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001271
Gerrit Renkerf5572852008-04-14 00:05:28 -07001272static 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
Stephen Hemminger300ce172005-10-30 13:47:34 -08001279/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * __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 */
Joe Perches7965bd42013-09-26 14:48:15 -07001289void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static inline void __skb_queue_head(struct sk_buff_head *list,
1291 struct sk_buff *newsk)
1292{
Stephen Hemminger300ce172005-10-30 13:47:34 -08001293 __skb_queue_after(list, (struct sk_buff *)list, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
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 */
Joe Perches7965bd42013-09-26 14:48:15 -07001306void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307static inline void __skb_queue_tail(struct sk_buff_head *list,
1308 struct sk_buff *newsk)
1309{
Gerrit Renkerf5572852008-04-14 00:05:28 -07001310 __skb_queue_before(list, (struct sk_buff *)list, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * remove sk_buff from list. _Must_ be called atomically, and with
1315 * the list known..
1316 */
Joe Perches7965bd42013-09-26 14:48:15 -07001317void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 next->prev = prev;
1327 prev->next = next;
1328}
1329
Gerrit Renkerf525c062008-04-14 00:04:12 -07001330/**
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 */
Joe Perches7965bd42013-09-26 14:48:15 -07001338struct sk_buff *skb_dequeue(struct sk_buff_head *list);
Gerrit Renkerf525c062008-04-14 00:04:12 -07001339static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
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 */
Joe Perches7965bd42013-09-26 14:48:15 -07001355struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static 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
David S. Millerbdcc0922012-03-07 20:53:36 -05001365static inline bool skb_is_nonlinear(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
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--)
Eric Dumazet9e903e02011-10-18 21:00:24 +00001380 len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return len + skb_headlen(skb);
1382}
1383
Ian Campbell131ea662011-08-19 06:25:00 +00001384/**
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1401
Mel Gormanc48a11c2012-07-31 16:44:23 -07001402 /*
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 */
Ian Campbella8605c62011-10-19 23:01:49 +00001411 frag->page.p = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 frag->page_offset = off;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001413 skb_frag_size_set(frag, size);
Pavel Emelyanovcca7af32013-03-14 03:29:40 +00001414
1415 page = compound_head(page);
1416 if (page->pfmemalloc && !page->mapping)
1417 skb->pfmemalloc = true;
Ian Campbell131ea662011-08-19 06:25:00 +00001418}
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
Mathias Krausebc323832013-11-07 14:18:26 +01001429 * @skb to point to @size bytes at offset @off within @page. In
Ian Campbell131ea662011-08-19 06:25:00 +00001430 * 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 skb_shinfo(skb)->nr_frags = i + 1;
1439}
1440
Joe Perches7965bd42013-09-26 14:48:15 -07001441void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
1442 int size, unsigned int truesize);
Peter Zijlstra654bed12008-10-07 14:22:33 -07001443
Jason Wangf8e617e2013-11-01 14:07:47 +08001444void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
1445 unsigned int truesize);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447#define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
David S. Miller21dc3302010-08-23 00:13:46 -07001448#define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1450
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001451#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}
Simon Horman7cc46192013-05-28 20:34:29 +00001467
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001468#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}
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001483
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001484#endif /* NET_SKBUFF_DATA_USES_OFFSET */
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/*
1487 * Add data to an sk_buff
1488 */
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001489unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len);
Joe Perches7965bd42013-09-26 14:48:15 -07001490unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
1492{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001493 unsigned char *tmp = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 SKB_LINEAR_ASSERT(skb);
1495 skb->tail += len;
1496 skb->len += len;
1497 return tmp;
1498}
1499
Joe Perches7965bd42013-09-26 14:48:15 -07001500unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static 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
Joe Perches7965bd42013-09-26 14:48:15 -07001508unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509static 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
David S. Miller47d29642010-05-02 02:21:44 -07001516static 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
Joe Perches7965bd42013-09-26 14:48:15 -07001521unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
1524{
1525 if (len > skb_headlen(skb) &&
Gerrit Renker987c4022008-08-11 18:17:17 -07001526 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 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;
Gerrit Renker987c4022008-08-11 18:17:17 -07001543 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
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 */
Chuck Leverc2636b42007-10-23 21:07:32 -07001552static inline unsigned int skb_headroom(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
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{
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001565 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568/**
Eric Dumazeta21d4572012-04-10 20:30:48 +00001569 * 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{
Eric Dumazet16fad692013-03-14 05:40:32 +00001577 if (skb_is_nonlinear(skb))
1578 return 0;
1579
1580 return skb->end - skb->tail - skb->reserved_tailroom;
Eric Dumazeta21d4572012-04-10 20:30:48 +00001581}
1582
1583/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * 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 */
David S. Miller8243126c2006-01-17 02:54:21 -08001591static inline void skb_reserve(struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 skb->data += len;
1594 skb->tail += len;
1595}
1596
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001597static inline void skb_reset_inner_headers(struct sk_buff *skb)
1598{
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001599 skb->inner_mac_header = skb->mac_header;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001600 skb->inner_network_header = skb->network_header;
1601 skb->inner_transport_header = skb->transport_header;
1602}
1603
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00001604static inline void skb_reset_mac_len(struct sk_buff *skb)
1605{
1606 skb->mac_len = skb->network_header - skb->mac_header;
1607}
1608
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001609static 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
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001644static 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}
Eric Dumazetfda55ec2013-01-07 09:28:21 +00001660static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
1661{
Cong Wang35d04612013-05-29 15:16:05 +08001662 return skb->transport_header != (typeof(skb->transport_header))~0U;
Eric Dumazetfda55ec2013-01-07 09:28:21 +00001663}
1664
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001665static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1666{
1667 return skb->head + skb->transport_header;
1668}
1669
1670static inline void skb_reset_transport_header(struct sk_buff *skb)
1671{
1672 skb->transport_header = skb->data - skb->head;
1673}
1674
1675static inline void skb_set_transport_header(struct sk_buff *skb,
1676 const int offset)
1677{
1678 skb_reset_transport_header(skb);
1679 skb->transport_header += offset;
1680}
1681
1682static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1683{
1684 return skb->head + skb->network_header;
1685}
1686
1687static inline void skb_reset_network_header(struct sk_buff *skb)
1688{
1689 skb->network_header = skb->data - skb->head;
1690}
1691
1692static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1693{
1694 skb_reset_network_header(skb);
1695 skb->network_header += offset;
1696}
1697
1698static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1699{
1700 return skb->head + skb->mac_header;
1701}
1702
1703static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1704{
Cong Wang35d04612013-05-29 15:16:05 +08001705 return skb->mac_header != (typeof(skb->mac_header))~0U;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001706}
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
Timo Teräs0e3da5b2013-12-16 11:02:09 +02001719static inline void skb_pop_mac_header(struct sk_buff *skb)
1720{
1721 skb->mac_header = skb->network_header;
1722}
1723
Ying Xuefbbdb8f2013-03-27 16:46:06 +00001724static 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
Eric Dumazet03606892012-02-23 10:55:02 +00001737static 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
Michał Mirosław04fb4512010-12-14 15:24:08 +00001747static inline int skb_checksum_start_offset(const struct sk_buff *skb)
1748{
1749 return skb->csum_start - skb_headroom(skb);
1750}
1751
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001752static 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
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001762static 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
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001767static inline int skb_network_offset(const struct sk_buff *skb)
1768{
1769 return skb_network_header(skb) - skb->data;
1770}
Arnaldo Carvalho de Melo48d49d0c2007-03-10 12:30:58 -03001771
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001772static inline int skb_inner_network_offset(const struct sk_buff *skb)
1773{
1774 return skb_inner_network_header(skb) - skb->data;
1775}
1776
Changli Gaof9599ce2010-08-04 04:43:44 +00001777static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
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 *
Tobias Klauser8660c122009-07-13 22:48:16 +00001793 * skb_reserve(skb, NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 *
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.
Tobias Klauser8660c122009-07-13 22:48:16 +00001798 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * 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
Anton Blanchard025be812006-03-31 02:27:06 -08001806/*
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
David S. Millerd6301d32009-02-08 19:24:13 -08001810 * 32 bytes or less we avoid the reallocation.
Anton Blanchard025be812006-03-31 02:27:06 -08001811 *
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 *
David S. Millerd6301d32009-02-08 19:24:13 -08001818 * Various parts of the networking layer expect at least 32 bytes of
Anton Blanchard025be812006-03-31 02:27:06 -08001819 * headroom, you should not reduce this.
Eric Dumazet5933dd22010-06-15 18:16:43 -07001820 *
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 :
Eric Dumazet18e8c132010-05-06 21:58:51 -07001824 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
Anton Blanchard025be812006-03-31 02:27:06 -08001825 */
1826#ifndef NET_SKB_PAD
Eric Dumazet5933dd22010-06-15 18:16:43 -07001827#define NET_SKB_PAD max(32, L1_CACHE_BYTES)
Anton Blanchard025be812006-03-31 02:27:06 -08001828#endif
1829
Joe Perches7965bd42013-09-26 14:48:15 -07001830int ___pskb_trim(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
1833{
Emmanuel Grumbachc4264f22011-05-21 19:46:09 +00001834 if (unlikely(skb_is_nonlinear(skb))) {
Herbert Xu3cc0e872006-06-09 16:13:38 -07001835 WARN_ON(1);
1836 return;
1837 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001838 skb->len = len;
1839 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Joe Perches7965bd42013-09-26 14:48:15 -07001842void skb_trim(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
1845{
Herbert Xu3cc0e872006-06-09 16:13:38 -07001846 if (skb->data_len)
1847 return ___pskb_trim(skb, len);
1848 __skb_trim(skb, len);
1849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
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
1857/**
Herbert Xue9fa4f72006-08-13 20:12:58 -07001858 * 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
1872/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * 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{
Eric Dumazetc34a7612013-07-30 16:11:15 -07001882 if (skb->destructor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 skb->destructor(skb);
Eric Dumazetc34a7612013-07-30 16:11:15 -07001884 skb->destructor = NULL;
1885 skb->sk = NULL;
Eric Dumazet376c7312013-08-01 11:43:08 -07001886 } else {
1887 BUG_ON(skb->sk);
Eric Dumazetc34a7612013-07-30 16:11:15 -07001888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
1891/**
Michael S. Tsirkina353e0c2012-07-20 09:23:07 +00001892 * 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
1907/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 * __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 */
Joe Perches7965bd42013-09-26 14:48:15 -07001915void skb_queue_purge(struct sk_buff_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static 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
Alexander Duycke5e67302013-02-08 10:17:15 +00001923#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
Joe Perches7965bd42013-09-26 14:48:15 -07001927void *netdev_alloc_frag(unsigned int fragsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Joe Perches7965bd42013-09-26 14:48:15 -07001929struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
1930 gfp_t gfp_mask);
Christoph Hellwig8af27452006-07-31 22:35:23 -07001931
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,
Eric Dumazet6f532612012-05-18 05:12:12 +00001946 unsigned int length)
Christoph Hellwig8af27452006-07-31 22:35:23 -07001947{
1948 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
1949}
1950
Eric Dumazet6f532612012-05-18 05:12:12 +00001951/* 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
Eric Dumazet4915a0d2011-07-11 20:08:34 -07001965static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
1966 unsigned int length, gfp_t gfp)
Eric Dumazet61321bb2009-10-07 17:11:23 +00001967{
Eric Dumazet4915a0d2011-07-11 20:08:34 -07001968 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
Eric Dumazet61321bb2009-10-07 17:11:23 +00001969
1970 if (NET_IP_ALIGN && skb)
1971 skb_reserve(skb, NET_IP_ALIGN);
1972 return skb;
1973}
1974
Eric Dumazet4915a0d2011-07-11 20:08:34 -07001975static 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
Florian Fainellibc6fc9f2013-08-30 15:36:14 +01001981/**
1982 * __skb_alloc_pages - allocate pages for ps-rx on a skb and preserve pfmemalloc data
Mel Gorman06140022012-07-31 16:44:24 -07001983 * @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
Eric Dumazet564824b2010-10-11 19:05:25 +00002036/**
Ian Campbell131ea662011-08-19 06:25:00 +00002037 * 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{
Ian Campbella8605c62011-10-19 23:01:49 +00002044 return frag->page.p;
Ian Campbell131ea662011-08-19 06:25:00 +00002045}
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{
Ian Campbella8605c62011-10-19 23:01:49 +00002130 frag->page.p = page;
Ian Campbell131ea662011-08-19 06:25:00 +00002131}
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
Eric Dumazet400dfd32013-10-17 16:27:07 -07002147bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
2148
Ian Campbell131ea662011-08-19 06:25:00 +00002149/**
2150 * skb_frag_dma_map - maps a paged fragment via the DMA API
Marcos Paulo de Souzaf83347d2011-10-31 15:11:45 +00002151 * @dev: the device to map the fragment to
Ian Campbell131ea662011-08-19 06:25:00 +00002152 * @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
Marcos Paulo de Souzaf83347d2011-10-31 15:11:45 +00002156 * @dir: the direction of the mapping (%PCI_DMA_*)
Ian Campbell131ea662011-08-19 06:25:00 +00002157 *
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
Eric Dumazet117632e2011-12-03 21:39:53 +00002169static 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
Ian Campbell131ea662011-08-19 06:25:00 +00002175/**
Patrick McHardy334a8132007-06-25 04:35:20 -07002176 * 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 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04002183static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
Patrick McHardy334a8132007-06-25 04:35:20 -07002184{
2185 return !skb_header_cloned(skb) &&
2186 skb_headroom(skb) + len <= skb->hdr_len;
2187}
2188
Herbert Xud9cc2042007-09-16 16:21:16 -07002189static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
2190 int cloned)
2191{
2192 int delta = 0;
2193
Herbert Xud9cc2042007-09-16 16:21:16 -07002194 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
Patrick McHardy334a8132007-06-25 04:35:20 -07002203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 * 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{
Herbert Xud9cc2042007-09-16 16:21:16 -07002217 return __skb_cow(skb, headroom, skb_cloned(skb));
2218}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Herbert Xud9cc2042007-09-16 16:21:16 -07002220/**
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
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
Herbert Xu5b057c62006-06-23 02:06:41 -07002242 * is untouched. Otherwise it is extended. Returns zero on
2243 * success. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
2245
Herbert Xu5b057c62006-06-23 02:06:41 -07002246static inline int skb_padto(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 unsigned int size = skb->len;
2249 if (likely(size >= len))
Herbert Xu5b057c62006-06-23 02:06:41 -07002250 return 0;
Gerrit Renker987c4022008-08-11 18:17:17 -07002251 return skb_pad(skb, len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
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;
Al Viro50842052006-11-14 21:36:34 -08002261 __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 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
Eric Dumazet38ba0a62012-04-23 17:48:27 +00002274static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
2275 const struct page *page, int off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 if (i) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002278 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Ian Campbellea2ab692011-08-22 23:44:58 +00002280 return page == skb_frag_page(frag) &&
Eric Dumazet9e903e02011-10-18 21:00:24 +00002281 off == frag->page_offset + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Eric Dumazet38ba0a62012-04-23 17:48:27 +00002283 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Herbert Xu364c6ba2006-06-09 16:10:40 -07002286static inline int __skb_linearize(struct sk_buff *skb)
2287{
2288 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
2289}
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/**
2292 * skb_linearize - convert paged skb to linear one
2293 * @skb: buffer to linarize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 *
2295 * If there is no free memory -ENOMEM is returned, otherwise zero
2296 * is returned and the old skb data released.
2297 */
Herbert Xu364c6ba2006-06-09 16:10:40 -07002298static inline int skb_linearize(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Herbert Xu364c6ba2006-06-09 16:10:40 -07002300 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
2301}
2302
2303/**
Eric Dumazetcef401d2013-01-25 20:34:37 +00002304 * 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{
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00002312 return skb_is_nonlinear(skb) &&
2313 skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00002314}
2315
2316/**
Herbert Xu364c6ba2006-06-09 16:10:40 -07002317 * 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)
2324{
2325 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
2326 __skb_linearize(skb) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
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
Patrick McHardy84fa7932006-08-29 16:44:56 -07002336 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
2337 * CHECKSUM_NONE so that it can be recomputed from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
2339
2340static inline void skb_postpull_rcsum(struct sk_buff *skb,
Herbert Xucbb042f2006-03-20 22:43:56 -08002341 const void *start, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Patrick McHardy84fa7932006-08-29 16:44:56 -07002343 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
2345}
2346
Herbert Xucbb042f2006-03-20 22:43:56 -08002347unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
2348
David S. Miller7ce5a272013-12-02 17:26:05 -05002349/**
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367#define skb_queue_walk(queue, skb) \
2368 for (skb = (queue)->next; \
Linus Torvaldsa1e48912011-05-22 16:51:43 -07002369 skb != (struct sk_buff *)(queue); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 skb = skb->next)
2371
James Chapman46f89142007-04-30 00:07:31 -07002372#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
David S. Miller1164f522008-09-23 00:49:44 -07002377#define skb_queue_walk_from(queue, skb) \
Linus Torvaldsa1e48912011-05-22 16:51:43 -07002378 for (; skb != (struct sk_buff *)(queue); \
David S. Miller1164f522008-09-23 00:49:44 -07002379 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
Stephen Hemminger300ce172005-10-30 13:47:34 -08002386#define skb_queue_reverse_walk(queue, skb) \
2387 for (skb = (queue)->prev; \
Linus Torvaldsa1e48912011-05-22 16:51:43 -07002388 skb != (struct sk_buff *)(queue); \
Stephen Hemminger300ce172005-10-30 13:47:34 -08002389 skb = skb->prev)
2390
David S. Miller686a2952011-01-20 22:47:32 -08002391#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
David S. Miller21dc3302010-08-23 00:13:46 -07002401static inline bool skb_has_frag_list(const struct sk_buff *skb)
David S. Milleree039872009-06-09 00:17:13 -07002402{
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
Joe Perches7965bd42013-09-26 14:48:15 -07002420struct 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);
Joe Perches7965bd42013-09-26 14:48:15 -07002441int 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);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002449unsigned 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);
Joe Perches7965bd42013-09-26 14:48:15 -07002452void 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);
Florian Westphalde960aa2014-01-26 10:58:16 +01002455unsigned int skb_gso_transport_seglen(const struct sk_buff *skb);
Joe Perches7965bd42013-09-26 14:48:15 -07002456struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002457
Daniel Borkmann2817a332013-10-30 11:50:51 +01002458struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468static 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
Patrick McHardy55820ee2005-07-05 14:08:10 -07002473 if (hlen - offset >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return skb->data + offset;
2475
2476 if (skb_copy_bits(skb, offset, buffer, len) < 0)
2477 return NULL;
2478
2479 return buffer;
2480}
2481
Daniel Borkmann4262e5c2013-12-06 11:36:16 +01002482/**
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
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002500static 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
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002514static 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
Joe Perches7965bd42013-09-26 14:48:15 -07002529void skb_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Patrick Ohlyac45f602009-02-12 05:03:37 +00002531static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
2532{
2533 return skb->tstamp;
2534}
2535
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002536/**
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 */
Patrick Ohlyac45f602009-02-12 05:03:37 +00002545static inline void skb_get_timestamp(const struct sk_buff *skb,
2546 struct timeval *stamp)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002547{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002548 *stamp = ktime_to_timeval(skb->tstamp);
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002549}
2550
Patrick Ohlyac45f602009-02-12 05:03:37 +00002551static inline void skb_get_timestampns(const struct sk_buff *skb,
2552 struct timespec *stamp)
2553{
2554 *stamp = ktime_to_timespec(skb->tstamp);
2555}
2556
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002557static inline void __net_timestamp(struct sk_buff *skb)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002558{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002559 skb->tstamp = ktime_get_real();
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002560}
2561
Stephen Hemminger164891a2007-04-23 22:26:16 -07002562static inline ktime_t net_timedelta(ktime_t t)
2563{
2564 return ktime_sub(ktime_get_real(), t);
2565}
2566
Ilpo Järvinenb9ce2042007-06-15 15:08:43 -07002567static inline ktime_t net_invalid_timestamp(void)
2568{
2569 return ktime_set(0, 0);
2570}
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002571
Joe Perches7965bd42013-09-26 14:48:15 -07002572void skb_timestamping_init(void);
Richard Cochranc1f19b52010-07-17 08:49:36 +00002573
2574#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2575
Joe Perches7965bd42013-09-26 14:48:15 -07002576void skb_clone_tx_timestamp(struct sk_buff *skb);
2577bool skb_defer_rx_timestamp(struct sk_buff *skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +00002578
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 *
Richard Cochranda92b192011-10-21 00:49:15 +00002595 * 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 *
Richard Cochranc1f19b52010-07-17 08:49:36 +00002600 * @skb: clone of the the original outgoing packet
Richard Cochranda92b192011-10-21 00:49:15 +00002601 * @hwtstamps: hardware time stamps, may be NULL if not available
Richard Cochranc1f19b52010-07-17 08:49:36 +00002602 *
2603 */
2604void skb_complete_tx_timestamp(struct sk_buff *skb,
2605 struct skb_shared_hwtstamps *hwtstamps);
2606
Patrick Ohlyac45f602009-02-12 05:03:37 +00002607/**
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 */
Joe Perches7965bd42013-09-26 14:48:15 -07002618void skb_tstamp_tx(struct sk_buff *orig_skb,
2619 struct skb_shared_hwtstamps *hwtstamps);
Patrick Ohlyac45f602009-02-12 05:03:37 +00002620
Richard Cochran4507a712010-07-17 08:48:28 +00002621static inline void sw_tx_timestamp(struct sk_buff *skb)
2622{
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002623 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
2624 !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Richard Cochran4507a712010-07-17 08:48:28 +00002625 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()
Richard Cochran4ff75b72011-06-19 03:31:39 +00002632 * function immediately before giving the sk_buff to the MAC hardware.
Richard Cochran4507a712010-07-17 08:48:28 +00002633 *
David S. Miller73409f32013-12-27 13:04:33 -05002634 * 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 *
Richard Cochran4507a712010-07-17 08:48:28 +00002638 * @skb: A socket buffer.
2639 */
2640static inline void skb_tx_timestamp(struct sk_buff *skb)
2641{
Richard Cochranc1f19b52010-07-17 08:49:36 +00002642 skb_clone_tx_timestamp(skb);
Richard Cochran4507a712010-07-17 08:48:28 +00002643 sw_tx_timestamp(skb);
2644}
2645
Johannes Berg6e3e9392011-11-09 10:15:42 +01002646/**
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
Joe Perches7965bd42013-09-26 14:48:15 -07002655__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
2656__sum16 __skb_checksum_complete(struct sk_buff *skb);
Herbert Xufb286bb2005-11-10 13:01:24 -08002657
Herbert Xu60476372007-04-09 11:59:39 -07002658static inline int skb_csum_unnecessary(const struct sk_buff *skb)
2659{
2660 return skb->ip_summed & CHECKSUM_UNNECESSARY;
2661}
2662
Herbert Xufb286bb2005-11-10 13:01:24 -08002663/**
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 */
Al Viro4381ca32007-07-15 21:00:11 +01002679static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08002680{
Herbert Xu60476372007-04-09 11:59:39 -07002681 return skb_csum_unnecessary(skb) ?
2682 0 : __skb_checksum_complete(skb);
Herbert Xufb286bb2005-11-10 13:01:24 -08002683}
2684
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07002685#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Joe Perches7965bd42013-09-26 14:48:15 -07002686void nf_conntrack_destroy(struct nf_conntrack *nfct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687static inline void nf_conntrack_put(struct nf_conntrack *nfct)
2688{
2689 if (nfct && atomic_dec_and_test(&nfct->use))
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07002690 nf_conntrack_destroy(nfct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692static inline void nf_conntrack_get(struct nf_conntrack *nfct)
2693{
2694 if (nfct)
2695 atomic_inc(&nfct->use);
2696}
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01002697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698#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 */
Patrick McHardya193a4a2006-03-20 19:23:05 -08002710static inline void nf_reset(struct sk_buff *skb)
2711{
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07002712#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Patrick McHardya193a4a2006-03-20 19:23:05 -08002713 nf_conntrack_put(skb->nfct);
2714 skb->nfct = NULL;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01002715#endif
Patrick McHardya193a4a2006-03-20 19:23:05 -08002716#ifdef CONFIG_BRIDGE_NETFILTER
2717 nf_bridge_put(skb->nf_bridge);
2718 skb->nf_bridge = NULL;
2719#endif
2720}
2721
Patrick McHardy124dff02013-04-05 20:42:05 +02002722static inline void nf_reset_trace(struct sk_buff *skb)
2723{
Florian Westphal478b3602014-02-15 23:48:45 +01002724#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
Gao feng130549fe2013-03-21 19:48:41 +00002725 skb->nf_trace = 0;
2726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07002729/* 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{
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07002732#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07002733 dst->nfct = src->nfct;
2734 nf_conntrack_get(src->nfct);
2735 dst->nfctinfo = src->nfctinfo;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01002736#endif
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07002737#ifdef CONFIG_BRIDGE_NETFILTER
2738 dst->nf_bridge = src->nf_bridge;
2739 nf_bridge_get(src->nf_bridge);
2740#endif
Florian Westphal478b3602014-02-15 23:48:45 +01002741#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
2742 dst->nf_trace = src->nf_trace;
2743#endif
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07002744}
2745
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07002746static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2747{
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07002748#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07002749 nf_conntrack_put(dst->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01002750#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07002751#ifdef CONFIG_BRIDGE_NETFILTER
2752 nf_bridge_put(dst->nf_bridge);
2753#endif
2754 __nf_copy(dst, src);
2755}
2756
James Morris984bc162006-06-09 00:29:17 -07002757#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
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002775static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
2776{
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002777 skb->queue_mapping = queue_mapping;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002778}
2779
Stephen Hemminger92477442009-03-21 13:39:26 -07002780static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
Pavel Emelyanov4e3ab472007-10-21 17:01:29 -07002781{
Pavel Emelyanov4e3ab472007-10-21 17:01:29 -07002782 return skb->queue_mapping;
Pavel Emelyanov4e3ab472007-10-21 17:01:29 -07002783}
2784
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002785static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
2786{
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002787 to->queue_mapping = from->queue_mapping;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002788}
2789
David S. Millerd5a9e242009-01-27 16:22:11 -08002790static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
2791{
2792 skb->queue_mapping = rx_queue + 1;
2793}
2794
Stephen Hemminger92477442009-03-21 13:39:26 -07002795static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
David S. Millerd5a9e242009-01-27 16:22:11 -08002796{
2797 return skb->queue_mapping - 1;
2798}
2799
Stephen Hemminger92477442009-03-21 13:39:26 -07002800static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
David S. Millerd5a9e242009-01-27 16:22:11 -08002801{
Eric Dumazeta02cec22010-09-22 20:43:57 +00002802 return skb->queue_mapping != 0;
David S. Millerd5a9e242009-01-27 16:22:11 -08002803}
2804
Joe Perches7965bd42013-09-26 14:48:15 -07002805u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
2806 unsigned int num_tx_queues);
Stephen Hemminger92477442009-03-21 13:39:26 -07002807
Denis Kirjanov0b3d8e02013-10-02 05:58:32 +04002808static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
2809{
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07002810#ifdef CONFIG_XFRM
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07002811 return skb->sp;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07002812#else
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07002813 return NULL;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07002814#endif
Denis Kirjanov0b3d8e02013-10-02 05:58:32 +04002815}
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07002816
Pravin B Shelar68c33162013-02-14 14:02:41 +00002817/* 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
Eric Dumazet3347c962013-10-19 11:42:56 -07002820 * tunnel skb it points to outer mac header.
2821 * Keeps track of level of encapsulation of network headers.
2822 */
Pravin B Shelar68c33162013-02-14 14:02:41 +00002823struct skb_gso_cb {
Eric Dumazet3347c962013-10-19 11:42:56 -07002824 int mac_offset;
2825 int encap_level;
Pravin B Shelar68c33162013-02-14 14:02:41 +00002826};
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
Pravin B Shelar1e2bd512013-05-30 06:45:27 +00002835static 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
David S. Millerbdcc0922012-03-07 20:53:36 -05002850static inline bool skb_is_gso(const struct sk_buff *skb)
Herbert Xu89114af2006-07-08 13:34:32 -07002851{
2852 return skb_shinfo(skb)->gso_size;
2853}
2854
Eric Dumazet36a8f392013-09-29 01:21:32 -07002855/* Note: Should be called only if skb_is_gso(skb) is true */
David S. Millerbdcc0922012-03-07 20:53:36 -05002856static inline bool skb_is_gso_v6(const struct sk_buff *skb)
Brice Goglineabd7e32007-10-13 12:33:32 +02002857{
2858 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
2859}
2860
Joe Perches7965bd42013-09-26 14:48:15 -07002861void __skb_warn_lro_forwarding(const struct sk_buff *skb);
Ben Hutchings4497b072008-06-19 16:22:28 -07002862
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. */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04002867 const struct skb_shared_info *shinfo = skb_shinfo(skb);
2868
Alexander Duyckb78462e2010-06-02 12:24:37 +00002869 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
2870 unlikely(shinfo->gso_type == 0)) {
Ben Hutchings4497b072008-06-19 16:22:28 -07002871 __skb_warn_lro_forwarding(skb);
2872 return true;
2873 }
2874 return false;
2875}
2876
Herbert Xu35fc92a2007-03-26 23:22:20 -07002877static 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
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002884/**
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 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04002892static inline void skb_checksum_none_assert(const struct sk_buff *skb)
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002893{
2894#ifdef DEBUG
2895 BUG_ON(skb->ip_summed != CHECKSUM_NONE);
2896#endif
2897}
2898
Rusty Russellf35d9d82008-02-04 23:49:54 -05002899bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
Shirley Maa6686f22011-07-06 12:22:12 +00002900
Paul Durranted1f50c2014-01-09 10:02:46 +00002901int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
2902
Daniel Borkmannf77668d2013-03-19 06:39:30 +00002903u32 __skb_get_poff(const struct sk_buff *skb);
2904
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002905/**
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}
Florian Westphalfe6cc552014-02-13 23:09:12 +01002918
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935#endif /* __KERNEL__ */
2936#endif /* _LINUX_SKBUFF_H */