blob: 6148716884ae85e6919ec6e8e93885f1fb8c2e9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphal3fb03b52014-02-22 10:30:17 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/netdevice.h>
53#ifdef CONFIG_NET_CLS_ACT
54#include <net/pkt_sched.h>
55#endif
56#include <linux/string.h>
57#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080058#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/cache.h>
60#include <linux/rtnetlink.h>
61#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070062#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000063#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070064#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <net/protocol.h>
67#include <net/dst.h>
68#include <net/sock.h>
69#include <net/checksum.h>
70#include <net/xfrm.h>
71
72#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040073#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020074#include <linux/highmem.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040075
Eric Dumazetd7e88832012-04-30 08:10:34 +000076struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080077static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/**
Jean Sacrenf05de732013-02-11 13:30:38 +000080 * skb_panic - private function for out-of-line support
81 * @skb: buffer
82 * @sz: size
83 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000084 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 *
Jean Sacrenf05de732013-02-11 13:30:38 +000086 * Out-of-line support for skb_put() and skb_push().
87 * Called via the wrapper skb_over_panic() or skb_under_panic().
88 * Keep out of line to prevent kernel bloat.
89 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Jean Sacrenf05de732013-02-11 13:30:38 +000091static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000092 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Joe Perchese005d192012-05-16 19:58:40 +000094 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +000095 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +000096 (unsigned long)skb->tail, (unsigned long)skb->end,
97 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 BUG();
99}
100
Jean Sacrenf05de732013-02-11 13:30:38 +0000101static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Jean Sacrenf05de732013-02-11 13:30:38 +0000103 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Jean Sacrenf05de732013-02-11 13:30:38 +0000106static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
107{
108 skb_panic(skb, sz, addr, __func__);
109}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700110
111/*
112 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
113 * the caller if emergency pfmemalloc reserves are being used. If it is and
114 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
115 * may be used. Otherwise, the packet data may be discarded until enough
116 * memory is free
117 */
118#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
119 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000120
121static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
122 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700123{
124 void *obj;
125 bool ret_pfmemalloc = false;
126
127 /*
128 * Try a regular allocation, when that fails and we're not entitled
129 * to the reserves, fail.
130 */
131 obj = kmalloc_node_track_caller(size,
132 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
133 node);
134 if (obj || !(gfp_pfmemalloc_allowed(flags)))
135 goto out;
136
137 /* Try again but now we are using pfmemalloc reserves */
138 ret_pfmemalloc = true;
139 obj = kmalloc_node_track_caller(size, flags, node);
140
141out:
142 if (pfmemalloc)
143 *pfmemalloc = ret_pfmemalloc;
144
145 return obj;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* Allocate a new skbuff. We do this ourselves so we can fill in a few
149 * 'private' fields and also do memory statistics to find all the
150 * [BEEP] leaks.
151 *
152 */
153
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000154struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
155{
156 struct sk_buff *skb;
157
158 /* Get the HEAD */
159 skb = kmem_cache_alloc_node(skbuff_head_cache,
160 gfp_mask & ~__GFP_DMA, node);
161 if (!skb)
162 goto out;
163
164 /*
165 * Only clear those fields we need to clear, not those that we will
166 * actually initialise below. Hence, don't put any more fields after
167 * the tail pointer in struct sk_buff!
168 */
169 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000170 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000171 skb->truesize = sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
173
174#ifdef NET_SKBUFF_DATA_USES_OFFSET
175 skb->mac_header = ~0U;
176#endif
177out:
178 return skb;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
David S. Millerd179cd12005-08-17 14:57:30 -0700182 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @size: size to allocate
184 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800189 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
196 * %GFP_ATOMIC.
197 */
Al Virodd0fc662005-10-07 07:46:04 +0100198struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700199 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Lametere18b8902006-12-06 20:33:20 -0800201 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800202 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct sk_buff *skb;
204 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700205 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
209
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!skb)
216 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700217 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
223 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000224 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!data)
228 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
232 */
233 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700234 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300236 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300240 */
241 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700244 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 atomic_set(&skb->users, 1);
246 skb->head = data;
247 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700248 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700249 skb->end = skb->tail + size;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000250#ifdef NET_SKBUFF_DATA_USES_OFFSET
251 skb->mac_header = ~0U;
Eric Dumazetfda55ec2013-01-07 09:28:21 +0000252 skb->transport_header = ~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000253#endif
254
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000259 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800260
Mel Gormanc93bdd02012-07-31 16:44:19 -0700261 if (flags & SKB_ALLOC_FCLONE) {
David S. Millerd179cd12005-08-17 14:57:30 -0700262 struct sk_buff *child = skb + 1;
263 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200265 kmemcheck_annotate_bitfield(child, flags1);
266 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700267 skb->fclone = SKB_FCLONE_ORIG;
268 atomic_set(fclone_ref, 1);
269
270 child->fclone = SKB_FCLONE_UNAVAILABLE;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700271 child->pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273out:
274 return skb;
275nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800276 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb = NULL;
278 goto out;
279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800280EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000283 * build_skb - build a network buffer
284 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000285 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
288 * skb_shared_info. @data must have been allocated by kmalloc()
289 * The return is the new skb buffer.
290 * On a failure the return is %NULL, and @data is not freed.
291 * Notes :
292 * Before IO, driver allocates only data buffer where NIC put incoming frame
293 * Driver should add room at head (NET_SKB_PAD) and
294 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
295 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
296 * before giving packet to stack.
297 * RX rings only contains data buffers, not full skbs.
298 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000299struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000300{
301 struct skb_shared_info *shinfo;
302 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000303 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000304
305 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
306 if (!skb)
307 return NULL;
308
Eric Dumazetd3836f22012-04-27 00:33:38 +0000309 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000310
311 memset(skb, 0, offsetof(struct sk_buff, tail));
312 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000313 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000314 atomic_set(&skb->users, 1);
315 skb->head = data;
316 skb->data = data;
317 skb_reset_tail_pointer(skb);
318 skb->end = skb->tail + size;
319#ifdef NET_SKBUFF_DATA_USES_OFFSET
320 skb->mac_header = ~0U;
Eric Dumazetfda55ec2013-01-07 09:28:21 +0000321 skb->transport_header = ~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000322#endif
323
324 /* make sure we initialize shinfo sequentially */
325 shinfo = skb_shinfo(skb);
326 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
327 atomic_set(&shinfo->dataref, 1);
328 kmemcheck_annotate_variable(shinfo->destructor_arg);
329
330 return skb;
331}
332EXPORT_SYMBOL(build_skb);
333
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000334struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000335 struct page_frag frag;
336 /* we maintain a pagecount bias, so that we dont dirty cache line
337 * containing page->_count every time we allocate a fragment.
338 */
339 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000340};
341static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
342
Mel Gormanc93bdd02012-07-31 16:44:19 -0700343static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000344{
345 struct netdev_alloc_cache *nc;
346 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000347 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000348 unsigned long flags;
349
350 local_irq_save(flags);
351 nc = &__get_cpu_var(netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000352 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000353refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000354 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
355 gfp_t gfp = gfp_mask;
356
357 if (order)
358 gfp |= __GFP_COMP | __GFP_NOWARN;
359 nc->frag.page = alloc_pages(gfp, order);
360 if (likely(nc->frag.page))
361 break;
362 if (--order < 0)
363 goto end;
364 }
365 nc->frag.size = PAGE_SIZE << order;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000366recycle:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000367 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
369 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000370 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000371
Eric Dumazet69b08f62012-09-26 06:46:57 +0000372 if (nc->frag.offset + fragsz > nc->frag.size) {
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000373 /* avoid unnecessary locked operations if possible */
Eric Dumazet69b08f62012-09-26 06:46:57 +0000374 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
375 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000376 goto recycle;
377 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000378 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000379
Eric Dumazet69b08f62012-09-26 06:46:57 +0000380 data = page_address(nc->frag.page) + nc->frag.offset;
381 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000382 nc->pagecnt_bias--;
383end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000384 local_irq_restore(flags);
385 return data;
386}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700387
388/**
389 * netdev_alloc_frag - allocate a page fragment
390 * @fragsz: fragment size
391 *
392 * Allocates a frag from a page for receive buffer.
393 * Uses GFP_ATOMIC allocations.
394 */
395void *netdev_alloc_frag(unsigned int fragsz)
396{
397 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
398}
Eric Dumazet6f532612012-05-18 05:12:12 +0000399EXPORT_SYMBOL(netdev_alloc_frag);
400
401/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700402 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
403 * @dev: network device to receive on
404 * @length: length to allocate
405 * @gfp_mask: get_free_pages mask, passed to alloc_skb
406 *
407 * Allocate a new &sk_buff and assign it a usage count of one. The
408 * buffer has unspecified headroom built in. Users should allocate
409 * the headroom they think they need without accounting for the
410 * built in space. The built in space is used for optimisations.
411 *
412 * %NULL is returned if there is no free memory.
413 */
414struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000415 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700416{
Eric Dumazet6f532612012-05-18 05:12:12 +0000417 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000418 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
419 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700420
Eric Dumazet310e1582012-07-16 13:15:52 +0200421 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700422 void *data;
423
424 if (sk_memalloc_socks())
425 gfp_mask |= __GFP_MEMALLOC;
426
427 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000428
Eric Dumazet6f532612012-05-18 05:12:12 +0000429 if (likely(data)) {
430 skb = build_skb(data, fragsz);
431 if (unlikely(!skb))
432 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000433 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000434 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700435 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
436 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000437 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700438 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700439 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700440 skb->dev = dev;
441 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700442 return skb;
443}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800444EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Peter Zijlstra654bed12008-10-07 14:22:33 -0700446void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000447 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700448{
449 skb_fill_page_desc(skb, i, page, off, size);
450 skb->len += size;
451 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000452 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700453}
454EXPORT_SYMBOL(skb_add_rx_frag);
455
Herbert Xu27b437c2006-07-13 19:26:39 -0700456static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700458 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700459 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Herbert Xu27b437c2006-07-13 19:26:39 -0700462static inline void skb_drop_fraglist(struct sk_buff *skb)
463{
464 skb_drop_list(&skb_shinfo(skb)->frag_list);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static void skb_clone_fraglist(struct sk_buff *skb)
468{
469 struct sk_buff *list;
470
David S. Millerfbb398a2009-06-09 00:18:59 -0700471 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 skb_get(list);
473}
474
Eric Dumazetd3836f22012-04-27 00:33:38 +0000475static void skb_free_head(struct sk_buff *skb)
476{
477 if (skb->head_frag)
478 put_page(virt_to_head_page(skb->head));
479 else
480 kfree(skb->head);
481}
482
Adrian Bunk5bba1712006-06-29 13:02:35 -0700483static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 if (!skb->cloned ||
486 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
487 &skb_shinfo(skb)->dataref)) {
488 if (skb_shinfo(skb)->nr_frags) {
489 int i;
490 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +0000491 skb_frag_unref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Shirley Maa6686f22011-07-06 12:22:12 +0000494 /*
495 * If skb buf is from userspace, we need to notify the caller
496 * the lower device DMA has done;
497 */
498 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
499 struct ubuf_info *uarg;
500
501 uarg = skb_shinfo(skb)->destructor_arg;
502 if (uarg->callback)
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000503 uarg->callback(uarg, true);
Shirley Maa6686f22011-07-06 12:22:12 +0000504 }
505
David S. Miller21dc3302010-08-23 00:13:46 -0700506 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 skb_drop_fraglist(skb);
508
Eric Dumazetd3836f22012-04-27 00:33:38 +0000509 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511}
512
513/*
514 * Free an skbuff by memory without cleaning the state.
515 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800516static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
David S. Millerd179cd12005-08-17 14:57:30 -0700518 struct sk_buff *other;
519 atomic_t *fclone_ref;
520
David S. Millerd179cd12005-08-17 14:57:30 -0700521 switch (skb->fclone) {
522 case SKB_FCLONE_UNAVAILABLE:
523 kmem_cache_free(skbuff_head_cache, skb);
524 break;
525
526 case SKB_FCLONE_ORIG:
527 fclone_ref = (atomic_t *) (skb + 2);
528 if (atomic_dec_and_test(fclone_ref))
529 kmem_cache_free(skbuff_fclone_cache, skb);
530 break;
531
532 case SKB_FCLONE_CLONE:
533 fclone_ref = (atomic_t *) (skb + 1);
534 other = skb - 1;
535
536 /* The clone portion is available for
537 * fast-cloning again.
538 */
539 skb->fclone = SKB_FCLONE_UNAVAILABLE;
540
541 if (atomic_dec_and_test(fclone_ref))
542 kmem_cache_free(skbuff_fclone_cache, other);
543 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700547static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Eric Dumazetadf30902009-06-02 05:19:30 +0000549 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#ifdef CONFIG_XFRM
551 secpath_put(skb->sp);
552#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700553 if (skb->destructor) {
554 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 skb->destructor(skb);
556 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000557#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700558 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#ifdef CONFIG_BRIDGE_NETFILTER
561 nf_bridge_put(skb->nf_bridge);
562#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/* XXX: IS this still necessary? - JHS */
564#ifdef CONFIG_NET_SCHED
565 skb->tc_index = 0;
566#ifdef CONFIG_NET_CLS_ACT
567 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568#endif
569#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700570}
571
572/* Free everything but the sk_buff shell. */
573static void skb_release_all(struct sk_buff *skb)
574{
575 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000576 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000577 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Herbert Xu2d4baff2007-11-26 23:11:19 +0800580/**
581 * __kfree_skb - private function
582 * @skb: buffer
583 *
584 * Free an sk_buff. Release anything attached to the buffer.
585 * Clean the state. This is an internal helper function. Users should
586 * always call kfree_skb
587 */
588
589void __kfree_skb(struct sk_buff *skb)
590{
591 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 kfree_skbmem(skb);
593}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800594EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800597 * kfree_skb - free an sk_buff
598 * @skb: buffer to free
599 *
600 * Drop a reference to the buffer and free it if the usage count has
601 * hit zero.
602 */
603void kfree_skb(struct sk_buff *skb)
604{
605 if (unlikely(!skb))
606 return;
607 if (likely(atomic_read(&skb->users) == 1))
608 smp_rmb();
609 else if (likely(!atomic_dec_and_test(&skb->users)))
610 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000611 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800612 __kfree_skb(skb);
613}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800614EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800615
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700616void kfree_skb_list(struct sk_buff *segs)
617{
618 while (segs) {
619 struct sk_buff *next = segs->next;
620
621 kfree_skb(segs);
622 segs = next;
623 }
624}
625EXPORT_SYMBOL(kfree_skb_list);
626
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700627/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000628 * skb_tx_error - report an sk_buff xmit error
629 * @skb: buffer that triggered an error
630 *
631 * Report xmit error if a device callback is tracking this skb.
632 * skb must be freed afterwards.
633 */
634void skb_tx_error(struct sk_buff *skb)
635{
636 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
637 struct ubuf_info *uarg;
638
639 uarg = skb_shinfo(skb)->destructor_arg;
640 if (uarg->callback)
641 uarg->callback(uarg, false);
642 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
643 }
644}
645EXPORT_SYMBOL(skb_tx_error);
646
647/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000648 * consume_skb - free an skbuff
649 * @skb: buffer to free
650 *
651 * Drop a ref to the buffer and free it if the usage count has hit zero
652 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
653 * is being dropped after a failure and notes that
654 */
655void consume_skb(struct sk_buff *skb)
656{
657 if (unlikely(!skb))
658 return;
659 if (likely(atomic_read(&skb->users) == 1))
660 smp_rmb();
661 else if (likely(!atomic_dec_and_test(&skb->users)))
662 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900663 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000664 __kfree_skb(skb);
665}
666EXPORT_SYMBOL(consume_skb);
667
Herbert Xudec18812007-10-14 00:37:30 -0700668static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
669{
670 new->tstamp = old->tstamp;
671 new->dev = old->dev;
672 new->transport_header = old->transport_header;
673 new->network_header = old->network_header;
674 new->mac_header = old->mac_header;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000675 new->inner_transport_header = old->inner_transport_header;
Pravin B Shelar92df9b22013-02-01 15:18:49 +0000676 new->inner_network_header = old->inner_network_header;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000677 new->inner_mac_header = old->inner_mac_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000678 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000679 new->rxhash = old->rxhash;
Changli Gao6461be32011-08-19 04:44:18 +0000680 new->ooo_okay = old->ooo_okay;
Tom Herbertbdeab992011-08-14 19:45:55 +0000681 new->l4_rxhash = old->l4_rxhash;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000682 new->no_fcs = old->no_fcs;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000683 new->encapsulation = old->encapsulation;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700684#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700685 new->sp = secpath_get(old->sp);
686#endif
687 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000688 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700689 new->local_df = old->local_df;
690 new->pkt_type = old->pkt_type;
691 new->ip_summed = old->ip_summed;
692 skb_copy_queue_mapping(new, old);
693 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000694#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700695 new->ipvs_property = old->ipvs_property;
696#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700697 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700698 new->protocol = old->protocol;
699 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800700 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700701 __nf_copy(new, old);
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000702#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Herbert Xudec18812007-10-14 00:37:30 -0700703 new->nf_trace = old->nf_trace;
704#endif
705#ifdef CONFIG_NET_SCHED
706 new->tc_index = old->tc_index;
707#ifdef CONFIG_NET_CLS_ACT
708 new->tc_verd = old->tc_verd;
709#endif
710#endif
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000711 new->vlan_proto = old->vlan_proto;
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700712 new->vlan_tci = old->vlan_tci;
713
Herbert Xudec18812007-10-14 00:37:30 -0700714 skb_copy_secmark(new, old);
715}
716
Herbert Xu82c49a32009-05-22 22:11:37 +0000717/*
718 * You should not add any new code to this function. Add it to
719 * __copy_skb_header above instead.
720 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700721static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723#define C(x) n->x = skb->x
724
725 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700727 __copy_skb_header(n, skb);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 C(len);
730 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700731 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700732 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800733 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 C(tail);
737 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800738 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000739 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800740 C(data);
741 C(truesize);
742 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 atomic_inc(&(skb_shinfo(skb)->dataref));
745 skb->cloned = 1;
746
747 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700748#undef C
749}
750
751/**
752 * skb_morph - morph one skb into another
753 * @dst: the skb to receive the contents
754 * @src: the skb to supply the contents
755 *
756 * This is identical to skb_clone except that the target skb is
757 * supplied by the user.
758 *
759 * The target skb is returned upon exit.
760 */
761struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
762{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800763 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700764 return __skb_clone(dst, src);
765}
766EXPORT_SYMBOL_GPL(skb_morph);
767
Ben Hutchings2c530402012-07-10 10:55:09 +0000768/**
769 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000770 * @skb: the skb to modify
771 * @gfp_mask: allocation priority
772 *
773 * This must be called on SKBTX_DEV_ZEROCOPY skb.
774 * It will copy all frags into kernel and drop the reference
775 * to userspace pages.
776 *
777 * If this function is called from an interrupt gfp_mask() must be
778 * %GFP_ATOMIC.
779 *
780 * Returns 0 on success or a negative error code on failure
781 * to allocate kernel memory to copy to.
782 */
783int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000784{
785 int i;
786 int num_frags = skb_shinfo(skb)->nr_frags;
787 struct page *page, *head = NULL;
788 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
789
790 for (i = 0; i < num_frags; i++) {
791 u8 *vaddr;
792 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
793
Krishna Kumar02756ed2012-07-17 02:05:29 +0000794 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000795 if (!page) {
796 while (head) {
797 struct page *next = (struct page *)head->private;
798 put_page(head);
799 head = next;
800 }
801 return -ENOMEM;
802 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200803 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000804 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000805 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200806 kunmap_atomic(vaddr);
Shirley Maa6686f22011-07-06 12:22:12 +0000807 page->private = (unsigned long)head;
808 head = page;
809 }
810
811 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000812 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000813 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000814
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000815 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000816
817 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000818 for (i = num_frags - 1; i >= 0; i--) {
819 __skb_fill_page_desc(skb, i, head, 0,
820 skb_shinfo(skb)->frags[i].size);
Shirley Maa6686f22011-07-06 12:22:12 +0000821 head = (struct page *)head->private;
822 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000823
824 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000825 return 0;
826}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000827EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000828
Herbert Xue0053ec2007-10-14 00:37:52 -0700829/**
830 * skb_clone - duplicate an sk_buff
831 * @skb: buffer to clone
832 * @gfp_mask: allocation priority
833 *
834 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
835 * copies share the same packet data but not structure. The new
836 * buffer has a reference count of 1. If the allocation fails the
837 * function returns %NULL otherwise the new buffer is returned.
838 *
839 * If this function is called from an interrupt gfp_mask() must be
840 * %GFP_ATOMIC.
841 */
842
843struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
844{
845 struct sk_buff *n;
846
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000847 if (skb_orphan_frags(skb, gfp_mask))
848 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000849
Herbert Xue0053ec2007-10-14 00:37:52 -0700850 n = skb + 1;
851 if (skb->fclone == SKB_FCLONE_ORIG &&
852 n->fclone == SKB_FCLONE_UNAVAILABLE) {
853 atomic_t *fclone_ref = (atomic_t *) (n + 1);
854 n->fclone = SKB_FCLONE_CLONE;
855 atomic_inc(fclone_ref);
856 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700857 if (skb_pfmemalloc(skb))
858 gfp_mask |= __GFP_MEMALLOC;
859
Herbert Xue0053ec2007-10-14 00:37:52 -0700860 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
861 if (!n)
862 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200863
864 kmemcheck_annotate_bitfield(n, flags1);
865 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700866 n->fclone = SKB_FCLONE_UNAVAILABLE;
867 }
868
869 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800871EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000873static void skb_headers_offset_update(struct sk_buff *skb, int off)
874{
875 /* {transport,network,mac}_header and tail are relative to skb->head */
876 skb->transport_header += off;
877 skb->network_header += off;
878 if (skb_mac_header_was_set(skb))
879 skb->mac_header += off;
880 skb->inner_transport_header += off;
881 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000882 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
886{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700887#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
889 * Shift between the two data areas in bytes
890 */
891 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700892#endif
Herbert Xudec18812007-10-14 00:37:30 -0700893
894 __copy_skb_header(new, old);
895
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700896#ifndef NET_SKBUFF_DATA_USES_OFFSET
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000897 skb_headers_offset_update(new, offset);
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700898#endif
Herbert Xu79671682006-06-22 02:40:14 -0700899 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
900 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
901 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Mel Gormanc93bdd02012-07-31 16:44:19 -0700904static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
905{
906 if (skb_pfmemalloc(skb))
907 return SKB_ALLOC_RX;
908 return 0;
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/**
912 * skb_copy - create private copy of an sk_buff
913 * @skb: buffer to copy
914 * @gfp_mask: allocation priority
915 *
916 * Make a copy of both an &sk_buff and its data. This is used when the
917 * caller wishes to modify the data and needs a private copy of the
918 * data to alter. Returns %NULL on failure or the pointer to the buffer
919 * on success. The returned buffer has a reference count of 1.
920 *
921 * As by-product this function converts non-linear &sk_buff to linear
922 * one, so that &sk_buff becomes completely private and caller is allowed
923 * to modify all the data of returned buffer. This means that this
924 * function is not recommended for use in circumstances when only
925 * header is going to be modified. Use pskb_copy() instead.
926 */
927
Al Virodd0fc662005-10-07 07:46:04 +0100928struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000930 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000931 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700932 struct sk_buff *n = __alloc_skb(size, gfp_mask,
933 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (!n)
936 return NULL;
937
938 /* Set the data pointer */
939 skb_reserve(n, headerlen);
940 /* Set the tail pointer and length */
941 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
944 BUG();
945
946 copy_skb_header(n, skb);
947 return n;
948}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800949EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951/**
Eric Dumazet117632e2011-12-03 21:39:53 +0000952 * __pskb_copy - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000954 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * @gfp_mask: allocation priority
956 *
957 * Make a copy of both an &sk_buff and part of its data, located
958 * in header. Fragmented data remain shared. This is used when
959 * the caller wishes to modify only header of &sk_buff and needs
960 * private copy of the header to alter. Returns %NULL on failure
961 * or the pointer to the buffer on success.
962 * The returned buffer has a reference count of 1.
963 */
964
Eric Dumazet117632e2011-12-03 21:39:53 +0000965struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Eric Dumazet117632e2011-12-03 21:39:53 +0000967 unsigned int size = skb_headlen(skb) + headroom;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700968 struct sk_buff *n = __alloc_skb(size, gfp_mask,
969 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!n)
972 goto out;
973
974 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000975 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Set the tail pointer and length */
977 skb_put(n, skb_headlen(skb));
978 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300979 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Herbert Xu25f484a2006-11-07 14:57:15 -0800981 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 n->data_len = skb->data_len;
983 n->len = skb->len;
984
985 if (skb_shinfo(skb)->nr_frags) {
986 int i;
987
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000988 if (skb_orphan_frags(skb, gfp_mask)) {
989 kfree_skb(n);
990 n = NULL;
991 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +0000992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
994 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +0000995 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 skb_shinfo(n)->nr_frags = i;
998 }
999
David S. Miller21dc3302010-08-23 00:13:46 -07001000 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1002 skb_clone_fraglist(n);
1003 }
1004
1005 copy_skb_header(n, skb);
1006out:
1007 return n;
1008}
Eric Dumazet117632e2011-12-03 21:39:53 +00001009EXPORT_SYMBOL(__pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011/**
1012 * pskb_expand_head - reallocate header of &sk_buff
1013 * @skb: buffer to reallocate
1014 * @nhead: room to add at head
1015 * @ntail: room to add at tail
1016 * @gfp_mask: allocation priority
1017 *
1018 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1019 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1020 * reference count of 1. Returns zero in the case of success or error,
1021 * if expansion failed. In the last case, &sk_buff is not changed.
1022 *
1023 * All the pointers pointing into skb header may change and must be
1024 * reloaded after call to this function.
1025 */
1026
Victor Fusco86a76ca2005-07-08 14:57:47 -07001027int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001028 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 int i;
1031 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001032 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 long off;
1034
Herbert Xu4edd87a2008-10-01 07:09:38 -07001035 BUG_ON(nhead < 0);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (skb_shared(skb))
1038 BUG();
1039
1040 size = SKB_DATA_ALIGN(size);
1041
Mel Gormanc93bdd02012-07-31 16:44:19 -07001042 if (skb_pfmemalloc(skb))
1043 gfp_mask |= __GFP_MEMALLOC;
1044 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1045 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!data)
1047 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001048 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001051 * optimized for the cases when header is void.
1052 */
1053 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1054
1055 memcpy((struct skb_shared_info *)(data + size),
1056 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001057 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Alexander Duyck3e245912012-05-04 14:26:51 +00001059 /*
1060 * if shinfo is shared we must drop the old head gracefully, but if it
1061 * is not we can just drop the old head and let the existing refcount
1062 * be since all we did is relocate the values
1063 */
1064 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001065 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001066 if (skb_orphan_frags(skb, gfp_mask))
1067 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001068 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001069 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Eric Dumazet1fd63042010-09-02 23:09:32 +00001071 if (skb_has_frag_list(skb))
1072 skb_clone_fraglist(skb);
1073
1074 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001075 } else {
1076 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 off = (data + nhead) - skb->head;
1079
1080 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001081 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001083#ifdef NET_SKBUFF_DATA_USES_OFFSET
1084 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001085 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001086#else
1087 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001088#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001089 skb->tail += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001090 skb_headers_offset_update(skb, off);
Andrea Shepard00c5a982010-07-22 09:12:35 +00001091 /* Only adjust this if it actually is csum_start rather than csum */
1092 if (skb->ip_summed == CHECKSUM_PARTIAL)
1093 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001095 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 skb->nohdr = 0;
1097 atomic_set(&skb_shinfo(skb)->dataref, 1);
1098 return 0;
1099
Shirley Maa6686f22011-07-06 12:22:12 +00001100nofrags:
1101 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102nodata:
1103 return -ENOMEM;
1104}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001105EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107/* Make private copy of skb with writable head and some headroom */
1108
1109struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1110{
1111 struct sk_buff *skb2;
1112 int delta = headroom - skb_headroom(skb);
1113
1114 if (delta <= 0)
1115 skb2 = pskb_copy(skb, GFP_ATOMIC);
1116 else {
1117 skb2 = skb_clone(skb, GFP_ATOMIC);
1118 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1119 GFP_ATOMIC)) {
1120 kfree_skb(skb2);
1121 skb2 = NULL;
1122 }
1123 }
1124 return skb2;
1125}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001126EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128/**
1129 * skb_copy_expand - copy and expand sk_buff
1130 * @skb: buffer to copy
1131 * @newheadroom: new free bytes at head
1132 * @newtailroom: new free bytes at tail
1133 * @gfp_mask: allocation priority
1134 *
1135 * Make a copy of both an &sk_buff and its data and while doing so
1136 * allocate additional space.
1137 *
1138 * This is used when the caller wishes to modify the data and needs a
1139 * private copy of the data to alter as well as more space for new fields.
1140 * Returns %NULL on failure or the pointer to the buffer
1141 * on success. The returned buffer has a reference count of 1.
1142 *
1143 * You must pass %GFP_ATOMIC as the allocation priority if this function
1144 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
1146struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001147 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001148 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 /*
1151 * Allocate the copy buffer
1152 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001153 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1154 gfp_mask, skb_alloc_rx_flag(skb),
1155 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001156 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -07001158 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if (!n)
1161 return NULL;
1162
1163 skb_reserve(n, newheadroom);
1164
1165 /* Set the tail pointer and length */
1166 skb_put(n, skb->len);
1167
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001168 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 head_copy_off = 0;
1170 if (newheadroom <= head_copy_len)
1171 head_copy_len = newheadroom;
1172 else
1173 head_copy_off = newheadroom - head_copy_len;
1174
1175 /* Copy the linear header and data. */
1176 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1177 skb->len + head_copy_len))
1178 BUG();
1179
1180 copy_skb_header(n, skb);
1181
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001182 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -07001183 if (n->ip_summed == CHECKSUM_PARTIAL)
1184 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -07001185#ifdef NET_SKBUFF_DATA_USES_OFFSET
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001186 skb_headers_offset_update(n, off);
Herbert Xu52886052007-09-16 16:32:11 -07001187#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return n;
1190}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001191EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193/**
1194 * skb_pad - zero pad the tail of an skb
1195 * @skb: buffer to pad
1196 * @pad: space to pad
1197 *
1198 * Ensure that a buffer is followed by a padding area that is zero
1199 * filled. Used by network drivers which may DMA or transfer data
1200 * beyond the buffer end onto the wire.
1201 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001202 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001204
Herbert Xu5b057c62006-06-23 02:06:41 -07001205int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Herbert Xu5b057c62006-06-23 02:06:41 -07001207 int err;
1208 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001211 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001215
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001216 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001217 if (likely(skb_cloned(skb) || ntail > 0)) {
1218 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1219 if (unlikely(err))
1220 goto free_skb;
1221 }
1222
1223 /* FIXME: The use of this function with non-linear skb's really needs
1224 * to be audited.
1225 */
1226 err = skb_linearize(skb);
1227 if (unlikely(err))
1228 goto free_skb;
1229
1230 memset(skb->data + skb->len, 0, pad);
1231 return 0;
1232
1233free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001235 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001236}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001237EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001238
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001239/**
1240 * skb_put - add data to a buffer
1241 * @skb: buffer to use
1242 * @len: amount of data to add
1243 *
1244 * This function extends the used data area of the buffer. If this would
1245 * exceed the total buffer size the kernel will panic. A pointer to the
1246 * first byte of the extra data is returned.
1247 */
1248unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1249{
1250 unsigned char *tmp = skb_tail_pointer(skb);
1251 SKB_LINEAR_ASSERT(skb);
1252 skb->tail += len;
1253 skb->len += len;
1254 if (unlikely(skb->tail > skb->end))
1255 skb_over_panic(skb, len, __builtin_return_address(0));
1256 return tmp;
1257}
1258EXPORT_SYMBOL(skb_put);
1259
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001260/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001261 * skb_push - add data to the start of a buffer
1262 * @skb: buffer to use
1263 * @len: amount of data to add
1264 *
1265 * This function extends the used data area of the buffer at the buffer
1266 * start. If this would exceed the total buffer headroom the kernel will
1267 * panic. A pointer to the first byte of the extra data is returned.
1268 */
1269unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1270{
1271 skb->data -= len;
1272 skb->len += len;
1273 if (unlikely(skb->data<skb->head))
1274 skb_under_panic(skb, len, __builtin_return_address(0));
1275 return skb->data;
1276}
1277EXPORT_SYMBOL(skb_push);
1278
1279/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001280 * skb_pull - remove data from the start of a buffer
1281 * @skb: buffer to use
1282 * @len: amount of data to remove
1283 *
1284 * This function removes data from the start of a buffer, returning
1285 * the memory to the headroom. A pointer to the next data in the buffer
1286 * is returned. Once the data has been pulled future pushes will overwrite
1287 * the old data.
1288 */
1289unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1290{
David S. Miller47d29642010-05-02 02:21:44 -07001291 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001292}
1293EXPORT_SYMBOL(skb_pull);
1294
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001295/**
1296 * skb_trim - remove end from a buffer
1297 * @skb: buffer to alter
1298 * @len: new length
1299 *
1300 * Cut the length of a buffer down by removing data from the tail. If
1301 * the buffer is already under the length specified it is not modified.
1302 * The skb must be linear.
1303 */
1304void skb_trim(struct sk_buff *skb, unsigned int len)
1305{
1306 if (skb->len > len)
1307 __skb_trim(skb, len);
1308}
1309EXPORT_SYMBOL(skb_trim);
1310
Herbert Xu3cc0e872006-06-09 16:13:38 -07001311/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 */
1313
Herbert Xu3cc0e872006-06-09 16:13:38 -07001314int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Herbert Xu27b437c2006-07-13 19:26:39 -07001316 struct sk_buff **fragp;
1317 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int offset = skb_headlen(skb);
1319 int nfrags = skb_shinfo(skb)->nr_frags;
1320 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001321 int err;
1322
1323 if (skb_cloned(skb) &&
1324 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001327 i = 0;
1328 if (offset >= len)
1329 goto drop_pages;
1330
1331 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001332 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001333
1334 if (end < len) {
1335 offset = end;
1336 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001338
Eric Dumazet9e903e02011-10-18 21:00:24 +00001339 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001340
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001341drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001342 skb_shinfo(skb)->nr_frags = i;
1343
1344 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001345 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001346
David S. Miller21dc3302010-08-23 00:13:46 -07001347 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001348 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001349 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
Herbert Xu27b437c2006-07-13 19:26:39 -07001352 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1353 fragp = &frag->next) {
1354 int end = offset + frag->len;
1355
1356 if (skb_shared(frag)) {
1357 struct sk_buff *nfrag;
1358
1359 nfrag = skb_clone(frag, GFP_ATOMIC);
1360 if (unlikely(!nfrag))
1361 return -ENOMEM;
1362
1363 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001364 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001365 frag = nfrag;
1366 *fragp = frag;
1367 }
1368
1369 if (end < len) {
1370 offset = end;
1371 continue;
1372 }
1373
1374 if (end > len &&
1375 unlikely((err = pskb_trim(frag, len - offset))))
1376 return err;
1377
1378 if (frag->next)
1379 skb_drop_list(&frag->next);
1380 break;
1381 }
1382
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001383done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001384 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 skb->data_len -= skb->len - len;
1386 skb->len = len;
1387 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001388 skb->len = len;
1389 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001390 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 return 0;
1394}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001395EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397/**
1398 * __pskb_pull_tail - advance tail of skb header
1399 * @skb: buffer to reallocate
1400 * @delta: number of bytes to advance tail
1401 *
1402 * The function makes a sense only on a fragmented &sk_buff,
1403 * it expands header moving its tail forward and copying necessary
1404 * data from fragmented part.
1405 *
1406 * &sk_buff MUST have reference count of 1.
1407 *
1408 * Returns %NULL (and &sk_buff does not change) if pull failed
1409 * or value of new tail of skb in the case of success.
1410 *
1411 * All the pointers pointing into skb header may change and must be
1412 * reloaded after call to this function.
1413 */
1414
1415/* Moves tail of skb head forward, copying data from fragmented part,
1416 * when it is necessary.
1417 * 1. It may fail due to malloc failure.
1418 * 2. It may change skb pointers.
1419 *
1420 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1421 */
1422unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1423{
1424 /* If skb has not enough free space at tail, get new one
1425 * plus 128 bytes for future expansions. If we have enough
1426 * room at tail, reallocate without expansion only if skb is cloned.
1427 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001428 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (eat > 0 || skb_cloned(skb)) {
1431 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1432 GFP_ATOMIC))
1433 return NULL;
1434 }
1435
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001436 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 BUG();
1438
1439 /* Optimization: no fragments, no reasons to preestimate
1440 * size of pulled pages. Superb.
1441 */
David S. Miller21dc3302010-08-23 00:13:46 -07001442 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto pull_pages;
1444
1445 /* Estimate size of pulled pages. */
1446 eat = delta;
1447 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001448 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1449
1450 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001452 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454
1455 /* If we need update frag list, we are in troubles.
1456 * Certainly, it possible to add an offset to skb data,
1457 * but taking into account that pulling is expected to
1458 * be very rare operation, it is worth to fight against
1459 * further bloating skb head and crucify ourselves here instead.
1460 * Pure masohism, indeed. 8)8)
1461 */
1462 if (eat) {
1463 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1464 struct sk_buff *clone = NULL;
1465 struct sk_buff *insp = NULL;
1466
1467 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001468 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 if (list->len <= eat) {
1471 /* Eaten as whole. */
1472 eat -= list->len;
1473 list = list->next;
1474 insp = list;
1475 } else {
1476 /* Eaten partially. */
1477
1478 if (skb_shared(list)) {
1479 /* Sucks! We need to fork list. :-( */
1480 clone = skb_clone(list, GFP_ATOMIC);
1481 if (!clone)
1482 return NULL;
1483 insp = list->next;
1484 list = clone;
1485 } else {
1486 /* This may be pulled without
1487 * problems. */
1488 insp = list;
1489 }
1490 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001491 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return NULL;
1493 }
1494 break;
1495 }
1496 } while (eat);
1497
1498 /* Free pulled out fragments. */
1499 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1500 skb_shinfo(skb)->frag_list = list->next;
1501 kfree_skb(list);
1502 }
1503 /* And insert new clone at head. */
1504 if (clone) {
1505 clone->next = list;
1506 skb_shinfo(skb)->frag_list = clone;
1507 }
1508 }
1509 /* Success! Now we may commit changes to skb data. */
1510
1511pull_pages:
1512 eat = delta;
1513 k = 0;
1514 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001515 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1516
1517 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001518 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001519 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 } else {
1521 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1522 if (eat) {
1523 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001524 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 eat = 0;
1526 }
1527 k++;
1528 }
1529 }
1530 skb_shinfo(skb)->nr_frags = k;
1531
1532 skb->tail += delta;
1533 skb->data_len -= delta;
1534
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001535 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001537EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Eric Dumazet22019b12011-07-29 18:37:31 +00001539/**
1540 * skb_copy_bits - copy bits from skb to kernel buffer
1541 * @skb: source skb
1542 * @offset: offset in source
1543 * @to: destination buffer
1544 * @len: number of bytes to copy
1545 *
1546 * Copy the specified number of bytes from the source skb to the
1547 * destination buffer.
1548 *
1549 * CAUTION ! :
1550 * If its prototype is ever changed,
1551 * check arch/{*}/net/{*}.S files,
1552 * since it is called from BPF assembly code.
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1555{
David S. Miller1a028e52007-04-27 15:21:23 -07001556 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001557 struct sk_buff *frag_iter;
1558 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (offset > (int)skb->len - len)
1561 goto fault;
1562
1563 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001564 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (copy > len)
1566 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001567 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if ((len -= copy) == 0)
1569 return 0;
1570 offset += copy;
1571 to += copy;
1572 }
1573
1574 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001575 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001576 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001578 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001579
Eric Dumazet51c56b02012-04-05 11:35:15 +02001580 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if ((copy = end - offset) > 0) {
1582 u8 *vaddr;
1583
1584 if (copy > len)
1585 copy = len;
1586
Eric Dumazet51c56b02012-04-05 11:35:15 +02001587 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001589 vaddr + f->page_offset + offset - start,
1590 copy);
1591 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 if ((len -= copy) == 0)
1594 return 0;
1595 offset += copy;
1596 to += copy;
1597 }
David S. Miller1a028e52007-04-27 15:21:23 -07001598 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600
David S. Millerfbb398a2009-06-09 00:18:59 -07001601 skb_walk_frags(skb, frag_iter) {
1602 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
David S. Millerfbb398a2009-06-09 00:18:59 -07001604 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
David S. Millerfbb398a2009-06-09 00:18:59 -07001606 end = start + frag_iter->len;
1607 if ((copy = end - offset) > 0) {
1608 if (copy > len)
1609 copy = len;
1610 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1611 goto fault;
1612 if ((len -= copy) == 0)
1613 return 0;
1614 offset += copy;
1615 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001617 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Shirley Maa6686f22011-07-06 12:22:12 +00001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (!len)
1621 return 0;
1622
1623fault:
1624 return -EFAULT;
1625}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001626EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Jens Axboe9c55e012007-11-06 23:30:13 -08001628/*
1629 * Callback from splice_to_pipe(), if we need to release some pages
1630 * at the end of the spd in case we error'ed out in filling the pipe.
1631 */
1632static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1633{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001634 put_page(spd->pages[i]);
1635}
Jens Axboe9c55e012007-11-06 23:30:13 -08001636
David S. Millera108d5f2012-04-23 23:06:11 -04001637static struct page *linear_to_page(struct page *page, unsigned int *len,
1638 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001639 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001640{
Eric Dumazet5640f762012-09-23 23:04:42 +00001641 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001642
Eric Dumazet5640f762012-09-23 23:04:42 +00001643 if (!sk_page_frag_refill(sk, pfrag))
1644 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001645
Eric Dumazet5640f762012-09-23 23:04:42 +00001646 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001647
Eric Dumazet5640f762012-09-23 23:04:42 +00001648 memcpy(page_address(pfrag->page) + pfrag->offset,
1649 page_address(page) + *offset, *len);
1650 *offset = pfrag->offset;
1651 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001652
Eric Dumazet5640f762012-09-23 23:04:42 +00001653 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001654}
1655
Eric Dumazet41c73a02012-04-22 12:26:16 +00001656static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1657 struct page *page,
1658 unsigned int offset)
1659{
1660 return spd->nr_pages &&
1661 spd->pages[spd->nr_pages - 1] == page &&
1662 (spd->partial[spd->nr_pages - 1].offset +
1663 spd->partial[spd->nr_pages - 1].len == offset);
1664}
1665
Jens Axboe9c55e012007-11-06 23:30:13 -08001666/*
1667 * Fill page/offset/length into spd, if it can hold more pages.
1668 */
David S. Millera108d5f2012-04-23 23:06:11 -04001669static bool spd_fill_page(struct splice_pipe_desc *spd,
1670 struct pipe_inode_info *pipe, struct page *page,
1671 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001672 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001673 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001674{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001675 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001676 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001677
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001678 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001679 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001680 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001681 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001682 }
1683 if (spd_can_coalesce(spd, page, offset)) {
1684 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001685 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001686 }
1687 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001688 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001689 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001690 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001691 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001692
David S. Millera108d5f2012-04-23 23:06:11 -04001693 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001694}
1695
David S. Millera108d5f2012-04-23 23:06:11 -04001696static bool __splice_segment(struct page *page, unsigned int poff,
1697 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001698 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001699 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001700 struct sock *sk,
1701 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001702{
1703 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001704 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001705
1706 /* skip this segment if already processed */
1707 if (*off >= plen) {
1708 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001709 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001710 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001711
Octavian Purdila2870c432008-07-15 00:49:11 -07001712 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001713 poff += *off;
1714 plen -= *off;
1715 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001716
Eric Dumazet18aafc62013-01-11 14:46:37 +00001717 do {
1718 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001719
Eric Dumazet18aafc62013-01-11 14:46:37 +00001720 if (spd_fill_page(spd, pipe, page, &flen, poff,
1721 linear, sk))
1722 return true;
1723 poff += flen;
1724 plen -= flen;
1725 *len -= flen;
1726 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001727
David S. Millera108d5f2012-04-23 23:06:11 -04001728 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001729}
1730
1731/*
David S. Millera108d5f2012-04-23 23:06:11 -04001732 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001733 * pipe is full or if we already spliced the requested length.
1734 */
David S. Millera108d5f2012-04-23 23:06:11 -04001735static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1736 unsigned int *offset, unsigned int *len,
1737 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001738{
1739 int seg;
1740
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001741 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001742 * If skb->head_frag is set, this 'linear' part is backed by a
1743 * fragment, and if the head is not shared with any clones then
1744 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001745 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001746 if (__splice_segment(virt_to_page(skb->data),
1747 (unsigned long) skb->data & (PAGE_SIZE - 1),
1748 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001749 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001750 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001751 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001752 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001753
1754 /*
1755 * then map the fragments
1756 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001757 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1758 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1759
Ian Campbellea2ab692011-08-22 23:44:58 +00001760 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001761 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001762 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001763 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001764 }
1765
David S. Millera108d5f2012-04-23 23:06:11 -04001766 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001767}
1768
1769/*
1770 * Map data from the skb to a pipe. Should handle both the linear part,
1771 * the fragments, and the frag list. It does NOT handle frag lists within
1772 * the frag list, if such a thing exists. We'd probably need to recurse to
1773 * handle that cleanly.
1774 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001775int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001776 struct pipe_inode_info *pipe, unsigned int tlen,
1777 unsigned int flags)
1778{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001779 struct partial_page partial[MAX_SKB_FRAGS];
1780 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001781 struct splice_pipe_desc spd = {
1782 .pages = pages,
1783 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001784 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001785 .flags = flags,
Miklos Szeredid840f982014-01-22 19:36:57 +01001786 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001787 .spd_release = sock_spd_release,
1788 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001789 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001790 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001791 int ret = 0;
1792
Jens Axboe9c55e012007-11-06 23:30:13 -08001793 /*
1794 * __skb_splice_bits() only fails if the output has no room left,
1795 * so no point in going over the frag_list for the error case.
1796 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001797 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001798 goto done;
1799 else if (!tlen)
1800 goto done;
1801
1802 /*
1803 * now see if we have a frag_list to map
1804 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001805 skb_walk_frags(skb, frag_iter) {
1806 if (!tlen)
1807 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001808 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001809 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001810 }
1811
1812done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001813 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001814 /*
1815 * Drop the socket lock, otherwise we have reverse
1816 * locking dependencies between sk_lock and i_mutex
1817 * here as compared to sendfile(). We enter here
1818 * with the socket lock held, and splice_to_pipe() will
1819 * grab the pipe inode lock. For sendfile() emulation,
1820 * we call into ->sendpage() with the i_mutex lock held
1821 * and networking will grab the socket lock.
1822 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001823 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001824 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001825 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001826 }
1827
Jens Axboe35f3d142010-05-20 10:43:18 +02001828 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001829}
1830
Herbert Xu357b40a2005-04-19 22:30:14 -07001831/**
1832 * skb_store_bits - store bits from kernel buffer to skb
1833 * @skb: destination buffer
1834 * @offset: offset in destination
1835 * @from: source buffer
1836 * @len: number of bytes to copy
1837 *
1838 * Copy the specified number of bytes from the source buffer to the
1839 * destination skb. This function handles all the messy bits of
1840 * traversing fragment lists and such.
1841 */
1842
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001843int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001844{
David S. Miller1a028e52007-04-27 15:21:23 -07001845 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001846 struct sk_buff *frag_iter;
1847 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001848
1849 if (offset > (int)skb->len - len)
1850 goto fault;
1851
David S. Miller1a028e52007-04-27 15:21:23 -07001852 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001853 if (copy > len)
1854 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001855 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001856 if ((len -= copy) == 0)
1857 return 0;
1858 offset += copy;
1859 from += copy;
1860 }
1861
1862 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1863 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001864 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001865
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001866 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001867
Eric Dumazet9e903e02011-10-18 21:00:24 +00001868 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001869 if ((copy = end - offset) > 0) {
1870 u8 *vaddr;
1871
1872 if (copy > len)
1873 copy = len;
1874
Eric Dumazet51c56b02012-04-05 11:35:15 +02001875 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001876 memcpy(vaddr + frag->page_offset + offset - start,
1877 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001878 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001879
1880 if ((len -= copy) == 0)
1881 return 0;
1882 offset += copy;
1883 from += copy;
1884 }
David S. Miller1a028e52007-04-27 15:21:23 -07001885 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001886 }
1887
David S. Millerfbb398a2009-06-09 00:18:59 -07001888 skb_walk_frags(skb, frag_iter) {
1889 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001890
David S. Millerfbb398a2009-06-09 00:18:59 -07001891 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001892
David S. Millerfbb398a2009-06-09 00:18:59 -07001893 end = start + frag_iter->len;
1894 if ((copy = end - offset) > 0) {
1895 if (copy > len)
1896 copy = len;
1897 if (skb_store_bits(frag_iter, offset - start,
1898 from, copy))
1899 goto fault;
1900 if ((len -= copy) == 0)
1901 return 0;
1902 offset += copy;
1903 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001904 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001905 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001906 }
1907 if (!len)
1908 return 0;
1909
1910fault:
1911 return -EFAULT;
1912}
Herbert Xu357b40a2005-04-19 22:30:14 -07001913EXPORT_SYMBOL(skb_store_bits);
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915/* Checksum skb data. */
1916
Al Viro2bbbc862006-11-14 21:37:14 -08001917__wsum skb_checksum(const struct sk_buff *skb, int offset,
1918 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
David S. Miller1a028e52007-04-27 15:21:23 -07001920 int start = skb_headlen(skb);
1921 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001922 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int pos = 0;
1924
1925 /* Checksum header. */
1926 if (copy > 0) {
1927 if (copy > len)
1928 copy = len;
1929 csum = csum_partial(skb->data + offset, copy, csum);
1930 if ((len -= copy) == 0)
1931 return csum;
1932 offset += copy;
1933 pos = copy;
1934 }
1935
1936 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001937 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001938 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001940 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001941
Eric Dumazet51c56b02012-04-05 11:35:15 +02001942 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001944 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 if (copy > len)
1948 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001949 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001950 csum2 = csum_partial(vaddr + frag->page_offset +
1951 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001952 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 csum = csum_block_add(csum, csum2, pos);
1954 if (!(len -= copy))
1955 return csum;
1956 offset += copy;
1957 pos += copy;
1958 }
David S. Miller1a028e52007-04-27 15:21:23 -07001959 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
David S. Millerfbb398a2009-06-09 00:18:59 -07001962 skb_walk_frags(skb, frag_iter) {
1963 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
David S. Millerfbb398a2009-06-09 00:18:59 -07001965 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
David S. Millerfbb398a2009-06-09 00:18:59 -07001967 end = start + frag_iter->len;
1968 if ((copy = end - offset) > 0) {
1969 __wsum csum2;
1970 if (copy > len)
1971 copy = len;
1972 csum2 = skb_checksum(frag_iter, offset - start,
1973 copy, 0);
1974 csum = csum_block_add(csum, csum2, pos);
1975 if ((len -= copy) == 0)
1976 return csum;
1977 offset += copy;
1978 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001980 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001982 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 return csum;
1985}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001986EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988/* Both of above in one bottle. */
1989
Al Viro81d77662006-11-14 21:37:33 -08001990__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1991 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
David S. Miller1a028e52007-04-27 15:21:23 -07001993 int start = skb_headlen(skb);
1994 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001995 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int pos = 0;
1997
1998 /* Copy header. */
1999 if (copy > 0) {
2000 if (copy > len)
2001 copy = len;
2002 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2003 copy, csum);
2004 if ((len -= copy) == 0)
2005 return csum;
2006 offset += copy;
2007 to += copy;
2008 pos = copy;
2009 }
2010
2011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002012 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002014 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002015
Eric Dumazet9e903e02011-10-18 21:00:24 +00002016 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002018 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 u8 *vaddr;
2020 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2021
2022 if (copy > len)
2023 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002024 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002026 frag->page_offset +
2027 offset - start, to,
2028 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002029 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 csum = csum_block_add(csum, csum2, pos);
2031 if (!(len -= copy))
2032 return csum;
2033 offset += copy;
2034 to += copy;
2035 pos += copy;
2036 }
David S. Miller1a028e52007-04-27 15:21:23 -07002037 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039
David S. Millerfbb398a2009-06-09 00:18:59 -07002040 skb_walk_frags(skb, frag_iter) {
2041 __wsum csum2;
2042 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
David S. Millerfbb398a2009-06-09 00:18:59 -07002044 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
David S. Millerfbb398a2009-06-09 00:18:59 -07002046 end = start + frag_iter->len;
2047 if ((copy = end - offset) > 0) {
2048 if (copy > len)
2049 copy = len;
2050 csum2 = skb_copy_and_csum_bits(frag_iter,
2051 offset - start,
2052 to, copy, 0);
2053 csum = csum_block_add(csum, csum2, pos);
2054 if ((len -= copy) == 0)
2055 return csum;
2056 offset += copy;
2057 to += copy;
2058 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002060 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002062 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return csum;
2064}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002065EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2068{
Al Virod3bc23e2006-11-14 21:24:49 -08002069 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 long csstart;
2071
Patrick McHardy84fa7932006-08-29 16:44:56 -07002072 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002073 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 else
2075 csstart = skb_headlen(skb);
2076
Kris Katterjohn09a62662006-01-08 22:24:28 -08002077 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002079 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 csum = 0;
2082 if (csstart != skb->len)
2083 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2084 skb->len - csstart, 0);
2085
Patrick McHardy84fa7932006-08-29 16:44:56 -07002086 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002087 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Al Virod3bc23e2006-11-14 21:24:49 -08002089 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002092EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094/**
2095 * skb_dequeue - remove from the head of the queue
2096 * @list: list to dequeue from
2097 *
2098 * Remove the head of the list. The list lock is taken so the function
2099 * may be used safely with other locking list functions. The head item is
2100 * returned or %NULL if the list is empty.
2101 */
2102
2103struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2104{
2105 unsigned long flags;
2106 struct sk_buff *result;
2107
2108 spin_lock_irqsave(&list->lock, flags);
2109 result = __skb_dequeue(list);
2110 spin_unlock_irqrestore(&list->lock, flags);
2111 return result;
2112}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002113EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115/**
2116 * skb_dequeue_tail - remove from the tail of the queue
2117 * @list: list to dequeue from
2118 *
2119 * Remove the tail of the list. The list lock is taken so the function
2120 * may be used safely with other locking list functions. The tail item is
2121 * returned or %NULL if the list is empty.
2122 */
2123struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2124{
2125 unsigned long flags;
2126 struct sk_buff *result;
2127
2128 spin_lock_irqsave(&list->lock, flags);
2129 result = __skb_dequeue_tail(list);
2130 spin_unlock_irqrestore(&list->lock, flags);
2131 return result;
2132}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002133EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135/**
2136 * skb_queue_purge - empty a list
2137 * @list: list to empty
2138 *
2139 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2140 * the list and one reference dropped. This function takes the list
2141 * lock and is atomic with respect to other list locking functions.
2142 */
2143void skb_queue_purge(struct sk_buff_head *list)
2144{
2145 struct sk_buff *skb;
2146 while ((skb = skb_dequeue(list)) != NULL)
2147 kfree_skb(skb);
2148}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002149EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151/**
2152 * skb_queue_head - queue a buffer at the list head
2153 * @list: list to use
2154 * @newsk: buffer to queue
2155 *
2156 * Queue a buffer at the start of the list. This function takes the
2157 * list lock and can be used safely with other locking &sk_buff functions
2158 * safely.
2159 *
2160 * A buffer cannot be placed on two lists at the same time.
2161 */
2162void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2163{
2164 unsigned long flags;
2165
2166 spin_lock_irqsave(&list->lock, flags);
2167 __skb_queue_head(list, newsk);
2168 spin_unlock_irqrestore(&list->lock, flags);
2169}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002170EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172/**
2173 * skb_queue_tail - queue a buffer at the list tail
2174 * @list: list to use
2175 * @newsk: buffer to queue
2176 *
2177 * Queue a buffer at the tail of the list. This function takes the
2178 * list lock and can be used safely with other locking &sk_buff functions
2179 * safely.
2180 *
2181 * A buffer cannot be placed on two lists at the same time.
2182 */
2183void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2184{
2185 unsigned long flags;
2186
2187 spin_lock_irqsave(&list->lock, flags);
2188 __skb_queue_tail(list, newsk);
2189 spin_unlock_irqrestore(&list->lock, flags);
2190}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002191EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/**
2194 * skb_unlink - remove a buffer from a list
2195 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002196 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 *
David S. Miller8728b832005-08-09 19:25:21 -07002198 * Remove a packet from a list. The list locks are taken and this
2199 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 *
David S. Miller8728b832005-08-09 19:25:21 -07002201 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 */
David S. Miller8728b832005-08-09 19:25:21 -07002203void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
David S. Miller8728b832005-08-09 19:25:21 -07002205 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
David S. Miller8728b832005-08-09 19:25:21 -07002207 spin_lock_irqsave(&list->lock, flags);
2208 __skb_unlink(skb, list);
2209 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002211EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213/**
2214 * skb_append - append a buffer
2215 * @old: buffer to insert after
2216 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002217 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 *
2219 * Place a packet after a given packet in a list. The list locks are taken
2220 * and this function is atomic with respect to other list locked calls.
2221 * A buffer cannot be placed on two lists at the same time.
2222 */
David S. Miller8728b832005-08-09 19:25:21 -07002223void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 unsigned long flags;
2226
David S. Miller8728b832005-08-09 19:25:21 -07002227 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002228 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002229 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002231EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233/**
2234 * skb_insert - insert a buffer
2235 * @old: buffer to insert before
2236 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002237 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 *
David S. Miller8728b832005-08-09 19:25:21 -07002239 * Place a packet before a given packet in a list. The list locks are
2240 * taken and this function is atomic with respect to other list locked
2241 * calls.
2242 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * A buffer cannot be placed on two lists at the same time.
2244 */
David S. Miller8728b832005-08-09 19:25:21 -07002245void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 unsigned long flags;
2248
David S. Miller8728b832005-08-09 19:25:21 -07002249 spin_lock_irqsave(&list->lock, flags);
2250 __skb_insert(newsk, old->prev, old, list);
2251 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002253EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static inline void skb_split_inside_header(struct sk_buff *skb,
2256 struct sk_buff* skb1,
2257 const u32 len, const int pos)
2258{
2259 int i;
2260
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002261 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2262 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /* And move data appendix as is. */
2264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2265 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2266
2267 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2268 skb_shinfo(skb)->nr_frags = 0;
2269 skb1->data_len = skb->data_len;
2270 skb1->len += skb1->data_len;
2271 skb->data_len = 0;
2272 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002273 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
2276static inline void skb_split_no_header(struct sk_buff *skb,
2277 struct sk_buff* skb1,
2278 const u32 len, int pos)
2279{
2280 int i, k = 0;
2281 const int nfrags = skb_shinfo(skb)->nr_frags;
2282
2283 skb_shinfo(skb)->nr_frags = 0;
2284 skb1->len = skb1->data_len = skb->len - len;
2285 skb->len = len;
2286 skb->data_len = len - pos;
2287
2288 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002289 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 if (pos + size > len) {
2292 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2293
2294 if (pos < len) {
2295 /* Split frag.
2296 * We have two variants in this case:
2297 * 1. Move all the frag to the second
2298 * part, if it is possible. F.e.
2299 * this approach is mandatory for TUX,
2300 * where splitting is expensive.
2301 * 2. Split is accurately. We make this.
2302 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002303 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002305 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2306 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 skb_shinfo(skb)->nr_frags++;
2308 }
2309 k++;
2310 } else
2311 skb_shinfo(skb)->nr_frags++;
2312 pos += size;
2313 }
2314 skb_shinfo(skb1)->nr_frags = k;
2315}
2316
2317/**
2318 * skb_split - Split fragmented skb to two parts at length len.
2319 * @skb: the buffer to split
2320 * @skb1: the buffer to receive the second part
2321 * @len: new length for skb
2322 */
2323void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2324{
2325 int pos = skb_headlen(skb);
2326
Amerigo Wang68534c62013-02-19 22:51:30 +00002327 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (len < pos) /* Split line is inside header. */
2329 skb_split_inside_header(skb, skb1, len, pos);
2330 else /* Second chunk has no header, nothing to copy. */
2331 skb_split_no_header(skb, skb1, len, pos);
2332}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002333EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002335/* Shifting from/to a cloned skb is a no-go.
2336 *
2337 * Caller cannot keep skb_shinfo related pointers past calling here!
2338 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002339static int skb_prepare_for_shift(struct sk_buff *skb)
2340{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002341 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002342}
2343
2344/**
2345 * skb_shift - Shifts paged data partially from skb to another
2346 * @tgt: buffer into which tail data gets added
2347 * @skb: buffer from which the paged data comes from
2348 * @shiftlen: shift up to this many bytes
2349 *
2350 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002351 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002352 * It's up to caller to free skb if everything was shifted.
2353 *
2354 * If @tgt runs out of frags, the whole operation is aborted.
2355 *
2356 * Skb cannot include anything else but paged data while tgt is allowed
2357 * to have non-paged data as well.
2358 *
2359 * TODO: full sized shift could be optimized but that would need
2360 * specialized skb free'er to handle frags without up-to-date nr_frags.
2361 */
2362int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2363{
2364 int from, to, merge, todo;
2365 struct skb_frag_struct *fragfrom, *fragto;
2366
2367 BUG_ON(shiftlen > skb->len);
2368 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2369
2370 todo = shiftlen;
2371 from = 0;
2372 to = skb_shinfo(tgt)->nr_frags;
2373 fragfrom = &skb_shinfo(skb)->frags[from];
2374
2375 /* Actual merge is delayed until the point when we know we can
2376 * commit all, so that we don't have to undo partial changes
2377 */
2378 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002379 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2380 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002381 merge = -1;
2382 } else {
2383 merge = to - 1;
2384
Eric Dumazet9e903e02011-10-18 21:00:24 +00002385 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002386 if (todo < 0) {
2387 if (skb_prepare_for_shift(skb) ||
2388 skb_prepare_for_shift(tgt))
2389 return 0;
2390
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002391 /* All previous frag pointers might be stale! */
2392 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002393 fragto = &skb_shinfo(tgt)->frags[merge];
2394
Eric Dumazet9e903e02011-10-18 21:00:24 +00002395 skb_frag_size_add(fragto, shiftlen);
2396 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002397 fragfrom->page_offset += shiftlen;
2398
2399 goto onlymerged;
2400 }
2401
2402 from++;
2403 }
2404
2405 /* Skip full, not-fitting skb to avoid expensive operations */
2406 if ((shiftlen == skb->len) &&
2407 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2408 return 0;
2409
2410 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2411 return 0;
2412
2413 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2414 if (to == MAX_SKB_FRAGS)
2415 return 0;
2416
2417 fragfrom = &skb_shinfo(skb)->frags[from];
2418 fragto = &skb_shinfo(tgt)->frags[to];
2419
Eric Dumazet9e903e02011-10-18 21:00:24 +00002420 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002421 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002422 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002423 from++;
2424 to++;
2425
2426 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002427 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002428 fragto->page = fragfrom->page;
2429 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002430 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002431
2432 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002433 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002434 todo = 0;
2435
2436 to++;
2437 break;
2438 }
2439 }
2440
2441 /* Ready to "commit" this state change to tgt */
2442 skb_shinfo(tgt)->nr_frags = to;
2443
2444 if (merge >= 0) {
2445 fragfrom = &skb_shinfo(skb)->frags[0];
2446 fragto = &skb_shinfo(tgt)->frags[merge];
2447
Eric Dumazet9e903e02011-10-18 21:00:24 +00002448 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002449 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002450 }
2451
2452 /* Reposition in the original skb */
2453 to = 0;
2454 while (from < skb_shinfo(skb)->nr_frags)
2455 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2456 skb_shinfo(skb)->nr_frags = to;
2457
2458 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2459
2460onlymerged:
2461 /* Most likely the tgt won't ever need its checksum anymore, skb on
2462 * the other hand might need it if it needs to be resent
2463 */
2464 tgt->ip_summed = CHECKSUM_PARTIAL;
2465 skb->ip_summed = CHECKSUM_PARTIAL;
2466
2467 /* Yak, is it really working this way? Some helper please? */
2468 skb->len -= shiftlen;
2469 skb->data_len -= shiftlen;
2470 skb->truesize -= shiftlen;
2471 tgt->len += shiftlen;
2472 tgt->data_len += shiftlen;
2473 tgt->truesize += shiftlen;
2474
2475 return shiftlen;
2476}
2477
Thomas Graf677e90e2005-06-23 20:59:51 -07002478/**
2479 * skb_prepare_seq_read - Prepare a sequential read of skb data
2480 * @skb: the buffer to read
2481 * @from: lower offset of data to be read
2482 * @to: upper offset of data to be read
2483 * @st: state variable
2484 *
2485 * Initializes the specified state variable. Must be called before
2486 * invoking skb_seq_read() for the first time.
2487 */
2488void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2489 unsigned int to, struct skb_seq_state *st)
2490{
2491 st->lower_offset = from;
2492 st->upper_offset = to;
2493 st->root_skb = st->cur_skb = skb;
2494 st->frag_idx = st->stepped_offset = 0;
2495 st->frag_data = NULL;
2496}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002497EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002498
2499/**
2500 * skb_seq_read - Sequentially read skb data
2501 * @consumed: number of bytes consumed by the caller so far
2502 * @data: destination pointer for data to be returned
2503 * @st: state variable
2504 *
2505 * Reads a block of skb data at &consumed relative to the
2506 * lower offset specified to skb_prepare_seq_read(). Assigns
2507 * the head of the data block to &data and returns the length
2508 * of the block or 0 if the end of the skb data or the upper
2509 * offset has been reached.
2510 *
2511 * The caller is not required to consume all of the data
2512 * returned, i.e. &consumed is typically set to the number
2513 * of bytes already consumed and the next call to
2514 * skb_seq_read() will return the remaining part of the block.
2515 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002516 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002517 * this limitation is the cost for zerocopy seqeuental
2518 * reads of potentially non linear data.
2519 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002520 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002521 * at the moment, state->root_skb could be replaced with
2522 * a stack for this purpose.
2523 */
2524unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2525 struct skb_seq_state *st)
2526{
2527 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2528 skb_frag_t *frag;
2529
2530 if (unlikely(abs_offset >= st->upper_offset))
2531 return 0;
2532
2533next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002534 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002535
Thomas Chenault995b3372009-05-18 21:43:27 -07002536 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002537 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002538 return block_limit - abs_offset;
2539 }
2540
2541 if (st->frag_idx == 0 && !st->frag_data)
2542 st->stepped_offset += skb_headlen(st->cur_skb);
2543
2544 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2545 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002546 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002547
2548 if (abs_offset < block_limit) {
2549 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002550 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002551
2552 *data = (u8 *) st->frag_data + frag->page_offset +
2553 (abs_offset - st->stepped_offset);
2554
2555 return block_limit - abs_offset;
2556 }
2557
2558 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002559 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002560 st->frag_data = NULL;
2561 }
2562
2563 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002564 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002565 }
2566
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002567 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002568 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002569 st->frag_data = NULL;
2570 }
2571
David S. Miller21dc3302010-08-23 00:13:46 -07002572 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002573 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002574 st->frag_idx = 0;
2575 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002576 } else if (st->cur_skb->next) {
2577 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002578 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002579 goto next_skb;
2580 }
2581
2582 return 0;
2583}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002584EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002585
2586/**
2587 * skb_abort_seq_read - Abort a sequential read of skb data
2588 * @st: state variable
2589 *
2590 * Must be called if skb_seq_read() was not called until it
2591 * returned 0.
2592 */
2593void skb_abort_seq_read(struct skb_seq_state *st)
2594{
2595 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002596 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002597}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002598EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002599
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002600#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2601
2602static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2603 struct ts_config *conf,
2604 struct ts_state *state)
2605{
2606 return skb_seq_read(offset, text, TS_SKB_CB(state));
2607}
2608
2609static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2610{
2611 skb_abort_seq_read(TS_SKB_CB(state));
2612}
2613
2614/**
2615 * skb_find_text - Find a text pattern in skb data
2616 * @skb: the buffer to look in
2617 * @from: search offset
2618 * @to: search limit
2619 * @config: textsearch configuration
2620 * @state: uninitialized textsearch state variable
2621 *
2622 * Finds a pattern in the skb data according to the specified
2623 * textsearch configuration. Use textsearch_next() to retrieve
2624 * subsequent occurrences of the pattern. Returns the offset
2625 * to the first occurrence or UINT_MAX if no match was found.
2626 */
2627unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2628 unsigned int to, struct ts_config *config,
2629 struct ts_state *state)
2630{
Phil Oesterf72b9482006-06-26 00:00:57 -07002631 unsigned int ret;
2632
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002633 config->get_next_block = skb_ts_get_next_block;
2634 config->finish = skb_ts_finish;
2635
2636 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2637
Phil Oesterf72b9482006-06-26 00:00:57 -07002638 ret = textsearch_find(config, state);
2639 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002640}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002641EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002642
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002643/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002644 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002645 * @sk: sock structure
2646 * @skb: skb structure to be appened with user data.
2647 * @getfrag: call back function to be used for getting the user data
2648 * @from: pointer to user message iov
2649 * @length: length of the iov message
2650 *
2651 * Description: This procedure append the user data in the fragment part
2652 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2653 */
2654int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002655 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002656 int len, int odd, struct sk_buff *skb),
2657 void *from, int length)
2658{
Eric Dumazetb2111722012-12-28 06:06:37 +00002659 int frg_cnt = skb_shinfo(skb)->nr_frags;
2660 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002661 int offset = 0;
2662 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002663 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002664
2665 do {
2666 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002667 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002668 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002669
Eric Dumazetb2111722012-12-28 06:06:37 +00002670 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002671 return -ENOMEM;
2672
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002673 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002674 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002675
Eric Dumazetb2111722012-12-28 06:06:37 +00002676 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2677 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002678 if (ret < 0)
2679 return -EFAULT;
2680
2681 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002682 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2683 copy);
2684 frg_cnt++;
2685 pfrag->offset += copy;
2686 get_page(pfrag->page);
2687
2688 skb->truesize += copy;
2689 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002690 skb->len += copy;
2691 skb->data_len += copy;
2692 offset += copy;
2693 length -= copy;
2694
2695 } while (length > 0);
2696
2697 return 0;
2698}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002699EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002700
Herbert Xucbb042f2006-03-20 22:43:56 -08002701/**
2702 * skb_pull_rcsum - pull skb and update receive checksum
2703 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002704 * @len: length of data pulled
2705 *
2706 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002707 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002708 * receive path processing instead of skb_pull unless you know
2709 * that the checksum difference is zero (e.g., a valid IP header)
2710 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002711 */
2712unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2713{
2714 BUG_ON(len > skb->len);
2715 skb->len -= len;
2716 BUG_ON(skb->len < skb->data_len);
2717 skb_postpull_rcsum(skb, skb->data, len);
2718 return skb->data += len;
2719}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002720EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2721
Herbert Xuf4c50d92006-06-22 03:02:40 -07002722/**
2723 * skb_segment - Perform protocol segmentation on skb.
2724 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002725 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002726 *
2727 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002728 * a pointer to the first in a list of new skbs for the segments.
2729 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002730 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002731struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002732{
2733 struct sk_buff *segs = NULL;
2734 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002735 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002736 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002737 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002738 unsigned int offset = doffset;
Pravin B Shelar68c33162013-02-14 14:02:41 +00002739 unsigned int tnl_hlen = skb_tnl_header_len(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002740 unsigned int headroom;
2741 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002742 __be16 proto;
2743 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002744 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002745 int nfrags = skb_shinfo(skb)->nr_frags;
2746 int err = -ENOMEM;
2747 int i = 0;
2748 int pos;
2749
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002750 proto = skb_network_protocol(skb);
2751 if (unlikely(!proto))
2752 return ERR_PTR(-EINVAL);
2753
2754 csum = !!can_checksum_protocol(features, proto);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002755 __skb_push(skb, doffset);
2756 headroom = skb_headroom(skb);
2757 pos = skb_headlen(skb);
2758
2759 do {
2760 struct sk_buff *nskb;
2761 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002762 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002763 int size;
2764
2765 len = skb->len - offset;
2766 if (len > mss)
2767 len = mss;
2768
2769 hsize = skb_headlen(skb) - offset;
2770 if (hsize < 0)
2771 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002772 if (hsize > len || !sg)
2773 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002774
Herbert Xu89319d382008-12-15 23:26:06 -08002775 if (!hsize && i >= nfrags) {
2776 BUG_ON(fskb->len != len);
2777
2778 pos += len;
2779 nskb = skb_clone(fskb, GFP_ATOMIC);
2780 fskb = fskb->next;
2781
2782 if (unlikely(!nskb))
2783 goto err;
2784
Alexander Duyckec47ea82012-05-04 14:26:56 +00002785 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002786 if (skb_cow_head(nskb, doffset + headroom)) {
2787 kfree_skb(nskb);
2788 goto err;
2789 }
2790
Alexander Duyckec47ea82012-05-04 14:26:56 +00002791 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002792 skb_release_head_state(nskb);
2793 __skb_push(nskb, doffset);
2794 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002795 nskb = __alloc_skb(hsize + doffset + headroom,
2796 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2797 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002798
2799 if (unlikely(!nskb))
2800 goto err;
2801
2802 skb_reserve(nskb, headroom);
2803 __skb_put(nskb, doffset);
2804 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002805
2806 if (segs)
2807 tail->next = nskb;
2808 else
2809 segs = nskb;
2810 tail = nskb;
2811
Herbert Xu6f85a122008-08-15 14:55:02 -07002812 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002813
Eric Dumazet3d3be432010-09-01 00:50:51 +00002814 /* nskb and skb might have different headroom */
2815 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2816 nskb->csum_start += skb_headroom(nskb) - headroom;
2817
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002818 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002819 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002820 nskb->transport_header = (nskb->network_header +
2821 skb_network_header_len(skb));
Vlad Yasevichc290a4e2014-07-31 10:33:06 -04002822 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00002823
2824 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
2825 nskb->data - tnl_hlen,
2826 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08002827
Herbert Xu2f181852009-03-28 23:39:18 -07002828 if (fskb != skb_shinfo(skb)->frag_list)
Simon Horman1e42fa02013-05-19 15:46:49 +00002829 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08002830
Herbert Xuf4c50d92006-06-22 03:02:40 -07002831 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002832 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002833 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2834 skb_put(nskb, len),
2835 len, 0);
2836 continue;
2837 }
2838
2839 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002840
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002841 skb_copy_from_linear_data_offset(skb, offset,
2842 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002843
Pravin B Shelarc9af6db2013-02-11 09:27:41 +00002844 skb_shinfo(nskb)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00002845
Herbert Xu89319d382008-12-15 23:26:06 -08002846 while (pos < offset + len && i < nfrags) {
Michael S. Tsirkinb1246952014-03-10 19:28:08 +02002847 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
2848 goto err;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002849 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002850 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002851 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002852
2853 if (pos < offset) {
2854 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002855 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002856 }
2857
Herbert Xu89319d382008-12-15 23:26:06 -08002858 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002859
2860 if (pos + size <= offset + len) {
2861 i++;
2862 pos += size;
2863 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002864 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002865 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002866 }
2867
2868 frag++;
2869 }
2870
Herbert Xu89319d382008-12-15 23:26:06 -08002871 if (pos < offset + len) {
2872 struct sk_buff *fskb2 = fskb;
2873
2874 BUG_ON(pos + fskb->len != offset + len);
2875
2876 pos += fskb->len;
2877 fskb = fskb->next;
2878
2879 if (fskb2->next) {
2880 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2881 if (!fskb2)
2882 goto err;
2883 } else
2884 skb_get(fskb2);
2885
David S. Millerfbb398a2009-06-09 00:18:59 -07002886 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002887 skb_shinfo(nskb)->frag_list = fskb2;
2888 }
2889
2890skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002891 nskb->data_len = len - hsize;
2892 nskb->len += nskb->data_len;
2893 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002894
Simon Horman1e42fa02013-05-19 15:46:49 +00002895perform_csum_check:
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002896 if (!csum) {
2897 nskb->csum = skb_checksum(nskb, doffset,
2898 nskb->len - doffset, 0);
2899 nskb->ip_summed = CHECKSUM_NONE;
2900 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002901 } while ((offset += len) < skb->len);
2902
2903 return segs;
2904
2905err:
2906 while ((skb = segs)) {
2907 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002908 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002909 }
2910 return ERR_PTR(err);
2911}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002912EXPORT_SYMBOL_GPL(skb_segment);
2913
Herbert Xu71d93b32008-12-15 23:42:33 -08002914int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2915{
2916 struct sk_buff *p = *head;
2917 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002918 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2919 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002920 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002921 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002922 unsigned int offset = skb_gro_offset(skb);
2923 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002924 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002925
Herbert Xu86911732009-01-29 14:19:50 +00002926 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002927 return -E2BIG;
2928
Herbert Xu9aaa1562009-05-26 18:50:33 +00002929 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002930 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002931 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002932 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002933 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002934 int i = skbinfo->nr_frags;
2935 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002936
Herbert Xu66e92fc2009-05-26 18:50:32 +00002937 offset -= headlen;
2938
2939 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002940 return -E2BIG;
2941
Herbert Xu9aaa1562009-05-26 18:50:33 +00002942 pinfo->nr_frags = nr_frags;
2943 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002944
Herbert Xu9aaa1562009-05-26 18:50:33 +00002945 frag = pinfo->frags + nr_frags;
2946 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002947 do {
2948 *--frag = *--frag2;
2949 } while (--i);
2950
2951 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002952 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002953
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002954 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002955 delta_truesize = skb->truesize -
2956 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002957
Herbert Xuf5572062009-01-14 20:40:03 -08002958 skb->truesize -= skb->data_len;
2959 skb->len -= skb->data_len;
2960 skb->data_len = 0;
2961
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002962 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08002963 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00002964 } else if (skb->head_frag) {
2965 int nr_frags = pinfo->nr_frags;
2966 skb_frag_t *frag = pinfo->frags + nr_frags;
2967 struct page *page = virt_to_head_page(skb->head);
2968 unsigned int first_size = headlen - offset;
2969 unsigned int first_offset;
2970
2971 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2972 return -E2BIG;
2973
2974 first_offset = skb->data -
2975 (unsigned char *)page_address(page) +
2976 offset;
2977
2978 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2979
2980 frag->page.p = page;
2981 frag->page_offset = first_offset;
2982 skb_frag_size_set(frag, first_size);
2983
2984 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2985 /* We dont need to clear skbinfo->nr_frags here */
2986
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002987 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00002988 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2989 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08002990 } else if (skb_gro_len(p) != pinfo->gso_size)
2991 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08002992
2993 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00002994 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08002995 if (unlikely(!nskb))
2996 return -ENOMEM;
2997
2998 __copy_skb_header(nskb, p);
2999 nskb->mac_len = p->mac_len;
3000
3001 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003002 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003003
Herbert Xu86911732009-01-29 14:19:50 +00003004 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003005 skb_set_network_header(nskb, skb_network_offset(p));
3006 skb_set_transport_header(nskb, skb_transport_offset(p));
3007
Herbert Xu86911732009-01-29 14:19:50 +00003008 __skb_pull(p, skb_gro_offset(p));
3009 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3010 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003011
Herbert Xu71d93b32008-12-15 23:42:33 -08003012 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003013 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003014 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003015 skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003016 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003017
3018 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003019 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003020 nskb->len += p->len;
3021
3022 *head = nskb;
3023 nskb->next = p->next;
3024 p->next = NULL;
3025
3026 p = nskb;
3027
3028merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003029 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003030 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003031 unsigned int eat = offset - headlen;
3032
3033 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003034 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003035 skb->data_len -= eat;
3036 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003037 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003038 }
3039
Herbert Xu67147ba2009-05-26 18:50:22 +00003040 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003041
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003042 NAPI_GRO_CB(p)->last->next = skb;
3043 NAPI_GRO_CB(p)->last = skb;
Herbert Xu71d93b32008-12-15 23:42:33 -08003044 skb_header_release(skb);
3045
Herbert Xu5d38a072009-01-04 16:13:40 -08003046done:
3047 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003048 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003049 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003050 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003051
3052 NAPI_GRO_CB(skb)->same_flow = 1;
3053 return 0;
3054}
3055EXPORT_SYMBOL_GPL(skb_gro_receive);
3056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057void __init skb_init(void)
3058{
3059 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3060 sizeof(struct sk_buff),
3061 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003062 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003063 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003064 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3065 (2*sizeof(struct sk_buff)) +
3066 sizeof(atomic_t),
3067 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003068 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003069 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
David Howells716ea3a2007-04-02 20:19:53 -07003072/**
3073 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3074 * @skb: Socket buffer containing the buffers to be mapped
3075 * @sg: The scatter-gather list to map into
3076 * @offset: The offset into the buffer's contents to start mapping
3077 * @len: Length of buffer space to be mapped
3078 *
3079 * Fill the specified scatter-gather list with mappings/pointers into a
3080 * region of the buffer space attached to a socket buffer.
3081 */
David S. Miller51c739d2007-10-30 21:29:29 -07003082static int
3083__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003084{
David S. Miller1a028e52007-04-27 15:21:23 -07003085 int start = skb_headlen(skb);
3086 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003087 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003088 int elt = 0;
3089
3090 if (copy > 0) {
3091 if (copy > len)
3092 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003093 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003094 elt++;
3095 if ((len -= copy) == 0)
3096 return elt;
3097 offset += copy;
3098 }
3099
3100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003101 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003102
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003103 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003104
Eric Dumazet9e903e02011-10-18 21:00:24 +00003105 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003106 if ((copy = end - offset) > 0) {
3107 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3108
3109 if (copy > len)
3110 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003111 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003112 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003113 elt++;
3114 if (!(len -= copy))
3115 return elt;
3116 offset += copy;
3117 }
David S. Miller1a028e52007-04-27 15:21:23 -07003118 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003119 }
3120
David S. Millerfbb398a2009-06-09 00:18:59 -07003121 skb_walk_frags(skb, frag_iter) {
3122 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003123
David S. Millerfbb398a2009-06-09 00:18:59 -07003124 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003125
David S. Millerfbb398a2009-06-09 00:18:59 -07003126 end = start + frag_iter->len;
3127 if ((copy = end - offset) > 0) {
3128 if (copy > len)
3129 copy = len;
3130 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3131 copy);
3132 if ((len -= copy) == 0)
3133 return elt;
3134 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003135 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003136 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003137 }
3138 BUG_ON(len);
3139 return elt;
3140}
3141
David S. Miller51c739d2007-10-30 21:29:29 -07003142int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3143{
3144 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3145
Jens Axboec46f2332007-10-31 12:06:37 +01003146 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003147
3148 return nsg;
3149}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003150EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003151
David Howells716ea3a2007-04-02 20:19:53 -07003152/**
3153 * skb_cow_data - Check that a socket buffer's data buffers are writable
3154 * @skb: The socket buffer to check.
3155 * @tailbits: Amount of trailing space to be added
3156 * @trailer: Returned pointer to the skb where the @tailbits space begins
3157 *
3158 * Make sure that the data buffers attached to a socket buffer are
3159 * writable. If they are not, private copies are made of the data buffers
3160 * and the socket buffer is set to use these instead.
3161 *
3162 * If @tailbits is given, make sure that there is space to write @tailbits
3163 * bytes of data beyond current end of socket buffer. @trailer will be
3164 * set to point to the skb in which this space begins.
3165 *
3166 * The number of scatterlist elements required to completely map the
3167 * COW'd and extended socket buffer will be returned.
3168 */
3169int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3170{
3171 int copyflag;
3172 int elt;
3173 struct sk_buff *skb1, **skb_p;
3174
3175 /* If skb is cloned or its head is paged, reallocate
3176 * head pulling out all the pages (pages are considered not writable
3177 * at the moment even if they are anonymous).
3178 */
3179 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3180 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3181 return -ENOMEM;
3182
3183 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003184 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003185 /* A little of trouble, not enough of space for trailer.
3186 * This should not happen, when stack is tuned to generate
3187 * good frames. OK, on miss we reallocate and reserve even more
3188 * space, 128 bytes is fair. */
3189
3190 if (skb_tailroom(skb) < tailbits &&
3191 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3192 return -ENOMEM;
3193
3194 /* Voila! */
3195 *trailer = skb;
3196 return 1;
3197 }
3198
3199 /* Misery. We are in troubles, going to mincer fragments... */
3200
3201 elt = 1;
3202 skb_p = &skb_shinfo(skb)->frag_list;
3203 copyflag = 0;
3204
3205 while ((skb1 = *skb_p) != NULL) {
3206 int ntail = 0;
3207
3208 /* The fragment is partially pulled by someone,
3209 * this can happen on input. Copy it and everything
3210 * after it. */
3211
3212 if (skb_shared(skb1))
3213 copyflag = 1;
3214
3215 /* If the skb is the last, worry about trailer. */
3216
3217 if (skb1->next == NULL && tailbits) {
3218 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003219 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003220 skb_tailroom(skb1) < tailbits)
3221 ntail = tailbits + 128;
3222 }
3223
3224 if (copyflag ||
3225 skb_cloned(skb1) ||
3226 ntail ||
3227 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003228 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003229 struct sk_buff *skb2;
3230
3231 /* Fuck, we are miserable poor guys... */
3232 if (ntail == 0)
3233 skb2 = skb_copy(skb1, GFP_ATOMIC);
3234 else
3235 skb2 = skb_copy_expand(skb1,
3236 skb_headroom(skb1),
3237 ntail,
3238 GFP_ATOMIC);
3239 if (unlikely(skb2 == NULL))
3240 return -ENOMEM;
3241
3242 if (skb1->sk)
3243 skb_set_owner_w(skb2, skb1->sk);
3244
3245 /* Looking around. Are we still alive?
3246 * OK, link new skb, drop old one */
3247
3248 skb2->next = skb1->next;
3249 *skb_p = skb2;
3250 kfree_skb(skb1);
3251 skb1 = skb2;
3252 }
3253 elt++;
3254 *trailer = skb1;
3255 skb_p = &skb1->next;
3256 }
3257
3258 return elt;
3259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003260EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003261
Eric Dumazetb1faf562010-05-31 23:44:05 -07003262static void sock_rmem_free(struct sk_buff *skb)
3263{
3264 struct sock *sk = skb->sk;
3265
3266 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3267}
3268
3269/*
3270 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3271 */
3272int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3273{
Eric Dumazet110c4332012-04-06 10:49:10 +02003274 int len = skb->len;
3275
Eric Dumazetb1faf562010-05-31 23:44:05 -07003276 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003277 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003278 return -ENOMEM;
3279
3280 skb_orphan(skb);
3281 skb->sk = sk;
3282 skb->destructor = sock_rmem_free;
3283 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3284
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003285 /* before exiting rcu section, make sure dst is refcounted */
3286 skb_dst_force(skb);
3287
Eric Dumazetb1faf562010-05-31 23:44:05 -07003288 skb_queue_tail(&sk->sk_error_queue, skb);
3289 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003290 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003291 return 0;
3292}
3293EXPORT_SYMBOL(sock_queue_err_skb);
3294
Patrick Ohlyac45f602009-02-12 05:03:37 +00003295void skb_tstamp_tx(struct sk_buff *orig_skb,
3296 struct skb_shared_hwtstamps *hwtstamps)
3297{
3298 struct sock *sk = orig_skb->sk;
3299 struct sock_exterr_skb *serr;
3300 struct sk_buff *skb;
3301 int err;
3302
3303 if (!sk)
3304 return;
3305
Patrick Ohlyac45f602009-02-12 05:03:37 +00003306 if (hwtstamps) {
Willem de Bruijn2e313962013-04-23 00:39:28 +00003307 *skb_hwtstamps(orig_skb) =
Patrick Ohlyac45f602009-02-12 05:03:37 +00003308 *hwtstamps;
3309 } else {
3310 /*
3311 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003312 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003313 * store software time stamp
3314 */
Willem de Bruijn2e313962013-04-23 00:39:28 +00003315 orig_skb->tstamp = ktime_get_real();
Patrick Ohlyac45f602009-02-12 05:03:37 +00003316 }
3317
Willem de Bruijn2e313962013-04-23 00:39:28 +00003318 skb = skb_clone(orig_skb, GFP_ATOMIC);
3319 if (!skb)
3320 return;
3321
Patrick Ohlyac45f602009-02-12 05:03:37 +00003322 serr = SKB_EXT_ERR(skb);
3323 memset(serr, 0, sizeof(*serr));
3324 serr->ee.ee_errno = ENOMSG;
3325 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003326
Patrick Ohlyac45f602009-02-12 05:03:37 +00003327 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003328
Patrick Ohlyac45f602009-02-12 05:03:37 +00003329 if (err)
3330 kfree_skb(skb);
3331}
3332EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3333
Johannes Berg6e3e9392011-11-09 10:15:42 +01003334void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3335{
3336 struct sock *sk = skb->sk;
3337 struct sock_exterr_skb *serr;
3338 int err;
3339
3340 skb->wifi_acked_valid = 1;
3341 skb->wifi_acked = acked;
3342
3343 serr = SKB_EXT_ERR(skb);
3344 memset(serr, 0, sizeof(*serr));
3345 serr->ee.ee_errno = ENOMSG;
3346 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3347
3348 err = sock_queue_err_skb(sk, skb);
3349 if (err)
3350 kfree_skb(skb);
3351}
3352EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3353
Patrick Ohlyac45f602009-02-12 05:03:37 +00003354
Rusty Russellf35d9d82008-02-04 23:49:54 -05003355/**
3356 * skb_partial_csum_set - set up and verify partial csum values for packet
3357 * @skb: the skb to set
3358 * @start: the number of bytes after skb->data to start checksumming.
3359 * @off: the offset from start to place the checksum.
3360 *
3361 * For untrusted partially-checksummed packets, we need to make sure the values
3362 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3363 *
3364 * This function checks and sets those values and skb->ip_summed: if this
3365 * returns false you should drop the packet.
3366 */
3367bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3368{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003369 if (unlikely(start > skb_headlen(skb)) ||
3370 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003371 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3372 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003373 return false;
3374 }
3375 skb->ip_summed = CHECKSUM_PARTIAL;
3376 skb->csum_start = skb_headroom(skb) + start;
3377 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003378 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003379 return true;
3380}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003381EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003382
Ben Hutchings4497b072008-06-19 16:22:28 -07003383void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3384{
Joe Perchese87cc472012-05-13 21:56:26 +00003385 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3386 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003387}
Ben Hutchings4497b072008-06-19 16:22:28 -07003388EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003389
3390void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3391{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003392 if (head_stolen) {
3393 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003394 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003395 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003396 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003397 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003398}
3399EXPORT_SYMBOL(kfree_skb_partial);
3400
3401/**
3402 * skb_try_coalesce - try to merge skb to prior one
3403 * @to: prior buffer
3404 * @from: buffer to add
3405 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003406 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003407 */
3408bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3409 bool *fragstolen, int *delta_truesize)
3410{
3411 int i, delta, len = from->len;
3412
3413 *fragstolen = false;
3414
3415 if (skb_cloned(to))
3416 return false;
3417
3418 if (len <= skb_tailroom(to)) {
3419 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3420 *delta_truesize = 0;
3421 return true;
3422 }
3423
3424 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3425 return false;
3426
3427 if (skb_headlen(from) != 0) {
3428 struct page *page;
3429 unsigned int offset;
3430
3431 if (skb_shinfo(to)->nr_frags +
3432 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3433 return false;
3434
3435 if (skb_head_is_locked(from))
3436 return false;
3437
3438 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3439
3440 page = virt_to_head_page(from->head);
3441 offset = from->data - (unsigned char *)page_address(page);
3442
3443 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3444 page, offset, skb_headlen(from));
3445 *fragstolen = true;
3446 } else {
3447 if (skb_shinfo(to)->nr_frags +
3448 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3449 return false;
3450
Weiping Panf4b549a2012-09-28 20:15:30 +00003451 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003452 }
3453
3454 WARN_ON_ONCE(delta < len);
3455
3456 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3457 skb_shinfo(from)->frags,
3458 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3459 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3460
3461 if (!skb_cloned(from))
3462 skb_shinfo(from)->nr_frags = 0;
3463
Li RongQing8ea853f2012-09-18 16:53:21 +00003464 /* if the skb is not cloned this does nothing
3465 * since we set nr_frags to 0.
3466 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003467 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3468 skb_frag_ref(from, i);
3469
3470 to->truesize += delta;
3471 to->len += len;
3472 to->data_len += len;
3473
3474 *delta_truesize = delta;
3475 return true;
3476}
3477EXPORT_SYMBOL(skb_try_coalesce);
Florian Westphal3fb03b52014-02-22 10:30:17 +01003478
3479/**
3480 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3481 *
3482 * @skb: GSO skb
3483 *
3484 * skb_gso_transport_seglen is used to determine the real size of the
3485 * individual segments, including Layer4 headers (TCP/UDP).
3486 *
3487 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3488 */
3489unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3490{
3491 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphal3fb03b52014-02-22 10:30:17 +01003492
3493 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
Florian Westphalb8362bf2014-04-09 10:28:50 +02003494 return tcp_hdrlen(skb) + shinfo->gso_size;
3495
3496 /* UFO sets gso_size to the size of the fragmentation
3497 * payload, i.e. the size of the L4 (UDP) header is already
3498 * accounted for.
3499 */
3500 return shinfo->gso_size;
Florian Westphal3fb03b52014-02-22 10:30:17 +01003501}
3502EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);