blob: 32443ebc3e890532810b94c6196b182ccdf558d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
50#include <linux/netdevice.h>
51#ifdef CONFIG_NET_CLS_ACT
52#include <net/pkt_sched.h>
53#endif
54#include <linux/string.h>
55#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080056#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/cache.h>
58#include <linux/rtnetlink.h>
59#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070060#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000061#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070062#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <net/protocol.h>
65#include <net/dst.h>
66#include <net/sock.h>
67#include <net/checksum.h>
68#include <net/xfrm.h>
69
70#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040071#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020072#include <linux/highmem.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040073
Eric Dumazetd7e88832012-04-30 08:10:34 +000074struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080075static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jens Axboe9c55e012007-11-06 23:30:13 -080077static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
78 struct pipe_buffer *buf)
79{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080080 put_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080081}
82
83static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
84 struct pipe_buffer *buf)
85{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080086 get_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080087}
88
89static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
91{
92 return 1;
93}
94
95
96/* Pipe buffer operations for a socket. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -080097static const struct pipe_buf_operations sock_pipe_buf_ops = {
Jens Axboe9c55e012007-11-06 23:30:13 -080098 .can_merge = 0,
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
110 * reliable.
111 */
112
113/**
114 * skb_over_panic - private function
115 * @skb: buffer
116 * @sz: size
117 * @here: address
118 *
119 * Out of line support code for skb_put(). Not user callable.
120 */
Rami Rosenccb7c772010-04-20 22:39:53 -0700121static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Joe Perchese005d192012-05-16 19:58:40 +0000123 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
124 __func__, here, skb->len, sz, skb->head, skb->data,
125 (unsigned long)skb->tail, (unsigned long)skb->end,
126 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 BUG();
128}
129
130/**
131 * skb_under_panic - private function
132 * @skb: buffer
133 * @sz: size
134 * @here: address
135 *
136 * Out of line support code for skb_push(). Not user callable.
137 */
138
Rami Rosenccb7c772010-04-20 22:39:53 -0700139static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Joe Perchese005d192012-05-16 19:58:40 +0000141 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
142 __func__, here, skb->len, sz, skb->head, skb->data,
143 (unsigned long)skb->tail, (unsigned long)skb->end,
144 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 BUG();
146}
147
Mel Gormanc93bdd02012-07-31 16:44:19 -0700148
149/*
150 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
151 * the caller if emergency pfmemalloc reserves are being used. If it is and
152 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
153 * may be used. Otherwise, the packet data may be discarded until enough
154 * memory is free
155 */
156#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
157 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
158void *__kmalloc_reserve(size_t size, gfp_t flags, int node, unsigned long ip,
159 bool *pfmemalloc)
160{
161 void *obj;
162 bool ret_pfmemalloc = false;
163
164 /*
165 * Try a regular allocation, when that fails and we're not entitled
166 * to the reserves, fail.
167 */
168 obj = kmalloc_node_track_caller(size,
169 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
170 node);
171 if (obj || !(gfp_pfmemalloc_allowed(flags)))
172 goto out;
173
174 /* Try again but now we are using pfmemalloc reserves */
175 ret_pfmemalloc = true;
176 obj = kmalloc_node_track_caller(size, flags, node);
177
178out:
179 if (pfmemalloc)
180 *pfmemalloc = ret_pfmemalloc;
181
182 return obj;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Allocate a new skbuff. We do this ourselves so we can fill in a few
186 * 'private' fields and also do memory statistics to find all the
187 * [BEEP] leaks.
188 *
189 */
190
191/**
David S. Millerd179cd12005-08-17 14:57:30 -0700192 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * @size: size to allocate
194 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700195 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
196 * instead of head cache and allocate a cloned (child) skb.
197 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
198 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800199 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000202 * tail room of at least size bytes. The object has a reference count
203 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
205 * Buffers may only be allocated from interrupts using a @gfp_mask of
206 * %GFP_ATOMIC.
207 */
Al Virodd0fc662005-10-07 07:46:04 +0100208struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700209 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Christoph Lametere18b8902006-12-06 20:33:20 -0800211 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct sk_buff *skb;
214 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700215 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Mel Gormanc93bdd02012-07-31 16:44:19 -0700217 cache = (flags & SKB_ALLOC_FCLONE)
218 ? skbuff_fclone_cache : skbuff_head_cache;
219
220 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
221 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800224 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!skb)
226 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700227 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 /* We do our best to align skb_shared_info on a separate cache
230 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
231 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
232 * Both skb->head and skb_shared_info are cache line aligned.
233 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000234 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000235 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700236 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!data)
238 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000239 /* kmalloc(size) might give us more room than requested.
240 * Put skb_shared_info exactly at the end of allocated zone,
241 * to allow max possible filling before reallocation.
242 */
243 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700244 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300246 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700247 * Only clear those fields we need to clear, not those that we will
248 * actually initialise below. Hence, don't put any more fields after
249 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300250 */
251 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000252 /* Account for allocated memory : skb + skb->head */
253 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700254 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 atomic_set(&skb->users, 1);
256 skb->head = data;
257 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700258 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700259 skb->end = skb->tail + size;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000260#ifdef NET_SKBUFF_DATA_USES_OFFSET
261 skb->mac_header = ~0U;
262#endif
263
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800264 /* make sure we initialize shinfo sequentially */
265 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700266 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800267 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000268 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800269
Mel Gormanc93bdd02012-07-31 16:44:19 -0700270 if (flags & SKB_ALLOC_FCLONE) {
David S. Millerd179cd12005-08-17 14:57:30 -0700271 struct sk_buff *child = skb + 1;
272 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200274 kmemcheck_annotate_bitfield(child, flags1);
275 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700276 skb->fclone = SKB_FCLONE_ORIG;
277 atomic_set(fclone_ref, 1);
278
279 child->fclone = SKB_FCLONE_UNAVAILABLE;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700280 child->pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282out:
283 return skb;
284nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800285 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 skb = NULL;
287 goto out;
288}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800289EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000292 * build_skb - build a network buffer
293 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000294 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000295 *
296 * Allocate a new &sk_buff. Caller provides space holding head and
297 * skb_shared_info. @data must have been allocated by kmalloc()
298 * The return is the new skb buffer.
299 * On a failure the return is %NULL, and @data is not freed.
300 * Notes :
301 * Before IO, driver allocates only data buffer where NIC put incoming frame
302 * Driver should add room at head (NET_SKB_PAD) and
303 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
304 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
305 * before giving packet to stack.
306 * RX rings only contains data buffers, not full skbs.
307 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000308struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000309{
310 struct skb_shared_info *shinfo;
311 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000312 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313
314 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
315 if (!skb)
316 return NULL;
317
Eric Dumazetd3836f22012-04-27 00:33:38 +0000318 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000319
320 memset(skb, 0, offsetof(struct sk_buff, tail));
321 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000322 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000323 atomic_set(&skb->users, 1);
324 skb->head = data;
325 skb->data = data;
326 skb_reset_tail_pointer(skb);
327 skb->end = skb->tail + size;
328#ifdef NET_SKBUFF_DATA_USES_OFFSET
329 skb->mac_header = ~0U;
330#endif
331
332 /* make sure we initialize shinfo sequentially */
333 shinfo = skb_shinfo(skb);
334 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
335 atomic_set(&shinfo->dataref, 1);
336 kmemcheck_annotate_variable(shinfo->destructor_arg);
337
338 return skb;
339}
340EXPORT_SYMBOL(build_skb);
341
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000342struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000343 struct page_frag frag;
344 /* we maintain a pagecount bias, so that we dont dirty cache line
345 * containing page->_count every time we allocate a fragment.
346 */
347 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000348};
349static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
350
Eric Dumazet69b08f62012-09-26 06:46:57 +0000351#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
352#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
353#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000354
Mel Gormanc93bdd02012-07-31 16:44:19 -0700355static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000356{
357 struct netdev_alloc_cache *nc;
358 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000359 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000360 unsigned long flags;
361
362 local_irq_save(flags);
363 nc = &__get_cpu_var(netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000364 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000365refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000366 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
367 gfp_t gfp = gfp_mask;
368
369 if (order)
370 gfp |= __GFP_COMP | __GFP_NOWARN;
371 nc->frag.page = alloc_pages(gfp, order);
372 if (likely(nc->frag.page))
373 break;
374 if (--order < 0)
375 goto end;
376 }
377 nc->frag.size = PAGE_SIZE << order;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000378recycle:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000379 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
380 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
381 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000382 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000383
Eric Dumazet69b08f62012-09-26 06:46:57 +0000384 if (nc->frag.offset + fragsz > nc->frag.size) {
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000385 /* avoid unnecessary locked operations if possible */
Eric Dumazet69b08f62012-09-26 06:46:57 +0000386 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
387 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000388 goto recycle;
389 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000390 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000391
Eric Dumazet69b08f62012-09-26 06:46:57 +0000392 data = page_address(nc->frag.page) + nc->frag.offset;
393 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000394 nc->pagecnt_bias--;
395end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000396 local_irq_restore(flags);
397 return data;
398}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700399
400/**
401 * netdev_alloc_frag - allocate a page fragment
402 * @fragsz: fragment size
403 *
404 * Allocates a frag from a page for receive buffer.
405 * Uses GFP_ATOMIC allocations.
406 */
407void *netdev_alloc_frag(unsigned int fragsz)
408{
409 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
410}
Eric Dumazet6f532612012-05-18 05:12:12 +0000411EXPORT_SYMBOL(netdev_alloc_frag);
412
413/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700414 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
415 * @dev: network device to receive on
416 * @length: length to allocate
417 * @gfp_mask: get_free_pages mask, passed to alloc_skb
418 *
419 * Allocate a new &sk_buff and assign it a usage count of one. The
420 * buffer has unspecified headroom built in. Users should allocate
421 * the headroom they think they need without accounting for the
422 * built in space. The built in space is used for optimisations.
423 *
424 * %NULL is returned if there is no free memory.
425 */
426struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000427 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700428{
Eric Dumazet6f532612012-05-18 05:12:12 +0000429 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000430 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
431 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700432
Eric Dumazet310e1582012-07-16 13:15:52 +0200433 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700434 void *data;
435
436 if (sk_memalloc_socks())
437 gfp_mask |= __GFP_MEMALLOC;
438
439 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000440
Eric Dumazet6f532612012-05-18 05:12:12 +0000441 if (likely(data)) {
442 skb = build_skb(data, fragsz);
443 if (unlikely(!skb))
444 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000445 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000446 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700447 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
448 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000449 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700450 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700451 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700452 skb->dev = dev;
453 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700454 return skb;
455}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800456EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Peter Zijlstra654bed12008-10-07 14:22:33 -0700458void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000459 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700460{
461 skb_fill_page_desc(skb, i, page, off, size);
462 skb->len += size;
463 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000464 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700465}
466EXPORT_SYMBOL(skb_add_rx_frag);
467
Herbert Xu27b437c2006-07-13 19:26:39 -0700468static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Herbert Xu27b437c2006-07-13 19:26:39 -0700470 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Herbert Xu27b437c2006-07-13 19:26:39 -0700472 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 do {
475 struct sk_buff *this = list;
476 list = list->next;
477 kfree_skb(this);
478 } while (list);
479}
480
Herbert Xu27b437c2006-07-13 19:26:39 -0700481static inline void skb_drop_fraglist(struct sk_buff *skb)
482{
483 skb_drop_list(&skb_shinfo(skb)->frag_list);
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static void skb_clone_fraglist(struct sk_buff *skb)
487{
488 struct sk_buff *list;
489
David S. Millerfbb398a2009-06-09 00:18:59 -0700490 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 skb_get(list);
492}
493
Eric Dumazetd3836f22012-04-27 00:33:38 +0000494static void skb_free_head(struct sk_buff *skb)
495{
496 if (skb->head_frag)
497 put_page(virt_to_head_page(skb->head));
498 else
499 kfree(skb->head);
500}
501
Adrian Bunk5bba1712006-06-29 13:02:35 -0700502static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 if (!skb->cloned ||
505 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
506 &skb_shinfo(skb)->dataref)) {
507 if (skb_shinfo(skb)->nr_frags) {
508 int i;
509 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +0000510 skb_frag_unref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Shirley Maa6686f22011-07-06 12:22:12 +0000513 /*
514 * If skb buf is from userspace, we need to notify the caller
515 * the lower device DMA has done;
516 */
517 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
518 struct ubuf_info *uarg;
519
520 uarg = skb_shinfo(skb)->destructor_arg;
521 if (uarg->callback)
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000522 uarg->callback(uarg, true);
Shirley Maa6686f22011-07-06 12:22:12 +0000523 }
524
David S. Miller21dc3302010-08-23 00:13:46 -0700525 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 skb_drop_fraglist(skb);
527
Eric Dumazetd3836f22012-04-27 00:33:38 +0000528 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530}
531
532/*
533 * Free an skbuff by memory without cleaning the state.
534 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800535static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
David S. Millerd179cd12005-08-17 14:57:30 -0700537 struct sk_buff *other;
538 atomic_t *fclone_ref;
539
David S. Millerd179cd12005-08-17 14:57:30 -0700540 switch (skb->fclone) {
541 case SKB_FCLONE_UNAVAILABLE:
542 kmem_cache_free(skbuff_head_cache, skb);
543 break;
544
545 case SKB_FCLONE_ORIG:
546 fclone_ref = (atomic_t *) (skb + 2);
547 if (atomic_dec_and_test(fclone_ref))
548 kmem_cache_free(skbuff_fclone_cache, skb);
549 break;
550
551 case SKB_FCLONE_CLONE:
552 fclone_ref = (atomic_t *) (skb + 1);
553 other = skb - 1;
554
555 /* The clone portion is available for
556 * fast-cloning again.
557 */
558 skb->fclone = SKB_FCLONE_UNAVAILABLE;
559
560 if (atomic_dec_and_test(fclone_ref))
561 kmem_cache_free(skbuff_fclone_cache, other);
562 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700566static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Eric Dumazetadf30902009-06-02 05:19:30 +0000568 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#ifdef CONFIG_XFRM
570 secpath_put(skb->sp);
571#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700572 if (skb->destructor) {
573 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 skb->destructor(skb);
575 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000576#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700577 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100578#endif
579#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800580 nf_conntrack_put_reasm(skb->nfct_reasm);
581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#ifdef CONFIG_BRIDGE_NETFILTER
583 nf_bridge_put(skb->nf_bridge);
584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* XXX: IS this still necessary? - JHS */
586#ifdef CONFIG_NET_SCHED
587 skb->tc_index = 0;
588#ifdef CONFIG_NET_CLS_ACT
589 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#endif
591#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700592}
593
594/* Free everything but the sk_buff shell. */
595static void skb_release_all(struct sk_buff *skb)
596{
597 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800598 skb_release_data(skb);
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Herbert Xu2d4baff2007-11-26 23:11:19 +0800601/**
602 * __kfree_skb - private function
603 * @skb: buffer
604 *
605 * Free an sk_buff. Release anything attached to the buffer.
606 * Clean the state. This is an internal helper function. Users should
607 * always call kfree_skb
608 */
609
610void __kfree_skb(struct sk_buff *skb)
611{
612 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kfree_skbmem(skb);
614}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800615EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800618 * kfree_skb - free an sk_buff
619 * @skb: buffer to free
620 *
621 * Drop a reference to the buffer and free it if the usage count has
622 * hit zero.
623 */
624void kfree_skb(struct sk_buff *skb)
625{
626 if (unlikely(!skb))
627 return;
628 if (likely(atomic_read(&skb->users) == 1))
629 smp_rmb();
630 else if (likely(!atomic_dec_and_test(&skb->users)))
631 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000632 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800633 __kfree_skb(skb);
634}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800635EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800636
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700637/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000638 * skb_tx_error - report an sk_buff xmit error
639 * @skb: buffer that triggered an error
640 *
641 * Report xmit error if a device callback is tracking this skb.
642 * skb must be freed afterwards.
643 */
644void skb_tx_error(struct sk_buff *skb)
645{
646 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
647 struct ubuf_info *uarg;
648
649 uarg = skb_shinfo(skb)->destructor_arg;
650 if (uarg->callback)
651 uarg->callback(uarg, false);
652 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
653 }
654}
655EXPORT_SYMBOL(skb_tx_error);
656
657/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000658 * consume_skb - free an skbuff
659 * @skb: buffer to free
660 *
661 * Drop a ref to the buffer and free it if the usage count has hit zero
662 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
663 * is being dropped after a failure and notes that
664 */
665void consume_skb(struct sk_buff *skb)
666{
667 if (unlikely(!skb))
668 return;
669 if (likely(atomic_read(&skb->users) == 1))
670 smp_rmb();
671 else if (likely(!atomic_dec_and_test(&skb->users)))
672 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900673 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000674 __kfree_skb(skb);
675}
676EXPORT_SYMBOL(consume_skb);
677
Herbert Xudec18812007-10-14 00:37:30 -0700678static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
679{
680 new->tstamp = old->tstamp;
681 new->dev = old->dev;
682 new->transport_header = old->transport_header;
683 new->network_header = old->network_header;
684 new->mac_header = old->mac_header;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000685 new->inner_transport_header = old->inner_transport_header;
Pravin B Shelar92df9b22013-02-01 15:18:49 +0000686 new->inner_network_header = old->inner_network_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000687 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000688 new->rxhash = old->rxhash;
Changli Gao6461be32011-08-19 04:44:18 +0000689 new->ooo_okay = old->ooo_okay;
Tom Herbertbdeab992011-08-14 19:45:55 +0000690 new->l4_rxhash = old->l4_rxhash;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000691 new->no_fcs = old->no_fcs;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000692 new->encapsulation = old->encapsulation;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700693#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700694 new->sp = secpath_get(old->sp);
695#endif
696 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000697 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700698 new->local_df = old->local_df;
699 new->pkt_type = old->pkt_type;
700 new->ip_summed = old->ip_summed;
701 skb_copy_queue_mapping(new, old);
702 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000703#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700704 new->ipvs_property = old->ipvs_property;
705#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700706 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700707 new->protocol = old->protocol;
708 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800709 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700710 __nf_copy(new, old);
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000711#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Herbert Xudec18812007-10-14 00:37:30 -0700712 new->nf_trace = old->nf_trace;
713#endif
714#ifdef CONFIG_NET_SCHED
715 new->tc_index = old->tc_index;
716#ifdef CONFIG_NET_CLS_ACT
717 new->tc_verd = old->tc_verd;
718#endif
719#endif
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700720 new->vlan_tci = old->vlan_tci;
721
Herbert Xudec18812007-10-14 00:37:30 -0700722 skb_copy_secmark(new, old);
723}
724
Herbert Xu82c49a32009-05-22 22:11:37 +0000725/*
726 * You should not add any new code to this function. Add it to
727 * __copy_skb_header above instead.
728 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700729static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#define C(x) n->x = skb->x
732
733 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700735 __copy_skb_header(n, skb);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 C(len);
738 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700739 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700740 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800741 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 C(tail);
745 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800746 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000747 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800748 C(data);
749 C(truesize);
750 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 atomic_inc(&(skb_shinfo(skb)->dataref));
753 skb->cloned = 1;
754
755 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700756#undef C
757}
758
759/**
760 * skb_morph - morph one skb into another
761 * @dst: the skb to receive the contents
762 * @src: the skb to supply the contents
763 *
764 * This is identical to skb_clone except that the target skb is
765 * supplied by the user.
766 *
767 * The target skb is returned upon exit.
768 */
769struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
770{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800771 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700772 return __skb_clone(dst, src);
773}
774EXPORT_SYMBOL_GPL(skb_morph);
775
Ben Hutchings2c530402012-07-10 10:55:09 +0000776/**
777 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000778 * @skb: the skb to modify
779 * @gfp_mask: allocation priority
780 *
781 * This must be called on SKBTX_DEV_ZEROCOPY skb.
782 * It will copy all frags into kernel and drop the reference
783 * to userspace pages.
784 *
785 * If this function is called from an interrupt gfp_mask() must be
786 * %GFP_ATOMIC.
787 *
788 * Returns 0 on success or a negative error code on failure
789 * to allocate kernel memory to copy to.
790 */
791int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000792{
793 int i;
794 int num_frags = skb_shinfo(skb)->nr_frags;
795 struct page *page, *head = NULL;
796 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
797
798 for (i = 0; i < num_frags; i++) {
799 u8 *vaddr;
800 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
801
Krishna Kumar02756ed2012-07-17 02:05:29 +0000802 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000803 if (!page) {
804 while (head) {
805 struct page *next = (struct page *)head->private;
806 put_page(head);
807 head = next;
808 }
809 return -ENOMEM;
810 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200811 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000812 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000813 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200814 kunmap_atomic(vaddr);
Shirley Maa6686f22011-07-06 12:22:12 +0000815 page->private = (unsigned long)head;
816 head = page;
817 }
818
819 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000820 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000821 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000822
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000823 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000824
825 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000826 for (i = num_frags - 1; i >= 0; i--) {
827 __skb_fill_page_desc(skb, i, head, 0,
828 skb_shinfo(skb)->frags[i].size);
Shirley Maa6686f22011-07-06 12:22:12 +0000829 head = (struct page *)head->private;
830 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000831
832 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000833 return 0;
834}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000835EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000836
Herbert Xue0053ec2007-10-14 00:37:52 -0700837/**
838 * skb_clone - duplicate an sk_buff
839 * @skb: buffer to clone
840 * @gfp_mask: allocation priority
841 *
842 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
843 * copies share the same packet data but not structure. The new
844 * buffer has a reference count of 1. If the allocation fails the
845 * function returns %NULL otherwise the new buffer is returned.
846 *
847 * If this function is called from an interrupt gfp_mask() must be
848 * %GFP_ATOMIC.
849 */
850
851struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
852{
853 struct sk_buff *n;
854
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000855 if (skb_orphan_frags(skb, gfp_mask))
856 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000857
Herbert Xue0053ec2007-10-14 00:37:52 -0700858 n = skb + 1;
859 if (skb->fclone == SKB_FCLONE_ORIG &&
860 n->fclone == SKB_FCLONE_UNAVAILABLE) {
861 atomic_t *fclone_ref = (atomic_t *) (n + 1);
862 n->fclone = SKB_FCLONE_CLONE;
863 atomic_inc(fclone_ref);
864 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700865 if (skb_pfmemalloc(skb))
866 gfp_mask |= __GFP_MEMALLOC;
867
Herbert Xue0053ec2007-10-14 00:37:52 -0700868 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
869 if (!n)
870 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200871
872 kmemcheck_annotate_bitfield(n, flags1);
873 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700874 n->fclone = SKB_FCLONE_UNAVAILABLE;
875 }
876
877 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800879EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
882{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700883#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
885 * Shift between the two data areas in bytes
886 */
887 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700888#endif
Herbert Xudec18812007-10-14 00:37:30 -0700889
890 __copy_skb_header(new, old);
891
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700892#ifndef NET_SKBUFF_DATA_USES_OFFSET
893 /* {transport,network,mac}_header are relative to skb->head */
894 new->transport_header += offset;
895 new->network_header += offset;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000896 if (skb_mac_header_was_set(new))
897 new->mac_header += offset;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000898 new->inner_transport_header += offset;
899 new->inner_network_header += offset;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700900#endif
Herbert Xu79671682006-06-22 02:40:14 -0700901 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
902 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
903 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Mel Gormanc93bdd02012-07-31 16:44:19 -0700906static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
907{
908 if (skb_pfmemalloc(skb))
909 return SKB_ALLOC_RX;
910 return 0;
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/**
914 * skb_copy - create private copy of an sk_buff
915 * @skb: buffer to copy
916 * @gfp_mask: allocation priority
917 *
918 * Make a copy of both an &sk_buff and its data. This is used when the
919 * caller wishes to modify the data and needs a private copy of the
920 * data to alter. Returns %NULL on failure or the pointer to the buffer
921 * on success. The returned buffer has a reference count of 1.
922 *
923 * As by-product this function converts non-linear &sk_buff to linear
924 * one, so that &sk_buff becomes completely private and caller is allowed
925 * to modify all the data of returned buffer. This means that this
926 * function is not recommended for use in circumstances when only
927 * header is going to be modified. Use pskb_copy() instead.
928 */
929
Al Virodd0fc662005-10-07 07:46:04 +0100930struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000932 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000933 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700934 struct sk_buff *n = __alloc_skb(size, gfp_mask,
935 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!n)
938 return NULL;
939
940 /* Set the data pointer */
941 skb_reserve(n, headerlen);
942 /* Set the tail pointer and length */
943 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
946 BUG();
947
948 copy_skb_header(n, skb);
949 return n;
950}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800951EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953/**
Eric Dumazet117632e2011-12-03 21:39:53 +0000954 * __pskb_copy - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000956 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * @gfp_mask: allocation priority
958 *
959 * Make a copy of both an &sk_buff and part of its data, located
960 * in header. Fragmented data remain shared. This is used when
961 * the caller wishes to modify only header of &sk_buff and needs
962 * private copy of the header to alter. Returns %NULL on failure
963 * or the pointer to the buffer on success.
964 * The returned buffer has a reference count of 1.
965 */
966
Eric Dumazet117632e2011-12-03 21:39:53 +0000967struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Eric Dumazet117632e2011-12-03 21:39:53 +0000969 unsigned int size = skb_headlen(skb) + headroom;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700970 struct sk_buff *n = __alloc_skb(size, gfp_mask,
971 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!n)
974 goto out;
975
976 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000977 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* Set the tail pointer and length */
979 skb_put(n, skb_headlen(skb));
980 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300981 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Herbert Xu25f484a2006-11-07 14:57:15 -0800983 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 n->data_len = skb->data_len;
985 n->len = skb->len;
986
987 if (skb_shinfo(skb)->nr_frags) {
988 int i;
989
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000990 if (skb_orphan_frags(skb, gfp_mask)) {
991 kfree_skb(n);
992 n = NULL;
993 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +0000994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
996 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +0000997 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 skb_shinfo(n)->nr_frags = i;
1000 }
1001
David S. Miller21dc3302010-08-23 00:13:46 -07001002 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1004 skb_clone_fraglist(n);
1005 }
1006
1007 copy_skb_header(n, skb);
1008out:
1009 return n;
1010}
Eric Dumazet117632e2011-12-03 21:39:53 +00001011EXPORT_SYMBOL(__pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013/**
1014 * pskb_expand_head - reallocate header of &sk_buff
1015 * @skb: buffer to reallocate
1016 * @nhead: room to add at head
1017 * @ntail: room to add at tail
1018 * @gfp_mask: allocation priority
1019 *
1020 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1021 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1022 * reference count of 1. Returns zero in the case of success or error,
1023 * if expansion failed. In the last case, &sk_buff is not changed.
1024 *
1025 * All the pointers pointing into skb header may change and must be
1026 * reloaded after call to this function.
1027 */
1028
Victor Fusco86a76ca2005-07-08 14:57:47 -07001029int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001030 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int i;
1033 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001034 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 long off;
1036
Herbert Xu4edd87a2008-10-01 07:09:38 -07001037 BUG_ON(nhead < 0);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (skb_shared(skb))
1040 BUG();
1041
1042 size = SKB_DATA_ALIGN(size);
1043
Mel Gormanc93bdd02012-07-31 16:44:19 -07001044 if (skb_pfmemalloc(skb))
1045 gfp_mask |= __GFP_MEMALLOC;
1046 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1047 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (!data)
1049 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001050 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001053 * optimized for the cases when header is void.
1054 */
1055 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1056
1057 memcpy((struct skb_shared_info *)(data + size),
1058 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001059 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alexander Duyck3e245912012-05-04 14:26:51 +00001061 /*
1062 * if shinfo is shared we must drop the old head gracefully, but if it
1063 * is not we can just drop the old head and let the existing refcount
1064 * be since all we did is relocate the values
1065 */
1066 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001067 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001068 if (skb_orphan_frags(skb, gfp_mask))
1069 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001070 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001071 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Eric Dumazet1fd63042010-09-02 23:09:32 +00001073 if (skb_has_frag_list(skb))
1074 skb_clone_fraglist(skb);
1075
1076 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001077 } else {
1078 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 off = (data + nhead) - skb->head;
1081
1082 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001083 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001085#ifdef NET_SKBUFF_DATA_USES_OFFSET
1086 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001087 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001088#else
1089 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001090#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001091 /* {transport,network,mac}_header and tail are relative to skb->head */
1092 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001093 skb->transport_header += off;
1094 skb->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001095 if (skb_mac_header_was_set(skb))
1096 skb->mac_header += off;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001097 skb->inner_transport_header += off;
1098 skb->inner_network_header += off;
Andrea Shepard00c5a982010-07-22 09:12:35 +00001099 /* Only adjust this if it actually is csum_start rather than csum */
1100 if (skb->ip_summed == CHECKSUM_PARTIAL)
1101 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001103 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 skb->nohdr = 0;
1105 atomic_set(&skb_shinfo(skb)->dataref, 1);
1106 return 0;
1107
Shirley Maa6686f22011-07-06 12:22:12 +00001108nofrags:
1109 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110nodata:
1111 return -ENOMEM;
1112}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001113EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115/* Make private copy of skb with writable head and some headroom */
1116
1117struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1118{
1119 struct sk_buff *skb2;
1120 int delta = headroom - skb_headroom(skb);
1121
1122 if (delta <= 0)
1123 skb2 = pskb_copy(skb, GFP_ATOMIC);
1124 else {
1125 skb2 = skb_clone(skb, GFP_ATOMIC);
1126 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1127 GFP_ATOMIC)) {
1128 kfree_skb(skb2);
1129 skb2 = NULL;
1130 }
1131 }
1132 return skb2;
1133}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001134EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136/**
1137 * skb_copy_expand - copy and expand sk_buff
1138 * @skb: buffer to copy
1139 * @newheadroom: new free bytes at head
1140 * @newtailroom: new free bytes at tail
1141 * @gfp_mask: allocation priority
1142 *
1143 * Make a copy of both an &sk_buff and its data and while doing so
1144 * allocate additional space.
1145 *
1146 * This is used when the caller wishes to modify the data and needs a
1147 * private copy of the data to alter as well as more space for new fields.
1148 * Returns %NULL on failure or the pointer to the buffer
1149 * on success. The returned buffer has a reference count of 1.
1150 *
1151 * You must pass %GFP_ATOMIC as the allocation priority if this function
1152 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
1154struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001155 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001156 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 /*
1159 * Allocate the copy buffer
1160 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001161 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1162 gfp_mask, skb_alloc_rx_flag(skb),
1163 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001164 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -07001166 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!n)
1169 return NULL;
1170
1171 skb_reserve(n, newheadroom);
1172
1173 /* Set the tail pointer and length */
1174 skb_put(n, skb->len);
1175
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001176 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 head_copy_off = 0;
1178 if (newheadroom <= head_copy_len)
1179 head_copy_len = newheadroom;
1180 else
1181 head_copy_off = newheadroom - head_copy_len;
1182
1183 /* Copy the linear header and data. */
1184 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1185 skb->len + head_copy_len))
1186 BUG();
1187
1188 copy_skb_header(n, skb);
1189
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001190 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -07001191 if (n->ip_summed == CHECKSUM_PARTIAL)
1192 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -07001193#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001194 n->transport_header += off;
1195 n->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001196 if (skb_mac_header_was_set(skb))
1197 n->mac_header += off;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +00001198 n->inner_transport_header += off;
1199 n->inner_network_header += off;
Herbert Xu52886052007-09-16 16:32:11 -07001200#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return n;
1203}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001204EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206/**
1207 * skb_pad - zero pad the tail of an skb
1208 * @skb: buffer to pad
1209 * @pad: space to pad
1210 *
1211 * Ensure that a buffer is followed by a padding area that is zero
1212 * filled. Used by network drivers which may DMA or transfer data
1213 * beyond the buffer end onto the wire.
1214 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001215 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001217
Herbert Xu5b057c62006-06-23 02:06:41 -07001218int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Herbert Xu5b057c62006-06-23 02:06:41 -07001220 int err;
1221 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001224 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001228
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001229 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001230 if (likely(skb_cloned(skb) || ntail > 0)) {
1231 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1232 if (unlikely(err))
1233 goto free_skb;
1234 }
1235
1236 /* FIXME: The use of this function with non-linear skb's really needs
1237 * to be audited.
1238 */
1239 err = skb_linearize(skb);
1240 if (unlikely(err))
1241 goto free_skb;
1242
1243 memset(skb->data + skb->len, 0, pad);
1244 return 0;
1245
1246free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001248 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001249}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001250EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001251
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001252/**
1253 * skb_put - add data to a buffer
1254 * @skb: buffer to use
1255 * @len: amount of data to add
1256 *
1257 * This function extends the used data area of the buffer. If this would
1258 * exceed the total buffer size the kernel will panic. A pointer to the
1259 * first byte of the extra data is returned.
1260 */
1261unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1262{
1263 unsigned char *tmp = skb_tail_pointer(skb);
1264 SKB_LINEAR_ASSERT(skb);
1265 skb->tail += len;
1266 skb->len += len;
1267 if (unlikely(skb->tail > skb->end))
1268 skb_over_panic(skb, len, __builtin_return_address(0));
1269 return tmp;
1270}
1271EXPORT_SYMBOL(skb_put);
1272
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001273/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001274 * skb_push - add data to the start of a buffer
1275 * @skb: buffer to use
1276 * @len: amount of data to add
1277 *
1278 * This function extends the used data area of the buffer at the buffer
1279 * start. If this would exceed the total buffer headroom the kernel will
1280 * panic. A pointer to the first byte of the extra data is returned.
1281 */
1282unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1283{
1284 skb->data -= len;
1285 skb->len += len;
1286 if (unlikely(skb->data<skb->head))
1287 skb_under_panic(skb, len, __builtin_return_address(0));
1288 return skb->data;
1289}
1290EXPORT_SYMBOL(skb_push);
1291
1292/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001293 * skb_pull - remove data from the start of a buffer
1294 * @skb: buffer to use
1295 * @len: amount of data to remove
1296 *
1297 * This function removes data from the start of a buffer, returning
1298 * the memory to the headroom. A pointer to the next data in the buffer
1299 * is returned. Once the data has been pulled future pushes will overwrite
1300 * the old data.
1301 */
1302unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1303{
David S. Miller47d29642010-05-02 02:21:44 -07001304 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001305}
1306EXPORT_SYMBOL(skb_pull);
1307
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001308/**
1309 * skb_trim - remove end from a buffer
1310 * @skb: buffer to alter
1311 * @len: new length
1312 *
1313 * Cut the length of a buffer down by removing data from the tail. If
1314 * the buffer is already under the length specified it is not modified.
1315 * The skb must be linear.
1316 */
1317void skb_trim(struct sk_buff *skb, unsigned int len)
1318{
1319 if (skb->len > len)
1320 __skb_trim(skb, len);
1321}
1322EXPORT_SYMBOL(skb_trim);
1323
Herbert Xu3cc0e872006-06-09 16:13:38 -07001324/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
1326
Herbert Xu3cc0e872006-06-09 16:13:38 -07001327int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Herbert Xu27b437c2006-07-13 19:26:39 -07001329 struct sk_buff **fragp;
1330 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int offset = skb_headlen(skb);
1332 int nfrags = skb_shinfo(skb)->nr_frags;
1333 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001334 int err;
1335
1336 if (skb_cloned(skb) &&
1337 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001340 i = 0;
1341 if (offset >= len)
1342 goto drop_pages;
1343
1344 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001345 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001346
1347 if (end < len) {
1348 offset = end;
1349 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001351
Eric Dumazet9e903e02011-10-18 21:00:24 +00001352 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001353
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001354drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001355 skb_shinfo(skb)->nr_frags = i;
1356
1357 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001358 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001359
David S. Miller21dc3302010-08-23 00:13:46 -07001360 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001361 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001362 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Herbert Xu27b437c2006-07-13 19:26:39 -07001365 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1366 fragp = &frag->next) {
1367 int end = offset + frag->len;
1368
1369 if (skb_shared(frag)) {
1370 struct sk_buff *nfrag;
1371
1372 nfrag = skb_clone(frag, GFP_ATOMIC);
1373 if (unlikely(!nfrag))
1374 return -ENOMEM;
1375
1376 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001377 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001378 frag = nfrag;
1379 *fragp = frag;
1380 }
1381
1382 if (end < len) {
1383 offset = end;
1384 continue;
1385 }
1386
1387 if (end > len &&
1388 unlikely((err = pskb_trim(frag, len - offset))))
1389 return err;
1390
1391 if (frag->next)
1392 skb_drop_list(&frag->next);
1393 break;
1394 }
1395
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001396done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001397 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 skb->data_len -= skb->len - len;
1399 skb->len = len;
1400 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001401 skb->len = len;
1402 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001403 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
1406 return 0;
1407}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001408EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410/**
1411 * __pskb_pull_tail - advance tail of skb header
1412 * @skb: buffer to reallocate
1413 * @delta: number of bytes to advance tail
1414 *
1415 * The function makes a sense only on a fragmented &sk_buff,
1416 * it expands header moving its tail forward and copying necessary
1417 * data from fragmented part.
1418 *
1419 * &sk_buff MUST have reference count of 1.
1420 *
1421 * Returns %NULL (and &sk_buff does not change) if pull failed
1422 * or value of new tail of skb in the case of success.
1423 *
1424 * All the pointers pointing into skb header may change and must be
1425 * reloaded after call to this function.
1426 */
1427
1428/* Moves tail of skb head forward, copying data from fragmented part,
1429 * when it is necessary.
1430 * 1. It may fail due to malloc failure.
1431 * 2. It may change skb pointers.
1432 *
1433 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1434 */
1435unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1436{
1437 /* If skb has not enough free space at tail, get new one
1438 * plus 128 bytes for future expansions. If we have enough
1439 * room at tail, reallocate without expansion only if skb is cloned.
1440 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001441 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (eat > 0 || skb_cloned(skb)) {
1444 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1445 GFP_ATOMIC))
1446 return NULL;
1447 }
1448
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001449 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 BUG();
1451
1452 /* Optimization: no fragments, no reasons to preestimate
1453 * size of pulled pages. Superb.
1454 */
David S. Miller21dc3302010-08-23 00:13:46 -07001455 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto pull_pages;
1457
1458 /* Estimate size of pulled pages. */
1459 eat = delta;
1460 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001461 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1462
1463 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001465 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 /* If we need update frag list, we are in troubles.
1469 * Certainly, it possible to add an offset to skb data,
1470 * but taking into account that pulling is expected to
1471 * be very rare operation, it is worth to fight against
1472 * further bloating skb head and crucify ourselves here instead.
1473 * Pure masohism, indeed. 8)8)
1474 */
1475 if (eat) {
1476 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1477 struct sk_buff *clone = NULL;
1478 struct sk_buff *insp = NULL;
1479
1480 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001481 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (list->len <= eat) {
1484 /* Eaten as whole. */
1485 eat -= list->len;
1486 list = list->next;
1487 insp = list;
1488 } else {
1489 /* Eaten partially. */
1490
1491 if (skb_shared(list)) {
1492 /* Sucks! We need to fork list. :-( */
1493 clone = skb_clone(list, GFP_ATOMIC);
1494 if (!clone)
1495 return NULL;
1496 insp = list->next;
1497 list = clone;
1498 } else {
1499 /* This may be pulled without
1500 * problems. */
1501 insp = list;
1502 }
1503 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001504 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return NULL;
1506 }
1507 break;
1508 }
1509 } while (eat);
1510
1511 /* Free pulled out fragments. */
1512 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1513 skb_shinfo(skb)->frag_list = list->next;
1514 kfree_skb(list);
1515 }
1516 /* And insert new clone at head. */
1517 if (clone) {
1518 clone->next = list;
1519 skb_shinfo(skb)->frag_list = clone;
1520 }
1521 }
1522 /* Success! Now we may commit changes to skb data. */
1523
1524pull_pages:
1525 eat = delta;
1526 k = 0;
1527 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001528 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1529
1530 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001531 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001532 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 } else {
1534 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1535 if (eat) {
1536 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001537 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 eat = 0;
1539 }
1540 k++;
1541 }
1542 }
1543 skb_shinfo(skb)->nr_frags = k;
1544
1545 skb->tail += delta;
1546 skb->data_len -= delta;
1547
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001548 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001550EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Eric Dumazet22019b12011-07-29 18:37:31 +00001552/**
1553 * skb_copy_bits - copy bits from skb to kernel buffer
1554 * @skb: source skb
1555 * @offset: offset in source
1556 * @to: destination buffer
1557 * @len: number of bytes to copy
1558 *
1559 * Copy the specified number of bytes from the source skb to the
1560 * destination buffer.
1561 *
1562 * CAUTION ! :
1563 * If its prototype is ever changed,
1564 * check arch/{*}/net/{*}.S files,
1565 * since it is called from BPF assembly code.
1566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1568{
David S. Miller1a028e52007-04-27 15:21:23 -07001569 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001570 struct sk_buff *frag_iter;
1571 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (offset > (int)skb->len - len)
1574 goto fault;
1575
1576 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001577 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (copy > len)
1579 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001580 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if ((len -= copy) == 0)
1582 return 0;
1583 offset += copy;
1584 to += copy;
1585 }
1586
1587 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001588 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001589 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001591 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001592
Eric Dumazet51c56b02012-04-05 11:35:15 +02001593 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if ((copy = end - offset) > 0) {
1595 u8 *vaddr;
1596
1597 if (copy > len)
1598 copy = len;
1599
Eric Dumazet51c56b02012-04-05 11:35:15 +02001600 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001602 vaddr + f->page_offset + offset - start,
1603 copy);
1604 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 if ((len -= copy) == 0)
1607 return 0;
1608 offset += copy;
1609 to += copy;
1610 }
David S. Miller1a028e52007-04-27 15:21:23 -07001611 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
David S. Millerfbb398a2009-06-09 00:18:59 -07001614 skb_walk_frags(skb, frag_iter) {
1615 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
David S. Millerfbb398a2009-06-09 00:18:59 -07001617 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
David S. Millerfbb398a2009-06-09 00:18:59 -07001619 end = start + frag_iter->len;
1620 if ((copy = end - offset) > 0) {
1621 if (copy > len)
1622 copy = len;
1623 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1624 goto fault;
1625 if ((len -= copy) == 0)
1626 return 0;
1627 offset += copy;
1628 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001630 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Shirley Maa6686f22011-07-06 12:22:12 +00001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (!len)
1634 return 0;
1635
1636fault:
1637 return -EFAULT;
1638}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001639EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jens Axboe9c55e012007-11-06 23:30:13 -08001641/*
1642 * Callback from splice_to_pipe(), if we need to release some pages
1643 * at the end of the spd in case we error'ed out in filling the pipe.
1644 */
1645static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1646{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001647 put_page(spd->pages[i]);
1648}
Jens Axboe9c55e012007-11-06 23:30:13 -08001649
David S. Millera108d5f2012-04-23 23:06:11 -04001650static struct page *linear_to_page(struct page *page, unsigned int *len,
1651 unsigned int *offset,
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001652 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001653{
Eric Dumazet5640f762012-09-23 23:04:42 +00001654 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001655
Eric Dumazet5640f762012-09-23 23:04:42 +00001656 if (!sk_page_frag_refill(sk, pfrag))
1657 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001658
Eric Dumazet5640f762012-09-23 23:04:42 +00001659 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001660
Eric Dumazet5640f762012-09-23 23:04:42 +00001661 memcpy(page_address(pfrag->page) + pfrag->offset,
1662 page_address(page) + *offset, *len);
1663 *offset = pfrag->offset;
1664 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001665
Eric Dumazet5640f762012-09-23 23:04:42 +00001666 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001667}
1668
Eric Dumazet41c73a02012-04-22 12:26:16 +00001669static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1670 struct page *page,
1671 unsigned int offset)
1672{
1673 return spd->nr_pages &&
1674 spd->pages[spd->nr_pages - 1] == page &&
1675 (spd->partial[spd->nr_pages - 1].offset +
1676 spd->partial[spd->nr_pages - 1].len == offset);
1677}
1678
Jens Axboe9c55e012007-11-06 23:30:13 -08001679/*
1680 * Fill page/offset/length into spd, if it can hold more pages.
1681 */
David S. Millera108d5f2012-04-23 23:06:11 -04001682static bool spd_fill_page(struct splice_pipe_desc *spd,
1683 struct pipe_inode_info *pipe, struct page *page,
1684 unsigned int *len, unsigned int offset,
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001685 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001686 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001687{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001688 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001689 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001690
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001691 if (linear) {
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001692 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001693 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001694 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001695 }
1696 if (spd_can_coalesce(spd, page, offset)) {
1697 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001698 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001699 }
1700 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001701 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001702 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001703 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001704 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001705
David S. Millera108d5f2012-04-23 23:06:11 -04001706 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001707}
1708
David S. Millera108d5f2012-04-23 23:06:11 -04001709static bool __splice_segment(struct page *page, unsigned int poff,
1710 unsigned int plen, unsigned int *off,
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001711 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001712 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001713 struct sock *sk,
1714 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001715{
1716 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001717 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001718
1719 /* skip this segment if already processed */
1720 if (*off >= plen) {
1721 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001722 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001723 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001724
Octavian Purdila2870c432008-07-15 00:49:11 -07001725 /* ignore any bits we already processed */
Eric Dumazet82bda612013-01-05 21:31:18 +00001726 poff += *off;
1727 plen -= *off;
1728 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001729
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001730 do {
1731 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001732
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001733 if (spd_fill_page(spd, pipe, page, &flen, poff,
1734 linear, sk))
1735 return true;
1736 poff += flen;
1737 plen -= flen;
1738 *len -= flen;
1739 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001740
David S. Millera108d5f2012-04-23 23:06:11 -04001741 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001742}
1743
1744/*
David S. Millera108d5f2012-04-23 23:06:11 -04001745 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001746 * pipe is full or if we already spliced the requested length.
1747 */
David S. Millera108d5f2012-04-23 23:06:11 -04001748static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1749 unsigned int *offset, unsigned int *len,
1750 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001751{
1752 int seg;
1753
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001754 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001755 * If skb->head_frag is set, this 'linear' part is backed by a
1756 * fragment, and if the head is not shared with any clones then
1757 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001758 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001759 if (__splice_segment(virt_to_page(skb->data),
1760 (unsigned long) skb->data & (PAGE_SIZE - 1),
1761 skb_headlen(skb),
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001762 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001763 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001764 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001765 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001766
1767 /*
1768 * then map the fragments
1769 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001770 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1771 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1772
Ian Campbellea2ab692011-08-22 23:44:58 +00001773 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001774 f->page_offset, skb_frag_size(f),
Eric Dumazetbc9540c2013-01-11 14:46:37 +00001775 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001776 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001777 }
1778
David S. Millera108d5f2012-04-23 23:06:11 -04001779 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001780}
1781
1782/*
1783 * Map data from the skb to a pipe. Should handle both the linear part,
1784 * the fragments, and the frag list. It does NOT handle frag lists within
1785 * the frag list, if such a thing exists. We'd probably need to recurse to
1786 * handle that cleanly.
1787 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001788int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001789 struct pipe_inode_info *pipe, unsigned int tlen,
1790 unsigned int flags)
1791{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001792 struct partial_page partial[MAX_SKB_FRAGS];
1793 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001794 struct splice_pipe_desc spd = {
1795 .pages = pages,
1796 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001797 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001798 .flags = flags,
1799 .ops = &sock_pipe_buf_ops,
1800 .spd_release = sock_spd_release,
1801 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001802 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001803 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001804 int ret = 0;
1805
Jens Axboe9c55e012007-11-06 23:30:13 -08001806 /*
1807 * __skb_splice_bits() only fails if the output has no room left,
1808 * so no point in going over the frag_list for the error case.
1809 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001810 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001811 goto done;
1812 else if (!tlen)
1813 goto done;
1814
1815 /*
1816 * now see if we have a frag_list to map
1817 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001818 skb_walk_frags(skb, frag_iter) {
1819 if (!tlen)
1820 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001821 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001822 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001823 }
1824
1825done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001826 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001827 /*
1828 * Drop the socket lock, otherwise we have reverse
1829 * locking dependencies between sk_lock and i_mutex
1830 * here as compared to sendfile(). We enter here
1831 * with the socket lock held, and splice_to_pipe() will
1832 * grab the pipe inode lock. For sendfile() emulation,
1833 * we call into ->sendpage() with the i_mutex lock held
1834 * and networking will grab the socket lock.
1835 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001836 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001837 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001838 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001839 }
1840
Jens Axboe35f3d142010-05-20 10:43:18 +02001841 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001842}
1843
Herbert Xu357b40a2005-04-19 22:30:14 -07001844/**
1845 * skb_store_bits - store bits from kernel buffer to skb
1846 * @skb: destination buffer
1847 * @offset: offset in destination
1848 * @from: source buffer
1849 * @len: number of bytes to copy
1850 *
1851 * Copy the specified number of bytes from the source buffer to the
1852 * destination skb. This function handles all the messy bits of
1853 * traversing fragment lists and such.
1854 */
1855
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001856int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001857{
David S. Miller1a028e52007-04-27 15:21:23 -07001858 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001859 struct sk_buff *frag_iter;
1860 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001861
1862 if (offset > (int)skb->len - len)
1863 goto fault;
1864
David S. Miller1a028e52007-04-27 15:21:23 -07001865 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001866 if (copy > len)
1867 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001868 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001869 if ((len -= copy) == 0)
1870 return 0;
1871 offset += copy;
1872 from += copy;
1873 }
1874
1875 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1876 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001877 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001878
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001879 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001880
Eric Dumazet9e903e02011-10-18 21:00:24 +00001881 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001882 if ((copy = end - offset) > 0) {
1883 u8 *vaddr;
1884
1885 if (copy > len)
1886 copy = len;
1887
Eric Dumazet51c56b02012-04-05 11:35:15 +02001888 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001889 memcpy(vaddr + frag->page_offset + offset - start,
1890 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001891 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001892
1893 if ((len -= copy) == 0)
1894 return 0;
1895 offset += copy;
1896 from += copy;
1897 }
David S. Miller1a028e52007-04-27 15:21:23 -07001898 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001899 }
1900
David S. Millerfbb398a2009-06-09 00:18:59 -07001901 skb_walk_frags(skb, frag_iter) {
1902 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001903
David S. Millerfbb398a2009-06-09 00:18:59 -07001904 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001905
David S. Millerfbb398a2009-06-09 00:18:59 -07001906 end = start + frag_iter->len;
1907 if ((copy = end - offset) > 0) {
1908 if (copy > len)
1909 copy = len;
1910 if (skb_store_bits(frag_iter, offset - start,
1911 from, copy))
1912 goto fault;
1913 if ((len -= copy) == 0)
1914 return 0;
1915 offset += copy;
1916 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001917 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001918 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001919 }
1920 if (!len)
1921 return 0;
1922
1923fault:
1924 return -EFAULT;
1925}
Herbert Xu357b40a2005-04-19 22:30:14 -07001926EXPORT_SYMBOL(skb_store_bits);
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928/* Checksum skb data. */
1929
Al Viro2bbbc862006-11-14 21:37:14 -08001930__wsum skb_checksum(const struct sk_buff *skb, int offset,
1931 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
David S. Miller1a028e52007-04-27 15:21:23 -07001933 int start = skb_headlen(skb);
1934 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001935 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int pos = 0;
1937
1938 /* Checksum header. */
1939 if (copy > 0) {
1940 if (copy > len)
1941 copy = len;
1942 csum = csum_partial(skb->data + offset, copy, csum);
1943 if ((len -= copy) == 0)
1944 return csum;
1945 offset += copy;
1946 pos = copy;
1947 }
1948
1949 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001950 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001951 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001953 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001954
Eric Dumazet51c56b02012-04-05 11:35:15 +02001955 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001957 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (copy > len)
1961 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001962 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001963 csum2 = csum_partial(vaddr + frag->page_offset +
1964 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001965 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 csum = csum_block_add(csum, csum2, pos);
1967 if (!(len -= copy))
1968 return csum;
1969 offset += copy;
1970 pos += copy;
1971 }
David S. Miller1a028e52007-04-27 15:21:23 -07001972 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974
David S. Millerfbb398a2009-06-09 00:18:59 -07001975 skb_walk_frags(skb, frag_iter) {
1976 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
David S. Millerfbb398a2009-06-09 00:18:59 -07001978 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
David S. Millerfbb398a2009-06-09 00:18:59 -07001980 end = start + frag_iter->len;
1981 if ((copy = end - offset) > 0) {
1982 __wsum csum2;
1983 if (copy > len)
1984 copy = len;
1985 csum2 = skb_checksum(frag_iter, offset - start,
1986 copy, 0);
1987 csum = csum_block_add(csum, csum2, pos);
1988 if ((len -= copy) == 0)
1989 return csum;
1990 offset += copy;
1991 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001993 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001995 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 return csum;
1998}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001999EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001/* Both of above in one bottle. */
2002
Al Viro81d77662006-11-14 21:37:33 -08002003__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2004 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
David S. Miller1a028e52007-04-27 15:21:23 -07002006 int start = skb_headlen(skb);
2007 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002008 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 int pos = 0;
2010
2011 /* Copy header. */
2012 if (copy > 0) {
2013 if (copy > len)
2014 copy = len;
2015 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2016 copy, csum);
2017 if ((len -= copy) == 0)
2018 return csum;
2019 offset += copy;
2020 to += copy;
2021 pos = copy;
2022 }
2023
2024 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002025 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002027 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002028
Eric Dumazet9e903e02011-10-18 21:00:24 +00002029 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002031 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 u8 *vaddr;
2033 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2034
2035 if (copy > len)
2036 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002037 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002039 frag->page_offset +
2040 offset - start, to,
2041 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002042 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 csum = csum_block_add(csum, csum2, pos);
2044 if (!(len -= copy))
2045 return csum;
2046 offset += copy;
2047 to += copy;
2048 pos += copy;
2049 }
David S. Miller1a028e52007-04-27 15:21:23 -07002050 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
David S. Millerfbb398a2009-06-09 00:18:59 -07002053 skb_walk_frags(skb, frag_iter) {
2054 __wsum csum2;
2055 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
David S. Millerfbb398a2009-06-09 00:18:59 -07002057 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
David S. Millerfbb398a2009-06-09 00:18:59 -07002059 end = start + frag_iter->len;
2060 if ((copy = end - offset) > 0) {
2061 if (copy > len)
2062 copy = len;
2063 csum2 = skb_copy_and_csum_bits(frag_iter,
2064 offset - start,
2065 to, copy, 0);
2066 csum = csum_block_add(csum, csum2, pos);
2067 if ((len -= copy) == 0)
2068 return csum;
2069 offset += copy;
2070 to += copy;
2071 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002073 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002075 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return csum;
2077}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002078EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2081{
Al Virod3bc23e2006-11-14 21:24:49 -08002082 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 long csstart;
2084
Patrick McHardy84fa7932006-08-29 16:44:56 -07002085 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002086 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 else
2088 csstart = skb_headlen(skb);
2089
Kris Katterjohn09a62662006-01-08 22:24:28 -08002090 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002092 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 csum = 0;
2095 if (csstart != skb->len)
2096 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2097 skb->len - csstart, 0);
2098
Patrick McHardy84fa7932006-08-29 16:44:56 -07002099 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002100 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Al Virod3bc23e2006-11-14 21:24:49 -08002102 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002105EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107/**
2108 * skb_dequeue - remove from the head of the queue
2109 * @list: list to dequeue from
2110 *
2111 * Remove the head of the list. The list lock is taken so the function
2112 * may be used safely with other locking list functions. The head item is
2113 * returned or %NULL if the list is empty.
2114 */
2115
2116struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2117{
2118 unsigned long flags;
2119 struct sk_buff *result;
2120
2121 spin_lock_irqsave(&list->lock, flags);
2122 result = __skb_dequeue(list);
2123 spin_unlock_irqrestore(&list->lock, flags);
2124 return result;
2125}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002126EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128/**
2129 * skb_dequeue_tail - remove from the tail of the queue
2130 * @list: list to dequeue from
2131 *
2132 * Remove the tail of the list. The list lock is taken so the function
2133 * may be used safely with other locking list functions. The tail item is
2134 * returned or %NULL if the list is empty.
2135 */
2136struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2137{
2138 unsigned long flags;
2139 struct sk_buff *result;
2140
2141 spin_lock_irqsave(&list->lock, flags);
2142 result = __skb_dequeue_tail(list);
2143 spin_unlock_irqrestore(&list->lock, flags);
2144 return result;
2145}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002146EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148/**
2149 * skb_queue_purge - empty a list
2150 * @list: list to empty
2151 *
2152 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2153 * the list and one reference dropped. This function takes the list
2154 * lock and is atomic with respect to other list locking functions.
2155 */
2156void skb_queue_purge(struct sk_buff_head *list)
2157{
2158 struct sk_buff *skb;
2159 while ((skb = skb_dequeue(list)) != NULL)
2160 kfree_skb(skb);
2161}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002162EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164/**
2165 * skb_queue_head - queue a buffer at the list head
2166 * @list: list to use
2167 * @newsk: buffer to queue
2168 *
2169 * Queue a buffer at the start of the list. This function takes the
2170 * list lock and can be used safely with other locking &sk_buff functions
2171 * safely.
2172 *
2173 * A buffer cannot be placed on two lists at the same time.
2174 */
2175void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2176{
2177 unsigned long flags;
2178
2179 spin_lock_irqsave(&list->lock, flags);
2180 __skb_queue_head(list, newsk);
2181 spin_unlock_irqrestore(&list->lock, flags);
2182}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002183EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185/**
2186 * skb_queue_tail - queue a buffer at the list tail
2187 * @list: list to use
2188 * @newsk: buffer to queue
2189 *
2190 * Queue a buffer at the tail of the list. This function takes the
2191 * list lock and can be used safely with other locking &sk_buff functions
2192 * safely.
2193 *
2194 * A buffer cannot be placed on two lists at the same time.
2195 */
2196void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2197{
2198 unsigned long flags;
2199
2200 spin_lock_irqsave(&list->lock, flags);
2201 __skb_queue_tail(list, newsk);
2202 spin_unlock_irqrestore(&list->lock, flags);
2203}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002204EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206/**
2207 * skb_unlink - remove a buffer from a list
2208 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002209 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 *
David S. Miller8728b832005-08-09 19:25:21 -07002211 * Remove a packet from a list. The list locks are taken and this
2212 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 *
David S. Miller8728b832005-08-09 19:25:21 -07002214 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
David S. Miller8728b832005-08-09 19:25:21 -07002216void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
David S. Miller8728b832005-08-09 19:25:21 -07002218 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
David S. Miller8728b832005-08-09 19:25:21 -07002220 spin_lock_irqsave(&list->lock, flags);
2221 __skb_unlink(skb, list);
2222 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002224EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226/**
2227 * skb_append - append a buffer
2228 * @old: buffer to insert after
2229 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002230 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 *
2232 * Place a packet after a given packet in a list. The list locks are taken
2233 * and this function is atomic with respect to other list locked calls.
2234 * A buffer cannot be placed on two lists at the same time.
2235 */
David S. Miller8728b832005-08-09 19:25:21 -07002236void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 unsigned long flags;
2239
David S. Miller8728b832005-08-09 19:25:21 -07002240 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002241 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002242 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002244EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246/**
2247 * skb_insert - insert a buffer
2248 * @old: buffer to insert before
2249 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002250 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 *
David S. Miller8728b832005-08-09 19:25:21 -07002252 * Place a packet before a given packet in a list. The list locks are
2253 * taken and this function is atomic with respect to other list locked
2254 * calls.
2255 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 * A buffer cannot be placed on two lists at the same time.
2257 */
David S. Miller8728b832005-08-09 19:25:21 -07002258void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 unsigned long flags;
2261
David S. Miller8728b832005-08-09 19:25:21 -07002262 spin_lock_irqsave(&list->lock, flags);
2263 __skb_insert(newsk, old->prev, old, list);
2264 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002266EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268static inline void skb_split_inside_header(struct sk_buff *skb,
2269 struct sk_buff* skb1,
2270 const u32 len, const int pos)
2271{
2272 int i;
2273
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002274 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2275 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 /* And move data appendix as is. */
2277 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2278 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2279
2280 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2281 skb_shinfo(skb)->nr_frags = 0;
2282 skb1->data_len = skb->data_len;
2283 skb1->len += skb1->data_len;
2284 skb->data_len = 0;
2285 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002286 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
2289static inline void skb_split_no_header(struct sk_buff *skb,
2290 struct sk_buff* skb1,
2291 const u32 len, int pos)
2292{
2293 int i, k = 0;
2294 const int nfrags = skb_shinfo(skb)->nr_frags;
2295
2296 skb_shinfo(skb)->nr_frags = 0;
2297 skb1->len = skb1->data_len = skb->len - len;
2298 skb->len = len;
2299 skb->data_len = len - pos;
2300
2301 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002302 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 if (pos + size > len) {
2305 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2306
2307 if (pos < len) {
2308 /* Split frag.
2309 * We have two variants in this case:
2310 * 1. Move all the frag to the second
2311 * part, if it is possible. F.e.
2312 * this approach is mandatory for TUX,
2313 * where splitting is expensive.
2314 * 2. Split is accurately. We make this.
2315 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002316 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002318 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2319 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 skb_shinfo(skb)->nr_frags++;
2321 }
2322 k++;
2323 } else
2324 skb_shinfo(skb)->nr_frags++;
2325 pos += size;
2326 }
2327 skb_shinfo(skb1)->nr_frags = k;
2328}
2329
2330/**
2331 * skb_split - Split fragmented skb to two parts at length len.
2332 * @skb: the buffer to split
2333 * @skb1: the buffer to receive the second part
2334 * @len: new length for skb
2335 */
2336void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2337{
2338 int pos = skb_headlen(skb);
2339
2340 if (len < pos) /* Split line is inside header. */
2341 skb_split_inside_header(skb, skb1, len, pos);
2342 else /* Second chunk has no header, nothing to copy. */
2343 skb_split_no_header(skb, skb1, len, pos);
2344}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002345EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002347/* Shifting from/to a cloned skb is a no-go.
2348 *
2349 * Caller cannot keep skb_shinfo related pointers past calling here!
2350 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002351static int skb_prepare_for_shift(struct sk_buff *skb)
2352{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002353 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002354}
2355
2356/**
2357 * skb_shift - Shifts paged data partially from skb to another
2358 * @tgt: buffer into which tail data gets added
2359 * @skb: buffer from which the paged data comes from
2360 * @shiftlen: shift up to this many bytes
2361 *
2362 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002363 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002364 * It's up to caller to free skb if everything was shifted.
2365 *
2366 * If @tgt runs out of frags, the whole operation is aborted.
2367 *
2368 * Skb cannot include anything else but paged data while tgt is allowed
2369 * to have non-paged data as well.
2370 *
2371 * TODO: full sized shift could be optimized but that would need
2372 * specialized skb free'er to handle frags without up-to-date nr_frags.
2373 */
2374int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2375{
2376 int from, to, merge, todo;
2377 struct skb_frag_struct *fragfrom, *fragto;
2378
2379 BUG_ON(shiftlen > skb->len);
2380 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2381
2382 todo = shiftlen;
2383 from = 0;
2384 to = skb_shinfo(tgt)->nr_frags;
2385 fragfrom = &skb_shinfo(skb)->frags[from];
2386
2387 /* Actual merge is delayed until the point when we know we can
2388 * commit all, so that we don't have to undo partial changes
2389 */
2390 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002391 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2392 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002393 merge = -1;
2394 } else {
2395 merge = to - 1;
2396
Eric Dumazet9e903e02011-10-18 21:00:24 +00002397 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002398 if (todo < 0) {
2399 if (skb_prepare_for_shift(skb) ||
2400 skb_prepare_for_shift(tgt))
2401 return 0;
2402
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002403 /* All previous frag pointers might be stale! */
2404 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002405 fragto = &skb_shinfo(tgt)->frags[merge];
2406
Eric Dumazet9e903e02011-10-18 21:00:24 +00002407 skb_frag_size_add(fragto, shiftlen);
2408 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002409 fragfrom->page_offset += shiftlen;
2410
2411 goto onlymerged;
2412 }
2413
2414 from++;
2415 }
2416
2417 /* Skip full, not-fitting skb to avoid expensive operations */
2418 if ((shiftlen == skb->len) &&
2419 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2420 return 0;
2421
2422 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2423 return 0;
2424
2425 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2426 if (to == MAX_SKB_FRAGS)
2427 return 0;
2428
2429 fragfrom = &skb_shinfo(skb)->frags[from];
2430 fragto = &skb_shinfo(tgt)->frags[to];
2431
Eric Dumazet9e903e02011-10-18 21:00:24 +00002432 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002433 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002434 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002435 from++;
2436 to++;
2437
2438 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002439 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002440 fragto->page = fragfrom->page;
2441 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002442 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002443
2444 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002445 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002446 todo = 0;
2447
2448 to++;
2449 break;
2450 }
2451 }
2452
2453 /* Ready to "commit" this state change to tgt */
2454 skb_shinfo(tgt)->nr_frags = to;
2455
2456 if (merge >= 0) {
2457 fragfrom = &skb_shinfo(skb)->frags[0];
2458 fragto = &skb_shinfo(tgt)->frags[merge];
2459
Eric Dumazet9e903e02011-10-18 21:00:24 +00002460 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002461 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002462 }
2463
2464 /* Reposition in the original skb */
2465 to = 0;
2466 while (from < skb_shinfo(skb)->nr_frags)
2467 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2468 skb_shinfo(skb)->nr_frags = to;
2469
2470 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2471
2472onlymerged:
2473 /* Most likely the tgt won't ever need its checksum anymore, skb on
2474 * the other hand might need it if it needs to be resent
2475 */
2476 tgt->ip_summed = CHECKSUM_PARTIAL;
2477 skb->ip_summed = CHECKSUM_PARTIAL;
2478
2479 /* Yak, is it really working this way? Some helper please? */
2480 skb->len -= shiftlen;
2481 skb->data_len -= shiftlen;
2482 skb->truesize -= shiftlen;
2483 tgt->len += shiftlen;
2484 tgt->data_len += shiftlen;
2485 tgt->truesize += shiftlen;
2486
2487 return shiftlen;
2488}
2489
Thomas Graf677e90e2005-06-23 20:59:51 -07002490/**
2491 * skb_prepare_seq_read - Prepare a sequential read of skb data
2492 * @skb: the buffer to read
2493 * @from: lower offset of data to be read
2494 * @to: upper offset of data to be read
2495 * @st: state variable
2496 *
2497 * Initializes the specified state variable. Must be called before
2498 * invoking skb_seq_read() for the first time.
2499 */
2500void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2501 unsigned int to, struct skb_seq_state *st)
2502{
2503 st->lower_offset = from;
2504 st->upper_offset = to;
2505 st->root_skb = st->cur_skb = skb;
2506 st->frag_idx = st->stepped_offset = 0;
2507 st->frag_data = NULL;
2508}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002509EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002510
2511/**
2512 * skb_seq_read - Sequentially read skb data
2513 * @consumed: number of bytes consumed by the caller so far
2514 * @data: destination pointer for data to be returned
2515 * @st: state variable
2516 *
2517 * Reads a block of skb data at &consumed relative to the
2518 * lower offset specified to skb_prepare_seq_read(). Assigns
2519 * the head of the data block to &data and returns the length
2520 * of the block or 0 if the end of the skb data or the upper
2521 * offset has been reached.
2522 *
2523 * The caller is not required to consume all of the data
2524 * returned, i.e. &consumed is typically set to the number
2525 * of bytes already consumed and the next call to
2526 * skb_seq_read() will return the remaining part of the block.
2527 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002528 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002529 * this limitation is the cost for zerocopy seqeuental
2530 * reads of potentially non linear data.
2531 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002532 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002533 * at the moment, state->root_skb could be replaced with
2534 * a stack for this purpose.
2535 */
2536unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2537 struct skb_seq_state *st)
2538{
2539 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2540 skb_frag_t *frag;
2541
2542 if (unlikely(abs_offset >= st->upper_offset))
2543 return 0;
2544
2545next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002546 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002547
Thomas Chenault995b3372009-05-18 21:43:27 -07002548 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002549 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002550 return block_limit - abs_offset;
2551 }
2552
2553 if (st->frag_idx == 0 && !st->frag_data)
2554 st->stepped_offset += skb_headlen(st->cur_skb);
2555
2556 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2557 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002558 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002559
2560 if (abs_offset < block_limit) {
2561 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002562 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002563
2564 *data = (u8 *) st->frag_data + frag->page_offset +
2565 (abs_offset - st->stepped_offset);
2566
2567 return block_limit - abs_offset;
2568 }
2569
2570 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002571 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002572 st->frag_data = NULL;
2573 }
2574
2575 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002576 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002577 }
2578
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002579 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002580 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002581 st->frag_data = NULL;
2582 }
2583
David S. Miller21dc3302010-08-23 00:13:46 -07002584 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002585 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002586 st->frag_idx = 0;
2587 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002588 } else if (st->cur_skb->next) {
2589 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002590 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002591 goto next_skb;
2592 }
2593
2594 return 0;
2595}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002596EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002597
2598/**
2599 * skb_abort_seq_read - Abort a sequential read of skb data
2600 * @st: state variable
2601 *
2602 * Must be called if skb_seq_read() was not called until it
2603 * returned 0.
2604 */
2605void skb_abort_seq_read(struct skb_seq_state *st)
2606{
2607 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002608 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002609}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002610EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002611
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002612#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2613
2614static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2615 struct ts_config *conf,
2616 struct ts_state *state)
2617{
2618 return skb_seq_read(offset, text, TS_SKB_CB(state));
2619}
2620
2621static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2622{
2623 skb_abort_seq_read(TS_SKB_CB(state));
2624}
2625
2626/**
2627 * skb_find_text - Find a text pattern in skb data
2628 * @skb: the buffer to look in
2629 * @from: search offset
2630 * @to: search limit
2631 * @config: textsearch configuration
2632 * @state: uninitialized textsearch state variable
2633 *
2634 * Finds a pattern in the skb data according to the specified
2635 * textsearch configuration. Use textsearch_next() to retrieve
2636 * subsequent occurrences of the pattern. Returns the offset
2637 * to the first occurrence or UINT_MAX if no match was found.
2638 */
2639unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2640 unsigned int to, struct ts_config *config,
2641 struct ts_state *state)
2642{
Phil Oesterf72b9482006-06-26 00:00:57 -07002643 unsigned int ret;
2644
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002645 config->get_next_block = skb_ts_get_next_block;
2646 config->finish = skb_ts_finish;
2647
2648 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2649
Phil Oesterf72b9482006-06-26 00:00:57 -07002650 ret = textsearch_find(config, state);
2651 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002652}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002653EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002654
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002655/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002656 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002657 * @sk: sock structure
2658 * @skb: skb structure to be appened with user data.
2659 * @getfrag: call back function to be used for getting the user data
2660 * @from: pointer to user message iov
2661 * @length: length of the iov message
2662 *
2663 * Description: This procedure append the user data in the fragment part
2664 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2665 */
2666int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002667 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002668 int len, int odd, struct sk_buff *skb),
2669 void *from, int length)
2670{
2671 int frg_cnt = 0;
2672 skb_frag_t *frag = NULL;
2673 struct page *page = NULL;
2674 int copy, left;
2675 int offset = 0;
2676 int ret;
2677
2678 do {
2679 /* Return error if we don't have space for new frag */
2680 frg_cnt = skb_shinfo(skb)->nr_frags;
2681 if (frg_cnt >= MAX_SKB_FRAGS)
2682 return -EFAULT;
2683
2684 /* allocate a new page for next frag */
2685 page = alloc_pages(sk->sk_allocation, 0);
2686
2687 /* If alloc_page fails just return failure and caller will
2688 * free previous allocated pages by doing kfree_skb()
2689 */
2690 if (page == NULL)
2691 return -ENOMEM;
2692
2693 /* initialize the next frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002694 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2695 skb->truesize += PAGE_SIZE;
2696 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2697
2698 /* get the new initialized frag */
2699 frg_cnt = skb_shinfo(skb)->nr_frags;
2700 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2701
2702 /* copy the user data to page */
2703 left = PAGE_SIZE - frag->page_offset;
2704 copy = (length > left)? left : length;
2705
Eric Dumazet9e903e02011-10-18 21:00:24 +00002706 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002707 offset, copy, 0, skb);
2708 if (ret < 0)
2709 return -EFAULT;
2710
2711 /* copy was successful so update the size parameters */
Eric Dumazet9e903e02011-10-18 21:00:24 +00002712 skb_frag_size_add(frag, copy);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002713 skb->len += copy;
2714 skb->data_len += copy;
2715 offset += copy;
2716 length -= copy;
2717
2718 } while (length > 0);
2719
2720 return 0;
2721}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002722EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002723
Herbert Xucbb042f2006-03-20 22:43:56 -08002724/**
2725 * skb_pull_rcsum - pull skb and update receive checksum
2726 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002727 * @len: length of data pulled
2728 *
2729 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002730 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002731 * receive path processing instead of skb_pull unless you know
2732 * that the checksum difference is zero (e.g., a valid IP header)
2733 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002734 */
2735unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2736{
2737 BUG_ON(len > skb->len);
2738 skb->len -= len;
2739 BUG_ON(skb->len < skb->data_len);
2740 skb_postpull_rcsum(skb, skb->data, len);
2741 return skb->data += len;
2742}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002743EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2744
Herbert Xuf4c50d92006-06-22 03:02:40 -07002745/**
2746 * skb_segment - Perform protocol segmentation on skb.
2747 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002748 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002749 *
2750 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002751 * a pointer to the first in a list of new skbs for the segments.
2752 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002753 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002754struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002755{
2756 struct sk_buff *segs = NULL;
2757 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002758 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002759 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002760 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002761 unsigned int offset = doffset;
2762 unsigned int headroom;
2763 unsigned int len;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002764 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002765 int nfrags = skb_shinfo(skb)->nr_frags;
2766 int err = -ENOMEM;
2767 int i = 0;
2768 int pos;
2769
2770 __skb_push(skb, doffset);
2771 headroom = skb_headroom(skb);
2772 pos = skb_headlen(skb);
2773
2774 do {
2775 struct sk_buff *nskb;
2776 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002777 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002778 int size;
2779
2780 len = skb->len - offset;
2781 if (len > mss)
2782 len = mss;
2783
2784 hsize = skb_headlen(skb) - offset;
2785 if (hsize < 0)
2786 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002787 if (hsize > len || !sg)
2788 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002789
Herbert Xu89319d382008-12-15 23:26:06 -08002790 if (!hsize && i >= nfrags) {
2791 BUG_ON(fskb->len != len);
2792
2793 pos += len;
2794 nskb = skb_clone(fskb, GFP_ATOMIC);
2795 fskb = fskb->next;
2796
2797 if (unlikely(!nskb))
2798 goto err;
2799
Alexander Duyckec47ea82012-05-04 14:26:56 +00002800 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002801 if (skb_cow_head(nskb, doffset + headroom)) {
2802 kfree_skb(nskb);
2803 goto err;
2804 }
2805
Alexander Duyckec47ea82012-05-04 14:26:56 +00002806 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002807 skb_release_head_state(nskb);
2808 __skb_push(nskb, doffset);
2809 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002810 nskb = __alloc_skb(hsize + doffset + headroom,
2811 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2812 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002813
2814 if (unlikely(!nskb))
2815 goto err;
2816
2817 skb_reserve(nskb, headroom);
2818 __skb_put(nskb, doffset);
2819 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002820
2821 if (segs)
2822 tail->next = nskb;
2823 else
2824 segs = nskb;
2825 tail = nskb;
2826
Herbert Xu6f85a122008-08-15 14:55:02 -07002827 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002828 nskb->mac_len = skb->mac_len;
2829
Eric Dumazet3d3be432010-09-01 00:50:51 +00002830 /* nskb and skb might have different headroom */
2831 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2832 nskb->csum_start += skb_headroom(nskb) - headroom;
2833
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002834 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002835 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002836 nskb->transport_header = (nskb->network_header +
2837 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002838 skb_copy_from_linear_data(skb, nskb->data, doffset);
2839
Herbert Xu2f181852009-03-28 23:39:18 -07002840 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002841 continue;
2842
Herbert Xuf4c50d92006-06-22 03:02:40 -07002843 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002844 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002845 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2846 skb_put(nskb, len),
2847 len, 0);
2848 continue;
2849 }
2850
2851 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002852
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002853 skb_copy_from_linear_data_offset(skb, offset,
2854 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002855
Herbert Xu89319d382008-12-15 23:26:06 -08002856 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002857 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002858 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002859 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002860
2861 if (pos < offset) {
2862 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002863 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002864 }
2865
Herbert Xu89319d382008-12-15 23:26:06 -08002866 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002867
2868 if (pos + size <= offset + len) {
2869 i++;
2870 pos += size;
2871 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002872 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002873 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002874 }
2875
2876 frag++;
2877 }
2878
Herbert Xu89319d382008-12-15 23:26:06 -08002879 if (pos < offset + len) {
2880 struct sk_buff *fskb2 = fskb;
2881
2882 BUG_ON(pos + fskb->len != offset + len);
2883
2884 pos += fskb->len;
2885 fskb = fskb->next;
2886
2887 if (fskb2->next) {
2888 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2889 if (!fskb2)
2890 goto err;
2891 } else
2892 skb_get(fskb2);
2893
David S. Millerfbb398a2009-06-09 00:18:59 -07002894 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002895 skb_shinfo(nskb)->frag_list = fskb2;
2896 }
2897
2898skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002899 nskb->data_len = len - hsize;
2900 nskb->len += nskb->data_len;
2901 nskb->truesize += nskb->data_len;
2902 } while ((offset += len) < skb->len);
2903
2904 return segs;
2905
2906err:
2907 while ((skb = segs)) {
2908 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002909 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002910 }
2911 return ERR_PTR(err);
2912}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002913EXPORT_SYMBOL_GPL(skb_segment);
2914
Herbert Xu71d93b32008-12-15 23:42:33 -08002915int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2916{
2917 struct sk_buff *p = *head;
2918 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002919 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2920 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002921 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002922 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002923 unsigned int offset = skb_gro_offset(skb);
2924 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002925 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002926
Herbert Xu86911732009-01-29 14:19:50 +00002927 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002928 return -E2BIG;
2929
Herbert Xu9aaa1562009-05-26 18:50:33 +00002930 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002931 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002932 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002933 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002934 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002935 int i = skbinfo->nr_frags;
2936 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002937
Herbert Xu66e92fc2009-05-26 18:50:32 +00002938 offset -= headlen;
2939
2940 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002941 return -E2BIG;
2942
Herbert Xu9aaa1562009-05-26 18:50:33 +00002943 pinfo->nr_frags = nr_frags;
2944 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002945
Herbert Xu9aaa1562009-05-26 18:50:33 +00002946 frag = pinfo->frags + nr_frags;
2947 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002948 do {
2949 *--frag = *--frag2;
2950 } while (--i);
2951
2952 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002953 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002954
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002955 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002956 delta_truesize = skb->truesize -
2957 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002958
Herbert Xuf5572062009-01-14 20:40:03 -08002959 skb->truesize -= skb->data_len;
2960 skb->len -= skb->data_len;
2961 skb->data_len = 0;
2962
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002963 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08002964 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00002965 } else if (skb->head_frag) {
2966 int nr_frags = pinfo->nr_frags;
2967 skb_frag_t *frag = pinfo->frags + nr_frags;
2968 struct page *page = virt_to_head_page(skb->head);
2969 unsigned int first_size = headlen - offset;
2970 unsigned int first_offset;
2971
2972 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2973 return -E2BIG;
2974
2975 first_offset = skb->data -
2976 (unsigned char *)page_address(page) +
2977 offset;
2978
2979 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2980
2981 frag->page.p = page;
2982 frag->page_offset = first_offset;
2983 skb_frag_size_set(frag, first_size);
2984
2985 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2986 /* We dont need to clear skbinfo->nr_frags here */
2987
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002988 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00002989 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2990 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08002991 } else if (skb_gro_len(p) != pinfo->gso_size)
2992 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08002993
2994 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00002995 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08002996 if (unlikely(!nskb))
2997 return -ENOMEM;
2998
2999 __copy_skb_header(nskb, p);
3000 nskb->mac_len = p->mac_len;
3001
3002 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003003 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003004
Herbert Xu86911732009-01-29 14:19:50 +00003005 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003006 skb_set_network_header(nskb, skb_network_offset(p));
3007 skb_set_transport_header(nskb, skb_transport_offset(p));
3008
Herbert Xu86911732009-01-29 14:19:50 +00003009 __skb_pull(p, skb_gro_offset(p));
3010 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3011 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003012
Herbert Xu71d93b32008-12-15 23:42:33 -08003013 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003014 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003015 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003016 skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003017 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003018
3019 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003020 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003021 nskb->len += p->len;
3022
3023 *head = nskb;
3024 nskb->next = p->next;
3025 p->next = NULL;
3026
3027 p = nskb;
3028
3029merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003030 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003031 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003032 unsigned int eat = offset - headlen;
3033
3034 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003035 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003036 skb->data_len -= eat;
3037 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003038 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003039 }
3040
Herbert Xu67147ba2009-05-26 18:50:22 +00003041 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003042
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003043 NAPI_GRO_CB(p)->last->next = skb;
3044 NAPI_GRO_CB(p)->last = skb;
Herbert Xu71d93b32008-12-15 23:42:33 -08003045 skb_header_release(skb);
3046
Herbert Xu5d38a072009-01-04 16:13:40 -08003047done:
3048 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003049 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003050 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003051 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003052
3053 NAPI_GRO_CB(skb)->same_flow = 1;
3054 return 0;
3055}
3056EXPORT_SYMBOL_GPL(skb_gro_receive);
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058void __init skb_init(void)
3059{
3060 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3061 sizeof(struct sk_buff),
3062 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003063 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003064 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003065 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3066 (2*sizeof(struct sk_buff)) +
3067 sizeof(atomic_t),
3068 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003069 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003070 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
David Howells716ea3a2007-04-02 20:19:53 -07003073/**
3074 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3075 * @skb: Socket buffer containing the buffers to be mapped
3076 * @sg: The scatter-gather list to map into
3077 * @offset: The offset into the buffer's contents to start mapping
3078 * @len: Length of buffer space to be mapped
3079 *
3080 * Fill the specified scatter-gather list with mappings/pointers into a
3081 * region of the buffer space attached to a socket buffer.
3082 */
David S. Miller51c739d2007-10-30 21:29:29 -07003083static int
3084__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003085{
David S. Miller1a028e52007-04-27 15:21:23 -07003086 int start = skb_headlen(skb);
3087 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003088 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003089 int elt = 0;
3090
3091 if (copy > 0) {
3092 if (copy > len)
3093 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003094 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003095 elt++;
3096 if ((len -= copy) == 0)
3097 return elt;
3098 offset += copy;
3099 }
3100
3101 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003102 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003103
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003104 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003105
Eric Dumazet9e903e02011-10-18 21:00:24 +00003106 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003107 if ((copy = end - offset) > 0) {
3108 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3109
3110 if (copy > len)
3111 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003112 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003113 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003114 elt++;
3115 if (!(len -= copy))
3116 return elt;
3117 offset += copy;
3118 }
David S. Miller1a028e52007-04-27 15:21:23 -07003119 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003120 }
3121
David S. Millerfbb398a2009-06-09 00:18:59 -07003122 skb_walk_frags(skb, frag_iter) {
3123 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003124
David S. Millerfbb398a2009-06-09 00:18:59 -07003125 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003126
David S. Millerfbb398a2009-06-09 00:18:59 -07003127 end = start + frag_iter->len;
3128 if ((copy = end - offset) > 0) {
3129 if (copy > len)
3130 copy = len;
3131 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3132 copy);
3133 if ((len -= copy) == 0)
3134 return elt;
3135 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003136 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003137 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003138 }
3139 BUG_ON(len);
3140 return elt;
3141}
3142
David S. Miller51c739d2007-10-30 21:29:29 -07003143int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3144{
3145 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3146
Jens Axboec46f2332007-10-31 12:06:37 +01003147 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003148
3149 return nsg;
3150}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003151EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003152
David Howells716ea3a2007-04-02 20:19:53 -07003153/**
3154 * skb_cow_data - Check that a socket buffer's data buffers are writable
3155 * @skb: The socket buffer to check.
3156 * @tailbits: Amount of trailing space to be added
3157 * @trailer: Returned pointer to the skb where the @tailbits space begins
3158 *
3159 * Make sure that the data buffers attached to a socket buffer are
3160 * writable. If they are not, private copies are made of the data buffers
3161 * and the socket buffer is set to use these instead.
3162 *
3163 * If @tailbits is given, make sure that there is space to write @tailbits
3164 * bytes of data beyond current end of socket buffer. @trailer will be
3165 * set to point to the skb in which this space begins.
3166 *
3167 * The number of scatterlist elements required to completely map the
3168 * COW'd and extended socket buffer will be returned.
3169 */
3170int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3171{
3172 int copyflag;
3173 int elt;
3174 struct sk_buff *skb1, **skb_p;
3175
3176 /* If skb is cloned or its head is paged, reallocate
3177 * head pulling out all the pages (pages are considered not writable
3178 * at the moment even if they are anonymous).
3179 */
3180 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3181 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3182 return -ENOMEM;
3183
3184 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003185 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003186 /* A little of trouble, not enough of space for trailer.
3187 * This should not happen, when stack is tuned to generate
3188 * good frames. OK, on miss we reallocate and reserve even more
3189 * space, 128 bytes is fair. */
3190
3191 if (skb_tailroom(skb) < tailbits &&
3192 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3193 return -ENOMEM;
3194
3195 /* Voila! */
3196 *trailer = skb;
3197 return 1;
3198 }
3199
3200 /* Misery. We are in troubles, going to mincer fragments... */
3201
3202 elt = 1;
3203 skb_p = &skb_shinfo(skb)->frag_list;
3204 copyflag = 0;
3205
3206 while ((skb1 = *skb_p) != NULL) {
3207 int ntail = 0;
3208
3209 /* The fragment is partially pulled by someone,
3210 * this can happen on input. Copy it and everything
3211 * after it. */
3212
3213 if (skb_shared(skb1))
3214 copyflag = 1;
3215
3216 /* If the skb is the last, worry about trailer. */
3217
3218 if (skb1->next == NULL && tailbits) {
3219 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003220 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003221 skb_tailroom(skb1) < tailbits)
3222 ntail = tailbits + 128;
3223 }
3224
3225 if (copyflag ||
3226 skb_cloned(skb1) ||
3227 ntail ||
3228 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003229 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003230 struct sk_buff *skb2;
3231
3232 /* Fuck, we are miserable poor guys... */
3233 if (ntail == 0)
3234 skb2 = skb_copy(skb1, GFP_ATOMIC);
3235 else
3236 skb2 = skb_copy_expand(skb1,
3237 skb_headroom(skb1),
3238 ntail,
3239 GFP_ATOMIC);
3240 if (unlikely(skb2 == NULL))
3241 return -ENOMEM;
3242
3243 if (skb1->sk)
3244 skb_set_owner_w(skb2, skb1->sk);
3245
3246 /* Looking around. Are we still alive?
3247 * OK, link new skb, drop old one */
3248
3249 skb2->next = skb1->next;
3250 *skb_p = skb2;
3251 kfree_skb(skb1);
3252 skb1 = skb2;
3253 }
3254 elt++;
3255 *trailer = skb1;
3256 skb_p = &skb1->next;
3257 }
3258
3259 return elt;
3260}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003261EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003262
Eric Dumazetb1faf562010-05-31 23:44:05 -07003263static void sock_rmem_free(struct sk_buff *skb)
3264{
3265 struct sock *sk = skb->sk;
3266
3267 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3268}
3269
3270/*
3271 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3272 */
3273int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3274{
Eric Dumazet110c4332012-04-06 10:49:10 +02003275 int len = skb->len;
3276
Eric Dumazetb1faf562010-05-31 23:44:05 -07003277 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003278 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003279 return -ENOMEM;
3280
3281 skb_orphan(skb);
3282 skb->sk = sk;
3283 skb->destructor = sock_rmem_free;
3284 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3285
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003286 /* before exiting rcu section, make sure dst is refcounted */
3287 skb_dst_force(skb);
3288
Eric Dumazetb1faf562010-05-31 23:44:05 -07003289 skb_queue_tail(&sk->sk_error_queue, skb);
3290 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003291 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003292 return 0;
3293}
3294EXPORT_SYMBOL(sock_queue_err_skb);
3295
Patrick Ohlyac45f602009-02-12 05:03:37 +00003296void skb_tstamp_tx(struct sk_buff *orig_skb,
3297 struct skb_shared_hwtstamps *hwtstamps)
3298{
3299 struct sock *sk = orig_skb->sk;
3300 struct sock_exterr_skb *serr;
3301 struct sk_buff *skb;
3302 int err;
3303
3304 if (!sk)
3305 return;
3306
3307 skb = skb_clone(orig_skb, GFP_ATOMIC);
3308 if (!skb)
3309 return;
3310
3311 if (hwtstamps) {
3312 *skb_hwtstamps(skb) =
3313 *hwtstamps;
3314 } else {
3315 /*
3316 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003317 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003318 * store software time stamp
3319 */
3320 skb->tstamp = ktime_get_real();
3321 }
3322
3323 serr = SKB_EXT_ERR(skb);
3324 memset(serr, 0, sizeof(*serr));
3325 serr->ee.ee_errno = ENOMSG;
3326 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003327
Patrick Ohlyac45f602009-02-12 05:03:37 +00003328 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003329
Patrick Ohlyac45f602009-02-12 05:03:37 +00003330 if (err)
3331 kfree_skb(skb);
3332}
3333EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3334
Johannes Berg6e3e9392011-11-09 10:15:42 +01003335void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3336{
3337 struct sock *sk = skb->sk;
3338 struct sock_exterr_skb *serr;
3339 int err;
3340
3341 skb->wifi_acked_valid = 1;
3342 skb->wifi_acked = acked;
3343
3344 serr = SKB_EXT_ERR(skb);
3345 memset(serr, 0, sizeof(*serr));
3346 serr->ee.ee_errno = ENOMSG;
3347 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3348
3349 err = sock_queue_err_skb(sk, skb);
3350 if (err)
3351 kfree_skb(skb);
3352}
3353EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3354
Patrick Ohlyac45f602009-02-12 05:03:37 +00003355
Rusty Russellf35d9d82008-02-04 23:49:54 -05003356/**
3357 * skb_partial_csum_set - set up and verify partial csum values for packet
3358 * @skb: the skb to set
3359 * @start: the number of bytes after skb->data to start checksumming.
3360 * @off: the offset from start to place the checksum.
3361 *
3362 * For untrusted partially-checksummed packets, we need to make sure the values
3363 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3364 *
3365 * This function checks and sets those values and skb->ip_summed: if this
3366 * returns false you should drop the packet.
3367 */
3368bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3369{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003370 if (unlikely(start > skb_headlen(skb)) ||
3371 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003372 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3373 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003374 return false;
3375 }
3376 skb->ip_summed = CHECKSUM_PARTIAL;
3377 skb->csum_start = skb_headroom(skb) + start;
3378 skb->csum_offset = off;
3379 return true;
3380}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003381EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003382
Ben Hutchings4497b072008-06-19 16:22:28 -07003383void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3384{
Joe Perchese87cc472012-05-13 21:56:26 +00003385 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3386 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003387}
Ben Hutchings4497b072008-06-19 16:22:28 -07003388EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003389
3390void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3391{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003392 if (head_stolen) {
3393 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003394 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003395 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003396 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003397 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003398}
3399EXPORT_SYMBOL(kfree_skb_partial);
3400
3401/**
3402 * skb_try_coalesce - try to merge skb to prior one
3403 * @to: prior buffer
3404 * @from: buffer to add
3405 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003406 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003407 */
3408bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3409 bool *fragstolen, int *delta_truesize)
3410{
3411 int i, delta, len = from->len;
3412
3413 *fragstolen = false;
3414
3415 if (skb_cloned(to))
3416 return false;
3417
3418 if (len <= skb_tailroom(to)) {
3419 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3420 *delta_truesize = 0;
3421 return true;
3422 }
3423
3424 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3425 return false;
3426
3427 if (skb_headlen(from) != 0) {
3428 struct page *page;
3429 unsigned int offset;
3430
3431 if (skb_shinfo(to)->nr_frags +
3432 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3433 return false;
3434
3435 if (skb_head_is_locked(from))
3436 return false;
3437
3438 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3439
3440 page = virt_to_head_page(from->head);
3441 offset = from->data - (unsigned char *)page_address(page);
3442
3443 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3444 page, offset, skb_headlen(from));
3445 *fragstolen = true;
3446 } else {
3447 if (skb_shinfo(to)->nr_frags +
3448 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3449 return false;
3450
Weiping Panf4b549a2012-09-28 20:15:30 +00003451 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003452 }
3453
3454 WARN_ON_ONCE(delta < len);
3455
3456 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3457 skb_shinfo(from)->frags,
3458 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3459 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3460
3461 if (!skb_cloned(from))
3462 skb_shinfo(from)->nr_frags = 0;
3463
Li RongQing8ea853f2012-09-18 16:53:21 +00003464 /* if the skb is not cloned this does nothing
3465 * since we set nr_frags to 0.
3466 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003467 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3468 skb_frag_ref(from, i);
3469
3470 to->truesize += delta;
3471 to->len += len;
3472 to->data_len += len;
3473
3474 *delta_truesize = delta;
3475 return true;
3476}
3477EXPORT_SYMBOL(skb_try_coalesce);