blob: d4fdc649112c39c4e0f359174e17c2846b14d26e [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>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/netdevice.h>
53#ifdef CONFIG_NET_CLS_ACT
54#include <net/pkt_sched.h>
55#endif
56#include <linux/string.h>
57#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080058#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/cache.h>
60#include <linux/rtnetlink.h>
61#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070062#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000063#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070064#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040065#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <net/protocol.h>
68#include <net/dst.h>
69#include <net/sock.h>
70#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000071#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <net/xfrm.h>
73
74#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040075#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020076#include <linux/highmem.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040077
Eric Dumazetd7e88832012-04-30 08:10:34 +000078struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
Jean Sacrenf05de732013-02-11 13:30:38 +000082 * skb_panic - private function for out-of-line support
83 * @skb: buffer
84 * @sz: size
85 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000086 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Jean Sacrenf05de732013-02-11 13:30:38 +000088 * Out-of-line support for skb_put() and skb_push().
89 * Called via the wrapper skb_over_panic() or skb_under_panic().
90 * Keep out of line to prevent kernel bloat.
91 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Jean Sacrenf05de732013-02-11 13:30:38 +000093static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000094 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Joe Perchese005d192012-05-16 19:58:40 +000096 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +000097 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +000098 (unsigned long)skb->tail, (unsigned long)skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 BUG();
101}
102
Jean Sacrenf05de732013-02-11 13:30:38 +0000103static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jean Sacrenf05de732013-02-11 13:30:38 +0000105 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109{
110 skb_panic(skb, sz, addr, __func__);
111}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700112
113/*
114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115 * the caller if emergency pfmemalloc reserves are being used. If it is and
116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117 * may be used. Otherwise, the packet data may be discarded until enough
118 * memory is free
119 */
120#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000122
123static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
124 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700125{
126 void *obj;
127 bool ret_pfmemalloc = false;
128
129 /*
130 * Try a regular allocation, when that fails and we're not entitled
131 * to the reserves, fail.
132 */
133 obj = kmalloc_node_track_caller(size,
134 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
135 node);
136 if (obj || !(gfp_pfmemalloc_allowed(flags)))
137 goto out;
138
139 /* Try again but now we are using pfmemalloc reserves */
140 ret_pfmemalloc = true;
141 obj = kmalloc_node_track_caller(size, flags, node);
142
143out:
144 if (pfmemalloc)
145 *pfmemalloc = ret_pfmemalloc;
146
147 return obj;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
152 * [BEEP] leaks.
153 *
154 */
155
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000156struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
157{
158 struct sk_buff *skb;
159
160 /* Get the HEAD */
161 skb = kmem_cache_alloc_node(skbuff_head_cache,
162 gfp_mask & ~__GFP_DMA, node);
163 if (!skb)
164 goto out;
165
166 /*
167 * Only clear those fields we need to clear, not those that we will
168 * actually initialise below. Hence, don't put any more fields after
169 * the tail pointer in struct sk_buff!
170 */
171 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000172 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000173 skb->truesize = sizeof(struct sk_buff);
174 atomic_set(&skb->users, 1);
175
Cong Wang35d04612013-05-29 15:16:05 +0800176 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000177out:
178 return skb;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
David S. Millerd179cd12005-08-17 14:57:30 -0700182 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @size: size to allocate
184 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800189 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
196 * %GFP_ATOMIC.
197 */
Al Virodd0fc662005-10-07 07:46:04 +0100198struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700199 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Lametere18b8902006-12-06 20:33:20 -0800201 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800202 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct sk_buff *skb;
204 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700205 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
209
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!skb)
216 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700217 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
223 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000224 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!data)
228 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
232 */
233 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700234 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300236 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300240 */
241 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700244 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 atomic_set(&skb->users, 1);
246 skb->head = data;
247 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700248 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700249 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800250 skb->mac_header = (typeof(skb->mac_header))~0U;
251 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000252
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800253 /* make sure we initialize shinfo sequentially */
254 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700255 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800256 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000257 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258
Mel Gormanc93bdd02012-07-31 16:44:19 -0700259 if (flags & SKB_ALLOC_FCLONE) {
David S. Millerd179cd12005-08-17 14:57:30 -0700260 struct sk_buff *child = skb + 1;
261 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200263 kmemcheck_annotate_bitfield(child, flags1);
264 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700265 skb->fclone = SKB_FCLONE_ORIG;
266 atomic_set(fclone_ref, 1);
267
268 child->fclone = SKB_FCLONE_UNAVAILABLE;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700269 child->pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271out:
272 return skb;
273nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800274 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 skb = NULL;
276 goto out;
277}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800278EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000281 * build_skb - build a network buffer
282 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000283 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000284 *
285 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100286 * skb_shared_info. @data must have been allocated by kmalloc() only if
287 * @frag_size is 0, otherwise data should come from the page allocator.
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000288 * The return is the new skb buffer.
289 * On a failure the return is %NULL, and @data is not freed.
290 * Notes :
291 * Before IO, driver allocates only data buffer where NIC put incoming frame
292 * Driver should add room at head (NET_SKB_PAD) and
293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
295 * before giving packet to stack.
296 * RX rings only contains data buffers, not full skbs.
297 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000298struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000299{
300 struct skb_shared_info *shinfo;
301 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000302 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000303
304 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
305 if (!skb)
306 return NULL;
307
Eric Dumazetd3836f22012-04-27 00:33:38 +0000308 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000309
310 memset(skb, 0, offsetof(struct sk_buff, tail));
311 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000312 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313 atomic_set(&skb->users, 1);
314 skb->head = data;
315 skb->data = data;
316 skb_reset_tail_pointer(skb);
317 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800318 skb->mac_header = (typeof(skb->mac_header))~0U;
319 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000320
321 /* make sure we initialize shinfo sequentially */
322 shinfo = skb_shinfo(skb);
323 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
324 atomic_set(&shinfo->dataref, 1);
325 kmemcheck_annotate_variable(shinfo->destructor_arg);
326
327 return skb;
328}
329EXPORT_SYMBOL(build_skb);
330
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000331struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000332 struct page_frag frag;
333 /* we maintain a pagecount bias, so that we dont dirty cache line
334 * containing page->_count every time we allocate a fragment.
335 */
336 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000337};
338static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
339
Mel Gormanc93bdd02012-07-31 16:44:19 -0700340static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000341{
342 struct netdev_alloc_cache *nc;
343 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000344 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000345 unsigned long flags;
346
347 local_irq_save(flags);
348 nc = &__get_cpu_var(netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000349 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000350refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000351 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
352 gfp_t gfp = gfp_mask;
353
354 if (order)
355 gfp |= __GFP_COMP | __GFP_NOWARN;
356 nc->frag.page = alloc_pages(gfp, order);
357 if (likely(nc->frag.page))
358 break;
359 if (--order < 0)
360 goto end;
361 }
362 nc->frag.size = PAGE_SIZE << order;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000363recycle:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000364 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
365 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
366 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000367 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000368
Eric Dumazet69b08f62012-09-26 06:46:57 +0000369 if (nc->frag.offset + fragsz > nc->frag.size) {
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000370 /* avoid unnecessary locked operations if possible */
Eric Dumazet69b08f62012-09-26 06:46:57 +0000371 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
372 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000373 goto recycle;
374 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000375 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000376
Eric Dumazet69b08f62012-09-26 06:46:57 +0000377 data = page_address(nc->frag.page) + nc->frag.offset;
378 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000379 nc->pagecnt_bias--;
380end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000381 local_irq_restore(flags);
382 return data;
383}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700384
385/**
386 * netdev_alloc_frag - allocate a page fragment
387 * @fragsz: fragment size
388 *
389 * Allocates a frag from a page for receive buffer.
390 * Uses GFP_ATOMIC allocations.
391 */
392void *netdev_alloc_frag(unsigned int fragsz)
393{
394 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
395}
Eric Dumazet6f532612012-05-18 05:12:12 +0000396EXPORT_SYMBOL(netdev_alloc_frag);
397
398/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700399 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
400 * @dev: network device to receive on
401 * @length: length to allocate
402 * @gfp_mask: get_free_pages mask, passed to alloc_skb
403 *
404 * Allocate a new &sk_buff and assign it a usage count of one. The
405 * buffer has unspecified headroom built in. Users should allocate
406 * the headroom they think they need without accounting for the
407 * built in space. The built in space is used for optimisations.
408 *
409 * %NULL is returned if there is no free memory.
410 */
411struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000412 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700413{
Eric Dumazet6f532612012-05-18 05:12:12 +0000414 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000415 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
416 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700417
Eric Dumazet310e1582012-07-16 13:15:52 +0200418 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700419 void *data;
420
421 if (sk_memalloc_socks())
422 gfp_mask |= __GFP_MEMALLOC;
423
424 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000425
Eric Dumazet6f532612012-05-18 05:12:12 +0000426 if (likely(data)) {
427 skb = build_skb(data, fragsz);
428 if (unlikely(!skb))
429 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000430 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000431 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700432 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
433 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000434 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700435 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700436 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700437 skb->dev = dev;
438 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700439 return skb;
440}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800441EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Peter Zijlstra654bed12008-10-07 14:22:33 -0700443void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000444 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700445{
446 skb_fill_page_desc(skb, i, page, off, size);
447 skb->len += size;
448 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000449 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700450}
451EXPORT_SYMBOL(skb_add_rx_frag);
452
Jason Wangf8e617e2013-11-01 14:07:47 +0800453void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
454 unsigned int truesize)
455{
456 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
457
458 skb_frag_size_add(frag, size);
459 skb->len += size;
460 skb->data_len += size;
461 skb->truesize += truesize;
462}
463EXPORT_SYMBOL(skb_coalesce_rx_frag);
464
Herbert Xu27b437c2006-07-13 19:26:39 -0700465static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700467 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700468 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Herbert Xu27b437c2006-07-13 19:26:39 -0700471static inline void skb_drop_fraglist(struct sk_buff *skb)
472{
473 skb_drop_list(&skb_shinfo(skb)->frag_list);
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static void skb_clone_fraglist(struct sk_buff *skb)
477{
478 struct sk_buff *list;
479
David S. Millerfbb398a2009-06-09 00:18:59 -0700480 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 skb_get(list);
482}
483
Eric Dumazetd3836f22012-04-27 00:33:38 +0000484static void skb_free_head(struct sk_buff *skb)
485{
486 if (skb->head_frag)
487 put_page(virt_to_head_page(skb->head));
488 else
489 kfree(skb->head);
490}
491
Adrian Bunk5bba1712006-06-29 13:02:35 -0700492static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Eric Dumazetff04a772014-09-23 18:39:30 -0700494 struct skb_shared_info *shinfo = skb_shinfo(skb);
495 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Eric Dumazetff04a772014-09-23 18:39:30 -0700497 if (skb->cloned &&
498 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
499 &shinfo->dataref))
500 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000501
Eric Dumazetff04a772014-09-23 18:39:30 -0700502 for (i = 0; i < shinfo->nr_frags; i++)
503 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000504
Eric Dumazetff04a772014-09-23 18:39:30 -0700505 /*
506 * If skb buf is from userspace, we need to notify the caller
507 * the lower device DMA has done;
508 */
509 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
510 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Eric Dumazetff04a772014-09-23 18:39:30 -0700512 uarg = shinfo->destructor_arg;
513 if (uarg->callback)
514 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700516
517 if (shinfo->frag_list)
518 kfree_skb_list(shinfo->frag_list);
519
520 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/*
524 * Free an skbuff by memory without cleaning the state.
525 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800526static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
David S. Millerd179cd12005-08-17 14:57:30 -0700528 struct sk_buff *other;
529 atomic_t *fclone_ref;
530
David S. Millerd179cd12005-08-17 14:57:30 -0700531 switch (skb->fclone) {
532 case SKB_FCLONE_UNAVAILABLE:
533 kmem_cache_free(skbuff_head_cache, skb);
534 break;
535
536 case SKB_FCLONE_ORIG:
537 fclone_ref = (atomic_t *) (skb + 2);
538 if (atomic_dec_and_test(fclone_ref))
539 kmem_cache_free(skbuff_fclone_cache, skb);
540 break;
541
542 case SKB_FCLONE_CLONE:
543 fclone_ref = (atomic_t *) (skb + 1);
544 other = skb - 1;
545
546 /* The clone portion is available for
547 * fast-cloning again.
548 */
549 skb->fclone = SKB_FCLONE_UNAVAILABLE;
550
551 if (atomic_dec_and_test(fclone_ref))
552 kmem_cache_free(skbuff_fclone_cache, other);
553 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700557static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Eric Dumazetadf30902009-06-02 05:19:30 +0000559 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#ifdef CONFIG_XFRM
561 secpath_put(skb->sp);
562#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700563 if (skb->destructor) {
564 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 skb->destructor(skb);
566 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000567#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700568 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#ifdef CONFIG_BRIDGE_NETFILTER
571 nf_bridge_put(skb->nf_bridge);
572#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* XXX: IS this still necessary? - JHS */
574#ifdef CONFIG_NET_SCHED
575 skb->tc_index = 0;
576#ifdef CONFIG_NET_CLS_ACT
577 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700580}
581
582/* Free everything but the sk_buff shell. */
583static void skb_release_all(struct sk_buff *skb)
584{
585 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000586 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000587 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Herbert Xu2d4baff2007-11-26 23:11:19 +0800590/**
591 * __kfree_skb - private function
592 * @skb: buffer
593 *
594 * Free an sk_buff. Release anything attached to the buffer.
595 * Clean the state. This is an internal helper function. Users should
596 * always call kfree_skb
597 */
598
599void __kfree_skb(struct sk_buff *skb)
600{
601 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 kfree_skbmem(skb);
603}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800604EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800607 * kfree_skb - free an sk_buff
608 * @skb: buffer to free
609 *
610 * Drop a reference to the buffer and free it if the usage count has
611 * hit zero.
612 */
613void kfree_skb(struct sk_buff *skb)
614{
615 if (unlikely(!skb))
616 return;
617 if (likely(atomic_read(&skb->users) == 1))
618 smp_rmb();
619 else if (likely(!atomic_dec_and_test(&skb->users)))
620 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000621 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800622 __kfree_skb(skb);
623}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800624EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800625
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700626void kfree_skb_list(struct sk_buff *segs)
627{
628 while (segs) {
629 struct sk_buff *next = segs->next;
630
631 kfree_skb(segs);
632 segs = next;
633 }
634}
635EXPORT_SYMBOL(kfree_skb_list);
636
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700637/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000638 * skb_tx_error - report an sk_buff xmit error
639 * @skb: buffer that triggered an error
640 *
641 * Report xmit error if a device callback is tracking this skb.
642 * skb must be freed afterwards.
643 */
644void skb_tx_error(struct sk_buff *skb)
645{
646 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
647 struct ubuf_info *uarg;
648
649 uarg = skb_shinfo(skb)->destructor_arg;
650 if (uarg->callback)
651 uarg->callback(uarg, false);
652 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
653 }
654}
655EXPORT_SYMBOL(skb_tx_error);
656
657/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000658 * consume_skb - free an skbuff
659 * @skb: buffer to free
660 *
661 * Drop a ref to the buffer and free it if the usage count has hit zero
662 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
663 * is being dropped after a failure and notes that
664 */
665void consume_skb(struct sk_buff *skb)
666{
667 if (unlikely(!skb))
668 return;
669 if (likely(atomic_read(&skb->users) == 1))
670 smp_rmb();
671 else if (likely(!atomic_dec_and_test(&skb->users)))
672 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900673 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000674 __kfree_skb(skb);
675}
676EXPORT_SYMBOL(consume_skb);
677
Herbert Xudec18812007-10-14 00:37:30 -0700678static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
679{
680 new->tstamp = old->tstamp;
681 new->dev = old->dev;
682 new->transport_header = old->transport_header;
683 new->network_header = old->network_header;
684 new->mac_header = old->mac_header;
Joe Stringer4bc41b82013-07-03 16:04:25 +0900685 new->inner_protocol = old->inner_protocol;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000686 new->inner_transport_header = old->inner_transport_header;
Pravin B Shelar92df9b22013-02-01 15:18:49 +0000687 new->inner_network_header = old->inner_network_header;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000688 new->inner_mac_header = old->inner_mac_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000689 skb_dst_copy(new, old);
Tom Herbert3df7a742013-12-15 22:16:29 -0800690 skb_copy_hash(new, old);
Changli Gao6461be32011-08-19 04:44:18 +0000691 new->ooo_okay = old->ooo_okay;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000692 new->no_fcs = old->no_fcs;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000693 new->encapsulation = old->encapsulation;
Tom Herbert46fb51e2014-06-14 23:24:03 -0700694 new->encap_hdr_csum = old->encap_hdr_csum;
695 new->csum_valid = old->csum_valid;
696 new->csum_complete_sw = old->csum_complete_sw;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700697#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700698 new->sp = secpath_get(old->sp);
699#endif
700 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000701 new->csum = old->csum;
WANG Cong60ff7462014-05-04 16:39:18 -0700702 new->ignore_df = old->ignore_df;
Herbert Xudec18812007-10-14 00:37:30 -0700703 new->pkt_type = old->pkt_type;
704 new->ip_summed = old->ip_summed;
705 skb_copy_queue_mapping(new, old);
706 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000707#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700708 new->ipvs_property = old->ipvs_property;
709#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700710 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700711 new->protocol = old->protocol;
712 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800713 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700714 __nf_copy(new, old);
Herbert Xudec18812007-10-14 00:37:30 -0700715#ifdef CONFIG_NET_SCHED
716 new->tc_index = old->tc_index;
717#ifdef CONFIG_NET_CLS_ACT
718 new->tc_verd = old->tc_verd;
719#endif
720#endif
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000721 new->vlan_proto = old->vlan_proto;
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700722 new->vlan_tci = old->vlan_tci;
723
Herbert Xudec18812007-10-14 00:37:30 -0700724 skb_copy_secmark(new, old);
Eliezer Tamir06021292013-06-10 11:39:50 +0300725
Cong Wange0d10952013-08-01 11:10:25 +0800726#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +0300727 new->napi_id = old->napi_id;
728#endif
Herbert Xudec18812007-10-14 00:37:30 -0700729}
730
Herbert Xu82c49a32009-05-22 22:11:37 +0000731/*
732 * You should not add any new code to this function. Add it to
733 * __copy_skb_header above instead.
734 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700735static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#define C(x) n->x = skb->x
738
739 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700741 __copy_skb_header(n, skb);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 C(len);
744 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700745 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700746 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800747 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 C(tail);
751 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800752 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000753 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800754 C(data);
755 C(truesize);
756 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 atomic_inc(&(skb_shinfo(skb)->dataref));
759 skb->cloned = 1;
760
761 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700762#undef C
763}
764
765/**
766 * skb_morph - morph one skb into another
767 * @dst: the skb to receive the contents
768 * @src: the skb to supply the contents
769 *
770 * This is identical to skb_clone except that the target skb is
771 * supplied by the user.
772 *
773 * The target skb is returned upon exit.
774 */
775struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
776{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800777 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700778 return __skb_clone(dst, src);
779}
780EXPORT_SYMBOL_GPL(skb_morph);
781
Ben Hutchings2c530402012-07-10 10:55:09 +0000782/**
783 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000784 * @skb: the skb to modify
785 * @gfp_mask: allocation priority
786 *
787 * This must be called on SKBTX_DEV_ZEROCOPY skb.
788 * It will copy all frags into kernel and drop the reference
789 * to userspace pages.
790 *
791 * If this function is called from an interrupt gfp_mask() must be
792 * %GFP_ATOMIC.
793 *
794 * Returns 0 on success or a negative error code on failure
795 * to allocate kernel memory to copy to.
796 */
797int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000798{
799 int i;
800 int num_frags = skb_shinfo(skb)->nr_frags;
801 struct page *page, *head = NULL;
802 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
803
804 for (i = 0; i < num_frags; i++) {
805 u8 *vaddr;
806 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
807
Krishna Kumar02756ed2012-07-17 02:05:29 +0000808 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000809 if (!page) {
810 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900811 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000812 put_page(head);
813 head = next;
814 }
815 return -ENOMEM;
816 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200817 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000818 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000819 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200820 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900821 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000822 head = page;
823 }
824
825 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000826 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000827 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000828
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000829 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000830
831 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000832 for (i = num_frags - 1; i >= 0; i--) {
833 __skb_fill_page_desc(skb, i, head, 0,
834 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900835 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000836 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000837
838 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000839 return 0;
840}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000841EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000842
Herbert Xue0053ec2007-10-14 00:37:52 -0700843/**
844 * skb_clone - duplicate an sk_buff
845 * @skb: buffer to clone
846 * @gfp_mask: allocation priority
847 *
848 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
849 * copies share the same packet data but not structure. The new
850 * buffer has a reference count of 1. If the allocation fails the
851 * function returns %NULL otherwise the new buffer is returned.
852 *
853 * If this function is called from an interrupt gfp_mask() must be
854 * %GFP_ATOMIC.
855 */
856
857struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
858{
859 struct sk_buff *n;
860
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000861 if (skb_orphan_frags(skb, gfp_mask))
862 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000863
Herbert Xue0053ec2007-10-14 00:37:52 -0700864 n = skb + 1;
865 if (skb->fclone == SKB_FCLONE_ORIG &&
866 n->fclone == SKB_FCLONE_UNAVAILABLE) {
867 atomic_t *fclone_ref = (atomic_t *) (n + 1);
868 n->fclone = SKB_FCLONE_CLONE;
869 atomic_inc(fclone_ref);
870 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700871 if (skb_pfmemalloc(skb))
872 gfp_mask |= __GFP_MEMALLOC;
873
Herbert Xue0053ec2007-10-14 00:37:52 -0700874 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
875 if (!n)
876 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200877
878 kmemcheck_annotate_bitfield(n, flags1);
879 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700880 n->fclone = SKB_FCLONE_UNAVAILABLE;
881 }
882
883 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800885EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000887static void skb_headers_offset_update(struct sk_buff *skb, int off)
888{
Eric Dumazet030737b2013-10-19 11:42:54 -0700889 /* Only adjust this if it actually is csum_start rather than csum */
890 if (skb->ip_summed == CHECKSUM_PARTIAL)
891 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000892 /* {transport,network,mac}_header and tail are relative to skb->head */
893 skb->transport_header += off;
894 skb->network_header += off;
895 if (skb_mac_header_was_set(skb))
896 skb->mac_header += off;
897 skb->inner_transport_header += off;
898 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000899 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
903{
Herbert Xudec18812007-10-14 00:37:30 -0700904 __copy_skb_header(new, old);
905
Herbert Xu79671682006-06-22 02:40:14 -0700906 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
907 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
908 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Mel Gormanc93bdd02012-07-31 16:44:19 -0700911static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
912{
913 if (skb_pfmemalloc(skb))
914 return SKB_ALLOC_RX;
915 return 0;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/**
919 * skb_copy - create private copy of an sk_buff
920 * @skb: buffer to copy
921 * @gfp_mask: allocation priority
922 *
923 * Make a copy of both an &sk_buff and its data. This is used when the
924 * caller wishes to modify the data and needs a private copy of the
925 * data to alter. Returns %NULL on failure or the pointer to the buffer
926 * on success. The returned buffer has a reference count of 1.
927 *
928 * As by-product this function converts non-linear &sk_buff to linear
929 * one, so that &sk_buff becomes completely private and caller is allowed
930 * to modify all the data of returned buffer. This means that this
931 * function is not recommended for use in circumstances when only
932 * header is going to be modified. Use pskb_copy() instead.
933 */
934
Al Virodd0fc662005-10-07 07:46:04 +0100935struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000937 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000938 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700939 struct sk_buff *n = __alloc_skb(size, gfp_mask,
940 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!n)
943 return NULL;
944
945 /* Set the data pointer */
946 skb_reserve(n, headerlen);
947 /* Set the tail pointer and length */
948 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
951 BUG();
952
953 copy_skb_header(n, skb);
954 return n;
955}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800956EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958/**
Octavian Purdilabad93e92014-06-12 01:36:26 +0300959 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000961 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +0300963 * @fclone: if true allocate the copy of the skb from the fclone
964 * cache instead of the head cache; it is recommended to set this
965 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
967 * Make a copy of both an &sk_buff and part of its data, located
968 * in header. Fragmented data remain shared. This is used when
969 * the caller wishes to modify only header of &sk_buff and needs
970 * private copy of the header to alter. Returns %NULL on failure
971 * or the pointer to the buffer on success.
972 * The returned buffer has a reference count of 1.
973 */
974
Octavian Purdilabad93e92014-06-12 01:36:26 +0300975struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
976 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Eric Dumazet117632e2011-12-03 21:39:53 +0000978 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +0300979 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
980 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!n)
983 goto out;
984
985 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000986 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* Set the tail pointer and length */
988 skb_put(n, skb_headlen(skb));
989 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300990 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Herbert Xu25f484a2006-11-07 14:57:15 -0800992 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 n->data_len = skb->data_len;
994 n->len = skb->len;
995
996 if (skb_shinfo(skb)->nr_frags) {
997 int i;
998
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000999 if (skb_orphan_frags(skb, gfp_mask)) {
1000 kfree_skb(n);
1001 n = NULL;
1002 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1005 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001006 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 skb_shinfo(n)->nr_frags = i;
1009 }
1010
David S. Miller21dc3302010-08-23 00:13:46 -07001011 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1013 skb_clone_fraglist(n);
1014 }
1015
1016 copy_skb_header(n, skb);
1017out:
1018 return n;
1019}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001020EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022/**
1023 * pskb_expand_head - reallocate header of &sk_buff
1024 * @skb: buffer to reallocate
1025 * @nhead: room to add at head
1026 * @ntail: room to add at tail
1027 * @gfp_mask: allocation priority
1028 *
Mathias Krausebc323832013-11-07 14:18:26 +01001029 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1030 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * reference count of 1. Returns zero in the case of success or error,
1032 * if expansion failed. In the last case, &sk_buff is not changed.
1033 *
1034 * All the pointers pointing into skb header may change and must be
1035 * reloaded after call to this function.
1036 */
1037
Victor Fusco86a76ca2005-07-08 14:57:47 -07001038int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001039 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 int i;
1042 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001043 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 long off;
1045
Herbert Xu4edd87a2008-10-01 07:09:38 -07001046 BUG_ON(nhead < 0);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (skb_shared(skb))
1049 BUG();
1050
1051 size = SKB_DATA_ALIGN(size);
1052
Mel Gormanc93bdd02012-07-31 16:44:19 -07001053 if (skb_pfmemalloc(skb))
1054 gfp_mask |= __GFP_MEMALLOC;
1055 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1056 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (!data)
1058 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001059 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001062 * optimized for the cases when header is void.
1063 */
1064 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1065
1066 memcpy((struct skb_shared_info *)(data + size),
1067 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001068 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Alexander Duyck3e245912012-05-04 14:26:51 +00001070 /*
1071 * if shinfo is shared we must drop the old head gracefully, but if it
1072 * is not we can just drop the old head and let the existing refcount
1073 * be since all we did is relocate the values
1074 */
1075 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001076 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001077 if (skb_orphan_frags(skb, gfp_mask))
1078 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001079 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001080 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Eric Dumazet1fd63042010-09-02 23:09:32 +00001082 if (skb_has_frag_list(skb))
1083 skb_clone_fraglist(skb);
1084
1085 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001086 } else {
1087 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 off = (data + nhead) - skb->head;
1090
1091 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001092 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001094#ifdef NET_SKBUFF_DATA_USES_OFFSET
1095 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001096 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001097#else
1098 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001099#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001100 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001101 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001103 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 skb->nohdr = 0;
1105 atomic_set(&skb_shinfo(skb)->dataref, 1);
1106 return 0;
1107
Shirley Maa6686f22011-07-06 12:22:12 +00001108nofrags:
1109 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110nodata:
1111 return -ENOMEM;
1112}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001113EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115/* Make private copy of skb with writable head and some headroom */
1116
1117struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1118{
1119 struct sk_buff *skb2;
1120 int delta = headroom - skb_headroom(skb);
1121
1122 if (delta <= 0)
1123 skb2 = pskb_copy(skb, GFP_ATOMIC);
1124 else {
1125 skb2 = skb_clone(skb, GFP_ATOMIC);
1126 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1127 GFP_ATOMIC)) {
1128 kfree_skb(skb2);
1129 skb2 = NULL;
1130 }
1131 }
1132 return skb2;
1133}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001134EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136/**
1137 * skb_copy_expand - copy and expand sk_buff
1138 * @skb: buffer to copy
1139 * @newheadroom: new free bytes at head
1140 * @newtailroom: new free bytes at tail
1141 * @gfp_mask: allocation priority
1142 *
1143 * Make a copy of both an &sk_buff and its data and while doing so
1144 * allocate additional space.
1145 *
1146 * This is used when the caller wishes to modify the data and needs a
1147 * private copy of the data to alter as well as more space for new fields.
1148 * Returns %NULL on failure or the pointer to the buffer
1149 * on success. The returned buffer has a reference count of 1.
1150 *
1151 * You must pass %GFP_ATOMIC as the allocation priority if this function
1152 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
1154struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001155 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001156 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 /*
1159 * Allocate the copy buffer
1160 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001161 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1162 gfp_mask, skb_alloc_rx_flag(skb),
1163 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001164 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int head_copy_len, head_copy_off;
1166
1167 if (!n)
1168 return NULL;
1169
1170 skb_reserve(n, newheadroom);
1171
1172 /* Set the tail pointer and length */
1173 skb_put(n, skb->len);
1174
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001175 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 head_copy_off = 0;
1177 if (newheadroom <= head_copy_len)
1178 head_copy_len = newheadroom;
1179 else
1180 head_copy_off = newheadroom - head_copy_len;
1181
1182 /* Copy the linear header and data. */
1183 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1184 skb->len + head_copy_len))
1185 BUG();
1186
1187 copy_skb_header(n, skb);
1188
Eric Dumazet030737b2013-10-19 11:42:54 -07001189 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return n;
1192}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001193EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195/**
1196 * skb_pad - zero pad the tail of an skb
1197 * @skb: buffer to pad
1198 * @pad: space to pad
1199 *
1200 * Ensure that a buffer is followed by a padding area that is zero
1201 * filled. Used by network drivers which may DMA or transfer data
1202 * beyond the buffer end onto the wire.
1203 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001204 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001206
Herbert Xu5b057c62006-06-23 02:06:41 -07001207int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Herbert Xu5b057c62006-06-23 02:06:41 -07001209 int err;
1210 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001213 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001217
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001218 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001219 if (likely(skb_cloned(skb) || ntail > 0)) {
1220 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1221 if (unlikely(err))
1222 goto free_skb;
1223 }
1224
1225 /* FIXME: The use of this function with non-linear skb's really needs
1226 * to be audited.
1227 */
1228 err = skb_linearize(skb);
1229 if (unlikely(err))
1230 goto free_skb;
1231
1232 memset(skb->data + skb->len, 0, pad);
1233 return 0;
1234
1235free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001237 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001238}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001239EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001240
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001241/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001242 * pskb_put - add data to the tail of a potentially fragmented buffer
1243 * @skb: start of the buffer to use
1244 * @tail: tail fragment of the buffer to use
1245 * @len: amount of data to add
1246 *
1247 * This function extends the used data area of the potentially
1248 * fragmented buffer. @tail must be the last fragment of @skb -- or
1249 * @skb itself. If this would exceed the total buffer size the kernel
1250 * will panic. A pointer to the first byte of the extra data is
1251 * returned.
1252 */
1253
1254unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1255{
1256 if (tail != skb) {
1257 skb->data_len += len;
1258 skb->len += len;
1259 }
1260 return skb_put(tail, len);
1261}
1262EXPORT_SYMBOL_GPL(pskb_put);
1263
1264/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001265 * skb_put - add data to a buffer
1266 * @skb: buffer to use
1267 * @len: amount of data to add
1268 *
1269 * This function extends the used data area of the buffer. If this would
1270 * exceed the total buffer size the kernel will panic. A pointer to the
1271 * first byte of the extra data is returned.
1272 */
1273unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1274{
1275 unsigned char *tmp = skb_tail_pointer(skb);
1276 SKB_LINEAR_ASSERT(skb);
1277 skb->tail += len;
1278 skb->len += len;
1279 if (unlikely(skb->tail > skb->end))
1280 skb_over_panic(skb, len, __builtin_return_address(0));
1281 return tmp;
1282}
1283EXPORT_SYMBOL(skb_put);
1284
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001285/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001286 * skb_push - add data to the start of a buffer
1287 * @skb: buffer to use
1288 * @len: amount of data to add
1289 *
1290 * This function extends the used data area of the buffer at the buffer
1291 * start. If this would exceed the total buffer headroom the kernel will
1292 * panic. A pointer to the first byte of the extra data is returned.
1293 */
1294unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1295{
1296 skb->data -= len;
1297 skb->len += len;
1298 if (unlikely(skb->data<skb->head))
1299 skb_under_panic(skb, len, __builtin_return_address(0));
1300 return skb->data;
1301}
1302EXPORT_SYMBOL(skb_push);
1303
1304/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001305 * skb_pull - remove data from the start of a buffer
1306 * @skb: buffer to use
1307 * @len: amount of data to remove
1308 *
1309 * This function removes data from the start of a buffer, returning
1310 * the memory to the headroom. A pointer to the next data in the buffer
1311 * is returned. Once the data has been pulled future pushes will overwrite
1312 * the old data.
1313 */
1314unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1315{
David S. Miller47d29642010-05-02 02:21:44 -07001316 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001317}
1318EXPORT_SYMBOL(skb_pull);
1319
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001320/**
1321 * skb_trim - remove end from a buffer
1322 * @skb: buffer to alter
1323 * @len: new length
1324 *
1325 * Cut the length of a buffer down by removing data from the tail. If
1326 * the buffer is already under the length specified it is not modified.
1327 * The skb must be linear.
1328 */
1329void skb_trim(struct sk_buff *skb, unsigned int len)
1330{
1331 if (skb->len > len)
1332 __skb_trim(skb, len);
1333}
1334EXPORT_SYMBOL(skb_trim);
1335
Herbert Xu3cc0e872006-06-09 16:13:38 -07001336/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
1338
Herbert Xu3cc0e872006-06-09 16:13:38 -07001339int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Herbert Xu27b437c2006-07-13 19:26:39 -07001341 struct sk_buff **fragp;
1342 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int offset = skb_headlen(skb);
1344 int nfrags = skb_shinfo(skb)->nr_frags;
1345 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001346 int err;
1347
1348 if (skb_cloned(skb) &&
1349 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001352 i = 0;
1353 if (offset >= len)
1354 goto drop_pages;
1355
1356 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001357 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001358
1359 if (end < len) {
1360 offset = end;
1361 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001363
Eric Dumazet9e903e02011-10-18 21:00:24 +00001364 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001365
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001366drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001367 skb_shinfo(skb)->nr_frags = i;
1368
1369 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001370 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001371
David S. Miller21dc3302010-08-23 00:13:46 -07001372 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001373 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001374 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
Herbert Xu27b437c2006-07-13 19:26:39 -07001377 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1378 fragp = &frag->next) {
1379 int end = offset + frag->len;
1380
1381 if (skb_shared(frag)) {
1382 struct sk_buff *nfrag;
1383
1384 nfrag = skb_clone(frag, GFP_ATOMIC);
1385 if (unlikely(!nfrag))
1386 return -ENOMEM;
1387
1388 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001389 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001390 frag = nfrag;
1391 *fragp = frag;
1392 }
1393
1394 if (end < len) {
1395 offset = end;
1396 continue;
1397 }
1398
1399 if (end > len &&
1400 unlikely((err = pskb_trim(frag, len - offset))))
1401 return err;
1402
1403 if (frag->next)
1404 skb_drop_list(&frag->next);
1405 break;
1406 }
1407
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001408done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001409 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 skb->data_len -= skb->len - len;
1411 skb->len = len;
1412 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001413 skb->len = len;
1414 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001415 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 return 0;
1419}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001420EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422/**
1423 * __pskb_pull_tail - advance tail of skb header
1424 * @skb: buffer to reallocate
1425 * @delta: number of bytes to advance tail
1426 *
1427 * The function makes a sense only on a fragmented &sk_buff,
1428 * it expands header moving its tail forward and copying necessary
1429 * data from fragmented part.
1430 *
1431 * &sk_buff MUST have reference count of 1.
1432 *
1433 * Returns %NULL (and &sk_buff does not change) if pull failed
1434 * or value of new tail of skb in the case of success.
1435 *
1436 * All the pointers pointing into skb header may change and must be
1437 * reloaded after call to this function.
1438 */
1439
1440/* Moves tail of skb head forward, copying data from fragmented part,
1441 * when it is necessary.
1442 * 1. It may fail due to malloc failure.
1443 * 2. It may change skb pointers.
1444 *
1445 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1446 */
1447unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1448{
1449 /* If skb has not enough free space at tail, get new one
1450 * plus 128 bytes for future expansions. If we have enough
1451 * room at tail, reallocate without expansion only if skb is cloned.
1452 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001453 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (eat > 0 || skb_cloned(skb)) {
1456 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1457 GFP_ATOMIC))
1458 return NULL;
1459 }
1460
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001461 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 BUG();
1463
1464 /* Optimization: no fragments, no reasons to preestimate
1465 * size of pulled pages. Superb.
1466 */
David S. Miller21dc3302010-08-23 00:13:46 -07001467 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto pull_pages;
1469
1470 /* Estimate size of pulled pages. */
1471 eat = delta;
1472 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001473 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1474
1475 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001477 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479
1480 /* If we need update frag list, we are in troubles.
1481 * Certainly, it possible to add an offset to skb data,
1482 * but taking into account that pulling is expected to
1483 * be very rare operation, it is worth to fight against
1484 * further bloating skb head and crucify ourselves here instead.
1485 * Pure masohism, indeed. 8)8)
1486 */
1487 if (eat) {
1488 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1489 struct sk_buff *clone = NULL;
1490 struct sk_buff *insp = NULL;
1491
1492 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001493 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (list->len <= eat) {
1496 /* Eaten as whole. */
1497 eat -= list->len;
1498 list = list->next;
1499 insp = list;
1500 } else {
1501 /* Eaten partially. */
1502
1503 if (skb_shared(list)) {
1504 /* Sucks! We need to fork list. :-( */
1505 clone = skb_clone(list, GFP_ATOMIC);
1506 if (!clone)
1507 return NULL;
1508 insp = list->next;
1509 list = clone;
1510 } else {
1511 /* This may be pulled without
1512 * problems. */
1513 insp = list;
1514 }
1515 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001516 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return NULL;
1518 }
1519 break;
1520 }
1521 } while (eat);
1522
1523 /* Free pulled out fragments. */
1524 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1525 skb_shinfo(skb)->frag_list = list->next;
1526 kfree_skb(list);
1527 }
1528 /* And insert new clone at head. */
1529 if (clone) {
1530 clone->next = list;
1531 skb_shinfo(skb)->frag_list = clone;
1532 }
1533 }
1534 /* Success! Now we may commit changes to skb data. */
1535
1536pull_pages:
1537 eat = delta;
1538 k = 0;
1539 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001540 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1541
1542 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001543 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001544 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 } else {
1546 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1547 if (eat) {
1548 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001549 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 eat = 0;
1551 }
1552 k++;
1553 }
1554 }
1555 skb_shinfo(skb)->nr_frags = k;
1556
1557 skb->tail += delta;
1558 skb->data_len -= delta;
1559
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001560 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001562EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Eric Dumazet22019b12011-07-29 18:37:31 +00001564/**
1565 * skb_copy_bits - copy bits from skb to kernel buffer
1566 * @skb: source skb
1567 * @offset: offset in source
1568 * @to: destination buffer
1569 * @len: number of bytes to copy
1570 *
1571 * Copy the specified number of bytes from the source skb to the
1572 * destination buffer.
1573 *
1574 * CAUTION ! :
1575 * If its prototype is ever changed,
1576 * check arch/{*}/net/{*}.S files,
1577 * since it is called from BPF assembly code.
1578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1580{
David S. Miller1a028e52007-04-27 15:21:23 -07001581 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001582 struct sk_buff *frag_iter;
1583 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (offset > (int)skb->len - len)
1586 goto fault;
1587
1588 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001589 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (copy > len)
1591 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001592 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if ((len -= copy) == 0)
1594 return 0;
1595 offset += copy;
1596 to += copy;
1597 }
1598
1599 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001600 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001601 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001603 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001604
Eric Dumazet51c56b02012-04-05 11:35:15 +02001605 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if ((copy = end - offset) > 0) {
1607 u8 *vaddr;
1608
1609 if (copy > len)
1610 copy = len;
1611
Eric Dumazet51c56b02012-04-05 11:35:15 +02001612 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001614 vaddr + f->page_offset + offset - start,
1615 copy);
1616 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 if ((len -= copy) == 0)
1619 return 0;
1620 offset += copy;
1621 to += copy;
1622 }
David S. Miller1a028e52007-04-27 15:21:23 -07001623 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
David S. Millerfbb398a2009-06-09 00:18:59 -07001626 skb_walk_frags(skb, frag_iter) {
1627 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
David S. Millerfbb398a2009-06-09 00:18:59 -07001629 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
David S. Millerfbb398a2009-06-09 00:18:59 -07001631 end = start + frag_iter->len;
1632 if ((copy = end - offset) > 0) {
1633 if (copy > len)
1634 copy = len;
1635 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1636 goto fault;
1637 if ((len -= copy) == 0)
1638 return 0;
1639 offset += copy;
1640 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001642 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Shirley Maa6686f22011-07-06 12:22:12 +00001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (!len)
1646 return 0;
1647
1648fault:
1649 return -EFAULT;
1650}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001651EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jens Axboe9c55e012007-11-06 23:30:13 -08001653/*
1654 * Callback from splice_to_pipe(), if we need to release some pages
1655 * at the end of the spd in case we error'ed out in filling the pipe.
1656 */
1657static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1658{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001659 put_page(spd->pages[i]);
1660}
Jens Axboe9c55e012007-11-06 23:30:13 -08001661
David S. Millera108d5f2012-04-23 23:06:11 -04001662static struct page *linear_to_page(struct page *page, unsigned int *len,
1663 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001664 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001665{
Eric Dumazet5640f762012-09-23 23:04:42 +00001666 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001667
Eric Dumazet5640f762012-09-23 23:04:42 +00001668 if (!sk_page_frag_refill(sk, pfrag))
1669 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001670
Eric Dumazet5640f762012-09-23 23:04:42 +00001671 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001672
Eric Dumazet5640f762012-09-23 23:04:42 +00001673 memcpy(page_address(pfrag->page) + pfrag->offset,
1674 page_address(page) + *offset, *len);
1675 *offset = pfrag->offset;
1676 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001677
Eric Dumazet5640f762012-09-23 23:04:42 +00001678 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001679}
1680
Eric Dumazet41c73a02012-04-22 12:26:16 +00001681static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1682 struct page *page,
1683 unsigned int offset)
1684{
1685 return spd->nr_pages &&
1686 spd->pages[spd->nr_pages - 1] == page &&
1687 (spd->partial[spd->nr_pages - 1].offset +
1688 spd->partial[spd->nr_pages - 1].len == offset);
1689}
1690
Jens Axboe9c55e012007-11-06 23:30:13 -08001691/*
1692 * Fill page/offset/length into spd, if it can hold more pages.
1693 */
David S. Millera108d5f2012-04-23 23:06:11 -04001694static bool spd_fill_page(struct splice_pipe_desc *spd,
1695 struct pipe_inode_info *pipe, struct page *page,
1696 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001697 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001698 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001699{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001700 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001701 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001702
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001703 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001704 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001705 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001706 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001707 }
1708 if (spd_can_coalesce(spd, page, offset)) {
1709 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001710 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001711 }
1712 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001713 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001714 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001715 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001716 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001717
David S. Millera108d5f2012-04-23 23:06:11 -04001718 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001719}
1720
David S. Millera108d5f2012-04-23 23:06:11 -04001721static bool __splice_segment(struct page *page, unsigned int poff,
1722 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001723 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001724 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001725 struct sock *sk,
1726 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001727{
1728 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001729 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001730
1731 /* skip this segment if already processed */
1732 if (*off >= plen) {
1733 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001734 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001735 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001736
Octavian Purdila2870c432008-07-15 00:49:11 -07001737 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001738 poff += *off;
1739 plen -= *off;
1740 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001741
Eric Dumazet18aafc62013-01-11 14:46:37 +00001742 do {
1743 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001744
Eric Dumazet18aafc62013-01-11 14:46:37 +00001745 if (spd_fill_page(spd, pipe, page, &flen, poff,
1746 linear, sk))
1747 return true;
1748 poff += flen;
1749 plen -= flen;
1750 *len -= flen;
1751 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001752
David S. Millera108d5f2012-04-23 23:06:11 -04001753 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001754}
1755
1756/*
David S. Millera108d5f2012-04-23 23:06:11 -04001757 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001758 * pipe is full or if we already spliced the requested length.
1759 */
David S. Millera108d5f2012-04-23 23:06:11 -04001760static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1761 unsigned int *offset, unsigned int *len,
1762 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001763{
1764 int seg;
1765
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001766 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001767 * If skb->head_frag is set, this 'linear' part is backed by a
1768 * fragment, and if the head is not shared with any clones then
1769 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001770 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001771 if (__splice_segment(virt_to_page(skb->data),
1772 (unsigned long) skb->data & (PAGE_SIZE - 1),
1773 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001774 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001775 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001776 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001777 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001778
1779 /*
1780 * then map the fragments
1781 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001782 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1783 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1784
Ian Campbellea2ab692011-08-22 23:44:58 +00001785 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001786 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001787 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001788 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001789 }
1790
David S. Millera108d5f2012-04-23 23:06:11 -04001791 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001792}
1793
1794/*
1795 * Map data from the skb to a pipe. Should handle both the linear part,
1796 * the fragments, and the frag list. It does NOT handle frag lists within
1797 * the frag list, if such a thing exists. We'd probably need to recurse to
1798 * handle that cleanly.
1799 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001800int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001801 struct pipe_inode_info *pipe, unsigned int tlen,
1802 unsigned int flags)
1803{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001804 struct partial_page partial[MAX_SKB_FRAGS];
1805 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001806 struct splice_pipe_desc spd = {
1807 .pages = pages,
1808 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001809 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001810 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001811 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001812 .spd_release = sock_spd_release,
1813 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001814 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001815 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001816 int ret = 0;
1817
Jens Axboe9c55e012007-11-06 23:30:13 -08001818 /*
1819 * __skb_splice_bits() only fails if the output has no room left,
1820 * so no point in going over the frag_list for the error case.
1821 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001822 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001823 goto done;
1824 else if (!tlen)
1825 goto done;
1826
1827 /*
1828 * now see if we have a frag_list to map
1829 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001830 skb_walk_frags(skb, frag_iter) {
1831 if (!tlen)
1832 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001833 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001834 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001835 }
1836
1837done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001838 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001839 /*
1840 * Drop the socket lock, otherwise we have reverse
1841 * locking dependencies between sk_lock and i_mutex
1842 * here as compared to sendfile(). We enter here
1843 * with the socket lock held, and splice_to_pipe() will
1844 * grab the pipe inode lock. For sendfile() emulation,
1845 * we call into ->sendpage() with the i_mutex lock held
1846 * and networking will grab the socket lock.
1847 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001848 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001849 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001850 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001851 }
1852
Jens Axboe35f3d142010-05-20 10:43:18 +02001853 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001854}
1855
Herbert Xu357b40a2005-04-19 22:30:14 -07001856/**
1857 * skb_store_bits - store bits from kernel buffer to skb
1858 * @skb: destination buffer
1859 * @offset: offset in destination
1860 * @from: source buffer
1861 * @len: number of bytes to copy
1862 *
1863 * Copy the specified number of bytes from the source buffer to the
1864 * destination skb. This function handles all the messy bits of
1865 * traversing fragment lists and such.
1866 */
1867
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001868int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001869{
David S. Miller1a028e52007-04-27 15:21:23 -07001870 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001871 struct sk_buff *frag_iter;
1872 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001873
1874 if (offset > (int)skb->len - len)
1875 goto fault;
1876
David S. Miller1a028e52007-04-27 15:21:23 -07001877 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001878 if (copy > len)
1879 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001880 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001881 if ((len -= copy) == 0)
1882 return 0;
1883 offset += copy;
1884 from += copy;
1885 }
1886
1887 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1888 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001889 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001890
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001891 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001892
Eric Dumazet9e903e02011-10-18 21:00:24 +00001893 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001894 if ((copy = end - offset) > 0) {
1895 u8 *vaddr;
1896
1897 if (copy > len)
1898 copy = len;
1899
Eric Dumazet51c56b02012-04-05 11:35:15 +02001900 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001901 memcpy(vaddr + frag->page_offset + offset - start,
1902 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001903 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001904
1905 if ((len -= copy) == 0)
1906 return 0;
1907 offset += copy;
1908 from += copy;
1909 }
David S. Miller1a028e52007-04-27 15:21:23 -07001910 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001911 }
1912
David S. Millerfbb398a2009-06-09 00:18:59 -07001913 skb_walk_frags(skb, frag_iter) {
1914 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001915
David S. Millerfbb398a2009-06-09 00:18:59 -07001916 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001917
David S. Millerfbb398a2009-06-09 00:18:59 -07001918 end = start + frag_iter->len;
1919 if ((copy = end - offset) > 0) {
1920 if (copy > len)
1921 copy = len;
1922 if (skb_store_bits(frag_iter, offset - start,
1923 from, copy))
1924 goto fault;
1925 if ((len -= copy) == 0)
1926 return 0;
1927 offset += copy;
1928 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001929 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001930 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001931 }
1932 if (!len)
1933 return 0;
1934
1935fault:
1936 return -EFAULT;
1937}
Herbert Xu357b40a2005-04-19 22:30:14 -07001938EXPORT_SYMBOL(skb_store_bits);
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001941__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1942 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
David S. Miller1a028e52007-04-27 15:21:23 -07001944 int start = skb_headlen(skb);
1945 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001946 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 int pos = 0;
1948
1949 /* Checksum header. */
1950 if (copy > 0) {
1951 if (copy > len)
1952 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001953 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if ((len -= copy) == 0)
1955 return csum;
1956 offset += copy;
1957 pos = copy;
1958 }
1959
1960 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001961 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001962 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001964 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001965
Eric Dumazet51c56b02012-04-05 11:35:15 +02001966 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001968 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 if (copy > len)
1972 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001973 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01001974 csum2 = ops->update(vaddr + frag->page_offset +
1975 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001976 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01001977 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (!(len -= copy))
1979 return csum;
1980 offset += copy;
1981 pos += copy;
1982 }
David S. Miller1a028e52007-04-27 15:21:23 -07001983 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
David S. Millerfbb398a2009-06-09 00:18:59 -07001986 skb_walk_frags(skb, frag_iter) {
1987 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
David S. Millerfbb398a2009-06-09 00:18:59 -07001989 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
David S. Millerfbb398a2009-06-09 00:18:59 -07001991 end = start + frag_iter->len;
1992 if ((copy = end - offset) > 0) {
1993 __wsum csum2;
1994 if (copy > len)
1995 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001996 csum2 = __skb_checksum(frag_iter, offset - start,
1997 copy, 0, ops);
1998 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07001999 if ((len -= copy) == 0)
2000 return csum;
2001 offset += copy;
2002 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002004 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002006 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 return csum;
2009}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002010EXPORT_SYMBOL(__skb_checksum);
2011
2012__wsum skb_checksum(const struct sk_buff *skb, int offset,
2013 int len, __wsum csum)
2014{
2015 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea82013-11-04 17:10:25 +01002016 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002017 .combine = csum_block_add_ext,
2018 };
2019
2020 return __skb_checksum(skb, offset, len, csum, &ops);
2021}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002022EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024/* Both of above in one bottle. */
2025
Al Viro81d77662006-11-14 21:37:33 -08002026__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2027 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
David S. Miller1a028e52007-04-27 15:21:23 -07002029 int start = skb_headlen(skb);
2030 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002031 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int pos = 0;
2033
2034 /* Copy header. */
2035 if (copy > 0) {
2036 if (copy > len)
2037 copy = len;
2038 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2039 copy, csum);
2040 if ((len -= copy) == 0)
2041 return csum;
2042 offset += copy;
2043 to += copy;
2044 pos = copy;
2045 }
2046
2047 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002048 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002050 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002051
Eric Dumazet9e903e02011-10-18 21:00:24 +00002052 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002054 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 u8 *vaddr;
2056 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2057
2058 if (copy > len)
2059 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002060 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002062 frag->page_offset +
2063 offset - start, to,
2064 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002065 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 csum = csum_block_add(csum, csum2, pos);
2067 if (!(len -= copy))
2068 return csum;
2069 offset += copy;
2070 to += copy;
2071 pos += copy;
2072 }
David S. Miller1a028e52007-04-27 15:21:23 -07002073 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075
David S. Millerfbb398a2009-06-09 00:18:59 -07002076 skb_walk_frags(skb, frag_iter) {
2077 __wsum csum2;
2078 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
David S. Millerfbb398a2009-06-09 00:18:59 -07002080 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
David S. Millerfbb398a2009-06-09 00:18:59 -07002082 end = start + frag_iter->len;
2083 if ((copy = end - offset) > 0) {
2084 if (copy > len)
2085 copy = len;
2086 csum2 = skb_copy_and_csum_bits(frag_iter,
2087 offset - start,
2088 to, copy, 0);
2089 csum = csum_block_add(csum, csum2, pos);
2090 if ((len -= copy) == 0)
2091 return csum;
2092 offset += copy;
2093 to += copy;
2094 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002096 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002098 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return csum;
2100}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002101EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Thomas Grafaf2806f2013-12-13 15:22:17 +01002103 /**
2104 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2105 * @from: source buffer
2106 *
2107 * Calculates the amount of linear headroom needed in the 'to' skb passed
2108 * into skb_zerocopy().
2109 */
2110unsigned int
2111skb_zerocopy_headlen(const struct sk_buff *from)
2112{
2113 unsigned int hlen = 0;
2114
2115 if (!from->head_frag ||
2116 skb_headlen(from) < L1_CACHE_BYTES ||
2117 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2118 hlen = skb_headlen(from);
2119
2120 if (skb_has_frag_list(from))
2121 hlen = from->len;
2122
2123 return hlen;
2124}
2125EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2126
2127/**
2128 * skb_zerocopy - Zero copy skb to skb
2129 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002130 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002131 * @len: number of bytes to copy from source buffer
2132 * @hlen: size of linear headroom in destination buffer
2133 *
2134 * Copies up to `len` bytes from `from` to `to` by creating references
2135 * to the frags in the source buffer.
2136 *
2137 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2138 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002139 *
2140 * Return value:
2141 * 0: everything is OK
2142 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2143 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002144 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002145int
2146skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002147{
2148 int i, j = 0;
2149 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002150 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002151 struct page *page;
2152 unsigned int offset;
2153
2154 BUG_ON(!from->head_frag && !hlen);
2155
2156 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002157 if (len <= skb_tailroom(to))
2158 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002159
2160 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002161 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2162 if (unlikely(ret))
2163 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002164 len -= hlen;
2165 } else {
2166 plen = min_t(int, skb_headlen(from), len);
2167 if (plen) {
2168 page = virt_to_head_page(from->head);
2169 offset = from->data - (unsigned char *)page_address(page);
2170 __skb_fill_page_desc(to, 0, page, offset, plen);
2171 get_page(page);
2172 j = 1;
2173 len -= plen;
2174 }
2175 }
2176
2177 to->truesize += len + plen;
2178 to->len += len + plen;
2179 to->data_len += len + plen;
2180
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002181 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2182 skb_tx_error(from);
2183 return -ENOMEM;
2184 }
2185
Thomas Grafaf2806f2013-12-13 15:22:17 +01002186 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2187 if (!len)
2188 break;
2189 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2190 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2191 len -= skb_shinfo(to)->frags[j].size;
2192 skb_frag_ref(to, j);
2193 j++;
2194 }
2195 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002196
2197 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002198}
2199EXPORT_SYMBOL_GPL(skb_zerocopy);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2202{
Al Virod3bc23e2006-11-14 21:24:49 -08002203 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 long csstart;
2205
Patrick McHardy84fa7932006-08-29 16:44:56 -07002206 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002207 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 else
2209 csstart = skb_headlen(skb);
2210
Kris Katterjohn09a62662006-01-08 22:24:28 -08002211 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002213 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 csum = 0;
2216 if (csstart != skb->len)
2217 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2218 skb->len - csstart, 0);
2219
Patrick McHardy84fa7932006-08-29 16:44:56 -07002220 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002221 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Al Virod3bc23e2006-11-14 21:24:49 -08002223 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002226EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228/**
2229 * skb_dequeue - remove from the head of the queue
2230 * @list: list to dequeue from
2231 *
2232 * Remove the head of the list. The list lock is taken so the function
2233 * may be used safely with other locking list functions. The head item is
2234 * returned or %NULL if the list is empty.
2235 */
2236
2237struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2238{
2239 unsigned long flags;
2240 struct sk_buff *result;
2241
2242 spin_lock_irqsave(&list->lock, flags);
2243 result = __skb_dequeue(list);
2244 spin_unlock_irqrestore(&list->lock, flags);
2245 return result;
2246}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002247EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249/**
2250 * skb_dequeue_tail - remove from the tail of the queue
2251 * @list: list to dequeue from
2252 *
2253 * Remove the tail of the list. The list lock is taken so the function
2254 * may be used safely with other locking list functions. The tail item is
2255 * returned or %NULL if the list is empty.
2256 */
2257struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2258{
2259 unsigned long flags;
2260 struct sk_buff *result;
2261
2262 spin_lock_irqsave(&list->lock, flags);
2263 result = __skb_dequeue_tail(list);
2264 spin_unlock_irqrestore(&list->lock, flags);
2265 return result;
2266}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002267EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269/**
2270 * skb_queue_purge - empty a list
2271 * @list: list to empty
2272 *
2273 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2274 * the list and one reference dropped. This function takes the list
2275 * lock and is atomic with respect to other list locking functions.
2276 */
2277void skb_queue_purge(struct sk_buff_head *list)
2278{
2279 struct sk_buff *skb;
2280 while ((skb = skb_dequeue(list)) != NULL)
2281 kfree_skb(skb);
2282}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002283EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285/**
2286 * skb_queue_head - queue a buffer at the list head
2287 * @list: list to use
2288 * @newsk: buffer to queue
2289 *
2290 * Queue a buffer at the start of the list. This function takes the
2291 * list lock and can be used safely with other locking &sk_buff functions
2292 * safely.
2293 *
2294 * A buffer cannot be placed on two lists at the same time.
2295 */
2296void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2297{
2298 unsigned long flags;
2299
2300 spin_lock_irqsave(&list->lock, flags);
2301 __skb_queue_head(list, newsk);
2302 spin_unlock_irqrestore(&list->lock, flags);
2303}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002304EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306/**
2307 * skb_queue_tail - queue a buffer at the list tail
2308 * @list: list to use
2309 * @newsk: buffer to queue
2310 *
2311 * Queue a buffer at the tail of the list. This function takes the
2312 * list lock and can be used safely with other locking &sk_buff functions
2313 * safely.
2314 *
2315 * A buffer cannot be placed on two lists at the same time.
2316 */
2317void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2318{
2319 unsigned long flags;
2320
2321 spin_lock_irqsave(&list->lock, flags);
2322 __skb_queue_tail(list, newsk);
2323 spin_unlock_irqrestore(&list->lock, flags);
2324}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002325EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/**
2328 * skb_unlink - remove a buffer from a list
2329 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002330 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 *
David S. Miller8728b832005-08-09 19:25:21 -07002332 * Remove a packet from a list. The list locks are taken and this
2333 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 *
David S. Miller8728b832005-08-09 19:25:21 -07002335 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 */
David S. Miller8728b832005-08-09 19:25:21 -07002337void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
David S. Miller8728b832005-08-09 19:25:21 -07002339 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
David S. Miller8728b832005-08-09 19:25:21 -07002341 spin_lock_irqsave(&list->lock, flags);
2342 __skb_unlink(skb, list);
2343 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002345EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/**
2348 * skb_append - append a buffer
2349 * @old: buffer to insert after
2350 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002351 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 *
2353 * Place a packet after a given packet in a list. The list locks are taken
2354 * and this function is atomic with respect to other list locked calls.
2355 * A buffer cannot be placed on two lists at the same time.
2356 */
David S. Miller8728b832005-08-09 19:25:21 -07002357void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 unsigned long flags;
2360
David S. Miller8728b832005-08-09 19:25:21 -07002361 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002362 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002363 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002365EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367/**
2368 * skb_insert - insert a buffer
2369 * @old: buffer to insert before
2370 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002371 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *
David S. Miller8728b832005-08-09 19:25:21 -07002373 * Place a packet before a given packet in a list. The list locks are
2374 * taken and this function is atomic with respect to other list locked
2375 * calls.
2376 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 * A buffer cannot be placed on two lists at the same time.
2378 */
David S. Miller8728b832005-08-09 19:25:21 -07002379void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
2381 unsigned long flags;
2382
David S. Miller8728b832005-08-09 19:25:21 -07002383 spin_lock_irqsave(&list->lock, flags);
2384 __skb_insert(newsk, old->prev, old, list);
2385 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002387EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389static inline void skb_split_inside_header(struct sk_buff *skb,
2390 struct sk_buff* skb1,
2391 const u32 len, const int pos)
2392{
2393 int i;
2394
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002395 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2396 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* And move data appendix as is. */
2398 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2399 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2400
2401 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2402 skb_shinfo(skb)->nr_frags = 0;
2403 skb1->data_len = skb->data_len;
2404 skb1->len += skb1->data_len;
2405 skb->data_len = 0;
2406 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002407 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410static inline void skb_split_no_header(struct sk_buff *skb,
2411 struct sk_buff* skb1,
2412 const u32 len, int pos)
2413{
2414 int i, k = 0;
2415 const int nfrags = skb_shinfo(skb)->nr_frags;
2416
2417 skb_shinfo(skb)->nr_frags = 0;
2418 skb1->len = skb1->data_len = skb->len - len;
2419 skb->len = len;
2420 skb->data_len = len - pos;
2421
2422 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002423 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 if (pos + size > len) {
2426 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2427
2428 if (pos < len) {
2429 /* Split frag.
2430 * We have two variants in this case:
2431 * 1. Move all the frag to the second
2432 * part, if it is possible. F.e.
2433 * this approach is mandatory for TUX,
2434 * where splitting is expensive.
2435 * 2. Split is accurately. We make this.
2436 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002437 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002439 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2440 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 skb_shinfo(skb)->nr_frags++;
2442 }
2443 k++;
2444 } else
2445 skb_shinfo(skb)->nr_frags++;
2446 pos += size;
2447 }
2448 skb_shinfo(skb1)->nr_frags = k;
2449}
2450
2451/**
2452 * skb_split - Split fragmented skb to two parts at length len.
2453 * @skb: the buffer to split
2454 * @skb1: the buffer to receive the second part
2455 * @len: new length for skb
2456 */
2457void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2458{
2459 int pos = skb_headlen(skb);
2460
Amerigo Wang68534c62013-02-19 22:51:30 +00002461 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (len < pos) /* Split line is inside header. */
2463 skb_split_inside_header(skb, skb1, len, pos);
2464 else /* Second chunk has no header, nothing to copy. */
2465 skb_split_no_header(skb, skb1, len, pos);
2466}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002467EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002469/* Shifting from/to a cloned skb is a no-go.
2470 *
2471 * Caller cannot keep skb_shinfo related pointers past calling here!
2472 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002473static int skb_prepare_for_shift(struct sk_buff *skb)
2474{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002475 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002476}
2477
2478/**
2479 * skb_shift - Shifts paged data partially from skb to another
2480 * @tgt: buffer into which tail data gets added
2481 * @skb: buffer from which the paged data comes from
2482 * @shiftlen: shift up to this many bytes
2483 *
2484 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002485 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002486 * It's up to caller to free skb if everything was shifted.
2487 *
2488 * If @tgt runs out of frags, the whole operation is aborted.
2489 *
2490 * Skb cannot include anything else but paged data while tgt is allowed
2491 * to have non-paged data as well.
2492 *
2493 * TODO: full sized shift could be optimized but that would need
2494 * specialized skb free'er to handle frags without up-to-date nr_frags.
2495 */
2496int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2497{
2498 int from, to, merge, todo;
2499 struct skb_frag_struct *fragfrom, *fragto;
2500
2501 BUG_ON(shiftlen > skb->len);
2502 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2503
2504 todo = shiftlen;
2505 from = 0;
2506 to = skb_shinfo(tgt)->nr_frags;
2507 fragfrom = &skb_shinfo(skb)->frags[from];
2508
2509 /* Actual merge is delayed until the point when we know we can
2510 * commit all, so that we don't have to undo partial changes
2511 */
2512 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002513 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2514 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002515 merge = -1;
2516 } else {
2517 merge = to - 1;
2518
Eric Dumazet9e903e02011-10-18 21:00:24 +00002519 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002520 if (todo < 0) {
2521 if (skb_prepare_for_shift(skb) ||
2522 skb_prepare_for_shift(tgt))
2523 return 0;
2524
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002525 /* All previous frag pointers might be stale! */
2526 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002527 fragto = &skb_shinfo(tgt)->frags[merge];
2528
Eric Dumazet9e903e02011-10-18 21:00:24 +00002529 skb_frag_size_add(fragto, shiftlen);
2530 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002531 fragfrom->page_offset += shiftlen;
2532
2533 goto onlymerged;
2534 }
2535
2536 from++;
2537 }
2538
2539 /* Skip full, not-fitting skb to avoid expensive operations */
2540 if ((shiftlen == skb->len) &&
2541 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2542 return 0;
2543
2544 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2545 return 0;
2546
2547 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2548 if (to == MAX_SKB_FRAGS)
2549 return 0;
2550
2551 fragfrom = &skb_shinfo(skb)->frags[from];
2552 fragto = &skb_shinfo(tgt)->frags[to];
2553
Eric Dumazet9e903e02011-10-18 21:00:24 +00002554 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002555 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002556 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002557 from++;
2558 to++;
2559
2560 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002561 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002562 fragto->page = fragfrom->page;
2563 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002564 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002565
2566 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002567 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002568 todo = 0;
2569
2570 to++;
2571 break;
2572 }
2573 }
2574
2575 /* Ready to "commit" this state change to tgt */
2576 skb_shinfo(tgt)->nr_frags = to;
2577
2578 if (merge >= 0) {
2579 fragfrom = &skb_shinfo(skb)->frags[0];
2580 fragto = &skb_shinfo(tgt)->frags[merge];
2581
Eric Dumazet9e903e02011-10-18 21:00:24 +00002582 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002583 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002584 }
2585
2586 /* Reposition in the original skb */
2587 to = 0;
2588 while (from < skb_shinfo(skb)->nr_frags)
2589 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2590 skb_shinfo(skb)->nr_frags = to;
2591
2592 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2593
2594onlymerged:
2595 /* Most likely the tgt won't ever need its checksum anymore, skb on
2596 * the other hand might need it if it needs to be resent
2597 */
2598 tgt->ip_summed = CHECKSUM_PARTIAL;
2599 skb->ip_summed = CHECKSUM_PARTIAL;
2600
2601 /* Yak, is it really working this way? Some helper please? */
2602 skb->len -= shiftlen;
2603 skb->data_len -= shiftlen;
2604 skb->truesize -= shiftlen;
2605 tgt->len += shiftlen;
2606 tgt->data_len += shiftlen;
2607 tgt->truesize += shiftlen;
2608
2609 return shiftlen;
2610}
2611
Thomas Graf677e90e2005-06-23 20:59:51 -07002612/**
2613 * skb_prepare_seq_read - Prepare a sequential read of skb data
2614 * @skb: the buffer to read
2615 * @from: lower offset of data to be read
2616 * @to: upper offset of data to be read
2617 * @st: state variable
2618 *
2619 * Initializes the specified state variable. Must be called before
2620 * invoking skb_seq_read() for the first time.
2621 */
2622void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2623 unsigned int to, struct skb_seq_state *st)
2624{
2625 st->lower_offset = from;
2626 st->upper_offset = to;
2627 st->root_skb = st->cur_skb = skb;
2628 st->frag_idx = st->stepped_offset = 0;
2629 st->frag_data = NULL;
2630}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002631EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002632
2633/**
2634 * skb_seq_read - Sequentially read skb data
2635 * @consumed: number of bytes consumed by the caller so far
2636 * @data: destination pointer for data to be returned
2637 * @st: state variable
2638 *
Mathias Krausebc323832013-11-07 14:18:26 +01002639 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002640 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002641 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002642 * of the block or 0 if the end of the skb data or the upper
2643 * offset has been reached.
2644 *
2645 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002646 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002647 * of bytes already consumed and the next call to
2648 * skb_seq_read() will return the remaining part of the block.
2649 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002650 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002651 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002652 * reads of potentially non linear data.
2653 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002654 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002655 * at the moment, state->root_skb could be replaced with
2656 * a stack for this purpose.
2657 */
2658unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2659 struct skb_seq_state *st)
2660{
2661 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2662 skb_frag_t *frag;
2663
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002664 if (unlikely(abs_offset >= st->upper_offset)) {
2665 if (st->frag_data) {
2666 kunmap_atomic(st->frag_data);
2667 st->frag_data = NULL;
2668 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002669 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002670 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002671
2672next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002673 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002674
Thomas Chenault995b3372009-05-18 21:43:27 -07002675 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002676 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002677 return block_limit - abs_offset;
2678 }
2679
2680 if (st->frag_idx == 0 && !st->frag_data)
2681 st->stepped_offset += skb_headlen(st->cur_skb);
2682
2683 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2684 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002685 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002686
2687 if (abs_offset < block_limit) {
2688 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002689 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002690
2691 *data = (u8 *) st->frag_data + frag->page_offset +
2692 (abs_offset - st->stepped_offset);
2693
2694 return block_limit - abs_offset;
2695 }
2696
2697 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002698 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002699 st->frag_data = NULL;
2700 }
2701
2702 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002703 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002704 }
2705
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002706 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002707 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002708 st->frag_data = NULL;
2709 }
2710
David S. Miller21dc3302010-08-23 00:13:46 -07002711 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002712 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002713 st->frag_idx = 0;
2714 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002715 } else if (st->cur_skb->next) {
2716 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002717 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002718 goto next_skb;
2719 }
2720
2721 return 0;
2722}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002723EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002724
2725/**
2726 * skb_abort_seq_read - Abort a sequential read of skb data
2727 * @st: state variable
2728 *
2729 * Must be called if skb_seq_read() was not called until it
2730 * returned 0.
2731 */
2732void skb_abort_seq_read(struct skb_seq_state *st)
2733{
2734 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002735 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002736}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002737EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002738
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002739#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2740
2741static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2742 struct ts_config *conf,
2743 struct ts_state *state)
2744{
2745 return skb_seq_read(offset, text, TS_SKB_CB(state));
2746}
2747
2748static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2749{
2750 skb_abort_seq_read(TS_SKB_CB(state));
2751}
2752
2753/**
2754 * skb_find_text - Find a text pattern in skb data
2755 * @skb: the buffer to look in
2756 * @from: search offset
2757 * @to: search limit
2758 * @config: textsearch configuration
2759 * @state: uninitialized textsearch state variable
2760 *
2761 * Finds a pattern in the skb data according to the specified
2762 * textsearch configuration. Use textsearch_next() to retrieve
2763 * subsequent occurrences of the pattern. Returns the offset
2764 * to the first occurrence or UINT_MAX if no match was found.
2765 */
2766unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2767 unsigned int to, struct ts_config *config,
2768 struct ts_state *state)
2769{
Phil Oesterf72b9482006-06-26 00:00:57 -07002770 unsigned int ret;
2771
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002772 config->get_next_block = skb_ts_get_next_block;
2773 config->finish = skb_ts_finish;
2774
2775 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2776
Phil Oesterf72b9482006-06-26 00:00:57 -07002777 ret = textsearch_find(config, state);
2778 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002779}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002780EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002781
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002782/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002783 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002784 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002785 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002786 * @getfrag: call back function to be used for getting the user data
2787 * @from: pointer to user message iov
2788 * @length: length of the iov message
2789 *
2790 * Description: This procedure append the user data in the fragment part
2791 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2792 */
2793int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002794 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002795 int len, int odd, struct sk_buff *skb),
2796 void *from, int length)
2797{
Eric Dumazetb2111722012-12-28 06:06:37 +00002798 int frg_cnt = skb_shinfo(skb)->nr_frags;
2799 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002800 int offset = 0;
2801 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002802 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002803
2804 do {
2805 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002806 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002807 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002808
Eric Dumazetb2111722012-12-28 06:06:37 +00002809 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002810 return -ENOMEM;
2811
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002812 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002813 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002814
Eric Dumazetb2111722012-12-28 06:06:37 +00002815 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2816 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002817 if (ret < 0)
2818 return -EFAULT;
2819
2820 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002821 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2822 copy);
2823 frg_cnt++;
2824 pfrag->offset += copy;
2825 get_page(pfrag->page);
2826
2827 skb->truesize += copy;
2828 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002829 skb->len += copy;
2830 skb->data_len += copy;
2831 offset += copy;
2832 length -= copy;
2833
2834 } while (length > 0);
2835
2836 return 0;
2837}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002838EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002839
Herbert Xucbb042f92006-03-20 22:43:56 -08002840/**
2841 * skb_pull_rcsum - pull skb and update receive checksum
2842 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08002843 * @len: length of data pulled
2844 *
2845 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002846 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002847 * receive path processing instead of skb_pull unless you know
2848 * that the checksum difference is zero (e.g., a valid IP header)
2849 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08002850 */
2851unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2852{
2853 BUG_ON(len > skb->len);
2854 skb->len -= len;
2855 BUG_ON(skb->len < skb->data_len);
2856 skb_postpull_rcsum(skb, skb->data, len);
2857 return skb->data += len;
2858}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002859EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2860
Herbert Xuf4c50d92006-06-22 03:02:40 -07002861/**
2862 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002863 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002864 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002865 *
2866 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002867 * a pointer to the first in a list of new skbs for the segments.
2868 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002869 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002870struct sk_buff *skb_segment(struct sk_buff *head_skb,
2871 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002872{
2873 struct sk_buff *segs = NULL;
2874 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002875 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002876 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2877 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2878 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002879 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002880 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002881 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002882 unsigned int headroom;
2883 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002884 __be16 proto;
2885 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002886 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002887 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002888 int err = -ENOMEM;
2889 int i = 0;
2890 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002891 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002892
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002893 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002894 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002895 if (unlikely(!proto))
2896 return ERR_PTR(-EINVAL);
2897
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002898 csum = !head_skb->encap_hdr_csum &&
2899 !!can_checksum_protocol(features, proto);
2900
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002901 headroom = skb_headroom(head_skb);
2902 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002903
2904 do {
2905 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002906 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002907 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002908 int size;
2909
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002910 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002911 if (len > mss)
2912 len = mss;
2913
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002914 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002915 if (hsize < 0)
2916 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002917 if (hsize > len || !sg)
2918 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002919
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002920 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2921 (skb_headlen(list_skb) == len || sg)) {
2922 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002923
Herbert Xu9d8506c2013-11-21 11:10:04 -08002924 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002925 nfrags = skb_shinfo(list_skb)->nr_frags;
2926 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002927 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002928 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002929
2930 while (pos < offset + len) {
2931 BUG_ON(i >= nfrags);
2932
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002933 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002934 if (pos + size > offset + len)
2935 break;
2936
2937 i++;
2938 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002939 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002940 }
2941
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002942 nskb = skb_clone(list_skb, GFP_ATOMIC);
2943 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002944
2945 if (unlikely(!nskb))
2946 goto err;
2947
Herbert Xu9d8506c2013-11-21 11:10:04 -08002948 if (unlikely(pskb_trim(nskb, len))) {
2949 kfree_skb(nskb);
2950 goto err;
2951 }
2952
Alexander Duyckec47ea82012-05-04 14:26:56 +00002953 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002954 if (skb_cow_head(nskb, doffset + headroom)) {
2955 kfree_skb(nskb);
2956 goto err;
2957 }
2958
Alexander Duyckec47ea82012-05-04 14:26:56 +00002959 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002960 skb_release_head_state(nskb);
2961 __skb_push(nskb, doffset);
2962 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002963 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002964 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07002965 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002966
2967 if (unlikely(!nskb))
2968 goto err;
2969
2970 skb_reserve(nskb, headroom);
2971 __skb_put(nskb, doffset);
2972 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002973
2974 if (segs)
2975 tail->next = nskb;
2976 else
2977 segs = nskb;
2978 tail = nskb;
2979
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002980 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002981
Eric Dumazet030737b2013-10-19 11:42:54 -07002982 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04002983 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00002984
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002985 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00002986 nskb->data - tnl_hlen,
2987 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08002988
Herbert Xu9d8506c2013-11-21 11:10:04 -08002989 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00002990 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08002991
Herbert Xuf4c50d92006-06-22 03:02:40 -07002992 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002993 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002994 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07002995 skb_put(nskb, len),
2996 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002997 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07002998 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002999 continue;
3000 }
3001
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003002 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003003
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003004 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003005 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003006
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003007 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3008 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003009
Herbert Xu9d8506c2013-11-21 11:10:04 -08003010 while (pos < offset + len) {
3011 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003012 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003013
3014 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003015 nfrags = skb_shinfo(list_skb)->nr_frags;
3016 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003017 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003018
3019 BUG_ON(!nfrags);
3020
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003021 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003022 }
3023
3024 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3025 MAX_SKB_FRAGS)) {
3026 net_warn_ratelimited(
3027 "skb_segment: too many frags: %u %u\n",
3028 pos, mss);
3029 goto err;
3030 }
3031
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003032 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3033 goto err;
3034
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003035 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003036 __skb_frag_ref(nskb_frag);
3037 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003038
3039 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003040 nskb_frag->page_offset += offset - pos;
3041 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003042 }
3043
Herbert Xu89319d382008-12-15 23:26:06 -08003044 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003045
3046 if (pos + size <= offset + len) {
3047 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003048 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003049 pos += size;
3050 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003051 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003052 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003053 }
3054
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003055 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003056 }
3057
Herbert Xu89319d382008-12-15 23:26:06 -08003058skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003059 nskb->data_len = len - hsize;
3060 nskb->len += nskb->data_len;
3061 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003062
Simon Horman1cdbcb72013-05-19 15:46:49 +00003063perform_csum_check:
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003064 if (!csum) {
3065 nskb->csum = skb_checksum(nskb, doffset,
3066 nskb->len - doffset, 0);
3067 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003068 SKB_GSO_CB(nskb)->csum_start =
3069 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003070 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003071 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003072
3073 return segs;
3074
3075err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003076 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003077 return ERR_PTR(err);
3078}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003079EXPORT_SYMBOL_GPL(skb_segment);
3080
Herbert Xu71d93b32008-12-15 23:42:33 -08003081int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3082{
Eric Dumazet8a291112013-10-08 09:02:23 -07003083 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003084 unsigned int offset = skb_gro_offset(skb);
3085 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003086 struct sk_buff *nskb, *lp, *p = *head;
3087 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003088 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003089 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003090
Eric Dumazet8a291112013-10-08 09:02:23 -07003091 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003092 return -E2BIG;
3093
Eric Dumazet29e98242014-05-16 11:34:37 -07003094 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003095 pinfo = skb_shinfo(lp);
3096
3097 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003098 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003099 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003100 int i = skbinfo->nr_frags;
3101 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003102
Herbert Xu66e92fc2009-05-26 18:50:32 +00003103 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003104 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003105
Eric Dumazet8a291112013-10-08 09:02:23 -07003106 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003107 pinfo->nr_frags = nr_frags;
3108 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003109
Herbert Xu9aaa1562009-05-26 18:50:33 +00003110 frag = pinfo->frags + nr_frags;
3111 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003112 do {
3113 *--frag = *--frag2;
3114 } while (--i);
3115
3116 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003117 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003118
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003119 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003120 delta_truesize = skb->truesize -
3121 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003122
Herbert Xuf5572062009-01-14 20:40:03 -08003123 skb->truesize -= skb->data_len;
3124 skb->len -= skb->data_len;
3125 skb->data_len = 0;
3126
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003127 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003128 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003129 } else if (skb->head_frag) {
3130 int nr_frags = pinfo->nr_frags;
3131 skb_frag_t *frag = pinfo->frags + nr_frags;
3132 struct page *page = virt_to_head_page(skb->head);
3133 unsigned int first_size = headlen - offset;
3134 unsigned int first_offset;
3135
3136 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003137 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003138
3139 first_offset = skb->data -
3140 (unsigned char *)page_address(page) +
3141 offset;
3142
3143 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3144
3145 frag->page.p = page;
3146 frag->page_offset = first_offset;
3147 skb_frag_size_set(frag, first_size);
3148
3149 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3150 /* We dont need to clear skbinfo->nr_frags here */
3151
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003152 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003153 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3154 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003155 }
3156 if (pinfo->frag_list)
3157 goto merge;
3158 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003159 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003160
3161 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003162 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003163 if (unlikely(!nskb))
3164 return -ENOMEM;
3165
3166 __copy_skb_header(nskb, p);
3167 nskb->mac_len = p->mac_len;
3168
3169 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003170 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003171
Herbert Xu86911732009-01-29 14:19:50 +00003172 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003173 skb_set_network_header(nskb, skb_network_offset(p));
3174 skb_set_transport_header(nskb, skb_transport_offset(p));
3175
Herbert Xu86911732009-01-29 14:19:50 +00003176 __skb_pull(p, skb_gro_offset(p));
3177 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3178 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003179
Herbert Xu71d93b32008-12-15 23:42:33 -08003180 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003181 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003182 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003183 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003184 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003185
3186 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003187 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003188 nskb->len += p->len;
3189
3190 *head = nskb;
3191 nskb->next = p->next;
3192 p->next = NULL;
3193
3194 p = nskb;
3195
3196merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003197 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003198 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003199 unsigned int eat = offset - headlen;
3200
3201 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003202 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003203 skb->data_len -= eat;
3204 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003205 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003206 }
3207
Herbert Xu67147ba2009-05-26 18:50:22 +00003208 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003209
Eric Dumazet29e98242014-05-16 11:34:37 -07003210 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003211 skb_shinfo(p)->frag_list = skb;
3212 else
3213 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003214 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003215 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003216 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003217
Herbert Xu5d38a072009-01-04 16:13:40 -08003218done:
3219 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003220 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003221 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003222 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003223 if (lp != p) {
3224 lp->data_len += len;
3225 lp->truesize += delta_truesize;
3226 lp->len += len;
3227 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003228 NAPI_GRO_CB(skb)->same_flow = 1;
3229 return 0;
3230}
3231EXPORT_SYMBOL_GPL(skb_gro_receive);
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233void __init skb_init(void)
3234{
3235 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3236 sizeof(struct sk_buff),
3237 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003238 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003239 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003240 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3241 (2*sizeof(struct sk_buff)) +
3242 sizeof(atomic_t),
3243 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003244 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003245 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
David Howells716ea3a2007-04-02 20:19:53 -07003248/**
3249 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3250 * @skb: Socket buffer containing the buffers to be mapped
3251 * @sg: The scatter-gather list to map into
3252 * @offset: The offset into the buffer's contents to start mapping
3253 * @len: Length of buffer space to be mapped
3254 *
3255 * Fill the specified scatter-gather list with mappings/pointers into a
3256 * region of the buffer space attached to a socket buffer.
3257 */
David S. Miller51c739d2007-10-30 21:29:29 -07003258static int
3259__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003260{
David S. Miller1a028e52007-04-27 15:21:23 -07003261 int start = skb_headlen(skb);
3262 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003263 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003264 int elt = 0;
3265
3266 if (copy > 0) {
3267 if (copy > len)
3268 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003269 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003270 elt++;
3271 if ((len -= copy) == 0)
3272 return elt;
3273 offset += copy;
3274 }
3275
3276 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003277 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003278
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003279 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003280
Eric Dumazet9e903e02011-10-18 21:00:24 +00003281 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003282 if ((copy = end - offset) > 0) {
3283 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3284
3285 if (copy > len)
3286 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003287 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003288 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003289 elt++;
3290 if (!(len -= copy))
3291 return elt;
3292 offset += copy;
3293 }
David S. Miller1a028e52007-04-27 15:21:23 -07003294 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003295 }
3296
David S. Millerfbb398a2009-06-09 00:18:59 -07003297 skb_walk_frags(skb, frag_iter) {
3298 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003299
David S. Millerfbb398a2009-06-09 00:18:59 -07003300 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003301
David S. Millerfbb398a2009-06-09 00:18:59 -07003302 end = start + frag_iter->len;
3303 if ((copy = end - offset) > 0) {
3304 if (copy > len)
3305 copy = len;
3306 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3307 copy);
3308 if ((len -= copy) == 0)
3309 return elt;
3310 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003311 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003312 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003313 }
3314 BUG_ON(len);
3315 return elt;
3316}
3317
Fan Du25a91d82014-01-18 09:54:23 +08003318/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3319 * sglist without mark the sg which contain last skb data as the end.
3320 * So the caller can mannipulate sg list as will when padding new data after
3321 * the first call without calling sg_unmark_end to expend sg list.
3322 *
3323 * Scenario to use skb_to_sgvec_nomark:
3324 * 1. sg_init_table
3325 * 2. skb_to_sgvec_nomark(payload1)
3326 * 3. skb_to_sgvec_nomark(payload2)
3327 *
3328 * This is equivalent to:
3329 * 1. sg_init_table
3330 * 2. skb_to_sgvec(payload1)
3331 * 3. sg_unmark_end
3332 * 4. skb_to_sgvec(payload2)
3333 *
3334 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3335 * is more preferable.
3336 */
3337int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3338 int offset, int len)
3339{
3340 return __skb_to_sgvec(skb, sg, offset, len);
3341}
3342EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3343
David S. Miller51c739d2007-10-30 21:29:29 -07003344int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3345{
3346 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3347
Jens Axboec46f2332007-10-31 12:06:37 +01003348 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003349
3350 return nsg;
3351}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003352EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003353
David Howells716ea3a2007-04-02 20:19:53 -07003354/**
3355 * skb_cow_data - Check that a socket buffer's data buffers are writable
3356 * @skb: The socket buffer to check.
3357 * @tailbits: Amount of trailing space to be added
3358 * @trailer: Returned pointer to the skb where the @tailbits space begins
3359 *
3360 * Make sure that the data buffers attached to a socket buffer are
3361 * writable. If they are not, private copies are made of the data buffers
3362 * and the socket buffer is set to use these instead.
3363 *
3364 * If @tailbits is given, make sure that there is space to write @tailbits
3365 * bytes of data beyond current end of socket buffer. @trailer will be
3366 * set to point to the skb in which this space begins.
3367 *
3368 * The number of scatterlist elements required to completely map the
3369 * COW'd and extended socket buffer will be returned.
3370 */
3371int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3372{
3373 int copyflag;
3374 int elt;
3375 struct sk_buff *skb1, **skb_p;
3376
3377 /* If skb is cloned or its head is paged, reallocate
3378 * head pulling out all the pages (pages are considered not writable
3379 * at the moment even if they are anonymous).
3380 */
3381 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3382 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3383 return -ENOMEM;
3384
3385 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003386 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003387 /* A little of trouble, not enough of space for trailer.
3388 * This should not happen, when stack is tuned to generate
3389 * good frames. OK, on miss we reallocate and reserve even more
3390 * space, 128 bytes is fair. */
3391
3392 if (skb_tailroom(skb) < tailbits &&
3393 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3394 return -ENOMEM;
3395
3396 /* Voila! */
3397 *trailer = skb;
3398 return 1;
3399 }
3400
3401 /* Misery. We are in troubles, going to mincer fragments... */
3402
3403 elt = 1;
3404 skb_p = &skb_shinfo(skb)->frag_list;
3405 copyflag = 0;
3406
3407 while ((skb1 = *skb_p) != NULL) {
3408 int ntail = 0;
3409
3410 /* The fragment is partially pulled by someone,
3411 * this can happen on input. Copy it and everything
3412 * after it. */
3413
3414 if (skb_shared(skb1))
3415 copyflag = 1;
3416
3417 /* If the skb is the last, worry about trailer. */
3418
3419 if (skb1->next == NULL && tailbits) {
3420 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003421 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003422 skb_tailroom(skb1) < tailbits)
3423 ntail = tailbits + 128;
3424 }
3425
3426 if (copyflag ||
3427 skb_cloned(skb1) ||
3428 ntail ||
3429 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003430 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003431 struct sk_buff *skb2;
3432
3433 /* Fuck, we are miserable poor guys... */
3434 if (ntail == 0)
3435 skb2 = skb_copy(skb1, GFP_ATOMIC);
3436 else
3437 skb2 = skb_copy_expand(skb1,
3438 skb_headroom(skb1),
3439 ntail,
3440 GFP_ATOMIC);
3441 if (unlikely(skb2 == NULL))
3442 return -ENOMEM;
3443
3444 if (skb1->sk)
3445 skb_set_owner_w(skb2, skb1->sk);
3446
3447 /* Looking around. Are we still alive?
3448 * OK, link new skb, drop old one */
3449
3450 skb2->next = skb1->next;
3451 *skb_p = skb2;
3452 kfree_skb(skb1);
3453 skb1 = skb2;
3454 }
3455 elt++;
3456 *trailer = skb1;
3457 skb_p = &skb1->next;
3458 }
3459
3460 return elt;
3461}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003462EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003463
Eric Dumazetb1faf562010-05-31 23:44:05 -07003464static void sock_rmem_free(struct sk_buff *skb)
3465{
3466 struct sock *sk = skb->sk;
3467
3468 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3469}
3470
3471/*
3472 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3473 */
3474int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3475{
3476 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003477 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003478 return -ENOMEM;
3479
3480 skb_orphan(skb);
3481 skb->sk = sk;
3482 skb->destructor = sock_rmem_free;
3483 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3484
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003485 /* before exiting rcu section, make sure dst is refcounted */
3486 skb_dst_force(skb);
3487
Eric Dumazetb1faf562010-05-31 23:44:05 -07003488 skb_queue_tail(&sk->sk_error_queue, skb);
3489 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003490 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003491 return 0;
3492}
3493EXPORT_SYMBOL(sock_queue_err_skb);
3494
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003495struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3496{
3497 struct sk_buff_head *q = &sk->sk_error_queue;
3498 struct sk_buff *skb, *skb_next;
3499 int err = 0;
3500
3501 spin_lock_bh(&q->lock);
3502 skb = __skb_dequeue(q);
3503 if (skb && (skb_next = skb_peek(q)))
3504 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3505 spin_unlock_bh(&q->lock);
3506
3507 sk->sk_err = err;
3508 if (err)
3509 sk->sk_error_report(sk);
3510
3511 return skb;
3512}
3513EXPORT_SYMBOL(sock_dequeue_err_skb);
3514
Alexander Duyckcab41c42014-09-10 18:05:26 -04003515/**
3516 * skb_clone_sk - create clone of skb, and take reference to socket
3517 * @skb: the skb to clone
3518 *
3519 * This function creates a clone of a buffer that holds a reference on
3520 * sk_refcnt. Buffers created via this function are meant to be
3521 * returned using sock_queue_err_skb, or free via kfree_skb.
3522 *
3523 * When passing buffers allocated with this function to sock_queue_err_skb
3524 * it is necessary to wrap the call with sock_hold/sock_put in order to
3525 * prevent the socket from being released prior to being enqueued on
3526 * the sk_error_queue.
3527 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003528struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3529{
3530 struct sock *sk = skb->sk;
3531 struct sk_buff *clone;
3532
3533 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3534 return NULL;
3535
3536 clone = skb_clone(skb, GFP_ATOMIC);
3537 if (!clone) {
3538 sock_put(sk);
3539 return NULL;
3540 }
3541
3542 clone->sk = sk;
3543 clone->destructor = sock_efree;
3544
3545 return clone;
3546}
3547EXPORT_SYMBOL(skb_clone_sk);
3548
Alexander Duyck37846ef2014-09-04 13:31:10 -04003549static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3550 struct sock *sk,
3551 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003552{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003553 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003554 int err;
3555
Patrick Ohlyac45f602009-02-12 05:03:37 +00003556 serr = SKB_EXT_ERR(skb);
3557 memset(serr, 0, sizeof(*serr));
3558 serr->ee.ee_errno = ENOMSG;
3559 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003560 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003561 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003562 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003563 if (sk->sk_protocol == IPPROTO_TCP)
3564 serr->ee.ee_data -= sk->sk_tskey;
3565 }
Eric Dumazet29030372010-05-29 00:20:48 -07003566
Patrick Ohlyac45f602009-02-12 05:03:37 +00003567 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003568
Patrick Ohlyac45f602009-02-12 05:03:37 +00003569 if (err)
3570 kfree_skb(skb);
3571}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003572
3573void skb_complete_tx_timestamp(struct sk_buff *skb,
3574 struct skb_shared_hwtstamps *hwtstamps)
3575{
3576 struct sock *sk = skb->sk;
3577
Alexander Duyck62bccb82014-09-04 13:31:35 -04003578 /* take a reference to prevent skb_orphan() from freeing the socket */
3579 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003580
Alexander Duyck62bccb82014-09-04 13:31:35 -04003581 *skb_hwtstamps(skb) = *hwtstamps;
3582 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003583
3584 sock_put(sk);
3585}
3586EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3587
3588void __skb_tstamp_tx(struct sk_buff *orig_skb,
3589 struct skb_shared_hwtstamps *hwtstamps,
3590 struct sock *sk, int tstype)
3591{
3592 struct sk_buff *skb;
3593
3594 if (!sk)
3595 return;
3596
3597 if (hwtstamps)
3598 *skb_hwtstamps(orig_skb) = *hwtstamps;
3599 else
3600 orig_skb->tstamp = ktime_get_real();
3601
3602 skb = skb_clone(orig_skb, GFP_ATOMIC);
3603 if (!skb)
3604 return;
3605
3606 __skb_complete_tx_timestamp(skb, sk, tstype);
3607}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003608EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3609
3610void skb_tstamp_tx(struct sk_buff *orig_skb,
3611 struct skb_shared_hwtstamps *hwtstamps)
3612{
3613 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3614 SCM_TSTAMP_SND);
3615}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003616EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3617
Johannes Berg6e3e9392011-11-09 10:15:42 +01003618void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3619{
3620 struct sock *sk = skb->sk;
3621 struct sock_exterr_skb *serr;
3622 int err;
3623
3624 skb->wifi_acked_valid = 1;
3625 skb->wifi_acked = acked;
3626
3627 serr = SKB_EXT_ERR(skb);
3628 memset(serr, 0, sizeof(*serr));
3629 serr->ee.ee_errno = ENOMSG;
3630 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3631
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003632 /* take a reference to prevent skb_orphan() from freeing the socket */
3633 sock_hold(sk);
3634
Johannes Berg6e3e9392011-11-09 10:15:42 +01003635 err = sock_queue_err_skb(sk, skb);
3636 if (err)
3637 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003638
3639 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003640}
3641EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3642
Patrick Ohlyac45f602009-02-12 05:03:37 +00003643
Rusty Russellf35d9d82008-02-04 23:49:54 -05003644/**
3645 * skb_partial_csum_set - set up and verify partial csum values for packet
3646 * @skb: the skb to set
3647 * @start: the number of bytes after skb->data to start checksumming.
3648 * @off: the offset from start to place the checksum.
3649 *
3650 * For untrusted partially-checksummed packets, we need to make sure the values
3651 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3652 *
3653 * This function checks and sets those values and skb->ip_summed: if this
3654 * returns false you should drop the packet.
3655 */
3656bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3657{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003658 if (unlikely(start > skb_headlen(skb)) ||
3659 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003660 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3661 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003662 return false;
3663 }
3664 skb->ip_summed = CHECKSUM_PARTIAL;
3665 skb->csum_start = skb_headroom(skb) + start;
3666 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003667 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003668 return true;
3669}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003670EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003671
Paul Durranted1f50c2014-01-09 10:02:46 +00003672static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3673 unsigned int max)
3674{
3675 if (skb_headlen(skb) >= len)
3676 return 0;
3677
3678 /* If we need to pullup then pullup to the max, so we
3679 * won't need to do it again.
3680 */
3681 if (max > skb->len)
3682 max = skb->len;
3683
3684 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3685 return -ENOMEM;
3686
3687 if (skb_headlen(skb) < len)
3688 return -EPROTO;
3689
3690 return 0;
3691}
3692
Jan Beulichf9708b42014-03-11 13:56:05 +00003693#define MAX_TCP_HDR_LEN (15 * 4)
3694
3695static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3696 typeof(IPPROTO_IP) proto,
3697 unsigned int off)
3698{
3699 switch (proto) {
3700 int err;
3701
3702 case IPPROTO_TCP:
3703 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3704 off + MAX_TCP_HDR_LEN);
3705 if (!err && !skb_partial_csum_set(skb, off,
3706 offsetof(struct tcphdr,
3707 check)))
3708 err = -EPROTO;
3709 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3710
3711 case IPPROTO_UDP:
3712 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3713 off + sizeof(struct udphdr));
3714 if (!err && !skb_partial_csum_set(skb, off,
3715 offsetof(struct udphdr,
3716 check)))
3717 err = -EPROTO;
3718 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3719 }
3720
3721 return ERR_PTR(-EPROTO);
3722}
3723
Paul Durranted1f50c2014-01-09 10:02:46 +00003724/* This value should be large enough to cover a tagged ethernet header plus
3725 * maximally sized IP and TCP or UDP headers.
3726 */
3727#define MAX_IP_HDR_LEN 128
3728
Jan Beulichf9708b42014-03-11 13:56:05 +00003729static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003730{
3731 unsigned int off;
3732 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003733 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003734 int err;
3735
3736 fragment = false;
3737
3738 err = skb_maybe_pull_tail(skb,
3739 sizeof(struct iphdr),
3740 MAX_IP_HDR_LEN);
3741 if (err < 0)
3742 goto out;
3743
3744 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3745 fragment = true;
3746
3747 off = ip_hdrlen(skb);
3748
3749 err = -EPROTO;
3750
3751 if (fragment)
3752 goto out;
3753
Jan Beulichf9708b42014-03-11 13:56:05 +00003754 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3755 if (IS_ERR(csum))
3756 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003757
Jan Beulichf9708b42014-03-11 13:56:05 +00003758 if (recalculate)
3759 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3760 ip_hdr(skb)->daddr,
3761 skb->len - off,
3762 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003763 err = 0;
3764
3765out:
3766 return err;
3767}
3768
3769/* This value should be large enough to cover a tagged ethernet header plus
3770 * an IPv6 header, all options, and a maximal TCP or UDP header.
3771 */
3772#define MAX_IPV6_HDR_LEN 256
3773
3774#define OPT_HDR(type, skb, off) \
3775 (type *)(skb_network_header(skb) + (off))
3776
3777static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3778{
3779 int err;
3780 u8 nexthdr;
3781 unsigned int off;
3782 unsigned int len;
3783 bool fragment;
3784 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003785 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003786
3787 fragment = false;
3788 done = false;
3789
3790 off = sizeof(struct ipv6hdr);
3791
3792 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3793 if (err < 0)
3794 goto out;
3795
3796 nexthdr = ipv6_hdr(skb)->nexthdr;
3797
3798 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3799 while (off <= len && !done) {
3800 switch (nexthdr) {
3801 case IPPROTO_DSTOPTS:
3802 case IPPROTO_HOPOPTS:
3803 case IPPROTO_ROUTING: {
3804 struct ipv6_opt_hdr *hp;
3805
3806 err = skb_maybe_pull_tail(skb,
3807 off +
3808 sizeof(struct ipv6_opt_hdr),
3809 MAX_IPV6_HDR_LEN);
3810 if (err < 0)
3811 goto out;
3812
3813 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3814 nexthdr = hp->nexthdr;
3815 off += ipv6_optlen(hp);
3816 break;
3817 }
3818 case IPPROTO_AH: {
3819 struct ip_auth_hdr *hp;
3820
3821 err = skb_maybe_pull_tail(skb,
3822 off +
3823 sizeof(struct ip_auth_hdr),
3824 MAX_IPV6_HDR_LEN);
3825 if (err < 0)
3826 goto out;
3827
3828 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3829 nexthdr = hp->nexthdr;
3830 off += ipv6_authlen(hp);
3831 break;
3832 }
3833 case IPPROTO_FRAGMENT: {
3834 struct frag_hdr *hp;
3835
3836 err = skb_maybe_pull_tail(skb,
3837 off +
3838 sizeof(struct frag_hdr),
3839 MAX_IPV6_HDR_LEN);
3840 if (err < 0)
3841 goto out;
3842
3843 hp = OPT_HDR(struct frag_hdr, skb, off);
3844
3845 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3846 fragment = true;
3847
3848 nexthdr = hp->nexthdr;
3849 off += sizeof(struct frag_hdr);
3850 break;
3851 }
3852 default:
3853 done = true;
3854 break;
3855 }
3856 }
3857
3858 err = -EPROTO;
3859
3860 if (!done || fragment)
3861 goto out;
3862
Jan Beulichf9708b42014-03-11 13:56:05 +00003863 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3864 if (IS_ERR(csum))
3865 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003866
Jan Beulichf9708b42014-03-11 13:56:05 +00003867 if (recalculate)
3868 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3869 &ipv6_hdr(skb)->daddr,
3870 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003871 err = 0;
3872
3873out:
3874 return err;
3875}
3876
3877/**
3878 * skb_checksum_setup - set up partial checksum offset
3879 * @skb: the skb to set up
3880 * @recalculate: if true the pseudo-header checksum will be recalculated
3881 */
3882int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3883{
3884 int err;
3885
3886 switch (skb->protocol) {
3887 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003888 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003889 break;
3890
3891 case htons(ETH_P_IPV6):
3892 err = skb_checksum_setup_ipv6(skb, recalculate);
3893 break;
3894
3895 default:
3896 err = -EPROTO;
3897 break;
3898 }
3899
3900 return err;
3901}
3902EXPORT_SYMBOL(skb_checksum_setup);
3903
Ben Hutchings4497b072008-06-19 16:22:28 -07003904void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3905{
Joe Perchese87cc472012-05-13 21:56:26 +00003906 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3907 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003908}
Ben Hutchings4497b072008-06-19 16:22:28 -07003909EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003910
3911void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3912{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003913 if (head_stolen) {
3914 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003915 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003916 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003917 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003918 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003919}
3920EXPORT_SYMBOL(kfree_skb_partial);
3921
3922/**
3923 * skb_try_coalesce - try to merge skb to prior one
3924 * @to: prior buffer
3925 * @from: buffer to add
3926 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003927 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003928 */
3929bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3930 bool *fragstolen, int *delta_truesize)
3931{
3932 int i, delta, len = from->len;
3933
3934 *fragstolen = false;
3935
3936 if (skb_cloned(to))
3937 return false;
3938
3939 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07003940 if (len)
3941 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003942 *delta_truesize = 0;
3943 return true;
3944 }
3945
3946 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3947 return false;
3948
3949 if (skb_headlen(from) != 0) {
3950 struct page *page;
3951 unsigned int offset;
3952
3953 if (skb_shinfo(to)->nr_frags +
3954 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3955 return false;
3956
3957 if (skb_head_is_locked(from))
3958 return false;
3959
3960 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3961
3962 page = virt_to_head_page(from->head);
3963 offset = from->data - (unsigned char *)page_address(page);
3964
3965 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3966 page, offset, skb_headlen(from));
3967 *fragstolen = true;
3968 } else {
3969 if (skb_shinfo(to)->nr_frags +
3970 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3971 return false;
3972
Weiping Panf4b549a2012-09-28 20:15:30 +00003973 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003974 }
3975
3976 WARN_ON_ONCE(delta < len);
3977
3978 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3979 skb_shinfo(from)->frags,
3980 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3981 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3982
3983 if (!skb_cloned(from))
3984 skb_shinfo(from)->nr_frags = 0;
3985
Li RongQing8ea853f2012-09-18 16:53:21 +00003986 /* if the skb is not cloned this does nothing
3987 * since we set nr_frags to 0.
3988 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003989 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3990 skb_frag_ref(from, i);
3991
3992 to->truesize += delta;
3993 to->len += len;
3994 to->data_len += len;
3995
3996 *delta_truesize = delta;
3997 return true;
3998}
3999EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004000
4001/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004002 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004003 *
4004 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004005 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004006 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004007 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4008 * into/from a tunnel. Some information have to be cleared during these
4009 * operations.
4010 * skb_scrub_packet can also be used to clean a skb before injecting it in
4011 * another namespace (@xnet == true). We have to clear all information in the
4012 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004013 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004014void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004015{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004016 if (xnet)
4017 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004018 skb->tstamp.tv64 = 0;
4019 skb->pkt_type = PACKET_HOST;
4020 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004021 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004022 skb_dst_drop(skb);
4023 skb->mark = 0;
4024 secpath_reset(skb);
4025 nf_reset(skb);
4026 nf_reset_trace(skb);
4027}
4028EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004029
4030/**
4031 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4032 *
4033 * @skb: GSO skb
4034 *
4035 * skb_gso_transport_seglen is used to determine the real size of the
4036 * individual segments, including Layer4 headers (TCP/UDP).
4037 *
4038 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4039 */
4040unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4041{
4042 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalde960aa2014-01-26 10:58:16 +01004043
4044 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
Florian Westphal6d39d582014-04-09 10:28:50 +02004045 return tcp_hdrlen(skb) + shinfo->gso_size;
4046
4047 /* UFO sets gso_size to the size of the fragmentation
4048 * payload, i.e. the size of the L4 (UDP) header is already
4049 * accounted for.
4050 */
4051 return shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004052}
4053EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004054
4055static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4056{
4057 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4058 kfree_skb(skb);
4059 return NULL;
4060 }
4061
4062 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4063 skb->mac_header += VLAN_HLEN;
4064 return skb;
4065}
4066
4067struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4068{
4069 struct vlan_hdr *vhdr;
4070 u16 vlan_tci;
4071
4072 if (unlikely(vlan_tx_tag_present(skb))) {
4073 /* vlan_tci is already set-up so leave this for another time */
4074 return skb;
4075 }
4076
4077 skb = skb_share_check(skb, GFP_ATOMIC);
4078 if (unlikely(!skb))
4079 goto err_free;
4080
4081 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4082 goto err_free;
4083
4084 vhdr = (struct vlan_hdr *)skb->data;
4085 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4086 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4087
4088 skb_pull_rcsum(skb, VLAN_HLEN);
4089 vlan_set_encap_proto(skb, vhdr);
4090
4091 skb = skb_reorder_vlan_header(skb);
4092 if (unlikely(!skb))
4093 goto err_free;
4094
4095 skb_reset_network_header(skb);
4096 skb_reset_transport_header(skb);
4097 skb_reset_mac_len(skb);
4098
4099 return skb;
4100
4101err_free:
4102 kfree_skb(skb);
4103 return NULL;
4104}
4105EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004106
4107/**
4108 * alloc_skb_with_frags - allocate skb with page frags
4109 *
4110 * header_len: size of linear part
4111 * data_len: needed length in frags
4112 * max_page_order: max page order desired.
4113 * errcode: pointer to error code if any
4114 * gfp_mask: allocation mask
4115 *
4116 * This can be used to allocate a paged skb, given a maximal order for frags.
4117 */
4118struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4119 unsigned long data_len,
4120 int max_page_order,
4121 int *errcode,
4122 gfp_t gfp_mask)
4123{
4124 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4125 unsigned long chunk;
4126 struct sk_buff *skb;
4127 struct page *page;
4128 gfp_t gfp_head;
4129 int i;
4130
4131 *errcode = -EMSGSIZE;
4132 /* Note this test could be relaxed, if we succeed to allocate
4133 * high order pages...
4134 */
4135 if (npages > MAX_SKB_FRAGS)
4136 return NULL;
4137
4138 gfp_head = gfp_mask;
4139 if (gfp_head & __GFP_WAIT)
4140 gfp_head |= __GFP_REPEAT;
4141
4142 *errcode = -ENOBUFS;
4143 skb = alloc_skb(header_len, gfp_head);
4144 if (!skb)
4145 return NULL;
4146
4147 skb->truesize += npages << PAGE_SHIFT;
4148
4149 for (i = 0; npages > 0; i++) {
4150 int order = max_page_order;
4151
4152 while (order) {
4153 if (npages >= 1 << order) {
4154 page = alloc_pages(gfp_mask |
4155 __GFP_COMP |
4156 __GFP_NOWARN |
4157 __GFP_NORETRY,
4158 order);
4159 if (page)
4160 goto fill_page;
4161 /* Do not retry other high order allocations */
4162 order = 1;
4163 max_page_order = 0;
4164 }
4165 order--;
4166 }
4167 page = alloc_page(gfp_mask);
4168 if (!page)
4169 goto failure;
4170fill_page:
4171 chunk = min_t(unsigned long, data_len,
4172 PAGE_SIZE << order);
4173 skb_fill_page_desc(skb, i, page, 0, chunk);
4174 data_len -= chunk;
4175 npages -= 1 << order;
4176 }
4177 return skb;
4178
4179failure:
4180 kfree_skb(skb);
4181 return NULL;
4182}
4183EXPORT_SYMBOL(alloc_skb_with_frags);