blob: c011d7fab62d557980eb97f58159de8b47c8559b [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
148/* Allocate a new skbuff. We do this ourselves so we can fill in a few
149 * 'private' fields and also do memory statistics to find all the
150 * [BEEP] leaks.
151 *
152 */
153
154/**
David S. Millerd179cd12005-08-17 14:57:30 -0700155 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * @size: size to allocate
157 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700158 * @fclone: allocate from fclone cache instead of head cache
159 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800160 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
162 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000163 * tail room of at least size bytes. The object has a reference count
164 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
166 * Buffers may only be allocated from interrupts using a @gfp_mask of
167 * %GFP_ATOMIC.
168 */
Al Virodd0fc662005-10-07 07:46:04 +0100169struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800170 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Christoph Lametere18b8902006-12-06 20:33:20 -0800172 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800173 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct sk_buff *skb;
175 u8 *data;
176
Herbert Xu8798b3f2006-01-23 16:32:45 -0800177 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800180 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!skb)
182 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700183 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000185 /* We do our best to align skb_shared_info on a separate cache
186 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
187 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
188 * Both skb->head and skb_shared_info are cache line aligned.
189 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000190 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000191 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
192 data = kmalloc_node_track_caller(size, gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!data)
194 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000195 /* kmalloc(size) might give us more room than requested.
196 * Put skb_shared_info exactly at the end of allocated zone,
197 * to allow max possible filling before reallocation.
198 */
199 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700200 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300202 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700203 * Only clear those fields we need to clear, not those that we will
204 * actually initialise below. Hence, don't put any more fields after
205 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300206 */
207 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000208 /* Account for allocated memory : skb + skb->head */
209 skb->truesize = SKB_TRUESIZE(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 atomic_set(&skb->users, 1);
211 skb->head = data;
212 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700213 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700214 skb->end = skb->tail + size;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000215#ifdef NET_SKBUFF_DATA_USES_OFFSET
216 skb->mac_header = ~0U;
217#endif
218
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800219 /* make sure we initialize shinfo sequentially */
220 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700221 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800222 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000223 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800224
David S. Millerd179cd12005-08-17 14:57:30 -0700225 if (fclone) {
226 struct sk_buff *child = skb + 1;
227 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200229 kmemcheck_annotate_bitfield(child, flags1);
230 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700231 skb->fclone = SKB_FCLONE_ORIG;
232 atomic_set(fclone_ref, 1);
233
234 child->fclone = SKB_FCLONE_UNAVAILABLE;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236out:
237 return skb;
238nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800239 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 skb = NULL;
241 goto out;
242}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800243EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000246 * build_skb - build a network buffer
247 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000248 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000249 *
250 * Allocate a new &sk_buff. Caller provides space holding head and
251 * skb_shared_info. @data must have been allocated by kmalloc()
252 * The return is the new skb buffer.
253 * On a failure the return is %NULL, and @data is not freed.
254 * Notes :
255 * Before IO, driver allocates only data buffer where NIC put incoming frame
256 * Driver should add room at head (NET_SKB_PAD) and
257 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
258 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
259 * before giving packet to stack.
260 * RX rings only contains data buffers, not full skbs.
261 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000262struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000263{
264 struct skb_shared_info *shinfo;
265 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000266 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000267
268 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
269 if (!skb)
270 return NULL;
271
Eric Dumazetd3836f22012-04-27 00:33:38 +0000272 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000273
274 memset(skb, 0, offsetof(struct sk_buff, tail));
275 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000276 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000277 atomic_set(&skb->users, 1);
278 skb->head = data;
279 skb->data = data;
280 skb_reset_tail_pointer(skb);
281 skb->end = skb->tail + size;
282#ifdef NET_SKBUFF_DATA_USES_OFFSET
283 skb->mac_header = ~0U;
284#endif
285
286 /* make sure we initialize shinfo sequentially */
287 shinfo = skb_shinfo(skb);
288 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
289 atomic_set(&shinfo->dataref, 1);
290 kmemcheck_annotate_variable(shinfo->destructor_arg);
291
292 return skb;
293}
294EXPORT_SYMBOL(build_skb);
295
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000296struct netdev_alloc_cache {
297 struct page *page;
298 unsigned int offset;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000299 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000300};
301static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
302
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000303#define NETDEV_PAGECNT_BIAS (PAGE_SIZE / SMP_CACHE_BYTES)
304
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000305/**
Eric Dumazet6f532612012-05-18 05:12:12 +0000306 * netdev_alloc_frag - allocate a page fragment
307 * @fragsz: fragment size
308 *
309 * Allocates a frag from a page for receive buffer.
310 * Uses GFP_ATOMIC allocations.
311 */
312void *netdev_alloc_frag(unsigned int fragsz)
313{
314 struct netdev_alloc_cache *nc;
315 void *data = NULL;
316 unsigned long flags;
317
318 local_irq_save(flags);
319 nc = &__get_cpu_var(netdev_alloc_cache);
320 if (unlikely(!nc->page)) {
321refill:
322 nc->page = alloc_page(GFP_ATOMIC | __GFP_COLD);
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000323 if (unlikely(!nc->page))
324 goto end;
325recycle:
326 atomic_set(&nc->page->_count, NETDEV_PAGECNT_BIAS);
327 nc->pagecnt_bias = NETDEV_PAGECNT_BIAS;
Eric Dumazet6f532612012-05-18 05:12:12 +0000328 nc->offset = 0;
329 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000330
331 if (nc->offset + fragsz > PAGE_SIZE) {
332 /* avoid unnecessary locked operations if possible */
333 if ((atomic_read(&nc->page->_count) == nc->pagecnt_bias) ||
334 atomic_sub_and_test(nc->pagecnt_bias, &nc->page->_count))
335 goto recycle;
336 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000337 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000338
339 data = page_address(nc->page) + nc->offset;
340 nc->offset += fragsz;
341 nc->pagecnt_bias--;
342end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000343 local_irq_restore(flags);
344 return data;
345}
346EXPORT_SYMBOL(netdev_alloc_frag);
347
348/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700349 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
350 * @dev: network device to receive on
351 * @length: length to allocate
352 * @gfp_mask: get_free_pages mask, passed to alloc_skb
353 *
354 * Allocate a new &sk_buff and assign it a usage count of one. The
355 * buffer has unspecified headroom built in. Users should allocate
356 * the headroom they think they need without accounting for the
357 * built in space. The built in space is used for optimisations.
358 *
359 * %NULL is returned if there is no free memory.
360 */
361struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000362 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700363{
Eric Dumazet6f532612012-05-18 05:12:12 +0000364 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000365 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
366 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700367
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000368 if (fragsz <= PAGE_SIZE && !(gfp_mask & __GFP_WAIT)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000369 void *data = netdev_alloc_frag(fragsz);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000370
Eric Dumazet6f532612012-05-18 05:12:12 +0000371 if (likely(data)) {
372 skb = build_skb(data, fragsz);
373 if (unlikely(!skb))
374 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000375 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000376 } else {
377 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
378 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700379 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700380 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700381 skb->dev = dev;
382 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700383 return skb;
384}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800385EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Peter Zijlstra654bed12008-10-07 14:22:33 -0700387void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000388 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700389{
390 skb_fill_page_desc(skb, i, page, off, size);
391 skb->len += size;
392 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000393 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700394}
395EXPORT_SYMBOL(skb_add_rx_frag);
396
Herbert Xu27b437c2006-07-13 19:26:39 -0700397static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Herbert Xu27b437c2006-07-13 19:26:39 -0700399 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Herbert Xu27b437c2006-07-13 19:26:39 -0700401 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 do {
404 struct sk_buff *this = list;
405 list = list->next;
406 kfree_skb(this);
407 } while (list);
408}
409
Herbert Xu27b437c2006-07-13 19:26:39 -0700410static inline void skb_drop_fraglist(struct sk_buff *skb)
411{
412 skb_drop_list(&skb_shinfo(skb)->frag_list);
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void skb_clone_fraglist(struct sk_buff *skb)
416{
417 struct sk_buff *list;
418
David S. Millerfbb398a2009-06-09 00:18:59 -0700419 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 skb_get(list);
421}
422
Eric Dumazetd3836f22012-04-27 00:33:38 +0000423static void skb_free_head(struct sk_buff *skb)
424{
425 if (skb->head_frag)
426 put_page(virt_to_head_page(skb->head));
427 else
428 kfree(skb->head);
429}
430
Adrian Bunk5bba1712006-06-29 13:02:35 -0700431static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 if (!skb->cloned ||
434 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
435 &skb_shinfo(skb)->dataref)) {
436 if (skb_shinfo(skb)->nr_frags) {
437 int i;
438 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +0000439 skb_frag_unref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Shirley Maa6686f22011-07-06 12:22:12 +0000442 /*
443 * If skb buf is from userspace, we need to notify the caller
444 * the lower device DMA has done;
445 */
446 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
447 struct ubuf_info *uarg;
448
449 uarg = skb_shinfo(skb)->destructor_arg;
450 if (uarg->callback)
451 uarg->callback(uarg);
452 }
453
David S. Miller21dc3302010-08-23 00:13:46 -0700454 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 skb_drop_fraglist(skb);
456
Eric Dumazetd3836f22012-04-27 00:33:38 +0000457 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459}
460
461/*
462 * Free an skbuff by memory without cleaning the state.
463 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800464static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
David S. Millerd179cd12005-08-17 14:57:30 -0700466 struct sk_buff *other;
467 atomic_t *fclone_ref;
468
David S. Millerd179cd12005-08-17 14:57:30 -0700469 switch (skb->fclone) {
470 case SKB_FCLONE_UNAVAILABLE:
471 kmem_cache_free(skbuff_head_cache, skb);
472 break;
473
474 case SKB_FCLONE_ORIG:
475 fclone_ref = (atomic_t *) (skb + 2);
476 if (atomic_dec_and_test(fclone_ref))
477 kmem_cache_free(skbuff_fclone_cache, skb);
478 break;
479
480 case SKB_FCLONE_CLONE:
481 fclone_ref = (atomic_t *) (skb + 1);
482 other = skb - 1;
483
484 /* The clone portion is available for
485 * fast-cloning again.
486 */
487 skb->fclone = SKB_FCLONE_UNAVAILABLE;
488
489 if (atomic_dec_and_test(fclone_ref))
490 kmem_cache_free(skbuff_fclone_cache, other);
491 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700495static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Eric Dumazetadf30902009-06-02 05:19:30 +0000497 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498#ifdef CONFIG_XFRM
499 secpath_put(skb->sp);
500#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700501 if (skb->destructor) {
502 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 skb->destructor(skb);
504 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000505#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700506 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100507#endif
508#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800509 nf_conntrack_put_reasm(skb->nfct_reasm);
510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511#ifdef CONFIG_BRIDGE_NETFILTER
512 nf_bridge_put(skb->nf_bridge);
513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/* XXX: IS this still necessary? - JHS */
515#ifdef CONFIG_NET_SCHED
516 skb->tc_index = 0;
517#ifdef CONFIG_NET_CLS_ACT
518 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#endif
520#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700521}
522
523/* Free everything but the sk_buff shell. */
524static void skb_release_all(struct sk_buff *skb)
525{
526 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800527 skb_release_data(skb);
528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Herbert Xu2d4baff2007-11-26 23:11:19 +0800530/**
531 * __kfree_skb - private function
532 * @skb: buffer
533 *
534 * Free an sk_buff. Release anything attached to the buffer.
535 * Clean the state. This is an internal helper function. Users should
536 * always call kfree_skb
537 */
538
539void __kfree_skb(struct sk_buff *skb)
540{
541 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 kfree_skbmem(skb);
543}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800544EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800547 * kfree_skb - free an sk_buff
548 * @skb: buffer to free
549 *
550 * Drop a reference to the buffer and free it if the usage count has
551 * hit zero.
552 */
553void kfree_skb(struct sk_buff *skb)
554{
555 if (unlikely(!skb))
556 return;
557 if (likely(atomic_read(&skb->users) == 1))
558 smp_rmb();
559 else if (likely(!atomic_dec_and_test(&skb->users)))
560 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000561 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800562 __kfree_skb(skb);
563}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800564EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800565
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700566/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000567 * consume_skb - free an skbuff
568 * @skb: buffer to free
569 *
570 * Drop a ref to the buffer and free it if the usage count has hit zero
571 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
572 * is being dropped after a failure and notes that
573 */
574void consume_skb(struct sk_buff *skb)
575{
576 if (unlikely(!skb))
577 return;
578 if (likely(atomic_read(&skb->users) == 1))
579 smp_rmb();
580 else if (likely(!atomic_dec_and_test(&skb->users)))
581 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900582 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000583 __kfree_skb(skb);
584}
585EXPORT_SYMBOL(consume_skb);
586
587/**
Andy Fleming3d153a72011-10-13 04:33:54 +0000588 * skb_recycle - clean up an skb for reuse
589 * @skb: buffer
590 *
591 * Recycles the skb to be reused as a receive buffer. This
592 * function does any necessary reference count dropping, and
593 * cleans up the skbuff as if it just came from __alloc_skb().
594 */
595void skb_recycle(struct sk_buff *skb)
596{
597 struct skb_shared_info *shinfo;
598
599 skb_release_head_state(skb);
600
601 shinfo = skb_shinfo(skb);
602 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
603 atomic_set(&shinfo->dataref, 1);
604
605 memset(skb, 0, offsetof(struct sk_buff, tail));
606 skb->data = skb->head + NET_SKB_PAD;
607 skb_reset_tail_pointer(skb);
608}
609EXPORT_SYMBOL(skb_recycle);
610
611/**
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700612 * skb_recycle_check - check if skb can be reused for receive
613 * @skb: buffer
614 * @skb_size: minimum receive buffer size
615 *
616 * Checks that the skb passed in is not shared or cloned, and
617 * that it is linear and its head portion at least as large as
618 * skb_size so that it can be recycled as a receive buffer.
619 * If these conditions are met, this function does any necessary
620 * reference count dropping and cleans up the skbuff as if it
621 * just came from __alloc_skb().
622 */
Changli Gao5b0daa32010-05-29 00:12:13 -0700623bool skb_recycle_check(struct sk_buff *skb, int skb_size)
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700624{
Andy Fleming3d153a72011-10-13 04:33:54 +0000625 if (!skb_is_recycleable(skb, skb_size))
Changli Gao5b0daa32010-05-29 00:12:13 -0700626 return false;
Anton Vorontsove84af6d2009-11-10 14:11:01 +0000627
Andy Fleming3d153a72011-10-13 04:33:54 +0000628 skb_recycle(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700629
Changli Gao5b0daa32010-05-29 00:12:13 -0700630 return true;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700631}
632EXPORT_SYMBOL(skb_recycle_check);
633
Herbert Xudec18812007-10-14 00:37:30 -0700634static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
635{
636 new->tstamp = old->tstamp;
637 new->dev = old->dev;
638 new->transport_header = old->transport_header;
639 new->network_header = old->network_header;
640 new->mac_header = old->mac_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000641 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000642 new->rxhash = old->rxhash;
Changli Gao6461be32011-08-19 04:44:18 +0000643 new->ooo_okay = old->ooo_okay;
Tom Herbertbdeab992011-08-14 19:45:55 +0000644 new->l4_rxhash = old->l4_rxhash;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000645 new->no_fcs = old->no_fcs;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700646#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700647 new->sp = secpath_get(old->sp);
648#endif
649 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000650 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700651 new->local_df = old->local_df;
652 new->pkt_type = old->pkt_type;
653 new->ip_summed = old->ip_summed;
654 skb_copy_queue_mapping(new, old);
655 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000656#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700657 new->ipvs_property = old->ipvs_property;
658#endif
659 new->protocol = old->protocol;
660 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800661 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700662 __nf_copy(new, old);
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000663#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Herbert Xudec18812007-10-14 00:37:30 -0700664 new->nf_trace = old->nf_trace;
665#endif
666#ifdef CONFIG_NET_SCHED
667 new->tc_index = old->tc_index;
668#ifdef CONFIG_NET_CLS_ACT
669 new->tc_verd = old->tc_verd;
670#endif
671#endif
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700672 new->vlan_tci = old->vlan_tci;
673
Herbert Xudec18812007-10-14 00:37:30 -0700674 skb_copy_secmark(new, old);
675}
676
Herbert Xu82c49a32009-05-22 22:11:37 +0000677/*
678 * You should not add any new code to this function. Add it to
679 * __copy_skb_header above instead.
680 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700681static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#define C(x) n->x = skb->x
684
685 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700687 __copy_skb_header(n, skb);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 C(len);
690 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700691 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700692 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800693 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 C(tail);
697 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800698 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000699 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800700 C(data);
701 C(truesize);
702 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 atomic_inc(&(skb_shinfo(skb)->dataref));
705 skb->cloned = 1;
706
707 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700708#undef C
709}
710
711/**
712 * skb_morph - morph one skb into another
713 * @dst: the skb to receive the contents
714 * @src: the skb to supply the contents
715 *
716 * This is identical to skb_clone except that the target skb is
717 * supplied by the user.
718 *
719 * The target skb is returned upon exit.
720 */
721struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
722{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800723 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700724 return __skb_clone(dst, src);
725}
726EXPORT_SYMBOL_GPL(skb_morph);
727
Ben Hutchings2c530402012-07-10 10:55:09 +0000728/**
729 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000730 * @skb: the skb to modify
731 * @gfp_mask: allocation priority
732 *
733 * This must be called on SKBTX_DEV_ZEROCOPY skb.
734 * It will copy all frags into kernel and drop the reference
735 * to userspace pages.
736 *
737 * If this function is called from an interrupt gfp_mask() must be
738 * %GFP_ATOMIC.
739 *
740 * Returns 0 on success or a negative error code on failure
741 * to allocate kernel memory to copy to.
742 */
743int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000744{
745 int i;
746 int num_frags = skb_shinfo(skb)->nr_frags;
747 struct page *page, *head = NULL;
748 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
749
750 for (i = 0; i < num_frags; i++) {
751 u8 *vaddr;
752 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
753
Krishna Kumar02756ed2012-07-17 02:05:29 +0000754 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000755 if (!page) {
756 while (head) {
757 struct page *next = (struct page *)head->private;
758 put_page(head);
759 head = next;
760 }
761 return -ENOMEM;
762 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200763 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000764 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000765 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200766 kunmap_atomic(vaddr);
Shirley Maa6686f22011-07-06 12:22:12 +0000767 page->private = (unsigned long)head;
768 head = page;
769 }
770
771 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000772 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000773 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000774
775 uarg->callback(uarg);
776
777 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000778 for (i = num_frags - 1; i >= 0; i--) {
779 __skb_fill_page_desc(skb, i, head, 0,
780 skb_shinfo(skb)->frags[i].size);
Shirley Maa6686f22011-07-06 12:22:12 +0000781 head = (struct page *)head->private;
782 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000783
784 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000785 return 0;
786}
787
788
Herbert Xue0053ec2007-10-14 00:37:52 -0700789/**
790 * skb_clone - duplicate an sk_buff
791 * @skb: buffer to clone
792 * @gfp_mask: allocation priority
793 *
794 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
795 * copies share the same packet data but not structure. The new
796 * buffer has a reference count of 1. If the allocation fails the
797 * function returns %NULL otherwise the new buffer is returned.
798 *
799 * If this function is called from an interrupt gfp_mask() must be
800 * %GFP_ATOMIC.
801 */
802
803struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
804{
805 struct sk_buff *n;
806
Shirley Maa6686f22011-07-06 12:22:12 +0000807 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
808 if (skb_copy_ubufs(skb, gfp_mask))
809 return NULL;
810 }
811
Herbert Xue0053ec2007-10-14 00:37:52 -0700812 n = skb + 1;
813 if (skb->fclone == SKB_FCLONE_ORIG &&
814 n->fclone == SKB_FCLONE_UNAVAILABLE) {
815 atomic_t *fclone_ref = (atomic_t *) (n + 1);
816 n->fclone = SKB_FCLONE_CLONE;
817 atomic_inc(fclone_ref);
818 } else {
819 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
820 if (!n)
821 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200822
823 kmemcheck_annotate_bitfield(n, flags1);
824 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700825 n->fclone = SKB_FCLONE_UNAVAILABLE;
826 }
827
828 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800830EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
833{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700834#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * Shift between the two data areas in bytes
837 */
838 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700839#endif
Herbert Xudec18812007-10-14 00:37:30 -0700840
841 __copy_skb_header(new, old);
842
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700843#ifndef NET_SKBUFF_DATA_USES_OFFSET
844 /* {transport,network,mac}_header are relative to skb->head */
845 new->transport_header += offset;
846 new->network_header += offset;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000847 if (skb_mac_header_was_set(new))
848 new->mac_header += offset;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700849#endif
Herbert Xu79671682006-06-22 02:40:14 -0700850 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
851 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
852 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/**
856 * skb_copy - create private copy of an sk_buff
857 * @skb: buffer to copy
858 * @gfp_mask: allocation priority
859 *
860 * Make a copy of both an &sk_buff and its data. This is used when the
861 * caller wishes to modify the data and needs a private copy of the
862 * data to alter. Returns %NULL on failure or the pointer to the buffer
863 * on success. The returned buffer has a reference count of 1.
864 *
865 * As by-product this function converts non-linear &sk_buff to linear
866 * one, so that &sk_buff becomes completely private and caller is allowed
867 * to modify all the data of returned buffer. This means that this
868 * function is not recommended for use in circumstances when only
869 * header is going to be modified. Use pskb_copy() instead.
870 */
871
Al Virodd0fc662005-10-07 07:46:04 +0100872struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000874 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000875 unsigned int size = skb_end_offset(skb) + skb->data_len;
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000876 struct sk_buff *n = alloc_skb(size, gfp_mask);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!n)
879 return NULL;
880
881 /* Set the data pointer */
882 skb_reserve(n, headerlen);
883 /* Set the tail pointer and length */
884 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
887 BUG();
888
889 copy_skb_header(n, skb);
890 return n;
891}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800892EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894/**
Eric Dumazet117632e2011-12-03 21:39:53 +0000895 * __pskb_copy - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000897 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * @gfp_mask: allocation priority
899 *
900 * Make a copy of both an &sk_buff and part of its data, located
901 * in header. Fragmented data remain shared. This is used when
902 * the caller wishes to modify only header of &sk_buff and needs
903 * private copy of the header to alter. Returns %NULL on failure
904 * or the pointer to the buffer on success.
905 * The returned buffer has a reference count of 1.
906 */
907
Eric Dumazet117632e2011-12-03 21:39:53 +0000908struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Eric Dumazet117632e2011-12-03 21:39:53 +0000910 unsigned int size = skb_headlen(skb) + headroom;
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000911 struct sk_buff *n = alloc_skb(size, gfp_mask);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (!n)
914 goto out;
915
916 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000917 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Set the tail pointer and length */
919 skb_put(n, skb_headlen(skb));
920 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300921 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Herbert Xu25f484a2006-11-07 14:57:15 -0800923 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 n->data_len = skb->data_len;
925 n->len = skb->len;
926
927 if (skb_shinfo(skb)->nr_frags) {
928 int i;
929
Shirley Maa6686f22011-07-06 12:22:12 +0000930 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
931 if (skb_copy_ubufs(skb, gfp_mask)) {
Dan Carpenter15110222011-07-19 22:51:49 +0000932 kfree_skb(n);
933 n = NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000934 goto out;
935 }
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
938 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +0000939 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 skb_shinfo(n)->nr_frags = i;
942 }
943
David S. Miller21dc3302010-08-23 00:13:46 -0700944 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
946 skb_clone_fraglist(n);
947 }
948
949 copy_skb_header(n, skb);
950out:
951 return n;
952}
Eric Dumazet117632e2011-12-03 21:39:53 +0000953EXPORT_SYMBOL(__pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955/**
956 * pskb_expand_head - reallocate header of &sk_buff
957 * @skb: buffer to reallocate
958 * @nhead: room to add at head
959 * @ntail: room to add at tail
960 * @gfp_mask: allocation priority
961 *
962 * Expands (or creates identical copy, if &nhead and &ntail are zero)
963 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
964 * reference count of 1. Returns zero in the case of success or error,
965 * if expansion failed. In the last case, &sk_buff is not changed.
966 *
967 * All the pointers pointing into skb header may change and must be
968 * reloaded after call to this function.
969 */
970
Victor Fusco86a76ca2005-07-08 14:57:47 -0700971int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100972 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 int i;
975 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +0000976 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 long off;
978
Herbert Xu4edd87a2008-10-01 07:09:38 -0700979 BUG_ON(nhead < 0);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (skb_shared(skb))
982 BUG();
983
984 size = SKB_DATA_ALIGN(size);
985
Eric Dumazet87151b82012-04-10 20:08:39 +0000986 data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
987 gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!data)
989 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +0000990 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000993 * optimized for the cases when header is void.
994 */
995 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
996
997 memcpy((struct skb_shared_info *)(data + size),
998 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +0000999 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Alexander Duyck3e245912012-05-04 14:26:51 +00001001 /*
1002 * if shinfo is shared we must drop the old head gracefully, but if it
1003 * is not we can just drop the old head and let the existing refcount
1004 * be since all we did is relocate the values
1005 */
1006 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001007 /* copy this zero copy skb frags */
1008 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
1009 if (skb_copy_ubufs(skb, gfp_mask))
1010 goto nofrags;
1011 }
Eric Dumazet1fd63042010-09-02 23:09:32 +00001012 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001013 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Dumazet1fd63042010-09-02 23:09:32 +00001015 if (skb_has_frag_list(skb))
1016 skb_clone_fraglist(skb);
1017
1018 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001019 } else {
1020 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 off = (data + nhead) - skb->head;
1023
1024 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001025 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001027#ifdef NET_SKBUFF_DATA_USES_OFFSET
1028 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001029 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001030#else
1031 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001032#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001033 /* {transport,network,mac}_header and tail are relative to skb->head */
1034 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001035 skb->transport_header += off;
1036 skb->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001037 if (skb_mac_header_was_set(skb))
1038 skb->mac_header += off;
Andrea Shepard00c5a982010-07-22 09:12:35 +00001039 /* Only adjust this if it actually is csum_start rather than csum */
1040 if (skb->ip_summed == CHECKSUM_PARTIAL)
1041 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001043 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 skb->nohdr = 0;
1045 atomic_set(&skb_shinfo(skb)->dataref, 1);
1046 return 0;
1047
Shirley Maa6686f22011-07-06 12:22:12 +00001048nofrags:
1049 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050nodata:
1051 return -ENOMEM;
1052}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001053EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055/* Make private copy of skb with writable head and some headroom */
1056
1057struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1058{
1059 struct sk_buff *skb2;
1060 int delta = headroom - skb_headroom(skb);
1061
1062 if (delta <= 0)
1063 skb2 = pskb_copy(skb, GFP_ATOMIC);
1064 else {
1065 skb2 = skb_clone(skb, GFP_ATOMIC);
1066 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1067 GFP_ATOMIC)) {
1068 kfree_skb(skb2);
1069 skb2 = NULL;
1070 }
1071 }
1072 return skb2;
1073}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001074EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076/**
1077 * skb_copy_expand - copy and expand sk_buff
1078 * @skb: buffer to copy
1079 * @newheadroom: new free bytes at head
1080 * @newtailroom: new free bytes at tail
1081 * @gfp_mask: allocation priority
1082 *
1083 * Make a copy of both an &sk_buff and its data and while doing so
1084 * allocate additional space.
1085 *
1086 * This is used when the caller wishes to modify the data and needs a
1087 * private copy of the data to alter as well as more space for new fields.
1088 * Returns %NULL on failure or the pointer to the buffer
1089 * on success. The returned buffer has a reference count of 1.
1090 *
1091 * You must pass %GFP_ATOMIC as the allocation priority if this function
1092 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 */
1094struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001095 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001096 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 /*
1099 * Allocate the copy buffer
1100 */
1101 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
1102 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001103 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -07001105 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (!n)
1108 return NULL;
1109
1110 skb_reserve(n, newheadroom);
1111
1112 /* Set the tail pointer and length */
1113 skb_put(n, skb->len);
1114
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001115 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 head_copy_off = 0;
1117 if (newheadroom <= head_copy_len)
1118 head_copy_len = newheadroom;
1119 else
1120 head_copy_off = newheadroom - head_copy_len;
1121
1122 /* Copy the linear header and data. */
1123 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1124 skb->len + head_copy_len))
1125 BUG();
1126
1127 copy_skb_header(n, skb);
1128
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001129 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -07001130 if (n->ip_summed == CHECKSUM_PARTIAL)
1131 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -07001132#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001133 n->transport_header += off;
1134 n->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +00001135 if (skb_mac_header_was_set(skb))
1136 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -07001137#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return n;
1140}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001141EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143/**
1144 * skb_pad - zero pad the tail of an skb
1145 * @skb: buffer to pad
1146 * @pad: space to pad
1147 *
1148 * Ensure that a buffer is followed by a padding area that is zero
1149 * filled. Used by network drivers which may DMA or transfer data
1150 * beyond the buffer end onto the wire.
1151 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001152 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001154
Herbert Xu5b057c62006-06-23 02:06:41 -07001155int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Herbert Xu5b057c62006-06-23 02:06:41 -07001157 int err;
1158 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001161 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001165
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001166 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001167 if (likely(skb_cloned(skb) || ntail > 0)) {
1168 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1169 if (unlikely(err))
1170 goto free_skb;
1171 }
1172
1173 /* FIXME: The use of this function with non-linear skb's really needs
1174 * to be audited.
1175 */
1176 err = skb_linearize(skb);
1177 if (unlikely(err))
1178 goto free_skb;
1179
1180 memset(skb->data + skb->len, 0, pad);
1181 return 0;
1182
1183free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001185 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001186}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001187EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001188
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001189/**
1190 * skb_put - add data to a buffer
1191 * @skb: buffer to use
1192 * @len: amount of data to add
1193 *
1194 * This function extends the used data area of the buffer. If this would
1195 * exceed the total buffer size the kernel will panic. A pointer to the
1196 * first byte of the extra data is returned.
1197 */
1198unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1199{
1200 unsigned char *tmp = skb_tail_pointer(skb);
1201 SKB_LINEAR_ASSERT(skb);
1202 skb->tail += len;
1203 skb->len += len;
1204 if (unlikely(skb->tail > skb->end))
1205 skb_over_panic(skb, len, __builtin_return_address(0));
1206 return tmp;
1207}
1208EXPORT_SYMBOL(skb_put);
1209
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001210/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001211 * skb_push - add data to the start of a buffer
1212 * @skb: buffer to use
1213 * @len: amount of data to add
1214 *
1215 * This function extends the used data area of the buffer at the buffer
1216 * start. If this would exceed the total buffer headroom the kernel will
1217 * panic. A pointer to the first byte of the extra data is returned.
1218 */
1219unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1220{
1221 skb->data -= len;
1222 skb->len += len;
1223 if (unlikely(skb->data<skb->head))
1224 skb_under_panic(skb, len, __builtin_return_address(0));
1225 return skb->data;
1226}
1227EXPORT_SYMBOL(skb_push);
1228
1229/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001230 * skb_pull - remove data from the start of a buffer
1231 * @skb: buffer to use
1232 * @len: amount of data to remove
1233 *
1234 * This function removes data from the start of a buffer, returning
1235 * the memory to the headroom. A pointer to the next data in the buffer
1236 * is returned. Once the data has been pulled future pushes will overwrite
1237 * the old data.
1238 */
1239unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1240{
David S. Miller47d29642010-05-02 02:21:44 -07001241 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001242}
1243EXPORT_SYMBOL(skb_pull);
1244
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001245/**
1246 * skb_trim - remove end from a buffer
1247 * @skb: buffer to alter
1248 * @len: new length
1249 *
1250 * Cut the length of a buffer down by removing data from the tail. If
1251 * the buffer is already under the length specified it is not modified.
1252 * The skb must be linear.
1253 */
1254void skb_trim(struct sk_buff *skb, unsigned int len)
1255{
1256 if (skb->len > len)
1257 __skb_trim(skb, len);
1258}
1259EXPORT_SYMBOL(skb_trim);
1260
Herbert Xu3cc0e872006-06-09 16:13:38 -07001261/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
1263
Herbert Xu3cc0e872006-06-09 16:13:38 -07001264int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Herbert Xu27b437c2006-07-13 19:26:39 -07001266 struct sk_buff **fragp;
1267 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int offset = skb_headlen(skb);
1269 int nfrags = skb_shinfo(skb)->nr_frags;
1270 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001271 int err;
1272
1273 if (skb_cloned(skb) &&
1274 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001277 i = 0;
1278 if (offset >= len)
1279 goto drop_pages;
1280
1281 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001282 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001283
1284 if (end < len) {
1285 offset = end;
1286 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001288
Eric Dumazet9e903e02011-10-18 21:00:24 +00001289 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001290
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001291drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001292 skb_shinfo(skb)->nr_frags = i;
1293
1294 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001295 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001296
David S. Miller21dc3302010-08-23 00:13:46 -07001297 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001298 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001299 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Herbert Xu27b437c2006-07-13 19:26:39 -07001302 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1303 fragp = &frag->next) {
1304 int end = offset + frag->len;
1305
1306 if (skb_shared(frag)) {
1307 struct sk_buff *nfrag;
1308
1309 nfrag = skb_clone(frag, GFP_ATOMIC);
1310 if (unlikely(!nfrag))
1311 return -ENOMEM;
1312
1313 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001314 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001315 frag = nfrag;
1316 *fragp = frag;
1317 }
1318
1319 if (end < len) {
1320 offset = end;
1321 continue;
1322 }
1323
1324 if (end > len &&
1325 unlikely((err = pskb_trim(frag, len - offset))))
1326 return err;
1327
1328 if (frag->next)
1329 skb_drop_list(&frag->next);
1330 break;
1331 }
1332
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001333done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001334 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 skb->data_len -= skb->len - len;
1336 skb->len = len;
1337 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001338 skb->len = len;
1339 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001340 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 return 0;
1344}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001345EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347/**
1348 * __pskb_pull_tail - advance tail of skb header
1349 * @skb: buffer to reallocate
1350 * @delta: number of bytes to advance tail
1351 *
1352 * The function makes a sense only on a fragmented &sk_buff,
1353 * it expands header moving its tail forward and copying necessary
1354 * data from fragmented part.
1355 *
1356 * &sk_buff MUST have reference count of 1.
1357 *
1358 * Returns %NULL (and &sk_buff does not change) if pull failed
1359 * or value of new tail of skb in the case of success.
1360 *
1361 * All the pointers pointing into skb header may change and must be
1362 * reloaded after call to this function.
1363 */
1364
1365/* Moves tail of skb head forward, copying data from fragmented part,
1366 * when it is necessary.
1367 * 1. It may fail due to malloc failure.
1368 * 2. It may change skb pointers.
1369 *
1370 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1371 */
1372unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1373{
1374 /* If skb has not enough free space at tail, get new one
1375 * plus 128 bytes for future expansions. If we have enough
1376 * room at tail, reallocate without expansion only if skb is cloned.
1377 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001378 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (eat > 0 || skb_cloned(skb)) {
1381 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1382 GFP_ATOMIC))
1383 return NULL;
1384 }
1385
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001386 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 BUG();
1388
1389 /* Optimization: no fragments, no reasons to preestimate
1390 * size of pulled pages. Superb.
1391 */
David S. Miller21dc3302010-08-23 00:13:46 -07001392 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 goto pull_pages;
1394
1395 /* Estimate size of pulled pages. */
1396 eat = delta;
1397 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001398 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1399
1400 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001402 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 /* If we need update frag list, we are in troubles.
1406 * Certainly, it possible to add an offset to skb data,
1407 * but taking into account that pulling is expected to
1408 * be very rare operation, it is worth to fight against
1409 * further bloating skb head and crucify ourselves here instead.
1410 * Pure masohism, indeed. 8)8)
1411 */
1412 if (eat) {
1413 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1414 struct sk_buff *clone = NULL;
1415 struct sk_buff *insp = NULL;
1416
1417 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001418 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 if (list->len <= eat) {
1421 /* Eaten as whole. */
1422 eat -= list->len;
1423 list = list->next;
1424 insp = list;
1425 } else {
1426 /* Eaten partially. */
1427
1428 if (skb_shared(list)) {
1429 /* Sucks! We need to fork list. :-( */
1430 clone = skb_clone(list, GFP_ATOMIC);
1431 if (!clone)
1432 return NULL;
1433 insp = list->next;
1434 list = clone;
1435 } else {
1436 /* This may be pulled without
1437 * problems. */
1438 insp = list;
1439 }
1440 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001441 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return NULL;
1443 }
1444 break;
1445 }
1446 } while (eat);
1447
1448 /* Free pulled out fragments. */
1449 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1450 skb_shinfo(skb)->frag_list = list->next;
1451 kfree_skb(list);
1452 }
1453 /* And insert new clone at head. */
1454 if (clone) {
1455 clone->next = list;
1456 skb_shinfo(skb)->frag_list = clone;
1457 }
1458 }
1459 /* Success! Now we may commit changes to skb data. */
1460
1461pull_pages:
1462 eat = delta;
1463 k = 0;
1464 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001465 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1466
1467 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001468 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001469 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 } else {
1471 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1472 if (eat) {
1473 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001474 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 eat = 0;
1476 }
1477 k++;
1478 }
1479 }
1480 skb_shinfo(skb)->nr_frags = k;
1481
1482 skb->tail += delta;
1483 skb->data_len -= delta;
1484
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001485 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001487EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Eric Dumazet22019b12011-07-29 18:37:31 +00001489/**
1490 * skb_copy_bits - copy bits from skb to kernel buffer
1491 * @skb: source skb
1492 * @offset: offset in source
1493 * @to: destination buffer
1494 * @len: number of bytes to copy
1495 *
1496 * Copy the specified number of bytes from the source skb to the
1497 * destination buffer.
1498 *
1499 * CAUTION ! :
1500 * If its prototype is ever changed,
1501 * check arch/{*}/net/{*}.S files,
1502 * since it is called from BPF assembly code.
1503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1505{
David S. Miller1a028e52007-04-27 15:21:23 -07001506 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001507 struct sk_buff *frag_iter;
1508 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (offset > (int)skb->len - len)
1511 goto fault;
1512
1513 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001514 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (copy > len)
1516 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001517 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if ((len -= copy) == 0)
1519 return 0;
1520 offset += copy;
1521 to += copy;
1522 }
1523
1524 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001525 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001526 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001528 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001529
Eric Dumazet51c56b02012-04-05 11:35:15 +02001530 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if ((copy = end - offset) > 0) {
1532 u8 *vaddr;
1533
1534 if (copy > len)
1535 copy = len;
1536
Eric Dumazet51c56b02012-04-05 11:35:15 +02001537 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001539 vaddr + f->page_offset + offset - start,
1540 copy);
1541 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if ((len -= copy) == 0)
1544 return 0;
1545 offset += copy;
1546 to += copy;
1547 }
David S. Miller1a028e52007-04-27 15:21:23 -07001548 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
David S. Millerfbb398a2009-06-09 00:18:59 -07001551 skb_walk_frags(skb, frag_iter) {
1552 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
David S. Millerfbb398a2009-06-09 00:18:59 -07001554 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
David S. Millerfbb398a2009-06-09 00:18:59 -07001556 end = start + frag_iter->len;
1557 if ((copy = end - offset) > 0) {
1558 if (copy > len)
1559 copy = len;
1560 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1561 goto fault;
1562 if ((len -= copy) == 0)
1563 return 0;
1564 offset += copy;
1565 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001567 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Shirley Maa6686f22011-07-06 12:22:12 +00001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!len)
1571 return 0;
1572
1573fault:
1574 return -EFAULT;
1575}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001576EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jens Axboe9c55e012007-11-06 23:30:13 -08001578/*
1579 * Callback from splice_to_pipe(), if we need to release some pages
1580 * at the end of the spd in case we error'ed out in filling the pipe.
1581 */
1582static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1583{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001584 put_page(spd->pages[i]);
1585}
Jens Axboe9c55e012007-11-06 23:30:13 -08001586
David S. Millera108d5f2012-04-23 23:06:11 -04001587static struct page *linear_to_page(struct page *page, unsigned int *len,
1588 unsigned int *offset,
1589 struct sk_buff *skb, struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001590{
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001591 struct page *p = sk->sk_sndmsg_page;
1592 unsigned int off;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001593
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001594 if (!p) {
1595new_page:
1596 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1597 if (!p)
1598 return NULL;
1599
1600 off = sk->sk_sndmsg_off = 0;
1601 /* hold one ref to this page until it's full */
1602 } else {
1603 unsigned int mlen;
1604
Eric Dumazete66e9a32012-04-19 09:38:17 +00001605 /* If we are the only user of the page, we can reset offset */
1606 if (page_count(p) == 1)
1607 sk->sk_sndmsg_off = 0;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001608 off = sk->sk_sndmsg_off;
1609 mlen = PAGE_SIZE - off;
1610 if (mlen < 64 && mlen < *len) {
1611 put_page(p);
1612 goto new_page;
1613 }
1614
1615 *len = min_t(unsigned int, *len, mlen);
1616 }
1617
1618 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1619 sk->sk_sndmsg_off += *len;
1620 *offset = off;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001621
1622 return p;
Jens Axboe9c55e012007-11-06 23:30:13 -08001623}
1624
Eric Dumazet41c73a02012-04-22 12:26:16 +00001625static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1626 struct page *page,
1627 unsigned int offset)
1628{
1629 return spd->nr_pages &&
1630 spd->pages[spd->nr_pages - 1] == page &&
1631 (spd->partial[spd->nr_pages - 1].offset +
1632 spd->partial[spd->nr_pages - 1].len == offset);
1633}
1634
Jens Axboe9c55e012007-11-06 23:30:13 -08001635/*
1636 * Fill page/offset/length into spd, if it can hold more pages.
1637 */
David S. Millera108d5f2012-04-23 23:06:11 -04001638static bool spd_fill_page(struct splice_pipe_desc *spd,
1639 struct pipe_inode_info *pipe, struct page *page,
1640 unsigned int *len, unsigned int offset,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001641 struct sk_buff *skb, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001642 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001643{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001644 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001645 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001646
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001647 if (linear) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001648 page = linear_to_page(page, len, &offset, skb, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001649 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001650 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001651 }
1652 if (spd_can_coalesce(spd, page, offset)) {
1653 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001654 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001655 }
1656 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001657 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001658 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001659 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001660 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001661
David S. Millera108d5f2012-04-23 23:06:11 -04001662 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001663}
1664
Octavian Purdila2870c432008-07-15 00:49:11 -07001665static inline void __segment_seek(struct page **page, unsigned int *poff,
1666 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001667{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001668 unsigned long n;
1669
Octavian Purdila2870c432008-07-15 00:49:11 -07001670 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001671 n = *poff / PAGE_SIZE;
1672 if (n)
1673 *page = nth_page(*page, n);
1674
Octavian Purdila2870c432008-07-15 00:49:11 -07001675 *poff = *poff % PAGE_SIZE;
1676 *plen -= off;
1677}
Jens Axboe9c55e012007-11-06 23:30:13 -08001678
David S. Millera108d5f2012-04-23 23:06:11 -04001679static bool __splice_segment(struct page *page, unsigned int poff,
1680 unsigned int plen, unsigned int *off,
1681 unsigned int *len, struct sk_buff *skb,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001682 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001683 struct sock *sk,
1684 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001685{
1686 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001687 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001688
1689 /* skip this segment if already processed */
1690 if (*off >= plen) {
1691 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001692 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001693 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001694
Octavian Purdila2870c432008-07-15 00:49:11 -07001695 /* ignore any bits we already processed */
1696 if (*off) {
1697 __segment_seek(&page, &poff, &plen, *off);
1698 *off = 0;
1699 }
1700
1701 do {
1702 unsigned int flen = min(*len, plen);
1703
1704 /* the linear region may spread across several pages */
1705 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1706
Jens Axboe35f3d142010-05-20 10:43:18 +02001707 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
David S. Millera108d5f2012-04-23 23:06:11 -04001708 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001709
1710 __segment_seek(&page, &poff, &plen, flen);
1711 *len -= flen;
1712
1713 } while (*len && plen);
1714
David S. Millera108d5f2012-04-23 23:06:11 -04001715 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001716}
1717
1718/*
David S. Millera108d5f2012-04-23 23:06:11 -04001719 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001720 * pipe is full or if we already spliced the requested length.
1721 */
David S. Millera108d5f2012-04-23 23:06:11 -04001722static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1723 unsigned int *offset, unsigned int *len,
1724 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001725{
1726 int seg;
1727
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001728 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001729 * If skb->head_frag is set, this 'linear' part is backed by a
1730 * fragment, and if the head is not shared with any clones then
1731 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001732 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001733 if (__splice_segment(virt_to_page(skb->data),
1734 (unsigned long) skb->data & (PAGE_SIZE - 1),
1735 skb_headlen(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001736 offset, len, skb, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001737 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001738 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001739 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001740
1741 /*
1742 * then map the fragments
1743 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001744 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1745 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1746
Ian Campbellea2ab692011-08-22 23:44:58 +00001747 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001748 f->page_offset, skb_frag_size(f),
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001749 offset, len, skb, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001750 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001751 }
1752
David S. Millera108d5f2012-04-23 23:06:11 -04001753 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001754}
1755
1756/*
1757 * Map data from the skb to a pipe. Should handle both the linear part,
1758 * the fragments, and the frag list. It does NOT handle frag lists within
1759 * the frag list, if such a thing exists. We'd probably need to recurse to
1760 * handle that cleanly.
1761 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001762int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001763 struct pipe_inode_info *pipe, unsigned int tlen,
1764 unsigned int flags)
1765{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001766 struct partial_page partial[MAX_SKB_FRAGS];
1767 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001768 struct splice_pipe_desc spd = {
1769 .pages = pages,
1770 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001771 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001772 .flags = flags,
1773 .ops = &sock_pipe_buf_ops,
1774 .spd_release = sock_spd_release,
1775 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001776 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001777 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001778 int ret = 0;
1779
Jens Axboe9c55e012007-11-06 23:30:13 -08001780 /*
1781 * __skb_splice_bits() only fails if the output has no room left,
1782 * so no point in going over the frag_list for the error case.
1783 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001784 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001785 goto done;
1786 else if (!tlen)
1787 goto done;
1788
1789 /*
1790 * now see if we have a frag_list to map
1791 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001792 skb_walk_frags(skb, frag_iter) {
1793 if (!tlen)
1794 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001795 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001796 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001797 }
1798
1799done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001800 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001801 /*
1802 * Drop the socket lock, otherwise we have reverse
1803 * locking dependencies between sk_lock and i_mutex
1804 * here as compared to sendfile(). We enter here
1805 * with the socket lock held, and splice_to_pipe() will
1806 * grab the pipe inode lock. For sendfile() emulation,
1807 * we call into ->sendpage() with the i_mutex lock held
1808 * and networking will grab the socket lock.
1809 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001810 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001811 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001812 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001813 }
1814
Jens Axboe35f3d142010-05-20 10:43:18 +02001815 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001816}
1817
Herbert Xu357b40a2005-04-19 22:30:14 -07001818/**
1819 * skb_store_bits - store bits from kernel buffer to skb
1820 * @skb: destination buffer
1821 * @offset: offset in destination
1822 * @from: source buffer
1823 * @len: number of bytes to copy
1824 *
1825 * Copy the specified number of bytes from the source buffer to the
1826 * destination skb. This function handles all the messy bits of
1827 * traversing fragment lists and such.
1828 */
1829
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001830int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001831{
David S. Miller1a028e52007-04-27 15:21:23 -07001832 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001833 struct sk_buff *frag_iter;
1834 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001835
1836 if (offset > (int)skb->len - len)
1837 goto fault;
1838
David S. Miller1a028e52007-04-27 15:21:23 -07001839 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001840 if (copy > len)
1841 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001842 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001843 if ((len -= copy) == 0)
1844 return 0;
1845 offset += copy;
1846 from += copy;
1847 }
1848
1849 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1850 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001851 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001852
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001853 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001854
Eric Dumazet9e903e02011-10-18 21:00:24 +00001855 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001856 if ((copy = end - offset) > 0) {
1857 u8 *vaddr;
1858
1859 if (copy > len)
1860 copy = len;
1861
Eric Dumazet51c56b02012-04-05 11:35:15 +02001862 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001863 memcpy(vaddr + frag->page_offset + offset - start,
1864 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001865 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001866
1867 if ((len -= copy) == 0)
1868 return 0;
1869 offset += copy;
1870 from += copy;
1871 }
David S. Miller1a028e52007-04-27 15:21:23 -07001872 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001873 }
1874
David S. Millerfbb398a2009-06-09 00:18:59 -07001875 skb_walk_frags(skb, frag_iter) {
1876 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001877
David S. Millerfbb398a2009-06-09 00:18:59 -07001878 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001879
David S. Millerfbb398a2009-06-09 00:18:59 -07001880 end = start + frag_iter->len;
1881 if ((copy = end - offset) > 0) {
1882 if (copy > len)
1883 copy = len;
1884 if (skb_store_bits(frag_iter, offset - start,
1885 from, copy))
1886 goto fault;
1887 if ((len -= copy) == 0)
1888 return 0;
1889 offset += copy;
1890 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001891 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001892 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001893 }
1894 if (!len)
1895 return 0;
1896
1897fault:
1898 return -EFAULT;
1899}
Herbert Xu357b40a2005-04-19 22:30:14 -07001900EXPORT_SYMBOL(skb_store_bits);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/* Checksum skb data. */
1903
Al Viro2bbbc862006-11-14 21:37:14 -08001904__wsum skb_checksum(const struct sk_buff *skb, int offset,
1905 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
David S. Miller1a028e52007-04-27 15:21:23 -07001907 int start = skb_headlen(skb);
1908 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001909 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 int pos = 0;
1911
1912 /* Checksum header. */
1913 if (copy > 0) {
1914 if (copy > len)
1915 copy = len;
1916 csum = csum_partial(skb->data + offset, copy, csum);
1917 if ((len -= copy) == 0)
1918 return csum;
1919 offset += copy;
1920 pos = copy;
1921 }
1922
1923 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001924 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001925 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001927 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001928
Eric Dumazet51c56b02012-04-05 11:35:15 +02001929 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001931 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (copy > len)
1935 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001936 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001937 csum2 = csum_partial(vaddr + frag->page_offset +
1938 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001939 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 csum = csum_block_add(csum, csum2, pos);
1941 if (!(len -= copy))
1942 return csum;
1943 offset += copy;
1944 pos += copy;
1945 }
David S. Miller1a028e52007-04-27 15:21:23 -07001946 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
David S. Millerfbb398a2009-06-09 00:18:59 -07001949 skb_walk_frags(skb, frag_iter) {
1950 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
David S. Millerfbb398a2009-06-09 00:18:59 -07001952 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
David S. Millerfbb398a2009-06-09 00:18:59 -07001954 end = start + frag_iter->len;
1955 if ((copy = end - offset) > 0) {
1956 __wsum csum2;
1957 if (copy > len)
1958 copy = len;
1959 csum2 = skb_checksum(frag_iter, offset - start,
1960 copy, 0);
1961 csum = csum_block_add(csum, csum2, pos);
1962 if ((len -= copy) == 0)
1963 return csum;
1964 offset += copy;
1965 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001967 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001969 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 return csum;
1972}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001973EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975/* Both of above in one bottle. */
1976
Al Viro81d77662006-11-14 21:37:33 -08001977__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1978 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
David S. Miller1a028e52007-04-27 15:21:23 -07001980 int start = skb_headlen(skb);
1981 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001982 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int pos = 0;
1984
1985 /* Copy header. */
1986 if (copy > 0) {
1987 if (copy > len)
1988 copy = len;
1989 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1990 copy, csum);
1991 if ((len -= copy) == 0)
1992 return csum;
1993 offset += copy;
1994 to += copy;
1995 pos = copy;
1996 }
1997
1998 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001999 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002001 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002002
Eric Dumazet9e903e02011-10-18 21:00:24 +00002003 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002005 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 u8 *vaddr;
2007 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2008
2009 if (copy > len)
2010 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002011 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002013 frag->page_offset +
2014 offset - start, to,
2015 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002016 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 csum = csum_block_add(csum, csum2, pos);
2018 if (!(len -= copy))
2019 return csum;
2020 offset += copy;
2021 to += copy;
2022 pos += copy;
2023 }
David S. Miller1a028e52007-04-27 15:21:23 -07002024 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026
David S. Millerfbb398a2009-06-09 00:18:59 -07002027 skb_walk_frags(skb, frag_iter) {
2028 __wsum csum2;
2029 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
David S. Millerfbb398a2009-06-09 00:18:59 -07002031 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
David S. Millerfbb398a2009-06-09 00:18:59 -07002033 end = start + frag_iter->len;
2034 if ((copy = end - offset) > 0) {
2035 if (copy > len)
2036 copy = len;
2037 csum2 = skb_copy_and_csum_bits(frag_iter,
2038 offset - start,
2039 to, copy, 0);
2040 csum = csum_block_add(csum, csum2, pos);
2041 if ((len -= copy) == 0)
2042 return csum;
2043 offset += copy;
2044 to += copy;
2045 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002047 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002049 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return csum;
2051}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002052EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2055{
Al Virod3bc23e2006-11-14 21:24:49 -08002056 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 long csstart;
2058
Patrick McHardy84fa7932006-08-29 16:44:56 -07002059 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002060 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 else
2062 csstart = skb_headlen(skb);
2063
Kris Katterjohn09a62662006-01-08 22:24:28 -08002064 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002066 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 csum = 0;
2069 if (csstart != skb->len)
2070 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2071 skb->len - csstart, 0);
2072
Patrick McHardy84fa7932006-08-29 16:44:56 -07002073 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002074 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Al Virod3bc23e2006-11-14 21:24:49 -08002076 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002079EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081/**
2082 * skb_dequeue - remove from the head of the queue
2083 * @list: list to dequeue from
2084 *
2085 * Remove the head of the list. The list lock is taken so the function
2086 * may be used safely with other locking list functions. The head item is
2087 * returned or %NULL if the list is empty.
2088 */
2089
2090struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2091{
2092 unsigned long flags;
2093 struct sk_buff *result;
2094
2095 spin_lock_irqsave(&list->lock, flags);
2096 result = __skb_dequeue(list);
2097 spin_unlock_irqrestore(&list->lock, flags);
2098 return result;
2099}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002100EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102/**
2103 * skb_dequeue_tail - remove from the tail of the queue
2104 * @list: list to dequeue from
2105 *
2106 * Remove the tail of the list. The list lock is taken so the function
2107 * may be used safely with other locking list functions. The tail item is
2108 * returned or %NULL if the list is empty.
2109 */
2110struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2111{
2112 unsigned long flags;
2113 struct sk_buff *result;
2114
2115 spin_lock_irqsave(&list->lock, flags);
2116 result = __skb_dequeue_tail(list);
2117 spin_unlock_irqrestore(&list->lock, flags);
2118 return result;
2119}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002120EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122/**
2123 * skb_queue_purge - empty a list
2124 * @list: list to empty
2125 *
2126 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2127 * the list and one reference dropped. This function takes the list
2128 * lock and is atomic with respect to other list locking functions.
2129 */
2130void skb_queue_purge(struct sk_buff_head *list)
2131{
2132 struct sk_buff *skb;
2133 while ((skb = skb_dequeue(list)) != NULL)
2134 kfree_skb(skb);
2135}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002136EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138/**
2139 * skb_queue_head - queue a buffer at the list head
2140 * @list: list to use
2141 * @newsk: buffer to queue
2142 *
2143 * Queue a buffer at the start of the list. This function takes the
2144 * list lock and can be used safely with other locking &sk_buff functions
2145 * safely.
2146 *
2147 * A buffer cannot be placed on two lists at the same time.
2148 */
2149void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2150{
2151 unsigned long flags;
2152
2153 spin_lock_irqsave(&list->lock, flags);
2154 __skb_queue_head(list, newsk);
2155 spin_unlock_irqrestore(&list->lock, flags);
2156}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002157EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159/**
2160 * skb_queue_tail - queue a buffer at the list tail
2161 * @list: list to use
2162 * @newsk: buffer to queue
2163 *
2164 * Queue a buffer at the tail of the list. This function takes the
2165 * list lock and can be used safely with other locking &sk_buff functions
2166 * safely.
2167 *
2168 * A buffer cannot be placed on two lists at the same time.
2169 */
2170void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2171{
2172 unsigned long flags;
2173
2174 spin_lock_irqsave(&list->lock, flags);
2175 __skb_queue_tail(list, newsk);
2176 spin_unlock_irqrestore(&list->lock, flags);
2177}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002178EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180/**
2181 * skb_unlink - remove a buffer from a list
2182 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002183 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 *
David S. Miller8728b832005-08-09 19:25:21 -07002185 * Remove a packet from a list. The list locks are taken and this
2186 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 *
David S. Miller8728b832005-08-09 19:25:21 -07002188 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 */
David S. Miller8728b832005-08-09 19:25:21 -07002190void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
David S. Miller8728b832005-08-09 19:25:21 -07002192 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
David S. Miller8728b832005-08-09 19:25:21 -07002194 spin_lock_irqsave(&list->lock, flags);
2195 __skb_unlink(skb, list);
2196 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002198EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200/**
2201 * skb_append - append a buffer
2202 * @old: buffer to insert after
2203 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002204 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 *
2206 * Place a packet after a given packet in a list. The list locks are taken
2207 * and this function is atomic with respect to other list locked calls.
2208 * A buffer cannot be placed on two lists at the same time.
2209 */
David S. Miller8728b832005-08-09 19:25:21 -07002210void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 unsigned long flags;
2213
David S. Miller8728b832005-08-09 19:25:21 -07002214 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002215 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002216 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002218EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220/**
2221 * skb_insert - insert a buffer
2222 * @old: buffer to insert before
2223 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002224 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 *
David S. Miller8728b832005-08-09 19:25:21 -07002226 * Place a packet before a given packet in a list. The list locks are
2227 * taken and this function is atomic with respect to other list locked
2228 * calls.
2229 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 * A buffer cannot be placed on two lists at the same time.
2231 */
David S. Miller8728b832005-08-09 19:25:21 -07002232void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 unsigned long flags;
2235
David S. Miller8728b832005-08-09 19:25:21 -07002236 spin_lock_irqsave(&list->lock, flags);
2237 __skb_insert(newsk, old->prev, old, list);
2238 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002240EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242static inline void skb_split_inside_header(struct sk_buff *skb,
2243 struct sk_buff* skb1,
2244 const u32 len, const int pos)
2245{
2246 int i;
2247
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002248 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2249 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /* And move data appendix as is. */
2251 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2252 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2253
2254 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2255 skb_shinfo(skb)->nr_frags = 0;
2256 skb1->data_len = skb->data_len;
2257 skb1->len += skb1->data_len;
2258 skb->data_len = 0;
2259 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002260 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
2263static inline void skb_split_no_header(struct sk_buff *skb,
2264 struct sk_buff* skb1,
2265 const u32 len, int pos)
2266{
2267 int i, k = 0;
2268 const int nfrags = skb_shinfo(skb)->nr_frags;
2269
2270 skb_shinfo(skb)->nr_frags = 0;
2271 skb1->len = skb1->data_len = skb->len - len;
2272 skb->len = len;
2273 skb->data_len = len - pos;
2274
2275 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002276 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 if (pos + size > len) {
2279 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2280
2281 if (pos < len) {
2282 /* Split frag.
2283 * We have two variants in this case:
2284 * 1. Move all the frag to the second
2285 * part, if it is possible. F.e.
2286 * this approach is mandatory for TUX,
2287 * where splitting is expensive.
2288 * 2. Split is accurately. We make this.
2289 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002290 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002292 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2293 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 skb_shinfo(skb)->nr_frags++;
2295 }
2296 k++;
2297 } else
2298 skb_shinfo(skb)->nr_frags++;
2299 pos += size;
2300 }
2301 skb_shinfo(skb1)->nr_frags = k;
2302}
2303
2304/**
2305 * skb_split - Split fragmented skb to two parts at length len.
2306 * @skb: the buffer to split
2307 * @skb1: the buffer to receive the second part
2308 * @len: new length for skb
2309 */
2310void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2311{
2312 int pos = skb_headlen(skb);
2313
2314 if (len < pos) /* Split line is inside header. */
2315 skb_split_inside_header(skb, skb1, len, pos);
2316 else /* Second chunk has no header, nothing to copy. */
2317 skb_split_no_header(skb, skb1, len, pos);
2318}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002319EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002321/* Shifting from/to a cloned skb is a no-go.
2322 *
2323 * Caller cannot keep skb_shinfo related pointers past calling here!
2324 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002325static int skb_prepare_for_shift(struct sk_buff *skb)
2326{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002327 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002328}
2329
2330/**
2331 * skb_shift - Shifts paged data partially from skb to another
2332 * @tgt: buffer into which tail data gets added
2333 * @skb: buffer from which the paged data comes from
2334 * @shiftlen: shift up to this many bytes
2335 *
2336 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002337 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002338 * It's up to caller to free skb if everything was shifted.
2339 *
2340 * If @tgt runs out of frags, the whole operation is aborted.
2341 *
2342 * Skb cannot include anything else but paged data while tgt is allowed
2343 * to have non-paged data as well.
2344 *
2345 * TODO: full sized shift could be optimized but that would need
2346 * specialized skb free'er to handle frags without up-to-date nr_frags.
2347 */
2348int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2349{
2350 int from, to, merge, todo;
2351 struct skb_frag_struct *fragfrom, *fragto;
2352
2353 BUG_ON(shiftlen > skb->len);
2354 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2355
2356 todo = shiftlen;
2357 from = 0;
2358 to = skb_shinfo(tgt)->nr_frags;
2359 fragfrom = &skb_shinfo(skb)->frags[from];
2360
2361 /* Actual merge is delayed until the point when we know we can
2362 * commit all, so that we don't have to undo partial changes
2363 */
2364 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002365 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2366 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002367 merge = -1;
2368 } else {
2369 merge = to - 1;
2370
Eric Dumazet9e903e02011-10-18 21:00:24 +00002371 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002372 if (todo < 0) {
2373 if (skb_prepare_for_shift(skb) ||
2374 skb_prepare_for_shift(tgt))
2375 return 0;
2376
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002377 /* All previous frag pointers might be stale! */
2378 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002379 fragto = &skb_shinfo(tgt)->frags[merge];
2380
Eric Dumazet9e903e02011-10-18 21:00:24 +00002381 skb_frag_size_add(fragto, shiftlen);
2382 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002383 fragfrom->page_offset += shiftlen;
2384
2385 goto onlymerged;
2386 }
2387
2388 from++;
2389 }
2390
2391 /* Skip full, not-fitting skb to avoid expensive operations */
2392 if ((shiftlen == skb->len) &&
2393 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2394 return 0;
2395
2396 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2397 return 0;
2398
2399 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2400 if (to == MAX_SKB_FRAGS)
2401 return 0;
2402
2403 fragfrom = &skb_shinfo(skb)->frags[from];
2404 fragto = &skb_shinfo(tgt)->frags[to];
2405
Eric Dumazet9e903e02011-10-18 21:00:24 +00002406 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002407 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002408 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002409 from++;
2410 to++;
2411
2412 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002413 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002414 fragto->page = fragfrom->page;
2415 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002416 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002417
2418 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002419 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002420 todo = 0;
2421
2422 to++;
2423 break;
2424 }
2425 }
2426
2427 /* Ready to "commit" this state change to tgt */
2428 skb_shinfo(tgt)->nr_frags = to;
2429
2430 if (merge >= 0) {
2431 fragfrom = &skb_shinfo(skb)->frags[0];
2432 fragto = &skb_shinfo(tgt)->frags[merge];
2433
Eric Dumazet9e903e02011-10-18 21:00:24 +00002434 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002435 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002436 }
2437
2438 /* Reposition in the original skb */
2439 to = 0;
2440 while (from < skb_shinfo(skb)->nr_frags)
2441 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2442 skb_shinfo(skb)->nr_frags = to;
2443
2444 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2445
2446onlymerged:
2447 /* Most likely the tgt won't ever need its checksum anymore, skb on
2448 * the other hand might need it if it needs to be resent
2449 */
2450 tgt->ip_summed = CHECKSUM_PARTIAL;
2451 skb->ip_summed = CHECKSUM_PARTIAL;
2452
2453 /* Yak, is it really working this way? Some helper please? */
2454 skb->len -= shiftlen;
2455 skb->data_len -= shiftlen;
2456 skb->truesize -= shiftlen;
2457 tgt->len += shiftlen;
2458 tgt->data_len += shiftlen;
2459 tgt->truesize += shiftlen;
2460
2461 return shiftlen;
2462}
2463
Thomas Graf677e90e2005-06-23 20:59:51 -07002464/**
2465 * skb_prepare_seq_read - Prepare a sequential read of skb data
2466 * @skb: the buffer to read
2467 * @from: lower offset of data to be read
2468 * @to: upper offset of data to be read
2469 * @st: state variable
2470 *
2471 * Initializes the specified state variable. Must be called before
2472 * invoking skb_seq_read() for the first time.
2473 */
2474void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2475 unsigned int to, struct skb_seq_state *st)
2476{
2477 st->lower_offset = from;
2478 st->upper_offset = to;
2479 st->root_skb = st->cur_skb = skb;
2480 st->frag_idx = st->stepped_offset = 0;
2481 st->frag_data = NULL;
2482}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002483EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002484
2485/**
2486 * skb_seq_read - Sequentially read skb data
2487 * @consumed: number of bytes consumed by the caller so far
2488 * @data: destination pointer for data to be returned
2489 * @st: state variable
2490 *
2491 * Reads a block of skb data at &consumed relative to the
2492 * lower offset specified to skb_prepare_seq_read(). Assigns
2493 * the head of the data block to &data and returns the length
2494 * of the block or 0 if the end of the skb data or the upper
2495 * offset has been reached.
2496 *
2497 * The caller is not required to consume all of the data
2498 * returned, i.e. &consumed is typically set to the number
2499 * of bytes already consumed and the next call to
2500 * skb_seq_read() will return the remaining part of the block.
2501 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002502 * Note 1: The size of each block of data returned can be arbitrary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002503 * this limitation is the cost for zerocopy seqeuental
2504 * reads of potentially non linear data.
2505 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002506 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002507 * at the moment, state->root_skb could be replaced with
2508 * a stack for this purpose.
2509 */
2510unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2511 struct skb_seq_state *st)
2512{
2513 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2514 skb_frag_t *frag;
2515
2516 if (unlikely(abs_offset >= st->upper_offset))
2517 return 0;
2518
2519next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002520 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002521
Thomas Chenault995b3372009-05-18 21:43:27 -07002522 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002523 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002524 return block_limit - abs_offset;
2525 }
2526
2527 if (st->frag_idx == 0 && !st->frag_data)
2528 st->stepped_offset += skb_headlen(st->cur_skb);
2529
2530 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2531 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002532 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002533
2534 if (abs_offset < block_limit) {
2535 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002536 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002537
2538 *data = (u8 *) st->frag_data + frag->page_offset +
2539 (abs_offset - st->stepped_offset);
2540
2541 return block_limit - abs_offset;
2542 }
2543
2544 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002545 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002546 st->frag_data = NULL;
2547 }
2548
2549 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002550 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002551 }
2552
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002553 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002554 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002555 st->frag_data = NULL;
2556 }
2557
David S. Miller21dc3302010-08-23 00:13:46 -07002558 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002559 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002560 st->frag_idx = 0;
2561 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002562 } else if (st->cur_skb->next) {
2563 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002564 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002565 goto next_skb;
2566 }
2567
2568 return 0;
2569}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002570EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002571
2572/**
2573 * skb_abort_seq_read - Abort a sequential read of skb data
2574 * @st: state variable
2575 *
2576 * Must be called if skb_seq_read() was not called until it
2577 * returned 0.
2578 */
2579void skb_abort_seq_read(struct skb_seq_state *st)
2580{
2581 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002582 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002583}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002584EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002585
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002586#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2587
2588static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2589 struct ts_config *conf,
2590 struct ts_state *state)
2591{
2592 return skb_seq_read(offset, text, TS_SKB_CB(state));
2593}
2594
2595static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2596{
2597 skb_abort_seq_read(TS_SKB_CB(state));
2598}
2599
2600/**
2601 * skb_find_text - Find a text pattern in skb data
2602 * @skb: the buffer to look in
2603 * @from: search offset
2604 * @to: search limit
2605 * @config: textsearch configuration
2606 * @state: uninitialized textsearch state variable
2607 *
2608 * Finds a pattern in the skb data according to the specified
2609 * textsearch configuration. Use textsearch_next() to retrieve
2610 * subsequent occurrences of the pattern. Returns the offset
2611 * to the first occurrence or UINT_MAX if no match was found.
2612 */
2613unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2614 unsigned int to, struct ts_config *config,
2615 struct ts_state *state)
2616{
Phil Oesterf72b9482006-06-26 00:00:57 -07002617 unsigned int ret;
2618
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002619 config->get_next_block = skb_ts_get_next_block;
2620 config->finish = skb_ts_finish;
2621
2622 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2623
Phil Oesterf72b9482006-06-26 00:00:57 -07002624 ret = textsearch_find(config, state);
2625 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002626}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002627EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002628
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002629/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002630 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002631 * @sk: sock structure
2632 * @skb: skb structure to be appened with user data.
2633 * @getfrag: call back function to be used for getting the user data
2634 * @from: pointer to user message iov
2635 * @length: length of the iov message
2636 *
2637 * Description: This procedure append the user data in the fragment part
2638 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2639 */
2640int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002641 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002642 int len, int odd, struct sk_buff *skb),
2643 void *from, int length)
2644{
2645 int frg_cnt = 0;
2646 skb_frag_t *frag = NULL;
2647 struct page *page = NULL;
2648 int copy, left;
2649 int offset = 0;
2650 int ret;
2651
2652 do {
2653 /* Return error if we don't have space for new frag */
2654 frg_cnt = skb_shinfo(skb)->nr_frags;
2655 if (frg_cnt >= MAX_SKB_FRAGS)
2656 return -EFAULT;
2657
2658 /* allocate a new page for next frag */
2659 page = alloc_pages(sk->sk_allocation, 0);
2660
2661 /* If alloc_page fails just return failure and caller will
2662 * free previous allocated pages by doing kfree_skb()
2663 */
2664 if (page == NULL)
2665 return -ENOMEM;
2666
2667 /* initialize the next frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002668 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2669 skb->truesize += PAGE_SIZE;
2670 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2671
2672 /* get the new initialized frag */
2673 frg_cnt = skb_shinfo(skb)->nr_frags;
2674 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2675
2676 /* copy the user data to page */
2677 left = PAGE_SIZE - frag->page_offset;
2678 copy = (length > left)? left : length;
2679
Eric Dumazet9e903e02011-10-18 21:00:24 +00002680 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002681 offset, copy, 0, skb);
2682 if (ret < 0)
2683 return -EFAULT;
2684
2685 /* copy was successful so update the size parameters */
Eric Dumazet9e903e02011-10-18 21:00:24 +00002686 skb_frag_size_add(frag, copy);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002687 skb->len += copy;
2688 skb->data_len += copy;
2689 offset += copy;
2690 length -= copy;
2691
2692 } while (length > 0);
2693
2694 return 0;
2695}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002696EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002697
Herbert Xucbb042f2006-03-20 22:43:56 -08002698/**
2699 * skb_pull_rcsum - pull skb and update receive checksum
2700 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002701 * @len: length of data pulled
2702 *
2703 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002704 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002705 * receive path processing instead of skb_pull unless you know
2706 * that the checksum difference is zero (e.g., a valid IP header)
2707 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002708 */
2709unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2710{
2711 BUG_ON(len > skb->len);
2712 skb->len -= len;
2713 BUG_ON(skb->len < skb->data_len);
2714 skb_postpull_rcsum(skb, skb->data, len);
2715 return skb->data += len;
2716}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002717EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2718
Herbert Xuf4c50d92006-06-22 03:02:40 -07002719/**
2720 * skb_segment - Perform protocol segmentation on skb.
2721 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002722 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002723 *
2724 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002725 * a pointer to the first in a list of new skbs for the segments.
2726 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002727 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002728struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002729{
2730 struct sk_buff *segs = NULL;
2731 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002732 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002733 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002734 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002735 unsigned int offset = doffset;
2736 unsigned int headroom;
2737 unsigned int len;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002738 int sg = !!(features & NETIF_F_SG);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002739 int nfrags = skb_shinfo(skb)->nr_frags;
2740 int err = -ENOMEM;
2741 int i = 0;
2742 int pos;
2743
2744 __skb_push(skb, doffset);
2745 headroom = skb_headroom(skb);
2746 pos = skb_headlen(skb);
2747
2748 do {
2749 struct sk_buff *nskb;
2750 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002751 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002752 int size;
2753
2754 len = skb->len - offset;
2755 if (len > mss)
2756 len = mss;
2757
2758 hsize = skb_headlen(skb) - offset;
2759 if (hsize < 0)
2760 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002761 if (hsize > len || !sg)
2762 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002763
Herbert Xu89319d382008-12-15 23:26:06 -08002764 if (!hsize && i >= nfrags) {
2765 BUG_ON(fskb->len != len);
2766
2767 pos += len;
2768 nskb = skb_clone(fskb, GFP_ATOMIC);
2769 fskb = fskb->next;
2770
2771 if (unlikely(!nskb))
2772 goto err;
2773
Alexander Duyckec47ea82012-05-04 14:26:56 +00002774 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002775 if (skb_cow_head(nskb, doffset + headroom)) {
2776 kfree_skb(nskb);
2777 goto err;
2778 }
2779
Alexander Duyckec47ea82012-05-04 14:26:56 +00002780 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002781 skb_release_head_state(nskb);
2782 __skb_push(nskb, doffset);
2783 } else {
2784 nskb = alloc_skb(hsize + doffset + headroom,
2785 GFP_ATOMIC);
2786
2787 if (unlikely(!nskb))
2788 goto err;
2789
2790 skb_reserve(nskb, headroom);
2791 __skb_put(nskb, doffset);
2792 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002793
2794 if (segs)
2795 tail->next = nskb;
2796 else
2797 segs = nskb;
2798 tail = nskb;
2799
Herbert Xu6f85a122008-08-15 14:55:02 -07002800 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002801 nskb->mac_len = skb->mac_len;
2802
Eric Dumazet3d3be432010-09-01 00:50:51 +00002803 /* nskb and skb might have different headroom */
2804 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2805 nskb->csum_start += skb_headroom(nskb) - headroom;
2806
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002807 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002808 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002809 nskb->transport_header = (nskb->network_header +
2810 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002811 skb_copy_from_linear_data(skb, nskb->data, doffset);
2812
Herbert Xu2f181852009-03-28 23:39:18 -07002813 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002814 continue;
2815
Herbert Xuf4c50d92006-06-22 03:02:40 -07002816 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002817 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002818 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2819 skb_put(nskb, len),
2820 len, 0);
2821 continue;
2822 }
2823
2824 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002825
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002826 skb_copy_from_linear_data_offset(skb, offset,
2827 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002828
Herbert Xu89319d382008-12-15 23:26:06 -08002829 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002830 *frag = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00002831 __skb_frag_ref(frag);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002832 size = skb_frag_size(frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002833
2834 if (pos < offset) {
2835 frag->page_offset += offset - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002836 skb_frag_size_sub(frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002837 }
2838
Herbert Xu89319d382008-12-15 23:26:06 -08002839 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002840
2841 if (pos + size <= offset + len) {
2842 i++;
2843 pos += size;
2844 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002845 skb_frag_size_sub(frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08002846 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002847 }
2848
2849 frag++;
2850 }
2851
Herbert Xu89319d382008-12-15 23:26:06 -08002852 if (pos < offset + len) {
2853 struct sk_buff *fskb2 = fskb;
2854
2855 BUG_ON(pos + fskb->len != offset + len);
2856
2857 pos += fskb->len;
2858 fskb = fskb->next;
2859
2860 if (fskb2->next) {
2861 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2862 if (!fskb2)
2863 goto err;
2864 } else
2865 skb_get(fskb2);
2866
David S. Millerfbb398a2009-06-09 00:18:59 -07002867 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002868 skb_shinfo(nskb)->frag_list = fskb2;
2869 }
2870
2871skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002872 nskb->data_len = len - hsize;
2873 nskb->len += nskb->data_len;
2874 nskb->truesize += nskb->data_len;
2875 } while ((offset += len) < skb->len);
2876
2877 return segs;
2878
2879err:
2880 while ((skb = segs)) {
2881 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002882 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002883 }
2884 return ERR_PTR(err);
2885}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002886EXPORT_SYMBOL_GPL(skb_segment);
2887
Herbert Xu71d93b32008-12-15 23:42:33 -08002888int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2889{
2890 struct sk_buff *p = *head;
2891 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002892 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2893 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002894 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002895 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002896 unsigned int offset = skb_gro_offset(skb);
2897 unsigned int headlen = skb_headlen(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002898 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002899
Herbert Xu86911732009-01-29 14:19:50 +00002900 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002901 return -E2BIG;
2902
Herbert Xu9aaa1562009-05-26 18:50:33 +00002903 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002904 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002905 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002906 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002907 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002908 int i = skbinfo->nr_frags;
2909 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002910
Herbert Xu66e92fc2009-05-26 18:50:32 +00002911 offset -= headlen;
2912
2913 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002914 return -E2BIG;
2915
Herbert Xu9aaa1562009-05-26 18:50:33 +00002916 pinfo->nr_frags = nr_frags;
2917 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002918
Herbert Xu9aaa1562009-05-26 18:50:33 +00002919 frag = pinfo->frags + nr_frags;
2920 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002921 do {
2922 *--frag = *--frag2;
2923 } while (--i);
2924
2925 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002926 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00002927
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002928 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00002929 delta_truesize = skb->truesize -
2930 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002931
Herbert Xuf5572062009-01-14 20:40:03 -08002932 skb->truesize -= skb->data_len;
2933 skb->len -= skb->data_len;
2934 skb->data_len = 0;
2935
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002936 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08002937 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00002938 } else if (skb->head_frag) {
2939 int nr_frags = pinfo->nr_frags;
2940 skb_frag_t *frag = pinfo->frags + nr_frags;
2941 struct page *page = virt_to_head_page(skb->head);
2942 unsigned int first_size = headlen - offset;
2943 unsigned int first_offset;
2944
2945 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2946 return -E2BIG;
2947
2948 first_offset = skb->data -
2949 (unsigned char *)page_address(page) +
2950 offset;
2951
2952 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2953
2954 frag->page.p = page;
2955 frag->page_offset = first_offset;
2956 skb_frag_size_set(frag, first_size);
2957
2958 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2959 /* We dont need to clear skbinfo->nr_frags here */
2960
Eric Dumazet715dc1f2012-05-02 23:33:21 +00002961 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00002962 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2963 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08002964 } else if (skb_gro_len(p) != pinfo->gso_size)
2965 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08002966
2967 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00002968 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08002969 if (unlikely(!nskb))
2970 return -ENOMEM;
2971
2972 __copy_skb_header(nskb, p);
2973 nskb->mac_len = p->mac_len;
2974
2975 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00002976 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002977
Herbert Xu86911732009-01-29 14:19:50 +00002978 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08002979 skb_set_network_header(nskb, skb_network_offset(p));
2980 skb_set_transport_header(nskb, skb_transport_offset(p));
2981
Herbert Xu86911732009-01-29 14:19:50 +00002982 __skb_pull(p, skb_gro_offset(p));
2983 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2984 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002985
2986 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2987 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002988 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07002989 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08002990 skb_header_release(p);
2991 nskb->prev = p;
2992
2993 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00002994 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08002995 nskb->len += p->len;
2996
2997 *head = nskb;
2998 nskb->next = p->next;
2999 p->next = NULL;
3000
3001 p = nskb;
3002
3003merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003004 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003005 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003006 unsigned int eat = offset - headlen;
3007
3008 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003009 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003010 skb->data_len -= eat;
3011 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003012 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003013 }
3014
Herbert Xu67147ba2009-05-26 18:50:22 +00003015 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003016
Herbert Xu71d93b32008-12-15 23:42:33 -08003017 p->prev->next = skb;
3018 p->prev = skb;
3019 skb_header_release(skb);
3020
Herbert Xu5d38a072009-01-04 16:13:40 -08003021done:
3022 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003023 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003024 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003025 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08003026
3027 NAPI_GRO_CB(skb)->same_flow = 1;
3028 return 0;
3029}
3030EXPORT_SYMBOL_GPL(skb_gro_receive);
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032void __init skb_init(void)
3033{
3034 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3035 sizeof(struct sk_buff),
3036 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003037 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003038 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003039 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3040 (2*sizeof(struct sk_buff)) +
3041 sizeof(atomic_t),
3042 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003043 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003044 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
David Howells716ea3a2007-04-02 20:19:53 -07003047/**
3048 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3049 * @skb: Socket buffer containing the buffers to be mapped
3050 * @sg: The scatter-gather list to map into
3051 * @offset: The offset into the buffer's contents to start mapping
3052 * @len: Length of buffer space to be mapped
3053 *
3054 * Fill the specified scatter-gather list with mappings/pointers into a
3055 * region of the buffer space attached to a socket buffer.
3056 */
David S. Miller51c739d2007-10-30 21:29:29 -07003057static int
3058__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003059{
David S. Miller1a028e52007-04-27 15:21:23 -07003060 int start = skb_headlen(skb);
3061 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003062 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003063 int elt = 0;
3064
3065 if (copy > 0) {
3066 if (copy > len)
3067 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003068 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003069 elt++;
3070 if ((len -= copy) == 0)
3071 return elt;
3072 offset += copy;
3073 }
3074
3075 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003076 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003077
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003078 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003079
Eric Dumazet9e903e02011-10-18 21:00:24 +00003080 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003081 if ((copy = end - offset) > 0) {
3082 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3083
3084 if (copy > len)
3085 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003086 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003087 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003088 elt++;
3089 if (!(len -= copy))
3090 return elt;
3091 offset += copy;
3092 }
David S. Miller1a028e52007-04-27 15:21:23 -07003093 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003094 }
3095
David S. Millerfbb398a2009-06-09 00:18:59 -07003096 skb_walk_frags(skb, frag_iter) {
3097 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003098
David S. Millerfbb398a2009-06-09 00:18:59 -07003099 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003100
David S. Millerfbb398a2009-06-09 00:18:59 -07003101 end = start + frag_iter->len;
3102 if ((copy = end - offset) > 0) {
3103 if (copy > len)
3104 copy = len;
3105 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3106 copy);
3107 if ((len -= copy) == 0)
3108 return elt;
3109 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003110 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003111 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003112 }
3113 BUG_ON(len);
3114 return elt;
3115}
3116
David S. Miller51c739d2007-10-30 21:29:29 -07003117int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3118{
3119 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3120
Jens Axboec46f2332007-10-31 12:06:37 +01003121 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003122
3123 return nsg;
3124}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003125EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003126
David Howells716ea3a2007-04-02 20:19:53 -07003127/**
3128 * skb_cow_data - Check that a socket buffer's data buffers are writable
3129 * @skb: The socket buffer to check.
3130 * @tailbits: Amount of trailing space to be added
3131 * @trailer: Returned pointer to the skb where the @tailbits space begins
3132 *
3133 * Make sure that the data buffers attached to a socket buffer are
3134 * writable. If they are not, private copies are made of the data buffers
3135 * and the socket buffer is set to use these instead.
3136 *
3137 * If @tailbits is given, make sure that there is space to write @tailbits
3138 * bytes of data beyond current end of socket buffer. @trailer will be
3139 * set to point to the skb in which this space begins.
3140 *
3141 * The number of scatterlist elements required to completely map the
3142 * COW'd and extended socket buffer will be returned.
3143 */
3144int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3145{
3146 int copyflag;
3147 int elt;
3148 struct sk_buff *skb1, **skb_p;
3149
3150 /* If skb is cloned or its head is paged, reallocate
3151 * head pulling out all the pages (pages are considered not writable
3152 * at the moment even if they are anonymous).
3153 */
3154 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3155 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3156 return -ENOMEM;
3157
3158 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003159 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003160 /* A little of trouble, not enough of space for trailer.
3161 * This should not happen, when stack is tuned to generate
3162 * good frames. OK, on miss we reallocate and reserve even more
3163 * space, 128 bytes is fair. */
3164
3165 if (skb_tailroom(skb) < tailbits &&
3166 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3167 return -ENOMEM;
3168
3169 /* Voila! */
3170 *trailer = skb;
3171 return 1;
3172 }
3173
3174 /* Misery. We are in troubles, going to mincer fragments... */
3175
3176 elt = 1;
3177 skb_p = &skb_shinfo(skb)->frag_list;
3178 copyflag = 0;
3179
3180 while ((skb1 = *skb_p) != NULL) {
3181 int ntail = 0;
3182
3183 /* The fragment is partially pulled by someone,
3184 * this can happen on input. Copy it and everything
3185 * after it. */
3186
3187 if (skb_shared(skb1))
3188 copyflag = 1;
3189
3190 /* If the skb is the last, worry about trailer. */
3191
3192 if (skb1->next == NULL && tailbits) {
3193 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003194 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003195 skb_tailroom(skb1) < tailbits)
3196 ntail = tailbits + 128;
3197 }
3198
3199 if (copyflag ||
3200 skb_cloned(skb1) ||
3201 ntail ||
3202 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003203 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003204 struct sk_buff *skb2;
3205
3206 /* Fuck, we are miserable poor guys... */
3207 if (ntail == 0)
3208 skb2 = skb_copy(skb1, GFP_ATOMIC);
3209 else
3210 skb2 = skb_copy_expand(skb1,
3211 skb_headroom(skb1),
3212 ntail,
3213 GFP_ATOMIC);
3214 if (unlikely(skb2 == NULL))
3215 return -ENOMEM;
3216
3217 if (skb1->sk)
3218 skb_set_owner_w(skb2, skb1->sk);
3219
3220 /* Looking around. Are we still alive?
3221 * OK, link new skb, drop old one */
3222
3223 skb2->next = skb1->next;
3224 *skb_p = skb2;
3225 kfree_skb(skb1);
3226 skb1 = skb2;
3227 }
3228 elt++;
3229 *trailer = skb1;
3230 skb_p = &skb1->next;
3231 }
3232
3233 return elt;
3234}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003235EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003236
Eric Dumazetb1faf562010-05-31 23:44:05 -07003237static void sock_rmem_free(struct sk_buff *skb)
3238{
3239 struct sock *sk = skb->sk;
3240
3241 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3242}
3243
3244/*
3245 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3246 */
3247int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3248{
Eric Dumazet110c4332012-04-06 10:49:10 +02003249 int len = skb->len;
3250
Eric Dumazetb1faf562010-05-31 23:44:05 -07003251 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003252 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003253 return -ENOMEM;
3254
3255 skb_orphan(skb);
3256 skb->sk = sk;
3257 skb->destructor = sock_rmem_free;
3258 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3259
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003260 /* before exiting rcu section, make sure dst is refcounted */
3261 skb_dst_force(skb);
3262
Eric Dumazetb1faf562010-05-31 23:44:05 -07003263 skb_queue_tail(&sk->sk_error_queue, skb);
3264 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet110c4332012-04-06 10:49:10 +02003265 sk->sk_data_ready(sk, len);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003266 return 0;
3267}
3268EXPORT_SYMBOL(sock_queue_err_skb);
3269
Patrick Ohlyac45f602009-02-12 05:03:37 +00003270void skb_tstamp_tx(struct sk_buff *orig_skb,
3271 struct skb_shared_hwtstamps *hwtstamps)
3272{
3273 struct sock *sk = orig_skb->sk;
3274 struct sock_exterr_skb *serr;
3275 struct sk_buff *skb;
3276 int err;
3277
3278 if (!sk)
3279 return;
3280
3281 skb = skb_clone(orig_skb, GFP_ATOMIC);
3282 if (!skb)
3283 return;
3284
3285 if (hwtstamps) {
3286 *skb_hwtstamps(skb) =
3287 *hwtstamps;
3288 } else {
3289 /*
3290 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003291 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003292 * store software time stamp
3293 */
3294 skb->tstamp = ktime_get_real();
3295 }
3296
3297 serr = SKB_EXT_ERR(skb);
3298 memset(serr, 0, sizeof(*serr));
3299 serr->ee.ee_errno = ENOMSG;
3300 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003301
Patrick Ohlyac45f602009-02-12 05:03:37 +00003302 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003303
Patrick Ohlyac45f602009-02-12 05:03:37 +00003304 if (err)
3305 kfree_skb(skb);
3306}
3307EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3308
Johannes Berg6e3e9392011-11-09 10:15:42 +01003309void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3310{
3311 struct sock *sk = skb->sk;
3312 struct sock_exterr_skb *serr;
3313 int err;
3314
3315 skb->wifi_acked_valid = 1;
3316 skb->wifi_acked = acked;
3317
3318 serr = SKB_EXT_ERR(skb);
3319 memset(serr, 0, sizeof(*serr));
3320 serr->ee.ee_errno = ENOMSG;
3321 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3322
3323 err = sock_queue_err_skb(sk, skb);
3324 if (err)
3325 kfree_skb(skb);
3326}
3327EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3328
Patrick Ohlyac45f602009-02-12 05:03:37 +00003329
Rusty Russellf35d9d82008-02-04 23:49:54 -05003330/**
3331 * skb_partial_csum_set - set up and verify partial csum values for packet
3332 * @skb: the skb to set
3333 * @start: the number of bytes after skb->data to start checksumming.
3334 * @off: the offset from start to place the checksum.
3335 *
3336 * For untrusted partially-checksummed packets, we need to make sure the values
3337 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3338 *
3339 * This function checks and sets those values and skb->ip_summed: if this
3340 * returns false you should drop the packet.
3341 */
3342bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3343{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003344 if (unlikely(start > skb_headlen(skb)) ||
3345 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003346 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3347 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003348 return false;
3349 }
3350 skb->ip_summed = CHECKSUM_PARTIAL;
3351 skb->csum_start = skb_headroom(skb) + start;
3352 skb->csum_offset = off;
3353 return true;
3354}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003355EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003356
Ben Hutchings4497b072008-06-19 16:22:28 -07003357void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3358{
Joe Perchese87cc472012-05-13 21:56:26 +00003359 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3360 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003361}
Ben Hutchings4497b072008-06-19 16:22:28 -07003362EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003363
3364void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3365{
3366 if (head_stolen)
3367 kmem_cache_free(skbuff_head_cache, skb);
3368 else
3369 __kfree_skb(skb);
3370}
3371EXPORT_SYMBOL(kfree_skb_partial);
3372
3373/**
3374 * skb_try_coalesce - try to merge skb to prior one
3375 * @to: prior buffer
3376 * @from: buffer to add
3377 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003378 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003379 */
3380bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3381 bool *fragstolen, int *delta_truesize)
3382{
3383 int i, delta, len = from->len;
3384
3385 *fragstolen = false;
3386
3387 if (skb_cloned(to))
3388 return false;
3389
3390 if (len <= skb_tailroom(to)) {
3391 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3392 *delta_truesize = 0;
3393 return true;
3394 }
3395
3396 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3397 return false;
3398
3399 if (skb_headlen(from) != 0) {
3400 struct page *page;
3401 unsigned int offset;
3402
3403 if (skb_shinfo(to)->nr_frags +
3404 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3405 return false;
3406
3407 if (skb_head_is_locked(from))
3408 return false;
3409
3410 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3411
3412 page = virt_to_head_page(from->head);
3413 offset = from->data - (unsigned char *)page_address(page);
3414
3415 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3416 page, offset, skb_headlen(from));
3417 *fragstolen = true;
3418 } else {
3419 if (skb_shinfo(to)->nr_frags +
3420 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3421 return false;
3422
3423 delta = from->truesize -
3424 SKB_TRUESIZE(skb_end_pointer(from) - from->head);
3425 }
3426
3427 WARN_ON_ONCE(delta < len);
3428
3429 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3430 skb_shinfo(from)->frags,
3431 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3432 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3433
3434 if (!skb_cloned(from))
3435 skb_shinfo(from)->nr_frags = 0;
3436
3437 /* if the skb is cloned this does nothing since we set nr_frags to 0 */
3438 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3439 skb_frag_ref(from, i);
3440
3441 to->truesize += delta;
3442 to->len += len;
3443 to->data_len += len;
3444
3445 *delta_truesize = delta;
3446 return true;
3447}
3448EXPORT_SYMBOL(skb_try_coalesce);