blob: 607a70ff2cc21b40dc7fad8ab1f35b6ad7836a2f [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>
50#include <linux/netdevice.h>
51#ifdef CONFIG_NET_CLS_ACT
52#include <net/pkt_sched.h>
53#endif
54#include <linux/string.h>
55#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080056#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/cache.h>
58#include <linux/rtnetlink.h>
59#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070060#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000061#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070062#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <net/protocol.h>
65#include <net/dst.h>
66#include <net/sock.h>
67#include <net/checksum.h>
68#include <net/xfrm.h>
69
70#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040071#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020072#include <linux/highmem.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040073
Eric Dumazetd7e88832012-04-30 08:10:34 +000074struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080075static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jens Axboe9c55e012007-11-06 23:30:13 -080077static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
78 struct pipe_buffer *buf)
79{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080080 put_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080081}
82
83static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
84 struct pipe_buffer *buf)
85{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080086 get_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080087}
88
89static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
91{
92 return 1;
93}
94
95
96/* Pipe buffer operations for a socket. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -080097static const struct pipe_buf_operations sock_pipe_buf_ops = {
Jens Axboe9c55e012007-11-06 23:30:13 -080098 .can_merge = 0,
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
110 * reliable.
111 */
112
113/**
114 * skb_over_panic - private function
115 * @skb: buffer
116 * @sz: size
117 * @here: address
118 *
119 * Out of line support code for skb_put(). Not user callable.
120 */
Rami Rosenccb7c772010-04-20 22:39:53 -0700121static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Joe Perchese005d192012-05-16 19:58:40 +0000123 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
124 __func__, here, skb->len, sz, skb->head, skb->data,
125 (unsigned long)skb->tail, (unsigned long)skb->end,
126 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 BUG();
128}
129
130/**
131 * skb_under_panic - private function
132 * @skb: buffer
133 * @sz: size
134 * @here: address
135 *
136 * Out of line support code for skb_push(). Not user callable.
137 */
138
Rami Rosenccb7c772010-04-20 22:39:53 -0700139static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Joe Perchese005d192012-05-16 19:58:40 +0000141 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
142 __func__, here, skb->len, sz, skb->head, skb->data,
143 (unsigned long)skb->tail, (unsigned long)skb->end,
144 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 BUG();
146}
147
Mel Gormanc93bdd02012-07-31 16:44:19 -0700148
149/*
150 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
151 * the caller if emergency pfmemalloc reserves are being used. If it is and
152 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
153 * may be used. Otherwise, the packet data may be discarded until enough
154 * memory is free
155 */
156#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
157 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
158void *__kmalloc_reserve(size_t size, gfp_t flags, int node, unsigned long ip,
159 bool *pfmemalloc)
160{
161 void *obj;
162 bool ret_pfmemalloc = false;
163
164 /*
165 * Try a regular allocation, when that fails and we're not entitled
166 * to the reserves, fail.
167 */
168 obj = kmalloc_node_track_caller(size,
169 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
170 node);
171 if (obj || !(gfp_pfmemalloc_allowed(flags)))
172 goto out;
173
174 /* Try again but now we are using pfmemalloc reserves */
175 ret_pfmemalloc = true;
176 obj = kmalloc_node_track_caller(size, flags, node);
177
178out:
179 if (pfmemalloc)
180 *pfmemalloc = ret_pfmemalloc;
181
182 return obj;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Allocate a new skbuff. We do this ourselves so we can fill in a few
186 * 'private' fields and also do memory statistics to find all the
187 * [BEEP] leaks.
188 *
189 */
190
191/**
David S. Millerd179cd12005-08-17 14:57:30 -0700192 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * @size: size to allocate
194 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700195 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
196 * instead of head cache and allocate a cloned (child) skb.
197 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
198 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800199 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000202 * tail room of at least size bytes. The object has a reference count
203 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
205 * Buffers may only be allocated from interrupts using a @gfp_mask of
206 * %GFP_ATOMIC.
207 */
Al Virodd0fc662005-10-07 07:46:04 +0100208struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700209 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Christoph Lametere18b8902006-12-06 20:33:20 -0800211 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct sk_buff *skb;
214 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700215 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Mel Gormanc93bdd02012-07-31 16:44:19 -0700217 cache = (flags & SKB_ALLOC_FCLONE)
218 ? skbuff_fclone_cache : skbuff_head_cache;
219
220 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
221 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800224 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!skb)
226 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700227 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 /* We do our best to align skb_shared_info on a separate cache
230 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
231 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
232 * Both skb->head and skb_shared_info are cache line aligned.
233 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000234 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000235 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700236 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!data)
238 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000239 /* kmalloc(size) might give us more room than requested.
240 * Put skb_shared_info exactly at the end of allocated zone,
241 * to allow max possible filling before reallocation.
242 */
243 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700244 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300246 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700247 * Only clear those fields we need to clear, not those that we will
248 * actually initialise below. Hence, don't put any more fields after
249 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300250 */
251 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000252 /* Account for allocated memory : skb + skb->head */
253 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700254 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 atomic_set(&skb->users, 1);
256 skb->head = data;
257 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700258 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700259 skb->end = skb->tail + size;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000260#ifdef NET_SKBUFF_DATA_USES_OFFSET
261 skb->mac_header = ~0U;
262#endif
263
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800264 /* make sure we initialize shinfo sequentially */
265 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700266 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800267 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000268 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800269
Mel Gormanc93bdd02012-07-31 16:44:19 -0700270 if (flags & SKB_ALLOC_FCLONE) {
David S. Millerd179cd12005-08-17 14:57:30 -0700271 struct sk_buff *child = skb + 1;
272 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200274 kmemcheck_annotate_bitfield(child, flags1);
275 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700276 skb->fclone = SKB_FCLONE_ORIG;
277 atomic_set(fclone_ref, 1);
278
279 child->fclone = SKB_FCLONE_UNAVAILABLE;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700280 child->pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282out:
283 return skb;
284nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800285 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 skb = NULL;
287 goto out;
288}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800289EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000292 * build_skb - build a network buffer
293 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000294 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000295 *
296 * Allocate a new &sk_buff. Caller provides space holding head and
297 * skb_shared_info. @data must have been allocated by kmalloc()
298 * The return is the new skb buffer.
299 * On a failure the return is %NULL, and @data is not freed.
300 * Notes :
301 * Before IO, driver allocates only data buffer where NIC put incoming frame
302 * Driver should add room at head (NET_SKB_PAD) and
303 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
304 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
305 * before giving packet to stack.
306 * RX rings only contains data buffers, not full skbs.
307 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000308struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000309{
310 struct skb_shared_info *shinfo;
311 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000312 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313
314 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
315 if (!skb)
316 return NULL;
317
Eric Dumazetd3836f22012-04-27 00:33:38 +0000318 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000319
320 memset(skb, 0, offsetof(struct sk_buff, tail));
321 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000322 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000323 atomic_set(&skb->users, 1);
324 skb->head = data;
325 skb->data = data;
326 skb_reset_tail_pointer(skb);
327 skb->end = skb->tail + size;
328#ifdef NET_SKBUFF_DATA_USES_OFFSET
329 skb->mac_header = ~0U;
330#endif
331
332 /* make sure we initialize shinfo sequentially */
333 shinfo = skb_shinfo(skb);
334 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
335 atomic_set(&shinfo->dataref, 1);
336 kmemcheck_annotate_variable(shinfo->destructor_arg);
337
338 return skb;
339}
340EXPORT_SYMBOL(build_skb);
341
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000342struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000343 struct page_frag frag;
344 /* we maintain a pagecount bias, so that we dont dirty cache line
345 * containing page->_count every time we allocate a fragment.
346 */
347 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000348};
349static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
350
Eric Dumazet69b08f62012-09-26 06:46:57 +0000351#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
352#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
353#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000354
Mel Gormanc93bdd02012-07-31 16:44:19 -0700355static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000356{
357 struct netdev_alloc_cache *nc;
358 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000359 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000360 unsigned long flags;
361
362 local_irq_save(flags);
363 nc = &__get_cpu_var(netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000364 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000365refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000366 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
367 gfp_t gfp = gfp_mask;
368
369 if (order)
370 gfp |= __GFP_COMP | __GFP_NOWARN;
371 nc->frag.page = alloc_pages(gfp, order);
372 if (likely(nc->frag.page))
373 break;
374 if (--order < 0)
375 goto end;
376 }
377 nc->frag.size = PAGE_SIZE << order;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000378recycle:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000379 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
380 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
381 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000382 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000383
Eric Dumazet69b08f62012-09-26 06:46:57 +0000384 if (nc->frag.offset + fragsz > nc->frag.size) {
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000385 /* avoid unnecessary locked operations if possible */
Eric Dumazet69b08f62012-09-26 06:46:57 +0000386 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
387 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000388 goto recycle;
389 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000390 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000391
Eric Dumazet69b08f62012-09-26 06:46:57 +0000392 data = page_address(nc->frag.page) + nc->frag.offset;
393 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000394 nc->pagecnt_bias--;
395end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000396 local_irq_restore(flags);
397 return data;
398}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700399
400/**
401 * netdev_alloc_frag - allocate a page fragment
402 * @fragsz: fragment size
403 *
404 * Allocates a frag from a page for receive buffer.
405 * Uses GFP_ATOMIC allocations.
406 */
407void *netdev_alloc_frag(unsigned int fragsz)
408{
409 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
410}
Eric Dumazet6f532612012-05-18 05:12:12 +0000411EXPORT_SYMBOL(netdev_alloc_frag);
412
413/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700414 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
415 * @dev: network device to receive on
416 * @length: length to allocate
417 * @gfp_mask: get_free_pages mask, passed to alloc_skb
418 *
419 * Allocate a new &sk_buff and assign it a usage count of one. The
420 * buffer has unspecified headroom built in. Users should allocate
421 * the headroom they think they need without accounting for the
422 * built in space. The built in space is used for optimisations.
423 *
424 * %NULL is returned if there is no free memory.
425 */
426struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000427 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700428{
Eric Dumazet6f532612012-05-18 05:12:12 +0000429 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000430 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
431 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700432
Eric Dumazet310e1582012-07-16 13:15:52 +0200433 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700434 void *data;
435
436 if (sk_memalloc_socks())
437 gfp_mask |= __GFP_MEMALLOC;
438
439 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000440
Eric Dumazet6f532612012-05-18 05:12:12 +0000441 if (likely(data)) {
442 skb = build_skb(data, fragsz);
443 if (unlikely(!skb))
444 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000445 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000446 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700447 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
448 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000449 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700450 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700451 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700452 skb->dev = dev;
453 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700454 return skb;
455}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800456EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Peter Zijlstra654bed12008-10-07 14:22:33 -0700458void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000459 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700460{
461 skb_fill_page_desc(skb, i, page, off, size);
462 skb->len += size;
463 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000464 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700465}
466EXPORT_SYMBOL(skb_add_rx_frag);
467
Herbert Xu27b437c2006-07-13 19:26:39 -0700468static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Herbert Xu27b437c2006-07-13 19:26:39 -0700470 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Herbert Xu27b437c2006-07-13 19:26:39 -0700472 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 do {
475 struct sk_buff *this = list;
476 list = list->next;
477 kfree_skb(this);
478 } while (list);
479}
480
Herbert Xu27b437c2006-07-13 19:26:39 -0700481static inline void skb_drop_fraglist(struct sk_buff *skb)
482{
483 skb_drop_list(&skb_shinfo(skb)->frag_list);
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static void skb_clone_fraglist(struct sk_buff *skb)
487{
488 struct sk_buff *list;
489
David S. Millerfbb398a2009-06-09 00:18:59 -0700490 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 skb_get(list);
492}
493
Eric Dumazetd3836f22012-04-27 00:33:38 +0000494static void skb_free_head(struct sk_buff *skb)
495{
496 if (skb->head_frag)
497 put_page(virt_to_head_page(skb->head));
498 else
499 kfree(skb->head);
500}
501
Adrian Bunk5bba1712006-06-29 13:02:35 -0700502static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 if (!skb->cloned ||
505 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
506 &skb_shinfo(skb)->dataref)) {
507 if (skb_shinfo(skb)->nr_frags) {
508 int i;
509 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +0000510 skb_frag_unref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Shirley Maa6686f22011-07-06 12:22:12 +0000513 /*
514 * If skb buf is from userspace, we need to notify the caller
515 * the lower device DMA has done;
516 */
517 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
518 struct ubuf_info *uarg;
519
520 uarg = skb_shinfo(skb)->destructor_arg;
521 if (uarg->callback)
522 uarg->callback(uarg);
523 }
524
David S. Miller21dc3302010-08-23 00:13:46 -0700525 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 skb_drop_fraglist(skb);
527
Eric Dumazetd3836f22012-04-27 00:33:38 +0000528 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530}
531
532/*
533 * Free an skbuff by memory without cleaning the state.
534 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800535static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
David S. Millerd179cd12005-08-17 14:57:30 -0700537 struct sk_buff *other;
538 atomic_t *fclone_ref;
539
David S. Millerd179cd12005-08-17 14:57:30 -0700540 switch (skb->fclone) {
541 case SKB_FCLONE_UNAVAILABLE:
542 kmem_cache_free(skbuff_head_cache, skb);
543 break;
544
545 case SKB_FCLONE_ORIG:
546 fclone_ref = (atomic_t *) (skb + 2);
547 if (atomic_dec_and_test(fclone_ref))
548 kmem_cache_free(skbuff_fclone_cache, skb);
549 break;
550
551 case SKB_FCLONE_CLONE:
552 fclone_ref = (atomic_t *) (skb + 1);
553 other = skb - 1;
554
555 /* The clone portion is available for
556 * fast-cloning again.
557 */
558 skb->fclone = SKB_FCLONE_UNAVAILABLE;
559
560 if (atomic_dec_and_test(fclone_ref))
561 kmem_cache_free(skbuff_fclone_cache, other);
562 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700566static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Eric Dumazetadf30902009-06-02 05:19:30 +0000568 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#ifdef CONFIG_XFRM
570 secpath_put(skb->sp);
571#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700572 if (skb->destructor) {
573 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 skb->destructor(skb);
575 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000576#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700577 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100578#endif
579#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800580 nf_conntrack_put_reasm(skb->nfct_reasm);
581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#ifdef CONFIG_BRIDGE_NETFILTER
583 nf_bridge_put(skb->nf_bridge);
584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* XXX: IS this still necessary? - JHS */
586#ifdef CONFIG_NET_SCHED
587 skb->tc_index = 0;
588#ifdef CONFIG_NET_CLS_ACT
589 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#endif
591#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700592}
593
594/* Free everything but the sk_buff shell. */
595static void skb_release_all(struct sk_buff *skb)
596{
597 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800598 skb_release_data(skb);
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Herbert Xu2d4baff2007-11-26 23:11:19 +0800601/**
602 * __kfree_skb - private function
603 * @skb: buffer
604 *
605 * Free an sk_buff. Release anything attached to the buffer.
606 * Clean the state. This is an internal helper function. Users should
607 * always call kfree_skb
608 */
609
610void __kfree_skb(struct sk_buff *skb)
611{
612 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kfree_skbmem(skb);
614}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800615EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800618 * kfree_skb - free an sk_buff
619 * @skb: buffer to free
620 *
621 * Drop a reference to the buffer and free it if the usage count has
622 * hit zero.
623 */
624void kfree_skb(struct sk_buff *skb)
625{
626 if (unlikely(!skb))
627 return;
628 if (likely(atomic_read(&skb->users) == 1))
629 smp_rmb();
630 else if (likely(!atomic_dec_and_test(&skb->users)))
631 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000632 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800633 __kfree_skb(skb);
634}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800635EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800636
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700637/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000638 * consume_skb - free an skbuff
639 * @skb: buffer to free
640 *
641 * Drop a ref to the buffer and free it if the usage count has hit zero
642 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
643 * is being dropped after a failure and notes that
644 */
645void consume_skb(struct sk_buff *skb)
646{
647 if (unlikely(!skb))
648 return;
649 if (likely(atomic_read(&skb->users) == 1))
650 smp_rmb();
651 else if (likely(!atomic_dec_and_test(&skb->users)))
652 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900653 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000654 __kfree_skb(skb);
655}
656EXPORT_SYMBOL(consume_skb);
657
658/**
Andy Fleming3d153a72011-10-13 04:33:54 +0000659 * skb_recycle - clean up an skb for reuse
660 * @skb: buffer
661 *
662 * Recycles the skb to be reused as a receive buffer. This
663 * function does any necessary reference count dropping, and
664 * cleans up the skbuff as if it just came from __alloc_skb().
665 */
666void skb_recycle(struct sk_buff *skb)
667{
668 struct skb_shared_info *shinfo;
669
670 skb_release_head_state(skb);
671
672 shinfo = skb_shinfo(skb);
673 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
674 atomic_set(&shinfo->dataref, 1);
675
676 memset(skb, 0, offsetof(struct sk_buff, tail));
677 skb->data = skb->head + NET_SKB_PAD;
678 skb_reset_tail_pointer(skb);
679}
680EXPORT_SYMBOL(skb_recycle);
681
682/**
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700683 * skb_recycle_check - check if skb can be reused for receive
684 * @skb: buffer
685 * @skb_size: minimum receive buffer size
686 *
687 * Checks that the skb passed in is not shared or cloned, and
688 * that it is linear and its head portion at least as large as
689 * skb_size so that it can be recycled as a receive buffer.
690 * If these conditions are met, this function does any necessary
691 * reference count dropping and cleans up the skbuff as if it
692 * just came from __alloc_skb().
693 */
Changli Gao5b0daa32010-05-29 00:12:13 -0700694bool skb_recycle_check(struct sk_buff *skb, int skb_size)
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700695{
Andy Fleming3d153a72011-10-13 04:33:54 +0000696 if (!skb_is_recycleable(skb, skb_size))
Changli Gao5b0daa32010-05-29 00:12:13 -0700697 return false;
Anton Vorontsove84af6d2009-11-10 14:11:01 +0000698
Andy Fleming3d153a72011-10-13 04:33:54 +0000699 skb_recycle(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700700
Changli Gao5b0daa32010-05-29 00:12:13 -0700701 return true;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700702}
703EXPORT_SYMBOL(skb_recycle_check);
704
Herbert Xudec18812007-10-14 00:37:30 -0700705static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
706{
707 new->tstamp = old->tstamp;
708 new->dev = old->dev;
709 new->transport_header = old->transport_header;
710 new->network_header = old->network_header;
711 new->mac_header = old->mac_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000712 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000713 new->rxhash = old->rxhash;
Changli Gao6461be32011-08-19 04:44:18 +0000714 new->ooo_okay = old->ooo_okay;
Tom Herbertbdeab992011-08-14 19:45:55 +0000715 new->l4_rxhash = old->l4_rxhash;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000716 new->no_fcs = old->no_fcs;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700717#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700718 new->sp = secpath_get(old->sp);
719#endif
720 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000721 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700722 new->local_df = old->local_df;
723 new->pkt_type = old->pkt_type;
724 new->ip_summed = old->ip_summed;
725 skb_copy_queue_mapping(new, old);
726 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000727#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700728 new->ipvs_property = old->ipvs_property;
729#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700730 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700731 new->protocol = old->protocol;
732 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800733 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700734 __nf_copy(new, old);
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000735#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Herbert Xudec18812007-10-14 00:37:30 -0700736 new->nf_trace = old->nf_trace;
737#endif
738#ifdef CONFIG_NET_SCHED
739 new->tc_index = old->tc_index;
740#ifdef CONFIG_NET_CLS_ACT
741 new->tc_verd = old->tc_verd;
742#endif
743#endif
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700744 new->vlan_tci = old->vlan_tci;
745
Herbert Xudec18812007-10-14 00:37:30 -0700746 skb_copy_secmark(new, old);
747}
748
Herbert Xu82c49a32009-05-22 22:11:37 +0000749/*
750 * You should not add any new code to this function. Add it to
751 * __copy_skb_header above instead.
752 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700753static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#define C(x) n->x = skb->x
756
757 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700759 __copy_skb_header(n, skb);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 C(len);
762 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700763 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700764 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800765 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 C(tail);
769 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800770 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000771 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800772 C(data);
773 C(truesize);
774 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 atomic_inc(&(skb_shinfo(skb)->dataref));
777 skb->cloned = 1;
778
779 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700780#undef C
781}
782
783/**
784 * skb_morph - morph one skb into another
785 * @dst: the skb to receive the contents
786 * @src: the skb to supply the contents
787 *
788 * This is identical to skb_clone except that the target skb is
789 * supplied by the user.
790 *
791 * The target skb is returned upon exit.
792 */
793struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
794{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800795 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700796 return __skb_clone(dst, src);
797}
798EXPORT_SYMBOL_GPL(skb_morph);
799
Ben Hutchings2c530402012-07-10 10:55:09 +0000800/**
801 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000802 * @skb: the skb to modify
803 * @gfp_mask: allocation priority
804 *
805 * This must be called on SKBTX_DEV_ZEROCOPY skb.
806 * It will copy all frags into kernel and drop the reference
807 * to userspace pages.
808 *
809 * If this function is called from an interrupt gfp_mask() must be
810 * %GFP_ATOMIC.
811 *
812 * Returns 0 on success or a negative error code on failure
813 * to allocate kernel memory to copy to.
814 */
815int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000816{
817 int i;
818 int num_frags = skb_shinfo(skb)->nr_frags;
819 struct page *page, *head = NULL;
820 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
821
822 for (i = 0; i < num_frags; i++) {
823 u8 *vaddr;
824 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
825
Krishna Kumar02756ed2012-07-17 02:05:29 +0000826 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000827 if (!page) {
828 while (head) {
829 struct page *next = (struct page *)head->private;
830 put_page(head);
831 head = next;
832 }
833 return -ENOMEM;
834 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200835 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000836 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000837 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200838 kunmap_atomic(vaddr);
Shirley Maa6686f22011-07-06 12:22:12 +0000839 page->private = (unsigned long)head;
840 head = page;
841 }
842
843 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000844 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000845 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000846
847 uarg->callback(uarg);
848
849 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000850 for (i = num_frags - 1; i >= 0; i--) {
851 __skb_fill_page_desc(skb, i, head, 0,
852 skb_shinfo(skb)->frags[i].size);
Shirley Maa6686f22011-07-06 12:22:12 +0000853 head = (struct page *)head->private;
854 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000855
856 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000857 return 0;
858}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000859EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000860
Herbert Xue0053ec2007-10-14 00:37:52 -0700861/**
862 * skb_clone - duplicate an sk_buff
863 * @skb: buffer to clone
864 * @gfp_mask: allocation priority
865 *
866 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
867 * copies share the same packet data but not structure. The new
868 * buffer has a reference count of 1. If the allocation fails the
869 * function returns %NULL otherwise the new buffer is returned.
870 *
871 * If this function is called from an interrupt gfp_mask() must be
872 * %GFP_ATOMIC.
873 */
874
875struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
876{
877 struct sk_buff *n;
878
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000879 if (skb_orphan_frags(skb, gfp_mask))
880 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000881
Herbert Xue0053ec2007-10-14 00:37:52 -0700882 n = skb + 1;
883 if (skb->fclone == SKB_FCLONE_ORIG &&
884 n->fclone == SKB_FCLONE_UNAVAILABLE) {
885 atomic_t *fclone_ref = (atomic_t *) (n + 1);
886 n->fclone = SKB_FCLONE_CLONE;
887 atomic_inc(fclone_ref);
888 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700889 if (skb_pfmemalloc(skb))
890 gfp_mask |= __GFP_MEMALLOC;
891
Herbert Xue0053ec2007-10-14 00:37:52 -0700892 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
893 if (!n)
894 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200895
896 kmemcheck_annotate_bitfield(n, flags1);
897 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700898 n->fclone = SKB_FCLONE_UNAVAILABLE;
899 }
900
901 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800903EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
906{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700907#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /*
909 * Shift between the two data areas in bytes
910 */
911 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700912#endif
Herbert Xudec18812007-10-14 00:37:30 -0700913
914 __copy_skb_header(new, old);
915
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700916#ifndef NET_SKBUFF_DATA_USES_OFFSET
917 /* {transport,network,mac}_header are relative to skb->head */
918 new->transport_header += offset;
919 new->network_header += offset;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000920 if (skb_mac_header_was_set(new))
921 new->mac_header += offset;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700922#endif
Herbert Xu79671682006-06-22 02:40:14 -0700923 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
924 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
925 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Mel Gormanc93bdd02012-07-31 16:44:19 -0700928static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
929{
930 if (skb_pfmemalloc(skb))
931 return SKB_ALLOC_RX;
932 return 0;
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/**
936 * skb_copy - create private copy of an sk_buff
937 * @skb: buffer to copy
938 * @gfp_mask: allocation priority
939 *
940 * Make a copy of both an &sk_buff and its data. This is used when the
941 * caller wishes to modify the data and needs a private copy of the
942 * data to alter. Returns %NULL on failure or the pointer to the buffer
943 * on success. The returned buffer has a reference count of 1.
944 *
945 * As by-product this function converts non-linear &sk_buff to linear
946 * one, so that &sk_buff becomes completely private and caller is allowed
947 * to modify all the data of returned buffer. This means that this
948 * function is not recommended for use in circumstances when only
949 * header is going to be modified. Use pskb_copy() instead.
950 */
951
Al Virodd0fc662005-10-07 07:46:04 +0100952struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000954 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000955 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700956 struct sk_buff *n = __alloc_skb(size, gfp_mask,
957 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!n)
960 return NULL;
961
962 /* Set the data pointer */
963 skb_reserve(n, headerlen);
964 /* Set the tail pointer and length */
965 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
968 BUG();
969
970 copy_skb_header(n, skb);
971 return n;
972}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800973EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975/**
Eric Dumazet117632e2011-12-03 21:39:53 +0000976 * __pskb_copy - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000978 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * @gfp_mask: allocation priority
980 *
981 * Make a copy of both an &sk_buff and part of its data, located
982 * in header. Fragmented data remain shared. This is used when
983 * the caller wishes to modify only header of &sk_buff and needs
984 * private copy of the header to alter. Returns %NULL on failure
985 * or the pointer to the buffer on success.
986 * The returned buffer has a reference count of 1.
987 */
988
Eric Dumazet117632e2011-12-03 21:39:53 +0000989struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Eric Dumazet117632e2011-12-03 21:39:53 +0000991 unsigned int size = skb_headlen(skb) + headroom;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700992 struct sk_buff *n = __alloc_skb(size, gfp_mask,
993 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!n)
996 goto out;
997
998 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000999 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* Set the tail pointer and length */
1001 skb_put(n, skb_headlen(skb));
1002 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001003 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Herbert Xu25f484a2006-11-07 14:57:15 -08001005 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 n->data_len = skb->data_len;
1007 n->len = skb->len;
1008
1009 if (skb_shinfo(skb)->nr_frags) {
1010 int i;
1011
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001012 if (skb_orphan_frags(skb, gfp_mask)) {
1013 kfree_skb(n);
1014 n = NULL;
1015 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1018 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001019 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 skb_shinfo(n)->nr_frags = i;
1022 }
1023
David S. Miller21dc3302010-08-23 00:13:46 -07001024 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1026 skb_clone_fraglist(n);
1027 }
1028
1029 copy_skb_header(n, skb);
1030out:
1031 return n;
1032}
Eric Dumazet117632e2011-12-03 21:39:53 +00001033EXPORT_SYMBOL(__pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035/**
1036 * pskb_expand_head - reallocate header of &sk_buff
1037 * @skb: buffer to reallocate
1038 * @nhead: room to add at head
1039 * @ntail: room to add at tail
1040 * @gfp_mask: allocation priority
1041 *
1042 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1043 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1044 * reference count of 1. Returns zero in the case of success or error,
1045 * if expansion failed. In the last case, &sk_buff is not changed.
1046 *
1047 * All the pointers pointing into skb header may change and must be
1048 * reloaded after call to this function.
1049 */
1050
Victor Fusco86a76ca2005-07-08 14:57:47 -07001051int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001052 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 int i;
1055 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001056 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 long off;
1058
Herbert Xu4edd87a2008-10-01 07:09:38 -07001059 BUG_ON(nhead < 0);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (skb_shared(skb))
1062 BUG();
1063
1064 size = SKB_DATA_ALIGN(size);
1065
Mel Gormanc93bdd02012-07-31 16:44:19 -07001066 if (skb_pfmemalloc(skb))
1067 gfp_mask |= __GFP_MEMALLOC;
1068 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1069 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (!data)
1071 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001072 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001075 * optimized for the cases when header is void.
1076 */
1077 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1078
1079 memcpy((struct skb_shared_info *)(data + size),
1080 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001081 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Alexander Duyck3e245912012-05-04 14:26:51 +00001083 /*
1084 * if shinfo is shared we must drop the old head gracefully, but if it
1085 * is not we can just drop the old head and let the existing refcount
1086 * be since all we did is relocate the values
1087 */
1088 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001089 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001090 if (skb_orphan_frags(skb, gfp_mask))
1091 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001092 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001093 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Eric Dumazet1fd63042010-09-02 23:09:32 +00001095 if (skb_has_frag_list(skb))
1096 skb_clone_fraglist(skb);
1097
1098 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001099 } else {
1100 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 off = (data + nhead) - skb->head;
1103
1104 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001105 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001107#ifdef NET_SKBUFF_DATA_USES_OFFSET
1108 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001109 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001110#else
1111 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001112#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001113 /* {transport,network,mac}_header and tail are relative to skb->head */
1114 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001115 skb->transport_header += off;
1116 skb->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001117 if (skb_mac_header_was_set(skb))
1118 skb->mac_header += off;
Andrea Shepard00c5a982010-07-22 09:12:35 +00001119 /* Only adjust this if it actually is csum_start rather than csum */
1120 if (skb->ip_summed == CHECKSUM_PARTIAL)
1121 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001123 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 skb->nohdr = 0;
1125 atomic_set(&skb_shinfo(skb)->dataref, 1);
1126 return 0;
1127
Shirley Maa6686f22011-07-06 12:22:12 +00001128nofrags:
1129 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130nodata:
1131 return -ENOMEM;
1132}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001133EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135/* Make private copy of skb with writable head and some headroom */
1136
1137struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1138{
1139 struct sk_buff *skb2;
1140 int delta = headroom - skb_headroom(skb);
1141
1142 if (delta <= 0)
1143 skb2 = pskb_copy(skb, GFP_ATOMIC);
1144 else {
1145 skb2 = skb_clone(skb, GFP_ATOMIC);
1146 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1147 GFP_ATOMIC)) {
1148 kfree_skb(skb2);
1149 skb2 = NULL;
1150 }
1151 }
1152 return skb2;
1153}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001154EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156/**
1157 * skb_copy_expand - copy and expand sk_buff
1158 * @skb: buffer to copy
1159 * @newheadroom: new free bytes at head
1160 * @newtailroom: new free bytes at tail
1161 * @gfp_mask: allocation priority
1162 *
1163 * Make a copy of both an &sk_buff and its data and while doing so
1164 * allocate additional space.
1165 *
1166 * This is used when the caller wishes to modify the data and needs a
1167 * private copy of the data to alter as well as more space for new fields.
1168 * Returns %NULL on failure or the pointer to the buffer
1169 * on success. The returned buffer has a reference count of 1.
1170 *
1171 * You must pass %GFP_ATOMIC as the allocation priority if this function
1172 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
1174struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001175 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001176 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 /*
1179 * Allocate the copy buffer
1180 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001181 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1182 gfp_mask, skb_alloc_rx_flag(skb),
1183 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001184 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -07001186 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (!n)
1189 return NULL;
1190
1191 skb_reserve(n, newheadroom);
1192
1193 /* Set the tail pointer and length */
1194 skb_put(n, skb->len);
1195
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001196 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 head_copy_off = 0;
1198 if (newheadroom <= head_copy_len)
1199 head_copy_len = newheadroom;
1200 else
1201 head_copy_off = newheadroom - head_copy_len;
1202
1203 /* Copy the linear header and data. */
1204 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1205 skb->len + head_copy_len))
1206 BUG();
1207
1208 copy_skb_header(n, skb);
1209
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001210 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -07001211 if (n->ip_summed == CHECKSUM_PARTIAL)
1212 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -07001213#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001214 n->transport_header += off;
1215 n->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001216 if (skb_mac_header_was_set(skb))
1217 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -07001218#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return n;
1221}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001222EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224/**
1225 * skb_pad - zero pad the tail of an skb
1226 * @skb: buffer to pad
1227 * @pad: space to pad
1228 *
1229 * Ensure that a buffer is followed by a padding area that is zero
1230 * filled. Used by network drivers which may DMA or transfer data
1231 * beyond the buffer end onto the wire.
1232 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001233 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001235
Herbert Xu5b057c62006-06-23 02:06:41 -07001236int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Herbert Xu5b057c62006-06-23 02:06:41 -07001238 int err;
1239 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001242 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001246
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001247 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001248 if (likely(skb_cloned(skb) || ntail > 0)) {
1249 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1250 if (unlikely(err))
1251 goto free_skb;
1252 }
1253
1254 /* FIXME: The use of this function with non-linear skb's really needs
1255 * to be audited.
1256 */
1257 err = skb_linearize(skb);
1258 if (unlikely(err))
1259 goto free_skb;
1260
1261 memset(skb->data + skb->len, 0, pad);
1262 return 0;
1263
1264free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001266 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001267}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001268EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001269
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001270/**
1271 * skb_put - add data to a buffer
1272 * @skb: buffer to use
1273 * @len: amount of data to add
1274 *
1275 * This function extends the used data area of the buffer. If this would
1276 * exceed the total buffer size the kernel will panic. A pointer to the
1277 * first byte of the extra data is returned.
1278 */
1279unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1280{
1281 unsigned char *tmp = skb_tail_pointer(skb);
1282 SKB_LINEAR_ASSERT(skb);
1283 skb->tail += len;
1284 skb->len += len;
1285 if (unlikely(skb->tail > skb->end))
1286 skb_over_panic(skb, len, __builtin_return_address(0));
1287 return tmp;
1288}
1289EXPORT_SYMBOL(skb_put);
1290
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001291/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001292 * skb_push - add data to the start of a buffer
1293 * @skb: buffer to use
1294 * @len: amount of data to add
1295 *
1296 * This function extends the used data area of the buffer at the buffer
1297 * start. If this would exceed the total buffer headroom the kernel will
1298 * panic. A pointer to the first byte of the extra data is returned.
1299 */
1300unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1301{
1302 skb->data -= len;
1303 skb->len += len;
1304 if (unlikely(skb->data<skb->head))
1305 skb_under_panic(skb, len, __builtin_return_address(0));
1306 return skb->data;
1307}
1308EXPORT_SYMBOL(skb_push);
1309
1310/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001311 * skb_pull - remove data from the start of a buffer
1312 * @skb: buffer to use
1313 * @len: amount of data to remove
1314 *
1315 * This function removes data from the start of a buffer, returning
1316 * the memory to the headroom. A pointer to the next data in the buffer
1317 * is returned. Once the data has been pulled future pushes will overwrite
1318 * the old data.
1319 */
1320unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1321{
David S. Miller47d29642010-05-02 02:21:44 -07001322 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001323}
1324EXPORT_SYMBOL(skb_pull);
1325
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001326/**
1327 * skb_trim - remove end from a buffer
1328 * @skb: buffer to alter
1329 * @len: new length
1330 *
1331 * Cut the length of a buffer down by removing data from the tail. If
1332 * the buffer is already under the length specified it is not modified.
1333 * The skb must be linear.
1334 */
1335void skb_trim(struct sk_buff *skb, unsigned int len)
1336{
1337 if (skb->len > len)
1338 __skb_trim(skb, len);
1339}
1340EXPORT_SYMBOL(skb_trim);
1341
Herbert Xu3cc0e872006-06-09 16:13:38 -07001342/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
1344
Herbert Xu3cc0e872006-06-09 16:13:38 -07001345int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Herbert Xu27b437c2006-07-13 19:26:39 -07001347 struct sk_buff **fragp;
1348 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int offset = skb_headlen(skb);
1350 int nfrags = skb_shinfo(skb)->nr_frags;
1351 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001352 int err;
1353
1354 if (skb_cloned(skb) &&
1355 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001358 i = 0;
1359 if (offset >= len)
1360 goto drop_pages;
1361
1362 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001363 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001364
1365 if (end < len) {
1366 offset = end;
1367 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001369
Eric Dumazet9e903e02011-10-18 21:00:24 +00001370 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001371
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001372drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001373 skb_shinfo(skb)->nr_frags = i;
1374
1375 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001376 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001377
David S. Miller21dc3302010-08-23 00:13:46 -07001378 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001379 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001380 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Herbert Xu27b437c2006-07-13 19:26:39 -07001383 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1384 fragp = &frag->next) {
1385 int end = offset + frag->len;
1386
1387 if (skb_shared(frag)) {
1388 struct sk_buff *nfrag;
1389
1390 nfrag = skb_clone(frag, GFP_ATOMIC);
1391 if (unlikely(!nfrag))
1392 return -ENOMEM;
1393
1394 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001395 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001396 frag = nfrag;
1397 *fragp = frag;
1398 }
1399
1400 if (end < len) {
1401 offset = end;
1402 continue;
1403 }
1404
1405 if (end > len &&
1406 unlikely((err = pskb_trim(frag, len - offset))))
1407 return err;
1408
1409 if (frag->next)
1410 skb_drop_list(&frag->next);
1411 break;
1412 }
1413
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001414done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001415 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 skb->data_len -= skb->len - len;
1417 skb->len = len;
1418 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001419 skb->len = len;
1420 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001421 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424 return 0;
1425}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001426EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428/**
1429 * __pskb_pull_tail - advance tail of skb header
1430 * @skb: buffer to reallocate
1431 * @delta: number of bytes to advance tail
1432 *
1433 * The function makes a sense only on a fragmented &sk_buff,
1434 * it expands header moving its tail forward and copying necessary
1435 * data from fragmented part.
1436 *
1437 * &sk_buff MUST have reference count of 1.
1438 *
1439 * Returns %NULL (and &sk_buff does not change) if pull failed
1440 * or value of new tail of skb in the case of success.
1441 *
1442 * All the pointers pointing into skb header may change and must be
1443 * reloaded after call to this function.
1444 */
1445
1446/* Moves tail of skb head forward, copying data from fragmented part,
1447 * when it is necessary.
1448 * 1. It may fail due to malloc failure.
1449 * 2. It may change skb pointers.
1450 *
1451 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1452 */
1453unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1454{
1455 /* If skb has not enough free space at tail, get new one
1456 * plus 128 bytes for future expansions. If we have enough
1457 * room at tail, reallocate without expansion only if skb is cloned.
1458 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001459 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if (eat > 0 || skb_cloned(skb)) {
1462 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1463 GFP_ATOMIC))
1464 return NULL;
1465 }
1466
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001467 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 BUG();
1469
1470 /* Optimization: no fragments, no reasons to preestimate
1471 * size of pulled pages. Superb.
1472 */
David S. Miller21dc3302010-08-23 00:13:46 -07001473 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 goto pull_pages;
1475
1476 /* Estimate size of pulled pages. */
1477 eat = delta;
1478 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001479 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1480
1481 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001483 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
1486 /* If we need update frag list, we are in troubles.
1487 * Certainly, it possible to add an offset to skb data,
1488 * but taking into account that pulling is expected to
1489 * be very rare operation, it is worth to fight against
1490 * further bloating skb head and crucify ourselves here instead.
1491 * Pure masohism, indeed. 8)8)
1492 */
1493 if (eat) {
1494 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1495 struct sk_buff *clone = NULL;
1496 struct sk_buff *insp = NULL;
1497
1498 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001499 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 if (list->len <= eat) {
1502 /* Eaten as whole. */
1503 eat -= list->len;
1504 list = list->next;
1505 insp = list;
1506 } else {
1507 /* Eaten partially. */
1508
1509 if (skb_shared(list)) {
1510 /* Sucks! We need to fork list. :-( */
1511 clone = skb_clone(list, GFP_ATOMIC);
1512 if (!clone)
1513 return NULL;
1514 insp = list->next;
1515 list = clone;
1516 } else {
1517 /* This may be pulled without
1518 * problems. */
1519 insp = list;
1520 }
1521 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001522 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return NULL;
1524 }
1525 break;
1526 }
1527 } while (eat);
1528
1529 /* Free pulled out fragments. */
1530 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1531 skb_shinfo(skb)->frag_list = list->next;
1532 kfree_skb(list);
1533 }
1534 /* And insert new clone at head. */
1535 if (clone) {
1536 clone->next = list;
1537 skb_shinfo(skb)->frag_list = clone;
1538 }
1539 }
1540 /* Success! Now we may commit changes to skb data. */
1541
1542pull_pages:
1543 eat = delta;
1544 k = 0;
1545 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001546 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1547
1548 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001549 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001550 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 } else {
1552 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1553 if (eat) {
1554 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001555 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 eat = 0;
1557 }
1558 k++;
1559 }
1560 }
1561 skb_shinfo(skb)->nr_frags = k;
1562
1563 skb->tail += delta;
1564 skb->data_len -= delta;
1565
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001566 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001568EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Eric Dumazet22019b12011-07-29 18:37:31 +00001570/**
1571 * skb_copy_bits - copy bits from skb to kernel buffer
1572 * @skb: source skb
1573 * @offset: offset in source
1574 * @to: destination buffer
1575 * @len: number of bytes to copy
1576 *
1577 * Copy the specified number of bytes from the source skb to the
1578 * destination buffer.
1579 *
1580 * CAUTION ! :
1581 * If its prototype is ever changed,
1582 * check arch/{*}/net/{*}.S files,
1583 * since it is called from BPF assembly code.
1584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1586{
David S. Miller1a028e52007-04-27 15:21:23 -07001587 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001588 struct sk_buff *frag_iter;
1589 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (offset > (int)skb->len - len)
1592 goto fault;
1593
1594 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001595 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (copy > len)
1597 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001598 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if ((len -= copy) == 0)
1600 return 0;
1601 offset += copy;
1602 to += copy;
1603 }
1604
1605 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001606 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001607 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001609 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001610
Eric Dumazet51c56b02012-04-05 11:35:15 +02001611 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if ((copy = end - offset) > 0) {
1613 u8 *vaddr;
1614
1615 if (copy > len)
1616 copy = len;
1617
Eric Dumazet51c56b02012-04-05 11:35:15 +02001618 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001620 vaddr + f->page_offset + offset - start,
1621 copy);
1622 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if ((len -= copy) == 0)
1625 return 0;
1626 offset += copy;
1627 to += copy;
1628 }
David S. Miller1a028e52007-04-27 15:21:23 -07001629 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
David S. Millerfbb398a2009-06-09 00:18:59 -07001632 skb_walk_frags(skb, frag_iter) {
1633 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
David S. Millerfbb398a2009-06-09 00:18:59 -07001635 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
David S. Millerfbb398a2009-06-09 00:18:59 -07001637 end = start + frag_iter->len;
1638 if ((copy = end - offset) > 0) {
1639 if (copy > len)
1640 copy = len;
1641 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1642 goto fault;
1643 if ((len -= copy) == 0)
1644 return 0;
1645 offset += copy;
1646 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001648 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Shirley Maa6686f22011-07-06 12:22:12 +00001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!len)
1652 return 0;
1653
1654fault:
1655 return -EFAULT;
1656}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001657EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jens Axboe9c55e012007-11-06 23:30:13 -08001659/*
1660 * Callback from splice_to_pipe(), if we need to release some pages
1661 * at the end of the spd in case we error'ed out in filling the pipe.
1662 */
1663static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1664{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001665 put_page(spd->pages[i]);
1666}
Jens Axboe9c55e012007-11-06 23:30:13 -08001667
David S. Millera108d5f2012-04-23 23:06:11 -04001668static struct page *linear_to_page(struct page *page, unsigned int *len,
1669 unsigned int *offset,
1670 struct sk_buff *skb, struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001671{
Eric Dumazet5640f762012-09-23 23:04:42 +00001672 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001673
Eric Dumazet5640f762012-09-23 23:04:42 +00001674 if (!sk_page_frag_refill(sk, pfrag))
1675 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001676
Eric Dumazet5640f762012-09-23 23:04:42 +00001677 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001678
Eric Dumazet5640f762012-09-23 23:04:42 +00001679 memcpy(page_address(pfrag->page) + pfrag->offset,
1680 page_address(page) + *offset, *len);
1681 *offset = pfrag->offset;
1682 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001683
Eric Dumazet5640f762012-09-23 23:04:42 +00001684 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001685}
1686
Eric Dumazet41c73a02012-04-22 12:26:16 +00001687static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1688 struct page *page,
1689 unsigned int offset)
1690{
1691 return spd->nr_pages &&
1692 spd->pages[spd->nr_pages - 1] == page &&
1693 (spd->partial[spd->nr_pages - 1].offset +
1694 spd->partial[spd->nr_pages - 1].len == offset);
1695}
1696
Jens Axboe9c55e012007-11-06 23:30:13 -08001697/*
1698 * Fill page/offset/length into spd, if it can hold more pages.
1699 */
David S. Millera108d5f2012-04-23 23:06:11 -04001700static bool spd_fill_page(struct splice_pipe_desc *spd,
1701 struct pipe_inode_info *pipe, struct page *page,
1702 unsigned int *len, unsigned int offset,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001703 struct sk_buff *skb, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001704 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001705{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001706 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001707 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001708
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001709 if (linear) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001710 page = linear_to_page(page, len, &offset, skb, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001711 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001712 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001713 }
1714 if (spd_can_coalesce(spd, page, offset)) {
1715 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001716 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001717 }
1718 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001719 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001720 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001721 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001722 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001723
David S. Millera108d5f2012-04-23 23:06:11 -04001724 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001725}
1726
Octavian Purdila2870c432008-07-15 00:49:11 -07001727static inline void __segment_seek(struct page **page, unsigned int *poff,
1728 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001729{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001730 unsigned long n;
1731
Octavian Purdila2870c432008-07-15 00:49:11 -07001732 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001733 n = *poff / PAGE_SIZE;
1734 if (n)
1735 *page = nth_page(*page, n);
1736
Octavian Purdila2870c432008-07-15 00:49:11 -07001737 *poff = *poff % PAGE_SIZE;
1738 *plen -= off;
1739}
Jens Axboe9c55e012007-11-06 23:30:13 -08001740
David S. Millera108d5f2012-04-23 23:06:11 -04001741static bool __splice_segment(struct page *page, unsigned int poff,
1742 unsigned int plen, unsigned int *off,
1743 unsigned int *len, struct sk_buff *skb,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001744 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001745 struct sock *sk,
1746 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001747{
1748 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001749 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001750
1751 /* skip this segment if already processed */
1752 if (*off >= plen) {
1753 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001754 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001755 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001756
Octavian Purdila2870c432008-07-15 00:49:11 -07001757 /* ignore any bits we already processed */
1758 if (*off) {
1759 __segment_seek(&page, &poff, &plen, *off);
1760 *off = 0;
1761 }
1762
1763 do {
1764 unsigned int flen = min(*len, plen);
1765
1766 /* the linear region may spread across several pages */
1767 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1768
Jens Axboe35f3d142010-05-20 10:43:18 +02001769 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
David S. Millera108d5f2012-04-23 23:06:11 -04001770 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001771
1772 __segment_seek(&page, &poff, &plen, flen);
1773 *len -= flen;
1774
1775 } while (*len && plen);
1776
David S. Millera108d5f2012-04-23 23:06:11 -04001777 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001778}
1779
1780/*
David S. Millera108d5f2012-04-23 23:06:11 -04001781 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001782 * pipe is full or if we already spliced the requested length.
1783 */
David S. Millera108d5f2012-04-23 23:06:11 -04001784static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1785 unsigned int *offset, unsigned int *len,
1786 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001787{
1788 int seg;
1789
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001790 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001791 * If skb->head_frag is set, this 'linear' part is backed by a
1792 * fragment, and if the head is not shared with any clones then
1793 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001794 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001795 if (__splice_segment(virt_to_page(skb->data),
1796 (unsigned long) skb->data & (PAGE_SIZE - 1),
1797 skb_headlen(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001798 offset, len, skb, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001799 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001800 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001801 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001802
1803 /*
1804 * then map the fragments
1805 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001806 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1807 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1808
Ian Campbellea2ab692011-08-22 23:44:58 +00001809 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001810 f->page_offset, skb_frag_size(f),
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001811 offset, len, skb, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001812 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001813 }
1814
David S. Millera108d5f2012-04-23 23:06:11 -04001815 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001816}
1817
1818/*
1819 * Map data from the skb to a pipe. Should handle both the linear part,
1820 * the fragments, and the frag list. It does NOT handle frag lists within
1821 * the frag list, if such a thing exists. We'd probably need to recurse to
1822 * handle that cleanly.
1823 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001824int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 struct pipe_inode_info *pipe, unsigned int tlen,
1826 unsigned int flags)
1827{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001828 struct partial_page partial[MAX_SKB_FRAGS];
1829 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001830 struct splice_pipe_desc spd = {
1831 .pages = pages,
1832 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001833 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001834 .flags = flags,
1835 .ops = &sock_pipe_buf_ops,
1836 .spd_release = sock_spd_release,
1837 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001838 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001839 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001840 int ret = 0;
1841
Jens Axboe9c55e012007-11-06 23:30:13 -08001842 /*
1843 * __skb_splice_bits() only fails if the output has no room left,
1844 * so no point in going over the frag_list for the error case.
1845 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001846 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001847 goto done;
1848 else if (!tlen)
1849 goto done;
1850
1851 /*
1852 * now see if we have a frag_list to map
1853 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001854 skb_walk_frags(skb, frag_iter) {
1855 if (!tlen)
1856 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001857 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001858 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001859 }
1860
1861done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001862 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 /*
1864 * Drop the socket lock, otherwise we have reverse
1865 * locking dependencies between sk_lock and i_mutex
1866 * here as compared to sendfile(). We enter here
1867 * with the socket lock held, and splice_to_pipe() will
1868 * grab the pipe inode lock. For sendfile() emulation,
1869 * we call into ->sendpage() with the i_mutex lock held
1870 * and networking will grab the socket lock.
1871 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001872 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001873 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001874 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001875 }
1876
Jens Axboe35f3d142010-05-20 10:43:18 +02001877 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001878}
1879
Herbert Xu357b40a2005-04-19 22:30:14 -07001880/**
1881 * skb_store_bits - store bits from kernel buffer to skb
1882 * @skb: destination buffer
1883 * @offset: offset in destination
1884 * @from: source buffer
1885 * @len: number of bytes to copy
1886 *
1887 * Copy the specified number of bytes from the source buffer to the
1888 * destination skb. This function handles all the messy bits of
1889 * traversing fragment lists and such.
1890 */
1891
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001892int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001893{
David S. Miller1a028e52007-04-27 15:21:23 -07001894 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001895 struct sk_buff *frag_iter;
1896 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001897
1898 if (offset > (int)skb->len - len)
1899 goto fault;
1900
David S. Miller1a028e52007-04-27 15:21:23 -07001901 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001902 if (copy > len)
1903 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001904 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001905 if ((len -= copy) == 0)
1906 return 0;
1907 offset += copy;
1908 from += copy;
1909 }
1910
1911 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1912 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001913 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001914
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001915 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001916
Eric Dumazet9e903e02011-10-18 21:00:24 +00001917 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001918 if ((copy = end - offset) > 0) {
1919 u8 *vaddr;
1920
1921 if (copy > len)
1922 copy = len;
1923
Eric Dumazet51c56b02012-04-05 11:35:15 +02001924 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001925 memcpy(vaddr + frag->page_offset + offset - start,
1926 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001927 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001928
1929 if ((len -= copy) == 0)
1930 return 0;
1931 offset += copy;
1932 from += copy;
1933 }
David S. Miller1a028e52007-04-27 15:21:23 -07001934 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001935 }
1936
David S. Millerfbb398a2009-06-09 00:18:59 -07001937 skb_walk_frags(skb, frag_iter) {
1938 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001939
David S. Millerfbb398a2009-06-09 00:18:59 -07001940 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001941
David S. Millerfbb398a2009-06-09 00:18:59 -07001942 end = start + frag_iter->len;
1943 if ((copy = end - offset) > 0) {
1944 if (copy > len)
1945 copy = len;
1946 if (skb_store_bits(frag_iter, offset - start,
1947 from, copy))
1948 goto fault;
1949 if ((len -= copy) == 0)
1950 return 0;
1951 offset += copy;
1952 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001953 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001954 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001955 }
1956 if (!len)
1957 return 0;
1958
1959fault:
1960 return -EFAULT;
1961}
Herbert Xu357b40a2005-04-19 22:30:14 -07001962EXPORT_SYMBOL(skb_store_bits);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964/* Checksum skb data. */
1965
Al Viro2bbbc862006-11-14 21:37:14 -08001966__wsum skb_checksum(const struct sk_buff *skb, int offset,
1967 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
David S. Miller1a028e52007-04-27 15:21:23 -07001969 int start = skb_headlen(skb);
1970 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001971 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 int pos = 0;
1973
1974 /* Checksum header. */
1975 if (copy > 0) {
1976 if (copy > len)
1977 copy = len;
1978 csum = csum_partial(skb->data + offset, copy, csum);
1979 if ((len -= copy) == 0)
1980 return csum;
1981 offset += copy;
1982 pos = copy;
1983 }
1984
1985 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001986 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001987 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001989 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001990
Eric Dumazet51c56b02012-04-05 11:35:15 +02001991 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001993 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if (copy > len)
1997 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001998 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001999 csum2 = csum_partial(vaddr + frag->page_offset +
2000 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002001 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 csum = csum_block_add(csum, csum2, pos);
2003 if (!(len -= copy))
2004 return csum;
2005 offset += copy;
2006 pos += copy;
2007 }
David S. Miller1a028e52007-04-27 15:21:23 -07002008 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
David S. Millerfbb398a2009-06-09 00:18:59 -07002011 skb_walk_frags(skb, frag_iter) {
2012 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
David S. Millerfbb398a2009-06-09 00:18:59 -07002014 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
David S. Millerfbb398a2009-06-09 00:18:59 -07002016 end = start + frag_iter->len;
2017 if ((copy = end - offset) > 0) {
2018 __wsum csum2;
2019 if (copy > len)
2020 copy = len;
2021 csum2 = skb_checksum(frag_iter, offset - start,
2022 copy, 0);
2023 csum = csum_block_add(csum, csum2, pos);
2024 if ((len -= copy) == 0)
2025 return csum;
2026 offset += copy;
2027 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002029 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002031 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 return csum;
2034}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002035EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037/* Both of above in one bottle. */
2038
Al Viro81d77662006-11-14 21:37:33 -08002039__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2040 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
David S. Miller1a028e52007-04-27 15:21:23 -07002042 int start = skb_headlen(skb);
2043 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002044 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int pos = 0;
2046
2047 /* Copy header. */
2048 if (copy > 0) {
2049 if (copy > len)
2050 copy = len;
2051 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2052 copy, csum);
2053 if ((len -= copy) == 0)
2054 return csum;
2055 offset += copy;
2056 to += copy;
2057 pos = copy;
2058 }
2059
2060 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002061 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002063 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002064
Eric Dumazet9e903e02011-10-18 21:00:24 +00002065 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002067 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 u8 *vaddr;
2069 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2070
2071 if (copy > len)
2072 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002073 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002075 frag->page_offset +
2076 offset - start, to,
2077 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002078 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 csum = csum_block_add(csum, csum2, pos);
2080 if (!(len -= copy))
2081 return csum;
2082 offset += copy;
2083 to += copy;
2084 pos += copy;
2085 }
David S. Miller1a028e52007-04-27 15:21:23 -07002086 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
David S. Millerfbb398a2009-06-09 00:18:59 -07002089 skb_walk_frags(skb, frag_iter) {
2090 __wsum csum2;
2091 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
David S. Millerfbb398a2009-06-09 00:18:59 -07002093 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
David S. Millerfbb398a2009-06-09 00:18:59 -07002095 end = start + frag_iter->len;
2096 if ((copy = end - offset) > 0) {
2097 if (copy > len)
2098 copy = len;
2099 csum2 = skb_copy_and_csum_bits(frag_iter,
2100 offset - start,
2101 to, copy, 0);
2102 csum = csum_block_add(csum, csum2, pos);
2103 if ((len -= copy) == 0)
2104 return csum;
2105 offset += copy;
2106 to += copy;
2107 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002109 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002111 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return csum;
2113}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002114EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2117{
Al Virod3bc23e2006-11-14 21:24:49 -08002118 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 long csstart;
2120
Patrick McHardy84fa7932006-08-29 16:44:56 -07002121 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002122 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 else
2124 csstart = skb_headlen(skb);
2125
Kris Katterjohn09a62662006-01-08 22:24:28 -08002126 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002128 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 csum = 0;
2131 if (csstart != skb->len)
2132 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2133 skb->len - csstart, 0);
2134
Patrick McHardy84fa7932006-08-29 16:44:56 -07002135 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002136 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Al Virod3bc23e2006-11-14 21:24:49 -08002138 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002141EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143/**
2144 * skb_dequeue - remove from the head of the queue
2145 * @list: list to dequeue from
2146 *
2147 * Remove the head of the list. The list lock is taken so the function
2148 * may be used safely with other locking list functions. The head item is
2149 * returned or %NULL if the list is empty.
2150 */
2151
2152struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2153{
2154 unsigned long flags;
2155 struct sk_buff *result;
2156
2157 spin_lock_irqsave(&list->lock, flags);
2158 result = __skb_dequeue(list);
2159 spin_unlock_irqrestore(&list->lock, flags);
2160 return result;
2161}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002162EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164/**
2165 * skb_dequeue_tail - remove from the tail of the queue
2166 * @list: list to dequeue from
2167 *
2168 * Remove the tail of the list. The list lock is taken so the function
2169 * may be used safely with other locking list functions. The tail item is
2170 * returned or %NULL if the list is empty.
2171 */
2172struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2173{
2174 unsigned long flags;
2175 struct sk_buff *result;
2176
2177 spin_lock_irqsave(&list->lock, flags);
2178 result = __skb_dequeue_tail(list);
2179 spin_unlock_irqrestore(&list->lock, flags);
2180 return result;
2181}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002182EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184/**
2185 * skb_queue_purge - empty a list
2186 * @list: list to empty
2187 *
2188 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2189 * the list and one reference dropped. This function takes the list
2190 * lock and is atomic with respect to other list locking functions.
2191 */
2192void skb_queue_purge(struct sk_buff_head *list)
2193{
2194 struct sk_buff *skb;
2195 while ((skb = skb_dequeue(list)) != NULL)
2196 kfree_skb(skb);
2197}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002198EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200/**
2201 * skb_queue_head - queue a buffer at the list head
2202 * @list: list to use
2203 * @newsk: buffer to queue
2204 *
2205 * Queue a buffer at the start of the list. This function takes the
2206 * list lock and can be used safely with other locking &sk_buff functions
2207 * safely.
2208 *
2209 * A buffer cannot be placed on two lists at the same time.
2210 */
2211void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2212{
2213 unsigned long flags;
2214
2215 spin_lock_irqsave(&list->lock, flags);
2216 __skb_queue_head(list, newsk);
2217 spin_unlock_irqrestore(&list->lock, flags);
2218}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002219EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221/**
2222 * skb_queue_tail - queue a buffer at the list tail
2223 * @list: list to use
2224 * @newsk: buffer to queue
2225 *
2226 * Queue a buffer at the tail of the list. This function takes the
2227 * list lock and can be used safely with other locking &sk_buff functions
2228 * safely.
2229 *
2230 * A buffer cannot be placed on two lists at the same time.
2231 */
2232void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2233{
2234 unsigned long flags;
2235
2236 spin_lock_irqsave(&list->lock, flags);
2237 __skb_queue_tail(list, newsk);
2238 spin_unlock_irqrestore(&list->lock, flags);
2239}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002240EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242/**
2243 * skb_unlink - remove a buffer from a list
2244 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002245 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 *
David S. Miller8728b832005-08-09 19:25:21 -07002247 * Remove a packet from a list. The list locks are taken and this
2248 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 *
David S. Miller8728b832005-08-09 19:25:21 -07002250 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 */
David S. Miller8728b832005-08-09 19:25:21 -07002252void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
David S. Miller8728b832005-08-09 19:25:21 -07002254 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
David S. Miller8728b832005-08-09 19:25:21 -07002256 spin_lock_irqsave(&list->lock, flags);
2257 __skb_unlink(skb, list);
2258 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002260EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262/**
2263 * skb_append - append a buffer
2264 * @old: buffer to insert after
2265 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002266 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 *
2268 * Place a packet after a given packet in a list. The list locks are taken
2269 * and this function is atomic with respect to other list locked calls.
2270 * A buffer cannot be placed on two lists at the same time.
2271 */
David S. Miller8728b832005-08-09 19:25:21 -07002272void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 unsigned long flags;
2275
David S. Miller8728b832005-08-09 19:25:21 -07002276 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002277 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002278 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002280EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282/**
2283 * skb_insert - insert a buffer
2284 * @old: buffer to insert before
2285 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002286 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 *
David S. Miller8728b832005-08-09 19:25:21 -07002288 * Place a packet before a given packet in a list. The list locks are
2289 * taken and this function is atomic with respect to other list locked
2290 * calls.
2291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * A buffer cannot be placed on two lists at the same time.
2293 */
David S. Miller8728b832005-08-09 19:25:21 -07002294void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 unsigned long flags;
2297
David S. Miller8728b832005-08-09 19:25:21 -07002298 spin_lock_irqsave(&list->lock, flags);
2299 __skb_insert(newsk, old->prev, old, list);
2300 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002302EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static inline void skb_split_inside_header(struct sk_buff *skb,
2305 struct sk_buff* skb1,
2306 const u32 len, const int pos)
2307{
2308 int i;
2309
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002310 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2311 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* And move data appendix as is. */
2313 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2314 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2315
2316 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2317 skb_shinfo(skb)->nr_frags = 0;
2318 skb1->data_len = skb->data_len;
2319 skb1->len += skb1->data_len;
2320 skb->data_len = 0;
2321 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002322 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
2325static inline void skb_split_no_header(struct sk_buff *skb,
2326 struct sk_buff* skb1,
2327 const u32 len, int pos)
2328{
2329 int i, k = 0;
2330 const int nfrags = skb_shinfo(skb)->nr_frags;
2331
2332 skb_shinfo(skb)->nr_frags = 0;
2333 skb1->len = skb1->data_len = skb->len - len;
2334 skb->len = len;
2335 skb->data_len = len - pos;
2336
2337 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002338 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 if (pos + size > len) {
2341 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2342
2343 if (pos < len) {
2344 /* Split frag.
2345 * We have two variants in this case:
2346 * 1. Move all the frag to the second
2347 * part, if it is possible. F.e.
2348 * this approach is mandatory for TUX,
2349 * where splitting is expensive.
2350 * 2. Split is accurately. We make this.
2351 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002352 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002354 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2355 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 skb_shinfo(skb)->nr_frags++;
2357 }
2358 k++;
2359 } else
2360 skb_shinfo(skb)->nr_frags++;
2361 pos += size;
2362 }
2363 skb_shinfo(skb1)->nr_frags = k;
2364}
2365
2366/**
2367 * skb_split - Split fragmented skb to two parts at length len.
2368 * @skb: the buffer to split
2369 * @skb1: the buffer to receive the second part
2370 * @len: new length for skb
2371 */
2372void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2373{
2374 int pos = skb_headlen(skb);
2375
2376 if (len < pos) /* Split line is inside header. */
2377 skb_split_inside_header(skb, skb1, len, pos);
2378 else /* Second chunk has no header, nothing to copy. */
2379 skb_split_no_header(skb, skb1, len, pos);
2380}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002381EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002383/* Shifting from/to a cloned skb is a no-go.
2384 *
2385 * Caller cannot keep skb_shinfo related pointers past calling here!
2386 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002387static int skb_prepare_for_shift(struct sk_buff *skb)
2388{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002389 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002390}
2391
2392/**
2393 * skb_shift - Shifts paged data partially from skb to another
2394 * @tgt: buffer into which tail data gets added
2395 * @skb: buffer from which the paged data comes from
2396 * @shiftlen: shift up to this many bytes
2397 *
2398 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002399 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002400 * It's up to caller to free skb if everything was shifted.
2401 *
2402 * If @tgt runs out of frags, the whole operation is aborted.
2403 *
2404 * Skb cannot include anything else but paged data while tgt is allowed
2405 * to have non-paged data as well.
2406 *
2407 * TODO: full sized shift could be optimized but that would need
2408 * specialized skb free'er to handle frags without up-to-date nr_frags.
2409 */
2410int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2411{
2412 int from, to, merge, todo;
2413 struct skb_frag_struct *fragfrom, *fragto;
2414
2415 BUG_ON(shiftlen > skb->len);
2416 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2417
2418 todo = shiftlen;
2419 from = 0;
2420 to = skb_shinfo(tgt)->nr_frags;
2421 fragfrom = &skb_shinfo(skb)->frags[from];
2422
2423 /* Actual merge is delayed until the point when we know we can
2424 * commit all, so that we don't have to undo partial changes
2425 */
2426 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002427 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2428 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002429 merge = -1;
2430 } else {
2431 merge = to - 1;
2432
Eric Dumazet9e903e02011-10-18 21:00:24 +00002433 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002434 if (todo < 0) {
2435 if (skb_prepare_for_shift(skb) ||
2436 skb_prepare_for_shift(tgt))
2437 return 0;
2438
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002439 /* All previous frag pointers might be stale! */
2440 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002441 fragto = &skb_shinfo(tgt)->frags[merge];
2442
Eric Dumazet9e903e02011-10-18 21:00:24 +00002443 skb_frag_size_add(fragto, shiftlen);
2444 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002445 fragfrom->page_offset += shiftlen;
2446
2447 goto onlymerged;
2448 }
2449
2450 from++;
2451 }
2452
2453 /* Skip full, not-fitting skb to avoid expensive operations */
2454 if ((shiftlen == skb->len) &&
2455 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2456 return 0;
2457
2458 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2459 return 0;
2460
2461 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2462 if (to == MAX_SKB_FRAGS)
2463 return 0;
2464
2465 fragfrom = &skb_shinfo(skb)->frags[from];
2466 fragto = &skb_shinfo(tgt)->frags[to];
2467
Eric Dumazet9e903e02011-10-18 21:00:24 +00002468 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002469 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002470 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002471 from++;
2472 to++;
2473
2474 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002475 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002476 fragto->page = fragfrom->page;
2477 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002478 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002479
2480 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002481 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002482 todo = 0;
2483
2484 to++;
2485 break;
2486 }
2487 }
2488
2489 /* Ready to "commit" this state change to tgt */
2490 skb_shinfo(tgt)->nr_frags = to;
2491
2492 if (merge >= 0) {
2493 fragfrom = &skb_shinfo(skb)->frags[0];
2494 fragto = &skb_shinfo(tgt)->frags[merge];
2495
Eric Dumazet9e903e02011-10-18 21:00:24 +00002496 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002497 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002498 }
2499
2500 /* Reposition in the original skb */
2501 to = 0;
2502 while (from < skb_shinfo(skb)->nr_frags)
2503 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2504 skb_shinfo(skb)->nr_frags = to;
2505
2506 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2507
2508onlymerged:
2509 /* Most likely the tgt won't ever need its checksum anymore, skb on
2510 * the other hand might need it if it needs to be resent
2511 */
2512 tgt->ip_summed = CHECKSUM_PARTIAL;
2513 skb->ip_summed = CHECKSUM_PARTIAL;
2514
2515 /* Yak, is it really working this way? Some helper please? */
2516 skb->len -= shiftlen;
2517 skb->data_len -= shiftlen;
2518 skb->truesize -= shiftlen;
2519 tgt->len += shiftlen;
2520 tgt->data_len += shiftlen;
2521 tgt->truesize += shiftlen;
2522
2523 return shiftlen;
2524}
2525
Thomas Graf677e90e2005-06-23 20:59:51 -07002526/**
2527 * skb_prepare_seq_read - Prepare a sequential read of skb data
2528 * @skb: the buffer to read
2529 * @from: lower offset of data to be read
2530 * @to: upper offset of data to be read
2531 * @st: state variable
2532 *
2533 * Initializes the specified state variable. Must be called before
2534 * invoking skb_seq_read() for the first time.
2535 */
2536void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2537 unsigned int to, struct skb_seq_state *st)
2538{
2539 st->lower_offset = from;
2540 st->upper_offset = to;
2541 st->root_skb = st->cur_skb = skb;
2542 st->frag_idx = st->stepped_offset = 0;
2543 st->frag_data = NULL;
2544}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002545EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002546
2547/**
2548 * skb_seq_read - Sequentially read skb data
2549 * @consumed: number of bytes consumed by the caller so far
2550 * @data: destination pointer for data to be returned
2551 * @st: state variable
2552 *
2553 * Reads a block of skb data at &consumed relative to the
2554 * lower offset specified to skb_prepare_seq_read(). Assigns
2555 * the head of the data block to &data and returns the length
2556 * of the block or 0 if the end of the skb data or the upper
2557 * offset has been reached.
2558 *
2559 * The caller is not required to consume all of the data
2560 * returned, i.e. &consumed is typically set to the number
2561 * of bytes already consumed and the next call to
2562 * skb_seq_read() will return the remaining part of the block.
2563 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002564 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002565 * this limitation is the cost for zerocopy seqeuental
2566 * reads of potentially non linear data.
2567 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002568 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002569 * at the moment, state->root_skb could be replaced with
2570 * a stack for this purpose.
2571 */
2572unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2573 struct skb_seq_state *st)
2574{
2575 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2576 skb_frag_t *frag;
2577
2578 if (unlikely(abs_offset >= st->upper_offset))
2579 return 0;
2580
2581next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002582 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002583
Thomas Chenault995b3372009-05-18 21:43:27 -07002584 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002585 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002586 return block_limit - abs_offset;
2587 }
2588
2589 if (st->frag_idx == 0 && !st->frag_data)
2590 st->stepped_offset += skb_headlen(st->cur_skb);
2591
2592 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2593 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002594 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002595
2596 if (abs_offset < block_limit) {
2597 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002598 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002599
2600 *data = (u8 *) st->frag_data + frag->page_offset +
2601 (abs_offset - st->stepped_offset);
2602
2603 return block_limit - abs_offset;
2604 }
2605
2606 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002607 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002608 st->frag_data = NULL;
2609 }
2610
2611 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002612 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002613 }
2614
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002615 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002616 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002617 st->frag_data = NULL;
2618 }
2619
David S. Miller21dc3302010-08-23 00:13:46 -07002620 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002621 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002622 st->frag_idx = 0;
2623 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002624 } else if (st->cur_skb->next) {
2625 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002626 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002627 goto next_skb;
2628 }
2629
2630 return 0;
2631}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002632EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002633
2634/**
2635 * skb_abort_seq_read - Abort a sequential read of skb data
2636 * @st: state variable
2637 *
2638 * Must be called if skb_seq_read() was not called until it
2639 * returned 0.
2640 */
2641void skb_abort_seq_read(struct skb_seq_state *st)
2642{
2643 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002644 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002645}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002646EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002647
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002648#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2649
2650static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2651 struct ts_config *conf,
2652 struct ts_state *state)
2653{
2654 return skb_seq_read(offset, text, TS_SKB_CB(state));
2655}
2656
2657static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2658{
2659 skb_abort_seq_read(TS_SKB_CB(state));
2660}
2661
2662/**
2663 * skb_find_text - Find a text pattern in skb data
2664 * @skb: the buffer to look in
2665 * @from: search offset
2666 * @to: search limit
2667 * @config: textsearch configuration
2668 * @state: uninitialized textsearch state variable
2669 *
2670 * Finds a pattern in the skb data according to the specified
2671 * textsearch configuration. Use textsearch_next() to retrieve
2672 * subsequent occurrences of the pattern. Returns the offset
2673 * to the first occurrence or UINT_MAX if no match was found.
2674 */
2675unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2676 unsigned int to, struct ts_config *config,
2677 struct ts_state *state)
2678{
Phil Oesterf72b9482006-06-26 00:00:57 -07002679 unsigned int ret;
2680
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002681 config->get_next_block = skb_ts_get_next_block;
2682 config->finish = skb_ts_finish;
2683
2684 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2685
Phil Oesterf72b9482006-06-26 00:00:57 -07002686 ret = textsearch_find(config, state);
2687 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002688}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002689EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002690
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002691/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002692 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002693 * @sk: sock structure
2694 * @skb: skb structure to be appened with user data.
2695 * @getfrag: call back function to be used for getting the user data
2696 * @from: pointer to user message iov
2697 * @length: length of the iov message
2698 *
2699 * Description: This procedure append the user data in the fragment part
2700 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2701 */
2702int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002703 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002704 int len, int odd, struct sk_buff *skb),
2705 void *from, int length)
2706{
2707 int frg_cnt = 0;
2708 skb_frag_t *frag = NULL;
2709 struct page *page = NULL;
2710 int copy, left;
2711 int offset = 0;
2712 int ret;
2713
2714 do {
2715 /* Return error if we don't have space for new frag */
2716 frg_cnt = skb_shinfo(skb)->nr_frags;
2717 if (frg_cnt >= MAX_SKB_FRAGS)
2718 return -EFAULT;
2719
2720 /* allocate a new page for next frag */
2721 page = alloc_pages(sk->sk_allocation, 0);
2722
2723 /* If alloc_page fails just return failure and caller will
2724 * free previous allocated pages by doing kfree_skb()
2725 */
2726 if (page == NULL)
2727 return -ENOMEM;
2728
2729 /* initialize the next frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002730 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2731 skb->truesize += PAGE_SIZE;
2732 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2733
2734 /* get the new initialized frag */
2735 frg_cnt = skb_shinfo(skb)->nr_frags;
2736 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2737
2738 /* copy the user data to page */
2739 left = PAGE_SIZE - frag->page_offset;
2740 copy = (length > left)? left : length;
2741
Eric Dumazet9e903e02011-10-18 21:00:24 +00002742 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002743 offset, copy, 0, skb);
2744 if (ret < 0)
2745 return -EFAULT;
2746
2747 /* copy was successful so update the size parameters */
Eric Dumazet9e903e02011-10-18 21:00:24 +00002748 skb_frag_size_add(frag, copy);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002749 skb->len += copy;
2750 skb->data_len += copy;
2751 offset += copy;
2752 length -= copy;
2753
2754 } while (length > 0);
2755
2756 return 0;
2757}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002758EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002759
Herbert Xucbb042f2006-03-20 22:43:56 -08002760/**
2761 * skb_pull_rcsum - pull skb and update receive checksum
2762 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002763 * @len: length of data pulled
2764 *
2765 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002766 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002767 * receive path processing instead of skb_pull unless you know
2768 * that the checksum difference is zero (e.g., a valid IP header)
2769 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002770 */
2771unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2772{
2773 BUG_ON(len > skb->len);
2774 skb->len -= len;
2775 BUG_ON(skb->len < skb->data_len);
2776 skb_postpull_rcsum(skb, skb->data, len);
2777 return skb->data += len;
2778}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002779EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2780
Herbert Xuf4c50d92006-06-22 03:02:40 -07002781/**
2782 * skb_segment - Perform protocol segmentation on skb.
2783 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002784 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002785 *
2786 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002787 * a pointer to the first in a list of new skbs for the segments.
2788 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002789 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002790struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002791{
2792 struct sk_buff *segs = NULL;
2793 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002794 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002795 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002796 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002797 unsigned int offset = doffset;
2798 unsigned int headroom;
2799 unsigned int len;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002800 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002801 int nfrags = skb_shinfo(skb)->nr_frags;
2802 int err = -ENOMEM;
2803 int i = 0;
2804 int pos;
2805
2806 __skb_push(skb, doffset);
2807 headroom = skb_headroom(skb);
2808 pos = skb_headlen(skb);
2809
2810 do {
2811 struct sk_buff *nskb;
2812 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002813 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002814 int size;
2815
2816 len = skb->len - offset;
2817 if (len > mss)
2818 len = mss;
2819
2820 hsize = skb_headlen(skb) - offset;
2821 if (hsize < 0)
2822 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002823 if (hsize > len || !sg)
2824 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002825
Herbert Xu89319d382008-12-15 23:26:06 -08002826 if (!hsize && i >= nfrags) {
2827 BUG_ON(fskb->len != len);
2828
2829 pos += len;
2830 nskb = skb_clone(fskb, GFP_ATOMIC);
2831 fskb = fskb->next;
2832
2833 if (unlikely(!nskb))
2834 goto err;
2835
Alexander Duyckec47ea82012-05-04 14:26:56 +00002836 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002837 if (skb_cow_head(nskb, doffset + headroom)) {
2838 kfree_skb(nskb);
2839 goto err;
2840 }
2841
Alexander Duyckec47ea82012-05-04 14:26:56 +00002842 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002843 skb_release_head_state(nskb);
2844 __skb_push(nskb, doffset);
2845 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002846 nskb = __alloc_skb(hsize + doffset + headroom,
2847 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2848 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002849
2850 if (unlikely(!nskb))
2851 goto err;
2852
2853 skb_reserve(nskb, headroom);
2854 __skb_put(nskb, doffset);
2855 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002856
2857 if (segs)
2858 tail->next = nskb;
2859 else
2860 segs = nskb;
2861 tail = nskb;
2862
Herbert Xu6f85a122008-08-15 14:55:02 -07002863 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002864 nskb->mac_len = skb->mac_len;
2865
Eric Dumazet3d3be432010-09-01 00:50:51 +00002866 /* nskb and skb might have different headroom */
2867 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2868 nskb->csum_start += skb_headroom(nskb) - headroom;
2869
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002870 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002871 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002872 nskb->transport_header = (nskb->network_header +
2873 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002874 skb_copy_from_linear_data(skb, nskb->data, doffset);
2875
Herbert Xu2f181852009-03-28 23:39:18 -07002876 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002877 continue;
2878
Herbert Xuf4c50d92006-06-22 03:02:40 -07002879 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002880 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002881 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2882 skb_put(nskb, len),
2883 len, 0);
2884 continue;
2885 }
2886
2887 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002888
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002889 skb_copy_from_linear_data_offset(skb, offset,
2890 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002891
Herbert Xu89319d382008-12-15 23:26:06 -08002892 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002893 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002894 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002895 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002896
2897 if (pos < offset) {
2898 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002899 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002900 }
2901
Herbert Xu89319d382008-12-15 23:26:06 -08002902 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002903
2904 if (pos + size <= offset + len) {
2905 i++;
2906 pos += size;
2907 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002908 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002909 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002910 }
2911
2912 frag++;
2913 }
2914
Herbert Xu89319d382008-12-15 23:26:06 -08002915 if (pos < offset + len) {
2916 struct sk_buff *fskb2 = fskb;
2917
2918 BUG_ON(pos + fskb->len != offset + len);
2919
2920 pos += fskb->len;
2921 fskb = fskb->next;
2922
2923 if (fskb2->next) {
2924 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2925 if (!fskb2)
2926 goto err;
2927 } else
2928 skb_get(fskb2);
2929
David S. Millerfbb398a2009-06-09 00:18:59 -07002930 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002931 skb_shinfo(nskb)->frag_list = fskb2;
2932 }
2933
2934skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002935 nskb->data_len = len - hsize;
2936 nskb->len += nskb->data_len;
2937 nskb->truesize += nskb->data_len;
2938 } while ((offset += len) < skb->len);
2939
2940 return segs;
2941
2942err:
2943 while ((skb = segs)) {
2944 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002945 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002946 }
2947 return ERR_PTR(err);
2948}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002949EXPORT_SYMBOL_GPL(skb_segment);
2950
Herbert Xu71d93b32008-12-15 23:42:33 -08002951int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2952{
2953 struct sk_buff *p = *head;
2954 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002955 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2956 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002957 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002958 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002959 unsigned int offset = skb_gro_offset(skb);
2960 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002961 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002962
Herbert Xu86911732009-01-29 14:19:50 +00002963 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002964 return -E2BIG;
2965
Herbert Xu9aaa1562009-05-26 18:50:33 +00002966 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002967 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002968 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002969 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002970 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002971 int i = skbinfo->nr_frags;
2972 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002973
Herbert Xu66e92fc2009-05-26 18:50:32 +00002974 offset -= headlen;
2975
2976 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002977 return -E2BIG;
2978
Herbert Xu9aaa1562009-05-26 18:50:33 +00002979 pinfo->nr_frags = nr_frags;
2980 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002981
Herbert Xu9aaa1562009-05-26 18:50:33 +00002982 frag = pinfo->frags + nr_frags;
2983 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002984 do {
2985 *--frag = *--frag2;
2986 } while (--i);
2987
2988 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002989 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002990
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002991 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002992 delta_truesize = skb->truesize -
2993 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002994
Herbert Xuf5572062009-01-14 20:40:03 -08002995 skb->truesize -= skb->data_len;
2996 skb->len -= skb->data_len;
2997 skb->data_len = 0;
2998
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002999 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003000 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003001 } else if (skb->head_frag) {
3002 int nr_frags = pinfo->nr_frags;
3003 skb_frag_t *frag = pinfo->frags + nr_frags;
3004 struct page *page = virt_to_head_page(skb->head);
3005 unsigned int first_size = headlen - offset;
3006 unsigned int first_offset;
3007
3008 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3009 return -E2BIG;
3010
3011 first_offset = skb->data -
3012 (unsigned char *)page_address(page) +
3013 offset;
3014
3015 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3016
3017 frag->page.p = page;
3018 frag->page_offset = first_offset;
3019 skb_frag_size_set(frag, first_size);
3020
3021 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3022 /* We dont need to clear skbinfo->nr_frags here */
3023
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003024 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003025 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3026 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08003027 } else if (skb_gro_len(p) != pinfo->gso_size)
3028 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003029
3030 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003031 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003032 if (unlikely(!nskb))
3033 return -ENOMEM;
3034
3035 __copy_skb_header(nskb, p);
3036 nskb->mac_len = p->mac_len;
3037
3038 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003039 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003040
Herbert Xu86911732009-01-29 14:19:50 +00003041 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003042 skb_set_network_header(nskb, skb_network_offset(p));
3043 skb_set_transport_header(nskb, skb_transport_offset(p));
3044
Herbert Xu86911732009-01-29 14:19:50 +00003045 __skb_pull(p, skb_gro_offset(p));
3046 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3047 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003048
3049 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
3050 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003051 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003052 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003053 skb_header_release(p);
3054 nskb->prev = p;
3055
3056 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003057 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003058 nskb->len += p->len;
3059
3060 *head = nskb;
3061 nskb->next = p->next;
3062 p->next = NULL;
3063
3064 p = nskb;
3065
3066merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003067 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003068 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003069 unsigned int eat = offset - headlen;
3070
3071 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003072 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003073 skb->data_len -= eat;
3074 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003075 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003076 }
3077
Herbert Xu67147ba2009-05-26 18:50:22 +00003078 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003079
Herbert Xu71d93b32008-12-15 23:42:33 -08003080 p->prev->next = skb;
3081 p->prev = skb;
3082 skb_header_release(skb);
3083
Herbert Xu5d38a072009-01-04 16:13:40 -08003084done:
3085 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003086 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003087 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003088 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003089
3090 NAPI_GRO_CB(skb)->same_flow = 1;
3091 return 0;
3092}
3093EXPORT_SYMBOL_GPL(skb_gro_receive);
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095void __init skb_init(void)
3096{
3097 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3098 sizeof(struct sk_buff),
3099 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003100 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003101 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003102 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3103 (2*sizeof(struct sk_buff)) +
3104 sizeof(atomic_t),
3105 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003106 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003107 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
David Howells716ea3a2007-04-02 20:19:53 -07003110/**
3111 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3112 * @skb: Socket buffer containing the buffers to be mapped
3113 * @sg: The scatter-gather list to map into
3114 * @offset: The offset into the buffer's contents to start mapping
3115 * @len: Length of buffer space to be mapped
3116 *
3117 * Fill the specified scatter-gather list with mappings/pointers into a
3118 * region of the buffer space attached to a socket buffer.
3119 */
David S. Miller51c739d2007-10-30 21:29:29 -07003120static int
3121__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003122{
David S. Miller1a028e52007-04-27 15:21:23 -07003123 int start = skb_headlen(skb);
3124 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003125 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003126 int elt = 0;
3127
3128 if (copy > 0) {
3129 if (copy > len)
3130 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003131 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003132 elt++;
3133 if ((len -= copy) == 0)
3134 return elt;
3135 offset += copy;
3136 }
3137
3138 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003139 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003140
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003141 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003142
Eric Dumazet9e903e02011-10-18 21:00:24 +00003143 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003144 if ((copy = end - offset) > 0) {
3145 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3146
3147 if (copy > len)
3148 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003149 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003150 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003151 elt++;
3152 if (!(len -= copy))
3153 return elt;
3154 offset += copy;
3155 }
David S. Miller1a028e52007-04-27 15:21:23 -07003156 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003157 }
3158
David S. Millerfbb398a2009-06-09 00:18:59 -07003159 skb_walk_frags(skb, frag_iter) {
3160 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003161
David S. Millerfbb398a2009-06-09 00:18:59 -07003162 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003163
David S. Millerfbb398a2009-06-09 00:18:59 -07003164 end = start + frag_iter->len;
3165 if ((copy = end - offset) > 0) {
3166 if (copy > len)
3167 copy = len;
3168 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3169 copy);
3170 if ((len -= copy) == 0)
3171 return elt;
3172 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003173 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003174 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003175 }
3176 BUG_ON(len);
3177 return elt;
3178}
3179
David S. Miller51c739d2007-10-30 21:29:29 -07003180int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3181{
3182 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3183
Jens Axboec46f2332007-10-31 12:06:37 +01003184 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003185
3186 return nsg;
3187}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003188EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003189
David Howells716ea3a2007-04-02 20:19:53 -07003190/**
3191 * skb_cow_data - Check that a socket buffer's data buffers are writable
3192 * @skb: The socket buffer to check.
3193 * @tailbits: Amount of trailing space to be added
3194 * @trailer: Returned pointer to the skb where the @tailbits space begins
3195 *
3196 * Make sure that the data buffers attached to a socket buffer are
3197 * writable. If they are not, private copies are made of the data buffers
3198 * and the socket buffer is set to use these instead.
3199 *
3200 * If @tailbits is given, make sure that there is space to write @tailbits
3201 * bytes of data beyond current end of socket buffer. @trailer will be
3202 * set to point to the skb in which this space begins.
3203 *
3204 * The number of scatterlist elements required to completely map the
3205 * COW'd and extended socket buffer will be returned.
3206 */
3207int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3208{
3209 int copyflag;
3210 int elt;
3211 struct sk_buff *skb1, **skb_p;
3212
3213 /* If skb is cloned or its head is paged, reallocate
3214 * head pulling out all the pages (pages are considered not writable
3215 * at the moment even if they are anonymous).
3216 */
3217 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3218 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3219 return -ENOMEM;
3220
3221 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003222 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003223 /* A little of trouble, not enough of space for trailer.
3224 * This should not happen, when stack is tuned to generate
3225 * good frames. OK, on miss we reallocate and reserve even more
3226 * space, 128 bytes is fair. */
3227
3228 if (skb_tailroom(skb) < tailbits &&
3229 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3230 return -ENOMEM;
3231
3232 /* Voila! */
3233 *trailer = skb;
3234 return 1;
3235 }
3236
3237 /* Misery. We are in troubles, going to mincer fragments... */
3238
3239 elt = 1;
3240 skb_p = &skb_shinfo(skb)->frag_list;
3241 copyflag = 0;
3242
3243 while ((skb1 = *skb_p) != NULL) {
3244 int ntail = 0;
3245
3246 /* The fragment is partially pulled by someone,
3247 * this can happen on input. Copy it and everything
3248 * after it. */
3249
3250 if (skb_shared(skb1))
3251 copyflag = 1;
3252
3253 /* If the skb is the last, worry about trailer. */
3254
3255 if (skb1->next == NULL && tailbits) {
3256 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003257 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003258 skb_tailroom(skb1) < tailbits)
3259 ntail = tailbits + 128;
3260 }
3261
3262 if (copyflag ||
3263 skb_cloned(skb1) ||
3264 ntail ||
3265 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003266 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003267 struct sk_buff *skb2;
3268
3269 /* Fuck, we are miserable poor guys... */
3270 if (ntail == 0)
3271 skb2 = skb_copy(skb1, GFP_ATOMIC);
3272 else
3273 skb2 = skb_copy_expand(skb1,
3274 skb_headroom(skb1),
3275 ntail,
3276 GFP_ATOMIC);
3277 if (unlikely(skb2 == NULL))
3278 return -ENOMEM;
3279
3280 if (skb1->sk)
3281 skb_set_owner_w(skb2, skb1->sk);
3282
3283 /* Looking around. Are we still alive?
3284 * OK, link new skb, drop old one */
3285
3286 skb2->next = skb1->next;
3287 *skb_p = skb2;
3288 kfree_skb(skb1);
3289 skb1 = skb2;
3290 }
3291 elt++;
3292 *trailer = skb1;
3293 skb_p = &skb1->next;
3294 }
3295
3296 return elt;
3297}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003298EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003299
Eric Dumazetb1faf562010-05-31 23:44:05 -07003300static void sock_rmem_free(struct sk_buff *skb)
3301{
3302 struct sock *sk = skb->sk;
3303
3304 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3305}
3306
3307/*
3308 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3309 */
3310int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3311{
Eric Dumazet110c4332012-04-06 10:49:10 +02003312 int len = skb->len;
3313
Eric Dumazetb1faf562010-05-31 23:44:05 -07003314 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003315 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003316 return -ENOMEM;
3317
3318 skb_orphan(skb);
3319 skb->sk = sk;
3320 skb->destructor = sock_rmem_free;
3321 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3322
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003323 /* before exiting rcu section, make sure dst is refcounted */
3324 skb_dst_force(skb);
3325
Eric Dumazetb1faf562010-05-31 23:44:05 -07003326 skb_queue_tail(&sk->sk_error_queue, skb);
3327 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003328 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003329 return 0;
3330}
3331EXPORT_SYMBOL(sock_queue_err_skb);
3332
Patrick Ohlyac45f602009-02-12 05:03:37 +00003333void skb_tstamp_tx(struct sk_buff *orig_skb,
3334 struct skb_shared_hwtstamps *hwtstamps)
3335{
3336 struct sock *sk = orig_skb->sk;
3337 struct sock_exterr_skb *serr;
3338 struct sk_buff *skb;
3339 int err;
3340
3341 if (!sk)
3342 return;
3343
3344 skb = skb_clone(orig_skb, GFP_ATOMIC);
3345 if (!skb)
3346 return;
3347
3348 if (hwtstamps) {
3349 *skb_hwtstamps(skb) =
3350 *hwtstamps;
3351 } else {
3352 /*
3353 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003354 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003355 * store software time stamp
3356 */
3357 skb->tstamp = ktime_get_real();
3358 }
3359
3360 serr = SKB_EXT_ERR(skb);
3361 memset(serr, 0, sizeof(*serr));
3362 serr->ee.ee_errno = ENOMSG;
3363 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003364
Patrick Ohlyac45f602009-02-12 05:03:37 +00003365 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003366
Patrick Ohlyac45f602009-02-12 05:03:37 +00003367 if (err)
3368 kfree_skb(skb);
3369}
3370EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3371
Johannes Berg6e3e9392011-11-09 10:15:42 +01003372void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3373{
3374 struct sock *sk = skb->sk;
3375 struct sock_exterr_skb *serr;
3376 int err;
3377
3378 skb->wifi_acked_valid = 1;
3379 skb->wifi_acked = acked;
3380
3381 serr = SKB_EXT_ERR(skb);
3382 memset(serr, 0, sizeof(*serr));
3383 serr->ee.ee_errno = ENOMSG;
3384 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3385
3386 err = sock_queue_err_skb(sk, skb);
3387 if (err)
3388 kfree_skb(skb);
3389}
3390EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3391
Patrick Ohlyac45f602009-02-12 05:03:37 +00003392
Rusty Russellf35d9d82008-02-04 23:49:54 -05003393/**
3394 * skb_partial_csum_set - set up and verify partial csum values for packet
3395 * @skb: the skb to set
3396 * @start: the number of bytes after skb->data to start checksumming.
3397 * @off: the offset from start to place the checksum.
3398 *
3399 * For untrusted partially-checksummed packets, we need to make sure the values
3400 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3401 *
3402 * This function checks and sets those values and skb->ip_summed: if this
3403 * returns false you should drop the packet.
3404 */
3405bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3406{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003407 if (unlikely(start > skb_headlen(skb)) ||
3408 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003409 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3410 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003411 return false;
3412 }
3413 skb->ip_summed = CHECKSUM_PARTIAL;
3414 skb->csum_start = skb_headroom(skb) + start;
3415 skb->csum_offset = off;
3416 return true;
3417}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003418EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003419
Ben Hutchings4497b072008-06-19 16:22:28 -07003420void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3421{
Joe Perchese87cc472012-05-13 21:56:26 +00003422 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3423 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003424}
Ben Hutchings4497b072008-06-19 16:22:28 -07003425EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003426
3427void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3428{
3429 if (head_stolen)
3430 kmem_cache_free(skbuff_head_cache, skb);
3431 else
3432 __kfree_skb(skb);
3433}
3434EXPORT_SYMBOL(kfree_skb_partial);
3435
3436/**
3437 * skb_try_coalesce - try to merge skb to prior one
3438 * @to: prior buffer
3439 * @from: buffer to add
3440 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003441 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003442 */
3443bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3444 bool *fragstolen, int *delta_truesize)
3445{
3446 int i, delta, len = from->len;
3447
3448 *fragstolen = false;
3449
3450 if (skb_cloned(to))
3451 return false;
3452
3453 if (len <= skb_tailroom(to)) {
3454 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3455 *delta_truesize = 0;
3456 return true;
3457 }
3458
3459 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3460 return false;
3461
3462 if (skb_headlen(from) != 0) {
3463 struct page *page;
3464 unsigned int offset;
3465
3466 if (skb_shinfo(to)->nr_frags +
3467 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3468 return false;
3469
3470 if (skb_head_is_locked(from))
3471 return false;
3472
3473 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3474
3475 page = virt_to_head_page(from->head);
3476 offset = from->data - (unsigned char *)page_address(page);
3477
3478 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3479 page, offset, skb_headlen(from));
3480 *fragstolen = true;
3481 } else {
3482 if (skb_shinfo(to)->nr_frags +
3483 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3484 return false;
3485
3486 delta = from->truesize -
3487 SKB_TRUESIZE(skb_end_pointer(from) - from->head);
3488 }
3489
3490 WARN_ON_ONCE(delta < len);
3491
3492 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3493 skb_shinfo(from)->frags,
3494 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3495 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3496
3497 if (!skb_cloned(from))
3498 skb_shinfo(from)->nr_frags = 0;
3499
3500 /* if the skb is cloned this does nothing since we set nr_frags to 0 */
3501 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3502 skb_frag_ref(from, i);
3503
3504 to->truesize += delta;
3505 to->len += len;
3506 to->data_len += len;
3507
3508 *delta_truesize = delta;
3509 return true;
3510}
3511EXPORT_SYMBOL(skb_try_coalesce);