blob: b03fc0c6a952986fe144e7d19ba42e8338a379cb [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)
stephen hemminger61c5e882012-12-28 18:24:28 +0000158
159static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
160 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700161{
162 void *obj;
163 bool ret_pfmemalloc = false;
164
165 /*
166 * Try a regular allocation, when that fails and we're not entitled
167 * to the reserves, fail.
168 */
169 obj = kmalloc_node_track_caller(size,
170 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
171 node);
172 if (obj || !(gfp_pfmemalloc_allowed(flags)))
173 goto out;
174
175 /* Try again but now we are using pfmemalloc reserves */
176 ret_pfmemalloc = true;
177 obj = kmalloc_node_track_caller(size, flags, node);
178
179out:
180 if (pfmemalloc)
181 *pfmemalloc = ret_pfmemalloc;
182
183 return obj;
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* Allocate a new skbuff. We do this ourselves so we can fill in a few
187 * 'private' fields and also do memory statistics to find all the
188 * [BEEP] leaks.
189 *
190 */
191
192/**
David S. Millerd179cd12005-08-17 14:57:30 -0700193 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * @size: size to allocate
195 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700196 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
197 * instead of head cache and allocate a cloned (child) skb.
198 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
199 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800200 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
202 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000203 * tail room of at least size bytes. The object has a reference count
204 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
206 * Buffers may only be allocated from interrupts using a @gfp_mask of
207 * %GFP_ATOMIC.
208 */
Al Virodd0fc662005-10-07 07:46:04 +0100209struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700210 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Christoph Lametere18b8902006-12-06 20:33:20 -0800212 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800213 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct sk_buff *skb;
215 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700216 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Mel Gormanc93bdd02012-07-31 16:44:19 -0700218 cache = (flags & SKB_ALLOC_FCLONE)
219 ? skbuff_fclone_cache : skbuff_head_cache;
220
221 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
222 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800225 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!skb)
227 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700228 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000230 /* We do our best to align skb_shared_info on a separate cache
231 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
232 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
233 * Both skb->head and skb_shared_info are cache line aligned.
234 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000235 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000236 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700237 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!data)
239 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000240 /* kmalloc(size) might give us more room than requested.
241 * Put skb_shared_info exactly at the end of allocated zone,
242 * to allow max possible filling before reallocation.
243 */
244 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700245 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300247 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700248 * Only clear those fields we need to clear, not those that we will
249 * actually initialise below. Hence, don't put any more fields after
250 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300251 */
252 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000253 /* Account for allocated memory : skb + skb->head */
254 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700255 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 atomic_set(&skb->users, 1);
257 skb->head = data;
258 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700259 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700260 skb->end = skb->tail + size;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000261#ifdef NET_SKBUFF_DATA_USES_OFFSET
262 skb->mac_header = ~0U;
263#endif
264
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800265 /* make sure we initialize shinfo sequentially */
266 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700267 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800268 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000269 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800270
Mel Gormanc93bdd02012-07-31 16:44:19 -0700271 if (flags & SKB_ALLOC_FCLONE) {
David S. Millerd179cd12005-08-17 14:57:30 -0700272 struct sk_buff *child = skb + 1;
273 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200275 kmemcheck_annotate_bitfield(child, flags1);
276 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700277 skb->fclone = SKB_FCLONE_ORIG;
278 atomic_set(fclone_ref, 1);
279
280 child->fclone = SKB_FCLONE_UNAVAILABLE;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700281 child->pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283out:
284 return skb;
285nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800286 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 skb = NULL;
288 goto out;
289}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800290EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 * build_skb - build a network buffer
294 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000295 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000296 *
297 * Allocate a new &sk_buff. Caller provides space holding head and
298 * skb_shared_info. @data must have been allocated by kmalloc()
299 * The return is the new skb buffer.
300 * On a failure the return is %NULL, and @data is not freed.
301 * Notes :
302 * Before IO, driver allocates only data buffer where NIC put incoming frame
303 * Driver should add room at head (NET_SKB_PAD) and
304 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
305 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
306 * before giving packet to stack.
307 * RX rings only contains data buffers, not full skbs.
308 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000309struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000310{
311 struct skb_shared_info *shinfo;
312 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000313 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000314
315 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
316 if (!skb)
317 return NULL;
318
Eric Dumazetd3836f22012-04-27 00:33:38 +0000319 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000320
321 memset(skb, 0, offsetof(struct sk_buff, tail));
322 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000323 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000324 atomic_set(&skb->users, 1);
325 skb->head = data;
326 skb->data = data;
327 skb_reset_tail_pointer(skb);
328 skb->end = skb->tail + size;
329#ifdef NET_SKBUFF_DATA_USES_OFFSET
330 skb->mac_header = ~0U;
331#endif
332
333 /* make sure we initialize shinfo sequentially */
334 shinfo = skb_shinfo(skb);
335 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
336 atomic_set(&shinfo->dataref, 1);
337 kmemcheck_annotate_variable(shinfo->destructor_arg);
338
339 return skb;
340}
341EXPORT_SYMBOL(build_skb);
342
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000343struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000344 struct page_frag frag;
345 /* we maintain a pagecount bias, so that we dont dirty cache line
346 * containing page->_count every time we allocate a fragment.
347 */
348 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000349};
350static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
351
Eric Dumazet69b08f62012-09-26 06:46:57 +0000352#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
353#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
354#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000355
Mel Gormanc93bdd02012-07-31 16:44:19 -0700356static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000357{
358 struct netdev_alloc_cache *nc;
359 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000360 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000361 unsigned long flags;
362
363 local_irq_save(flags);
364 nc = &__get_cpu_var(netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000365 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000366refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000367 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
368 gfp_t gfp = gfp_mask;
369
370 if (order)
371 gfp |= __GFP_COMP | __GFP_NOWARN;
372 nc->frag.page = alloc_pages(gfp, order);
373 if (likely(nc->frag.page))
374 break;
375 if (--order < 0)
376 goto end;
377 }
378 nc->frag.size = PAGE_SIZE << order;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000379recycle:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000380 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
381 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
382 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000383 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000384
Eric Dumazet69b08f62012-09-26 06:46:57 +0000385 if (nc->frag.offset + fragsz > nc->frag.size) {
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000386 /* avoid unnecessary locked operations if possible */
Eric Dumazet69b08f62012-09-26 06:46:57 +0000387 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
388 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000389 goto recycle;
390 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000391 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000392
Eric Dumazet69b08f62012-09-26 06:46:57 +0000393 data = page_address(nc->frag.page) + nc->frag.offset;
394 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000395 nc->pagecnt_bias--;
396end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000397 local_irq_restore(flags);
398 return data;
399}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700400
401/**
402 * netdev_alloc_frag - allocate a page fragment
403 * @fragsz: fragment size
404 *
405 * Allocates a frag from a page for receive buffer.
406 * Uses GFP_ATOMIC allocations.
407 */
408void *netdev_alloc_frag(unsigned int fragsz)
409{
410 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
411}
Eric Dumazet6f532612012-05-18 05:12:12 +0000412EXPORT_SYMBOL(netdev_alloc_frag);
413
414/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700415 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
416 * @dev: network device to receive on
417 * @length: length to allocate
418 * @gfp_mask: get_free_pages mask, passed to alloc_skb
419 *
420 * Allocate a new &sk_buff and assign it a usage count of one. The
421 * buffer has unspecified headroom built in. Users should allocate
422 * the headroom they think they need without accounting for the
423 * built in space. The built in space is used for optimisations.
424 *
425 * %NULL is returned if there is no free memory.
426 */
427struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000428 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700429{
Eric Dumazet6f532612012-05-18 05:12:12 +0000430 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000431 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
432 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700433
Eric Dumazet310e1582012-07-16 13:15:52 +0200434 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700435 void *data;
436
437 if (sk_memalloc_socks())
438 gfp_mask |= __GFP_MEMALLOC;
439
440 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000441
Eric Dumazet6f532612012-05-18 05:12:12 +0000442 if (likely(data)) {
443 skb = build_skb(data, fragsz);
444 if (unlikely(!skb))
445 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000446 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000447 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700448 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
449 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000450 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700451 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700452 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700453 skb->dev = dev;
454 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700455 return skb;
456}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800457EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Peter Zijlstra654bed12008-10-07 14:22:33 -0700459void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000460 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700461{
462 skb_fill_page_desc(skb, i, page, off, size);
463 skb->len += size;
464 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000465 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700466}
467EXPORT_SYMBOL(skb_add_rx_frag);
468
Herbert Xu27b437c2006-07-13 19:26:39 -0700469static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Herbert Xu27b437c2006-07-13 19:26:39 -0700471 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Herbert Xu27b437c2006-07-13 19:26:39 -0700473 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 do {
476 struct sk_buff *this = list;
477 list = list->next;
478 kfree_skb(this);
479 } while (list);
480}
481
Herbert Xu27b437c2006-07-13 19:26:39 -0700482static inline void skb_drop_fraglist(struct sk_buff *skb)
483{
484 skb_drop_list(&skb_shinfo(skb)->frag_list);
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static void skb_clone_fraglist(struct sk_buff *skb)
488{
489 struct sk_buff *list;
490
David S. Millerfbb398a2009-06-09 00:18:59 -0700491 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 skb_get(list);
493}
494
Eric Dumazetd3836f22012-04-27 00:33:38 +0000495static void skb_free_head(struct sk_buff *skb)
496{
497 if (skb->head_frag)
498 put_page(virt_to_head_page(skb->head));
499 else
500 kfree(skb->head);
501}
502
Adrian Bunk5bba1712006-06-29 13:02:35 -0700503static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 if (!skb->cloned ||
506 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
507 &skb_shinfo(skb)->dataref)) {
508 if (skb_shinfo(skb)->nr_frags) {
509 int i;
510 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +0000511 skb_frag_unref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Shirley Maa6686f22011-07-06 12:22:12 +0000514 /*
515 * If skb buf is from userspace, we need to notify the caller
516 * the lower device DMA has done;
517 */
518 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
519 struct ubuf_info *uarg;
520
521 uarg = skb_shinfo(skb)->destructor_arg;
522 if (uarg->callback)
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000523 uarg->callback(uarg, true);
Shirley Maa6686f22011-07-06 12:22:12 +0000524 }
525
David S. Miller21dc3302010-08-23 00:13:46 -0700526 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 skb_drop_fraglist(skb);
528
Eric Dumazetd3836f22012-04-27 00:33:38 +0000529 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531}
532
533/*
534 * Free an skbuff by memory without cleaning the state.
535 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800536static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
David S. Millerd179cd12005-08-17 14:57:30 -0700538 struct sk_buff *other;
539 atomic_t *fclone_ref;
540
David S. Millerd179cd12005-08-17 14:57:30 -0700541 switch (skb->fclone) {
542 case SKB_FCLONE_UNAVAILABLE:
543 kmem_cache_free(skbuff_head_cache, skb);
544 break;
545
546 case SKB_FCLONE_ORIG:
547 fclone_ref = (atomic_t *) (skb + 2);
548 if (atomic_dec_and_test(fclone_ref))
549 kmem_cache_free(skbuff_fclone_cache, skb);
550 break;
551
552 case SKB_FCLONE_CLONE:
553 fclone_ref = (atomic_t *) (skb + 1);
554 other = skb - 1;
555
556 /* The clone portion is available for
557 * fast-cloning again.
558 */
559 skb->fclone = SKB_FCLONE_UNAVAILABLE;
560
561 if (atomic_dec_and_test(fclone_ref))
562 kmem_cache_free(skbuff_fclone_cache, other);
563 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700567static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Eric Dumazetadf30902009-06-02 05:19:30 +0000569 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#ifdef CONFIG_XFRM
571 secpath_put(skb->sp);
572#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700573 if (skb->destructor) {
574 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 skb->destructor(skb);
576 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000577#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700578 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100579#endif
580#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800581 nf_conntrack_put_reasm(skb->nfct_reasm);
582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583#ifdef CONFIG_BRIDGE_NETFILTER
584 nf_bridge_put(skb->nf_bridge);
585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/* XXX: IS this still necessary? - JHS */
587#ifdef CONFIG_NET_SCHED
588 skb->tc_index = 0;
589#ifdef CONFIG_NET_CLS_ACT
590 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#endif
592#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700593}
594
595/* Free everything but the sk_buff shell. */
596static void skb_release_all(struct sk_buff *skb)
597{
598 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800599 skb_release_data(skb);
600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Herbert Xu2d4baff2007-11-26 23:11:19 +0800602/**
603 * __kfree_skb - private function
604 * @skb: buffer
605 *
606 * Free an sk_buff. Release anything attached to the buffer.
607 * Clean the state. This is an internal helper function. Users should
608 * always call kfree_skb
609 */
610
611void __kfree_skb(struct sk_buff *skb)
612{
613 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 kfree_skbmem(skb);
615}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800616EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800619 * kfree_skb - free an sk_buff
620 * @skb: buffer to free
621 *
622 * Drop a reference to the buffer and free it if the usage count has
623 * hit zero.
624 */
625void kfree_skb(struct sk_buff *skb)
626{
627 if (unlikely(!skb))
628 return;
629 if (likely(atomic_read(&skb->users) == 1))
630 smp_rmb();
631 else if (likely(!atomic_dec_and_test(&skb->users)))
632 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000633 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800634 __kfree_skb(skb);
635}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800636EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800637
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700638/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000639 * skb_tx_error - report an sk_buff xmit error
640 * @skb: buffer that triggered an error
641 *
642 * Report xmit error if a device callback is tracking this skb.
643 * skb must be freed afterwards.
644 */
645void skb_tx_error(struct sk_buff *skb)
646{
647 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
648 struct ubuf_info *uarg;
649
650 uarg = skb_shinfo(skb)->destructor_arg;
651 if (uarg->callback)
652 uarg->callback(uarg, false);
653 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
654 }
655}
656EXPORT_SYMBOL(skb_tx_error);
657
658/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000659 * consume_skb - free an skbuff
660 * @skb: buffer to free
661 *
662 * Drop a ref to the buffer and free it if the usage count has hit zero
663 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
664 * is being dropped after a failure and notes that
665 */
666void consume_skb(struct sk_buff *skb)
667{
668 if (unlikely(!skb))
669 return;
670 if (likely(atomic_read(&skb->users) == 1))
671 smp_rmb();
672 else if (likely(!atomic_dec_and_test(&skb->users)))
673 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900674 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000675 __kfree_skb(skb);
676}
677EXPORT_SYMBOL(consume_skb);
678
Herbert Xudec18812007-10-14 00:37:30 -0700679static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
680{
681 new->tstamp = old->tstamp;
682 new->dev = old->dev;
683 new->transport_header = old->transport_header;
684 new->network_header = old->network_header;
685 new->mac_header = old->mac_header;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000686 new->inner_transport_header = old->inner_transport_header;
687 new->inner_network_header = old->inner_transport_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000688 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000689 new->rxhash = old->rxhash;
Changli Gao6461be32011-08-19 04:44:18 +0000690 new->ooo_okay = old->ooo_okay;
Tom Herbertbdeab992011-08-14 19:45:55 +0000691 new->l4_rxhash = old->l4_rxhash;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000692 new->no_fcs = old->no_fcs;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000693 new->encapsulation = old->encapsulation;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700694#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700695 new->sp = secpath_get(old->sp);
696#endif
697 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000698 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700699 new->local_df = old->local_df;
700 new->pkt_type = old->pkt_type;
701 new->ip_summed = old->ip_summed;
702 skb_copy_queue_mapping(new, old);
703 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000704#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700705 new->ipvs_property = old->ipvs_property;
706#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700707 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700708 new->protocol = old->protocol;
709 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800710 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700711 __nf_copy(new, old);
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000712#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Herbert Xudec18812007-10-14 00:37:30 -0700713 new->nf_trace = old->nf_trace;
714#endif
715#ifdef CONFIG_NET_SCHED
716 new->tc_index = old->tc_index;
717#ifdef CONFIG_NET_CLS_ACT
718 new->tc_verd = old->tc_verd;
719#endif
720#endif
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700721 new->vlan_tci = old->vlan_tci;
722
Herbert Xudec18812007-10-14 00:37:30 -0700723 skb_copy_secmark(new, old);
724}
725
Herbert Xu82c49a32009-05-22 22:11:37 +0000726/*
727 * You should not add any new code to this function. Add it to
728 * __copy_skb_header above instead.
729 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700730static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#define C(x) n->x = skb->x
733
734 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700736 __copy_skb_header(n, skb);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 C(len);
739 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700740 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700741 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800742 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 C(tail);
746 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800747 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000748 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800749 C(data);
750 C(truesize);
751 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 atomic_inc(&(skb_shinfo(skb)->dataref));
754 skb->cloned = 1;
755
756 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700757#undef C
758}
759
760/**
761 * skb_morph - morph one skb into another
762 * @dst: the skb to receive the contents
763 * @src: the skb to supply the contents
764 *
765 * This is identical to skb_clone except that the target skb is
766 * supplied by the user.
767 *
768 * The target skb is returned upon exit.
769 */
770struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
771{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800772 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700773 return __skb_clone(dst, src);
774}
775EXPORT_SYMBOL_GPL(skb_morph);
776
Ben Hutchings2c530402012-07-10 10:55:09 +0000777/**
778 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000779 * @skb: the skb to modify
780 * @gfp_mask: allocation priority
781 *
782 * This must be called on SKBTX_DEV_ZEROCOPY skb.
783 * It will copy all frags into kernel and drop the reference
784 * to userspace pages.
785 *
786 * If this function is called from an interrupt gfp_mask() must be
787 * %GFP_ATOMIC.
788 *
789 * Returns 0 on success or a negative error code on failure
790 * to allocate kernel memory to copy to.
791 */
792int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000793{
794 int i;
795 int num_frags = skb_shinfo(skb)->nr_frags;
796 struct page *page, *head = NULL;
797 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
798
799 for (i = 0; i < num_frags; i++) {
800 u8 *vaddr;
801 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
802
Krishna Kumar02756ed2012-07-17 02:05:29 +0000803 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000804 if (!page) {
805 while (head) {
806 struct page *next = (struct page *)head->private;
807 put_page(head);
808 head = next;
809 }
810 return -ENOMEM;
811 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200812 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000813 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000814 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200815 kunmap_atomic(vaddr);
Shirley Maa6686f22011-07-06 12:22:12 +0000816 page->private = (unsigned long)head;
817 head = page;
818 }
819
820 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000821 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000822 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000823
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000824 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000825
826 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000827 for (i = num_frags - 1; i >= 0; i--) {
828 __skb_fill_page_desc(skb, i, head, 0,
829 skb_shinfo(skb)->frags[i].size);
Shirley Maa6686f22011-07-06 12:22:12 +0000830 head = (struct page *)head->private;
831 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000832
833 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000834 return 0;
835}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000836EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000837
Herbert Xue0053ec2007-10-14 00:37:52 -0700838/**
839 * skb_clone - duplicate an sk_buff
840 * @skb: buffer to clone
841 * @gfp_mask: allocation priority
842 *
843 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
844 * copies share the same packet data but not structure. The new
845 * buffer has a reference count of 1. If the allocation fails the
846 * function returns %NULL otherwise the new buffer is returned.
847 *
848 * If this function is called from an interrupt gfp_mask() must be
849 * %GFP_ATOMIC.
850 */
851
852struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
853{
854 struct sk_buff *n;
855
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000856 if (skb_orphan_frags(skb, gfp_mask))
857 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000858
Herbert Xue0053ec2007-10-14 00:37:52 -0700859 n = skb + 1;
860 if (skb->fclone == SKB_FCLONE_ORIG &&
861 n->fclone == SKB_FCLONE_UNAVAILABLE) {
862 atomic_t *fclone_ref = (atomic_t *) (n + 1);
863 n->fclone = SKB_FCLONE_CLONE;
864 atomic_inc(fclone_ref);
865 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700866 if (skb_pfmemalloc(skb))
867 gfp_mask |= __GFP_MEMALLOC;
868
Herbert Xue0053ec2007-10-14 00:37:52 -0700869 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
870 if (!n)
871 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200872
873 kmemcheck_annotate_bitfield(n, flags1);
874 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700875 n->fclone = SKB_FCLONE_UNAVAILABLE;
876 }
877
878 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800880EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
883{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700884#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /*
886 * Shift between the two data areas in bytes
887 */
888 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700889#endif
Herbert Xudec18812007-10-14 00:37:30 -0700890
891 __copy_skb_header(new, old);
892
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700893#ifndef NET_SKBUFF_DATA_USES_OFFSET
894 /* {transport,network,mac}_header are relative to skb->head */
895 new->transport_header += offset;
896 new->network_header += offset;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000897 if (skb_mac_header_was_set(new))
898 new->mac_header += offset;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000899 new->inner_transport_header += offset;
900 new->inner_network_header += offset;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700901#endif
Herbert Xu79671682006-06-22 02:40:14 -0700902 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
903 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
904 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Mel Gormanc93bdd02012-07-31 16:44:19 -0700907static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
908{
909 if (skb_pfmemalloc(skb))
910 return SKB_ALLOC_RX;
911 return 0;
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914/**
915 * skb_copy - create private copy of an sk_buff
916 * @skb: buffer to copy
917 * @gfp_mask: allocation priority
918 *
919 * Make a copy of both an &sk_buff and its data. This is used when the
920 * caller wishes to modify the data and needs a private copy of the
921 * data to alter. Returns %NULL on failure or the pointer to the buffer
922 * on success. The returned buffer has a reference count of 1.
923 *
924 * As by-product this function converts non-linear &sk_buff to linear
925 * one, so that &sk_buff becomes completely private and caller is allowed
926 * to modify all the data of returned buffer. This means that this
927 * function is not recommended for use in circumstances when only
928 * header is going to be modified. Use pskb_copy() instead.
929 */
930
Al Virodd0fc662005-10-07 07:46:04 +0100931struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000933 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000934 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700935 struct sk_buff *n = __alloc_skb(size, gfp_mask,
936 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!n)
939 return NULL;
940
941 /* Set the data pointer */
942 skb_reserve(n, headerlen);
943 /* Set the tail pointer and length */
944 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
947 BUG();
948
949 copy_skb_header(n, skb);
950 return n;
951}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800952EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/**
Eric Dumazet117632e2011-12-03 21:39:53 +0000955 * __pskb_copy - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000957 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * @gfp_mask: allocation priority
959 *
960 * Make a copy of both an &sk_buff and part of its data, located
961 * in header. Fragmented data remain shared. This is used when
962 * the caller wishes to modify only header of &sk_buff and needs
963 * private copy of the header to alter. Returns %NULL on failure
964 * or the pointer to the buffer on success.
965 * The returned buffer has a reference count of 1.
966 */
967
Eric Dumazet117632e2011-12-03 21:39:53 +0000968struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Eric Dumazet117632e2011-12-03 21:39:53 +0000970 unsigned int size = skb_headlen(skb) + headroom;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700971 struct sk_buff *n = __alloc_skb(size, gfp_mask,
972 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!n)
975 goto out;
976
977 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000978 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Set the tail pointer and length */
980 skb_put(n, skb_headlen(skb));
981 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300982 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Herbert Xu25f484a2006-11-07 14:57:15 -0800984 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 n->data_len = skb->data_len;
986 n->len = skb->len;
987
988 if (skb_shinfo(skb)->nr_frags) {
989 int i;
990
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000991 if (skb_orphan_frags(skb, gfp_mask)) {
992 kfree_skb(n);
993 n = NULL;
994 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +0000995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
997 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +0000998 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 skb_shinfo(n)->nr_frags = i;
1001 }
1002
David S. Miller21dc3302010-08-23 00:13:46 -07001003 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1005 skb_clone_fraglist(n);
1006 }
1007
1008 copy_skb_header(n, skb);
1009out:
1010 return n;
1011}
Eric Dumazet117632e2011-12-03 21:39:53 +00001012EXPORT_SYMBOL(__pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014/**
1015 * pskb_expand_head - reallocate header of &sk_buff
1016 * @skb: buffer to reallocate
1017 * @nhead: room to add at head
1018 * @ntail: room to add at tail
1019 * @gfp_mask: allocation priority
1020 *
1021 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1022 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1023 * reference count of 1. Returns zero in the case of success or error,
1024 * if expansion failed. In the last case, &sk_buff is not changed.
1025 *
1026 * All the pointers pointing into skb header may change and must be
1027 * reloaded after call to this function.
1028 */
1029
Victor Fusco86a76ca2005-07-08 14:57:47 -07001030int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001031 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 int i;
1034 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001035 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 long off;
1037
Herbert Xu4edd87a2008-10-01 07:09:38 -07001038 BUG_ON(nhead < 0);
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (skb_shared(skb))
1041 BUG();
1042
1043 size = SKB_DATA_ALIGN(size);
1044
Mel Gormanc93bdd02012-07-31 16:44:19 -07001045 if (skb_pfmemalloc(skb))
1046 gfp_mask |= __GFP_MEMALLOC;
1047 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1048 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!data)
1050 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001051 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001054 * optimized for the cases when header is void.
1055 */
1056 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1057
1058 memcpy((struct skb_shared_info *)(data + size),
1059 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001060 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Alexander Duyck3e245912012-05-04 14:26:51 +00001062 /*
1063 * if shinfo is shared we must drop the old head gracefully, but if it
1064 * is not we can just drop the old head and let the existing refcount
1065 * be since all we did is relocate the values
1066 */
1067 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001068 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001069 if (skb_orphan_frags(skb, gfp_mask))
1070 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001071 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001072 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Eric Dumazet1fd63042010-09-02 23:09:32 +00001074 if (skb_has_frag_list(skb))
1075 skb_clone_fraglist(skb);
1076
1077 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001078 } else {
1079 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 off = (data + nhead) - skb->head;
1082
1083 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001084 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001086#ifdef NET_SKBUFF_DATA_USES_OFFSET
1087 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001088 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001089#else
1090 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001091#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001092 /* {transport,network,mac}_header and tail are relative to skb->head */
1093 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001094 skb->transport_header += off;
1095 skb->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001096 if (skb_mac_header_was_set(skb))
1097 skb->mac_header += off;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001098 skb->inner_transport_header += off;
1099 skb->inner_network_header += off;
Andrea Shepard00c5a982010-07-22 09:12:35 +00001100 /* Only adjust this if it actually is csum_start rather than csum */
1101 if (skb->ip_summed == CHECKSUM_PARTIAL)
1102 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001104 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 skb->nohdr = 0;
1106 atomic_set(&skb_shinfo(skb)->dataref, 1);
1107 return 0;
1108
Shirley Maa6686f22011-07-06 12:22:12 +00001109nofrags:
1110 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111nodata:
1112 return -ENOMEM;
1113}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001114EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116/* Make private copy of skb with writable head and some headroom */
1117
1118struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1119{
1120 struct sk_buff *skb2;
1121 int delta = headroom - skb_headroom(skb);
1122
1123 if (delta <= 0)
1124 skb2 = pskb_copy(skb, GFP_ATOMIC);
1125 else {
1126 skb2 = skb_clone(skb, GFP_ATOMIC);
1127 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1128 GFP_ATOMIC)) {
1129 kfree_skb(skb2);
1130 skb2 = NULL;
1131 }
1132 }
1133 return skb2;
1134}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001135EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137/**
1138 * skb_copy_expand - copy and expand sk_buff
1139 * @skb: buffer to copy
1140 * @newheadroom: new free bytes at head
1141 * @newtailroom: new free bytes at tail
1142 * @gfp_mask: allocation priority
1143 *
1144 * Make a copy of both an &sk_buff and its data and while doing so
1145 * allocate additional space.
1146 *
1147 * This is used when the caller wishes to modify the data and needs a
1148 * private copy of the data to alter as well as more space for new fields.
1149 * Returns %NULL on failure or the pointer to the buffer
1150 * on success. The returned buffer has a reference count of 1.
1151 *
1152 * You must pass %GFP_ATOMIC as the allocation priority if this function
1153 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
1155struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001156 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001157 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 /*
1160 * Allocate the copy buffer
1161 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001162 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1163 gfp_mask, skb_alloc_rx_flag(skb),
1164 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001165 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -07001167 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if (!n)
1170 return NULL;
1171
1172 skb_reserve(n, newheadroom);
1173
1174 /* Set the tail pointer and length */
1175 skb_put(n, skb->len);
1176
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001177 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 head_copy_off = 0;
1179 if (newheadroom <= head_copy_len)
1180 head_copy_len = newheadroom;
1181 else
1182 head_copy_off = newheadroom - head_copy_len;
1183
1184 /* Copy the linear header and data. */
1185 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1186 skb->len + head_copy_len))
1187 BUG();
1188
1189 copy_skb_header(n, skb);
1190
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001191 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -07001192 if (n->ip_summed == CHECKSUM_PARTIAL)
1193 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -07001194#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001195 n->transport_header += off;
1196 n->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001197 if (skb_mac_header_was_set(skb))
1198 n->mac_header += off;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001199 n->inner_transport_header += off;
1200 n->inner_network_header += off;
Herbert Xu52886052007-09-16 16:32:11 -07001201#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return n;
1204}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001205EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207/**
1208 * skb_pad - zero pad the tail of an skb
1209 * @skb: buffer to pad
1210 * @pad: space to pad
1211 *
1212 * Ensure that a buffer is followed by a padding area that is zero
1213 * filled. Used by network drivers which may DMA or transfer data
1214 * beyond the buffer end onto the wire.
1215 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001216 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001218
Herbert Xu5b057c62006-06-23 02:06:41 -07001219int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Herbert Xu5b057c62006-06-23 02:06:41 -07001221 int err;
1222 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001225 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001229
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001230 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001231 if (likely(skb_cloned(skb) || ntail > 0)) {
1232 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1233 if (unlikely(err))
1234 goto free_skb;
1235 }
1236
1237 /* FIXME: The use of this function with non-linear skb's really needs
1238 * to be audited.
1239 */
1240 err = skb_linearize(skb);
1241 if (unlikely(err))
1242 goto free_skb;
1243
1244 memset(skb->data + skb->len, 0, pad);
1245 return 0;
1246
1247free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001249 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001250}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001251EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001252
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001253/**
1254 * skb_put - add data to a buffer
1255 * @skb: buffer to use
1256 * @len: amount of data to add
1257 *
1258 * This function extends the used data area of the buffer. If this would
1259 * exceed the total buffer size the kernel will panic. A pointer to the
1260 * first byte of the extra data is returned.
1261 */
1262unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1263{
1264 unsigned char *tmp = skb_tail_pointer(skb);
1265 SKB_LINEAR_ASSERT(skb);
1266 skb->tail += len;
1267 skb->len += len;
1268 if (unlikely(skb->tail > skb->end))
1269 skb_over_panic(skb, len, __builtin_return_address(0));
1270 return tmp;
1271}
1272EXPORT_SYMBOL(skb_put);
1273
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001274/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001275 * skb_push - add data to the start of a buffer
1276 * @skb: buffer to use
1277 * @len: amount of data to add
1278 *
1279 * This function extends the used data area of the buffer at the buffer
1280 * start. If this would exceed the total buffer headroom the kernel will
1281 * panic. A pointer to the first byte of the extra data is returned.
1282 */
1283unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1284{
1285 skb->data -= len;
1286 skb->len += len;
1287 if (unlikely(skb->data<skb->head))
1288 skb_under_panic(skb, len, __builtin_return_address(0));
1289 return skb->data;
1290}
1291EXPORT_SYMBOL(skb_push);
1292
1293/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001294 * skb_pull - remove data from the start of a buffer
1295 * @skb: buffer to use
1296 * @len: amount of data to remove
1297 *
1298 * This function removes data from the start of a buffer, returning
1299 * the memory to the headroom. A pointer to the next data in the buffer
1300 * is returned. Once the data has been pulled future pushes will overwrite
1301 * the old data.
1302 */
1303unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1304{
David S. Miller47d29642010-05-02 02:21:44 -07001305 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001306}
1307EXPORT_SYMBOL(skb_pull);
1308
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001309/**
1310 * skb_trim - remove end from a buffer
1311 * @skb: buffer to alter
1312 * @len: new length
1313 *
1314 * Cut the length of a buffer down by removing data from the tail. If
1315 * the buffer is already under the length specified it is not modified.
1316 * The skb must be linear.
1317 */
1318void skb_trim(struct sk_buff *skb, unsigned int len)
1319{
1320 if (skb->len > len)
1321 __skb_trim(skb, len);
1322}
1323EXPORT_SYMBOL(skb_trim);
1324
Herbert Xu3cc0e872006-06-09 16:13:38 -07001325/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
1327
Herbert Xu3cc0e872006-06-09 16:13:38 -07001328int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Herbert Xu27b437c2006-07-13 19:26:39 -07001330 struct sk_buff **fragp;
1331 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int offset = skb_headlen(skb);
1333 int nfrags = skb_shinfo(skb)->nr_frags;
1334 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001335 int err;
1336
1337 if (skb_cloned(skb) &&
1338 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1339 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001341 i = 0;
1342 if (offset >= len)
1343 goto drop_pages;
1344
1345 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001346 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001347
1348 if (end < len) {
1349 offset = end;
1350 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001352
Eric Dumazet9e903e02011-10-18 21:00:24 +00001353 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001354
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001355drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001356 skb_shinfo(skb)->nr_frags = i;
1357
1358 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001359 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001360
David S. Miller21dc3302010-08-23 00:13:46 -07001361 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001362 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001363 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Herbert Xu27b437c2006-07-13 19:26:39 -07001366 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1367 fragp = &frag->next) {
1368 int end = offset + frag->len;
1369
1370 if (skb_shared(frag)) {
1371 struct sk_buff *nfrag;
1372
1373 nfrag = skb_clone(frag, GFP_ATOMIC);
1374 if (unlikely(!nfrag))
1375 return -ENOMEM;
1376
1377 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001378 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001379 frag = nfrag;
1380 *fragp = frag;
1381 }
1382
1383 if (end < len) {
1384 offset = end;
1385 continue;
1386 }
1387
1388 if (end > len &&
1389 unlikely((err = pskb_trim(frag, len - offset))))
1390 return err;
1391
1392 if (frag->next)
1393 skb_drop_list(&frag->next);
1394 break;
1395 }
1396
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001397done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001398 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 skb->data_len -= skb->len - len;
1400 skb->len = len;
1401 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001402 skb->len = len;
1403 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001404 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
1407 return 0;
1408}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001409EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411/**
1412 * __pskb_pull_tail - advance tail of skb header
1413 * @skb: buffer to reallocate
1414 * @delta: number of bytes to advance tail
1415 *
1416 * The function makes a sense only on a fragmented &sk_buff,
1417 * it expands header moving its tail forward and copying necessary
1418 * data from fragmented part.
1419 *
1420 * &sk_buff MUST have reference count of 1.
1421 *
1422 * Returns %NULL (and &sk_buff does not change) if pull failed
1423 * or value of new tail of skb in the case of success.
1424 *
1425 * All the pointers pointing into skb header may change and must be
1426 * reloaded after call to this function.
1427 */
1428
1429/* Moves tail of skb head forward, copying data from fragmented part,
1430 * when it is necessary.
1431 * 1. It may fail due to malloc failure.
1432 * 2. It may change skb pointers.
1433 *
1434 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1435 */
1436unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1437{
1438 /* If skb has not enough free space at tail, get new one
1439 * plus 128 bytes for future expansions. If we have enough
1440 * room at tail, reallocate without expansion only if skb is cloned.
1441 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001442 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 if (eat > 0 || skb_cloned(skb)) {
1445 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1446 GFP_ATOMIC))
1447 return NULL;
1448 }
1449
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001450 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 BUG();
1452
1453 /* Optimization: no fragments, no reasons to preestimate
1454 * size of pulled pages. Superb.
1455 */
David S. Miller21dc3302010-08-23 00:13:46 -07001456 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto pull_pages;
1458
1459 /* Estimate size of pulled pages. */
1460 eat = delta;
1461 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001462 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1463
1464 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001466 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
1469 /* If we need update frag list, we are in troubles.
1470 * Certainly, it possible to add an offset to skb data,
1471 * but taking into account that pulling is expected to
1472 * be very rare operation, it is worth to fight against
1473 * further bloating skb head and crucify ourselves here instead.
1474 * Pure masohism, indeed. 8)8)
1475 */
1476 if (eat) {
1477 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1478 struct sk_buff *clone = NULL;
1479 struct sk_buff *insp = NULL;
1480
1481 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001482 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (list->len <= eat) {
1485 /* Eaten as whole. */
1486 eat -= list->len;
1487 list = list->next;
1488 insp = list;
1489 } else {
1490 /* Eaten partially. */
1491
1492 if (skb_shared(list)) {
1493 /* Sucks! We need to fork list. :-( */
1494 clone = skb_clone(list, GFP_ATOMIC);
1495 if (!clone)
1496 return NULL;
1497 insp = list->next;
1498 list = clone;
1499 } else {
1500 /* This may be pulled without
1501 * problems. */
1502 insp = list;
1503 }
1504 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001505 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return NULL;
1507 }
1508 break;
1509 }
1510 } while (eat);
1511
1512 /* Free pulled out fragments. */
1513 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1514 skb_shinfo(skb)->frag_list = list->next;
1515 kfree_skb(list);
1516 }
1517 /* And insert new clone at head. */
1518 if (clone) {
1519 clone->next = list;
1520 skb_shinfo(skb)->frag_list = clone;
1521 }
1522 }
1523 /* Success! Now we may commit changes to skb data. */
1524
1525pull_pages:
1526 eat = delta;
1527 k = 0;
1528 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001529 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1530
1531 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001532 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001533 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } else {
1535 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1536 if (eat) {
1537 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001538 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 eat = 0;
1540 }
1541 k++;
1542 }
1543 }
1544 skb_shinfo(skb)->nr_frags = k;
1545
1546 skb->tail += delta;
1547 skb->data_len -= delta;
1548
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001549 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001551EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Eric Dumazet22019b12011-07-29 18:37:31 +00001553/**
1554 * skb_copy_bits - copy bits from skb to kernel buffer
1555 * @skb: source skb
1556 * @offset: offset in source
1557 * @to: destination buffer
1558 * @len: number of bytes to copy
1559 *
1560 * Copy the specified number of bytes from the source skb to the
1561 * destination buffer.
1562 *
1563 * CAUTION ! :
1564 * If its prototype is ever changed,
1565 * check arch/{*}/net/{*}.S files,
1566 * since it is called from BPF assembly code.
1567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1569{
David S. Miller1a028e52007-04-27 15:21:23 -07001570 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001571 struct sk_buff *frag_iter;
1572 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (offset > (int)skb->len - len)
1575 goto fault;
1576
1577 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001578 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (copy > len)
1580 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001581 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if ((len -= copy) == 0)
1583 return 0;
1584 offset += copy;
1585 to += copy;
1586 }
1587
1588 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001589 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001590 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001592 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001593
Eric Dumazet51c56b02012-04-05 11:35:15 +02001594 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if ((copy = end - offset) > 0) {
1596 u8 *vaddr;
1597
1598 if (copy > len)
1599 copy = len;
1600
Eric Dumazet51c56b02012-04-05 11:35:15 +02001601 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001603 vaddr + f->page_offset + offset - start,
1604 copy);
1605 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if ((len -= copy) == 0)
1608 return 0;
1609 offset += copy;
1610 to += copy;
1611 }
David S. Miller1a028e52007-04-27 15:21:23 -07001612 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614
David S. Millerfbb398a2009-06-09 00:18:59 -07001615 skb_walk_frags(skb, frag_iter) {
1616 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
David S. Millerfbb398a2009-06-09 00:18:59 -07001618 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
David S. Millerfbb398a2009-06-09 00:18:59 -07001620 end = start + frag_iter->len;
1621 if ((copy = end - offset) > 0) {
1622 if (copy > len)
1623 copy = len;
1624 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1625 goto fault;
1626 if ((len -= copy) == 0)
1627 return 0;
1628 offset += copy;
1629 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001631 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Shirley Maa6686f22011-07-06 12:22:12 +00001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (!len)
1635 return 0;
1636
1637fault:
1638 return -EFAULT;
1639}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001640EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jens Axboe9c55e012007-11-06 23:30:13 -08001642/*
1643 * Callback from splice_to_pipe(), if we need to release some pages
1644 * at the end of the spd in case we error'ed out in filling the pipe.
1645 */
1646static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1647{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001648 put_page(spd->pages[i]);
1649}
Jens Axboe9c55e012007-11-06 23:30:13 -08001650
David S. Millera108d5f2012-04-23 23:06:11 -04001651static struct page *linear_to_page(struct page *page, unsigned int *len,
1652 unsigned int *offset,
1653 struct sk_buff *skb, struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001654{
Eric Dumazet5640f762012-09-23 23:04:42 +00001655 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001656
Eric Dumazet5640f762012-09-23 23:04:42 +00001657 if (!sk_page_frag_refill(sk, pfrag))
1658 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001659
Eric Dumazet5640f762012-09-23 23:04:42 +00001660 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001661
Eric Dumazet5640f762012-09-23 23:04:42 +00001662 memcpy(page_address(pfrag->page) + pfrag->offset,
1663 page_address(page) + *offset, *len);
1664 *offset = pfrag->offset;
1665 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001666
Eric Dumazet5640f762012-09-23 23:04:42 +00001667 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001668}
1669
Eric Dumazet41c73a02012-04-22 12:26:16 +00001670static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1671 struct page *page,
1672 unsigned int offset)
1673{
1674 return spd->nr_pages &&
1675 spd->pages[spd->nr_pages - 1] == page &&
1676 (spd->partial[spd->nr_pages - 1].offset +
1677 spd->partial[spd->nr_pages - 1].len == offset);
1678}
1679
Jens Axboe9c55e012007-11-06 23:30:13 -08001680/*
1681 * Fill page/offset/length into spd, if it can hold more pages.
1682 */
David S. Millera108d5f2012-04-23 23:06:11 -04001683static bool spd_fill_page(struct splice_pipe_desc *spd,
1684 struct pipe_inode_info *pipe, struct page *page,
1685 unsigned int *len, unsigned int offset,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001686 struct sk_buff *skb, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001687 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001688{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001689 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001690 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001691
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001692 if (linear) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001693 page = linear_to_page(page, len, &offset, skb, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001694 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001695 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001696 }
1697 if (spd_can_coalesce(spd, page, offset)) {
1698 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001699 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001700 }
1701 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001702 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001703 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001704 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001705 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001706
David S. Millera108d5f2012-04-23 23:06:11 -04001707 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001708}
1709
David S. Millera108d5f2012-04-23 23:06:11 -04001710static bool __splice_segment(struct page *page, unsigned int poff,
1711 unsigned int plen, unsigned int *off,
1712 unsigned int *len, struct sk_buff *skb,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001713 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001714 struct sock *sk,
1715 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001716{
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001717 unsigned int flen;
1718
Octavian Purdila2870c432008-07-15 00:49:11 -07001719 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001720 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001721
1722 /* skip this segment if already processed */
1723 if (*off >= plen) {
1724 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001725 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001726 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001727
Octavian Purdila2870c432008-07-15 00:49:11 -07001728 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001729 poff += *off;
1730 plen -= *off;
1731 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001732
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001733 flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001734
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001735 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1736 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001737
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001738 *len -= flen;
Octavian Purdila2870c432008-07-15 00:49:11 -07001739
David S. Millera108d5f2012-04-23 23:06:11 -04001740 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001741}
1742
1743/*
David S. Millera108d5f2012-04-23 23:06:11 -04001744 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001745 * pipe is full or if we already spliced the requested length.
1746 */
David S. Millera108d5f2012-04-23 23:06:11 -04001747static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1748 unsigned int *offset, unsigned int *len,
1749 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001750{
1751 int seg;
1752
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001753 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001754 * If skb->head_frag is set, this 'linear' part is backed by a
1755 * fragment, and if the head is not shared with any clones then
1756 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001757 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001758 if (__splice_segment(virt_to_page(skb->data),
1759 (unsigned long) skb->data & (PAGE_SIZE - 1),
1760 skb_headlen(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001761 offset, len, skb, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001762 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001763 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001764 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001765
1766 /*
1767 * then map the fragments
1768 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001769 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1770 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1771
Ian Campbellea2ab692011-08-22 23:44:58 +00001772 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001773 f->page_offset, skb_frag_size(f),
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001774 offset, len, skb, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001775 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001776 }
1777
David S. Millera108d5f2012-04-23 23:06:11 -04001778 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001779}
1780
1781/*
1782 * Map data from the skb to a pipe. Should handle both the linear part,
1783 * the fragments, and the frag list. It does NOT handle frag lists within
1784 * the frag list, if such a thing exists. We'd probably need to recurse to
1785 * handle that cleanly.
1786 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001787int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001788 struct pipe_inode_info *pipe, unsigned int tlen,
1789 unsigned int flags)
1790{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001791 struct partial_page partial[MAX_SKB_FRAGS];
1792 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001793 struct splice_pipe_desc spd = {
1794 .pages = pages,
1795 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001796 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001797 .flags = flags,
1798 .ops = &sock_pipe_buf_ops,
1799 .spd_release = sock_spd_release,
1800 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001801 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001802 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001803 int ret = 0;
1804
Jens Axboe9c55e012007-11-06 23:30:13 -08001805 /*
1806 * __skb_splice_bits() only fails if the output has no room left,
1807 * so no point in going over the frag_list for the error case.
1808 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001809 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001810 goto done;
1811 else if (!tlen)
1812 goto done;
1813
1814 /*
1815 * now see if we have a frag_list to map
1816 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001817 skb_walk_frags(skb, frag_iter) {
1818 if (!tlen)
1819 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001820 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001821 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001822 }
1823
1824done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001826 /*
1827 * Drop the socket lock, otherwise we have reverse
1828 * locking dependencies between sk_lock and i_mutex
1829 * here as compared to sendfile(). We enter here
1830 * with the socket lock held, and splice_to_pipe() will
1831 * grab the pipe inode lock. For sendfile() emulation,
1832 * we call into ->sendpage() with the i_mutex lock held
1833 * and networking will grab the socket lock.
1834 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001835 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001836 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001837 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001838 }
1839
Jens Axboe35f3d142010-05-20 10:43:18 +02001840 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001841}
1842
Herbert Xu357b40a2005-04-19 22:30:14 -07001843/**
1844 * skb_store_bits - store bits from kernel buffer to skb
1845 * @skb: destination buffer
1846 * @offset: offset in destination
1847 * @from: source buffer
1848 * @len: number of bytes to copy
1849 *
1850 * Copy the specified number of bytes from the source buffer to the
1851 * destination skb. This function handles all the messy bits of
1852 * traversing fragment lists and such.
1853 */
1854
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001855int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001856{
David S. Miller1a028e52007-04-27 15:21:23 -07001857 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001858 struct sk_buff *frag_iter;
1859 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001860
1861 if (offset > (int)skb->len - len)
1862 goto fault;
1863
David S. Miller1a028e52007-04-27 15:21:23 -07001864 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001865 if (copy > len)
1866 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001867 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001868 if ((len -= copy) == 0)
1869 return 0;
1870 offset += copy;
1871 from += copy;
1872 }
1873
1874 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1875 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001876 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001877
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001878 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001879
Eric Dumazet9e903e02011-10-18 21:00:24 +00001880 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001881 if ((copy = end - offset) > 0) {
1882 u8 *vaddr;
1883
1884 if (copy > len)
1885 copy = len;
1886
Eric Dumazet51c56b02012-04-05 11:35:15 +02001887 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001888 memcpy(vaddr + frag->page_offset + offset - start,
1889 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001890 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001891
1892 if ((len -= copy) == 0)
1893 return 0;
1894 offset += copy;
1895 from += copy;
1896 }
David S. Miller1a028e52007-04-27 15:21:23 -07001897 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001898 }
1899
David S. Millerfbb398a2009-06-09 00:18:59 -07001900 skb_walk_frags(skb, frag_iter) {
1901 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001902
David S. Millerfbb398a2009-06-09 00:18:59 -07001903 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001904
David S. Millerfbb398a2009-06-09 00:18:59 -07001905 end = start + frag_iter->len;
1906 if ((copy = end - offset) > 0) {
1907 if (copy > len)
1908 copy = len;
1909 if (skb_store_bits(frag_iter, offset - start,
1910 from, copy))
1911 goto fault;
1912 if ((len -= copy) == 0)
1913 return 0;
1914 offset += copy;
1915 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001916 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001917 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001918 }
1919 if (!len)
1920 return 0;
1921
1922fault:
1923 return -EFAULT;
1924}
Herbert Xu357b40a2005-04-19 22:30:14 -07001925EXPORT_SYMBOL(skb_store_bits);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/* Checksum skb data. */
1928
Al Viro2bbbc862006-11-14 21:37:14 -08001929__wsum skb_checksum(const struct sk_buff *skb, int offset,
1930 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
David S. Miller1a028e52007-04-27 15:21:23 -07001932 int start = skb_headlen(skb);
1933 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001934 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 int pos = 0;
1936
1937 /* Checksum header. */
1938 if (copy > 0) {
1939 if (copy > len)
1940 copy = len;
1941 csum = csum_partial(skb->data + offset, copy, csum);
1942 if ((len -= copy) == 0)
1943 return csum;
1944 offset += copy;
1945 pos = copy;
1946 }
1947
1948 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001949 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001950 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001952 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001953
Eric Dumazet51c56b02012-04-05 11:35:15 +02001954 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001956 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 if (copy > len)
1960 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001961 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001962 csum2 = csum_partial(vaddr + frag->page_offset +
1963 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001964 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 csum = csum_block_add(csum, csum2, pos);
1966 if (!(len -= copy))
1967 return csum;
1968 offset += copy;
1969 pos += copy;
1970 }
David S. Miller1a028e52007-04-27 15:21:23 -07001971 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
David S. Millerfbb398a2009-06-09 00:18:59 -07001974 skb_walk_frags(skb, frag_iter) {
1975 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
David S. Millerfbb398a2009-06-09 00:18:59 -07001977 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
David S. Millerfbb398a2009-06-09 00:18:59 -07001979 end = start + frag_iter->len;
1980 if ((copy = end - offset) > 0) {
1981 __wsum csum2;
1982 if (copy > len)
1983 copy = len;
1984 csum2 = skb_checksum(frag_iter, offset - start,
1985 copy, 0);
1986 csum = csum_block_add(csum, csum2, pos);
1987 if ((len -= copy) == 0)
1988 return csum;
1989 offset += copy;
1990 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001992 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001994 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 return csum;
1997}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001998EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000/* Both of above in one bottle. */
2001
Al Viro81d77662006-11-14 21:37:33 -08002002__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2003 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
David S. Miller1a028e52007-04-27 15:21:23 -07002005 int start = skb_headlen(skb);
2006 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002007 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int pos = 0;
2009
2010 /* Copy header. */
2011 if (copy > 0) {
2012 if (copy > len)
2013 copy = len;
2014 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2015 copy, csum);
2016 if ((len -= copy) == 0)
2017 return csum;
2018 offset += copy;
2019 to += copy;
2020 pos = copy;
2021 }
2022
2023 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002024 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002026 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002027
Eric Dumazet9e903e02011-10-18 21:00:24 +00002028 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002030 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 u8 *vaddr;
2032 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2033
2034 if (copy > len)
2035 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002036 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002038 frag->page_offset +
2039 offset - start, to,
2040 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002041 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 csum = csum_block_add(csum, csum2, pos);
2043 if (!(len -= copy))
2044 return csum;
2045 offset += copy;
2046 to += copy;
2047 pos += copy;
2048 }
David S. Miller1a028e52007-04-27 15:21:23 -07002049 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
David S. Millerfbb398a2009-06-09 00:18:59 -07002052 skb_walk_frags(skb, frag_iter) {
2053 __wsum csum2;
2054 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
David S. Millerfbb398a2009-06-09 00:18:59 -07002056 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
David S. Millerfbb398a2009-06-09 00:18:59 -07002058 end = start + frag_iter->len;
2059 if ((copy = end - offset) > 0) {
2060 if (copy > len)
2061 copy = len;
2062 csum2 = skb_copy_and_csum_bits(frag_iter,
2063 offset - start,
2064 to, copy, 0);
2065 csum = csum_block_add(csum, csum2, pos);
2066 if ((len -= copy) == 0)
2067 return csum;
2068 offset += copy;
2069 to += copy;
2070 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002072 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002074 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return csum;
2076}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002077EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2080{
Al Virod3bc23e2006-11-14 21:24:49 -08002081 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 long csstart;
2083
Patrick McHardy84fa7932006-08-29 16:44:56 -07002084 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002085 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 else
2087 csstart = skb_headlen(skb);
2088
Kris Katterjohn09a62662006-01-08 22:24:28 -08002089 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002091 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 csum = 0;
2094 if (csstart != skb->len)
2095 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2096 skb->len - csstart, 0);
2097
Patrick McHardy84fa7932006-08-29 16:44:56 -07002098 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002099 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Al Virod3bc23e2006-11-14 21:24:49 -08002101 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002104EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106/**
2107 * skb_dequeue - remove from the head of the queue
2108 * @list: list to dequeue from
2109 *
2110 * Remove the head of the list. The list lock is taken so the function
2111 * may be used safely with other locking list functions. The head item is
2112 * returned or %NULL if the list is empty.
2113 */
2114
2115struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2116{
2117 unsigned long flags;
2118 struct sk_buff *result;
2119
2120 spin_lock_irqsave(&list->lock, flags);
2121 result = __skb_dequeue(list);
2122 spin_unlock_irqrestore(&list->lock, flags);
2123 return result;
2124}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002125EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127/**
2128 * skb_dequeue_tail - remove from the tail of the queue
2129 * @list: list to dequeue from
2130 *
2131 * Remove the tail of the list. The list lock is taken so the function
2132 * may be used safely with other locking list functions. The tail item is
2133 * returned or %NULL if the list is empty.
2134 */
2135struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2136{
2137 unsigned long flags;
2138 struct sk_buff *result;
2139
2140 spin_lock_irqsave(&list->lock, flags);
2141 result = __skb_dequeue_tail(list);
2142 spin_unlock_irqrestore(&list->lock, flags);
2143 return result;
2144}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002145EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147/**
2148 * skb_queue_purge - empty a list
2149 * @list: list to empty
2150 *
2151 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2152 * the list and one reference dropped. This function takes the list
2153 * lock and is atomic with respect to other list locking functions.
2154 */
2155void skb_queue_purge(struct sk_buff_head *list)
2156{
2157 struct sk_buff *skb;
2158 while ((skb = skb_dequeue(list)) != NULL)
2159 kfree_skb(skb);
2160}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002161EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163/**
2164 * skb_queue_head - queue a buffer at the list head
2165 * @list: list to use
2166 * @newsk: buffer to queue
2167 *
2168 * Queue a buffer at the start of the list. This function takes the
2169 * list lock and can be used safely with other locking &sk_buff functions
2170 * safely.
2171 *
2172 * A buffer cannot be placed on two lists at the same time.
2173 */
2174void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2175{
2176 unsigned long flags;
2177
2178 spin_lock_irqsave(&list->lock, flags);
2179 __skb_queue_head(list, newsk);
2180 spin_unlock_irqrestore(&list->lock, flags);
2181}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002182EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184/**
2185 * skb_queue_tail - queue a buffer at the list tail
2186 * @list: list to use
2187 * @newsk: buffer to queue
2188 *
2189 * Queue a buffer at the tail of the list. This function takes the
2190 * list lock and can be used safely with other locking &sk_buff functions
2191 * safely.
2192 *
2193 * A buffer cannot be placed on two lists at the same time.
2194 */
2195void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2196{
2197 unsigned long flags;
2198
2199 spin_lock_irqsave(&list->lock, flags);
2200 __skb_queue_tail(list, newsk);
2201 spin_unlock_irqrestore(&list->lock, flags);
2202}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002203EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205/**
2206 * skb_unlink - remove a buffer from a list
2207 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002208 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 *
David S. Miller8728b832005-08-09 19:25:21 -07002210 * Remove a packet from a list. The list locks are taken and this
2211 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 *
David S. Miller8728b832005-08-09 19:25:21 -07002213 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
David S. Miller8728b832005-08-09 19:25:21 -07002215void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
David S. Miller8728b832005-08-09 19:25:21 -07002217 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
David S. Miller8728b832005-08-09 19:25:21 -07002219 spin_lock_irqsave(&list->lock, flags);
2220 __skb_unlink(skb, list);
2221 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002223EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/**
2226 * skb_append - append a buffer
2227 * @old: buffer to insert after
2228 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002229 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 *
2231 * Place a packet after a given packet in a list. The list locks are taken
2232 * and this function is atomic with respect to other list locked calls.
2233 * A buffer cannot be placed on two lists at the same time.
2234 */
David S. Miller8728b832005-08-09 19:25:21 -07002235void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 unsigned long flags;
2238
David S. Miller8728b832005-08-09 19:25:21 -07002239 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002240 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002241 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002243EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245/**
2246 * skb_insert - insert a buffer
2247 * @old: buffer to insert before
2248 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002249 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 *
David S. Miller8728b832005-08-09 19:25:21 -07002251 * Place a packet before a given packet in a list. The list locks are
2252 * taken and this function is atomic with respect to other list locked
2253 * calls.
2254 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 * A buffer cannot be placed on two lists at the same time.
2256 */
David S. Miller8728b832005-08-09 19:25:21 -07002257void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 unsigned long flags;
2260
David S. Miller8728b832005-08-09 19:25:21 -07002261 spin_lock_irqsave(&list->lock, flags);
2262 __skb_insert(newsk, old->prev, old, list);
2263 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002265EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267static inline void skb_split_inside_header(struct sk_buff *skb,
2268 struct sk_buff* skb1,
2269 const u32 len, const int pos)
2270{
2271 int i;
2272
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002273 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2274 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* And move data appendix as is. */
2276 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2277 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2278
2279 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2280 skb_shinfo(skb)->nr_frags = 0;
2281 skb1->data_len = skb->data_len;
2282 skb1->len += skb1->data_len;
2283 skb->data_len = 0;
2284 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002285 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288static inline void skb_split_no_header(struct sk_buff *skb,
2289 struct sk_buff* skb1,
2290 const u32 len, int pos)
2291{
2292 int i, k = 0;
2293 const int nfrags = skb_shinfo(skb)->nr_frags;
2294
2295 skb_shinfo(skb)->nr_frags = 0;
2296 skb1->len = skb1->data_len = skb->len - len;
2297 skb->len = len;
2298 skb->data_len = len - pos;
2299
2300 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002301 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (pos + size > len) {
2304 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2305
2306 if (pos < len) {
2307 /* Split frag.
2308 * We have two variants in this case:
2309 * 1. Move all the frag to the second
2310 * part, if it is possible. F.e.
2311 * this approach is mandatory for TUX,
2312 * where splitting is expensive.
2313 * 2. Split is accurately. We make this.
2314 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002315 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002317 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2318 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 skb_shinfo(skb)->nr_frags++;
2320 }
2321 k++;
2322 } else
2323 skb_shinfo(skb)->nr_frags++;
2324 pos += size;
2325 }
2326 skb_shinfo(skb1)->nr_frags = k;
2327}
2328
2329/**
2330 * skb_split - Split fragmented skb to two parts at length len.
2331 * @skb: the buffer to split
2332 * @skb1: the buffer to receive the second part
2333 * @len: new length for skb
2334 */
2335void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2336{
2337 int pos = skb_headlen(skb);
2338
2339 if (len < pos) /* Split line is inside header. */
2340 skb_split_inside_header(skb, skb1, len, pos);
2341 else /* Second chunk has no header, nothing to copy. */
2342 skb_split_no_header(skb, skb1, len, pos);
2343}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002344EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002346/* Shifting from/to a cloned skb is a no-go.
2347 *
2348 * Caller cannot keep skb_shinfo related pointers past calling here!
2349 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002350static int skb_prepare_for_shift(struct sk_buff *skb)
2351{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002352 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002353}
2354
2355/**
2356 * skb_shift - Shifts paged data partially from skb to another
2357 * @tgt: buffer into which tail data gets added
2358 * @skb: buffer from which the paged data comes from
2359 * @shiftlen: shift up to this many bytes
2360 *
2361 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002362 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002363 * It's up to caller to free skb if everything was shifted.
2364 *
2365 * If @tgt runs out of frags, the whole operation is aborted.
2366 *
2367 * Skb cannot include anything else but paged data while tgt is allowed
2368 * to have non-paged data as well.
2369 *
2370 * TODO: full sized shift could be optimized but that would need
2371 * specialized skb free'er to handle frags without up-to-date nr_frags.
2372 */
2373int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2374{
2375 int from, to, merge, todo;
2376 struct skb_frag_struct *fragfrom, *fragto;
2377
2378 BUG_ON(shiftlen > skb->len);
2379 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2380
2381 todo = shiftlen;
2382 from = 0;
2383 to = skb_shinfo(tgt)->nr_frags;
2384 fragfrom = &skb_shinfo(skb)->frags[from];
2385
2386 /* Actual merge is delayed until the point when we know we can
2387 * commit all, so that we don't have to undo partial changes
2388 */
2389 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002390 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2391 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002392 merge = -1;
2393 } else {
2394 merge = to - 1;
2395
Eric Dumazet9e903e02011-10-18 21:00:24 +00002396 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002397 if (todo < 0) {
2398 if (skb_prepare_for_shift(skb) ||
2399 skb_prepare_for_shift(tgt))
2400 return 0;
2401
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002402 /* All previous frag pointers might be stale! */
2403 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002404 fragto = &skb_shinfo(tgt)->frags[merge];
2405
Eric Dumazet9e903e02011-10-18 21:00:24 +00002406 skb_frag_size_add(fragto, shiftlen);
2407 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002408 fragfrom->page_offset += shiftlen;
2409
2410 goto onlymerged;
2411 }
2412
2413 from++;
2414 }
2415
2416 /* Skip full, not-fitting skb to avoid expensive operations */
2417 if ((shiftlen == skb->len) &&
2418 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2419 return 0;
2420
2421 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2422 return 0;
2423
2424 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2425 if (to == MAX_SKB_FRAGS)
2426 return 0;
2427
2428 fragfrom = &skb_shinfo(skb)->frags[from];
2429 fragto = &skb_shinfo(tgt)->frags[to];
2430
Eric Dumazet9e903e02011-10-18 21:00:24 +00002431 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002432 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002433 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002434 from++;
2435 to++;
2436
2437 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002438 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002439 fragto->page = fragfrom->page;
2440 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002441 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002442
2443 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002444 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002445 todo = 0;
2446
2447 to++;
2448 break;
2449 }
2450 }
2451
2452 /* Ready to "commit" this state change to tgt */
2453 skb_shinfo(tgt)->nr_frags = to;
2454
2455 if (merge >= 0) {
2456 fragfrom = &skb_shinfo(skb)->frags[0];
2457 fragto = &skb_shinfo(tgt)->frags[merge];
2458
Eric Dumazet9e903e02011-10-18 21:00:24 +00002459 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002460 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002461 }
2462
2463 /* Reposition in the original skb */
2464 to = 0;
2465 while (from < skb_shinfo(skb)->nr_frags)
2466 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2467 skb_shinfo(skb)->nr_frags = to;
2468
2469 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2470
2471onlymerged:
2472 /* Most likely the tgt won't ever need its checksum anymore, skb on
2473 * the other hand might need it if it needs to be resent
2474 */
2475 tgt->ip_summed = CHECKSUM_PARTIAL;
2476 skb->ip_summed = CHECKSUM_PARTIAL;
2477
2478 /* Yak, is it really working this way? Some helper please? */
2479 skb->len -= shiftlen;
2480 skb->data_len -= shiftlen;
2481 skb->truesize -= shiftlen;
2482 tgt->len += shiftlen;
2483 tgt->data_len += shiftlen;
2484 tgt->truesize += shiftlen;
2485
2486 return shiftlen;
2487}
2488
Thomas Graf677e90e2005-06-23 20:59:51 -07002489/**
2490 * skb_prepare_seq_read - Prepare a sequential read of skb data
2491 * @skb: the buffer to read
2492 * @from: lower offset of data to be read
2493 * @to: upper offset of data to be read
2494 * @st: state variable
2495 *
2496 * Initializes the specified state variable. Must be called before
2497 * invoking skb_seq_read() for the first time.
2498 */
2499void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2500 unsigned int to, struct skb_seq_state *st)
2501{
2502 st->lower_offset = from;
2503 st->upper_offset = to;
2504 st->root_skb = st->cur_skb = skb;
2505 st->frag_idx = st->stepped_offset = 0;
2506 st->frag_data = NULL;
2507}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002508EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002509
2510/**
2511 * skb_seq_read - Sequentially read skb data
2512 * @consumed: number of bytes consumed by the caller so far
2513 * @data: destination pointer for data to be returned
2514 * @st: state variable
2515 *
2516 * Reads a block of skb data at &consumed relative to the
2517 * lower offset specified to skb_prepare_seq_read(). Assigns
2518 * the head of the data block to &data and returns the length
2519 * of the block or 0 if the end of the skb data or the upper
2520 * offset has been reached.
2521 *
2522 * The caller is not required to consume all of the data
2523 * returned, i.e. &consumed is typically set to the number
2524 * of bytes already consumed and the next call to
2525 * skb_seq_read() will return the remaining part of the block.
2526 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002527 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002528 * this limitation is the cost for zerocopy seqeuental
2529 * reads of potentially non linear data.
2530 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002531 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002532 * at the moment, state->root_skb could be replaced with
2533 * a stack for this purpose.
2534 */
2535unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2536 struct skb_seq_state *st)
2537{
2538 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2539 skb_frag_t *frag;
2540
2541 if (unlikely(abs_offset >= st->upper_offset))
2542 return 0;
2543
2544next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002545 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002546
Thomas Chenault995b3372009-05-18 21:43:27 -07002547 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002548 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002549 return block_limit - abs_offset;
2550 }
2551
2552 if (st->frag_idx == 0 && !st->frag_data)
2553 st->stepped_offset += skb_headlen(st->cur_skb);
2554
2555 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2556 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002557 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002558
2559 if (abs_offset < block_limit) {
2560 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002561 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002562
2563 *data = (u8 *) st->frag_data + frag->page_offset +
2564 (abs_offset - st->stepped_offset);
2565
2566 return block_limit - abs_offset;
2567 }
2568
2569 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002570 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002571 st->frag_data = NULL;
2572 }
2573
2574 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002575 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002576 }
2577
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002578 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002579 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002580 st->frag_data = NULL;
2581 }
2582
David S. Miller21dc3302010-08-23 00:13:46 -07002583 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002584 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002585 st->frag_idx = 0;
2586 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002587 } else if (st->cur_skb->next) {
2588 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002589 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002590 goto next_skb;
2591 }
2592
2593 return 0;
2594}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002595EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002596
2597/**
2598 * skb_abort_seq_read - Abort a sequential read of skb data
2599 * @st: state variable
2600 *
2601 * Must be called if skb_seq_read() was not called until it
2602 * returned 0.
2603 */
2604void skb_abort_seq_read(struct skb_seq_state *st)
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}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002609EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002610
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002611#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2612
2613static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2614 struct ts_config *conf,
2615 struct ts_state *state)
2616{
2617 return skb_seq_read(offset, text, TS_SKB_CB(state));
2618}
2619
2620static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2621{
2622 skb_abort_seq_read(TS_SKB_CB(state));
2623}
2624
2625/**
2626 * skb_find_text - Find a text pattern in skb data
2627 * @skb: the buffer to look in
2628 * @from: search offset
2629 * @to: search limit
2630 * @config: textsearch configuration
2631 * @state: uninitialized textsearch state variable
2632 *
2633 * Finds a pattern in the skb data according to the specified
2634 * textsearch configuration. Use textsearch_next() to retrieve
2635 * subsequent occurrences of the pattern. Returns the offset
2636 * to the first occurrence or UINT_MAX if no match was found.
2637 */
2638unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2639 unsigned int to, struct ts_config *config,
2640 struct ts_state *state)
2641{
Phil Oesterf72b9482006-06-26 00:00:57 -07002642 unsigned int ret;
2643
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002644 config->get_next_block = skb_ts_get_next_block;
2645 config->finish = skb_ts_finish;
2646
2647 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2648
Phil Oesterf72b9482006-06-26 00:00:57 -07002649 ret = textsearch_find(config, state);
2650 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002651}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002652EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002653
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002654/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002655 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002656 * @sk: sock structure
2657 * @skb: skb structure to be appened with user data.
2658 * @getfrag: call back function to be used for getting the user data
2659 * @from: pointer to user message iov
2660 * @length: length of the iov message
2661 *
2662 * Description: This procedure append the user data in the fragment part
2663 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2664 */
2665int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002666 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002667 int len, int odd, struct sk_buff *skb),
2668 void *from, int length)
2669{
Eric Dumazetb2111722012-12-28 06:06:37 +00002670 int frg_cnt = skb_shinfo(skb)->nr_frags;
2671 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002672 int offset = 0;
2673 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002674 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002675
2676 do {
2677 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002678 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002679 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002680
Eric Dumazetb2111722012-12-28 06:06:37 +00002681 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002682 return -ENOMEM;
2683
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002684 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002685 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002686
Eric Dumazetb2111722012-12-28 06:06:37 +00002687 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2688 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002689 if (ret < 0)
2690 return -EFAULT;
2691
2692 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002693 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2694 copy);
2695 frg_cnt++;
2696 pfrag->offset += copy;
2697 get_page(pfrag->page);
2698
2699 skb->truesize += copy;
2700 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002701 skb->len += copy;
2702 skb->data_len += copy;
2703 offset += copy;
2704 length -= copy;
2705
2706 } while (length > 0);
2707
2708 return 0;
2709}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002710EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002711
Herbert Xucbb042f2006-03-20 22:43:56 -08002712/**
2713 * skb_pull_rcsum - pull skb and update receive checksum
2714 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002715 * @len: length of data pulled
2716 *
2717 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002718 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002719 * receive path processing instead of skb_pull unless you know
2720 * that the checksum difference is zero (e.g., a valid IP header)
2721 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002722 */
2723unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2724{
2725 BUG_ON(len > skb->len);
2726 skb->len -= len;
2727 BUG_ON(skb->len < skb->data_len);
2728 skb_postpull_rcsum(skb, skb->data, len);
2729 return skb->data += len;
2730}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002731EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2732
Herbert Xuf4c50d92006-06-22 03:02:40 -07002733/**
2734 * skb_segment - Perform protocol segmentation on skb.
2735 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002736 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002737 *
2738 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002739 * a pointer to the first in a list of new skbs for the segments.
2740 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002741 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002742struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002743{
2744 struct sk_buff *segs = NULL;
2745 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002746 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002747 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002748 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002749 unsigned int offset = doffset;
2750 unsigned int headroom;
2751 unsigned int len;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002752 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002753 int nfrags = skb_shinfo(skb)->nr_frags;
2754 int err = -ENOMEM;
2755 int i = 0;
2756 int pos;
2757
2758 __skb_push(skb, doffset);
2759 headroom = skb_headroom(skb);
2760 pos = skb_headlen(skb);
2761
2762 do {
2763 struct sk_buff *nskb;
2764 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002765 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002766 int size;
2767
2768 len = skb->len - offset;
2769 if (len > mss)
2770 len = mss;
2771
2772 hsize = skb_headlen(skb) - offset;
2773 if (hsize < 0)
2774 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002775 if (hsize > len || !sg)
2776 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002777
Herbert Xu89319d382008-12-15 23:26:06 -08002778 if (!hsize && i >= nfrags) {
2779 BUG_ON(fskb->len != len);
2780
2781 pos += len;
2782 nskb = skb_clone(fskb, GFP_ATOMIC);
2783 fskb = fskb->next;
2784
2785 if (unlikely(!nskb))
2786 goto err;
2787
Alexander Duyckec47ea82012-05-04 14:26:56 +00002788 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002789 if (skb_cow_head(nskb, doffset + headroom)) {
2790 kfree_skb(nskb);
2791 goto err;
2792 }
2793
Alexander Duyckec47ea82012-05-04 14:26:56 +00002794 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002795 skb_release_head_state(nskb);
2796 __skb_push(nskb, doffset);
2797 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002798 nskb = __alloc_skb(hsize + doffset + headroom,
2799 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2800 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002801
2802 if (unlikely(!nskb))
2803 goto err;
2804
2805 skb_reserve(nskb, headroom);
2806 __skb_put(nskb, doffset);
2807 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002808
2809 if (segs)
2810 tail->next = nskb;
2811 else
2812 segs = nskb;
2813 tail = nskb;
2814
Herbert Xu6f85a122008-08-15 14:55:02 -07002815 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002816 nskb->mac_len = skb->mac_len;
2817
Eric Dumazet3d3be432010-09-01 00:50:51 +00002818 /* nskb and skb might have different headroom */
2819 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2820 nskb->csum_start += skb_headroom(nskb) - headroom;
2821
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002822 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002823 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002824 nskb->transport_header = (nskb->network_header +
2825 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002826 skb_copy_from_linear_data(skb, nskb->data, doffset);
2827
Herbert Xu2f181852009-03-28 23:39:18 -07002828 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002829 continue;
2830
Herbert Xuf4c50d92006-06-22 03:02:40 -07002831 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002832 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002833 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2834 skb_put(nskb, len),
2835 len, 0);
2836 continue;
2837 }
2838
2839 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002840
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002841 skb_copy_from_linear_data_offset(skb, offset,
2842 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002843
Herbert Xu89319d382008-12-15 23:26:06 -08002844 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002845 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002846 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002847 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002848
2849 if (pos < offset) {
2850 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002851 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002852 }
2853
Herbert Xu89319d382008-12-15 23:26:06 -08002854 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002855
2856 if (pos + size <= offset + len) {
2857 i++;
2858 pos += size;
2859 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002860 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002861 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002862 }
2863
2864 frag++;
2865 }
2866
Herbert Xu89319d382008-12-15 23:26:06 -08002867 if (pos < offset + len) {
2868 struct sk_buff *fskb2 = fskb;
2869
2870 BUG_ON(pos + fskb->len != offset + len);
2871
2872 pos += fskb->len;
2873 fskb = fskb->next;
2874
2875 if (fskb2->next) {
2876 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2877 if (!fskb2)
2878 goto err;
2879 } else
2880 skb_get(fskb2);
2881
David S. Millerfbb398a2009-06-09 00:18:59 -07002882 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002883 skb_shinfo(nskb)->frag_list = fskb2;
2884 }
2885
2886skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002887 nskb->data_len = len - hsize;
2888 nskb->len += nskb->data_len;
2889 nskb->truesize += nskb->data_len;
2890 } while ((offset += len) < skb->len);
2891
2892 return segs;
2893
2894err:
2895 while ((skb = segs)) {
2896 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002897 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002898 }
2899 return ERR_PTR(err);
2900}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002901EXPORT_SYMBOL_GPL(skb_segment);
2902
Herbert Xu71d93b32008-12-15 23:42:33 -08002903int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2904{
2905 struct sk_buff *p = *head;
2906 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002907 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2908 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002909 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002910 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002911 unsigned int offset = skb_gro_offset(skb);
2912 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002913 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002914
Herbert Xu86911732009-01-29 14:19:50 +00002915 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002916 return -E2BIG;
2917
Herbert Xu9aaa1562009-05-26 18:50:33 +00002918 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002919 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002920 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002921 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002922 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002923 int i = skbinfo->nr_frags;
2924 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002925
Herbert Xu66e92fc2009-05-26 18:50:32 +00002926 offset -= headlen;
2927
2928 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002929 return -E2BIG;
2930
Herbert Xu9aaa1562009-05-26 18:50:33 +00002931 pinfo->nr_frags = nr_frags;
2932 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002933
Herbert Xu9aaa1562009-05-26 18:50:33 +00002934 frag = pinfo->frags + nr_frags;
2935 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002936 do {
2937 *--frag = *--frag2;
2938 } while (--i);
2939
2940 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002941 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002942
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002943 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002944 delta_truesize = skb->truesize -
2945 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002946
Herbert Xuf5572062009-01-14 20:40:03 -08002947 skb->truesize -= skb->data_len;
2948 skb->len -= skb->data_len;
2949 skb->data_len = 0;
2950
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002951 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08002952 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00002953 } else if (skb->head_frag) {
2954 int nr_frags = pinfo->nr_frags;
2955 skb_frag_t *frag = pinfo->frags + nr_frags;
2956 struct page *page = virt_to_head_page(skb->head);
2957 unsigned int first_size = headlen - offset;
2958 unsigned int first_offset;
2959
2960 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2961 return -E2BIG;
2962
2963 first_offset = skb->data -
2964 (unsigned char *)page_address(page) +
2965 offset;
2966
2967 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2968
2969 frag->page.p = page;
2970 frag->page_offset = first_offset;
2971 skb_frag_size_set(frag, first_size);
2972
2973 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2974 /* We dont need to clear skbinfo->nr_frags here */
2975
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002976 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00002977 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2978 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08002979 } else if (skb_gro_len(p) != pinfo->gso_size)
2980 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08002981
2982 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00002983 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08002984 if (unlikely(!nskb))
2985 return -ENOMEM;
2986
2987 __copy_skb_header(nskb, p);
2988 nskb->mac_len = p->mac_len;
2989
2990 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00002991 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002992
Herbert Xu86911732009-01-29 14:19:50 +00002993 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08002994 skb_set_network_header(nskb, skb_network_offset(p));
2995 skb_set_transport_header(nskb, skb_transport_offset(p));
2996
Herbert Xu86911732009-01-29 14:19:50 +00002997 __skb_pull(p, skb_gro_offset(p));
2998 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2999 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003000
Herbert Xu71d93b32008-12-15 23:42:33 -08003001 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003002 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003003 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003004 skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003005 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003006
3007 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003008 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003009 nskb->len += p->len;
3010
3011 *head = nskb;
3012 nskb->next = p->next;
3013 p->next = NULL;
3014
3015 p = nskb;
3016
3017merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003018 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003019 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003020 unsigned int eat = offset - headlen;
3021
3022 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003023 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003024 skb->data_len -= eat;
3025 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003026 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003027 }
3028
Herbert Xu67147ba2009-05-26 18:50:22 +00003029 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003030
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003031 NAPI_GRO_CB(p)->last->next = skb;
3032 NAPI_GRO_CB(p)->last = skb;
Herbert Xu71d93b32008-12-15 23:42:33 -08003033 skb_header_release(skb);
3034
Herbert Xu5d38a072009-01-04 16:13:40 -08003035done:
3036 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003037 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003038 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003039 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003040
3041 NAPI_GRO_CB(skb)->same_flow = 1;
3042 return 0;
3043}
3044EXPORT_SYMBOL_GPL(skb_gro_receive);
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046void __init skb_init(void)
3047{
3048 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3049 sizeof(struct sk_buff),
3050 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003051 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003052 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003053 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3054 (2*sizeof(struct sk_buff)) +
3055 sizeof(atomic_t),
3056 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003057 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003058 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
David Howells716ea3a2007-04-02 20:19:53 -07003061/**
3062 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3063 * @skb: Socket buffer containing the buffers to be mapped
3064 * @sg: The scatter-gather list to map into
3065 * @offset: The offset into the buffer's contents to start mapping
3066 * @len: Length of buffer space to be mapped
3067 *
3068 * Fill the specified scatter-gather list with mappings/pointers into a
3069 * region of the buffer space attached to a socket buffer.
3070 */
David S. Miller51c739d2007-10-30 21:29:29 -07003071static int
3072__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003073{
David S. Miller1a028e52007-04-27 15:21:23 -07003074 int start = skb_headlen(skb);
3075 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003076 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003077 int elt = 0;
3078
3079 if (copy > 0) {
3080 if (copy > len)
3081 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003082 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003083 elt++;
3084 if ((len -= copy) == 0)
3085 return elt;
3086 offset += copy;
3087 }
3088
3089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003090 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003091
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003092 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003093
Eric Dumazet9e903e02011-10-18 21:00:24 +00003094 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003095 if ((copy = end - offset) > 0) {
3096 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3097
3098 if (copy > len)
3099 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003100 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003101 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003102 elt++;
3103 if (!(len -= copy))
3104 return elt;
3105 offset += copy;
3106 }
David S. Miller1a028e52007-04-27 15:21:23 -07003107 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003108 }
3109
David S. Millerfbb398a2009-06-09 00:18:59 -07003110 skb_walk_frags(skb, frag_iter) {
3111 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003112
David S. Millerfbb398a2009-06-09 00:18:59 -07003113 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003114
David S. Millerfbb398a2009-06-09 00:18:59 -07003115 end = start + frag_iter->len;
3116 if ((copy = end - offset) > 0) {
3117 if (copy > len)
3118 copy = len;
3119 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3120 copy);
3121 if ((len -= copy) == 0)
3122 return elt;
3123 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003124 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003125 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003126 }
3127 BUG_ON(len);
3128 return elt;
3129}
3130
David S. Miller51c739d2007-10-30 21:29:29 -07003131int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3132{
3133 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3134
Jens Axboec46f2332007-10-31 12:06:37 +01003135 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003136
3137 return nsg;
3138}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003139EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003140
David Howells716ea3a2007-04-02 20:19:53 -07003141/**
3142 * skb_cow_data - Check that a socket buffer's data buffers are writable
3143 * @skb: The socket buffer to check.
3144 * @tailbits: Amount of trailing space to be added
3145 * @trailer: Returned pointer to the skb where the @tailbits space begins
3146 *
3147 * Make sure that the data buffers attached to a socket buffer are
3148 * writable. If they are not, private copies are made of the data buffers
3149 * and the socket buffer is set to use these instead.
3150 *
3151 * If @tailbits is given, make sure that there is space to write @tailbits
3152 * bytes of data beyond current end of socket buffer. @trailer will be
3153 * set to point to the skb in which this space begins.
3154 *
3155 * The number of scatterlist elements required to completely map the
3156 * COW'd and extended socket buffer will be returned.
3157 */
3158int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3159{
3160 int copyflag;
3161 int elt;
3162 struct sk_buff *skb1, **skb_p;
3163
3164 /* If skb is cloned or its head is paged, reallocate
3165 * head pulling out all the pages (pages are considered not writable
3166 * at the moment even if they are anonymous).
3167 */
3168 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3169 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3170 return -ENOMEM;
3171
3172 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003173 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003174 /* A little of trouble, not enough of space for trailer.
3175 * This should not happen, when stack is tuned to generate
3176 * good frames. OK, on miss we reallocate and reserve even more
3177 * space, 128 bytes is fair. */
3178
3179 if (skb_tailroom(skb) < tailbits &&
3180 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3181 return -ENOMEM;
3182
3183 /* Voila! */
3184 *trailer = skb;
3185 return 1;
3186 }
3187
3188 /* Misery. We are in troubles, going to mincer fragments... */
3189
3190 elt = 1;
3191 skb_p = &skb_shinfo(skb)->frag_list;
3192 copyflag = 0;
3193
3194 while ((skb1 = *skb_p) != NULL) {
3195 int ntail = 0;
3196
3197 /* The fragment is partially pulled by someone,
3198 * this can happen on input. Copy it and everything
3199 * after it. */
3200
3201 if (skb_shared(skb1))
3202 copyflag = 1;
3203
3204 /* If the skb is the last, worry about trailer. */
3205
3206 if (skb1->next == NULL && tailbits) {
3207 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003208 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003209 skb_tailroom(skb1) < tailbits)
3210 ntail = tailbits + 128;
3211 }
3212
3213 if (copyflag ||
3214 skb_cloned(skb1) ||
3215 ntail ||
3216 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003217 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003218 struct sk_buff *skb2;
3219
3220 /* Fuck, we are miserable poor guys... */
3221 if (ntail == 0)
3222 skb2 = skb_copy(skb1, GFP_ATOMIC);
3223 else
3224 skb2 = skb_copy_expand(skb1,
3225 skb_headroom(skb1),
3226 ntail,
3227 GFP_ATOMIC);
3228 if (unlikely(skb2 == NULL))
3229 return -ENOMEM;
3230
3231 if (skb1->sk)
3232 skb_set_owner_w(skb2, skb1->sk);
3233
3234 /* Looking around. Are we still alive?
3235 * OK, link new skb, drop old one */
3236
3237 skb2->next = skb1->next;
3238 *skb_p = skb2;
3239 kfree_skb(skb1);
3240 skb1 = skb2;
3241 }
3242 elt++;
3243 *trailer = skb1;
3244 skb_p = &skb1->next;
3245 }
3246
3247 return elt;
3248}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003249EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003250
Eric Dumazetb1faf562010-05-31 23:44:05 -07003251static void sock_rmem_free(struct sk_buff *skb)
3252{
3253 struct sock *sk = skb->sk;
3254
3255 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3256}
3257
3258/*
3259 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3260 */
3261int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3262{
Eric Dumazet110c4332012-04-06 10:49:10 +02003263 int len = skb->len;
3264
Eric Dumazetb1faf562010-05-31 23:44:05 -07003265 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003266 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003267 return -ENOMEM;
3268
3269 skb_orphan(skb);
3270 skb->sk = sk;
3271 skb->destructor = sock_rmem_free;
3272 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3273
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003274 /* before exiting rcu section, make sure dst is refcounted */
3275 skb_dst_force(skb);
3276
Eric Dumazetb1faf562010-05-31 23:44:05 -07003277 skb_queue_tail(&sk->sk_error_queue, skb);
3278 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003279 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003280 return 0;
3281}
3282EXPORT_SYMBOL(sock_queue_err_skb);
3283
Patrick Ohlyac45f602009-02-12 05:03:37 +00003284void skb_tstamp_tx(struct sk_buff *orig_skb,
3285 struct skb_shared_hwtstamps *hwtstamps)
3286{
3287 struct sock *sk = orig_skb->sk;
3288 struct sock_exterr_skb *serr;
3289 struct sk_buff *skb;
3290 int err;
3291
3292 if (!sk)
3293 return;
3294
3295 skb = skb_clone(orig_skb, GFP_ATOMIC);
3296 if (!skb)
3297 return;
3298
3299 if (hwtstamps) {
3300 *skb_hwtstamps(skb) =
3301 *hwtstamps;
3302 } else {
3303 /*
3304 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003305 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003306 * store software time stamp
3307 */
3308 skb->tstamp = ktime_get_real();
3309 }
3310
3311 serr = SKB_EXT_ERR(skb);
3312 memset(serr, 0, sizeof(*serr));
3313 serr->ee.ee_errno = ENOMSG;
3314 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003315
Patrick Ohlyac45f602009-02-12 05:03:37 +00003316 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003317
Patrick Ohlyac45f602009-02-12 05:03:37 +00003318 if (err)
3319 kfree_skb(skb);
3320}
3321EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3322
Johannes Berg6e3e9392011-11-09 10:15:42 +01003323void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3324{
3325 struct sock *sk = skb->sk;
3326 struct sock_exterr_skb *serr;
3327 int err;
3328
3329 skb->wifi_acked_valid = 1;
3330 skb->wifi_acked = acked;
3331
3332 serr = SKB_EXT_ERR(skb);
3333 memset(serr, 0, sizeof(*serr));
3334 serr->ee.ee_errno = ENOMSG;
3335 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3336
3337 err = sock_queue_err_skb(sk, skb);
3338 if (err)
3339 kfree_skb(skb);
3340}
3341EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3342
Patrick Ohlyac45f602009-02-12 05:03:37 +00003343
Rusty Russellf35d9d82008-02-04 23:49:54 -05003344/**
3345 * skb_partial_csum_set - set up and verify partial csum values for packet
3346 * @skb: the skb to set
3347 * @start: the number of bytes after skb->data to start checksumming.
3348 * @off: the offset from start to place the checksum.
3349 *
3350 * For untrusted partially-checksummed packets, we need to make sure the values
3351 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3352 *
3353 * This function checks and sets those values and skb->ip_summed: if this
3354 * returns false you should drop the packet.
3355 */
3356bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3357{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003358 if (unlikely(start > skb_headlen(skb)) ||
3359 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003360 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3361 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003362 return false;
3363 }
3364 skb->ip_summed = CHECKSUM_PARTIAL;
3365 skb->csum_start = skb_headroom(skb) + start;
3366 skb->csum_offset = off;
3367 return true;
3368}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003369EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003370
Ben Hutchings4497b072008-06-19 16:22:28 -07003371void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3372{
Joe Perchese87cc472012-05-13 21:56:26 +00003373 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3374 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003375}
Ben Hutchings4497b072008-06-19 16:22:28 -07003376EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003377
3378void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3379{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003380 if (head_stolen) {
3381 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003382 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003383 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003384 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003385 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003386}
3387EXPORT_SYMBOL(kfree_skb_partial);
3388
3389/**
3390 * skb_try_coalesce - try to merge skb to prior one
3391 * @to: prior buffer
3392 * @from: buffer to add
3393 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003394 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003395 */
3396bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3397 bool *fragstolen, int *delta_truesize)
3398{
3399 int i, delta, len = from->len;
3400
3401 *fragstolen = false;
3402
3403 if (skb_cloned(to))
3404 return false;
3405
3406 if (len <= skb_tailroom(to)) {
3407 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3408 *delta_truesize = 0;
3409 return true;
3410 }
3411
3412 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3413 return false;
3414
3415 if (skb_headlen(from) != 0) {
3416 struct page *page;
3417 unsigned int offset;
3418
3419 if (skb_shinfo(to)->nr_frags +
3420 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3421 return false;
3422
3423 if (skb_head_is_locked(from))
3424 return false;
3425
3426 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3427
3428 page = virt_to_head_page(from->head);
3429 offset = from->data - (unsigned char *)page_address(page);
3430
3431 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3432 page, offset, skb_headlen(from));
3433 *fragstolen = true;
3434 } else {
3435 if (skb_shinfo(to)->nr_frags +
3436 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3437 return false;
3438
Weiping Panf4b549a2012-09-28 20:15:30 +00003439 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003440 }
3441
3442 WARN_ON_ONCE(delta < len);
3443
3444 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3445 skb_shinfo(from)->frags,
3446 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3447 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3448
3449 if (!skb_cloned(from))
3450 skb_shinfo(from)->nr_frags = 0;
3451
Li RongQing8ea853f2012-09-18 16:53:21 +00003452 /* if the skb is not cloned this does nothing
3453 * since we set nr_frags to 0.
3454 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003455 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3456 skb_frag_ref(from, i);
3457
3458 to->truesize += delta;
3459 to->len += len;
3460 to->data_len += len;
3461
3462 *delta_truesize = delta;
3463 return true;
3464}
3465EXPORT_SYMBOL(skb_try_coalesce);