blob: bc96100fe23c7ab2d8ff5a7bffe07a023014369a [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 McHardy6aa895b02008-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
Octavian Purdila2870c432008-07-15 00:49:11 -07001710static inline void __segment_seek(struct page **page, unsigned int *poff,
1711 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001712{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001713 unsigned long n;
1714
Octavian Purdila2870c432008-07-15 00:49:11 -07001715 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001716 n = *poff / PAGE_SIZE;
1717 if (n)
1718 *page = nth_page(*page, n);
1719
Octavian Purdila2870c432008-07-15 00:49:11 -07001720 *poff = *poff % PAGE_SIZE;
1721 *plen -= off;
1722}
Jens Axboe9c55e012007-11-06 23:30:13 -08001723
David S. Millera108d5f2012-04-23 23:06:11 -04001724static bool __splice_segment(struct page *page, unsigned int poff,
1725 unsigned int plen, unsigned int *off,
1726 unsigned int *len, struct sk_buff *skb,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001727 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001728 struct sock *sk,
1729 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001730{
1731 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001732 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001733
1734 /* skip this segment if already processed */
1735 if (*off >= plen) {
1736 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001737 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001738 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001739
Octavian Purdila2870c432008-07-15 00:49:11 -07001740 /* ignore any bits we already processed */
1741 if (*off) {
1742 __segment_seek(&page, &poff, &plen, *off);
1743 *off = 0;
1744 }
1745
1746 do {
1747 unsigned int flen = min(*len, plen);
1748
1749 /* the linear region may spread across several pages */
1750 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1751
Jens Axboe35f3d142010-05-20 10:43:18 +02001752 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
David S. Millera108d5f2012-04-23 23:06:11 -04001753 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001754
1755 __segment_seek(&page, &poff, &plen, flen);
1756 *len -= flen;
1757
1758 } while (*len && plen);
1759
David S. Millera108d5f2012-04-23 23:06:11 -04001760 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001761}
1762
1763/*
David S. Millera108d5f2012-04-23 23:06:11 -04001764 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001765 * pipe is full or if we already spliced the requested length.
1766 */
David S. Millera108d5f2012-04-23 23:06:11 -04001767static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1768 unsigned int *offset, unsigned int *len,
1769 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001770{
1771 int seg;
1772
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001773 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001774 * If skb->head_frag is set, this 'linear' part is backed by a
1775 * fragment, and if the head is not shared with any clones then
1776 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001777 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001778 if (__splice_segment(virt_to_page(skb->data),
1779 (unsigned long) skb->data & (PAGE_SIZE - 1),
1780 skb_headlen(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001781 offset, len, skb, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001782 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001783 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001784 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001785
1786 /*
1787 * then map the fragments
1788 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001789 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1790 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1791
Ian Campbellea2ab692011-08-22 23:44:58 +00001792 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001793 f->page_offset, skb_frag_size(f),
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001794 offset, len, skb, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001795 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001796 }
1797
David S. Millera108d5f2012-04-23 23:06:11 -04001798 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001799}
1800
1801/*
1802 * Map data from the skb to a pipe. Should handle both the linear part,
1803 * the fragments, and the frag list. It does NOT handle frag lists within
1804 * the frag list, if such a thing exists. We'd probably need to recurse to
1805 * handle that cleanly.
1806 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001807int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001808 struct pipe_inode_info *pipe, unsigned int tlen,
1809 unsigned int flags)
1810{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001811 struct partial_page partial[MAX_SKB_FRAGS];
1812 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001813 struct splice_pipe_desc spd = {
1814 .pages = pages,
1815 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001816 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001817 .flags = flags,
1818 .ops = &sock_pipe_buf_ops,
1819 .spd_release = sock_spd_release,
1820 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001821 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001822 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001823 int ret = 0;
1824
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 /*
1826 * __skb_splice_bits() only fails if the output has no room left,
1827 * so no point in going over the frag_list for the error case.
1828 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001829 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001830 goto done;
1831 else if (!tlen)
1832 goto done;
1833
1834 /*
1835 * now see if we have a frag_list to map
1836 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001837 skb_walk_frags(skb, frag_iter) {
1838 if (!tlen)
1839 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001840 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001841 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001842 }
1843
1844done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001845 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001846 /*
1847 * Drop the socket lock, otherwise we have reverse
1848 * locking dependencies between sk_lock and i_mutex
1849 * here as compared to sendfile(). We enter here
1850 * with the socket lock held, and splice_to_pipe() will
1851 * grab the pipe inode lock. For sendfile() emulation,
1852 * we call into ->sendpage() with the i_mutex lock held
1853 * and networking will grab the socket lock.
1854 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001855 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001856 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001857 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001858 }
1859
Jens Axboe35f3d142010-05-20 10:43:18 +02001860 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001861}
1862
Herbert Xu357b40a2005-04-19 22:30:14 -07001863/**
1864 * skb_store_bits - store bits from kernel buffer to skb
1865 * @skb: destination buffer
1866 * @offset: offset in destination
1867 * @from: source buffer
1868 * @len: number of bytes to copy
1869 *
1870 * Copy the specified number of bytes from the source buffer to the
1871 * destination skb. This function handles all the messy bits of
1872 * traversing fragment lists and such.
1873 */
1874
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001875int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001876{
David S. Miller1a028e52007-04-27 15:21:23 -07001877 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001878 struct sk_buff *frag_iter;
1879 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001880
1881 if (offset > (int)skb->len - len)
1882 goto fault;
1883
David S. Miller1a028e52007-04-27 15:21:23 -07001884 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001885 if (copy > len)
1886 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001887 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001888 if ((len -= copy) == 0)
1889 return 0;
1890 offset += copy;
1891 from += copy;
1892 }
1893
1894 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1895 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001896 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001897
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001898 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001899
Eric Dumazet9e903e02011-10-18 21:00:24 +00001900 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001901 if ((copy = end - offset) > 0) {
1902 u8 *vaddr;
1903
1904 if (copy > len)
1905 copy = len;
1906
Eric Dumazet51c56b02012-04-05 11:35:15 +02001907 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001908 memcpy(vaddr + frag->page_offset + offset - start,
1909 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001910 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001911
1912 if ((len -= copy) == 0)
1913 return 0;
1914 offset += copy;
1915 from += copy;
1916 }
David S. Miller1a028e52007-04-27 15:21:23 -07001917 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001918 }
1919
David S. Millerfbb398a2009-06-09 00:18:59 -07001920 skb_walk_frags(skb, frag_iter) {
1921 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001922
David S. Millerfbb398a2009-06-09 00:18:59 -07001923 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001924
David S. Millerfbb398a2009-06-09 00:18:59 -07001925 end = start + frag_iter->len;
1926 if ((copy = end - offset) > 0) {
1927 if (copy > len)
1928 copy = len;
1929 if (skb_store_bits(frag_iter, offset - start,
1930 from, copy))
1931 goto fault;
1932 if ((len -= copy) == 0)
1933 return 0;
1934 offset += copy;
1935 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001936 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001937 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001938 }
1939 if (!len)
1940 return 0;
1941
1942fault:
1943 return -EFAULT;
1944}
Herbert Xu357b40a2005-04-19 22:30:14 -07001945EXPORT_SYMBOL(skb_store_bits);
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/* Checksum skb data. */
1948
Al Viro2bbbc862006-11-14 21:37:14 -08001949__wsum skb_checksum(const struct sk_buff *skb, int offset,
1950 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
David S. Miller1a028e52007-04-27 15:21:23 -07001952 int start = skb_headlen(skb);
1953 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001954 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int pos = 0;
1956
1957 /* Checksum header. */
1958 if (copy > 0) {
1959 if (copy > len)
1960 copy = len;
1961 csum = csum_partial(skb->data + offset, copy, csum);
1962 if ((len -= copy) == 0)
1963 return csum;
1964 offset += copy;
1965 pos = copy;
1966 }
1967
1968 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001969 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001970 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001972 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001973
Eric Dumazet51c56b02012-04-05 11:35:15 +02001974 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001976 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (copy > len)
1980 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001981 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001982 csum2 = csum_partial(vaddr + frag->page_offset +
1983 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001984 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 csum = csum_block_add(csum, csum2, pos);
1986 if (!(len -= copy))
1987 return csum;
1988 offset += copy;
1989 pos += copy;
1990 }
David S. Miller1a028e52007-04-27 15:21:23 -07001991 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
David S. Millerfbb398a2009-06-09 00:18:59 -07001994 skb_walk_frags(skb, frag_iter) {
1995 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
David S. Millerfbb398a2009-06-09 00:18:59 -07001997 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
David S. Millerfbb398a2009-06-09 00:18:59 -07001999 end = start + frag_iter->len;
2000 if ((copy = end - offset) > 0) {
2001 __wsum csum2;
2002 if (copy > len)
2003 copy = len;
2004 csum2 = skb_checksum(frag_iter, offset - start,
2005 copy, 0);
2006 csum = csum_block_add(csum, csum2, pos);
2007 if ((len -= copy) == 0)
2008 return csum;
2009 offset += copy;
2010 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002012 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002014 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 return csum;
2017}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002018EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020/* Both of above in one bottle. */
2021
Al Viro81d77662006-11-14 21:37:33 -08002022__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2023 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
David S. Miller1a028e52007-04-27 15:21:23 -07002025 int start = skb_headlen(skb);
2026 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002027 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 int pos = 0;
2029
2030 /* Copy header. */
2031 if (copy > 0) {
2032 if (copy > len)
2033 copy = len;
2034 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2035 copy, csum);
2036 if ((len -= copy) == 0)
2037 return csum;
2038 offset += copy;
2039 to += copy;
2040 pos = copy;
2041 }
2042
2043 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002044 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002046 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002047
Eric Dumazet9e903e02011-10-18 21:00:24 +00002048 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002050 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 u8 *vaddr;
2052 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2053
2054 if (copy > len)
2055 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002056 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002058 frag->page_offset +
2059 offset - start, to,
2060 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002061 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 csum = csum_block_add(csum, csum2, pos);
2063 if (!(len -= copy))
2064 return csum;
2065 offset += copy;
2066 to += copy;
2067 pos += copy;
2068 }
David S. Miller1a028e52007-04-27 15:21:23 -07002069 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071
David S. Millerfbb398a2009-06-09 00:18:59 -07002072 skb_walk_frags(skb, frag_iter) {
2073 __wsum csum2;
2074 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
David S. Millerfbb398a2009-06-09 00:18:59 -07002076 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
David S. Millerfbb398a2009-06-09 00:18:59 -07002078 end = start + frag_iter->len;
2079 if ((copy = end - offset) > 0) {
2080 if (copy > len)
2081 copy = len;
2082 csum2 = skb_copy_and_csum_bits(frag_iter,
2083 offset - start,
2084 to, copy, 0);
2085 csum = csum_block_add(csum, csum2, pos);
2086 if ((len -= copy) == 0)
2087 return csum;
2088 offset += copy;
2089 to += copy;
2090 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002092 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002094 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return csum;
2096}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002097EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2100{
Al Virod3bc23e2006-11-14 21:24:49 -08002101 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 long csstart;
2103
Patrick McHardy84fa7932006-08-29 16:44:56 -07002104 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002105 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 else
2107 csstart = skb_headlen(skb);
2108
Kris Katterjohn09a62662006-01-08 22:24:28 -08002109 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002111 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 csum = 0;
2114 if (csstart != skb->len)
2115 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2116 skb->len - csstart, 0);
2117
Patrick McHardy84fa7932006-08-29 16:44:56 -07002118 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002119 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Al Virod3bc23e2006-11-14 21:24:49 -08002121 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002124EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126/**
2127 * skb_dequeue - remove from the head of the queue
2128 * @list: list to dequeue from
2129 *
2130 * Remove the head of the list. The list lock is taken so the function
2131 * may be used safely with other locking list functions. The head item is
2132 * returned or %NULL if the list is empty.
2133 */
2134
2135struct sk_buff *skb_dequeue(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(list);
2142 spin_unlock_irqrestore(&list->lock, flags);
2143 return result;
2144}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002145EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147/**
2148 * skb_dequeue_tail - remove from the tail of the queue
2149 * @list: list to dequeue from
2150 *
2151 * Remove the tail of the list. The list lock is taken so the function
2152 * may be used safely with other locking list functions. The tail item is
2153 * returned or %NULL if the list is empty.
2154 */
2155struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2156{
2157 unsigned long flags;
2158 struct sk_buff *result;
2159
2160 spin_lock_irqsave(&list->lock, flags);
2161 result = __skb_dequeue_tail(list);
2162 spin_unlock_irqrestore(&list->lock, flags);
2163 return result;
2164}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002165EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167/**
2168 * skb_queue_purge - empty a list
2169 * @list: list to empty
2170 *
2171 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2172 * the list and one reference dropped. This function takes the list
2173 * lock and is atomic with respect to other list locking functions.
2174 */
2175void skb_queue_purge(struct sk_buff_head *list)
2176{
2177 struct sk_buff *skb;
2178 while ((skb = skb_dequeue(list)) != NULL)
2179 kfree_skb(skb);
2180}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002181EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183/**
2184 * skb_queue_head - queue a buffer at the list head
2185 * @list: list to use
2186 * @newsk: buffer to queue
2187 *
2188 * Queue a buffer at the start of the list. This function takes the
2189 * list lock and can be used safely with other locking &sk_buff functions
2190 * safely.
2191 *
2192 * A buffer cannot be placed on two lists at the same time.
2193 */
2194void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2195{
2196 unsigned long flags;
2197
2198 spin_lock_irqsave(&list->lock, flags);
2199 __skb_queue_head(list, newsk);
2200 spin_unlock_irqrestore(&list->lock, flags);
2201}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002202EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204/**
2205 * skb_queue_tail - queue a buffer at the list tail
2206 * @list: list to use
2207 * @newsk: buffer to queue
2208 *
2209 * Queue a buffer at the tail of the list. This function takes the
2210 * list lock and can be used safely with other locking &sk_buff functions
2211 * safely.
2212 *
2213 * A buffer cannot be placed on two lists at the same time.
2214 */
2215void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2216{
2217 unsigned long flags;
2218
2219 spin_lock_irqsave(&list->lock, flags);
2220 __skb_queue_tail(list, newsk);
2221 spin_unlock_irqrestore(&list->lock, flags);
2222}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002223EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/**
2226 * skb_unlink - remove a buffer from a list
2227 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002228 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 *
David S. Miller8728b832005-08-09 19:25:21 -07002230 * Remove a packet from a list. The list locks are taken and this
2231 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 *
David S. Miller8728b832005-08-09 19:25:21 -07002233 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 */
David S. Miller8728b832005-08-09 19:25:21 -07002235void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
David S. Miller8728b832005-08-09 19:25:21 -07002237 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
David S. Miller8728b832005-08-09 19:25:21 -07002239 spin_lock_irqsave(&list->lock, flags);
2240 __skb_unlink(skb, list);
2241 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_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245/**
2246 * skb_append - append a buffer
2247 * @old: buffer to insert after
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 *
2251 * Place a packet after a given packet in a list. The list locks are taken
2252 * and this function is atomic with respect to other list locked calls.
2253 * A buffer cannot be placed on two lists at the same time.
2254 */
David S. Miller8728b832005-08-09 19:25:21 -07002255void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 unsigned long flags;
2258
David S. Miller8728b832005-08-09 19:25:21 -07002259 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002260 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002261 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002263EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265/**
2266 * skb_insert - insert a buffer
2267 * @old: buffer to insert before
2268 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002269 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 *
David S. Miller8728b832005-08-09 19:25:21 -07002271 * Place a packet before a given packet in a list. The list locks are
2272 * taken and this function is atomic with respect to other list locked
2273 * calls.
2274 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * A buffer cannot be placed on two lists at the same time.
2276 */
David S. Miller8728b832005-08-09 19:25:21 -07002277void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 unsigned long flags;
2280
David S. Miller8728b832005-08-09 19:25:21 -07002281 spin_lock_irqsave(&list->lock, flags);
2282 __skb_insert(newsk, old->prev, old, list);
2283 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002285EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287static inline void skb_split_inside_header(struct sk_buff *skb,
2288 struct sk_buff* skb1,
2289 const u32 len, const int pos)
2290{
2291 int i;
2292
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002293 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2294 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* And move data appendix as is. */
2296 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2297 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2298
2299 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2300 skb_shinfo(skb)->nr_frags = 0;
2301 skb1->data_len = skb->data_len;
2302 skb1->len += skb1->data_len;
2303 skb->data_len = 0;
2304 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002305 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
2308static inline void skb_split_no_header(struct sk_buff *skb,
2309 struct sk_buff* skb1,
2310 const u32 len, int pos)
2311{
2312 int i, k = 0;
2313 const int nfrags = skb_shinfo(skb)->nr_frags;
2314
2315 skb_shinfo(skb)->nr_frags = 0;
2316 skb1->len = skb1->data_len = skb->len - len;
2317 skb->len = len;
2318 skb->data_len = len - pos;
2319
2320 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002321 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (pos + size > len) {
2324 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2325
2326 if (pos < len) {
2327 /* Split frag.
2328 * We have two variants in this case:
2329 * 1. Move all the frag to the second
2330 * part, if it is possible. F.e.
2331 * this approach is mandatory for TUX,
2332 * where splitting is expensive.
2333 * 2. Split is accurately. We make this.
2334 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002335 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002337 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2338 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 skb_shinfo(skb)->nr_frags++;
2340 }
2341 k++;
2342 } else
2343 skb_shinfo(skb)->nr_frags++;
2344 pos += size;
2345 }
2346 skb_shinfo(skb1)->nr_frags = k;
2347}
2348
2349/**
2350 * skb_split - Split fragmented skb to two parts at length len.
2351 * @skb: the buffer to split
2352 * @skb1: the buffer to receive the second part
2353 * @len: new length for skb
2354 */
2355void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2356{
2357 int pos = skb_headlen(skb);
2358
2359 if (len < pos) /* Split line is inside header. */
2360 skb_split_inside_header(skb, skb1, len, pos);
2361 else /* Second chunk has no header, nothing to copy. */
2362 skb_split_no_header(skb, skb1, len, pos);
2363}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002364EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002366/* Shifting from/to a cloned skb is a no-go.
2367 *
2368 * Caller cannot keep skb_shinfo related pointers past calling here!
2369 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002370static int skb_prepare_for_shift(struct sk_buff *skb)
2371{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002372 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002373}
2374
2375/**
2376 * skb_shift - Shifts paged data partially from skb to another
2377 * @tgt: buffer into which tail data gets added
2378 * @skb: buffer from which the paged data comes from
2379 * @shiftlen: shift up to this many bytes
2380 *
2381 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002382 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002383 * It's up to caller to free skb if everything was shifted.
2384 *
2385 * If @tgt runs out of frags, the whole operation is aborted.
2386 *
2387 * Skb cannot include anything else but paged data while tgt is allowed
2388 * to have non-paged data as well.
2389 *
2390 * TODO: full sized shift could be optimized but that would need
2391 * specialized skb free'er to handle frags without up-to-date nr_frags.
2392 */
2393int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2394{
2395 int from, to, merge, todo;
2396 struct skb_frag_struct *fragfrom, *fragto;
2397
2398 BUG_ON(shiftlen > skb->len);
2399 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2400
2401 todo = shiftlen;
2402 from = 0;
2403 to = skb_shinfo(tgt)->nr_frags;
2404 fragfrom = &skb_shinfo(skb)->frags[from];
2405
2406 /* Actual merge is delayed until the point when we know we can
2407 * commit all, so that we don't have to undo partial changes
2408 */
2409 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002410 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2411 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002412 merge = -1;
2413 } else {
2414 merge = to - 1;
2415
Eric Dumazet9e903e02011-10-18 21:00:24 +00002416 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002417 if (todo < 0) {
2418 if (skb_prepare_for_shift(skb) ||
2419 skb_prepare_for_shift(tgt))
2420 return 0;
2421
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002422 /* All previous frag pointers might be stale! */
2423 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002424 fragto = &skb_shinfo(tgt)->frags[merge];
2425
Eric Dumazet9e903e02011-10-18 21:00:24 +00002426 skb_frag_size_add(fragto, shiftlen);
2427 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002428 fragfrom->page_offset += shiftlen;
2429
2430 goto onlymerged;
2431 }
2432
2433 from++;
2434 }
2435
2436 /* Skip full, not-fitting skb to avoid expensive operations */
2437 if ((shiftlen == skb->len) &&
2438 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2439 return 0;
2440
2441 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2442 return 0;
2443
2444 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2445 if (to == MAX_SKB_FRAGS)
2446 return 0;
2447
2448 fragfrom = &skb_shinfo(skb)->frags[from];
2449 fragto = &skb_shinfo(tgt)->frags[to];
2450
Eric Dumazet9e903e02011-10-18 21:00:24 +00002451 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002452 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002453 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002454 from++;
2455 to++;
2456
2457 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002458 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002459 fragto->page = fragfrom->page;
2460 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002461 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002462
2463 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002464 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002465 todo = 0;
2466
2467 to++;
2468 break;
2469 }
2470 }
2471
2472 /* Ready to "commit" this state change to tgt */
2473 skb_shinfo(tgt)->nr_frags = to;
2474
2475 if (merge >= 0) {
2476 fragfrom = &skb_shinfo(skb)->frags[0];
2477 fragto = &skb_shinfo(tgt)->frags[merge];
2478
Eric Dumazet9e903e02011-10-18 21:00:24 +00002479 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002480 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002481 }
2482
2483 /* Reposition in the original skb */
2484 to = 0;
2485 while (from < skb_shinfo(skb)->nr_frags)
2486 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2487 skb_shinfo(skb)->nr_frags = to;
2488
2489 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2490
2491onlymerged:
2492 /* Most likely the tgt won't ever need its checksum anymore, skb on
2493 * the other hand might need it if it needs to be resent
2494 */
2495 tgt->ip_summed = CHECKSUM_PARTIAL;
2496 skb->ip_summed = CHECKSUM_PARTIAL;
2497
2498 /* Yak, is it really working this way? Some helper please? */
2499 skb->len -= shiftlen;
2500 skb->data_len -= shiftlen;
2501 skb->truesize -= shiftlen;
2502 tgt->len += shiftlen;
2503 tgt->data_len += shiftlen;
2504 tgt->truesize += shiftlen;
2505
2506 return shiftlen;
2507}
2508
Thomas Graf677e90e2005-06-23 20:59:51 -07002509/**
2510 * skb_prepare_seq_read - Prepare a sequential read of skb data
2511 * @skb: the buffer to read
2512 * @from: lower offset of data to be read
2513 * @to: upper offset of data to be read
2514 * @st: state variable
2515 *
2516 * Initializes the specified state variable. Must be called before
2517 * invoking skb_seq_read() for the first time.
2518 */
2519void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2520 unsigned int to, struct skb_seq_state *st)
2521{
2522 st->lower_offset = from;
2523 st->upper_offset = to;
2524 st->root_skb = st->cur_skb = skb;
2525 st->frag_idx = st->stepped_offset = 0;
2526 st->frag_data = NULL;
2527}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002528EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002529
2530/**
2531 * skb_seq_read - Sequentially read skb data
2532 * @consumed: number of bytes consumed by the caller so far
2533 * @data: destination pointer for data to be returned
2534 * @st: state variable
2535 *
2536 * Reads a block of skb data at &consumed relative to the
2537 * lower offset specified to skb_prepare_seq_read(). Assigns
2538 * the head of the data block to &data and returns the length
2539 * of the block or 0 if the end of the skb data or the upper
2540 * offset has been reached.
2541 *
2542 * The caller is not required to consume all of the data
2543 * returned, i.e. &consumed is typically set to the number
2544 * of bytes already consumed and the next call to
2545 * skb_seq_read() will return the remaining part of the block.
2546 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002547 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002548 * this limitation is the cost for zerocopy seqeuental
2549 * reads of potentially non linear data.
2550 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002551 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002552 * at the moment, state->root_skb could be replaced with
2553 * a stack for this purpose.
2554 */
2555unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2556 struct skb_seq_state *st)
2557{
2558 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2559 skb_frag_t *frag;
2560
2561 if (unlikely(abs_offset >= st->upper_offset))
2562 return 0;
2563
2564next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002565 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002566
Thomas Chenault995b3372009-05-18 21:43:27 -07002567 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002568 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002569 return block_limit - abs_offset;
2570 }
2571
2572 if (st->frag_idx == 0 && !st->frag_data)
2573 st->stepped_offset += skb_headlen(st->cur_skb);
2574
2575 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2576 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002577 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002578
2579 if (abs_offset < block_limit) {
2580 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002581 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002582
2583 *data = (u8 *) st->frag_data + frag->page_offset +
2584 (abs_offset - st->stepped_offset);
2585
2586 return block_limit - abs_offset;
2587 }
2588
2589 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002590 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002591 st->frag_data = NULL;
2592 }
2593
2594 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002595 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002596 }
2597
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002598 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002599 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002600 st->frag_data = NULL;
2601 }
2602
David S. Miller21dc3302010-08-23 00:13:46 -07002603 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002604 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002605 st->frag_idx = 0;
2606 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002607 } else if (st->cur_skb->next) {
2608 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002609 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002610 goto next_skb;
2611 }
2612
2613 return 0;
2614}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002615EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002616
2617/**
2618 * skb_abort_seq_read - Abort a sequential read of skb data
2619 * @st: state variable
2620 *
2621 * Must be called if skb_seq_read() was not called until it
2622 * returned 0.
2623 */
2624void skb_abort_seq_read(struct skb_seq_state *st)
2625{
2626 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002627 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002628}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002629EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002630
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002631#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2632
2633static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2634 struct ts_config *conf,
2635 struct ts_state *state)
2636{
2637 return skb_seq_read(offset, text, TS_SKB_CB(state));
2638}
2639
2640static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2641{
2642 skb_abort_seq_read(TS_SKB_CB(state));
2643}
2644
2645/**
2646 * skb_find_text - Find a text pattern in skb data
2647 * @skb: the buffer to look in
2648 * @from: search offset
2649 * @to: search limit
2650 * @config: textsearch configuration
2651 * @state: uninitialized textsearch state variable
2652 *
2653 * Finds a pattern in the skb data according to the specified
2654 * textsearch configuration. Use textsearch_next() to retrieve
2655 * subsequent occurrences of the pattern. Returns the offset
2656 * to the first occurrence or UINT_MAX if no match was found.
2657 */
2658unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2659 unsigned int to, struct ts_config *config,
2660 struct ts_state *state)
2661{
Phil Oesterf72b9482006-06-26 00:00:57 -07002662 unsigned int ret;
2663
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002664 config->get_next_block = skb_ts_get_next_block;
2665 config->finish = skb_ts_finish;
2666
2667 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2668
Phil Oesterf72b9482006-06-26 00:00:57 -07002669 ret = textsearch_find(config, state);
2670 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002671}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002672EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002673
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002674/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002675 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002676 * @sk: sock structure
2677 * @skb: skb structure to be appened with user data.
2678 * @getfrag: call back function to be used for getting the user data
2679 * @from: pointer to user message iov
2680 * @length: length of the iov message
2681 *
2682 * Description: This procedure append the user data in the fragment part
2683 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2684 */
2685int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002686 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002687 int len, int odd, struct sk_buff *skb),
2688 void *from, int length)
2689{
Eric Dumazetb2111722012-12-28 06:06:37 +00002690 int frg_cnt = skb_shinfo(skb)->nr_frags;
2691 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002692 int offset = 0;
2693 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002694 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002695
2696 do {
2697 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002698 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002699 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002700
Eric Dumazetb2111722012-12-28 06:06:37 +00002701 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002702 return -ENOMEM;
2703
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002704 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002705 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002706
Eric Dumazetb2111722012-12-28 06:06:37 +00002707 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2708 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002709 if (ret < 0)
2710 return -EFAULT;
2711
2712 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002713 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2714 copy);
2715 frg_cnt++;
2716 pfrag->offset += copy;
2717 get_page(pfrag->page);
2718
2719 skb->truesize += copy;
2720 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002721 skb->len += copy;
2722 skb->data_len += copy;
2723 offset += copy;
2724 length -= copy;
2725
2726 } while (length > 0);
2727
2728 return 0;
2729}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002730EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002731
Herbert Xucbb042f2006-03-20 22:43:56 -08002732/**
2733 * skb_pull_rcsum - pull skb and update receive checksum
2734 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002735 * @len: length of data pulled
2736 *
2737 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002738 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002739 * receive path processing instead of skb_pull unless you know
2740 * that the checksum difference is zero (e.g., a valid IP header)
2741 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002742 */
2743unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2744{
2745 BUG_ON(len > skb->len);
2746 skb->len -= len;
2747 BUG_ON(skb->len < skb->data_len);
2748 skb_postpull_rcsum(skb, skb->data, len);
2749 return skb->data += len;
2750}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002751EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2752
Herbert Xuf4c50d92006-06-22 03:02:40 -07002753/**
2754 * skb_segment - Perform protocol segmentation on skb.
2755 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002756 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002757 *
2758 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002759 * a pointer to the first in a list of new skbs for the segments.
2760 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002761 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002762struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002763{
2764 struct sk_buff *segs = NULL;
2765 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002766 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002767 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002768 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002769 unsigned int offset = doffset;
2770 unsigned int headroom;
2771 unsigned int len;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002772 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002773 int nfrags = skb_shinfo(skb)->nr_frags;
2774 int err = -ENOMEM;
2775 int i = 0;
2776 int pos;
2777
2778 __skb_push(skb, doffset);
2779 headroom = skb_headroom(skb);
2780 pos = skb_headlen(skb);
2781
2782 do {
2783 struct sk_buff *nskb;
2784 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002785 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002786 int size;
2787
2788 len = skb->len - offset;
2789 if (len > mss)
2790 len = mss;
2791
2792 hsize = skb_headlen(skb) - offset;
2793 if (hsize < 0)
2794 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002795 if (hsize > len || !sg)
2796 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002797
Herbert Xu89319d382008-12-15 23:26:06 -08002798 if (!hsize && i >= nfrags) {
2799 BUG_ON(fskb->len != len);
2800
2801 pos += len;
2802 nskb = skb_clone(fskb, GFP_ATOMIC);
2803 fskb = fskb->next;
2804
2805 if (unlikely(!nskb))
2806 goto err;
2807
Alexander Duyckec47ea82012-05-04 14:26:56 +00002808 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002809 if (skb_cow_head(nskb, doffset + headroom)) {
2810 kfree_skb(nskb);
2811 goto err;
2812 }
2813
Alexander Duyckec47ea82012-05-04 14:26:56 +00002814 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002815 skb_release_head_state(nskb);
2816 __skb_push(nskb, doffset);
2817 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002818 nskb = __alloc_skb(hsize + doffset + headroom,
2819 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2820 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002821
2822 if (unlikely(!nskb))
2823 goto err;
2824
2825 skb_reserve(nskb, headroom);
2826 __skb_put(nskb, doffset);
2827 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002828
2829 if (segs)
2830 tail->next = nskb;
2831 else
2832 segs = nskb;
2833 tail = nskb;
2834
Herbert Xu6f85a122008-08-15 14:55:02 -07002835 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002836 nskb->mac_len = skb->mac_len;
2837
Eric Dumazet3d3be432010-09-01 00:50:51 +00002838 /* nskb and skb might have different headroom */
2839 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2840 nskb->csum_start += skb_headroom(nskb) - headroom;
2841
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002842 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002843 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002844 nskb->transport_header = (nskb->network_header +
2845 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002846 skb_copy_from_linear_data(skb, nskb->data, doffset);
2847
Herbert Xu2f181852009-03-28 23:39:18 -07002848 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002849 continue;
2850
Herbert Xuf4c50d92006-06-22 03:02:40 -07002851 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002852 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002853 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2854 skb_put(nskb, len),
2855 len, 0);
2856 continue;
2857 }
2858
2859 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002860
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002861 skb_copy_from_linear_data_offset(skb, offset,
2862 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002863
Herbert Xu89319d382008-12-15 23:26:06 -08002864 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002865 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002866 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002867 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002868
2869 if (pos < offset) {
2870 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002871 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002872 }
2873
Herbert Xu89319d382008-12-15 23:26:06 -08002874 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002875
2876 if (pos + size <= offset + len) {
2877 i++;
2878 pos += size;
2879 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002880 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002881 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002882 }
2883
2884 frag++;
2885 }
2886
Herbert Xu89319d382008-12-15 23:26:06 -08002887 if (pos < offset + len) {
2888 struct sk_buff *fskb2 = fskb;
2889
2890 BUG_ON(pos + fskb->len != offset + len);
2891
2892 pos += fskb->len;
2893 fskb = fskb->next;
2894
2895 if (fskb2->next) {
2896 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2897 if (!fskb2)
2898 goto err;
2899 } else
2900 skb_get(fskb2);
2901
David S. Millerfbb398a2009-06-09 00:18:59 -07002902 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002903 skb_shinfo(nskb)->frag_list = fskb2;
2904 }
2905
2906skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002907 nskb->data_len = len - hsize;
2908 nskb->len += nskb->data_len;
2909 nskb->truesize += nskb->data_len;
2910 } while ((offset += len) < skb->len);
2911
2912 return segs;
2913
2914err:
2915 while ((skb = segs)) {
2916 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002917 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002918 }
2919 return ERR_PTR(err);
2920}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002921EXPORT_SYMBOL_GPL(skb_segment);
2922
Herbert Xu71d93b32008-12-15 23:42:33 -08002923int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2924{
2925 struct sk_buff *p = *head;
2926 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002927 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2928 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002929 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002930 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002931 unsigned int offset = skb_gro_offset(skb);
2932 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002933 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002934
Herbert Xu86911732009-01-29 14:19:50 +00002935 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002936 return -E2BIG;
2937
Herbert Xu9aaa1562009-05-26 18:50:33 +00002938 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002939 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002940 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002941 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002942 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002943 int i = skbinfo->nr_frags;
2944 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002945
Herbert Xu66e92fc2009-05-26 18:50:32 +00002946 offset -= headlen;
2947
2948 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002949 return -E2BIG;
2950
Herbert Xu9aaa1562009-05-26 18:50:33 +00002951 pinfo->nr_frags = nr_frags;
2952 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002953
Herbert Xu9aaa1562009-05-26 18:50:33 +00002954 frag = pinfo->frags + nr_frags;
2955 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002956 do {
2957 *--frag = *--frag2;
2958 } while (--i);
2959
2960 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002961 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002962
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002963 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002964 delta_truesize = skb->truesize -
2965 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002966
Herbert Xuf5572062009-01-14 20:40:03 -08002967 skb->truesize -= skb->data_len;
2968 skb->len -= skb->data_len;
2969 skb->data_len = 0;
2970
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002971 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08002972 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00002973 } else if (skb->head_frag) {
2974 int nr_frags = pinfo->nr_frags;
2975 skb_frag_t *frag = pinfo->frags + nr_frags;
2976 struct page *page = virt_to_head_page(skb->head);
2977 unsigned int first_size = headlen - offset;
2978 unsigned int first_offset;
2979
2980 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2981 return -E2BIG;
2982
2983 first_offset = skb->data -
2984 (unsigned char *)page_address(page) +
2985 offset;
2986
2987 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2988
2989 frag->page.p = page;
2990 frag->page_offset = first_offset;
2991 skb_frag_size_set(frag, first_size);
2992
2993 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2994 /* We dont need to clear skbinfo->nr_frags here */
2995
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002996 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00002997 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2998 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08002999 } else if (skb_gro_len(p) != pinfo->gso_size)
3000 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003001
3002 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003003 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003004 if (unlikely(!nskb))
3005 return -ENOMEM;
3006
3007 __copy_skb_header(nskb, p);
3008 nskb->mac_len = p->mac_len;
3009
3010 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003011 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003012
Herbert Xu86911732009-01-29 14:19:50 +00003013 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003014 skb_set_network_header(nskb, skb_network_offset(p));
3015 skb_set_transport_header(nskb, skb_transport_offset(p));
3016
Herbert Xu86911732009-01-29 14:19:50 +00003017 __skb_pull(p, skb_gro_offset(p));
3018 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3019 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003020
Herbert Xu71d93b32008-12-15 23:42:33 -08003021 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003022 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003023 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003024 skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003025 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003026
3027 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003028 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003029 nskb->len += p->len;
3030
3031 *head = nskb;
3032 nskb->next = p->next;
3033 p->next = NULL;
3034
3035 p = nskb;
3036
3037merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003038 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003039 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003040 unsigned int eat = offset - headlen;
3041
3042 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003043 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003044 skb->data_len -= eat;
3045 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003046 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003047 }
3048
Herbert Xu67147ba2009-05-26 18:50:22 +00003049 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003050
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003051 NAPI_GRO_CB(p)->last->next = skb;
3052 NAPI_GRO_CB(p)->last = skb;
Herbert Xu71d93b32008-12-15 23:42:33 -08003053 skb_header_release(skb);
3054
Herbert Xu5d38a072009-01-04 16:13:40 -08003055done:
3056 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003057 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003058 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003059 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003060
3061 NAPI_GRO_CB(skb)->same_flow = 1;
3062 return 0;
3063}
3064EXPORT_SYMBOL_GPL(skb_gro_receive);
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066void __init skb_init(void)
3067{
3068 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3069 sizeof(struct sk_buff),
3070 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003071 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003072 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003073 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3074 (2*sizeof(struct sk_buff)) +
3075 sizeof(atomic_t),
3076 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003077 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003078 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
David Howells716ea3a2007-04-02 20:19:53 -07003081/**
3082 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3083 * @skb: Socket buffer containing the buffers to be mapped
3084 * @sg: The scatter-gather list to map into
3085 * @offset: The offset into the buffer's contents to start mapping
3086 * @len: Length of buffer space to be mapped
3087 *
3088 * Fill the specified scatter-gather list with mappings/pointers into a
3089 * region of the buffer space attached to a socket buffer.
3090 */
David S. Miller51c739d2007-10-30 21:29:29 -07003091static int
3092__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003093{
David S. Miller1a028e52007-04-27 15:21:23 -07003094 int start = skb_headlen(skb);
3095 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003096 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003097 int elt = 0;
3098
3099 if (copy > 0) {
3100 if (copy > len)
3101 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003102 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003103 elt++;
3104 if ((len -= copy) == 0)
3105 return elt;
3106 offset += copy;
3107 }
3108
3109 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003110 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003111
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003112 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003113
Eric Dumazet9e903e02011-10-18 21:00:24 +00003114 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003115 if ((copy = end - offset) > 0) {
3116 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3117
3118 if (copy > len)
3119 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003120 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003121 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003122 elt++;
3123 if (!(len -= copy))
3124 return elt;
3125 offset += copy;
3126 }
David S. Miller1a028e52007-04-27 15:21:23 -07003127 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003128 }
3129
David S. Millerfbb398a2009-06-09 00:18:59 -07003130 skb_walk_frags(skb, frag_iter) {
3131 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003132
David S. Millerfbb398a2009-06-09 00:18:59 -07003133 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003134
David S. Millerfbb398a2009-06-09 00:18:59 -07003135 end = start + frag_iter->len;
3136 if ((copy = end - offset) > 0) {
3137 if (copy > len)
3138 copy = len;
3139 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3140 copy);
3141 if ((len -= copy) == 0)
3142 return elt;
3143 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003144 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003145 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003146 }
3147 BUG_ON(len);
3148 return elt;
3149}
3150
David S. Miller51c739d2007-10-30 21:29:29 -07003151int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3152{
3153 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3154
Jens Axboec46f2332007-10-31 12:06:37 +01003155 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003156
3157 return nsg;
3158}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003159EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003160
David Howells716ea3a2007-04-02 20:19:53 -07003161/**
3162 * skb_cow_data - Check that a socket buffer's data buffers are writable
3163 * @skb: The socket buffer to check.
3164 * @tailbits: Amount of trailing space to be added
3165 * @trailer: Returned pointer to the skb where the @tailbits space begins
3166 *
3167 * Make sure that the data buffers attached to a socket buffer are
3168 * writable. If they are not, private copies are made of the data buffers
3169 * and the socket buffer is set to use these instead.
3170 *
3171 * If @tailbits is given, make sure that there is space to write @tailbits
3172 * bytes of data beyond current end of socket buffer. @trailer will be
3173 * set to point to the skb in which this space begins.
3174 *
3175 * The number of scatterlist elements required to completely map the
3176 * COW'd and extended socket buffer will be returned.
3177 */
3178int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3179{
3180 int copyflag;
3181 int elt;
3182 struct sk_buff *skb1, **skb_p;
3183
3184 /* If skb is cloned or its head is paged, reallocate
3185 * head pulling out all the pages (pages are considered not writable
3186 * at the moment even if they are anonymous).
3187 */
3188 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3189 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3190 return -ENOMEM;
3191
3192 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003193 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003194 /* A little of trouble, not enough of space for trailer.
3195 * This should not happen, when stack is tuned to generate
3196 * good frames. OK, on miss we reallocate and reserve even more
3197 * space, 128 bytes is fair. */
3198
3199 if (skb_tailroom(skb) < tailbits &&
3200 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3201 return -ENOMEM;
3202
3203 /* Voila! */
3204 *trailer = skb;
3205 return 1;
3206 }
3207
3208 /* Misery. We are in troubles, going to mincer fragments... */
3209
3210 elt = 1;
3211 skb_p = &skb_shinfo(skb)->frag_list;
3212 copyflag = 0;
3213
3214 while ((skb1 = *skb_p) != NULL) {
3215 int ntail = 0;
3216
3217 /* The fragment is partially pulled by someone,
3218 * this can happen on input. Copy it and everything
3219 * after it. */
3220
3221 if (skb_shared(skb1))
3222 copyflag = 1;
3223
3224 /* If the skb is the last, worry about trailer. */
3225
3226 if (skb1->next == NULL && tailbits) {
3227 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003228 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003229 skb_tailroom(skb1) < tailbits)
3230 ntail = tailbits + 128;
3231 }
3232
3233 if (copyflag ||
3234 skb_cloned(skb1) ||
3235 ntail ||
3236 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003237 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003238 struct sk_buff *skb2;
3239
3240 /* Fuck, we are miserable poor guys... */
3241 if (ntail == 0)
3242 skb2 = skb_copy(skb1, GFP_ATOMIC);
3243 else
3244 skb2 = skb_copy_expand(skb1,
3245 skb_headroom(skb1),
3246 ntail,
3247 GFP_ATOMIC);
3248 if (unlikely(skb2 == NULL))
3249 return -ENOMEM;
3250
3251 if (skb1->sk)
3252 skb_set_owner_w(skb2, skb1->sk);
3253
3254 /* Looking around. Are we still alive?
3255 * OK, link new skb, drop old one */
3256
3257 skb2->next = skb1->next;
3258 *skb_p = skb2;
3259 kfree_skb(skb1);
3260 skb1 = skb2;
3261 }
3262 elt++;
3263 *trailer = skb1;
3264 skb_p = &skb1->next;
3265 }
3266
3267 return elt;
3268}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003269EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003270
Eric Dumazetb1faf562010-05-31 23:44:05 -07003271static void sock_rmem_free(struct sk_buff *skb)
3272{
3273 struct sock *sk = skb->sk;
3274
3275 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3276}
3277
3278/*
3279 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3280 */
3281int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3282{
Eric Dumazet110c4332012-04-06 10:49:10 +02003283 int len = skb->len;
3284
Eric Dumazetb1faf562010-05-31 23:44:05 -07003285 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003286 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003287 return -ENOMEM;
3288
3289 skb_orphan(skb);
3290 skb->sk = sk;
3291 skb->destructor = sock_rmem_free;
3292 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3293
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003294 /* before exiting rcu section, make sure dst is refcounted */
3295 skb_dst_force(skb);
3296
Eric Dumazetb1faf562010-05-31 23:44:05 -07003297 skb_queue_tail(&sk->sk_error_queue, skb);
3298 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003299 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003300 return 0;
3301}
3302EXPORT_SYMBOL(sock_queue_err_skb);
3303
Patrick Ohlyac45f602009-02-12 05:03:37 +00003304void skb_tstamp_tx(struct sk_buff *orig_skb,
3305 struct skb_shared_hwtstamps *hwtstamps)
3306{
3307 struct sock *sk = orig_skb->sk;
3308 struct sock_exterr_skb *serr;
3309 struct sk_buff *skb;
3310 int err;
3311
3312 if (!sk)
3313 return;
3314
3315 skb = skb_clone(orig_skb, GFP_ATOMIC);
3316 if (!skb)
3317 return;
3318
3319 if (hwtstamps) {
3320 *skb_hwtstamps(skb) =
3321 *hwtstamps;
3322 } else {
3323 /*
3324 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003325 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003326 * store software time stamp
3327 */
3328 skb->tstamp = ktime_get_real();
3329 }
3330
3331 serr = SKB_EXT_ERR(skb);
3332 memset(serr, 0, sizeof(*serr));
3333 serr->ee.ee_errno = ENOMSG;
3334 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003335
Patrick Ohlyac45f602009-02-12 05:03:37 +00003336 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003337
Patrick Ohlyac45f602009-02-12 05:03:37 +00003338 if (err)
3339 kfree_skb(skb);
3340}
3341EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3342
Johannes Berg6e3e9392011-11-09 10:15:42 +01003343void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3344{
3345 struct sock *sk = skb->sk;
3346 struct sock_exterr_skb *serr;
3347 int err;
3348
3349 skb->wifi_acked_valid = 1;
3350 skb->wifi_acked = acked;
3351
3352 serr = SKB_EXT_ERR(skb);
3353 memset(serr, 0, sizeof(*serr));
3354 serr->ee.ee_errno = ENOMSG;
3355 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3356
3357 err = sock_queue_err_skb(sk, skb);
3358 if (err)
3359 kfree_skb(skb);
3360}
3361EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3362
Patrick Ohlyac45f602009-02-12 05:03:37 +00003363
Rusty Russellf35d9d82008-02-04 23:49:54 -05003364/**
3365 * skb_partial_csum_set - set up and verify partial csum values for packet
3366 * @skb: the skb to set
3367 * @start: the number of bytes after skb->data to start checksumming.
3368 * @off: the offset from start to place the checksum.
3369 *
3370 * For untrusted partially-checksummed packets, we need to make sure the values
3371 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3372 *
3373 * This function checks and sets those values and skb->ip_summed: if this
3374 * returns false you should drop the packet.
3375 */
3376bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3377{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003378 if (unlikely(start > skb_headlen(skb)) ||
3379 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003380 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3381 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003382 return false;
3383 }
3384 skb->ip_summed = CHECKSUM_PARTIAL;
3385 skb->csum_start = skb_headroom(skb) + start;
3386 skb->csum_offset = off;
3387 return true;
3388}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003389EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003390
Ben Hutchings4497b072008-06-19 16:22:28 -07003391void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3392{
Joe Perchese87cc472012-05-13 21:56:26 +00003393 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3394 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003395}
Ben Hutchings4497b072008-06-19 16:22:28 -07003396EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003397
3398void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3399{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003400 if (head_stolen) {
3401 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003402 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003403 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003404 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003405 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003406}
3407EXPORT_SYMBOL(kfree_skb_partial);
3408
3409/**
3410 * skb_try_coalesce - try to merge skb to prior one
3411 * @to: prior buffer
3412 * @from: buffer to add
3413 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003414 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003415 */
3416bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3417 bool *fragstolen, int *delta_truesize)
3418{
3419 int i, delta, len = from->len;
3420
3421 *fragstolen = false;
3422
3423 if (skb_cloned(to))
3424 return false;
3425
3426 if (len <= skb_tailroom(to)) {
3427 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3428 *delta_truesize = 0;
3429 return true;
3430 }
3431
3432 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3433 return false;
3434
3435 if (skb_headlen(from) != 0) {
3436 struct page *page;
3437 unsigned int offset;
3438
3439 if (skb_shinfo(to)->nr_frags +
3440 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3441 return false;
3442
3443 if (skb_head_is_locked(from))
3444 return false;
3445
3446 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3447
3448 page = virt_to_head_page(from->head);
3449 offset = from->data - (unsigned char *)page_address(page);
3450
3451 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3452 page, offset, skb_headlen(from));
3453 *fragstolen = true;
3454 } else {
3455 if (skb_shinfo(to)->nr_frags +
3456 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3457 return false;
3458
Weiping Panf4b549a2012-09-28 20:15:30 +00003459 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003460 }
3461
3462 WARN_ON_ONCE(delta < len);
3463
3464 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3465 skb_shinfo(from)->frags,
3466 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3467 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3468
3469 if (!skb_cloned(from))
3470 skb_shinfo(from)->nr_frags = 0;
3471
Li RongQing8ea853f2012-09-18 16:53:21 +00003472 /* if the skb is not cloned this does nothing
3473 * since we set nr_frags to 0.
3474 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003475 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3476 skb_frag_ref(from, i);
3477
3478 to->truesize += delta;
3479 to->len += len;
3480 to->data_len += len;
3481
3482 *delta_truesize = delta;
3483 return true;
3484}
3485EXPORT_SYMBOL(skb_try_coalesce);