blob: 56ed17cd2151e754abaaf42005bda4319a8783f2 [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) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700260 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700262 fclones = container_of(skb, struct sk_buff_fclones, skb1);
263
264 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700265 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700266 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700267
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800268 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700269 fclones->skb2.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);
Alexander Duyckffde7322014-12-09 19:40:42 -0800339static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000340
Alexander Duyckffde7322014-12-09 19:40:42 -0800341static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
342 gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000343{
Alexander Duyckffde7322014-12-09 19:40:42 -0800344 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
345 struct page *page = NULL;
346 gfp_t gfp = gfp_mask;
Eric Dumazet6f532612012-05-18 05:12:12 +0000347
Alexander Duyckffde7322014-12-09 19:40:42 -0800348 if (order) {
349 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
350 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
351 nc->frag.size = PAGE_SIZE << (page ? order : 0);
352 }
353
354 if (unlikely(!page))
355 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
356
357 nc->frag.page = page;
358
359 return page;
360}
361
362static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
363 unsigned int fragsz, gfp_t gfp_mask)
364{
365 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
366 struct page *page = nc->frag.page;
367 unsigned int size;
368 int offset;
369
370 if (unlikely(!page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000371refill:
Alexander Duyckffde7322014-12-09 19:40:42 -0800372 page = __page_frag_refill(nc, gfp_mask);
373 if (!page)
374 return NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000375
Alexander Duyckffde7322014-12-09 19:40:42 -0800376 /* if size can vary use frag.size else just use PAGE_SIZE */
377 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
378
Eric Dumazet4c450582014-10-10 04:48:18 -0700379 /* Even if we own the page, we do not use atomic_set().
380 * This would break get_page_unless_zero() users.
381 */
Alexander Duyckffde7322014-12-09 19:40:42 -0800382 atomic_add(size - 1, &page->_count);
383
384 /* reset page count bias and offset to start of new frag */
385 nc->pagecnt_bias = size;
386 nc->frag.offset = size;
Eric Dumazet6f532612012-05-18 05:12:12 +0000387 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000388
Alexander Duyckffde7322014-12-09 19:40:42 -0800389 offset = nc->frag.offset - fragsz;
390 if (unlikely(offset < 0)) {
391 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
392 goto refill;
393
394 /* if size can vary use frag.size else just use PAGE_SIZE */
395 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
396
397 /* OK, page count is 0, we can safely set it */
398 atomic_set(&page->_count, size);
399
400 /* reset page count bias and offset to start of new frag */
401 nc->pagecnt_bias = size;
402 offset = size - fragsz;
Eric Dumazet6f532612012-05-18 05:12:12 +0000403 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000404
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000405 nc->pagecnt_bias--;
Alexander Duyckffde7322014-12-09 19:40:42 -0800406 nc->frag.offset = offset;
407
408 return page_address(page) + offset;
409}
410
411static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
412{
413 unsigned long flags;
414 void *data;
415
416 local_irq_save(flags);
417 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000418 local_irq_restore(flags);
419 return data;
420}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700421
422/**
423 * netdev_alloc_frag - allocate a page fragment
424 * @fragsz: fragment size
425 *
426 * Allocates a frag from a page for receive buffer.
427 * Uses GFP_ATOMIC allocations.
428 */
429void *netdev_alloc_frag(unsigned int fragsz)
430{
431 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
432}
Eric Dumazet6f532612012-05-18 05:12:12 +0000433EXPORT_SYMBOL(netdev_alloc_frag);
434
Alexander Duyckffde7322014-12-09 19:40:42 -0800435static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
436{
437 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
438}
439
440void *napi_alloc_frag(unsigned int fragsz)
441{
442 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
443}
444EXPORT_SYMBOL(napi_alloc_frag);
445
Eric Dumazet6f532612012-05-18 05:12:12 +0000446/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700447 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
448 * @dev: network device to receive on
449 * @length: length to allocate
450 * @gfp_mask: get_free_pages mask, passed to alloc_skb
451 *
452 * Allocate a new &sk_buff and assign it a usage count of one. The
453 * buffer has unspecified headroom built in. Users should allocate
454 * the headroom they think they need without accounting for the
455 * built in space. The built in space is used for optimisations.
456 *
457 * %NULL is returned if there is no free memory.
458 */
459struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000460 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700461{
Eric Dumazet6f532612012-05-18 05:12:12 +0000462 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000463 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
464 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700465
Eric Dumazet310e1582012-07-16 13:15:52 +0200466 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700467 void *data;
468
469 if (sk_memalloc_socks())
470 gfp_mask |= __GFP_MEMALLOC;
471
472 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000473
Eric Dumazet6f532612012-05-18 05:12:12 +0000474 if (likely(data)) {
475 skb = build_skb(data, fragsz);
476 if (unlikely(!skb))
477 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000478 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000479 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700480 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
481 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000482 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700483 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700484 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700485 skb->dev = dev;
486 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700487 return skb;
488}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800489EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Peter Zijlstra654bed12008-10-07 14:22:33 -0700491void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000492 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700493{
494 skb_fill_page_desc(skb, i, page, off, size);
495 skb->len += size;
496 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000497 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700498}
499EXPORT_SYMBOL(skb_add_rx_frag);
500
Jason Wangf8e617e2013-11-01 14:07:47 +0800501void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
502 unsigned int truesize)
503{
504 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
505
506 skb_frag_size_add(frag, size);
507 skb->len += size;
508 skb->data_len += size;
509 skb->truesize += truesize;
510}
511EXPORT_SYMBOL(skb_coalesce_rx_frag);
512
Herbert Xu27b437c2006-07-13 19:26:39 -0700513static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700515 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700516 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Herbert Xu27b437c2006-07-13 19:26:39 -0700519static inline void skb_drop_fraglist(struct sk_buff *skb)
520{
521 skb_drop_list(&skb_shinfo(skb)->frag_list);
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524static void skb_clone_fraglist(struct sk_buff *skb)
525{
526 struct sk_buff *list;
527
David S. Millerfbb398a2009-06-09 00:18:59 -0700528 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 skb_get(list);
530}
531
Eric Dumazetd3836f22012-04-27 00:33:38 +0000532static void skb_free_head(struct sk_buff *skb)
533{
534 if (skb->head_frag)
535 put_page(virt_to_head_page(skb->head));
536 else
537 kfree(skb->head);
538}
539
Adrian Bunk5bba1712006-06-29 13:02:35 -0700540static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Eric Dumazetff04a772014-09-23 18:39:30 -0700542 struct skb_shared_info *shinfo = skb_shinfo(skb);
543 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Eric Dumazetff04a772014-09-23 18:39:30 -0700545 if (skb->cloned &&
546 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
547 &shinfo->dataref))
548 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000549
Eric Dumazetff04a772014-09-23 18:39:30 -0700550 for (i = 0; i < shinfo->nr_frags; i++)
551 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000552
Eric Dumazetff04a772014-09-23 18:39:30 -0700553 /*
554 * If skb buf is from userspace, we need to notify the caller
555 * the lower device DMA has done;
556 */
557 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
558 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Eric Dumazetff04a772014-09-23 18:39:30 -0700560 uarg = shinfo->destructor_arg;
561 if (uarg->callback)
562 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700564
565 if (shinfo->frag_list)
566 kfree_skb_list(shinfo->frag_list);
567
568 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
572 * Free an skbuff by memory without cleaning the state.
573 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800574static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700576 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700577
David S. Millerd179cd12005-08-17 14:57:30 -0700578 switch (skb->fclone) {
579 case SKB_FCLONE_UNAVAILABLE:
580 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800581 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700582
583 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700584 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800585
586 /* We usually free the clone (TX completion) before original skb
587 * This test would have no chance to be true for the clone,
588 * while here, branch prediction will be good.
589 */
590 if (atomic_read(&fclones->fclone_ref) == 1)
591 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700592 break;
593
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800594 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700595 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700596 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700597 }
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800598 if (!atomic_dec_and_test(&fclones->fclone_ref))
599 return;
600fastpath:
601 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700604static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Eric Dumazetadf30902009-06-02 05:19:30 +0000606 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#ifdef CONFIG_XFRM
608 secpath_put(skb->sp);
609#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700610 if (skb->destructor) {
611 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 skb->destructor(skb);
613 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000614#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700615 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100616#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200617#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 nf_bridge_put(skb->nf_bridge);
619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/* XXX: IS this still necessary? - JHS */
621#ifdef CONFIG_NET_SCHED
622 skb->tc_index = 0;
623#ifdef CONFIG_NET_CLS_ACT
624 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif
626#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700627}
628
629/* Free everything but the sk_buff shell. */
630static void skb_release_all(struct sk_buff *skb)
631{
632 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000633 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000634 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Herbert Xu2d4baff2007-11-26 23:11:19 +0800637/**
638 * __kfree_skb - private function
639 * @skb: buffer
640 *
641 * Free an sk_buff. Release anything attached to the buffer.
642 * Clean the state. This is an internal helper function. Users should
643 * always call kfree_skb
644 */
645
646void __kfree_skb(struct sk_buff *skb)
647{
648 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 kfree_skbmem(skb);
650}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800651EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800654 * kfree_skb - free an sk_buff
655 * @skb: buffer to free
656 *
657 * Drop a reference to the buffer and free it if the usage count has
658 * hit zero.
659 */
660void kfree_skb(struct sk_buff *skb)
661{
662 if (unlikely(!skb))
663 return;
664 if (likely(atomic_read(&skb->users) == 1))
665 smp_rmb();
666 else if (likely(!atomic_dec_and_test(&skb->users)))
667 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000668 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800669 __kfree_skb(skb);
670}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800671EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800672
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700673void kfree_skb_list(struct sk_buff *segs)
674{
675 while (segs) {
676 struct sk_buff *next = segs->next;
677
678 kfree_skb(segs);
679 segs = next;
680 }
681}
682EXPORT_SYMBOL(kfree_skb_list);
683
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700684/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000685 * skb_tx_error - report an sk_buff xmit error
686 * @skb: buffer that triggered an error
687 *
688 * Report xmit error if a device callback is tracking this skb.
689 * skb must be freed afterwards.
690 */
691void skb_tx_error(struct sk_buff *skb)
692{
693 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
694 struct ubuf_info *uarg;
695
696 uarg = skb_shinfo(skb)->destructor_arg;
697 if (uarg->callback)
698 uarg->callback(uarg, false);
699 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
700 }
701}
702EXPORT_SYMBOL(skb_tx_error);
703
704/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000705 * consume_skb - free an skbuff
706 * @skb: buffer to free
707 *
708 * Drop a ref to the buffer and free it if the usage count has hit zero
709 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
710 * is being dropped after a failure and notes that
711 */
712void consume_skb(struct sk_buff *skb)
713{
714 if (unlikely(!skb))
715 return;
716 if (likely(atomic_read(&skb->users) == 1))
717 smp_rmb();
718 else if (likely(!atomic_dec_and_test(&skb->users)))
719 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900720 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000721 __kfree_skb(skb);
722}
723EXPORT_SYMBOL(consume_skb);
724
Eric Dumazetb1937222014-09-28 22:18:47 -0700725/* Make sure a field is enclosed inside headers_start/headers_end section */
726#define CHECK_SKB_FIELD(field) \
727 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
728 offsetof(struct sk_buff, headers_start)); \
729 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
730 offsetof(struct sk_buff, headers_end)); \
731
Herbert Xudec18812007-10-14 00:37:30 -0700732static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
733{
734 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700735 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700736 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700737 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000738 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700739#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700740 new->sp = secpath_get(old->sp);
741#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700742 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700743
Eric Dumazetb1937222014-09-28 22:18:47 -0700744 /* Note : this field could be in headers_start/headers_end section
745 * It is not yet because we do not want to have a 16 bit hole
746 */
747 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300748
Eric Dumazetb1937222014-09-28 22:18:47 -0700749 memcpy(&new->headers_start, &old->headers_start,
750 offsetof(struct sk_buff, headers_end) -
751 offsetof(struct sk_buff, headers_start));
752 CHECK_SKB_FIELD(protocol);
753 CHECK_SKB_FIELD(csum);
754 CHECK_SKB_FIELD(hash);
755 CHECK_SKB_FIELD(priority);
756 CHECK_SKB_FIELD(skb_iif);
757 CHECK_SKB_FIELD(vlan_proto);
758 CHECK_SKB_FIELD(vlan_tci);
759 CHECK_SKB_FIELD(transport_header);
760 CHECK_SKB_FIELD(network_header);
761 CHECK_SKB_FIELD(mac_header);
762 CHECK_SKB_FIELD(inner_protocol);
763 CHECK_SKB_FIELD(inner_transport_header);
764 CHECK_SKB_FIELD(inner_network_header);
765 CHECK_SKB_FIELD(inner_mac_header);
766 CHECK_SKB_FIELD(mark);
767#ifdef CONFIG_NETWORK_SECMARK
768 CHECK_SKB_FIELD(secmark);
769#endif
Cong Wange0d10952013-08-01 11:10:25 +0800770#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700771 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300772#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700773#ifdef CONFIG_NET_SCHED
774 CHECK_SKB_FIELD(tc_index);
775#ifdef CONFIG_NET_CLS_ACT
776 CHECK_SKB_FIELD(tc_verd);
777#endif
778#endif
779
Herbert Xudec18812007-10-14 00:37:30 -0700780}
781
Herbert Xu82c49a32009-05-22 22:11:37 +0000782/*
783 * You should not add any new code to this function. Add it to
784 * __copy_skb_header above instead.
785 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700786static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#define C(x) n->x = skb->x
789
790 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700792 __copy_skb_header(n, skb);
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 C(len);
795 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700796 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700797 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800798 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 C(tail);
802 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800803 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000804 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800805 C(data);
806 C(truesize);
807 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 atomic_inc(&(skb_shinfo(skb)->dataref));
810 skb->cloned = 1;
811
812 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700813#undef C
814}
815
816/**
817 * skb_morph - morph one skb into another
818 * @dst: the skb to receive the contents
819 * @src: the skb to supply the contents
820 *
821 * This is identical to skb_clone except that the target skb is
822 * supplied by the user.
823 *
824 * The target skb is returned upon exit.
825 */
826struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
827{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800828 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700829 return __skb_clone(dst, src);
830}
831EXPORT_SYMBOL_GPL(skb_morph);
832
Ben Hutchings2c530402012-07-10 10:55:09 +0000833/**
834 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000835 * @skb: the skb to modify
836 * @gfp_mask: allocation priority
837 *
838 * This must be called on SKBTX_DEV_ZEROCOPY skb.
839 * It will copy all frags into kernel and drop the reference
840 * to userspace pages.
841 *
842 * If this function is called from an interrupt gfp_mask() must be
843 * %GFP_ATOMIC.
844 *
845 * Returns 0 on success or a negative error code on failure
846 * to allocate kernel memory to copy to.
847 */
848int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000849{
850 int i;
851 int num_frags = skb_shinfo(skb)->nr_frags;
852 struct page *page, *head = NULL;
853 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
854
855 for (i = 0; i < num_frags; i++) {
856 u8 *vaddr;
857 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
858
Krishna Kumar02756ed2012-07-17 02:05:29 +0000859 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000860 if (!page) {
861 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900862 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000863 put_page(head);
864 head = next;
865 }
866 return -ENOMEM;
867 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200868 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000869 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000870 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200871 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900872 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000873 head = page;
874 }
875
876 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000877 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000878 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000879
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000880 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000881
882 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000883 for (i = num_frags - 1; i >= 0; i--) {
884 __skb_fill_page_desc(skb, i, head, 0,
885 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900886 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000887 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000888
889 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000890 return 0;
891}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000892EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000893
Herbert Xue0053ec2007-10-14 00:37:52 -0700894/**
895 * skb_clone - duplicate an sk_buff
896 * @skb: buffer to clone
897 * @gfp_mask: allocation priority
898 *
899 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
900 * copies share the same packet data but not structure. The new
901 * buffer has a reference count of 1. If the allocation fails the
902 * function returns %NULL otherwise the new buffer is returned.
903 *
904 * If this function is called from an interrupt gfp_mask() must be
905 * %GFP_ATOMIC.
906 */
907
908struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
909{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700910 struct sk_buff_fclones *fclones = container_of(skb,
911 struct sk_buff_fclones,
912 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800913 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700914
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000915 if (skb_orphan_frags(skb, gfp_mask))
916 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000917
Herbert Xue0053ec2007-10-14 00:37:52 -0700918 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800919 atomic_read(&fclones->fclone_ref) == 1) {
920 n = &fclones->skb2;
921 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700922 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700923 if (skb_pfmemalloc(skb))
924 gfp_mask |= __GFP_MEMALLOC;
925
Herbert Xue0053ec2007-10-14 00:37:52 -0700926 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
927 if (!n)
928 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200929
930 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700931 n->fclone = SKB_FCLONE_UNAVAILABLE;
932 }
933
934 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800936EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000938static void skb_headers_offset_update(struct sk_buff *skb, int off)
939{
Eric Dumazet030737b2013-10-19 11:42:54 -0700940 /* Only adjust this if it actually is csum_start rather than csum */
941 if (skb->ip_summed == CHECKSUM_PARTIAL)
942 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000943 /* {transport,network,mac}_header and tail are relative to skb->head */
944 skb->transport_header += off;
945 skb->network_header += off;
946 if (skb_mac_header_was_set(skb))
947 skb->mac_header += off;
948 skb->inner_transport_header += off;
949 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000950 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
954{
Herbert Xudec18812007-10-14 00:37:30 -0700955 __copy_skb_header(new, old);
956
Herbert Xu79671682006-06-22 02:40:14 -0700957 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
958 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
959 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Mel Gormanc93bdd02012-07-31 16:44:19 -0700962static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
963{
964 if (skb_pfmemalloc(skb))
965 return SKB_ALLOC_RX;
966 return 0;
967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/**
970 * skb_copy - create private copy of an sk_buff
971 * @skb: buffer to copy
972 * @gfp_mask: allocation priority
973 *
974 * Make a copy of both an &sk_buff and its data. This is used when the
975 * caller wishes to modify the data and needs a private copy of the
976 * data to alter. Returns %NULL on failure or the pointer to the buffer
977 * on success. The returned buffer has a reference count of 1.
978 *
979 * As by-product this function converts non-linear &sk_buff to linear
980 * one, so that &sk_buff becomes completely private and caller is allowed
981 * to modify all the data of returned buffer. This means that this
982 * function is not recommended for use in circumstances when only
983 * header is going to be modified. Use pskb_copy() instead.
984 */
985
Al Virodd0fc662005-10-07 07:46:04 +0100986struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000988 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000989 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700990 struct sk_buff *n = __alloc_skb(size, gfp_mask,
991 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!n)
994 return NULL;
995
996 /* Set the data pointer */
997 skb_reserve(n, headerlen);
998 /* Set the tail pointer and length */
999 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1002 BUG();
1003
1004 copy_skb_header(n, skb);
1005 return n;
1006}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001007EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001010 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001012 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001014 * @fclone: if true allocate the copy of the skb from the fclone
1015 * cache instead of the head cache; it is recommended to set this
1016 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 *
1018 * Make a copy of both an &sk_buff and part of its data, located
1019 * in header. Fragmented data remain shared. This is used when
1020 * the caller wishes to modify only header of &sk_buff and needs
1021 * private copy of the header to alter. Returns %NULL on failure
1022 * or the pointer to the buffer on success.
1023 * The returned buffer has a reference count of 1.
1024 */
1025
Octavian Purdilabad93e92014-06-12 01:36:26 +03001026struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1027 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Eric Dumazet117632e2011-12-03 21:39:53 +00001029 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001030 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1031 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!n)
1034 goto out;
1035
1036 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001037 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Set the tail pointer and length */
1039 skb_put(n, skb_headlen(skb));
1040 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001041 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Herbert Xu25f484a2006-11-07 14:57:15 -08001043 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 n->data_len = skb->data_len;
1045 n->len = skb->len;
1046
1047 if (skb_shinfo(skb)->nr_frags) {
1048 int i;
1049
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001050 if (skb_orphan_frags(skb, gfp_mask)) {
1051 kfree_skb(n);
1052 n = NULL;
1053 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1056 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001057 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 skb_shinfo(n)->nr_frags = i;
1060 }
1061
David S. Miller21dc3302010-08-23 00:13:46 -07001062 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1064 skb_clone_fraglist(n);
1065 }
1066
1067 copy_skb_header(n, skb);
1068out:
1069 return n;
1070}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001071EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073/**
1074 * pskb_expand_head - reallocate header of &sk_buff
1075 * @skb: buffer to reallocate
1076 * @nhead: room to add at head
1077 * @ntail: room to add at tail
1078 * @gfp_mask: allocation priority
1079 *
Mathias Krausebc323832013-11-07 14:18:26 +01001080 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1081 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * reference count of 1. Returns zero in the case of success or error,
1083 * if expansion failed. In the last case, &sk_buff is not changed.
1084 *
1085 * All the pointers pointing into skb header may change and must be
1086 * reloaded after call to this function.
1087 */
1088
Victor Fusco86a76ca2005-07-08 14:57:47 -07001089int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001090 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 int i;
1093 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001094 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 long off;
1096
Herbert Xu4edd87a2008-10-01 07:09:38 -07001097 BUG_ON(nhead < 0);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (skb_shared(skb))
1100 BUG();
1101
1102 size = SKB_DATA_ALIGN(size);
1103
Mel Gormanc93bdd02012-07-31 16:44:19 -07001104 if (skb_pfmemalloc(skb))
1105 gfp_mask |= __GFP_MEMALLOC;
1106 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1107 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!data)
1109 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001110 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001113 * optimized for the cases when header is void.
1114 */
1115 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1116
1117 memcpy((struct skb_shared_info *)(data + size),
1118 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001119 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Alexander Duyck3e245912012-05-04 14:26:51 +00001121 /*
1122 * if shinfo is shared we must drop the old head gracefully, but if it
1123 * is not we can just drop the old head and let the existing refcount
1124 * be since all we did is relocate the values
1125 */
1126 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001127 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001128 if (skb_orphan_frags(skb, gfp_mask))
1129 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001130 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001131 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Eric Dumazet1fd63042010-09-02 23:09:32 +00001133 if (skb_has_frag_list(skb))
1134 skb_clone_fraglist(skb);
1135
1136 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001137 } else {
1138 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 off = (data + nhead) - skb->head;
1141
1142 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001143 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001145#ifdef NET_SKBUFF_DATA_USES_OFFSET
1146 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001147 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001148#else
1149 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001150#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001151 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001152 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001154 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 skb->nohdr = 0;
1156 atomic_set(&skb_shinfo(skb)->dataref, 1);
1157 return 0;
1158
Shirley Maa6686f22011-07-06 12:22:12 +00001159nofrags:
1160 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161nodata:
1162 return -ENOMEM;
1163}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001164EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166/* Make private copy of skb with writable head and some headroom */
1167
1168struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1169{
1170 struct sk_buff *skb2;
1171 int delta = headroom - skb_headroom(skb);
1172
1173 if (delta <= 0)
1174 skb2 = pskb_copy(skb, GFP_ATOMIC);
1175 else {
1176 skb2 = skb_clone(skb, GFP_ATOMIC);
1177 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1178 GFP_ATOMIC)) {
1179 kfree_skb(skb2);
1180 skb2 = NULL;
1181 }
1182 }
1183 return skb2;
1184}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001185EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187/**
1188 * skb_copy_expand - copy and expand sk_buff
1189 * @skb: buffer to copy
1190 * @newheadroom: new free bytes at head
1191 * @newtailroom: new free bytes at tail
1192 * @gfp_mask: allocation priority
1193 *
1194 * Make a copy of both an &sk_buff and its data and while doing so
1195 * allocate additional space.
1196 *
1197 * This is used when the caller wishes to modify the data and needs a
1198 * private copy of the data to alter as well as more space for new fields.
1199 * Returns %NULL on failure or the pointer to the buffer
1200 * on success. The returned buffer has a reference count of 1.
1201 *
1202 * You must pass %GFP_ATOMIC as the allocation priority if this function
1203 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
1205struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001206 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001207 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 /*
1210 * Allocate the copy buffer
1211 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001212 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1213 gfp_mask, skb_alloc_rx_flag(skb),
1214 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001215 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int head_copy_len, head_copy_off;
1217
1218 if (!n)
1219 return NULL;
1220
1221 skb_reserve(n, newheadroom);
1222
1223 /* Set the tail pointer and length */
1224 skb_put(n, skb->len);
1225
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001226 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 head_copy_off = 0;
1228 if (newheadroom <= head_copy_len)
1229 head_copy_len = newheadroom;
1230 else
1231 head_copy_off = newheadroom - head_copy_len;
1232
1233 /* Copy the linear header and data. */
1234 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1235 skb->len + head_copy_len))
1236 BUG();
1237
1238 copy_skb_header(n, skb);
1239
Eric Dumazet030737b2013-10-19 11:42:54 -07001240 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return n;
1243}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001244EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246/**
1247 * skb_pad - zero pad the tail of an skb
1248 * @skb: buffer to pad
1249 * @pad: space to pad
1250 *
1251 * Ensure that a buffer is followed by a padding area that is zero
1252 * filled. Used by network drivers which may DMA or transfer data
1253 * beyond the buffer end onto the wire.
1254 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001255 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001257
Herbert Xu5b057c62006-06-23 02:06:41 -07001258int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Herbert Xu5b057c62006-06-23 02:06:41 -07001260 int err;
1261 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001264 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001268
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001269 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001270 if (likely(skb_cloned(skb) || ntail > 0)) {
1271 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1272 if (unlikely(err))
1273 goto free_skb;
1274 }
1275
1276 /* FIXME: The use of this function with non-linear skb's really needs
1277 * to be audited.
1278 */
1279 err = skb_linearize(skb);
1280 if (unlikely(err))
1281 goto free_skb;
1282
1283 memset(skb->data + skb->len, 0, pad);
1284 return 0;
1285
1286free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001288 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001289}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001290EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001291
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001292/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001293 * pskb_put - add data to the tail of a potentially fragmented buffer
1294 * @skb: start of the buffer to use
1295 * @tail: tail fragment of the buffer to use
1296 * @len: amount of data to add
1297 *
1298 * This function extends the used data area of the potentially
1299 * fragmented buffer. @tail must be the last fragment of @skb -- or
1300 * @skb itself. If this would exceed the total buffer size the kernel
1301 * will panic. A pointer to the first byte of the extra data is
1302 * returned.
1303 */
1304
1305unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1306{
1307 if (tail != skb) {
1308 skb->data_len += len;
1309 skb->len += len;
1310 }
1311 return skb_put(tail, len);
1312}
1313EXPORT_SYMBOL_GPL(pskb_put);
1314
1315/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001316 * skb_put - add data to a buffer
1317 * @skb: buffer to use
1318 * @len: amount of data to add
1319 *
1320 * This function extends the used data area of the buffer. If this would
1321 * exceed the total buffer size the kernel will panic. A pointer to the
1322 * first byte of the extra data is returned.
1323 */
1324unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1325{
1326 unsigned char *tmp = skb_tail_pointer(skb);
1327 SKB_LINEAR_ASSERT(skb);
1328 skb->tail += len;
1329 skb->len += len;
1330 if (unlikely(skb->tail > skb->end))
1331 skb_over_panic(skb, len, __builtin_return_address(0));
1332 return tmp;
1333}
1334EXPORT_SYMBOL(skb_put);
1335
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001336/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001337 * skb_push - add data to the start of a buffer
1338 * @skb: buffer to use
1339 * @len: amount of data to add
1340 *
1341 * This function extends the used data area of the buffer at the buffer
1342 * start. If this would exceed the total buffer headroom the kernel will
1343 * panic. A pointer to the first byte of the extra data is returned.
1344 */
1345unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1346{
1347 skb->data -= len;
1348 skb->len += len;
1349 if (unlikely(skb->data<skb->head))
1350 skb_under_panic(skb, len, __builtin_return_address(0));
1351 return skb->data;
1352}
1353EXPORT_SYMBOL(skb_push);
1354
1355/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001356 * skb_pull - remove data from the start of a buffer
1357 * @skb: buffer to use
1358 * @len: amount of data to remove
1359 *
1360 * This function removes data from the start of a buffer, returning
1361 * the memory to the headroom. A pointer to the next data in the buffer
1362 * is returned. Once the data has been pulled future pushes will overwrite
1363 * the old data.
1364 */
1365unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1366{
David S. Miller47d29642010-05-02 02:21:44 -07001367 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001368}
1369EXPORT_SYMBOL(skb_pull);
1370
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001371/**
1372 * skb_trim - remove end from a buffer
1373 * @skb: buffer to alter
1374 * @len: new length
1375 *
1376 * Cut the length of a buffer down by removing data from the tail. If
1377 * the buffer is already under the length specified it is not modified.
1378 * The skb must be linear.
1379 */
1380void skb_trim(struct sk_buff *skb, unsigned int len)
1381{
1382 if (skb->len > len)
1383 __skb_trim(skb, len);
1384}
1385EXPORT_SYMBOL(skb_trim);
1386
Herbert Xu3cc0e872006-06-09 16:13:38 -07001387/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
1389
Herbert Xu3cc0e872006-06-09 16:13:38 -07001390int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Herbert Xu27b437c2006-07-13 19:26:39 -07001392 struct sk_buff **fragp;
1393 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int offset = skb_headlen(skb);
1395 int nfrags = skb_shinfo(skb)->nr_frags;
1396 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001397 int err;
1398
1399 if (skb_cloned(skb) &&
1400 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001403 i = 0;
1404 if (offset >= len)
1405 goto drop_pages;
1406
1407 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001408 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001409
1410 if (end < len) {
1411 offset = end;
1412 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001414
Eric Dumazet9e903e02011-10-18 21:00:24 +00001415 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001416
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001417drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001418 skb_shinfo(skb)->nr_frags = i;
1419
1420 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001421 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001422
David S. Miller21dc3302010-08-23 00:13:46 -07001423 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001424 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001425 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Herbert Xu27b437c2006-07-13 19:26:39 -07001428 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1429 fragp = &frag->next) {
1430 int end = offset + frag->len;
1431
1432 if (skb_shared(frag)) {
1433 struct sk_buff *nfrag;
1434
1435 nfrag = skb_clone(frag, GFP_ATOMIC);
1436 if (unlikely(!nfrag))
1437 return -ENOMEM;
1438
1439 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001440 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001441 frag = nfrag;
1442 *fragp = frag;
1443 }
1444
1445 if (end < len) {
1446 offset = end;
1447 continue;
1448 }
1449
1450 if (end > len &&
1451 unlikely((err = pskb_trim(frag, len - offset))))
1452 return err;
1453
1454 if (frag->next)
1455 skb_drop_list(&frag->next);
1456 break;
1457 }
1458
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001459done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001460 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 skb->data_len -= skb->len - len;
1462 skb->len = len;
1463 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001464 skb->len = len;
1465 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001466 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
1469 return 0;
1470}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001471EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473/**
1474 * __pskb_pull_tail - advance tail of skb header
1475 * @skb: buffer to reallocate
1476 * @delta: number of bytes to advance tail
1477 *
1478 * The function makes a sense only on a fragmented &sk_buff,
1479 * it expands header moving its tail forward and copying necessary
1480 * data from fragmented part.
1481 *
1482 * &sk_buff MUST have reference count of 1.
1483 *
1484 * Returns %NULL (and &sk_buff does not change) if pull failed
1485 * or value of new tail of skb in the case of success.
1486 *
1487 * All the pointers pointing into skb header may change and must be
1488 * reloaded after call to this function.
1489 */
1490
1491/* Moves tail of skb head forward, copying data from fragmented part,
1492 * when it is necessary.
1493 * 1. It may fail due to malloc failure.
1494 * 2. It may change skb pointers.
1495 *
1496 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1497 */
1498unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1499{
1500 /* If skb has not enough free space at tail, get new one
1501 * plus 128 bytes for future expansions. If we have enough
1502 * room at tail, reallocate without expansion only if skb is cloned.
1503 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001504 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 if (eat > 0 || skb_cloned(skb)) {
1507 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1508 GFP_ATOMIC))
1509 return NULL;
1510 }
1511
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001512 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 BUG();
1514
1515 /* Optimization: no fragments, no reasons to preestimate
1516 * size of pulled pages. Superb.
1517 */
David S. Miller21dc3302010-08-23 00:13:46 -07001518 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto pull_pages;
1520
1521 /* Estimate size of pulled pages. */
1522 eat = delta;
1523 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001524 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1525
1526 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001528 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530
1531 /* If we need update frag list, we are in troubles.
1532 * Certainly, it possible to add an offset to skb data,
1533 * but taking into account that pulling is expected to
1534 * be very rare operation, it is worth to fight against
1535 * further bloating skb head and crucify ourselves here instead.
1536 * Pure masohism, indeed. 8)8)
1537 */
1538 if (eat) {
1539 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1540 struct sk_buff *clone = NULL;
1541 struct sk_buff *insp = NULL;
1542
1543 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001544 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 if (list->len <= eat) {
1547 /* Eaten as whole. */
1548 eat -= list->len;
1549 list = list->next;
1550 insp = list;
1551 } else {
1552 /* Eaten partially. */
1553
1554 if (skb_shared(list)) {
1555 /* Sucks! We need to fork list. :-( */
1556 clone = skb_clone(list, GFP_ATOMIC);
1557 if (!clone)
1558 return NULL;
1559 insp = list->next;
1560 list = clone;
1561 } else {
1562 /* This may be pulled without
1563 * problems. */
1564 insp = list;
1565 }
1566 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001567 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return NULL;
1569 }
1570 break;
1571 }
1572 } while (eat);
1573
1574 /* Free pulled out fragments. */
1575 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1576 skb_shinfo(skb)->frag_list = list->next;
1577 kfree_skb(list);
1578 }
1579 /* And insert new clone at head. */
1580 if (clone) {
1581 clone->next = list;
1582 skb_shinfo(skb)->frag_list = clone;
1583 }
1584 }
1585 /* Success! Now we may commit changes to skb data. */
1586
1587pull_pages:
1588 eat = delta;
1589 k = 0;
1590 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001591 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1592
1593 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001594 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001595 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 } else {
1597 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1598 if (eat) {
1599 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001600 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 eat = 0;
1602 }
1603 k++;
1604 }
1605 }
1606 skb_shinfo(skb)->nr_frags = k;
1607
1608 skb->tail += delta;
1609 skb->data_len -= delta;
1610
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001611 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001613EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Eric Dumazet22019b12011-07-29 18:37:31 +00001615/**
1616 * skb_copy_bits - copy bits from skb to kernel buffer
1617 * @skb: source skb
1618 * @offset: offset in source
1619 * @to: destination buffer
1620 * @len: number of bytes to copy
1621 *
1622 * Copy the specified number of bytes from the source skb to the
1623 * destination buffer.
1624 *
1625 * CAUTION ! :
1626 * If its prototype is ever changed,
1627 * check arch/{*}/net/{*}.S files,
1628 * since it is called from BPF assembly code.
1629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1631{
David S. Miller1a028e52007-04-27 15:21:23 -07001632 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001633 struct sk_buff *frag_iter;
1634 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (offset > (int)skb->len - len)
1637 goto fault;
1638
1639 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001640 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (copy > len)
1642 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001643 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if ((len -= copy) == 0)
1645 return 0;
1646 offset += copy;
1647 to += copy;
1648 }
1649
1650 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001651 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001652 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001654 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001655
Eric Dumazet51c56b02012-04-05 11:35:15 +02001656 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if ((copy = end - offset) > 0) {
1658 u8 *vaddr;
1659
1660 if (copy > len)
1661 copy = len;
1662
Eric Dumazet51c56b02012-04-05 11:35:15 +02001663 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001665 vaddr + f->page_offset + offset - start,
1666 copy);
1667 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 if ((len -= copy) == 0)
1670 return 0;
1671 offset += copy;
1672 to += copy;
1673 }
David S. Miller1a028e52007-04-27 15:21:23 -07001674 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
David S. Millerfbb398a2009-06-09 00:18:59 -07001677 skb_walk_frags(skb, frag_iter) {
1678 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
David S. Millerfbb398a2009-06-09 00:18:59 -07001680 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David S. Millerfbb398a2009-06-09 00:18:59 -07001682 end = start + frag_iter->len;
1683 if ((copy = end - offset) > 0) {
1684 if (copy > len)
1685 copy = len;
1686 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1687 goto fault;
1688 if ((len -= copy) == 0)
1689 return 0;
1690 offset += copy;
1691 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001693 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Shirley Maa6686f22011-07-06 12:22:12 +00001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (!len)
1697 return 0;
1698
1699fault:
1700 return -EFAULT;
1701}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001702EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jens Axboe9c55e012007-11-06 23:30:13 -08001704/*
1705 * Callback from splice_to_pipe(), if we need to release some pages
1706 * at the end of the spd in case we error'ed out in filling the pipe.
1707 */
1708static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1709{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001710 put_page(spd->pages[i]);
1711}
Jens Axboe9c55e012007-11-06 23:30:13 -08001712
David S. Millera108d5f2012-04-23 23:06:11 -04001713static struct page *linear_to_page(struct page *page, unsigned int *len,
1714 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001715 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001716{
Eric Dumazet5640f762012-09-23 23:04:42 +00001717 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001718
Eric Dumazet5640f762012-09-23 23:04:42 +00001719 if (!sk_page_frag_refill(sk, pfrag))
1720 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001721
Eric Dumazet5640f762012-09-23 23:04:42 +00001722 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001723
Eric Dumazet5640f762012-09-23 23:04:42 +00001724 memcpy(page_address(pfrag->page) + pfrag->offset,
1725 page_address(page) + *offset, *len);
1726 *offset = pfrag->offset;
1727 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001728
Eric Dumazet5640f762012-09-23 23:04:42 +00001729 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001730}
1731
Eric Dumazet41c73a02012-04-22 12:26:16 +00001732static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1733 struct page *page,
1734 unsigned int offset)
1735{
1736 return spd->nr_pages &&
1737 spd->pages[spd->nr_pages - 1] == page &&
1738 (spd->partial[spd->nr_pages - 1].offset +
1739 spd->partial[spd->nr_pages - 1].len == offset);
1740}
1741
Jens Axboe9c55e012007-11-06 23:30:13 -08001742/*
1743 * Fill page/offset/length into spd, if it can hold more pages.
1744 */
David S. Millera108d5f2012-04-23 23:06:11 -04001745static bool spd_fill_page(struct splice_pipe_desc *spd,
1746 struct pipe_inode_info *pipe, struct page *page,
1747 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001748 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001749 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001750{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001751 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001752 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001753
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001754 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001755 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001756 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001757 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001758 }
1759 if (spd_can_coalesce(spd, page, offset)) {
1760 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001761 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001762 }
1763 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001764 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001765 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001766 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001767 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001768
David S. Millera108d5f2012-04-23 23:06:11 -04001769 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001770}
1771
David S. Millera108d5f2012-04-23 23:06:11 -04001772static bool __splice_segment(struct page *page, unsigned int poff,
1773 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001774 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001775 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001776 struct sock *sk,
1777 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001778{
1779 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001780 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001781
1782 /* skip this segment if already processed */
1783 if (*off >= plen) {
1784 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001785 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001786 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001787
Octavian Purdila2870c432008-07-15 00:49:11 -07001788 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001789 poff += *off;
1790 plen -= *off;
1791 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001792
Eric Dumazet18aafc62013-01-11 14:46:37 +00001793 do {
1794 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001795
Eric Dumazet18aafc62013-01-11 14:46:37 +00001796 if (spd_fill_page(spd, pipe, page, &flen, poff,
1797 linear, sk))
1798 return true;
1799 poff += flen;
1800 plen -= flen;
1801 *len -= flen;
1802 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001803
David S. Millera108d5f2012-04-23 23:06:11 -04001804 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001805}
1806
1807/*
David S. Millera108d5f2012-04-23 23:06:11 -04001808 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001809 * pipe is full or if we already spliced the requested length.
1810 */
David S. Millera108d5f2012-04-23 23:06:11 -04001811static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1812 unsigned int *offset, unsigned int *len,
1813 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001814{
1815 int seg;
1816
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001817 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001818 * If skb->head_frag is set, this 'linear' part is backed by a
1819 * fragment, and if the head is not shared with any clones then
1820 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001821 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001822 if (__splice_segment(virt_to_page(skb->data),
1823 (unsigned long) skb->data & (PAGE_SIZE - 1),
1824 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001825 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001826 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001827 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001828 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001829
1830 /*
1831 * then map the fragments
1832 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001833 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1834 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1835
Ian Campbellea2ab692011-08-22 23:44:58 +00001836 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001837 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001838 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001839 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001840 }
1841
David S. Millera108d5f2012-04-23 23:06:11 -04001842 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001843}
1844
1845/*
1846 * Map data from the skb to a pipe. Should handle both the linear part,
1847 * the fragments, and the frag list. It does NOT handle frag lists within
1848 * the frag list, if such a thing exists. We'd probably need to recurse to
1849 * handle that cleanly.
1850 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001851int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001852 struct pipe_inode_info *pipe, unsigned int tlen,
1853 unsigned int flags)
1854{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001855 struct partial_page partial[MAX_SKB_FRAGS];
1856 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001857 struct splice_pipe_desc spd = {
1858 .pages = pages,
1859 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001860 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001861 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001862 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 .spd_release = sock_spd_release,
1864 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001865 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001866 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001867 int ret = 0;
1868
Jens Axboe9c55e012007-11-06 23:30:13 -08001869 /*
1870 * __skb_splice_bits() only fails if the output has no room left,
1871 * so no point in going over the frag_list for the error case.
1872 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001873 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001874 goto done;
1875 else if (!tlen)
1876 goto done;
1877
1878 /*
1879 * now see if we have a frag_list to map
1880 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001881 skb_walk_frags(skb, frag_iter) {
1882 if (!tlen)
1883 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001884 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001885 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001886 }
1887
1888done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001889 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001890 /*
1891 * Drop the socket lock, otherwise we have reverse
1892 * locking dependencies between sk_lock and i_mutex
1893 * here as compared to sendfile(). We enter here
1894 * with the socket lock held, and splice_to_pipe() will
1895 * grab the pipe inode lock. For sendfile() emulation,
1896 * we call into ->sendpage() with the i_mutex lock held
1897 * and networking will grab the socket lock.
1898 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001899 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001900 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001901 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001902 }
1903
Jens Axboe35f3d142010-05-20 10:43:18 +02001904 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001905}
1906
Herbert Xu357b40a2005-04-19 22:30:14 -07001907/**
1908 * skb_store_bits - store bits from kernel buffer to skb
1909 * @skb: destination buffer
1910 * @offset: offset in destination
1911 * @from: source buffer
1912 * @len: number of bytes to copy
1913 *
1914 * Copy the specified number of bytes from the source buffer to the
1915 * destination skb. This function handles all the messy bits of
1916 * traversing fragment lists and such.
1917 */
1918
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001919int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001920{
David S. Miller1a028e52007-04-27 15:21:23 -07001921 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001922 struct sk_buff *frag_iter;
1923 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001924
1925 if (offset > (int)skb->len - len)
1926 goto fault;
1927
David S. Miller1a028e52007-04-27 15:21:23 -07001928 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001929 if (copy > len)
1930 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001931 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001932 if ((len -= copy) == 0)
1933 return 0;
1934 offset += copy;
1935 from += copy;
1936 }
1937
1938 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1939 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001940 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001941
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001942 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001943
Eric Dumazet9e903e02011-10-18 21:00:24 +00001944 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001945 if ((copy = end - offset) > 0) {
1946 u8 *vaddr;
1947
1948 if (copy > len)
1949 copy = len;
1950
Eric Dumazet51c56b02012-04-05 11:35:15 +02001951 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001952 memcpy(vaddr + frag->page_offset + offset - start,
1953 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001954 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001955
1956 if ((len -= copy) == 0)
1957 return 0;
1958 offset += copy;
1959 from += copy;
1960 }
David S. Miller1a028e52007-04-27 15:21:23 -07001961 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001962 }
1963
David S. Millerfbb398a2009-06-09 00:18:59 -07001964 skb_walk_frags(skb, frag_iter) {
1965 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001966
David S. Millerfbb398a2009-06-09 00:18:59 -07001967 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001968
David S. Millerfbb398a2009-06-09 00:18:59 -07001969 end = start + frag_iter->len;
1970 if ((copy = end - offset) > 0) {
1971 if (copy > len)
1972 copy = len;
1973 if (skb_store_bits(frag_iter, offset - start,
1974 from, copy))
1975 goto fault;
1976 if ((len -= copy) == 0)
1977 return 0;
1978 offset += copy;
1979 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001980 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001981 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001982 }
1983 if (!len)
1984 return 0;
1985
1986fault:
1987 return -EFAULT;
1988}
Herbert Xu357b40a2005-04-19 22:30:14 -07001989EXPORT_SYMBOL(skb_store_bits);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001992__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1993 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
David S. Miller1a028e52007-04-27 15:21:23 -07001995 int start = skb_headlen(skb);
1996 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001997 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int pos = 0;
1999
2000 /* Checksum header. */
2001 if (copy > 0) {
2002 if (copy > len)
2003 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002004 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if ((len -= copy) == 0)
2006 return csum;
2007 offset += copy;
2008 pos = copy;
2009 }
2010
2011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002012 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002013 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002015 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002016
Eric Dumazet51c56b02012-04-05 11:35:15 +02002017 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002019 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 if (copy > len)
2023 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002024 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002025 csum2 = ops->update(vaddr + frag->page_offset +
2026 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002027 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002028 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (!(len -= copy))
2030 return csum;
2031 offset += copy;
2032 pos += copy;
2033 }
David S. Miller1a028e52007-04-27 15:21:23 -07002034 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
David S. Millerfbb398a2009-06-09 00:18:59 -07002037 skb_walk_frags(skb, frag_iter) {
2038 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
David S. Millerfbb398a2009-06-09 00:18:59 -07002040 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
David S. Millerfbb398a2009-06-09 00:18:59 -07002042 end = start + frag_iter->len;
2043 if ((copy = end - offset) > 0) {
2044 __wsum csum2;
2045 if (copy > len)
2046 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002047 csum2 = __skb_checksum(frag_iter, offset - start,
2048 copy, 0, ops);
2049 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002050 if ((len -= copy) == 0)
2051 return csum;
2052 offset += copy;
2053 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002055 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002057 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 return csum;
2060}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002061EXPORT_SYMBOL(__skb_checksum);
2062
2063__wsum skb_checksum(const struct sk_buff *skb, int offset,
2064 int len, __wsum csum)
2065{
2066 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002067 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002068 .combine = csum_block_add_ext,
2069 };
2070
2071 return __skb_checksum(skb, offset, len, csum, &ops);
2072}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002073EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075/* Both of above in one bottle. */
2076
Al Viro81d77662006-11-14 21:37:33 -08002077__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2078 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
David S. Miller1a028e52007-04-27 15:21:23 -07002080 int start = skb_headlen(skb);
2081 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002082 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int pos = 0;
2084
2085 /* Copy header. */
2086 if (copy > 0) {
2087 if (copy > len)
2088 copy = len;
2089 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2090 copy, csum);
2091 if ((len -= copy) == 0)
2092 return csum;
2093 offset += copy;
2094 to += copy;
2095 pos = copy;
2096 }
2097
2098 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002099 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002101 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002102
Eric Dumazet9e903e02011-10-18 21:00:24 +00002103 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002105 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 u8 *vaddr;
2107 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2108
2109 if (copy > len)
2110 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002111 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002113 frag->page_offset +
2114 offset - start, to,
2115 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002116 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 csum = csum_block_add(csum, csum2, pos);
2118 if (!(len -= copy))
2119 return csum;
2120 offset += copy;
2121 to += copy;
2122 pos += copy;
2123 }
David S. Miller1a028e52007-04-27 15:21:23 -07002124 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
David S. Millerfbb398a2009-06-09 00:18:59 -07002127 skb_walk_frags(skb, frag_iter) {
2128 __wsum csum2;
2129 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
David S. Millerfbb398a2009-06-09 00:18:59 -07002131 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
David S. Millerfbb398a2009-06-09 00:18:59 -07002133 end = start + frag_iter->len;
2134 if ((copy = end - offset) > 0) {
2135 if (copy > len)
2136 copy = len;
2137 csum2 = skb_copy_and_csum_bits(frag_iter,
2138 offset - start,
2139 to, copy, 0);
2140 csum = csum_block_add(csum, csum2, pos);
2141 if ((len -= copy) == 0)
2142 return csum;
2143 offset += copy;
2144 to += copy;
2145 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002147 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002149 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return csum;
2151}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002152EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Thomas Grafaf2806f2013-12-13 15:22:17 +01002154 /**
2155 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2156 * @from: source buffer
2157 *
2158 * Calculates the amount of linear headroom needed in the 'to' skb passed
2159 * into skb_zerocopy().
2160 */
2161unsigned int
2162skb_zerocopy_headlen(const struct sk_buff *from)
2163{
2164 unsigned int hlen = 0;
2165
2166 if (!from->head_frag ||
2167 skb_headlen(from) < L1_CACHE_BYTES ||
2168 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2169 hlen = skb_headlen(from);
2170
2171 if (skb_has_frag_list(from))
2172 hlen = from->len;
2173
2174 return hlen;
2175}
2176EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2177
2178/**
2179 * skb_zerocopy - Zero copy skb to skb
2180 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002181 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002182 * @len: number of bytes to copy from source buffer
2183 * @hlen: size of linear headroom in destination buffer
2184 *
2185 * Copies up to `len` bytes from `from` to `to` by creating references
2186 * to the frags in the source buffer.
2187 *
2188 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2189 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002190 *
2191 * Return value:
2192 * 0: everything is OK
2193 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2194 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002195 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002196int
2197skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002198{
2199 int i, j = 0;
2200 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002201 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002202 struct page *page;
2203 unsigned int offset;
2204
2205 BUG_ON(!from->head_frag && !hlen);
2206
2207 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002208 if (len <= skb_tailroom(to))
2209 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002210
2211 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002212 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2213 if (unlikely(ret))
2214 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002215 len -= hlen;
2216 } else {
2217 plen = min_t(int, skb_headlen(from), len);
2218 if (plen) {
2219 page = virt_to_head_page(from->head);
2220 offset = from->data - (unsigned char *)page_address(page);
2221 __skb_fill_page_desc(to, 0, page, offset, plen);
2222 get_page(page);
2223 j = 1;
2224 len -= plen;
2225 }
2226 }
2227
2228 to->truesize += len + plen;
2229 to->len += len + plen;
2230 to->data_len += len + plen;
2231
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002232 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2233 skb_tx_error(from);
2234 return -ENOMEM;
2235 }
2236
Thomas Grafaf2806f2013-12-13 15:22:17 +01002237 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2238 if (!len)
2239 break;
2240 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2241 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2242 len -= skb_shinfo(to)->frags[j].size;
2243 skb_frag_ref(to, j);
2244 j++;
2245 }
2246 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002247
2248 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002249}
2250EXPORT_SYMBOL_GPL(skb_zerocopy);
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2253{
Al Virod3bc23e2006-11-14 21:24:49 -08002254 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 long csstart;
2256
Patrick McHardy84fa7932006-08-29 16:44:56 -07002257 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002258 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 else
2260 csstart = skb_headlen(skb);
2261
Kris Katterjohn09a62662006-01-08 22:24:28 -08002262 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002264 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 csum = 0;
2267 if (csstart != skb->len)
2268 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2269 skb->len - csstart, 0);
2270
Patrick McHardy84fa7932006-08-29 16:44:56 -07002271 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002272 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Al Virod3bc23e2006-11-14 21:24:49 -08002274 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002277EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279/**
2280 * skb_dequeue - remove from the head of the queue
2281 * @list: list to dequeue from
2282 *
2283 * Remove the head of the list. The list lock is taken so the function
2284 * may be used safely with other locking list functions. The head item is
2285 * returned or %NULL if the list is empty.
2286 */
2287
2288struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2289{
2290 unsigned long flags;
2291 struct sk_buff *result;
2292
2293 spin_lock_irqsave(&list->lock, flags);
2294 result = __skb_dequeue(list);
2295 spin_unlock_irqrestore(&list->lock, flags);
2296 return result;
2297}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002298EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300/**
2301 * skb_dequeue_tail - remove from the tail of the queue
2302 * @list: list to dequeue from
2303 *
2304 * Remove the tail of the list. The list lock is taken so the function
2305 * may be used safely with other locking list functions. The tail item is
2306 * returned or %NULL if the list is empty.
2307 */
2308struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2309{
2310 unsigned long flags;
2311 struct sk_buff *result;
2312
2313 spin_lock_irqsave(&list->lock, flags);
2314 result = __skb_dequeue_tail(list);
2315 spin_unlock_irqrestore(&list->lock, flags);
2316 return result;
2317}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002318EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320/**
2321 * skb_queue_purge - empty a list
2322 * @list: list to empty
2323 *
2324 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2325 * the list and one reference dropped. This function takes the list
2326 * lock and is atomic with respect to other list locking functions.
2327 */
2328void skb_queue_purge(struct sk_buff_head *list)
2329{
2330 struct sk_buff *skb;
2331 while ((skb = skb_dequeue(list)) != NULL)
2332 kfree_skb(skb);
2333}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002334EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336/**
2337 * skb_queue_head - queue a buffer at the list head
2338 * @list: list to use
2339 * @newsk: buffer to queue
2340 *
2341 * Queue a buffer at the start of the list. This function takes the
2342 * list lock and can be used safely with other locking &sk_buff functions
2343 * safely.
2344 *
2345 * A buffer cannot be placed on two lists at the same time.
2346 */
2347void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2348{
2349 unsigned long flags;
2350
2351 spin_lock_irqsave(&list->lock, flags);
2352 __skb_queue_head(list, newsk);
2353 spin_unlock_irqrestore(&list->lock, flags);
2354}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002355EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357/**
2358 * skb_queue_tail - queue a buffer at the list tail
2359 * @list: list to use
2360 * @newsk: buffer to queue
2361 *
2362 * Queue a buffer at the tail of the list. This function takes the
2363 * list lock and can be used safely with other locking &sk_buff functions
2364 * safely.
2365 *
2366 * A buffer cannot be placed on two lists at the same time.
2367 */
2368void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2369{
2370 unsigned long flags;
2371
2372 spin_lock_irqsave(&list->lock, flags);
2373 __skb_queue_tail(list, newsk);
2374 spin_unlock_irqrestore(&list->lock, flags);
2375}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002376EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378/**
2379 * skb_unlink - remove a buffer from a list
2380 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002381 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 *
David S. Miller8728b832005-08-09 19:25:21 -07002383 * Remove a packet from a list. The list locks are taken and this
2384 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 *
David S. Miller8728b832005-08-09 19:25:21 -07002386 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 */
David S. Miller8728b832005-08-09 19:25:21 -07002388void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
David S. Miller8728b832005-08-09 19:25:21 -07002390 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
David S. Miller8728b832005-08-09 19:25:21 -07002392 spin_lock_irqsave(&list->lock, flags);
2393 __skb_unlink(skb, list);
2394 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002396EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398/**
2399 * skb_append - append a buffer
2400 * @old: buffer to insert after
2401 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002402 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 *
2404 * Place a packet after a given packet in a list. The list locks are taken
2405 * and this function is atomic with respect to other list locked calls.
2406 * A buffer cannot be placed on two lists at the same time.
2407 */
David S. Miller8728b832005-08-09 19:25:21 -07002408void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 unsigned long flags;
2411
David S. Miller8728b832005-08-09 19:25:21 -07002412 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002413 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002414 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002416EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418/**
2419 * skb_insert - insert a buffer
2420 * @old: buffer to insert before
2421 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002422 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 *
David S. Miller8728b832005-08-09 19:25:21 -07002424 * Place a packet before a given packet in a list. The list locks are
2425 * taken and this function is atomic with respect to other list locked
2426 * calls.
2427 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 * A buffer cannot be placed on two lists at the same time.
2429 */
David S. Miller8728b832005-08-09 19:25:21 -07002430void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 unsigned long flags;
2433
David S. Miller8728b832005-08-09 19:25:21 -07002434 spin_lock_irqsave(&list->lock, flags);
2435 __skb_insert(newsk, old->prev, old, list);
2436 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002438EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static inline void skb_split_inside_header(struct sk_buff *skb,
2441 struct sk_buff* skb1,
2442 const u32 len, const int pos)
2443{
2444 int i;
2445
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002446 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2447 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* And move data appendix as is. */
2449 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2450 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2451
2452 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2453 skb_shinfo(skb)->nr_frags = 0;
2454 skb1->data_len = skb->data_len;
2455 skb1->len += skb1->data_len;
2456 skb->data_len = 0;
2457 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002458 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
2461static inline void skb_split_no_header(struct sk_buff *skb,
2462 struct sk_buff* skb1,
2463 const u32 len, int pos)
2464{
2465 int i, k = 0;
2466 const int nfrags = skb_shinfo(skb)->nr_frags;
2467
2468 skb_shinfo(skb)->nr_frags = 0;
2469 skb1->len = skb1->data_len = skb->len - len;
2470 skb->len = len;
2471 skb->data_len = len - pos;
2472
2473 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002474 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 if (pos + size > len) {
2477 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2478
2479 if (pos < len) {
2480 /* Split frag.
2481 * We have two variants in this case:
2482 * 1. Move all the frag to the second
2483 * part, if it is possible. F.e.
2484 * this approach is mandatory for TUX,
2485 * where splitting is expensive.
2486 * 2. Split is accurately. We make this.
2487 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002488 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002490 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2491 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 skb_shinfo(skb)->nr_frags++;
2493 }
2494 k++;
2495 } else
2496 skb_shinfo(skb)->nr_frags++;
2497 pos += size;
2498 }
2499 skb_shinfo(skb1)->nr_frags = k;
2500}
2501
2502/**
2503 * skb_split - Split fragmented skb to two parts at length len.
2504 * @skb: the buffer to split
2505 * @skb1: the buffer to receive the second part
2506 * @len: new length for skb
2507 */
2508void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2509{
2510 int pos = skb_headlen(skb);
2511
Amerigo Wang68534c62013-02-19 22:51:30 +00002512 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (len < pos) /* Split line is inside header. */
2514 skb_split_inside_header(skb, skb1, len, pos);
2515 else /* Second chunk has no header, nothing to copy. */
2516 skb_split_no_header(skb, skb1, len, pos);
2517}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002518EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002520/* Shifting from/to a cloned skb is a no-go.
2521 *
2522 * Caller cannot keep skb_shinfo related pointers past calling here!
2523 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002524static int skb_prepare_for_shift(struct sk_buff *skb)
2525{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002526 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002527}
2528
2529/**
2530 * skb_shift - Shifts paged data partially from skb to another
2531 * @tgt: buffer into which tail data gets added
2532 * @skb: buffer from which the paged data comes from
2533 * @shiftlen: shift up to this many bytes
2534 *
2535 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002536 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002537 * It's up to caller to free skb if everything was shifted.
2538 *
2539 * If @tgt runs out of frags, the whole operation is aborted.
2540 *
2541 * Skb cannot include anything else but paged data while tgt is allowed
2542 * to have non-paged data as well.
2543 *
2544 * TODO: full sized shift could be optimized but that would need
2545 * specialized skb free'er to handle frags without up-to-date nr_frags.
2546 */
2547int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2548{
2549 int from, to, merge, todo;
2550 struct skb_frag_struct *fragfrom, *fragto;
2551
2552 BUG_ON(shiftlen > skb->len);
2553 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2554
2555 todo = shiftlen;
2556 from = 0;
2557 to = skb_shinfo(tgt)->nr_frags;
2558 fragfrom = &skb_shinfo(skb)->frags[from];
2559
2560 /* Actual merge is delayed until the point when we know we can
2561 * commit all, so that we don't have to undo partial changes
2562 */
2563 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002564 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2565 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002566 merge = -1;
2567 } else {
2568 merge = to - 1;
2569
Eric Dumazet9e903e02011-10-18 21:00:24 +00002570 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002571 if (todo < 0) {
2572 if (skb_prepare_for_shift(skb) ||
2573 skb_prepare_for_shift(tgt))
2574 return 0;
2575
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002576 /* All previous frag pointers might be stale! */
2577 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002578 fragto = &skb_shinfo(tgt)->frags[merge];
2579
Eric Dumazet9e903e02011-10-18 21:00:24 +00002580 skb_frag_size_add(fragto, shiftlen);
2581 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002582 fragfrom->page_offset += shiftlen;
2583
2584 goto onlymerged;
2585 }
2586
2587 from++;
2588 }
2589
2590 /* Skip full, not-fitting skb to avoid expensive operations */
2591 if ((shiftlen == skb->len) &&
2592 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2593 return 0;
2594
2595 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2596 return 0;
2597
2598 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2599 if (to == MAX_SKB_FRAGS)
2600 return 0;
2601
2602 fragfrom = &skb_shinfo(skb)->frags[from];
2603 fragto = &skb_shinfo(tgt)->frags[to];
2604
Eric Dumazet9e903e02011-10-18 21:00:24 +00002605 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002606 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002607 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002608 from++;
2609 to++;
2610
2611 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002612 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002613 fragto->page = fragfrom->page;
2614 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002615 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002616
2617 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002618 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002619 todo = 0;
2620
2621 to++;
2622 break;
2623 }
2624 }
2625
2626 /* Ready to "commit" this state change to tgt */
2627 skb_shinfo(tgt)->nr_frags = to;
2628
2629 if (merge >= 0) {
2630 fragfrom = &skb_shinfo(skb)->frags[0];
2631 fragto = &skb_shinfo(tgt)->frags[merge];
2632
Eric Dumazet9e903e02011-10-18 21:00:24 +00002633 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002634 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002635 }
2636
2637 /* Reposition in the original skb */
2638 to = 0;
2639 while (from < skb_shinfo(skb)->nr_frags)
2640 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2641 skb_shinfo(skb)->nr_frags = to;
2642
2643 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2644
2645onlymerged:
2646 /* Most likely the tgt won't ever need its checksum anymore, skb on
2647 * the other hand might need it if it needs to be resent
2648 */
2649 tgt->ip_summed = CHECKSUM_PARTIAL;
2650 skb->ip_summed = CHECKSUM_PARTIAL;
2651
2652 /* Yak, is it really working this way? Some helper please? */
2653 skb->len -= shiftlen;
2654 skb->data_len -= shiftlen;
2655 skb->truesize -= shiftlen;
2656 tgt->len += shiftlen;
2657 tgt->data_len += shiftlen;
2658 tgt->truesize += shiftlen;
2659
2660 return shiftlen;
2661}
2662
Thomas Graf677e90e2005-06-23 20:59:51 -07002663/**
2664 * skb_prepare_seq_read - Prepare a sequential read of skb data
2665 * @skb: the buffer to read
2666 * @from: lower offset of data to be read
2667 * @to: upper offset of data to be read
2668 * @st: state variable
2669 *
2670 * Initializes the specified state variable. Must be called before
2671 * invoking skb_seq_read() for the first time.
2672 */
2673void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2674 unsigned int to, struct skb_seq_state *st)
2675{
2676 st->lower_offset = from;
2677 st->upper_offset = to;
2678 st->root_skb = st->cur_skb = skb;
2679 st->frag_idx = st->stepped_offset = 0;
2680 st->frag_data = NULL;
2681}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002682EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002683
2684/**
2685 * skb_seq_read - Sequentially read skb data
2686 * @consumed: number of bytes consumed by the caller so far
2687 * @data: destination pointer for data to be returned
2688 * @st: state variable
2689 *
Mathias Krausebc323832013-11-07 14:18:26 +01002690 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002691 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002692 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002693 * of the block or 0 if the end of the skb data or the upper
2694 * offset has been reached.
2695 *
2696 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002697 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002698 * of bytes already consumed and the next call to
2699 * skb_seq_read() will return the remaining part of the block.
2700 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002701 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002702 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002703 * reads of potentially non linear data.
2704 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002705 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002706 * at the moment, state->root_skb could be replaced with
2707 * a stack for this purpose.
2708 */
2709unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2710 struct skb_seq_state *st)
2711{
2712 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2713 skb_frag_t *frag;
2714
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002715 if (unlikely(abs_offset >= st->upper_offset)) {
2716 if (st->frag_data) {
2717 kunmap_atomic(st->frag_data);
2718 st->frag_data = NULL;
2719 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002720 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002721 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002722
2723next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002724 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002725
Thomas Chenault995b3372009-05-18 21:43:27 -07002726 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002727 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002728 return block_limit - abs_offset;
2729 }
2730
2731 if (st->frag_idx == 0 && !st->frag_data)
2732 st->stepped_offset += skb_headlen(st->cur_skb);
2733
2734 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2735 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002736 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002737
2738 if (abs_offset < block_limit) {
2739 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002740 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002741
2742 *data = (u8 *) st->frag_data + frag->page_offset +
2743 (abs_offset - st->stepped_offset);
2744
2745 return block_limit - abs_offset;
2746 }
2747
2748 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002749 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002750 st->frag_data = NULL;
2751 }
2752
2753 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002754 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002755 }
2756
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002757 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002758 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002759 st->frag_data = NULL;
2760 }
2761
David S. Miller21dc3302010-08-23 00:13:46 -07002762 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002763 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002764 st->frag_idx = 0;
2765 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002766 } else if (st->cur_skb->next) {
2767 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002768 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002769 goto next_skb;
2770 }
2771
2772 return 0;
2773}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002774EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002775
2776/**
2777 * skb_abort_seq_read - Abort a sequential read of skb data
2778 * @st: state variable
2779 *
2780 * Must be called if skb_seq_read() was not called until it
2781 * returned 0.
2782 */
2783void skb_abort_seq_read(struct skb_seq_state *st)
2784{
2785 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002786 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002787}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002788EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002789
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002790#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2791
2792static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2793 struct ts_config *conf,
2794 struct ts_state *state)
2795{
2796 return skb_seq_read(offset, text, TS_SKB_CB(state));
2797}
2798
2799static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2800{
2801 skb_abort_seq_read(TS_SKB_CB(state));
2802}
2803
2804/**
2805 * skb_find_text - Find a text pattern in skb data
2806 * @skb: the buffer to look in
2807 * @from: search offset
2808 * @to: search limit
2809 * @config: textsearch configuration
2810 * @state: uninitialized textsearch state variable
2811 *
2812 * Finds a pattern in the skb data according to the specified
2813 * textsearch configuration. Use textsearch_next() to retrieve
2814 * subsequent occurrences of the pattern. Returns the offset
2815 * to the first occurrence or UINT_MAX if no match was found.
2816 */
2817unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2818 unsigned int to, struct ts_config *config,
2819 struct ts_state *state)
2820{
Phil Oesterf72b9482006-06-26 00:00:57 -07002821 unsigned int ret;
2822
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002823 config->get_next_block = skb_ts_get_next_block;
2824 config->finish = skb_ts_finish;
2825
2826 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2827
Phil Oesterf72b9482006-06-26 00:00:57 -07002828 ret = textsearch_find(config, state);
2829 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002830}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002831EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002832
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002833/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002834 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002835 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002836 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002837 * @getfrag: call back function to be used for getting the user data
2838 * @from: pointer to user message iov
2839 * @length: length of the iov message
2840 *
2841 * Description: This procedure append the user data in the fragment part
2842 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2843 */
2844int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002845 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002846 int len, int odd, struct sk_buff *skb),
2847 void *from, int length)
2848{
Eric Dumazetb2111722012-12-28 06:06:37 +00002849 int frg_cnt = skb_shinfo(skb)->nr_frags;
2850 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002851 int offset = 0;
2852 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002853 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002854
2855 do {
2856 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002857 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002858 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002859
Eric Dumazetb2111722012-12-28 06:06:37 +00002860 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002861 return -ENOMEM;
2862
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002863 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002864 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002865
Eric Dumazetb2111722012-12-28 06:06:37 +00002866 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2867 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002868 if (ret < 0)
2869 return -EFAULT;
2870
2871 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002872 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2873 copy);
2874 frg_cnt++;
2875 pfrag->offset += copy;
2876 get_page(pfrag->page);
2877
2878 skb->truesize += copy;
2879 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002880 skb->len += copy;
2881 skb->data_len += copy;
2882 offset += copy;
2883 length -= copy;
2884
2885 } while (length > 0);
2886
2887 return 0;
2888}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002889EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002890
Herbert Xucbb042f2006-03-20 22:43:56 -08002891/**
2892 * skb_pull_rcsum - pull skb and update receive checksum
2893 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002894 * @len: length of data pulled
2895 *
2896 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002897 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002898 * receive path processing instead of skb_pull unless you know
2899 * that the checksum difference is zero (e.g., a valid IP header)
2900 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002901 */
2902unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2903{
2904 BUG_ON(len > skb->len);
2905 skb->len -= len;
2906 BUG_ON(skb->len < skb->data_len);
2907 skb_postpull_rcsum(skb, skb->data, len);
2908 return skb->data += len;
2909}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002910EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2911
Herbert Xuf4c50d92006-06-22 03:02:40 -07002912/**
2913 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002914 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002915 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002916 *
2917 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002918 * a pointer to the first in a list of new skbs for the segments.
2919 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002920 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002921struct sk_buff *skb_segment(struct sk_buff *head_skb,
2922 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002923{
2924 struct sk_buff *segs = NULL;
2925 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002926 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002927 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2928 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2929 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002930 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002931 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002932 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002933 unsigned int headroom;
2934 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002935 __be16 proto;
2936 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002937 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002938 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002939 int err = -ENOMEM;
2940 int i = 0;
2941 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002942 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002943
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002944 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002945 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002946 if (unlikely(!proto))
2947 return ERR_PTR(-EINVAL);
2948
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002949 csum = !head_skb->encap_hdr_csum &&
2950 !!can_checksum_protocol(features, proto);
2951
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002952 headroom = skb_headroom(head_skb);
2953 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002954
2955 do {
2956 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002957 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002958 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002959 int size;
2960
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002961 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002962 if (len > mss)
2963 len = mss;
2964
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002965 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002966 if (hsize < 0)
2967 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002968 if (hsize > len || !sg)
2969 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002970
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002971 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2972 (skb_headlen(list_skb) == len || sg)) {
2973 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002974
Herbert Xu9d8506c2013-11-21 11:10:04 -08002975 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002976 nfrags = skb_shinfo(list_skb)->nr_frags;
2977 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002978 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002979 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002980
2981 while (pos < offset + len) {
2982 BUG_ON(i >= nfrags);
2983
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002984 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002985 if (pos + size > offset + len)
2986 break;
2987
2988 i++;
2989 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002990 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002991 }
2992
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002993 nskb = skb_clone(list_skb, GFP_ATOMIC);
2994 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002995
2996 if (unlikely(!nskb))
2997 goto err;
2998
Herbert Xu9d8506c2013-11-21 11:10:04 -08002999 if (unlikely(pskb_trim(nskb, len))) {
3000 kfree_skb(nskb);
3001 goto err;
3002 }
3003
Alexander Duyckec47ea82012-05-04 14:26:56 +00003004 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003005 if (skb_cow_head(nskb, doffset + headroom)) {
3006 kfree_skb(nskb);
3007 goto err;
3008 }
3009
Alexander Duyckec47ea82012-05-04 14:26:56 +00003010 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003011 skb_release_head_state(nskb);
3012 __skb_push(nskb, doffset);
3013 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003014 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003015 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003016 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003017
3018 if (unlikely(!nskb))
3019 goto err;
3020
3021 skb_reserve(nskb, headroom);
3022 __skb_put(nskb, doffset);
3023 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003024
3025 if (segs)
3026 tail->next = nskb;
3027 else
3028 segs = nskb;
3029 tail = nskb;
3030
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003031 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003032
Eric Dumazet030737b2013-10-19 11:42:54 -07003033 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003034 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003035
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003036 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003037 nskb->data - tnl_hlen,
3038 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003039
Herbert Xu9d8506c2013-11-21 11:10:04 -08003040 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003041 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003042
Tom Herberte585f232014-11-04 09:06:54 -08003043 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003044 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003045 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003046 skb_put(nskb, len),
3047 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003048 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003049 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003050 continue;
3051 }
3052
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003053 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003054
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003055 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003056 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003057
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003058 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3059 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003060
Herbert Xu9d8506c2013-11-21 11:10:04 -08003061 while (pos < offset + len) {
3062 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003063 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003064
3065 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003066 nfrags = skb_shinfo(list_skb)->nr_frags;
3067 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003068 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003069
3070 BUG_ON(!nfrags);
3071
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003072 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003073 }
3074
3075 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3076 MAX_SKB_FRAGS)) {
3077 net_warn_ratelimited(
3078 "skb_segment: too many frags: %u %u\n",
3079 pos, mss);
3080 goto err;
3081 }
3082
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003083 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3084 goto err;
3085
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003086 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003087 __skb_frag_ref(nskb_frag);
3088 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003089
3090 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003091 nskb_frag->page_offset += offset - pos;
3092 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003093 }
3094
Herbert Xu89319d382008-12-15 23:26:06 -08003095 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003096
3097 if (pos + size <= offset + len) {
3098 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003099 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003100 pos += size;
3101 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003102 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003103 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003104 }
3105
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003106 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003107 }
3108
Herbert Xu89319d382008-12-15 23:26:06 -08003109skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003110 nskb->data_len = len - hsize;
3111 nskb->len += nskb->data_len;
3112 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003113
Simon Horman1cdbcb72013-05-19 15:46:49 +00003114perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003115 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003116 nskb->csum = skb_checksum(nskb, doffset,
3117 nskb->len - doffset, 0);
3118 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003119 SKB_GSO_CB(nskb)->csum_start =
3120 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003121 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003122 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003123
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003124 /* Some callers want to get the end of the list.
3125 * Put it in segs->prev to avoid walking the list.
3126 * (see validate_xmit_skb_list() for example)
3127 */
3128 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003129
3130 /* Following permits correct backpressure, for protocols
3131 * using skb_set_owner_w().
3132 * Idea is to tranfert ownership from head_skb to last segment.
3133 */
3134 if (head_skb->destructor == sock_wfree) {
3135 swap(tail->truesize, head_skb->truesize);
3136 swap(tail->destructor, head_skb->destructor);
3137 swap(tail->sk, head_skb->sk);
3138 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003139 return segs;
3140
3141err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003142 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003143 return ERR_PTR(err);
3144}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003145EXPORT_SYMBOL_GPL(skb_segment);
3146
Herbert Xu71d93b32008-12-15 23:42:33 -08003147int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3148{
Eric Dumazet8a291112013-10-08 09:02:23 -07003149 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003150 unsigned int offset = skb_gro_offset(skb);
3151 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003152 struct sk_buff *nskb, *lp, *p = *head;
3153 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003154 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003155 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003156
Eric Dumazet8a291112013-10-08 09:02:23 -07003157 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003158 return -E2BIG;
3159
Eric Dumazet29e98242014-05-16 11:34:37 -07003160 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003161 pinfo = skb_shinfo(lp);
3162
3163 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003164 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003165 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003166 int i = skbinfo->nr_frags;
3167 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003168
Herbert Xu66e92fc2009-05-26 18:50:32 +00003169 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003170 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003171
Eric Dumazet8a291112013-10-08 09:02:23 -07003172 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003173 pinfo->nr_frags = nr_frags;
3174 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003175
Herbert Xu9aaa1562009-05-26 18:50:33 +00003176 frag = pinfo->frags + nr_frags;
3177 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003178 do {
3179 *--frag = *--frag2;
3180 } while (--i);
3181
3182 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003183 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003184
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003185 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003186 delta_truesize = skb->truesize -
3187 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003188
Herbert Xuf5572062009-01-14 20:40:03 -08003189 skb->truesize -= skb->data_len;
3190 skb->len -= skb->data_len;
3191 skb->data_len = 0;
3192
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003193 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003194 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003195 } else if (skb->head_frag) {
3196 int nr_frags = pinfo->nr_frags;
3197 skb_frag_t *frag = pinfo->frags + nr_frags;
3198 struct page *page = virt_to_head_page(skb->head);
3199 unsigned int first_size = headlen - offset;
3200 unsigned int first_offset;
3201
3202 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003203 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003204
3205 first_offset = skb->data -
3206 (unsigned char *)page_address(page) +
3207 offset;
3208
3209 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3210
3211 frag->page.p = page;
3212 frag->page_offset = first_offset;
3213 skb_frag_size_set(frag, first_size);
3214
3215 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3216 /* We dont need to clear skbinfo->nr_frags here */
3217
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003218 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003219 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3220 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003221 }
Eric Dumazet73d3fe62014-09-29 10:34:29 -07003222 /* switch back to head shinfo */
3223 pinfo = skb_shinfo(p);
3224
Eric Dumazet8a291112013-10-08 09:02:23 -07003225 if (pinfo->frag_list)
3226 goto merge;
3227 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003228 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003229
3230 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003231 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003232 if (unlikely(!nskb))
3233 return -ENOMEM;
3234
3235 __copy_skb_header(nskb, p);
3236 nskb->mac_len = p->mac_len;
3237
3238 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003239 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003240
Herbert Xu86911732009-01-29 14:19:50 +00003241 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003242 skb_set_network_header(nskb, skb_network_offset(p));
3243 skb_set_transport_header(nskb, skb_transport_offset(p));
3244
Herbert Xu86911732009-01-29 14:19:50 +00003245 __skb_pull(p, skb_gro_offset(p));
3246 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3247 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003248
Herbert Xu71d93b32008-12-15 23:42:33 -08003249 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003250 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003251 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003252 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003253 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003254
3255 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003256 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003257 nskb->len += p->len;
3258
3259 *head = nskb;
3260 nskb->next = p->next;
3261 p->next = NULL;
3262
3263 p = nskb;
3264
3265merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003266 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003267 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003268 unsigned int eat = offset - headlen;
3269
3270 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003271 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003272 skb->data_len -= eat;
3273 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003274 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003275 }
3276
Herbert Xu67147ba2009-05-26 18:50:22 +00003277 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003278
Eric Dumazet29e98242014-05-16 11:34:37 -07003279 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003280 skb_shinfo(p)->frag_list = skb;
3281 else
3282 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003283 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003284 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003285 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003286
Herbert Xu5d38a072009-01-04 16:13:40 -08003287done:
3288 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003289 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003290 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003291 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003292 if (lp != p) {
3293 lp->data_len += len;
3294 lp->truesize += delta_truesize;
3295 lp->len += len;
3296 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003297 NAPI_GRO_CB(skb)->same_flow = 1;
3298 return 0;
3299}
Herbert Xu71d93b32008-12-15 23:42:33 -08003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301void __init skb_init(void)
3302{
3303 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3304 sizeof(struct sk_buff),
3305 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003306 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003307 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003308 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003309 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003310 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003311 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003312 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
David Howells716ea3a2007-04-02 20:19:53 -07003315/**
3316 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3317 * @skb: Socket buffer containing the buffers to be mapped
3318 * @sg: The scatter-gather list to map into
3319 * @offset: The offset into the buffer's contents to start mapping
3320 * @len: Length of buffer space to be mapped
3321 *
3322 * Fill the specified scatter-gather list with mappings/pointers into a
3323 * region of the buffer space attached to a socket buffer.
3324 */
David S. Miller51c739d2007-10-30 21:29:29 -07003325static int
3326__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003327{
David S. Miller1a028e52007-04-27 15:21:23 -07003328 int start = skb_headlen(skb);
3329 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003330 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003331 int elt = 0;
3332
3333 if (copy > 0) {
3334 if (copy > len)
3335 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02003336 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003337 elt++;
3338 if ((len -= copy) == 0)
3339 return elt;
3340 offset += copy;
3341 }
3342
3343 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003344 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003345
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003346 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003347
Eric Dumazet9e903e02011-10-18 21:00:24 +00003348 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003349 if ((copy = end - offset) > 0) {
3350 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3351
3352 if (copy > len)
3353 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003354 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f149032007-10-24 11:20:47 +02003355 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003356 elt++;
3357 if (!(len -= copy))
3358 return elt;
3359 offset += copy;
3360 }
David S. Miller1a028e52007-04-27 15:21:23 -07003361 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003362 }
3363
David S. Millerfbb398a2009-06-09 00:18:59 -07003364 skb_walk_frags(skb, frag_iter) {
3365 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003366
David S. Millerfbb398a2009-06-09 00:18:59 -07003367 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003368
David S. Millerfbb398a2009-06-09 00:18:59 -07003369 end = start + frag_iter->len;
3370 if ((copy = end - offset) > 0) {
3371 if (copy > len)
3372 copy = len;
3373 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3374 copy);
3375 if ((len -= copy) == 0)
3376 return elt;
3377 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003378 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003379 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003380 }
3381 BUG_ON(len);
3382 return elt;
3383}
3384
Fan Du25a91d82014-01-18 09:54:23 +08003385/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3386 * sglist without mark the sg which contain last skb data as the end.
3387 * So the caller can mannipulate sg list as will when padding new data after
3388 * the first call without calling sg_unmark_end to expend sg list.
3389 *
3390 * Scenario to use skb_to_sgvec_nomark:
3391 * 1. sg_init_table
3392 * 2. skb_to_sgvec_nomark(payload1)
3393 * 3. skb_to_sgvec_nomark(payload2)
3394 *
3395 * This is equivalent to:
3396 * 1. sg_init_table
3397 * 2. skb_to_sgvec(payload1)
3398 * 3. sg_unmark_end
3399 * 4. skb_to_sgvec(payload2)
3400 *
3401 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3402 * is more preferable.
3403 */
3404int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3405 int offset, int len)
3406{
3407 return __skb_to_sgvec(skb, sg, offset, len);
3408}
3409EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3410
David S. Miller51c739d2007-10-30 21:29:29 -07003411int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3412{
3413 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3414
Jens Axboec46f2332007-10-31 12:06:37 +01003415 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003416
3417 return nsg;
3418}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003419EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003420
David Howells716ea3a2007-04-02 20:19:53 -07003421/**
3422 * skb_cow_data - Check that a socket buffer's data buffers are writable
3423 * @skb: The socket buffer to check.
3424 * @tailbits: Amount of trailing space to be added
3425 * @trailer: Returned pointer to the skb where the @tailbits space begins
3426 *
3427 * Make sure that the data buffers attached to a socket buffer are
3428 * writable. If they are not, private copies are made of the data buffers
3429 * and the socket buffer is set to use these instead.
3430 *
3431 * If @tailbits is given, make sure that there is space to write @tailbits
3432 * bytes of data beyond current end of socket buffer. @trailer will be
3433 * set to point to the skb in which this space begins.
3434 *
3435 * The number of scatterlist elements required to completely map the
3436 * COW'd and extended socket buffer will be returned.
3437 */
3438int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3439{
3440 int copyflag;
3441 int elt;
3442 struct sk_buff *skb1, **skb_p;
3443
3444 /* If skb is cloned or its head is paged, reallocate
3445 * head pulling out all the pages (pages are considered not writable
3446 * at the moment even if they are anonymous).
3447 */
3448 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3449 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3450 return -ENOMEM;
3451
3452 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003453 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003454 /* A little of trouble, not enough of space for trailer.
3455 * This should not happen, when stack is tuned to generate
3456 * good frames. OK, on miss we reallocate and reserve even more
3457 * space, 128 bytes is fair. */
3458
3459 if (skb_tailroom(skb) < tailbits &&
3460 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3461 return -ENOMEM;
3462
3463 /* Voila! */
3464 *trailer = skb;
3465 return 1;
3466 }
3467
3468 /* Misery. We are in troubles, going to mincer fragments... */
3469
3470 elt = 1;
3471 skb_p = &skb_shinfo(skb)->frag_list;
3472 copyflag = 0;
3473
3474 while ((skb1 = *skb_p) != NULL) {
3475 int ntail = 0;
3476
3477 /* The fragment is partially pulled by someone,
3478 * this can happen on input. Copy it and everything
3479 * after it. */
3480
3481 if (skb_shared(skb1))
3482 copyflag = 1;
3483
3484 /* If the skb is the last, worry about trailer. */
3485
3486 if (skb1->next == NULL && tailbits) {
3487 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003488 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003489 skb_tailroom(skb1) < tailbits)
3490 ntail = tailbits + 128;
3491 }
3492
3493 if (copyflag ||
3494 skb_cloned(skb1) ||
3495 ntail ||
3496 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003497 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003498 struct sk_buff *skb2;
3499
3500 /* Fuck, we are miserable poor guys... */
3501 if (ntail == 0)
3502 skb2 = skb_copy(skb1, GFP_ATOMIC);
3503 else
3504 skb2 = skb_copy_expand(skb1,
3505 skb_headroom(skb1),
3506 ntail,
3507 GFP_ATOMIC);
3508 if (unlikely(skb2 == NULL))
3509 return -ENOMEM;
3510
3511 if (skb1->sk)
3512 skb_set_owner_w(skb2, skb1->sk);
3513
3514 /* Looking around. Are we still alive?
3515 * OK, link new skb, drop old one */
3516
3517 skb2->next = skb1->next;
3518 *skb_p = skb2;
3519 kfree_skb(skb1);
3520 skb1 = skb2;
3521 }
3522 elt++;
3523 *trailer = skb1;
3524 skb_p = &skb1->next;
3525 }
3526
3527 return elt;
3528}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003529EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003530
Eric Dumazetb1faf562010-05-31 23:44:05 -07003531static void sock_rmem_free(struct sk_buff *skb)
3532{
3533 struct sock *sk = skb->sk;
3534
3535 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3536}
3537
3538/*
3539 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3540 */
3541int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3542{
3543 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003544 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003545 return -ENOMEM;
3546
3547 skb_orphan(skb);
3548 skb->sk = sk;
3549 skb->destructor = sock_rmem_free;
3550 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3551
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003552 /* before exiting rcu section, make sure dst is refcounted */
3553 skb_dst_force(skb);
3554
Eric Dumazetb1faf562010-05-31 23:44:05 -07003555 skb_queue_tail(&sk->sk_error_queue, skb);
3556 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003557 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003558 return 0;
3559}
3560EXPORT_SYMBOL(sock_queue_err_skb);
3561
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003562struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3563{
3564 struct sk_buff_head *q = &sk->sk_error_queue;
3565 struct sk_buff *skb, *skb_next;
3566 int err = 0;
3567
3568 spin_lock_bh(&q->lock);
3569 skb = __skb_dequeue(q);
3570 if (skb && (skb_next = skb_peek(q)))
3571 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3572 spin_unlock_bh(&q->lock);
3573
3574 sk->sk_err = err;
3575 if (err)
3576 sk->sk_error_report(sk);
3577
3578 return skb;
3579}
3580EXPORT_SYMBOL(sock_dequeue_err_skb);
3581
Alexander Duyckcab41c42014-09-10 18:05:26 -04003582/**
3583 * skb_clone_sk - create clone of skb, and take reference to socket
3584 * @skb: the skb to clone
3585 *
3586 * This function creates a clone of a buffer that holds a reference on
3587 * sk_refcnt. Buffers created via this function are meant to be
3588 * returned using sock_queue_err_skb, or free via kfree_skb.
3589 *
3590 * When passing buffers allocated with this function to sock_queue_err_skb
3591 * it is necessary to wrap the call with sock_hold/sock_put in order to
3592 * prevent the socket from being released prior to being enqueued on
3593 * the sk_error_queue.
3594 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003595struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3596{
3597 struct sock *sk = skb->sk;
3598 struct sk_buff *clone;
3599
3600 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3601 return NULL;
3602
3603 clone = skb_clone(skb, GFP_ATOMIC);
3604 if (!clone) {
3605 sock_put(sk);
3606 return NULL;
3607 }
3608
3609 clone->sk = sk;
3610 clone->destructor = sock_efree;
3611
3612 return clone;
3613}
3614EXPORT_SYMBOL(skb_clone_sk);
3615
Alexander Duyck37846ef2014-09-04 13:31:10 -04003616static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3617 struct sock *sk,
3618 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003619{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003620 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003621 int err;
3622
Patrick Ohlyac45f602009-02-12 05:03:37 +00003623 serr = SKB_EXT_ERR(skb);
3624 memset(serr, 0, sizeof(*serr));
3625 serr->ee.ee_errno = ENOMSG;
3626 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003627 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003628 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003629 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003630 if (sk->sk_protocol == IPPROTO_TCP)
3631 serr->ee.ee_data -= sk->sk_tskey;
3632 }
Eric Dumazet29030372010-05-29 00:20:48 -07003633
Patrick Ohlyac45f602009-02-12 05:03:37 +00003634 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003635
Patrick Ohlyac45f602009-02-12 05:03:37 +00003636 if (err)
3637 kfree_skb(skb);
3638}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003639
3640void skb_complete_tx_timestamp(struct sk_buff *skb,
3641 struct skb_shared_hwtstamps *hwtstamps)
3642{
3643 struct sock *sk = skb->sk;
3644
Alexander Duyck62bccb82014-09-04 13:31:35 -04003645 /* take a reference to prevent skb_orphan() from freeing the socket */
3646 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003647
Alexander Duyck62bccb82014-09-04 13:31:35 -04003648 *skb_hwtstamps(skb) = *hwtstamps;
3649 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003650
3651 sock_put(sk);
3652}
3653EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3654
3655void __skb_tstamp_tx(struct sk_buff *orig_skb,
3656 struct skb_shared_hwtstamps *hwtstamps,
3657 struct sock *sk, int tstype)
3658{
3659 struct sk_buff *skb;
3660
3661 if (!sk)
3662 return;
3663
3664 if (hwtstamps)
3665 *skb_hwtstamps(orig_skb) = *hwtstamps;
3666 else
3667 orig_skb->tstamp = ktime_get_real();
3668
3669 skb = skb_clone(orig_skb, GFP_ATOMIC);
3670 if (!skb)
3671 return;
3672
3673 __skb_complete_tx_timestamp(skb, sk, tstype);
3674}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003675EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3676
3677void skb_tstamp_tx(struct sk_buff *orig_skb,
3678 struct skb_shared_hwtstamps *hwtstamps)
3679{
3680 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3681 SCM_TSTAMP_SND);
3682}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003683EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3684
Johannes Berg6e3e9392011-11-09 10:15:42 +01003685void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3686{
3687 struct sock *sk = skb->sk;
3688 struct sock_exterr_skb *serr;
3689 int err;
3690
3691 skb->wifi_acked_valid = 1;
3692 skb->wifi_acked = acked;
3693
3694 serr = SKB_EXT_ERR(skb);
3695 memset(serr, 0, sizeof(*serr));
3696 serr->ee.ee_errno = ENOMSG;
3697 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3698
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003699 /* take a reference to prevent skb_orphan() from freeing the socket */
3700 sock_hold(sk);
3701
Johannes Berg6e3e9392011-11-09 10:15:42 +01003702 err = sock_queue_err_skb(sk, skb);
3703 if (err)
3704 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003705
3706 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003707}
3708EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3709
Patrick Ohlyac45f602009-02-12 05:03:37 +00003710
Rusty Russellf35d9d82008-02-04 23:49:54 -05003711/**
3712 * skb_partial_csum_set - set up and verify partial csum values for packet
3713 * @skb: the skb to set
3714 * @start: the number of bytes after skb->data to start checksumming.
3715 * @off: the offset from start to place the checksum.
3716 *
3717 * For untrusted partially-checksummed packets, we need to make sure the values
3718 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3719 *
3720 * This function checks and sets those values and skb->ip_summed: if this
3721 * returns false you should drop the packet.
3722 */
3723bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3724{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003725 if (unlikely(start > skb_headlen(skb)) ||
3726 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003727 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3728 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003729 return false;
3730 }
3731 skb->ip_summed = CHECKSUM_PARTIAL;
3732 skb->csum_start = skb_headroom(skb) + start;
3733 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003734 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003735 return true;
3736}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003737EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003738
Paul Durranted1f50c2014-01-09 10:02:46 +00003739static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3740 unsigned int max)
3741{
3742 if (skb_headlen(skb) >= len)
3743 return 0;
3744
3745 /* If we need to pullup then pullup to the max, so we
3746 * won't need to do it again.
3747 */
3748 if (max > skb->len)
3749 max = skb->len;
3750
3751 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3752 return -ENOMEM;
3753
3754 if (skb_headlen(skb) < len)
3755 return -EPROTO;
3756
3757 return 0;
3758}
3759
Jan Beulichf9708b42014-03-11 13:56:05 +00003760#define MAX_TCP_HDR_LEN (15 * 4)
3761
3762static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3763 typeof(IPPROTO_IP) proto,
3764 unsigned int off)
3765{
3766 switch (proto) {
3767 int err;
3768
3769 case IPPROTO_TCP:
3770 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3771 off + MAX_TCP_HDR_LEN);
3772 if (!err && !skb_partial_csum_set(skb, off,
3773 offsetof(struct tcphdr,
3774 check)))
3775 err = -EPROTO;
3776 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3777
3778 case IPPROTO_UDP:
3779 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3780 off + sizeof(struct udphdr));
3781 if (!err && !skb_partial_csum_set(skb, off,
3782 offsetof(struct udphdr,
3783 check)))
3784 err = -EPROTO;
3785 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3786 }
3787
3788 return ERR_PTR(-EPROTO);
3789}
3790
Paul Durranted1f50c2014-01-09 10:02:46 +00003791/* This value should be large enough to cover a tagged ethernet header plus
3792 * maximally sized IP and TCP or UDP headers.
3793 */
3794#define MAX_IP_HDR_LEN 128
3795
Jan Beulichf9708b42014-03-11 13:56:05 +00003796static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003797{
3798 unsigned int off;
3799 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003800 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003801 int err;
3802
3803 fragment = false;
3804
3805 err = skb_maybe_pull_tail(skb,
3806 sizeof(struct iphdr),
3807 MAX_IP_HDR_LEN);
3808 if (err < 0)
3809 goto out;
3810
3811 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3812 fragment = true;
3813
3814 off = ip_hdrlen(skb);
3815
3816 err = -EPROTO;
3817
3818 if (fragment)
3819 goto out;
3820
Jan Beulichf9708b42014-03-11 13:56:05 +00003821 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3822 if (IS_ERR(csum))
3823 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003824
Jan Beulichf9708b42014-03-11 13:56:05 +00003825 if (recalculate)
3826 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3827 ip_hdr(skb)->daddr,
3828 skb->len - off,
3829 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003830 err = 0;
3831
3832out:
3833 return err;
3834}
3835
3836/* This value should be large enough to cover a tagged ethernet header plus
3837 * an IPv6 header, all options, and a maximal TCP or UDP header.
3838 */
3839#define MAX_IPV6_HDR_LEN 256
3840
3841#define OPT_HDR(type, skb, off) \
3842 (type *)(skb_network_header(skb) + (off))
3843
3844static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3845{
3846 int err;
3847 u8 nexthdr;
3848 unsigned int off;
3849 unsigned int len;
3850 bool fragment;
3851 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003852 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003853
3854 fragment = false;
3855 done = false;
3856
3857 off = sizeof(struct ipv6hdr);
3858
3859 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3860 if (err < 0)
3861 goto out;
3862
3863 nexthdr = ipv6_hdr(skb)->nexthdr;
3864
3865 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3866 while (off <= len && !done) {
3867 switch (nexthdr) {
3868 case IPPROTO_DSTOPTS:
3869 case IPPROTO_HOPOPTS:
3870 case IPPROTO_ROUTING: {
3871 struct ipv6_opt_hdr *hp;
3872
3873 err = skb_maybe_pull_tail(skb,
3874 off +
3875 sizeof(struct ipv6_opt_hdr),
3876 MAX_IPV6_HDR_LEN);
3877 if (err < 0)
3878 goto out;
3879
3880 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3881 nexthdr = hp->nexthdr;
3882 off += ipv6_optlen(hp);
3883 break;
3884 }
3885 case IPPROTO_AH: {
3886 struct ip_auth_hdr *hp;
3887
3888 err = skb_maybe_pull_tail(skb,
3889 off +
3890 sizeof(struct ip_auth_hdr),
3891 MAX_IPV6_HDR_LEN);
3892 if (err < 0)
3893 goto out;
3894
3895 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3896 nexthdr = hp->nexthdr;
3897 off += ipv6_authlen(hp);
3898 break;
3899 }
3900 case IPPROTO_FRAGMENT: {
3901 struct frag_hdr *hp;
3902
3903 err = skb_maybe_pull_tail(skb,
3904 off +
3905 sizeof(struct frag_hdr),
3906 MAX_IPV6_HDR_LEN);
3907 if (err < 0)
3908 goto out;
3909
3910 hp = OPT_HDR(struct frag_hdr, skb, off);
3911
3912 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3913 fragment = true;
3914
3915 nexthdr = hp->nexthdr;
3916 off += sizeof(struct frag_hdr);
3917 break;
3918 }
3919 default:
3920 done = true;
3921 break;
3922 }
3923 }
3924
3925 err = -EPROTO;
3926
3927 if (!done || fragment)
3928 goto out;
3929
Jan Beulichf9708b42014-03-11 13:56:05 +00003930 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3931 if (IS_ERR(csum))
3932 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003933
Jan Beulichf9708b42014-03-11 13:56:05 +00003934 if (recalculate)
3935 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3936 &ipv6_hdr(skb)->daddr,
3937 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003938 err = 0;
3939
3940out:
3941 return err;
3942}
3943
3944/**
3945 * skb_checksum_setup - set up partial checksum offset
3946 * @skb: the skb to set up
3947 * @recalculate: if true the pseudo-header checksum will be recalculated
3948 */
3949int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3950{
3951 int err;
3952
3953 switch (skb->protocol) {
3954 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003955 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003956 break;
3957
3958 case htons(ETH_P_IPV6):
3959 err = skb_checksum_setup_ipv6(skb, recalculate);
3960 break;
3961
3962 default:
3963 err = -EPROTO;
3964 break;
3965 }
3966
3967 return err;
3968}
3969EXPORT_SYMBOL(skb_checksum_setup);
3970
Ben Hutchings4497b072008-06-19 16:22:28 -07003971void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3972{
Joe Perchese87cc472012-05-13 21:56:26 +00003973 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3974 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003975}
Ben Hutchings4497b072008-06-19 16:22:28 -07003976EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003977
3978void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3979{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003980 if (head_stolen) {
3981 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003982 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003983 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003984 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003985 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003986}
3987EXPORT_SYMBOL(kfree_skb_partial);
3988
3989/**
3990 * skb_try_coalesce - try to merge skb to prior one
3991 * @to: prior buffer
3992 * @from: buffer to add
3993 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003994 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003995 */
3996bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3997 bool *fragstolen, int *delta_truesize)
3998{
3999 int i, delta, len = from->len;
4000
4001 *fragstolen = false;
4002
4003 if (skb_cloned(to))
4004 return false;
4005
4006 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004007 if (len)
4008 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004009 *delta_truesize = 0;
4010 return true;
4011 }
4012
4013 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4014 return false;
4015
4016 if (skb_headlen(from) != 0) {
4017 struct page *page;
4018 unsigned int offset;
4019
4020 if (skb_shinfo(to)->nr_frags +
4021 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4022 return false;
4023
4024 if (skb_head_is_locked(from))
4025 return false;
4026
4027 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4028
4029 page = virt_to_head_page(from->head);
4030 offset = from->data - (unsigned char *)page_address(page);
4031
4032 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4033 page, offset, skb_headlen(from));
4034 *fragstolen = true;
4035 } else {
4036 if (skb_shinfo(to)->nr_frags +
4037 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4038 return false;
4039
Weiping Panf4b549a2012-09-28 20:15:30 +00004040 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004041 }
4042
4043 WARN_ON_ONCE(delta < len);
4044
4045 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4046 skb_shinfo(from)->frags,
4047 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4048 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4049
4050 if (!skb_cloned(from))
4051 skb_shinfo(from)->nr_frags = 0;
4052
Li RongQing8ea853f2012-09-18 16:53:21 +00004053 /* if the skb is not cloned this does nothing
4054 * since we set nr_frags to 0.
4055 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004056 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4057 skb_frag_ref(from, i);
4058
4059 to->truesize += delta;
4060 to->len += len;
4061 to->data_len += len;
4062
4063 *delta_truesize = delta;
4064 return true;
4065}
4066EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004067
4068/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004069 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004070 *
4071 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004072 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004073 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004074 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4075 * into/from a tunnel. Some information have to be cleared during these
4076 * operations.
4077 * skb_scrub_packet can also be used to clean a skb before injecting it in
4078 * another namespace (@xnet == true). We have to clear all information in the
4079 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004080 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004081void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004082{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004083 if (xnet)
4084 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004085 skb->tstamp.tv64 = 0;
4086 skb->pkt_type = PACKET_HOST;
4087 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004088 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004089 skb_dst_drop(skb);
4090 skb->mark = 0;
4091 secpath_reset(skb);
4092 nf_reset(skb);
4093 nf_reset_trace(skb);
4094}
4095EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004096
4097/**
4098 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4099 *
4100 * @skb: GSO skb
4101 *
4102 * skb_gso_transport_seglen is used to determine the real size of the
4103 * individual segments, including Layer4 headers (TCP/UDP).
4104 *
4105 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4106 */
4107unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4108{
4109 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004110 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004111
Florian Westphalf993bc22014-10-20 13:49:18 +02004112 if (skb->encapsulation) {
4113 thlen = skb_inner_transport_header(skb) -
4114 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004115
Florian Westphalf993bc22014-10-20 13:49:18 +02004116 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4117 thlen += inner_tcp_hdrlen(skb);
4118 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4119 thlen = tcp_hdrlen(skb);
4120 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004121 /* UFO sets gso_size to the size of the fragmentation
4122 * payload, i.e. the size of the L4 (UDP) header is already
4123 * accounted for.
4124 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004125 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004126}
4127EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004128
4129static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4130{
4131 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4132 kfree_skb(skb);
4133 return NULL;
4134 }
4135
4136 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4137 skb->mac_header += VLAN_HLEN;
4138 return skb;
4139}
4140
4141struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4142{
4143 struct vlan_hdr *vhdr;
4144 u16 vlan_tci;
4145
4146 if (unlikely(vlan_tx_tag_present(skb))) {
4147 /* vlan_tci is already set-up so leave this for another time */
4148 return skb;
4149 }
4150
4151 skb = skb_share_check(skb, GFP_ATOMIC);
4152 if (unlikely(!skb))
4153 goto err_free;
4154
4155 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4156 goto err_free;
4157
4158 vhdr = (struct vlan_hdr *)skb->data;
4159 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4160 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4161
4162 skb_pull_rcsum(skb, VLAN_HLEN);
4163 vlan_set_encap_proto(skb, vhdr);
4164
4165 skb = skb_reorder_vlan_header(skb);
4166 if (unlikely(!skb))
4167 goto err_free;
4168
4169 skb_reset_network_header(skb);
4170 skb_reset_transport_header(skb);
4171 skb_reset_mac_len(skb);
4172
4173 return skb;
4174
4175err_free:
4176 kfree_skb(skb);
4177 return NULL;
4178}
4179EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004180
Jiri Pirkoe2195122014-11-19 14:05:01 +01004181int skb_ensure_writable(struct sk_buff *skb, int write_len)
4182{
4183 if (!pskb_may_pull(skb, write_len))
4184 return -ENOMEM;
4185
4186 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4187 return 0;
4188
4189 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4190}
4191EXPORT_SYMBOL(skb_ensure_writable);
4192
Jiri Pirko93515d52014-11-19 14:05:02 +01004193/* remove VLAN header from packet and update csum accordingly. */
4194static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4195{
4196 struct vlan_hdr *vhdr;
4197 unsigned int offset = skb->data - skb_mac_header(skb);
4198 int err;
4199
4200 __skb_push(skb, offset);
4201 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4202 if (unlikely(err))
4203 goto pull;
4204
4205 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4206
4207 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4208 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4209
4210 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4211 __skb_pull(skb, VLAN_HLEN);
4212
4213 vlan_set_encap_proto(skb, vhdr);
4214 skb->mac_header += VLAN_HLEN;
4215
4216 if (skb_network_offset(skb) < ETH_HLEN)
4217 skb_set_network_header(skb, ETH_HLEN);
4218
4219 skb_reset_mac_len(skb);
4220pull:
4221 __skb_pull(skb, offset);
4222
4223 return err;
4224}
4225
4226int skb_vlan_pop(struct sk_buff *skb)
4227{
4228 u16 vlan_tci;
4229 __be16 vlan_proto;
4230 int err;
4231
4232 if (likely(vlan_tx_tag_present(skb))) {
4233 skb->vlan_tci = 0;
4234 } else {
4235 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4236 skb->protocol != htons(ETH_P_8021AD)) ||
4237 skb->len < VLAN_ETH_HLEN))
4238 return 0;
4239
4240 err = __skb_vlan_pop(skb, &vlan_tci);
4241 if (err)
4242 return err;
4243 }
4244 /* move next vlan tag to hw accel tag */
4245 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4246 skb->protocol != htons(ETH_P_8021AD)) ||
4247 skb->len < VLAN_ETH_HLEN))
4248 return 0;
4249
4250 vlan_proto = skb->protocol;
4251 err = __skb_vlan_pop(skb, &vlan_tci);
4252 if (unlikely(err))
4253 return err;
4254
4255 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4256 return 0;
4257}
4258EXPORT_SYMBOL(skb_vlan_pop);
4259
4260int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4261{
4262 if (vlan_tx_tag_present(skb)) {
4263 unsigned int offset = skb->data - skb_mac_header(skb);
4264 int err;
4265
4266 /* __vlan_insert_tag expect skb->data pointing to mac header.
4267 * So change skb->data before calling it and change back to
4268 * original position later
4269 */
4270 __skb_push(skb, offset);
4271 err = __vlan_insert_tag(skb, skb->vlan_proto,
4272 vlan_tx_tag_get(skb));
4273 if (err)
4274 return err;
4275 skb->protocol = skb->vlan_proto;
4276 skb->mac_len += VLAN_HLEN;
4277 __skb_pull(skb, offset);
4278
4279 if (skb->ip_summed == CHECKSUM_COMPLETE)
4280 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4281 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4282 }
4283 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4284 return 0;
4285}
4286EXPORT_SYMBOL(skb_vlan_push);
4287
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004288/**
4289 * alloc_skb_with_frags - allocate skb with page frags
4290 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004291 * @header_len: size of linear part
4292 * @data_len: needed length in frags
4293 * @max_page_order: max page order desired.
4294 * @errcode: pointer to error code if any
4295 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004296 *
4297 * This can be used to allocate a paged skb, given a maximal order for frags.
4298 */
4299struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4300 unsigned long data_len,
4301 int max_page_order,
4302 int *errcode,
4303 gfp_t gfp_mask)
4304{
4305 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4306 unsigned long chunk;
4307 struct sk_buff *skb;
4308 struct page *page;
4309 gfp_t gfp_head;
4310 int i;
4311
4312 *errcode = -EMSGSIZE;
4313 /* Note this test could be relaxed, if we succeed to allocate
4314 * high order pages...
4315 */
4316 if (npages > MAX_SKB_FRAGS)
4317 return NULL;
4318
4319 gfp_head = gfp_mask;
4320 if (gfp_head & __GFP_WAIT)
4321 gfp_head |= __GFP_REPEAT;
4322
4323 *errcode = -ENOBUFS;
4324 skb = alloc_skb(header_len, gfp_head);
4325 if (!skb)
4326 return NULL;
4327
4328 skb->truesize += npages << PAGE_SHIFT;
4329
4330 for (i = 0; npages > 0; i++) {
4331 int order = max_page_order;
4332
4333 while (order) {
4334 if (npages >= 1 << order) {
4335 page = alloc_pages(gfp_mask |
4336 __GFP_COMP |
4337 __GFP_NOWARN |
4338 __GFP_NORETRY,
4339 order);
4340 if (page)
4341 goto fill_page;
4342 /* Do not retry other high order allocations */
4343 order = 1;
4344 max_page_order = 0;
4345 }
4346 order--;
4347 }
4348 page = alloc_page(gfp_mask);
4349 if (!page)
4350 goto failure;
4351fill_page:
4352 chunk = min_t(unsigned long, data_len,
4353 PAGE_SIZE << order);
4354 skb_fill_page_desc(skb, i, page, 0, chunk);
4355 data_len -= chunk;
4356 npages -= 1 << order;
4357 }
4358 return skb;
4359
4360failure:
4361 kfree_skb(skb);
4362 return NULL;
4363}
4364EXPORT_SYMBOL(alloc_skb_with_frags);