blob: da1378a3e2c72e0f23e9e21581dfd2ece5491d09 [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{
494 if (!skb->cloned ||
495 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
496 &skb_shinfo(skb)->dataref)) {
497 if (skb_shinfo(skb)->nr_frags) {
498 int i;
499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +0000500 skb_frag_unref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
Shirley Maa6686f22011-07-06 12:22:12 +0000503 /*
504 * If skb buf is from userspace, we need to notify the caller
505 * the lower device DMA has done;
506 */
507 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
508 struct ubuf_info *uarg;
509
510 uarg = skb_shinfo(skb)->destructor_arg;
511 if (uarg->callback)
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000512 uarg->callback(uarg, true);
Shirley Maa6686f22011-07-06 12:22:12 +0000513 }
514
David S. Miller21dc3302010-08-23 00:13:46 -0700515 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 skb_drop_fraglist(skb);
517
Eric Dumazetd3836f22012-04-27 00:33:38 +0000518 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520}
521
522/*
523 * Free an skbuff by memory without cleaning the state.
524 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800525static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
David S. Millerd179cd12005-08-17 14:57:30 -0700527 struct sk_buff *other;
528 atomic_t *fclone_ref;
529
David S. Millerd179cd12005-08-17 14:57:30 -0700530 switch (skb->fclone) {
531 case SKB_FCLONE_UNAVAILABLE:
532 kmem_cache_free(skbuff_head_cache, skb);
533 break;
534
535 case SKB_FCLONE_ORIG:
536 fclone_ref = (atomic_t *) (skb + 2);
537 if (atomic_dec_and_test(fclone_ref))
538 kmem_cache_free(skbuff_fclone_cache, skb);
539 break;
540
541 case SKB_FCLONE_CLONE:
542 fclone_ref = (atomic_t *) (skb + 1);
543 other = skb - 1;
544
545 /* The clone portion is available for
546 * fast-cloning again.
547 */
548 skb->fclone = SKB_FCLONE_UNAVAILABLE;
549
550 if (atomic_dec_and_test(fclone_ref))
551 kmem_cache_free(skbuff_fclone_cache, other);
552 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700556static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Eric Dumazetadf30902009-06-02 05:19:30 +0000558 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#ifdef CONFIG_XFRM
560 secpath_put(skb->sp);
561#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700562 if (skb->destructor) {
563 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 skb->destructor(skb);
565 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000566#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700567 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100568#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#ifdef CONFIG_BRIDGE_NETFILTER
570 nf_bridge_put(skb->nf_bridge);
571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* XXX: IS this still necessary? - JHS */
573#ifdef CONFIG_NET_SCHED
574 skb->tc_index = 0;
575#ifdef CONFIG_NET_CLS_ACT
576 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700579}
580
581/* Free everything but the sk_buff shell. */
582static void skb_release_all(struct sk_buff *skb)
583{
584 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000585 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000586 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Herbert Xu2d4baff2007-11-26 23:11:19 +0800589/**
590 * __kfree_skb - private function
591 * @skb: buffer
592 *
593 * Free an sk_buff. Release anything attached to the buffer.
594 * Clean the state. This is an internal helper function. Users should
595 * always call kfree_skb
596 */
597
598void __kfree_skb(struct sk_buff *skb)
599{
600 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 kfree_skbmem(skb);
602}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800603EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800606 * kfree_skb - free an sk_buff
607 * @skb: buffer to free
608 *
609 * Drop a reference to the buffer and free it if the usage count has
610 * hit zero.
611 */
612void kfree_skb(struct sk_buff *skb)
613{
614 if (unlikely(!skb))
615 return;
616 if (likely(atomic_read(&skb->users) == 1))
617 smp_rmb();
618 else if (likely(!atomic_dec_and_test(&skb->users)))
619 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000620 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800621 __kfree_skb(skb);
622}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800623EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800624
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700625void kfree_skb_list(struct sk_buff *segs)
626{
627 while (segs) {
628 struct sk_buff *next = segs->next;
629
630 kfree_skb(segs);
631 segs = next;
632 }
633}
634EXPORT_SYMBOL(kfree_skb_list);
635
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700636/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000637 * skb_tx_error - report an sk_buff xmit error
638 * @skb: buffer that triggered an error
639 *
640 * Report xmit error if a device callback is tracking this skb.
641 * skb must be freed afterwards.
642 */
643void skb_tx_error(struct sk_buff *skb)
644{
645 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
646 struct ubuf_info *uarg;
647
648 uarg = skb_shinfo(skb)->destructor_arg;
649 if (uarg->callback)
650 uarg->callback(uarg, false);
651 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
652 }
653}
654EXPORT_SYMBOL(skb_tx_error);
655
656/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000657 * consume_skb - free an skbuff
658 * @skb: buffer to free
659 *
660 * Drop a ref to the buffer and free it if the usage count has hit zero
661 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
662 * is being dropped after a failure and notes that
663 */
664void consume_skb(struct sk_buff *skb)
665{
666 if (unlikely(!skb))
667 return;
668 if (likely(atomic_read(&skb->users) == 1))
669 smp_rmb();
670 else if (likely(!atomic_dec_and_test(&skb->users)))
671 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900672 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000673 __kfree_skb(skb);
674}
675EXPORT_SYMBOL(consume_skb);
676
Herbert Xudec18812007-10-14 00:37:30 -0700677static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
678{
679 new->tstamp = old->tstamp;
680 new->dev = old->dev;
681 new->transport_header = old->transport_header;
682 new->network_header = old->network_header;
683 new->mac_header = old->mac_header;
Joe Stringer4bc41b82013-07-03 16:04:25 +0900684 new->inner_protocol = old->inner_protocol;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000685 new->inner_transport_header = old->inner_transport_header;
Pravin B Shelar92df9b22013-02-01 15:18:49 +0000686 new->inner_network_header = old->inner_network_header;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000687 new->inner_mac_header = old->inner_mac_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000688 skb_dst_copy(new, old);
Tom Herbert3df7a742013-12-15 22:16:29 -0800689 skb_copy_hash(new, old);
Changli Gao6461be32011-08-19 04:44:18 +0000690 new->ooo_okay = old->ooo_okay;
Ben Greear3bdc0eb2012-02-11 15:39:30 +0000691 new->no_fcs = old->no_fcs;
Joseph Gasparakis6a674e92012-12-07 14:14:14 +0000692 new->encapsulation = old->encapsulation;
Tom Herbert46fb51e2014-06-14 23:24:03 -0700693 new->encap_hdr_csum = old->encap_hdr_csum;
694 new->csum_valid = old->csum_valid;
695 new->csum_complete_sw = old->csum_complete_sw;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700696#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700697 new->sp = secpath_get(old->sp);
698#endif
699 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000700 new->csum = old->csum;
WANG Cong60ff7462014-05-04 16:39:18 -0700701 new->ignore_df = old->ignore_df;
Herbert Xudec18812007-10-14 00:37:30 -0700702 new->pkt_type = old->pkt_type;
703 new->ip_summed = old->ip_summed;
704 skb_copy_queue_mapping(new, old);
705 new->priority = old->priority;
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000706#if IS_ENABLED(CONFIG_IP_VS)
Herbert Xudec18812007-10-14 00:37:30 -0700707 new->ipvs_property = old->ipvs_property;
708#endif
Mel Gormanc93bdd02012-07-31 16:44:19 -0700709 new->pfmemalloc = old->pfmemalloc;
Herbert Xudec18812007-10-14 00:37:30 -0700710 new->protocol = old->protocol;
711 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800712 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700713 __nf_copy(new, old);
Herbert Xudec18812007-10-14 00:37:30 -0700714#ifdef CONFIG_NET_SCHED
715 new->tc_index = old->tc_index;
716#ifdef CONFIG_NET_CLS_ACT
717 new->tc_verd = old->tc_verd;
718#endif
719#endif
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000720 new->vlan_proto = old->vlan_proto;
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700721 new->vlan_tci = old->vlan_tci;
722
Herbert Xudec18812007-10-14 00:37:30 -0700723 skb_copy_secmark(new, old);
Eliezer Tamir06021292013-06-10 11:39:50 +0300724
Cong Wange0d10952013-08-01 11:10:25 +0800725#ifdef CONFIG_NET_RX_BUSY_POLL
Eliezer Tamir06021292013-06-10 11:39:50 +0300726 new->napi_id = old->napi_id;
727#endif
Herbert Xudec18812007-10-14 00:37:30 -0700728}
729
Herbert Xu82c49a32009-05-22 22:11:37 +0000730/*
731 * You should not add any new code to this function. Add it to
732 * __copy_skb_header above instead.
733 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700734static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#define C(x) n->x = skb->x
737
738 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700740 __copy_skb_header(n, skb);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 C(len);
743 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700744 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700745 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800746 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 C(tail);
750 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800751 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000752 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800753 C(data);
754 C(truesize);
755 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 atomic_inc(&(skb_shinfo(skb)->dataref));
758 skb->cloned = 1;
759
760 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700761#undef C
762}
763
764/**
765 * skb_morph - morph one skb into another
766 * @dst: the skb to receive the contents
767 * @src: the skb to supply the contents
768 *
769 * This is identical to skb_clone except that the target skb is
770 * supplied by the user.
771 *
772 * The target skb is returned upon exit.
773 */
774struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
775{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800776 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700777 return __skb_clone(dst, src);
778}
779EXPORT_SYMBOL_GPL(skb_morph);
780
Ben Hutchings2c530402012-07-10 10:55:09 +0000781/**
782 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000783 * @skb: the skb to modify
784 * @gfp_mask: allocation priority
785 *
786 * This must be called on SKBTX_DEV_ZEROCOPY skb.
787 * It will copy all frags into kernel and drop the reference
788 * to userspace pages.
789 *
790 * If this function is called from an interrupt gfp_mask() must be
791 * %GFP_ATOMIC.
792 *
793 * Returns 0 on success or a negative error code on failure
794 * to allocate kernel memory to copy to.
795 */
796int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000797{
798 int i;
799 int num_frags = skb_shinfo(skb)->nr_frags;
800 struct page *page, *head = NULL;
801 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
802
803 for (i = 0; i < num_frags; i++) {
804 u8 *vaddr;
805 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
806
Krishna Kumar02756ed2012-07-17 02:05:29 +0000807 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000808 if (!page) {
809 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900810 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000811 put_page(head);
812 head = next;
813 }
814 return -ENOMEM;
815 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200816 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000817 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000818 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200819 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900820 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000821 head = page;
822 }
823
824 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000825 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000826 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000827
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000828 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000829
830 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000831 for (i = num_frags - 1; i >= 0; i--) {
832 __skb_fill_page_desc(skb, i, head, 0,
833 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900834 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000835 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000836
837 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000838 return 0;
839}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000840EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000841
Herbert Xue0053ec2007-10-14 00:37:52 -0700842/**
843 * skb_clone - duplicate an sk_buff
844 * @skb: buffer to clone
845 * @gfp_mask: allocation priority
846 *
847 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
848 * copies share the same packet data but not structure. The new
849 * buffer has a reference count of 1. If the allocation fails the
850 * function returns %NULL otherwise the new buffer is returned.
851 *
852 * If this function is called from an interrupt gfp_mask() must be
853 * %GFP_ATOMIC.
854 */
855
856struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
857{
858 struct sk_buff *n;
859
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000860 if (skb_orphan_frags(skb, gfp_mask))
861 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000862
Herbert Xue0053ec2007-10-14 00:37:52 -0700863 n = skb + 1;
864 if (skb->fclone == SKB_FCLONE_ORIG &&
865 n->fclone == SKB_FCLONE_UNAVAILABLE) {
866 atomic_t *fclone_ref = (atomic_t *) (n + 1);
867 n->fclone = SKB_FCLONE_CLONE;
868 atomic_inc(fclone_ref);
869 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700870 if (skb_pfmemalloc(skb))
871 gfp_mask |= __GFP_MEMALLOC;
872
Herbert Xue0053ec2007-10-14 00:37:52 -0700873 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
874 if (!n)
875 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200876
877 kmemcheck_annotate_bitfield(n, flags1);
878 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700879 n->fclone = SKB_FCLONE_UNAVAILABLE;
880 }
881
882 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800884EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000886static void skb_headers_offset_update(struct sk_buff *skb, int off)
887{
Eric Dumazet030737b2013-10-19 11:42:54 -0700888 /* Only adjust this if it actually is csum_start rather than csum */
889 if (skb->ip_summed == CHECKSUM_PARTIAL)
890 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000891 /* {transport,network,mac}_header and tail are relative to skb->head */
892 skb->transport_header += off;
893 skb->network_header += off;
894 if (skb_mac_header_was_set(skb))
895 skb->mac_header += off;
896 skb->inner_transport_header += off;
897 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000898 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
902{
Herbert Xudec18812007-10-14 00:37:30 -0700903 __copy_skb_header(new, old);
904
Herbert Xu79671682006-06-22 02:40:14 -0700905 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
906 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
907 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Mel Gormanc93bdd02012-07-31 16:44:19 -0700910static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
911{
912 if (skb_pfmemalloc(skb))
913 return SKB_ALLOC_RX;
914 return 0;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/**
918 * skb_copy - create private copy of an sk_buff
919 * @skb: buffer to copy
920 * @gfp_mask: allocation priority
921 *
922 * Make a copy of both an &sk_buff and its data. This is used when the
923 * caller wishes to modify the data and needs a private copy of the
924 * data to alter. Returns %NULL on failure or the pointer to the buffer
925 * on success. The returned buffer has a reference count of 1.
926 *
927 * As by-product this function converts non-linear &sk_buff to linear
928 * one, so that &sk_buff becomes completely private and caller is allowed
929 * to modify all the data of returned buffer. This means that this
930 * function is not recommended for use in circumstances when only
931 * header is going to be modified. Use pskb_copy() instead.
932 */
933
Al Virodd0fc662005-10-07 07:46:04 +0100934struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000936 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000937 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700938 struct sk_buff *n = __alloc_skb(size, gfp_mask,
939 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!n)
942 return NULL;
943
944 /* Set the data pointer */
945 skb_reserve(n, headerlen);
946 /* Set the tail pointer and length */
947 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
950 BUG();
951
952 copy_skb_header(n, skb);
953 return n;
954}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800955EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957/**
Octavian Purdilabad93e92014-06-12 01:36:26 +0300958 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000960 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +0300962 * @fclone: if true allocate the copy of the skb from the fclone
963 * cache instead of the head cache; it is recommended to set this
964 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 *
966 * Make a copy of both an &sk_buff and part of its data, located
967 * in header. Fragmented data remain shared. This is used when
968 * the caller wishes to modify only header of &sk_buff and needs
969 * private copy of the header to alter. Returns %NULL on failure
970 * or the pointer to the buffer on success.
971 * The returned buffer has a reference count of 1.
972 */
973
Octavian Purdilabad93e92014-06-12 01:36:26 +0300974struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
975 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Eric Dumazet117632e2011-12-03 21:39:53 +0000977 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +0300978 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
979 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!n)
982 goto out;
983
984 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000985 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* Set the tail pointer and length */
987 skb_put(n, skb_headlen(skb));
988 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300989 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Herbert Xu25f484a2006-11-07 14:57:15 -0800991 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 n->data_len = skb->data_len;
993 n->len = skb->len;
994
995 if (skb_shinfo(skb)->nr_frags) {
996 int i;
997
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000998 if (skb_orphan_frags(skb, gfp_mask)) {
999 kfree_skb(n);
1000 n = NULL;
1001 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1004 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001005 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 skb_shinfo(n)->nr_frags = i;
1008 }
1009
David S. Miller21dc3302010-08-23 00:13:46 -07001010 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1012 skb_clone_fraglist(n);
1013 }
1014
1015 copy_skb_header(n, skb);
1016out:
1017 return n;
1018}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001019EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021/**
1022 * pskb_expand_head - reallocate header of &sk_buff
1023 * @skb: buffer to reallocate
1024 * @nhead: room to add at head
1025 * @ntail: room to add at tail
1026 * @gfp_mask: allocation priority
1027 *
Mathias Krausebc323832013-11-07 14:18:26 +01001028 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1029 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * reference count of 1. Returns zero in the case of success or error,
1031 * if expansion failed. In the last case, &sk_buff is not changed.
1032 *
1033 * All the pointers pointing into skb header may change and must be
1034 * reloaded after call to this function.
1035 */
1036
Victor Fusco86a76ca2005-07-08 14:57:47 -07001037int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001038 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 int i;
1041 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001042 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 long off;
1044
Herbert Xu4edd87a2008-10-01 07:09:38 -07001045 BUG_ON(nhead < 0);
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (skb_shared(skb))
1048 BUG();
1049
1050 size = SKB_DATA_ALIGN(size);
1051
Mel Gormanc93bdd02012-07-31 16:44:19 -07001052 if (skb_pfmemalloc(skb))
1053 gfp_mask |= __GFP_MEMALLOC;
1054 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1055 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!data)
1057 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001058 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001061 * optimized for the cases when header is void.
1062 */
1063 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1064
1065 memcpy((struct skb_shared_info *)(data + size),
1066 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001067 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Alexander Duyck3e245912012-05-04 14:26:51 +00001069 /*
1070 * if shinfo is shared we must drop the old head gracefully, but if it
1071 * is not we can just drop the old head and let the existing refcount
1072 * be since all we did is relocate the values
1073 */
1074 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001075 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001076 if (skb_orphan_frags(skb, gfp_mask))
1077 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001078 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001079 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Eric Dumazet1fd63042010-09-02 23:09:32 +00001081 if (skb_has_frag_list(skb))
1082 skb_clone_fraglist(skb);
1083
1084 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001085 } else {
1086 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 off = (data + nhead) - skb->head;
1089
1090 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001091 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001093#ifdef NET_SKBUFF_DATA_USES_OFFSET
1094 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001095 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001096#else
1097 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001098#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001099 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001100 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001102 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 skb->nohdr = 0;
1104 atomic_set(&skb_shinfo(skb)->dataref, 1);
1105 return 0;
1106
Shirley Maa6686f22011-07-06 12:22:12 +00001107nofrags:
1108 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109nodata:
1110 return -ENOMEM;
1111}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001112EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114/* Make private copy of skb with writable head and some headroom */
1115
1116struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1117{
1118 struct sk_buff *skb2;
1119 int delta = headroom - skb_headroom(skb);
1120
1121 if (delta <= 0)
1122 skb2 = pskb_copy(skb, GFP_ATOMIC);
1123 else {
1124 skb2 = skb_clone(skb, GFP_ATOMIC);
1125 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1126 GFP_ATOMIC)) {
1127 kfree_skb(skb2);
1128 skb2 = NULL;
1129 }
1130 }
1131 return skb2;
1132}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001133EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135/**
1136 * skb_copy_expand - copy and expand sk_buff
1137 * @skb: buffer to copy
1138 * @newheadroom: new free bytes at head
1139 * @newtailroom: new free bytes at tail
1140 * @gfp_mask: allocation priority
1141 *
1142 * Make a copy of both an &sk_buff and its data and while doing so
1143 * allocate additional space.
1144 *
1145 * This is used when the caller wishes to modify the data and needs a
1146 * private copy of the data to alter as well as more space for new fields.
1147 * Returns %NULL on failure or the pointer to the buffer
1148 * on success. The returned buffer has a reference count of 1.
1149 *
1150 * You must pass %GFP_ATOMIC as the allocation priority if this function
1151 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
1153struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001154 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001155 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 /*
1158 * Allocate the copy buffer
1159 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001160 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1161 gfp_mask, skb_alloc_rx_flag(skb),
1162 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001163 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int head_copy_len, head_copy_off;
1165
1166 if (!n)
1167 return NULL;
1168
1169 skb_reserve(n, newheadroom);
1170
1171 /* Set the tail pointer and length */
1172 skb_put(n, skb->len);
1173
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001174 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 head_copy_off = 0;
1176 if (newheadroom <= head_copy_len)
1177 head_copy_len = newheadroom;
1178 else
1179 head_copy_off = newheadroom - head_copy_len;
1180
1181 /* Copy the linear header and data. */
1182 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1183 skb->len + head_copy_len))
1184 BUG();
1185
1186 copy_skb_header(n, skb);
1187
Eric Dumazet030737b2013-10-19 11:42:54 -07001188 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return n;
1191}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001192EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194/**
1195 * skb_pad - zero pad the tail of an skb
1196 * @skb: buffer to pad
1197 * @pad: space to pad
1198 *
1199 * Ensure that a buffer is followed by a padding area that is zero
1200 * filled. Used by network drivers which may DMA or transfer data
1201 * beyond the buffer end onto the wire.
1202 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001203 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001205
Herbert Xu5b057c62006-06-23 02:06:41 -07001206int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Herbert Xu5b057c62006-06-23 02:06:41 -07001208 int err;
1209 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001212 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001216
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001217 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001218 if (likely(skb_cloned(skb) || ntail > 0)) {
1219 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1220 if (unlikely(err))
1221 goto free_skb;
1222 }
1223
1224 /* FIXME: The use of this function with non-linear skb's really needs
1225 * to be audited.
1226 */
1227 err = skb_linearize(skb);
1228 if (unlikely(err))
1229 goto free_skb;
1230
1231 memset(skb->data + skb->len, 0, pad);
1232 return 0;
1233
1234free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001236 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001237}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001238EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001239
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001240/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001241 * pskb_put - add data to the tail of a potentially fragmented buffer
1242 * @skb: start of the buffer to use
1243 * @tail: tail fragment of the buffer to use
1244 * @len: amount of data to add
1245 *
1246 * This function extends the used data area of the potentially
1247 * fragmented buffer. @tail must be the last fragment of @skb -- or
1248 * @skb itself. If this would exceed the total buffer size the kernel
1249 * will panic. A pointer to the first byte of the extra data is
1250 * returned.
1251 */
1252
1253unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1254{
1255 if (tail != skb) {
1256 skb->data_len += len;
1257 skb->len += len;
1258 }
1259 return skb_put(tail, len);
1260}
1261EXPORT_SYMBOL_GPL(pskb_put);
1262
1263/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001264 * skb_put - add data to a buffer
1265 * @skb: buffer to use
1266 * @len: amount of data to add
1267 *
1268 * This function extends the used data area of the buffer. If this would
1269 * exceed the total buffer size the kernel will panic. A pointer to the
1270 * first byte of the extra data is returned.
1271 */
1272unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1273{
1274 unsigned char *tmp = skb_tail_pointer(skb);
1275 SKB_LINEAR_ASSERT(skb);
1276 skb->tail += len;
1277 skb->len += len;
1278 if (unlikely(skb->tail > skb->end))
1279 skb_over_panic(skb, len, __builtin_return_address(0));
1280 return tmp;
1281}
1282EXPORT_SYMBOL(skb_put);
1283
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001284/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001285 * skb_push - add data to the start of a buffer
1286 * @skb: buffer to use
1287 * @len: amount of data to add
1288 *
1289 * This function extends the used data area of the buffer at the buffer
1290 * start. If this would exceed the total buffer headroom the kernel will
1291 * panic. A pointer to the first byte of the extra data is returned.
1292 */
1293unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1294{
1295 skb->data -= len;
1296 skb->len += len;
1297 if (unlikely(skb->data<skb->head))
1298 skb_under_panic(skb, len, __builtin_return_address(0));
1299 return skb->data;
1300}
1301EXPORT_SYMBOL(skb_push);
1302
1303/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001304 * skb_pull - remove data from the start of a buffer
1305 * @skb: buffer to use
1306 * @len: amount of data to remove
1307 *
1308 * This function removes data from the start of a buffer, returning
1309 * the memory to the headroom. A pointer to the next data in the buffer
1310 * is returned. Once the data has been pulled future pushes will overwrite
1311 * the old data.
1312 */
1313unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1314{
David S. Miller47d29642010-05-02 02:21:44 -07001315 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001316}
1317EXPORT_SYMBOL(skb_pull);
1318
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001319/**
1320 * skb_trim - remove end from a buffer
1321 * @skb: buffer to alter
1322 * @len: new length
1323 *
1324 * Cut the length of a buffer down by removing data from the tail. If
1325 * the buffer is already under the length specified it is not modified.
1326 * The skb must be linear.
1327 */
1328void skb_trim(struct sk_buff *skb, unsigned int len)
1329{
1330 if (skb->len > len)
1331 __skb_trim(skb, len);
1332}
1333EXPORT_SYMBOL(skb_trim);
1334
Herbert Xu3cc0e872006-06-09 16:13:38 -07001335/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
1337
Herbert Xu3cc0e872006-06-09 16:13:38 -07001338int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Herbert Xu27b437c2006-07-13 19:26:39 -07001340 struct sk_buff **fragp;
1341 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int offset = skb_headlen(skb);
1343 int nfrags = skb_shinfo(skb)->nr_frags;
1344 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001345 int err;
1346
1347 if (skb_cloned(skb) &&
1348 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001351 i = 0;
1352 if (offset >= len)
1353 goto drop_pages;
1354
1355 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001356 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001357
1358 if (end < len) {
1359 offset = end;
1360 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001362
Eric Dumazet9e903e02011-10-18 21:00:24 +00001363 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001364
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001365drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001366 skb_shinfo(skb)->nr_frags = i;
1367
1368 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001369 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001370
David S. Miller21dc3302010-08-23 00:13:46 -07001371 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001372 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001373 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
Herbert Xu27b437c2006-07-13 19:26:39 -07001376 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1377 fragp = &frag->next) {
1378 int end = offset + frag->len;
1379
1380 if (skb_shared(frag)) {
1381 struct sk_buff *nfrag;
1382
1383 nfrag = skb_clone(frag, GFP_ATOMIC);
1384 if (unlikely(!nfrag))
1385 return -ENOMEM;
1386
1387 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001388 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001389 frag = nfrag;
1390 *fragp = frag;
1391 }
1392
1393 if (end < len) {
1394 offset = end;
1395 continue;
1396 }
1397
1398 if (end > len &&
1399 unlikely((err = pskb_trim(frag, len - offset))))
1400 return err;
1401
1402 if (frag->next)
1403 skb_drop_list(&frag->next);
1404 break;
1405 }
1406
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001407done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001408 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 skb->data_len -= skb->len - len;
1410 skb->len = len;
1411 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001412 skb->len = len;
1413 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001414 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
1417 return 0;
1418}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001419EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421/**
1422 * __pskb_pull_tail - advance tail of skb header
1423 * @skb: buffer to reallocate
1424 * @delta: number of bytes to advance tail
1425 *
1426 * The function makes a sense only on a fragmented &sk_buff,
1427 * it expands header moving its tail forward and copying necessary
1428 * data from fragmented part.
1429 *
1430 * &sk_buff MUST have reference count of 1.
1431 *
1432 * Returns %NULL (and &sk_buff does not change) if pull failed
1433 * or value of new tail of skb in the case of success.
1434 *
1435 * All the pointers pointing into skb header may change and must be
1436 * reloaded after call to this function.
1437 */
1438
1439/* Moves tail of skb head forward, copying data from fragmented part,
1440 * when it is necessary.
1441 * 1. It may fail due to malloc failure.
1442 * 2. It may change skb pointers.
1443 *
1444 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1445 */
1446unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1447{
1448 /* If skb has not enough free space at tail, get new one
1449 * plus 128 bytes for future expansions. If we have enough
1450 * room at tail, reallocate without expansion only if skb is cloned.
1451 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001452 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (eat > 0 || skb_cloned(skb)) {
1455 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1456 GFP_ATOMIC))
1457 return NULL;
1458 }
1459
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001460 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 BUG();
1462
1463 /* Optimization: no fragments, no reasons to preestimate
1464 * size of pulled pages. Superb.
1465 */
David S. Miller21dc3302010-08-23 00:13:46 -07001466 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto pull_pages;
1468
1469 /* Estimate size of pulled pages. */
1470 eat = delta;
1471 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001472 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1473
1474 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001476 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
1479 /* If we need update frag list, we are in troubles.
1480 * Certainly, it possible to add an offset to skb data,
1481 * but taking into account that pulling is expected to
1482 * be very rare operation, it is worth to fight against
1483 * further bloating skb head and crucify ourselves here instead.
1484 * Pure masohism, indeed. 8)8)
1485 */
1486 if (eat) {
1487 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1488 struct sk_buff *clone = NULL;
1489 struct sk_buff *insp = NULL;
1490
1491 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001492 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 if (list->len <= eat) {
1495 /* Eaten as whole. */
1496 eat -= list->len;
1497 list = list->next;
1498 insp = list;
1499 } else {
1500 /* Eaten partially. */
1501
1502 if (skb_shared(list)) {
1503 /* Sucks! We need to fork list. :-( */
1504 clone = skb_clone(list, GFP_ATOMIC);
1505 if (!clone)
1506 return NULL;
1507 insp = list->next;
1508 list = clone;
1509 } else {
1510 /* This may be pulled without
1511 * problems. */
1512 insp = list;
1513 }
1514 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001515 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return NULL;
1517 }
1518 break;
1519 }
1520 } while (eat);
1521
1522 /* Free pulled out fragments. */
1523 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1524 skb_shinfo(skb)->frag_list = list->next;
1525 kfree_skb(list);
1526 }
1527 /* And insert new clone at head. */
1528 if (clone) {
1529 clone->next = list;
1530 skb_shinfo(skb)->frag_list = clone;
1531 }
1532 }
1533 /* Success! Now we may commit changes to skb data. */
1534
1535pull_pages:
1536 eat = delta;
1537 k = 0;
1538 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001539 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1540
1541 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001542 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001543 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 } else {
1545 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1546 if (eat) {
1547 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001548 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 eat = 0;
1550 }
1551 k++;
1552 }
1553 }
1554 skb_shinfo(skb)->nr_frags = k;
1555
1556 skb->tail += delta;
1557 skb->data_len -= delta;
1558
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001559 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001561EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Eric Dumazet22019b12011-07-29 18:37:31 +00001563/**
1564 * skb_copy_bits - copy bits from skb to kernel buffer
1565 * @skb: source skb
1566 * @offset: offset in source
1567 * @to: destination buffer
1568 * @len: number of bytes to copy
1569 *
1570 * Copy the specified number of bytes from the source skb to the
1571 * destination buffer.
1572 *
1573 * CAUTION ! :
1574 * If its prototype is ever changed,
1575 * check arch/{*}/net/{*}.S files,
1576 * since it is called from BPF assembly code.
1577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1579{
David S. Miller1a028e52007-04-27 15:21:23 -07001580 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001581 struct sk_buff *frag_iter;
1582 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (offset > (int)skb->len - len)
1585 goto fault;
1586
1587 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001588 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (copy > len)
1590 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001591 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if ((len -= copy) == 0)
1593 return 0;
1594 offset += copy;
1595 to += copy;
1596 }
1597
1598 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001599 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001600 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001602 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001603
Eric Dumazet51c56b02012-04-05 11:35:15 +02001604 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if ((copy = end - offset) > 0) {
1606 u8 *vaddr;
1607
1608 if (copy > len)
1609 copy = len;
1610
Eric Dumazet51c56b02012-04-05 11:35:15 +02001611 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001613 vaddr + f->page_offset + offset - start,
1614 copy);
1615 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if ((len -= copy) == 0)
1618 return 0;
1619 offset += copy;
1620 to += copy;
1621 }
David S. Miller1a028e52007-04-27 15:21:23 -07001622 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
David S. Millerfbb398a2009-06-09 00:18:59 -07001625 skb_walk_frags(skb, frag_iter) {
1626 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
David S. Millerfbb398a2009-06-09 00:18:59 -07001628 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
David S. Millerfbb398a2009-06-09 00:18:59 -07001630 end = start + frag_iter->len;
1631 if ((copy = end - offset) > 0) {
1632 if (copy > len)
1633 copy = len;
1634 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1635 goto fault;
1636 if ((len -= copy) == 0)
1637 return 0;
1638 offset += copy;
1639 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001641 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Shirley Maa6686f22011-07-06 12:22:12 +00001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (!len)
1645 return 0;
1646
1647fault:
1648 return -EFAULT;
1649}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001650EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Jens Axboe9c55e012007-11-06 23:30:13 -08001652/*
1653 * Callback from splice_to_pipe(), if we need to release some pages
1654 * at the end of the spd in case we error'ed out in filling the pipe.
1655 */
1656static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1657{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001658 put_page(spd->pages[i]);
1659}
Jens Axboe9c55e012007-11-06 23:30:13 -08001660
David S. Millera108d5f2012-04-23 23:06:11 -04001661static struct page *linear_to_page(struct page *page, unsigned int *len,
1662 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001663 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001664{
Eric Dumazet5640f762012-09-23 23:04:42 +00001665 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001666
Eric Dumazet5640f762012-09-23 23:04:42 +00001667 if (!sk_page_frag_refill(sk, pfrag))
1668 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001669
Eric Dumazet5640f762012-09-23 23:04:42 +00001670 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001671
Eric Dumazet5640f762012-09-23 23:04:42 +00001672 memcpy(page_address(pfrag->page) + pfrag->offset,
1673 page_address(page) + *offset, *len);
1674 *offset = pfrag->offset;
1675 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001676
Eric Dumazet5640f762012-09-23 23:04:42 +00001677 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001678}
1679
Eric Dumazet41c73a02012-04-22 12:26:16 +00001680static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1681 struct page *page,
1682 unsigned int offset)
1683{
1684 return spd->nr_pages &&
1685 spd->pages[spd->nr_pages - 1] == page &&
1686 (spd->partial[spd->nr_pages - 1].offset +
1687 spd->partial[spd->nr_pages - 1].len == offset);
1688}
1689
Jens Axboe9c55e012007-11-06 23:30:13 -08001690/*
1691 * Fill page/offset/length into spd, if it can hold more pages.
1692 */
David S. Millera108d5f2012-04-23 23:06:11 -04001693static bool spd_fill_page(struct splice_pipe_desc *spd,
1694 struct pipe_inode_info *pipe, struct page *page,
1695 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001696 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001697 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001698{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001699 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001700 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001701
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001702 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001703 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001704 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001705 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001706 }
1707 if (spd_can_coalesce(spd, page, offset)) {
1708 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001709 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001710 }
1711 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001712 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001713 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001714 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001715 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001716
David S. Millera108d5f2012-04-23 23:06:11 -04001717 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001718}
1719
David S. Millera108d5f2012-04-23 23:06:11 -04001720static bool __splice_segment(struct page *page, unsigned int poff,
1721 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001722 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001723 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001724 struct sock *sk,
1725 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001726{
1727 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001728 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001729
1730 /* skip this segment if already processed */
1731 if (*off >= plen) {
1732 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001733 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001734 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001735
Octavian Purdila2870c432008-07-15 00:49:11 -07001736 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001737 poff += *off;
1738 plen -= *off;
1739 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001740
Eric Dumazet18aafc62013-01-11 14:46:37 +00001741 do {
1742 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001743
Eric Dumazet18aafc62013-01-11 14:46:37 +00001744 if (spd_fill_page(spd, pipe, page, &flen, poff,
1745 linear, sk))
1746 return true;
1747 poff += flen;
1748 plen -= flen;
1749 *len -= flen;
1750 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001751
David S. Millera108d5f2012-04-23 23:06:11 -04001752 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001753}
1754
1755/*
David S. Millera108d5f2012-04-23 23:06:11 -04001756 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001757 * pipe is full or if we already spliced the requested length.
1758 */
David S. Millera108d5f2012-04-23 23:06:11 -04001759static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1760 unsigned int *offset, unsigned int *len,
1761 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001762{
1763 int seg;
1764
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001765 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001766 * If skb->head_frag is set, this 'linear' part is backed by a
1767 * fragment, and if the head is not shared with any clones then
1768 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001769 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001770 if (__splice_segment(virt_to_page(skb->data),
1771 (unsigned long) skb->data & (PAGE_SIZE - 1),
1772 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001773 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001774 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001775 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001776 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001777
1778 /*
1779 * then map the fragments
1780 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001781 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1782 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1783
Ian Campbellea2ab692011-08-22 23:44:58 +00001784 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001785 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001786 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001787 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001788 }
1789
David S. Millera108d5f2012-04-23 23:06:11 -04001790 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001791}
1792
1793/*
1794 * Map data from the skb to a pipe. Should handle both the linear part,
1795 * the fragments, and the frag list. It does NOT handle frag lists within
1796 * the frag list, if such a thing exists. We'd probably need to recurse to
1797 * handle that cleanly.
1798 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001799int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001800 struct pipe_inode_info *pipe, unsigned int tlen,
1801 unsigned int flags)
1802{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001803 struct partial_page partial[MAX_SKB_FRAGS];
1804 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001805 struct splice_pipe_desc spd = {
1806 .pages = pages,
1807 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001808 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001809 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001810 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001811 .spd_release = sock_spd_release,
1812 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001813 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001814 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001815 int ret = 0;
1816
Jens Axboe9c55e012007-11-06 23:30:13 -08001817 /*
1818 * __skb_splice_bits() only fails if the output has no room left,
1819 * so no point in going over the frag_list for the error case.
1820 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001821 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001822 goto done;
1823 else if (!tlen)
1824 goto done;
1825
1826 /*
1827 * now see if we have a frag_list to map
1828 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001829 skb_walk_frags(skb, frag_iter) {
1830 if (!tlen)
1831 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001832 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001833 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001834 }
1835
1836done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001837 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001838 /*
1839 * Drop the socket lock, otherwise we have reverse
1840 * locking dependencies between sk_lock and i_mutex
1841 * here as compared to sendfile(). We enter here
1842 * with the socket lock held, and splice_to_pipe() will
1843 * grab the pipe inode lock. For sendfile() emulation,
1844 * we call into ->sendpage() with the i_mutex lock held
1845 * and networking will grab the socket lock.
1846 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001847 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001848 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001849 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001850 }
1851
Jens Axboe35f3d142010-05-20 10:43:18 +02001852 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001853}
1854
Herbert Xu357b40a2005-04-19 22:30:14 -07001855/**
1856 * skb_store_bits - store bits from kernel buffer to skb
1857 * @skb: destination buffer
1858 * @offset: offset in destination
1859 * @from: source buffer
1860 * @len: number of bytes to copy
1861 *
1862 * Copy the specified number of bytes from the source buffer to the
1863 * destination skb. This function handles all the messy bits of
1864 * traversing fragment lists and such.
1865 */
1866
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001867int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001868{
David S. Miller1a028e52007-04-27 15:21:23 -07001869 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001870 struct sk_buff *frag_iter;
1871 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001872
1873 if (offset > (int)skb->len - len)
1874 goto fault;
1875
David S. Miller1a028e52007-04-27 15:21:23 -07001876 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001877 if (copy > len)
1878 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001879 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001880 if ((len -= copy) == 0)
1881 return 0;
1882 offset += copy;
1883 from += copy;
1884 }
1885
1886 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1887 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001888 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001889
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001890 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001891
Eric Dumazet9e903e02011-10-18 21:00:24 +00001892 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001893 if ((copy = end - offset) > 0) {
1894 u8 *vaddr;
1895
1896 if (copy > len)
1897 copy = len;
1898
Eric Dumazet51c56b02012-04-05 11:35:15 +02001899 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001900 memcpy(vaddr + frag->page_offset + offset - start,
1901 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001902 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001903
1904 if ((len -= copy) == 0)
1905 return 0;
1906 offset += copy;
1907 from += copy;
1908 }
David S. Miller1a028e52007-04-27 15:21:23 -07001909 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001910 }
1911
David S. Millerfbb398a2009-06-09 00:18:59 -07001912 skb_walk_frags(skb, frag_iter) {
1913 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001914
David S. Millerfbb398a2009-06-09 00:18:59 -07001915 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001916
David S. Millerfbb398a2009-06-09 00:18:59 -07001917 end = start + frag_iter->len;
1918 if ((copy = end - offset) > 0) {
1919 if (copy > len)
1920 copy = len;
1921 if (skb_store_bits(frag_iter, offset - start,
1922 from, copy))
1923 goto fault;
1924 if ((len -= copy) == 0)
1925 return 0;
1926 offset += copy;
1927 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001928 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001929 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001930 }
1931 if (!len)
1932 return 0;
1933
1934fault:
1935 return -EFAULT;
1936}
Herbert Xu357b40a2005-04-19 22:30:14 -07001937EXPORT_SYMBOL(skb_store_bits);
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001940__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1941 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
David S. Miller1a028e52007-04-27 15:21:23 -07001943 int start = skb_headlen(skb);
1944 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001945 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 int pos = 0;
1947
1948 /* Checksum header. */
1949 if (copy > 0) {
1950 if (copy > len)
1951 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001952 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if ((len -= copy) == 0)
1954 return csum;
1955 offset += copy;
1956 pos = copy;
1957 }
1958
1959 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001960 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001961 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001963 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001964
Eric Dumazet51c56b02012-04-05 11:35:15 +02001965 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001967 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 if (copy > len)
1971 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001972 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01001973 csum2 = ops->update(vaddr + frag->page_offset +
1974 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001975 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01001976 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (!(len -= copy))
1978 return csum;
1979 offset += copy;
1980 pos += copy;
1981 }
David S. Miller1a028e52007-04-27 15:21:23 -07001982 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
David S. Millerfbb398a2009-06-09 00:18:59 -07001985 skb_walk_frags(skb, frag_iter) {
1986 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
David S. Millerfbb398a2009-06-09 00:18:59 -07001988 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
David S. Millerfbb398a2009-06-09 00:18:59 -07001990 end = start + frag_iter->len;
1991 if ((copy = end - offset) > 0) {
1992 __wsum csum2;
1993 if (copy > len)
1994 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001995 csum2 = __skb_checksum(frag_iter, offset - start,
1996 copy, 0, ops);
1997 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07001998 if ((len -= copy) == 0)
1999 return csum;
2000 offset += copy;
2001 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002003 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002005 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 return csum;
2008}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002009EXPORT_SYMBOL(__skb_checksum);
2010
2011__wsum skb_checksum(const struct sk_buff *skb, int offset,
2012 int len, __wsum csum)
2013{
2014 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002015 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002016 .combine = csum_block_add_ext,
2017 };
2018
2019 return __skb_checksum(skb, offset, len, csum, &ops);
2020}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002021EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023/* Both of above in one bottle. */
2024
Al Viro81d77662006-11-14 21:37:33 -08002025__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2026 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
David S. Miller1a028e52007-04-27 15:21:23 -07002028 int start = skb_headlen(skb);
2029 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002030 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int pos = 0;
2032
2033 /* Copy header. */
2034 if (copy > 0) {
2035 if (copy > len)
2036 copy = len;
2037 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2038 copy, csum);
2039 if ((len -= copy) == 0)
2040 return csum;
2041 offset += copy;
2042 to += copy;
2043 pos = copy;
2044 }
2045
2046 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002047 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002049 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002050
Eric Dumazet9e903e02011-10-18 21:00:24 +00002051 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002053 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 u8 *vaddr;
2055 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2056
2057 if (copy > len)
2058 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002059 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002061 frag->page_offset +
2062 offset - start, to,
2063 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002064 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 csum = csum_block_add(csum, csum2, pos);
2066 if (!(len -= copy))
2067 return csum;
2068 offset += copy;
2069 to += copy;
2070 pos += copy;
2071 }
David S. Miller1a028e52007-04-27 15:21:23 -07002072 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074
David S. Millerfbb398a2009-06-09 00:18:59 -07002075 skb_walk_frags(skb, frag_iter) {
2076 __wsum csum2;
2077 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
David S. Millerfbb398a2009-06-09 00:18:59 -07002079 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
David S. Millerfbb398a2009-06-09 00:18:59 -07002081 end = start + frag_iter->len;
2082 if ((copy = end - offset) > 0) {
2083 if (copy > len)
2084 copy = len;
2085 csum2 = skb_copy_and_csum_bits(frag_iter,
2086 offset - start,
2087 to, copy, 0);
2088 csum = csum_block_add(csum, csum2, pos);
2089 if ((len -= copy) == 0)
2090 return csum;
2091 offset += copy;
2092 to += copy;
2093 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002095 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002097 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return csum;
2099}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002100EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Thomas Grafaf2806f2013-12-13 15:22:17 +01002102 /**
2103 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2104 * @from: source buffer
2105 *
2106 * Calculates the amount of linear headroom needed in the 'to' skb passed
2107 * into skb_zerocopy().
2108 */
2109unsigned int
2110skb_zerocopy_headlen(const struct sk_buff *from)
2111{
2112 unsigned int hlen = 0;
2113
2114 if (!from->head_frag ||
2115 skb_headlen(from) < L1_CACHE_BYTES ||
2116 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2117 hlen = skb_headlen(from);
2118
2119 if (skb_has_frag_list(from))
2120 hlen = from->len;
2121
2122 return hlen;
2123}
2124EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2125
2126/**
2127 * skb_zerocopy - Zero copy skb to skb
2128 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002129 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002130 * @len: number of bytes to copy from source buffer
2131 * @hlen: size of linear headroom in destination buffer
2132 *
2133 * Copies up to `len` bytes from `from` to `to` by creating references
2134 * to the frags in the source buffer.
2135 *
2136 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2137 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002138 *
2139 * Return value:
2140 * 0: everything is OK
2141 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2142 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002143 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002144int
2145skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002146{
2147 int i, j = 0;
2148 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002149 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002150 struct page *page;
2151 unsigned int offset;
2152
2153 BUG_ON(!from->head_frag && !hlen);
2154
2155 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002156 if (len <= skb_tailroom(to))
2157 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002158
2159 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002160 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2161 if (unlikely(ret))
2162 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002163 len -= hlen;
2164 } else {
2165 plen = min_t(int, skb_headlen(from), len);
2166 if (plen) {
2167 page = virt_to_head_page(from->head);
2168 offset = from->data - (unsigned char *)page_address(page);
2169 __skb_fill_page_desc(to, 0, page, offset, plen);
2170 get_page(page);
2171 j = 1;
2172 len -= plen;
2173 }
2174 }
2175
2176 to->truesize += len + plen;
2177 to->len += len + plen;
2178 to->data_len += len + plen;
2179
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002180 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2181 skb_tx_error(from);
2182 return -ENOMEM;
2183 }
2184
Thomas Grafaf2806f2013-12-13 15:22:17 +01002185 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2186 if (!len)
2187 break;
2188 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2189 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2190 len -= skb_shinfo(to)->frags[j].size;
2191 skb_frag_ref(to, j);
2192 j++;
2193 }
2194 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002195
2196 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002197}
2198EXPORT_SYMBOL_GPL(skb_zerocopy);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2201{
Al Virod3bc23e2006-11-14 21:24:49 -08002202 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 long csstart;
2204
Patrick McHardy84fa7932006-08-29 16:44:56 -07002205 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002206 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 else
2208 csstart = skb_headlen(skb);
2209
Kris Katterjohn09a62662006-01-08 22:24:28 -08002210 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002212 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 csum = 0;
2215 if (csstart != skb->len)
2216 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2217 skb->len - csstart, 0);
2218
Patrick McHardy84fa7932006-08-29 16:44:56 -07002219 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002220 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Al Virod3bc23e2006-11-14 21:24:49 -08002222 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002225EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227/**
2228 * skb_dequeue - remove from the head of the queue
2229 * @list: list to dequeue from
2230 *
2231 * Remove the head of the list. The list lock is taken so the function
2232 * may be used safely with other locking list functions. The head item is
2233 * returned or %NULL if the list is empty.
2234 */
2235
2236struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2237{
2238 unsigned long flags;
2239 struct sk_buff *result;
2240
2241 spin_lock_irqsave(&list->lock, flags);
2242 result = __skb_dequeue(list);
2243 spin_unlock_irqrestore(&list->lock, flags);
2244 return result;
2245}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002246EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248/**
2249 * skb_dequeue_tail - remove from the tail of the queue
2250 * @list: list to dequeue from
2251 *
2252 * Remove the tail of the list. The list lock is taken so the function
2253 * may be used safely with other locking list functions. The tail item is
2254 * returned or %NULL if the list is empty.
2255 */
2256struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2257{
2258 unsigned long flags;
2259 struct sk_buff *result;
2260
2261 spin_lock_irqsave(&list->lock, flags);
2262 result = __skb_dequeue_tail(list);
2263 spin_unlock_irqrestore(&list->lock, flags);
2264 return result;
2265}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002266EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268/**
2269 * skb_queue_purge - empty a list
2270 * @list: list to empty
2271 *
2272 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2273 * the list and one reference dropped. This function takes the list
2274 * lock and is atomic with respect to other list locking functions.
2275 */
2276void skb_queue_purge(struct sk_buff_head *list)
2277{
2278 struct sk_buff *skb;
2279 while ((skb = skb_dequeue(list)) != NULL)
2280 kfree_skb(skb);
2281}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002282EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284/**
2285 * skb_queue_head - queue a buffer at the list head
2286 * @list: list to use
2287 * @newsk: buffer to queue
2288 *
2289 * Queue a buffer at the start of the list. This function takes the
2290 * list lock and can be used safely with other locking &sk_buff functions
2291 * safely.
2292 *
2293 * A buffer cannot be placed on two lists at the same time.
2294 */
2295void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2296{
2297 unsigned long flags;
2298
2299 spin_lock_irqsave(&list->lock, flags);
2300 __skb_queue_head(list, newsk);
2301 spin_unlock_irqrestore(&list->lock, flags);
2302}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002303EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305/**
2306 * skb_queue_tail - queue a buffer at the list tail
2307 * @list: list to use
2308 * @newsk: buffer to queue
2309 *
2310 * Queue a buffer at the tail of the list. This function takes the
2311 * list lock and can be used safely with other locking &sk_buff functions
2312 * safely.
2313 *
2314 * A buffer cannot be placed on two lists at the same time.
2315 */
2316void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2317{
2318 unsigned long flags;
2319
2320 spin_lock_irqsave(&list->lock, flags);
2321 __skb_queue_tail(list, newsk);
2322 spin_unlock_irqrestore(&list->lock, flags);
2323}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002324EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326/**
2327 * skb_unlink - remove a buffer from a list
2328 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002329 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 *
David S. Miller8728b832005-08-09 19:25:21 -07002331 * Remove a packet from a list. The list locks are taken and this
2332 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 *
David S. Miller8728b832005-08-09 19:25:21 -07002334 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 */
David S. Miller8728b832005-08-09 19:25:21 -07002336void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
David S. Miller8728b832005-08-09 19:25:21 -07002338 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
David S. Miller8728b832005-08-09 19:25:21 -07002340 spin_lock_irqsave(&list->lock, flags);
2341 __skb_unlink(skb, list);
2342 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002344EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346/**
2347 * skb_append - append a buffer
2348 * @old: buffer to insert after
2349 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002350 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 *
2352 * Place a packet after a given packet in a list. The list locks are taken
2353 * and this function is atomic with respect to other list locked calls.
2354 * A buffer cannot be placed on two lists at the same time.
2355 */
David S. Miller8728b832005-08-09 19:25:21 -07002356void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 unsigned long flags;
2359
David S. Miller8728b832005-08-09 19:25:21 -07002360 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002361 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002362 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002364EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366/**
2367 * skb_insert - insert a buffer
2368 * @old: buffer to insert before
2369 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002370 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 *
David S. Miller8728b832005-08-09 19:25:21 -07002372 * Place a packet before a given packet in a list. The list locks are
2373 * taken and this function is atomic with respect to other list locked
2374 * calls.
2375 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 * A buffer cannot be placed on two lists at the same time.
2377 */
David S. Miller8728b832005-08-09 19:25:21 -07002378void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
2380 unsigned long flags;
2381
David S. Miller8728b832005-08-09 19:25:21 -07002382 spin_lock_irqsave(&list->lock, flags);
2383 __skb_insert(newsk, old->prev, old, list);
2384 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002386EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388static inline void skb_split_inside_header(struct sk_buff *skb,
2389 struct sk_buff* skb1,
2390 const u32 len, const int pos)
2391{
2392 int i;
2393
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002394 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2395 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /* And move data appendix as is. */
2397 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2398 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2399
2400 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2401 skb_shinfo(skb)->nr_frags = 0;
2402 skb1->data_len = skb->data_len;
2403 skb1->len += skb1->data_len;
2404 skb->data_len = 0;
2405 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002406 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409static inline void skb_split_no_header(struct sk_buff *skb,
2410 struct sk_buff* skb1,
2411 const u32 len, int pos)
2412{
2413 int i, k = 0;
2414 const int nfrags = skb_shinfo(skb)->nr_frags;
2415
2416 skb_shinfo(skb)->nr_frags = 0;
2417 skb1->len = skb1->data_len = skb->len - len;
2418 skb->len = len;
2419 skb->data_len = len - pos;
2420
2421 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002422 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 if (pos + size > len) {
2425 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2426
2427 if (pos < len) {
2428 /* Split frag.
2429 * We have two variants in this case:
2430 * 1. Move all the frag to the second
2431 * part, if it is possible. F.e.
2432 * this approach is mandatory for TUX,
2433 * where splitting is expensive.
2434 * 2. Split is accurately. We make this.
2435 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002436 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002438 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2439 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 skb_shinfo(skb)->nr_frags++;
2441 }
2442 k++;
2443 } else
2444 skb_shinfo(skb)->nr_frags++;
2445 pos += size;
2446 }
2447 skb_shinfo(skb1)->nr_frags = k;
2448}
2449
2450/**
2451 * skb_split - Split fragmented skb to two parts at length len.
2452 * @skb: the buffer to split
2453 * @skb1: the buffer to receive the second part
2454 * @len: new length for skb
2455 */
2456void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2457{
2458 int pos = skb_headlen(skb);
2459
Amerigo Wang68534c62013-02-19 22:51:30 +00002460 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (len < pos) /* Split line is inside header. */
2462 skb_split_inside_header(skb, skb1, len, pos);
2463 else /* Second chunk has no header, nothing to copy. */
2464 skb_split_no_header(skb, skb1, len, pos);
2465}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002466EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002468/* Shifting from/to a cloned skb is a no-go.
2469 *
2470 * Caller cannot keep skb_shinfo related pointers past calling here!
2471 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002472static int skb_prepare_for_shift(struct sk_buff *skb)
2473{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002474 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002475}
2476
2477/**
2478 * skb_shift - Shifts paged data partially from skb to another
2479 * @tgt: buffer into which tail data gets added
2480 * @skb: buffer from which the paged data comes from
2481 * @shiftlen: shift up to this many bytes
2482 *
2483 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002484 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002485 * It's up to caller to free skb if everything was shifted.
2486 *
2487 * If @tgt runs out of frags, the whole operation is aborted.
2488 *
2489 * Skb cannot include anything else but paged data while tgt is allowed
2490 * to have non-paged data as well.
2491 *
2492 * TODO: full sized shift could be optimized but that would need
2493 * specialized skb free'er to handle frags without up-to-date nr_frags.
2494 */
2495int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2496{
2497 int from, to, merge, todo;
2498 struct skb_frag_struct *fragfrom, *fragto;
2499
2500 BUG_ON(shiftlen > skb->len);
2501 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2502
2503 todo = shiftlen;
2504 from = 0;
2505 to = skb_shinfo(tgt)->nr_frags;
2506 fragfrom = &skb_shinfo(skb)->frags[from];
2507
2508 /* Actual merge is delayed until the point when we know we can
2509 * commit all, so that we don't have to undo partial changes
2510 */
2511 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002512 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2513 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002514 merge = -1;
2515 } else {
2516 merge = to - 1;
2517
Eric Dumazet9e903e02011-10-18 21:00:24 +00002518 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002519 if (todo < 0) {
2520 if (skb_prepare_for_shift(skb) ||
2521 skb_prepare_for_shift(tgt))
2522 return 0;
2523
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002524 /* All previous frag pointers might be stale! */
2525 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002526 fragto = &skb_shinfo(tgt)->frags[merge];
2527
Eric Dumazet9e903e02011-10-18 21:00:24 +00002528 skb_frag_size_add(fragto, shiftlen);
2529 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002530 fragfrom->page_offset += shiftlen;
2531
2532 goto onlymerged;
2533 }
2534
2535 from++;
2536 }
2537
2538 /* Skip full, not-fitting skb to avoid expensive operations */
2539 if ((shiftlen == skb->len) &&
2540 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2541 return 0;
2542
2543 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2544 return 0;
2545
2546 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2547 if (to == MAX_SKB_FRAGS)
2548 return 0;
2549
2550 fragfrom = &skb_shinfo(skb)->frags[from];
2551 fragto = &skb_shinfo(tgt)->frags[to];
2552
Eric Dumazet9e903e02011-10-18 21:00:24 +00002553 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002554 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002555 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002556 from++;
2557 to++;
2558
2559 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002560 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002561 fragto->page = fragfrom->page;
2562 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002563 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002564
2565 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002566 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002567 todo = 0;
2568
2569 to++;
2570 break;
2571 }
2572 }
2573
2574 /* Ready to "commit" this state change to tgt */
2575 skb_shinfo(tgt)->nr_frags = to;
2576
2577 if (merge >= 0) {
2578 fragfrom = &skb_shinfo(skb)->frags[0];
2579 fragto = &skb_shinfo(tgt)->frags[merge];
2580
Eric Dumazet9e903e02011-10-18 21:00:24 +00002581 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002582 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002583 }
2584
2585 /* Reposition in the original skb */
2586 to = 0;
2587 while (from < skb_shinfo(skb)->nr_frags)
2588 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2589 skb_shinfo(skb)->nr_frags = to;
2590
2591 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2592
2593onlymerged:
2594 /* Most likely the tgt won't ever need its checksum anymore, skb on
2595 * the other hand might need it if it needs to be resent
2596 */
2597 tgt->ip_summed = CHECKSUM_PARTIAL;
2598 skb->ip_summed = CHECKSUM_PARTIAL;
2599
2600 /* Yak, is it really working this way? Some helper please? */
2601 skb->len -= shiftlen;
2602 skb->data_len -= shiftlen;
2603 skb->truesize -= shiftlen;
2604 tgt->len += shiftlen;
2605 tgt->data_len += shiftlen;
2606 tgt->truesize += shiftlen;
2607
2608 return shiftlen;
2609}
2610
Thomas Graf677e90e2005-06-23 20:59:51 -07002611/**
2612 * skb_prepare_seq_read - Prepare a sequential read of skb data
2613 * @skb: the buffer to read
2614 * @from: lower offset of data to be read
2615 * @to: upper offset of data to be read
2616 * @st: state variable
2617 *
2618 * Initializes the specified state variable. Must be called before
2619 * invoking skb_seq_read() for the first time.
2620 */
2621void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2622 unsigned int to, struct skb_seq_state *st)
2623{
2624 st->lower_offset = from;
2625 st->upper_offset = to;
2626 st->root_skb = st->cur_skb = skb;
2627 st->frag_idx = st->stepped_offset = 0;
2628 st->frag_data = NULL;
2629}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002630EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002631
2632/**
2633 * skb_seq_read - Sequentially read skb data
2634 * @consumed: number of bytes consumed by the caller so far
2635 * @data: destination pointer for data to be returned
2636 * @st: state variable
2637 *
Mathias Krausebc323832013-11-07 14:18:26 +01002638 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002639 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002640 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002641 * of the block or 0 if the end of the skb data or the upper
2642 * offset has been reached.
2643 *
2644 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002645 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002646 * of bytes already consumed and the next call to
2647 * skb_seq_read() will return the remaining part of the block.
2648 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002649 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002650 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002651 * reads of potentially non linear data.
2652 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002653 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002654 * at the moment, state->root_skb could be replaced with
2655 * a stack for this purpose.
2656 */
2657unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2658 struct skb_seq_state *st)
2659{
2660 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2661 skb_frag_t *frag;
2662
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002663 if (unlikely(abs_offset >= st->upper_offset)) {
2664 if (st->frag_data) {
2665 kunmap_atomic(st->frag_data);
2666 st->frag_data = NULL;
2667 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002668 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002669 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002670
2671next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002672 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002673
Thomas Chenault995b3372009-05-18 21:43:27 -07002674 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002675 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002676 return block_limit - abs_offset;
2677 }
2678
2679 if (st->frag_idx == 0 && !st->frag_data)
2680 st->stepped_offset += skb_headlen(st->cur_skb);
2681
2682 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2683 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002684 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002685
2686 if (abs_offset < block_limit) {
2687 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002688 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002689
2690 *data = (u8 *) st->frag_data + frag->page_offset +
2691 (abs_offset - st->stepped_offset);
2692
2693 return block_limit - abs_offset;
2694 }
2695
2696 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002697 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002698 st->frag_data = NULL;
2699 }
2700
2701 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002702 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002703 }
2704
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002705 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002706 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002707 st->frag_data = NULL;
2708 }
2709
David S. Miller21dc3302010-08-23 00:13:46 -07002710 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002711 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002712 st->frag_idx = 0;
2713 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002714 } else if (st->cur_skb->next) {
2715 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002716 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002717 goto next_skb;
2718 }
2719
2720 return 0;
2721}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002722EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002723
2724/**
2725 * skb_abort_seq_read - Abort a sequential read of skb data
2726 * @st: state variable
2727 *
2728 * Must be called if skb_seq_read() was not called until it
2729 * returned 0.
2730 */
2731void skb_abort_seq_read(struct skb_seq_state *st)
2732{
2733 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002734 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002735}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002736EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002737
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002738#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2739
2740static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2741 struct ts_config *conf,
2742 struct ts_state *state)
2743{
2744 return skb_seq_read(offset, text, TS_SKB_CB(state));
2745}
2746
2747static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2748{
2749 skb_abort_seq_read(TS_SKB_CB(state));
2750}
2751
2752/**
2753 * skb_find_text - Find a text pattern in skb data
2754 * @skb: the buffer to look in
2755 * @from: search offset
2756 * @to: search limit
2757 * @config: textsearch configuration
2758 * @state: uninitialized textsearch state variable
2759 *
2760 * Finds a pattern in the skb data according to the specified
2761 * textsearch configuration. Use textsearch_next() to retrieve
2762 * subsequent occurrences of the pattern. Returns the offset
2763 * to the first occurrence or UINT_MAX if no match was found.
2764 */
2765unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2766 unsigned int to, struct ts_config *config,
2767 struct ts_state *state)
2768{
Phil Oesterf72b9482006-06-26 00:00:57 -07002769 unsigned int ret;
2770
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002771 config->get_next_block = skb_ts_get_next_block;
2772 config->finish = skb_ts_finish;
2773
2774 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2775
Phil Oesterf72b9482006-06-26 00:00:57 -07002776 ret = textsearch_find(config, state);
2777 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002778}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002779EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002780
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002781/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002782 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002783 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002784 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002785 * @getfrag: call back function to be used for getting the user data
2786 * @from: pointer to user message iov
2787 * @length: length of the iov message
2788 *
2789 * Description: This procedure append the user data in the fragment part
2790 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2791 */
2792int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002793 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002794 int len, int odd, struct sk_buff *skb),
2795 void *from, int length)
2796{
Eric Dumazetb2111722012-12-28 06:06:37 +00002797 int frg_cnt = skb_shinfo(skb)->nr_frags;
2798 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002799 int offset = 0;
2800 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002801 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002802
2803 do {
2804 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002805 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002806 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002807
Eric Dumazetb2111722012-12-28 06:06:37 +00002808 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002809 return -ENOMEM;
2810
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002811 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002812 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002813
Eric Dumazetb2111722012-12-28 06:06:37 +00002814 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2815 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002816 if (ret < 0)
2817 return -EFAULT;
2818
2819 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002820 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2821 copy);
2822 frg_cnt++;
2823 pfrag->offset += copy;
2824 get_page(pfrag->page);
2825
2826 skb->truesize += copy;
2827 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002828 skb->len += copy;
2829 skb->data_len += copy;
2830 offset += copy;
2831 length -= copy;
2832
2833 } while (length > 0);
2834
2835 return 0;
2836}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002837EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002838
Herbert Xucbb042f2006-03-20 22:43:56 -08002839/**
2840 * skb_pull_rcsum - pull skb and update receive checksum
2841 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002842 * @len: length of data pulled
2843 *
2844 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002845 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002846 * receive path processing instead of skb_pull unless you know
2847 * that the checksum difference is zero (e.g., a valid IP header)
2848 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002849 */
2850unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2851{
2852 BUG_ON(len > skb->len);
2853 skb->len -= len;
2854 BUG_ON(skb->len < skb->data_len);
2855 skb_postpull_rcsum(skb, skb->data, len);
2856 return skb->data += len;
2857}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002858EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2859
Herbert Xuf4c50d92006-06-22 03:02:40 -07002860/**
2861 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002862 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002863 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002864 *
2865 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002866 * a pointer to the first in a list of new skbs for the segments.
2867 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002868 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002869struct sk_buff *skb_segment(struct sk_buff *head_skb,
2870 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002871{
2872 struct sk_buff *segs = NULL;
2873 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002874 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002875 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2876 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2877 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002878 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002879 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002880 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002881 unsigned int headroom;
2882 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002883 __be16 proto;
2884 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002885 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002886 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002887 int err = -ENOMEM;
2888 int i = 0;
2889 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002890 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002891
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002892 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002893 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002894 if (unlikely(!proto))
2895 return ERR_PTR(-EINVAL);
2896
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002897 csum = !head_skb->encap_hdr_csum &&
2898 !!can_checksum_protocol(features, proto);
2899
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002900 headroom = skb_headroom(head_skb);
2901 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002902
2903 do {
2904 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002905 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002906 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002907 int size;
2908
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002909 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002910 if (len > mss)
2911 len = mss;
2912
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002913 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002914 if (hsize < 0)
2915 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002916 if (hsize > len || !sg)
2917 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002918
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002919 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2920 (skb_headlen(list_skb) == len || sg)) {
2921 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002922
Herbert Xu9d8506c2013-11-21 11:10:04 -08002923 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002924 nfrags = skb_shinfo(list_skb)->nr_frags;
2925 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002926 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002927 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002928
2929 while (pos < offset + len) {
2930 BUG_ON(i >= nfrags);
2931
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002932 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002933 if (pos + size > offset + len)
2934 break;
2935
2936 i++;
2937 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002938 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002939 }
2940
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002941 nskb = skb_clone(list_skb, GFP_ATOMIC);
2942 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002943
2944 if (unlikely(!nskb))
2945 goto err;
2946
Herbert Xu9d8506c2013-11-21 11:10:04 -08002947 if (unlikely(pskb_trim(nskb, len))) {
2948 kfree_skb(nskb);
2949 goto err;
2950 }
2951
Alexander Duyckec47ea82012-05-04 14:26:56 +00002952 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002953 if (skb_cow_head(nskb, doffset + headroom)) {
2954 kfree_skb(nskb);
2955 goto err;
2956 }
2957
Alexander Duyckec47ea82012-05-04 14:26:56 +00002958 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002959 skb_release_head_state(nskb);
2960 __skb_push(nskb, doffset);
2961 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002962 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002963 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07002964 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002965
2966 if (unlikely(!nskb))
2967 goto err;
2968
2969 skb_reserve(nskb, headroom);
2970 __skb_put(nskb, doffset);
2971 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002972
2973 if (segs)
2974 tail->next = nskb;
2975 else
2976 segs = nskb;
2977 tail = nskb;
2978
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002979 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002980
Eric Dumazet030737b2013-10-19 11:42:54 -07002981 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04002982 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00002983
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002984 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00002985 nskb->data - tnl_hlen,
2986 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08002987
Herbert Xu9d8506c2013-11-21 11:10:04 -08002988 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00002989 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08002990
Herbert Xuf4c50d92006-06-22 03:02:40 -07002991 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002992 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002993 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07002994 skb_put(nskb, len),
2995 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002996 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07002997 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002998 continue;
2999 }
3000
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003001 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003002
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003003 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003004 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003005
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003006 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3007 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003008
Herbert Xu9d8506c2013-11-21 11:10:04 -08003009 while (pos < offset + len) {
3010 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003011 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003012
3013 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003014 nfrags = skb_shinfo(list_skb)->nr_frags;
3015 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003016 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003017
3018 BUG_ON(!nfrags);
3019
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003020 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003021 }
3022
3023 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3024 MAX_SKB_FRAGS)) {
3025 net_warn_ratelimited(
3026 "skb_segment: too many frags: %u %u\n",
3027 pos, mss);
3028 goto err;
3029 }
3030
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003031 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3032 goto err;
3033
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003034 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003035 __skb_frag_ref(nskb_frag);
3036 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003037
3038 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003039 nskb_frag->page_offset += offset - pos;
3040 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003041 }
3042
Herbert Xu89319d382008-12-15 23:26:06 -08003043 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003044
3045 if (pos + size <= offset + len) {
3046 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003047 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003048 pos += size;
3049 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003050 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003051 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003052 }
3053
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003054 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003055 }
3056
Herbert Xu89319d382008-12-15 23:26:06 -08003057skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003058 nskb->data_len = len - hsize;
3059 nskb->len += nskb->data_len;
3060 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003061
Simon Horman1cdbcb72013-05-19 15:46:49 +00003062perform_csum_check:
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003063 if (!csum) {
3064 nskb->csum = skb_checksum(nskb, doffset,
3065 nskb->len - doffset, 0);
3066 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003067 SKB_GSO_CB(nskb)->csum_start =
3068 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003069 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003070 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003071
3072 return segs;
3073
3074err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003075 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003076 return ERR_PTR(err);
3077}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003078EXPORT_SYMBOL_GPL(skb_segment);
3079
Herbert Xu71d93b32008-12-15 23:42:33 -08003080int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3081{
Eric Dumazet8a291112013-10-08 09:02:23 -07003082 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003083 unsigned int offset = skb_gro_offset(skb);
3084 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003085 struct sk_buff *nskb, *lp, *p = *head;
3086 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003087 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003088 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003089
Eric Dumazet8a291112013-10-08 09:02:23 -07003090 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003091 return -E2BIG;
3092
Eric Dumazet29e98242014-05-16 11:34:37 -07003093 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003094 pinfo = skb_shinfo(lp);
3095
3096 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003097 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003098 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003099 int i = skbinfo->nr_frags;
3100 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003101
Herbert Xu66e92fc2009-05-26 18:50:32 +00003102 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003103 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003104
Eric Dumazet8a291112013-10-08 09:02:23 -07003105 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003106 pinfo->nr_frags = nr_frags;
3107 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003108
Herbert Xu9aaa1562009-05-26 18:50:33 +00003109 frag = pinfo->frags + nr_frags;
3110 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003111 do {
3112 *--frag = *--frag2;
3113 } while (--i);
3114
3115 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003116 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003117
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003118 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003119 delta_truesize = skb->truesize -
3120 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003121
Herbert Xuf5572062009-01-14 20:40:03 -08003122 skb->truesize -= skb->data_len;
3123 skb->len -= skb->data_len;
3124 skb->data_len = 0;
3125
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003126 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003127 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003128 } else if (skb->head_frag) {
3129 int nr_frags = pinfo->nr_frags;
3130 skb_frag_t *frag = pinfo->frags + nr_frags;
3131 struct page *page = virt_to_head_page(skb->head);
3132 unsigned int first_size = headlen - offset;
3133 unsigned int first_offset;
3134
3135 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003136 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003137
3138 first_offset = skb->data -
3139 (unsigned char *)page_address(page) +
3140 offset;
3141
3142 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3143
3144 frag->page.p = page;
3145 frag->page_offset = first_offset;
3146 skb_frag_size_set(frag, first_size);
3147
3148 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3149 /* We dont need to clear skbinfo->nr_frags here */
3150
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003151 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003152 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3153 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003154 }
3155 if (pinfo->frag_list)
3156 goto merge;
3157 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003158 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003159
3160 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003161 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003162 if (unlikely(!nskb))
3163 return -ENOMEM;
3164
3165 __copy_skb_header(nskb, p);
3166 nskb->mac_len = p->mac_len;
3167
3168 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003169 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003170
Herbert Xu86911732009-01-29 14:19:50 +00003171 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003172 skb_set_network_header(nskb, skb_network_offset(p));
3173 skb_set_transport_header(nskb, skb_transport_offset(p));
3174
Herbert Xu86911732009-01-29 14:19:50 +00003175 __skb_pull(p, skb_gro_offset(p));
3176 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3177 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003178
Herbert Xu71d93b32008-12-15 23:42:33 -08003179 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003180 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003181 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08003182 skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003183 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003184
3185 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003186 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003187 nskb->len += p->len;
3188
3189 *head = nskb;
3190 nskb->next = p->next;
3191 p->next = NULL;
3192
3193 p = nskb;
3194
3195merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003196 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003197 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003198 unsigned int eat = offset - headlen;
3199
3200 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003201 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003202 skb->data_len -= eat;
3203 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003204 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003205 }
3206
Herbert Xu67147ba2009-05-26 18:50:22 +00003207 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003208
Eric Dumazet29e98242014-05-16 11:34:37 -07003209 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003210 skb_shinfo(p)->frag_list = skb;
3211 else
3212 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003213 NAPI_GRO_CB(p)->last = skb;
Herbert Xu71d93b32008-12-15 23:42:33 -08003214 skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003215 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003216
Herbert Xu5d38a072009-01-04 16:13:40 -08003217done:
3218 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003219 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003220 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003221 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003222 if (lp != p) {
3223 lp->data_len += len;
3224 lp->truesize += delta_truesize;
3225 lp->len += len;
3226 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003227 NAPI_GRO_CB(skb)->same_flow = 1;
3228 return 0;
3229}
3230EXPORT_SYMBOL_GPL(skb_gro_receive);
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232void __init skb_init(void)
3233{
3234 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3235 sizeof(struct sk_buff),
3236 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003237 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003238 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003239 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3240 (2*sizeof(struct sk_buff)) +
3241 sizeof(atomic_t),
3242 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003243 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003244 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
David Howells716ea3a2007-04-02 20:19:53 -07003247/**
3248 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3249 * @skb: Socket buffer containing the buffers to be mapped
3250 * @sg: The scatter-gather list to map into
3251 * @offset: The offset into the buffer's contents to start mapping
3252 * @len: Length of buffer space to be mapped
3253 *
3254 * Fill the specified scatter-gather list with mappings/pointers into a
3255 * region of the buffer space attached to a socket buffer.
3256 */
David S. Miller51c739d2007-10-30 21:29:29 -07003257static int
3258__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003259{
David S. Miller1a028e52007-04-27 15:21:23 -07003260 int start = skb_headlen(skb);
3261 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003262 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003263 int elt = 0;
3264
3265 if (copy > 0) {
3266 if (copy > len)
3267 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003268 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003269 elt++;
3270 if ((len -= copy) == 0)
3271 return elt;
3272 offset += copy;
3273 }
3274
3275 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003276 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003277
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003278 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003279
Eric Dumazet9e903e02011-10-18 21:00:24 +00003280 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003281 if ((copy = end - offset) > 0) {
3282 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3283
3284 if (copy > len)
3285 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003286 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003287 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003288 elt++;
3289 if (!(len -= copy))
3290 return elt;
3291 offset += copy;
3292 }
David S. Miller1a028e52007-04-27 15:21:23 -07003293 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003294 }
3295
David S. Millerfbb398a2009-06-09 00:18:59 -07003296 skb_walk_frags(skb, frag_iter) {
3297 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003298
David S. Millerfbb398a2009-06-09 00:18:59 -07003299 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003300
David S. Millerfbb398a2009-06-09 00:18:59 -07003301 end = start + frag_iter->len;
3302 if ((copy = end - offset) > 0) {
3303 if (copy > len)
3304 copy = len;
3305 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3306 copy);
3307 if ((len -= copy) == 0)
3308 return elt;
3309 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003310 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003311 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003312 }
3313 BUG_ON(len);
3314 return elt;
3315}
3316
Fan Du25a91d82014-01-18 09:54:23 +08003317/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3318 * sglist without mark the sg which contain last skb data as the end.
3319 * So the caller can mannipulate sg list as will when padding new data after
3320 * the first call without calling sg_unmark_end to expend sg list.
3321 *
3322 * Scenario to use skb_to_sgvec_nomark:
3323 * 1. sg_init_table
3324 * 2. skb_to_sgvec_nomark(payload1)
3325 * 3. skb_to_sgvec_nomark(payload2)
3326 *
3327 * This is equivalent to:
3328 * 1. sg_init_table
3329 * 2. skb_to_sgvec(payload1)
3330 * 3. sg_unmark_end
3331 * 4. skb_to_sgvec(payload2)
3332 *
3333 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3334 * is more preferable.
3335 */
3336int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3337 int offset, int len)
3338{
3339 return __skb_to_sgvec(skb, sg, offset, len);
3340}
3341EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3342
David S. Miller51c739d2007-10-30 21:29:29 -07003343int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3344{
3345 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3346
Jens Axboec46f2332007-10-31 12:06:37 +01003347 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003348
3349 return nsg;
3350}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003351EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003352
David Howells716ea3a2007-04-02 20:19:53 -07003353/**
3354 * skb_cow_data - Check that a socket buffer's data buffers are writable
3355 * @skb: The socket buffer to check.
3356 * @tailbits: Amount of trailing space to be added
3357 * @trailer: Returned pointer to the skb where the @tailbits space begins
3358 *
3359 * Make sure that the data buffers attached to a socket buffer are
3360 * writable. If they are not, private copies are made of the data buffers
3361 * and the socket buffer is set to use these instead.
3362 *
3363 * If @tailbits is given, make sure that there is space to write @tailbits
3364 * bytes of data beyond current end of socket buffer. @trailer will be
3365 * set to point to the skb in which this space begins.
3366 *
3367 * The number of scatterlist elements required to completely map the
3368 * COW'd and extended socket buffer will be returned.
3369 */
3370int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3371{
3372 int copyflag;
3373 int elt;
3374 struct sk_buff *skb1, **skb_p;
3375
3376 /* If skb is cloned or its head is paged, reallocate
3377 * head pulling out all the pages (pages are considered not writable
3378 * at the moment even if they are anonymous).
3379 */
3380 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3381 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3382 return -ENOMEM;
3383
3384 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003385 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003386 /* A little of trouble, not enough of space for trailer.
3387 * This should not happen, when stack is tuned to generate
3388 * good frames. OK, on miss we reallocate and reserve even more
3389 * space, 128 bytes is fair. */
3390
3391 if (skb_tailroom(skb) < tailbits &&
3392 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3393 return -ENOMEM;
3394
3395 /* Voila! */
3396 *trailer = skb;
3397 return 1;
3398 }
3399
3400 /* Misery. We are in troubles, going to mincer fragments... */
3401
3402 elt = 1;
3403 skb_p = &skb_shinfo(skb)->frag_list;
3404 copyflag = 0;
3405
3406 while ((skb1 = *skb_p) != NULL) {
3407 int ntail = 0;
3408
3409 /* The fragment is partially pulled by someone,
3410 * this can happen on input. Copy it and everything
3411 * after it. */
3412
3413 if (skb_shared(skb1))
3414 copyflag = 1;
3415
3416 /* If the skb is the last, worry about trailer. */
3417
3418 if (skb1->next == NULL && tailbits) {
3419 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003420 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003421 skb_tailroom(skb1) < tailbits)
3422 ntail = tailbits + 128;
3423 }
3424
3425 if (copyflag ||
3426 skb_cloned(skb1) ||
3427 ntail ||
3428 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003429 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003430 struct sk_buff *skb2;
3431
3432 /* Fuck, we are miserable poor guys... */
3433 if (ntail == 0)
3434 skb2 = skb_copy(skb1, GFP_ATOMIC);
3435 else
3436 skb2 = skb_copy_expand(skb1,
3437 skb_headroom(skb1),
3438 ntail,
3439 GFP_ATOMIC);
3440 if (unlikely(skb2 == NULL))
3441 return -ENOMEM;
3442
3443 if (skb1->sk)
3444 skb_set_owner_w(skb2, skb1->sk);
3445
3446 /* Looking around. Are we still alive?
3447 * OK, link new skb, drop old one */
3448
3449 skb2->next = skb1->next;
3450 *skb_p = skb2;
3451 kfree_skb(skb1);
3452 skb1 = skb2;
3453 }
3454 elt++;
3455 *trailer = skb1;
3456 skb_p = &skb1->next;
3457 }
3458
3459 return elt;
3460}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003461EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003462
Eric Dumazetb1faf562010-05-31 23:44:05 -07003463static void sock_rmem_free(struct sk_buff *skb)
3464{
3465 struct sock *sk = skb->sk;
3466
3467 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3468}
3469
3470/*
3471 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3472 */
3473int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3474{
3475 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003476 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003477 return -ENOMEM;
3478
3479 skb_orphan(skb);
3480 skb->sk = sk;
3481 skb->destructor = sock_rmem_free;
3482 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3483
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003484 /* before exiting rcu section, make sure dst is refcounted */
3485 skb_dst_force(skb);
3486
Eric Dumazetb1faf562010-05-31 23:44:05 -07003487 skb_queue_tail(&sk->sk_error_queue, skb);
3488 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003489 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003490 return 0;
3491}
3492EXPORT_SYMBOL(sock_queue_err_skb);
3493
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003494void __skb_tstamp_tx(struct sk_buff *orig_skb,
3495 struct skb_shared_hwtstamps *hwtstamps,
3496 struct sock *sk, int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003497{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003498 struct sock_exterr_skb *serr;
3499 struct sk_buff *skb;
3500 int err;
3501
3502 if (!sk)
3503 return;
3504
Patrick Ohlyac45f602009-02-12 05:03:37 +00003505 if (hwtstamps) {
Willem de Bruijn2e313962013-04-23 00:39:28 +00003506 *skb_hwtstamps(orig_skb) =
Patrick Ohlyac45f602009-02-12 05:03:37 +00003507 *hwtstamps;
3508 } else {
3509 /*
3510 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003511 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003512 * store software time stamp
3513 */
Willem de Bruijn2e313962013-04-23 00:39:28 +00003514 orig_skb->tstamp = ktime_get_real();
Patrick Ohlyac45f602009-02-12 05:03:37 +00003515 }
3516
Willem de Bruijn2e313962013-04-23 00:39:28 +00003517 skb = skb_clone(orig_skb, GFP_ATOMIC);
3518 if (!skb)
3519 return;
3520
Patrick Ohlyac45f602009-02-12 05:03:37 +00003521 serr = SKB_EXT_ERR(skb);
3522 memset(serr, 0, sizeof(*serr));
3523 serr->ee.ee_errno = ENOMSG;
3524 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003525 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003526 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003527 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003528 if (sk->sk_protocol == IPPROTO_TCP)
3529 serr->ee.ee_data -= sk->sk_tskey;
3530 }
Eric Dumazet29030372010-05-29 00:20:48 -07003531
Patrick Ohlyac45f602009-02-12 05:03:37 +00003532 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003533
Patrick Ohlyac45f602009-02-12 05:03:37 +00003534 if (err)
3535 kfree_skb(skb);
3536}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003537EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3538
3539void skb_tstamp_tx(struct sk_buff *orig_skb,
3540 struct skb_shared_hwtstamps *hwtstamps)
3541{
3542 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3543 SCM_TSTAMP_SND);
3544}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003545EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3546
Johannes Berg6e3e9392011-11-09 10:15:42 +01003547void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3548{
3549 struct sock *sk = skb->sk;
3550 struct sock_exterr_skb *serr;
3551 int err;
3552
3553 skb->wifi_acked_valid = 1;
3554 skb->wifi_acked = acked;
3555
3556 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_TXSTATUS;
3560
3561 err = sock_queue_err_skb(sk, skb);
3562 if (err)
3563 kfree_skb(skb);
3564}
3565EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3566
Patrick Ohlyac45f602009-02-12 05:03:37 +00003567
Rusty Russellf35d9d82008-02-04 23:49:54 -05003568/**
3569 * skb_partial_csum_set - set up and verify partial csum values for packet
3570 * @skb: the skb to set
3571 * @start: the number of bytes after skb->data to start checksumming.
3572 * @off: the offset from start to place the checksum.
3573 *
3574 * For untrusted partially-checksummed packets, we need to make sure the values
3575 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3576 *
3577 * This function checks and sets those values and skb->ip_summed: if this
3578 * returns false you should drop the packet.
3579 */
3580bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3581{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003582 if (unlikely(start > skb_headlen(skb)) ||
3583 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003584 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3585 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003586 return false;
3587 }
3588 skb->ip_summed = CHECKSUM_PARTIAL;
3589 skb->csum_start = skb_headroom(skb) + start;
3590 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003591 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003592 return true;
3593}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003594EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003595
Paul Durranted1f50c2014-01-09 10:02:46 +00003596static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3597 unsigned int max)
3598{
3599 if (skb_headlen(skb) >= len)
3600 return 0;
3601
3602 /* If we need to pullup then pullup to the max, so we
3603 * won't need to do it again.
3604 */
3605 if (max > skb->len)
3606 max = skb->len;
3607
3608 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3609 return -ENOMEM;
3610
3611 if (skb_headlen(skb) < len)
3612 return -EPROTO;
3613
3614 return 0;
3615}
3616
Jan Beulichf9708b42014-03-11 13:56:05 +00003617#define MAX_TCP_HDR_LEN (15 * 4)
3618
3619static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3620 typeof(IPPROTO_IP) proto,
3621 unsigned int off)
3622{
3623 switch (proto) {
3624 int err;
3625
3626 case IPPROTO_TCP:
3627 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3628 off + MAX_TCP_HDR_LEN);
3629 if (!err && !skb_partial_csum_set(skb, off,
3630 offsetof(struct tcphdr,
3631 check)))
3632 err = -EPROTO;
3633 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3634
3635 case IPPROTO_UDP:
3636 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3637 off + sizeof(struct udphdr));
3638 if (!err && !skb_partial_csum_set(skb, off,
3639 offsetof(struct udphdr,
3640 check)))
3641 err = -EPROTO;
3642 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3643 }
3644
3645 return ERR_PTR(-EPROTO);
3646}
3647
Paul Durranted1f50c2014-01-09 10:02:46 +00003648/* This value should be large enough to cover a tagged ethernet header plus
3649 * maximally sized IP and TCP or UDP headers.
3650 */
3651#define MAX_IP_HDR_LEN 128
3652
Jan Beulichf9708b42014-03-11 13:56:05 +00003653static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003654{
3655 unsigned int off;
3656 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003657 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003658 int err;
3659
3660 fragment = false;
3661
3662 err = skb_maybe_pull_tail(skb,
3663 sizeof(struct iphdr),
3664 MAX_IP_HDR_LEN);
3665 if (err < 0)
3666 goto out;
3667
3668 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3669 fragment = true;
3670
3671 off = ip_hdrlen(skb);
3672
3673 err = -EPROTO;
3674
3675 if (fragment)
3676 goto out;
3677
Jan Beulichf9708b42014-03-11 13:56:05 +00003678 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3679 if (IS_ERR(csum))
3680 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003681
Jan Beulichf9708b42014-03-11 13:56:05 +00003682 if (recalculate)
3683 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3684 ip_hdr(skb)->daddr,
3685 skb->len - off,
3686 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003687 err = 0;
3688
3689out:
3690 return err;
3691}
3692
3693/* This value should be large enough to cover a tagged ethernet header plus
3694 * an IPv6 header, all options, and a maximal TCP or UDP header.
3695 */
3696#define MAX_IPV6_HDR_LEN 256
3697
3698#define OPT_HDR(type, skb, off) \
3699 (type *)(skb_network_header(skb) + (off))
3700
3701static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3702{
3703 int err;
3704 u8 nexthdr;
3705 unsigned int off;
3706 unsigned int len;
3707 bool fragment;
3708 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003709 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003710
3711 fragment = false;
3712 done = false;
3713
3714 off = sizeof(struct ipv6hdr);
3715
3716 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3717 if (err < 0)
3718 goto out;
3719
3720 nexthdr = ipv6_hdr(skb)->nexthdr;
3721
3722 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3723 while (off <= len && !done) {
3724 switch (nexthdr) {
3725 case IPPROTO_DSTOPTS:
3726 case IPPROTO_HOPOPTS:
3727 case IPPROTO_ROUTING: {
3728 struct ipv6_opt_hdr *hp;
3729
3730 err = skb_maybe_pull_tail(skb,
3731 off +
3732 sizeof(struct ipv6_opt_hdr),
3733 MAX_IPV6_HDR_LEN);
3734 if (err < 0)
3735 goto out;
3736
3737 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3738 nexthdr = hp->nexthdr;
3739 off += ipv6_optlen(hp);
3740 break;
3741 }
3742 case IPPROTO_AH: {
3743 struct ip_auth_hdr *hp;
3744
3745 err = skb_maybe_pull_tail(skb,
3746 off +
3747 sizeof(struct ip_auth_hdr),
3748 MAX_IPV6_HDR_LEN);
3749 if (err < 0)
3750 goto out;
3751
3752 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3753 nexthdr = hp->nexthdr;
3754 off += ipv6_authlen(hp);
3755 break;
3756 }
3757 case IPPROTO_FRAGMENT: {
3758 struct frag_hdr *hp;
3759
3760 err = skb_maybe_pull_tail(skb,
3761 off +
3762 sizeof(struct frag_hdr),
3763 MAX_IPV6_HDR_LEN);
3764 if (err < 0)
3765 goto out;
3766
3767 hp = OPT_HDR(struct frag_hdr, skb, off);
3768
3769 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3770 fragment = true;
3771
3772 nexthdr = hp->nexthdr;
3773 off += sizeof(struct frag_hdr);
3774 break;
3775 }
3776 default:
3777 done = true;
3778 break;
3779 }
3780 }
3781
3782 err = -EPROTO;
3783
3784 if (!done || fragment)
3785 goto out;
3786
Jan Beulichf9708b42014-03-11 13:56:05 +00003787 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3788 if (IS_ERR(csum))
3789 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003790
Jan Beulichf9708b42014-03-11 13:56:05 +00003791 if (recalculate)
3792 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3793 &ipv6_hdr(skb)->daddr,
3794 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003795 err = 0;
3796
3797out:
3798 return err;
3799}
3800
3801/**
3802 * skb_checksum_setup - set up partial checksum offset
3803 * @skb: the skb to set up
3804 * @recalculate: if true the pseudo-header checksum will be recalculated
3805 */
3806int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3807{
3808 int err;
3809
3810 switch (skb->protocol) {
3811 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003812 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003813 break;
3814
3815 case htons(ETH_P_IPV6):
3816 err = skb_checksum_setup_ipv6(skb, recalculate);
3817 break;
3818
3819 default:
3820 err = -EPROTO;
3821 break;
3822 }
3823
3824 return err;
3825}
3826EXPORT_SYMBOL(skb_checksum_setup);
3827
Ben Hutchings4497b072008-06-19 16:22:28 -07003828void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3829{
Joe Perchese87cc472012-05-13 21:56:26 +00003830 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3831 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003832}
Ben Hutchings4497b072008-06-19 16:22:28 -07003833EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003834
3835void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3836{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003837 if (head_stolen) {
3838 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003839 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003840 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003841 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003842 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003843}
3844EXPORT_SYMBOL(kfree_skb_partial);
3845
3846/**
3847 * skb_try_coalesce - try to merge skb to prior one
3848 * @to: prior buffer
3849 * @from: buffer to add
3850 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003851 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003852 */
3853bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3854 bool *fragstolen, int *delta_truesize)
3855{
3856 int i, delta, len = from->len;
3857
3858 *fragstolen = false;
3859
3860 if (skb_cloned(to))
3861 return false;
3862
3863 if (len <= skb_tailroom(to)) {
3864 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3865 *delta_truesize = 0;
3866 return true;
3867 }
3868
3869 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3870 return false;
3871
3872 if (skb_headlen(from) != 0) {
3873 struct page *page;
3874 unsigned int offset;
3875
3876 if (skb_shinfo(to)->nr_frags +
3877 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3878 return false;
3879
3880 if (skb_head_is_locked(from))
3881 return false;
3882
3883 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3884
3885 page = virt_to_head_page(from->head);
3886 offset = from->data - (unsigned char *)page_address(page);
3887
3888 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3889 page, offset, skb_headlen(from));
3890 *fragstolen = true;
3891 } else {
3892 if (skb_shinfo(to)->nr_frags +
3893 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3894 return false;
3895
Weiping Panf4b549a2012-09-28 20:15:30 +00003896 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003897 }
3898
3899 WARN_ON_ONCE(delta < len);
3900
3901 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3902 skb_shinfo(from)->frags,
3903 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3904 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3905
3906 if (!skb_cloned(from))
3907 skb_shinfo(from)->nr_frags = 0;
3908
Li RongQing8ea853f2012-09-18 16:53:21 +00003909 /* if the skb is not cloned this does nothing
3910 * since we set nr_frags to 0.
3911 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003912 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3913 skb_frag_ref(from, i);
3914
3915 to->truesize += delta;
3916 to->len += len;
3917 to->data_len += len;
3918
3919 *delta_truesize = delta;
3920 return true;
3921}
3922EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003923
3924/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02003925 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003926 *
3927 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02003928 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003929 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02003930 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
3931 * into/from a tunnel. Some information have to be cleared during these
3932 * operations.
3933 * skb_scrub_packet can also be used to clean a skb before injecting it in
3934 * another namespace (@xnet == true). We have to clear all information in the
3935 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003936 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02003937void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003938{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02003939 if (xnet)
3940 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003941 skb->tstamp.tv64 = 0;
3942 skb->pkt_type = PACKET_HOST;
3943 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07003944 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02003945 skb_dst_drop(skb);
3946 skb->mark = 0;
3947 secpath_reset(skb);
3948 nf_reset(skb);
3949 nf_reset_trace(skb);
3950}
3951EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01003952
3953/**
3954 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3955 *
3956 * @skb: GSO skb
3957 *
3958 * skb_gso_transport_seglen is used to determine the real size of the
3959 * individual segments, including Layer4 headers (TCP/UDP).
3960 *
3961 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3962 */
3963unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3964{
3965 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalde960aa2014-01-26 10:58:16 +01003966
3967 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
Florian Westphal6d39d582014-04-09 10:28:50 +02003968 return tcp_hdrlen(skb) + shinfo->gso_size;
3969
3970 /* UFO sets gso_size to the size of the fragmentation
3971 * payload, i.e. the size of the L4 (UDP) header is already
3972 * accounted for.
3973 */
3974 return shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01003975}
3976EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04003977
3978static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
3979{
3980 if (skb_cow(skb, skb_headroom(skb)) < 0) {
3981 kfree_skb(skb);
3982 return NULL;
3983 }
3984
3985 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
3986 skb->mac_header += VLAN_HLEN;
3987 return skb;
3988}
3989
3990struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
3991{
3992 struct vlan_hdr *vhdr;
3993 u16 vlan_tci;
3994
3995 if (unlikely(vlan_tx_tag_present(skb))) {
3996 /* vlan_tci is already set-up so leave this for another time */
3997 return skb;
3998 }
3999
4000 skb = skb_share_check(skb, GFP_ATOMIC);
4001 if (unlikely(!skb))
4002 goto err_free;
4003
4004 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4005 goto err_free;
4006
4007 vhdr = (struct vlan_hdr *)skb->data;
4008 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4009 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4010
4011 skb_pull_rcsum(skb, VLAN_HLEN);
4012 vlan_set_encap_proto(skb, vhdr);
4013
4014 skb = skb_reorder_vlan_header(skb);
4015 if (unlikely(!skb))
4016 goto err_free;
4017
4018 skb_reset_network_header(skb);
4019 skb_reset_transport_header(skb);
4020 skb_reset_mac_len(skb);
4021
4022 return skb;
4023
4024err_free:
4025 kfree_skb(skb);
4026 return NULL;
4027}
4028EXPORT_SYMBOL(skb_vlan_untag);