blob: 7c5036d11feb93f42a6a64bd36616a50a45b0d71 [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>
Chris Leech97fc2f02006-05-23 17:55:33 -070031#include <linux/dmaengine.h>
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -070032#include <linux/hrtimer.h>
Ian Campbell131ea662011-08-19 06:25:00 +000033#include <linux/dma-mapping.h>
Michał Mirosławc8f44af2011-11-15 15:29:55 +000034#include <linux/netdev_features.h>
Eric Dumazet363ec39232014-02-26 14:02:11 -080035#include <linux/sched.h>
Jason Wang5203cd22013-03-26 23:11:21 +000036#include <net/flow_keys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Daniel Borkmann78ea85f2013-12-16 23:27:09 +010038/* A. Checksumming of received packets by device.
39 *
40 * CHECKSUM_NONE:
41 *
42 * Device failed to checksum this packet e.g. due to lack of capabilities.
43 * The packet contains full (though not verified) checksum in packet but
44 * not in skb->csum. Thus, skb->csum is undefined in this case.
45 *
46 * CHECKSUM_UNNECESSARY:
47 *
48 * The hardware you're dealing with doesn't calculate the full checksum
49 * (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums
Tom Herbert77cffe22014-08-27 21:26:46 -070050 * for specific protocols. For such packets it will set CHECKSUM_UNNECESSARY
51 * if their checksums are okay. skb->csum is still undefined in this case
52 * though. It is a bad option, but, unfortunately, nowadays most vendors do
53 * this. Apparently with the secret goal to sell you new devices, when you
54 * will add new protocol to your host, f.e. IPv6 8)
55 *
56 * CHECKSUM_UNNECESSARY is applicable to following protocols:
57 * TCP: IPv6 and IPv4.
58 * UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a
59 * zero UDP checksum for either IPv4 or IPv6, the networking stack
60 * may perform further validation in this case.
61 * GRE: only if the checksum is present in the header.
62 * SCTP: indicates the CRC in SCTP header has been validated.
63 *
64 * skb->csum_level indicates the number of consecutive checksums found in
65 * the packet minus one that have been verified as CHECKSUM_UNNECESSARY.
66 * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet
67 * and a device is able to verify the checksums for UDP (possibly zero),
68 * GRE (checksum flag is set), and TCP-- skb->csum_level would be set to
69 * two. If the device were only able to verify the UDP checksum and not
70 * GRE, either because it doesn't support GRE checksum of because GRE
71 * checksum is bad, skb->csum_level would be set to zero (TCP checksum is
72 * not considered in this case).
Daniel Borkmann78ea85f2013-12-16 23:27:09 +010073 *
74 * CHECKSUM_COMPLETE:
75 *
76 * This is the most generic way. The device supplied checksum of the _whole_
77 * packet as seen by netif_rx() and fills out in skb->csum. Meaning, the
78 * hardware doesn't need to parse L3/L4 headers to implement this.
79 *
80 * Note: Even if device supports only some protocols, but is able to produce
81 * skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY.
82 *
83 * CHECKSUM_PARTIAL:
84 *
85 * This is identical to the case for output below. This may occur on a packet
86 * received directly from another Linux OS, e.g., a virtualized Linux kernel
87 * on the same host. The packet can be treated in the same way as
88 * CHECKSUM_UNNECESSARY, except that on output (i.e., forwarding) the
89 * checksum must be filled in by the OS or the hardware.
90 *
91 * B. Checksumming on output.
92 *
93 * CHECKSUM_NONE:
94 *
95 * The skb was already checksummed by the protocol, or a checksum is not
96 * required.
97 *
98 * CHECKSUM_PARTIAL:
99 *
100 * The device is required to checksum the packet as seen by hard_start_xmit()
101 * from skb->csum_start up to the end, and to record/write the checksum at
102 * offset skb->csum_start + skb->csum_offset.
103 *
104 * The device must show its capabilities in dev->features, set up at device
105 * setup time, e.g. netdev_features.h:
106 *
107 * NETIF_F_HW_CSUM - It's a clever device, it's able to checksum everything.
108 * NETIF_F_IP_CSUM - Device is dumb, it's able to checksum only TCP/UDP over
109 * IPv4. Sigh. Vendors like this way for an unknown reason.
110 * Though, see comment above about CHECKSUM_UNNECESSARY. 8)
111 * NETIF_F_IPV6_CSUM - About as dumb as the last one but does IPv6 instead.
112 * NETIF_F_... - Well, you get the picture.
113 *
114 * CHECKSUM_UNNECESSARY:
115 *
116 * Normally, the device will do per protocol specific checksumming. Protocol
117 * implementations that do not want the NIC to perform the checksum
118 * calculation should use this flag in their outgoing skbs.
119 *
120 * NETIF_F_FCOE_CRC - This indicates that the device can do FCoE FC CRC
121 * offload. Correspondingly, the FCoE protocol driver
122 * stack should use CHECKSUM_UNNECESSARY.
123 *
124 * Any questions? No questions, good. --ANK
125 */
126
Herbert Xu60476372007-04-09 11:59:39 -0700127/* Don't change this without changing skb_csum_unnecessary! */
Daniel Borkmann78ea85f2013-12-16 23:27:09 +0100128#define CHECKSUM_NONE 0
129#define CHECKSUM_UNNECESSARY 1
130#define CHECKSUM_COMPLETE 2
131#define CHECKSUM_PARTIAL 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Tom Herbert77cffe22014-08-27 21:26:46 -0700133/* Maximum value in skb->csum_level */
134#define SKB_MAX_CSUM_LEVEL 3
135
Tobias Klauser0bec8c82014-07-22 12:06:23 +0200136#define SKB_DATA_ALIGN(X) ALIGN(X, SMP_CACHE_BYTES)
David S. Millerfc910a22007-03-25 20:27:59 -0700137#define SKB_WITH_OVERHEAD(X) \
Herbert Xudeea84b2007-10-21 16:27:46 -0700138 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
David S. Millerfc910a22007-03-25 20:27:59 -0700139#define SKB_MAX_ORDER(X, ORDER) \
140 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
142#define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
143
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000144/* return minimum truesize of one skb containing X bytes of data */
145#define SKB_TRUESIZE(X) ((X) + \
146 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
147 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct net_device;
David Howells716ea3a2007-04-02 20:19:53 -0700150struct scatterlist;
Jens Axboe9c55e012007-11-06 23:30:13 -0800151struct pipe_inode_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700153#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154struct nf_conntrack {
155 atomic_t use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200159#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160struct nf_bridge_info {
Eric Dumazetbf1ac5c2012-04-18 23:19:25 +0000161 atomic_t use;
162 unsigned int mask;
163 struct net_device *physindev;
164 struct net_device *physoutdev;
165 unsigned long data[32 / sizeof(unsigned long)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167#endif
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169struct sk_buff_head {
170 /* These two members must be first. */
171 struct sk_buff *next;
172 struct sk_buff *prev;
173
174 __u32 qlen;
175 spinlock_t lock;
176};
177
178struct sk_buff;
179
Ian Campbell9d4dde52011-12-22 23:39:14 +0000180/* To allow 64K frame to be packed as single skb without frag_list we
181 * require 64K/PAGE_SIZE pages plus 1 additional page to allow for
182 * buffers which do not start on a page boundary.
183 *
184 * Since GRO uses frags we allocate at least 16 regardless of page
185 * size.
Anton Blancharda715dea2011-03-27 14:57:26 +0000186 */
Ian Campbell9d4dde52011-12-22 23:39:14 +0000187#if (65536/PAGE_SIZE + 1) < 16
David S. Millereec00952011-03-29 23:34:08 -0700188#define MAX_SKB_FRAGS 16UL
Anton Blancharda715dea2011-03-27 14:57:26 +0000189#else
Ian Campbell9d4dde52011-12-22 23:39:14 +0000190#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
Anton Blancharda715dea2011-03-27 14:57:26 +0000191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193typedef struct skb_frag_struct skb_frag_t;
194
195struct skb_frag_struct {
Ian Campbella8605c62011-10-19 23:01:49 +0000196 struct {
197 struct page *p;
198 } page;
Eric Dumazetcb4dfe52010-09-23 05:06:54 +0000199#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
David S. Millera309bb02007-07-30 18:47:03 -0700200 __u32 page_offset;
201 __u32 size;
Eric Dumazetcb4dfe52010-09-23 05:06:54 +0000202#else
203 __u16 page_offset;
204 __u16 size;
205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Eric Dumazet9e903e02011-10-18 21:00:24 +0000208static inline unsigned int skb_frag_size(const skb_frag_t *frag)
209{
210 return frag->size;
211}
212
213static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
214{
215 frag->size = size;
216}
217
218static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
219{
220 frag->size += delta;
221}
222
223static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
224{
225 frag->size -= delta;
226}
227
Patrick Ohlyac45f602009-02-12 05:03:37 +0000228#define HAVE_HW_TIME_STAMP
229
230/**
Randy Dunlapd3a21be2009-03-02 03:15:58 -0800231 * struct skb_shared_hwtstamps - hardware time stamps
Patrick Ohlyac45f602009-02-12 05:03:37 +0000232 * @hwtstamp: hardware time stamp transformed into duration
233 * since arbitrary point in time
Patrick Ohlyac45f602009-02-12 05:03:37 +0000234 *
235 * Software time stamps generated by ktime_get_real() are stored in
Willem de Bruijn4d276eb2014-07-25 18:01:32 -0400236 * skb->tstamp.
Patrick Ohlyac45f602009-02-12 05:03:37 +0000237 *
238 * hwtstamps can only be compared against other hwtstamps from
239 * the same device.
240 *
241 * This structure is attached to packets as part of the
242 * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
243 */
244struct skb_shared_hwtstamps {
245 ktime_t hwtstamp;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000246};
247
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000248/* Definitions for tx_flags in struct skb_shared_info */
249enum {
250 /* generate hardware time stamp */
251 SKBTX_HW_TSTAMP = 1 << 0,
252
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400253 /* generate software time stamp when queueing packet to NIC */
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000254 SKBTX_SW_TSTAMP = 1 << 1,
255
256 /* device driver is going to provide hardware time stamp */
257 SKBTX_IN_PROGRESS = 1 << 2,
258
Shirley Maa6686f22011-07-06 12:22:12 +0000259 /* device driver supports TX zero-copy buffers */
Eric Dumazet62b1a8a2012-06-14 06:42:44 +0000260 SKBTX_DEV_ZEROCOPY = 1 << 3,
Johannes Berg6e3e9392011-11-09 10:15:42 +0100261
262 /* generate wifi status information (where possible) */
Eric Dumazet62b1a8a2012-06-14 06:42:44 +0000263 SKBTX_WIFI_STATUS = 1 << 4,
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000264
265 /* This indicates at least one fragment might be overwritten
266 * (as in vmsplice(), sendfile() ...)
267 * If we need to compute a TX checksum, we'll need to copy
268 * all frags to avoid possible bad checksum
269 */
270 SKBTX_SHARED_FRAG = 1 << 5,
Willem de Bruijne7fd2882014-08-04 22:11:48 -0400271
272 /* generate software time stamp when entering packet scheduling */
273 SKBTX_SCHED_TSTAMP = 1 << 6,
Willem de Bruijne1c8a602014-08-04 22:11:50 -0400274
275 /* generate software timestamp on peer data acknowledgment */
276 SKBTX_ACK_TSTAMP = 1 << 7,
Shirley Maa6686f22011-07-06 12:22:12 +0000277};
278
Willem de Bruijne1c8a602014-08-04 22:11:50 -0400279#define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \
280 SKBTX_SCHED_TSTAMP | \
281 SKBTX_ACK_TSTAMP)
Willem de Bruijnf24b9be2014-08-04 22:11:45 -0400282#define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP)
283
Shirley Maa6686f22011-07-06 12:22:12 +0000284/*
285 * The callback notifies userspace to release buffers when skb DMA is done in
286 * lower device, the skb last reference should be 0 when calling this.
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000287 * The zerocopy_success argument is true if zero copy transmit occurred,
288 * false on data copy or out of memory error caused by data copy attempt.
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +0000289 * The ctx field is used to track device context.
290 * The desc field is used to track userspace buffer index.
Shirley Maa6686f22011-07-06 12:22:12 +0000291 */
292struct ubuf_info {
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000293 void (*callback)(struct ubuf_info *, bool zerocopy_success);
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +0000294 void *ctx;
Shirley Maa6686f22011-07-06 12:22:12 +0000295 unsigned long desc;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000296};
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/* This data is invariant across clones and lives at
299 * the end of the header data, ie. at skb->end.
300 */
301struct skb_shared_info {
Ian Campbell9f42f122012-01-05 07:13:39 +0000302 unsigned char nr_frags;
303 __u8 tx_flags;
Herbert Xu79671682006-06-22 02:40:14 -0700304 unsigned short gso_size;
305 /* Warning: this field is not always filled in (UFO)! */
306 unsigned short gso_segs;
307 unsigned short gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct sk_buff *frag_list;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000309 struct skb_shared_hwtstamps hwtstamps;
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400310 u32 tskey;
Ian Campbell9f42f122012-01-05 07:13:39 +0000311 __be32 ip6_frag_id;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700312
313 /*
314 * Warning : all fields before dataref are cleared in __alloc_skb()
315 */
316 atomic_t dataref;
317
Johann Baudy69e3c752009-05-18 22:11:22 -0700318 /* Intermediate layers must ensure that destructor_arg
319 * remains valid until skb destructor */
320 void * destructor_arg;
Shirley Maa6686f22011-07-06 12:22:12 +0000321
Eric Dumazetfed66382010-07-22 19:09:08 +0000322 /* must be last field, see pskb_expand_head() */
323 skb_frag_t frags[MAX_SKB_FRAGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324};
325
326/* We divide dataref into two halves. The higher 16 bits hold references
327 * to the payload part of skb->data. The lower 16 bits hold references to
Patrick McHardy334a8132007-06-25 04:35:20 -0700328 * the entire skb->data. A clone of a headerless skb holds the length of
329 * the header in skb->hdr_len.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
331 * All users must obey the rule that the skb->data reference count must be
332 * greater than or equal to the payload reference count.
333 *
334 * Holding a reference to the payload part means that the user does not
335 * care about modifications to the header part of skb->data.
336 */
337#define SKB_DATAREF_SHIFT 16
338#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
339
David S. Millerd179cd12005-08-17 14:57:30 -0700340
341enum {
342 SKB_FCLONE_UNAVAILABLE,
343 SKB_FCLONE_ORIG,
344 SKB_FCLONE_CLONE,
345};
346
Herbert Xu79671682006-06-22 02:40:14 -0700347enum {
348 SKB_GSO_TCPV4 = 1 << 0,
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700349 SKB_GSO_UDP = 1 << 1,
Herbert Xu576a30e2006-06-27 13:22:38 -0700350
351 /* This indicates the skb is from an untrusted source. */
352 SKB_GSO_DODGY = 1 << 2,
Michael Chanb0da85372006-06-29 12:30:00 -0700353
354 /* This indicates the tcp segment has CWR set. */
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700355 SKB_GSO_TCP_ECN = 1 << 3,
356
357 SKB_GSO_TCPV6 = 1 << 4,
Chris Leech01d5b2f2009-02-27 14:06:49 -0800358
359 SKB_GSO_FCOE = 1 << 5,
Pravin B Shelar68c33162013-02-14 14:02:41 +0000360
361 SKB_GSO_GRE = 1 << 6,
Pravin B Shelar73136262013-03-07 13:21:51 +0000362
Tom Herbert4b282522014-06-14 23:23:52 -0700363 SKB_GSO_GRE_CSUM = 1 << 7,
Simon Horman0d89d202013-05-23 21:02:52 +0000364
Tom Herbert4b282522014-06-14 23:23:52 -0700365 SKB_GSO_IPIP = 1 << 8,
Eric Dumazetcb32f512013-10-19 11:42:57 -0700366
Tom Herbert4b282522014-06-14 23:23:52 -0700367 SKB_GSO_SIT = 1 << 9,
Eric Dumazet61c1db72013-10-20 20:47:30 -0700368
Tom Herbert4b282522014-06-14 23:23:52 -0700369 SKB_GSO_UDP_TUNNEL = 1 << 10,
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700370
371 SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11,
Tom Herbert4749c092014-06-04 17:20:23 -0700372
Tom Herbert4b282522014-06-14 23:23:52 -0700373 SKB_GSO_MPLS = 1 << 12,
374
Herbert Xu79671682006-06-22 02:40:14 -0700375};
376
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700377#if BITS_PER_LONG > 32
378#define NET_SKBUFF_DATA_USES_OFFSET 1
379#endif
380
381#ifdef NET_SKBUFF_DATA_USES_OFFSET
382typedef unsigned int sk_buff_data_t;
383#else
384typedef unsigned char *sk_buff_data_t;
385#endif
386
Eric Dumazet363ec39232014-02-26 14:02:11 -0800387/**
388 * struct skb_mstamp - multi resolution time stamps
389 * @stamp_us: timestamp in us resolution
390 * @stamp_jiffies: timestamp in jiffies
391 */
392struct skb_mstamp {
393 union {
394 u64 v64;
395 struct {
396 u32 stamp_us;
397 u32 stamp_jiffies;
398 };
399 };
400};
401
402/**
403 * skb_mstamp_get - get current timestamp
404 * @cl: place to store timestamps
405 */
406static inline void skb_mstamp_get(struct skb_mstamp *cl)
407{
408 u64 val = local_clock();
409
410 do_div(val, NSEC_PER_USEC);
411 cl->stamp_us = (u32)val;
412 cl->stamp_jiffies = (u32)jiffies;
413}
414
415/**
416 * skb_mstamp_delta - compute the difference in usec between two skb_mstamp
417 * @t1: pointer to newest sample
418 * @t0: pointer to oldest sample
419 */
420static inline u32 skb_mstamp_us_delta(const struct skb_mstamp *t1,
421 const struct skb_mstamp *t0)
422{
423 s32 delta_us = t1->stamp_us - t0->stamp_us;
424 u32 delta_jiffies = t1->stamp_jiffies - t0->stamp_jiffies;
425
426 /* If delta_us is negative, this might be because interval is too big,
427 * or local_clock() drift is too big : fallback using jiffies.
428 */
429 if (delta_us <= 0 ||
430 delta_jiffies >= (INT_MAX / (USEC_PER_SEC / HZ)))
431
432 delta_us = jiffies_to_usecs(delta_jiffies);
433
434 return delta_us;
435}
436
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/**
439 * struct sk_buff - socket buffer
440 * @next: Next buffer in list
441 * @prev: Previous buffer in list
Eric Dumazet363ec39232014-02-26 14:02:11 -0800442 * @tstamp: Time we arrived/left
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700443 * @sk: Socket we are owned by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * @dev: Device we arrived on/are leaving by
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700445 * @cb: Control buffer. Free for use by every layer. Put private vars here
Eric Dumazet7fee2262010-05-11 23:19:48 +0000446 * @_skb_refdst: destination entry (with norefcount bit)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700447 * @sp: the security path, used for xfrm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * @len: Length of actual data
449 * @data_len: Data length
450 * @mac_len: Length of link layer header
Patrick McHardy334a8132007-06-25 04:35:20 -0700451 * @hdr_len: writable header length of cloned skb
Herbert Xu663ead32007-04-09 11:59:07 -0700452 * @csum: Checksum (must include start/offset pair)
453 * @csum_start: Offset from skb->head where checksumming should start
454 * @csum_offset: Offset from csum_start where checksum should be stored
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700455 * @priority: Packet queueing priority
WANG Cong60ff7462014-05-04 16:39:18 -0700456 * @ignore_df: allow local fragmentation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * @cloned: Head may be cloned (check refcnt to be sure)
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700458 * @ip_summed: Driver fed us an IP checksum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * @nohdr: Payload reference only, must not modify header
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700460 * @nfctinfo: Relationship of this skb to the connection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * @pkt_type: Packet class
Randy Dunlapc83c2482005-10-18 22:07:41 -0700462 * @fclone: skbuff clone status
Randy Dunlapc83c2482005-10-18 22:07:41 -0700463 * @ipvs_property: skbuff is owned by ipvs
Randy Dunlap31729362008-02-18 20:52:13 -0800464 * @peeked: this packet has been seen already, so stats have been
465 * done for it, don't do them again
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700466 * @nf_trace: netfilter packet trace flag
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700467 * @protocol: Packet protocol from driver
468 * @destructor: Destruct function
469 * @nfct: Associated connection, if any
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
Eric Dumazet8964be42009-11-20 15:35:04 -0800471 * @skb_iif: ifindex of device we arrived on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * @tc_index: Traffic control index
473 * @tc_verd: traffic control verdict
Tom Herbert61b905d2014-03-24 15:34:47 -0700474 * @hash: the packet hash
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700475 * @queue_mapping: Queue mapping for multiqueue devices
David S. Miller0b725a22014-08-25 15:51:53 -0700476 * @xmit_more: More SKBs are pending for this queue
Randy Dunlap553a5672008-04-20 10:51:01 -0700477 * @ndisc_nodetype: router type (from link layer)
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700478 * @ooo_okay: allow the mapping of a socket to a queue to be changed
Tom Herbert61b905d2014-03-24 15:34:47 -0700479 * @l4_hash: indicate hash is a canonical 4-tuple hash over transport
Changli Gao4ca24622011-08-19 07:26:44 -0700480 * ports.
Tom Herberta3b18dd2014-07-01 21:33:17 -0700481 * @sw_hash: indicates hash was computed in software stack
Johannes Berg6e3e9392011-11-09 10:15:42 +0100482 * @wifi_acked_valid: wifi_acked was set
483 * @wifi_acked: whether frame was acked on wifi or not
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000484 * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS
Randy Dunlapf4b8ea72006-06-22 16:00:11 -0700485 * @dma_cookie: a cookie to one of several possible DMA operations
486 * done by skb DMA functions
Eliezer Tamir06021292013-06-10 11:39:50 +0300487 * @napi_id: id of the NAPI struct this skb came from
James Morris984bc162006-06-09 00:29:17 -0700488 * @secmark: security marking
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700489 * @mark: Generic packet mark
490 * @dropcount: total number of sk_receive_queue overflows
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000491 * @vlan_proto: vlan encapsulation protocol
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700492 * @vlan_tci: vlan tag control information
Simon Horman0d89d202013-05-23 21:02:52 +0000493 * @inner_protocol: Protocol (encapsulation)
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000494 * @inner_transport_header: Inner transport layer header (encapsulation)
495 * @inner_network_header: Network layer header (encapsulation)
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000496 * @inner_mac_header: Link layer header (encapsulation)
Daniel Balutad84e0bd2011-07-10 07:04:04 -0700497 * @transport_header: Transport layer header
498 * @network_header: Network layer header
499 * @mac_header: Link layer header
500 * @tail: Tail pointer
501 * @end: End pointer
502 * @head: Head of buffer
503 * @data: Data head pointer
504 * @truesize: Buffer size
505 * @users: User count - see {datagram,tcp}.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
507
508struct sk_buff {
509 /* These two members must be first. */
510 struct sk_buff *next;
511 struct sk_buff *prev;
512
Eric Dumazet363ec39232014-02-26 14:02:11 -0800513 union {
514 ktime_t tstamp;
515 struct skb_mstamp skb_mstamp;
516 };
Felix Fietkauda3f5cf2010-02-23 11:45:51 +0000517
518 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * This is the control buffer. It is free to use for every
523 * layer. Please put your private variables there. If you
524 * want to keep them across layers you have to do a skb_clone()
525 * first. This is owned by whoever has the skb queued ATM.
526 */
Felix Fietkauda3f5cf2010-02-23 11:45:51 +0000527 char cb[48] __aligned(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Eric Dumazet7fee2262010-05-11 23:19:48 +0000529 unsigned long _skb_refdst;
Eric Dumazetb1937222014-09-28 22:18:47 -0700530 void (*destructor)(struct sk_buff *skb);
Felix Fietkauda3f5cf2010-02-23 11:45:51 +0000531#ifdef CONFIG_XFRM
532 struct sec_path *sp;
533#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700534#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
535 struct nf_conntrack *nfct;
536#endif
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200537#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Eric Dumazetb1937222014-09-28 22:18:47 -0700538 struct nf_bridge_info *nf_bridge;
539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 unsigned int len,
Patrick McHardy334a8132007-06-25 04:35:20 -0700541 data_len;
542 __u16 mac_len,
543 hdr_len;
Eric Dumazetb1937222014-09-28 22:18:47 -0700544
545 /* Following fields are _not_ copied in __copy_skb_header()
546 * Note that queue_mapping is here mostly to fill a hole.
547 */
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200548 kmemcheck_bitfield_begin(flags1);
Eric Dumazetb1937222014-09-28 22:18:47 -0700549 __u16 queue_mapping;
550 __u8 cloned:1,
Harald Welte6869c4d2005-08-09 19:24:19 -0700551 nohdr:1,
Eric Dumazetb1937222014-09-28 22:18:47 -0700552 fclone:2,
553 peeked:1,
554 head_frag:1,
555 xmit_more:1;
556 /* one bit hole */
557 kmemcheck_bitfield_end(flags1);
558
Eric Dumazetb1937222014-09-28 22:18:47 -0700559 /* fields enclosed in headers_start/headers_end are copied
560 * using a single memcpy() in __copy_skb_header()
561 */
562 __u32 headers_start[0];
Hannes Frederic Sowa233577a2014-09-12 14:04:43 +0200563
564/* if you move pkt_type around you also must adapt those constants */
565#ifdef __BIG_ENDIAN_BITFIELD
566#define PKT_TYPE_MAX (7 << 5)
567#else
568#define PKT_TYPE_MAX 7
569#endif
570#define PKT_TYPE_OFFSET() offsetof(struct sk_buff, __pkt_type_offset)
571
572 __u8 __pkt_type_offset[0];
Eric Dumazetb1937222014-09-28 22:18:47 -0700573 __u8 pkt_type:3;
574 __u8 pfmemalloc:1;
575 __u8 ignore_df:1;
576 __u8 nfctinfo:3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Eric Dumazetb1937222014-09-28 22:18:47 -0700578 __u8 nf_trace:1;
579 __u8 ip_summed:2;
580 __u8 ooo_okay:1;
581 __u8 l4_hash:1;
582 __u8 sw_hash:1;
583 __u8 wifi_acked_valid:1;
584 __u8 wifi_acked:1;
585
586 __u8 no_fcs:1;
587 /* Indicates the inner headers are valid in the skbuff. */
588 __u8 encapsulation:1;
589 __u8 encap_hdr_csum:1;
590 __u8 csum_valid:1;
591 __u8 csum_complete_sw:1;
592 __u8 csum_level:2;
593 __u8 csum_bad:1;
594
595#ifdef CONFIG_IPV6_NDISC_NODETYPE
596 __u8 ndisc_nodetype:2;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100597#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700598 __u8 ipvs_property:1;
Tom Herbert8bce6d72014-09-29 20:22:29 -0700599 __u8 inner_protocol_type:1;
600 /* 4 or 6 bit hole */
Alexander Duyck4031ae62012-01-27 06:22:53 +0000601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#ifdef CONFIG_NET_SCHED
Patrick McHardyb6b99eb2005-08-09 19:33:51 -0700603 __u16 tc_index; /* traffic control index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604#ifdef CONFIG_NET_CLS_ACT
Patrick McHardyb6b99eb2005-08-09 19:33:51 -0700605 __u16 tc_verd; /* traffic control verdict */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200608
Eric Dumazetb1937222014-09-28 22:18:47 -0700609 union {
610 __wsum csum;
611 struct {
612 __u16 csum_start;
613 __u16 csum_offset;
614 };
615 };
616 __u32 priority;
617 int skb_iif;
618 __u32 hash;
619 __be16 vlan_proto;
620 __u16 vlan_tci;
Cong Wange0d10952013-08-01 11:10:25 +0800621#if defined CONFIG_NET_DMA || defined CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +0300622 union {
623 unsigned int napi_id;
624 dma_cookie_t dma_cookie;
625 };
Chris Leech97fc2f02006-05-23 17:55:33 -0700626#endif
James Morris984bc162006-06-09 00:29:17 -0700627#ifdef CONFIG_NETWORK_SECMARK
628 __u32 secmark;
629#endif
Neil Horman3b885782009-10-12 13:26:31 -0700630 union {
631 __u32 mark;
632 __u32 dropcount;
Eric Dumazet16fad692013-03-14 05:40:32 +0000633 __u32 reserved_tailroom;
Neil Horman3b885782009-10-12 13:26:31 -0700634 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Tom Herbert8bce6d72014-09-29 20:22:29 -0700636 union {
637 __be16 inner_protocol;
638 __u8 inner_ipproto;
639 };
640
Simon Horman1a37e412013-05-23 21:02:51 +0000641 __u16 inner_transport_header;
642 __u16 inner_network_header;
643 __u16 inner_mac_header;
Eric Dumazetb1937222014-09-28 22:18:47 -0700644
645 __be16 protocol;
Simon Horman1a37e412013-05-23 21:02:51 +0000646 __u16 transport_header;
647 __u16 network_header;
648 __u16 mac_header;
Eric Dumazetb1937222014-09-28 22:18:47 -0700649
650 __u32 headers_end[0];
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* These elements must be at the end, see alloc_skb() for details. */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700653 sk_buff_data_t tail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700654 sk_buff_data_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 unsigned char *head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700656 *data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700657 unsigned int truesize;
658 atomic_t users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659};
660
661#ifdef __KERNEL__
662/*
663 * Handling routines are only of interest to the kernel
664 */
665#include <linux/slab.h>
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Mel Gormanc93bdd02012-07-31 16:44:19 -0700668#define SKB_ALLOC_FCLONE 0x01
669#define SKB_ALLOC_RX 0x02
670
671/* Returns true if the skb was allocated from PFMEMALLOC reserves */
672static inline bool skb_pfmemalloc(const struct sk_buff *skb)
673{
674 return unlikely(skb->pfmemalloc);
675}
676
Eric Dumazet7fee2262010-05-11 23:19:48 +0000677/*
678 * skb might have a dst pointer attached, refcounted or not.
679 * _skb_refdst low order bit is set if refcount was _not_ taken
680 */
681#define SKB_DST_NOREF 1UL
682#define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
683
684/**
685 * skb_dst - returns skb dst_entry
686 * @skb: buffer
687 *
688 * Returns skb dst_entry, regardless of reference taken or not.
689 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000690static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
691{
Eric Dumazet7fee2262010-05-11 23:19:48 +0000692 /* If refdst was not refcounted, check we still are in a
693 * rcu_read_lock section
694 */
695 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
696 !rcu_read_lock_held() &&
697 !rcu_read_lock_bh_held());
698 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
Eric Dumazetadf30902009-06-02 05:19:30 +0000699}
700
Eric Dumazet7fee2262010-05-11 23:19:48 +0000701/**
702 * skb_dst_set - sets skb dst
703 * @skb: buffer
704 * @dst: dst entry
705 *
706 * Sets skb dst, assuming a reference was taken on dst and should
707 * be released by skb_dst_drop()
708 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000709static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
710{
Eric Dumazet7fee2262010-05-11 23:19:48 +0000711 skb->_skb_refdst = (unsigned long)dst;
712}
713
Joe Perches7965bd42013-09-26 14:48:15 -0700714void __skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst,
715 bool force);
Julian Anastasov932bc4d2013-03-21 11:57:58 +0200716
717/**
718 * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
719 * @skb: buffer
720 * @dst: dst entry
721 *
722 * Sets skb dst, assuming a reference was not taken on dst.
723 * If dst entry is cached, we do not take reference and dst_release
724 * will be avoided by refdst_drop. If dst entry is not cached, we take
725 * reference, so that last dst_release can destroy the dst immediately.
726 */
727static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
728{
729 __skb_dst_set_noref(skb, dst, false);
730}
731
732/**
733 * skb_dst_set_noref_force - sets skb dst, without taking reference
734 * @skb: buffer
735 * @dst: dst entry
736 *
737 * Sets skb dst, assuming a reference was not taken on dst.
738 * No reference is taken and no dst_release will be called. While for
739 * cached dsts deferred reclaim is a basic feature, for entries that are
740 * not cached it is caller's job to guarantee that last dst_release for
741 * provided dst happens when nobody uses it, eg. after a RCU grace period.
742 */
743static inline void skb_dst_set_noref_force(struct sk_buff *skb,
744 struct dst_entry *dst)
745{
746 __skb_dst_set_noref(skb, dst, true);
747}
Eric Dumazet7fee2262010-05-11 23:19:48 +0000748
749/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300750 * skb_dst_is_noref - Test if skb dst isn't refcounted
Eric Dumazet7fee2262010-05-11 23:19:48 +0000751 * @skb: buffer
752 */
753static inline bool skb_dst_is_noref(const struct sk_buff *skb)
754{
755 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000756}
757
Eric Dumazet511c3f92009-06-02 05:14:27 +0000758static inline struct rtable *skb_rtable(const struct sk_buff *skb)
759{
Eric Dumazetadf30902009-06-02 05:19:30 +0000760 return (struct rtable *)skb_dst(skb);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000761}
762
Joe Perches7965bd42013-09-26 14:48:15 -0700763void kfree_skb(struct sk_buff *skb);
764void kfree_skb_list(struct sk_buff *segs);
765void skb_tx_error(struct sk_buff *skb);
766void consume_skb(struct sk_buff *skb);
767void __kfree_skb(struct sk_buff *skb);
Eric Dumazetd7e88832012-04-30 08:10:34 +0000768extern struct kmem_cache *skbuff_head_cache;
Eric Dumazetbad43ca2012-05-19 03:02:02 +0000769
Joe Perches7965bd42013-09-26 14:48:15 -0700770void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
771bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
772 bool *fragstolen, int *delta_truesize);
Eric Dumazetbad43ca2012-05-19 03:02:02 +0000773
Joe Perches7965bd42013-09-26 14:48:15 -0700774struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
775 int node);
776struct sk_buff *build_skb(void *data, unsigned int frag_size);
David S. Millerd179cd12005-08-17 14:57:30 -0700777static inline struct sk_buff *alloc_skb(unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100778 gfp_t priority)
David S. Millerd179cd12005-08-17 14:57:30 -0700779{
Eric Dumazet564824b2010-10-11 19:05:25 +0000780 return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
David S. Millerd179cd12005-08-17 14:57:30 -0700781}
782
Eric Dumazet2e4e4412014-09-17 04:49:49 -0700783struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
784 unsigned long data_len,
785 int max_page_order,
786 int *errcode,
787 gfp_t gfp_mask);
788
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700789/* Layout of fast clones : [skb1][skb2][fclone_ref] */
790struct sk_buff_fclones {
791 struct sk_buff skb1;
792
793 struct sk_buff skb2;
794
795 atomic_t fclone_ref;
796};
797
798/**
799 * skb_fclone_busy - check if fclone is busy
800 * @skb: buffer
801 *
802 * Returns true is skb is a fast clone, and its clone is not freed.
803 */
804static inline bool skb_fclone_busy(const struct sk_buff *skb)
805{
806 const struct sk_buff_fclones *fclones;
807
808 fclones = container_of(skb, struct sk_buff_fclones, skb1);
809
810 return skb->fclone == SKB_FCLONE_ORIG &&
811 fclones->skb2.fclone == SKB_FCLONE_CLONE;
812}
813
David S. Millerd179cd12005-08-17 14:57:30 -0700814static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100815 gfp_t priority)
David S. Millerd179cd12005-08-17 14:57:30 -0700816{
Mel Gormanc93bdd02012-07-31 16:44:19 -0700817 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
David S. Millerd179cd12005-08-17 14:57:30 -0700818}
819
Joe Perches7965bd42013-09-26 14:48:15 -0700820struct sk_buff *__alloc_skb_head(gfp_t priority, int node);
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000821static inline struct sk_buff *alloc_skb_head(gfp_t priority)
822{
823 return __alloc_skb_head(priority, -1);
824}
825
Joe Perches7965bd42013-09-26 14:48:15 -0700826struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
827int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
828struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
829struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
Octavian Purdilabad93e92014-06-12 01:36:26 +0300830struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
831 gfp_t gfp_mask, bool fclone);
832static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom,
833 gfp_t gfp_mask)
834{
835 return __pskb_copy_fclone(skb, headroom, gfp_mask, false);
836}
Eric Dumazet117632e2011-12-03 21:39:53 +0000837
Joe Perches7965bd42013-09-26 14:48:15 -0700838int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask);
839struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
840 unsigned int headroom);
841struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom,
842 int newtailroom, gfp_t priority);
Fan Du25a91d82014-01-18 09:54:23 +0800843int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
844 int offset, int len);
Joe Perches7965bd42013-09-26 14:48:15 -0700845int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset,
846 int len);
847int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer);
848int skb_pad(struct sk_buff *skb, int pad);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000849#define dev_kfree_skb(a) consume_skb(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Joe Perches7965bd42013-09-26 14:48:15 -0700851int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
852 int getfrag(void *from, char *to, int offset,
853 int len, int odd, struct sk_buff *skb),
854 void *from, int length);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700855
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800856struct skb_seq_state {
Thomas Graf677e90e2005-06-23 20:59:51 -0700857 __u32 lower_offset;
858 __u32 upper_offset;
859 __u32 frag_idx;
860 __u32 stepped_offset;
861 struct sk_buff *root_skb;
862 struct sk_buff *cur_skb;
863 __u8 *frag_data;
864};
865
Joe Perches7965bd42013-09-26 14:48:15 -0700866void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
867 unsigned int to, struct skb_seq_state *st);
868unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
869 struct skb_seq_state *st);
870void skb_abort_seq_read(struct skb_seq_state *st);
Thomas Graf677e90e2005-06-23 20:59:51 -0700871
Joe Perches7965bd42013-09-26 14:48:15 -0700872unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
873 unsigned int to, struct ts_config *config,
874 struct ts_state *state);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -0700875
Tom Herbert09323cc2013-12-15 22:16:19 -0800876/*
877 * Packet hash types specify the type of hash in skb_set_hash.
878 *
879 * Hash types refer to the protocol layer addresses which are used to
880 * construct a packet's hash. The hashes are used to differentiate or identify
881 * flows of the protocol layer for the hash type. Hash types are either
882 * layer-2 (L2), layer-3 (L3), or layer-4 (L4).
883 *
884 * Properties of hashes:
885 *
886 * 1) Two packets in different flows have different hash values
887 * 2) Two packets in the same flow should have the same hash value
888 *
889 * A hash at a higher layer is considered to be more specific. A driver should
890 * set the most specific hash possible.
891 *
892 * A driver cannot indicate a more specific hash than the layer at which a hash
893 * was computed. For instance an L3 hash cannot be set as an L4 hash.
894 *
895 * A driver may indicate a hash level which is less specific than the
896 * actual layer the hash was computed on. For instance, a hash computed
897 * at L4 may be considered an L3 hash. This should only be done if the
898 * driver can't unambiguously determine that the HW computed the hash at
899 * the higher layer. Note that the "should" in the second property above
900 * permits this.
901 */
902enum pkt_hash_types {
903 PKT_HASH_TYPE_NONE, /* Undefined type */
904 PKT_HASH_TYPE_L2, /* Input: src_MAC, dest_MAC */
905 PKT_HASH_TYPE_L3, /* Input: src_IP, dst_IP */
906 PKT_HASH_TYPE_L4, /* Input: src_IP, dst_IP, src_port, dst_port */
907};
908
909static inline void
910skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
911{
Tom Herbert61b905d2014-03-24 15:34:47 -0700912 skb->l4_hash = (type == PKT_HASH_TYPE_L4);
Tom Herberta3b18dd2014-07-01 21:33:17 -0700913 skb->sw_hash = 0;
Tom Herbert61b905d2014-03-24 15:34:47 -0700914 skb->hash = hash;
Tom Herbert09323cc2013-12-15 22:16:19 -0800915}
916
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800917void __skb_get_hash(struct sk_buff *skb);
918static inline __u32 skb_get_hash(struct sk_buff *skb)
Krishna Kumarbfb564e2010-08-04 06:15:52 +0000919{
Tom Herberta3b18dd2014-07-01 21:33:17 -0700920 if (!skb->l4_hash && !skb->sw_hash)
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800921 __skb_get_hash(skb);
Krishna Kumarbfb564e2010-08-04 06:15:52 +0000922
Tom Herbert61b905d2014-03-24 15:34:47 -0700923 return skb->hash;
Krishna Kumarbfb564e2010-08-04 06:15:52 +0000924}
925
Tom Herbert57bdf7f42014-01-15 08:57:54 -0800926static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
927{
Tom Herbert61b905d2014-03-24 15:34:47 -0700928 return skb->hash;
Tom Herbert57bdf7f42014-01-15 08:57:54 -0800929}
930
Tom Herbert7539fad2013-12-15 22:12:18 -0800931static inline void skb_clear_hash(struct sk_buff *skb)
932{
Tom Herbert61b905d2014-03-24 15:34:47 -0700933 skb->hash = 0;
Tom Herberta3b18dd2014-07-01 21:33:17 -0700934 skb->sw_hash = 0;
Tom Herbert61b905d2014-03-24 15:34:47 -0700935 skb->l4_hash = 0;
Tom Herbert7539fad2013-12-15 22:12:18 -0800936}
937
938static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
939{
Tom Herbert61b905d2014-03-24 15:34:47 -0700940 if (!skb->l4_hash)
Tom Herbert7539fad2013-12-15 22:12:18 -0800941 skb_clear_hash(skb);
942}
943
Tom Herbert3df7a742013-12-15 22:16:29 -0800944static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
945{
Tom Herbert61b905d2014-03-24 15:34:47 -0700946 to->hash = from->hash;
Tom Herberta3b18dd2014-07-01 21:33:17 -0700947 to->sw_hash = from->sw_hash;
Tom Herbert61b905d2014-03-24 15:34:47 -0700948 to->l4_hash = from->l4_hash;
Tom Herbert3df7a742013-12-15 22:16:29 -0800949};
950
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700951#ifdef NET_SKBUFF_DATA_USES_OFFSET
952static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
953{
954 return skb->head + skb->end;
955}
Alexander Duyckec47ea82012-05-04 14:26:56 +0000956
957static inline unsigned int skb_end_offset(const struct sk_buff *skb)
958{
959 return skb->end;
960}
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700961#else
962static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
963{
964 return skb->end;
965}
Alexander Duyckec47ea82012-05-04 14:26:56 +0000966
967static inline unsigned int skb_end_offset(const struct sk_buff *skb)
968{
969 return skb->end - skb->head;
970}
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700971#endif
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/* Internal */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700974#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Patrick Ohlyac45f602009-02-12 05:03:37 +0000976static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
977{
978 return &skb_shinfo(skb)->hwtstamps;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/**
982 * skb_queue_empty - check if a queue is empty
983 * @list: queue head
984 *
985 * Returns true if the queue is empty, false otherwise.
986 */
987static inline int skb_queue_empty(const struct sk_buff_head *list)
988{
Daniel Borkmannfd44b932014-01-07 23:23:44 +0100989 return list->next == (const struct sk_buff *) list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/**
David S. Millerfc7ebb22008-09-23 00:34:07 -0700993 * skb_queue_is_last - check if skb is the last entry in the queue
994 * @list: queue head
995 * @skb: buffer
996 *
997 * Returns true if @skb is the last buffer on the list.
998 */
999static inline bool skb_queue_is_last(const struct sk_buff_head *list,
1000 const struct sk_buff *skb)
1001{
Daniel Borkmannfd44b932014-01-07 23:23:44 +01001002 return skb->next == (const struct sk_buff *) list;
David S. Millerfc7ebb22008-09-23 00:34:07 -07001003}
1004
1005/**
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001006 * skb_queue_is_first - check if skb is the first entry in the queue
1007 * @list: queue head
1008 * @skb: buffer
1009 *
1010 * Returns true if @skb is the first buffer on the list.
1011 */
1012static inline bool skb_queue_is_first(const struct sk_buff_head *list,
1013 const struct sk_buff *skb)
1014{
Daniel Borkmannfd44b932014-01-07 23:23:44 +01001015 return skb->prev == (const struct sk_buff *) list;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001016}
1017
1018/**
David S. Miller249c8b42008-09-23 00:44:42 -07001019 * skb_queue_next - return the next packet in the queue
1020 * @list: queue head
1021 * @skb: current buffer
1022 *
1023 * Return the next packet in @list after @skb. It is only valid to
1024 * call this if skb_queue_is_last() evaluates to false.
1025 */
1026static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
1027 const struct sk_buff *skb)
1028{
1029 /* This BUG_ON may seem severe, but if we just return then we
1030 * are going to dereference garbage.
1031 */
1032 BUG_ON(skb_queue_is_last(list, skb));
1033 return skb->next;
1034}
1035
1036/**
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001037 * skb_queue_prev - return the prev packet in the queue
1038 * @list: queue head
1039 * @skb: current buffer
1040 *
1041 * Return the prev packet in @list before @skb. It is only valid to
1042 * call this if skb_queue_is_first() evaluates to false.
1043 */
1044static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
1045 const struct sk_buff *skb)
1046{
1047 /* This BUG_ON may seem severe, but if we just return then we
1048 * are going to dereference garbage.
1049 */
1050 BUG_ON(skb_queue_is_first(list, skb));
1051 return skb->prev;
1052}
1053
1054/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * skb_get - reference buffer
1056 * @skb: buffer to reference
1057 *
1058 * Makes another reference to a socket buffer and returns a pointer
1059 * to the buffer.
1060 */
1061static inline struct sk_buff *skb_get(struct sk_buff *skb)
1062{
1063 atomic_inc(&skb->users);
1064 return skb;
1065}
1066
1067/*
1068 * If users == 1, we are the only owner and are can avoid redundant
1069 * atomic change.
1070 */
1071
1072/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * skb_cloned - is the buffer a clone
1074 * @skb: buffer to check
1075 *
1076 * Returns true if the buffer was generated with skb_clone() and is
1077 * one of multiple shared copies of the buffer. Cloned buffers are
1078 * shared data so must not be written to under normal circumstances.
1079 */
1080static inline int skb_cloned(const struct sk_buff *skb)
1081{
1082 return skb->cloned &&
1083 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
1084}
1085
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +00001086static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
1087{
1088 might_sleep_if(pri & __GFP_WAIT);
1089
1090 if (skb_cloned(skb))
1091 return pskb_expand_head(skb, 0, 0, pri);
1092
1093 return 0;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/**
1097 * skb_header_cloned - is the header a clone
1098 * @skb: buffer to check
1099 *
1100 * Returns true if modifying the header part of the buffer requires
1101 * the data to be copied.
1102 */
1103static inline int skb_header_cloned(const struct sk_buff *skb)
1104{
1105 int dataref;
1106
1107 if (!skb->cloned)
1108 return 0;
1109
1110 dataref = atomic_read(&skb_shinfo(skb)->dataref);
1111 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
1112 return dataref != 1;
1113}
1114
1115/**
1116 * skb_header_release - release reference to header
1117 * @skb: buffer to operate on
1118 *
1119 * Drop a reference to the header part of the buffer. This is done
1120 * by acquiring a payload reference. You must not read from the header
1121 * part of skb->data after this.
Eric Dumazetf4a775d2014-09-22 16:29:32 -07001122 * Note : Check if you can use __skb_header_release() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
1124static inline void skb_header_release(struct sk_buff *skb)
1125{
1126 BUG_ON(skb->nohdr);
1127 skb->nohdr = 1;
1128 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
1129}
1130
1131/**
Eric Dumazetf4a775d2014-09-22 16:29:32 -07001132 * __skb_header_release - release reference to header
1133 * @skb: buffer to operate on
1134 *
1135 * Variant of skb_header_release() assuming skb is private to caller.
1136 * We can avoid one atomic operation.
1137 */
1138static inline void __skb_header_release(struct sk_buff *skb)
1139{
1140 skb->nohdr = 1;
1141 atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT));
1142}
1143
1144
1145/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * skb_shared - is the buffer shared
1147 * @skb: buffer to check
1148 *
1149 * Returns true if more than one person has a reference to this
1150 * buffer.
1151 */
1152static inline int skb_shared(const struct sk_buff *skb)
1153{
1154 return atomic_read(&skb->users) != 1;
1155}
1156
1157/**
1158 * skb_share_check - check if buffer is shared and if so clone it
1159 * @skb: buffer to check
1160 * @pri: priority for memory allocation
1161 *
1162 * If the buffer is shared the buffer is cloned and the old copy
1163 * drops a reference. A new clone with a single reference is returned.
1164 * If the buffer is not shared the original buffer is returned. When
1165 * being called from interrupt status or with spinlocks held pri must
1166 * be GFP_ATOMIC.
1167 *
1168 * NULL is returned on a memory allocation failure.
1169 */
Eric Dumazet47061bc2012-08-03 20:54:15 +00001170static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 might_sleep_if(pri & __GFP_WAIT);
1173 if (skb_shared(skb)) {
1174 struct sk_buff *nskb = skb_clone(skb, pri);
Eric Dumazet47061bc2012-08-03 20:54:15 +00001175
1176 if (likely(nskb))
1177 consume_skb(skb);
1178 else
1179 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 skb = nskb;
1181 }
1182 return skb;
1183}
1184
1185/*
1186 * Copy shared buffers into a new sk_buff. We effectively do COW on
1187 * packets to handle cases where we have a local reader and forward
1188 * and a couple of other messy ones. The normal one is tcpdumping
1189 * a packet thats being forwarded.
1190 */
1191
1192/**
1193 * skb_unshare - make a copy of a shared buffer
1194 * @skb: buffer to check
1195 * @pri: priority for memory allocation
1196 *
1197 * If the socket buffer is a clone then this function creates a new
1198 * copy of the data, drops a reference count on the old copy and returns
1199 * the new copy with the reference count at 1. If the buffer is not a clone
1200 * the original buffer is returned. When called with a spinlock held or
1201 * from interrupt state @pri must be %GFP_ATOMIC
1202 *
1203 * %NULL is returned on a memory allocation failure.
1204 */
Victor Fuscoe2bf5212005-07-18 13:36:38 -07001205static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
Al Virodd0fc662005-10-07 07:46:04 +01001206 gfp_t pri)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 might_sleep_if(pri & __GFP_WAIT);
1209 if (skb_cloned(skb)) {
1210 struct sk_buff *nskb = skb_copy(skb, pri);
1211 kfree_skb(skb); /* Free our shared copy */
1212 skb = nskb;
1213 }
1214 return skb;
1215}
1216
1217/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -08001218 * skb_peek - peek at the head of an &sk_buff_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * @list_: list to peek at
1220 *
1221 * Peek an &sk_buff. Unlike most other operations you _MUST_
1222 * be careful with this one. A peek leaves the buffer on the
1223 * list and someone else may run off with it. You must hold
1224 * the appropriate locks or have a private queue to do this.
1225 *
1226 * Returns %NULL for an empty list or a pointer to the head element.
1227 * The reference count is not incremented and the reference is therefore
1228 * volatile. Use with caution.
1229 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04001230static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Eric Dumazet18d07002012-04-30 16:31:46 +00001232 struct sk_buff *skb = list_->next;
1233
1234 if (skb == (struct sk_buff *)list_)
1235 skb = NULL;
1236 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239/**
Pavel Emelyanovda5ef6e2012-02-21 07:31:18 +00001240 * skb_peek_next - peek skb following the given one from a queue
1241 * @skb: skb to start from
1242 * @list_: list to peek at
1243 *
1244 * Returns %NULL when the end of the list is met or a pointer to the
1245 * next element. The reference count is not incremented and the
1246 * reference is therefore volatile. Use with caution.
1247 */
1248static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
1249 const struct sk_buff_head *list_)
1250{
1251 struct sk_buff *next = skb->next;
Eric Dumazet18d07002012-04-30 16:31:46 +00001252
Pavel Emelyanovda5ef6e2012-02-21 07:31:18 +00001253 if (next == (struct sk_buff *)list_)
1254 next = NULL;
1255 return next;
1256}
1257
1258/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -08001259 * skb_peek_tail - peek at the tail of an &sk_buff_head
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * @list_: list to peek at
1261 *
1262 * Peek an &sk_buff. Unlike most other operations you _MUST_
1263 * be careful with this one. A peek leaves the buffer on the
1264 * list and someone else may run off with it. You must hold
1265 * the appropriate locks or have a private queue to do this.
1266 *
1267 * Returns %NULL for an empty list or a pointer to the tail element.
1268 * The reference count is not incremented and the reference is therefore
1269 * volatile. Use with caution.
1270 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04001271static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Eric Dumazet18d07002012-04-30 16:31:46 +00001273 struct sk_buff *skb = list_->prev;
1274
1275 if (skb == (struct sk_buff *)list_)
1276 skb = NULL;
1277 return skb;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/**
1282 * skb_queue_len - get queue length
1283 * @list_: list to measure
1284 *
1285 * Return the length of an &sk_buff queue.
1286 */
1287static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
1288{
1289 return list_->qlen;
1290}
1291
David S. Miller67fed452008-09-21 22:36:24 -07001292/**
1293 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
1294 * @list: queue to initialize
1295 *
1296 * This initializes only the list and queue length aspects of
1297 * an sk_buff_head object. This allows to initialize the list
1298 * aspects of an sk_buff_head without reinitializing things like
1299 * the spinlock. It can also be used for on-stack sk_buff_head
1300 * objects where the spinlock is known to not be used.
1301 */
1302static inline void __skb_queue_head_init(struct sk_buff_head *list)
1303{
1304 list->prev = list->next = (struct sk_buff *)list;
1305 list->qlen = 0;
1306}
1307
Arjan van de Ven76f10ad2006-08-02 14:06:55 -07001308/*
1309 * This function creates a split out lock class for each invocation;
1310 * this is needed for now since a whole lot of users of the skb-queue
1311 * infrastructure in drivers have different locking usage (in hardirq)
1312 * than the networking core (in softirq only). In the long run either the
1313 * network layer or drivers should need annotation to consolidate the
1314 * main types of usage into 3 classes.
1315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316static inline void skb_queue_head_init(struct sk_buff_head *list)
1317{
1318 spin_lock_init(&list->lock);
David S. Miller67fed452008-09-21 22:36:24 -07001319 __skb_queue_head_init(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Pavel Emelianovc2ecba72007-04-17 12:45:31 -07001322static inline void skb_queue_head_init_class(struct sk_buff_head *list,
1323 struct lock_class_key *class)
1324{
1325 skb_queue_head_init(list);
1326 lockdep_set_class(&list->lock, class);
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/*
Gerrit Renkerbf299272008-04-14 00:04:51 -07001330 * Insert an sk_buff on a list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
1332 * The "__skb_xxxx()" functions are the non-atomic ones that
1333 * can only be called with interrupts disabled.
1334 */
Joe Perches7965bd42013-09-26 14:48:15 -07001335void skb_insert(struct sk_buff *old, struct sk_buff *newsk,
1336 struct sk_buff_head *list);
Gerrit Renkerbf299272008-04-14 00:04:51 -07001337static inline void __skb_insert(struct sk_buff *newsk,
1338 struct sk_buff *prev, struct sk_buff *next,
1339 struct sk_buff_head *list)
1340{
1341 newsk->next = next;
1342 newsk->prev = prev;
1343 next->prev = prev->next = newsk;
1344 list->qlen++;
1345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
David S. Miller67fed452008-09-21 22:36:24 -07001347static inline void __skb_queue_splice(const struct sk_buff_head *list,
1348 struct sk_buff *prev,
1349 struct sk_buff *next)
1350{
1351 struct sk_buff *first = list->next;
1352 struct sk_buff *last = list->prev;
1353
1354 first->prev = prev;
1355 prev->next = first;
1356
1357 last->next = next;
1358 next->prev = last;
1359}
1360
1361/**
1362 * skb_queue_splice - join two skb lists, this is designed for stacks
1363 * @list: the new list to add
1364 * @head: the place to add it in the first list
1365 */
1366static inline void skb_queue_splice(const struct sk_buff_head *list,
1367 struct sk_buff_head *head)
1368{
1369 if (!skb_queue_empty(list)) {
1370 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001371 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001372 }
1373}
1374
1375/**
Eric Dumazetd961949662012-04-30 21:29:16 +00001376 * skb_queue_splice_init - join two skb lists and reinitialise the emptied list
David S. Miller67fed452008-09-21 22:36:24 -07001377 * @list: the new list to add
1378 * @head: the place to add it in the first list
1379 *
1380 * The list at @list is reinitialised
1381 */
1382static inline void skb_queue_splice_init(struct sk_buff_head *list,
1383 struct sk_buff_head *head)
1384{
1385 if (!skb_queue_empty(list)) {
1386 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001387 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001388 __skb_queue_head_init(list);
1389 }
1390}
1391
1392/**
1393 * skb_queue_splice_tail - join two skb lists, each list being a queue
1394 * @list: the new list to add
1395 * @head: the place to add it in the first list
1396 */
1397static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
1398 struct sk_buff_head *head)
1399{
1400 if (!skb_queue_empty(list)) {
1401 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001402 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001403 }
1404}
1405
1406/**
Eric Dumazetd961949662012-04-30 21:29:16 +00001407 * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
David S. Miller67fed452008-09-21 22:36:24 -07001408 * @list: the new list to add
1409 * @head: the place to add it in the first list
1410 *
1411 * Each of the lists is a queue.
1412 * The list at @list is reinitialised
1413 */
1414static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
1415 struct sk_buff_head *head)
1416{
1417 if (!skb_queue_empty(list)) {
1418 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
David S. Miller1d4a31d2008-09-22 21:57:21 -07001419 head->qlen += list->qlen;
David S. Miller67fed452008-09-21 22:36:24 -07001420 __skb_queue_head_init(list);
1421 }
1422}
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424/**
Stephen Hemminger300ce172005-10-30 13:47:34 -08001425 * __skb_queue_after - queue a buffer at the list head
1426 * @list: list to use
1427 * @prev: place after this buffer
1428 * @newsk: buffer to queue
1429 *
1430 * Queue a buffer int the middle of a list. This function takes no locks
1431 * and you must therefore hold required locks before calling it.
1432 *
1433 * A buffer cannot be placed on two lists at the same time.
1434 */
1435static inline void __skb_queue_after(struct sk_buff_head *list,
1436 struct sk_buff *prev,
1437 struct sk_buff *newsk)
1438{
Gerrit Renkerbf299272008-04-14 00:04:51 -07001439 __skb_insert(newsk, prev, prev->next, list);
Stephen Hemminger300ce172005-10-30 13:47:34 -08001440}
1441
Joe Perches7965bd42013-09-26 14:48:15 -07001442void skb_append(struct sk_buff *old, struct sk_buff *newsk,
1443 struct sk_buff_head *list);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001444
Gerrit Renkerf5572852008-04-14 00:05:28 -07001445static inline void __skb_queue_before(struct sk_buff_head *list,
1446 struct sk_buff *next,
1447 struct sk_buff *newsk)
1448{
1449 __skb_insert(newsk, next->prev, next, list);
1450}
1451
Stephen Hemminger300ce172005-10-30 13:47:34 -08001452/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 * __skb_queue_head - queue a buffer at the list head
1454 * @list: list to use
1455 * @newsk: buffer to queue
1456 *
1457 * Queue a buffer at the start of a list. This function takes no locks
1458 * and you must therefore hold required locks before calling it.
1459 *
1460 * A buffer cannot be placed on two lists at the same time.
1461 */
Joe Perches7965bd42013-09-26 14:48:15 -07001462void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463static inline void __skb_queue_head(struct sk_buff_head *list,
1464 struct sk_buff *newsk)
1465{
Stephen Hemminger300ce172005-10-30 13:47:34 -08001466 __skb_queue_after(list, (struct sk_buff *)list, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469/**
1470 * __skb_queue_tail - queue a buffer at the list tail
1471 * @list: list to use
1472 * @newsk: buffer to queue
1473 *
1474 * Queue a buffer at the end of a list. This function takes no locks
1475 * and you must therefore hold required locks before calling it.
1476 *
1477 * A buffer cannot be placed on two lists at the same time.
1478 */
Joe Perches7965bd42013-09-26 14:48:15 -07001479void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480static inline void __skb_queue_tail(struct sk_buff_head *list,
1481 struct sk_buff *newsk)
1482{
Gerrit Renkerf5572852008-04-14 00:05:28 -07001483 __skb_queue_before(list, (struct sk_buff *)list, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * remove sk_buff from list. _Must_ be called atomically, and with
1488 * the list known..
1489 */
Joe Perches7965bd42013-09-26 14:48:15 -07001490void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1492{
1493 struct sk_buff *next, *prev;
1494
1495 list->qlen--;
1496 next = skb->next;
1497 prev = skb->prev;
1498 skb->next = skb->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 next->prev = prev;
1500 prev->next = next;
1501}
1502
Gerrit Renkerf525c062008-04-14 00:04:12 -07001503/**
1504 * __skb_dequeue - remove from the head of the queue
1505 * @list: list to dequeue from
1506 *
1507 * Remove the head of the list. This function does not take any locks
1508 * so must be used with appropriate locks held only. The head item is
1509 * returned or %NULL if the list is empty.
1510 */
Joe Perches7965bd42013-09-26 14:48:15 -07001511struct sk_buff *skb_dequeue(struct sk_buff_head *list);
Gerrit Renkerf525c062008-04-14 00:04:12 -07001512static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
1513{
1514 struct sk_buff *skb = skb_peek(list);
1515 if (skb)
1516 __skb_unlink(skb, list);
1517 return skb;
1518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520/**
1521 * __skb_dequeue_tail - remove from the tail of the queue
1522 * @list: list to dequeue from
1523 *
1524 * Remove the tail of the list. This function does not take any locks
1525 * so must be used with appropriate locks held only. The tail item is
1526 * returned or %NULL if the list is empty.
1527 */
Joe Perches7965bd42013-09-26 14:48:15 -07001528struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
1530{
1531 struct sk_buff *skb = skb_peek_tail(list);
1532 if (skb)
1533 __skb_unlink(skb, list);
1534 return skb;
1535}
1536
1537
David S. Millerbdcc0922012-03-07 20:53:36 -05001538static inline bool skb_is_nonlinear(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 return skb->data_len;
1541}
1542
1543static inline unsigned int skb_headlen(const struct sk_buff *skb)
1544{
1545 return skb->len - skb->data_len;
1546}
1547
1548static inline int skb_pagelen(const struct sk_buff *skb)
1549{
1550 int i, len = 0;
1551
1552 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
Eric Dumazet9e903e02011-10-18 21:00:24 +00001553 len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return len + skb_headlen(skb);
1555}
1556
Ian Campbell131ea662011-08-19 06:25:00 +00001557/**
1558 * __skb_fill_page_desc - initialise a paged fragment in an skb
1559 * @skb: buffer containing fragment to be initialised
1560 * @i: paged fragment index to initialise
1561 * @page: the page to use for this fragment
1562 * @off: the offset to the data with @page
1563 * @size: the length of the data
1564 *
1565 * Initialises the @i'th fragment of @skb to point to &size bytes at
1566 * offset @off within @page.
1567 *
1568 * Does not take any additional reference on the fragment.
1569 */
1570static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
1571 struct page *page, int off, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1574
Mel Gormanc48a11c2012-07-31 16:44:23 -07001575 /*
1576 * Propagate page->pfmemalloc to the skb if we can. The problem is
1577 * that not all callers have unique ownership of the page. If
1578 * pfmemalloc is set, we check the mapping as a mapping implies
1579 * page->index is set (index and pfmemalloc share space).
1580 * If it's a valid mapping, we cannot use page->pfmemalloc but we
1581 * do not lose pfmemalloc information as the pages would not be
1582 * allocated using __GFP_MEMALLOC.
1583 */
Ian Campbella8605c62011-10-19 23:01:49 +00001584 frag->page.p = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 frag->page_offset = off;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001586 skb_frag_size_set(frag, size);
Pavel Emelyanovcca7af32013-03-14 03:29:40 +00001587
1588 page = compound_head(page);
1589 if (page->pfmemalloc && !page->mapping)
1590 skb->pfmemalloc = true;
Ian Campbell131ea662011-08-19 06:25:00 +00001591}
1592
1593/**
1594 * skb_fill_page_desc - initialise a paged fragment in an skb
1595 * @skb: buffer containing fragment to be initialised
1596 * @i: paged fragment index to initialise
1597 * @page: the page to use for this fragment
1598 * @off: the offset to the data with @page
1599 * @size: the length of the data
1600 *
1601 * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
Mathias Krausebc323832013-11-07 14:18:26 +01001602 * @skb to point to @size bytes at offset @off within @page. In
Ian Campbell131ea662011-08-19 06:25:00 +00001603 * addition updates @skb such that @i is the last fragment.
1604 *
1605 * Does not take any additional reference on the fragment.
1606 */
1607static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
1608 struct page *page, int off, int size)
1609{
1610 __skb_fill_page_desc(skb, i, page, off, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 skb_shinfo(skb)->nr_frags = i + 1;
1612}
1613
Joe Perches7965bd42013-09-26 14:48:15 -07001614void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
1615 int size, unsigned int truesize);
Peter Zijlstra654bed12008-10-07 14:22:33 -07001616
Jason Wangf8e617e2013-11-01 14:07:47 +08001617void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
1618 unsigned int truesize);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
David S. Miller21dc3302010-08-23 00:13:46 -07001621#define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622#define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1623
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001624#ifdef NET_SKBUFF_DATA_USES_OFFSET
1625static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1626{
1627 return skb->head + skb->tail;
1628}
1629
1630static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1631{
1632 skb->tail = skb->data - skb->head;
1633}
1634
1635static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1636{
1637 skb_reset_tail_pointer(skb);
1638 skb->tail += offset;
1639}
Simon Horman7cc46192013-05-28 20:34:29 +00001640
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001641#else /* NET_SKBUFF_DATA_USES_OFFSET */
1642static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1643{
1644 return skb->tail;
1645}
1646
1647static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1648{
1649 skb->tail = skb->data;
1650}
1651
1652static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1653{
1654 skb->tail = skb->data + offset;
1655}
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001656
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001657#endif /* NET_SKBUFF_DATA_USES_OFFSET */
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/*
1660 * Add data to an sk_buff
1661 */
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001662unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len);
Joe Perches7965bd42013-09-26 14:48:15 -07001663unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
1665{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001666 unsigned char *tmp = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 SKB_LINEAR_ASSERT(skb);
1668 skb->tail += len;
1669 skb->len += len;
1670 return tmp;
1671}
1672
Joe Perches7965bd42013-09-26 14:48:15 -07001673unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
1675{
1676 skb->data -= len;
1677 skb->len += len;
1678 return skb->data;
1679}
1680
Joe Perches7965bd42013-09-26 14:48:15 -07001681unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
1683{
1684 skb->len -= len;
1685 BUG_ON(skb->len < skb->data_len);
1686 return skb->data += len;
1687}
1688
David S. Miller47d29642010-05-02 02:21:44 -07001689static inline unsigned char *skb_pull_inline(struct sk_buff *skb, unsigned int len)
1690{
1691 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1692}
1693
Joe Perches7965bd42013-09-26 14:48:15 -07001694unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
1697{
1698 if (len > skb_headlen(skb) &&
Gerrit Renker987c4022008-08-11 18:17:17 -07001699 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return NULL;
1701 skb->len -= len;
1702 return skb->data += len;
1703}
1704
1705static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
1706{
1707 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
1708}
1709
1710static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
1711{
1712 if (likely(len <= skb_headlen(skb)))
1713 return 1;
1714 if (unlikely(len > skb->len))
1715 return 0;
Gerrit Renker987c4022008-08-11 18:17:17 -07001716 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719/**
1720 * skb_headroom - bytes at buffer head
1721 * @skb: buffer to check
1722 *
1723 * Return the number of bytes of free space at the head of an &sk_buff.
1724 */
Chuck Leverc2636b42007-10-23 21:07:32 -07001725static inline unsigned int skb_headroom(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 return skb->data - skb->head;
1728}
1729
1730/**
1731 * skb_tailroom - bytes at buffer end
1732 * @skb: buffer to check
1733 *
1734 * Return the number of bytes of free space at the tail of an sk_buff
1735 */
1736static inline int skb_tailroom(const struct sk_buff *skb)
1737{
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001738 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741/**
Eric Dumazeta21d4572012-04-10 20:30:48 +00001742 * skb_availroom - bytes at buffer end
1743 * @skb: buffer to check
1744 *
1745 * Return the number of bytes of free space at the tail of an sk_buff
1746 * allocated by sk_stream_alloc()
1747 */
1748static inline int skb_availroom(const struct sk_buff *skb)
1749{
Eric Dumazet16fad692013-03-14 05:40:32 +00001750 if (skb_is_nonlinear(skb))
1751 return 0;
1752
1753 return skb->end - skb->tail - skb->reserved_tailroom;
Eric Dumazeta21d4572012-04-10 20:30:48 +00001754}
1755
1756/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * skb_reserve - adjust headroom
1758 * @skb: buffer to alter
1759 * @len: bytes to move
1760 *
1761 * Increase the headroom of an empty &sk_buff by reducing the tail
1762 * room. This is only allowed for an empty buffer.
1763 */
David S. Miller8243126c2006-01-17 02:54:21 -08001764static inline void skb_reserve(struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 skb->data += len;
1767 skb->tail += len;
1768}
1769
Tom Herbert8bce6d72014-09-29 20:22:29 -07001770#define ENCAP_TYPE_ETHER 0
1771#define ENCAP_TYPE_IPPROTO 1
1772
1773static inline void skb_set_inner_protocol(struct sk_buff *skb,
1774 __be16 protocol)
1775{
1776 skb->inner_protocol = protocol;
1777 skb->inner_protocol_type = ENCAP_TYPE_ETHER;
1778}
1779
1780static inline void skb_set_inner_ipproto(struct sk_buff *skb,
1781 __u8 ipproto)
1782{
1783 skb->inner_ipproto = ipproto;
1784 skb->inner_protocol_type = ENCAP_TYPE_IPPROTO;
1785}
1786
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001787static inline void skb_reset_inner_headers(struct sk_buff *skb)
1788{
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001789 skb->inner_mac_header = skb->mac_header;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001790 skb->inner_network_header = skb->network_header;
1791 skb->inner_transport_header = skb->transport_header;
1792}
1793
Jiri Pirko0b5c9db2011-06-10 06:56:58 +00001794static inline void skb_reset_mac_len(struct sk_buff *skb)
1795{
1796 skb->mac_len = skb->network_header - skb->mac_header;
1797}
1798
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001799static inline unsigned char *skb_inner_transport_header(const struct sk_buff
1800 *skb)
1801{
1802 return skb->head + skb->inner_transport_header;
1803}
1804
1805static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
1806{
1807 skb->inner_transport_header = skb->data - skb->head;
1808}
1809
1810static inline void skb_set_inner_transport_header(struct sk_buff *skb,
1811 const int offset)
1812{
1813 skb_reset_inner_transport_header(skb);
1814 skb->inner_transport_header += offset;
1815}
1816
1817static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
1818{
1819 return skb->head + skb->inner_network_header;
1820}
1821
1822static inline void skb_reset_inner_network_header(struct sk_buff *skb)
1823{
1824 skb->inner_network_header = skb->data - skb->head;
1825}
1826
1827static inline void skb_set_inner_network_header(struct sk_buff *skb,
1828 const int offset)
1829{
1830 skb_reset_inner_network_header(skb);
1831 skb->inner_network_header += offset;
1832}
1833
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001834static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb)
1835{
1836 return skb->head + skb->inner_mac_header;
1837}
1838
1839static inline void skb_reset_inner_mac_header(struct sk_buff *skb)
1840{
1841 skb->inner_mac_header = skb->data - skb->head;
1842}
1843
1844static inline void skb_set_inner_mac_header(struct sk_buff *skb,
1845 const int offset)
1846{
1847 skb_reset_inner_mac_header(skb);
1848 skb->inner_mac_header += offset;
1849}
Eric Dumazetfda55ec2013-01-07 09:28:21 +00001850static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
1851{
Cong Wang35d04612013-05-29 15:16:05 +08001852 return skb->transport_header != (typeof(skb->transport_header))~0U;
Eric Dumazetfda55ec2013-01-07 09:28:21 +00001853}
1854
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001855static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1856{
1857 return skb->head + skb->transport_header;
1858}
1859
1860static inline void skb_reset_transport_header(struct sk_buff *skb)
1861{
1862 skb->transport_header = skb->data - skb->head;
1863}
1864
1865static inline void skb_set_transport_header(struct sk_buff *skb,
1866 const int offset)
1867{
1868 skb_reset_transport_header(skb);
1869 skb->transport_header += offset;
1870}
1871
1872static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1873{
1874 return skb->head + skb->network_header;
1875}
1876
1877static inline void skb_reset_network_header(struct sk_buff *skb)
1878{
1879 skb->network_header = skb->data - skb->head;
1880}
1881
1882static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1883{
1884 skb_reset_network_header(skb);
1885 skb->network_header += offset;
1886}
1887
1888static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1889{
1890 return skb->head + skb->mac_header;
1891}
1892
1893static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1894{
Cong Wang35d04612013-05-29 15:16:05 +08001895 return skb->mac_header != (typeof(skb->mac_header))~0U;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001896}
1897
1898static inline void skb_reset_mac_header(struct sk_buff *skb)
1899{
1900 skb->mac_header = skb->data - skb->head;
1901}
1902
1903static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1904{
1905 skb_reset_mac_header(skb);
1906 skb->mac_header += offset;
1907}
1908
Timo Teräs0e3da5b2013-12-16 11:02:09 +02001909static inline void skb_pop_mac_header(struct sk_buff *skb)
1910{
1911 skb->mac_header = skb->network_header;
1912}
1913
Ying Xuefbbdb8f2013-03-27 16:46:06 +00001914static inline void skb_probe_transport_header(struct sk_buff *skb,
1915 const int offset_hint)
1916{
1917 struct flow_keys keys;
1918
1919 if (skb_transport_header_was_set(skb))
1920 return;
1921 else if (skb_flow_dissect(skb, &keys))
1922 skb_set_transport_header(skb, keys.thoff);
1923 else
1924 skb_set_transport_header(skb, offset_hint);
1925}
1926
Eric Dumazet03606892012-02-23 10:55:02 +00001927static inline void skb_mac_header_rebuild(struct sk_buff *skb)
1928{
1929 if (skb_mac_header_was_set(skb)) {
1930 const unsigned char *old_mac = skb_mac_header(skb);
1931
1932 skb_set_mac_header(skb, -skb->mac_len);
1933 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
1934 }
1935}
1936
Michał Mirosław04fb4512010-12-14 15:24:08 +00001937static inline int skb_checksum_start_offset(const struct sk_buff *skb)
1938{
1939 return skb->csum_start - skb_headroom(skb);
1940}
1941
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001942static inline int skb_transport_offset(const struct sk_buff *skb)
1943{
1944 return skb_transport_header(skb) - skb->data;
1945}
1946
1947static inline u32 skb_network_header_len(const struct sk_buff *skb)
1948{
1949 return skb->transport_header - skb->network_header;
1950}
1951
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001952static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
1953{
1954 return skb->inner_transport_header - skb->inner_network_header;
1955}
1956
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -07001957static inline int skb_network_offset(const struct sk_buff *skb)
1958{
1959 return skb_network_header(skb) - skb->data;
1960}
Arnaldo Carvalho de Melo48d49d0c2007-03-10 12:30:58 -03001961
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001962static inline int skb_inner_network_offset(const struct sk_buff *skb)
1963{
1964 return skb_inner_network_header(skb) - skb->data;
1965}
1966
Changli Gaof9599ce2010-08-04 04:43:44 +00001967static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
1968{
1969 return pskb_may_pull(skb, skb_network_offset(skb) + len);
1970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972/*
1973 * CPUs often take a performance hit when accessing unaligned memory
1974 * locations. The actual performance hit varies, it can be small if the
1975 * hardware handles it or large if we have to take an exception and fix it
1976 * in software.
1977 *
1978 * Since an ethernet header is 14 bytes network drivers often end up with
1979 * the IP header at an unaligned offset. The IP header can be aligned by
1980 * shifting the start of the packet by 2 bytes. Drivers should do this
1981 * with:
1982 *
Tobias Klauser8660c122009-07-13 22:48:16 +00001983 * skb_reserve(skb, NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 *
1985 * The downside to this alignment of the IP header is that the DMA is now
1986 * unaligned. On some architectures the cost of an unaligned DMA is high
1987 * and this cost outweighs the gains made by aligning the IP header.
Tobias Klauser8660c122009-07-13 22:48:16 +00001988 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
1990 * to be overridden.
1991 */
1992#ifndef NET_IP_ALIGN
1993#define NET_IP_ALIGN 2
1994#endif
1995
Anton Blanchard025be812006-03-31 02:27:06 -08001996/*
1997 * The networking layer reserves some headroom in skb data (via
1998 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
1999 * the header has to grow. In the default case, if the header has to grow
David S. Millerd6301d32009-02-08 19:24:13 -08002000 * 32 bytes or less we avoid the reallocation.
Anton Blanchard025be812006-03-31 02:27:06 -08002001 *
2002 * Unfortunately this headroom changes the DMA alignment of the resulting
2003 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
2004 * on some architectures. An architecture can override this value,
2005 * perhaps setting it to a cacheline in size (since that will maintain
2006 * cacheline alignment of the DMA). It must be a power of 2.
2007 *
David S. Millerd6301d32009-02-08 19:24:13 -08002008 * Various parts of the networking layer expect at least 32 bytes of
Anton Blanchard025be812006-03-31 02:27:06 -08002009 * headroom, you should not reduce this.
Eric Dumazet5933dd22010-06-15 18:16:43 -07002010 *
2011 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
2012 * to reduce average number of cache lines per packet.
2013 * get_rps_cpus() for example only access one 64 bytes aligned block :
Eric Dumazet18e8c132010-05-06 21:58:51 -07002014 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
Anton Blanchard025be812006-03-31 02:27:06 -08002015 */
2016#ifndef NET_SKB_PAD
Eric Dumazet5933dd22010-06-15 18:16:43 -07002017#define NET_SKB_PAD max(32, L1_CACHE_BYTES)
Anton Blanchard025be812006-03-31 02:27:06 -08002018#endif
2019
Joe Perches7965bd42013-09-26 14:48:15 -07002020int ___pskb_trim(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
2023{
Emmanuel Grumbachc4264f22011-05-21 19:46:09 +00002024 if (unlikely(skb_is_nonlinear(skb))) {
Herbert Xu3cc0e872006-06-09 16:13:38 -07002025 WARN_ON(1);
2026 return;
2027 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002028 skb->len = len;
2029 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Joe Perches7965bd42013-09-26 14:48:15 -07002032void skb_trim(struct sk_buff *skb, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
2035{
Herbert Xu3cc0e872006-06-09 16:13:38 -07002036 if (skb->data_len)
2037 return ___pskb_trim(skb, len);
2038 __skb_trim(skb, len);
2039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
2043{
2044 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
2045}
2046
2047/**
Herbert Xue9fa4f72006-08-13 20:12:58 -07002048 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
2049 * @skb: buffer to alter
2050 * @len: new length
2051 *
2052 * This is identical to pskb_trim except that the caller knows that
2053 * the skb is not cloned so we should never get an error due to out-
2054 * of-memory.
2055 */
2056static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
2057{
2058 int err = pskb_trim(skb, len);
2059 BUG_ON(err);
2060}
2061
2062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * skb_orphan - orphan a buffer
2064 * @skb: buffer to orphan
2065 *
2066 * If a buffer currently has an owner then we call the owner's
2067 * destructor function and make the @skb unowned. The buffer continues
2068 * to exist but is no longer charged to its former owner.
2069 */
2070static inline void skb_orphan(struct sk_buff *skb)
2071{
Eric Dumazetc34a7612013-07-30 16:11:15 -07002072 if (skb->destructor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 skb->destructor(skb);
Eric Dumazetc34a7612013-07-30 16:11:15 -07002074 skb->destructor = NULL;
2075 skb->sk = NULL;
Eric Dumazet376c7312013-08-01 11:43:08 -07002076 } else {
2077 BUG_ON(skb->sk);
Eric Dumazetc34a7612013-07-30 16:11:15 -07002078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
2081/**
Michael S. Tsirkina353e0c2012-07-20 09:23:07 +00002082 * skb_orphan_frags - orphan the frags contained in a buffer
2083 * @skb: buffer to orphan frags from
2084 * @gfp_mask: allocation mask for replacement pages
2085 *
2086 * For each frag in the SKB which needs a destructor (i.e. has an
2087 * owner) create a copy of that frag and release the original
2088 * page by calling the destructor.
2089 */
2090static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
2091{
2092 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY)))
2093 return 0;
2094 return skb_copy_ubufs(skb, gfp_mask);
2095}
2096
2097/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * __skb_queue_purge - empty a list
2099 * @list: list to empty
2100 *
2101 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2102 * the list and one reference dropped. This function does not take the
2103 * list lock and the caller must hold the relevant locks to use it.
2104 */
Joe Perches7965bd42013-09-26 14:48:15 -07002105void skb_queue_purge(struct sk_buff_head *list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106static inline void __skb_queue_purge(struct sk_buff_head *list)
2107{
2108 struct sk_buff *skb;
2109 while ((skb = __skb_dequeue(list)) != NULL)
2110 kfree_skb(skb);
2111}
2112
Alexander Duycke5e67302013-02-08 10:17:15 +00002113#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
2114#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
2115#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
2116
Joe Perches7965bd42013-09-26 14:48:15 -07002117void *netdev_alloc_frag(unsigned int fragsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Joe Perches7965bd42013-09-26 14:48:15 -07002119struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
2120 gfp_t gfp_mask);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002121
2122/**
2123 * netdev_alloc_skb - allocate an skbuff for rx on a specific device
2124 * @dev: network device to receive on
2125 * @length: length to allocate
2126 *
2127 * Allocate a new &sk_buff and assign it a usage count of one. The
2128 * buffer has unspecified headroom built in. Users should allocate
2129 * the headroom they think they need without accounting for the
2130 * built in space. The built in space is used for optimisations.
2131 *
2132 * %NULL is returned if there is no free memory. Although this function
2133 * allocates memory it can be called from an interrupt.
2134 */
2135static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +00002136 unsigned int length)
Christoph Hellwig8af27452006-07-31 22:35:23 -07002137{
2138 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
2139}
2140
Eric Dumazet6f532612012-05-18 05:12:12 +00002141/* legacy helper around __netdev_alloc_skb() */
2142static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
2143 gfp_t gfp_mask)
2144{
2145 return __netdev_alloc_skb(NULL, length, gfp_mask);
2146}
2147
2148/* legacy helper around netdev_alloc_skb() */
2149static inline struct sk_buff *dev_alloc_skb(unsigned int length)
2150{
2151 return netdev_alloc_skb(NULL, length);
2152}
2153
2154
Eric Dumazet4915a0d2011-07-11 20:08:34 -07002155static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
2156 unsigned int length, gfp_t gfp)
Eric Dumazet61321bb2009-10-07 17:11:23 +00002157{
Eric Dumazet4915a0d2011-07-11 20:08:34 -07002158 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
Eric Dumazet61321bb2009-10-07 17:11:23 +00002159
2160 if (NET_IP_ALIGN && skb)
2161 skb_reserve(skb, NET_IP_ALIGN);
2162 return skb;
2163}
2164
Eric Dumazet4915a0d2011-07-11 20:08:34 -07002165static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
2166 unsigned int length)
2167{
2168 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
2169}
2170
Florian Fainellibc6fc9f2013-08-30 15:36:14 +01002171/**
2172 * __skb_alloc_pages - allocate pages for ps-rx on a skb and preserve pfmemalloc data
Mel Gorman06140022012-07-31 16:44:24 -07002173 * @gfp_mask: alloc_pages_node mask. Set __GFP_NOMEMALLOC if not for network packet RX
2174 * @skb: skb to set pfmemalloc on if __GFP_MEMALLOC is used
2175 * @order: size of the allocation
2176 *
2177 * Allocate a new page.
2178 *
2179 * %NULL is returned if there is no free memory.
2180*/
2181static inline struct page *__skb_alloc_pages(gfp_t gfp_mask,
2182 struct sk_buff *skb,
2183 unsigned int order)
2184{
2185 struct page *page;
2186
2187 gfp_mask |= __GFP_COLD;
2188
2189 if (!(gfp_mask & __GFP_NOMEMALLOC))
2190 gfp_mask |= __GFP_MEMALLOC;
2191
2192 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
2193 if (skb && page && page->pfmemalloc)
2194 skb->pfmemalloc = true;
2195
2196 return page;
2197}
2198
2199/**
2200 * __skb_alloc_page - allocate a page for ps-rx for a given skb and preserve pfmemalloc data
2201 * @gfp_mask: alloc_pages_node mask. Set __GFP_NOMEMALLOC if not for network packet RX
2202 * @skb: skb to set pfmemalloc on if __GFP_MEMALLOC is used
2203 *
2204 * Allocate a new page.
2205 *
2206 * %NULL is returned if there is no free memory.
2207 */
2208static inline struct page *__skb_alloc_page(gfp_t gfp_mask,
2209 struct sk_buff *skb)
2210{
2211 return __skb_alloc_pages(gfp_mask, skb, 0);
2212}
2213
2214/**
2215 * skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page
2216 * @page: The page that was allocated from skb_alloc_page
2217 * @skb: The skb that may need pfmemalloc set
2218 */
2219static inline void skb_propagate_pfmemalloc(struct page *page,
2220 struct sk_buff *skb)
2221{
2222 if (page && page->pfmemalloc)
2223 skb->pfmemalloc = true;
2224}
2225
Eric Dumazet564824b2010-10-11 19:05:25 +00002226/**
Masanari Iidae2278672014-02-18 22:54:36 +09002227 * skb_frag_page - retrieve the page referred to by a paged fragment
Ian Campbell131ea662011-08-19 06:25:00 +00002228 * @frag: the paged fragment
2229 *
2230 * Returns the &struct page associated with @frag.
2231 */
2232static inline struct page *skb_frag_page(const skb_frag_t *frag)
2233{
Ian Campbella8605c62011-10-19 23:01:49 +00002234 return frag->page.p;
Ian Campbell131ea662011-08-19 06:25:00 +00002235}
2236
2237/**
2238 * __skb_frag_ref - take an addition reference on a paged fragment.
2239 * @frag: the paged fragment
2240 *
2241 * Takes an additional reference on the paged fragment @frag.
2242 */
2243static inline void __skb_frag_ref(skb_frag_t *frag)
2244{
2245 get_page(skb_frag_page(frag));
2246}
2247
2248/**
2249 * skb_frag_ref - take an addition reference on a paged fragment of an skb.
2250 * @skb: the buffer
2251 * @f: the fragment offset.
2252 *
2253 * Takes an additional reference on the @f'th paged fragment of @skb.
2254 */
2255static inline void skb_frag_ref(struct sk_buff *skb, int f)
2256{
2257 __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
2258}
2259
2260/**
2261 * __skb_frag_unref - release a reference on a paged fragment.
2262 * @frag: the paged fragment
2263 *
2264 * Releases a reference on the paged fragment @frag.
2265 */
2266static inline void __skb_frag_unref(skb_frag_t *frag)
2267{
2268 put_page(skb_frag_page(frag));
2269}
2270
2271/**
2272 * skb_frag_unref - release a reference on a paged fragment of an skb.
2273 * @skb: the buffer
2274 * @f: the fragment offset
2275 *
2276 * Releases a reference on the @f'th paged fragment of @skb.
2277 */
2278static inline void skb_frag_unref(struct sk_buff *skb, int f)
2279{
2280 __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
2281}
2282
2283/**
2284 * skb_frag_address - gets the address of the data contained in a paged fragment
2285 * @frag: the paged fragment buffer
2286 *
2287 * Returns the address of the data within @frag. The page must already
2288 * be mapped.
2289 */
2290static inline void *skb_frag_address(const skb_frag_t *frag)
2291{
2292 return page_address(skb_frag_page(frag)) + frag->page_offset;
2293}
2294
2295/**
2296 * skb_frag_address_safe - gets the address of the data contained in a paged fragment
2297 * @frag: the paged fragment buffer
2298 *
2299 * Returns the address of the data within @frag. Checks that the page
2300 * is mapped and returns %NULL otherwise.
2301 */
2302static inline void *skb_frag_address_safe(const skb_frag_t *frag)
2303{
2304 void *ptr = page_address(skb_frag_page(frag));
2305 if (unlikely(!ptr))
2306 return NULL;
2307
2308 return ptr + frag->page_offset;
2309}
2310
2311/**
2312 * __skb_frag_set_page - sets the page contained in a paged fragment
2313 * @frag: the paged fragment
2314 * @page: the page to set
2315 *
2316 * Sets the fragment @frag to contain @page.
2317 */
2318static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
2319{
Ian Campbella8605c62011-10-19 23:01:49 +00002320 frag->page.p = page;
Ian Campbell131ea662011-08-19 06:25:00 +00002321}
2322
2323/**
2324 * skb_frag_set_page - sets the page contained in a paged fragment of an skb
2325 * @skb: the buffer
2326 * @f: the fragment offset
2327 * @page: the page to set
2328 *
2329 * Sets the @f'th fragment of @skb to contain @page.
2330 */
2331static inline void skb_frag_set_page(struct sk_buff *skb, int f,
2332 struct page *page)
2333{
2334 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
2335}
2336
Eric Dumazet400dfd32013-10-17 16:27:07 -07002337bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
2338
Ian Campbell131ea662011-08-19 06:25:00 +00002339/**
2340 * skb_frag_dma_map - maps a paged fragment via the DMA API
Marcos Paulo de Souzaf83347d2011-10-31 15:11:45 +00002341 * @dev: the device to map the fragment to
Ian Campbell131ea662011-08-19 06:25:00 +00002342 * @frag: the paged fragment to map
2343 * @offset: the offset within the fragment (starting at the
2344 * fragment's own offset)
2345 * @size: the number of bytes to map
Marcos Paulo de Souzaf83347d2011-10-31 15:11:45 +00002346 * @dir: the direction of the mapping (%PCI_DMA_*)
Ian Campbell131ea662011-08-19 06:25:00 +00002347 *
2348 * Maps the page associated with @frag to @device.
2349 */
2350static inline dma_addr_t skb_frag_dma_map(struct device *dev,
2351 const skb_frag_t *frag,
2352 size_t offset, size_t size,
2353 enum dma_data_direction dir)
2354{
2355 return dma_map_page(dev, skb_frag_page(frag),
2356 frag->page_offset + offset, size, dir);
2357}
2358
Eric Dumazet117632e2011-12-03 21:39:53 +00002359static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
2360 gfp_t gfp_mask)
2361{
2362 return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
2363}
2364
Octavian Purdilabad93e92014-06-12 01:36:26 +03002365
2366static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb,
2367 gfp_t gfp_mask)
2368{
2369 return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true);
2370}
2371
2372
Ian Campbell131ea662011-08-19 06:25:00 +00002373/**
Patrick McHardy334a8132007-06-25 04:35:20 -07002374 * skb_clone_writable - is the header of a clone writable
2375 * @skb: buffer to check
2376 * @len: length up to which to write
2377 *
2378 * Returns true if modifying the header part of the cloned buffer
2379 * does not requires the data to be copied.
2380 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04002381static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
Patrick McHardy334a8132007-06-25 04:35:20 -07002382{
2383 return !skb_header_cloned(skb) &&
2384 skb_headroom(skb) + len <= skb->hdr_len;
2385}
2386
Herbert Xud9cc2042007-09-16 16:21:16 -07002387static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
2388 int cloned)
2389{
2390 int delta = 0;
2391
Herbert Xud9cc2042007-09-16 16:21:16 -07002392 if (headroom > skb_headroom(skb))
2393 delta = headroom - skb_headroom(skb);
2394
2395 if (delta || cloned)
2396 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
2397 GFP_ATOMIC);
2398 return 0;
2399}
2400
Patrick McHardy334a8132007-06-25 04:35:20 -07002401/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 * skb_cow - copy header of skb when it is required
2403 * @skb: buffer to cow
2404 * @headroom: needed headroom
2405 *
2406 * If the skb passed lacks sufficient headroom or its data part
2407 * is shared, data is reallocated. If reallocation fails, an error
2408 * is returned and original skb is not changed.
2409 *
2410 * The result is skb with writable area skb->head...skb->tail
2411 * and at least @headroom of space at head.
2412 */
2413static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
2414{
Herbert Xud9cc2042007-09-16 16:21:16 -07002415 return __skb_cow(skb, headroom, skb_cloned(skb));
2416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Herbert Xud9cc2042007-09-16 16:21:16 -07002418/**
2419 * skb_cow_head - skb_cow but only making the head writable
2420 * @skb: buffer to cow
2421 * @headroom: needed headroom
2422 *
2423 * This function is identical to skb_cow except that we replace the
2424 * skb_cloned check by skb_header_cloned. It should be used when
2425 * you only need to push on some header and do not need to modify
2426 * the data.
2427 */
2428static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
2429{
2430 return __skb_cow(skb, headroom, skb_header_cloned(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433/**
2434 * skb_padto - pad an skbuff up to a minimal size
2435 * @skb: buffer to pad
2436 * @len: minimal length
2437 *
2438 * Pads up a buffer to ensure the trailing bytes exist and are
2439 * blanked. If the buffer already contains sufficient data it
Herbert Xu5b057c62006-06-23 02:06:41 -07002440 * is untouched. Otherwise it is extended. Returns zero on
2441 * success. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
2443
Herbert Xu5b057c62006-06-23 02:06:41 -07002444static inline int skb_padto(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 unsigned int size = skb->len;
2447 if (likely(size >= len))
Herbert Xu5b057c62006-06-23 02:06:41 -07002448 return 0;
Gerrit Renker987c4022008-08-11 18:17:17 -07002449 return skb_pad(skb, len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452static inline int skb_add_data(struct sk_buff *skb,
2453 char __user *from, int copy)
2454{
2455 const int off = skb->len;
2456
2457 if (skb->ip_summed == CHECKSUM_NONE) {
2458 int err = 0;
Al Viro50842052006-11-14 21:36:34 -08002459 __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 copy, 0, &err);
2461 if (!err) {
2462 skb->csum = csum_block_add(skb->csum, csum, off);
2463 return 0;
2464 }
2465 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
2466 return 0;
2467
2468 __skb_trim(skb, off);
2469 return -EFAULT;
2470}
2471
Eric Dumazet38ba0a62012-04-23 17:48:27 +00002472static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
2473 const struct page *page, int off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 if (i) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002476 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Ian Campbellea2ab692011-08-22 23:44:58 +00002478 return page == skb_frag_page(frag) &&
Eric Dumazet9e903e02011-10-18 21:00:24 +00002479 off == frag->page_offset + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Eric Dumazet38ba0a62012-04-23 17:48:27 +00002481 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Herbert Xu364c6ba2006-06-09 16:10:40 -07002484static inline int __skb_linearize(struct sk_buff *skb)
2485{
2486 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
2487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489/**
2490 * skb_linearize - convert paged skb to linear one
2491 * @skb: buffer to linarize
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 *
2493 * If there is no free memory -ENOMEM is returned, otherwise zero
2494 * is returned and the old skb data released.
2495 */
Herbert Xu364c6ba2006-06-09 16:10:40 -07002496static inline int skb_linearize(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Herbert Xu364c6ba2006-06-09 16:10:40 -07002498 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
2499}
2500
2501/**
Eric Dumazetcef401d2013-01-25 20:34:37 +00002502 * skb_has_shared_frag - can any frag be overwritten
2503 * @skb: buffer to test
2504 *
2505 * Return true if the skb has at least one frag that might be modified
2506 * by an external entity (as in vmsplice()/sendfile())
2507 */
2508static inline bool skb_has_shared_frag(const struct sk_buff *skb)
2509{
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00002510 return skb_is_nonlinear(skb) &&
2511 skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00002512}
2513
2514/**
Herbert Xu364c6ba2006-06-09 16:10:40 -07002515 * skb_linearize_cow - make sure skb is linear and writable
2516 * @skb: buffer to process
2517 *
2518 * If there is no free memory -ENOMEM is returned, otherwise zero
2519 * is returned and the old skb data released.
2520 */
2521static inline int skb_linearize_cow(struct sk_buff *skb)
2522{
2523 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
2524 __skb_linearize(skb) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
2527/**
2528 * skb_postpull_rcsum - update checksum for received skb after pull
2529 * @skb: buffer to update
2530 * @start: start of data before pull
2531 * @len: length of data pulled
2532 *
2533 * After doing a pull on a received packet, you need to call this to
Patrick McHardy84fa7932006-08-29 16:44:56 -07002534 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
2535 * CHECKSUM_NONE so that it can be recomputed from scratch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 */
2537
2538static inline void skb_postpull_rcsum(struct sk_buff *skb,
Herbert Xucbb042f2006-03-20 22:43:56 -08002539 const void *start, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Patrick McHardy84fa7932006-08-29 16:44:56 -07002541 if (skb->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
2543}
2544
Herbert Xucbb042f2006-03-20 22:43:56 -08002545unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
2546
David S. Miller7ce5a272013-12-02 17:26:05 -05002547/**
2548 * pskb_trim_rcsum - trim received skb and update checksum
2549 * @skb: buffer to trim
2550 * @len: new length
2551 *
2552 * This is exactly the same as pskb_trim except that it ensures the
2553 * checksum of received packets are still valid after the operation.
2554 */
2555
2556static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
2557{
2558 if (likely(len >= skb->len))
2559 return 0;
2560 if (skb->ip_summed == CHECKSUM_COMPLETE)
2561 skb->ip_summed = CHECKSUM_NONE;
2562 return __pskb_trim(skb, len);
2563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565#define skb_queue_walk(queue, skb) \
2566 for (skb = (queue)->next; \
Linus Torvaldsa1e48912011-05-22 16:51:43 -07002567 skb != (struct sk_buff *)(queue); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 skb = skb->next)
2569
James Chapman46f89142007-04-30 00:07:31 -07002570#define skb_queue_walk_safe(queue, skb, tmp) \
2571 for (skb = (queue)->next, tmp = skb->next; \
2572 skb != (struct sk_buff *)(queue); \
2573 skb = tmp, tmp = skb->next)
2574
David S. Miller1164f522008-09-23 00:49:44 -07002575#define skb_queue_walk_from(queue, skb) \
Linus Torvaldsa1e48912011-05-22 16:51:43 -07002576 for (; skb != (struct sk_buff *)(queue); \
David S. Miller1164f522008-09-23 00:49:44 -07002577 skb = skb->next)
2578
2579#define skb_queue_walk_from_safe(queue, skb, tmp) \
2580 for (tmp = skb->next; \
2581 skb != (struct sk_buff *)(queue); \
2582 skb = tmp, tmp = skb->next)
2583
Stephen Hemminger300ce172005-10-30 13:47:34 -08002584#define skb_queue_reverse_walk(queue, skb) \
2585 for (skb = (queue)->prev; \
Linus Torvaldsa1e48912011-05-22 16:51:43 -07002586 skb != (struct sk_buff *)(queue); \
Stephen Hemminger300ce172005-10-30 13:47:34 -08002587 skb = skb->prev)
2588
David S. Miller686a2952011-01-20 22:47:32 -08002589#define skb_queue_reverse_walk_safe(queue, skb, tmp) \
2590 for (skb = (queue)->prev, tmp = skb->prev; \
2591 skb != (struct sk_buff *)(queue); \
2592 skb = tmp, tmp = skb->prev)
2593
2594#define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
2595 for (tmp = skb->prev; \
2596 skb != (struct sk_buff *)(queue); \
2597 skb = tmp, tmp = skb->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
David S. Miller21dc3302010-08-23 00:13:46 -07002599static inline bool skb_has_frag_list(const struct sk_buff *skb)
David S. Milleree039872009-06-09 00:17:13 -07002600{
2601 return skb_shinfo(skb)->frag_list != NULL;
2602}
2603
2604static inline void skb_frag_list_init(struct sk_buff *skb)
2605{
2606 skb_shinfo(skb)->frag_list = NULL;
2607}
2608
2609static inline void skb_frag_add_head(struct sk_buff *skb, struct sk_buff *frag)
2610{
2611 frag->next = skb_shinfo(skb)->frag_list;
2612 skb_shinfo(skb)->frag_list = frag;
2613}
2614
2615#define skb_walk_frags(skb, iter) \
2616 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
2617
Joe Perches7965bd42013-09-26 14:48:15 -07002618struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
2619 int *peeked, int *off, int *err);
2620struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock,
2621 int *err);
2622unsigned int datagram_poll(struct file *file, struct socket *sock,
2623 struct poll_table_struct *wait);
2624int skb_copy_datagram_iovec(const struct sk_buff *from, int offset,
2625 struct iovec *to, int size);
2626int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, int hlen,
2627 struct iovec *iov);
2628int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
2629 const struct iovec *from, int from_offset,
2630 int len);
2631int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *frm,
2632 int offset, size_t count);
2633int skb_copy_datagram_const_iovec(const struct sk_buff *from, int offset,
2634 const struct iovec *to, int to_offset,
2635 int size);
2636void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
2637void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb);
2638int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags);
Joe Perches7965bd42013-09-26 14:48:15 -07002639int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
2640int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len);
2641__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to,
2642 int len, __wsum csum);
2643int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
2644 struct pipe_inode_info *pipe, unsigned int len,
2645 unsigned int flags);
2646void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002647unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002648int skb_zerocopy(struct sk_buff *to, struct sk_buff *from,
2649 int len, int hlen);
Joe Perches7965bd42013-09-26 14:48:15 -07002650void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len);
2651int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen);
2652void skb_scrub_packet(struct sk_buff *skb, bool xnet);
Florian Westphalde960aa2014-01-26 10:58:16 +01002653unsigned int skb_gso_transport_seglen(const struct sk_buff *skb);
Joe Perches7965bd42013-09-26 14:48:15 -07002654struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04002655struct sk_buff *skb_vlan_untag(struct sk_buff *skb);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002656
Daniel Borkmann2817a332013-10-30 11:50:51 +01002657struct skb_checksum_ops {
2658 __wsum (*update)(const void *mem, int len, __wsum wsum);
2659 __wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len);
2660};
2661
2662__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2663 __wsum csum, const struct skb_checksum_ops *ops);
2664__wsum skb_checksum(const struct sk_buff *skb, int offset, int len,
2665 __wsum csum);
2666
David S. Miller690e36e2014-08-23 12:13:41 -07002667static inline void *__skb_header_pointer(const struct sk_buff *skb, int offset,
2668 int len, void *data, int hlen, void *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Patrick McHardy55820ee2005-07-05 14:08:10 -07002670 if (hlen - offset >= len)
David S. Miller690e36e2014-08-23 12:13:41 -07002671 return data + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
David S. Miller690e36e2014-08-23 12:13:41 -07002673 if (!skb ||
2674 skb_copy_bits(skb, offset, buffer, len) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return NULL;
2676
2677 return buffer;
2678}
2679
David S. Miller690e36e2014-08-23 12:13:41 -07002680static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
2681 int len, void *buffer)
2682{
2683 return __skb_header_pointer(skb, offset, len, skb->data,
2684 skb_headlen(skb), buffer);
2685}
2686
Daniel Borkmann4262e5c2013-12-06 11:36:16 +01002687/**
2688 * skb_needs_linearize - check if we need to linearize a given skb
2689 * depending on the given device features.
2690 * @skb: socket buffer to check
2691 * @features: net device features
2692 *
2693 * Returns true if either:
2694 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
2695 * 2. skb is fragmented and the device does not support SG.
2696 */
2697static inline bool skb_needs_linearize(struct sk_buff *skb,
2698 netdev_features_t features)
2699{
2700 return skb_is_nonlinear(skb) &&
2701 ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) ||
2702 (skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG)));
2703}
2704
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002705static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
2706 void *to,
2707 const unsigned int len)
2708{
2709 memcpy(to, skb->data, len);
2710}
2711
2712static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
2713 const int offset, void *to,
2714 const unsigned int len)
2715{
2716 memcpy(to, skb->data + offset, len);
2717}
2718
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002719static inline void skb_copy_to_linear_data(struct sk_buff *skb,
2720 const void *from,
2721 const unsigned int len)
2722{
2723 memcpy(skb->data, from, len);
2724}
2725
2726static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
2727 const int offset,
2728 const void *from,
2729 const unsigned int len)
2730{
2731 memcpy(skb->data + offset, from, len);
2732}
2733
Joe Perches7965bd42013-09-26 14:48:15 -07002734void skb_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Patrick Ohlyac45f602009-02-12 05:03:37 +00002736static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
2737{
2738 return skb->tstamp;
2739}
2740
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002741/**
2742 * skb_get_timestamp - get timestamp from a skb
2743 * @skb: skb to get stamp from
2744 * @stamp: pointer to struct timeval to store stamp in
2745 *
2746 * Timestamps are stored in the skb as offsets to a base timestamp.
2747 * This function converts the offset back to a struct timeval and stores
2748 * it in stamp.
2749 */
Patrick Ohlyac45f602009-02-12 05:03:37 +00002750static inline void skb_get_timestamp(const struct sk_buff *skb,
2751 struct timeval *stamp)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002752{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002753 *stamp = ktime_to_timeval(skb->tstamp);
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002754}
2755
Patrick Ohlyac45f602009-02-12 05:03:37 +00002756static inline void skb_get_timestampns(const struct sk_buff *skb,
2757 struct timespec *stamp)
2758{
2759 *stamp = ktime_to_timespec(skb->tstamp);
2760}
2761
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002762static inline void __net_timestamp(struct sk_buff *skb)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002763{
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002764 skb->tstamp = ktime_get_real();
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002765}
2766
Stephen Hemminger164891a2007-04-23 22:26:16 -07002767static inline ktime_t net_timedelta(ktime_t t)
2768{
2769 return ktime_sub(ktime_get_real(), t);
2770}
2771
Ilpo Järvinenb9ce2042007-06-15 15:08:43 -07002772static inline ktime_t net_invalid_timestamp(void)
2773{
2774 return ktime_set(0, 0);
2775}
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002776
Alexander Duyck62bccb82014-09-04 13:31:35 -04002777struct sk_buff *skb_clone_sk(struct sk_buff *skb);
2778
Richard Cochranc1f19b52010-07-17 08:49:36 +00002779#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2780
Joe Perches7965bd42013-09-26 14:48:15 -07002781void skb_clone_tx_timestamp(struct sk_buff *skb);
2782bool skb_defer_rx_timestamp(struct sk_buff *skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +00002783
2784#else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
2785
2786static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
2787{
2788}
2789
2790static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
2791{
2792 return false;
2793}
2794
2795#endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
2796
2797/**
2798 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
2799 *
Richard Cochranda92b192011-10-21 00:49:15 +00002800 * PHY drivers may accept clones of transmitted packets for
2801 * timestamping via their phy_driver.txtstamp method. These drivers
2802 * must call this function to return the skb back to the stack, with
2803 * or without a timestamp.
2804 *
Richard Cochranc1f19b52010-07-17 08:49:36 +00002805 * @skb: clone of the the original outgoing packet
Richard Cochranda92b192011-10-21 00:49:15 +00002806 * @hwtstamps: hardware time stamps, may be NULL if not available
Richard Cochranc1f19b52010-07-17 08:49:36 +00002807 *
2808 */
2809void skb_complete_tx_timestamp(struct sk_buff *skb,
2810 struct skb_shared_hwtstamps *hwtstamps);
2811
Willem de Bruijne7fd2882014-08-04 22:11:48 -04002812void __skb_tstamp_tx(struct sk_buff *orig_skb,
2813 struct skb_shared_hwtstamps *hwtstamps,
2814 struct sock *sk, int tstype);
2815
Patrick Ohlyac45f602009-02-12 05:03:37 +00002816/**
2817 * skb_tstamp_tx - queue clone of skb with send time stamps
2818 * @orig_skb: the original outgoing packet
2819 * @hwtstamps: hardware time stamps, may be NULL if not available
2820 *
2821 * If the skb has a socket associated, then this function clones the
2822 * skb (thus sharing the actual data and optional structures), stores
2823 * the optional hardware time stamping information (if non NULL) or
2824 * generates a software time stamp (otherwise), then queues the clone
2825 * to the error queue of the socket. Errors are silently ignored.
2826 */
Joe Perches7965bd42013-09-26 14:48:15 -07002827void skb_tstamp_tx(struct sk_buff *orig_skb,
2828 struct skb_shared_hwtstamps *hwtstamps);
Patrick Ohlyac45f602009-02-12 05:03:37 +00002829
Richard Cochran4507a712010-07-17 08:48:28 +00002830static inline void sw_tx_timestamp(struct sk_buff *skb)
2831{
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002832 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
2833 !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Richard Cochran4507a712010-07-17 08:48:28 +00002834 skb_tstamp_tx(skb, NULL);
2835}
2836
2837/**
2838 * skb_tx_timestamp() - Driver hook for transmit timestamping
2839 *
2840 * Ethernet MAC Drivers should call this function in their hard_xmit()
Richard Cochran4ff75b72011-06-19 03:31:39 +00002841 * function immediately before giving the sk_buff to the MAC hardware.
Richard Cochran4507a712010-07-17 08:48:28 +00002842 *
David S. Miller73409f32013-12-27 13:04:33 -05002843 * Specifically, one should make absolutely sure that this function is
2844 * called before TX completion of this packet can trigger. Otherwise
2845 * the packet could potentially already be freed.
2846 *
Richard Cochran4507a712010-07-17 08:48:28 +00002847 * @skb: A socket buffer.
2848 */
2849static inline void skb_tx_timestamp(struct sk_buff *skb)
2850{
Richard Cochranc1f19b52010-07-17 08:49:36 +00002851 skb_clone_tx_timestamp(skb);
Richard Cochran4507a712010-07-17 08:48:28 +00002852 sw_tx_timestamp(skb);
2853}
2854
Johannes Berg6e3e9392011-11-09 10:15:42 +01002855/**
2856 * skb_complete_wifi_ack - deliver skb with wifi status
2857 *
2858 * @skb: the original outgoing packet
2859 * @acked: ack status
2860 *
2861 */
2862void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
2863
Joe Perches7965bd42013-09-26 14:48:15 -07002864__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
2865__sum16 __skb_checksum_complete(struct sk_buff *skb);
Herbert Xufb286bb2005-11-10 13:01:24 -08002866
Herbert Xu60476372007-04-09 11:59:39 -07002867static inline int skb_csum_unnecessary(const struct sk_buff *skb)
2868{
Tom Herbert5d0c2b92014-06-10 18:54:13 -07002869 return ((skb->ip_summed & CHECKSUM_UNNECESSARY) || skb->csum_valid);
Herbert Xu60476372007-04-09 11:59:39 -07002870}
2871
Herbert Xufb286bb2005-11-10 13:01:24 -08002872/**
2873 * skb_checksum_complete - Calculate checksum of an entire packet
2874 * @skb: packet to process
2875 *
2876 * This function calculates the checksum over the entire packet plus
2877 * the value of skb->csum. The latter can be used to supply the
2878 * checksum of a pseudo header as used by TCP/UDP. It returns the
2879 * checksum.
2880 *
2881 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
2882 * this function can be used to verify that checksum on received
2883 * packets. In that case the function should return zero if the
2884 * checksum is correct. In particular, this function will return zero
2885 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
2886 * hardware has already verified the correctness of the checksum.
2887 */
Al Viro4381ca32007-07-15 21:00:11 +01002888static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
Herbert Xufb286bb2005-11-10 13:01:24 -08002889{
Herbert Xu60476372007-04-09 11:59:39 -07002890 return skb_csum_unnecessary(skb) ?
2891 0 : __skb_checksum_complete(skb);
Herbert Xufb286bb2005-11-10 13:01:24 -08002892}
2893
Tom Herbert77cffe22014-08-27 21:26:46 -07002894static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb)
2895{
2896 if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
2897 if (skb->csum_level == 0)
2898 skb->ip_summed = CHECKSUM_NONE;
2899 else
2900 skb->csum_level--;
2901 }
2902}
2903
2904static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb)
2905{
2906 if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
2907 if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
2908 skb->csum_level++;
2909 } else if (skb->ip_summed == CHECKSUM_NONE) {
2910 skb->ip_summed = CHECKSUM_UNNECESSARY;
2911 skb->csum_level = 0;
2912 }
2913}
2914
Tom Herbert5a212322014-08-31 15:12:41 -07002915static inline void __skb_mark_checksum_bad(struct sk_buff *skb)
2916{
2917 /* Mark current checksum as bad (typically called from GRO
2918 * path). In the case that ip_summed is CHECKSUM_NONE
2919 * this must be the first checksum encountered in the packet.
2920 * When ip_summed is CHECKSUM_UNNECESSARY, this is the first
2921 * checksum after the last one validated. For UDP, a zero
2922 * checksum can not be marked as bad.
2923 */
2924
2925 if (skb->ip_summed == CHECKSUM_NONE ||
2926 skb->ip_summed == CHECKSUM_UNNECESSARY)
2927 skb->csum_bad = 1;
2928}
2929
Tom Herbert76ba0aa2014-05-02 16:29:18 -07002930/* Check if we need to perform checksum complete validation.
2931 *
2932 * Returns true if checksum complete is needed, false otherwise
2933 * (either checksum is unnecessary or zero checksum is allowed).
2934 */
2935static inline bool __skb_checksum_validate_needed(struct sk_buff *skb,
2936 bool zero_okay,
2937 __sum16 check)
2938{
Tom Herbert5d0c2b92014-06-10 18:54:13 -07002939 if (skb_csum_unnecessary(skb) || (zero_okay && !check)) {
2940 skb->csum_valid = 1;
Tom Herbert77cffe22014-08-27 21:26:46 -07002941 __skb_decr_checksum_unnecessary(skb);
Tom Herbert76ba0aa2014-05-02 16:29:18 -07002942 return false;
2943 }
2944
2945 return true;
2946}
2947
2948/* For small packets <= CHECKSUM_BREAK peform checksum complete directly
2949 * in checksum_init.
2950 */
2951#define CHECKSUM_BREAK 76
2952
2953/* Validate (init) checksum based on checksum complete.
2954 *
2955 * Return values:
2956 * 0: checksum is validated or try to in skb_checksum_complete. In the latter
2957 * case the ip_summed will not be CHECKSUM_UNNECESSARY and the pseudo
2958 * checksum is stored in skb->csum for use in __skb_checksum_complete
2959 * non-zero: value of invalid checksum
2960 *
2961 */
2962static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb,
2963 bool complete,
2964 __wsum psum)
2965{
2966 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2967 if (!csum_fold(csum_add(psum, skb->csum))) {
Tom Herbert5d0c2b92014-06-10 18:54:13 -07002968 skb->csum_valid = 1;
Tom Herbert76ba0aa2014-05-02 16:29:18 -07002969 return 0;
2970 }
Tom Herbert5a212322014-08-31 15:12:41 -07002971 } else if (skb->csum_bad) {
2972 /* ip_summed == CHECKSUM_NONE in this case */
2973 return 1;
Tom Herbert76ba0aa2014-05-02 16:29:18 -07002974 }
2975
2976 skb->csum = psum;
2977
Tom Herbert5d0c2b92014-06-10 18:54:13 -07002978 if (complete || skb->len <= CHECKSUM_BREAK) {
2979 __sum16 csum;
2980
2981 csum = __skb_checksum_complete(skb);
2982 skb->csum_valid = !csum;
2983 return csum;
2984 }
Tom Herbert76ba0aa2014-05-02 16:29:18 -07002985
2986 return 0;
2987}
2988
2989static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto)
2990{
2991 return 0;
2992}
2993
2994/* Perform checksum validate (init). Note that this is a macro since we only
2995 * want to calculate the pseudo header which is an input function if necessary.
2996 * First we try to validate without any computation (checksum unnecessary) and
2997 * then calculate based on checksum complete calling the function to compute
2998 * pseudo header.
2999 *
3000 * Return values:
3001 * 0: checksum is validated or try to in skb_checksum_complete
3002 * non-zero: value of invalid checksum
3003 */
3004#define __skb_checksum_validate(skb, proto, complete, \
3005 zero_okay, check, compute_pseudo) \
3006({ \
3007 __sum16 __ret = 0; \
Tom Herbert5d0c2b92014-06-10 18:54:13 -07003008 skb->csum_valid = 0; \
Tom Herbert76ba0aa2014-05-02 16:29:18 -07003009 if (__skb_checksum_validate_needed(skb, zero_okay, check)) \
3010 __ret = __skb_checksum_validate_complete(skb, \
3011 complete, compute_pseudo(skb, proto)); \
3012 __ret; \
3013})
3014
3015#define skb_checksum_init(skb, proto, compute_pseudo) \
3016 __skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo)
3017
3018#define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo) \
3019 __skb_checksum_validate(skb, proto, false, true, check, compute_pseudo)
3020
3021#define skb_checksum_validate(skb, proto, compute_pseudo) \
3022 __skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo)
3023
3024#define skb_checksum_validate_zero_check(skb, proto, check, \
3025 compute_pseudo) \
3026 __skb_checksum_validate_(skb, proto, true, true, check, compute_pseudo)
3027
3028#define skb_checksum_simple_validate(skb) \
3029 __skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo)
3030
Tom Herbertd96535a2014-08-31 15:12:42 -07003031static inline bool __skb_checksum_convert_check(struct sk_buff *skb)
3032{
3033 return (skb->ip_summed == CHECKSUM_NONE &&
3034 skb->csum_valid && !skb->csum_bad);
3035}
3036
3037static inline void __skb_checksum_convert(struct sk_buff *skb,
3038 __sum16 check, __wsum pseudo)
3039{
3040 skb->csum = ~pseudo;
3041 skb->ip_summed = CHECKSUM_COMPLETE;
3042}
3043
3044#define skb_checksum_try_convert(skb, proto, check, compute_pseudo) \
3045do { \
3046 if (__skb_checksum_convert_check(skb)) \
3047 __skb_checksum_convert(skb, check, \
3048 compute_pseudo(skb, proto)); \
3049} while (0)
3050
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07003051#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Joe Perches7965bd42013-09-26 14:48:15 -07003052void nf_conntrack_destroy(struct nf_conntrack *nfct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053static inline void nf_conntrack_put(struct nf_conntrack *nfct)
3054{
3055 if (nfct && atomic_dec_and_test(&nfct->use))
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -07003056 nf_conntrack_destroy(nfct);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058static inline void nf_conntrack_get(struct nf_conntrack *nfct)
3059{
3060 if (nfct)
3061 atomic_inc(&nfct->use);
3062}
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01003063#endif
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02003064#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
3066{
3067 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
3068 kfree(nf_bridge);
3069}
3070static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
3071{
3072 if (nf_bridge)
3073 atomic_inc(&nf_bridge->use);
3074}
3075#endif /* CONFIG_BRIDGE_NETFILTER */
Patrick McHardya193a4a2006-03-20 19:23:05 -08003076static inline void nf_reset(struct sk_buff *skb)
3077{
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07003078#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Patrick McHardya193a4a2006-03-20 19:23:05 -08003079 nf_conntrack_put(skb->nfct);
3080 skb->nfct = NULL;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01003081#endif
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02003082#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Patrick McHardya193a4a2006-03-20 19:23:05 -08003083 nf_bridge_put(skb->nf_bridge);
3084 skb->nf_bridge = NULL;
3085#endif
3086}
3087
Patrick McHardy124dff02013-04-05 20:42:05 +02003088static inline void nf_reset_trace(struct sk_buff *skb)
3089{
Florian Westphal478b3602014-02-15 23:48:45 +01003090#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
Gao feng130549fe2013-03-21 19:48:41 +00003091 skb->nf_trace = 0;
3092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07003095/* Note: This doesn't put any conntrack and bridge info in dst. */
Eric Dumazetb1937222014-09-28 22:18:47 -07003096static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src,
3097 bool copy)
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07003098{
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07003099#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07003100 dst->nfct = src->nfct;
3101 nf_conntrack_get(src->nfct);
Eric Dumazetb1937222014-09-28 22:18:47 -07003102 if (copy)
3103 dst->nfctinfo = src->nfctinfo;
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01003104#endif
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02003105#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07003106 dst->nf_bridge = src->nf_bridge;
3107 nf_bridge_get(src->nf_bridge);
3108#endif
Florian Westphal478b3602014-02-15 23:48:45 +01003109#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
Eric Dumazetb1937222014-09-28 22:18:47 -07003110 if (copy)
3111 dst->nf_trace = src->nf_trace;
Florian Westphal478b3602014-02-15 23:48:45 +01003112#endif
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -07003113}
3114
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07003115static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
3116{
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07003117#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -07003118 nf_conntrack_put(dst->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +01003119#endif
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02003120#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07003121 nf_bridge_put(dst->nf_bridge);
3122#endif
Eric Dumazetb1937222014-09-28 22:18:47 -07003123 __nf_copy(dst, src, true);
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -07003124}
3125
James Morris984bc162006-06-09 00:29:17 -07003126#ifdef CONFIG_NETWORK_SECMARK
3127static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
3128{
3129 to->secmark = from->secmark;
3130}
3131
3132static inline void skb_init_secmark(struct sk_buff *skb)
3133{
3134 skb->secmark = 0;
3135}
3136#else
3137static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
3138{ }
3139
3140static inline void skb_init_secmark(struct sk_buff *skb)
3141{ }
3142#endif
3143
Eric W. Biederman574f7192014-04-01 12:20:24 -07003144static inline bool skb_irq_freeable(const struct sk_buff *skb)
3145{
3146 return !skb->destructor &&
3147#if IS_ENABLED(CONFIG_XFRM)
3148 !skb->sp &&
3149#endif
3150#if IS_ENABLED(CONFIG_NF_CONNTRACK)
3151 !skb->nfct &&
3152#endif
3153 !skb->_skb_refdst &&
3154 !skb_has_frag_list(skb);
3155}
3156
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003157static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
3158{
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003159 skb->queue_mapping = queue_mapping;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003160}
3161
Stephen Hemminger92477442009-03-21 13:39:26 -07003162static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
Pavel Emelyanov4e3ab472007-10-21 17:01:29 -07003163{
Pavel Emelyanov4e3ab472007-10-21 17:01:29 -07003164 return skb->queue_mapping;
Pavel Emelyanov4e3ab472007-10-21 17:01:29 -07003165}
3166
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003167static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
3168{
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003169 to->queue_mapping = from->queue_mapping;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07003170}
3171
David S. Millerd5a9e242009-01-27 16:22:11 -08003172static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
3173{
3174 skb->queue_mapping = rx_queue + 1;
3175}
3176
Stephen Hemminger92477442009-03-21 13:39:26 -07003177static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
David S. Millerd5a9e242009-01-27 16:22:11 -08003178{
3179 return skb->queue_mapping - 1;
3180}
3181
Stephen Hemminger92477442009-03-21 13:39:26 -07003182static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
David S. Millerd5a9e242009-01-27 16:22:11 -08003183{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003184 return skb->queue_mapping != 0;
David S. Millerd5a9e242009-01-27 16:22:11 -08003185}
3186
Tom Herbert0e001612014-07-01 21:32:27 -07003187u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
Joe Perches7965bd42013-09-26 14:48:15 -07003188 unsigned int num_tx_queues);
Stephen Hemminger92477442009-03-21 13:39:26 -07003189
Denis Kirjanov0b3d8e02013-10-02 05:58:32 +04003190static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
3191{
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07003192#ifdef CONFIG_XFRM
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07003193 return skb->sp;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07003194#else
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07003195 return NULL;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07003196#endif
Denis Kirjanov0b3d8e02013-10-02 05:58:32 +04003197}
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07003198
Pravin B Shelar68c33162013-02-14 14:02:41 +00003199/* Keeps track of mac header offset relative to skb->head.
3200 * It is useful for TSO of Tunneling protocol. e.g. GRE.
3201 * For non-tunnel skb it points to skb_mac_header() and for
Eric Dumazet3347c962013-10-19 11:42:56 -07003202 * tunnel skb it points to outer mac header.
3203 * Keeps track of level of encapsulation of network headers.
3204 */
Pravin B Shelar68c33162013-02-14 14:02:41 +00003205struct skb_gso_cb {
Eric Dumazet3347c962013-10-19 11:42:56 -07003206 int mac_offset;
3207 int encap_level;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003208 __u16 csum_start;
Pravin B Shelar68c33162013-02-14 14:02:41 +00003209};
3210#define SKB_GSO_CB(skb) ((struct skb_gso_cb *)(skb)->cb)
3211
3212static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
3213{
3214 return (skb_mac_header(inner_skb) - inner_skb->head) -
3215 SKB_GSO_CB(inner_skb)->mac_offset;
3216}
3217
Pravin B Shelar1e2bd512013-05-30 06:45:27 +00003218static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
3219{
3220 int new_headroom, headroom;
3221 int ret;
3222
3223 headroom = skb_headroom(skb);
3224 ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
3225 if (ret)
3226 return ret;
3227
3228 new_headroom = skb_headroom(skb);
3229 SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
3230 return 0;
3231}
3232
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003233/* Compute the checksum for a gso segment. First compute the checksum value
3234 * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and
3235 * then add in skb->csum (checksum from csum_start to end of packet).
3236 * skb->csum and csum_start are then updated to reflect the checksum of the
3237 * resultant packet starting from the transport header-- the resultant checksum
3238 * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo
3239 * header.
3240 */
3241static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
3242{
3243 int plen = SKB_GSO_CB(skb)->csum_start - skb_headroom(skb) -
3244 skb_transport_offset(skb);
3245 __u16 csum;
3246
3247 csum = csum_fold(csum_partial(skb_transport_header(skb),
3248 plen, skb->csum));
3249 skb->csum = res;
3250 SKB_GSO_CB(skb)->csum_start -= plen;
3251
3252 return csum;
3253}
3254
David S. Millerbdcc0922012-03-07 20:53:36 -05003255static inline bool skb_is_gso(const struct sk_buff *skb)
Herbert Xu89114af2006-07-08 13:34:32 -07003256{
3257 return skb_shinfo(skb)->gso_size;
3258}
3259
Eric Dumazet36a8f392013-09-29 01:21:32 -07003260/* Note: Should be called only if skb_is_gso(skb) is true */
David S. Millerbdcc0922012-03-07 20:53:36 -05003261static inline bool skb_is_gso_v6(const struct sk_buff *skb)
Brice Goglineabd7e32007-10-13 12:33:32 +02003262{
3263 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
3264}
3265
Joe Perches7965bd42013-09-26 14:48:15 -07003266void __skb_warn_lro_forwarding(const struct sk_buff *skb);
Ben Hutchings4497b072008-06-19 16:22:28 -07003267
3268static inline bool skb_warn_if_lro(const struct sk_buff *skb)
3269{
3270 /* LRO sets gso_size but not gso_type, whereas if GSO is really
3271 * wanted then gso_type will be set. */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04003272 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3273
Alexander Duyckb78462e2010-06-02 12:24:37 +00003274 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
3275 unlikely(shinfo->gso_type == 0)) {
Ben Hutchings4497b072008-06-19 16:22:28 -07003276 __skb_warn_lro_forwarding(skb);
3277 return true;
3278 }
3279 return false;
3280}
3281
Herbert Xu35fc92a2007-03-26 23:22:20 -07003282static inline void skb_forward_csum(struct sk_buff *skb)
3283{
3284 /* Unfortunately we don't support this one. Any brave souls? */
3285 if (skb->ip_summed == CHECKSUM_COMPLETE)
3286 skb->ip_summed = CHECKSUM_NONE;
3287}
3288
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003289/**
3290 * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
3291 * @skb: skb to check
3292 *
3293 * fresh skbs have their ip_summed set to CHECKSUM_NONE.
3294 * Instead of forcing ip_summed to CHECKSUM_NONE, we can
3295 * use this helper, to document places where we make this assertion.
3296 */
Eric Dumazet05bdd2f2011-10-20 17:45:43 -04003297static inline void skb_checksum_none_assert(const struct sk_buff *skb)
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003298{
3299#ifdef DEBUG
3300 BUG_ON(skb->ip_summed != CHECKSUM_NONE);
3301#endif
3302}
3303
Rusty Russellf35d9d82008-02-04 23:49:54 -05003304bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
Shirley Maa6686f22011-07-06 12:22:12 +00003305
Paul Durranted1f50c2014-01-09 10:02:46 +00003306int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
3307
Alexander Duyck56193d12014-09-05 19:20:26 -04003308u32 skb_get_poff(const struct sk_buff *skb);
3309u32 __skb_get_poff(const struct sk_buff *skb, void *data,
3310 const struct flow_keys *keys, int hlen);
Daniel Borkmannf77668d2013-03-19 06:39:30 +00003311
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00003312/**
3313 * skb_head_is_locked - Determine if the skb->head is locked down
3314 * @skb: skb to check
3315 *
3316 * The head on skbs build around a head frag can be removed if they are
3317 * not cloned. This function returns true if the skb head is locked down
3318 * due to either being allocated via kmalloc, or by being a clone with
3319 * multiple references to the head.
3320 */
3321static inline bool skb_head_is_locked(const struct sk_buff *skb)
3322{
3323 return !skb->head_frag || skb_cloned(skb);
3324}
Florian Westphalfe6cc552014-02-13 23:09:12 +01003325
3326/**
3327 * skb_gso_network_seglen - Return length of individual segments of a gso packet
3328 *
3329 * @skb: GSO skb
3330 *
3331 * skb_gso_network_seglen is used to determine the real size of the
3332 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
3333 *
3334 * The MAC/L2 header is not accounted for.
3335 */
3336static inline unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
3337{
3338 unsigned int hdr_len = skb_transport_header(skb) -
3339 skb_network_header(skb);
3340 return hdr_len + skb_gso_transport_seglen(skb);
3341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342#endif /* __KERNEL__ */
3343#endif /* _LINUX_SKBUFF_H */