blob: 65a3798f43e697f5cb7a5b6039a203069698cbbe [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 Dumazet6ffe75e2014-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/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800447 * __alloc_rx_skb - allocate an skbuff for rx
Christoph Hellwig8af27452006-07-31 22:35:23 -0700448 * @length: length to allocate
449 * @gfp_mask: get_free_pages mask, passed to alloc_skb
Alexander Duyckfd11a832014-12-09 19:40:49 -0800450 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
451 * allocations in case we have to fallback to __alloc_skb()
452 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
453 * from napi_cache instead of netdev_cache.
Christoph Hellwig8af27452006-07-31 22:35:23 -0700454 *
455 * Allocate a new &sk_buff and assign it a usage count of one. The
456 * buffer has unspecified headroom built in. Users should allocate
457 * the headroom they think they need without accounting for the
458 * built in space. The built in space is used for optimisations.
459 *
460 * %NULL is returned if there is no free memory.
461 */
Alexander Duyckfd11a832014-12-09 19:40:49 -0800462static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
463 int flags)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700464{
Eric Dumazet6f532612012-05-18 05:12:12 +0000465 struct sk_buff *skb = NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800466 unsigned int fragsz = SKB_DATA_ALIGN(length) +
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000467 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700468
Eric Dumazet310e1582012-07-16 13:15:52 +0200469 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700470 void *data;
471
472 if (sk_memalloc_socks())
473 gfp_mask |= __GFP_MEMALLOC;
474
Alexander Duyckfd11a832014-12-09 19:40:49 -0800475 data = (flags & SKB_ALLOC_NAPI) ?
476 __napi_alloc_frag(fragsz, gfp_mask) :
477 __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000478
Eric Dumazet6f532612012-05-18 05:12:12 +0000479 if (likely(data)) {
480 skb = build_skb(data, fragsz);
481 if (unlikely(!skb))
482 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000483 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000484 } else {
Alexander Duyckfd11a832014-12-09 19:40:49 -0800485 skb = __alloc_skb(length, gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700486 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000487 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800488 return skb;
489}
490
491/**
492 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
493 * @dev: network device to receive on
494 * @length: length to allocate
495 * @gfp_mask: get_free_pages mask, passed to alloc_skb
496 *
497 * Allocate a new &sk_buff and assign it a usage count of one. The
498 * buffer has NET_SKB_PAD headroom built in. Users should allocate
499 * the headroom they think they need without accounting for the
500 * built in space. The built in space is used for optimisations.
501 *
502 * %NULL is returned if there is no free memory.
503 */
504struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
505 unsigned int length, gfp_t gfp_mask)
506{
507 struct sk_buff *skb;
508
509 length += NET_SKB_PAD;
510 skb = __alloc_rx_skb(length, gfp_mask, 0);
511
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700512 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700513 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700514 skb->dev = dev;
515 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800516
Christoph Hellwig8af27452006-07-31 22:35:23 -0700517 return skb;
518}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800519EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Alexander Duyckfd11a832014-12-09 19:40:49 -0800521/**
522 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
523 * @napi: napi instance this buffer was allocated for
524 * @length: length to allocate
525 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
526 *
527 * Allocate a new sk_buff for use in NAPI receive. This buffer will
528 * attempt to allocate the head from a special reserved region used
529 * only for NAPI Rx allocation. By doing this we can save several
530 * CPU cycles by avoiding having to disable and re-enable IRQs.
531 *
532 * %NULL is returned if there is no free memory.
533 */
534struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
535 unsigned int length, gfp_t gfp_mask)
536{
537 struct sk_buff *skb;
538
539 length += NET_SKB_PAD + NET_IP_ALIGN;
540 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
541
542 if (likely(skb)) {
543 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
544 skb->dev = napi->dev;
545 }
546
547 return skb;
548}
549EXPORT_SYMBOL(__napi_alloc_skb);
550
Peter Zijlstra654bed12008-10-07 14:22:33 -0700551void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000552 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700553{
554 skb_fill_page_desc(skb, i, page, off, size);
555 skb->len += size;
556 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000557 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700558}
559EXPORT_SYMBOL(skb_add_rx_frag);
560
Jason Wangf8e617e2013-11-01 14:07:47 +0800561void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
562 unsigned int truesize)
563{
564 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
565
566 skb_frag_size_add(frag, size);
567 skb->len += size;
568 skb->data_len += size;
569 skb->truesize += truesize;
570}
571EXPORT_SYMBOL(skb_coalesce_rx_frag);
572
Herbert Xu27b437c2006-07-13 19:26:39 -0700573static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700575 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700576 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Herbert Xu27b437c2006-07-13 19:26:39 -0700579static inline void skb_drop_fraglist(struct sk_buff *skb)
580{
581 skb_drop_list(&skb_shinfo(skb)->frag_list);
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static void skb_clone_fraglist(struct sk_buff *skb)
585{
586 struct sk_buff *list;
587
David S. Millerfbb398a2009-06-09 00:18:59 -0700588 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 skb_get(list);
590}
591
Eric Dumazetd3836f22012-04-27 00:33:38 +0000592static void skb_free_head(struct sk_buff *skb)
593{
594 if (skb->head_frag)
595 put_page(virt_to_head_page(skb->head));
596 else
597 kfree(skb->head);
598}
599
Adrian Bunk5bba1712006-06-29 13:02:35 -0700600static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Eric Dumazetff04a772014-09-23 18:39:30 -0700602 struct skb_shared_info *shinfo = skb_shinfo(skb);
603 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Eric Dumazetff04a772014-09-23 18:39:30 -0700605 if (skb->cloned &&
606 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
607 &shinfo->dataref))
608 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000609
Eric Dumazetff04a772014-09-23 18:39:30 -0700610 for (i = 0; i < shinfo->nr_frags; i++)
611 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000612
Eric Dumazetff04a772014-09-23 18:39:30 -0700613 /*
614 * If skb buf is from userspace, we need to notify the caller
615 * the lower device DMA has done;
616 */
617 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
618 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Eric Dumazetff04a772014-09-23 18:39:30 -0700620 uarg = shinfo->destructor_arg;
621 if (uarg->callback)
622 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700624
625 if (shinfo->frag_list)
626 kfree_skb_list(shinfo->frag_list);
627
628 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/*
632 * Free an skbuff by memory without cleaning the state.
633 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800634static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700636 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700637
David S. Millerd179cd12005-08-17 14:57:30 -0700638 switch (skb->fclone) {
639 case SKB_FCLONE_UNAVAILABLE:
640 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800641 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700642
643 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700644 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800645
646 /* We usually free the clone (TX completion) before original skb
647 * This test would have no chance to be true for the clone,
648 * while here, branch prediction will be good.
649 */
650 if (atomic_read(&fclones->fclone_ref) == 1)
651 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700652 break;
653
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800654 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700655 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700656 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700657 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800658 if (!atomic_dec_and_test(&fclones->fclone_ref))
659 return;
660fastpath:
661 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700664static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Eric Dumazetadf30902009-06-02 05:19:30 +0000666 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#ifdef CONFIG_XFRM
668 secpath_put(skb->sp);
669#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700670 if (skb->destructor) {
671 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 skb->destructor(skb);
673 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000674#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700675 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100676#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200677#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 nf_bridge_put(skb->nf_bridge);
679#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700680}
681
682/* Free everything but the sk_buff shell. */
683static void skb_release_all(struct sk_buff *skb)
684{
685 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000686 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000687 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Herbert Xu2d4baff2007-11-26 23:11:19 +0800690/**
691 * __kfree_skb - private function
692 * @skb: buffer
693 *
694 * Free an sk_buff. Release anything attached to the buffer.
695 * Clean the state. This is an internal helper function. Users should
696 * always call kfree_skb
697 */
698
699void __kfree_skb(struct sk_buff *skb)
700{
701 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 kfree_skbmem(skb);
703}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800704EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800707 * kfree_skb - free an sk_buff
708 * @skb: buffer to free
709 *
710 * Drop a reference to the buffer and free it if the usage count has
711 * hit zero.
712 */
713void kfree_skb(struct sk_buff *skb)
714{
715 if (unlikely(!skb))
716 return;
717 if (likely(atomic_read(&skb->users) == 1))
718 smp_rmb();
719 else if (likely(!atomic_dec_and_test(&skb->users)))
720 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000721 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800722 __kfree_skb(skb);
723}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800724EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800725
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700726void kfree_skb_list(struct sk_buff *segs)
727{
728 while (segs) {
729 struct sk_buff *next = segs->next;
730
731 kfree_skb(segs);
732 segs = next;
733 }
734}
735EXPORT_SYMBOL(kfree_skb_list);
736
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700737/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000738 * skb_tx_error - report an sk_buff xmit error
739 * @skb: buffer that triggered an error
740 *
741 * Report xmit error if a device callback is tracking this skb.
742 * skb must be freed afterwards.
743 */
744void skb_tx_error(struct sk_buff *skb)
745{
746 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
747 struct ubuf_info *uarg;
748
749 uarg = skb_shinfo(skb)->destructor_arg;
750 if (uarg->callback)
751 uarg->callback(uarg, false);
752 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
753 }
754}
755EXPORT_SYMBOL(skb_tx_error);
756
757/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000758 * consume_skb - free an skbuff
759 * @skb: buffer to free
760 *
761 * Drop a ref to the buffer and free it if the usage count has hit zero
762 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
763 * is being dropped after a failure and notes that
764 */
765void consume_skb(struct sk_buff *skb)
766{
767 if (unlikely(!skb))
768 return;
769 if (likely(atomic_read(&skb->users) == 1))
770 smp_rmb();
771 else if (likely(!atomic_dec_and_test(&skb->users)))
772 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900773 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000774 __kfree_skb(skb);
775}
776EXPORT_SYMBOL(consume_skb);
777
Eric Dumazetb1937222014-09-28 22:18:47 -0700778/* Make sure a field is enclosed inside headers_start/headers_end section */
779#define CHECK_SKB_FIELD(field) \
780 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
781 offsetof(struct sk_buff, headers_start)); \
782 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
783 offsetof(struct sk_buff, headers_end)); \
784
Herbert Xudec18812007-10-14 00:37:30 -0700785static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
786{
787 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700788 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700789 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700790 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000791 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700792#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700793 new->sp = secpath_get(old->sp);
794#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700795 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700796
Eric Dumazetb1937222014-09-28 22:18:47 -0700797 /* Note : this field could be in headers_start/headers_end section
798 * It is not yet because we do not want to have a 16 bit hole
799 */
800 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300801
Eric Dumazetb1937222014-09-28 22:18:47 -0700802 memcpy(&new->headers_start, &old->headers_start,
803 offsetof(struct sk_buff, headers_end) -
804 offsetof(struct sk_buff, headers_start));
805 CHECK_SKB_FIELD(protocol);
806 CHECK_SKB_FIELD(csum);
807 CHECK_SKB_FIELD(hash);
808 CHECK_SKB_FIELD(priority);
809 CHECK_SKB_FIELD(skb_iif);
810 CHECK_SKB_FIELD(vlan_proto);
811 CHECK_SKB_FIELD(vlan_tci);
812 CHECK_SKB_FIELD(transport_header);
813 CHECK_SKB_FIELD(network_header);
814 CHECK_SKB_FIELD(mac_header);
815 CHECK_SKB_FIELD(inner_protocol);
816 CHECK_SKB_FIELD(inner_transport_header);
817 CHECK_SKB_FIELD(inner_network_header);
818 CHECK_SKB_FIELD(inner_mac_header);
819 CHECK_SKB_FIELD(mark);
820#ifdef CONFIG_NETWORK_SECMARK
821 CHECK_SKB_FIELD(secmark);
822#endif
Cong Wange0d10952013-08-01 11:10:25 +0800823#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700824 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300825#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700826#ifdef CONFIG_NET_SCHED
827 CHECK_SKB_FIELD(tc_index);
828#ifdef CONFIG_NET_CLS_ACT
829 CHECK_SKB_FIELD(tc_verd);
830#endif
831#endif
832
Herbert Xudec18812007-10-14 00:37:30 -0700833}
834
Herbert Xu82c49a32009-05-22 22:11:37 +0000835/*
836 * You should not add any new code to this function. Add it to
837 * __copy_skb_header above instead.
838 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700839static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841#define C(x) n->x = skb->x
842
843 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700845 __copy_skb_header(n, skb);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 C(len);
848 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700849 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700850 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800851 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 C(tail);
855 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800856 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000857 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800858 C(data);
859 C(truesize);
860 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 atomic_inc(&(skb_shinfo(skb)->dataref));
863 skb->cloned = 1;
864
865 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700866#undef C
867}
868
869/**
870 * skb_morph - morph one skb into another
871 * @dst: the skb to receive the contents
872 * @src: the skb to supply the contents
873 *
874 * This is identical to skb_clone except that the target skb is
875 * supplied by the user.
876 *
877 * The target skb is returned upon exit.
878 */
879struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
880{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800881 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700882 return __skb_clone(dst, src);
883}
884EXPORT_SYMBOL_GPL(skb_morph);
885
Ben Hutchings2c530402012-07-10 10:55:09 +0000886/**
887 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000888 * @skb: the skb to modify
889 * @gfp_mask: allocation priority
890 *
891 * This must be called on SKBTX_DEV_ZEROCOPY skb.
892 * It will copy all frags into kernel and drop the reference
893 * to userspace pages.
894 *
895 * If this function is called from an interrupt gfp_mask() must be
896 * %GFP_ATOMIC.
897 *
898 * Returns 0 on success or a negative error code on failure
899 * to allocate kernel memory to copy to.
900 */
901int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000902{
903 int i;
904 int num_frags = skb_shinfo(skb)->nr_frags;
905 struct page *page, *head = NULL;
906 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
907
908 for (i = 0; i < num_frags; i++) {
909 u8 *vaddr;
910 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
911
Krishna Kumar02756ed2012-07-17 02:05:29 +0000912 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000913 if (!page) {
914 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900915 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000916 put_page(head);
917 head = next;
918 }
919 return -ENOMEM;
920 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200921 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000922 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000923 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200924 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900925 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000926 head = page;
927 }
928
929 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000930 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000931 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000932
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000933 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000934
935 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000936 for (i = num_frags - 1; i >= 0; i--) {
937 __skb_fill_page_desc(skb, i, head, 0,
938 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900939 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000940 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000941
942 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000943 return 0;
944}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000945EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000946
Herbert Xue0053ec2007-10-14 00:37:52 -0700947/**
948 * skb_clone - duplicate an sk_buff
949 * @skb: buffer to clone
950 * @gfp_mask: allocation priority
951 *
952 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
953 * copies share the same packet data but not structure. The new
954 * buffer has a reference count of 1. If the allocation fails the
955 * function returns %NULL otherwise the new buffer is returned.
956 *
957 * If this function is called from an interrupt gfp_mask() must be
958 * %GFP_ATOMIC.
959 */
960
961struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
962{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700963 struct sk_buff_fclones *fclones = container_of(skb,
964 struct sk_buff_fclones,
965 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800966 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700967
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000968 if (skb_orphan_frags(skb, gfp_mask))
969 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000970
Herbert Xue0053ec2007-10-14 00:37:52 -0700971 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800972 atomic_read(&fclones->fclone_ref) == 1) {
973 n = &fclones->skb2;
974 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700975 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700976 if (skb_pfmemalloc(skb))
977 gfp_mask |= __GFP_MEMALLOC;
978
Herbert Xue0053ec2007-10-14 00:37:52 -0700979 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
980 if (!n)
981 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200982
983 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700984 n->fclone = SKB_FCLONE_UNAVAILABLE;
985 }
986
987 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800989EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000991static void skb_headers_offset_update(struct sk_buff *skb, int off)
992{
Eric Dumazet030737b2013-10-19 11:42:54 -0700993 /* Only adjust this if it actually is csum_start rather than csum */
994 if (skb->ip_summed == CHECKSUM_PARTIAL)
995 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000996 /* {transport,network,mac}_header and tail are relative to skb->head */
997 skb->transport_header += off;
998 skb->network_header += off;
999 if (skb_mac_header_was_set(skb))
1000 skb->mac_header += off;
1001 skb->inner_transport_header += off;
1002 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001003 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1007{
Herbert Xudec18812007-10-14 00:37:30 -07001008 __copy_skb_header(new, old);
1009
Herbert Xu79671682006-06-22 02:40:14 -07001010 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1011 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1012 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Mel Gormanc93bdd02012-07-31 16:44:19 -07001015static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1016{
1017 if (skb_pfmemalloc(skb))
1018 return SKB_ALLOC_RX;
1019 return 0;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/**
1023 * skb_copy - create private copy of an sk_buff
1024 * @skb: buffer to copy
1025 * @gfp_mask: allocation priority
1026 *
1027 * Make a copy of both an &sk_buff and its data. This is used when the
1028 * caller wishes to modify the data and needs a private copy of the
1029 * data to alter. Returns %NULL on failure or the pointer to the buffer
1030 * on success. The returned buffer has a reference count of 1.
1031 *
1032 * As by-product this function converts non-linear &sk_buff to linear
1033 * one, so that &sk_buff becomes completely private and caller is allowed
1034 * to modify all the data of returned buffer. This means that this
1035 * function is not recommended for use in circumstances when only
1036 * header is going to be modified. Use pskb_copy() instead.
1037 */
1038
Al Virodd0fc662005-10-07 07:46:04 +01001039struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001041 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001042 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001043 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1044 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!n)
1047 return NULL;
1048
1049 /* Set the data pointer */
1050 skb_reserve(n, headerlen);
1051 /* Set the tail pointer and length */
1052 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1055 BUG();
1056
1057 copy_skb_header(n, skb);
1058 return n;
1059}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001060EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001063 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001065 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001067 * @fclone: if true allocate the copy of the skb from the fclone
1068 * cache instead of the head cache; it is recommended to set this
1069 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *
1071 * Make a copy of both an &sk_buff and part of its data, located
1072 * in header. Fragmented data remain shared. This is used when
1073 * the caller wishes to modify only header of &sk_buff and needs
1074 * private copy of the header to alter. Returns %NULL on failure
1075 * or the pointer to the buffer on success.
1076 * The returned buffer has a reference count of 1.
1077 */
1078
Octavian Purdilabad93e92014-06-12 01:36:26 +03001079struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1080 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Eric Dumazet117632e2011-12-03 21:39:53 +00001082 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001083 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1084 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!n)
1087 goto out;
1088
1089 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001090 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* Set the tail pointer and length */
1092 skb_put(n, skb_headlen(skb));
1093 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001094 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Herbert Xu25f484a2006-11-07 14:57:15 -08001096 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 n->data_len = skb->data_len;
1098 n->len = skb->len;
1099
1100 if (skb_shinfo(skb)->nr_frags) {
1101 int i;
1102
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001103 if (skb_orphan_frags(skb, gfp_mask)) {
1104 kfree_skb(n);
1105 n = NULL;
1106 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1109 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001110 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 skb_shinfo(n)->nr_frags = i;
1113 }
1114
David S. Miller21dc3302010-08-23 00:13:46 -07001115 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1117 skb_clone_fraglist(n);
1118 }
1119
1120 copy_skb_header(n, skb);
1121out:
1122 return n;
1123}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001124EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126/**
1127 * pskb_expand_head - reallocate header of &sk_buff
1128 * @skb: buffer to reallocate
1129 * @nhead: room to add at head
1130 * @ntail: room to add at tail
1131 * @gfp_mask: allocation priority
1132 *
Mathias Krausebc323832013-11-07 14:18:26 +01001133 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1134 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 * reference count of 1. Returns zero in the case of success or error,
1136 * if expansion failed. In the last case, &sk_buff is not changed.
1137 *
1138 * All the pointers pointing into skb header may change and must be
1139 * reloaded after call to this function.
1140 */
1141
Victor Fusco86a76ca2005-07-08 14:57:47 -07001142int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001143 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 int i;
1146 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001147 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 long off;
1149
Herbert Xu4edd87a2008-10-01 07:09:38 -07001150 BUG_ON(nhead < 0);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (skb_shared(skb))
1153 BUG();
1154
1155 size = SKB_DATA_ALIGN(size);
1156
Mel Gormanc93bdd02012-07-31 16:44:19 -07001157 if (skb_pfmemalloc(skb))
1158 gfp_mask |= __GFP_MEMALLOC;
1159 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1160 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!data)
1162 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001163 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001166 * optimized for the cases when header is void.
1167 */
1168 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1169
1170 memcpy((struct skb_shared_info *)(data + size),
1171 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001172 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Alexander Duyck3e245912012-05-04 14:26:51 +00001174 /*
1175 * if shinfo is shared we must drop the old head gracefully, but if it
1176 * is not we can just drop the old head and let the existing refcount
1177 * be since all we did is relocate the values
1178 */
1179 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001180 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001181 if (skb_orphan_frags(skb, gfp_mask))
1182 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001183 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001184 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Eric Dumazet1fd63042010-09-02 23:09:32 +00001186 if (skb_has_frag_list(skb))
1187 skb_clone_fraglist(skb);
1188
1189 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001190 } else {
1191 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 off = (data + nhead) - skb->head;
1194
1195 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001196 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001198#ifdef NET_SKBUFF_DATA_USES_OFFSET
1199 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001200 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001201#else
1202 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001203#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001204 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001205 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001207 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 skb->nohdr = 0;
1209 atomic_set(&skb_shinfo(skb)->dataref, 1);
1210 return 0;
1211
Shirley Maa6686f22011-07-06 12:22:12 +00001212nofrags:
1213 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214nodata:
1215 return -ENOMEM;
1216}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001217EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219/* Make private copy of skb with writable head and some headroom */
1220
1221struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1222{
1223 struct sk_buff *skb2;
1224 int delta = headroom - skb_headroom(skb);
1225
1226 if (delta <= 0)
1227 skb2 = pskb_copy(skb, GFP_ATOMIC);
1228 else {
1229 skb2 = skb_clone(skb, GFP_ATOMIC);
1230 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1231 GFP_ATOMIC)) {
1232 kfree_skb(skb2);
1233 skb2 = NULL;
1234 }
1235 }
1236 return skb2;
1237}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001238EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240/**
1241 * skb_copy_expand - copy and expand sk_buff
1242 * @skb: buffer to copy
1243 * @newheadroom: new free bytes at head
1244 * @newtailroom: new free bytes at tail
1245 * @gfp_mask: allocation priority
1246 *
1247 * Make a copy of both an &sk_buff and its data and while doing so
1248 * allocate additional space.
1249 *
1250 * This is used when the caller wishes to modify the data and needs a
1251 * private copy of the data to alter as well as more space for new fields.
1252 * Returns %NULL on failure or the pointer to the buffer
1253 * on success. The returned buffer has a reference count of 1.
1254 *
1255 * You must pass %GFP_ATOMIC as the allocation priority if this function
1256 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
1258struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001259 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001260 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 /*
1263 * Allocate the copy buffer
1264 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001265 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1266 gfp_mask, skb_alloc_rx_flag(skb),
1267 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001268 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int head_copy_len, head_copy_off;
1270
1271 if (!n)
1272 return NULL;
1273
1274 skb_reserve(n, newheadroom);
1275
1276 /* Set the tail pointer and length */
1277 skb_put(n, skb->len);
1278
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001279 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 head_copy_off = 0;
1281 if (newheadroom <= head_copy_len)
1282 head_copy_len = newheadroom;
1283 else
1284 head_copy_off = newheadroom - head_copy_len;
1285
1286 /* Copy the linear header and data. */
1287 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1288 skb->len + head_copy_len))
1289 BUG();
1290
1291 copy_skb_header(n, skb);
1292
Eric Dumazet030737b2013-10-19 11:42:54 -07001293 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return n;
1296}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001297EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299/**
1300 * skb_pad - zero pad the tail of an skb
1301 * @skb: buffer to pad
1302 * @pad: space to pad
1303 *
1304 * Ensure that a buffer is followed by a padding area that is zero
1305 * filled. Used by network drivers which may DMA or transfer data
1306 * beyond the buffer end onto the wire.
1307 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001308 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001310
Herbert Xu5b057c62006-06-23 02:06:41 -07001311int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Herbert Xu5b057c62006-06-23 02:06:41 -07001313 int err;
1314 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001317 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001321
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001322 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001323 if (likely(skb_cloned(skb) || ntail > 0)) {
1324 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1325 if (unlikely(err))
1326 goto free_skb;
1327 }
1328
1329 /* FIXME: The use of this function with non-linear skb's really needs
1330 * to be audited.
1331 */
1332 err = skb_linearize(skb);
1333 if (unlikely(err))
1334 goto free_skb;
1335
1336 memset(skb->data + skb->len, 0, pad);
1337 return 0;
1338
1339free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001341 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001342}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001343EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001344
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001345/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001346 * pskb_put - add data to the tail of a potentially fragmented buffer
1347 * @skb: start of the buffer to use
1348 * @tail: tail fragment of the buffer to use
1349 * @len: amount of data to add
1350 *
1351 * This function extends the used data area of the potentially
1352 * fragmented buffer. @tail must be the last fragment of @skb -- or
1353 * @skb itself. If this would exceed the total buffer size the kernel
1354 * will panic. A pointer to the first byte of the extra data is
1355 * returned.
1356 */
1357
1358unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1359{
1360 if (tail != skb) {
1361 skb->data_len += len;
1362 skb->len += len;
1363 }
1364 return skb_put(tail, len);
1365}
1366EXPORT_SYMBOL_GPL(pskb_put);
1367
1368/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001369 * skb_put - add data to a buffer
1370 * @skb: buffer to use
1371 * @len: amount of data to add
1372 *
1373 * This function extends the used data area of the buffer. If this would
1374 * exceed the total buffer size the kernel will panic. A pointer to the
1375 * first byte of the extra data is returned.
1376 */
1377unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1378{
1379 unsigned char *tmp = skb_tail_pointer(skb);
1380 SKB_LINEAR_ASSERT(skb);
1381 skb->tail += len;
1382 skb->len += len;
1383 if (unlikely(skb->tail > skb->end))
1384 skb_over_panic(skb, len, __builtin_return_address(0));
1385 return tmp;
1386}
1387EXPORT_SYMBOL(skb_put);
1388
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001389/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001390 * skb_push - add data to the start of a buffer
1391 * @skb: buffer to use
1392 * @len: amount of data to add
1393 *
1394 * This function extends the used data area of the buffer at the buffer
1395 * start. If this would exceed the total buffer headroom the kernel will
1396 * panic. A pointer to the first byte of the extra data is returned.
1397 */
1398unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1399{
1400 skb->data -= len;
1401 skb->len += len;
1402 if (unlikely(skb->data<skb->head))
1403 skb_under_panic(skb, len, __builtin_return_address(0));
1404 return skb->data;
1405}
1406EXPORT_SYMBOL(skb_push);
1407
1408/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001409 * skb_pull - remove data from the start of a buffer
1410 * @skb: buffer to use
1411 * @len: amount of data to remove
1412 *
1413 * This function removes data from the start of a buffer, returning
1414 * the memory to the headroom. A pointer to the next data in the buffer
1415 * is returned. Once the data has been pulled future pushes will overwrite
1416 * the old data.
1417 */
1418unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1419{
David S. Miller47d29642010-05-02 02:21:44 -07001420 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001421}
1422EXPORT_SYMBOL(skb_pull);
1423
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001424/**
1425 * skb_trim - remove end from a buffer
1426 * @skb: buffer to alter
1427 * @len: new length
1428 *
1429 * Cut the length of a buffer down by removing data from the tail. If
1430 * the buffer is already under the length specified it is not modified.
1431 * The skb must be linear.
1432 */
1433void skb_trim(struct sk_buff *skb, unsigned int len)
1434{
1435 if (skb->len > len)
1436 __skb_trim(skb, len);
1437}
1438EXPORT_SYMBOL(skb_trim);
1439
Herbert Xu3cc0e872006-06-09 16:13:38 -07001440/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
1442
Herbert Xu3cc0e872006-06-09 16:13:38 -07001443int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Herbert Xu27b437c2006-07-13 19:26:39 -07001445 struct sk_buff **fragp;
1446 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int offset = skb_headlen(skb);
1448 int nfrags = skb_shinfo(skb)->nr_frags;
1449 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001450 int err;
1451
1452 if (skb_cloned(skb) &&
1453 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001456 i = 0;
1457 if (offset >= len)
1458 goto drop_pages;
1459
1460 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001461 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001462
1463 if (end < len) {
1464 offset = end;
1465 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001467
Eric Dumazet9e903e02011-10-18 21:00:24 +00001468 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001469
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001470drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001471 skb_shinfo(skb)->nr_frags = i;
1472
1473 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001474 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001475
David S. Miller21dc3302010-08-23 00:13:46 -07001476 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001477 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001478 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
Herbert Xu27b437c2006-07-13 19:26:39 -07001481 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1482 fragp = &frag->next) {
1483 int end = offset + frag->len;
1484
1485 if (skb_shared(frag)) {
1486 struct sk_buff *nfrag;
1487
1488 nfrag = skb_clone(frag, GFP_ATOMIC);
1489 if (unlikely(!nfrag))
1490 return -ENOMEM;
1491
1492 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001493 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001494 frag = nfrag;
1495 *fragp = frag;
1496 }
1497
1498 if (end < len) {
1499 offset = end;
1500 continue;
1501 }
1502
1503 if (end > len &&
1504 unlikely((err = pskb_trim(frag, len - offset))))
1505 return err;
1506
1507 if (frag->next)
1508 skb_drop_list(&frag->next);
1509 break;
1510 }
1511
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001512done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001513 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 skb->data_len -= skb->len - len;
1515 skb->len = len;
1516 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001517 skb->len = len;
1518 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001519 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
1522 return 0;
1523}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001524EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526/**
1527 * __pskb_pull_tail - advance tail of skb header
1528 * @skb: buffer to reallocate
1529 * @delta: number of bytes to advance tail
1530 *
1531 * The function makes a sense only on a fragmented &sk_buff,
1532 * it expands header moving its tail forward and copying necessary
1533 * data from fragmented part.
1534 *
1535 * &sk_buff MUST have reference count of 1.
1536 *
1537 * Returns %NULL (and &sk_buff does not change) if pull failed
1538 * or value of new tail of skb in the case of success.
1539 *
1540 * All the pointers pointing into skb header may change and must be
1541 * reloaded after call to this function.
1542 */
1543
1544/* Moves tail of skb head forward, copying data from fragmented part,
1545 * when it is necessary.
1546 * 1. It may fail due to malloc failure.
1547 * 2. It may change skb pointers.
1548 *
1549 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1550 */
1551unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1552{
1553 /* If skb has not enough free space at tail, get new one
1554 * plus 128 bytes for future expansions. If we have enough
1555 * room at tail, reallocate without expansion only if skb is cloned.
1556 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001557 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 if (eat > 0 || skb_cloned(skb)) {
1560 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1561 GFP_ATOMIC))
1562 return NULL;
1563 }
1564
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001565 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 BUG();
1567
1568 /* Optimization: no fragments, no reasons to preestimate
1569 * size of pulled pages. Superb.
1570 */
David S. Miller21dc3302010-08-23 00:13:46 -07001571 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 goto pull_pages;
1573
1574 /* Estimate size of pulled pages. */
1575 eat = delta;
1576 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001577 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1578
1579 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001581 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
1584 /* If we need update frag list, we are in troubles.
1585 * Certainly, it possible to add an offset to skb data,
1586 * but taking into account that pulling is expected to
1587 * be very rare operation, it is worth to fight against
1588 * further bloating skb head and crucify ourselves here instead.
1589 * Pure masohism, indeed. 8)8)
1590 */
1591 if (eat) {
1592 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1593 struct sk_buff *clone = NULL;
1594 struct sk_buff *insp = NULL;
1595
1596 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001597 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (list->len <= eat) {
1600 /* Eaten as whole. */
1601 eat -= list->len;
1602 list = list->next;
1603 insp = list;
1604 } else {
1605 /* Eaten partially. */
1606
1607 if (skb_shared(list)) {
1608 /* Sucks! We need to fork list. :-( */
1609 clone = skb_clone(list, GFP_ATOMIC);
1610 if (!clone)
1611 return NULL;
1612 insp = list->next;
1613 list = clone;
1614 } else {
1615 /* This may be pulled without
1616 * problems. */
1617 insp = list;
1618 }
1619 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001620 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return NULL;
1622 }
1623 break;
1624 }
1625 } while (eat);
1626
1627 /* Free pulled out fragments. */
1628 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1629 skb_shinfo(skb)->frag_list = list->next;
1630 kfree_skb(list);
1631 }
1632 /* And insert new clone at head. */
1633 if (clone) {
1634 clone->next = list;
1635 skb_shinfo(skb)->frag_list = clone;
1636 }
1637 }
1638 /* Success! Now we may commit changes to skb data. */
1639
1640pull_pages:
1641 eat = delta;
1642 k = 0;
1643 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001644 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1645
1646 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001647 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001648 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 } else {
1650 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1651 if (eat) {
1652 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001653 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 eat = 0;
1655 }
1656 k++;
1657 }
1658 }
1659 skb_shinfo(skb)->nr_frags = k;
1660
1661 skb->tail += delta;
1662 skb->data_len -= delta;
1663
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001664 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001666EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Eric Dumazet22019b12011-07-29 18:37:31 +00001668/**
1669 * skb_copy_bits - copy bits from skb to kernel buffer
1670 * @skb: source skb
1671 * @offset: offset in source
1672 * @to: destination buffer
1673 * @len: number of bytes to copy
1674 *
1675 * Copy the specified number of bytes from the source skb to the
1676 * destination buffer.
1677 *
1678 * CAUTION ! :
1679 * If its prototype is ever changed,
1680 * check arch/{*}/net/{*}.S files,
1681 * since it is called from BPF assembly code.
1682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1684{
David S. Miller1a028e52007-04-27 15:21:23 -07001685 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001686 struct sk_buff *frag_iter;
1687 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (offset > (int)skb->len - len)
1690 goto fault;
1691
1692 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001693 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (copy > len)
1695 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001696 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if ((len -= copy) == 0)
1698 return 0;
1699 offset += copy;
1700 to += copy;
1701 }
1702
1703 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001704 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001705 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001707 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001708
Eric Dumazet51c56b02012-04-05 11:35:15 +02001709 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if ((copy = end - offset) > 0) {
1711 u8 *vaddr;
1712
1713 if (copy > len)
1714 copy = len;
1715
Eric Dumazet51c56b02012-04-05 11:35:15 +02001716 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001718 vaddr + f->page_offset + offset - start,
1719 copy);
1720 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 if ((len -= copy) == 0)
1723 return 0;
1724 offset += copy;
1725 to += copy;
1726 }
David S. Miller1a028e52007-04-27 15:21:23 -07001727 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
David S. Millerfbb398a2009-06-09 00:18:59 -07001730 skb_walk_frags(skb, frag_iter) {
1731 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
David S. Millerfbb398a2009-06-09 00:18:59 -07001733 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
David S. Millerfbb398a2009-06-09 00:18:59 -07001735 end = start + frag_iter->len;
1736 if ((copy = end - offset) > 0) {
1737 if (copy > len)
1738 copy = len;
1739 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1740 goto fault;
1741 if ((len -= copy) == 0)
1742 return 0;
1743 offset += copy;
1744 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001746 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Shirley Maa6686f22011-07-06 12:22:12 +00001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!len)
1750 return 0;
1751
1752fault:
1753 return -EFAULT;
1754}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001755EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jens Axboe9c55e012007-11-06 23:30:13 -08001757/*
1758 * Callback from splice_to_pipe(), if we need to release some pages
1759 * at the end of the spd in case we error'ed out in filling the pipe.
1760 */
1761static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1762{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001763 put_page(spd->pages[i]);
1764}
Jens Axboe9c55e012007-11-06 23:30:13 -08001765
David S. Millera108d5f2012-04-23 23:06:11 -04001766static struct page *linear_to_page(struct page *page, unsigned int *len,
1767 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001768 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001769{
Eric Dumazet5640f762012-09-23 23:04:42 +00001770 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001771
Eric Dumazet5640f762012-09-23 23:04:42 +00001772 if (!sk_page_frag_refill(sk, pfrag))
1773 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001774
Eric Dumazet5640f762012-09-23 23:04:42 +00001775 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001776
Eric Dumazet5640f762012-09-23 23:04:42 +00001777 memcpy(page_address(pfrag->page) + pfrag->offset,
1778 page_address(page) + *offset, *len);
1779 *offset = pfrag->offset;
1780 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001781
Eric Dumazet5640f762012-09-23 23:04:42 +00001782 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001783}
1784
Eric Dumazet41c73a02012-04-22 12:26:16 +00001785static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1786 struct page *page,
1787 unsigned int offset)
1788{
1789 return spd->nr_pages &&
1790 spd->pages[spd->nr_pages - 1] == page &&
1791 (spd->partial[spd->nr_pages - 1].offset +
1792 spd->partial[spd->nr_pages - 1].len == offset);
1793}
1794
Jens Axboe9c55e012007-11-06 23:30:13 -08001795/*
1796 * Fill page/offset/length into spd, if it can hold more pages.
1797 */
David S. Millera108d5f2012-04-23 23:06:11 -04001798static bool spd_fill_page(struct splice_pipe_desc *spd,
1799 struct pipe_inode_info *pipe, struct page *page,
1800 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001801 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001802 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001803{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001804 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001805 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001806
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001807 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001808 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001809 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001810 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001811 }
1812 if (spd_can_coalesce(spd, page, offset)) {
1813 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001814 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001815 }
1816 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001817 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001818 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001819 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001820 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001821
David S. Millera108d5f2012-04-23 23:06:11 -04001822 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001823}
1824
David S. Millera108d5f2012-04-23 23:06:11 -04001825static bool __splice_segment(struct page *page, unsigned int poff,
1826 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001827 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001828 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001829 struct sock *sk,
1830 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001831{
1832 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001833 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001834
1835 /* skip this segment if already processed */
1836 if (*off >= plen) {
1837 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001838 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001839 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001840
Octavian Purdila2870c432008-07-15 00:49:11 -07001841 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001842 poff += *off;
1843 plen -= *off;
1844 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001845
Eric Dumazet18aafc62013-01-11 14:46:37 +00001846 do {
1847 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001848
Eric Dumazet18aafc62013-01-11 14:46:37 +00001849 if (spd_fill_page(spd, pipe, page, &flen, poff,
1850 linear, sk))
1851 return true;
1852 poff += flen;
1853 plen -= flen;
1854 *len -= flen;
1855 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001856
David S. Millera108d5f2012-04-23 23:06:11 -04001857 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001858}
1859
1860/*
David S. Millera108d5f2012-04-23 23:06:11 -04001861 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001862 * pipe is full or if we already spliced the requested length.
1863 */
David S. Millera108d5f2012-04-23 23:06:11 -04001864static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1865 unsigned int *offset, unsigned int *len,
1866 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001867{
1868 int seg;
1869
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001870 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001871 * If skb->head_frag is set, this 'linear' part is backed by a
1872 * fragment, and if the head is not shared with any clones then
1873 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001874 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001875 if (__splice_segment(virt_to_page(skb->data),
1876 (unsigned long) skb->data & (PAGE_SIZE - 1),
1877 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001878 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001879 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001880 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001881 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001882
1883 /*
1884 * then map the fragments
1885 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001886 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1887 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1888
Ian Campbellea2ab692011-08-22 23:44:58 +00001889 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001890 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001891 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001892 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001893 }
1894
David S. Millera108d5f2012-04-23 23:06:11 -04001895 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001896}
1897
1898/*
1899 * Map data from the skb to a pipe. Should handle both the linear part,
1900 * the fragments, and the frag list. It does NOT handle frag lists within
1901 * the frag list, if such a thing exists. We'd probably need to recurse to
1902 * handle that cleanly.
1903 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001904int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001905 struct pipe_inode_info *pipe, unsigned int tlen,
1906 unsigned int flags)
1907{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001908 struct partial_page partial[MAX_SKB_FRAGS];
1909 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001910 struct splice_pipe_desc spd = {
1911 .pages = pages,
1912 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001913 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001914 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001915 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001916 .spd_release = sock_spd_release,
1917 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001918 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001919 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001920 int ret = 0;
1921
Jens Axboe9c55e012007-11-06 23:30:13 -08001922 /*
1923 * __skb_splice_bits() only fails if the output has no room left,
1924 * so no point in going over the frag_list for the error case.
1925 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001926 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001927 goto done;
1928 else if (!tlen)
1929 goto done;
1930
1931 /*
1932 * now see if we have a frag_list to map
1933 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001934 skb_walk_frags(skb, frag_iter) {
1935 if (!tlen)
1936 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001937 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001938 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001939 }
1940
1941done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001942 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001943 /*
1944 * Drop the socket lock, otherwise we have reverse
1945 * locking dependencies between sk_lock and i_mutex
1946 * here as compared to sendfile(). We enter here
1947 * with the socket lock held, and splice_to_pipe() will
1948 * grab the pipe inode lock. For sendfile() emulation,
1949 * we call into ->sendpage() with the i_mutex lock held
1950 * and networking will grab the socket lock.
1951 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001952 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001953 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001954 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001955 }
1956
Jens Axboe35f3d142010-05-20 10:43:18 +02001957 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001958}
1959
Herbert Xu357b40a2005-04-19 22:30:14 -07001960/**
1961 * skb_store_bits - store bits from kernel buffer to skb
1962 * @skb: destination buffer
1963 * @offset: offset in destination
1964 * @from: source buffer
1965 * @len: number of bytes to copy
1966 *
1967 * Copy the specified number of bytes from the source buffer to the
1968 * destination skb. This function handles all the messy bits of
1969 * traversing fragment lists and such.
1970 */
1971
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001972int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001973{
David S. Miller1a028e52007-04-27 15:21:23 -07001974 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001975 struct sk_buff *frag_iter;
1976 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001977
1978 if (offset > (int)skb->len - len)
1979 goto fault;
1980
David S. Miller1a028e52007-04-27 15:21:23 -07001981 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001982 if (copy > len)
1983 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001984 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001985 if ((len -= copy) == 0)
1986 return 0;
1987 offset += copy;
1988 from += copy;
1989 }
1990
1991 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1992 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001993 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001994
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001995 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001996
Eric Dumazet9e903e02011-10-18 21:00:24 +00001997 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001998 if ((copy = end - offset) > 0) {
1999 u8 *vaddr;
2000
2001 if (copy > len)
2002 copy = len;
2003
Eric Dumazet51c56b02012-04-05 11:35:15 +02002004 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002005 memcpy(vaddr + frag->page_offset + offset - start,
2006 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002007 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002008
2009 if ((len -= copy) == 0)
2010 return 0;
2011 offset += copy;
2012 from += copy;
2013 }
David S. Miller1a028e52007-04-27 15:21:23 -07002014 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002015 }
2016
David S. Millerfbb398a2009-06-09 00:18:59 -07002017 skb_walk_frags(skb, frag_iter) {
2018 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002019
David S. Millerfbb398a2009-06-09 00:18:59 -07002020 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002021
David S. Millerfbb398a2009-06-09 00:18:59 -07002022 end = start + frag_iter->len;
2023 if ((copy = end - offset) > 0) {
2024 if (copy > len)
2025 copy = len;
2026 if (skb_store_bits(frag_iter, offset - start,
2027 from, copy))
2028 goto fault;
2029 if ((len -= copy) == 0)
2030 return 0;
2031 offset += copy;
2032 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002033 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002034 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002035 }
2036 if (!len)
2037 return 0;
2038
2039fault:
2040 return -EFAULT;
2041}
Herbert Xu357b40a2005-04-19 22:30:14 -07002042EXPORT_SYMBOL(skb_store_bits);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002045__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2046 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
David S. Miller1a028e52007-04-27 15:21:23 -07002048 int start = skb_headlen(skb);
2049 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002050 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int pos = 0;
2052
2053 /* Checksum header. */
2054 if (copy > 0) {
2055 if (copy > len)
2056 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002057 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if ((len -= copy) == 0)
2059 return csum;
2060 offset += copy;
2061 pos = copy;
2062 }
2063
2064 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002065 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002066 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002068 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002069
Eric Dumazet51c56b02012-04-05 11:35:15 +02002070 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002072 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (copy > len)
2076 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002077 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002078 csum2 = ops->update(vaddr + frag->page_offset +
2079 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002080 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002081 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (!(len -= copy))
2083 return csum;
2084 offset += copy;
2085 pos += copy;
2086 }
David S. Miller1a028e52007-04-27 15:21:23 -07002087 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
David S. Millerfbb398a2009-06-09 00:18:59 -07002090 skb_walk_frags(skb, frag_iter) {
2091 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
David S. Millerfbb398a2009-06-09 00:18:59 -07002093 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
David S. Millerfbb398a2009-06-09 00:18:59 -07002095 end = start + frag_iter->len;
2096 if ((copy = end - offset) > 0) {
2097 __wsum csum2;
2098 if (copy > len)
2099 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002100 csum2 = __skb_checksum(frag_iter, offset - start,
2101 copy, 0, ops);
2102 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002103 if ((len -= copy) == 0)
2104 return csum;
2105 offset += copy;
2106 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002108 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002110 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 return csum;
2113}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002114EXPORT_SYMBOL(__skb_checksum);
2115
2116__wsum skb_checksum(const struct sk_buff *skb, int offset,
2117 int len, __wsum csum)
2118{
2119 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002120 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002121 .combine = csum_block_add_ext,
2122 };
2123
2124 return __skb_checksum(skb, offset, len, csum, &ops);
2125}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002126EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128/* Both of above in one bottle. */
2129
Al Viro81d77662006-11-14 21:37:33 -08002130__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2131 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
David S. Miller1a028e52007-04-27 15:21:23 -07002133 int start = skb_headlen(skb);
2134 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002135 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int pos = 0;
2137
2138 /* Copy header. */
2139 if (copy > 0) {
2140 if (copy > len)
2141 copy = len;
2142 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2143 copy, csum);
2144 if ((len -= copy) == 0)
2145 return csum;
2146 offset += copy;
2147 to += copy;
2148 pos = copy;
2149 }
2150
2151 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002152 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002154 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002155
Eric Dumazet9e903e02011-10-18 21:00:24 +00002156 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002158 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 u8 *vaddr;
2160 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2161
2162 if (copy > len)
2163 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002164 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002166 frag->page_offset +
2167 offset - start, to,
2168 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002169 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 csum = csum_block_add(csum, csum2, pos);
2171 if (!(len -= copy))
2172 return csum;
2173 offset += copy;
2174 to += copy;
2175 pos += copy;
2176 }
David S. Miller1a028e52007-04-27 15:21:23 -07002177 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
2179
David S. Millerfbb398a2009-06-09 00:18:59 -07002180 skb_walk_frags(skb, frag_iter) {
2181 __wsum csum2;
2182 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
David S. Millerfbb398a2009-06-09 00:18:59 -07002184 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
David S. Millerfbb398a2009-06-09 00:18:59 -07002186 end = start + frag_iter->len;
2187 if ((copy = end - offset) > 0) {
2188 if (copy > len)
2189 copy = len;
2190 csum2 = skb_copy_and_csum_bits(frag_iter,
2191 offset - start,
2192 to, copy, 0);
2193 csum = csum_block_add(csum, csum2, pos);
2194 if ((len -= copy) == 0)
2195 return csum;
2196 offset += copy;
2197 to += copy;
2198 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002200 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002202 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return csum;
2204}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002205EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Grafaf2806f2013-12-13 15:22:17 +01002207 /**
2208 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2209 * @from: source buffer
2210 *
2211 * Calculates the amount of linear headroom needed in the 'to' skb passed
2212 * into skb_zerocopy().
2213 */
2214unsigned int
2215skb_zerocopy_headlen(const struct sk_buff *from)
2216{
2217 unsigned int hlen = 0;
2218
2219 if (!from->head_frag ||
2220 skb_headlen(from) < L1_CACHE_BYTES ||
2221 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2222 hlen = skb_headlen(from);
2223
2224 if (skb_has_frag_list(from))
2225 hlen = from->len;
2226
2227 return hlen;
2228}
2229EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2230
2231/**
2232 * skb_zerocopy - Zero copy skb to skb
2233 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002234 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002235 * @len: number of bytes to copy from source buffer
2236 * @hlen: size of linear headroom in destination buffer
2237 *
2238 * Copies up to `len` bytes from `from` to `to` by creating references
2239 * to the frags in the source buffer.
2240 *
2241 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2242 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002243 *
2244 * Return value:
2245 * 0: everything is OK
2246 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2247 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002248 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002249int
2250skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002251{
2252 int i, j = 0;
2253 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002254 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002255 struct page *page;
2256 unsigned int offset;
2257
2258 BUG_ON(!from->head_frag && !hlen);
2259
2260 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002261 if (len <= skb_tailroom(to))
2262 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002263
2264 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002265 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2266 if (unlikely(ret))
2267 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002268 len -= hlen;
2269 } else {
2270 plen = min_t(int, skb_headlen(from), len);
2271 if (plen) {
2272 page = virt_to_head_page(from->head);
2273 offset = from->data - (unsigned char *)page_address(page);
2274 __skb_fill_page_desc(to, 0, page, offset, plen);
2275 get_page(page);
2276 j = 1;
2277 len -= plen;
2278 }
2279 }
2280
2281 to->truesize += len + plen;
2282 to->len += len + plen;
2283 to->data_len += len + plen;
2284
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002285 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2286 skb_tx_error(from);
2287 return -ENOMEM;
2288 }
2289
Thomas Grafaf2806f2013-12-13 15:22:17 +01002290 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2291 if (!len)
2292 break;
2293 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2294 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2295 len -= skb_shinfo(to)->frags[j].size;
2296 skb_frag_ref(to, j);
2297 j++;
2298 }
2299 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002300
2301 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002302}
2303EXPORT_SYMBOL_GPL(skb_zerocopy);
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2306{
Al Virod3bc23e2006-11-14 21:24:49 -08002307 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 long csstart;
2309
Patrick McHardy84fa7932006-08-29 16:44:56 -07002310 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002311 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 else
2313 csstart = skb_headlen(skb);
2314
Kris Katterjohn09a62662006-01-08 22:24:28 -08002315 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002317 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 csum = 0;
2320 if (csstart != skb->len)
2321 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2322 skb->len - csstart, 0);
2323
Patrick McHardy84fa7932006-08-29 16:44:56 -07002324 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002325 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Al Virod3bc23e2006-11-14 21:24:49 -08002327 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002330EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332/**
2333 * skb_dequeue - remove from the head of the queue
2334 * @list: list to dequeue from
2335 *
2336 * Remove the head of the list. The list lock is taken so the function
2337 * may be used safely with other locking list functions. The head item is
2338 * returned or %NULL if the list is empty.
2339 */
2340
2341struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2342{
2343 unsigned long flags;
2344 struct sk_buff *result;
2345
2346 spin_lock_irqsave(&list->lock, flags);
2347 result = __skb_dequeue(list);
2348 spin_unlock_irqrestore(&list->lock, flags);
2349 return result;
2350}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002351EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353/**
2354 * skb_dequeue_tail - remove from the tail of the queue
2355 * @list: list to dequeue from
2356 *
2357 * Remove the tail of the list. The list lock is taken so the function
2358 * may be used safely with other locking list functions. The tail item is
2359 * returned or %NULL if the list is empty.
2360 */
2361struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2362{
2363 unsigned long flags;
2364 struct sk_buff *result;
2365
2366 spin_lock_irqsave(&list->lock, flags);
2367 result = __skb_dequeue_tail(list);
2368 spin_unlock_irqrestore(&list->lock, flags);
2369 return result;
2370}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002371EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373/**
2374 * skb_queue_purge - empty a list
2375 * @list: list to empty
2376 *
2377 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2378 * the list and one reference dropped. This function takes the list
2379 * lock and is atomic with respect to other list locking functions.
2380 */
2381void skb_queue_purge(struct sk_buff_head *list)
2382{
2383 struct sk_buff *skb;
2384 while ((skb = skb_dequeue(list)) != NULL)
2385 kfree_skb(skb);
2386}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002387EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389/**
2390 * skb_queue_head - queue a buffer at the list head
2391 * @list: list to use
2392 * @newsk: buffer to queue
2393 *
2394 * Queue a buffer at the start of the list. This function takes the
2395 * list lock and can be used safely with other locking &sk_buff functions
2396 * safely.
2397 *
2398 * A buffer cannot be placed on two lists at the same time.
2399 */
2400void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2401{
2402 unsigned long flags;
2403
2404 spin_lock_irqsave(&list->lock, flags);
2405 __skb_queue_head(list, newsk);
2406 spin_unlock_irqrestore(&list->lock, flags);
2407}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002408EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410/**
2411 * skb_queue_tail - queue a buffer at the list tail
2412 * @list: list to use
2413 * @newsk: buffer to queue
2414 *
2415 * Queue a buffer at the tail of the list. This function takes the
2416 * list lock and can be used safely with other locking &sk_buff functions
2417 * safely.
2418 *
2419 * A buffer cannot be placed on two lists at the same time.
2420 */
2421void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2422{
2423 unsigned long flags;
2424
2425 spin_lock_irqsave(&list->lock, flags);
2426 __skb_queue_tail(list, newsk);
2427 spin_unlock_irqrestore(&list->lock, flags);
2428}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002429EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431/**
2432 * skb_unlink - remove a buffer from a list
2433 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002434 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 *
David S. Miller8728b832005-08-09 19:25:21 -07002436 * Remove a packet from a list. The list locks are taken and this
2437 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 *
David S. Miller8728b832005-08-09 19:25:21 -07002439 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
David S. Miller8728b832005-08-09 19:25:21 -07002441void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
David S. Miller8728b832005-08-09 19:25:21 -07002443 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
David S. Miller8728b832005-08-09 19:25:21 -07002445 spin_lock_irqsave(&list->lock, flags);
2446 __skb_unlink(skb, list);
2447 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002449EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/**
2452 * skb_append - append a buffer
2453 * @old: buffer to insert after
2454 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002455 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
2457 * Place a packet after a given packet in a list. The list locks are taken
2458 * and this function is atomic with respect to other list locked calls.
2459 * A buffer cannot be placed on two lists at the same time.
2460 */
David S. Miller8728b832005-08-09 19:25:21 -07002461void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 unsigned long flags;
2464
David S. Miller8728b832005-08-09 19:25:21 -07002465 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002466 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002467 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002469EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471/**
2472 * skb_insert - insert a buffer
2473 * @old: buffer to insert before
2474 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002475 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 *
David S. Miller8728b832005-08-09 19:25:21 -07002477 * Place a packet before a given packet in a list. The list locks are
2478 * taken and this function is atomic with respect to other list locked
2479 * calls.
2480 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 * A buffer cannot be placed on two lists at the same time.
2482 */
David S. Miller8728b832005-08-09 19:25:21 -07002483void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
2485 unsigned long flags;
2486
David S. Miller8728b832005-08-09 19:25:21 -07002487 spin_lock_irqsave(&list->lock, flags);
2488 __skb_insert(newsk, old->prev, old, list);
2489 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002491EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493static inline void skb_split_inside_header(struct sk_buff *skb,
2494 struct sk_buff* skb1,
2495 const u32 len, const int pos)
2496{
2497 int i;
2498
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002499 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2500 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /* And move data appendix as is. */
2502 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2503 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2504
2505 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2506 skb_shinfo(skb)->nr_frags = 0;
2507 skb1->data_len = skb->data_len;
2508 skb1->len += skb1->data_len;
2509 skb->data_len = 0;
2510 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002511 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static inline void skb_split_no_header(struct sk_buff *skb,
2515 struct sk_buff* skb1,
2516 const u32 len, int pos)
2517{
2518 int i, k = 0;
2519 const int nfrags = skb_shinfo(skb)->nr_frags;
2520
2521 skb_shinfo(skb)->nr_frags = 0;
2522 skb1->len = skb1->data_len = skb->len - len;
2523 skb->len = len;
2524 skb->data_len = len - pos;
2525
2526 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002527 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 if (pos + size > len) {
2530 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2531
2532 if (pos < len) {
2533 /* Split frag.
2534 * We have two variants in this case:
2535 * 1. Move all the frag to the second
2536 * part, if it is possible. F.e.
2537 * this approach is mandatory for TUX,
2538 * where splitting is expensive.
2539 * 2. Split is accurately. We make this.
2540 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002541 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002543 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2544 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 skb_shinfo(skb)->nr_frags++;
2546 }
2547 k++;
2548 } else
2549 skb_shinfo(skb)->nr_frags++;
2550 pos += size;
2551 }
2552 skb_shinfo(skb1)->nr_frags = k;
2553}
2554
2555/**
2556 * skb_split - Split fragmented skb to two parts at length len.
2557 * @skb: the buffer to split
2558 * @skb1: the buffer to receive the second part
2559 * @len: new length for skb
2560 */
2561void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2562{
2563 int pos = skb_headlen(skb);
2564
Amerigo Wang68534c62013-02-19 22:51:30 +00002565 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (len < pos) /* Split line is inside header. */
2567 skb_split_inside_header(skb, skb1, len, pos);
2568 else /* Second chunk has no header, nothing to copy. */
2569 skb_split_no_header(skb, skb1, len, pos);
2570}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002571EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002573/* Shifting from/to a cloned skb is a no-go.
2574 *
2575 * Caller cannot keep skb_shinfo related pointers past calling here!
2576 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002577static int skb_prepare_for_shift(struct sk_buff *skb)
2578{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002579 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002580}
2581
2582/**
2583 * skb_shift - Shifts paged data partially from skb to another
2584 * @tgt: buffer into which tail data gets added
2585 * @skb: buffer from which the paged data comes from
2586 * @shiftlen: shift up to this many bytes
2587 *
2588 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002589 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002590 * It's up to caller to free skb if everything was shifted.
2591 *
2592 * If @tgt runs out of frags, the whole operation is aborted.
2593 *
2594 * Skb cannot include anything else but paged data while tgt is allowed
2595 * to have non-paged data as well.
2596 *
2597 * TODO: full sized shift could be optimized but that would need
2598 * specialized skb free'er to handle frags without up-to-date nr_frags.
2599 */
2600int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2601{
2602 int from, to, merge, todo;
2603 struct skb_frag_struct *fragfrom, *fragto;
2604
2605 BUG_ON(shiftlen > skb->len);
2606 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2607
2608 todo = shiftlen;
2609 from = 0;
2610 to = skb_shinfo(tgt)->nr_frags;
2611 fragfrom = &skb_shinfo(skb)->frags[from];
2612
2613 /* Actual merge is delayed until the point when we know we can
2614 * commit all, so that we don't have to undo partial changes
2615 */
2616 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002617 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2618 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002619 merge = -1;
2620 } else {
2621 merge = to - 1;
2622
Eric Dumazet9e903e02011-10-18 21:00:24 +00002623 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002624 if (todo < 0) {
2625 if (skb_prepare_for_shift(skb) ||
2626 skb_prepare_for_shift(tgt))
2627 return 0;
2628
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002629 /* All previous frag pointers might be stale! */
2630 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002631 fragto = &skb_shinfo(tgt)->frags[merge];
2632
Eric Dumazet9e903e02011-10-18 21:00:24 +00002633 skb_frag_size_add(fragto, shiftlen);
2634 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002635 fragfrom->page_offset += shiftlen;
2636
2637 goto onlymerged;
2638 }
2639
2640 from++;
2641 }
2642
2643 /* Skip full, not-fitting skb to avoid expensive operations */
2644 if ((shiftlen == skb->len) &&
2645 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2646 return 0;
2647
2648 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2649 return 0;
2650
2651 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2652 if (to == MAX_SKB_FRAGS)
2653 return 0;
2654
2655 fragfrom = &skb_shinfo(skb)->frags[from];
2656 fragto = &skb_shinfo(tgt)->frags[to];
2657
Eric Dumazet9e903e02011-10-18 21:00:24 +00002658 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002659 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002660 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002661 from++;
2662 to++;
2663
2664 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002665 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002666 fragto->page = fragfrom->page;
2667 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002668 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002669
2670 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002671 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002672 todo = 0;
2673
2674 to++;
2675 break;
2676 }
2677 }
2678
2679 /* Ready to "commit" this state change to tgt */
2680 skb_shinfo(tgt)->nr_frags = to;
2681
2682 if (merge >= 0) {
2683 fragfrom = &skb_shinfo(skb)->frags[0];
2684 fragto = &skb_shinfo(tgt)->frags[merge];
2685
Eric Dumazet9e903e02011-10-18 21:00:24 +00002686 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002687 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002688 }
2689
2690 /* Reposition in the original skb */
2691 to = 0;
2692 while (from < skb_shinfo(skb)->nr_frags)
2693 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2694 skb_shinfo(skb)->nr_frags = to;
2695
2696 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2697
2698onlymerged:
2699 /* Most likely the tgt won't ever need its checksum anymore, skb on
2700 * the other hand might need it if it needs to be resent
2701 */
2702 tgt->ip_summed = CHECKSUM_PARTIAL;
2703 skb->ip_summed = CHECKSUM_PARTIAL;
2704
2705 /* Yak, is it really working this way? Some helper please? */
2706 skb->len -= shiftlen;
2707 skb->data_len -= shiftlen;
2708 skb->truesize -= shiftlen;
2709 tgt->len += shiftlen;
2710 tgt->data_len += shiftlen;
2711 tgt->truesize += shiftlen;
2712
2713 return shiftlen;
2714}
2715
Thomas Graf677e90e2005-06-23 20:59:51 -07002716/**
2717 * skb_prepare_seq_read - Prepare a sequential read of skb data
2718 * @skb: the buffer to read
2719 * @from: lower offset of data to be read
2720 * @to: upper offset of data to be read
2721 * @st: state variable
2722 *
2723 * Initializes the specified state variable. Must be called before
2724 * invoking skb_seq_read() for the first time.
2725 */
2726void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2727 unsigned int to, struct skb_seq_state *st)
2728{
2729 st->lower_offset = from;
2730 st->upper_offset = to;
2731 st->root_skb = st->cur_skb = skb;
2732 st->frag_idx = st->stepped_offset = 0;
2733 st->frag_data = NULL;
2734}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002735EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002736
2737/**
2738 * skb_seq_read - Sequentially read skb data
2739 * @consumed: number of bytes consumed by the caller so far
2740 * @data: destination pointer for data to be returned
2741 * @st: state variable
2742 *
Mathias Krausebc323832013-11-07 14:18:26 +01002743 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002744 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002745 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002746 * of the block or 0 if the end of the skb data or the upper
2747 * offset has been reached.
2748 *
2749 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002750 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002751 * of bytes already consumed and the next call to
2752 * skb_seq_read() will return the remaining part of the block.
2753 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002754 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002755 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002756 * reads of potentially non linear data.
2757 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002758 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002759 * at the moment, state->root_skb could be replaced with
2760 * a stack for this purpose.
2761 */
2762unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2763 struct skb_seq_state *st)
2764{
2765 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2766 skb_frag_t *frag;
2767
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002768 if (unlikely(abs_offset >= st->upper_offset)) {
2769 if (st->frag_data) {
2770 kunmap_atomic(st->frag_data);
2771 st->frag_data = NULL;
2772 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002773 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002774 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002775
2776next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002777 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002778
Thomas Chenault995b3372009-05-18 21:43:27 -07002779 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002780 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002781 return block_limit - abs_offset;
2782 }
2783
2784 if (st->frag_idx == 0 && !st->frag_data)
2785 st->stepped_offset += skb_headlen(st->cur_skb);
2786
2787 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2788 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002789 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002790
2791 if (abs_offset < block_limit) {
2792 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002793 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002794
2795 *data = (u8 *) st->frag_data + frag->page_offset +
2796 (abs_offset - st->stepped_offset);
2797
2798 return block_limit - abs_offset;
2799 }
2800
2801 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002802 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002803 st->frag_data = NULL;
2804 }
2805
2806 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002807 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002808 }
2809
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002810 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002811 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002812 st->frag_data = NULL;
2813 }
2814
David S. Miller21dc3302010-08-23 00:13:46 -07002815 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002816 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002817 st->frag_idx = 0;
2818 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002819 } else if (st->cur_skb->next) {
2820 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002821 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002822 goto next_skb;
2823 }
2824
2825 return 0;
2826}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002827EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002828
2829/**
2830 * skb_abort_seq_read - Abort a sequential read of skb data
2831 * @st: state variable
2832 *
2833 * Must be called if skb_seq_read() was not called until it
2834 * returned 0.
2835 */
2836void skb_abort_seq_read(struct skb_seq_state *st)
2837{
2838 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002839 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002840}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002841EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002842
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002843#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2844
2845static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2846 struct ts_config *conf,
2847 struct ts_state *state)
2848{
2849 return skb_seq_read(offset, text, TS_SKB_CB(state));
2850}
2851
2852static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2853{
2854 skb_abort_seq_read(TS_SKB_CB(state));
2855}
2856
2857/**
2858 * skb_find_text - Find a text pattern in skb data
2859 * @skb: the buffer to look in
2860 * @from: search offset
2861 * @to: search limit
2862 * @config: textsearch configuration
2863 * @state: uninitialized textsearch state variable
2864 *
2865 * Finds a pattern in the skb data according to the specified
2866 * textsearch configuration. Use textsearch_next() to retrieve
2867 * subsequent occurrences of the pattern. Returns the offset
2868 * to the first occurrence or UINT_MAX if no match was found.
2869 */
2870unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2871 unsigned int to, struct ts_config *config,
2872 struct ts_state *state)
2873{
Phil Oesterf72b9482006-06-26 00:00:57 -07002874 unsigned int ret;
2875
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002876 config->get_next_block = skb_ts_get_next_block;
2877 config->finish = skb_ts_finish;
2878
2879 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2880
Phil Oesterf72b9482006-06-26 00:00:57 -07002881 ret = textsearch_find(config, state);
2882 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002883}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002884EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002885
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002886/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002887 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002888 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002889 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002890 * @getfrag: call back function to be used for getting the user data
2891 * @from: pointer to user message iov
2892 * @length: length of the iov message
2893 *
2894 * Description: This procedure append the user data in the fragment part
2895 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2896 */
2897int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002898 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002899 int len, int odd, struct sk_buff *skb),
2900 void *from, int length)
2901{
Eric Dumazetb2111722012-12-28 06:06:37 +00002902 int frg_cnt = skb_shinfo(skb)->nr_frags;
2903 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002904 int offset = 0;
2905 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002906 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002907
2908 do {
2909 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002910 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002911 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002912
Eric Dumazetb2111722012-12-28 06:06:37 +00002913 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002914 return -ENOMEM;
2915
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002916 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002917 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002918
Eric Dumazetb2111722012-12-28 06:06:37 +00002919 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2920 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002921 if (ret < 0)
2922 return -EFAULT;
2923
2924 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002925 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2926 copy);
2927 frg_cnt++;
2928 pfrag->offset += copy;
2929 get_page(pfrag->page);
2930
2931 skb->truesize += copy;
2932 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002933 skb->len += copy;
2934 skb->data_len += copy;
2935 offset += copy;
2936 length -= copy;
2937
2938 } while (length > 0);
2939
2940 return 0;
2941}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002942EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002943
Herbert Xucbb042f2006-03-20 22:43:56 -08002944/**
2945 * skb_pull_rcsum - pull skb and update receive checksum
2946 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002947 * @len: length of data pulled
2948 *
2949 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002950 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002951 * receive path processing instead of skb_pull unless you know
2952 * that the checksum difference is zero (e.g., a valid IP header)
2953 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002954 */
2955unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2956{
2957 BUG_ON(len > skb->len);
2958 skb->len -= len;
2959 BUG_ON(skb->len < skb->data_len);
2960 skb_postpull_rcsum(skb, skb->data, len);
2961 return skb->data += len;
2962}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002963EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2964
Herbert Xuf4c50d92006-06-22 03:02:40 -07002965/**
2966 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002967 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002968 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002969 *
2970 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002971 * a pointer to the first in a list of new skbs for the segments.
2972 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002973 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002974struct sk_buff *skb_segment(struct sk_buff *head_skb,
2975 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002976{
2977 struct sk_buff *segs = NULL;
2978 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002979 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002980 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2981 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2982 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002983 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002984 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002985 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002986 unsigned int headroom;
2987 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002988 __be16 proto;
2989 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002990 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002991 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002992 int err = -ENOMEM;
2993 int i = 0;
2994 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002995 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002996
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002997 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002998 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002999 if (unlikely(!proto))
3000 return ERR_PTR(-EINVAL);
3001
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003002 csum = !head_skb->encap_hdr_csum &&
3003 !!can_checksum_protocol(features, proto);
3004
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003005 headroom = skb_headroom(head_skb);
3006 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003007
3008 do {
3009 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003010 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003011 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003012 int size;
3013
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003014 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003015 if (len > mss)
3016 len = mss;
3017
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003018 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003019 if (hsize < 0)
3020 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003021 if (hsize > len || !sg)
3022 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003023
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003024 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3025 (skb_headlen(list_skb) == len || sg)) {
3026 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003027
Herbert Xu9d8506c2013-11-21 11:10:04 -08003028 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003029 nfrags = skb_shinfo(list_skb)->nr_frags;
3030 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003031 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003032 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003033
3034 while (pos < offset + len) {
3035 BUG_ON(i >= nfrags);
3036
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003037 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003038 if (pos + size > offset + len)
3039 break;
3040
3041 i++;
3042 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003043 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003044 }
3045
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003046 nskb = skb_clone(list_skb, GFP_ATOMIC);
3047 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003048
3049 if (unlikely(!nskb))
3050 goto err;
3051
Herbert Xu9d8506c2013-11-21 11:10:04 -08003052 if (unlikely(pskb_trim(nskb, len))) {
3053 kfree_skb(nskb);
3054 goto err;
3055 }
3056
Alexander Duyckec47ea82012-05-04 14:26:56 +00003057 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003058 if (skb_cow_head(nskb, doffset + headroom)) {
3059 kfree_skb(nskb);
3060 goto err;
3061 }
3062
Alexander Duyckec47ea82012-05-04 14:26:56 +00003063 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003064 skb_release_head_state(nskb);
3065 __skb_push(nskb, doffset);
3066 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003067 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003068 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003069 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003070
3071 if (unlikely(!nskb))
3072 goto err;
3073
3074 skb_reserve(nskb, headroom);
3075 __skb_put(nskb, doffset);
3076 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003077
3078 if (segs)
3079 tail->next = nskb;
3080 else
3081 segs = nskb;
3082 tail = nskb;
3083
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003084 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003085
Eric Dumazet030737b2013-10-19 11:42:54 -07003086 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003087 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003088
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003089 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003090 nskb->data - tnl_hlen,
3091 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003092
Herbert Xu9d8506c2013-11-21 11:10:04 -08003093 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003094 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003095
Tom Herberte585f232014-11-04 09:06:54 -08003096 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003097 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003098 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003099 skb_put(nskb, len),
3100 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003101 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003102 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003103 continue;
3104 }
3105
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003106 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003107
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003108 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003109 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003110
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003111 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3112 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003113
Herbert Xu9d8506c2013-11-21 11:10:04 -08003114 while (pos < offset + len) {
3115 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003116 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003117
3118 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003119 nfrags = skb_shinfo(list_skb)->nr_frags;
3120 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003121 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003122
3123 BUG_ON(!nfrags);
3124
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003125 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003126 }
3127
3128 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3129 MAX_SKB_FRAGS)) {
3130 net_warn_ratelimited(
3131 "skb_segment: too many frags: %u %u\n",
3132 pos, mss);
3133 goto err;
3134 }
3135
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003136 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3137 goto err;
3138
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003139 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003140 __skb_frag_ref(nskb_frag);
3141 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003142
3143 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003144 nskb_frag->page_offset += offset - pos;
3145 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003146 }
3147
Herbert Xu89319d382008-12-15 23:26:06 -08003148 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003149
3150 if (pos + size <= offset + len) {
3151 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003152 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003153 pos += size;
3154 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003155 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003156 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003157 }
3158
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003159 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003160 }
3161
Herbert Xu89319d382008-12-15 23:26:06 -08003162skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003163 nskb->data_len = len - hsize;
3164 nskb->len += nskb->data_len;
3165 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003166
Simon Horman1cdbcb72013-05-19 15:46:49 +00003167perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003168 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003169 nskb->csum = skb_checksum(nskb, doffset,
3170 nskb->len - doffset, 0);
3171 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003172 SKB_GSO_CB(nskb)->csum_start =
3173 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003174 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003175 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003176
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003177 /* Some callers want to get the end of the list.
3178 * Put it in segs->prev to avoid walking the list.
3179 * (see validate_xmit_skb_list() for example)
3180 */
3181 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003182
3183 /* Following permits correct backpressure, for protocols
3184 * using skb_set_owner_w().
3185 * Idea is to tranfert ownership from head_skb to last segment.
3186 */
3187 if (head_skb->destructor == sock_wfree) {
3188 swap(tail->truesize, head_skb->truesize);
3189 swap(tail->destructor, head_skb->destructor);
3190 swap(tail->sk, head_skb->sk);
3191 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003192 return segs;
3193
3194err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003195 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003196 return ERR_PTR(err);
3197}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003198EXPORT_SYMBOL_GPL(skb_segment);
3199
Herbert Xu71d93b32008-12-15 23:42:33 -08003200int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3201{
Eric Dumazet8a291112013-10-08 09:02:23 -07003202 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003203 unsigned int offset = skb_gro_offset(skb);
3204 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003205 struct sk_buff *nskb, *lp, *p = *head;
3206 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003207 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003208 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003209
Eric Dumazet8a291112013-10-08 09:02:23 -07003210 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003211 return -E2BIG;
3212
Eric Dumazet29e98242014-05-16 11:34:37 -07003213 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003214 pinfo = skb_shinfo(lp);
3215
3216 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003217 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003218 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003219 int i = skbinfo->nr_frags;
3220 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003221
Herbert Xu66e92fc2009-05-26 18:50:32 +00003222 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003223 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003224
Eric Dumazet8a291112013-10-08 09:02:23 -07003225 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003226 pinfo->nr_frags = nr_frags;
3227 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003228
Herbert Xu9aaa1562009-05-26 18:50:33 +00003229 frag = pinfo->frags + nr_frags;
3230 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003231 do {
3232 *--frag = *--frag2;
3233 } while (--i);
3234
3235 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003236 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003237
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003238 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003239 delta_truesize = skb->truesize -
3240 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003241
Herbert Xuf5572062009-01-14 20:40:03 -08003242 skb->truesize -= skb->data_len;
3243 skb->len -= skb->data_len;
3244 skb->data_len = 0;
3245
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003246 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003247 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003248 } else if (skb->head_frag) {
3249 int nr_frags = pinfo->nr_frags;
3250 skb_frag_t *frag = pinfo->frags + nr_frags;
3251 struct page *page = virt_to_head_page(skb->head);
3252 unsigned int first_size = headlen - offset;
3253 unsigned int first_offset;
3254
3255 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003256 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003257
3258 first_offset = skb->data -
3259 (unsigned char *)page_address(page) +
3260 offset;
3261
3262 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3263
3264 frag->page.p = page;
3265 frag->page_offset = first_offset;
3266 skb_frag_size_set(frag, first_size);
3267
3268 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3269 /* We dont need to clear skbinfo->nr_frags here */
3270
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003271 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003272 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3273 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003274 }
Eric Dumazet73d3fe62014-09-29 10:34:29 -07003275 /* switch back to head shinfo */
3276 pinfo = skb_shinfo(p);
3277
Eric Dumazet8a291112013-10-08 09:02:23 -07003278 if (pinfo->frag_list)
3279 goto merge;
3280 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003281 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003282
3283 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003284 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003285 if (unlikely(!nskb))
3286 return -ENOMEM;
3287
3288 __copy_skb_header(nskb, p);
3289 nskb->mac_len = p->mac_len;
3290
3291 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003292 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003293
Herbert Xu86911732009-01-29 14:19:50 +00003294 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003295 skb_set_network_header(nskb, skb_network_offset(p));
3296 skb_set_transport_header(nskb, skb_transport_offset(p));
3297
Herbert Xu86911732009-01-29 14:19:50 +00003298 __skb_pull(p, skb_gro_offset(p));
3299 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3300 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003301
Herbert Xu71d93b32008-12-15 23:42:33 -08003302 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003303 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003304 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003305 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003306 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003307
3308 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003309 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003310 nskb->len += p->len;
3311
3312 *head = nskb;
3313 nskb->next = p->next;
3314 p->next = NULL;
3315
3316 p = nskb;
3317
3318merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003319 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003320 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003321 unsigned int eat = offset - headlen;
3322
3323 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003324 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003325 skb->data_len -= eat;
3326 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003327 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003328 }
3329
Herbert Xu67147ba2009-05-26 18:50:22 +00003330 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003331
Eric Dumazet29e98242014-05-16 11:34:37 -07003332 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003333 skb_shinfo(p)->frag_list = skb;
3334 else
3335 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003336 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003337 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003338 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003339
Herbert Xu5d38a072009-01-04 16:13:40 -08003340done:
3341 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003342 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003343 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003344 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003345 if (lp != p) {
3346 lp->data_len += len;
3347 lp->truesize += delta_truesize;
3348 lp->len += len;
3349 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003350 NAPI_GRO_CB(skb)->same_flow = 1;
3351 return 0;
3352}
Herbert Xu71d93b32008-12-15 23:42:33 -08003353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354void __init skb_init(void)
3355{
3356 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3357 sizeof(struct sk_buff),
3358 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003359 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003360 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003361 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003362 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003363 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003364 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003365 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
David Howells716ea3a2007-04-02 20:19:53 -07003368/**
3369 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3370 * @skb: Socket buffer containing the buffers to be mapped
3371 * @sg: The scatter-gather list to map into
3372 * @offset: The offset into the buffer's contents to start mapping
3373 * @len: Length of buffer space to be mapped
3374 *
3375 * Fill the specified scatter-gather list with mappings/pointers into a
3376 * region of the buffer space attached to a socket buffer.
3377 */
David S. Miller51c739d2007-10-30 21:29:29 -07003378static int
3379__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003380{
David S. Miller1a028e52007-04-27 15:21:23 -07003381 int start = skb_headlen(skb);
3382 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003383 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003384 int elt = 0;
3385
3386 if (copy > 0) {
3387 if (copy > len)
3388 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003389 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003390 elt++;
3391 if ((len -= copy) == 0)
3392 return elt;
3393 offset += copy;
3394 }
3395
3396 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003397 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003398
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003399 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003400
Eric Dumazet9e903e02011-10-18 21:00:24 +00003401 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003402 if ((copy = end - offset) > 0) {
3403 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3404
3405 if (copy > len)
3406 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003407 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003408 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003409 elt++;
3410 if (!(len -= copy))
3411 return elt;
3412 offset += copy;
3413 }
David S. Miller1a028e52007-04-27 15:21:23 -07003414 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003415 }
3416
David S. Millerfbb398a2009-06-09 00:18:59 -07003417 skb_walk_frags(skb, frag_iter) {
3418 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003419
David S. Millerfbb398a2009-06-09 00:18:59 -07003420 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003421
David S. Millerfbb398a2009-06-09 00:18:59 -07003422 end = start + frag_iter->len;
3423 if ((copy = end - offset) > 0) {
3424 if (copy > len)
3425 copy = len;
3426 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3427 copy);
3428 if ((len -= copy) == 0)
3429 return elt;
3430 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003431 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003432 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003433 }
3434 BUG_ON(len);
3435 return elt;
3436}
3437
Fan Du25a91d82014-01-18 09:54:23 +08003438/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3439 * sglist without mark the sg which contain last skb data as the end.
3440 * So the caller can mannipulate sg list as will when padding new data after
3441 * the first call without calling sg_unmark_end to expend sg list.
3442 *
3443 * Scenario to use skb_to_sgvec_nomark:
3444 * 1. sg_init_table
3445 * 2. skb_to_sgvec_nomark(payload1)
3446 * 3. skb_to_sgvec_nomark(payload2)
3447 *
3448 * This is equivalent to:
3449 * 1. sg_init_table
3450 * 2. skb_to_sgvec(payload1)
3451 * 3. sg_unmark_end
3452 * 4. skb_to_sgvec(payload2)
3453 *
3454 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3455 * is more preferable.
3456 */
3457int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3458 int offset, int len)
3459{
3460 return __skb_to_sgvec(skb, sg, offset, len);
3461}
3462EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3463
David S. Miller51c739d2007-10-30 21:29:29 -07003464int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3465{
3466 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3467
Jens Axboec46f2332007-10-31 12:06:37 +01003468 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003469
3470 return nsg;
3471}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003472EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003473
David Howells716ea3a2007-04-02 20:19:53 -07003474/**
3475 * skb_cow_data - Check that a socket buffer's data buffers are writable
3476 * @skb: The socket buffer to check.
3477 * @tailbits: Amount of trailing space to be added
3478 * @trailer: Returned pointer to the skb where the @tailbits space begins
3479 *
3480 * Make sure that the data buffers attached to a socket buffer are
3481 * writable. If they are not, private copies are made of the data buffers
3482 * and the socket buffer is set to use these instead.
3483 *
3484 * If @tailbits is given, make sure that there is space to write @tailbits
3485 * bytes of data beyond current end of socket buffer. @trailer will be
3486 * set to point to the skb in which this space begins.
3487 *
3488 * The number of scatterlist elements required to completely map the
3489 * COW'd and extended socket buffer will be returned.
3490 */
3491int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3492{
3493 int copyflag;
3494 int elt;
3495 struct sk_buff *skb1, **skb_p;
3496
3497 /* If skb is cloned or its head is paged, reallocate
3498 * head pulling out all the pages (pages are considered not writable
3499 * at the moment even if they are anonymous).
3500 */
3501 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3502 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3503 return -ENOMEM;
3504
3505 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003506 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003507 /* A little of trouble, not enough of space for trailer.
3508 * This should not happen, when stack is tuned to generate
3509 * good frames. OK, on miss we reallocate and reserve even more
3510 * space, 128 bytes is fair. */
3511
3512 if (skb_tailroom(skb) < tailbits &&
3513 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3514 return -ENOMEM;
3515
3516 /* Voila! */
3517 *trailer = skb;
3518 return 1;
3519 }
3520
3521 /* Misery. We are in troubles, going to mincer fragments... */
3522
3523 elt = 1;
3524 skb_p = &skb_shinfo(skb)->frag_list;
3525 copyflag = 0;
3526
3527 while ((skb1 = *skb_p) != NULL) {
3528 int ntail = 0;
3529
3530 /* The fragment is partially pulled by someone,
3531 * this can happen on input. Copy it and everything
3532 * after it. */
3533
3534 if (skb_shared(skb1))
3535 copyflag = 1;
3536
3537 /* If the skb is the last, worry about trailer. */
3538
3539 if (skb1->next == NULL && tailbits) {
3540 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003541 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003542 skb_tailroom(skb1) < tailbits)
3543 ntail = tailbits + 128;
3544 }
3545
3546 if (copyflag ||
3547 skb_cloned(skb1) ||
3548 ntail ||
3549 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003550 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003551 struct sk_buff *skb2;
3552
3553 /* Fuck, we are miserable poor guys... */
3554 if (ntail == 0)
3555 skb2 = skb_copy(skb1, GFP_ATOMIC);
3556 else
3557 skb2 = skb_copy_expand(skb1,
3558 skb_headroom(skb1),
3559 ntail,
3560 GFP_ATOMIC);
3561 if (unlikely(skb2 == NULL))
3562 return -ENOMEM;
3563
3564 if (skb1->sk)
3565 skb_set_owner_w(skb2, skb1->sk);
3566
3567 /* Looking around. Are we still alive?
3568 * OK, link new skb, drop old one */
3569
3570 skb2->next = skb1->next;
3571 *skb_p = skb2;
3572 kfree_skb(skb1);
3573 skb1 = skb2;
3574 }
3575 elt++;
3576 *trailer = skb1;
3577 skb_p = &skb1->next;
3578 }
3579
3580 return elt;
3581}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003582EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003583
Eric Dumazetb1faf562010-05-31 23:44:05 -07003584static void sock_rmem_free(struct sk_buff *skb)
3585{
3586 struct sock *sk = skb->sk;
3587
3588 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3589}
3590
3591/*
3592 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3593 */
3594int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3595{
3596 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003597 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003598 return -ENOMEM;
3599
3600 skb_orphan(skb);
3601 skb->sk = sk;
3602 skb->destructor = sock_rmem_free;
3603 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3604
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003605 /* before exiting rcu section, make sure dst is refcounted */
3606 skb_dst_force(skb);
3607
Eric Dumazetb1faf562010-05-31 23:44:05 -07003608 skb_queue_tail(&sk->sk_error_queue, skb);
3609 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003610 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003611 return 0;
3612}
3613EXPORT_SYMBOL(sock_queue_err_skb);
3614
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003615struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3616{
3617 struct sk_buff_head *q = &sk->sk_error_queue;
3618 struct sk_buff *skb, *skb_next;
3619 int err = 0;
3620
3621 spin_lock_bh(&q->lock);
3622 skb = __skb_dequeue(q);
3623 if (skb && (skb_next = skb_peek(q)))
3624 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3625 spin_unlock_bh(&q->lock);
3626
3627 sk->sk_err = err;
3628 if (err)
3629 sk->sk_error_report(sk);
3630
3631 return skb;
3632}
3633EXPORT_SYMBOL(sock_dequeue_err_skb);
3634
Alexander Duyckcab41c42014-09-10 18:05:26 -04003635/**
3636 * skb_clone_sk - create clone of skb, and take reference to socket
3637 * @skb: the skb to clone
3638 *
3639 * This function creates a clone of a buffer that holds a reference on
3640 * sk_refcnt. Buffers created via this function are meant to be
3641 * returned using sock_queue_err_skb, or free via kfree_skb.
3642 *
3643 * When passing buffers allocated with this function to sock_queue_err_skb
3644 * it is necessary to wrap the call with sock_hold/sock_put in order to
3645 * prevent the socket from being released prior to being enqueued on
3646 * the sk_error_queue.
3647 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003648struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3649{
3650 struct sock *sk = skb->sk;
3651 struct sk_buff *clone;
3652
3653 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3654 return NULL;
3655
3656 clone = skb_clone(skb, GFP_ATOMIC);
3657 if (!clone) {
3658 sock_put(sk);
3659 return NULL;
3660 }
3661
3662 clone->sk = sk;
3663 clone->destructor = sock_efree;
3664
3665 return clone;
3666}
3667EXPORT_SYMBOL(skb_clone_sk);
3668
Alexander Duyck37846ef2014-09-04 13:31:10 -04003669static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3670 struct sock *sk,
3671 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003672{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003673 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003674 int err;
3675
Patrick Ohlyac45f602009-02-12 05:03:37 +00003676 serr = SKB_EXT_ERR(skb);
3677 memset(serr, 0, sizeof(*serr));
3678 serr->ee.ee_errno = ENOMSG;
3679 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003680 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003681 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003682 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003683 if (sk->sk_protocol == IPPROTO_TCP)
3684 serr->ee.ee_data -= sk->sk_tskey;
3685 }
Eric Dumazet29030372010-05-29 00:20:48 -07003686
Patrick Ohlyac45f602009-02-12 05:03:37 +00003687 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003688
Patrick Ohlyac45f602009-02-12 05:03:37 +00003689 if (err)
3690 kfree_skb(skb);
3691}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003692
3693void skb_complete_tx_timestamp(struct sk_buff *skb,
3694 struct skb_shared_hwtstamps *hwtstamps)
3695{
3696 struct sock *sk = skb->sk;
3697
Alexander Duyck62bccb82014-09-04 13:31:35 -04003698 /* take a reference to prevent skb_orphan() from freeing the socket */
3699 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003700
Alexander Duyck62bccb82014-09-04 13:31:35 -04003701 *skb_hwtstamps(skb) = *hwtstamps;
3702 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003703
3704 sock_put(sk);
3705}
3706EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3707
3708void __skb_tstamp_tx(struct sk_buff *orig_skb,
3709 struct skb_shared_hwtstamps *hwtstamps,
3710 struct sock *sk, int tstype)
3711{
3712 struct sk_buff *skb;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003713 bool tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003714
3715 if (!sk)
3716 return;
3717
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003718 if (tsonly)
3719 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003720 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003721 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003722 if (!skb)
3723 return;
3724
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003725 if (tsonly) {
3726 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3727 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3728 }
3729
3730 if (hwtstamps)
3731 *skb_hwtstamps(skb) = *hwtstamps;
3732 else
3733 skb->tstamp = ktime_get_real();
3734
Alexander Duyck37846ef2014-09-04 13:31:10 -04003735 __skb_complete_tx_timestamp(skb, sk, tstype);
3736}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003737EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3738
3739void skb_tstamp_tx(struct sk_buff *orig_skb,
3740 struct skb_shared_hwtstamps *hwtstamps)
3741{
3742 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3743 SCM_TSTAMP_SND);
3744}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003745EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3746
Johannes Berg6e3e9392011-11-09 10:15:42 +01003747void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3748{
3749 struct sock *sk = skb->sk;
3750 struct sock_exterr_skb *serr;
3751 int err;
3752
3753 skb->wifi_acked_valid = 1;
3754 skb->wifi_acked = acked;
3755
3756 serr = SKB_EXT_ERR(skb);
3757 memset(serr, 0, sizeof(*serr));
3758 serr->ee.ee_errno = ENOMSG;
3759 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3760
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003761 /* take a reference to prevent skb_orphan() from freeing the socket */
3762 sock_hold(sk);
3763
Johannes Berg6e3e9392011-11-09 10:15:42 +01003764 err = sock_queue_err_skb(sk, skb);
3765 if (err)
3766 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003767
3768 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003769}
3770EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3771
Patrick Ohlyac45f602009-02-12 05:03:37 +00003772
Rusty Russellf35d9d82008-02-04 23:49:54 -05003773/**
3774 * skb_partial_csum_set - set up and verify partial csum values for packet
3775 * @skb: the skb to set
3776 * @start: the number of bytes after skb->data to start checksumming.
3777 * @off: the offset from start to place the checksum.
3778 *
3779 * For untrusted partially-checksummed packets, we need to make sure the values
3780 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3781 *
3782 * This function checks and sets those values and skb->ip_summed: if this
3783 * returns false you should drop the packet.
3784 */
3785bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3786{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003787 if (unlikely(start > skb_headlen(skb)) ||
3788 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003789 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3790 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003791 return false;
3792 }
3793 skb->ip_summed = CHECKSUM_PARTIAL;
3794 skb->csum_start = skb_headroom(skb) + start;
3795 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003796 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003797 return true;
3798}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003799EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003800
Paul Durranted1f50c2014-01-09 10:02:46 +00003801static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3802 unsigned int max)
3803{
3804 if (skb_headlen(skb) >= len)
3805 return 0;
3806
3807 /* If we need to pullup then pullup to the max, so we
3808 * won't need to do it again.
3809 */
3810 if (max > skb->len)
3811 max = skb->len;
3812
3813 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3814 return -ENOMEM;
3815
3816 if (skb_headlen(skb) < len)
3817 return -EPROTO;
3818
3819 return 0;
3820}
3821
Jan Beulichf9708b42014-03-11 13:56:05 +00003822#define MAX_TCP_HDR_LEN (15 * 4)
3823
3824static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3825 typeof(IPPROTO_IP) proto,
3826 unsigned int off)
3827{
3828 switch (proto) {
3829 int err;
3830
3831 case IPPROTO_TCP:
3832 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3833 off + MAX_TCP_HDR_LEN);
3834 if (!err && !skb_partial_csum_set(skb, off,
3835 offsetof(struct tcphdr,
3836 check)))
3837 err = -EPROTO;
3838 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3839
3840 case IPPROTO_UDP:
3841 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3842 off + sizeof(struct udphdr));
3843 if (!err && !skb_partial_csum_set(skb, off,
3844 offsetof(struct udphdr,
3845 check)))
3846 err = -EPROTO;
3847 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3848 }
3849
3850 return ERR_PTR(-EPROTO);
3851}
3852
Paul Durranted1f50c2014-01-09 10:02:46 +00003853/* This value should be large enough to cover a tagged ethernet header plus
3854 * maximally sized IP and TCP or UDP headers.
3855 */
3856#define MAX_IP_HDR_LEN 128
3857
Jan Beulichf9708b42014-03-11 13:56:05 +00003858static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003859{
3860 unsigned int off;
3861 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003862 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003863 int err;
3864
3865 fragment = false;
3866
3867 err = skb_maybe_pull_tail(skb,
3868 sizeof(struct iphdr),
3869 MAX_IP_HDR_LEN);
3870 if (err < 0)
3871 goto out;
3872
3873 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3874 fragment = true;
3875
3876 off = ip_hdrlen(skb);
3877
3878 err = -EPROTO;
3879
3880 if (fragment)
3881 goto out;
3882
Jan Beulichf9708b42014-03-11 13:56:05 +00003883 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3884 if (IS_ERR(csum))
3885 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003886
Jan Beulichf9708b42014-03-11 13:56:05 +00003887 if (recalculate)
3888 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3889 ip_hdr(skb)->daddr,
3890 skb->len - off,
3891 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003892 err = 0;
3893
3894out:
3895 return err;
3896}
3897
3898/* This value should be large enough to cover a tagged ethernet header plus
3899 * an IPv6 header, all options, and a maximal TCP or UDP header.
3900 */
3901#define MAX_IPV6_HDR_LEN 256
3902
3903#define OPT_HDR(type, skb, off) \
3904 (type *)(skb_network_header(skb) + (off))
3905
3906static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3907{
3908 int err;
3909 u8 nexthdr;
3910 unsigned int off;
3911 unsigned int len;
3912 bool fragment;
3913 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003914 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003915
3916 fragment = false;
3917 done = false;
3918
3919 off = sizeof(struct ipv6hdr);
3920
3921 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3922 if (err < 0)
3923 goto out;
3924
3925 nexthdr = ipv6_hdr(skb)->nexthdr;
3926
3927 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3928 while (off <= len && !done) {
3929 switch (nexthdr) {
3930 case IPPROTO_DSTOPTS:
3931 case IPPROTO_HOPOPTS:
3932 case IPPROTO_ROUTING: {
3933 struct ipv6_opt_hdr *hp;
3934
3935 err = skb_maybe_pull_tail(skb,
3936 off +
3937 sizeof(struct ipv6_opt_hdr),
3938 MAX_IPV6_HDR_LEN);
3939 if (err < 0)
3940 goto out;
3941
3942 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3943 nexthdr = hp->nexthdr;
3944 off += ipv6_optlen(hp);
3945 break;
3946 }
3947 case IPPROTO_AH: {
3948 struct ip_auth_hdr *hp;
3949
3950 err = skb_maybe_pull_tail(skb,
3951 off +
3952 sizeof(struct ip_auth_hdr),
3953 MAX_IPV6_HDR_LEN);
3954 if (err < 0)
3955 goto out;
3956
3957 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3958 nexthdr = hp->nexthdr;
3959 off += ipv6_authlen(hp);
3960 break;
3961 }
3962 case IPPROTO_FRAGMENT: {
3963 struct frag_hdr *hp;
3964
3965 err = skb_maybe_pull_tail(skb,
3966 off +
3967 sizeof(struct frag_hdr),
3968 MAX_IPV6_HDR_LEN);
3969 if (err < 0)
3970 goto out;
3971
3972 hp = OPT_HDR(struct frag_hdr, skb, off);
3973
3974 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3975 fragment = true;
3976
3977 nexthdr = hp->nexthdr;
3978 off += sizeof(struct frag_hdr);
3979 break;
3980 }
3981 default:
3982 done = true;
3983 break;
3984 }
3985 }
3986
3987 err = -EPROTO;
3988
3989 if (!done || fragment)
3990 goto out;
3991
Jan Beulichf9708b42014-03-11 13:56:05 +00003992 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3993 if (IS_ERR(csum))
3994 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003995
Jan Beulichf9708b42014-03-11 13:56:05 +00003996 if (recalculate)
3997 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3998 &ipv6_hdr(skb)->daddr,
3999 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004000 err = 0;
4001
4002out:
4003 return err;
4004}
4005
4006/**
4007 * skb_checksum_setup - set up partial checksum offset
4008 * @skb: the skb to set up
4009 * @recalculate: if true the pseudo-header checksum will be recalculated
4010 */
4011int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4012{
4013 int err;
4014
4015 switch (skb->protocol) {
4016 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004017 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004018 break;
4019
4020 case htons(ETH_P_IPV6):
4021 err = skb_checksum_setup_ipv6(skb, recalculate);
4022 break;
4023
4024 default:
4025 err = -EPROTO;
4026 break;
4027 }
4028
4029 return err;
4030}
4031EXPORT_SYMBOL(skb_checksum_setup);
4032
Ben Hutchings4497b072008-06-19 16:22:28 -07004033void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4034{
Joe Perchese87cc472012-05-13 21:56:26 +00004035 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4036 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004037}
Ben Hutchings4497b072008-06-19 16:22:28 -07004038EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004039
4040void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4041{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004042 if (head_stolen) {
4043 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004044 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004045 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004046 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004047 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004048}
4049EXPORT_SYMBOL(kfree_skb_partial);
4050
4051/**
4052 * skb_try_coalesce - try to merge skb to prior one
4053 * @to: prior buffer
4054 * @from: buffer to add
4055 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004056 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004057 */
4058bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4059 bool *fragstolen, int *delta_truesize)
4060{
4061 int i, delta, len = from->len;
4062
4063 *fragstolen = false;
4064
4065 if (skb_cloned(to))
4066 return false;
4067
4068 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004069 if (len)
4070 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004071 *delta_truesize = 0;
4072 return true;
4073 }
4074
4075 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4076 return false;
4077
4078 if (skb_headlen(from) != 0) {
4079 struct page *page;
4080 unsigned int offset;
4081
4082 if (skb_shinfo(to)->nr_frags +
4083 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4084 return false;
4085
4086 if (skb_head_is_locked(from))
4087 return false;
4088
4089 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4090
4091 page = virt_to_head_page(from->head);
4092 offset = from->data - (unsigned char *)page_address(page);
4093
4094 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4095 page, offset, skb_headlen(from));
4096 *fragstolen = true;
4097 } else {
4098 if (skb_shinfo(to)->nr_frags +
4099 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4100 return false;
4101
Weiping Panf4b549a2012-09-28 20:15:30 +00004102 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004103 }
4104
4105 WARN_ON_ONCE(delta < len);
4106
4107 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4108 skb_shinfo(from)->frags,
4109 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4110 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4111
4112 if (!skb_cloned(from))
4113 skb_shinfo(from)->nr_frags = 0;
4114
Li RongQing8ea853f2012-09-18 16:53:21 +00004115 /* if the skb is not cloned this does nothing
4116 * since we set nr_frags to 0.
4117 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004118 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4119 skb_frag_ref(from, i);
4120
4121 to->truesize += delta;
4122 to->len += len;
4123 to->data_len += len;
4124
4125 *delta_truesize = delta;
4126 return true;
4127}
4128EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004129
4130/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004131 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004132 *
4133 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004134 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004135 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004136 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4137 * into/from a tunnel. Some information have to be cleared during these
4138 * operations.
4139 * skb_scrub_packet can also be used to clean a skb before injecting it in
4140 * another namespace (@xnet == true). We have to clear all information in the
4141 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004142 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004143void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004144{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004145 if (xnet)
4146 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004147 skb->tstamp.tv64 = 0;
4148 skb->pkt_type = PACKET_HOST;
4149 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004150 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004151 skb_dst_drop(skb);
4152 skb->mark = 0;
Thomas Grafb8fb4e02014-12-23 01:13:18 +01004153 skb_init_secmark(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004154 secpath_reset(skb);
4155 nf_reset(skb);
4156 nf_reset_trace(skb);
4157}
4158EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004159
4160/**
4161 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4162 *
4163 * @skb: GSO skb
4164 *
4165 * skb_gso_transport_seglen is used to determine the real size of the
4166 * individual segments, including Layer4 headers (TCP/UDP).
4167 *
4168 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4169 */
4170unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4171{
4172 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004173 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004174
Florian Westphalf993bc22014-10-20 13:49:18 +02004175 if (skb->encapsulation) {
4176 thlen = skb_inner_transport_header(skb) -
4177 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004178
Florian Westphalf993bc22014-10-20 13:49:18 +02004179 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4180 thlen += inner_tcp_hdrlen(skb);
4181 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4182 thlen = tcp_hdrlen(skb);
4183 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004184 /* UFO sets gso_size to the size of the fragmentation
4185 * payload, i.e. the size of the L4 (UDP) header is already
4186 * accounted for.
4187 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004188 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004189}
4190EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004191
4192static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4193{
4194 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4195 kfree_skb(skb);
4196 return NULL;
4197 }
4198
4199 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4200 skb->mac_header += VLAN_HLEN;
4201 return skb;
4202}
4203
4204struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4205{
4206 struct vlan_hdr *vhdr;
4207 u16 vlan_tci;
4208
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004209 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004210 /* vlan_tci is already set-up so leave this for another time */
4211 return skb;
4212 }
4213
4214 skb = skb_share_check(skb, GFP_ATOMIC);
4215 if (unlikely(!skb))
4216 goto err_free;
4217
4218 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4219 goto err_free;
4220
4221 vhdr = (struct vlan_hdr *)skb->data;
4222 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4223 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4224
4225 skb_pull_rcsum(skb, VLAN_HLEN);
4226 vlan_set_encap_proto(skb, vhdr);
4227
4228 skb = skb_reorder_vlan_header(skb);
4229 if (unlikely(!skb))
4230 goto err_free;
4231
4232 skb_reset_network_header(skb);
4233 skb_reset_transport_header(skb);
4234 skb_reset_mac_len(skb);
4235
4236 return skb;
4237
4238err_free:
4239 kfree_skb(skb);
4240 return NULL;
4241}
4242EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004243
Jiri Pirkoe2195122014-11-19 14:05:01 +01004244int skb_ensure_writable(struct sk_buff *skb, int write_len)
4245{
4246 if (!pskb_may_pull(skb, write_len))
4247 return -ENOMEM;
4248
4249 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4250 return 0;
4251
4252 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4253}
4254EXPORT_SYMBOL(skb_ensure_writable);
4255
Jiri Pirko93515d52014-11-19 14:05:02 +01004256/* remove VLAN header from packet and update csum accordingly. */
4257static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4258{
4259 struct vlan_hdr *vhdr;
4260 unsigned int offset = skb->data - skb_mac_header(skb);
4261 int err;
4262
4263 __skb_push(skb, offset);
4264 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4265 if (unlikely(err))
4266 goto pull;
4267
4268 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4269
4270 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4271 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4272
4273 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4274 __skb_pull(skb, VLAN_HLEN);
4275
4276 vlan_set_encap_proto(skb, vhdr);
4277 skb->mac_header += VLAN_HLEN;
4278
4279 if (skb_network_offset(skb) < ETH_HLEN)
4280 skb_set_network_header(skb, ETH_HLEN);
4281
4282 skb_reset_mac_len(skb);
4283pull:
4284 __skb_pull(skb, offset);
4285
4286 return err;
4287}
4288
4289int skb_vlan_pop(struct sk_buff *skb)
4290{
4291 u16 vlan_tci;
4292 __be16 vlan_proto;
4293 int err;
4294
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004295 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004296 skb->vlan_tci = 0;
4297 } else {
4298 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4299 skb->protocol != htons(ETH_P_8021AD)) ||
4300 skb->len < VLAN_ETH_HLEN))
4301 return 0;
4302
4303 err = __skb_vlan_pop(skb, &vlan_tci);
4304 if (err)
4305 return err;
4306 }
4307 /* move next vlan tag to hw accel tag */
4308 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4309 skb->protocol != htons(ETH_P_8021AD)) ||
4310 skb->len < VLAN_ETH_HLEN))
4311 return 0;
4312
4313 vlan_proto = skb->protocol;
4314 err = __skb_vlan_pop(skb, &vlan_tci);
4315 if (unlikely(err))
4316 return err;
4317
4318 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4319 return 0;
4320}
4321EXPORT_SYMBOL(skb_vlan_pop);
4322
4323int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4324{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004325 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004326 unsigned int offset = skb->data - skb_mac_header(skb);
4327 int err;
4328
4329 /* __vlan_insert_tag expect skb->data pointing to mac header.
4330 * So change skb->data before calling it and change back to
4331 * original position later
4332 */
4333 __skb_push(skb, offset);
4334 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004335 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004336 if (err)
4337 return err;
4338 skb->protocol = skb->vlan_proto;
4339 skb->mac_len += VLAN_HLEN;
4340 __skb_pull(skb, offset);
4341
4342 if (skb->ip_summed == CHECKSUM_COMPLETE)
4343 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4344 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4345 }
4346 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4347 return 0;
4348}
4349EXPORT_SYMBOL(skb_vlan_push);
4350
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004351/**
4352 * alloc_skb_with_frags - allocate skb with page frags
4353 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004354 * @header_len: size of linear part
4355 * @data_len: needed length in frags
4356 * @max_page_order: max page order desired.
4357 * @errcode: pointer to error code if any
4358 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004359 *
4360 * This can be used to allocate a paged skb, given a maximal order for frags.
4361 */
4362struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4363 unsigned long data_len,
4364 int max_page_order,
4365 int *errcode,
4366 gfp_t gfp_mask)
4367{
4368 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4369 unsigned long chunk;
4370 struct sk_buff *skb;
4371 struct page *page;
4372 gfp_t gfp_head;
4373 int i;
4374
4375 *errcode = -EMSGSIZE;
4376 /* Note this test could be relaxed, if we succeed to allocate
4377 * high order pages...
4378 */
4379 if (npages > MAX_SKB_FRAGS)
4380 return NULL;
4381
4382 gfp_head = gfp_mask;
4383 if (gfp_head & __GFP_WAIT)
4384 gfp_head |= __GFP_REPEAT;
4385
4386 *errcode = -ENOBUFS;
4387 skb = alloc_skb(header_len, gfp_head);
4388 if (!skb)
4389 return NULL;
4390
4391 skb->truesize += npages << PAGE_SHIFT;
4392
4393 for (i = 0; npages > 0; i++) {
4394 int order = max_page_order;
4395
4396 while (order) {
4397 if (npages >= 1 << order) {
4398 page = alloc_pages(gfp_mask |
4399 __GFP_COMP |
4400 __GFP_NOWARN |
4401 __GFP_NORETRY,
4402 order);
4403 if (page)
4404 goto fill_page;
4405 /* Do not retry other high order allocations */
4406 order = 1;
4407 max_page_order = 0;
4408 }
4409 order--;
4410 }
4411 page = alloc_page(gfp_mask);
4412 if (!page)
4413 goto failure;
4414fill_page:
4415 chunk = min_t(unsigned long, data_len,
4416 PAGE_SIZE << order);
4417 skb_fill_page_desc(skb, i, page, 0, chunk);
4418 data_len -= chunk;
4419 npages -= 1 << order;
4420 }
4421 return skb;
4422
4423failure:
4424 kfree_skb(skb);
4425 return NULL;
4426}
4427EXPORT_SYMBOL(alloc_skb_with_frags);