blob: 880722e22cc576be1e9147898dee651bb6b45b6d [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;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000685 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000686 new->rxhash = old->rxhash;
Changli Gao6461be32011-08-19 04:44:18 +0000687 new->ooo_okay = old->ooo_okay;
Tom Herbertbdeab992011-08-14 19:45:55 +0000688 new->l4_rxhash = old->l4_rxhash;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000689 new->no_fcs = old->no_fcs;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700690#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700691 new->sp = secpath_get(old->sp);
692#endif
693 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000694 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700695 new->local_df = old->local_df;
696 new->pkt_type = old->pkt_type;
697 new->ip_summed = old->ip_summed;
698 skb_copy_queue_mapping(new, old);
699 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000700#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700701 new->ipvs_property = old->ipvs_property;
702#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700703 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700704 new->protocol = old->protocol;
705 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800706 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700707 __nf_copy(new, old);
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000708#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Herbert Xudec18812007-10-14 00:37:30 -0700709 new->nf_trace = old->nf_trace;
710#endif
711#ifdef CONFIG_NET_SCHED
712 new->tc_index = old->tc_index;
713#ifdef CONFIG_NET_CLS_ACT
714 new->tc_verd = old->tc_verd;
715#endif
716#endif
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700717 new->vlan_tci = old->vlan_tci;
718
Herbert Xudec18812007-10-14 00:37:30 -0700719 skb_copy_secmark(new, old);
720}
721
Herbert Xu82c49a32009-05-22 22:11:37 +0000722/*
723 * You should not add any new code to this function. Add it to
724 * __copy_skb_header above instead.
725 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700726static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#define C(x) n->x = skb->x
729
730 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700732 __copy_skb_header(n, skb);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 C(len);
735 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700736 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700737 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800738 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 C(tail);
742 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800743 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000744 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800745 C(data);
746 C(truesize);
747 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 atomic_inc(&(skb_shinfo(skb)->dataref));
750 skb->cloned = 1;
751
752 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700753#undef C
754}
755
756/**
757 * skb_morph - morph one skb into another
758 * @dst: the skb to receive the contents
759 * @src: the skb to supply the contents
760 *
761 * This is identical to skb_clone except that the target skb is
762 * supplied by the user.
763 *
764 * The target skb is returned upon exit.
765 */
766struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
767{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800768 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700769 return __skb_clone(dst, src);
770}
771EXPORT_SYMBOL_GPL(skb_morph);
772
Ben Hutchings2c530402012-07-10 10:55:09 +0000773/**
774 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000775 * @skb: the skb to modify
776 * @gfp_mask: allocation priority
777 *
778 * This must be called on SKBTX_DEV_ZEROCOPY skb.
779 * It will copy all frags into kernel and drop the reference
780 * to userspace pages.
781 *
782 * If this function is called from an interrupt gfp_mask() must be
783 * %GFP_ATOMIC.
784 *
785 * Returns 0 on success or a negative error code on failure
786 * to allocate kernel memory to copy to.
787 */
788int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000789{
790 int i;
791 int num_frags = skb_shinfo(skb)->nr_frags;
792 struct page *page, *head = NULL;
793 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
794
795 for (i = 0; i < num_frags; i++) {
796 u8 *vaddr;
797 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
798
Krishna Kumar02756ed2012-07-17 02:05:29 +0000799 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000800 if (!page) {
801 while (head) {
802 struct page *next = (struct page *)head->private;
803 put_page(head);
804 head = next;
805 }
806 return -ENOMEM;
807 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200808 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000809 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000810 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200811 kunmap_atomic(vaddr);
Shirley Maa6686f22011-07-06 12:22:12 +0000812 page->private = (unsigned long)head;
813 head = page;
814 }
815
816 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000817 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000818 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000819
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000820 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000821
822 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000823 for (i = num_frags - 1; i >= 0; i--) {
824 __skb_fill_page_desc(skb, i, head, 0,
825 skb_shinfo(skb)->frags[i].size);
Shirley Maa6686f22011-07-06 12:22:12 +0000826 head = (struct page *)head->private;
827 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000828
829 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000830 return 0;
831}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000832EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000833
Herbert Xue0053ec2007-10-14 00:37:52 -0700834/**
835 * skb_clone - duplicate an sk_buff
836 * @skb: buffer to clone
837 * @gfp_mask: allocation priority
838 *
839 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
840 * copies share the same packet data but not structure. The new
841 * buffer has a reference count of 1. If the allocation fails the
842 * function returns %NULL otherwise the new buffer is returned.
843 *
844 * If this function is called from an interrupt gfp_mask() must be
845 * %GFP_ATOMIC.
846 */
847
848struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
849{
850 struct sk_buff *n;
851
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000852 if (skb_orphan_frags(skb, gfp_mask))
853 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000854
Herbert Xue0053ec2007-10-14 00:37:52 -0700855 n = skb + 1;
856 if (skb->fclone == SKB_FCLONE_ORIG &&
857 n->fclone == SKB_FCLONE_UNAVAILABLE) {
858 atomic_t *fclone_ref = (atomic_t *) (n + 1);
859 n->fclone = SKB_FCLONE_CLONE;
860 atomic_inc(fclone_ref);
861 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700862 if (skb_pfmemalloc(skb))
863 gfp_mask |= __GFP_MEMALLOC;
864
Herbert Xue0053ec2007-10-14 00:37:52 -0700865 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
866 if (!n)
867 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200868
869 kmemcheck_annotate_bitfield(n, flags1);
870 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700871 n->fclone = SKB_FCLONE_UNAVAILABLE;
872 }
873
874 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800876EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
879{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700880#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * Shift between the two data areas in bytes
883 */
884 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700885#endif
Herbert Xudec18812007-10-14 00:37:30 -0700886
887 __copy_skb_header(new, old);
888
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700889#ifndef NET_SKBUFF_DATA_USES_OFFSET
890 /* {transport,network,mac}_header are relative to skb->head */
891 new->transport_header += offset;
892 new->network_header += offset;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000893 if (skb_mac_header_was_set(new))
894 new->mac_header += offset;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700895#endif
Herbert Xu79671682006-06-22 02:40:14 -0700896 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
897 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
898 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Mel Gormanc93bdd02012-07-31 16:44:19 -0700901static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
902{
903 if (skb_pfmemalloc(skb))
904 return SKB_ALLOC_RX;
905 return 0;
906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/**
909 * skb_copy - create private copy of an sk_buff
910 * @skb: buffer to copy
911 * @gfp_mask: allocation priority
912 *
913 * Make a copy of both an &sk_buff and its data. This is used when the
914 * caller wishes to modify the data and needs a private copy of the
915 * data to alter. Returns %NULL on failure or the pointer to the buffer
916 * on success. The returned buffer has a reference count of 1.
917 *
918 * As by-product this function converts non-linear &sk_buff to linear
919 * one, so that &sk_buff becomes completely private and caller is allowed
920 * to modify all the data of returned buffer. This means that this
921 * function is not recommended for use in circumstances when only
922 * header is going to be modified. Use pskb_copy() instead.
923 */
924
Al Virodd0fc662005-10-07 07:46:04 +0100925struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000927 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000928 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700929 struct sk_buff *n = __alloc_skb(size, gfp_mask,
930 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!n)
933 return NULL;
934
935 /* Set the data pointer */
936 skb_reserve(n, headerlen);
937 /* Set the tail pointer and length */
938 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
941 BUG();
942
943 copy_skb_header(n, skb);
944 return n;
945}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800946EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948/**
Eric Dumazet117632e2011-12-03 21:39:53 +0000949 * __pskb_copy - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000951 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * @gfp_mask: allocation priority
953 *
954 * Make a copy of both an &sk_buff and part of its data, located
955 * in header. Fragmented data remain shared. This is used when
956 * the caller wishes to modify only header of &sk_buff and needs
957 * private copy of the header to alter. Returns %NULL on failure
958 * or the pointer to the buffer on success.
959 * The returned buffer has a reference count of 1.
960 */
961
Eric Dumazet117632e2011-12-03 21:39:53 +0000962struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Eric Dumazet117632e2011-12-03 21:39:53 +0000964 unsigned int size = skb_headlen(skb) + headroom;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700965 struct sk_buff *n = __alloc_skb(size, gfp_mask,
966 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!n)
969 goto out;
970
971 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000972 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* Set the tail pointer and length */
974 skb_put(n, skb_headlen(skb));
975 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300976 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Herbert Xu25f484a2006-11-07 14:57:15 -0800978 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 n->data_len = skb->data_len;
980 n->len = skb->len;
981
982 if (skb_shinfo(skb)->nr_frags) {
983 int i;
984
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000985 if (skb_orphan_frags(skb, gfp_mask)) {
986 kfree_skb(n);
987 n = NULL;
988 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +0000989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
991 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +0000992 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 skb_shinfo(n)->nr_frags = i;
995 }
996
David S. Miller21dc3302010-08-23 00:13:46 -0700997 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
999 skb_clone_fraglist(n);
1000 }
1001
1002 copy_skb_header(n, skb);
1003out:
1004 return n;
1005}
Eric Dumazet117632e2011-12-03 21:39:53 +00001006EXPORT_SYMBOL(__pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008/**
1009 * pskb_expand_head - reallocate header of &sk_buff
1010 * @skb: buffer to reallocate
1011 * @nhead: room to add at head
1012 * @ntail: room to add at tail
1013 * @gfp_mask: allocation priority
1014 *
1015 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1016 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1017 * reference count of 1. Returns zero in the case of success or error,
1018 * if expansion failed. In the last case, &sk_buff is not changed.
1019 *
1020 * All the pointers pointing into skb header may change and must be
1021 * reloaded after call to this function.
1022 */
1023
Victor Fusco86a76ca2005-07-08 14:57:47 -07001024int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001025 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 int i;
1028 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001029 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 long off;
1031
Herbert Xu4edd87a2008-10-01 07:09:38 -07001032 BUG_ON(nhead < 0);
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (skb_shared(skb))
1035 BUG();
1036
1037 size = SKB_DATA_ALIGN(size);
1038
Mel Gormanc93bdd02012-07-31 16:44:19 -07001039 if (skb_pfmemalloc(skb))
1040 gfp_mask |= __GFP_MEMALLOC;
1041 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1042 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!data)
1044 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001045 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001048 * optimized for the cases when header is void.
1049 */
1050 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1051
1052 memcpy((struct skb_shared_info *)(data + size),
1053 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001054 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alexander Duyck3e245912012-05-04 14:26:51 +00001056 /*
1057 * if shinfo is shared we must drop the old head gracefully, but if it
1058 * is not we can just drop the old head and let the existing refcount
1059 * be since all we did is relocate the values
1060 */
1061 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001062 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001063 if (skb_orphan_frags(skb, gfp_mask))
1064 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001065 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001066 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Eric Dumazet1fd63042010-09-02 23:09:32 +00001068 if (skb_has_frag_list(skb))
1069 skb_clone_fraglist(skb);
1070
1071 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001072 } else {
1073 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 off = (data + nhead) - skb->head;
1076
1077 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001078 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001080#ifdef NET_SKBUFF_DATA_USES_OFFSET
1081 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001082 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001083#else
1084 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001085#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001086 /* {transport,network,mac}_header and tail are relative to skb->head */
1087 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001088 skb->transport_header += off;
1089 skb->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001090 if (skb_mac_header_was_set(skb))
1091 skb->mac_header += off;
Andrea Shepard00c5a982010-07-22 09:12:35 +00001092 /* Only adjust this if it actually is csum_start rather than csum */
1093 if (skb->ip_summed == CHECKSUM_PARTIAL)
1094 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001096 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 skb->nohdr = 0;
1098 atomic_set(&skb_shinfo(skb)->dataref, 1);
1099 return 0;
1100
Shirley Maa6686f22011-07-06 12:22:12 +00001101nofrags:
1102 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103nodata:
1104 return -ENOMEM;
1105}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001106EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108/* Make private copy of skb with writable head and some headroom */
1109
1110struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1111{
1112 struct sk_buff *skb2;
1113 int delta = headroom - skb_headroom(skb);
1114
1115 if (delta <= 0)
1116 skb2 = pskb_copy(skb, GFP_ATOMIC);
1117 else {
1118 skb2 = skb_clone(skb, GFP_ATOMIC);
1119 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1120 GFP_ATOMIC)) {
1121 kfree_skb(skb2);
1122 skb2 = NULL;
1123 }
1124 }
1125 return skb2;
1126}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001127EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129/**
1130 * skb_copy_expand - copy and expand sk_buff
1131 * @skb: buffer to copy
1132 * @newheadroom: new free bytes at head
1133 * @newtailroom: new free bytes at tail
1134 * @gfp_mask: allocation priority
1135 *
1136 * Make a copy of both an &sk_buff and its data and while doing so
1137 * allocate additional space.
1138 *
1139 * This is used when the caller wishes to modify the data and needs a
1140 * private copy of the data to alter as well as more space for new fields.
1141 * Returns %NULL on failure or the pointer to the buffer
1142 * on success. The returned buffer has a reference count of 1.
1143 *
1144 * You must pass %GFP_ATOMIC as the allocation priority if this function
1145 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
1147struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001148 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001149 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 /*
1152 * Allocate the copy buffer
1153 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001154 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1155 gfp_mask, skb_alloc_rx_flag(skb),
1156 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001157 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -07001159 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (!n)
1162 return NULL;
1163
1164 skb_reserve(n, newheadroom);
1165
1166 /* Set the tail pointer and length */
1167 skb_put(n, skb->len);
1168
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001169 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 head_copy_off = 0;
1171 if (newheadroom <= head_copy_len)
1172 head_copy_len = newheadroom;
1173 else
1174 head_copy_off = newheadroom - head_copy_len;
1175
1176 /* Copy the linear header and data. */
1177 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1178 skb->len + head_copy_len))
1179 BUG();
1180
1181 copy_skb_header(n, skb);
1182
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001183 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -07001184 if (n->ip_summed == CHECKSUM_PARTIAL)
1185 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -07001186#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001187 n->transport_header += off;
1188 n->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001189 if (skb_mac_header_was_set(skb))
1190 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -07001191#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return n;
1194}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001195EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/**
1198 * skb_pad - zero pad the tail of an skb
1199 * @skb: buffer to pad
1200 * @pad: space to pad
1201 *
1202 * Ensure that a buffer is followed by a padding area that is zero
1203 * filled. Used by network drivers which may DMA or transfer data
1204 * beyond the buffer end onto the wire.
1205 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001206 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001208
Herbert Xu5b057c62006-06-23 02:06:41 -07001209int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Herbert Xu5b057c62006-06-23 02:06:41 -07001211 int err;
1212 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001215 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001219
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001220 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001221 if (likely(skb_cloned(skb) || ntail > 0)) {
1222 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1223 if (unlikely(err))
1224 goto free_skb;
1225 }
1226
1227 /* FIXME: The use of this function with non-linear skb's really needs
1228 * to be audited.
1229 */
1230 err = skb_linearize(skb);
1231 if (unlikely(err))
1232 goto free_skb;
1233
1234 memset(skb->data + skb->len, 0, pad);
1235 return 0;
1236
1237free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001239 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001240}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001241EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001242
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001243/**
1244 * skb_put - add data to a buffer
1245 * @skb: buffer to use
1246 * @len: amount of data to add
1247 *
1248 * This function extends the used data area of the buffer. If this would
1249 * exceed the total buffer size the kernel will panic. A pointer to the
1250 * first byte of the extra data is returned.
1251 */
1252unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1253{
1254 unsigned char *tmp = skb_tail_pointer(skb);
1255 SKB_LINEAR_ASSERT(skb);
1256 skb->tail += len;
1257 skb->len += len;
1258 if (unlikely(skb->tail > skb->end))
1259 skb_over_panic(skb, len, __builtin_return_address(0));
1260 return tmp;
1261}
1262EXPORT_SYMBOL(skb_put);
1263
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001264/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001265 * skb_push - add data to the start of a buffer
1266 * @skb: buffer to use
1267 * @len: amount of data to add
1268 *
1269 * This function extends the used data area of the buffer at the buffer
1270 * start. If this would exceed the total buffer headroom the kernel will
1271 * panic. A pointer to the first byte of the extra data is returned.
1272 */
1273unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1274{
1275 skb->data -= len;
1276 skb->len += len;
1277 if (unlikely(skb->data<skb->head))
1278 skb_under_panic(skb, len, __builtin_return_address(0));
1279 return skb->data;
1280}
1281EXPORT_SYMBOL(skb_push);
1282
1283/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001284 * skb_pull - remove data from the start of a buffer
1285 * @skb: buffer to use
1286 * @len: amount of data to remove
1287 *
1288 * This function removes data from the start of a buffer, returning
1289 * the memory to the headroom. A pointer to the next data in the buffer
1290 * is returned. Once the data has been pulled future pushes will overwrite
1291 * the old data.
1292 */
1293unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1294{
David S. Miller47d29642010-05-02 02:21:44 -07001295 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001296}
1297EXPORT_SYMBOL(skb_pull);
1298
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001299/**
1300 * skb_trim - remove end from a buffer
1301 * @skb: buffer to alter
1302 * @len: new length
1303 *
1304 * Cut the length of a buffer down by removing data from the tail. If
1305 * the buffer is already under the length specified it is not modified.
1306 * The skb must be linear.
1307 */
1308void skb_trim(struct sk_buff *skb, unsigned int len)
1309{
1310 if (skb->len > len)
1311 __skb_trim(skb, len);
1312}
1313EXPORT_SYMBOL(skb_trim);
1314
Herbert Xu3cc0e872006-06-09 16:13:38 -07001315/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
1317
Herbert Xu3cc0e872006-06-09 16:13:38 -07001318int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Herbert Xu27b437c2006-07-13 19:26:39 -07001320 struct sk_buff **fragp;
1321 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int offset = skb_headlen(skb);
1323 int nfrags = skb_shinfo(skb)->nr_frags;
1324 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001325 int err;
1326
1327 if (skb_cloned(skb) &&
1328 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001331 i = 0;
1332 if (offset >= len)
1333 goto drop_pages;
1334
1335 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001336 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001337
1338 if (end < len) {
1339 offset = end;
1340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001342
Eric Dumazet9e903e02011-10-18 21:00:24 +00001343 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001344
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001345drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001346 skb_shinfo(skb)->nr_frags = i;
1347
1348 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001349 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001350
David S. Miller21dc3302010-08-23 00:13:46 -07001351 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001352 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001353 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Herbert Xu27b437c2006-07-13 19:26:39 -07001356 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1357 fragp = &frag->next) {
1358 int end = offset + frag->len;
1359
1360 if (skb_shared(frag)) {
1361 struct sk_buff *nfrag;
1362
1363 nfrag = skb_clone(frag, GFP_ATOMIC);
1364 if (unlikely(!nfrag))
1365 return -ENOMEM;
1366
1367 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001368 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001369 frag = nfrag;
1370 *fragp = frag;
1371 }
1372
1373 if (end < len) {
1374 offset = end;
1375 continue;
1376 }
1377
1378 if (end > len &&
1379 unlikely((err = pskb_trim(frag, len - offset))))
1380 return err;
1381
1382 if (frag->next)
1383 skb_drop_list(&frag->next);
1384 break;
1385 }
1386
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001387done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001388 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 skb->data_len -= skb->len - len;
1390 skb->len = len;
1391 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001392 skb->len = len;
1393 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001394 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
1397 return 0;
1398}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001399EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401/**
1402 * __pskb_pull_tail - advance tail of skb header
1403 * @skb: buffer to reallocate
1404 * @delta: number of bytes to advance tail
1405 *
1406 * The function makes a sense only on a fragmented &sk_buff,
1407 * it expands header moving its tail forward and copying necessary
1408 * data from fragmented part.
1409 *
1410 * &sk_buff MUST have reference count of 1.
1411 *
1412 * Returns %NULL (and &sk_buff does not change) if pull failed
1413 * or value of new tail of skb in the case of success.
1414 *
1415 * All the pointers pointing into skb header may change and must be
1416 * reloaded after call to this function.
1417 */
1418
1419/* Moves tail of skb head forward, copying data from fragmented part,
1420 * when it is necessary.
1421 * 1. It may fail due to malloc failure.
1422 * 2. It may change skb pointers.
1423 *
1424 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1425 */
1426unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1427{
1428 /* If skb has not enough free space at tail, get new one
1429 * plus 128 bytes for future expansions. If we have enough
1430 * room at tail, reallocate without expansion only if skb is cloned.
1431 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001432 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (eat > 0 || skb_cloned(skb)) {
1435 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1436 GFP_ATOMIC))
1437 return NULL;
1438 }
1439
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001440 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 BUG();
1442
1443 /* Optimization: no fragments, no reasons to preestimate
1444 * size of pulled pages. Superb.
1445 */
David S. Miller21dc3302010-08-23 00:13:46 -07001446 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto pull_pages;
1448
1449 /* Estimate size of pulled pages. */
1450 eat = delta;
1451 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001452 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1453
1454 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001456 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
1459 /* If we need update frag list, we are in troubles.
1460 * Certainly, it possible to add an offset to skb data,
1461 * but taking into account that pulling is expected to
1462 * be very rare operation, it is worth to fight against
1463 * further bloating skb head and crucify ourselves here instead.
1464 * Pure masohism, indeed. 8)8)
1465 */
1466 if (eat) {
1467 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1468 struct sk_buff *clone = NULL;
1469 struct sk_buff *insp = NULL;
1470
1471 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001472 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (list->len <= eat) {
1475 /* Eaten as whole. */
1476 eat -= list->len;
1477 list = list->next;
1478 insp = list;
1479 } else {
1480 /* Eaten partially. */
1481
1482 if (skb_shared(list)) {
1483 /* Sucks! We need to fork list. :-( */
1484 clone = skb_clone(list, GFP_ATOMIC);
1485 if (!clone)
1486 return NULL;
1487 insp = list->next;
1488 list = clone;
1489 } else {
1490 /* This may be pulled without
1491 * problems. */
1492 insp = list;
1493 }
1494 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001495 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return NULL;
1497 }
1498 break;
1499 }
1500 } while (eat);
1501
1502 /* Free pulled out fragments. */
1503 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1504 skb_shinfo(skb)->frag_list = list->next;
1505 kfree_skb(list);
1506 }
1507 /* And insert new clone at head. */
1508 if (clone) {
1509 clone->next = list;
1510 skb_shinfo(skb)->frag_list = clone;
1511 }
1512 }
1513 /* Success! Now we may commit changes to skb data. */
1514
1515pull_pages:
1516 eat = delta;
1517 k = 0;
1518 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001519 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1520
1521 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001522 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001523 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 } else {
1525 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1526 if (eat) {
1527 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001528 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 eat = 0;
1530 }
1531 k++;
1532 }
1533 }
1534 skb_shinfo(skb)->nr_frags = k;
1535
1536 skb->tail += delta;
1537 skb->data_len -= delta;
1538
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001539 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001541EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Eric Dumazet22019b12011-07-29 18:37:31 +00001543/**
1544 * skb_copy_bits - copy bits from skb to kernel buffer
1545 * @skb: source skb
1546 * @offset: offset in source
1547 * @to: destination buffer
1548 * @len: number of bytes to copy
1549 *
1550 * Copy the specified number of bytes from the source skb to the
1551 * destination buffer.
1552 *
1553 * CAUTION ! :
1554 * If its prototype is ever changed,
1555 * check arch/{*}/net/{*}.S files,
1556 * since it is called from BPF assembly code.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1559{
David S. Miller1a028e52007-04-27 15:21:23 -07001560 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001561 struct sk_buff *frag_iter;
1562 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 if (offset > (int)skb->len - len)
1565 goto fault;
1566
1567 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001568 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (copy > len)
1570 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001571 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if ((len -= copy) == 0)
1573 return 0;
1574 offset += copy;
1575 to += copy;
1576 }
1577
1578 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001579 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001580 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001582 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001583
Eric Dumazet51c56b02012-04-05 11:35:15 +02001584 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if ((copy = end - offset) > 0) {
1586 u8 *vaddr;
1587
1588 if (copy > len)
1589 copy = len;
1590
Eric Dumazet51c56b02012-04-05 11:35:15 +02001591 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001593 vaddr + f->page_offset + offset - start,
1594 copy);
1595 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 if ((len -= copy) == 0)
1598 return 0;
1599 offset += copy;
1600 to += copy;
1601 }
David S. Miller1a028e52007-04-27 15:21:23 -07001602 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
David S. Millerfbb398a2009-06-09 00:18:59 -07001605 skb_walk_frags(skb, frag_iter) {
1606 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David S. Millerfbb398a2009-06-09 00:18:59 -07001608 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
David S. Millerfbb398a2009-06-09 00:18:59 -07001610 end = start + frag_iter->len;
1611 if ((copy = end - offset) > 0) {
1612 if (copy > len)
1613 copy = len;
1614 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1615 goto fault;
1616 if ((len -= copy) == 0)
1617 return 0;
1618 offset += copy;
1619 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001621 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Shirley Maa6686f22011-07-06 12:22:12 +00001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (!len)
1625 return 0;
1626
1627fault:
1628 return -EFAULT;
1629}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001630EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Jens Axboe9c55e012007-11-06 23:30:13 -08001632/*
1633 * Callback from splice_to_pipe(), if we need to release some pages
1634 * at the end of the spd in case we error'ed out in filling the pipe.
1635 */
1636static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1637{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001638 put_page(spd->pages[i]);
1639}
Jens Axboe9c55e012007-11-06 23:30:13 -08001640
David S. Millera108d5f2012-04-23 23:06:11 -04001641static struct page *linear_to_page(struct page *page, unsigned int *len,
1642 unsigned int *offset,
1643 struct sk_buff *skb, struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001644{
Eric Dumazet5640f762012-09-23 23:04:42 +00001645 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001646
Eric Dumazet5640f762012-09-23 23:04:42 +00001647 if (!sk_page_frag_refill(sk, pfrag))
1648 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001649
Eric Dumazet5640f762012-09-23 23:04:42 +00001650 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001651
Eric Dumazet5640f762012-09-23 23:04:42 +00001652 memcpy(page_address(pfrag->page) + pfrag->offset,
1653 page_address(page) + *offset, *len);
1654 *offset = pfrag->offset;
1655 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001656
Eric Dumazet5640f762012-09-23 23:04:42 +00001657 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001658}
1659
Eric Dumazet41c73a02012-04-22 12:26:16 +00001660static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1661 struct page *page,
1662 unsigned int offset)
1663{
1664 return spd->nr_pages &&
1665 spd->pages[spd->nr_pages - 1] == page &&
1666 (spd->partial[spd->nr_pages - 1].offset +
1667 spd->partial[spd->nr_pages - 1].len == offset);
1668}
1669
Jens Axboe9c55e012007-11-06 23:30:13 -08001670/*
1671 * Fill page/offset/length into spd, if it can hold more pages.
1672 */
David S. Millera108d5f2012-04-23 23:06:11 -04001673static bool spd_fill_page(struct splice_pipe_desc *spd,
1674 struct pipe_inode_info *pipe, struct page *page,
1675 unsigned int *len, unsigned int offset,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001676 struct sk_buff *skb, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001677 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001678{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001679 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001680 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001681
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001682 if (linear) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001683 page = linear_to_page(page, len, &offset, skb, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001684 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001685 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001686 }
1687 if (spd_can_coalesce(spd, page, offset)) {
1688 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001689 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001690 }
1691 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001692 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001693 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001694 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001695 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001696
David S. Millera108d5f2012-04-23 23:06:11 -04001697 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001698}
1699
Octavian Purdila2870c432008-07-15 00:49:11 -07001700static inline void __segment_seek(struct page **page, unsigned int *poff,
1701 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001702{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001703 unsigned long n;
1704
Octavian Purdila2870c432008-07-15 00:49:11 -07001705 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001706 n = *poff / PAGE_SIZE;
1707 if (n)
1708 *page = nth_page(*page, n);
1709
Octavian Purdila2870c432008-07-15 00:49:11 -07001710 *poff = *poff % PAGE_SIZE;
1711 *plen -= off;
1712}
Jens Axboe9c55e012007-11-06 23:30:13 -08001713
David S. Millera108d5f2012-04-23 23:06:11 -04001714static bool __splice_segment(struct page *page, unsigned int poff,
1715 unsigned int plen, unsigned int *off,
1716 unsigned int *len, struct sk_buff *skb,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001717 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001718 struct sock *sk,
1719 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001720{
1721 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001722 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001723
1724 /* skip this segment if already processed */
1725 if (*off >= plen) {
1726 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001727 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001728 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001729
Octavian Purdila2870c432008-07-15 00:49:11 -07001730 /* ignore any bits we already processed */
1731 if (*off) {
1732 __segment_seek(&page, &poff, &plen, *off);
1733 *off = 0;
1734 }
1735
1736 do {
1737 unsigned int flen = min(*len, plen);
1738
1739 /* the linear region may spread across several pages */
1740 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1741
Jens Axboe35f3d142010-05-20 10:43:18 +02001742 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
David S. Millera108d5f2012-04-23 23:06:11 -04001743 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001744
1745 __segment_seek(&page, &poff, &plen, flen);
1746 *len -= flen;
1747
1748 } while (*len && plen);
1749
David S. Millera108d5f2012-04-23 23:06:11 -04001750 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001751}
1752
1753/*
David S. Millera108d5f2012-04-23 23:06:11 -04001754 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001755 * pipe is full or if we already spliced the requested length.
1756 */
David S. Millera108d5f2012-04-23 23:06:11 -04001757static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1758 unsigned int *offset, unsigned int *len,
1759 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001760{
1761 int seg;
1762
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001763 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001764 * If skb->head_frag is set, this 'linear' part is backed by a
1765 * fragment, and if the head is not shared with any clones then
1766 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001767 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001768 if (__splice_segment(virt_to_page(skb->data),
1769 (unsigned long) skb->data & (PAGE_SIZE - 1),
1770 skb_headlen(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001771 offset, len, skb, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001772 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001773 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001774 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001775
1776 /*
1777 * then map the fragments
1778 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001779 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1780 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1781
Ian Campbellea2ab692011-08-22 23:44:58 +00001782 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001783 f->page_offset, skb_frag_size(f),
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001784 offset, len, skb, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001785 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001786 }
1787
David S. Millera108d5f2012-04-23 23:06:11 -04001788 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001789}
1790
1791/*
1792 * Map data from the skb to a pipe. Should handle both the linear part,
1793 * the fragments, and the frag list. It does NOT handle frag lists within
1794 * the frag list, if such a thing exists. We'd probably need to recurse to
1795 * handle that cleanly.
1796 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001797int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001798 struct pipe_inode_info *pipe, unsigned int tlen,
1799 unsigned int flags)
1800{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001801 struct partial_page partial[MAX_SKB_FRAGS];
1802 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001803 struct splice_pipe_desc spd = {
1804 .pages = pages,
1805 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001806 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001807 .flags = flags,
1808 .ops = &sock_pipe_buf_ops,
1809 .spd_release = sock_spd_release,
1810 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001811 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001812 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001813 int ret = 0;
1814
Jens Axboe9c55e012007-11-06 23:30:13 -08001815 /*
1816 * __skb_splice_bits() only fails if the output has no room left,
1817 * so no point in going over the frag_list for the error case.
1818 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001819 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001820 goto done;
1821 else if (!tlen)
1822 goto done;
1823
1824 /*
1825 * now see if we have a frag_list to map
1826 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001827 skb_walk_frags(skb, frag_iter) {
1828 if (!tlen)
1829 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001830 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001831 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001832 }
1833
1834done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001835 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001836 /*
1837 * Drop the socket lock, otherwise we have reverse
1838 * locking dependencies between sk_lock and i_mutex
1839 * here as compared to sendfile(). We enter here
1840 * with the socket lock held, and splice_to_pipe() will
1841 * grab the pipe inode lock. For sendfile() emulation,
1842 * we call into ->sendpage() with the i_mutex lock held
1843 * and networking will grab the socket lock.
1844 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001845 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001846 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001847 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001848 }
1849
Jens Axboe35f3d142010-05-20 10:43:18 +02001850 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001851}
1852
Herbert Xu357b40a2005-04-19 22:30:14 -07001853/**
1854 * skb_store_bits - store bits from kernel buffer to skb
1855 * @skb: destination buffer
1856 * @offset: offset in destination
1857 * @from: source buffer
1858 * @len: number of bytes to copy
1859 *
1860 * Copy the specified number of bytes from the source buffer to the
1861 * destination skb. This function handles all the messy bits of
1862 * traversing fragment lists and such.
1863 */
1864
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001865int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001866{
David S. Miller1a028e52007-04-27 15:21:23 -07001867 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001868 struct sk_buff *frag_iter;
1869 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001870
1871 if (offset > (int)skb->len - len)
1872 goto fault;
1873
David S. Miller1a028e52007-04-27 15:21:23 -07001874 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001875 if (copy > len)
1876 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001877 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001878 if ((len -= copy) == 0)
1879 return 0;
1880 offset += copy;
1881 from += copy;
1882 }
1883
1884 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1885 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001886 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001887
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001888 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001889
Eric Dumazet9e903e02011-10-18 21:00:24 +00001890 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001891 if ((copy = end - offset) > 0) {
1892 u8 *vaddr;
1893
1894 if (copy > len)
1895 copy = len;
1896
Eric Dumazet51c56b02012-04-05 11:35:15 +02001897 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001898 memcpy(vaddr + frag->page_offset + offset - start,
1899 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001900 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001901
1902 if ((len -= copy) == 0)
1903 return 0;
1904 offset += copy;
1905 from += copy;
1906 }
David S. Miller1a028e52007-04-27 15:21:23 -07001907 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001908 }
1909
David S. Millerfbb398a2009-06-09 00:18:59 -07001910 skb_walk_frags(skb, frag_iter) {
1911 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001912
David S. Millerfbb398a2009-06-09 00:18:59 -07001913 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001914
David S. Millerfbb398a2009-06-09 00:18:59 -07001915 end = start + frag_iter->len;
1916 if ((copy = end - offset) > 0) {
1917 if (copy > len)
1918 copy = len;
1919 if (skb_store_bits(frag_iter, offset - start,
1920 from, copy))
1921 goto fault;
1922 if ((len -= copy) == 0)
1923 return 0;
1924 offset += copy;
1925 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001926 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001927 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001928 }
1929 if (!len)
1930 return 0;
1931
1932fault:
1933 return -EFAULT;
1934}
Herbert Xu357b40a2005-04-19 22:30:14 -07001935EXPORT_SYMBOL(skb_store_bits);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/* Checksum skb data. */
1938
Al Viro2bbbc862006-11-14 21:37:14 -08001939__wsum skb_checksum(const struct sk_buff *skb, int offset,
1940 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
David S. Miller1a028e52007-04-27 15:21:23 -07001942 int start = skb_headlen(skb);
1943 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001944 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 int pos = 0;
1946
1947 /* Checksum header. */
1948 if (copy > 0) {
1949 if (copy > len)
1950 copy = len;
1951 csum = csum_partial(skb->data + offset, copy, csum);
1952 if ((len -= copy) == 0)
1953 return csum;
1954 offset += copy;
1955 pos = copy;
1956 }
1957
1958 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001959 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001960 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001962 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001963
Eric Dumazet51c56b02012-04-05 11:35:15 +02001964 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001966 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 if (copy > len)
1970 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001971 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001972 csum2 = csum_partial(vaddr + frag->page_offset +
1973 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001974 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 csum = csum_block_add(csum, csum2, pos);
1976 if (!(len -= copy))
1977 return csum;
1978 offset += copy;
1979 pos += copy;
1980 }
David S. Miller1a028e52007-04-27 15:21:23 -07001981 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
David S. Millerfbb398a2009-06-09 00:18:59 -07001984 skb_walk_frags(skb, frag_iter) {
1985 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
David S. Millerfbb398a2009-06-09 00:18:59 -07001987 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
David S. Millerfbb398a2009-06-09 00:18:59 -07001989 end = start + frag_iter->len;
1990 if ((copy = end - offset) > 0) {
1991 __wsum csum2;
1992 if (copy > len)
1993 copy = len;
1994 csum2 = skb_checksum(frag_iter, offset - start,
1995 copy, 0);
1996 csum = csum_block_add(csum, csum2, pos);
1997 if ((len -= copy) == 0)
1998 return csum;
1999 offset += copy;
2000 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002002 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002004 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 return csum;
2007}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002008EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010/* Both of above in one bottle. */
2011
Al Viro81d77662006-11-14 21:37:33 -08002012__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2013 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
David S. Miller1a028e52007-04-27 15:21:23 -07002015 int start = skb_headlen(skb);
2016 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002017 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 int pos = 0;
2019
2020 /* Copy header. */
2021 if (copy > 0) {
2022 if (copy > len)
2023 copy = len;
2024 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2025 copy, csum);
2026 if ((len -= copy) == 0)
2027 return csum;
2028 offset += copy;
2029 to += copy;
2030 pos = copy;
2031 }
2032
2033 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002034 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002036 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002037
Eric Dumazet9e903e02011-10-18 21:00:24 +00002038 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002040 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 u8 *vaddr;
2042 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2043
2044 if (copy > len)
2045 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002046 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002048 frag->page_offset +
2049 offset - start, to,
2050 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002051 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 csum = csum_block_add(csum, csum2, pos);
2053 if (!(len -= copy))
2054 return csum;
2055 offset += copy;
2056 to += copy;
2057 pos += copy;
2058 }
David S. Miller1a028e52007-04-27 15:21:23 -07002059 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061
David S. Millerfbb398a2009-06-09 00:18:59 -07002062 skb_walk_frags(skb, frag_iter) {
2063 __wsum csum2;
2064 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
David S. Millerfbb398a2009-06-09 00:18:59 -07002066 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
David S. Millerfbb398a2009-06-09 00:18:59 -07002068 end = start + frag_iter->len;
2069 if ((copy = end - offset) > 0) {
2070 if (copy > len)
2071 copy = len;
2072 csum2 = skb_copy_and_csum_bits(frag_iter,
2073 offset - start,
2074 to, copy, 0);
2075 csum = csum_block_add(csum, csum2, pos);
2076 if ((len -= copy) == 0)
2077 return csum;
2078 offset += copy;
2079 to += copy;
2080 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002082 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002084 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return csum;
2086}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002087EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2090{
Al Virod3bc23e2006-11-14 21:24:49 -08002091 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 long csstart;
2093
Patrick McHardy84fa7932006-08-29 16:44:56 -07002094 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002095 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 else
2097 csstart = skb_headlen(skb);
2098
Kris Katterjohn09a62662006-01-08 22:24:28 -08002099 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002101 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 csum = 0;
2104 if (csstart != skb->len)
2105 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2106 skb->len - csstart, 0);
2107
Patrick McHardy84fa7932006-08-29 16:44:56 -07002108 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002109 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Al Virod3bc23e2006-11-14 21:24:49 -08002111 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002114EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116/**
2117 * skb_dequeue - remove from the head of the queue
2118 * @list: list to dequeue from
2119 *
2120 * Remove the head of the list. The list lock is taken so the function
2121 * may be used safely with other locking list functions. The head item is
2122 * returned or %NULL if the list is empty.
2123 */
2124
2125struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2126{
2127 unsigned long flags;
2128 struct sk_buff *result;
2129
2130 spin_lock_irqsave(&list->lock, flags);
2131 result = __skb_dequeue(list);
2132 spin_unlock_irqrestore(&list->lock, flags);
2133 return result;
2134}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002135EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137/**
2138 * skb_dequeue_tail - remove from the tail of the queue
2139 * @list: list to dequeue from
2140 *
2141 * Remove the tail of the list. The list lock is taken so the function
2142 * may be used safely with other locking list functions. The tail item is
2143 * returned or %NULL if the list is empty.
2144 */
2145struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2146{
2147 unsigned long flags;
2148 struct sk_buff *result;
2149
2150 spin_lock_irqsave(&list->lock, flags);
2151 result = __skb_dequeue_tail(list);
2152 spin_unlock_irqrestore(&list->lock, flags);
2153 return result;
2154}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002155EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157/**
2158 * skb_queue_purge - empty a list
2159 * @list: list to empty
2160 *
2161 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2162 * the list and one reference dropped. This function takes the list
2163 * lock and is atomic with respect to other list locking functions.
2164 */
2165void skb_queue_purge(struct sk_buff_head *list)
2166{
2167 struct sk_buff *skb;
2168 while ((skb = skb_dequeue(list)) != NULL)
2169 kfree_skb(skb);
2170}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002171EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173/**
2174 * skb_queue_head - queue a buffer at the list head
2175 * @list: list to use
2176 * @newsk: buffer to queue
2177 *
2178 * Queue a buffer at the start of the list. This function takes the
2179 * list lock and can be used safely with other locking &sk_buff functions
2180 * safely.
2181 *
2182 * A buffer cannot be placed on two lists at the same time.
2183 */
2184void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2185{
2186 unsigned long flags;
2187
2188 spin_lock_irqsave(&list->lock, flags);
2189 __skb_queue_head(list, newsk);
2190 spin_unlock_irqrestore(&list->lock, flags);
2191}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002192EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194/**
2195 * skb_queue_tail - queue a buffer at the list tail
2196 * @list: list to use
2197 * @newsk: buffer to queue
2198 *
2199 * Queue a buffer at the tail of the list. This function takes the
2200 * list lock and can be used safely with other locking &sk_buff functions
2201 * safely.
2202 *
2203 * A buffer cannot be placed on two lists at the same time.
2204 */
2205void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2206{
2207 unsigned long flags;
2208
2209 spin_lock_irqsave(&list->lock, flags);
2210 __skb_queue_tail(list, newsk);
2211 spin_unlock_irqrestore(&list->lock, flags);
2212}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002213EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215/**
2216 * skb_unlink - remove a buffer from a list
2217 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002218 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 *
David S. Miller8728b832005-08-09 19:25:21 -07002220 * Remove a packet from a list. The list locks are taken and this
2221 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 *
David S. Miller8728b832005-08-09 19:25:21 -07002223 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 */
David S. Miller8728b832005-08-09 19:25:21 -07002225void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
David S. Miller8728b832005-08-09 19:25:21 -07002227 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
David S. Miller8728b832005-08-09 19:25:21 -07002229 spin_lock_irqsave(&list->lock, flags);
2230 __skb_unlink(skb, list);
2231 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002233EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235/**
2236 * skb_append - append a buffer
2237 * @old: buffer to insert after
2238 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002239 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
2241 * Place a packet after a given packet in a list. The list locks are taken
2242 * and this function is atomic with respect to other list locked calls.
2243 * A buffer cannot be placed on two lists at the same time.
2244 */
David S. Miller8728b832005-08-09 19:25:21 -07002245void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 unsigned long flags;
2248
David S. Miller8728b832005-08-09 19:25:21 -07002249 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002250 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002251 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002253EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255/**
2256 * skb_insert - insert a buffer
2257 * @old: buffer to insert before
2258 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002259 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 *
David S. Miller8728b832005-08-09 19:25:21 -07002261 * Place a packet before a given packet in a list. The list locks are
2262 * taken and this function is atomic with respect to other list locked
2263 * calls.
2264 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * A buffer cannot be placed on two lists at the same time.
2266 */
David S. Miller8728b832005-08-09 19:25:21 -07002267void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 unsigned long flags;
2270
David S. Miller8728b832005-08-09 19:25:21 -07002271 spin_lock_irqsave(&list->lock, flags);
2272 __skb_insert(newsk, old->prev, old, list);
2273 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002275EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277static inline void skb_split_inside_header(struct sk_buff *skb,
2278 struct sk_buff* skb1,
2279 const u32 len, const int pos)
2280{
2281 int i;
2282
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002283 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2284 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* And move data appendix as is. */
2286 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2287 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2288
2289 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2290 skb_shinfo(skb)->nr_frags = 0;
2291 skb1->data_len = skb->data_len;
2292 skb1->len += skb1->data_len;
2293 skb->data_len = 0;
2294 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002295 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
2298static inline void skb_split_no_header(struct sk_buff *skb,
2299 struct sk_buff* skb1,
2300 const u32 len, int pos)
2301{
2302 int i, k = 0;
2303 const int nfrags = skb_shinfo(skb)->nr_frags;
2304
2305 skb_shinfo(skb)->nr_frags = 0;
2306 skb1->len = skb1->data_len = skb->len - len;
2307 skb->len = len;
2308 skb->data_len = len - pos;
2309
2310 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002311 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (pos + size > len) {
2314 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2315
2316 if (pos < len) {
2317 /* Split frag.
2318 * We have two variants in this case:
2319 * 1. Move all the frag to the second
2320 * part, if it is possible. F.e.
2321 * this approach is mandatory for TUX,
2322 * where splitting is expensive.
2323 * 2. Split is accurately. We make this.
2324 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002325 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002327 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2328 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 skb_shinfo(skb)->nr_frags++;
2330 }
2331 k++;
2332 } else
2333 skb_shinfo(skb)->nr_frags++;
2334 pos += size;
2335 }
2336 skb_shinfo(skb1)->nr_frags = k;
2337}
2338
2339/**
2340 * skb_split - Split fragmented skb to two parts at length len.
2341 * @skb: the buffer to split
2342 * @skb1: the buffer to receive the second part
2343 * @len: new length for skb
2344 */
2345void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2346{
2347 int pos = skb_headlen(skb);
2348
2349 if (len < pos) /* Split line is inside header. */
2350 skb_split_inside_header(skb, skb1, len, pos);
2351 else /* Second chunk has no header, nothing to copy. */
2352 skb_split_no_header(skb, skb1, len, pos);
2353}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002354EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002356/* Shifting from/to a cloned skb is a no-go.
2357 *
2358 * Caller cannot keep skb_shinfo related pointers past calling here!
2359 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002360static int skb_prepare_for_shift(struct sk_buff *skb)
2361{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002362 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002363}
2364
2365/**
2366 * skb_shift - Shifts paged data partially from skb to another
2367 * @tgt: buffer into which tail data gets added
2368 * @skb: buffer from which the paged data comes from
2369 * @shiftlen: shift up to this many bytes
2370 *
2371 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002372 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002373 * It's up to caller to free skb if everything was shifted.
2374 *
2375 * If @tgt runs out of frags, the whole operation is aborted.
2376 *
2377 * Skb cannot include anything else but paged data while tgt is allowed
2378 * to have non-paged data as well.
2379 *
2380 * TODO: full sized shift could be optimized but that would need
2381 * specialized skb free'er to handle frags without up-to-date nr_frags.
2382 */
2383int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2384{
2385 int from, to, merge, todo;
2386 struct skb_frag_struct *fragfrom, *fragto;
2387
2388 BUG_ON(shiftlen > skb->len);
2389 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2390
2391 todo = shiftlen;
2392 from = 0;
2393 to = skb_shinfo(tgt)->nr_frags;
2394 fragfrom = &skb_shinfo(skb)->frags[from];
2395
2396 /* Actual merge is delayed until the point when we know we can
2397 * commit all, so that we don't have to undo partial changes
2398 */
2399 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002400 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2401 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002402 merge = -1;
2403 } else {
2404 merge = to - 1;
2405
Eric Dumazet9e903e02011-10-18 21:00:24 +00002406 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002407 if (todo < 0) {
2408 if (skb_prepare_for_shift(skb) ||
2409 skb_prepare_for_shift(tgt))
2410 return 0;
2411
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002412 /* All previous frag pointers might be stale! */
2413 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002414 fragto = &skb_shinfo(tgt)->frags[merge];
2415
Eric Dumazet9e903e02011-10-18 21:00:24 +00002416 skb_frag_size_add(fragto, shiftlen);
2417 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002418 fragfrom->page_offset += shiftlen;
2419
2420 goto onlymerged;
2421 }
2422
2423 from++;
2424 }
2425
2426 /* Skip full, not-fitting skb to avoid expensive operations */
2427 if ((shiftlen == skb->len) &&
2428 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2429 return 0;
2430
2431 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2432 return 0;
2433
2434 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2435 if (to == MAX_SKB_FRAGS)
2436 return 0;
2437
2438 fragfrom = &skb_shinfo(skb)->frags[from];
2439 fragto = &skb_shinfo(tgt)->frags[to];
2440
Eric Dumazet9e903e02011-10-18 21:00:24 +00002441 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002442 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002443 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002444 from++;
2445 to++;
2446
2447 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002448 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002449 fragto->page = fragfrom->page;
2450 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002451 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002452
2453 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002454 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002455 todo = 0;
2456
2457 to++;
2458 break;
2459 }
2460 }
2461
2462 /* Ready to "commit" this state change to tgt */
2463 skb_shinfo(tgt)->nr_frags = to;
2464
2465 if (merge >= 0) {
2466 fragfrom = &skb_shinfo(skb)->frags[0];
2467 fragto = &skb_shinfo(tgt)->frags[merge];
2468
Eric Dumazet9e903e02011-10-18 21:00:24 +00002469 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002470 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002471 }
2472
2473 /* Reposition in the original skb */
2474 to = 0;
2475 while (from < skb_shinfo(skb)->nr_frags)
2476 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2477 skb_shinfo(skb)->nr_frags = to;
2478
2479 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2480
2481onlymerged:
2482 /* Most likely the tgt won't ever need its checksum anymore, skb on
2483 * the other hand might need it if it needs to be resent
2484 */
2485 tgt->ip_summed = CHECKSUM_PARTIAL;
2486 skb->ip_summed = CHECKSUM_PARTIAL;
2487
2488 /* Yak, is it really working this way? Some helper please? */
2489 skb->len -= shiftlen;
2490 skb->data_len -= shiftlen;
2491 skb->truesize -= shiftlen;
2492 tgt->len += shiftlen;
2493 tgt->data_len += shiftlen;
2494 tgt->truesize += shiftlen;
2495
2496 return shiftlen;
2497}
2498
Thomas Graf677e90e2005-06-23 20:59:51 -07002499/**
2500 * skb_prepare_seq_read - Prepare a sequential read of skb data
2501 * @skb: the buffer to read
2502 * @from: lower offset of data to be read
2503 * @to: upper offset of data to be read
2504 * @st: state variable
2505 *
2506 * Initializes the specified state variable. Must be called before
2507 * invoking skb_seq_read() for the first time.
2508 */
2509void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2510 unsigned int to, struct skb_seq_state *st)
2511{
2512 st->lower_offset = from;
2513 st->upper_offset = to;
2514 st->root_skb = st->cur_skb = skb;
2515 st->frag_idx = st->stepped_offset = 0;
2516 st->frag_data = NULL;
2517}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002518EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002519
2520/**
2521 * skb_seq_read - Sequentially read skb data
2522 * @consumed: number of bytes consumed by the caller so far
2523 * @data: destination pointer for data to be returned
2524 * @st: state variable
2525 *
2526 * Reads a block of skb data at &consumed relative to the
2527 * lower offset specified to skb_prepare_seq_read(). Assigns
2528 * the head of the data block to &data and returns the length
2529 * of the block or 0 if the end of the skb data or the upper
2530 * offset has been reached.
2531 *
2532 * The caller is not required to consume all of the data
2533 * returned, i.e. &consumed is typically set to the number
2534 * of bytes already consumed and the next call to
2535 * skb_seq_read() will return the remaining part of the block.
2536 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002537 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002538 * this limitation is the cost for zerocopy seqeuental
2539 * reads of potentially non linear data.
2540 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002541 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002542 * at the moment, state->root_skb could be replaced with
2543 * a stack for this purpose.
2544 */
2545unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2546 struct skb_seq_state *st)
2547{
2548 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2549 skb_frag_t *frag;
2550
2551 if (unlikely(abs_offset >= st->upper_offset))
2552 return 0;
2553
2554next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002555 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002556
Thomas Chenault995b3372009-05-18 21:43:27 -07002557 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002558 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002559 return block_limit - abs_offset;
2560 }
2561
2562 if (st->frag_idx == 0 && !st->frag_data)
2563 st->stepped_offset += skb_headlen(st->cur_skb);
2564
2565 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2566 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002567 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002568
2569 if (abs_offset < block_limit) {
2570 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002571 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002572
2573 *data = (u8 *) st->frag_data + frag->page_offset +
2574 (abs_offset - st->stepped_offset);
2575
2576 return block_limit - abs_offset;
2577 }
2578
2579 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002580 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002581 st->frag_data = NULL;
2582 }
2583
2584 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002585 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002586 }
2587
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002588 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002589 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002590 st->frag_data = NULL;
2591 }
2592
David S. Miller21dc3302010-08-23 00:13:46 -07002593 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002594 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002595 st->frag_idx = 0;
2596 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002597 } else if (st->cur_skb->next) {
2598 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002599 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002600 goto next_skb;
2601 }
2602
2603 return 0;
2604}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002605EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002606
2607/**
2608 * skb_abort_seq_read - Abort a sequential read of skb data
2609 * @st: state variable
2610 *
2611 * Must be called if skb_seq_read() was not called until it
2612 * returned 0.
2613 */
2614void skb_abort_seq_read(struct skb_seq_state *st)
2615{
2616 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002617 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002618}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002619EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002620
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002621#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2622
2623static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2624 struct ts_config *conf,
2625 struct ts_state *state)
2626{
2627 return skb_seq_read(offset, text, TS_SKB_CB(state));
2628}
2629
2630static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2631{
2632 skb_abort_seq_read(TS_SKB_CB(state));
2633}
2634
2635/**
2636 * skb_find_text - Find a text pattern in skb data
2637 * @skb: the buffer to look in
2638 * @from: search offset
2639 * @to: search limit
2640 * @config: textsearch configuration
2641 * @state: uninitialized textsearch state variable
2642 *
2643 * Finds a pattern in the skb data according to the specified
2644 * textsearch configuration. Use textsearch_next() to retrieve
2645 * subsequent occurrences of the pattern. Returns the offset
2646 * to the first occurrence or UINT_MAX if no match was found.
2647 */
2648unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2649 unsigned int to, struct ts_config *config,
2650 struct ts_state *state)
2651{
Phil Oesterf72b9482006-06-26 00:00:57 -07002652 unsigned int ret;
2653
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002654 config->get_next_block = skb_ts_get_next_block;
2655 config->finish = skb_ts_finish;
2656
2657 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2658
Phil Oesterf72b9482006-06-26 00:00:57 -07002659 ret = textsearch_find(config, state);
2660 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002661}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002662EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002663
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002664/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002665 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002666 * @sk: sock structure
2667 * @skb: skb structure to be appened with user data.
2668 * @getfrag: call back function to be used for getting the user data
2669 * @from: pointer to user message iov
2670 * @length: length of the iov message
2671 *
2672 * Description: This procedure append the user data in the fragment part
2673 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2674 */
2675int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002676 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002677 int len, int odd, struct sk_buff *skb),
2678 void *from, int length)
2679{
2680 int frg_cnt = 0;
2681 skb_frag_t *frag = NULL;
2682 struct page *page = NULL;
2683 int copy, left;
2684 int offset = 0;
2685 int ret;
2686
2687 do {
2688 /* Return error if we don't have space for new frag */
2689 frg_cnt = skb_shinfo(skb)->nr_frags;
2690 if (frg_cnt >= MAX_SKB_FRAGS)
2691 return -EFAULT;
2692
2693 /* allocate a new page for next frag */
2694 page = alloc_pages(sk->sk_allocation, 0);
2695
2696 /* If alloc_page fails just return failure and caller will
2697 * free previous allocated pages by doing kfree_skb()
2698 */
2699 if (page == NULL)
2700 return -ENOMEM;
2701
2702 /* initialize the next frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002703 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2704 skb->truesize += PAGE_SIZE;
2705 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2706
2707 /* get the new initialized frag */
2708 frg_cnt = skb_shinfo(skb)->nr_frags;
2709 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2710
2711 /* copy the user data to page */
2712 left = PAGE_SIZE - frag->page_offset;
2713 copy = (length > left)? left : length;
2714
Eric Dumazet9e903e02011-10-18 21:00:24 +00002715 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002716 offset, copy, 0, skb);
2717 if (ret < 0)
2718 return -EFAULT;
2719
2720 /* copy was successful so update the size parameters */
Eric Dumazet9e903e02011-10-18 21:00:24 +00002721 skb_frag_size_add(frag, copy);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002722 skb->len += copy;
2723 skb->data_len += copy;
2724 offset += copy;
2725 length -= copy;
2726
2727 } while (length > 0);
2728
2729 return 0;
2730}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002731EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002732
Herbert Xucbb042f2006-03-20 22:43:56 -08002733/**
2734 * skb_pull_rcsum - pull skb and update receive checksum
2735 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002736 * @len: length of data pulled
2737 *
2738 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002739 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002740 * receive path processing instead of skb_pull unless you know
2741 * that the checksum difference is zero (e.g., a valid IP header)
2742 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002743 */
2744unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2745{
2746 BUG_ON(len > skb->len);
2747 skb->len -= len;
2748 BUG_ON(skb->len < skb->data_len);
2749 skb_postpull_rcsum(skb, skb->data, len);
2750 return skb->data += len;
2751}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002752EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2753
Herbert Xuf4c50d92006-06-22 03:02:40 -07002754/**
2755 * skb_segment - Perform protocol segmentation on skb.
2756 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002757 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002758 *
2759 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002760 * a pointer to the first in a list of new skbs for the segments.
2761 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002762 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002763struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002764{
2765 struct sk_buff *segs = NULL;
2766 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002767 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002768 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002769 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002770 unsigned int offset = doffset;
2771 unsigned int headroom;
2772 unsigned int len;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002773 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002774 int nfrags = skb_shinfo(skb)->nr_frags;
2775 int err = -ENOMEM;
2776 int i = 0;
2777 int pos;
2778
2779 __skb_push(skb, doffset);
2780 headroom = skb_headroom(skb);
2781 pos = skb_headlen(skb);
2782
2783 do {
2784 struct sk_buff *nskb;
2785 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002786 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002787 int size;
2788
2789 len = skb->len - offset;
2790 if (len > mss)
2791 len = mss;
2792
2793 hsize = skb_headlen(skb) - offset;
2794 if (hsize < 0)
2795 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002796 if (hsize > len || !sg)
2797 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002798
Herbert Xu89319d382008-12-15 23:26:06 -08002799 if (!hsize && i >= nfrags) {
2800 BUG_ON(fskb->len != len);
2801
2802 pos += len;
2803 nskb = skb_clone(fskb, GFP_ATOMIC);
2804 fskb = fskb->next;
2805
2806 if (unlikely(!nskb))
2807 goto err;
2808
Alexander Duyckec47ea82012-05-04 14:26:56 +00002809 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002810 if (skb_cow_head(nskb, doffset + headroom)) {
2811 kfree_skb(nskb);
2812 goto err;
2813 }
2814
Alexander Duyckec47ea82012-05-04 14:26:56 +00002815 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002816 skb_release_head_state(nskb);
2817 __skb_push(nskb, doffset);
2818 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002819 nskb = __alloc_skb(hsize + doffset + headroom,
2820 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2821 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002822
2823 if (unlikely(!nskb))
2824 goto err;
2825
2826 skb_reserve(nskb, headroom);
2827 __skb_put(nskb, doffset);
2828 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002829
2830 if (segs)
2831 tail->next = nskb;
2832 else
2833 segs = nskb;
2834 tail = nskb;
2835
Herbert Xu6f85a122008-08-15 14:55:02 -07002836 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002837 nskb->mac_len = skb->mac_len;
2838
Eric Dumazet3d3be432010-09-01 00:50:51 +00002839 /* nskb and skb might have different headroom */
2840 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2841 nskb->csum_start += skb_headroom(nskb) - headroom;
2842
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002843 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002844 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002845 nskb->transport_header = (nskb->network_header +
2846 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002847 skb_copy_from_linear_data(skb, nskb->data, doffset);
2848
Herbert Xu2f181852009-03-28 23:39:18 -07002849 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002850 continue;
2851
Herbert Xuf4c50d92006-06-22 03:02:40 -07002852 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002853 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002854 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2855 skb_put(nskb, len),
2856 len, 0);
2857 continue;
2858 }
2859
2860 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002861
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002862 skb_copy_from_linear_data_offset(skb, offset,
2863 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002864
Herbert Xu89319d382008-12-15 23:26:06 -08002865 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002866 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002867 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002868 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002869
2870 if (pos < offset) {
2871 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002872 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002873 }
2874
Herbert Xu89319d382008-12-15 23:26:06 -08002875 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002876
2877 if (pos + size <= offset + len) {
2878 i++;
2879 pos += size;
2880 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002881 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002882 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002883 }
2884
2885 frag++;
2886 }
2887
Herbert Xu89319d382008-12-15 23:26:06 -08002888 if (pos < offset + len) {
2889 struct sk_buff *fskb2 = fskb;
2890
2891 BUG_ON(pos + fskb->len != offset + len);
2892
2893 pos += fskb->len;
2894 fskb = fskb->next;
2895
2896 if (fskb2->next) {
2897 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2898 if (!fskb2)
2899 goto err;
2900 } else
2901 skb_get(fskb2);
2902
David S. Millerfbb398a2009-06-09 00:18:59 -07002903 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002904 skb_shinfo(nskb)->frag_list = fskb2;
2905 }
2906
2907skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002908 nskb->data_len = len - hsize;
2909 nskb->len += nskb->data_len;
2910 nskb->truesize += nskb->data_len;
2911 } while ((offset += len) < skb->len);
2912
2913 return segs;
2914
2915err:
2916 while ((skb = segs)) {
2917 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002918 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002919 }
2920 return ERR_PTR(err);
2921}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002922EXPORT_SYMBOL_GPL(skb_segment);
2923
Herbert Xu71d93b32008-12-15 23:42:33 -08002924int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2925{
2926 struct sk_buff *p = *head;
2927 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002928 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2929 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002930 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002931 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002932 unsigned int offset = skb_gro_offset(skb);
2933 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002934 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002935
Herbert Xu86911732009-01-29 14:19:50 +00002936 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002937 return -E2BIG;
2938
Herbert Xu9aaa1562009-05-26 18:50:33 +00002939 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002940 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002941 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002942 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002943 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002944 int i = skbinfo->nr_frags;
2945 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002946
Herbert Xu66e92fc2009-05-26 18:50:32 +00002947 offset -= headlen;
2948
2949 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002950 return -E2BIG;
2951
Herbert Xu9aaa1562009-05-26 18:50:33 +00002952 pinfo->nr_frags = nr_frags;
2953 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002954
Herbert Xu9aaa1562009-05-26 18:50:33 +00002955 frag = pinfo->frags + nr_frags;
2956 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002957 do {
2958 *--frag = *--frag2;
2959 } while (--i);
2960
2961 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002962 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002963
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002964 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002965 delta_truesize = skb->truesize -
2966 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002967
Herbert Xuf5572062009-01-14 20:40:03 -08002968 skb->truesize -= skb->data_len;
2969 skb->len -= skb->data_len;
2970 skb->data_len = 0;
2971
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002972 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08002973 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00002974 } else if (skb->head_frag) {
2975 int nr_frags = pinfo->nr_frags;
2976 skb_frag_t *frag = pinfo->frags + nr_frags;
2977 struct page *page = virt_to_head_page(skb->head);
2978 unsigned int first_size = headlen - offset;
2979 unsigned int first_offset;
2980
2981 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2982 return -E2BIG;
2983
2984 first_offset = skb->data -
2985 (unsigned char *)page_address(page) +
2986 offset;
2987
2988 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2989
2990 frag->page.p = page;
2991 frag->page_offset = first_offset;
2992 skb_frag_size_set(frag, first_size);
2993
2994 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2995 /* We dont need to clear skbinfo->nr_frags here */
2996
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002997 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00002998 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2999 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08003000 } else if (skb_gro_len(p) != pinfo->gso_size)
3001 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003002
3003 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003004 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003005 if (unlikely(!nskb))
3006 return -ENOMEM;
3007
3008 __copy_skb_header(nskb, p);
3009 nskb->mac_len = p->mac_len;
3010
3011 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003012 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003013
Herbert Xu86911732009-01-29 14:19:50 +00003014 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003015 skb_set_network_header(nskb, skb_network_offset(p));
3016 skb_set_transport_header(nskb, skb_transport_offset(p));
3017
Herbert Xu86911732009-01-29 14:19:50 +00003018 __skb_pull(p, skb_gro_offset(p));
3019 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3020 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003021
3022 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
3023 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003024 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003025 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003026 skb_header_release(p);
3027 nskb->prev = p;
3028
3029 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003030 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003031 nskb->len += p->len;
3032
3033 *head = nskb;
3034 nskb->next = p->next;
3035 p->next = NULL;
3036
3037 p = nskb;
3038
3039merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003040 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003041 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003042 unsigned int eat = offset - headlen;
3043
3044 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003045 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003046 skb->data_len -= eat;
3047 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003048 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003049 }
3050
Herbert Xu67147ba2009-05-26 18:50:22 +00003051 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003052
Herbert Xu71d93b32008-12-15 23:42:33 -08003053 p->prev->next = skb;
3054 p->prev = skb;
3055 skb_header_release(skb);
3056
Herbert Xu5d38a072009-01-04 16:13:40 -08003057done:
3058 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003059 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003060 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003061 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003062
3063 NAPI_GRO_CB(skb)->same_flow = 1;
3064 return 0;
3065}
3066EXPORT_SYMBOL_GPL(skb_gro_receive);
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068void __init skb_init(void)
3069{
3070 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3071 sizeof(struct sk_buff),
3072 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003073 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003074 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003075 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3076 (2*sizeof(struct sk_buff)) +
3077 sizeof(atomic_t),
3078 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003079 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003080 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
David Howells716ea3a2007-04-02 20:19:53 -07003083/**
3084 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3085 * @skb: Socket buffer containing the buffers to be mapped
3086 * @sg: The scatter-gather list to map into
3087 * @offset: The offset into the buffer's contents to start mapping
3088 * @len: Length of buffer space to be mapped
3089 *
3090 * Fill the specified scatter-gather list with mappings/pointers into a
3091 * region of the buffer space attached to a socket buffer.
3092 */
David S. Miller51c739d2007-10-30 21:29:29 -07003093static int
3094__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003095{
David S. Miller1a028e52007-04-27 15:21:23 -07003096 int start = skb_headlen(skb);
3097 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003098 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003099 int elt = 0;
3100
3101 if (copy > 0) {
3102 if (copy > len)
3103 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003104 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003105 elt++;
3106 if ((len -= copy) == 0)
3107 return elt;
3108 offset += copy;
3109 }
3110
3111 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003112 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003113
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003114 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003115
Eric Dumazet9e903e02011-10-18 21:00:24 +00003116 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003117 if ((copy = end - offset) > 0) {
3118 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3119
3120 if (copy > len)
3121 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003122 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003123 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003124 elt++;
3125 if (!(len -= copy))
3126 return elt;
3127 offset += copy;
3128 }
David S. Miller1a028e52007-04-27 15:21:23 -07003129 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003130 }
3131
David S. Millerfbb398a2009-06-09 00:18:59 -07003132 skb_walk_frags(skb, frag_iter) {
3133 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003134
David S. Millerfbb398a2009-06-09 00:18:59 -07003135 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003136
David S. Millerfbb398a2009-06-09 00:18:59 -07003137 end = start + frag_iter->len;
3138 if ((copy = end - offset) > 0) {
3139 if (copy > len)
3140 copy = len;
3141 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3142 copy);
3143 if ((len -= copy) == 0)
3144 return elt;
3145 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003146 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003147 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003148 }
3149 BUG_ON(len);
3150 return elt;
3151}
3152
David S. Miller51c739d2007-10-30 21:29:29 -07003153int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3154{
3155 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3156
Jens Axboec46f2332007-10-31 12:06:37 +01003157 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003158
3159 return nsg;
3160}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003161EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003162
David Howells716ea3a2007-04-02 20:19:53 -07003163/**
3164 * skb_cow_data - Check that a socket buffer's data buffers are writable
3165 * @skb: The socket buffer to check.
3166 * @tailbits: Amount of trailing space to be added
3167 * @trailer: Returned pointer to the skb where the @tailbits space begins
3168 *
3169 * Make sure that the data buffers attached to a socket buffer are
3170 * writable. If they are not, private copies are made of the data buffers
3171 * and the socket buffer is set to use these instead.
3172 *
3173 * If @tailbits is given, make sure that there is space to write @tailbits
3174 * bytes of data beyond current end of socket buffer. @trailer will be
3175 * set to point to the skb in which this space begins.
3176 *
3177 * The number of scatterlist elements required to completely map the
3178 * COW'd and extended socket buffer will be returned.
3179 */
3180int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3181{
3182 int copyflag;
3183 int elt;
3184 struct sk_buff *skb1, **skb_p;
3185
3186 /* If skb is cloned or its head is paged, reallocate
3187 * head pulling out all the pages (pages are considered not writable
3188 * at the moment even if they are anonymous).
3189 */
3190 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3191 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3192 return -ENOMEM;
3193
3194 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003195 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003196 /* A little of trouble, not enough of space for trailer.
3197 * This should not happen, when stack is tuned to generate
3198 * good frames. OK, on miss we reallocate and reserve even more
3199 * space, 128 bytes is fair. */
3200
3201 if (skb_tailroom(skb) < tailbits &&
3202 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3203 return -ENOMEM;
3204
3205 /* Voila! */
3206 *trailer = skb;
3207 return 1;
3208 }
3209
3210 /* Misery. We are in troubles, going to mincer fragments... */
3211
3212 elt = 1;
3213 skb_p = &skb_shinfo(skb)->frag_list;
3214 copyflag = 0;
3215
3216 while ((skb1 = *skb_p) != NULL) {
3217 int ntail = 0;
3218
3219 /* The fragment is partially pulled by someone,
3220 * this can happen on input. Copy it and everything
3221 * after it. */
3222
3223 if (skb_shared(skb1))
3224 copyflag = 1;
3225
3226 /* If the skb is the last, worry about trailer. */
3227
3228 if (skb1->next == NULL && tailbits) {
3229 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003230 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003231 skb_tailroom(skb1) < tailbits)
3232 ntail = tailbits + 128;
3233 }
3234
3235 if (copyflag ||
3236 skb_cloned(skb1) ||
3237 ntail ||
3238 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003239 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003240 struct sk_buff *skb2;
3241
3242 /* Fuck, we are miserable poor guys... */
3243 if (ntail == 0)
3244 skb2 = skb_copy(skb1, GFP_ATOMIC);
3245 else
3246 skb2 = skb_copy_expand(skb1,
3247 skb_headroom(skb1),
3248 ntail,
3249 GFP_ATOMIC);
3250 if (unlikely(skb2 == NULL))
3251 return -ENOMEM;
3252
3253 if (skb1->sk)
3254 skb_set_owner_w(skb2, skb1->sk);
3255
3256 /* Looking around. Are we still alive?
3257 * OK, link new skb, drop old one */
3258
3259 skb2->next = skb1->next;
3260 *skb_p = skb2;
3261 kfree_skb(skb1);
3262 skb1 = skb2;
3263 }
3264 elt++;
3265 *trailer = skb1;
3266 skb_p = &skb1->next;
3267 }
3268
3269 return elt;
3270}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003271EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003272
Eric Dumazetb1faf562010-05-31 23:44:05 -07003273static void sock_rmem_free(struct sk_buff *skb)
3274{
3275 struct sock *sk = skb->sk;
3276
3277 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3278}
3279
3280/*
3281 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3282 */
3283int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3284{
Eric Dumazet110c4332012-04-06 10:49:10 +02003285 int len = skb->len;
3286
Eric Dumazetb1faf562010-05-31 23:44:05 -07003287 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003288 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003289 return -ENOMEM;
3290
3291 skb_orphan(skb);
3292 skb->sk = sk;
3293 skb->destructor = sock_rmem_free;
3294 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3295
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003296 /* before exiting rcu section, make sure dst is refcounted */
3297 skb_dst_force(skb);
3298
Eric Dumazetb1faf562010-05-31 23:44:05 -07003299 skb_queue_tail(&sk->sk_error_queue, skb);
3300 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003301 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003302 return 0;
3303}
3304EXPORT_SYMBOL(sock_queue_err_skb);
3305
Patrick Ohlyac45f602009-02-12 05:03:37 +00003306void skb_tstamp_tx(struct sk_buff *orig_skb,
3307 struct skb_shared_hwtstamps *hwtstamps)
3308{
3309 struct sock *sk = orig_skb->sk;
3310 struct sock_exterr_skb *serr;
3311 struct sk_buff *skb;
3312 int err;
3313
3314 if (!sk)
3315 return;
3316
3317 skb = skb_clone(orig_skb, GFP_ATOMIC);
3318 if (!skb)
3319 return;
3320
3321 if (hwtstamps) {
3322 *skb_hwtstamps(skb) =
3323 *hwtstamps;
3324 } else {
3325 /*
3326 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003327 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003328 * store software time stamp
3329 */
3330 skb->tstamp = ktime_get_real();
3331 }
3332
3333 serr = SKB_EXT_ERR(skb);
3334 memset(serr, 0, sizeof(*serr));
3335 serr->ee.ee_errno = ENOMSG;
3336 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003337
Patrick Ohlyac45f602009-02-12 05:03:37 +00003338 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003339
Patrick Ohlyac45f602009-02-12 05:03:37 +00003340 if (err)
3341 kfree_skb(skb);
3342}
3343EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3344
Johannes Berg6e3e9392011-11-09 10:15:42 +01003345void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3346{
3347 struct sock *sk = skb->sk;
3348 struct sock_exterr_skb *serr;
3349 int err;
3350
3351 skb->wifi_acked_valid = 1;
3352 skb->wifi_acked = acked;
3353
3354 serr = SKB_EXT_ERR(skb);
3355 memset(serr, 0, sizeof(*serr));
3356 serr->ee.ee_errno = ENOMSG;
3357 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3358
3359 err = sock_queue_err_skb(sk, skb);
3360 if (err)
3361 kfree_skb(skb);
3362}
3363EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3364
Patrick Ohlyac45f602009-02-12 05:03:37 +00003365
Rusty Russellf35d9d82008-02-04 23:49:54 -05003366/**
3367 * skb_partial_csum_set - set up and verify partial csum values for packet
3368 * @skb: the skb to set
3369 * @start: the number of bytes after skb->data to start checksumming.
3370 * @off: the offset from start to place the checksum.
3371 *
3372 * For untrusted partially-checksummed packets, we need to make sure the values
3373 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3374 *
3375 * This function checks and sets those values and skb->ip_summed: if this
3376 * returns false you should drop the packet.
3377 */
3378bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3379{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003380 if (unlikely(start > skb_headlen(skb)) ||
3381 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003382 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3383 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003384 return false;
3385 }
3386 skb->ip_summed = CHECKSUM_PARTIAL;
3387 skb->csum_start = skb_headroom(skb) + start;
3388 skb->csum_offset = off;
3389 return true;
3390}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003391EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003392
Ben Hutchings4497b072008-06-19 16:22:28 -07003393void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3394{
Joe Perchese87cc472012-05-13 21:56:26 +00003395 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3396 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003397}
Ben Hutchings4497b072008-06-19 16:22:28 -07003398EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003399
3400void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3401{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003402 if (head_stolen) {
3403 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003404 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003405 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003406 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003407 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003408}
3409EXPORT_SYMBOL(kfree_skb_partial);
3410
3411/**
3412 * skb_try_coalesce - try to merge skb to prior one
3413 * @to: prior buffer
3414 * @from: buffer to add
3415 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003416 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003417 */
3418bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3419 bool *fragstolen, int *delta_truesize)
3420{
3421 int i, delta, len = from->len;
3422
3423 *fragstolen = false;
3424
3425 if (skb_cloned(to))
3426 return false;
3427
3428 if (len <= skb_tailroom(to)) {
3429 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3430 *delta_truesize = 0;
3431 return true;
3432 }
3433
3434 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3435 return false;
3436
3437 if (skb_headlen(from) != 0) {
3438 struct page *page;
3439 unsigned int offset;
3440
3441 if (skb_shinfo(to)->nr_frags +
3442 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3443 return false;
3444
3445 if (skb_head_is_locked(from))
3446 return false;
3447
3448 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3449
3450 page = virt_to_head_page(from->head);
3451 offset = from->data - (unsigned char *)page_address(page);
3452
3453 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3454 page, offset, skb_headlen(from));
3455 *fragstolen = true;
3456 } else {
3457 if (skb_shinfo(to)->nr_frags +
3458 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3459 return false;
3460
Weiping Panf4b549a2012-09-28 20:15:30 +00003461 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003462 }
3463
3464 WARN_ON_ONCE(delta < len);
3465
3466 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3467 skb_shinfo(from)->frags,
3468 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3469 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3470
3471 if (!skb_cloned(from))
3472 skb_shinfo(from)->nr_frags = 0;
3473
Li RongQing8ea853f2012-09-18 16:53:21 +00003474 /* if the skb is not cloned this does nothing
3475 * since we set nr_frags to 0.
3476 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003477 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3478 skb_frag_ref(from, i);
3479
3480 to->truesize += delta;
3481 to->len += len;
3482 to->data_len += len;
3483
3484 *delta_truesize = delta;
3485 return true;
3486}
3487EXPORT_SYMBOL(skb_try_coalesce);